[llvm] [GISel][AArch64][AMDGPU][RISCV] Canonicalize (sub X, C) -> (add X, -C) (PR #114309)

Thorsten Schütt via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 30 15:04:17 PDT 2024


================
@@ -335,6 +335,13 @@ def mul_to_shl : GICombineRule<
          [{ return Helper.matchCombineMulToShl(*${mi}, ${matchinfo}); }]),
   (apply [{ Helper.applyCombineMulToShl(*${mi}, ${matchinfo}); }])>;
 
+// (sub x, C) -> (add x, -C)
+def sub_to_add : GICombineRule<
+  (defs root:$d, build_fn_matchinfo:$matchinfo),
+  (match (G_SUB $d, $op1, $op2):$mi,
----------------
tschuett wrote:

Could you take a look at:
```
def extract_vector_element_build_vector : GICombineRule<
   (defs root:$root, build_fn_matchinfo:$matchinfo),
   (match (G_CONSTANT $idx, $imm),
          (G_BUILD_VECTOR $src, GIVariadic<>:$unused):$Build,
          (G_EXTRACT_VECTOR_ELT $root, $src, $idx):$Extract,
   [{ return Helper.matchExtractVectorElementWithBuildVector(*${Extract}, *${Build},
      ${matchinfo}); }]),
   (apply [{ Helper.applyBuildFn(*${Extract}, ${matchinfo}); }])>;
```
The example is unrelated, but it shows how to put a constant on a register. We want to match for G_SUB x, c.

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


More information about the llvm-commits mailing list