[llvm] [ConstraintElim] Strengthen flags on mul and shl with constant operand. (PR #219293)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 28 03:06:36 PDT 2026


================
@@ -1294,32 +1294,99 @@ static bool getConstraintFromMemoryAccess(GetElementPtrInst &GEP,
 /// Returns true if \p I is a candidate whose poison-generating flags may be
 /// strengthened using the constraint systems.
 static bool canStrengthenFlags(Instruction *I) {
-  switch (I->getOpcode()) {
+  auto *BO = dyn_cast<BinaryOperator>(I);
+  if (!BO || !BO->getType()->isIntegerTy())
+    return false;
+
+  switch (BO->getOpcode()) {
   case Instruction::Sub:
     // A - B does not wrap unsigned, if A >=u B. Subs with constant operands get
     // canonicalized to Add.
-    return I->getType()->isIntegerTy() && !I->hasNoUnsignedWrap() &&
-           !isa<Constant>(I->getOperand(1));
+    return !BO->hasNoUnsignedWrap() && !isa<Constant>(BO->getOperand(1));
+  case Instruction::Mul:
+  case Instruction::Shl:
+    if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
+      return false;
+    // A constant second operand can be used to bound the first operand to
+    // refine no-wrap flags.
+    return isa<ConstantInt>(BO->getOperand(1));
   default:
     return false;
   }
 }
 
+/// Returns true if \p Info implies that \p Op is in \p R, interpreting \p R as
+/// a signed range if \p Signed is set and as an unsigned range otherwise.
+static bool doesHoldInRange(const ConstraintInfo &Info, Value *Op,
+                            const ConstantRange &R, bool Signed) {
+  if (R.isFullSet() || R.isEmptySet() ||
----------------
fhahn wrote:

I added an early out for full-set to return true; I don't think it will catch many interesting cases in practice, but potentially avoids more expensive queries to the full system

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


More information about the llvm-commits mailing list