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 auto HiOpc =
Hi.getOpcode();
87 if (HiOpc != RISCV::LUI && HiOpc != RISCV::AUIPC &&
88 HiOpc != RISCV::PseudoMovAddr)
92 unsigned ExpectedFlags =
101 if (HiOpc == RISCV::PseudoMovAddr) {
106 Register HiDestReg =
Hi.getOperand(0).getReg();
107 if (!
MRI->hasOneUse(HiDestReg))
110 Lo = &*
MRI->use_instr_begin(HiDestReg);
111 if (
Lo->getOpcode() != RISCV::ADDI)
116 if (HiOpc == RISCV::LUI || HiOpc == RISCV::PseudoMovAddr) {
122 assert(HiOpc == RISCV::AUIPC);
129 LLVM_DEBUG(dbgs() <<
" Found lowered global address: "
130 << *HiOp1.getGlobal() <<
"\n");
132 LLVM_DEBUG(dbgs() <<
" Found lowered basic address: "
133 << *HiOp1.getBlockAddress() <<
"\n");
135 LLVM_DEBUG(dbgs() <<
" Found lowered constant pool: " << HiOp1.getIndex()
152 auto HiOpc =
Hi.getOpcode();
153 if (HiOpc == RISCV::AUIPC &&
Hi.getOperand(1).isGlobal()) {
154 const GlobalValue *GV =
Hi.getOperand(1).getGlobal();
163 if (HiOpc != RISCV::AUIPC)
166 Register LoOp0Reg =
Lo.getOperand(0).getReg();
168 MRI->constrainRegClass(LoOp0Reg,
MRI->getRegClass(TailOp0Reg));
169 MRI->replaceRegWith(TailOp0Reg, LoOp0Reg);
170 Tail.eraseFromParent();
172 <<
" " <<
Hi <<
" " <<
Lo;);
196bool RISCVMergeBaseOffsetOpt::foldLargeOffset(MachineInstr &
Hi,
198 MachineInstr &TailAdd,
200 assert((TailAdd.
getOpcode() == RISCV::ADD) &&
"Expected ADD instruction!");
209 MachineInstr &OffsetTail = *
MRI->getVRegDef(
Reg);
210 auto OffsetTailOpc = OffsetTail.
getOpcode();
211 if (OffsetTailOpc == RISCV::ADDI || OffsetTailOpc == RISCV::ADDIW) {
214 MachineOperand &AddiImmOp = OffsetTail.
getOperand(2);
218 int64_t OffLo = AddiImmOp.
getImm();
221 if (AddiReg == RISCV::X0) {
223 if (!foldOffset(
Hi,
Lo, TailAdd, OffLo))
229 MachineInstr &OffsetLui = *
MRI->getVRegDef(AddiReg);
230 MachineOperand &LuiImmOp = OffsetLui.
getOperand(1);
231 if (OffsetLui.
getOpcode() != RISCV::LUI ||
238 if (!ST->
is64Bit() || OffsetTailOpc == RISCV::ADDIW)
244 <<
" " << OffsetLui);
250 }
else if (OffsetTailOpc == RISCV::LUI) {
274bool RISCVMergeBaseOffsetOpt::foldShiftedOffset(MachineInstr &
Hi,
276 MachineInstr &TailShXAdd,
279 TailShXAdd.
getOpcode() == RISCV::SH2ADD ||
280 TailShXAdd.
getOpcode() == RISCV::SH3ADD) &&
281 "Expected SHXADD instruction!");
293 MachineInstr &OffsetTail = *
MRI->getVRegDef(Rs1);
294 if (OffsetTail.
getOpcode() != RISCV::ADDI)
307 case RISCV::SH1ADD: ShAmt = 1;
break;
308 case RISCV::SH2ADD: ShAmt = 2;
break;
309 case RISCV::SH3ADD: ShAmt = 3;
break;
321bool RISCVMergeBaseOffsetOpt::detectAndFoldOffset(MachineInstr &
Hi,
328 if (!
MRI->hasOneUse(DestReg))
332 MachineInstr &
Tail = *
MRI->use_instr_begin(DestReg);
333 switch (
Tail.getOpcode()) {
335 LLVM_DEBUG(
dbgs() <<
"Don't know how to get offset from this instr:"
344 if (
MRI->hasOneUse(TailDestReg)) {
345 MachineInstr &TailTail = *
MRI->use_instr_begin(TailDestReg);
346 if (TailTail.
getOpcode() == RISCV::ADDI) {
351 Tail.eraseFromParent();
368 return foldLargeOffset(
Hi,
Lo,
Tail, DestReg);
375 return foldShiftedOffset(
Hi,
Lo,
Tail, DestReg);
381bool RISCVMergeBaseOffsetOpt::foldIntoMemoryOps(MachineInstr &
Hi,
397 std::optional<int64_t> CommonOffset;
398 DenseMap<const MachineInstr *, SmallVector<unsigned>>
399 InlineAsmMemoryOpIndexesMap;
400 for (
const MachineInstr &
UseMI :
MRI->use_instructions(DestReg)) {
401 switch (
UseMI.getOpcode()) {
428 if (
UseMI.getOperand(1).isFI())
434 "Expected base address use");
437 if (CommonOffset &&
Offset != CommonOffset)
442 case RISCV::INLINEASM:
443 case RISCV::INLINEASM_BR: {
444 SmallVector<unsigned> InlineAsmMemoryOpIndexes;
448 const MachineOperand &FlagsMO =
UseMI.getOperand(
I);
450 if (!FlagsMO.
isImm())
460 for (
unsigned J = 0; J <
NumOps; ++J) {
461 const MachineOperand &MO =
UseMI.getOperand(
I + 1 + J);
470 if (
Flags.getMemoryConstraintID() == InlineAsm::ConstraintCode::A)
473 const MachineOperand &AddrMO =
UseMI.getOperand(
I + 1);
477 const MachineOperand &OffsetMO =
UseMI.getOperand(
I + 2);
478 if (!OffsetMO.
isImm())
483 if (CommonOffset &&
Offset != CommonOffset)
488 InlineAsmMemoryOpIndexesMap.
insert(
489 std::make_pair(&
UseMI, InlineAsmMemoryOpIndexes));
499 int64_t NewOffset =
Hi.getOperand(1).getOffset() + *CommonOffset;
507 Hi.getOperand(1).setOffset(NewOffset);
508 MachineOperand &ImmOp =
Lo.getOperand(2);
509 auto HiOpc =
Hi.getOpcode();
511 if (HiOpc == RISCV::PseudoMovAddr) {
513 Hi.setDesc(
TII->get(RISCV::LUI));
517 if (HiOpc != RISCV::AUIPC)
521 for (MachineInstr &
UseMI :
523 if (
UseMI.getOpcode() == RISCV::INLINEASM ||
524 UseMI.getOpcode() == RISCV::INLINEASM_BR) {
525 auto &InlineAsmMemoryOpIndexes = InlineAsmMemoryOpIndexesMap[&
UseMI];
526 for (
unsigned I : InlineAsmMemoryOpIndexes) {
527 MachineOperand &MO =
UseMI.getOperand(
I + 1);
547 UseMI.removeOperand(2);
548 UseMI.addOperand(ImmOp);
557 MRI->replaceRegWith(
Lo.getOperand(0).getReg(),
Hi.getOperand(0).getReg());
558 Lo.eraseFromParent();
562bool RISCVMergeBaseOffsetOpt::runOnMachineFunction(MachineFunction &Fn) {
568 bool MadeChange =
false;
570 for (MachineBasicBlock &
MBB : Fn) {
572 for (MachineInstr &
Hi :
MBB) {
573 MachineInstr *
Lo =
nullptr;
574 if (!detectFoldable(
Hi,
Lo))
576 MadeChange |= detectAndFoldOffset(
Hi, *
Lo);
577 MadeChange |= foldIntoMemoryOps(
Hi, *
Lo);
586 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.