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: @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)
33
34
35;; It is ok and desirable to hoist this potentially trapping instruction.
36define i32 @test2(i1 %c) {
37; CHECK: @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: 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