alloca.ll 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ; RUN: opt < %s -instcombine -S -default-data-layout="E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128" | FileCheck %s -check-prefix=CHECK -check-prefix=ALL
  2. ; RUN: opt < %s -instcombine -S -default-data-layout="E-p:32:32:32-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128" | FileCheck %s -check-prefix=P32 -check-prefix=ALL
  3. ; RUN: opt < %s -instcombine -S | FileCheck %s -check-prefix=NODL -check-prefix=ALL
  4. declare void @use(...)
  5. @int = global i32 zeroinitializer
  6. ; Zero byte allocas should be merged if they can't be deleted.
  7. ; CHECK-LABEL: @test(
  8. ; CHECK: alloca
  9. ; CHECK-NOT: alloca
  10. define void @test() {
  11. %X = alloca [0 x i32] ; <[0 x i32]*> [#uses=1]
  12. call void (...) @use( [0 x i32]* %X )
  13. %Y = alloca i32, i32 0 ; <i32*> [#uses=1]
  14. call void (...) @use( i32* %Y )
  15. %Z = alloca { } ; <{ }*> [#uses=1]
  16. call void (...) @use( { }* %Z )
  17. %size = load i32, i32* @int
  18. %A = alloca {{}}, i32 %size
  19. call void (...) @use( {{}}* %A )
  20. ret void
  21. }
  22. ; Zero byte allocas should be deleted.
  23. ; CHECK-LABEL: @test2(
  24. ; CHECK-NOT: alloca
  25. define void @test2() {
  26. %A = alloca i32 ; <i32*> [#uses=1]
  27. store i32 123, i32* %A
  28. ret void
  29. }
  30. ; Zero byte allocas should be deleted.
  31. ; CHECK-LABEL: @test3(
  32. ; CHECK-NOT: alloca
  33. define void @test3() {
  34. %A = alloca { i32 } ; <{ i32 }*> [#uses=1]
  35. %B = getelementptr { i32 }, { i32 }* %A, i32 0, i32 0 ; <i32*> [#uses=1]
  36. store i32 123, i32* %B
  37. ret void
  38. }
  39. ; CHECK-LABEL: @test4(
  40. ; CHECK: = zext i32 %n to i64
  41. ; CHECK: %A = alloca i32, i64 %
  42. define i32* @test4(i32 %n) {
  43. %A = alloca i32, i32 %n
  44. ret i32* %A
  45. }
  46. ; Allocas which are only used by GEPs, bitcasts, and stores (transitively)
  47. ; should be deleted.
  48. define void @test5() {
  49. ; CHECK-LABEL: @test5(
  50. ; CHECK-NOT: alloca
  51. ; CHECK-NOT: store
  52. ; CHECK: ret
  53. entry:
  54. %a = alloca { i32 }
  55. %b = alloca i32*
  56. %a.1 = getelementptr { i32 }, { i32 }* %a, i32 0, i32 0
  57. store i32 123, i32* %a.1
  58. store i32* %a.1, i32** %b
  59. %b.1 = bitcast i32** %b to i32*
  60. store i32 123, i32* %b.1
  61. %a.2 = getelementptr { i32 }, { i32 }* %a, i32 0, i32 0
  62. store atomic i32 2, i32* %a.2 unordered, align 4
  63. %a.3 = getelementptr { i32 }, { i32 }* %a, i32 0, i32 0
  64. store atomic i32 3, i32* %a.3 release, align 4
  65. %a.4 = getelementptr { i32 }, { i32 }* %a, i32 0, i32 0
  66. store atomic i32 4, i32* %a.4 seq_cst, align 4
  67. ret void
  68. }
  69. declare void @f(i32* %p)
  70. ; Check that we don't delete allocas in some erroneous cases.
  71. define void @test6() {
  72. ; CHECK-LABEL: @test6(
  73. ; CHECK-NOT: ret
  74. ; CHECK: alloca
  75. ; CHECK-NEXT: alloca
  76. ; CHECK: ret
  77. entry:
  78. %a = alloca { i32 }
  79. %b = alloca i32
  80. %a.1 = getelementptr { i32 }, { i32 }* %a, i32 0, i32 0
  81. store volatile i32 123, i32* %a.1
  82. tail call void @f(i32* %b)
  83. ret void
  84. }
  85. ; PR14371
  86. %opaque_type = type opaque
  87. %real_type = type { { i32, i32* } }
  88. @opaque_global = external constant %opaque_type, align 4
  89. define void @test7() {
  90. entry:
  91. %0 = alloca %real_type, align 4
  92. %1 = bitcast %real_type* %0 to i8*
  93. call void @llvm.memcpy.p0i8.p0i8.i32(i8* %1, i8* bitcast (%opaque_type* @opaque_global to i8*), i32 8, i32 1, i1 false)
  94. ret void
  95. }
  96. declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture, i32, i32, i1) nounwind
  97. ; Check that the GEP indices use the pointer size, or 64 if unknown
  98. define void @test8() {
  99. ; CHECK-LABEL: @test8(
  100. ; CHECK: alloca [100 x i32]
  101. ; CHECK: getelementptr inbounds [100 x i32], [100 x i32]* %x1, i64 0, i64 0
  102. ; P32-LABEL: @test8(
  103. ; P32: alloca [100 x i32]
  104. ; P32: getelementptr inbounds [100 x i32], [100 x i32]* %x1, i32 0, i32 0
  105. ; NODL-LABEL: @test8(
  106. ; NODL: alloca [100 x i32]
  107. ; NODL: getelementptr inbounds [100 x i32], [100 x i32]* %x1, i64 0, i64 0
  108. %x = alloca i32, i32 100
  109. call void (...) @use(i32* %x)
  110. ret void
  111. }
  112. ; PR19569
  113. %struct_type = type { i32, i32 }
  114. declare void @test9_aux(<{ %struct_type }>* inalloca)
  115. declare i8* @llvm.stacksave()
  116. declare void @llvm.stackrestore(i8*)
  117. define void @test9(%struct_type* %a) {
  118. ; CHECK-LABEL: @test9(
  119. entry:
  120. %inalloca.save = call i8* @llvm.stacksave()
  121. %argmem = alloca inalloca <{ %struct_type }>
  122. ; CHECK: alloca inalloca i64, align 8
  123. %0 = getelementptr inbounds <{ %struct_type }>, <{ %struct_type }>* %argmem, i32 0, i32 0
  124. %1 = bitcast %struct_type* %0 to i8*
  125. %2 = bitcast %struct_type* %a to i8*
  126. call void @llvm.memcpy.p0i8.p0i8.i32(i8* %1, i8* %2, i32 8, i32 4, i1 false)
  127. call void @test9_aux(<{ %struct_type }>* inalloca %argmem)
  128. call void @llvm.stackrestore(i8* %inalloca.save)
  129. ret void
  130. }
  131. define void @test10() {
  132. entry:
  133. ; ALL-LABEL: @test10(
  134. ; ALL: %v32 = alloca i1, align 8
  135. ; ALL: %v64 = alloca i1, align 8
  136. ; ALL: %v33 = alloca i1, align 8
  137. %v32 = alloca i1, align 8
  138. %v64 = alloca i1, i64 1, align 8
  139. %v33 = alloca i1, i33 1, align 8
  140. call void (...) @use(i1* %v32, i1* %v64, i1* %v33)
  141. ret void
  142. }