1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt -passes=newgvn -S %s | FileCheck %s 3 4@a = local_unnamed_addr global i32 9, align 4 5@.str4 = private unnamed_addr constant [6 x i8] c"D:%d\0A\00", align 1 6 7define i32 @test1() local_unnamed_addr { 8; CHECK-LABEL: @test1( 9; CHECK-NEXT: entry: 10; CHECK-NEXT: [[TMP:%.*]] = load i32, ptr @a, align 4 11; CHECK-NEXT: [[CMP1_I:%.*]] = icmp ne i32 [[TMP]], 0 12; CHECK-NEXT: br label [[FOR_BODY_I:%.*]] 13; CHECK: for.body.i: 14; CHECK-NEXT: [[TMP1:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ false, [[COND_END_I:%.*]] ] 15; CHECK-NEXT: [[F_08_I:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[INC_I:%.*]], [[COND_END_I]] ] 16; CHECK-NEXT: [[MUL_I:%.*]] = select i1 [[CMP1_I]], i32 [[F_08_I]], i32 0 17; CHECK-NEXT: br i1 [[TMP1]], label [[COND_END_I]], label [[COND_TRUE_I:%.*]] 18; CHECK: cond.true.i: 19; CHECK-NEXT: [[DIV_I:%.*]] = udiv i32 [[MUL_I]], [[F_08_I]] 20; CHECK-NEXT: br label [[COND_END_I]] 21; CHECK: cond.end.i: 22; CHECK-NEXT: [[COND_I:%.*]] = phi i32 [ [[DIV_I]], [[COND_TRUE_I]] ], [ 0, [[FOR_BODY_I]] ] 23; CHECK-NEXT: [[INC_I]] = add nuw nsw i32 [[F_08_I]], 1 24; CHECK-NEXT: [[EXITCOND_I:%.*]] = icmp eq i32 [[INC_I]], 4 25; CHECK-NEXT: br i1 [[EXITCOND_I]], label [[FN1_EXIT:%.*]], label [[FOR_BODY_I]] 26; CHECK: fn1.exit: 27; CHECK-NEXT: [[CALL4:%.*]] = tail call i32 (ptr, ...) @printf(ptr @.str4, i32 [[COND_I]]) 28; CHECK-NEXT: ret i32 0 29; 30entry: 31 %tmp = load i32, ptr @a, align 4 32 %cmp1.i = icmp ne i32 %tmp, 0 33 br label %for.body.i 34 35for.body.i: 36 %tmp1 = phi i1 [ true, %entry ], [ false, %cond.end.i ] 37 %f.08.i = phi i32 [ 0, %entry ], [ %inc.i, %cond.end.i ] 38 %mul.i = select i1 %cmp1.i, i32 %f.08.i, i32 0 39 br i1 %tmp1, label %cond.end.i, label %cond.true.i 40 41cond.true.i: 42 ;; Ensure we don't replace this divide with a phi of ops that merges the wrong loop iteration value 43 %div.i = udiv i32 %mul.i, %f.08.i 44 br label %cond.end.i 45 46cond.end.i: 47 %cond.i = phi i32 [ %div.i, %cond.true.i ], [ 0, %for.body.i ] 48 %inc.i = add nuw nsw i32 %f.08.i, 1 49 %exitcond.i = icmp eq i32 %inc.i, 4 50 br i1 %exitcond.i, label %fn1.exit, label %for.body.i 51 52fn1.exit: 53 %cond.i.lcssa = phi i32 [ %cond.i, %cond.end.i ] 54 %call4= tail call i32 (ptr, ...) @printf(ptr @.str4, i32 %cond.i.lcssa) 55 ret i32 0 56} 57 58declare i32 @printf(ptr nocapture readonly, ...) 59 60;; Variant of the above where we have made the udiv available in each predecessor with the wrong values. 61;; In the entry block, it is always 0, so we don't try to create a leader there, only in %cond.end.i. 62;; We should not create a phi of ops for it using these leaders. 63;; A correct phi of ops for this udiv would be phi(0, 1), which we are not smart enough to figure out. 64;; If we reuse the incorrect leaders, we will get phi(0, 0). 65define i32 @test2() local_unnamed_addr { 66; CHECK-LABEL: @test2( 67; CHECK-NEXT: entry: 68; CHECK-NEXT: [[TMP:%.*]] = load i32, ptr @a, align 4 69; CHECK-NEXT: [[CMP1_I:%.*]] = icmp ne i32 [[TMP]], 0 70; CHECK-NEXT: br label [[FOR_BODY_I:%.*]] 71; CHECK: for.body.i: 72; CHECK-NEXT: [[TMP1:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ false, [[COND_END_I:%.*]] ] 73; CHECK-NEXT: [[F_08_I:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[INC_I:%.*]], [[COND_END_I]] ] 74; CHECK-NEXT: [[MUL_I:%.*]] = select i1 [[CMP1_I]], i32 [[F_08_I]], i32 0 75; CHECK-NEXT: br i1 [[TMP1]], label [[COND_END_I]], label [[COND_TRUE_I:%.*]] 76; CHECK: cond.true.i: 77; CHECK-NEXT: [[DIV_I:%.*]] = udiv i32 [[MUL_I]], [[F_08_I]] 78; CHECK-NEXT: br label [[COND_END_I]] 79; CHECK: cond.end.i: 80; CHECK-NEXT: [[COND_I:%.*]] = phi i32 [ [[DIV_I]], [[COND_TRUE_I]] ], [ 0, [[FOR_BODY_I]] ] 81; CHECK-NEXT: [[INC_I]] = add nuw nsw i32 [[F_08_I]], 1 82; CHECK-NEXT: [[TEST:%.*]] = udiv i32 [[MUL_I]], [[INC_I]] 83; CHECK-NEXT: [[CALL5:%.*]] = tail call i32 (ptr, ...) @printf(ptr @.str4, i32 [[TEST]]) 84; CHECK-NEXT: [[EXITCOND_I:%.*]] = icmp eq i32 [[INC_I]], 4 85; CHECK-NEXT: br i1 [[EXITCOND_I]], label [[FN1_EXIT:%.*]], label [[FOR_BODY_I]] 86; CHECK: fn1.exit: 87; CHECK-NEXT: [[CALL4:%.*]] = tail call i32 (ptr, ...) @printf(ptr @.str4, i32 [[COND_I]]) 88; CHECK-NEXT: ret i32 0 89; 90entry: 91 %tmp = load i32, ptr @a, align 4 92 %cmp1.i = icmp ne i32 %tmp, 0 93 br label %for.body.i 94 95for.body.i: 96 %tmp1 = phi i1 [ true, %entry ], [ false, %cond.end.i ] 97 %f.08.i = phi i32 [ 0, %entry ], [ %inc.i, %cond.end.i ] 98 %mul.i = select i1 %cmp1.i, i32 %f.08.i, i32 0 99 br i1 %tmp1, label %cond.end.i, label %cond.true.i 100 101cond.true.i: 102 ;; Ensure we don't replace this divide with a phi of ops that merges the wrong loop iteration value 103 %div.i = udiv i32 %mul.i, %f.08.i 104 br label %cond.end.i 105 106cond.end.i: 107 %cond.i = phi i32 [ %div.i, %cond.true.i ], [ 0, %for.body.i ] 108 %inc.i = add nuw nsw i32 %f.08.i, 1 109 %test = udiv i32 %mul.i, %inc.i 110 %call5= tail call i32 (ptr, ...) @printf(ptr @.str4, i32 %test) 111 %exitcond.i = icmp eq i32 %inc.i, 4 112 br i1 %exitcond.i, label %fn1.exit, label %for.body.i 113 114fn1.exit: 115 %cond.i.lcssa = phi i32 [ %cond.i, %cond.end.i ] 116 %call4= tail call i32 (ptr, ...) @printf(ptr @.str4, i32 %cond.i.lcssa) 117 ret i32 0 118} 119 120 121