1 ; RUN: opt < %s -loop-unswitch -enable-new-pm=0 -verify-loop-info -verify-memoryssa -S < %s 2>&1 | FileCheck %s
3 @sink = global i32 0, align 4
4 @y = global i64 0, align 8
6 ; The following is approximately:
7 ; void f(bool x, int p, int q) {
8 ; volatile bool x2 = x;
9 ; for (int i = 0; i < 1; ++i) {
18 ; With MemorySanitizer, the loop can not be unswitched on "y", because "y" could
19 ; be uninitialized when x == false.
20 ; Test that the branch on "y" is inside the loop (after the first unconditional
23 define void @may_not_execute(i1 zeroext %x, i32 %p, i32 %q) sanitize_memory {
24 ; CHECK-LABEL: @may_not_execute(
26 ; CHECK: %[[Y:.*]] = load i64, i64* @y, align 8
27 ; CHECK: %[[YB:.*]] = icmp eq i64 %[[Y]], 0
30 ; CHECK: br i1 %[[YB]]
32 %x2 = alloca i8, align 1
33 %frombool1 = zext i1 %x to i8
34 store volatile i8 %frombool1, i8* %x2, align 1
35 %0 = load i64, i64* @y, align 8
36 %tobool3 = icmp eq i64 %0, 0
40 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
41 %x2.0. = load volatile i8, i8* %x2, align 1
42 %tobool2 = icmp eq i8 %x2.0., 0
43 br i1 %tobool2, label %for.inc, label %if.then
46 br i1 %tobool3, label %if.else, label %if.then4
49 store volatile i32 %p, i32* @sink, align 4
53 store volatile i32 %q, i32* @sink, align 4
57 %inc = add nsw i32 %i.01, 1
58 %cmp = icmp slt i32 %inc, 1
59 br i1 %cmp, label %for.body, label %for.end
66 ; The same as above, but "y" is a function parameter instead of a global.
67 ; This shows that it is not enough to suppress hoisting of load instructions,
68 ; the actual problem is in the speculative branching.
70 define void @may_not_execute2(i1 zeroext %x, i1 zeroext %y, i32 %p, i32 %q) sanitize_memory {
71 ; CHECK-LABEL: @may_not_execute2(
76 %x2 = alloca i8, align 1
77 %frombool2 = zext i1 %x to i8
78 store volatile i8 %frombool2, i8* %x2, align 1
82 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
83 %x2.0. = load volatile i8, i8* %x2, align 1
84 %tobool3 = icmp eq i8 %x2.0., 0
85 br i1 %tobool3, label %for.inc, label %if.then
88 br i1 %y, label %if.then5, label %if.else
91 store volatile i32 %p, i32* @sink, align 4
95 store volatile i32 %q, i32* @sink, align 4
99 %inc = add nsw i32 %i.01, 1
100 %cmp = icmp slt i32 %inc, 1
101 br i1 %cmp, label %for.body, label %for.end
108 ; The following is approximately:
109 ; void f(bool x, int p, int q) {
110 ; volatile bool x2 = x;
111 ; for (int i = 0; i < 1; ++i) {
118 ; "if (y)" is guaranteed to execute; the loop can be unswitched.
120 define void @must_execute(i1 zeroext %x, i32 %p, i32 %q) sanitize_memory {
121 ; CHECK-LABEL: @must_execute(
123 ; CHECK: %[[Y:.*]] = load i64, i64* @y, align 8
124 ; CHECK-NEXT: %[[YB:.*]] = icmp eq i64 %[[Y]], 0
125 ; CHECK-NEXT: br i1 %[[YB]],
127 %x2 = alloca i8, align 1
128 %frombool1 = zext i1 %x to i8
129 store volatile i8 %frombool1, i8* %x2, align 1
130 %0 = load i64, i64* @y, align 8
131 %tobool2 = icmp eq i64 %0, 0
135 %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
136 br i1 %tobool2, label %if.else, label %if.then
139 store volatile i32 %p, i32* @sink, align 4
143 store volatile i32 %q, i32* @sink, align 4
147 %inc = add nsw i32 %i.01, 1
148 %cmp = icmp slt i32 %inc, 1
149 br i1 %cmp, label %for.body, label %for.end