24#define DEBUG_TYPE "riscv-merge-base-offset"
25#define RISCV_MERGE_BASE_OFFSET_NAME "RISC-V Merge Base Offset"
64char RISCVMergeBaseOffsetOpt::ID = 0;
88 auto HiOpc =
Hi.getOpcode();
89 if (HiOpc != RISCV::LUI && HiOpc != RISCV::AUIPC &&
90 HiOpc != RISCV::PseudoMovAddr && HiOpc != RISCV::QC_E_LI)
104 if (HiOpc == RISCV::PseudoMovAddr || HiOpc == RISCV::QC_E_LI) {
109 Register HiDestReg =
Hi.getOperand(0).getReg();
110 if (!
MRI->hasOneUse(HiDestReg))
113 Lo = &*
MRI->use_instr_begin(HiDestReg);
114 if (
Lo->getOpcode() != RISCV::ADDI)
118 if (HiOpc != RISCV::QC_E_LI) {
120 if (HiOpc == RISCV::LUI || HiOpc == RISCV::PseudoMovAddr) {
126 assert(HiOpc == RISCV::AUIPC);
134 LLVM_DEBUG(dbgs() <<
" Found lowered global address: "
135 << *HiOp1.getGlobal() <<
"\n");
137 LLVM_DEBUG(dbgs() <<
" Found lowered basic address: "
138 << *HiOp1.getBlockAddress() <<
"\n");
140 LLVM_DEBUG(dbgs() <<
" Found lowered constant pool: " << HiOp1.getIndex()
157 auto HiOpc =
Hi.getOpcode();
158 if (HiOpc == RISCV::AUIPC &&
Hi.getOperand(1).isGlobal()) {
159 const GlobalValue *GV =
Hi.getOperand(1).getGlobal();
168 if (
Hi.getOpcode() != RISCV::AUIPC &&
Hi.getOpcode() != RISCV::QC_E_LI)
171 Register LoOp0Reg =
Lo.getOperand(0).getReg();
173 MRI->constrainRegClass(LoOp0Reg,
MRI->getRegClass(TailOp0Reg));
174 MRI->replaceRegWith(TailOp0Reg, LoOp0Reg);
175 Tail.eraseFromParent();
177 <<
" " <<
Hi <<
" " <<
Lo;);
201bool RISCVMergeBaseOffsetOpt::foldLargeOffset(MachineInstr &
Hi,
203 MachineInstr &TailAdd,
205 assert((TailAdd.
getOpcode() == RISCV::ADD) &&
"Expected ADD instruction!");
214 MachineInstr &OffsetTail = *
MRI->getVRegDef(
Reg);
215 auto OffsetTailOpc = OffsetTail.
getOpcode();
216 if (OffsetTailOpc == RISCV::ADDI || OffsetTailOpc == RISCV::ADDIW) {
219 MachineOperand &AddiImmOp = OffsetTail.
getOperand(2);
223 int64_t OffLo = AddiImmOp.
getImm();
226 if (AddiReg == RISCV::X0) {
228 if (!foldOffset(
Hi,
Lo, TailAdd, OffLo))
234 MachineInstr &OffsetLui = *
MRI->getVRegDef(AddiReg);
235 MachineOperand &LuiImmOp = OffsetLui.
getOperand(1);
236 if (OffsetLui.
getOpcode() != RISCV::LUI ||
243 if (!ST->
is64Bit() || OffsetTailOpc == RISCV::ADDIW)
249 <<
" " << OffsetLui);
255 }
else if (OffsetTailOpc == RISCV::LUI) {
279bool RISCVMergeBaseOffsetOpt::foldShiftedOffset(MachineInstr &
Hi,
281 MachineInstr &TailShXAdd,
284 TailShXAdd.
getOpcode() == RISCV::SH2ADD ||
285 TailShXAdd.
getOpcode() == RISCV::SH3ADD) &&
286 "Expected SHXADD instruction!");
298 MachineInstr &OffsetTail = *
MRI->getVRegDef(Rs1);
299 if (OffsetTail.
getOpcode() != RISCV::ADDI)
312 case RISCV::SH1ADD: ShAmt = 1;
break;
313 case RISCV::SH2ADD: ShAmt = 2;
break;
314 case RISCV::SH3ADD: ShAmt = 3;
break;
326bool RISCVMergeBaseOffsetOpt::detectAndFoldOffset(MachineInstr &
Hi,
333 if (!
MRI->hasOneUse(DestReg))
337 MachineInstr &
Tail = *
MRI->use_instr_begin(DestReg);
338 switch (
Tail.getOpcode()) {
340 LLVM_DEBUG(
dbgs() <<
"Don't know how to get offset from this instr:"
344 case RISCV::QC_E_ADDI:
345 case RISCV::QC_E_ADDAI: {
348 if (
Tail.getOpcode() == RISCV::ADDI) {
351 if (
MRI->hasOneUse(TailDestReg)) {
352 MachineInstr &TailTail = *
MRI->use_instr_begin(TailDestReg);
353 if (TailTail.
getOpcode() == RISCV::ADDI) {
358 Tail.eraseFromParent();
376 return foldLargeOffset(
Hi,
Lo,
Tail, DestReg);
383 return foldShiftedOffset(
Hi,
Lo,
Tail, DestReg);
389bool RISCVMergeBaseOffsetOpt::foldIntoMemoryOps(MachineInstr &
Hi,
408 std::optional<int64_t> CommonOffset;
409 DenseMap<const MachineInstr *, SmallVector<unsigned>>
410 InlineAsmMemoryOpIndexesMap;
411 for (
const MachineInstr &
UseMI :
MRI->use_instructions(DestReg)) {
412 switch (
UseMI.getOpcode()) {
439 if (
UseMI.getOperand(1).isFI())
445 "Expected base address use");
448 if (CommonOffset &&
Offset != CommonOffset)
453 case RISCV::INLINEASM:
454 case RISCV::INLINEASM_BR: {
455 SmallVector<unsigned> InlineAsmMemoryOpIndexes;
459 const MachineOperand &FlagsMO =
UseMI.getOperand(
I);
461 if (!FlagsMO.
isImm())
471 for (
unsigned J = 0; J <
NumOps; ++J) {
472 const MachineOperand &MO =
UseMI.getOperand(
I + 1 + J);
481 if (
Flags.getMemoryConstraintID() == InlineAsm::ConstraintCode::A)
484 const MachineOperand &AddrMO =
UseMI.getOperand(
I + 1);
488 const MachineOperand &OffsetMO =
UseMI.getOperand(
I + 2);
489 if (!OffsetMO.
isImm())
494 if (CommonOffset &&
Offset != CommonOffset)
499 InlineAsmMemoryOpIndexesMap.
insert(
500 std::make_pair(&
UseMI, InlineAsmMemoryOpIndexes));
510 int64_t NewOffset =
Hi.getOperand(1).getOffset() + *CommonOffset;
518 Hi.getOperand(1).setOffset(NewOffset);
519 MachineOperand &ImmOp =
520 Hi.getOpcode() == RISCV::QC_E_LI ?
Lo.getOperand(1) :
Lo.getOperand(2);
521 auto HiOpc =
Hi.getOpcode();
523 if (HiOpc == RISCV::PseudoMovAddr) {
525 Hi.setDesc(
TII->get(RISCV::LUI));
529 if (HiOpc != RISCV::AUIPC)
533 for (MachineInstr &
UseMI :
535 if (
UseMI.getOpcode() == RISCV::INLINEASM ||
536 UseMI.getOpcode() == RISCV::INLINEASM_BR) {
537 auto &InlineAsmMemoryOpIndexes = InlineAsmMemoryOpIndexesMap[&
UseMI];
538 for (
unsigned I : InlineAsmMemoryOpIndexes) {
539 MachineOperand &MO =
UseMI.getOperand(
I + 1);
559 if (
Hi.getOpcode() == RISCV::QC_E_LI) {
560 UseMI.getOperand(2).ChangeToImmediate(0);
562 UseMI.removeOperand(2);
563 UseMI.addOperand(ImmOp);
573 MRI->replaceRegWith(
Lo.getOperand(0).getReg(),
Hi.getOperand(0).getReg());
574 Lo.eraseFromParent();
578bool RISCVMergeBaseOffsetOpt::runOnMachineFunction(MachineFunction &Fn) {
584 bool MadeChange =
false;
586 for (MachineBasicBlock &
MBB : Fn) {
588 for (MachineInstr &
Hi :
MBB) {
589 MachineInstr *
Lo =
nullptr;
590 if (!detectFoldable(
Hi,
Lo))
592 MadeChange |= detectAndFoldOffset(
Hi, *
Lo);
593 MadeChange |= foldIntoMemoryOps(
Hi, *
Lo);
602 return new RISCVMergeBaseOffsetOpt();
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const HexagonInstrInfo * TII
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
#define RISCV_MERGE_BASE_OFFSET_NAME
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
LLVM_ABI TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
FunctionPass class - This class is used to implement most global optimizations.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Type * getValueType() const
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Register getReg(unsigned Idx) const
Get the register for the operand index.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
LLVM_ABI void ChangeToMCSymbol(MCSymbol *Sym, unsigned TargetFlags=0)
ChangeToMCSymbol - Replace this operand with a new MC symbol operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
LLVM_ABI void ChangeToGA(const GlobalValue *GV, int64_t Offset, unsigned TargetFlags=0)
ChangeToGA - Replace this operand with a new global address operand.
LLVM_ABI void ChangeToBA(const BlockAddress *BA, int64_t Offset, unsigned TargetFlags=0)
ChangeToBA - Replace this operand with a new block address operand.
const BlockAddress * getBlockAddress() const
void setOffset(int64_t Offset)
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
bool isBlockAddress() const
isBlockAddress - Tests if this is a MO_BlockAddress operand.
Register getReg() const
getReg - Returns the register number.
MCSymbol * getMCSymbol() const
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
@ MO_GlobalAddress
Address of a global value.
@ MO_BlockAddress
Address of a basic block.
int64_t getOffset() const
Return the offset from the symbol in this operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const RISCVInstrInfo * getInstrInfo() const override
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ Tail
Attemps to make calls as fast as possible while guaranteeing that tail call optimization can always b...
This is an optimization pass for GlobalISel generic memory operations.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
FunctionPass * createRISCVMergeBaseOffsetOptPass()
Returns an instance of the Merge Base Offset Optimization pass.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.