Index: lib/Analysis/ValueTracking.cpp =================================================================== --- lib/Analysis/ValueTracking.cpp +++ lib/Analysis/ValueTracking.cpp @@ -780,6 +780,26 @@ } } +// Compute known sign bit from a shift operator, based on the known sign bit of +// its first operand and the nsw flag. KnownZero2 and KnownOne2 are the computed +// known bits of its first operand. +static void computeKnownSignBitFromShift(Operator *I, + APInt &KnownZero, APInt &KnownOne, + APInt &KnownZero2, APInt &KnownOne2) { + // If this is a left shift with nsw, then the shift produces a poison value + // if it shifts out any bits that disagree with the resultant sign bit. + // This means the result is either a poison value or has the same sign bit + // as the first operand. + unsigned BitWidth = KnownZero.getBitWidth(); + if (I->getOpcode() == Instruction::Shl && + cast(I)->hasNoSignedWrap()) { + if (KnownZero2.isNegative()) + KnownZero |= APInt::getSignBit(BitWidth); + else if (KnownOne2.isNegative()) + KnownOne |= APInt::getSignBit(BitWidth); + } +} + // Compute known bits from a shift operator, including those with a // non-constant shift amount. KnownZero and KnownOne are the outputs of this // function. KnownZero2 and KnownOne2 are pre-allocated temporaries with the @@ -798,9 +818,12 @@ if (auto *SA = dyn_cast(I->getOperand(1))) { unsigned ShiftAmt = SA->getLimitedValue(BitWidth-1); - computeKnownBits(I->getOperand(0), KnownZero, KnownOne, Depth + 1, Q); - KnownZero = KZF(KnownZero, ShiftAmt); - KnownOne = KOF(KnownOne, ShiftAmt); + computeKnownBits(I->getOperand(0), KnownZero2, KnownOne2, Depth + 1, Q); + KnownZero = KZF(KnownZero2, ShiftAmt); + KnownOne = KOF(KnownOne2, ShiftAmt); + + computeKnownSignBitFromShift(I, KnownZero, KnownOne, KnownZero2, KnownOne2); + return; } @@ -855,6 +878,8 @@ KnownOne &= KOF(KnownOne2, ShiftAmt); } + computeKnownSignBitFromShift(I, KnownZero, KnownOne, KnownZero2, KnownOne2); + // If there are no compatible shift amounts, then we've proven that the shift // amount must be >= the BitWidth, and the result is undefined. We could // return anything we'd like, but we need to make sure the sets of known bits @@ -1215,7 +1240,9 @@ unsigned Opcode = LU->getOpcode(); // Check for operations that have the property that if // both their operands have low zero bits, the result - // will have low zero bits. + // will have low zero bits. Also check for operations + // that are known to produce non-negative or negative + // recurrence values. if (Opcode == Instruction::Add || Opcode == Instruction::Sub || Opcode == Instruction::And || @@ -1241,6 +1268,40 @@ KnownZero = APInt::getLowBitsSet(BitWidth, std::min(KnownZero2.countTrailingOnes(), KnownZero3.countTrailingOnes())); + + auto OverflowOp = dyn_cast(LU); + if (OverflowOp && OverflowOp->hasNoSignedWrap()) { + // If Initial value of recurrence is nonnegative, and we are adding + // a nonnegative number with nsw, the result can only be nonnegative + // or poison value regardless of the number of times we execute the + // add in phi recurrence. If initial value is negative and we are + // adding a negative number with nsw, the result can only be + // negative or poison value. Similar arguments apply to sub and mul. + // + // (add non-negative, non-negative) --> non-negative + // (add negative, negative) --> negative + if (Opcode == Instruction::Add) { + if (KnownZero2.isNegative() && KnownZero3.isNegative()) + KnownZero |= APInt::getSignBit(BitWidth); + else if (KnownOne2.isNegative() && KnownOne3.isNegative()) + KnownOne |= APInt::getSignBit(BitWidth); + } + + // (sub nsw non-negative, negative) --> non-negative + // (sub nsw negative, non-negative) --> negative + if (Opcode == Instruction::Sub && LL == I) { + if (KnownZero2.isNegative() && KnownOne3.isNegative()) + KnownZero |= APInt::getSignBit(BitWidth); + else if (KnownOne2.isNegative() && KnownZero3.isNegative()) + KnownOne |= APInt::getSignBit(BitWidth); + } + + // (mul nsw non-negative, non-negative) --> non-negative + if (Opcode == Instruction::Mul && KnownZero2.isNegative() && + KnownZero3.isNegative()) + KnownZero |= APInt::getSignBit(BitWidth); + } + break; } } Index: test/Analysis/ValueTracking/known-non-negative.ll =================================================================== --- test/Analysis/ValueTracking/known-non-negative.ll +++ test/Analysis/ValueTracking/known-non-negative.ll @@ -0,0 +1,62 @@ +; RUN: opt < %s -instcombine -S | FileCheck %s + +; Induction variable is known to be non-negative +define i32 @test_indvar_nonnegative_add() { +; CHECK-LABEL: @test_indvar_nonnegative_add( +; CHECK: br i1 true, label %for.end, label %for.body +entry: + br label %for.body + +for.body: + %i = phi i32 [0, %entry], [%inc, %for.body] + %inc = add nsw i32 %i, 1 + %cmp = icmp sge i32 %i, 0 + br i1 %cmp, label %for.end, label %for.body + +for.end: + ret i32 %i +} + +define i32 @test_indvar_nonnegative_mul() { +; CHECK-LABEL: @test_indvar_nonnegative_mul( +; CHECK: br i1 true, label %for.end, label %for.body +entry: + br label %for.body + +for.body: + %i = phi i32 [1, %entry], [%inc, %for.body] + %inc = mul nsw i32 %i, 3 + %cmp = icmp sge i32 %i, 0 + br i1 %cmp, label %for.end, label %for.body + +for.end: + ret i32 %i +} + +define i32 @test_indvar_nonnegative_sub(i32 %a) { +; CHECK-LABEL: @test_indvar_nonnegative_sub( +; CHECK: br i1 true, label %for.end, label %for.body +entry: + br label %for.body + +for.body: + %i = phi i32 [0, %entry], [%inc, %for.body] + %b = or i32 %a, -2147483648 + %inc = sub nsw i32 %i, %b + %cmp = icmp sge i32 %i, 0 + br i1 %cmp, label %for.end, label %for.body + +for.end: + ret i32 %i +} + +; Result of left shifting a non-negative integer +; with nsw flag should also be non-negative +define i1 @test_shift_nonnegative(i32 %a) { +; CHECK-LABEL: @test_shift_nonnegative( +; CHECK: ret i1 true + %b = lshr i32 %a, 2 + %shift = shl nsw i32 %b, 3 + %cmp = icmp sge i32 %shift, 0 + ret i1 %cmp +} Index: test/Transforms/BBVectorize/loop1.ll =================================================================== --- test/Transforms/BBVectorize/loop1.ll +++ test/Transforms/BBVectorize/loop1.ll @@ -83,7 +83,7 @@ ; CHECK-UNRL: %add12 = fadd <2 x double> %add7, %mul11 ; CHECK-UNRL: %4 = bitcast double* %arrayidx14 to <2 x double>* ; CHECK-UNRL: store <2 x double> %add12, <2 x double>* %4, align 8 -; CHECK-UNRL: %indvars.iv.next.1 = add nsw i64 %indvars.iv, 2 +; CHECK-UNRL: %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2 ; CHECK-UNRL: %lftr.wideiv.1 = trunc i64 %indvars.iv.next.1 to i32 ; CHECK-UNRL: %exitcond.1 = icmp eq i32 %lftr.wideiv.1, 10 ; CHECK-UNRL: br i1 %exitcond.1, label %for.end, label %for.body