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 if (
Hi.getOpcode() != RISCV::LUI &&
Hi.getOpcode() != RISCV::AUIPC &&
87 Hi.getOpcode() != RISCV::PseudoMovAddr)
91 unsigned ExpectedFlags =
100 if (
Hi.getOpcode() == RISCV::PseudoMovAddr) {
105 Register HiDestReg =
Hi.getOperand(0).getReg();
106 if (!
MRI->hasOneUse(HiDestReg))
109 Lo = &*
MRI->use_instr_begin(HiDestReg);
110 if (
Lo->getOpcode() != RISCV::ADDI)
115 if (
Hi.getOpcode() == RISCV::LUI ||
Hi.getOpcode() == RISCV::PseudoMovAddr) {
121 assert(
Hi.getOpcode() == RISCV::AUIPC);
133 }
else if (HiOp1.
isCPI()) {
151 if (
Hi.getOpcode() == RISCV::AUIPC &&
Hi.getOperand(1).isGlobal()) {
161 if (
Hi.getOpcode() != RISCV::AUIPC)
164 MRI->constrainRegClass(
Lo.getOperand(0).getReg(),
165 MRI->getRegClass(
Tail.getOperand(0).getReg()));
166 MRI->replaceRegWith(
Tail.getOperand(0).getReg(),
Lo.getOperand(0).getReg());
167 Tail.eraseFromParent();
169 <<
" " <<
Hi <<
" " <<
Lo;);
197 assert((TailAdd.
getOpcode() == RISCV::ADD) &&
"Expected ADD instruction!");
203 if (!
Reg.isVirtual() || !
MRI->hasOneUse(Reg))
207 if (OffsetTail.
getOpcode() == RISCV::ADDI ||
208 OffsetTail.
getOpcode() == RISCV::ADDIW) {
215 int64_t OffLo = AddiImmOp.
getImm();
218 if (AddiReg == RISCV::X0) {
220 if (!foldOffset(
Hi,
Lo, TailAdd, OffLo))
228 if (OffsetLui.
getOpcode() != RISCV::LUI ||
232 int64_t
Offset = SignExtend64<32>(LuiImmOp.
getImm() << 12);
235 if (!
ST->is64Bit() || OffsetTail.
getOpcode() == RISCV::ADDIW)
241 <<
" " << OffsetLui);
247 }
else if (OffsetTail.
getOpcode() == RISCV::LUI) {
276 TailShXAdd.
getOpcode() == RISCV::SH2ADD ||
277 TailShXAdd.
getOpcode() == RISCV::SH3ADD) &&
278 "Expected SHXADD instruction!");
291 if (OffsetTail.
getOpcode() != RISCV::ADDI)
304 case RISCV::SH1ADD: ShAmt = 1;
break;
305 case RISCV::SH2ADD: ShAmt = 2;
break;
306 case RISCV::SH3ADD: ShAmt = 3;
break;
318bool RISCVMergeBaseOffsetOpt::detectAndFoldOffset(
MachineInstr &
Hi,
325 if (!
MRI->hasOneUse(DestReg))
330 switch (
Tail.getOpcode()) {
332 LLVM_DEBUG(
dbgs() <<
"Don't know how to get offset from this instr:"
341 if (
MRI->hasOneUse(TailDestReg)) {
343 if (TailTail.
getOpcode() == RISCV::ADDI) {
348 Tail.eraseFromParent();
365 return foldLargeOffset(
Hi,
Lo,
Tail, DestReg);
372 return foldShiftedOffset(
Hi,
Lo,
Tail, DestReg);
394 std::optional<int64_t> CommonOffset;
396 InlineAsmMemoryOpIndexesMap;
398 switch (
UseMI.getOpcode()) {
425 if (
UseMI.getOperand(1).isFI())
431 "Expected base address use");
434 if (CommonOffset &&
Offset != CommonOffset)
439 case RISCV::INLINEASM:
440 case RISCV::INLINEASM_BR: {
444 I <
UseMI.getNumOperands();
I += 1 + NumOps) {
447 if (!FlagsMO.
isImm())
451 NumOps =
Flags.getNumOperandRegisters();
454 if (NumOps != 2 || !
Flags.isMemKind()) {
457 for (
unsigned J = 0; J < NumOps; ++J) {
467 if (
Flags.getMemoryConstraintID() == InlineAsm::ConstraintCode::A)
475 if (!OffsetMO.
isImm())
480 if (CommonOffset &&
Offset != CommonOffset)
485 InlineAsmMemoryOpIndexesMap.
insert(
486 std::make_pair(&
UseMI, InlineAsmMemoryOpIndexes));
496 int64_t NewOffset =
Hi.getOperand(1).getOffset() + *CommonOffset;
499 NewOffset = SignExtend64<32>(NewOffset);
501 if (!isInt<32>(NewOffset))
504 Hi.getOperand(1).setOffset(NewOffset);
507 if (
Hi.getOpcode() == RISCV::PseudoMovAddr) {
508 auto *
TII =
ST->getInstrInfo();
509 Hi.setDesc(
TII->get(RISCV::LUI));
513 if (
Hi.getOpcode() != RISCV::AUIPC)
519 if (
UseMI.getOpcode() == RISCV::INLINEASM ||
520 UseMI.getOpcode() == RISCV::INLINEASM_BR) {
521 auto &InlineAsmMemoryOpIndexes = InlineAsmMemoryOpIndexesMap[&
UseMI];
522 for (
unsigned I : InlineAsmMemoryOpIndexes) {
543 UseMI.removeOperand(2);
544 UseMI.addOperand(ImmOp);
553 MRI->replaceRegWith(
Lo.getOperand(0).getReg(),
Hi.getOperand(0).getReg());
554 Lo.eraseFromParent();
558bool RISCVMergeBaseOffsetOpt::runOnMachineFunction(
MachineFunction &Fn) {
564 bool MadeChange =
false;
570 if (!detectFoldable(
Hi,
Lo))
572 MadeChange |= detectAndFoldOffset(
Hi, *
Lo);
573 MadeChange |= foldIntoMemoryOps(
Hi, *
Lo);
582 return new RISCVMergeBaseOffsetOpt();
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const HexagonInstrInfo * TII
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
#define RISCV_MERGE_BASE_OFFSET_NAME
support::ulittle16_t & Lo
support::ulittle16_t & Hi
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
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,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
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.
StringRef - 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.
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...
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
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)