Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -2502,6 +2502,7 @@ return isKnownNonZero(Op, Depth + 1, Q); } + /// Return true if it is known that V1 != V2. static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth, const Query &Q) { @@ -2514,7 +2515,9 @@ if (Depth >= MaxAnalysisRecursionDepth) return false; - // See if we can recurse through (exactly one of) our operands. + // See if we can recurse through (exactly one of) our operands. This + // requires our operation be 1-to-1 and map every input value to exactly + // one output value. Such an operation is invertable. auto *O1 = dyn_cast(V1); auto *O2 = dyn_cast(V2); if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) { @@ -2530,6 +2533,25 @@ return isKnownNonEqual(O1->getOperand(0), O2->getOperand(0), Depth + 1, Q); break; + case Instruction::Mul: + // invertable if A * B == (A * B) mod 2^N where A, and B are integers + // and N is the bitwdith. + if (!cast(O1)->hasNoUnsignedWrap() || + !cast(O2)->hasNoUnsignedWrap()) + break; + + // Assume operand order has been canonicalized + if (O1->getOperand(0) == O2->getOperand(0) && + isa(O1->getOperand(0)) && + !cast(O1->getOperand(0))->isZero()) + return isKnownNonEqual(O1->getOperand(1), O2->getOperand(1), + Depth + 1, Q); + if (O1->getOperand(1) == O2->getOperand(1) && + isa(O1->getOperand(1)) && + !cast(O1->getOperand(1))->isZero()) + return isKnownNonEqual(O1->getOperand(0), O2->getOperand(0), + Depth + 1, Q); + break; case Instruction::SExt: case Instruction::ZExt: if (O1->getOperand(0)->getType() == O2->getOperand(0)->getType()) Index: llvm/test/Analysis/ValueTracking/known-non-equal.ll =================================================================== --- llvm/test/Analysis/ValueTracking/known-non-equal.ll +++ llvm/test/Analysis/ValueTracking/known-non-equal.ll @@ -130,4 +130,47 @@ ret i1 %cmp } +; op could wrap mapping two values to the same output value. +define i1 @mul1(i8 %B) { +; CHECK-LABEL: @mul1( +; CHECK-NEXT: [[A:%.*]] = add i8 [[B:%.*]], 1 +; CHECK-NEXT: [[A_OP:%.*]] = mul i8 27, [[A]] +; CHECK-NEXT: [[B_OP:%.*]] = mul i8 27, [[B]] +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A_OP]], [[B_OP]] +; CHECK-NEXT: ret i1 [[CMP]] +; + %A = add i8 %B, 1 + %A.op = mul i8 27, %A + %B.op = mul i8 27, %B + + %cmp = icmp eq i8 %A.op, %B.op + ret i1 %cmp +} + +define i1 @mul2(i8 %B) { +; CHECK-LABEL: @mul2( +; CHECK-NEXT: ret i1 false +; + %A = add i8 %B, 1 + %A.op = mul nuw i8 27, %A + %B.op = mul nuw i8 27, %B + + %cmp = icmp eq i8 %A.op, %B.op + ret i1 %cmp +} + +; Multiply by zero collapses all values to one +define i1 @mul3(i8 %B) { +; CHECK-LABEL: @mul3( +; CHECK-NEXT: ret i1 true +; + %A = add i8 %B, 1 + %A.op = mul i8 0, %A + %B.op = mul i8 0, %B + + %cmp = icmp eq i8 %A.op, %B.op + ret i1 %cmp +} + + !0 = !{ i8 1, i8 5 }