getelementptr.ll 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. ; RUN: opt < %s -instcombine -S | FileCheck %s
  2. target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64"
  3. %intstruct = type { i32 }
  4. %pair = type { i32, i32 }
  5. %struct.B = type { double }
  6. %struct.A = type { %struct.B, i32, i32 }
  7. %struct.C = type { [7 x i8] }
  8. @Global = constant [10 x i8] c"helloworld"
  9. @Global_as1 = addrspace(1) constant [10 x i8] c"helloworld"
  10. ; Test noop elimination
  11. define i32* @test1(i32* %I) {
  12. %A = getelementptr i32, i32* %I, i64 0
  13. ret i32* %A
  14. ; CHECK-LABEL: @test1(
  15. ; CHECK: ret i32* %I
  16. }
  17. define i32 addrspace(1)* @test1_as1(i32 addrspace(1)* %I) {
  18. %A = getelementptr i32, i32 addrspace(1)* %I, i64 0
  19. ret i32 addrspace(1)* %A
  20. ; CHECK-LABEL: @test1_as1(
  21. ; CHECK: ret i32 addrspace(1)* %I
  22. }
  23. ; Test noop elimination
  24. define i32* @test2(i32* %I) {
  25. %A = getelementptr i32, i32* %I
  26. ret i32* %A
  27. ; CHECK-LABEL: @test2(
  28. ; CHECK: ret i32* %I
  29. }
  30. ; Test that two array indexing geps fold
  31. define i32* @test3(i32* %I) {
  32. %A = getelementptr i32, i32* %I, i64 17
  33. %B = getelementptr i32, i32* %A, i64 4
  34. ret i32* %B
  35. ; CHECK-LABEL: @test3(
  36. ; CHECK: getelementptr i32, i32* %I, i64 21
  37. }
  38. ; Test that two getelementptr insts fold
  39. define i32* @test4({ i32 }* %I) {
  40. %A = getelementptr { i32 }, { i32 }* %I, i64 1
  41. %B = getelementptr { i32 }, { i32 }* %A, i64 0, i32 0
  42. ret i32* %B
  43. ; CHECK-LABEL: @test4(
  44. ; CHECK: getelementptr { i32 }, { i32 }* %I, i64 1, i32 0
  45. }
  46. define void @test5(i8 %B) {
  47. ; This should be turned into a constexpr instead of being an instruction
  48. %A = getelementptr [10 x i8], [10 x i8]* @Global, i64 0, i64 4
  49. store i8 %B, i8* %A
  50. ret void
  51. ; CHECK-LABEL: @test5(
  52. ; CHECK: store i8 %B, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @Global, i64 0, i64 4)
  53. }
  54. define void @test5_as1(i8 %B) {
  55. ; This should be turned into a constexpr instead of being an instruction
  56. %A = getelementptr [10 x i8], [10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4
  57. store i8 %B, i8 addrspace(1)* %A
  58. ret void
  59. ; CHECK-LABEL: @test5_as1(
  60. ; CHECK: store i8 %B, i8 addrspace(1)* getelementptr inbounds ([10 x i8], [10 x i8] addrspace(1)* @Global_as1, i16 0, i16 4)
  61. }
  62. %as1_ptr_struct = type { i32 addrspace(1)* }
  63. %as2_ptr_struct = type { i32 addrspace(2)* }
  64. @global_as2 = addrspace(2) global i32 zeroinitializer
  65. @global_as1_as2_ptr = addrspace(1) global %as2_ptr_struct { i32 addrspace(2)* @global_as2 }
  66. ; This should be turned into a constexpr instead of being an instruction
  67. define void @test_evaluate_gep_nested_as_ptrs(i32 addrspace(2)* %B) {
  68. ; CHECK-LABEL: @test_evaluate_gep_nested_as_ptrs(
  69. ; CHECK-NEXT: store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* getelementptr inbounds (%as2_ptr_struct, %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0), align 8
  70. ; CHECK-NEXT: ret void
  71. %A = getelementptr %as2_ptr_struct, %as2_ptr_struct addrspace(1)* @global_as1_as2_ptr, i16 0, i32 0
  72. store i32 addrspace(2)* %B, i32 addrspace(2)* addrspace(1)* %A
  73. ret void
  74. }
  75. @arst = addrspace(1) global [4 x i8 addrspace(2)*] zeroinitializer
  76. define void @test_evaluate_gep_as_ptrs_array(i8 addrspace(2)* %B) {
  77. ; CHECK-LABEL: @test_evaluate_gep_as_ptrs_array(
  78. ; CHECK-NEXT: store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* getelementptr inbounds ([4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2), align 4
  79. ; CHECK-NEXT: ret void
  80. %A = getelementptr [4 x i8 addrspace(2)*], [4 x i8 addrspace(2)*] addrspace(1)* @arst, i16 0, i16 2
  81. store i8 addrspace(2)* %B, i8 addrspace(2)* addrspace(1)* %A
  82. ret void
  83. }
  84. define i32* @test7(i32* %I, i64 %C, i64 %D) {
  85. %A = getelementptr i32, i32* %I, i64 %C
  86. %B = getelementptr i32, i32* %A, i64 %D
  87. ret i32* %B
  88. ; CHECK-LABEL: @test7(
  89. ; CHECK: %A = getelementptr i32, i32* %I, i64 %C
  90. ; CHECK: %B = getelementptr i32, i32* %A, i64 %D
  91. }
  92. define i8* @test8([10 x i32]* %X) {
  93. ;; Fold into the cast.
  94. %A = getelementptr [10 x i32], [10 x i32]* %X, i64 0, i64 0
  95. %B = bitcast i32* %A to i8*
  96. ret i8* %B
  97. ; CHECK-LABEL: @test8(
  98. ; CHECK: bitcast [10 x i32]* %X to i8*
  99. }
  100. define i32 @test9() {
  101. %A = getelementptr { i32, double }, { i32, double }* null, i32 0, i32 1
  102. %B = ptrtoint double* %A to i32
  103. ret i32 %B
  104. ; CHECK-LABEL: @test9(
  105. ; CHECK: ret i32 8
  106. }
  107. define i1 @test10({ i32, i32 }* %x, { i32, i32 }* %y) {
  108. %tmp.1 = getelementptr { i32, i32 }, { i32, i32 }* %x, i32 0, i32 1
  109. %tmp.3 = getelementptr { i32, i32 }, { i32, i32 }* %y, i32 0, i32 1
  110. ;; seteq x, y
  111. %tmp.4 = icmp eq i32* %tmp.1, %tmp.3
  112. ret i1 %tmp.4
  113. ; CHECK-LABEL: @test10(
  114. ; CHECK: icmp eq { i32, i32 }* %x, %y
  115. }
  116. define i1 @test11({ i32, i32 }* %X) {
  117. %P = getelementptr { i32, i32 }, { i32, i32 }* %X, i32 0, i32 0
  118. %Q = icmp eq i32* %P, null
  119. ret i1 %Q
  120. ; CHECK-LABEL: @test11(
  121. ; CHECK: icmp eq { i32, i32 }* %X, null
  122. }
  123. ; PR4748
  124. define i32 @test12(%struct.A* %a) {
  125. entry:
  126. %g3 = getelementptr %struct.A, %struct.A* %a, i32 0, i32 1
  127. store i32 10, i32* %g3, align 4
  128. %g4 = getelementptr %struct.A, %struct.A* %a, i32 0, i32 0
  129. %new_a = bitcast %struct.B* %g4 to %struct.A*
  130. %g5 = getelementptr %struct.A, %struct.A* %new_a, i32 0, i32 1
  131. %a_a = load i32, i32* %g5, align 4
  132. ret i32 %a_a
  133. ; CHECK-LABEL: @test12(
  134. ; CHECK: getelementptr %struct.A, %struct.A* %a, i64 0, i32 1
  135. ; CHECK-NEXT: store i32 10, i32* %g3
  136. ; CHECK-NEXT: ret i32 10
  137. }
  138. ; PR2235
  139. %S = type { i32, [ 100 x i32] }
  140. define i1 @test13(i64 %X, %S* %P) {
  141. %A = getelementptr inbounds %S, %S* %P, i32 0, i32 1, i64 %X
  142. %B = getelementptr inbounds %S, %S* %P, i32 0, i32 0
  143. %C = icmp eq i32* %A, %B
  144. ret i1 %C
  145. ; CHECK-LABEL: @test13(
  146. ; CHECK: %C = icmp eq i64 %X, -1
  147. }
  148. define <2 x i1> @test13_vector(<2 x i64> %X, <2 x %S*> %P) nounwind {
  149. ; CHECK-LABEL: @test13_vector(
  150. ; CHECK-NEXT: shl nuw <2 x i64> %X, <i64 2, i64 2>
  151. ; CHECK-NEXT: add <2 x i64> %A.idx, <i64 4, i64 4>
  152. ; CHECK-NEXT: icmp eq <2 x i64> %A.offs, zeroinitializer
  153. %A = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> zeroinitializer, <2 x i32> <i32 1, i32 1>, <2 x i64> %X
  154. %B = getelementptr inbounds %S, <2 x %S*> %P, <2 x i64> <i64 0, i64 0>, <2 x i32> <i32 0, i32 0>
  155. %C = icmp eq <2 x i32*> %A, %B
  156. ret <2 x i1> %C
  157. }
  158. define i1 @test13_as1(i16 %X, %S addrspace(1)* %P) {
  159. ; CHECK-LABEL: @test13_as1(
  160. ; CHECK-NEXT: %C = icmp eq i16 %X, -1
  161. ; CHECK-NEXT: ret i1 %C
  162. %A = getelementptr inbounds %S, %S addrspace(1)* %P, i16 0, i32 1, i16 %X
  163. %B = getelementptr inbounds %S, %S addrspace(1)* %P, i16 0, i32 0
  164. %C = icmp eq i32 addrspace(1)* %A, %B
  165. ret i1 %C
  166. }
  167. define <2 x i1> @test13_vector_as1(<2 x i16> %X, <2 x %S addrspace(1)*> %P) {
  168. ; CHECK-LABEL: @test13_vector_as1(
  169. ; CHECK-NEXT: shl nuw <2 x i16> %X, <i16 2, i16 2>
  170. ; CHECK-NEXT: add <2 x i16> %A.idx, <i16 4, i16 4>
  171. ; CHECK-NEXT: icmp eq <2 x i16> %A.offs, zeroinitializer
  172. ; CHECK-NEXT: ret <2 x i1>
  173. %A = getelementptr inbounds %S, <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 1, i32 1>, <2 x i16> %X
  174. %B = getelementptr inbounds %S, <2 x %S addrspace(1)*> %P, <2 x i16> <i16 0, i16 0>, <2 x i32> <i32 0, i32 0>
  175. %C = icmp eq <2 x i32 addrspace(1)*> %A, %B
  176. ret <2 x i1> %C
  177. }
  178. define i1 @test13_i32(i32 %X, %S* %P) {
  179. ; CHECK-LABEL: @test13_i32(
  180. ; CHECK: %C = icmp eq i32 %X, -1
  181. %A = getelementptr inbounds %S, %S* %P, i32 0, i32 1, i32 %X
  182. %B = getelementptr inbounds %S, %S* %P, i32 0, i32 0
  183. %C = icmp eq i32* %A, %B
  184. ret i1 %C
  185. }
  186. define i1 @test13_i16(i16 %X, %S* %P) {
  187. ; CHECK-LABEL: @test13_i16(
  188. ; CHECK: %C = icmp eq i16 %X, -1
  189. %A = getelementptr inbounds %S, %S* %P, i16 0, i32 1, i16 %X
  190. %B = getelementptr inbounds %S, %S* %P, i16 0, i32 0
  191. %C = icmp eq i32* %A, %B
  192. ret i1 %C
  193. }
  194. define i1 @test13_i128(i128 %X, %S* %P) {
  195. ; CHECK-LABEL: @test13_i128(
  196. ; CHECK: %C = icmp eq i64 %1, -1
  197. %A = getelementptr inbounds %S, %S* %P, i128 0, i32 1, i128 %X
  198. %B = getelementptr inbounds %S, %S* %P, i128 0, i32 0
  199. %C = icmp eq i32* %A, %B
  200. ret i1 %C
  201. }
  202. @G = external global [3 x i8]
  203. define i8* @test14(i32 %Idx) {
  204. %idx = zext i32 %Idx to i64
  205. %tmp = getelementptr i8, i8* getelementptr ([3 x i8], [3 x i8]* @G, i32 0, i32 0), i64 %idx
  206. ret i8* %tmp
  207. ; CHECK-LABEL: @test14(
  208. ; CHECK: getelementptr [3 x i8], [3 x i8]* @G, i64 0, i64 %idx
  209. }
  210. ; Test folding of constantexpr geps into normal geps.
  211. @Array = external global [40 x i32]
  212. define i32 *@test15(i64 %X) {
  213. %A = getelementptr i32, i32* getelementptr ([40 x i32], [40 x i32]* @Array, i64 0, i64 0), i64 %X
  214. ret i32* %A
  215. ; CHECK-LABEL: @test15(
  216. ; CHECK: getelementptr [40 x i32], [40 x i32]* @Array, i64 0, i64 %X
  217. }
  218. define i32* @test16(i32* %X, i32 %Idx) {
  219. %R = getelementptr i32, i32* %X, i32 %Idx
  220. ret i32* %R
  221. ; CHECK-LABEL: @test16(
  222. ; CHECK: sext i32 %Idx to i64
  223. }
  224. define i1 @test17(i16* %P, i32 %I, i32 %J) {
  225. %X = getelementptr inbounds i16, i16* %P, i32 %I
  226. %Y = getelementptr inbounds i16, i16* %P, i32 %J
  227. %C = icmp ult i16* %X, %Y
  228. ret i1 %C
  229. ; CHECK-LABEL: @test17(
  230. ; CHECK: %C = icmp slt i32 %I, %J
  231. }
  232. define i1 @test18(i16* %P, i32 %I) {
  233. %X = getelementptr inbounds i16, i16* %P, i32 %I
  234. %C = icmp ult i16* %X, %P
  235. ret i1 %C
  236. ; CHECK-LABEL: @test18(
  237. ; CHECK: %C = icmp slt i32 %I, 0
  238. }
  239. ; Larger than the pointer size for a non-zero address space
  240. define i1 @test18_as1(i16 addrspace(1)* %P, i32 %I) {
  241. ; CHECK-LABEL: @test18_as1(
  242. ; CHECK-NEXT: %1 = trunc i32 %I to i16
  243. ; CHECK-NEXT: %C = icmp slt i16 %1, 0
  244. ; CHECK-NEXT: ret i1 %C
  245. %X = getelementptr inbounds i16, i16 addrspace(1)* %P, i32 %I
  246. %C = icmp ult i16 addrspace(1)* %X, %P
  247. ret i1 %C
  248. }
  249. ; Smaller than the pointer size for a non-zero address space
  250. define i1 @test18_as1_i32(i16 addrspace(1)* %P, i32 %I) {
  251. ; CHECK-LABEL: @test18_as1_i32(
  252. ; CHECK-NEXT: %1 = trunc i32 %I to i16
  253. ; CHECK-NEXT: %C = icmp slt i16 %1, 0
  254. ; CHECK-NEXT: ret i1 %C
  255. %X = getelementptr inbounds i16, i16 addrspace(1)* %P, i32 %I
  256. %C = icmp ult i16 addrspace(1)* %X, %P
  257. ret i1 %C
  258. }
  259. ; Smaller than pointer size
  260. define i1 @test18_i16(i16* %P, i16 %I) {
  261. ; CHECK-LABEL: @test18_i16(
  262. ; CHECK: %C = icmp slt i16 %I, 0
  263. %X = getelementptr inbounds i16, i16* %P, i16 %I
  264. %C = icmp ult i16* %X, %P
  265. ret i1 %C
  266. }
  267. ; Same as pointer size
  268. define i1 @test18_i64(i16* %P, i64 %I) {
  269. ; CHECK-LABEL: @test18_i64(
  270. ; CHECK: %C = icmp slt i64 %I, 0
  271. %X = getelementptr inbounds i16, i16* %P, i64 %I
  272. %C = icmp ult i16* %X, %P
  273. ret i1 %C
  274. }
  275. ; Larger than the pointer size
  276. define i1 @test18_i128(i16* %P, i128 %I) {
  277. ; CHECK-LABEL: @test18_i128(
  278. ; CHECK: %C = icmp slt i64 %1, 0
  279. %X = getelementptr inbounds i16, i16* %P, i128 %I
  280. %C = icmp ult i16* %X, %P
  281. ret i1 %C
  282. }
  283. define i32 @test19(i32* %P, i32 %A, i32 %B) {
  284. %tmp.4 = getelementptr inbounds i32, i32* %P, i32 %A
  285. %tmp.9 = getelementptr inbounds i32, i32* %P, i32 %B
  286. %tmp.10 = icmp eq i32* %tmp.4, %tmp.9
  287. %tmp.11 = zext i1 %tmp.10 to i32
  288. ret i32 %tmp.11
  289. ; CHECK-LABEL: @test19(
  290. ; CHECK: icmp eq i32 %A, %B
  291. }
  292. define i32 @test20(i32* %P, i32 %A, i32 %B) {
  293. %tmp.4 = getelementptr inbounds i32, i32* %P, i32 %A
  294. %tmp.6 = icmp eq i32* %tmp.4, %P
  295. %tmp.7 = zext i1 %tmp.6 to i32
  296. ret i32 %tmp.7
  297. ; CHECK-LABEL: @test20(
  298. ; CHECK: icmp eq i32 %A, 0
  299. }
  300. define i32 @test20_as1(i32 addrspace(1)* %P, i32 %A, i32 %B) {
  301. %tmp.4 = getelementptr inbounds i32, i32 addrspace(1)* %P, i32 %A
  302. %tmp.6 = icmp eq i32 addrspace(1)* %tmp.4, %P
  303. %tmp.7 = zext i1 %tmp.6 to i32
  304. ret i32 %tmp.7
  305. ; CHECK-LABEL: @test20_as1(
  306. ; CHECK: icmp eq i16 %1, 0
  307. }
  308. define i32 @test21() {
  309. %pbob1 = alloca %intstruct
  310. %pbob2 = getelementptr %intstruct, %intstruct* %pbob1
  311. %pbobel = getelementptr %intstruct, %intstruct* %pbob2, i64 0, i32 0
  312. %rval = load i32, i32* %pbobel
  313. ret i32 %rval
  314. ; CHECK-LABEL: @test21(
  315. ; CHECK: getelementptr %intstruct, %intstruct* %pbob1, i64 0, i32 0
  316. }
  317. @A = global i32 1 ; <i32*> [#uses=1]
  318. @B = global i32 2 ; <i32*> [#uses=1]
  319. define i1 @test22() {
  320. %C = icmp ult i32* getelementptr (i32, i32* @A, i64 1),
  321. getelementptr (i32, i32* @B, i64 2)
  322. ret i1 %C
  323. ; CHECK-LABEL: @test22(
  324. ; CHECK: icmp ult (i32* getelementptr inbounds (i32, i32* @A, i64 1), i32* getelementptr (i32, i32* @B, i64 2))
  325. }
  326. %X = type { [10 x i32], float }
  327. define i1 @test23() {
  328. %A = getelementptr %X, %X* null, i64 0, i32 0, i64 0 ; <i32*> [#uses=1]
  329. %B = icmp ne i32* %A, null ; <i1> [#uses=1]
  330. ret i1 %B
  331. ; CHECK-LABEL: @test23(
  332. ; CHECK: ret i1 false
  333. }
  334. define void @test25() {
  335. entry:
  336. %tmp = getelementptr { i64, i64, i64, i64 }, { i64, i64, i64, i64 }* null, i32 0, i32 3 ; <i64*> [#uses=1]
  337. %tmp.upgrd.1 = load i64, i64* %tmp ; <i64> [#uses=1]
  338. %tmp8.ui = load i64, i64* null ; <i64> [#uses=1]
  339. %tmp8 = bitcast i64 %tmp8.ui to i64 ; <i64> [#uses=1]
  340. %tmp9 = and i64 %tmp8, %tmp.upgrd.1 ; <i64> [#uses=1]
  341. %sext = trunc i64 %tmp9 to i32 ; <i32> [#uses=1]
  342. %tmp27.i = sext i32 %sext to i64 ; <i64> [#uses=1]
  343. tail call void @foo25( i32 0, i64 %tmp27.i )
  344. unreachable
  345. ; CHECK-LABEL: @test25(
  346. }
  347. declare void @foo25(i32, i64)
  348. ; PR1637
  349. define i1 @test26(i8* %arr) {
  350. %X = getelementptr i8, i8* %arr, i32 1
  351. %Y = getelementptr i8, i8* %arr, i32 1
  352. %test = icmp uge i8* %X, %Y
  353. ret i1 %test
  354. ; CHECK-LABEL: @test26(
  355. ; CHECK: ret i1 true
  356. }
  357. %struct.__large_struct = type { [100 x i64] }
  358. %struct.compat_siginfo = type { i32, i32, i32, { [29 x i32] } }
  359. %struct.siginfo_t = type { i32, i32, i32, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] } }
  360. %struct.sigval_t = type { i8* }
  361. define i32 @test27(%struct.compat_siginfo* %to, %struct.siginfo_t* %from) {
  362. entry:
  363. %from_addr = alloca %struct.siginfo_t*
  364. %tmp344 = load %struct.siginfo_t*, %struct.siginfo_t** %from_addr, align 8
  365. %tmp345 = getelementptr %struct.siginfo_t, %struct.siginfo_t* %tmp344, i32 0, i32 3
  366. %tmp346 = getelementptr { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }, { { i32, i32, [0 x i8], %struct.sigval_t, i32 }, [88 x i8] }* %tmp345, i32 0, i32 0
  367. %tmp346347 = bitcast { i32, i32, [0 x i8], %struct.sigval_t, i32 }* %tmp346 to { i32, i32, %struct.sigval_t }*
  368. %tmp348 = getelementptr { i32, i32, %struct.sigval_t }, { i32, i32, %struct.sigval_t }* %tmp346347, i32 0, i32 2
  369. %tmp349 = getelementptr %struct.sigval_t, %struct.sigval_t* %tmp348, i32 0, i32 0
  370. %tmp349350 = bitcast i8** %tmp349 to i32*
  371. %tmp351 = load i32, i32* %tmp349350, align 8
  372. %tmp360 = call i32 asm sideeffect "...",
  373. "=r,ir,*m,i,0,~{dirflag},~{fpsr},~{flags}"( i32 %tmp351,
  374. %struct.__large_struct* null, i32 -14, i32 0 )
  375. unreachable
  376. ; CHECK-LABEL: @test27(
  377. }
  378. ; PR1978
  379. %struct.x = type <{ i8 }>
  380. @.str = internal constant [6 x i8] c"Main!\00"
  381. @.str1 = internal constant [12 x i8] c"destroy %p\0A\00"
  382. define i32 @test28() nounwind {
  383. entry:
  384. %orientations = alloca [1 x [1 x %struct.x]]
  385. %tmp3 = call i32 @puts( i8* getelementptr ([6 x i8], [6 x i8]* @.str, i32 0, i32 0) ) nounwind
  386. %tmp45 = getelementptr inbounds [1 x [1 x %struct.x]], [1 x [1 x %struct.x]]* %orientations, i32 1, i32 0, i32 0
  387. %orientations62 = getelementptr [1 x [1 x %struct.x]], [1 x [1 x %struct.x]]* %orientations, i32 0, i32 0, i32 0
  388. br label %bb10
  389. bb10:
  390. %indvar = phi i32 [ 0, %entry ], [ %indvar.next, %bb10 ]
  391. %tmp.0.reg2mem.0.rec = mul i32 %indvar, -1
  392. %tmp12.rec = add i32 %tmp.0.reg2mem.0.rec, -1
  393. %tmp12 = getelementptr inbounds %struct.x, %struct.x* %tmp45, i32 %tmp12.rec
  394. %tmp16 = call i32 (i8*, ...) @printf( i8* getelementptr ([12 x i8], [12 x i8]* @.str1, i32 0, i32 0), %struct.x* %tmp12 ) nounwind
  395. %tmp84 = icmp eq %struct.x* %tmp12, %orientations62
  396. %indvar.next = add i32 %indvar, 1
  397. br i1 %tmp84, label %bb17, label %bb10
  398. bb17:
  399. ret i32 0
  400. ; CHECK-LABEL: @test28(
  401. ; CHECK: icmp eq i32 %indvar, 0
  402. }
  403. declare i32 @puts(i8*)
  404. declare i32 @printf(i8*, ...)
  405. ; rdar://6762290
  406. %T = type <{ i64, i64, i64 }>
  407. define i32 @test29(i8* %start, i32 %X) nounwind {
  408. entry:
  409. %tmp3 = load i64, i64* null
  410. %add.ptr = getelementptr i8, i8* %start, i64 %tmp3
  411. %tmp158 = load i32, i32* null
  412. %add.ptr159 = getelementptr %T, %T* null, i32 %tmp158
  413. %add.ptr209 = getelementptr i8, i8* %start, i64 0
  414. %add.ptr212 = getelementptr i8, i8* %add.ptr209, i32 %X
  415. %cmp214 = icmp ugt i8* %add.ptr212, %add.ptr
  416. br i1 %cmp214, label %if.then216, label %if.end363
  417. if.then216:
  418. ret i32 1
  419. if.end363:
  420. ret i32 0
  421. ; CHECK-LABEL: @test29(
  422. }
  423. ; PR3694
  424. define i32 @test30(i32 %m, i32 %n) nounwind {
  425. entry:
  426. %0 = alloca i32, i32 %n, align 4
  427. %1 = bitcast i32* %0 to [0 x i32]*
  428. call void @test30f(i32* %0) nounwind
  429. %2 = getelementptr [0 x i32], [0 x i32]* %1, i32 0, i32 %m
  430. %3 = load i32, i32* %2, align 4
  431. ret i32 %3
  432. ; CHECK-LABEL: @test30(
  433. ; CHECK: getelementptr i32
  434. }
  435. declare void @test30f(i32*)
  436. define i1 @test31(i32* %A) {
  437. %B = getelementptr i32, i32* %A, i32 1
  438. %C = getelementptr i32, i32* %A, i64 1
  439. %V = icmp eq i32* %B, %C
  440. ret i1 %V
  441. ; CHECK-LABEL: @test31(
  442. ; CHECK: ret i1 true
  443. }
  444. ; PR1345
  445. define i8* @test32(i8* %v) {
  446. %A = alloca [4 x i8*], align 16
  447. %B = getelementptr [4 x i8*], [4 x i8*]* %A, i32 0, i32 0
  448. store i8* null, i8** %B
  449. %C = bitcast [4 x i8*]* %A to { [16 x i8] }*
  450. %D = getelementptr { [16 x i8] }, { [16 x i8] }* %C, i32 0, i32 0, i32 8
  451. %E = bitcast i8* %D to i8**
  452. store i8* %v, i8** %E
  453. %F = getelementptr [4 x i8*], [4 x i8*]* %A, i32 0, i32 2
  454. %G = load i8*, i8** %F
  455. ret i8* %G
  456. ; CHECK-LABEL: @test32(
  457. ; CHECK: %D = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 1
  458. ; CHECK: %F = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 2
  459. }
  460. ; PR3290
  461. %struct.Key = type { { i32, i32 } }
  462. %struct.anon = type <{ i8, [3 x i8], i32 }>
  463. define i32* @test33(%struct.Key* %A) {
  464. ; CHECK-LABEL: @test33(
  465. ; CHECK: getelementptr %struct.Key, %struct.Key* %A, i64 0, i32 0, i32 1
  466. %B = bitcast %struct.Key* %A to %struct.anon*
  467. %C = getelementptr %struct.anon, %struct.anon* %B, i32 0, i32 2
  468. ret i32* %C
  469. }
  470. define i32 addrspace(1)* @test33_as1(%struct.Key addrspace(1)* %A) {
  471. ; CHECK-LABEL: @test33_as1(
  472. ; CHECK: getelementptr %struct.Key, %struct.Key addrspace(1)* %A, i16 0, i32 0, i32 1
  473. %B = bitcast %struct.Key addrspace(1)* %A to %struct.anon addrspace(1)*
  474. %C = getelementptr %struct.anon, %struct.anon addrspace(1)* %B, i32 0, i32 2
  475. ret i32 addrspace(1)* %C
  476. }
  477. define i32 addrspace(1)* @test33_array_as1([10 x i32] addrspace(1)* %A) {
  478. ; CHECK-LABEL: @test33_array_as1(
  479. ; CHECK: getelementptr [10 x i32], [10 x i32] addrspace(1)* %A, i16 0, i16 2
  480. %B = bitcast [10 x i32] addrspace(1)* %A to [5 x i32] addrspace(1)*
  481. %C = getelementptr [5 x i32], [5 x i32] addrspace(1)* %B, i32 0, i32 2
  482. ret i32 addrspace(1)* %C
  483. }
  484. ; Make sure the GEP indices use the right pointer sized integer
  485. define i32 addrspace(1)* @test33_array_struct_as1([10 x %struct.Key] addrspace(1)* %A) {
  486. ; CHECK-LABEL: @test33_array_struct_as1(
  487. ; CHECK: getelementptr [10 x %struct.Key], [10 x %struct.Key] addrspace(1)* %A, i16 0, i16 1, i32 0, i32 0
  488. %B = bitcast [10 x %struct.Key] addrspace(1)* %A to [20 x i32] addrspace(1)*
  489. %C = getelementptr [20 x i32], [20 x i32] addrspace(1)* %B, i32 0, i32 2
  490. ret i32 addrspace(1)* %C
  491. }
  492. define i32 addrspace(1)* @test33_addrspacecast(%struct.Key* %A) {
  493. ; CHECK-LABEL: @test33_addrspacecast(
  494. ; CHECK: %C = getelementptr %struct.Key, %struct.Key* %A, i64 0, i32 0, i32 1
  495. ; CHECK-NEXT: addrspacecast i32* %C to i32 addrspace(1)*
  496. ; CHECK-NEXT: ret
  497. %B = addrspacecast %struct.Key* %A to %struct.anon addrspace(1)*
  498. %C = getelementptr %struct.anon, %struct.anon addrspace(1)* %B, i32 0, i32 2
  499. ret i32 addrspace(1)* %C
  500. }
  501. %T2 = type { i8*, i8 }
  502. define i8* @test34(i8* %Val, i64 %V) nounwind {
  503. entry:
  504. %A = alloca %T2, align 8
  505. %mrv_gep = bitcast %T2* %A to i64*
  506. %B = getelementptr %T2, %T2* %A, i64 0, i32 0
  507. store i64 %V, i64* %mrv_gep
  508. %C = load i8*, i8** %B, align 8
  509. ret i8* %C
  510. ; CHECK-LABEL: @test34(
  511. ; CHECK: %[[C:.*]] = inttoptr i64 %V to i8*
  512. ; CHECK: ret i8* %[[C]]
  513. }
  514. %t0 = type { i8*, [19 x i8] }
  515. %t1 = type { i8*, [0 x i8] }
  516. @array = external global [11 x i8]
  517. @s = external global %t0
  518. @"\01LC8" = external constant [17 x i8]
  519. ; Instcombine should be able to fold this getelementptr.
  520. define i32 @test35() nounwind {
  521. call i32 (i8*, ...) @printf(i8* getelementptr ([17 x i8], [17 x i8]* @"\01LC8", i32 0, i32 0),
  522. i8* getelementptr (%t1, %t1* bitcast (%t0* @s to %t1*), i32 0, i32 1, i32 0)) nounwind
  523. ret i32 0
  524. ; CHECK-LABEL: @test35(
  525. ; CHECK: call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @"\01LC8", i64 0, i64 0), i8* getelementptr inbounds (%t0, %t0* @s, i64 0, i32 1, i64 0)) [[NUW:#[0-9]+]]
  526. }
  527. ; Instcombine should constant-fold the GEP so that indices that have
  528. ; static array extents are within bounds of those array extents.
  529. ; In the below, -1 is not in the range [0,11). After the transformation,
  530. ; the same address is computed, but 3 is in the range of [0,11).
  531. define i8* @test36() nounwind {
  532. ret i8* getelementptr ([11 x i8], [11 x i8]* @array, i32 0, i64 -1)
  533. ; CHECK-LABEL: @test36(
  534. ; CHECK: ret i8* getelementptr ([11 x i8], [11 x i8]* @array, i64 1676976733973595601, i64 4)
  535. }
  536. ; Instcombine shouldn't assume that gep(A,0,1) != gep(A,1,0).
  537. @A37 = external constant [1 x i8]
  538. define i1 @test37() nounwind {
  539. ; CHECK-LABEL: @test37(
  540. ; CHECK: ret i1 true
  541. %t = icmp eq i8* getelementptr ([1 x i8], [1 x i8]* @A37, i64 0, i64 1),
  542. getelementptr ([1 x i8], [1 x i8]* @A37, i64 1, i64 0)
  543. ret i1 %t
  544. }
  545. ; Test index promotion
  546. define i32* @test38(i32* %I, i32 %n) {
  547. %A = getelementptr i32, i32* %I, i32 %n
  548. ret i32* %A
  549. ; CHECK-LABEL: @test38(
  550. ; CHECK: = sext i32 %n to i64
  551. ; CHECK: %A = getelementptr i32, i32* %I, i64 %
  552. }
  553. ; Test that we don't duplicate work when the second gep is a "bitcast".
  554. %pr10322_t = type { i8* }
  555. declare void @pr10322_f2(%pr10322_t*)
  556. declare void @pr10322_f3(i8**)
  557. define void @pr10322_f1(%pr10322_t* %foo) {
  558. entry:
  559. %arrayidx8 = getelementptr inbounds %pr10322_t, %pr10322_t* %foo, i64 2
  560. call void @pr10322_f2(%pr10322_t* %arrayidx8) nounwind
  561. %tmp2 = getelementptr inbounds %pr10322_t, %pr10322_t* %arrayidx8, i64 0, i32 0
  562. call void @pr10322_f3(i8** %tmp2) nounwind
  563. ret void
  564. ; CHECK-LABEL: @pr10322_f1(
  565. ; CHECK: %tmp2 = getelementptr inbounds %pr10322_t, %pr10322_t* %arrayidx8, i64 0, i32 0
  566. }
  567. ; Test that we combine the last two geps in this sequence, before we
  568. ; would wait for gep1 and gep2 to be combined and never combine 2 and 3.
  569. %three_gep_t = type {i32}
  570. %three_gep_t2 = type {%three_gep_t}
  571. define void @three_gep_f(%three_gep_t2* %x) {
  572. %gep1 = getelementptr %three_gep_t2, %three_gep_t2* %x, i64 2
  573. call void @three_gep_h(%three_gep_t2* %gep1)
  574. %gep2 = getelementptr %three_gep_t2, %three_gep_t2* %gep1, i64 0, i32 0
  575. %gep3 = getelementptr %three_gep_t, %three_gep_t* %gep2, i64 0, i32 0
  576. call void @three_gep_g(i32* %gep3)
  577. ; CHECK-LABEL: @three_gep_f(
  578. ; CHECK: %gep3 = getelementptr %three_gep_t2, %three_gep_t2* %gep1, i64 0, i32 0, i32 0
  579. ret void
  580. }
  581. declare void @three_gep_g(i32*)
  582. declare void @three_gep_h(%three_gep_t2*)
  583. %struct.ham = type { i32, %struct.zot*, %struct.zot*, %struct.zot* }
  584. %struct.zot = type { i64, i8 }
  585. define void @test39(%struct.ham* %arg, i8 %arg1) nounwind {
  586. %tmp = getelementptr inbounds %struct.ham, %struct.ham* %arg, i64 0, i32 2
  587. %tmp2 = load %struct.zot*, %struct.zot** %tmp, align 8
  588. %tmp3 = bitcast %struct.zot* %tmp2 to i8*
  589. %tmp4 = getelementptr inbounds i8, i8* %tmp3, i64 -8
  590. store i8 %arg1, i8* %tmp4, align 8
  591. ret void
  592. ; CHECK-LABEL: @test39(
  593. ; CHECK: getelementptr inbounds %struct.ham, %struct.ham* %arg, i64 0, i32 2
  594. ; CHECK: getelementptr inbounds i8, i8* %{{.+}}, i64 -8
  595. }
  596. define i1 @pr16483([1 x i8]* %a, [1 x i8]* %b) {
  597. %c = getelementptr [1 x i8], [1 x i8]* %a, i32 0, i32 0
  598. %d = getelementptr [1 x i8], [1 x i8]* %b, i32 0, i32 0
  599. %cmp = icmp ult i8* %c, %d
  600. ret i1 %cmp
  601. ; CHECK-LABEL: @pr16483(
  602. ; CHECK-NEXT: icmp ult [1 x i8]* %a, %b
  603. }
  604. define i8 @test_gep_bitcast_as1(i32 addrspace(1)* %arr, i16 %N) {
  605. ; CHECK-LABEL: @test_gep_bitcast_as1(
  606. ; CHECK: getelementptr i32, i32 addrspace(1)* %arr, i16 %N
  607. ; CHECK: bitcast
  608. %cast = bitcast i32 addrspace(1)* %arr to i8 addrspace(1)*
  609. %V = mul i16 %N, 4
  610. %t = getelementptr i8, i8 addrspace(1)* %cast, i16 %V
  611. %x = load i8, i8 addrspace(1)* %t
  612. ret i8 %x
  613. }
  614. ; The element size of the array matches the element size of the pointer
  615. define i64 @test_gep_bitcast_array_same_size_element([100 x double]* %arr, i64 %N) {
  616. ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element(
  617. ; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %V
  618. ; CHECK: bitcast
  619. %cast = bitcast [100 x double]* %arr to i64*
  620. %V = mul i64 %N, 8
  621. %t = getelementptr i64, i64* %cast, i64 %V
  622. %x = load i64, i64* %t
  623. ret i64 %x
  624. }
  625. ; gep should be done in the original address space.
  626. define i64 @test_gep_bitcast_array_same_size_element_addrspacecast([100 x double]* %arr, i64 %N) {
  627. ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_addrspacecast(
  628. ; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %V
  629. ; CHECK-NEXT: bitcast double*
  630. ; CHECK-NEXT: %t = addrspacecast i64*
  631. ; CHECK: load i64, i64 addrspace(3)* %t
  632. %cast = addrspacecast [100 x double]* %arr to i64 addrspace(3)*
  633. %V = mul i64 %N, 8
  634. %t = getelementptr i64, i64 addrspace(3)* %cast, i64 %V
  635. %x = load i64, i64 addrspace(3)* %t
  636. ret i64 %x
  637. }
  638. ; The element size of the array is different the element size of the pointer
  639. define i8 @test_gep_bitcast_array_different_size_element([100 x double]* %arr, i64 %N) {
  640. ; CHECK-LABEL: @test_gep_bitcast_array_different_size_element(
  641. ; CHECK: getelementptr [100 x double], [100 x double]* %arr, i64 0, i64 %N
  642. ; CHECK: bitcast
  643. %cast = bitcast [100 x double]* %arr to i8*
  644. %V = mul i64 %N, 8
  645. %t = getelementptr i8, i8* %cast, i64 %V
  646. %x = load i8, i8* %t
  647. ret i8 %x
  648. }
  649. define i64 @test_gep_bitcast_array_same_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
  650. ; CHECK-LABEL: @test_gep_bitcast_array_same_size_element_as1(
  651. ; CHECK: getelementptr [100 x double], [100 x double] addrspace(1)* %arr, i16 0, i16 %V
  652. ; CHECK: bitcast
  653. %cast = bitcast [100 x double] addrspace(1)* %arr to i64 addrspace(1)*
  654. %V = mul i16 %N, 8
  655. %t = getelementptr i64, i64 addrspace(1)* %cast, i16 %V
  656. %x = load i64, i64 addrspace(1)* %t
  657. ret i64 %x
  658. }
  659. define i8 @test_gep_bitcast_array_different_size_element_as1([100 x double] addrspace(1)* %arr, i16 %N) {
  660. ; CHECK-LABEL: @test_gep_bitcast_array_different_size_element_as1(
  661. ; CHECK: getelementptr [100 x double], [100 x double] addrspace(1)* %arr, i16 0, i16 %N
  662. ; CHECK: bitcast
  663. %cast = bitcast [100 x double] addrspace(1)* %arr to i8 addrspace(1)*
  664. %V = mul i16 %N, 8
  665. %t = getelementptr i8, i8 addrspace(1)* %cast, i16 %V
  666. %x = load i8, i8 addrspace(1)* %t
  667. ret i8 %x
  668. }
  669. define i64 @test40() {
  670. %array = alloca [3 x i32], align 4
  671. %gep = getelementptr inbounds [3 x i32], [3 x i32]* %array, i64 0, i64 2
  672. %gepi8 = bitcast i32* %gep to i8*
  673. %p = ptrtoint [3 x i32]* %array to i64
  674. %np = sub i64 0, %p
  675. %gep2 = getelementptr i8, i8* %gepi8, i64 %np
  676. %ret = ptrtoint i8* %gep2 to i64
  677. ret i64 %ret
  678. ; CHECK-LABEL: @test40
  679. ; CHECK-NEXT: ret i64 8
  680. }
  681. define i16 @test41([3 x i32] addrspace(1)* %array) {
  682. %gep = getelementptr inbounds [3 x i32], [3 x i32] addrspace(1)* %array, i16 0, i16 2
  683. %gepi8 = bitcast i32 addrspace(1)* %gep to i8 addrspace(1)*
  684. %p = ptrtoint [3 x i32] addrspace(1)* %array to i16
  685. %np = sub i16 0, %p
  686. %gep2 = getelementptr i8, i8 addrspace(1)* %gepi8, i16 %np
  687. %ret = ptrtoint i8 addrspace(1)* %gep2 to i16
  688. ret i16 %ret
  689. ; CHECK-LABEL: @test41(
  690. ; CHECK-NEXT: ret i16 8
  691. }
  692. define i8* @test42(i8* %c1, i8* %c2) {
  693. %ptrtoint = ptrtoint i8* %c1 to i64
  694. %sub = sub i64 0, %ptrtoint
  695. %gep = getelementptr inbounds i8, i8* %c2, i64 %sub
  696. ret i8* %gep
  697. ; CHECK-LABEL: @test42(
  698. ; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint i8* %c1 to i64
  699. ; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint i8* %c2 to i64
  700. ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
  701. ; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to i8*
  702. ; CHECK-NEXT: ret i8* [[INTTOPTR]]
  703. }
  704. define i16* @test43(i16* %c1, i16* %c2) {
  705. %ptrtoint = ptrtoint i16* %c1 to i64
  706. %sub = sub i64 0, %ptrtoint
  707. %shr = ashr i64 %sub, 1
  708. %gep = getelementptr inbounds i16, i16* %c2, i64 %shr
  709. ret i16* %gep
  710. ; CHECK-LABEL: @test43(
  711. ; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint i16* %c1 to i64
  712. ; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint i16* %c2 to i64
  713. ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
  714. ; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to i16*
  715. ; CHECK-NEXT: ret i16* [[INTTOPTR]]
  716. }
  717. define %struct.C* @test44(%struct.C* %c1, %struct.C* %c2) {
  718. %ptrtoint = ptrtoint %struct.C* %c1 to i64
  719. %sub = sub i64 0, %ptrtoint
  720. %shr = sdiv i64 %sub, 7
  721. %gep = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %shr
  722. ret %struct.C* %gep
  723. ; CHECK-LABEL: @test44(
  724. ; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint %struct.C* %c1 to i64
  725. ; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint %struct.C* %c2 to i64
  726. ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
  727. ; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to %struct.C*
  728. ; CHECK-NEXT: ret %struct.C* [[INTTOPTR]]
  729. }
  730. define %struct.C* @test45(%struct.C* %c1, %struct.C** %c2) {
  731. %ptrtoint1 = ptrtoint %struct.C* %c1 to i64
  732. %ptrtoint2 = ptrtoint %struct.C** %c2 to i64
  733. %sub = sub i64 %ptrtoint2, %ptrtoint1 ; C2 - C1
  734. %shr = sdiv i64 %sub, 7
  735. %gep = getelementptr inbounds %struct.C, %struct.C* %c1, i64 %shr ; C1 + (C2 - C1)
  736. ret %struct.C* %gep
  737. ; CHECK-LABEL: @test45(
  738. ; CHECK-NEXT: [[BITCAST:%.*]] = bitcast %struct.C** %c2 to %struct.C*
  739. ; CHECK-NEXT: ret %struct.C* [[BITCAST]]
  740. }
  741. define %struct.C* @test46(%struct.C* %c1, %struct.C* %c2, i64 %N) {
  742. %ptrtoint = ptrtoint %struct.C* %c1 to i64
  743. %sub = sub i64 0, %ptrtoint
  744. %sdiv = sdiv i64 %sub, %N
  745. %gep = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %sdiv
  746. ret %struct.C* %gep
  747. ; CHECK-LABEL: @test46(
  748. ; CHECK-NEXT: [[PTRTOINT:%.*]] = ptrtoint %struct.C* %c1 to i64
  749. ; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[PTRTOINT]]
  750. ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i64 [[SUB]], %N
  751. ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds %struct.C, %struct.C* %c2, i64 %sdiv
  752. ; CHECK-NEXT: ret %struct.C* [[GEP]]
  753. }
  754. define i32 addrspace(1)* @ascast_0_gep(i32* %p) nounwind {
  755. ; CHECK-LABEL: @ascast_0_gep(
  756. ; CHECK-NOT: getelementptr
  757. ; CHECK: ret
  758. %gep = getelementptr i32, i32* %p, i32 0
  759. %x = addrspacecast i32* %gep to i32 addrspace(1)*
  760. ret i32 addrspace(1)* %x
  761. }
  762. ; Do not merge the GEP and the addrspacecast, because it would undo the
  763. ; addrspacecast canonicalization.
  764. define i32 addrspace(1)* @ascast_0_0_gep([128 x i32]* %p) nounwind {
  765. ; CHECK-LABEL: @ascast_0_0_gep(
  766. ; CHECK-NEXT: getelementptr [128 x i32]
  767. ; CHECK-NEXT: addrspacecast i32*
  768. ; CHECK-NEXT: ret i32 addrspace(1)*
  769. %gep = getelementptr [128 x i32], [128 x i32]* %p, i32 0, i32 0
  770. %x = addrspacecast i32* %gep to i32 addrspace(1)*
  771. ret i32 addrspace(1)* %x
  772. }
  773. ; CHECK: attributes [[NUW]] = { nounwind }