[llvm] [GlobalISel][TableGen] Adds GIReplaceRegWithConstant/GIReplaceRegWithFConstant MIR builtin patterns (PR #219563)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 28 13:55:10 PDT 2026


================
@@ -435,7 +435,32 @@ def Foo : GICombineRule<
   (apply (GIEraseRoot))>;
 ```
 
-#### Common Pattern #3: Emitting a Constant Value
+#### Common Pattern #3: Replacing the Root with a Constant
+
+If we want to replace all uses of a register defined by the match root with a
+freshly materialized constant of the same type, we can use the
+`GIReplaceRegWithConstant` and `GIReplaceRegWithFConstant` builtins. They are
+the declarative forms of `Helper.replaceInstWithConstant` and
+`Helper.replaceInstWithFConstant`.
+
+```text
+// Replaces all uses of $dst with a G_CONSTANT of $dst's type, then erases the
+// root.
+def Foo : GICombineRule<
+  (defs root:$dst),
+  (match (G_FOO $dst, $src)),
+  (apply (GIReplaceRegWithConstant $dst, 42))>;
----------------
arsenm wrote:

Why does this need a custom operator? I'd hope you can just write this directly as the new constant instruction by its usual name 

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


More information about the llvm-commits mailing list