1; RUN: opt -scalar-evolution-max-arith-depth=0 -scalar-evolution-max-cast-depth=0 -disable-output "-passes=print<scalar-evolution>" < %s 2>&1 | FileCheck %s 2 3; Check that depth set to 0 prevents getAddExpr and getMulExpr from making 4; transformations in SCEV. We expect the result to be very straightforward. 5 6define void @test_add(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) { 7; CHECK-LABEL: @test_add 8; CHECK: %s2 = add i32 %s1, %p3 9; CHECK-NEXT: --> (%a + %a + %b + %b + %c + %c + %d + %d + %e + %e + %f + %f) 10 %tmp0 = add i32 %a, %b 11 %tmp1 = add i32 %b, %c 12 %tmp2 = add i32 %c, %d 13 %tmp3 = add i32 %d, %e 14 %tmp4 = add i32 %e, %f 15 %tmp5 = add i32 %f, %a 16 17 %p1 = add i32 %tmp0, %tmp3 18 %p2 = add i32 %tmp1, %tmp4 19 %p3 = add i32 %tmp2, %tmp5 20 21 %s1 = add i32 %p1, %p2 22 %s2 = add i32 %s1, %p3 23 ret void 24} 25 26; Constant factors still get folded together. 27define void @test_mul_consts(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) { 28; CHECK-LABEL: @test_mul_consts 29; CHECK: %s2 = mul i32 %s1, %p3 30; CHECK-NEXT: --> (5040 * %a * %b * %c * %d * %e * %f) 31 %tmp0 = mul i32 %a, 2 32 %tmp1 = mul i32 %b, 3 33 %tmp2 = mul i32 %c, 4 34 %tmp3 = mul i32 %d, 5 35 %tmp4 = mul i32 %e, 6 36 %tmp5 = mul i32 %f, 7 37 38 %p1 = mul i32 %tmp0, %tmp3 39 %p2 = mul i32 %tmp1, %tmp4 40 %p3 = mul i32 %tmp2, %tmp5 41 42 %s1 = mul i32 %p1, %p2 43 %s2 = mul i32 %s1, %p3 44 ret void 45} 46 47; The outer *5 gets distributed because it is at depth=0, but the resulting 48; nested multiply doesn't get flattened, because it is at depth=1. 49define void @test_mul(i32 %a, i32 %b) { 50; CHECK-LABEL: @test_mul 51; CHECK: %tmp2 = mul i32 %tmp1, 5 52; CHECK-NEXT: --> (20 + (5 * (3 * %a))) 53 %tmp0 = mul i32 %a, 3 54 %tmp1 = add i32 %tmp0, 4 55 %tmp2 = mul i32 %tmp1, 5 56 ret void 57} 58 59define void @test_sext(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) { 60; CHECK-LABEL: @test_sext 61; CHECK: %se2 = sext i64 %iv2.inc to i128 62; CHECK-NEXT: --> {(1 + (sext i64 {(sext i32 (1 + %a)<nsw> to i64),+,1}<nsw><%loop> to i128))<nsw>,+,1}<nsw><%loop2> 63entry: 64 br label %loop 65 66loop: 67 %iv = phi i32 [ %a, %entry ], [ %iv.inc, %loop ] 68 %iv.inc = add nsw i32 %iv, 1 69 %cond = icmp sle i32 %iv.inc, 50 70 br i1 %cond, label %loop, label %between 71 72between: 73 %se = sext i32 %iv.inc to i64 74 br label %loop2 75 76loop2: 77 %iv2 = phi i64 [ %se, %between ], [ %iv2.inc, %loop2 ] 78 %iv2.inc = add nsw i64 %iv2, 1 79 %cond2 = icmp sle i64 %iv2.inc, 50 80 br i1 %cond2, label %loop2, label %exit 81 82exit: 83 %se2 = sext i64 %iv2.inc to i128 84 ret void 85} 86 87define void @test_zext(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) { 88; CHECK-LABEL: @test_zext 89; CHECK: %ze2 = zext i64 %iv2.inc to i128 90; CHECK-NEXT: --> {(1 + (zext i64 {7,+,1}<nuw><nsw><%loop> to i128))<nuw><nsw>,+,1}<nuw><nsw><%loop2> 91entry: 92 br label %loop 93 94loop: 95 %iv = phi i32 [ 6, %entry ], [ %iv.inc, %loop ] 96 %iv.inc = add nsw i32 %iv, 1 97 %cond = icmp sle i32 %iv.inc, 50 98 br i1 %cond, label %loop, label %between 99 100between: 101 %ze = zext i32 %iv.inc to i64 102 br label %loop2 103 104loop2: 105 %iv2 = phi i64 [ %ze, %between ], [ %iv2.inc, %loop2 ] 106 %iv2.inc = add nuw i64 %iv2, 1 107 %cond2 = icmp sle i64 %iv2.inc, 50 108 br i1 %cond2, label %loop2, label %exit 109 110exit: 111 %ze2 = zext i64 %iv2.inc to i128 112 ret void 113} 114 115define void @test_trunc(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e, i32 %f) { 116; CHECK-LABEL: @test_trunc 117; CHECK: %trunc2 = trunc i64 %iv2.inc to i32 118; CHECK-NEXT: --> {(trunc i64 (1 + {7,+,1}<%loop>)<nuw><nsw> to i32),+,1}<%loop2> U: [8,53) S: [8,53) --> 52 U: [52,53) S: [52,53) 119entry: 120 br label %loop 121 122loop: 123 %iv = phi i128 [ 6, %entry ], [ %iv.inc, %loop ] 124 %iv.inc = add nsw i128 %iv, 1 125 %cond = icmp sle i128 %iv.inc, 50 126 br i1 %cond, label %loop, label %between 127 128between: 129 %trunc = trunc i128 %iv.inc to i64 130 br label %loop2 131 132loop2: 133 %iv2 = phi i64 [ %trunc, %between ], [ %iv2.inc, %loop2 ] 134 %iv2.inc = add nuw i64 %iv2, 1 135 %cond2 = icmp sle i64 %iv2.inc, 50 136 br i1 %cond2, label %loop2, label %exit 137 138exit: 139 %trunc2 = trunc i64 %iv2.inc to i32 140 ret void 141} 142 143; Check that all constant SCEVs are folded regardless depth limit. 144define void @test_mul_const(i32 %a) { 145; CHECK-LABEL: @test_mul_const 146; CHECK: %test3 = mul i32 %test2, 3 147; CHECK-NEXT: --> (9 + (3 * (3 * %a))) 148; CHECK: %test4 = mul i32 3, 3 149; CHECK-NEXT: --> 9 U: [9,10) S: [9,10) 150 %test = mul i32 3, %a 151 %test2 = add i32 3, %test 152 %test3 = mul i32 %test2, 3 153 %test4 = mul i32 3, 3 154 ret void 155} 156