Index: llvm/lib/Analysis/ScalarEvolution.cpp =================================================================== --- llvm/lib/Analysis/ScalarEvolution.cpp +++ llvm/lib/Analysis/ScalarEvolution.cpp @@ -2233,22 +2233,35 @@ SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask); - if (SignOrUnsignWrap != SignOrUnsignMask && Type == scAddExpr && - Ops.size() == 2 && isa(Ops[0])) { - - // (A + C) --> (A + C) if the addition does not sign overflow - // (A + C) --> (A + C) if the addition does not unsign overflow + if (SignOrUnsignWrap != SignOrUnsignMask && + (Type == scAddExpr || Type == scMulExpr) && Ops.size() == 2 && + isa(Ops[0])) { + + auto Opcode = [&] { + switch (Type) { + case scAddExpr: + return Instruction::Add; + case scMulExpr: + return Instruction::Mul; + default: + llvm_unreachable("Unexpected SCEV op."); + } + }(); const APInt &C = cast(Ops[0])->getAPInt(); + + // (A C) --> (A C) if the op doesn't sign overflow. if (!(SignOrUnsignWrap & SCEV::FlagNSW)) { auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( - Instruction::Add, C, OBO::NoSignedWrap); + Opcode, C, OBO::NoSignedWrap); if (NSWRegion.contains(SE->getSignedRange(Ops[1]))) Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); } + + // (A C) --> (A C) if the op doesn't unsign overflow. if (!(SignOrUnsignWrap & SCEV::FlagNUW)) { auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( - Instruction::Add, C, OBO::NoUnsignedWrap); + Opcode, C, OBO::NoUnsignedWrap); if (NUWRegion.contains(SE->getUnsignedRange(Ops[1]))) Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); } Index: llvm/test/Analysis/Delinearization/a.ll =================================================================== --- llvm/test/Analysis/Delinearization/a.ll +++ llvm/test/Analysis/Delinearization/a.ll @@ -10,7 +10,7 @@ ; AddRec: {{{(28 + (4 * (-4 + (3 * %m)) * %o) + %A),+,(8 * %m * %o)}<%for.i>,+,(12 * %o)}<%for.j>,+,20}<%for.k> ; CHECK: Base offset: %A ; CHECK: ArrayDecl[UnknownSize][%m][%o] with elements of 4 bytes. -; CHECK: ArrayRef[{3,+,2}<%for.i>][{-4,+,3}<%for.j>][{7,+,5}<%for.k>] +; CHECK: ArrayRef[{3,+,2}<%for.i>][{-4,+,3}<%for.j>][{7,+,5}<%for.k>] define void @foo(i64 %n, i64 %m, i64 %o, i32* nocapture %A) #0 { entry: Index: llvm/test/Analysis/Delinearization/iv_times_constant_in_subscript.ll =================================================================== --- llvm/test/Analysis/Delinearization/iv_times_constant_in_subscript.ll +++ llvm/test/Analysis/Delinearization/iv_times_constant_in_subscript.ll @@ -11,7 +11,7 @@ ; AddRec: {{((%m * %b * 8) + %A),+,(2 * %m * 8)}<%for.i>,+,(2 * 8)}<%for.j> ; CHECK: Base offset: %A ; CHECK: ArrayDecl[UnknownSize][%m] with elements of 8 bytes. -; CHECK: ArrayRef[{%b,+,2}<%for.i>][{0,+,2}<%for.j>] +; CHECK: ArrayRef[{%b,+,2}<%for.i>][{0,+,2}<%for.j>] define void @foo(i64 %n, i64 %m, i64 %b, double* %A) { Index: llvm/test/Analysis/IVUsers/quadradic-exit-value.ll =================================================================== --- llvm/test/Analysis/IVUsers/quadradic-exit-value.ll +++ llvm/test/Analysis/IVUsers/quadradic-exit-value.ll @@ -70,7 +70,7 @@ ; sure they aren't marked as post-inc users. ; ; CHECK-LABEL: IV Users for loop %test2.loop -; CHECK-NO-LCSSA: %sub.cond.us = ((-1 * %sub.us) + {0,+,1}<%test2.loop>) (post-inc with loop %test2.loop) in %sext.us = mul i32 %mul.us, %sub.cond.us +; CHECK-NO-LCSSA: %sub.cond.us = ((-1 * %sub.us) + {0,+,1}<%test2.loop>) (post-inc with loop %test2.loop) in %sext.us = mul i32 %mul.us, %sub.cond.us define i32 @test2() { entry: br label %test2.loop Index: llvm/test/Analysis/LoopAccessAnalysis/number-of-memchecks.ll =================================================================== --- llvm/test/Analysis/LoopAccessAnalysis/number-of-memchecks.ll +++ llvm/test/Analysis/LoopAccessAnalysis/number-of-memchecks.ll @@ -251,10 +251,10 @@ ; CHECK-NEXT: Member: {((2 * %offset) + %a),+,2}<%for.body> ; CHECK-NEXT: Group {{.*}}[[ONE]]: ; CHECK-NEXT: (Low: %a High: (10000 + %a)) -; CHECK-NEXT: Member: {%a,+,2}<%for.body> +; CHECK-NEXT: Member: {%a,+,2}<%for.body> ; CHECK-NEXT: Group {{.*}}[[TWO]]: ; CHECK-NEXT: (Low: (20000 + %a) High: (30000 + %a)) -; CHECK-NEXT: Member: {(20000 + %a),+,2}<%for.body> +; CHECK-NEXT: Member: {(20000 + %a),+,2}<%for.body> define void @testi(i16* %a, i64 %offset) { Index: llvm/test/Analysis/LoopAccessAnalysis/wrapping-pointer-versioning.ll =================================================================== --- llvm/test/Analysis/LoopAccessAnalysis/wrapping-pointer-versioning.ll +++ llvm/test/Analysis/LoopAccessAnalysis/wrapping-pointer-versioning.ll @@ -33,7 +33,7 @@ ; i64 {0,+,2}<%for.body> ; LAA: [PSE] %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext: -; LAA-NEXT: ((2 * (zext i32 {0,+,2}<%for.body> to i64)) + %a) +; LAA-NEXT: ((2 * (zext i32 {0,+,2}<%for.body> to i64)) + %a) ; LAA-NEXT: --> {%a,+,4}<%for.body> @@ -130,7 +130,7 @@ ; i64 {zext i32 (2 * (trunc i64 %N to i32)) to i64,+,-2}<%for.body> ; LAA: [PSE] %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext: -; LAA-NEXT: ((2 * (zext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64)) + %a) +; LAA-NEXT: ((2 * (zext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64)) + %a) ; LAA-NEXT: --> {((4 * (zext i31 (trunc i64 %N to i31) to i64)) + %a),+,-4}<%for.body> ; LV-LABEL: f2 @@ -210,7 +210,7 @@ ; i64 {0,+,2}<%for.body> ; LAA: [PSE] %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext: -; LAA-NEXT: ((2 * (sext i32 {0,+,2}<%for.body> to i64)) + %a) +; LAA-NEXT: ((2 * (sext i32 {0,+,2}<%for.body> to i64)) + %a) ; LAA-NEXT: --> {%a,+,4}<%for.body> ; LV-LABEL: f3 @@ -278,7 +278,7 @@ ; LAA: Memory dependences are safe{{$}} ; LAA: SCEV assumptions: ; LAA-NEXT: {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> Added Flags: -; LAA-NEXT: {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64)) + %a),+,-4}<%for.body> Added Flags: +; LAA-NEXT: {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64)) + %a),+,-4}<%for.body> Added Flags: ; The expression for %mul_ext as analyzed by SCEV is ; i64 (sext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64) @@ -286,8 +286,8 @@ ; i64 {sext i32 (2 * (trunc i64 %N to i32)) to i64,+,-2}<%for.body> ; LAA: [PSE] %arrayidxA = getelementptr i16, i16* %a, i64 %mul_ext: -; LAA-NEXT: ((2 * (sext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64)) + %a) -; LAA-NEXT: --> {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64)) + %a),+,-4}<%for.body> +; LAA-NEXT: ((2 * (sext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64)) + %a) +; LAA-NEXT: --> {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64)) + %a),+,-4}<%for.body> ; LV-LABEL: f4 ; LV-LABEL: for.body.lver.check @@ -362,11 +362,11 @@ ; LAA: Memory dependences are safe{{$}} ; LAA: SCEV assumptions: ; LAA-NEXT: {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> Added Flags: -; LAA-NEXT: {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64)) + %a),+,-4}<%for.body> Added Flags: +; LAA-NEXT: {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64)) + %a),+,-4}<%for.body> Added Flags: ; LAA: [PSE] %arrayidxA = getelementptr inbounds i16, i16* %a, i32 %mul: ; LAA-NEXT: ((2 * (sext i32 {(2 * (trunc i64 %N to i32)),+,-2}<%for.body> to i64)) + %a) -; LAA-NEXT: --> {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64)) + %a),+,-4}<%for.body> +; LAA-NEXT: --> {((2 * (sext i32 (2 * (trunc i64 %N to i32)) to i64)) + %a),+,-4}<%for.body> ; LV-LABEL: f5 ; LV-LABEL: for.body.lver.check Index: llvm/test/Analysis/ScalarEvolution/different-loops-recs.ll =================================================================== --- llvm/test/Analysis/ScalarEvolution/different-loops-recs.ll +++ llvm/test/Analysis/ScalarEvolution/different-loops-recs.ll @@ -322,7 +322,7 @@ ; CHECK: %SQ = mul i32 %i.0, %i.0 ; CHECK-NEXT: --> {4,+,5,+,2}<%bb3> ; CHECK: %tmp4 = mul i32 %i.0, 2 -; CHECK-NEXT: --> {4,+,2}<%bb3> +; CHECK-NEXT: --> {4,+,2}<%bb3> ; CHECK: %tmp5 = sub i32 %SQ, %tmp4 ; CHECK-NEXT: --> {0,+,3,+,2}<%bb3> Index: llvm/test/Analysis/ScalarEvolution/nsw-offset-assume.ll =================================================================== --- llvm/test/Analysis/ScalarEvolution/nsw-offset-assume.ll +++ llvm/test/Analysis/ScalarEvolution/nsw-offset-assume.ll @@ -79,5 +79,5 @@ ; Note: Without the preheader assume, there is an 'smax' in the ; backedge-taken count expression: -; CHECK: Loop %bb: backedge-taken count is ((-1 + (2 * (%no /u 2))) /u 2) +; CHECK: Loop %bb: backedge-taken count is ((-1 + (2 * (%no /u 2))) /u 2) ; CHECK: Loop %bb: max backedge-taken count is 1073741822 Index: llvm/test/Analysis/ScalarEvolution/nsw-offset.ll =================================================================== --- llvm/test/Analysis/ScalarEvolution/nsw-offset.ll +++ llvm/test/Analysis/ScalarEvolution/nsw-offset.ll @@ -73,5 +73,5 @@ ret void } -; CHECK: Loop %bb: backedge-taken count is ((-1 + (2 * (%no /u 2))) /u 2) +; CHECK: Loop %bb: backedge-taken count is ((-1 + (2 * (%no /u 2))) /u 2) ; CHECK: Loop %bb: max backedge-taken count is 1073741822 Index: llvm/test/Analysis/ScalarEvolution/nsw.ll =================================================================== --- llvm/test/Analysis/ScalarEvolution/nsw.ll +++ llvm/test/Analysis/ScalarEvolution/nsw.ll @@ -126,7 +126,7 @@ } ; CHECK-LABEL: PR12375 -; CHECK: --> {(4 + %arg),+,4}<%bb1>{{ U: [^ ]+ S: [^ ]+}}{{ *}}Exits: (4 + (4 * ((-1 + (-1 * %arg) + ((4 + %arg) umax (8 + %arg))) /u 4)) + %arg) +; CHECK: --> {(4 + %arg),+,4}<%bb1>{{ U: [^ ]+ S: [^ ]+}}{{ *}}Exits: (4 + (4 * ((-1 + (-1 * %arg) + ((4 + %arg) umax (8 + %arg))) /u 4)) + %arg) define i32 @PR12375(i32* readnone %arg) { bb: %tmp = getelementptr inbounds i32, i32* %arg, i64 2 @@ -145,7 +145,7 @@ } ; CHECK-LABEL: PR12376 -; CHECK: --> {(4 + %arg),+,4}<%bb2>{{ U: [^ ]+ S: [^ ]+}}{{ *}}Exits: (4 + (4 * ((-1 + (-1 * %arg) + ((4 + %arg) umax %arg1)) /u 4)) + %arg) +; CHECK: --> {(4 + %arg),+,4}<%bb2>{{ U: [^ ]+ S: [^ ]+}}{{ *}}Exits: (4 + (4 * ((-1 + (-1 * %arg) + ((4 + %arg) umax %arg1)) /u 4)) + %arg) define void @PR12376(i32* nocapture %arg, i32* nocapture %arg1) { bb: br label %bb2 Index: llvm/test/Analysis/ScalarEvolution/predicated-trip-count.ll =================================================================== --- llvm/test/Analysis/ScalarEvolution/predicated-trip-count.ll +++ llvm/test/Analysis/ScalarEvolution/predicated-trip-count.ll @@ -80,7 +80,7 @@ ; CHECK-NEXT: --> (sext i16 {%Start,+,-1}<%bb3> to i32) ; CHECK: Loop %bb3: Unpredictable backedge-taken count. ; CHECK-NEXT: Loop %bb3: Unpredictable max backedge-taken count. -; CHECK-NEXT: Loop %bb3: Predicated backedge-taken count is (2 + (sext i16 %Start to i32) + ((-2 + (-1 * (sext i16 %Start to i32))) smax (-1 + (-1 * %M)))) +; CHECK-NEXT: Loop %bb3: Predicated backedge-taken count is (2 + (sext i16 %Start to i32) + ((-2 + (-1 * (sext i16 %Start to i32))) smax (-1 + (-1 * %M)))) ; CHECK-NEXT: Predicates: ; CHECK-NEXT: {%Start,+,-1}<%bb3> Added Flags: Index: llvm/test/Analysis/ScalarEvolution/sext-mul.ll =================================================================== --- llvm/test/Analysis/ScalarEvolution/sext-mul.ll +++ llvm/test/Analysis/ScalarEvolution/sext-mul.ll @@ -5,11 +5,11 @@ ; CHECK: %tmp10 = ashr exact i64 %tmp9, 32 ; CHECK-NEXT: --> {{.*}} Exits: (sext i32 (-2 + (2 * %arg2)) to i64) ; CHECK: %tmp11 = getelementptr inbounds i32, i32* %arg, i64 %tmp10 -; CHECK-NEXT: --> {{.*}} Exits: ((4 * (sext i32 (-2 + (2 * %arg2)) to i64)) + %arg) +; CHECK-NEXT: --> {{.*}} Exits: ((4 * (sext i32 (-2 + (2 * %arg2)) to i64)) + %arg) ; CHECK: %tmp14 = or i64 %tmp10, 1 ; CHECK-NEXT: --> {{.*}} Exits: (1 + (sext i32 (-2 + (2 * %arg2)) to i64)) ; CHECK: %tmp15 = getelementptr inbounds i32, i32* %arg, i64 %tmp14 -; CHECK-NEXT: --> {{.*}} Exits: (4 + (4 * (sext i32 (-2 + (2 * %arg2)) to i64)) + %arg) +; CHECK-NEXT: --> {{.*}} Exits: (4 + (4 * (sext i32 (-2 + (2 * %arg2)) to i64)) + %arg) ; CHECK:Loop %bb7: backedge-taken count is (-1 + (zext i32 %arg2 to i64)) ; CHECK-NEXT:Loop %bb7: max backedge-taken count is -1 ; CHECK-NEXT:Loop %bb7: Predicated backedge-taken count is (-1 + (zext i32 %arg2 to i64)) Index: llvm/test/Analysis/ScalarEvolution/trip-count-pow2.ll =================================================================== --- llvm/test/Analysis/ScalarEvolution/trip-count-pow2.ll +++ llvm/test/Analysis/ScalarEvolution/trip-count-pow2.ll @@ -31,7 +31,7 @@ ret i32 %i ; CHECK-LABEL: @test2 -; CHECK: Loop %loop: backedge-taken count is ((-32 + (32 * (%n /u 32))) /u 32) +; CHECK: Loop %loop: backedge-taken count is ((-32 + (32 * (%n /u 32))) /u 32) ; CHECK: Loop %loop: max backedge-taken count is 134217727 } Index: llvm/test/Analysis/ScalarEvolution/zext-mul.ll =================================================================== --- llvm/test/Analysis/ScalarEvolution/zext-mul.ll +++ llvm/test/Analysis/ScalarEvolution/zext-mul.ll @@ -19,16 +19,20 @@ } ; CHECK-LABEL: @range -; -; This had to be disabled when r334428 was reverted. We should enable this test -; when r334428 is reapplied with a fix. define void @range() { - %a = call i32 @get_int(), !range !0 + %a = call i32 @get_int(), !range !{i32 0, i32 100} %b = mul i32 %a, 4 %c = zext i32 %b to i64 ; CHECK: %c - ; CHECK-NEXT: --> (zext i32 (4 * %a) to i64) + ; CHECK-NEXT: --> (4 * (zext i32 %a to i64)) ret void } -!0 = !{i32 0, i32 100} +; CHECK-LABEL: @no_nuw +define void @no_nuw() { + %a = call i32 @get_int(), !range !{i32 0, i32 3} + %b = mul i32 %a, -100 + ; CHECK: %b + ; CHECK-NEXT: --> (-100 * %a) + ret void +} Index: llvm/test/Transforms/LoopVersioning/incorrect-phi.ll =================================================================== --- llvm/test/Transforms/LoopVersioning/incorrect-phi.ll +++ llvm/test/Transforms/LoopVersioning/incorrect-phi.ll @@ -14,18 +14,17 @@ br label %bb6 bb6: ; preds = %bb6.lr.ph, %bb6 - %_tmp1423 = phi i16 [ undef, %bb6.lr.ph ], [ %_tmp142, %bb6 ] + %_tmp1423 = phi i64 [ undef, %bb6.lr.ph ], [ %_tmp142, %bb6 ] %_tmp123 = getelementptr [2 x [3 x [5 x i16]]], [2 x [3 x [5 x i16]]]* @x, i16 0, i64 undef - %_tmp125 = sext i16 %_tmp1423 to i64 - %_tmp126 = getelementptr [3 x [5 x i16]], [3 x [5 x i16]]* %_tmp123, i16 0, i64 %_tmp125 + %_tmp126 = getelementptr [3 x [5 x i16]], [3 x [5 x i16]]* %_tmp123, i16 0, i64 %_tmp1423 %_tmp129 = getelementptr [5 x i16], [5 x i16]* %_tmp126, i16 0, i64 undef %_tmp130 = load i16, i16* %_tmp129 store i16 undef, i16* getelementptr ([2 x [3 x [5 x i16]]], [2 x [3 x [5 x i16]]]* @x, i64 0, i64 undef, i64 undef, i64 undef) - %_tmp142 = add i16 %_tmp1423, 1 + %_tmp142 = add i64 %_tmp1423, 1 br i1 false, label %bb6, label %loop.exit loop.exit: ; preds = %bb6 - %_tmp142.lcssa = phi i16 [ %_tmp142, %bb6 ] + %_tmp142.lcssa = phi i64 [ %_tmp142, %bb6 ] %split = phi i16 [ undef, %bb6 ] ; CHECK: %split = phi i16 [ undef, %bb6 ], [ undef, %bb6.lver.orig ] br label %bb9 @@ -41,18 +40,17 @@ br label %bb6 bb6: ; preds = %bb6.lr.ph, %bb6 - %_tmp1423 = phi i16 [ undef, %bb6.lr.ph ], [ %_tmp142, %bb6 ] + %_tmp1423 = phi i64 [ undef, %bb6.lr.ph ], [ %_tmp142, %bb6 ] %_tmp123 = getelementptr [2 x [3 x [5 x i16]]], [2 x [3 x [5 x i16]]]* @x, i16 0, i64 undef - %_tmp125 = sext i16 %_tmp1423 to i64 - %_tmp126 = getelementptr [3 x [5 x i16]], [3 x [5 x i16]]* %_tmp123, i16 0, i64 %_tmp125 + %_tmp126 = getelementptr [3 x [5 x i16]], [3 x [5 x i16]]* %_tmp123, i16 0, i64 %_tmp1423 %_tmp129 = getelementptr [5 x i16], [5 x i16]* %_tmp126, i16 0, i64 undef %_tmp130 = load i16, i16* %_tmp129 store i16 undef, i16* getelementptr ([2 x [3 x [5 x i16]]], [2 x [3 x [5 x i16]]]* @x, i64 0, i64 undef, i64 undef, i64 undef) - %_tmp142 = add i16 %_tmp1423, 1 + %_tmp142 = add i64 %_tmp1423, 1 br i1 false, label %bb6, label %loop.exit loop.exit: ; preds = %bb6 - %_tmp142.lcssa = phi i16 [ %_tmp142, %bb6 ] + %_tmp142.lcssa = phi i64 [ %_tmp142, %bb6 ] %split = phi i16 [ %t, %bb6 ] ; CHECK: %split = phi i16 [ %t, %bb6 ], [ %t, %bb6.lver.orig ] br label %bb9