[llvm] [InstCombine] Offset both sides of an equality icmp (PR #134086)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 28 06:17:43 PDT 2025


================
@@ -5808,6 +5808,129 @@ static Instruction *foldICmpPow2Test(ICmpInst &I,
   return nullptr;
 }
 
+/// Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
+using OffsetOp = std::pair<Instruction::BinaryOps, Value *>;
+static void collectOffsetOp(Value *V, SmallVectorImpl<OffsetOp> &Offsets,
+                            bool AllowRecursion) {
+  Instruction *Inst = dyn_cast<Instruction>(V);
+  if (!Inst || !Inst->hasOneUse())
+    return;
+
+  switch (Inst->getOpcode()) {
+  case Instruction::Add:
+    if (isGuaranteedNotToBeUndefOrPoison(Inst->getOperand(1)))
----------------
nikic wrote:

We could generalize this by deferring this check to ApplyOfffset, in which case we could also use implied poison reasoning instead. I think we did that, we could handle cases like `a + x == b + x` regardless of whether `x` is poison, and that would maybe allow dropping some existing folds. Possibly better left for later though.

https://github.com/llvm/llvm-project/pull/134086


More information about the llvm-commits mailing list