[llvm] [RISCV][GlobalISel] Create generic instructions to handle legalization and selection of G_GLOBAL_VALUE for medlow code model (PR #69562)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 29 21:55:34 PST 2024
================
@@ -176,10 +181,39 @@ RISCVLegalizerInfo::RISCVLegalizerInfo(const RISCVSubtarget &ST) {
bool RISCVLegalizerInfo::legalizeCustom(LegalizerHelper &Helper,
MachineInstr &MI) const {
+ MachineIRBuilder &MIB = Helper.MIRBuilder;
+
switch (MI.getOpcode()) {
default:
// No idea what to do.
return false;
+ case TargetOpcode::G_GLOBAL_VALUE: {
+ auto &TM = ST->getTargetLowering()->getTargetMachine();
+
+ // FIXME: Handle position independent code.
+ if (TM.isPositionIndependent())
+ return false;
+
+ Register DstReg = MI.getOperand(0).getReg();
+ MachineOperand &GlobalOp = MI.getOperand(1);
+ const GlobalValue *GV = GlobalOp.getGlobal();
+ int64_t Offset = GlobalOp.getOffset();
+
+ switch (TM.getCodeModel()) {
+ case CodeModel::Small: {
+ auto AddrHi = MIB.buildInstr(RISCV::G_RISCV_HI, {LLT::pointer(0, 64)}, {})
+ .addGlobalAddress(GV, Offset, RISCVII::MO_HI);
+
+ MIB.buildInstr(RISCV::G_RISCV_ADD_LO, {DstReg}, {AddrHi})
+ .addGlobalAddress(GV, Offset, RISCVII::MO_LO);
+
+ MI.eraseFromParent();
+ return true;
+ }
+ default:
+ llvm_unreachable("Code model not handled!");
----------------
arsenm wrote:
should prefer to fail legalization by returning false instead of asserting
https://github.com/llvm/llvm-project/pull/69562
More information about the llvm-commits
mailing list