[llvm] [RISCV][GlobalISel] Select G_GLOBAL_VALUE (PR #70091)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 24 12:10:54 PDT 2023
================
@@ -483,6 +489,66 @@ bool RISCVInstructionSelector::selectConstant(MachineInstr &MI,
return true;
}
+bool RISCVInstructionSelector::selectGlobalValue(
+ MachineInstr &MI, MachineIRBuilder &MIB, MachineRegisterInfo &MRI) const {
+ assert(MI.getOpcode() == TargetOpcode::G_GLOBAL_VALUE &&
+ "Expected G_GLOBAL_VALUE");
+ auto GV = MI.getOperand(1).getGlobal();
+ if (GV->isThreadLocal()) {
+ // TODO: implement this case.
+ return false;
+ }
+
+ switch (TM.getCodeModel()) {
+ default:
+ report_fatal_error("Unsupported code model for lowering");
+ case CodeModel::Small: {
+ // Must lie within a single 2 GiB address range and must lie between
+ // absolute addresses -2 GiB and +2 GiB. This generates the pattern (addi
+ // (lui %hi(sym)) %lo(sym)).
+ Register AddrHiDest = MRI.createVirtualRegister(&RISCV::GPRRegClass);
+ MachineInstr *AddrHi = MIB.buildInstr(RISCV::LUI)
+ .addDef(AddrHiDest)
+ .addGlobalAddress(GV, RISCVII::MO_HI);
+ if (!constrainSelectedInstRegOperands(*AddrHi, TII, TRI, RBI))
+ return false;
+
+ Register DstReg = MI.getOperand(0).getReg();
+ MachineInstr *Result = MIB.buildInstr(RISCV::ADDI)
+ .addDef(DstReg)
+ .addReg(AddrHiDest)
+ .addGlobalAddress(GV, 0, RISCVII::MO_LO);
+ if (!constrainSelectedInstRegOperands(*Result, TII, TRI, RBI))
+ return false;
+
+ MI.eraseFromParent();
+ return true;
+ }
+ case CodeModel::Medium: {
+ // Must lie within *any* 2GiB range. The generates (addi (auipc
+ // %pcrel_hi(sym)) %pcrel_lo(auipc)).
+ Register AddrHiDest = MRI.createVirtualRegister(&RISCV::GPRRegClass);
+ MachineInstr *AddrHi = MIB.buildInstr(RISCV::AUIPC)
----------------
topperc wrote:
This needs to be emitted as PseudoLGA and get expanded later. The pcrel_lo relocation needs to reference a label that points to the AUIPC instruction itself, not the global. We can't do that from inside the instruction selector.
https://github.com/llvm/llvm-project/pull/70091
More information about the llvm-commits
mailing list