[llvm] [GlobalIsel] Combine G_ADD and G_SUB with constants (PR #97771)

Thorsten Schütt via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 24 22:58:49 PDT 2024


================
@@ -7457,3 +7457,143 @@ void CombinerHelper::applyExpandFPowI(MachineInstr &MI, int64_t Exponent) {
   Builder.buildCopy(Dst, *Res);
   MI.eraseFromParent();
 }
+
+bool CombinerHelper::matchFoldAPlusC1MinusC2(const MachineInstr &MI,
+                                             BuildFnTy &MatchInfo) {
+  // fold (A+C1)-C2 -> A+(C1-C2)
+  const GSub *Sub = cast<GSub>(&MI);
+  GAdd *Add = cast<GAdd>(MRI.getVRegDef(Sub->getLHSReg()));
+
+  if (!MRI.hasOneNonDBGUse(Add->getReg(0)))
+    return false;
+
+  std::optional<APInt> MaybeC2 = getIConstantVRegVal(Sub->getRHSReg(), MRI);
+  if (!MaybeC2)
+    return false;
+
+  std::optional<APInt> MaybeC1 = getIConstantVRegVal(Add->getRHSReg(), MRI);
+  if (!MaybeC1)
+    return false;
+
+  Register Dst = Sub->getReg(0);
+  LLT DstTy = MRI.getType(Dst);
+
+  MatchInfo = [=](MachineIRBuilder &B) {
+    auto Const = B.buildConstant(DstTy, *MaybeC1 - *MaybeC2);
+    B.buildAdd(Dst, Add->getLHSReg(), Const);
+  };
+
+  return true;
+}
+
+bool CombinerHelper::matchFoldC2MinusAPlusC1(const MachineInstr &MI,
+                                             BuildFnTy &MatchInfo) {
+  // fold C2-(A+C1) -> (C2-C1)-A
+  const GSub *Sub = cast<GSub>(&MI);
+  GAdd *Add = cast<GAdd>(MRI.getVRegDef(Sub->getRHSReg()));
+
+  if (!MRI.hasOneNonDBGUse(Add->getReg(0)))
+    return false;
+
+  std::optional<APInt> MaybeC2 = getIConstantVRegVal(Sub->getLHSReg(), MRI);
+  if (!MaybeC2)
+    return false;
----------------
tschuett wrote:

`getIConstantVRegVal` is the standard tool for extracting an APInt from a G_CONSTANT sitting on a given register, but there could be a G_ABS instead. Thus it must be fallible. It abstracts away MRI, Cimm, and ... away from all of us. I am not big fan of the pattern: I known what I am doing and can take some short-cuts.

If you want a non-fallible API, then we have to work at lower abstractions and leak implementation details. We need to know the MachineOperand and how to extract the Cimm.

Same concept: I prefer auto-vectorizer over intrinsics and assembler.

https://github.com/llvm/llvm-project/pull/97771


More information about the llvm-commits mailing list