[llvm] [ConstraintElim] Unify signed and unsigned decomposition paths (NFC) (PR #223835)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 16 01:01:39 PDT 2026
================
@@ -656,145 +656,94 @@ static Decomposition decompose(Value *V, const ConstraintInfo &Info,
if (!Ty->isIntegerTy() || Ty->getIntegerBitWidth() > 64)
return V;
- // Decompose \p V used with a signed predicate.
- if (IsSigned) {
- if (auto *CI = dyn_cast<ConstantInt>(V)) {
+ if (auto *CI = dyn_cast<ConstantInt>(V)) {
+ if (IsSigned) {
if (canUseSExt(CI))
return CI->getSExtValue();
+ } else if (!CI->uge(MaxConstraintValue)) {
+ return int64_t(CI->getZExtValue());
}
- Value *Op0;
- Value *Op1;
-
- if (match(V, m_SExt(m_Value(Op0))))
- V = Op0;
- else if (match(V, m_NNegZExt(m_Value(Op0)))) {
- V = Op0;
- } else if (auto *Trunc = dyn_cast<TruncInst>(V)) {
- if (Trunc->getSrcTy()->getScalarSizeInBits() <= 64 &&
- isKnownNoWrap(Trunc, Info, IsSigned))
- V = Trunc->getOperand(0);
- }
-
- if (match(V, m_AddLike(m_Value(Op0), m_Value(Op1)))) {
- if (isKnownNoWrap(V, Info, IsSigned))
- if (auto Decomp = MergeResults(Op0, Op1, IsSigned))
- return *Decomp;
- return V;
- }
-
- // `xor %x, -1` is equivalent to `sub nsw -1, %x`.
- if (match(V, m_Not(m_Value(Op0)))) {
- Decomposition Result(-1);
- if (!Result.sub(decompose(Op0, Info, IsSigned, DL)))
- return Result;
- return V;
- }
-
- if (match(V, m_Sub(m_Value(Op0), m_Value(Op1)))) {
- if (isKnownNoWrap(V, Info, IsSigned)) {
- auto ResA = decompose(Op0, Info, IsSigned, DL);
- auto ResB = decompose(Op1, Info, IsSigned, DL);
- if (!ResA.sub(ResB))
- return ResA;
- }
- return V;
- }
-
- ConstantInt *CI;
- if (match(V, m_Mul(m_Value(Op0), m_ConstantInt(CI))) && canUseSExt(CI)) {
- if (isKnownNoWrap(V, Info, IsSigned)) {
- auto Result = decompose(Op0, Info, IsSigned, DL);
- if (!Result.mul(CI->getSExtValue()))
- return Result;
- }
- return V;
- }
-
- // (shl nsw x, shift) is (mul nsw x, (1<<shift)), with the exception of
- // shift == bw-1.
- if (match(V, m_Shl(m_Value(Op0), m_ConstantInt(CI)))) {
- uint64_t Shift = CI->getValue().getLimitedValue();
- if (Shift < Ty->getIntegerBitWidth() - 1 &&
- isKnownNoWrap(V, Info, IsSigned)) {
- assert(Shift < 64 && "Would overflow");
- auto Result = decompose(Op0, Info, IsSigned, DL);
- if (!Result.mul(int64_t(1) << Shift))
- return Result;
- return V;
- }
- }
-
return V;
}
- if (auto *CI = dyn_cast<ConstantInt>(V)) {
- if (CI->uge(MaxConstraintValue))
- return V;
- return int64_t(CI->getZExtValue());
- }
-
Value *Op0;
+ Value *Op1;
+ ConstantInt *CI;
+
if (match(V, m_ZExt(m_Value(Op0)))) {
+ // In the signed system, the ZExt must be non-negative.
+ if (IsSigned && !cast<ZExtInst>(V)->hasNonNeg())
+ return V;
V = Op0;
} else if (match(V, m_SExt(m_Value(Op0)))) {
- // Looking through the sext is only valid if the operand is non-negative.
- if (!Info.isKnownNonNegative(Op0))
+ // In the signed system, the SExt must be non-negative.
----------------
antoniofrighetto wrote:
```suggestion
// In the unsigned system, the SExt operand must be non-negative.
```
https://github.com/llvm/llvm-project/pull/223835
More information about the llvm-commits
mailing list