1; RUN: opt < %s -licm -S | FileCheck %s
2
3@X = global i32 0		; <i32*> [#uses=1]
4
5declare void @foo()
6
7; This testcase tests for a problem where LICM hoists
8; potentially trapping instructions when they are not guaranteed to execute.
9define i32 @test1(i1 %c) {
10; CHECK-LABEL: @test1(
11	%A = load i32* @X		; <i32> [#uses=2]
12	br label %Loop
13Loop:		; preds = %LoopTail, %0
14	call void @foo( )
15	br i1 %c, label %LoopTail, label %IfUnEqual
16
17IfUnEqual:		; preds = %Loop
18; CHECK: IfUnEqual:
19; CHECK-NEXT: sdiv i32 4, %A
20	%B1 = sdiv i32 4, %A		; <i32> [#uses=1]
21	br label %LoopTail
22
23LoopTail:		; preds = %IfUnEqual, %Loop
24	%B = phi i32 [ 0, %Loop ], [ %B1, %IfUnEqual ]		; <i32> [#uses=1]
25	br i1 %c, label %Loop, label %Out
26Out:		; preds = %LoopTail
27	%C = sub i32 %A, %B		; <i32> [#uses=1]
28	ret i32 %C
29}
30
31
32declare void @foo2(i32) nounwind
33
34
35;; It is ok and desirable to hoist this potentially trapping instruction.
36define 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]
41	br label %Loop
42Loop:
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
47Out:		; preds = %Loop
48	%C = sub i32 %A, %B		; <i32> [#uses=1]
49	ret i32 %C
50}
51
52
53; This loop invariant instruction should be constant folded, not hoisted.
54define i32 @test3(i1 %c) {
55; CHECK-LABEL: define i32 @test3(
56; CHECK: call void @foo2(i32 6)
57	%A = load i32* @X		; <i32> [#uses=2]
58	br label %Loop
59Loop:
60	%B = add i32 4, 2		; <i32> [#uses=2]
61	call void @foo2( i32 %B )
62	br i1 %c, label %Loop, label %Out
63Out:		; preds = %Loop
64	%C = sub i32 %A, %B		; <i32> [#uses=1]
65	ret i32 %C
66}
67
68; CHECK-LABEL: @test4(
69; CHECK: call
70; CHECK: sdiv
71; CHECK: ret
72define i32 @test4(i32 %x, i32 %y) nounwind uwtable ssp {
73entry:
74  br label %for.body
75
76for.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
85
86for.end:                                          ; preds = %for.body
87  %n.0.lcssa = phi i32 [ %add, %for.body ]
88  ret i32 %n.0.lcssa
89}
90
91declare void @foo_may_call_exit(i32)
92
93; PR14854
94; CHECK-LABEL: @test5(
95; CHECK: extractvalue
96; CHECK: br label %tailrecurse
97; CHECK: tailrecurse:
98; CHECK: ifend:
99; CHECK: insertvalue
100define { i32*, i32 } @test5(i32 %i, { i32*, i32 } %e) {
101entry:
102  br label %tailrecurse
103
104tailrecurse:                                      ; 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
110
111then:                                             ; preds = %tailrecurse
112  call void @foo()
113  %cmp2 = add i32 %i.tr, 1
114  br label %tailrecurse
115
116ifend:                                            ; preds = %tailrecurse
117  ret { i32*, i32 } %d
118}
119