24#define DEBUG_TYPE "riscv-merge-base-offset"
25#define RISCV_MERGE_BASE_OFFSET_NAME "RISC-V Merge Base Offset"
64char RISCVMergeBaseOffsetOpt::ID = 0;
86 if (
Hi.getOpcode() != RISCV::LUI &&
Hi.getOpcode() != RISCV::AUIPC &&
87 Hi.getOpcode() != RISCV::PseudoMovAddr)
91 unsigned ExpectedFlags =
93 if (HiOp1.getTargetFlags() != ExpectedFlags)
96 if (!(HiOp1.isGlobal() || HiOp1.isCPI() || HiOp1.isBlockAddress()) ||
97 HiOp1.getOffset() != 0)
100 if (
Hi.getOpcode() == RISCV::PseudoMovAddr) {
105 Register HiDestReg = Hi.getOperand(0).getReg();
106 if (!MRI->hasOneUse(HiDestReg))
109 Lo = &*MRI->use_instr_begin(HiDestReg);
110 if (Lo->getOpcode() != RISCV::ADDI)
115 if (
Hi.getOpcode() == RISCV::LUI ||
Hi.getOpcode() == RISCV::PseudoMovAddr) {
116 if (LoOp2.getTargetFlags() != RISCVII::MO_LO ||
117 !(LoOp2.isGlobal() || LoOp2.isCPI() || LoOp2.isBlockAddress()) ||
118 LoOp2.getOffset() != 0)
121 assert(Hi.getOpcode() == RISCV::AUIPC);
122 if (LoOp2.getTargetFlags() != RISCVII::MO_PCREL_LO ||
123 LoOp2.getType() != MachineOperand::MO_MCSymbol)
127 if (HiOp1.isGlobal()) {
128 LLVM_DEBUG(dbgs() <<
" Found lowered global address: "
129 << *HiOp1.getGlobal() <<
"\n");
130 }
else if (HiOp1.isBlockAddress()) {
131 LLVM_DEBUG(dbgs() <<
" Found lowered basic address: "
132 << *HiOp1.getBlockAddress() <<
"\n");
133 }
else if (HiOp1.isCPI()) {
134 LLVM_DEBUG(dbgs() <<
" Found lowered constant pool: " << HiOp1.getIndex()
151 if (
Hi.getOpcode() == RISCV::AUIPC &&
Hi.getOperand(1).isGlobal()) {
152 const GlobalValue *GV =
Hi.getOperand(1).getGlobal();
161 if (
Hi.getOpcode() != RISCV::AUIPC)
164 MRI->constrainRegClass(
Lo.getOperand(0).getReg(),
165 MRI->getRegClass(
Tail.getOperand(0).getReg()));
166 MRI->replaceRegWith(
Tail.getOperand(0).getReg(),
Lo.getOperand(0).getReg());
167 Tail.eraseFromParent();
169 <<
" " <<
Hi <<
" " <<
Lo;);
193bool RISCVMergeBaseOffsetOpt::foldLargeOffset(MachineInstr &
Hi,
195 MachineInstr &TailAdd,
197 assert((TailAdd.
getOpcode() == RISCV::ADD) &&
"Expected ADD instruction!");
206 MachineInstr &OffsetTail = *
MRI->getVRegDef(
Reg);
207 if (OffsetTail.
getOpcode() == RISCV::ADDI ||
208 OffsetTail.
getOpcode() == RISCV::ADDIW) {
211 MachineOperand &AddiImmOp = OffsetTail.
getOperand(2);
215 int64_t OffLo = AddiImmOp.
getImm();
218 if (AddiReg == RISCV::X0) {
220 if (!foldOffset(
Hi,
Lo, TailAdd, OffLo))
226 MachineInstr &OffsetLui = *
MRI->getVRegDef(AddiReg);
227 MachineOperand &LuiImmOp = OffsetLui.
getOperand(1);
228 if (OffsetLui.
getOpcode() != RISCV::LUI ||
241 <<
" " << OffsetLui);
247 }
else if (OffsetTail.
getOpcode() == RISCV::LUI) {
271bool RISCVMergeBaseOffsetOpt::foldShiftedOffset(MachineInstr &
Hi,
273 MachineInstr &TailShXAdd,
276 TailShXAdd.
getOpcode() == RISCV::SH2ADD ||
277 TailShXAdd.
getOpcode() == RISCV::SH3ADD) &&
278 "Expected SHXADD instruction!");
290 MachineInstr &OffsetTail = *
MRI->getVRegDef(Rs1);
291 if (OffsetTail.
getOpcode() != RISCV::ADDI)
304 case RISCV::SH1ADD: ShAmt = 1;
break;
305 case RISCV::SH2ADD: ShAmt = 2;
break;
306 case RISCV::SH3ADD: ShAmt = 3;
break;
318bool RISCVMergeBaseOffsetOpt::detectAndFoldOffset(MachineInstr &
Hi,
325 if (!
MRI->hasOneUse(DestReg))
329 MachineInstr &
Tail = *
MRI->use_instr_begin(DestReg);
330 switch (
Tail.getOpcode()) {
332 LLVM_DEBUG(
dbgs() <<
"Don't know how to get offset from this instr:"
341 if (
MRI->hasOneUse(TailDestReg)) {
342 MachineInstr &TailTail = *
MRI->use_instr_begin(TailDestReg);
343 if (TailTail.
getOpcode() == RISCV::ADDI) {
348 Tail.eraseFromParent();
365 return foldLargeOffset(
Hi,
Lo,
Tail, DestReg);
372 return foldShiftedOffset(
Hi,
Lo,
Tail, DestReg);
378bool RISCVMergeBaseOffsetOpt::foldIntoMemoryOps(MachineInstr &
Hi,
394 std::optional<int64_t> CommonOffset;
395 DenseMap<const MachineInstr *, SmallVector<unsigned>>
396 InlineAsmMemoryOpIndexesMap;
397 for (
const MachineInstr &
UseMI :
MRI->use_instructions(DestReg)) {
398 switch (
UseMI.getOpcode()) {
425 if (
UseMI.getOperand(1).isFI())
431 "Expected base address use");
434 if (CommonOffset &&
Offset != CommonOffset)
439 case RISCV::INLINEASM:
440 case RISCV::INLINEASM_BR: {
441 SmallVector<unsigned> InlineAsmMemoryOpIndexes;
445 const MachineOperand &FlagsMO =
UseMI.getOperand(
I);
447 if (!FlagsMO.
isImm())
457 for (
unsigned J = 0; J <
NumOps; ++J) {
458 const MachineOperand &MO =
UseMI.getOperand(
I + 1 + J);
467 if (
Flags.getMemoryConstraintID() == InlineAsm::ConstraintCode::A)
470 const MachineOperand &AddrMO =
UseMI.getOperand(
I + 1);
474 const MachineOperand &OffsetMO =
UseMI.getOperand(
I + 2);
475 if (!OffsetMO.
isImm())
480 if (CommonOffset &&
Offset != CommonOffset)
485 InlineAsmMemoryOpIndexesMap.
insert(
486 std::make_pair(&
UseMI, InlineAsmMemoryOpIndexes));
496 int64_t NewOffset =
Hi.getOperand(1).getOffset() + *CommonOffset;
504 Hi.getOperand(1).setOffset(NewOffset);
505 MachineOperand &ImmOp =
Lo.getOperand(2);
507 if (
Hi.getOpcode() == RISCV::PseudoMovAddr) {
509 Hi.setDesc(
TII->get(RISCV::LUI));
513 if (
Hi.getOpcode() != RISCV::AUIPC)
517 for (MachineInstr &
UseMI :
519 if (
UseMI.getOpcode() == RISCV::INLINEASM ||
520 UseMI.getOpcode() == RISCV::INLINEASM_BR) {
521 auto &InlineAsmMemoryOpIndexes = InlineAsmMemoryOpIndexesMap[&
UseMI];
522 for (
unsigned I : InlineAsmMemoryOpIndexes) {
523 MachineOperand &MO =
UseMI.getOperand(
I + 1);
543 UseMI.removeOperand(2);
544 UseMI.addOperand(ImmOp);
553 MRI->replaceRegWith(
Lo.getOperand(0).getReg(),
Hi.getOperand(0).getReg());
554 Lo.eraseFromParent();
558bool RISCVMergeBaseOffsetOpt::runOnMachineFunction(MachineFunction &Fn) {
564 bool MadeChange =
false;
566 for (MachineBasicBlock &
MBB : Fn) {
568 for (MachineInstr &
Hi :
MBB) {
569 MachineInstr *
Lo =
nullptr;
570 if (!detectFoldable(
Hi,
Lo))
572 MadeChange |= detectAndFoldOffset(
Hi, *
Lo);
573 MadeChange |= foldIntoMemoryOps(
Hi, *
Lo);
582 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:
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.
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
MachineOperandType getType() const
getType - Returns the MachineOperandType for this 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.