30#define DEBUG_TYPE "riscv-merge-base-offset"
31#define RISCV_MERGE_BASE_OFFSET_NAME "RISC-V Merge Base Offset"
56char RISCVMergeBaseOffsetLegacy::ID = 0;
80 auto HiOpc =
Hi.getOpcode();
81 if (HiOpc != RISCV::LUI && HiOpc != RISCV::AUIPC &&
82 HiOpc != RISCV::PseudoMovAddr && HiOpc != RISCV::QC_E_LI)
93 HiOp1.getOffset() != 0)
96 if (HiOpc == RISCV::PseudoMovAddr || HiOpc == RISCV::QC_E_LI) {
101 Register HiDestReg =
Hi.getOperand(0).getReg();
102 if (!
MRI->hasOneUse(HiDestReg))
105 Lo = &*
MRI->use_instr_begin(HiDestReg);
106 if (
Lo->getOpcode() != RISCV::ADDI)
110 if (HiOpc != RISCV::QC_E_LI) {
112 if (HiOpc == RISCV::LUI || HiOpc == RISCV::PseudoMovAddr) {
118 assert(HiOpc == RISCV::AUIPC);
126 LLVM_DEBUG(dbgs() <<
" Found lowered global address: "
127 << *HiOp1.getGlobal() <<
"\n");
128 }
else if (
HiOp1.isBlockAddress()) {
129 LLVM_DEBUG(dbgs() <<
" Found lowered basic address: "
130 << *HiOp1.getBlockAddress() <<
"\n");
132 LLVM_DEBUG(dbgs() <<
" Found lowered constant pool: " << HiOp1.getIndex()
149 auto HiOpc =
Hi.getOpcode();
150 if (HiOpc == RISCV::AUIPC &&
Hi.getOperand(1).isGlobal()) {
153 if (!Ty->isSized() ||
Offset < 0 ||
172 if (
Hi.getOpcode() != RISCV::AUIPC &&
Hi.getOpcode() != RISCV::QC_E_LI)
175 Register LoOp0Reg =
Lo.getOperand(0).getReg();
176 Register TailOp0Reg = Tail.getOperand(0).getReg();
177 MRI->constrainRegClass(LoOp0Reg,
MRI->getRegClass(TailOp0Reg));
178 MRI->replaceRegWith(TailOp0Reg, LoOp0Reg);
179 Tail.eraseFromParent();
181 <<
" " <<
Hi <<
" " <<
Lo;);
209 assert((TailAdd.
getOpcode() == RISCV::ADD) &&
"Expected ADD instruction!");
215 if (!
Reg.isVirtual() || !
MRI->hasOneNonDBGUse(
Reg))
219 auto OffsetTailOpc = OffsetTail.
getOpcode();
220 if (OffsetTailOpc == RISCV::ADDI || OffsetTailOpc == RISCV::ADDIW) {
230 int64_t OffLo = AddiImmOp.
getImm();
233 if (AddiReg == RISCV::X0) {
243 if (OffsetLui.
getOpcode() != RISCV::LUI ||
250 if (!ST->is64Bit() || OffsetTailOpc == RISCV::ADDIW)
256 <<
" " << OffsetLui);
262 }
else if (OffsetTailOpc == RISCV::LUI) {
290 TailShXAdd.
getOpcode() == RISCV::SH2ADD ||
291 TailShXAdd.
getOpcode() == RISCV::SH3ADD) &&
292 "Expected SHXADD instruction!");
305 if (OffsetTail.
getOpcode() != RISCV::ADDI)
318 case RISCV::SH1ADD: ShAmt = 1;
break;
319 case RISCV::SH2ADD: ShAmt = 2;
break;
320 case RISCV::SH3ADD: ShAmt = 3;
break;
340 if (!
MRI->hasOneNonDBGUse(DestReg))
345 switch (Tail.getOpcode()) {
347 LLVM_DEBUG(
dbgs() <<
"Don't know how to get offset from this instr: "
351 case RISCV::QC_E_ADDI:
352 case RISCV::QC_E_ADDAI: {
354 int64_t
Offset = Tail.getOperand(2).getImm();
355 if (Tail.getOpcode() == RISCV::ADDI) {
357 Register TailDestReg = Tail.getOperand(0).getReg();
358 if (
MRI->hasOneNonDBGUse(TailDestReg)) {
360 if (TailTail.
getOpcode() == RISCV::ADDI) {
365 Tail.eraseFromParent();
439 std::optional<int64_t> CommonOffset;
441 InlineAsmMemoryOpIndexesMap;
443 switch (
UseMI.getOpcode()) {
470 if (
UseMI.getOperand(1).isFI())
473 if (DestReg ==
UseMI.getOperand(0).getReg())
476 "Expected base address use");
479 if (CommonOffset &&
Offset != CommonOffset)
484 case RISCV::PseudoCCLD:
485 case RISCV::PseudoCCLW:
486 case RISCV::PseudoCCLWU:
487 case RISCV::PseudoCCLH:
488 case RISCV::PseudoCCLHU:
489 case RISCV::PseudoCCLB:
490 case RISCV::PseudoCCLBU: {
493 if (
UseMI.getOperand(2).isFI())
497 if (DestReg ==
UseMI.getOperand(1).getReg() ||
498 DestReg ==
UseMI.getOperand(5).getReg())
500 if (
UseMI.getOperand(6).isReg() &&
501 DestReg ==
UseMI.getOperand(6).getReg())
504 "Expected base address use");
507 if (CommonOffset &&
Offset != CommonOffset)
512 case RISCV::INLINEASM:
513 case RISCV::INLINEASM_BR: {
520 if (!FlagsMO.
isImm())
524 NumOps = Flags.getNumOperandRegisters();
527 if (
NumOps != 2 || !Flags.isMemKind()) {
530 for (
unsigned J = 0; J <
NumOps; ++J) {
548 if (!OffsetMO.
isImm())
553 if (CommonOffset &&
Offset != CommonOffset)
558 InlineAsmMemoryOpIndexesMap.
insert(
559 std::make_pair(&
UseMI, InlineAsmMemoryOpIndexes));
572 int64_t NewOffset =
Hi.getOperand(1).getOffset() + *CommonOffset;
582 MRI->markUsesInDebugValueAsUndef(DestReg);
584 Hi.getOperand(1).setOffset(NewOffset);
586 Hi.getOpcode() == RISCV::QC_E_LI ?
Lo.getOperand(1) :
Lo.getOperand(2);
587 auto HiOpc =
Hi.getOpcode();
589 if (HiOpc == RISCV::PseudoMovAddr) {
590 auto *
TII = ST->getInstrInfo();
591 Hi.setDesc(
TII->get(RISCV::LUI));
595 if (HiOpc != RISCV::AUIPC)
601 if (
UseMI.getOpcode() == RISCV::INLINEASM ||
602 UseMI.getOpcode() == RISCV::INLINEASM_BR) {
603 auto &InlineAsmMemoryOpIndexes = InlineAsmMemoryOpIndexesMap[&
UseMI];
604 for (
unsigned I : InlineAsmMemoryOpIndexes) {
610 switch (
UseMI.getOpcode()) {
611 case RISCV::INLINEASM:
612 case RISCV::INLINEASM_BR:
614 case RISCV::PseudoCCLD:
615 case RISCV::PseudoCCLW:
616 case RISCV::PseudoCCLWU:
617 case RISCV::PseudoCCLH:
618 case RISCV::PseudoCCLHU:
619 case RISCV::PseudoCCLB:
620 case RISCV::PseudoCCLBU:
653 if (
Hi.getOpcode() == RISCV::QC_E_LI) {
666 MRI->replaceRegWith(
Lo.getOperand(0).getReg(),
Hi.getOperand(0).getReg());
667 Lo.eraseFromParent();
678 if (!ST->hasVendorXqcisls() || ST->is64Bit())
681 if (
Hi.getOpcode() != RISCV::QC_E_LI)
685 if (!BaseReg.isVirtual() || !
MRI->hasOneUse(BaseReg))
703 case RISCV::QC_SHLADD:
721 if (!ScaledReg.
isVirtual() || !
MRI->hasOneUse(ScaledReg))
730 NewOpc = RISCV::QC_LRB;
733 NewOpc = RISCV::QC_LRBU;
736 NewOpc = RISCV::QC_LRH;
739 NewOpc = RISCV::QC_LRHU;
742 NewOpc = RISCV::QC_LRW;
745 NewOpc = RISCV::QC_SRB;
748 NewOpc = RISCV::QC_SRH;
751 NewOpc = RISCV::QC_SRW;
767 Hi.getOperand(1).setOffset(NewOffset);
770 auto *
TII = ST->getInstrInfo();
774 MRI->constrainRegClass(IndexReg, &RISCV::GPRNoX0RegClass);
791 bool MadeChange =
false;
797 if (!detectFoldable(
Hi,
Lo,
MRI))
810 return new RISCVMergeBaseOffsetLegacy();
813bool 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.