[llvm] [GlobalISel] Combine G_MERGE_VALUES of undef (PR #113381)

Thorsten Schütt via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 23 03:02:10 PDT 2024


================
@@ -420,7 +420,8 @@ def unary_undef_to_zero: GICombineRule<
 // replaced with undef.
 def propagate_undef_any_op: GICombineRule<
   (defs root:$root),
-  (match (wip_match_opcode G_ADD, G_FPTOSI, G_FPTOUI, G_SUB, G_XOR, G_TRUNC, G_BITCAST, G_ANYEXT):$root,
+  (match (wip_match_opcode G_ADD, G_FPTOSI, G_FPTOUI, G_SUB, G_XOR, G_TRUNC, G_BITCAST,
+                           G_ANYEXT, G_MERGE_VALUES):$root,
----------------
tschuett wrote:

```
; NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
; RUN: llc %s -stop-after=legalizer -verify-machineinstrs -mtriple aarch64-apple-darwin -global-isel -o - 2>&1 | FileCheck %s

; The zext here is optimised to an any_extend during isel..
define i128 @bswap_i16_to_i128_anyext(i16 %a) {
  ; CHECK-LABEL: name: bswap_i16_to_i128_anyext
  ; CHECK: bb.1 (%ir-block.0):
  ; CHECK-NEXT:   liveins: $w0
  ; CHECK-NEXT: {{  $}}
  ; CHECK-NEXT:   [[COPY:%[0-9]+]]:_(s32) = COPY $w0
  ; CHECK-NEXT:   [[BSWAP:%[0-9]+]]:_(s32) = G_BSWAP [[COPY]]
  ; CHECK-NEXT:   [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 16
  ; CHECK-NEXT:   [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[BSWAP]], [[C]](s64)
  ; CHECK-NEXT:   [[DEF:%[0-9]+]]:_(s32) = G_IMPLICIT_DEF
  ; CHECK-NEXT:   [[MV:%[0-9]+]]:_(s64) = G_MERGE_VALUES [[LSHR]](s32), [[DEF]](s32)
  ; CHECK-NEXT:   [[C1:%[0-9]+]]:_(s64) = G_CONSTANT i64 65535
  ; CHECK-NEXT:   [[C2:%[0-9]+]]:_(s64) = G_CONSTANT i64 0
  ; CHECK-NEXT:   [[AND:%[0-9]+]]:_(s64) = G_AND [[MV]], [[C1]]
  ; CHECK-NEXT:   [[C3:%[0-9]+]]:_(s64) = G_CONSTANT i64 48
  ; CHECK-NEXT:   [[SHL:%[0-9]+]]:_(s64) = G_SHL [[AND]], [[C3]](s64)
  ; CHECK-NEXT:   $x0 = COPY [[C2]](s64)
  ; CHECK-NEXT:   $x1 = COPY [[SHL]](s64)
  ; CHECK-NEXT:   RET_ReallyLR implicit $x0, implicit $x1
   %3 = call i16 @llvm.bswap.i16(i16 %a)
    %4 = zext i16 %3 to i128
    %5 = shl i128 %4, 112
    ret i128 %5
}
```
* We have a G_MERGE_VALUES of undef
* The new undef feeds the G_AND
* The new undef of the G_AND feeds the G_SHL.
* The G_SHL of the new undef becomes  zero.
```
def binop_left_undef_to_zero: GICombineRule<
  (defs root:$root),
  (match (wip_match_opcode G_SHL, G_UDIV, G_UREM):$root,
         [{ return Helper.matchOperandIsUndef(*${root}, 1); }]),
  (apply [{ Helper.replaceInstWithConstant(*${root}, 0); }])>;
```
The result is zero.

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


More information about the llvm-commits mailing list