Skip to content

Commit f465b1a

Browse files
committed
[GlobalISel] Tweak lowering of G_SMULO/G_UMULO
Summary: Applying this cleanup: - MIRBuilder.buildInstr(TargetOpcode::G_ASHR) - .addDef(Shifted) - .addUse(Res) - .addUse(ShiftAmt); + MIRBuilder.buildAShr(Shifted, Res, ShiftAmt); caused an assertion failure here: llc: /home/jayfoad2/git/llvm-project/llvm/lib/CodeGen/MachineRegisterInfo.cpp:404: llvm::MachineInstr *llvm::MachineRegisterInfo::getVRegDef(unsigned int) const: Assertion `(I.atEnd() || std::next(I) == def_instr_end()) && "getVRegDef assumes a single definition or no definition"' failed. #4 0x00000000050a6d96 in llvm::MachineRegisterInfo::getVRegDef (this=0x74606a0, Reg=2147483650) at /home/jayfoad2/git/llvm-project/llvm/lib/CodeGen/MachineRegisterInfo.cpp:403 #5 0x00000000066148f6 in llvm::getConstantVRegValWithLookThrough (VReg=2147483650, MRI=..., LookThroughInstrs=false, HandleFConstant=true) at /home/jayfoad2/git/llvm-project/llvm/lib/CodeGen/GlobalISel/Utils.cpp:244 #6 0x00000000066147da in llvm::getConstantVRegVal (VReg=2147483650, MRI=...) at /home/jayfoad2/git/llvm-project/llvm/lib/CodeGen/GlobalISel/Utils.cpp:210 #7 0x0000000006615367 in llvm::ConstantFoldBinOp (Opcode=101, Op1=2147483650, Op2=2147483656, MRI=...) at /home/jayfoad2/git/llvm-project/llvm/lib/CodeGen/GlobalISel/Utils.cpp:341 #8 0x000000000657eee0 in llvm::CSEMIRBuilder::buildInstr (this=0x7465010, Opc=101, DstOps=..., SrcOps=..., Flag=...) at /home/jayfoad2/git/llvm-project/llvm/lib/CodeGen/GlobalISel/CSEMIRBuilder.cpp:160 #9 0x0000000003645958 in llvm::MachineIRBuilder::buildAShr (this=0x7465010, Dst=..., Src0=..., Src1=..., Flags=...) at /home/jayfoad2/git/llvm-project/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h:1298 #10 0x00000000065c35b1 in llvm::LegalizerHelper::lower (this=0x7fffffffb5f8, MI=..., TypeIdx=0, Ty=...) at /home/jayfoad2/git/llvm-project/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp:2020 because at this point there are two instructions defining Res: the original G_SMULO/G_UMULO and the new G_MUL that we built. The fix is to modify the original mul in place, so that there is only ever one definition of Res. Reviewers: arsenm, aditya_nandakumar Subscribers: wdng, rovka, hiraditya, volkan, Petar.Avramovic, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D72842
1 parent b9bf930 commit f465b1a

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

+11-15
Original file line numberDiff line numberDiff line change
@@ -2131,36 +2131,32 @@ LegalizerHelper::lower(MachineInstr &MI, unsigned TypeIdx, LLT Ty) {
21312131
Register LHS = MI.getOperand(2).getReg();
21322132
Register RHS = MI.getOperand(3).getReg();
21332133

2134-
MIRBuilder.buildMul(Res, LHS, RHS);
2135-
21362134
unsigned Opcode = MI.getOpcode() == TargetOpcode::G_SMULO
21372135
? TargetOpcode::G_SMULH
21382136
: TargetOpcode::G_UMULH;
21392137

2140-
Register HiPart = MRI.createGenericVirtualRegister(Ty);
2141-
MIRBuilder.buildInstr(Opcode)
2142-
.addDef(HiPart)
2143-
.addUse(LHS)
2144-
.addUse(RHS);
2138+
Observer.changingInstr(MI);
2139+
const auto &TII = MIRBuilder.getTII();
2140+
MI.setDesc(TII.get(TargetOpcode::G_MUL));
2141+
MI.RemoveOperand(1);
2142+
Observer.changedInstr(MI);
2143+
2144+
MIRBuilder.setInsertPt(MIRBuilder.getMBB(), ++MIRBuilder.getInsertPt());
2145+
2146+
auto HiPart = MIRBuilder.buildInstr(Opcode, {Ty}, {LHS, RHS});
21452147

21462148
Register Zero = MRI.createGenericVirtualRegister(Ty);
21472149
MIRBuilder.buildConstant(Zero, 0);
21482150

21492151
// For *signed* multiply, overflow is detected by checking:
21502152
// (hi != (lo >> bitwidth-1))
21512153
if (Opcode == TargetOpcode::G_SMULH) {
2152-
Register Shifted = MRI.createGenericVirtualRegister(Ty);
2153-
Register ShiftAmt = MRI.createGenericVirtualRegister(Ty);
2154-
MIRBuilder.buildConstant(ShiftAmt, Ty.getSizeInBits() - 1);
2155-
MIRBuilder.buildInstr(TargetOpcode::G_ASHR)
2156-
.addDef(Shifted)
2157-
.addUse(Res)
2158-
.addUse(ShiftAmt);
2154+
auto ShiftAmt = MIRBuilder.buildConstant(Ty, Ty.getSizeInBits() - 1);
2155+
auto Shifted = MIRBuilder.buildAShr(Ty, Res, ShiftAmt);
21592156
MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Shifted);
21602157
} else {
21612158
MIRBuilder.buildICmp(CmpInst::ICMP_NE, Overflow, HiPart, Zero);
21622159
}
2163-
MI.eraseFromParent();
21642160
return Legalized;
21652161
}
21662162
case TargetOpcode::G_FNEG: {

0 commit comments

Comments
 (0)