phi-select.ll 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ; RUN: opt -scalarrepl -S < %s | FileCheck %s
  2. ; Test promotion of allocas that have phis and select users.
  3. 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"
  4. target triple = "x86_64-apple-darwin10.2"
  5. %struct.X = type { i32 }
  6. %PairTy = type {i32, i32}
  7. ; CHECK-LABEL: @test1(
  8. ; CHECK: %a.0 = alloca i32
  9. ; CHECK: %b.0 = alloca i32
  10. define i32 @test1(i32 %x) nounwind readnone ssp {
  11. entry:
  12. %a = alloca %struct.X, align 8 ; <%struct.X*> [#uses=2]
  13. %b = alloca %struct.X, align 8 ; <%struct.X*> [#uses=2]
  14. %0 = getelementptr inbounds %struct.X, %struct.X* %a, i64 0, i32 0 ; <i32*> [#uses=1]
  15. store i32 1, i32* %0, align 8
  16. %1 = getelementptr inbounds %struct.X, %struct.X* %b, i64 0, i32 0 ; <i32*> [#uses=1]
  17. store i32 2, i32* %1, align 8
  18. %2 = icmp eq i32 %x, 0 ; <i1> [#uses=1]
  19. %p.0 = select i1 %2, %struct.X* %b, %struct.X* %a ; <%struct.X*> [#uses=1]
  20. %3 = getelementptr inbounds %struct.X, %struct.X* %p.0, i64 0, i32 0 ; <i32*> [#uses=1]
  21. %4 = load i32, i32* %3, align 8 ; <i32> [#uses=1]
  22. ret i32 %4
  23. }
  24. ; CHECK-LABEL: @test2(
  25. ; CHECK: %X.ld = phi i32 [ 1, %entry ], [ 2, %T ]
  26. ; CHECK-NEXT: ret i32 %X.ld
  27. define i32 @test2(i1 %c) {
  28. entry:
  29. %A = alloca {i32, i32}
  30. %B = getelementptr {i32, i32}, {i32, i32}* %A, i32 0, i32 0
  31. store i32 1, i32* %B
  32. br i1 %c, label %T, label %F
  33. T:
  34. %C = getelementptr {i32, i32}, {i32, i32}* %A, i32 0, i32 1
  35. store i32 2, i32* %C
  36. br label %F
  37. F:
  38. %X = phi i32* [%B, %entry], [%C, %T]
  39. %Q = load i32, i32* %X
  40. ret i32 %Q
  41. }
  42. ; CHECK-LABEL: @test3(
  43. ; CHECK-NEXT: %Q = select i1 %c, i32 1, i32 2
  44. ; CHECK-NEXT: ret i32 %Q
  45. ; rdar://8904039
  46. define i32 @test3(i1 %c) {
  47. %A = alloca {i32, i32}
  48. %B = getelementptr {i32, i32}, {i32, i32}* %A, i32 0, i32 0
  49. store i32 1, i32* %B
  50. %C = getelementptr {i32, i32}, {i32, i32}* %A, i32 0, i32 1
  51. store i32 2, i32* %C
  52. %X = select i1 %c, i32* %B, i32* %C
  53. %Q = load i32, i32* %X
  54. ret i32 %Q
  55. }
  56. ;; We can't scalarize this, a use of the select is not an element access.
  57. define i64 @test4(i1 %c) {
  58. entry:
  59. %A = alloca %PairTy
  60. ; CHECK-LABEL: @test4(
  61. ; CHECK: %A = alloca %PairTy
  62. %B = getelementptr %PairTy, %PairTy* %A, i32 0, i32 0
  63. store i32 1, i32* %B
  64. %C = getelementptr %PairTy, %PairTy* %A, i32 0, i32 1
  65. store i32 2, i32* %B
  66. %X = select i1 %c, i32* %B, i32* %C
  67. %Y = bitcast i32* %X to i64*
  68. %Q = load i64, i64* %Y
  69. ret i64 %Q
  70. }
  71. ;;
  72. ;; Tests for promoting allocas used by selects.
  73. ;; rdar://7339113
  74. ;;
  75. define i32 @test5(i32 *%P) nounwind readnone ssp {
  76. entry:
  77. %b = alloca i32, align 8
  78. store i32 2, i32* %b, align 8
  79. ;; Select on constant condition should be folded.
  80. %p.0 = select i1 false, i32* %b, i32* %P
  81. store i32 123, i32* %p.0
  82. %r = load i32, i32* %b, align 8
  83. ret i32 %r
  84. ; CHECK-LABEL: @test5(
  85. ; CHECK: store i32 123, i32* %P
  86. ; CHECK: ret i32 2
  87. }
  88. define i32 @test6(i32 %x, i1 %c) nounwind readnone ssp {
  89. %a = alloca i32, align 8
  90. %b = alloca i32, align 8
  91. store i32 1, i32* %a, align 8
  92. store i32 2, i32* %b, align 8
  93. %p.0 = select i1 %c, i32* %b, i32* %a
  94. %r = load i32, i32* %p.0, align 8
  95. ret i32 %r
  96. ; CHECK-LABEL: @test6(
  97. ; CHECK-NEXT: %r = select i1 %c, i32 2, i32 1
  98. ; CHECK-NEXT: ret i32 %r
  99. }
  100. ; Verify that the loads happen where the loads are, not where the select is.
  101. define i32 @test7(i32 %x, i1 %c) nounwind readnone ssp {
  102. %a = alloca i32, align 8
  103. %b = alloca i32, align 8
  104. store i32 1, i32* %a
  105. store i32 2, i32* %b
  106. %p.0 = select i1 %c, i32* %b, i32* %a
  107. store i32 0, i32* %a
  108. %r = load i32, i32* %p.0, align 8
  109. ret i32 %r
  110. ; CHECK-LABEL: @test7(
  111. ; CHECK-NOT: alloca i32
  112. ; CHECK: %r = select i1 %c, i32 2, i32 0
  113. ; CHECK: ret i32 %r
  114. }
  115. ;; Promote allocs that are PHI'd together by moving the loads.
  116. define i32 @test8(i32 %x) nounwind readnone ssp {
  117. ; CHECK-LABEL: @test8(
  118. ; CHECK-NOT: load i32
  119. ; CHECK-NOT: store i32
  120. ; CHECK: %p.0.ld = phi i32 [ 2, %entry ], [ 1, %T ]
  121. ; CHECK-NEXT: ret i32 %p.0.ld
  122. entry:
  123. %a = alloca i32, align 8
  124. %b = alloca i32, align 8
  125. store i32 1, i32* %a, align 8
  126. store i32 2, i32* %b, align 8
  127. %c = icmp eq i32 %x, 0
  128. br i1 %c, label %T, label %Cont
  129. T:
  130. br label %Cont
  131. Cont:
  132. %p.0 = phi i32* [%b, %entry],[%a, %T]
  133. %r = load i32, i32* %p.0, align 8
  134. ret i32 %r
  135. }