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);
227 int64_t OffLo = AddiImmOp.
getImm();
230 if (AddiReg == RISCV::X0) {
232 if (!foldOffset(
Hi,
Lo, TailAdd, OffLo))
238 MachineInstr &OffsetLui = *MRI->
getVRegDef(AddiReg);
239 MachineOperand &LuiImmOp = OffsetLui.
getOperand(1);
240 if (OffsetLui.
getOpcode() != RISCV::LUI ||
247 if (!ST->
is64Bit() || OffsetTailOpc == RISCV::ADDIW)
253 <<
" " << OffsetLui);
259 }
else if (OffsetTailOpc == RISCV::LUI) {
283bool RISCVMergeBaseOffsetOpt::foldShiftedOffset(MachineInstr &
Hi,
285 MachineInstr &TailShXAdd,
288 TailShXAdd.
getOpcode() == RISCV::SH2ADD ||
289 TailShXAdd.
getOpcode() == RISCV::SH3ADD) &&
290 "Expected SHXADD instruction!");
302 MachineInstr &OffsetTail = *MRI->
getVRegDef(Rs1);
303 if (OffsetTail.
getOpcode() != RISCV::ADDI)
316 case RISCV::SH1ADD: ShAmt = 1;
break;
317 case RISCV::SH2ADD: ShAmt = 2;
break;
318 case RISCV::SH3ADD: ShAmt = 3;
break;
330bool RISCVMergeBaseOffsetOpt::detectAndFoldOffset(MachineInstr &
Hi,
342 switch (
Tail.getOpcode()) {
344 LLVM_DEBUG(
dbgs() <<
"Don't know how to get offset from this instr: "
348 case RISCV::QC_E_ADDI:
349 case RISCV::QC_E_ADDAI: {
352 if (
Tail.getOpcode() == RISCV::ADDI) {
357 if (TailTail.
getOpcode() == RISCV::ADDI) {
362 Tail.eraseFromParent();
380 return foldLargeOffset(
Hi,
Lo, Tail, DestReg);
387 return foldShiftedOffset(
Hi,
Lo, Tail, DestReg);
416bool RISCVMergeBaseOffsetOpt::foldIntoMemoryOps(MachineInstr &
Hi,
435 std::optional<int64_t> CommonOffset;
436 DenseMap<const MachineInstr *, SmallVector<unsigned>>
437 InlineAsmMemoryOpIndexesMap;
439 switch (
UseMI.getOpcode()) {
466 if (
UseMI.getOperand(1).isFI())
472 "Expected base address use");
475 if (CommonOffset &&
Offset != CommonOffset)
480 case RISCV::PseudoCCLD:
481 case RISCV::PseudoCCLW:
482 case RISCV::PseudoCCLWU:
483 case RISCV::PseudoCCLH:
484 case RISCV::PseudoCCLHU:
485 case RISCV::PseudoCCLB:
486 case RISCV::PseudoCCLBU: {
489 if (
UseMI.getOperand(2).isFI())
496 if (
UseMI.getOperand(6).isReg() &&
500 "Expected base address use");
503 if (CommonOffset &&
Offset != CommonOffset)
508 case RISCV::INLINEASM:
509 case RISCV::INLINEASM_BR: {
510 SmallVector<unsigned> InlineAsmMemoryOpIndexes;
514 const MachineOperand &FlagsMO =
UseMI.getOperand(
I);
516 if (!FlagsMO.
isImm())
526 for (
unsigned J = 0; J <
NumOps; ++J) {
527 const MachineOperand &MO =
UseMI.getOperand(
I + 1 + J);
536 if (
Flags.getMemoryConstraintID() == InlineAsm::ConstraintCode::A)
539 const MachineOperand &AddrMO =
UseMI.getOperand(
I + 1);
543 const MachineOperand &OffsetMO =
UseMI.getOperand(
I + 2);
544 if (!OffsetMO.
isImm())
549 if (CommonOffset &&
Offset != CommonOffset)
554 InlineAsmMemoryOpIndexesMap.
insert(
555 std::make_pair(&
UseMI, InlineAsmMemoryOpIndexes));
568 int64_t NewOffset =
Hi.getOperand(1).getOffset() + *CommonOffset;
580 Hi.getOperand(1).setOffset(NewOffset);
581 MachineOperand &ImmOp =
582 Hi.getOpcode() == RISCV::QC_E_LI ?
Lo.getOperand(1) :
Lo.getOperand(2);
583 auto HiOpc =
Hi.getOpcode();
585 if (HiOpc == RISCV::PseudoMovAddr) {
587 Hi.setDesc(
TII->get(RISCV::LUI));
591 if (HiOpc != RISCV::AUIPC)
595 for (MachineInstr &
UseMI :
597 if (
UseMI.getOpcode() == RISCV::INLINEASM ||
598 UseMI.getOpcode() == RISCV::INLINEASM_BR) {
599 auto &InlineAsmMemoryOpIndexes = InlineAsmMemoryOpIndexesMap[&
UseMI];
600 for (
unsigned I : InlineAsmMemoryOpIndexes) {
601 MachineOperand &MO =
UseMI.getOperand(
I + 1);
606 switch (
UseMI.getOpcode()) {
607 case RISCV::INLINEASM:
608 case RISCV::INLINEASM_BR:
610 case RISCV::PseudoCCLD:
611 case RISCV::PseudoCCLW:
612 case RISCV::PseudoCCLWU:
613 case RISCV::PseudoCCLH:
614 case RISCV::PseudoCCLHU:
615 case RISCV::PseudoCCLB:
616 case RISCV::PseudoCCLBU:
648 MachineOperand &MO =
UseMI.getOperand(ImmIdx);
649 if (
Hi.getOpcode() == RISCV::QC_E_LI) {
663 Lo.eraseFromParent();
671bool RISCVMergeBaseOffsetOpt::foldShxaddIntoScaledMemory(MachineInstr &
Hi,
673 if (!ST->hasVendorXqcisls() || ST->
is64Bit())
676 if (
Hi.getOpcode() != RISCV::QC_E_LI)
698 case RISCV::QC_SHLADD:
725 NewOpc = RISCV::QC_LRB;
728 NewOpc = RISCV::QC_LRBU;
731 NewOpc = RISCV::QC_LRH;
734 NewOpc = RISCV::QC_LRHU;
737 NewOpc = RISCV::QC_LRW;
740 NewOpc = RISCV::QC_SRB;
743 NewOpc = RISCV::QC_SRH;
746 NewOpc = RISCV::QC_SRW;
762 Hi.getOperand(1).setOffset(NewOffset);
783bool RISCVMergeBaseOffsetOpt::runOnMachineFunction(
MachineFunction &Fn) {
789 bool MadeChange =
false;
791 for (MachineBasicBlock &
MBB : Fn) {
793 for (MachineInstr &
Hi :
MBB) {
794 MachineInstr *
Lo =
nullptr;
795 if (!detectFoldable(
Hi,
Lo))
797 MadeChange |= detectAndFoldOffset(
Hi, *
Lo);
798 MadeChange |= foldIntoMemoryOps(
Hi, *
Lo);
799 MadeChange |= foldShxaddIntoScaledMemory(
Hi, *
Lo);
808 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,...
LLVM_ABI void markUsesInDebugValueAsUndef(Register Reg) const
markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the specified register as undefined wh...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI LLVM_READONLY 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_nodbg_iterator > use_nodbg_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)
Represent a constant reference to a string, i.e.
bool isSized() 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.
@ 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.