hoisting.ll 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ; RUN: opt < %s -licm -S | FileCheck %s
  2. @X = global i32 0 ; <i32*> [#uses=1]
  3. declare void @foo()
  4. ; This testcase tests for a problem where LICM hoists
  5. ; potentially trapping instructions when they are not guaranteed to execute.
  6. define i32 @test1(i1 %c) {
  7. ; CHECK-LABEL: @test1(
  8. %A = load i32, i32* @X ; <i32> [#uses=2]
  9. br label %Loop
  10. Loop: ; preds = %LoopTail, %0
  11. call void @foo( )
  12. br i1 %c, label %LoopTail, label %IfUnEqual
  13. IfUnEqual: ; preds = %Loop
  14. ; CHECK: IfUnEqual:
  15. ; CHECK-NEXT: sdiv i32 4, %A
  16. %B1 = sdiv i32 4, %A ; <i32> [#uses=1]
  17. br label %LoopTail
  18. LoopTail: ; preds = %IfUnEqual, %Loop
  19. %B = phi i32 [ 0, %Loop ], [ %B1, %IfUnEqual ] ; <i32> [#uses=1]
  20. br i1 %c, label %Loop, label %Out
  21. Out: ; preds = %LoopTail
  22. %C = sub i32 %A, %B ; <i32> [#uses=1]
  23. ret i32 %C
  24. }
  25. declare void @foo2(i32) nounwind
  26. ;; It is ok and desirable to hoist this potentially trapping instruction.
  27. define i32 @test2(i1 %c) {
  28. ; CHECK-LABEL: @test2(
  29. ; CHECK-NEXT: load i32, i32* @X
  30. ; CHECK-NEXT: %B = sdiv i32 4, %A
  31. %A = load i32, i32* @X ; <i32> [#uses=2]
  32. br label %Loop
  33. Loop:
  34. ;; Should have hoisted this div!
  35. %B = sdiv i32 4, %A ; <i32> [#uses=2]
  36. call void @foo2( i32 %B )
  37. br i1 %c, label %Loop, label %Out
  38. Out: ; preds = %Loop
  39. %C = sub i32 %A, %B ; <i32> [#uses=1]
  40. ret i32 %C
  41. }
  42. ; This loop invariant instruction should be constant folded, not hoisted.
  43. define i32 @test3(i1 %c) {
  44. ; CHECK-LABEL: define i32 @test3(
  45. ; CHECK: call void @foo2(i32 6)
  46. %A = load i32, i32* @X ; <i32> [#uses=2]
  47. br label %Loop
  48. Loop:
  49. %B = add i32 4, 2 ; <i32> [#uses=2]
  50. call void @foo2( i32 %B )
  51. br i1 %c, label %Loop, label %Out
  52. Out: ; preds = %Loop
  53. %C = sub i32 %A, %B ; <i32> [#uses=1]
  54. ret i32 %C
  55. }
  56. ; CHECK-LABEL: @test4(
  57. ; CHECK: call
  58. ; CHECK: sdiv
  59. ; CHECK: ret
  60. define i32 @test4(i32 %x, i32 %y) nounwind uwtable ssp {
  61. entry:
  62. br label %for.body
  63. for.body: ; preds = %entry, %for.body
  64. %i.02 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
  65. %n.01 = phi i32 [ 0, %entry ], [ %add, %for.body ]
  66. call void @foo_may_call_exit(i32 0)
  67. %div = sdiv i32 %x, %y
  68. %add = add nsw i32 %n.01, %div
  69. %inc = add nsw i32 %i.02, 1
  70. %cmp = icmp slt i32 %inc, 10000
  71. br i1 %cmp, label %for.body, label %for.end
  72. for.end: ; preds = %for.body
  73. %n.0.lcssa = phi i32 [ %add, %for.body ]
  74. ret i32 %n.0.lcssa
  75. }
  76. declare void @foo_may_call_exit(i32)
  77. ; PR14854
  78. ; CHECK-LABEL: @test5(
  79. ; CHECK: extractvalue
  80. ; CHECK: br label %tailrecurse
  81. ; CHECK: tailrecurse:
  82. ; CHECK: ifend:
  83. ; CHECK: insertvalue
  84. define { i32*, i32 } @test5(i32 %i, { i32*, i32 } %e) {
  85. entry:
  86. br label %tailrecurse
  87. tailrecurse: ; preds = %then, %entry
  88. %i.tr = phi i32 [ %i, %entry ], [ %cmp2, %then ]
  89. %out = extractvalue { i32*, i32 } %e, 1
  90. %d = insertvalue { i32*, i32 } %e, i32* null, 0
  91. %cmp1 = icmp sgt i32 %out, %i.tr
  92. br i1 %cmp1, label %then, label %ifend
  93. then: ; preds = %tailrecurse
  94. call void @foo()
  95. %cmp2 = add i32 %i.tr, 1
  96. br label %tailrecurse
  97. ifend: ; preds = %tailrecurse
  98. ret { i32*, i32 } %d
  99. }