36 #define DEBUG_TYPE "riscv-merge-base-offset" 37 #define RISCV_MERGE_BASE_OFFSET_NAME "RISCV Merge Base Offset" 49 bool matchLargeOffset(
MachineInstr &TailAdd,
unsigned GSReg, int64_t &Offset);
63 std::set<MachineInstr *> DeadInstrs;
83 if (HiLUI.getOpcode() != RISCV::LUI ||
86 HiLUI.getOperand(1).getOffset() != 0 ||
87 !
MRI->hasOneUse(HiLUI.getOperand(0).getReg()))
89 unsigned HiLuiDestReg = HiLUI.getOperand(0).getReg();
90 LoADDI =
MRI->use_begin(HiLuiDestReg)->getParent();
91 if (LoADDI->getOpcode() != RISCV::ADDI ||
94 LoADDI->getOperand(2).getOffset() != 0 ||
95 !
MRI->hasOneUse(LoADDI->getOperand(0).getReg()))
103 void RISCVMergeBaseOffsetOpt::foldOffset(
MachineInstr &HiLUI,
110 DeadInstrs.insert(&Tail);
113 LLVM_DEBUG(
dbgs() <<
" Merged offset " << Offset <<
" into base.\n" 114 <<
" " << HiLUI <<
" " << LoADDI;);
135 bool RISCVMergeBaseOffsetOpt::matchLargeOffset(
MachineInstr &TailAdd,
141 unsigned Reg = Rs == GAReg ? Rt : Rs;
144 if (!
MRI->hasOneUse(Reg))
148 if (OffsetTail.
getOpcode() == RISCV::ADDI) {
154 int64_t OffLo = AddiImmOp.
getImm();
158 if (OffsetLui.
getOpcode() != RISCV::LUI ||
163 Offset = (OffHi << 12) + OffLo;
165 <<
" " << OffsetLui);
166 DeadInstrs.insert(&OffsetTail);
167 DeadInstrs.insert(&OffsetLui);
169 }
else if (OffsetTail.
getOpcode() == RISCV::LUI) {
174 DeadInstrs.insert(&OffsetTail);
180 bool RISCVMergeBaseOffsetOpt::detectAndFoldOffset(
MachineInstr &HiLUI,
183 assert(
MRI->hasOneUse(DestReg) &&
"expected one use for LoADDI");
186 switch (Tail.getOpcode()) {
188 LLVM_DEBUG(
dbgs() <<
"Don't know how to get offset from this instr:" 193 int64_t
Offset = Tail.getOperand(2).getImm();
195 foldOffset(HiLUI, LoADDI, Tail, Offset);
208 if (!matchLargeOffset(Tail, DestReg, Offset))
210 foldOffset(HiLUI, LoADDI, Tail, Offset);
232 if (Tail.getOperand(1).isFI())
236 unsigned BaseAddrReg = Tail.getOperand(1).getReg();
237 if (DestReg != BaseAddrReg)
244 Tail.RemoveOperand(2);
247 Tail.addOperand(ImmOp);
252 DeadInstrs.insert(&LoADDI);
259 bool RISCVMergeBaseOffsetOpt::runOnMachineFunction(
MachineFunction &Fn) {
269 if (!detectLuiAddiGlobal(HiLUI, LoADDI))
271 LLVM_DEBUG(
dbgs() <<
" Found lowered global address with one use: " 274 detectAndFoldOffset(HiLUI, *LoADDI);
278 for (
auto *
MI : DeadInstrs)
279 MI->eraseFromParent();
285 return new RISCVMergeBaseOffsetOpt();
unsigned getTargetFlags() const
This class represents lattice values for constants.
unsigned getReg() const
getReg - Returns the register number.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
#define RISCV_MERGE_BASE_OFFSET_NAME
Simple integer binary arithmetic operators.
Address of a global value.
unsigned const MachineRegisterInfo * MRI
This instruction implements an extending load to FP stack slots.
const GlobalValue * getGlobal() const
FunctionPass class - This class is used to implement most global optimizations.
void setOffset(int64_t Offset)
MachineOperand class - Representation of each machine instruction operand.
INITIALIZE_PASS(RISCVMergeBaseOffsetOpt, "riscv-merge-base-offset", RISCV_MERGE_BASE_OFFSET_NAME, false, false) bool RISCVMergeBaseOffsetOpt
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
FunctionPass * createRISCVMergeBaseOffsetOptPass()
Returns an instance of the Merge Base Offset Optimization pass.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
const MachineOperand & getOperand(unsigned i) const
Properties which a MachineFunction may have at a given point in time.