File indexing completed on 2026-05-10 08:43:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef LLVM_CODEGEN_GLOBALISEL_LOADSTOREOPT_H
0015 #define LLVM_CODEGEN_GLOBALISEL_LOADSTOREOPT_H
0016
0017 #include "llvm/ADT/BitVector.h"
0018 #include "llvm/ADT/SmallPtrSet.h"
0019 #include "llvm/ADT/SmallSet.h"
0020 #include "llvm/ADT/SmallVector.h"
0021 #include "llvm/Analysis/AliasAnalysis.h"
0022 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
0023 #include "llvm/CodeGen/MachineFunction.h"
0024 #include "llvm/CodeGen/MachineFunctionPass.h"
0025
0026 namespace llvm {
0027
0028 class AnalysisUsage;
0029 class GStore;
0030 class LegalizerInfo;
0031 class MachineBasicBlock;
0032 class MachineInstr;
0033 class TargetLowering;
0034 struct LegalityQuery;
0035 class MachineRegisterInfo;
0036 namespace GISelAddressing {
0037
0038 class BaseIndexOffset {
0039 private:
0040 Register BaseReg;
0041 Register IndexReg;
0042 std::optional<int64_t> Offset;
0043
0044 public:
0045 BaseIndexOffset() = default;
0046 Register getBase() { return BaseReg; }
0047 Register getBase() const { return BaseReg; }
0048 Register getIndex() { return IndexReg; }
0049 Register getIndex() const { return IndexReg; }
0050 void setBase(Register NewBase) { BaseReg = NewBase; }
0051 void setIndex(Register NewIndex) { IndexReg = NewIndex; }
0052 void setOffset(std::optional<int64_t> NewOff) { Offset = NewOff; }
0053 bool hasValidOffset() const { return Offset.has_value(); }
0054 int64_t getOffset() const { return *Offset; }
0055 };
0056
0057
0058 BaseIndexOffset getPointerInfo(Register Ptr, MachineRegisterInfo &MRI);
0059
0060
0061
0062
0063 bool aliasIsKnownForLoadStore(const MachineInstr &MI1, const MachineInstr &MI2,
0064 bool &IsAlias, MachineRegisterInfo &MRI);
0065
0066
0067
0068
0069
0070 bool instMayAlias(const MachineInstr &MI, const MachineInstr &Other,
0071 MachineRegisterInfo &MRI, AliasAnalysis *AA);
0072 }
0073
0074 using namespace GISelAddressing;
0075
0076 class LoadStoreOpt : public MachineFunctionPass {
0077 public:
0078 static char ID;
0079
0080 private:
0081
0082
0083 std::function<bool(const MachineFunction &)> DoNotRunPass;
0084
0085 MachineRegisterInfo *MRI = nullptr;
0086 const TargetLowering *TLI = nullptr;
0087 MachineFunction *MF = nullptr;
0088 AliasAnalysis *AA = nullptr;
0089 const LegalizerInfo *LI = nullptr;
0090
0091 MachineIRBuilder Builder;
0092
0093
0094 void init(MachineFunction &MF);
0095
0096 class StoreMergeCandidate {
0097 public:
0098
0099 Register BasePtr;
0100
0101
0102
0103
0104 int64_t CurrentLowestOffset;
0105 SmallVector<GStore *> Stores;
0106
0107
0108
0109
0110
0111
0112
0113 SmallVector<std::pair<MachineInstr *, unsigned>> PotentialAliases;
0114
0115 void addPotentialAlias(MachineInstr &MI);
0116
0117
0118 void reset() {
0119 Stores.clear();
0120 PotentialAliases.clear();
0121 CurrentLowestOffset = 0;
0122 BasePtr = Register();
0123 }
0124 };
0125
0126 bool isLegalOrBeforeLegalizer(const LegalityQuery &Query,
0127 MachineFunction &MF) const;
0128
0129
0130 bool addStoreToCandidate(GStore &MI, StoreMergeCandidate &C);
0131
0132
0133 bool operationAliasesWithCandidate(MachineInstr &MI, StoreMergeCandidate &C);
0134
0135
0136
0137
0138 bool mergeStores(SmallVectorImpl<GStore *> &StoresToMerge);
0139
0140
0141
0142
0143 bool doSingleStoreMerge(SmallVectorImpl<GStore *> &Stores);
0144 bool processMergeCandidate(StoreMergeCandidate &C);
0145 bool mergeBlockStores(MachineBasicBlock &MBB);
0146 bool mergeFunctionStores(MachineFunction &MF);
0147
0148 bool mergeTruncStore(GStore &StoreMI,
0149 SmallPtrSetImpl<GStore *> &DeletedStores);
0150 bool mergeTruncStoresBlock(MachineBasicBlock &MBB);
0151
0152
0153
0154
0155 void initializeStoreMergeTargetInfo(unsigned AddrSpace = 0);
0156
0157
0158
0159
0160 DenseMap<unsigned, BitVector> LegalStoreSizes;
0161 bool IsPreLegalizer = false;
0162
0163 SmallSet<MachineInstr *, 16> InstsToErase;
0164
0165 public:
0166 LoadStoreOpt();
0167 LoadStoreOpt(std::function<bool(const MachineFunction &)>);
0168
0169 StringRef getPassName() const override { return "LoadStoreOpt"; }
0170
0171 MachineFunctionProperties getRequiredProperties() const override {
0172 return MachineFunctionProperties()
0173 .set(MachineFunctionProperties::Property::IsSSA);
0174 }
0175
0176 void getAnalysisUsage(AnalysisUsage &AU) const override;
0177
0178 bool runOnMachineFunction(MachineFunction &MF) override;
0179 };
0180
0181 }
0182
0183 #endif