1 ; RUN: opt < %s -loop-unswitch -verify-loop-info -S < %s 2>&1 | FileCheck %s
2 ; RUN: opt < %s -loop-unswitch -verify-loop-info -enable-mssa-loop-dependency=true -verify-memoryssa -S < %s 2>&1 | FileCheck %s
4 @sink = global i32 0, align 4
5 @y = global i64 0, align 8
7 ; The following is approximately:
8 ; void f(bool x, int p, int q) {
9 ; volatile bool x2 = x;
10 ; for (int i = 0; i < 1; ++i) {
19 ; With MemorySanitizer, the loop can not be unswitched on "y", because "y" could
20 ; be uninitialized when x == false.
21 ; Test that the branch on "y" is inside the loop (after the first unconditional
24 define void @may_not_execute(i1 zeroext %x, i32 %p, i32 %q) sanitize_memory {
25 ; CHECK-LABEL: @may_not_execute(
27 ; CHECK: %[[Y:.*]] = load i64, i64* @y, align 8
28 ; CHECK: %[[YB:.*]] = icmp eq i64 %[[Y]], 0
31 ; CHECK: br i1 %[[YB]]
33 %x2 = alloca i8, align 1
34 %frombool1 = zext i1 %x to i8
35 store volatile i8 %frombool1, i8* %x2, align 1
36 %0 = load i64, i64* @y, align 8
37 %tobool3 = icmp eq i64 %0, 0
41 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
42 %x2.0. = load volatile i8, i8* %x2, align 1
43 %tobool2 = icmp eq i8 %x2.0., 0
44 br i1 %tobool2, label %for.inc, label %if.then
47 br i1 %tobool3, label %if.else, label %if.then4
50 store volatile i32 %p, i32* @sink, align 4
54 store volatile i32 %q, i32* @sink, align 4
58 %inc = add nsw i32 %i.01, 1
59 %cmp = icmp slt i32 %inc, 1
60 br i1 %cmp, label %for.body, label %for.end
67 ; The same as above, but "y" is a function parameter instead of a global.
68 ; This shows that it is not enough to suppress hoisting of load instructions,
69 ; the actual problem is in the speculative branching.
71 define void @may_not_execute2(i1 zeroext %x, i1 zeroext %y, i32 %p, i32 %q) sanitize_memory {
72 ; CHECK-LABEL: @may_not_execute2(
77 %x2 = alloca i8, align 1
78 %frombool2 = zext i1 %x to i8
79 store volatile i8 %frombool2, i8* %x2, align 1
83 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
84 %x2.0. = load volatile i8, i8* %x2, align 1
85 %tobool3 = icmp eq i8 %x2.0., 0
86 br i1 %tobool3, label %for.inc, label %if.then
89 br i1 %y, label %if.then5, label %if.else
92 store volatile i32 %p, i32* @sink, align 4
96 store volatile i32 %q, i32* @sink, align 4
100 %inc = add nsw i32 %i.01, 1
101 %cmp = icmp slt i32 %inc, 1
102 br i1 %cmp, label %for.body, label %for.end
109 ; The following is approximately:
110 ; void f(bool x, int p, int q) {
111 ; volatile bool x2 = x;
112 ; for (int i = 0; i < 1; ++i) {
119 ; "if (y)" is guaranteed to execute; the loop can be unswitched.
121 define void @must_execute(i1 zeroext %x, i32 %p, i32 %q) sanitize_memory {
122 ; CHECK-LABEL: @must_execute(
124 ; CHECK: %[[Y:.*]] = load i64, i64* @y, align 8
125 ; CHECK-NEXT: %[[YB:.*]] = icmp eq i64 %[[Y]], 0
126 ; CHECK-NEXT: br i1 %[[YB]],
128 %x2 = alloca i8, align 1
129 %frombool1 = zext i1 %x to i8
130 store volatile i8 %frombool1, i8* %x2, align 1
131 %0 = load i64, i64* @y, align 8
132 %tobool2 = icmp eq i64 %0, 0
136 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
137 br i1 %tobool2, label %if.else, label %if.then
140 store volatile i32 %p, i32* @sink, align 4
144 store volatile i32 %q, i32* @sink, align 4
148 %inc = add nsw i32 %i.01, 1
149 %cmp = icmp slt i32 %inc, 1
150 br i1 %cmp, label %for.body, label %for.end