[llvm] [GlobalISel] Combine G_ZEXT of undef -> 0 (PR #113764)

Amara Emerson via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 30 09:54:59 PDT 2024


Thorsten =?utf-8?q?Schütt?= <schuett at gmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/113764 at github.com>


================
@@ -2916,8 +2916,11 @@ void CombinerHelper::replaceInstWithFConstant(MachineInstr &MI, double C) {
 
 void CombinerHelper::replaceInstWithConstant(MachineInstr &MI, int64_t C) {
   assert(MI.getNumDefs() == 1 && "Expected only one def?");
-  Builder.buildConstant(MI.getOperand(0), C);
-  MI.eraseFromParent();
+  LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
+  if (isConstantLegalOrBeforeLegalizer(DstTy)) {
----------------
aemerson wrote:

> One option is to move away from the generic matchers `Helper.matchOperandIsUndef` to specific matchers for `unary_undef_to_zero`, i.e., `Helper.matchUndefToZero`.

Why not just check in the matcher C++ code? The matcher already knows what the intended output looks like, it just defers the actual mutation to the applicator.

> 
> But I really really hate to have a function that builds something and I have to search around if and where the legality check is.

If I understand right you're basically saying you don't like the combiner infrastructure as it was designed, because that's how it's supposed to work.

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


More information about the llvm-commits mailing list