1 ; RUN: opt < %s -licm -S | FileCheck %s
3 @X = global i32 0 ; <i32*> [#uses=1]
7 ; This testcase tests for a problem where LICM hoists
8 ; potentially trapping instructions when they are not guaranteed to execute.
9 define i32 @test1(i1 %c) {
10 ; CHECK-LABEL: @test1(
11 %A = load i32* @X ; <i32> [#uses=2]
13 Loop: ; preds = %LoopTail, %0
15 br i1 %c, label %LoopTail, label %IfUnEqual
17 IfUnEqual: ; preds = %Loop
19 ; CHECK-NEXT: sdiv i32 4, %A
20 %B1 = sdiv i32 4, %A ; <i32> [#uses=1]
23 LoopTail: ; preds = %IfUnEqual, %Loop
24 %B = phi i32 [ 0, %Loop ], [ %B1, %IfUnEqual ] ; <i32> [#uses=1]
25 br i1 %c, label %Loop, label %Out
26 Out: ; preds = %LoopTail
27 %C = sub i32 %A, %B ; <i32> [#uses=1]
32 declare void @foo2(i32) nounwind
35 ;; It is ok and desirable to hoist this potentially trapping instruction.
36 define i32 @test2(i1 %c) {
37 ; CHECK-LABEL: @test2(
38 ; CHECK-NEXT: load i32* @X
39 ; CHECK-NEXT: %B = sdiv i32 4, %A
40 %A = load i32* @X ; <i32> [#uses=2]
43 ;; Should have hoisted this div!
44 %B = sdiv i32 4, %A ; <i32> [#uses=2]
45 call void @foo2( i32 %B )
46 br i1 %c, label %Loop, label %Out
48 %C = sub i32 %A, %B ; <i32> [#uses=1]
53 ; This loop invariant instruction should be constant folded, not hoisted.
54 define i32 @test3(i1 %c) {
55 ; CHECK: define i32 @test3
56 ; CHECK: call void @foo2(i32 6)
57 %A = load i32* @X ; <i32> [#uses=2]
60 %B = add i32 4, 2 ; <i32> [#uses=2]
61 call void @foo2( i32 %B )
62 br i1 %c, label %Loop, label %Out
64 %C = sub i32 %A, %B ; <i32> [#uses=1]
68 ; CHECK-LABEL: @test4(
72 define i32 @test4(i32 %x, i32 %y) nounwind uwtable ssp {
76 for.body: ; preds = %entry, %for.body
77 %i.02 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
78 %n.01 = phi i32 [ 0, %entry ], [ %add, %for.body ]
79 call void @foo_may_call_exit(i32 0)
80 %div = sdiv i32 %x, %y
81 %add = add nsw i32 %n.01, %div
82 %inc = add nsw i32 %i.02, 1
83 %cmp = icmp slt i32 %inc, 10000
84 br i1 %cmp, label %for.body, label %for.end
86 for.end: ; preds = %for.body
87 %n.0.lcssa = phi i32 [ %add, %for.body ]
91 declare void @foo_may_call_exit(i32)
94 ; CHECK-LABEL: @test5(
96 ; CHECK: br label %tailrecurse
100 define { i32*, i32 } @test5(i32 %i, { i32*, i32 } %e) {
102 br label %tailrecurse
104 tailrecurse: ; preds = %then, %entry
105 %i.tr = phi i32 [ %i, %entry ], [ %cmp2, %then ]
106 %out = extractvalue { i32*, i32 } %e, 1
107 %d = insertvalue { i32*, i32 } %e, i32* null, 0
108 %cmp1 = icmp sgt i32 %out, %i.tr
109 br i1 %cmp1, label %then, label %ifend
111 then: ; preds = %tailrecurse
113 %cmp2 = add i32 %i.tr, 1
114 br label %tailrecurse
116 ifend: ; preds = %tailrecurse