[llvm] [GlobalIsel] Combine cast of const integer. (PR #96139)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 02:15:11 PDT 2024


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


================
@@ -7428,12 +7428,98 @@ bool CombinerHelper::matchNonNegZext(const MachineOperand &MO,
   LLT SrcTy = MRI.getType(Src);
   const auto &TLI = getTargetLowering();
 
+  const MachineFunction &MF = *MO.getParent()->getMF();
+  LLVMContext &Ctx = MF.getFunction().getContext();
+  auto &DL = MF.getDataLayout();
+
   // Convert zext nneg to sext if sext is the preferred form for the target.
   if (isLegalOrBeforeLegalizer({TargetOpcode::G_SEXT, {DstTy, SrcTy}}) &&
-      TLI.isSExtCheaperThanZExt(getMVTForLLT(SrcTy), getMVTForLLT(DstTy))) {
+      TLI.isSExtCheaperThanZExt(SrcTy, DstTy, DL, Ctx)) {
     MatchInfo = [=](MachineIRBuilder &B) { B.buildSExt(Dst, Src); };
     return true;
   }
 
   return false;
 }
+
+bool CombinerHelper::matchZextInteger(const MachineInstr &MI,
+                                      APInt &MatchInfo) {
+  const GZext *Zext = cast<GZext>(&MI);
+
+  std::optional<APInt> Input = getIConstantVRegVal(Zext->getSrcReg(), MRI);
+  if (!Input)
+    return false;
+
+  LLT DstTy = MRI.getType(Zext->getReg(0));
+
+  if (!isConstantLegalOrBeforeLegalizer(DstTy))
+    return false;
+
+  MatchInfo = Input->zext(DstTy.getScalarSizeInBits());
+
+  return true;
+}
+
+bool CombinerHelper::matchSextInteger(const MachineInstr &MI,
+                                      APInt &MatchInfo) {
+  const GSext *Sext = cast<GSext>(&MI);
+
+  std::optional<APInt> Input = getIConstantVRegVal(Sext->getSrcReg(), MRI);
+  if (!Input)
+    return false;
----------------
arsenm wrote:

Cannot fail 

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


More information about the llvm-commits mailing list