[llvm] 62ce4b0 - [GlobalISel] Combine narrowScalar of G_ADD and G_SUB. NFC
Justin Bogner via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 3 11:06:28 PST 2021
Author: Justin Bogner
Date: 2021-02-03T11:06:04-08:00
New Revision: 62ce4b048ff76adb4bb67a40aa28f69e0be26349
URL: https://github.com/llvm/llvm-project/commit/62ce4b048ff76adb4bb67a40aa28f69e0be26349
DIFF: https://github.com/llvm/llvm-project/commit/62ce4b048ff76adb4bb67a40aa28f69e0be26349.diff
LOG: [GlobalISel] Combine narrowScalar of G_ADD and G_SUB. NFC
These two cases have identical implementations other than an
unreachable part of `G_ADD` that checks if the scalar we're narrowing
is a vector. Combining them to avoid unnecessary divergence.
Added:
Modified:
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index 6dc488e8231c..d485031ea14c 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -860,7 +860,8 @@ LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
case TargetOpcode::G_FREEZE:
return reduceOperationWidth(MI, TypeIdx, NarrowTy);
- case TargetOpcode::G_ADD: {
+ case TargetOpcode::G_ADD:
+ case TargetOpcode::G_SUB: {
// FIXME: add support for when SizeOp0 isn't an exact multiple of
// NarrowSize.
if (SizeOp0 % NarrowSize != 0)
@@ -868,60 +869,29 @@ LegalizerHelper::LegalizeResult LegalizerHelper::narrowScalar(MachineInstr &MI,
// Expand in terms of carry-setting/consuming G_ADDE instructions.
int NumParts = SizeOp0 / NarrowTy.getSizeInBits();
+ bool IsAdd = MI.getOpcode() == TargetOpcode::G_ADD;
+ auto Opo = IsAdd ? TargetOpcode::G_UADDO : TargetOpcode::G_USUBO;
+ auto Ope = IsAdd ? TargetOpcode::G_UADDE : TargetOpcode::G_USUBE;
+
SmallVector<Register, 2> Src1Regs, Src2Regs, DstRegs;
extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src1Regs);
extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src2Regs);
- Register CarryIn;
+ Register BitIn;
for (int i = 0; i < NumParts; ++i) {
Register DstReg = MRI.createGenericVirtualRegister(NarrowTy);
- Register CarryOut = MRI.createGenericVirtualRegister(LLT::scalar(1));
+ Register BitOut = MRI.createGenericVirtualRegister(LLT::scalar(1));
if (i == 0)
- MIRBuilder.buildUAddo(DstReg, CarryOut, Src1Regs[i], Src2Regs[i]);
+ MIRBuilder.buildInstr(Opo, {DstReg, BitOut},
+ {Src1Regs[i], Src2Regs[i]});
else {
- MIRBuilder.buildUAdde(DstReg, CarryOut, Src1Regs[i],
- Src2Regs[i], CarryIn);
+ MIRBuilder.buildInstr(Ope, {DstReg, BitOut},
+ {Src1Regs[i], Src2Regs[i], BitIn});
}
DstRegs.push_back(DstReg);
- CarryIn = CarryOut;
- }
- Register DstReg = MI.getOperand(0).getReg();
- if(MRI.getType(DstReg).isVector())
- MIRBuilder.buildBuildVector(DstReg, DstRegs);
- else
- MIRBuilder.buildMerge(DstReg, DstRegs);
- MI.eraseFromParent();
- return Legalized;
- }
- case TargetOpcode::G_SUB: {
- // FIXME: add support for when SizeOp0 isn't an exact multiple of
- // NarrowSize.
- if (SizeOp0 % NarrowSize != 0)
- return UnableToLegalize;
-
- int NumParts = SizeOp0 / NarrowTy.getSizeInBits();
-
- SmallVector<Register, 2> Src1Regs, Src2Regs, DstRegs;
- extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, Src1Regs);
- extractParts(MI.getOperand(2).getReg(), NarrowTy, NumParts, Src2Regs);
-
- Register DstReg = MRI.createGenericVirtualRegister(NarrowTy);
- Register BorrowOut = MRI.createGenericVirtualRegister(LLT::scalar(1));
- MIRBuilder.buildInstr(TargetOpcode::G_USUBO, {DstReg, BorrowOut},
- {Src1Regs[0], Src2Regs[0]});
- DstRegs.push_back(DstReg);
- Register BorrowIn = BorrowOut;
- for (int i = 1; i < NumParts; ++i) {
- DstReg = MRI.createGenericVirtualRegister(NarrowTy);
- BorrowOut = MRI.createGenericVirtualRegister(LLT::scalar(1));
-
- MIRBuilder.buildInstr(TargetOpcode::G_USUBE, {DstReg, BorrowOut},
- {Src1Regs[i], Src2Regs[i], BorrowIn});
-
- DstRegs.push_back(DstReg);
- BorrowIn = BorrowOut;
+ BitIn = BitOut;
}
MIRBuilder.buildMerge(MI.getOperand(0), DstRegs);
MI.eraseFromParent();
More information about the llvm-commits
mailing list