29#define DEBUG_TYPE "riscv-merge-base-offset"
30#define RISCV_MERGE_BASE_OFFSET_NAME "RISC-V Merge Base Offset"
55char RISCVMergeBaseOffsetLegacy::ID = 0;
79 auto HiOpc =
Hi.getOpcode();
80 if (HiOpc != RISCV::LUI && HiOpc != RISCV::AUIPC &&
81 HiOpc != RISCV::PseudoMovAddr && HiOpc != RISCV::QC_E_LI)
92 HiOp1.getOffset() != 0)
95 if (HiOpc == RISCV::PseudoMovAddr || HiOpc == RISCV::QC_E_LI) {
100 Register HiDestReg =
Hi.getOperand(0).getReg();
101 if (!
MRI->hasOneUse(HiDestReg))
104 Lo = &*
MRI->use_instr_begin(HiDestReg);
105 if (
Lo->getOpcode() != RISCV::ADDI)
109 if (HiOpc != RISCV::QC_E_LI) {
111 if (HiOpc == RISCV::LUI || HiOpc == RISCV::PseudoMovAddr) {
117 assert(HiOpc == RISCV::AUIPC);
125 LLVM_DEBUG(dbgs() <<
" Found lowered global address: "
126 << *HiOp1.getGlobal() <<
"\n");
127 }
else if (
HiOp1.isBlockAddress()) {
128 LLVM_DEBUG(dbgs() <<
" Found lowered basic address: "
129 << *HiOp1.getBlockAddress() <<
"\n");
131 LLVM_DEBUG(dbgs() <<
" Found lowered constant pool: " << HiOp1.getIndex()
148 auto HiOpc =
Hi.getOpcode();
149 if (HiOpc == RISCV::AUIPC &&
Hi.getOperand(1).isGlobal()) {
152 if (!Ty->isSized() ||
Offset < 0 ||
171 if (
Hi.getOpcode() != RISCV::AUIPC &&
Hi.getOpcode() != RISCV::QC_E_LI)
174 Register LoOp0Reg =
Lo.getOperand(0).getReg();
175 Register TailOp0Reg = Tail.getOperand(0).getReg();
176 MRI->constrainRegClass(LoOp0Reg,
MRI->getRegClass(TailOp0Reg));
177 MRI->replaceRegWith(TailOp0Reg, LoOp0Reg);
178 Tail.eraseFromParent();
180 <<
" " <<
Hi <<
" " <<
Lo;);
208 assert((TailAdd.
getOpcode() == RISCV::ADD) &&
"Expected ADD instruction!");
214 if (!
Reg.isVirtual() || !
MRI->hasOneNonDBGUse(
Reg))
218 auto OffsetTailOpc = OffsetTail.
getOpcode();
219 if (OffsetTailOpc == RISCV::ADDI || OffsetTailOpc == RISCV::ADDIW) {
229 int64_t OffLo = AddiImmOp.
getImm();
232 if (AddiReg == RISCV::X0) {
242 if (OffsetLui.
getOpcode() != RISCV::LUI ||
249 if (!ST->is64Bit() || OffsetTailOpc == RISCV::ADDIW)
255 <<
" " << OffsetLui);
261 }
else if (OffsetTailOpc == RISCV::LUI) {
289 TailShXAdd.
getOpcode() == RISCV::SH2ADD ||
290 TailShXAdd.
getOpcode() == RISCV::SH3ADD) &&
291 "Expected SHXADD instruction!");
304 if (OffsetTail.
getOpcode() != RISCV::ADDI)
317 case RISCV::SH1ADD: ShAmt = 1;
break;
318 case RISCV::SH2ADD: ShAmt = 2;
break;
319 case RISCV::SH3ADD: ShAmt = 3;
break;
339 if (!
MRI->hasOneNonDBGUse(DestReg))
344 switch (Tail.getOpcode()) {
346 LLVM_DEBUG(
dbgs() <<
"Don't know how to get offset from this instr: "
350 case RISCV::QC_E_ADDI:
351 case RISCV::QC_E_ADDAI: {
353 int64_t
Offset = Tail.getOperand(2).getImm();
354 if (Tail.getOpcode() == RISCV::ADDI) {
356 Register TailDestReg = Tail.getOperand(0).getReg();
357 if (
MRI->hasOneNonDBGUse(TailDestReg)) {
359 if (TailTail.
getOpcode() == RISCV::ADDI) {
364 Tail.eraseFromParent();
438 std::optional<int64_t> CommonOffset;
440 InlineAsmMemoryOpIndexesMap;
442 switch (
UseMI.getOpcode()) {
469 if (
UseMI.getOperand(1).isFI())
472 if (DestReg ==
UseMI.getOperand(0).getReg())
475 "Expected base address use");
478 if (CommonOffset &&
Offset != CommonOffset)
483 case RISCV::PseudoCCLD:
484 case RISCV::PseudoCCLW:
485 case RISCV::PseudoCCLWU:
486 case RISCV::PseudoCCLH:
487 case RISCV::PseudoCCLHU:
488 case RISCV::PseudoCCLB:
489 case RISCV::PseudoCCLBU: {
492 if (
UseMI.getOperand(2).isFI())
496 if (DestReg ==
UseMI.getOperand(1).getReg() ||
497 DestReg ==
UseMI.getOperand(5).getReg())
499 if (
UseMI.getOperand(6).isReg() &&
500 DestReg ==
UseMI.getOperand(6).getReg())
503 "Expected base address use");
506 if (CommonOffset &&
Offset != CommonOffset)
511 case RISCV::INLINEASM:
512 case RISCV::INLINEASM_BR: {
519 if (!FlagsMO.
isImm())
523 NumOps = Flags.getNumOperandRegisters();
526 if (
NumOps != 2 || !Flags.isMemKind()) {
529 for (
unsigned J = 0; J <
NumOps; ++J) {
547 if (!OffsetMO.
isImm())
552 if (CommonOffset &&
Offset != CommonOffset)
557 InlineAsmMemoryOpIndexesMap.
insert(
558 std::make_pair(&
UseMI, InlineAsmMemoryOpIndexes));
571 int64_t NewOffset =
Hi.getOperand(1).getOffset() + *CommonOffset;
581 MRI->markUsesInDebugValueAsUndef(DestReg);
583 Hi.getOperand(1).setOffset(NewOffset);
585 Hi.getOpcode() == RISCV::QC_E_LI ?
Lo.getOperand(1) :
Lo.getOperand(2);
586 auto HiOpc =
Hi.getOpcode();
588 if (HiOpc == RISCV::PseudoMovAddr) {
589 auto *
TII = ST->getInstrInfo();
590 Hi.setDesc(
TII->get(RISCV::LUI));
594 if (HiOpc != RISCV::AUIPC)
600 if (
UseMI.getOpcode() == RISCV::INLINEASM ||
601 UseMI.getOpcode() == RISCV::INLINEASM_BR) {
602 auto &InlineAsmMemoryOpIndexes = InlineAsmMemoryOpIndexesMap[&
UseMI];
603 for (
unsigned I : InlineAsmMemoryOpIndexes) {
609 switch (
UseMI.getOpcode()) {
610 case RISCV::INLINEASM:
611 case RISCV::INLINEASM_BR:
613 case RISCV::PseudoCCLD:
614 case RISCV::PseudoCCLW:
615 case RISCV::PseudoCCLWU:
616 case RISCV::PseudoCCLH:
617 case RISCV::PseudoCCLHU:
618 case RISCV::PseudoCCLB:
619 case RISCV::PseudoCCLBU:
652 if (
Hi.getOpcode() == RISCV::QC_E_LI) {
665 MRI->replaceRegWith(
Lo.getOperand(0).getReg(),
Hi.getOperand(0).getReg());
666 Lo.eraseFromParent();
677 if (!ST->hasVendorXqcisls() || ST->is64Bit())
680 if (
Hi.getOpcode() != RISCV::QC_E_LI)
684 if (!BaseReg.isVirtual() || !
MRI->hasOneUse(BaseReg))
702 case RISCV::QC_SHLADD:
720 if (!ScaledReg.
isVirtual() || !
MRI->hasOneUse(ScaledReg))
729 NewOpc = RISCV::QC_LRB;
732 NewOpc = RISCV::QC_LRBU;
735 NewOpc = RISCV::QC_LRH;
738 NewOpc = RISCV::QC_LRHU;
741 NewOpc = RISCV::QC_LRW;
744 NewOpc = RISCV::QC_SRB;
747 NewOpc = RISCV::QC_SRH;
750 NewOpc = RISCV::QC_SRW;
766 Hi.getOperand(1).setOffset(NewOffset);
769 auto *
TII = ST->getInstrInfo();
773 MRI->constrainRegClass(IndexReg, &RISCV::GPRNoX0RegClass);
790 bool MadeChange =
false;
796 if (!detectFoldable(
Hi,
Lo,
MRI))
809 return new RISCVMergeBaseOffsetLegacy();
812bool RISCVMergeBaseOffsetLegacy::runOnMachineFunction(
MachineFunction &MF) {
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const HexagonInstrInfo * TII
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
MachineRegisterInfo & MRI
static void overwriteMachineOperandInPlace(MachineOperand &MO, const MachineOperand &ImmOp)
static bool detectAndFoldOffset(MachineInstr &Hi, MachineInstr &Lo, MachineRegisterInfo *MRI, const RISCVSubtarget *ST)
static bool foldShxaddIntoScaledMemory(MachineInstr &Hi, MachineInstr &Lo, MachineRegisterInfo *MRI, const RISCVSubtarget *ST)
static bool foldShiftedOffset(MachineInstr &Hi, MachineInstr &Lo, MachineInstr &TailShXAdd, Register GAReg, MachineRegisterInfo *MRI)
static bool foldIntoMemoryOps(MachineInstr &Hi, MachineInstr &Lo, MachineRegisterInfo *MRI, const RISCVSubtarget *ST)
const MachineOperand & HiOp1
#define RISCV_MERGE_BASE_OFFSET_NAME
static bool foldOffset(MachineInstr &Hi, MachineInstr &Lo, MachineInstr &Tail, int64_t Offset, MachineRegisterInfo *MRI)
static bool foldLargeOffset(MachineInstr &Hi, MachineInstr &Lo, MachineInstr &TailAdd, Register GAReg, MachineRegisterInfo *MRI, const RISCVSubtarget *ST)
static bool runMergeBaseOffset(MachineFunction &Fn)
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
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
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.
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
LLVM_ABI const MachineOperand & getDebugExpressionOp() const
Return the operand for the complex address expression referenced by this DBG_VALUE instruction.
LLVM_ABI const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
bool isDebugValue() const
unsigned getDebugOperandIndex(const MachineOperand *Op) const
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.
void setMetadata(const MDNode *MD)
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,...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
The instances of the Type class are immutable: once they are created, they are never changed.
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createRISCVMergeBaseOffsetLegacyPass()
Returns an instance of the Merge Base Offset Optimization pass.
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.