diff options
author | Nikita Popov <npopov@redhat.com> | 2025-07-04 09:16:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-04 09:16:28 +0200 |
commit | 1d5d1256487c1574e5a8addcf27983fd569966e5 (patch) | |
tree | 03f18119640464d423a78cfec310e70ed0eaa472 /llvm/lib/Analysis | |
parent | 25bf90eaede41156e45f974c772e320758cbb3c8 (diff) | |
download | llvm-1d5d1256487c1574e5a8addcf27983fd569966e5.zip llvm-1d5d1256487c1574e5a8addcf27983fd569966e5.tar.gz llvm-1d5d1256487c1574e5a8addcf27983fd569966e5.tar.bz2 |
[ConstantFolding] Consolidate poison propagation for intrinsics (#146878)
This consolidates the "fold poison arg to poison result" constant
folding logic for intrinsics, based on a common
intrinsicPropagatesPoison() helper, which is also used for poison
propagation reasoning in ValueTracking. This ensures that the set of
supported intrinsics is consistent.
This add ucmp, scmp, smul.fix, smul.fix.sat, canonicalize and sqrt to
the intrinsicPropagatesPoison list, as these were handled by
ConstantFolding but not ValueTracking. The ctpop test is an example of
the converse, where it was handled by ValueTracking but not
ConstantFolding.
Diffstat (limited to 'llvm/lib/Analysis')
-rw-r--r-- | llvm/lib/Analysis/ConstantFolding.cpp | 39 | ||||
-rw-r--r-- | llvm/lib/Analysis/ValueTracking.cpp | 75 |
2 files changed, 48 insertions, 66 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 9e3c271..af955f2 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -2229,17 +2229,6 @@ static Constant *ConstantFoldScalarCall1(StringRef Name, return nullptr; } - if (isa<PoisonValue>(Operands[0])) { - // TODO: All of these operations should probably propagate poison. - switch (IntrinsicID) { - case Intrinsic::canonicalize: - case Intrinsic::sqrt: - return PoisonValue::get(Ty); - default: - break; - } - } - if (isa<UndefValue>(Operands[0])) { // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN. // ctpop() is between 0 and bitwidth, pick 0 for undef. @@ -3228,11 +3217,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty, case Intrinsic::smin: case Intrinsic::umax: case Intrinsic::umin: - // This is the same as for binary ops - poison propagates. - // TODO: Poison handling should be consolidated. - if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1])) - return PoisonValue::get(Ty); - if (!C0 && !C1) return UndefValue::get(Ty); if (!C0 || !C1) @@ -3245,9 +3229,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty, case Intrinsic::scmp: case Intrinsic::ucmp: - if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1])) - return PoisonValue::get(Ty); - if (!C0 || !C1) return ConstantInt::get(Ty, 0); @@ -3314,11 +3295,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty, } case Intrinsic::uadd_sat: case Intrinsic::sadd_sat: - // This is the same as for binary ops - poison propagates. - // TODO: Poison handling should be consolidated. - if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1])) - return PoisonValue::get(Ty); - if (!C0 && !C1) return UndefValue::get(Ty); if (!C0 || !C1) @@ -3329,11 +3305,6 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty, return ConstantInt::get(Ty, C0->sadd_sat(*C1)); case Intrinsic::usub_sat: case Intrinsic::ssub_sat: - // This is the same as for binary ops - poison propagates. - // TODO: Poison handling should be consolidated. - if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1])) - return PoisonValue::get(Ty); - if (!C0 && !C1) return UndefValue::get(Ty); if (!C0 || !C1) @@ -3592,11 +3563,6 @@ static Constant *ConstantFoldScalarCall3(StringRef Name, if (IntrinsicID == Intrinsic::smul_fix || IntrinsicID == Intrinsic::smul_fix_sat) { - // poison * C -> poison - // C * poison -> poison - if (isa<PoisonValue>(Operands[0]) || isa<PoisonValue>(Operands[1])) - return PoisonValue::get(Ty); - const APInt *C0, *C1; if (!getConstIntOrUndef(Operands[0], C0) || !getConstIntOrUndef(Operands[1], C1)) @@ -3670,6 +3636,11 @@ static Constant *ConstantFoldScalarCall(StringRef Name, ArrayRef<Constant *> Operands, const TargetLibraryInfo *TLI, const CallBase *Call) { + if (IntrinsicID != Intrinsic::not_intrinsic && + any_of(Operands, IsaPred<PoisonValue>) && + intrinsicPropagatesPoison(IntrinsicID)) + return PoisonValue::get(Ty); + if (Operands.size() == 1) return ConstantFoldScalarCall1(Name, IntrinsicID, Ty, Operands, TLI, Call); diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index e576f48..2b7b1ee 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -7879,6 +7879,47 @@ bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I, llvm_unreachable("Instruction not contained in its own parent basic block."); } +bool llvm::intrinsicPropagatesPoison(Intrinsic::ID IID) { + switch (IID) { + // TODO: Add more intrinsics. + case Intrinsic::sadd_with_overflow: + case Intrinsic::ssub_with_overflow: + case Intrinsic::smul_with_overflow: + case Intrinsic::uadd_with_overflow: + case Intrinsic::usub_with_overflow: + case Intrinsic::umul_with_overflow: + // If an input is a vector containing a poison element, the + // two output vectors (calculated results, overflow bits)' + // corresponding lanes are poison. + return true; + case Intrinsic::ctpop: + case Intrinsic::ctlz: + case Intrinsic::cttz: + case Intrinsic::abs: + case Intrinsic::smax: + case Intrinsic::smin: + case Intrinsic::umax: + case Intrinsic::umin: + case Intrinsic::scmp: + case Intrinsic::ucmp: + case Intrinsic::bitreverse: + case Intrinsic::bswap: + case Intrinsic::sadd_sat: + case Intrinsic::ssub_sat: + case Intrinsic::sshl_sat: + case Intrinsic::uadd_sat: + case Intrinsic::usub_sat: + case Intrinsic::ushl_sat: + case Intrinsic::smul_fix: + case Intrinsic::smul_fix_sat: + case Intrinsic::canonicalize: + case Intrinsic::sqrt: + return true; + default: + return false; + } +} + bool llvm::propagatesPoison(const Use &PoisonOp) { const Operator *I = cast<Operator>(PoisonOp.getUser()); switch (I->getOpcode()) { @@ -7889,38 +7930,8 @@ bool llvm::propagatesPoison(const Use &PoisonOp) { case Instruction::Select: return PoisonOp.getOperandNo() == 0; case Instruction::Call: - if (auto *II = dyn_cast<IntrinsicInst>(I)) { - switch (II->getIntrinsicID()) { - // TODO: Add more intrinsics. - case Intrinsic::sadd_with_overflow: - case Intrinsic::ssub_with_overflow: - case Intrinsic::smul_with_overflow: - case Intrinsic::uadd_with_overflow: - case Intrinsic::usub_with_overflow: - case Intrinsic::umul_with_overflow: - // If an input is a vector containing a poison element, the - // two output vectors (calculated results, overflow bits)' - // corresponding lanes are poison. - return true; - case Intrinsic::ctpop: - case Intrinsic::ctlz: - case Intrinsic::cttz: - case Intrinsic::abs: - case Intrinsic::smax: - case Intrinsic::smin: - case Intrinsic::umax: - case Intrinsic::umin: - case Intrinsic::bitreverse: - case Intrinsic::bswap: - case Intrinsic::sadd_sat: - case Intrinsic::ssub_sat: - case Intrinsic::sshl_sat: - case Intrinsic::uadd_sat: - case Intrinsic::usub_sat: - case Intrinsic::ushl_sat: - return true; - } - } + if (auto *II = dyn_cast<IntrinsicInst>(I)) + return intrinsicPropagatesPoison(II->getIntrinsicID()); return false; case Instruction::ICmp: case Instruction::FCmp: |