25#define DEBUG_TYPE "riscv-fold-mem-offset"
26#define RISCV_FOLD_MEM_OFFSET_NAME "RISC-V Fold Memory Offset"
38 bool foldOffset(
Register OrigReg, int64_t InitialOffset,
52 std::optional<int64_t>
Offset;
55 bool hasValue()
const {
return Offset.has_value(); }
56 int64_t getValue()
const {
return *
Offset; }
58 FoldableOffset &operator=(int64_t
RHS) {
75char RISCVFoldMemOffset::ID = 0;
80 return new RISCVFoldMemOffset();
90bool RISCVFoldMemOffset::foldOffset(
94 DenseMap<Register, int64_t> RegToOffsetMap;
97 RegToOffsetMap[OrigReg] = InitialOffset;
99 std::queue<Register> Worklist;
100 Worklist.push(OrigReg);
102 while (!Worklist.empty()) {
109 for (
auto &User :
MRI.use_nodbg_instructions(
Reg)) {
112 switch (
User.getOpcode()) {
116 if (
auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
117 I != RegToOffsetMap.
end())
119 if (
auto I = RegToOffsetMap.
find(
User.getOperand(2).getReg());
120 I != RegToOffsetMap.
end())
124 if (
auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
125 I != RegToOffsetMap.
end())
126 Offset = (uint64_t)
I->second << 1;
127 if (
auto I = RegToOffsetMap.
find(
User.getOperand(2).getReg());
128 I != RegToOffsetMap.
end())
132 if (
auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
133 I != RegToOffsetMap.
end())
134 Offset = (uint64_t)
I->second << 2;
135 if (
auto I = RegToOffsetMap.
find(
User.getOperand(2).getReg());
136 I != RegToOffsetMap.
end())
140 if (
auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
141 I != RegToOffsetMap.
end())
142 Offset = (uint64_t)
I->second << 3;
143 if (
auto I = RegToOffsetMap.
find(
User.getOperand(2).getReg());
144 I != RegToOffsetMap.
end())
148 case RISCV::SH1ADD_UW:
149 case RISCV::SH2ADD_UW:
150 case RISCV::SH3ADD_UW:
152 if (
User.getOperand(1).getReg() ==
Reg)
154 if (
auto I = RegToOffsetMap.
find(
User.getOperand(2).getReg());
155 I != RegToOffsetMap.
end())
159 unsigned ShAmt =
User.getOperand(2).getImm();
160 if (
auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
161 I != RegToOffsetMap.
end())
162 Offset = (uint64_t)
I->second << ShAmt;
189 if (
User.getOperand(0).getReg() ==
Reg)
193 if (!
User.getOperand(2).isImm())
198 if (
User.getOperand(1).getReg() == OrigReg)
201 auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
202 if (
I == RegToOffsetMap.
end())
205 int64_t LocalOffset =
User.getOperand(2).getImm();
207 int64_t CombinedOffset = (uint64_t)LocalOffset + (uint64_t)
I->second;
211 FoldableInstrs[&
User] = CombinedOffset;
221 int64_t OffsetVal =
Offset.getValue();
225 Worklist.push(
User.getOperand(0).getReg());
226 }
else if (
P.first->second != OffsetVal) {
227 P.first->second = OffsetVal;
228 Worklist.push(
User.getOperand(0).getReg());
236bool RISCVFoldMemOffset::runOnMachineFunction(MachineFunction &MF) {
246 bool MadeChange =
false;
247 for (MachineBasicBlock &
MBB : MF) {
251 if (
MI.getOpcode() != RISCV::ADDI)
255 if (!
MI.getOperand(1).isReg() || !
MI.getOperand(2).isImm())
259 if (
MI.getOperand(1).getReg() == RISCV::X0)
262 int64_t
Offset =
MI.getOperand(2).getImm();
265 DenseMap<MachineInstr *, int64_t> FoldableInstrs;
267 if (!foldOffset(
MI.getOperand(0).getReg(),
Offset,
MRI, FoldableInstrs))
270 if (FoldableInstrs.
empty())
275 for (
auto [MemMI, NewOffset] : FoldableInstrs)
276 MemMI->getOperand(2).setImm(NewOffset);
278 MRI.replaceRegWith(
MI.getOperand(0).getReg(),
MI.getOperand(1).getReg());
279 MRI.clearKillFlags(
MI.getOperand(1).getReg());
280 MI.eraseFromParent();
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
#define RISCV_FOLD_MEM_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:
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
FunctionPass class - This class is used to implement most global optimizations.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
StringRef - Represent a constant reference to a string, i.e.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ User
could "use" a pointer
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createRISCVFoldMemOffsetPass()
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
APInt operator*(APInt a, uint64_t RHS)
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt & operator+=(DynamicAPInt &A, int64_t 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...