[llvm] [GlobalIsel] Combine G_PTR_ADD. (PR #95647)
Pierre van Houtryve via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 16 23:59:04 PDT 2024
Thorsten =?utf-8?q?Schütt?= <schuett at gmail.com>,
Thorsten =?utf-8?q?Schütt?= <schuett at gmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/95647 at github.com>
================
@@ -7437,3 +7437,212 @@ bool CombinerHelper::matchNonNegZext(const MachineOperand &MO,
return false;
}
+
+bool CombinerHelper::matchPtrAddWithSub(const MachineOperand &MO,
+ BuildFnTy &MatchInfo) {
+ GPtrAdd *Inner = cast<GPtrAdd>(MRI.getVRegDef(MO.getReg()));
+ GSub *Sub = cast<GSub>(MRI.getVRegDef(Inner->getOffsetReg()));
+
+ // sub(x, c) -> add(x, -c)
+
+ // one-use check
+ if (!MRI.hasOneNonDBGUse(Sub->getReg(0)))
+ return false;
+
+ // Cannot fail due to pattern.
+ std::optional<APInt> MaybeImm = getIConstantVRegVal(Sub->getRHSReg(), MRI);
+ if (!MaybeImm)
+ return false;
+
+ LLT ConstTy = MRI.getType(Inner->getOffsetReg());
+
+ if (!isConstantLegalOrBeforeLegalizer(ConstTy))
+ return false;
+
+ Register Dst = MO.getReg();
+ LLT DstTy = MRI.getType(Dst);
+
+ MatchInfo = [=](MachineIRBuilder &B) {
----------------
Pierre-vh wrote:
Can you please try to avoid this pattern (using a "build function") ?
Seems like a good amount of these could just be MIR patterns, or just normal C++ functions
For instance, you don't have to directly negate the constant, a pattern can just emit `G_NEG` and let another rule do it.
https://github.com/llvm/llvm-project/pull/95647
More information about the llvm-commits
mailing list