24#define DEBUG_TYPE "riscv-merge-base-offset"
25#define RISCV_MERGE_BASE_OFFSET_NAME "RISC-V Merge Base Offset"
65char RISCVMergeBaseOffsetOpt::ID = 0;
89 auto HiOpc =
Hi.getOpcode();
90 if (HiOpc != RISCV::LUI && HiOpc != RISCV::AUIPC &&
91 HiOpc != RISCV::PseudoMovAddr && HiOpc != RISCV::QC_E_LI)
105 if (HiOpc == RISCV::PseudoMovAddr || HiOpc == RISCV::QC_E_LI) {
110 Register HiDestReg =
Hi.getOperand(0).getReg();
111 if (!MRI->hasOneUse(HiDestReg))
114 Lo = &*MRI->use_instr_begin(HiDestReg);
115 if (
Lo->getOpcode() != RISCV::ADDI)
119 if (HiOpc != RISCV::QC_E_LI) {
121 if (HiOpc == RISCV::LUI || HiOpc == RISCV::PseudoMovAddr) {
127 assert(HiOpc == RISCV::AUIPC);
135 LLVM_DEBUG(dbgs() <<
" Found lowered global address: "
136 << *HiOp1.getGlobal() <<
"\n");
138 LLVM_DEBUG(dbgs() <<
" Found lowered basic address: "
139 << *HiOp1.getBlockAddress() <<
"\n");
141 LLVM_DEBUG(dbgs() <<
" Found lowered constant pool: " << HiOp1.getIndex()
158 auto HiOpc =
Hi.getOpcode();
159 if (HiOpc == RISCV::AUIPC &&
Hi.getOperand(1).isGlobal()) {
160 const GlobalValue *GV =
Hi.getOperand(1).getGlobal();
169 if (
Hi.getOpcode() != RISCV::AUIPC &&
Hi.getOpcode() != RISCV::QC_E_LI)
172 Register LoOp0Reg =
Lo.getOperand(0).getReg();
176 Tail.eraseFromParent();
178 <<
" " <<
Hi <<
" " <<
Lo;);
202bool RISCVMergeBaseOffsetOpt::foldLargeOffset(MachineInstr &
Hi,
204 MachineInstr &TailAdd,
206 assert((TailAdd.
getOpcode() == RISCV::ADD) &&
"Expected ADD instruction!");
216 auto OffsetTailOpc = OffsetTail.
getOpcode();
217 if (OffsetTailOpc == RISCV::ADDI || OffsetTailOpc == RISCV::ADDIW) {
220 MachineOperand &AddiImmOp = OffsetTail.
getOperand(2);
224 int64_t OffLo = AddiImmOp.
getImm();
227 if (AddiReg == RISCV::X0) {
229 if (!foldOffset(
Hi,
Lo, TailAdd, OffLo))
235 MachineInstr &OffsetLui = *MRI->
getVRegDef(AddiReg);
236 MachineOperand &LuiImmOp = OffsetLui.
getOperand(1);
237 if (OffsetLui.
getOpcode() != RISCV::LUI ||
244 if (!ST->
is64Bit() || OffsetTailOpc == RISCV::ADDIW)
250 <<
" " << OffsetLui);
256 }
else if (OffsetTailOpc == RISCV::LUI) {
280bool RISCVMergeBaseOffsetOpt::foldShiftedOffset(MachineInstr &
Hi,
282 MachineInstr &TailShXAdd,
285 TailShXAdd.
getOpcode() == RISCV::SH2ADD ||
286 TailShXAdd.
getOpcode() == RISCV::SH3ADD) &&
287 "Expected SHXADD instruction!");
299 MachineInstr &OffsetTail = *MRI->
getVRegDef(Rs1);
300 if (OffsetTail.
getOpcode() != RISCV::ADDI)
313 case RISCV::SH1ADD: ShAmt = 1;
break;
314 case RISCV::SH2ADD: ShAmt = 2;
break;
315 case RISCV::SH3ADD: ShAmt = 3;
break;
327bool RISCVMergeBaseOffsetOpt::detectAndFoldOffset(MachineInstr &
Hi,
339 switch (
Tail.getOpcode()) {
341 LLVM_DEBUG(
dbgs() <<
"Don't know how to get offset from this instr: "
345 case RISCV::QC_E_ADDI:
346 case RISCV::QC_E_ADDAI: {
349 if (
Tail.getOpcode() == RISCV::ADDI) {
354 if (TailTail.
getOpcode() == RISCV::ADDI) {
359 Tail.eraseFromParent();
377 return foldLargeOffset(
Hi,
Lo,
Tail, DestReg);
384 return foldShiftedOffset(
Hi,
Lo,
Tail, DestReg);
413bool RISCVMergeBaseOffsetOpt::foldIntoMemoryOps(MachineInstr &
Hi,
432 std::optional<int64_t> CommonOffset;
433 DenseMap<const MachineInstr *, SmallVector<unsigned>>
434 InlineAsmMemoryOpIndexesMap;
436 switch (
UseMI.getOpcode()) {
463 if (
UseMI.getOperand(1).isFI())
469 "Expected base address use");
472 if (CommonOffset &&
Offset != CommonOffset)
477 case RISCV::PseudoCCLD:
478 case RISCV::PseudoCCLW:
479 case RISCV::PseudoCCLWU:
480 case RISCV::PseudoCCLH:
481 case RISCV::PseudoCCLHU:
482 case RISCV::PseudoCCLB:
483 case RISCV::PseudoCCLBU: {
486 if (
UseMI.getOperand(2).isFI())
493 if (
UseMI.getOperand(6).isReg() &&
497 "Expected base address use");
500 if (CommonOffset &&
Offset != CommonOffset)
505 case RISCV::INLINEASM:
506 case RISCV::INLINEASM_BR: {
507 SmallVector<unsigned> InlineAsmMemoryOpIndexes;
511 const MachineOperand &FlagsMO =
UseMI.getOperand(
I);
513 if (!FlagsMO.
isImm())
523 for (
unsigned J = 0; J <
NumOps; ++J) {
524 const MachineOperand &MO =
UseMI.getOperand(
I + 1 + J);
533 if (
Flags.getMemoryConstraintID() == InlineAsm::ConstraintCode::A)
536 const MachineOperand &AddrMO =
UseMI.getOperand(
I + 1);
540 const MachineOperand &OffsetMO =
UseMI.getOperand(
I + 2);
541 if (!OffsetMO.
isImm())
546 if (CommonOffset &&
Offset != CommonOffset)
551 InlineAsmMemoryOpIndexesMap.
insert(
552 std::make_pair(&
UseMI, InlineAsmMemoryOpIndexes));
562 int64_t NewOffset =
Hi.getOperand(1).getOffset() + *CommonOffset;
570 Hi.getOperand(1).setOffset(NewOffset);
571 MachineOperand &ImmOp =
572 Hi.getOpcode() == RISCV::QC_E_LI ?
Lo.getOperand(1) :
Lo.getOperand(2);
573 auto HiOpc =
Hi.getOpcode();
575 if (HiOpc == RISCV::PseudoMovAddr) {
577 Hi.setDesc(
TII->get(RISCV::LUI));
581 if (HiOpc != RISCV::AUIPC)
585 for (MachineInstr &
UseMI :
587 if (
UseMI.getOpcode() == RISCV::INLINEASM ||
588 UseMI.getOpcode() == RISCV::INLINEASM_BR) {
589 auto &InlineAsmMemoryOpIndexes = InlineAsmMemoryOpIndexesMap[&
UseMI];
590 for (
unsigned I : InlineAsmMemoryOpIndexes) {
591 MachineOperand &MO =
UseMI.getOperand(
I + 1);
596 switch (
UseMI.getOpcode()) {
597 case RISCV::INLINEASM:
598 case RISCV::INLINEASM_BR:
600 case RISCV::PseudoCCLD:
601 case RISCV::PseudoCCLW:
602 case RISCV::PseudoCCLWU:
603 case RISCV::PseudoCCLH:
604 case RISCV::PseudoCCLHU:
605 case RISCV::PseudoCCLB:
606 case RISCV::PseudoCCLBU:
638 MachineOperand &MO =
UseMI.getOperand(ImmIdx);
639 if (
Hi.getOpcode() == RISCV::QC_E_LI) {
653 Lo.eraseFromParent();
661bool RISCVMergeBaseOffsetOpt::foldShxaddIntoScaledMemory(MachineInstr &
Hi,
663 if (!ST->hasVendorXqcisls() || ST->
is64Bit())
666 if (
Hi.getOpcode() != RISCV::QC_E_LI)
688 case RISCV::QC_SHLADD:
715 NewOpc = RISCV::QC_LRB;
718 NewOpc = RISCV::QC_LRBU;
721 NewOpc = RISCV::QC_LRH;
724 NewOpc = RISCV::QC_LRHU;
727 NewOpc = RISCV::QC_LRW;
730 NewOpc = RISCV::QC_SRB;
733 NewOpc = RISCV::QC_SRH;
736 NewOpc = RISCV::QC_SRW;
752 Hi.getOperand(1).setOffset(NewOffset);
773bool RISCVMergeBaseOffsetOpt::runOnMachineFunction(MachineFunction &Fn) {
779 bool MadeChange =
false;
781 for (MachineBasicBlock &
MBB : Fn) {
783 for (MachineInstr &
Hi :
MBB) {
784 MachineInstr *
Lo =
nullptr;
785 if (!detectFoldable(
Hi,
Lo))
787 MadeChange |= detectAndFoldOffset(
Hi, *
Lo);
788 MadeChange |= foldIntoMemoryOps(
Hi, *
Lo);
789 MadeChange |= foldShxaddIntoScaledMemory(
Hi, *
Lo);
798 return new RISCVMergeBaseOffsetOpt();
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)
static void overwriteMachineOperandInPlace(MachineOperand &MO, const MachineOperand &ImmOp)
#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.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & cloneMemRefs(const MachineInstr &OtherMI) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
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 ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
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.
LLVM_ABI void ChangeToCPI(unsigned Idx, int Offset, unsigned TargetFlags=0)
ChangeToCPI - Replace this operand with a new constant pool index operand.
MCSymbol * getMCSymbol() const
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ 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 TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
use_instr_iterator use_instr_begin(Register RegNo) const
bool hasOneUse(Register RegNo) const
hasOneUse - Return true if there is exactly one instruction using the specified register.
iterator_range< use_instr_iterator > use_instructions(Register Reg) const
LLVM_ABI const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
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...
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
constexpr RegState getKillRegState(bool B)
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.