Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
LoadStoreOpt.h
Go to the documentation of this file.
1//== llvm/CodeGen/GlobalISel/LoadStoreOpt.h - LoadStoreOpt -------*- C++ -*-==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// This is an optimization pass for GlobalISel generic memory operations.
10/// Specifically, it focuses on merging stores and loads to consecutive
11/// addresses.
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_GLOBALISEL_LOADSTOREOPT_H
15#define LLVM_CODEGEN_GLOBALISEL_LOADSTOREOPT_H
16
17#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallSet.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/Analysis/AliasAnalysis.h"
22#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25
26namespacellvm {
27// Forward declarations.
28classAnalysisUsage;
29classGStore;
30classLegalizerInfo;
31classMachineBasicBlock;
32classMachineInstr;
33classTargetLowering;
34structLegalityQuery;
35classMachineRegisterInfo;
36namespaceGISelAddressing {
37/// Helper struct to store a base, index and offset that forms an address
38classBaseIndexOffset {
39private:
40Register BaseReg;
41Register IndexReg;
42 std::optional<int64_t> Offset;
43
44public:
45BaseIndexOffset() =default;
46RegistergetBase() {return BaseReg; }
47RegistergetBase() const{return BaseReg; }
48RegistergetIndex() {return IndexReg; }
49RegistergetIndex() const{return IndexReg; }
50voidsetBase(Register NewBase) { BaseReg = NewBase; }
51voidsetIndex(Register NewIndex) { IndexReg = NewIndex; }
52voidsetOffset(std::optional<int64_t> NewOff) { Offset = NewOff; }
53boolhasValidOffset() const{return Offset.has_value(); }
54 int64_tgetOffset() const{return *Offset; }
55};
56
57/// Returns a BaseIndexOffset which describes the pointer in \p Ptr.
58BaseIndexOffsetgetPointerInfo(RegisterPtr,MachineRegisterInfo &MRI);
59
60/// Compute whether or not a memory access at \p MI1 aliases with an access at
61/// \p MI2 \returns true if either alias/no-alias is known. Sets \p IsAlias
62/// accordingly.
63boolaliasIsKnownForLoadStore(constMachineInstr &MI1,constMachineInstr &MI2,
64bool &IsAlias,MachineRegisterInfo &MRI);
65
66/// Returns true if the instruction \p MI may alias \p Other.
67/// This function uses multiple strategies to detect aliasing, whereas
68/// aliasIsKnownForLoadStore just looks at the addresses of load/stores and is
69/// tries to reason about base/index/offsets.
70boolinstMayAlias(constMachineInstr &MI,constMachineInstr &Other,
71MachineRegisterInfo &MRI,AliasAnalysis *AA);
72}// namespace GISelAddressing
73
74using namespaceGISelAddressing;
75
76classLoadStoreOpt :publicMachineFunctionPass {
77public:
78staticcharID;
79
80private:
81 /// An input function to decide if the pass should run or not
82 /// on the given MachineFunction.
83 std::function<bool(constMachineFunction &)> DoNotRunPass;
84
85MachineRegisterInfo *MRI =nullptr;
86constTargetLowering *TLI =nullptr;
87MachineFunction *MF =nullptr;
88AliasAnalysis *AA =nullptr;
89constLegalizerInfo *LI =nullptr;
90
91MachineIRBuilder Builder;
92
93 /// Initialize the field members using \p MF.
94void init(MachineFunction &MF);
95
96classStoreMergeCandidate {
97public:
98// The base pointer used as the base for all stores in this candidate.
99Register BasePtr;
100// Our algorithm is very simple at the moment. We assume that in instruction
101// order stores are writing to incremeneting consecutive addresses. So when
102// we walk the block in reverse order, the next eligible store must write to
103// an offset one store width lower than CurrentLowestOffset.
104 int64_t CurrentLowestOffset;
105SmallVector<GStore *> Stores;
106// A vector of MachineInstr/unsigned pairs to denote potential aliases that
107// need to be checked before the candidate is considered safe to merge. The
108// unsigned value is an index into the Stores vector. The indexed store is
109// the highest-indexed store that has already been checked to not have an
110// alias with the instruction. We record this so we don't have to repeat
111// alias checks that have been already done, only those with stores added
112// after the potential alias is recorded.
113SmallVector<std::pair<MachineInstr *, unsigned>> PotentialAliases;
114
115void addPotentialAlias(MachineInstr &MI);
116
117 /// Reset this candidate back to an empty one.
118void reset() {
119 Stores.clear();
120 PotentialAliases.clear();
121 CurrentLowestOffset = 0;
122 BasePtr =Register();
123 }
124 };
125
126bool isLegalOrBeforeLegalizer(const LegalityQuery &Query,
127 MachineFunction &MF)const;
128 /// If the given store is valid to be a member of the candidate, add it and
129 /// return true. Otherwise, returns false.
130bool addStoreToCandidate(GStore &MI, StoreMergeCandidate &C);
131 /// Returns true if the instruction \p MI would potentially alias with any
132 /// stores in the candidate \p C.
133bool operationAliasesWithCandidate(MachineInstr &MI, StoreMergeCandidate &C);
134 /// Merges the stores in the given vector into a wide store.
135 /// \p returns true if at least some of the stores were merged.
136 /// This may decide not to merge stores if heuristics predict it will not be
137 /// worth it.
138bool mergeStores(SmallVectorImpl<GStore *> &StoresToMerge);
139 /// Perform a merge of all the stores in \p Stores into a single store.
140 /// Erases the old stores from the block when finished.
141 /// \returns true if merging was done. It may fail to perform a merge if
142 /// there are issues with materializing legal wide values.
143bool doSingleStoreMerge(SmallVectorImpl<GStore *> &Stores);
144bool processMergeCandidate(StoreMergeCandidate &C);
145bool mergeBlockStores(MachineBasicBlock &MBB);
146bool mergeFunctionStores(MachineFunction &MF);
147
148bool mergeTruncStore(GStore &StoreMI,
149 SmallPtrSetImpl<GStore *> &DeletedStores);
150bool mergeTruncStoresBlock(MachineBasicBlock &MBB);
151
152 /// Initialize some target-specific data structures for the store merging
153 /// optimization. \p AddrSpace indicates which address space to use when
154 /// probing the legalizer info for legal stores.
155void initializeStoreMergeTargetInfo(unsigned AddrSpace = 0);
156 /// A map between address space numbers and a bitvector of supported stores
157 /// sizes. Each bit in the bitvector represents whether a store size of
158 /// that bit's value is legal. E.g. if bit 64 is set, then 64 bit scalar
159 /// stores are legal.
160 DenseMap<unsigned, BitVector> LegalStoreSizes;
161bool IsPreLegalizer =false;
162 /// Contains instructions to be erased at the end of a block scan.
163 SmallSet<MachineInstr *, 16> InstsToErase;
164
165public:
166LoadStoreOpt();
167LoadStoreOpt(std::function<bool(const MachineFunction &)>);
168
169StringRefgetPassName() const override{return"LoadStoreOpt"; }
170
171MachineFunctionPropertiesgetRequiredProperties() const override{
172returnMachineFunctionProperties()
173 .set(MachineFunctionProperties::Property::IsSSA);
174 }
175
176voidgetAnalysisUsage(AnalysisUsage &AU)const override;
177
178boolrunOnMachineFunction(MachineFunction &MF)override;
179};
180
181}// End namespace llvm.
182
183#endif
MRI
unsigned const MachineRegisterInfo * MRI
Definition:AArch64AdvSIMDScalarPass.cpp:105
MBB
MachineBasicBlock & MBB
Definition:ARMSLSHardening.cpp:71
AliasAnalysis.h
BitVector.h
This file implements the BitVector class.
MI
IRTranslator LLVM IR MI
Definition:IRTranslator.cpp:112
MachineFunctionPass.h
MachineFunction.h
MachineIRBuilder.h
This file declares the MachineIRBuilder class.
SmallPtrSet.h
This file defines the SmallPtrSet class.
SmallSet.h
This file defines the SmallSet class.
SmallVector.h
This file defines the SmallVector class.
Ptr
@ Ptr
Definition:TargetLibraryInfo.cpp:77
bool
llvm::AAResults
Definition:AliasAnalysis.h:314
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition:PassAnalysisSupport.h:47
llvm::GISelAddressing::BaseIndexOffset
Helper struct to store a base, index and offset that forms an address.
Definition:LoadStoreOpt.h:38
llvm::GISelAddressing::BaseIndexOffset::getIndex
Register getIndex() const
Definition:LoadStoreOpt.h:49
llvm::GISelAddressing::BaseIndexOffset::setOffset
void setOffset(std::optional< int64_t > NewOff)
Definition:LoadStoreOpt.h:52
llvm::GISelAddressing::BaseIndexOffset::getIndex
Register getIndex()
Definition:LoadStoreOpt.h:48
llvm::GISelAddressing::BaseIndexOffset::getOffset
int64_t getOffset() const
Definition:LoadStoreOpt.h:54
llvm::GISelAddressing::BaseIndexOffset::getBase
Register getBase() const
Definition:LoadStoreOpt.h:47
llvm::GISelAddressing::BaseIndexOffset::BaseIndexOffset
BaseIndexOffset()=default
llvm::GISelAddressing::BaseIndexOffset::hasValidOffset
bool hasValidOffset() const
Definition:LoadStoreOpt.h:53
llvm::GISelAddressing::BaseIndexOffset::setIndex
void setIndex(Register NewIndex)
Definition:LoadStoreOpt.h:51
llvm::GISelAddressing::BaseIndexOffset::setBase
void setBase(Register NewBase)
Definition:LoadStoreOpt.h:50
llvm::GISelAddressing::BaseIndexOffset::getBase
Register getBase()
Definition:LoadStoreOpt.h:46
llvm::LegalizerInfo
Definition:LegalizerInfo.h:1311
llvm::LoadStoreOpt
Definition:LoadStoreOpt.h:76
llvm::LoadStoreOpt::ID
static char ID
Definition:LoadStoreOpt.h:78
llvm::LoadStoreOpt::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition:LoadStoreOpt.cpp:75
llvm::LoadStoreOpt::getRequiredProperties
MachineFunctionProperties getRequiredProperties() const override
Definition:LoadStoreOpt.h:171
llvm::LoadStoreOpt::LoadStoreOpt
LoadStoreOpt()
Definition:LoadStoreOpt.cpp:60
llvm::LoadStoreOpt::getPassName
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Definition:LoadStoreOpt.h:169
llvm::LoadStoreOpt::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition:LoadStoreOpt.cpp:975
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition:MachineFunctionPass.h:30
llvm::MachineFunctionProperties
Properties which a MachineFunction may have at a given point in time.
Definition:MachineFunction.h:137
llvm::MachineFunctionProperties::set
MachineFunctionProperties & set(Property P)
Definition:MachineFunction.h:207
llvm::MachineFunctionProperties::Property::IsSSA
@ IsSSA
llvm::MachineFunction
Definition:MachineFunction.h:267
llvm::MachineIRBuilder
Helper class to build MachineInstr.
Definition:MachineIRBuilder.h:235
llvm::MachineInstr
Representation of each machine instruction.
Definition:MachineInstr.h:71
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition:MachineRegisterInfo.h:51
llvm::Register
Wrapper class representing virtual and physical registers.
Definition:Register.h:19
llvm::SmallVectorImpl::clear
void clear()
Definition:SmallVector.h:610
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::TargetLowering
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
Definition:TargetLowering.h:3780
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::GISelAddressing::aliasIsKnownForLoadStore
bool aliasIsKnownForLoadStore(const MachineInstr &MI1, const MachineInstr &MI2, bool &IsAlias, MachineRegisterInfo &MRI)
Compute whether or not a memory access at MI1 aliases with an access at MI2.
Definition:LoadStoreOpt.cpp:103
llvm::GISelAddressing::getPointerInfo
BaseIndexOffset getPointerInfo(Register Ptr, MachineRegisterInfo &MRI)
Returns a BaseIndexOffset which describes the pointer in Ptr.
Definition:LoadStoreOpt.cpp:82
llvm::GISelAddressing::instMayAlias
bool instMayAlias(const MachineInstr &MI, const MachineInstr &Other, MachineRegisterInfo &MRI, AliasAnalysis *AA)
Returns true if the instruction MI may alias Other.
Definition:LoadStoreOpt.cpp:187
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::IRMemLocation::Other
@ Other
Any other memory.

Generated on Thu Jul 17 2025 09:46:09 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp