[llvm] [AggressiveInstCombine] Add `umin` and `umax` to `TruncInstCombine` DAG (PR #222914)
Ömer Sinan Ağacan via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 11 03:39:38 PDT 2026
https://github.com/osa1 created https://github.com/llvm/llvm-project/pull/222914
This depends on #222890. I'll update tests after that PR.
Commit message inspired by other commits to the same file doing similar changes (adding more instruction handling).
>From 41ecd112b90598a987e21e43abbcaea9e698e99b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= <omer at osa1.net>
Date: Fri, 11 Sep 2026 11:08:26 +0100
Subject: [PATCH] [AggressiveInstCombine] Add `umin` and `umax` to
`TruncInstCombine` DAG
---
.../TruncInstCombine.cpp | 71 +++++++++++++++++--
1 file changed, 64 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp b/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp
index 4b29e33c2e7f7..5216b1718fdb8 100644
--- a/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp
+++ b/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp
@@ -75,9 +75,20 @@ static bool isRelevantOperand(const Instruction *I, unsigned OpNo) {
return true;
case Instruction::ShuffleVector:
return true;
- default:
+ default: {
+ if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
+ switch (II->getIntrinsicID()) {
+ case Intrinsic::umin:
+ case Intrinsic::umax: {
+ return OpNo < 2;
+ }
+ default:
+ break;
+ }
+ }
llvm_unreachable("Unreachable!");
}
+ }
}
/// Given an instruction and a container, it fills all the relevant operands of
@@ -169,7 +180,21 @@ bool TruncInstCombine::buildTruncExpressionGraph() {
// TODO: Can handle more cases here:
// 1. sdiv, srem
// ...
- return false;
+ if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
+ switch (II->getIntrinsicID()) {
+ case Intrinsic::umin:
+ case Intrinsic::umax: {
+ SmallVector<Value *, 2> Operands;
+ getRelevantOperands(I, Operands);
+ append_range(Worklist, Operands);
+ break;
+ }
+ default:
+ return false;
+ }
+ } else {
+ return false;
+ }
}
}
return true;
@@ -330,9 +355,8 @@ Type *TruncInstCombine::getBestTruncatedType() {
if (MinBitWidth >= OrigBitWidth)
return nullptr;
Itr.second.MinBitWidth = MinBitWidth;
- }
- if (I->getOpcode() == Instruction::UDiv ||
- I->getOpcode() == Instruction::URem) {
+ } else if (I->getOpcode() == Instruction::UDiv ||
+ I->getOpcode() == Instruction::URem) {
unsigned MinBitWidth = 0;
for (const auto &Op : I->operands()) {
KnownBits Known = computeKnownBits(Op);
@@ -342,6 +366,24 @@ Type *TruncInstCombine::getBestTruncatedType() {
return nullptr;
}
Itr.second.MinBitWidth = MinBitWidth;
+ } else if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
+ switch (II->getIntrinsicID()) {
+ case Intrinsic::umin:
+ case Intrinsic::umax: {
+ unsigned MinBitWidth = 0;
+ for (const auto &Op : II->args()) {
+ KnownBits Known = computeKnownBits(Op);
+ MinBitWidth =
+ std::max(Known.getMaxValue().getActiveBits(), MinBitWidth);
+ if (MinBitWidth >= OrigBitWidth)
+ return nullptr;
+ }
+ Itr.second.MinBitWidth = MinBitWidth;
+ break;
+ }
+ default:
+ break;
+ }
}
}
@@ -480,8 +522,23 @@ void TruncInstCombine::ReduceExpressionGraph(Type *SclTy) {
std::make_pair(cast<PHINode>(I), cast<PHINode>(Res)));
break;
}
- default:
- llvm_unreachable("Unhandled instruction");
+ default: {
+ if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
+ switch (II->getIntrinsicID()) {
+ case Intrinsic::umin:
+ case Intrinsic::umax: {
+ Value *LHS = getReducedOperand(I->getOperand(0), SclTy);
+ Value *RHS = getReducedOperand(I->getOperand(1), SclTy);
+ Res = Builder.CreateBinaryIntrinsic(II->getIntrinsicID(), LHS, RHS);
+ break;
+ }
+ default:
+ llvm_unreachable("Unhandled intrinsic");
+ }
+ } else {
+ llvm_unreachable("Unhandled instruction");
+ }
+ }
}
NodeInfo.NewValue = Res;
More information about the llvm-commits
mailing list