[llvm] [GlobalISel][TableGen] Add GIReplaceRegWithConstant/FConstant builtins (PR #222298)
Vikash Gupta via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 9 04:06:02 PDT 2026
https://github.com/vg0204 created https://github.com/llvm/llvm-project/pull/222298
It adds apply-only MIR-pattern builtins feature that replace a match-root register with a fresh `G_CONSTANT` or `G_FCONSTANT` of the same type, including splat for fixed-vector destinations. Thus, it eliminates the need for `replaceInstWithConstant/replaceInstWithFConstant` C++ helpers.
>From 24513e29a42d6c26a22ac386af32b4d5c63a116c Mon Sep 17 00:00:00 2001
From: vg0204 <Vikash.Gupta at amd.com>
Date: Wed, 9 Sep 2026 16:28:22 +0530
Subject: [PATCH] [GlobalISel][TableGen] Add GIReplaceRegWithConstant/FConstant
builtins
It adds apply-only MIR-pattern builtins feature that replace a match-root
register with a fresh `G_CONSTANT` or `G_FCONSTANT` of the same type,
including splat for fixed-vector destinations. Thus, it eliminates the
need for `replaceInstWithConstant/replaceInstWithFConstant` C++ helpers.
---
llvm/docs/GlobalISel/MIRPatterns.md | 64 ++++++
.../CodeGen/GlobalISel/GIMatchTableExecutor.h | 5 +
.../GlobalISel/GIMatchTableExecutorImpl.h | 15 ++
.../include/llvm/Target/GlobalISel/Combine.td | 44 +++-
.../combine-binop-undef-left-to-zero.mir | 16 +-
.../combine-constant-fold-itofp-zero.mir | 8 +-
.../combine-replacereg-fconstant-widths.mir | 208 ++++++++++++++++++
.../combine-unary-undef-to-zero.mir | 8 +-
.../prelegalizercombiner-hoist-same-hands.mir | 4 +-
.../builtins/builtin-pattern-errors.td | 62 +++++-
.../builtins/builtin-pattern-parrsing.td | 23 +-
.../match-table-replacereg-fconstant.td | 51 +++++
.../Common/GlobalISel/MatchTable/Matchers.cpp | 9 +
.../Common/GlobalISel/MatchTable/Matchers.h | 18 ++
.../TableGen/Common/GlobalISel/Patterns.cpp | 14 +-
.../TableGen/Common/GlobalISel/Patterns.h | 6 +-
.../TableGen/GlobalISelCombinerEmitter.cpp | 43 +++-
17 files changed, 568 insertions(+), 30 deletions(-)
create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/combine-replacereg-fconstant-widths.mir
create mode 100644 llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/match-table-replacereg-fconstant.td
diff --git a/llvm/docs/GlobalISel/MIRPatterns.md b/llvm/docs/GlobalISel/MIRPatterns.md
index 8253edf4635628..0e556873cf5da3 100644
--- a/llvm/docs/GlobalISel/MIRPatterns.md
+++ b/llvm/docs/GlobalISel/MIRPatterns.md
@@ -211,6 +211,51 @@ Semantics:
- If both old/new are operands of matched instructions,
`canReplaceReg` is checked before applying the rule.
+#### GIReplaceRegWithConstant
+
+```{code-block} text
+:caption: Usage
+
+(apply (GIReplaceRegWithConstant $reg, Imm))
+```
+
+Operands:
+
+- `$reg` (out) register defined by the match root
+- `Imm`: an integer literal
+
+Semantics:
+
+- Can only appear in an 'apply' pattern.
+- `$reg` must be a register defined by the match root.
+- Replaces all uses of `$reg` with a fresh constant of `$reg`'s type. If that
+ type is a fixed vector, the constant is splatted across all lanes.
+
+#### GIReplaceRegWithFConstant
+
+```{code-block} text
+:caption: Usage
+
+(apply (GIReplaceRegWithFConstant $reg, Imm))
+```
+
+Operands:
+
+- `$reg` (out) register defined by the match root
+- `Imm`: an integer literal
+
+```{warning}
+`Imm` is the **IEEE bit pattern** of `$reg`'s (scalar) type, not a decimal fp
+value: e.g. for an f32 `$reg`, `0` means `+0.0` (bit pattern `0x00000000`).
+```
+
+Semantics:
+
+- Can only appear in an 'apply' pattern.
+- `$reg` must be a register defined by the match root.
+- Replaces all uses of `$reg` with a fresh `G_FCONSTANT` of `$reg`'s type. If
+ that type is a fixed vector, the constant is splatted across all lanes.
+
#### GIEraseRoot
```{code-block} text
@@ -423,6 +468,25 @@ def ReplaceTemp : GICombineRule<
(GIReplaceReg $b, $new))>
```
+#### Common Pattern #1b: Replace a Register with a Constant
+
+To replace a match root's def with a fresh constant, use
+`GIReplaceRegWithConstant` (or `GIReplaceRegWithFConstant` for a
+`G_FCONSTANT`, where `Imm` is an IEEE bit pattern rather than a decimal fp
+value).
+
+```text
+def Foo : GICombineRule<
+ (defs root:$dst),
+ (match (G_AND $dst, $x, $undef):$mi, (G_IMPLICIT_DEF $undef)),
+ (apply (GIReplaceRegWithConstant $dst, 0))>;
+
+def Bar : GICombineRule<
+ (defs root:$dst),
+ (match (G_UITOFP $dst, 0)),
+ (apply (GIReplaceRegWithFConstant $dst, 0))>;
+```
+
#### Common Pattern #2: Erasing a Def-less Root
If we simply want to erase a def-less match root, we can use the
diff --git a/llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h b/llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h
index 6e3ccf1923c401..786594374ed2b6 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutor.h
@@ -384,6 +384,11 @@ enum {
/// - Imm(8) - The immediate to add
GIR_BuildConstant,
+ /// Builds an FP constant and stores its result in a TempReg.
+ /// - TempRegID(ULEB128) - Temp Register to define.
+ /// - Imm(8) - IEEE bit pattern of the TempReg's (scalar) type.
+ GIR_BuildFConstant,
+
/// Copy an operand to the specified instruction
/// - NewInsnID(ULEB128) - Instruction ID to modify
/// - OldInsnID(ULEB128) - Instruction ID to copy from
diff --git a/llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h b/llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h
index 905fe847223f7e..9281dc6bc53041 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/GIMatchTableExecutorImpl.h
@@ -20,6 +20,7 @@
#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
#include "llvm/CodeGen/GlobalISel/Utils.h"
+#include "llvm/CodeGen/LowLevelTypeUtils.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
@@ -1123,6 +1124,20 @@ bool GIMatchTableExecutor::executeMatchTable(
break;
}
+ case GIR_BuildFConstant: {
+ uint64_t TempRegID = readULEB();
+ uint64_t Bits = readU64();
+ initializeBuilder();
+ Register TempReg = State.TempRegisters[TempRegID];
+ LLT Ty = MRI.getType(TempReg).getScalarType();
+ APFloat Val(getFltSemanticForLLT(Ty), APInt(Ty.getSizeInBits(), Bits));
+ Builder.buildFConstant(TempReg, Val);
+ DEBUG_WITH_TYPE(TgtExecutor::getName(),
+ dbgs() << CurrentIdx << ": GIR_BuildFConstant(TempReg["
+ << TempRegID << "], Bits=" << Bits << ")\n");
+ break;
+ }
+
case GIR_RootToRootCopy:
case GIR_Copy: {
uint64_t NewInsnID =
diff --git a/llvm/include/llvm/Target/GlobalISel/Combine.td b/llvm/include/llvm/Target/GlobalISel/Combine.td
index 3735e95ee8f680..773696669ea06a 100644
--- a/llvm/include/llvm/Target/GlobalISel/Combine.td
+++ b/llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -162,6 +162,38 @@ class GIBuiltinInst;
// "canReplaceReg" is checked before applying the rule.
def GIReplaceReg : GIBuiltinInst;
+// Replace a register defined by the match root with a fresh G_CONSTANT.
+// Splatted across all lanes if the type is a fixed vector.
+//
+// Usage:
+// (apply (GIReplaceRegWithConstant $reg, Imm))
+//
+// Operands:
+// - $reg (out) register defined by the match root
+// - Imm: an integer literal
+//
+// Semantics:
+// - Can only appear in an 'apply' pattern.
+// - $reg must be a register defined by the match root.
+def GIReplaceRegWithConstant : GIBuiltinInst;
+
+// Replace a register defined by the match root with a fresh G_FCONSTANT.
+// Splatted across all lanes if the type is a fixed vector.
+//
+// Usage:
+// (apply (GIReplaceRegWithFConstant $reg, Imm))
+//
+// Operands:
+// - $reg (out) register defined by the match root
+// - Imm: an integer literal, interpreted as the IEEE bit pattern of $reg's
+// (scalar) type, not a decimal fp value: e.g. for an f32 $reg, `0` means
+// the bit pattern 0x00000000 (i.e. +0.0).
+//
+// Semantics:
+// - Can only appear in an 'apply' pattern.
+// - $reg must be a register defined by the match root.
+def GIReplaceRegWithFConstant : GIBuiltinInst;
+
// Apply action that erases the match root.
//
// Usage:
@@ -513,25 +545,25 @@ def undef_to_fp_zero_frags : unary_undef_frag<[G_UITOFP, G_SITOFP]>;
def undef_to_fp_zero : GICombineRule<
(defs root:$dst),
(match (undef_to_fp_zero_frags $dst)),
- (apply [{ Helper.replaceInstWithFConstant(*${dst}.getParent(), 0.0); }])>;
+ (apply (GIReplaceRegWithFConstant $dst, 0))>;
def undef_to_int_zero_frags : binop_any_undef_frag<[G_AND, G_MUL]>;
def undef_to_int_zero: GICombineRule<
(defs root:$dst),
(match (undef_to_int_zero_frags $dst)),
- (apply [{ Helper.replaceInstWithConstant(*${dst}.getParent(), 0); }])>;
+ (apply (GIReplaceRegWithConstant $dst, 0))>;
def undef_to_negative_one_frags : binop_any_undef_frag<[G_OR]>;
def undef_to_negative_one: GICombineRule<
(defs root:$dst),
(match (undef_to_negative_one_frags $dst)),
- (apply [{ Helper.replaceInstWithConstant(*${dst}.getParent(), -1); }])>;
+ (apply (GIReplaceRegWithConstant $dst, -1))>;
def binop_left_undef_to_zero_frags : binop_left_undef_frag<[G_SHL, G_UDIV, G_UREM]>;
def binop_left_undef_to_zero: GICombineRule<
(defs root:$dst),
(match (binop_left_undef_to_zero_frags $dst)),
- (apply [{ Helper.replaceInstWithConstant(*${dst}.getParent(), 0); }])>;
+ (apply (GIReplaceRegWithConstant $dst, 0))>;
def binop_right_undef_to_undef_frags : binop_right_undef_frag<[G_SHL, G_ASHR, G_LSHR]>;
def binop_right_undef_to_undef: GICombineRule<
@@ -543,7 +575,7 @@ def unary_undef_to_zero_frags : unary_undef_frag<[G_ABS]>;
def unary_undef_to_zero: GICombineRule<
(defs root:$dst),
(match (unary_undef_to_zero_frags $dst)),
- (apply [{ Helper.replaceInstWithConstant(*${dst}.getParent(), 0); }])>;
+ (apply (GIReplaceRegWithConstant $dst, 0))>;
def unary_undef_to_undef_frags : unary_undef_frag<
[G_TRUNC, G_BITCAST, G_ANYEXT, G_PTRTOINT, G_INTTOPTR, G_FPTOSI, G_FPTOUI]>;
@@ -889,7 +921,7 @@ class itof_const_zero_fold_rule<Instruction opcode> : GICombineRule <
(match (opcode $dst, 0)),
// Can't use COPY $dst, 0 here because the 0 operand may be a smaller type
// than the destination for itofp.
- (apply [{ Helper.replaceInstWithFConstant(*${dst}.getParent(), 0.0); }])
+ (apply (GIReplaceRegWithFConstant $dst, 0))
>;
def itof_const_zero_fold_si : itof_const_zero_fold_rule<G_SITOFP>;
def itof_const_zero_fold_ui : itof_const_zero_fold_rule<G_UITOFP>;
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-binop-undef-left-to-zero.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-binop-undef-left-to-zero.mir
index b64ac71ef5806f..ca77c8fbf5bc7f 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-binop-undef-left-to-zero.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-binop-undef-left-to-zero.mir
@@ -39,8 +39,8 @@ body: |
; CHECK-LABEL: name: udiv_scalar
; CHECK: liveins: $w0
; CHECK-NEXT: {{ $}}
- ; CHECK-NEXT: %op:_(i32) = G_CONSTANT i32 0
- ; CHECK-NEXT: $w0 = COPY %op(i32)
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(i32) = G_CONSTANT i32 0
+ ; CHECK-NEXT: $w0 = COPY [[C]](i32)
%rhs:_(i32) = COPY $w0
%undef:_(i32) = G_IMPLICIT_DEF
%op:_(i32) = G_UDIV %undef(i32), %rhs(i32)
@@ -55,8 +55,8 @@ body: |
; CHECK: liveins: $d0
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[C:%[0-9]+]]:_(i32) = G_CONSTANT i32 0
- ; CHECK-NEXT: %op:_(<2 x i32>) = G_BUILD_VECTOR [[C]](i32), [[C]](i32)
- ; CHECK-NEXT: $d0 = COPY %op(<2 x i32>)
+ ; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<2 x i32>) = G_BUILD_VECTOR [[C]](i32), [[C]](i32)
+ ; CHECK-NEXT: $d0 = COPY [[BUILD_VECTOR]](<2 x i32>)
%rhs:_(<2 x i32>) = COPY $d0
%undef:_(<2 x i32>) = G_IMPLICIT_DEF
%op:_(<2 x i32>) = G_UDIV %undef(<2 x i32>), %rhs(<2 x i32>)
@@ -70,8 +70,8 @@ body: |
; CHECK-LABEL: name: urem_scalar
; CHECK: liveins: $w0
; CHECK-NEXT: {{ $}}
- ; CHECK-NEXT: %op:_(i32) = G_CONSTANT i32 0
- ; CHECK-NEXT: $w0 = COPY %op(i32)
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(i32) = G_CONSTANT i32 0
+ ; CHECK-NEXT: $w0 = COPY [[C]](i32)
%rhs:_(i32) = COPY $w0
%undef:_(i32) = G_IMPLICIT_DEF
%op:_(i32) = G_UREM %undef(i32), %rhs(i32)
@@ -86,8 +86,8 @@ body: |
; CHECK: liveins: $d0
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[C:%[0-9]+]]:_(i32) = G_CONSTANT i32 0
- ; CHECK-NEXT: %op:_(<2 x i32>) = G_BUILD_VECTOR [[C]](i32), [[C]](i32)
- ; CHECK-NEXT: $d0 = COPY %op(<2 x i32>)
+ ; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<2 x i32>) = G_BUILD_VECTOR [[C]](i32), [[C]](i32)
+ ; CHECK-NEXT: $d0 = COPY [[BUILD_VECTOR]](<2 x i32>)
%rhs:_(<2 x i32>) = COPY $d0
%undef:_(<2 x i32>) = G_IMPLICIT_DEF
%op:_(<2 x i32>) = G_UREM %undef(<2 x i32>), %rhs(<2 x i32>)
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-constant-fold-itofp-zero.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-constant-fold-itofp-zero.mir
index 83d61027c7e22d..48e0817a197185 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-constant-fold-itofp-zero.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-constant-fold-itofp-zero.mir
@@ -12,8 +12,8 @@ body: |
; CHECK-LABEL: name: sitofp
; CHECK: liveins: $d0
; CHECK-NEXT: {{ $}}
- ; CHECK-NEXT: %res:_(f64) = G_FCONSTANT double 0.000000e+00
- ; CHECK-NEXT: $d0 = COPY %res(f64)
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(f64) = G_FCONSTANT double 0.000000e+00
+ ; CHECK-NEXT: $d0 = COPY [[C]](f64)
; CHECK-NEXT: RET_ReallyLR implicit $d0
%a:_(i64) = G_CONSTANT i64 0
%res:_(f64) = G_SITOFP %a
@@ -32,8 +32,8 @@ body: |
; CHECK-LABEL: name: uitofp
; CHECK: liveins: $d0
; CHECK-NEXT: {{ $}}
- ; CHECK-NEXT: %res:_(f64) = G_FCONSTANT double 0.000000e+00
- ; CHECK-NEXT: $d0 = COPY %res(f64)
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(f64) = G_FCONSTANT double 0.000000e+00
+ ; CHECK-NEXT: $d0 = COPY [[C]](f64)
; CHECK-NEXT: RET_ReallyLR implicit $d0
%a:_(i64) = G_CONSTANT i64 0
%res:_(f64) = G_UITOFP %a
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-replacereg-fconstant-widths.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-replacereg-fconstant-widths.mir
new file mode 100644
index 00000000000000..16b24c274f9cda
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-replacereg-fconstant-widths.mir
@@ -0,0 +1,208 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
+# RUN: llc -mtriple aarch64 -run-pass=aarch64-prelegalizer-combiner -verify-machineinstrs %s -o - | FileCheck %s
+
+# Exercises GIReplaceRegWithFConstant (via the itof_const_zero_fold_* and
+# undef_to_fp_zero combine rules) across scalar and vector float widths, to
+# make sure the runtime GIR_BuildFConstant handler reconstructs the correct
+# IEEE bit pattern (and splat, for fixed vectors) for each destination type.
+
+---
+name: uitofp_zero_f16
+liveins:
+ - { reg: '$h0' }
+body: |
+ bb.1.entry:
+ liveins: $h0
+
+ ; CHECK-LABEL: name: uitofp_zero_f16
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(f16) = G_FCONSTANT half 0.000000e+00
+ ; CHECK-NEXT: $h0 = COPY [[C]](f16)
+ ; CHECK-NEXT: RET_ReallyLR implicit $h0
+ %a:_(i32) = G_CONSTANT i32 0
+ %res:_(f16) = G_UITOFP %a
+ $h0 = COPY %res(f16)
+ RET_ReallyLR implicit $h0
+
+...
+---
+name: sitofp_zero_f16
+liveins:
+ - { reg: '$h0' }
+body: |
+ bb.1.entry:
+ liveins: $h0
+
+ ; CHECK-LABEL: name: sitofp_zero_f16
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(f16) = G_FCONSTANT half 0.000000e+00
+ ; CHECK-NEXT: $h0 = COPY [[C]](f16)
+ ; CHECK-NEXT: RET_ReallyLR implicit $h0
+ %a:_(i32) = G_CONSTANT i32 0
+ %res:_(f16) = G_SITOFP %a
+ $h0 = COPY %res(f16)
+ RET_ReallyLR implicit $h0
+
+...
+---
+name: uitofp_zero_f32
+liveins:
+ - { reg: '$s0' }
+body: |
+ bb.1.entry:
+ liveins: $s0
+
+ ; CHECK-LABEL: name: uitofp_zero_f32
+ ; CHECK: liveins: $s0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(f32) = G_FCONSTANT float 0.000000e+00
+ ; CHECK-NEXT: $s0 = COPY [[C]](f32)
+ ; CHECK-NEXT: RET_ReallyLR implicit $s0
+ %a:_(i32) = G_CONSTANT i32 0
+ %res:_(f32) = G_UITOFP %a
+ $s0 = COPY %res(f32)
+ RET_ReallyLR implicit $s0
+
+...
+---
+name: uitofp_zero_f64
+liveins:
+ - { reg: '$d0' }
+body: |
+ bb.1.entry:
+ liveins: $d0
+
+ ; CHECK-LABEL: name: uitofp_zero_f64
+ ; CHECK: liveins: $d0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(f64) = G_FCONSTANT double 0.000000e+00
+ ; CHECK-NEXT: $d0 = COPY [[C]](f64)
+ ; CHECK-NEXT: RET_ReallyLR implicit $d0
+ %a:_(i32) = G_CONSTANT i32 0
+ %res:_(f64) = G_UITOFP %a
+ $d0 = COPY %res(f64)
+ RET_ReallyLR implicit $d0
+
+...
+---
+name: uitofp_zero_f128
+liveins:
+ - { reg: '$q0' }
+body: |
+ bb.1.entry:
+ liveins: $q0
+
+ ; CHECK-LABEL: name: uitofp_zero_f128
+ ; CHECK: liveins: $q0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(f128) = G_FCONSTANT fp128 0.000000e+00
+ ; CHECK-NEXT: $q0 = COPY [[C]](f128)
+ ; CHECK-NEXT: RET_ReallyLR implicit $q0
+ %a:_(i32) = G_CONSTANT i32 0
+ %res:_(f128) = G_UITOFP %a
+ $q0 = COPY %res(f128)
+ RET_ReallyLR implicit $q0
+
+...
+---
+name: undef_uitofp_zero_f16
+alignment: 4
+tracksRegLiveness: true
+body: |
+ bb.0:
+ ; CHECK-LABEL: name: undef_uitofp_zero_f16
+ ; CHECK: [[C:%[0-9]+]]:_(f16) = G_FCONSTANT half 0.000000e+00
+ ; CHECK-NEXT: $h0 = COPY [[C]](f16)
+ ; CHECK-NEXT: RET_ReallyLR implicit $h0
+ %0:_(i32) = G_IMPLICIT_DEF
+ %1:_(f16) = G_UITOFP %0(i32)
+ $h0 = COPY %1(f16)
+ RET_ReallyLR implicit $h0
+
+...
+---
+name: undef_sitofp_zero_f64
+alignment: 4
+tracksRegLiveness: true
+body: |
+ bb.0:
+ ; CHECK-LABEL: name: undef_sitofp_zero_f64
+ ; CHECK: [[C:%[0-9]+]]:_(f64) = G_FCONSTANT double 0.000000e+00
+ ; CHECK-NEXT: $d0 = COPY [[C]](f64)
+ ; CHECK-NEXT: RET_ReallyLR implicit $d0
+ %0:_(i32) = G_IMPLICIT_DEF
+ %1:_(f64) = G_SITOFP %0(i32)
+ $d0 = COPY %1(f64)
+ RET_ReallyLR implicit $d0
+
+...
+---
+name: undef_uitofp_zero_v2f32
+alignment: 4
+tracksRegLiveness: true
+body: |
+ bb.0:
+ ; CHECK-LABEL: name: undef_uitofp_zero_v2f32
+ ; CHECK: [[C:%[0-9]+]]:_(f32) = G_FCONSTANT float 0.000000e+00
+ ; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<2 x f32>) = G_BUILD_VECTOR [[C]](f32), [[C]](f32)
+ ; CHECK-NEXT: $d0 = COPY [[BUILD_VECTOR]](<2 x f32>)
+ ; CHECK-NEXT: RET_ReallyLR implicit $d0
+ %0:_(<2 x i32>) = G_IMPLICIT_DEF
+ %1:_(<2 x f32>) = G_UITOFP %0(<2 x i32>)
+ $d0 = COPY %1(<2 x f32>)
+ RET_ReallyLR implicit $d0
+
+...
+---
+name: undef_sitofp_zero_v4f32
+alignment: 4
+tracksRegLiveness: true
+body: |
+ bb.0:
+ ; CHECK-LABEL: name: undef_sitofp_zero_v4f32
+ ; CHECK: [[C:%[0-9]+]]:_(f32) = G_FCONSTANT float 0.000000e+00
+ ; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<4 x f32>) = G_BUILD_VECTOR [[C]](f32), [[C]](f32), [[C]](f32), [[C]](f32)
+ ; CHECK-NEXT: $q0 = COPY [[BUILD_VECTOR]](<4 x f32>)
+ ; CHECK-NEXT: RET_ReallyLR implicit $q0
+ %0:_(<4 x i32>) = G_IMPLICIT_DEF
+ %1:_(<4 x f32>) = G_SITOFP %0(<4 x i32>)
+ $q0 = COPY %1(<4 x f32>)
+ RET_ReallyLR implicit $q0
+
+...
+---
+name: undef_uitofp_zero_v4f16
+alignment: 4
+tracksRegLiveness: true
+body: |
+ bb.0:
+ ; CHECK-LABEL: name: undef_uitofp_zero_v4f16
+ ; CHECK: [[C:%[0-9]+]]:_(f16) = G_FCONSTANT half 0.000000e+00
+ ; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<4 x f16>) = G_BUILD_VECTOR [[C]](f16), [[C]](f16), [[C]](f16), [[C]](f16)
+ ; CHECK-NEXT: $d0 = COPY [[BUILD_VECTOR]](<4 x f16>)
+ ; CHECK-NEXT: RET_ReallyLR implicit $d0
+ %0:_(<4 x i16>) = G_IMPLICIT_DEF
+ %1:_(<4 x f16>) = G_UITOFP %0(<4 x i16>)
+ $d0 = COPY %1(<4 x f16>)
+ RET_ReallyLR implicit $d0
+
+...
+---
+name: undef_uitofp_zero_v2f64
+alignment: 4
+tracksRegLiveness: true
+body: |
+ bb.0:
+ ; CHECK-LABEL: name: undef_uitofp_zero_v2f64
+ ; CHECK: [[C:%[0-9]+]]:_(f64) = G_FCONSTANT double 0.000000e+00
+ ; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<2 x f64>) = G_BUILD_VECTOR [[C]](f64), [[C]](f64)
+ ; CHECK-NEXT: $q0 = COPY [[BUILD_VECTOR]](<2 x f64>)
+ ; CHECK-NEXT: RET_ReallyLR implicit $q0
+ %0:_(<2 x i64>) = G_IMPLICIT_DEF
+ %1:_(<2 x f64>) = G_UITOFP %0(<2 x i64>)
+ $q0 = COPY %1(<2 x f64>)
+ RET_ReallyLR implicit $q0
+
+...
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-unary-undef-to-zero.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-unary-undef-to-zero.mir
index 12eb1d56250001..c042d8d3c48cdd 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-unary-undef-to-zero.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-unary-undef-to-zero.mir
@@ -13,8 +13,8 @@ body: |
; CHECK-LABEL: name: abs_scalar
; CHECK: liveins: $w0
; CHECK-NEXT: {{ $}}
- ; CHECK-NEXT: %abs:_(i32) = G_CONSTANT i32 0
- ; CHECK-NEXT: $w0 = COPY %abs(i32)
+ ; CHECK-NEXT: [[C:%[0-9]+]]:_(i32) = G_CONSTANT i32 0
+ ; CHECK-NEXT: $w0 = COPY [[C]](i32)
; CHECK-NEXT: RET_ReallyLR implicit $w0
%undef:_(i32) = G_IMPLICIT_DEF
%abs:_(i32) = G_ABS %undef
@@ -31,8 +31,8 @@ body: |
; CHECK: liveins: $d0
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[C:%[0-9]+]]:_(i32) = G_CONSTANT i32 0
- ; CHECK-NEXT: %abs:_(<2 x i32>) = G_BUILD_VECTOR [[C]](i32), [[C]](i32)
- ; CHECK-NEXT: $d0 = COPY %abs(<2 x i32>)
+ ; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<2 x i32>) = G_BUILD_VECTOR [[C]](i32), [[C]](i32)
+ ; CHECK-NEXT: $d0 = COPY [[BUILD_VECTOR]](<2 x i32>)
; CHECK-NEXT: RET_ReallyLR implicit $d0
%undef:_(<2 x i32>) = G_IMPLICIT_DEF
%abs:_(<2 x i32>) = G_ABS %undef
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-hoist-same-hands.mir b/llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-hoist-same-hands.mir
index c054e1a75d0246..6dbab62c3c9c86 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-hoist-same-hands.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-hoist-same-hands.mir
@@ -741,8 +741,8 @@ body: |
; CHECK: liveins: $d0, $d1
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[C:%[0-9]+]]:_(i64) = G_CONSTANT i64 0
- ; CHECK-NEXT: %logic_op:_(<2 x i64>) = G_BUILD_VECTOR [[C]](i64), [[C]](i64)
- ; CHECK-NEXT: $q0 = COPY %logic_op(<2 x i64>)
+ ; CHECK-NEXT: [[BUILD_VECTOR:%[0-9]+]]:_(<2 x i64>) = G_BUILD_VECTOR [[C]](i64), [[C]](i64)
+ ; CHECK-NEXT: $q0 = COPY [[BUILD_VECTOR]](<2 x i64>)
; CHECK-NEXT: RET_ReallyLR implicit $q0
%x:_(<2 x i64>) = G_IMPLICIT_DEF
%y:_(<2 x i64>) = G_IMPLICIT_DEF
diff --git a/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/builtin-pattern-errors.td b/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/builtin-pattern-errors.td
index 8e3e27daaa5bfa..386b4210a935d7 100644
--- a/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/builtin-pattern-errors.td
+++ b/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/builtin-pattern-errors.td
@@ -78,6 +78,58 @@ def replacereg_nonroot : GICombineRule<
(match (COPY $dst, $tmp), (COPY $tmp, $src)),
(apply (GIReplaceReg $dst, $src), (GIReplaceReg $tmp, $src))>;
+// CHECK: :[[@LINE+1]]:{{[0-9]+}}: error: 'GIReplaceRegWithConstant' cannot be used in a 'match' pattern
+def replaceregconstant_in_match : GICombineRule<
+ (defs root:$dst),
+ (match (GIReplaceRegWithConstant $dst, 0)),
+ (apply (COPY $dst, $src))>;
+
+// CHECK: :[[@LINE+2]]:{{[0-9]+}}: error: expected operand 1 of 'GIReplaceRegWithConstant' to be an immediate value
+// CHECK: :[[@LINE+1]]:{{[0-9]+}}: error: Failed to parse pattern: '(GIReplaceRegWithConstant ?:$dst, ?:$src)'
+def replaceregconstant_nonimm : GICombineRule<
+ (defs root:$dst),
+ (match (COPY $dst, $src)),
+ (apply (GIReplaceRegWithConstant $dst, $src))>;
+
+// CHECK: :[[@LINE+2]]:{{[0-9]+}}: error: expected operand 1 of 'GIReplaceRegWithConstant' to be an immediate value
+// CHECK: :[[@LINE+1]]:{{[0-9]+}}: error: Failed to parse pattern: '(GIReplaceRegWithConstant ?:$dst, (i32 0):$k)'
+def replaceregconstant_namedimm : GICombineRule<
+ (defs root:$dst),
+ (match (COPY $dst, $src)),
+ (apply (GIReplaceRegWithConstant $dst, (i32 0):$k))>;
+
+// CHECK: :[[@LINE+1]]:{{[0-9]+}}: error: GIReplaceRegWithConstant cannot replace 'tmp': this builtin can only replace a register defined by the match root
+def replaceregconstant_nonroot : GICombineRule<
+ (defs root:$dst),
+ (match (COPY $dst, $tmp), (COPY $tmp, $src)),
+ (apply (GIReplaceReg $dst, $src), (GIReplaceRegWithConstant $tmp, 0))>;
+
+// CHECK: :[[@LINE+1]]:{{[0-9]+}}: error: 'GIReplaceRegWithFConstant' cannot be used in a 'match' pattern
+def replaceregfconstant_in_match : GICombineRule<
+ (defs root:$dst),
+ (match (GIReplaceRegWithFConstant $dst, 0)),
+ (apply (COPY $dst, $src))>;
+
+// CHECK: :[[@LINE+2]]:{{[0-9]+}}: error: expected operand 1 of 'GIReplaceRegWithFConstant' to be an immediate value
+// CHECK: :[[@LINE+1]]:{{[0-9]+}}: error: Failed to parse pattern: '(GIReplaceRegWithFConstant ?:$dst, ?:$src)'
+def replaceregfconstant_nonimm : GICombineRule<
+ (defs root:$dst),
+ (match (COPY $dst, $src)),
+ (apply (GIReplaceRegWithFConstant $dst, $src))>;
+
+// CHECK: :[[@LINE+2]]:{{[0-9]+}}: error: expected operand 1 of 'GIReplaceRegWithFConstant' to be an immediate value
+// CHECK: :[[@LINE+1]]:{{[0-9]+}}: error: Failed to parse pattern: '(GIReplaceRegWithFConstant ?:$dst, (i32 0):$k)'
+def replaceregfconstant_namedimm : GICombineRule<
+ (defs root:$dst),
+ (match (COPY $dst, $src)),
+ (apply (GIReplaceRegWithFConstant $dst, (i32 0):$k))>;
+
+// CHECK: :[[@LINE+1]]:{{[0-9]+}}: error: GIReplaceRegWithFConstant cannot replace 'tmp': this builtin can only replace a register defined by the match root
+def replaceregfconstant_nonroot : GICombineRule<
+ (defs root:$dst),
+ (match (COPY $dst, $tmp), (COPY $tmp, $src)),
+ (apply (GIReplaceReg $dst, $src), (GIReplaceRegWithFConstant $tmp, 0))>;
+
// CHECK: error: Failed to parse one or more rules
def MyCombiner: GICombiner<"GenMyCombiner", [
@@ -90,5 +142,13 @@ def MyCombiner: GICombiner<"GenMyCombiner", [
eraseroot_notinstmatch,
replacereg_in_match,
replacereg_ops,
- replacereg_nonroot
+ replacereg_nonroot,
+ replaceregconstant_in_match,
+ replaceregconstant_nonimm,
+ replaceregconstant_namedimm,
+ replaceregconstant_nonroot,
+ replaceregfconstant_in_match,
+ replaceregfconstant_nonimm,
+ replaceregfconstant_namedimm,
+ replaceregfconstant_nonroot
]>;
diff --git a/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/builtin-pattern-parrsing.td b/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/builtin-pattern-parrsing.td
index 9c839ee48d011e..c1897e085f2147 100644
--- a/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/builtin-pattern-parrsing.td
+++ b/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/builtin-pattern-parrsing.td
@@ -49,7 +49,28 @@ def BuiltinTest1 : GICombineRule<
(apply (GIEraseRoot))
>;
+// CHECK: (CombineRule name:BuiltinTest2 id:2 root:dst
+// CHECK-NEXT: (MatchPats
+// CHECK-NEXT: <match_root>__BuiltinTest2_match_0:(CodeGenInstructionPattern G_UITOFP operands:[<def>$dst, 0])
+// CHECK-NEXT: )
+// CHECK-NEXT: (ApplyPats
+// CHECK-NEXT: <apply_root>__BuiltinTest2_apply_0:(BuiltinPattern GIReplaceRegWithFConstant operands:[<def>$dst, 0])
+// CHECK-NEXT: )
+// CHECK-NEXT: (OperandTable MatchPats
+// CHECK-NEXT: dst -> __BuiltinTest2_match_0
+// CHECK-NEXT: )
+// CHECK-NEXT: (OperandTable ApplyPats
+// CHECK-NEXT: dst -> __BuiltinTest2_apply_0
+// CHECK-NEXT: )
+// CHECK-NEXT: )
+def BuiltinTest2 : GICombineRule<
+ (defs root:$dst),
+ (match (G_UITOFP $dst, 0)),
+ (apply (GIReplaceRegWithFConstant $dst, 0))
+>;
+
def MyCombiner: GICombiner<"GenMyCombiner", [
BuiltinTest0,
- BuiltinTest1
+ BuiltinTest1,
+ BuiltinTest2
]>;
diff --git a/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/match-table-replacereg-fconstant.td b/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/match-table-replacereg-fconstant.td
new file mode 100644
index 00000000000000..4661ab8d01a7d5
--- /dev/null
+++ b/llvm/test/TableGen/GlobalISelCombinerEmitter/builtins/match-table-replacereg-fconstant.td
@@ -0,0 +1,51 @@
+// RUN: llvm-tblgen -I %p/../../../../include -gen-global-isel-combiner \
+// RUN: -combiners=MyCombiner %s | \
+// RUN: FileCheck %s
+
+include "llvm/Target/Target.td"
+include "llvm/Target/GlobalISel/Combine.td"
+
+def MyTargetISA : InstrInfo;
+def MyTarget : Target { let InstructionSet = MyTargetISA; }
+
+def ReplaceWithFConstant : GICombineRule<
+ (defs root:$dst),
+ (match (G_UITOFP $dst, 0)),
+ (apply (GIReplaceRegWithFConstant $dst, 0))>;
+
+// Edge cases: the immediate is a raw IEEE bit pattern, not a decimal fp
+// value, so this must survive as -1 (all-ones), not get reinterpreted.
+def ReplaceWithFConstantAllOnes : GICombineRule<
+ (defs root:$dst),
+ (match (G_SITOFP $dst, 0)),
+ (apply (GIReplaceRegWithFConstant $dst, -1))>;
+
+// Arbitrary non-zero bit pattern (single-precision pi/4).
+def ReplaceWithFConstantArbitrary : GICombineRule<
+ (defs root:$dst),
+ (match (G_FPEXT $dst, 0)),
+ (apply (GIReplaceRegWithFConstant $dst, 0x3f490fdb))>;
+
+def MyCombiner: GICombiner<"GenMyCombiner", [
+ ReplaceWithFConstant,
+ ReplaceWithFConstantAllOnes,
+ ReplaceWithFConstantArbitrary
+]>;
+
+// CHECK: GIR_MakeTempReg, /*TempRegID*/0, /*TypeID*/
+// CHECK-NEXT: GIR_BuildFConstant, /*TempRegID*/0, /*Val*/GIMT_Encode8(1061752795),
+// CHECK-NEXT: // Combiner Rule #2: ReplaceWithFConstantArbitrary
+// CHECK-NEXT: GIR_ReplaceRegWithTempReg, /*OldInsnID*/0, /*OldOpIdx*/0, /*TempRegID*/0,
+// CHECK-NEXT: GIR_EraseRootFromParent_Done,
+
+// CHECK: GIR_MakeTempReg, /*TempRegID*/0, /*TypeID*/
+// CHECK-NEXT: GIR_BuildFConstant, /*TempRegID*/0, /*Val*/GIMT_Encode8(18446744073709551615u),
+// CHECK-NEXT: // Combiner Rule #1: ReplaceWithFConstantAllOnes
+// CHECK-NEXT: GIR_ReplaceRegWithTempReg, /*OldInsnID*/0, /*OldOpIdx*/0, /*TempRegID*/0,
+// CHECK-NEXT: GIR_EraseRootFromParent_Done,
+
+// CHECK: GIR_MakeTempReg, /*TempRegID*/0, /*TypeID*/
+// CHECK-NEXT: GIR_BuildFConstant, /*TempRegID*/0, /*Val*/GIMT_Encode8(0),
+// CHECK-NEXT: // Combiner Rule #0: ReplaceWithFConstant
+// CHECK-NEXT: GIR_ReplaceRegWithTempReg, /*OldInsnID*/0, /*OldOpIdx*/0, /*TempRegID*/0,
+// CHECK-NEXT: GIR_EraseRootFromParent_Done,
diff --git a/llvm/utils/TableGen/Common/GlobalISel/MatchTable/Matchers.cpp b/llvm/utils/TableGen/Common/GlobalISel/MatchTable/Matchers.cpp
index cc1089fc316c7a..90aaee43079ff0 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/MatchTable/Matchers.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/MatchTable/Matchers.cpp
@@ -2076,6 +2076,15 @@ void BuildConstantAction::emitActionOpcodes(MatchTable &Table) const {
<< MatchTable::IntValue(8, Val) << MatchTable::LineBreak;
}
+//===- BuildFConstantAction -----------------------------------------------===//
+
+void BuildFConstantAction::emitActionOpcodes(MatchTable &Table) const {
+ Table << MatchTable::Opcode("GIR_BuildFConstant")
+ << MatchTable::Comment("TempRegID")
+ << MatchTable::ULEB128Value(TempRegID) << MatchTable::Comment("Val")
+ << MatchTable::IntValue(8, Val) << MatchTable::LineBreak;
+}
+
//===- EraseInstAction ----------------------------------------------------===//
void EraseInstAction::emitActionOpcodes(MatchTable &Table) const {
diff --git a/llvm/utils/TableGen/Common/GlobalISel/MatchTable/Matchers.h b/llvm/utils/TableGen/Common/GlobalISel/MatchTable/Matchers.h
index b7ea01279eea95..20fc7e368ec30d 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/MatchTable/Matchers.h
+++ b/llvm/utils/TableGen/Common/GlobalISel/MatchTable/Matchers.h
@@ -2191,6 +2191,7 @@ class MatchAction {
AK_DebugComment,
AK_BuildMI,
AK_BuildConstantMI,
+ AK_BuildFConstantMI,
AK_EraseInst,
AK_ReplaceReg,
AK_ConstraintOpsToDef,
@@ -2320,6 +2321,23 @@ class BuildConstantAction : public MatchAction {
void emitActionOpcodes(MatchTable &Table) const override;
};
+/// Generates code to create an FP constant that defines a TempReg.
+/// \p Val is the IEEE bit pattern of the TempReg's (scalar) type.
+class BuildFConstantAction : public MatchAction {
+ unsigned TempRegID;
+ int64_t Val;
+
+public:
+ BuildFConstantAction(unsigned TempRegID, int64_t Val)
+ : MatchAction(AK_BuildFConstantMI), TempRegID(TempRegID), Val(Val) {}
+
+ static bool classof(const MatchAction *A) {
+ return A->getKind() == AK_BuildFConstantMI;
+ }
+
+ void emitActionOpcodes(MatchTable &Table) const override;
+};
+
class EraseInstAction : public MatchAction {
unsigned InsnID;
diff --git a/llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp b/llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp
index dfd37fd64a6520..9be0904559dc89 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp
@@ -873,8 +873,20 @@ bool BuiltinPattern::checkSemantics(ArrayRef<SMLoc> Loc) {
if (!InstructionPattern::checkSemantics(Loc))
return false;
- // For now all builtins just take names, no immediates.
+ // All builtins just take names, except for the second operand of
+ // GIReplaceRegWithConstant/GIReplaceRegWithFConstant, which is a literal.
for (const auto &[Idx, Op] : enumerate(operands())) {
+ if ((I.Kind == BI_ReplaceRegWithConstant ||
+ I.Kind == BI_ReplaceRegWithFConstant) &&
+ Idx == 1) {
+ if (!Op.hasImmValue() || Op.isNamedOperand()) {
+ PrintError(Loc, "expected operand " + std::to_string(Idx) + " of '" +
+ getInstName() + "' to be an immediate value");
+ return false;
+ }
+ continue;
+ }
+
if (!Op.isNamedOperand() || Op.isNamedImmediate()) {
PrintError(Loc, "expected operand " + std::to_string(Idx) + " of '" +
getInstName() + "' to be a name");
diff --git a/llvm/utils/TableGen/Common/GlobalISel/Patterns.h b/llvm/utils/TableGen/Common/GlobalISel/Patterns.h
index 2bf2a22ee99988..832ee22b05c9b0 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/Patterns.h
+++ b/llvm/utils/TableGen/Common/GlobalISel/Patterns.h
@@ -691,6 +691,8 @@ class PatFragPattern : public InstructionPattern {
/// Represents builtin instructions such as "GIReplaceReg" and "GIEraseRoot".
enum BuiltinKind {
BI_ReplaceReg,
+ BI_ReplaceRegWithConstant,
+ BI_ReplaceRegWithFConstant,
BI_EraseRoot,
};
@@ -702,8 +704,10 @@ class BuiltinPattern : public InstructionPattern {
unsigned NumDefs;
};
- static constexpr std::array<BuiltinInfo, 2> KnownBuiltins = {{
+ static constexpr std::array<BuiltinInfo, 4> KnownBuiltins = {{
{"GIReplaceReg", BI_ReplaceReg, 2, 1},
+ {"GIReplaceRegWithConstant", BI_ReplaceRegWithConstant, 2, 1},
+ {"GIReplaceRegWithFConstant", BI_ReplaceRegWithFConstant, 2, 1},
{"GIEraseRoot", BI_EraseRoot, 0, 0},
}};
diff --git a/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp b/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
index bb93d0248e55f2..d4d3ce955ee0a5 100644
--- a/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
@@ -1353,8 +1353,10 @@ bool CombineRuleBuilder::checkSemantics() {
}
break;
}
- case BI_ReplaceReg: {
- // (GIReplaceReg can only be used on the root instruction)
+ case BI_ReplaceReg:
+ case BI_ReplaceRegWithConstant:
+ case BI_ReplaceRegWithFConstant: {
+ // (can only be used on the root instruction)
// TODO: When we allow rewriting non-root instructions, also allow this.
StringRef OldRegName = BIP->getOperand(0).getOperandName();
auto *Def = MatchOpTable.getDef(OldRegName);
@@ -1369,6 +1371,12 @@ bool CombineRuleBuilder::checkSemantics() {
"match root");
return false;
}
+ if ((BIP->getBuiltinKind() == BI_ReplaceRegWithConstant ||
+ BIP->getBuiltinKind() == BI_ReplaceRegWithFConstant) &&
+ !BIP->getOperand(1).hasImmValue()) {
+ PrintError(Name + " expects an immediate value as its second operand");
+ return false;
+ }
break;
}
}
@@ -2206,6 +2214,37 @@ bool CombineRuleBuilder::emitBuiltinApplyPattern(
assert(MatchOpTable.getDef(Old) == MatchRoot);
return true;
}
+ case BI_ReplaceRegWithConstant: {
+ StringRef Old = P.getOperand(0).getOperandName();
+ int64_t Val = P.getOperand(1).getImmValue();
+
+ auto &OldOM = M.getOperandMatcher(Old);
+ unsigned TempRegID = M.allocateTempRegID();
+ auto InsertIt = M.insertAction<MakeTempRegisterAction>(
+ M.actions_begin(), OldOM.getTempTypeIdx(M), TempRegID);
+ M.insertAction<BuildConstantAction>(++InsertIt, TempRegID, Val);
+
+ M.addAction<ReplaceRegAction>(OldOM.getInsnVarID(), OldOM.getOpIdx(),
+ TempRegID);
+ assert(MatchOpTable.getDef(Old) == MatchRoot);
+ return true;
+ }
+ case BI_ReplaceRegWithFConstant: {
+ StringRef Old = P.getOperand(0).getOperandName();
+ // Imm is the IEEE bit pattern of $reg's (scalar) type.
+ int64_t Val = P.getOperand(1).getImmValue();
+
+ auto &OldOM = M.getOperandMatcher(Old);
+ unsigned TempRegID = M.allocateTempRegID();
+ auto InsertIt = M.insertAction<MakeTempRegisterAction>(
+ M.actions_begin(), OldOM.getTempTypeIdx(M), TempRegID);
+ M.insertAction<BuildFConstantAction>(++InsertIt, TempRegID, Val);
+
+ M.addAction<ReplaceRegAction>(OldOM.getInsnVarID(), OldOM.getOpIdx(),
+ TempRegID);
+ assert(MatchOpTable.getDef(Old) == MatchRoot);
+ return true;
+ }
}
llvm_unreachable("Unknown BuiltinKind!");
More information about the llvm-commits
mailing list