diff options
author | Thorsten Schütt <schuett@gmail.com> | 2024-07-24 22:13:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-24 22:13:52 +0200 |
commit | bfcfb0fd2dea5e06f74e49ff8b4e1dc10c9acf6a (patch) | |
tree | 4c3e0208c4e38fd7b755f1fc29a78112ee8d9e68 /llvm/lib/CodeGen | |
parent | b1f263e4c2466a693609a3930f53b9887be67b5b (diff) | |
download | llvm-bfcfb0fd2dea5e06f74e49ff8b4e1dc10c9acf6a.zip llvm-bfcfb0fd2dea5e06f74e49ff8b4e1dc10c9acf6a.tar.gz llvm-bfcfb0fd2dea5e06f74e49ff8b4e1dc10c9acf6a.tar.bz2 |
[GlobalIsel] Modernize truncate of ext. (#100338)
Credits:
https://github.com/llvm/llvm-project/pull/90964
https://reviews.llvm.org/D87050
combine-trunc.mir
Functional changes intended.
Diffstat (limited to 'llvm/lib/CodeGen')
-rw-r--r-- | llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp | 34 | ||||
-rw-r--r-- | llvm/lib/CodeGen/GlobalISel/CombinerHelperCasts.cpp | 48 |
2 files changed, 48 insertions, 34 deletions
diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp index 208f554..8c05931 100644 --- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp +++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp @@ -2582,40 +2582,6 @@ void CombinerHelper::applyCombineExtOfExt( } } -bool CombinerHelper::matchCombineTruncOfExt( - MachineInstr &MI, std::pair<Register, unsigned> &MatchInfo) { - assert(MI.getOpcode() == TargetOpcode::G_TRUNC && "Expected a G_TRUNC"); - Register SrcReg = MI.getOperand(1).getReg(); - MachineInstr *SrcMI = MRI.getVRegDef(SrcReg); - unsigned SrcOpc = SrcMI->getOpcode(); - if (SrcOpc == TargetOpcode::G_ANYEXT || SrcOpc == TargetOpcode::G_SEXT || - SrcOpc == TargetOpcode::G_ZEXT) { - MatchInfo = std::make_pair(SrcMI->getOperand(1).getReg(), SrcOpc); - return true; - } - return false; -} - -void CombinerHelper::applyCombineTruncOfExt( - MachineInstr &MI, std::pair<Register, unsigned> &MatchInfo) { - assert(MI.getOpcode() == TargetOpcode::G_TRUNC && "Expected a G_TRUNC"); - Register SrcReg = MatchInfo.first; - unsigned SrcExtOp = MatchInfo.second; - Register DstReg = MI.getOperand(0).getReg(); - LLT SrcTy = MRI.getType(SrcReg); - LLT DstTy = MRI.getType(DstReg); - if (SrcTy == DstTy) { - MI.eraseFromParent(); - replaceRegWith(MRI, DstReg, SrcReg); - return; - } - if (SrcTy.getSizeInBits() < DstTy.getSizeInBits()) - Builder.buildInstr(SrcExtOp, {DstReg}, {SrcReg}); - else - Builder.buildTrunc(DstReg, SrcReg); - MI.eraseFromParent(); -} - static LLT getMidVTForTruncRightShiftCombine(LLT ShiftTy, LLT TruncTy) { const unsigned ShiftSize = ShiftTy.getScalarSizeInBits(); const unsigned TruncSize = TruncTy.getScalarSizeInBits(); diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelperCasts.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelperCasts.cpp index 8fe69f2..d36685b 100644 --- a/llvm/lib/CodeGen/GlobalISel/CombinerHelperCasts.cpp +++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelperCasts.cpp @@ -113,3 +113,51 @@ bool CombinerHelper::matchNonNegZext(const MachineOperand &MO, return false; } + +bool CombinerHelper::matchTruncateOfExt(const MachineInstr &Root, + const MachineInstr &ExtMI, + BuildFnTy &MatchInfo) { + const GTrunc *Trunc = cast<GTrunc>(&Root); + const GExtOp *Ext = cast<GExtOp>(&ExtMI); + + if (!MRI.hasOneNonDBGUse(Ext->getReg(0))) + return false; + + Register Dst = Trunc->getReg(0); + Register Src = Ext->getSrcReg(); + LLT DstTy = MRI.getType(Dst); + LLT SrcTy = MRI.getType(Src); + + if (SrcTy == DstTy) { + // The source and the destination are equally sized. We need to copy. + MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); }; + + return true; + } + + if (SrcTy.getScalarSizeInBits() < DstTy.getScalarSizeInBits()) { + // If the source is smaller than the destination, we need to extend. + + if (!isLegalOrBeforeLegalizer({Ext->getOpcode(), {DstTy, SrcTy}})) + return false; + + MatchInfo = [=](MachineIRBuilder &B) { + B.buildInstr(Ext->getOpcode(), {Dst}, {Src}); + }; + + return true; + } + + if (SrcTy.getScalarSizeInBits() > DstTy.getScalarSizeInBits()) { + // If the source is larger than the destination, then we need to truncate. + + if (!isLegalOrBeforeLegalizer({TargetOpcode::G_TRUNC, {DstTy, SrcTy}})) + return false; + + MatchInfo = [=](MachineIRBuilder &B) { B.buildTrunc(Dst, Src); }; + + return true; + } + + return false; +} |