[llvm] [GlobalISel] Handle div-by-pow2 (PR #83155)

Pierre van Houtryve via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 3 23:36:36 PST 2024


================
@@ -1036,7 +1036,20 @@ def sdiv_by_const : GICombineRule<
    [{ return Helper.matchSDivByConst(*${root}); }]),
   (apply [{ Helper.applySDivByConst(*${root}); }])>;
 
-def intdiv_combines : GICombineGroup<[udiv_by_const, sdiv_by_const]>;
+def sdiv_by_pow2 : GICombineRule<
+  (defs root:$root),
+  (match (wip_match_opcode G_SDIV):$root,
+   [{ return Helper.matchSDivByPow2(*${root}); }]),
+  (apply [{ Helper.applySDivByPow2(*${root}); }])>;
+
+def udiv_by_pow2 : GICombineRule<
+  (defs root:$root),
+  (match (wip_match_opcode G_UDIV):$root,
+   [{ return Helper.matchUDivByPow2(*${root}); }]),
+  (apply [{ Helper.applyUDivByPow2(*${root}); }])>;
----------------
Pierre-vh wrote:

- Let's avoid introducing new `wip_match_opcode` uses unless necessary :)
- Both `match` functions can be merged, just pass `${y}.getReg()` directly + a `IsSigned` bool?

```suggestion
def sdiv_by_pow2 : GICombineRule<
  (defs root:$root),
  (match (G_SDIV $dst, $x, $y):$root,
   [{ return Helper.matchDivByPow2(${y}.getReg(), /*Signed*/true); }]),
  (apply [{ Helper.applySDivByPow2(*${root}); }])>;

def udiv_by_pow2 : GICombineRule<
  (defs root:$root),
  (match (G_UDIV $dst, $x, $y):$root,
   [{ return Helper.matchDivByPow2(${y}.getReg(), /*Signed*/false); }]),
  (apply [{ Helper.applyUDivByPow2(*${root}); }])>;
```


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


More information about the llvm-commits mailing list