[PATCH] D81876: [GlobalISel] Combine (0 * x) -> 0

Jessica Paquette via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 15 13:48:12 PDT 2020


paquette created this revision.
paquette added reviewers: aemerson, arsenm.
Herald added subscribers: rovka, wdng.
Herald added a project: LLVM.

Before, we only folded (x * 0) -> 0. It's possible that the 0 can show up on the RHS instead of the LHS. (E.g. inlining a memset and getting a multiply of two constants.)

So, let's fold 0 on the LHS as well.

Example: https://godbolt.org/z/CHQVs9


https://reviews.llvm.org/D81876

Files:
  llvm/include/llvm/Target/GlobalISel/Combine.td
  llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-trivial-arith.mir


Index: llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-trivial-arith.mir
===================================================================
--- llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-trivial-arith.mir
+++ llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-trivial-arith.mir
@@ -41,14 +41,14 @@
 
 ...
 ---
-name:            mul_0
+name:            mul_0_rhs
 tracksRegLiveness: true
 body:             |
   bb.1.entry:
     liveins: $w0
     ; Fold (x * 0) -> 0
     ;
-    ; CHECK-LABEL: name: mul_0
+    ; CHECK-LABEL: name: mul_0_rhs
     ; CHECK: liveins: $w0
     ; CHECK: %cst:_(s32) = G_CONSTANT i32 0
     ; CHECK: $w0 = COPY %cst(s32)
@@ -59,6 +59,26 @@
     $w0 = COPY %op(s32)
     RET_ReallyLR implicit $w0
 
+...
+---
+name:            mul_0_lhs
+tracksRegLiveness: true
+body:             |
+  bb.1.entry:
+    liveins: $w0
+    ; Fold (0 * x) -> 0
+    ;
+    ; CHECK-LABEL: name: mul_0_lhs
+    ; CHECK: liveins: $w0
+    ; CHECK: %cst:_(s32) = G_CONSTANT i32 0
+    ; CHECK: $w0 = COPY %cst(s32)
+    ; CHECK: RET_ReallyLR implicit $w0
+    %x:_(s32) = COPY $w0
+    %cst:_(s32) = G_CONSTANT i32 0
+    %op:_(s32) = G_MUL %cst(s32), %x
+    $w0 = COPY %op(s32)
+    RET_ReallyLR implicit $w0
+
 ...
 
 # FIXME: Probably should be able to replace this.
Index: llvm/include/llvm/Target/GlobalISel/Combine.td
===================================================================
--- llvm/include/llvm/Target/GlobalISel/Combine.td
+++ llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -211,7 +211,7 @@
 // Fold (0 op x) - > 0
 def binop_left_to_zero: GICombineRule<
   (defs root:$root),
-  (match (wip_match_opcode G_SDIV, G_UDIV, G_SREM, G_UREM):$root,
+  (match (wip_match_opcode G_SDIV, G_UDIV, G_SREM, G_UREM, G_MUL):$root,
     [{ return Helper.matchOperandIsZero(*${root}, 1); }]),
   (apply [{ return Helper.replaceSingleDefInstWithOperand(*${root}, 1); }])
 >;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81876.270849.patch
Type: text/x-patch
Size: 1918 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200615/dd7b90a8/attachment.bin>


More information about the llvm-commits mailing list