aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2025-07-23 10:05:09 +0200
committerGitHub <noreply@github.com>2025-07-23 10:05:09 +0200
commit7e878aaf23dd559fa491a0bf6168f15f939c5965 (patch)
treeab9260f9418df91d574d892ee9ddc48f513c6fc7 /llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
parentb7889a65a8e54f2d9c7f578a515a7bf970044bfe (diff)
downloadllvm-7e878aaf23dd559fa491a0bf6168f15f939c5965.zip
llvm-7e878aaf23dd559fa491a0bf6168f15f939c5965.tar.gz
llvm-7e878aaf23dd559fa491a0bf6168f15f939c5965.tar.bz2
[PatternMatch] Add support for capture-and-match (NFC) (#149825)
When using PatternMatch, there is a common problem where we want to both match something against a pattern, but also capture the value/instruction for various reasons (e.g. to access flags). Currently the two ways to do that is to either capture using m_Value/m_Instruction and do a separate match on the result, or to use the somewhat awkward `m_CombineAnd(m_XYZ, m_Value(V))` pattern. This PR introduces to add a variant of `m_Value`/`m_Instruction` which does both a capture and a match. `m_Value(V, m_XYZ)` is basically equivalent to `m_CombineAnd(m_XYZ, m_Value(V))`. I've ported two InstCombine files to this pattern as a sample.
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp15
1 files changed, 7 insertions, 8 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 7f605be..d934638 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1355,9 +1355,9 @@ Instruction *InstCombinerImpl::
// right-shift of X and a "select".
Value *X, *Select;
Instruction *LowBitsToSkip, *Extract;
- if (!match(&I, m_c_BinOp(m_TruncOrSelf(m_CombineAnd(
- m_LShr(m_Value(X), m_Instruction(LowBitsToSkip)),
- m_Instruction(Extract))),
+ if (!match(&I, m_c_BinOp(m_TruncOrSelf(m_Instruction(
+ Extract, m_LShr(m_Value(X),
+ m_Instruction(LowBitsToSkip)))),
m_Value(Select))))
return nullptr;
@@ -1763,13 +1763,12 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
Constant *C;
// (add X, (sext/zext (icmp eq X, C)))
// -> (select (icmp eq X, C), (add C, (sext/zext 1)), X)
- auto CondMatcher = m_CombineAnd(
- m_Value(Cond),
- m_SpecificICmp(ICmpInst::ICMP_EQ, m_Deferred(A), m_ImmConstant(C)));
+ auto CondMatcher =
+ m_Value(Cond, m_SpecificICmp(ICmpInst::ICMP_EQ, m_Deferred(A),
+ m_ImmConstant(C)));
if (match(&I,
- m_c_Add(m_Value(A),
- m_CombineAnd(m_Value(Ext), m_ZExtOrSExt(CondMatcher)))) &&
+ m_c_Add(m_Value(A), m_Value(Ext, m_ZExtOrSExt(CondMatcher)))) &&
Ext->hasOneUse()) {
Value *Add = isa<ZExtInst>(Ext) ? InstCombiner::AddOne(C)
: InstCombiner::SubOne(C);