[llvm] [GISel] Convert zext nneg to sext if it is cheaper (PR #93856)

Thorsten Schütt via llvm-commits llvm-commits at lists.llvm.org
Thu May 30 23:39:57 PDT 2024


================
@@ -7407,3 +7407,23 @@ bool CombinerHelper::matchZextOfTrunc(const MachineOperand &MO,
 
   return false;
 }
+
+bool CombinerHelper::matchNonNegZext(const MachineOperand &MO,
+                                     BuildFnTy &MatchInfo) {
+  GZext *Zext = cast<GZext>(MRI.getVRegDef(MO.getReg()));
+
+  Register Dst = Zext->getReg(0);
+  Register Src = Zext->getSrcReg();
+
+  LLT DstTy = MRI.getType(Dst);
+  LLT SrcTy = MRI.getType(Src);
+  const auto &TLI = getTargetLowering();
+
+  // Convert zext nneg to sext if sext is the preferred form for the target.
+  if (TLI.isSExtCheaperThanZExt(getMVTForLLT(SrcTy), getMVTForLLT(DstTy))) {
+    MatchInfo = [=](MachineIRBuilder &B) { B.buildSExt(Dst, Src); };
----------------
tschuett wrote:

```
if (!isLegalOrBeforeLegalizer({TargetOpcode::G_SEXT, {DstTy, SrcTy}}))
  return false;
```
We have to check whether the sext is legal or we are before the legalizer..

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


More information about the llvm-commits mailing list