26#define DEBUG_TYPE "riscv-fold-mem-offset"
27#define RISCV_FOLD_MEM_OFFSET_NAME "RISC-V Fold Memory Offset"
39 bool foldOffset(
Register OrigReg, int64_t InitialOffset,
54 std::optional<int64_t>
Offset;
57 bool hasValue()
const {
return Offset.has_value(); }
60 FoldableOffset &operator=(int64_t
RHS) {
77char RISCVFoldMemOffset::ID = 0;
82 return new RISCVFoldMemOffset();
92bool RISCVFoldMemOffset::foldOffset(
96 DenseMap<Register, int64_t> RegToOffsetMap;
99 RegToOffsetMap[OrigReg] = InitialOffset;
101 std::queue<Register> Worklist;
102 Worklist.push(OrigReg);
104 while (!Worklist.empty()) {
114 switch (
User.getOpcode()) {
118 if (
auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
119 I != RegToOffsetMap.
end())
121 if (
auto I = RegToOffsetMap.
find(
User.getOperand(2).getReg());
122 I != RegToOffsetMap.
end())
126 if (
auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
127 I != RegToOffsetMap.
end())
128 Offset = (uint64_t)
I->second << 1;
129 if (
auto I = RegToOffsetMap.
find(
User.getOperand(2).getReg());
130 I != RegToOffsetMap.
end())
134 if (
auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
135 I != RegToOffsetMap.
end())
136 Offset = (uint64_t)
I->second << 2;
137 if (
auto I = RegToOffsetMap.
find(
User.getOperand(2).getReg());
138 I != RegToOffsetMap.
end())
142 if (
auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
143 I != RegToOffsetMap.
end())
144 Offset = (uint64_t)
I->second << 3;
145 if (
auto I = RegToOffsetMap.
find(
User.getOperand(2).getReg());
146 I != RegToOffsetMap.
end())
150 case RISCV::SH1ADD_UW:
151 case RISCV::SH2ADD_UW:
152 case RISCV::SH3ADD_UW:
154 if (
User.getOperand(1).getReg() ==
Reg)
156 if (
auto I = RegToOffsetMap.
find(
User.getOperand(2).getReg());
157 I != RegToOffsetMap.
end())
161 unsigned ShAmt =
User.getOperand(2).getImm();
162 if (
auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
163 I != RegToOffsetMap.
end())
164 Offset = (uint64_t)
I->second << ShAmt;
191 if (
User.getOperand(0).getReg() ==
Reg)
195 if (!
User.getOperand(2).isImm())
200 if (
User.getOperand(1).getReg() == OrigReg)
203 auto I = RegToOffsetMap.
find(
User.getOperand(1).getReg());
204 if (
I == RegToOffsetMap.
end())
207 int64_t LocalOffset =
User.getOperand(2).getImm();
209 int64_t CombinedOffset = (uint64_t)LocalOffset + (uint64_t)
I->second;
213 FoldableInstrs[&
User] = CombinedOffset;
223 int64_t OffsetVal =
Offset.getValue();
227 Worklist.push(
User.getOperand(0).getReg());
228 }
else if (
P.first->second != OffsetVal) {
229 P.first->second = OffsetVal;
230 Worklist.push(
User.getOperand(0).getReg());
238bool RISCVFoldMemOffset::runOnMachineFunction(MachineFunction &MF) {
248 bool MadeChange =
false;
249 for (MachineBasicBlock &
MBB : MF) {
253 if (
MI.getOpcode() != RISCV::ADDI)
257 if (!
MI.getOperand(1).isReg() || !
MI.getOperand(2).isImm())
261 if (
MI.getOperand(1).getReg() == RISCV::X0)
264 int64_t
Offset =
MI.getOperand(2).getImm();
267 DenseMap<MachineInstr *, int64_t> FoldableInstrs;
269 if (!foldOffset(
MI.getOperand(0).getReg(),
Offset, MRI, FoldableInstrs))
272 if (FoldableInstrs.
empty())
277 for (
auto [MemMI, NewOffset] : FoldableInstrs)
278 MemMI->getOperand(2).setImm(NewOffset);
282 MI.eraseFromParent();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static constexpr Value * getValue(Ty &ValueOrUse)
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.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this 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,...
LLVM_ABI void clearKillFlags(Register Reg) const
clearKillFlags - Iterate over all the uses of the given register and clear the kill flag from the Mac...
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(Register Reg) const
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Represent a constant reference to a string, i.e.
@ 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...