1 ; RUN: opt -mtriple=amdgcn-- -O3 -S %s | FileCheck %s
3 ; Check that loop unswitch happened and condition hoisted out of the loop.
4 ; Condition is uniform so all targets should perform unswitching.
6 ; CHECK-LABEL: {{^}}define amdgpu_kernel void @uniform_unswitch
8 ; CHECK-NEXT: [[LOOP_COND:%[a-z0-9]+]] = icmp
9 ; CHECK-NEXT: [[IF_COND:%[a-z0-9]+]] = icmp eq i32 %x, 123456
10 ; CHECK-NEXT: and i1 [[LOOP_COND]], [[IF_COND]]
13 define amdgpu_kernel void @uniform_unswitch(i32 * nocapture %out, i32 %n, i32 %x) {
15 %cmp6 = icmp sgt i32 %n, 0
16 br i1 %cmp6, label %for.body.lr.ph, label %for.cond.cleanup
18 for.body.lr.ph: ; preds = %entry
19 %cmp1 = icmp eq i32 %x, 123456
22 for.cond.cleanup.loopexit: ; preds = %for.inc
23 br label %for.cond.cleanup
25 for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry
28 for.body: ; preds = %for.inc, %for.body.lr.ph
29 %i.07 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.inc ]
30 br i1 %cmp1, label %if.then, label %for.inc
32 if.then: ; preds = %for.body
33 %arrayidx = getelementptr inbounds i32, i32 * %out, i32 %i.07
34 store i32 %i.07, i32 * %arrayidx, align 4
37 for.inc: ; preds = %for.body, %if.then
38 %inc = add nuw nsw i32 %i.07, 1
39 %exitcond = icmp eq i32 %inc, %n
40 br i1 %exitcond, label %for.cond.cleanup.loopexit, label %for.body
43 ; Check that loop unswitch does not happen if condition is divergent.
45 ; CHECK-LABEL: {{^}}define amdgpu_kernel void @divergent_unswitch
48 ; CHECK: [[IF_COND:%[a-z0-9]+]] = icmp {{.*}} 567890
50 ; CHECK: br i1 [[IF_COND]]
52 define amdgpu_kernel void @divergent_unswitch(i32 * nocapture %out, i32 %n) {
54 %cmp9 = icmp sgt i32 %n, 0
55 br i1 %cmp9, label %for.body.lr.ph, label %for.cond.cleanup
57 for.body.lr.ph: ; preds = %entry
58 %call = tail call i32 @llvm.amdgcn.workitem.id.x() #0
59 %cmp2 = icmp eq i32 %call, 567890
62 for.cond.cleanup.loopexit: ; preds = %for.inc
63 br label %for.cond.cleanup
65 for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry
68 for.body: ; preds = %for.inc, %for.body.lr.ph
69 %i.010 = phi i32 [ 0, %for.body.lr.ph ], [ %inc, %for.inc ]
70 br i1 %cmp2, label %if.then, label %for.inc
72 if.then: ; preds = %for.body
73 %arrayidx = getelementptr inbounds i32, i32 * %out, i32 %i.010
74 store i32 %i.010, i32 * %arrayidx, align 4
77 for.inc: ; preds = %for.body, %if.then
78 %inc = add nuw nsw i32 %i.010, 1
79 %exitcond = icmp eq i32 %inc, %n
80 br i1 %exitcond, label %for.cond.cleanup.loopexit, label %for.body
83 declare i32 @llvm.amdgcn.workitem.id.x() #0
85 attributes #0 = { nounwind readnone }