[llvm] [InstCombine] Fold select of intrinsic into intrinsic of select (PR #178002)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 28 12:02:23 PST 2026
================
@@ -492,6 +492,55 @@ Instruction *InstCombinerImpl::foldSelectOpOp(SelectInst &SI, Instruction *TI,
return nullptr;
}
+/// This transforms patterns of the form:
+/// select cond, intrinsic(x, ...), intrinsic(y, ...)
+/// into:
+/// intrinsic(select cond, x, y, ...)
+Instruction *InstCombinerImpl::foldSelectIntrinsic(SelectInst &SI) {
+ auto *LHSIntrinsic = dyn_cast<IntrinsicInst>(SI.getTrueValue());
+ if (!LHSIntrinsic)
+ return nullptr;
+ auto *RHSIntrinsic = dyn_cast<IntrinsicInst>(SI.getFalseValue());
+ if (!RHSIntrinsic ||
+ LHSIntrinsic->getIntrinsicID() != RHSIntrinsic->getIntrinsicID() ||
+ (!LHSIntrinsic->hasOneUse() || !RHSIntrinsic->hasOneUse()))
+ return nullptr;
+
+ const Intrinsic::ID IID = LHSIntrinsic->getIntrinsicID();
+ switch (IID) {
+ case Intrinsic::abs:
+ case Intrinsic::cttz:
+ case Intrinsic::ctlz: {
+ auto *TZ = dyn_cast<ConstantInt>(LHSIntrinsic->getArgOperand(1));
+ auto *FZ = dyn_cast<ConstantInt>(RHSIntrinsic->getArgOperand(1));
+ if (!TZ || !FZ)
+ return nullptr;
----------------
dtcxzyw wrote:
```suggestion
auto *TZ = cast<ConstantInt>(LHSIntrinsic->getArgOperand(1));
auto *FZ = cast<ConstantInt>(RHSIntrinsic->getArgOperand(1));
```
It is always safe to cast parameters with immarg into constants.
https://github.com/llvm/llvm-project/pull/178002
More information about the llvm-commits
mailing list