devirtualize.ll 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ; RUN: opt -S -Os < %s | FileCheck %s
  2. target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
  3. target triple = "x86_64-apple-darwin10.0.0"
  4. ; Simple devirt testcase, requires iteration between inliner and GVN.
  5. ; rdar://6295824
  6. define i32 @foo(i32 ()** noalias %p, i64* noalias %q) nounwind ssp {
  7. entry:
  8. store i32 ()* @bar, i32 ()** %p
  9. store i64 0, i64* %q
  10. %tmp3 = load i32 ()*, i32 ()** %p ; <i32 ()*> [#uses=1]
  11. %call = call i32 %tmp3() ; <i32> [#uses=1]
  12. %X = add i32 %call, 4
  13. ret i32 %X
  14. ; CHECK-LABEL: @foo(
  15. ; CHECK-NEXT: entry:
  16. ; CHECK-NEXT: store
  17. ; CHECK-NEXT: store
  18. ; CHECK-NEXT: ret i32 11
  19. }
  20. define internal i32 @bar() nounwind ssp {
  21. entry:
  22. ret i32 7
  23. }
  24. ;; More complex devirt case, from PR6724
  25. ; CHECK: @_Z1gv()
  26. ; CHECK-NEXT: entry:
  27. ; CHECK-NEXT: ret i32 7
  28. %0 = type { i8*, i8* }
  29. %1 = type { i8*, i8*, i32, i32, i8*, i64, i8*, i64 }
  30. %2 = type { i8*, i8*, i8* }
  31. %struct.A = type { i8** }
  32. %struct.B = type { i8** }
  33. %struct.C = type { [16 x i8] }
  34. %struct.D = type { [16 x i8] }
  35. @_ZTV1D = linkonce_odr constant [6 x i8*] [i8* null, i8* bitcast (%2* @_ZTI1D to i8*), i8* bitcast (i32 (%struct.C*)* @_ZN1D1fEv to i8*), i8* inttoptr (i64 -8 to i8*), i8* bitcast (%2* @_ZTI1D to i8*), i8* bitcast (i32 (%struct.C*)* @_ZThn8_N1D1fEv to i8*)] ; <[6 x i8*]*> [#uses=2]
  36. @_ZTVN10__cxxabiv120__si_class_type_infoE = external global i8* ; <i8**> [#uses=1]
  37. @_ZTS1D = linkonce_odr constant [3 x i8] c"1D\00" ; <[3 x i8]*> [#uses=1]
  38. @_ZTVN10__cxxabiv121__vmi_class_type_infoE = external global i8* ; <i8**> [#uses=1]
  39. @_ZTS1C = linkonce_odr constant [3 x i8] c"1C\00" ; <[3 x i8]*> [#uses=1]
  40. @_ZTVN10__cxxabiv117__class_type_infoE = external global i8* ; <i8**> [#uses=1]
  41. @_ZTS1A = linkonce_odr constant [3 x i8] c"1A\00" ; <[3 x i8]*> [#uses=1]
  42. @_ZTI1A = linkonce_odr constant %0 { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv117__class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_ZTS1A, i32 0, i32 0) } ; <%0*> [#uses=1]
  43. @_ZTS1B = linkonce_odr constant [3 x i8] c"1B\00" ; <[3 x i8]*> [#uses=1]
  44. @_ZTI1B = linkonce_odr constant %0 { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv117__class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_ZTS1B, i32 0, i32 0) } ; <%0*> [#uses=1]
  45. @_ZTI1C = linkonce_odr constant %1 { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv121__vmi_class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_ZTS1C, i32 0, i32 0), i32 0, i32 2, i8* bitcast (%0* @_ZTI1A to i8*), i64 2, i8* bitcast (%0* @_ZTI1B to i8*), i64 2050 } ; <%1*> [#uses=1]
  46. @_ZTI1D = linkonce_odr constant %2 { i8* bitcast (i8** getelementptr inbounds (i8*, i8** @_ZTVN10__cxxabiv120__si_class_type_infoE, i64 2) to i8*), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @_ZTS1D, i32 0, i32 0), i8* bitcast (%1* @_ZTI1C to i8*) } ; <%2*> [#uses=1]
  47. @_ZTV1C = linkonce_odr constant [6 x i8*] [i8* null, i8* bitcast (%1* @_ZTI1C to i8*), i8* bitcast (i32 (%struct.C*)* @_ZN1C1fEv to i8*), i8* inttoptr (i64 -8 to i8*), i8* bitcast (%1* @_ZTI1C to i8*), i8* bitcast (i32 (%struct.C*)* @_ZThn8_N1C1fEv to i8*)] ; <[6 x i8*]*> [#uses=2]
  48. @_ZTV1B = linkonce_odr constant [3 x i8*] [i8* null, i8* bitcast (%0* @_ZTI1B to i8*), i8* bitcast (i32 (%struct.A*)* @_ZN1B1fEv to i8*)] ; <[3 x i8*]*> [#uses=1]
  49. @_ZTV1A = linkonce_odr constant [3 x i8*] [i8* null, i8* bitcast (%0* @_ZTI1A to i8*), i8* bitcast (i32 (%struct.A*)* @_ZN1A1fEv to i8*)] ; <[3 x i8*]*> [#uses=1]
  50. define i32 @_Z1gv() ssp {
  51. entry:
  52. %d = alloca %struct.C, align 8 ; <%struct.C*> [#uses=2]
  53. call void @_ZN1DC1Ev(%struct.C* %d)
  54. %call = call i32 @_Z1fP1D(%struct.C* %d) ; <i32> [#uses=1]
  55. %X = add i32 %call, 3
  56. ret i32 %X
  57. }
  58. define linkonce_odr void @_ZN1DC1Ev(%struct.C* %this) inlinehint ssp align 2 {
  59. entry:
  60. call void @_ZN1DC2Ev(%struct.C* %this)
  61. ret void
  62. }
  63. define internal i32 @_Z1fP1D(%struct.C* %d) ssp {
  64. entry:
  65. %0 = icmp eq %struct.C* %d, null ; <i1> [#uses=1]
  66. br i1 %0, label %cast.end, label %cast.notnull
  67. cast.notnull: ; preds = %entry
  68. %1 = bitcast %struct.C* %d to i8* ; <i8*> [#uses=1]
  69. %add.ptr = getelementptr i8, i8* %1, i64 8 ; <i8*> [#uses=1]
  70. %2 = bitcast i8* %add.ptr to %struct.A* ; <%struct.A*> [#uses=1]
  71. br label %cast.end
  72. cast.end: ; preds = %entry, %cast.notnull
  73. %3 = phi %struct.A* [ %2, %cast.notnull ], [ null, %entry ] ; <%struct.A*> [#uses=2]
  74. %4 = bitcast %struct.A* %3 to i32 (%struct.A*)*** ; <i32 (%struct.A*)***> [#uses=1]
  75. %5 = load i32 (%struct.A*)**, i32 (%struct.A*)*** %4 ; <i32 (%struct.A*)**> [#uses=1]
  76. %vfn = getelementptr inbounds i32 (%struct.A*)*, i32 (%struct.A*)** %5, i64 0 ; <i32 (%struct.A*)**> [#uses=1]
  77. %6 = load i32 (%struct.A*)*, i32 (%struct.A*)** %vfn ; <i32 (%struct.A*)*> [#uses=1]
  78. %call = call i32 %6(%struct.A* %3) ; <i32> [#uses=1]
  79. ret i32 %call
  80. }
  81. define linkonce_odr i32 @_ZN1D1fEv(%struct.C* %this) ssp align 2 {
  82. entry:
  83. ret i32 4
  84. }
  85. define linkonce_odr i32 @_ZThn8_N1D1fEv(%struct.C* %this) {
  86. entry:
  87. %0 = bitcast %struct.C* %this to i8* ; <i8*> [#uses=1]
  88. %1 = getelementptr inbounds i8, i8* %0, i64 -8 ; <i8*> [#uses=1]
  89. %2 = bitcast i8* %1 to %struct.C* ; <%struct.C*> [#uses=1]
  90. %call = call i32 @_ZN1D1fEv(%struct.C* %2) ; <i32> [#uses=1]
  91. ret i32 %call
  92. }
  93. define linkonce_odr void @_ZN1DC2Ev(%struct.C* %this) inlinehint ssp align 2 {
  94. entry:
  95. call void @_ZN1CC2Ev(%struct.C* %this)
  96. %0 = bitcast %struct.C* %this to i8* ; <i8*> [#uses=1]
  97. %1 = getelementptr inbounds i8, i8* %0, i64 0 ; <i8*> [#uses=1]
  98. %2 = bitcast i8* %1 to i8*** ; <i8***> [#uses=1]
  99. store i8** getelementptr inbounds ([6 x i8*], [6 x i8*]* @_ZTV1D, i64 0, i64 2), i8*** %2
  100. %3 = bitcast %struct.C* %this to i8* ; <i8*> [#uses=1]
  101. %4 = getelementptr inbounds i8, i8* %3, i64 8 ; <i8*> [#uses=1]
  102. %5 = bitcast i8* %4 to i8*** ; <i8***> [#uses=1]
  103. store i8** getelementptr inbounds ([6 x i8*], [6 x i8*]* @_ZTV1D, i64 0, i64 5), i8*** %5
  104. ret void
  105. }
  106. define linkonce_odr void @_ZN1CC2Ev(%struct.C* %this) inlinehint ssp align 2 {
  107. entry:
  108. %0 = bitcast %struct.C* %this to %struct.A* ; <%struct.A*> [#uses=1]
  109. call void @_ZN1AC2Ev(%struct.A* %0)
  110. %1 = bitcast %struct.C* %this to i8* ; <i8*> [#uses=1]
  111. %2 = getelementptr inbounds i8, i8* %1, i64 8 ; <i8*> [#uses=1]
  112. %3 = bitcast i8* %2 to %struct.A* ; <%struct.A*> [#uses=1]
  113. call void @_ZN1BC2Ev(%struct.A* %3)
  114. %4 = bitcast %struct.C* %this to i8* ; <i8*> [#uses=1]
  115. %5 = getelementptr inbounds i8, i8* %4, i64 0 ; <i8*> [#uses=1]
  116. %6 = bitcast i8* %5 to i8*** ; <i8***> [#uses=1]
  117. store i8** getelementptr inbounds ([6 x i8*], [6 x i8*]* @_ZTV1C, i64 0, i64 2), i8*** %6
  118. %7 = bitcast %struct.C* %this to i8* ; <i8*> [#uses=1]
  119. %8 = getelementptr inbounds i8, i8* %7, i64 8 ; <i8*> [#uses=1]
  120. %9 = bitcast i8* %8 to i8*** ; <i8***> [#uses=1]
  121. store i8** getelementptr inbounds ([6 x i8*], [6 x i8*]* @_ZTV1C, i64 0, i64 5), i8*** %9
  122. ret void
  123. }
  124. define linkonce_odr i32 @_ZN1C1fEv(%struct.C* %this) ssp align 2 {
  125. entry:
  126. ret i32 3
  127. }
  128. define linkonce_odr i32 @_ZThn8_N1C1fEv(%struct.C* %this) {
  129. entry:
  130. %0 = bitcast %struct.C* %this to i8* ; <i8*> [#uses=1]
  131. %1 = getelementptr inbounds i8, i8* %0, i64 -8 ; <i8*> [#uses=1]
  132. %2 = bitcast i8* %1 to %struct.C* ; <%struct.C*> [#uses=1]
  133. %call = call i32 @_ZN1C1fEv(%struct.C* %2) ; <i32> [#uses=1]
  134. ret i32 %call
  135. }
  136. define linkonce_odr void @_ZN1AC2Ev(%struct.A* %this) inlinehint ssp align 2 {
  137. entry:
  138. %0 = bitcast %struct.A* %this to i8* ; <i8*> [#uses=1]
  139. %1 = getelementptr inbounds i8, i8* %0, i64 0 ; <i8*> [#uses=1]
  140. %2 = bitcast i8* %1 to i8*** ; <i8***> [#uses=1]
  141. store i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @_ZTV1A, i64 0, i64 2), i8*** %2
  142. ret void
  143. }
  144. define linkonce_odr void @_ZN1BC2Ev(%struct.A* %this) inlinehint ssp align 2 {
  145. entry:
  146. %0 = bitcast %struct.A* %this to i8* ; <i8*> [#uses=1]
  147. %1 = getelementptr inbounds i8, i8* %0, i64 0 ; <i8*> [#uses=1]
  148. %2 = bitcast i8* %1 to i8*** ; <i8***> [#uses=1]
  149. store i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @_ZTV1B, i64 0, i64 2), i8*** %2
  150. ret void
  151. }
  152. define linkonce_odr i32 @_ZN1B1fEv(%struct.A* %this) ssp align 2 {
  153. entry:
  154. ret i32 2
  155. }
  156. define linkonce_odr i32 @_ZN1A1fEv(%struct.A* %this) ssp align 2 {
  157. entry:
  158. ret i32 1
  159. }