LLVM 23.0.0git
LoadStoreVec.cpp
Go to the documentation of this file.
1//===- LoadStoreVec.cpp - Vectorizer pass short load-store chains ---------===//
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
19
20namespace llvm {
21
22extern cl::opt<int> CostThreshold; // Defined in TransactionAcceptOrRevert.cpp
23
24namespace sandboxir {
25
26#define DEBUG_PREFIX_LOCAL DEBUG_PREFIX "LoadStoreVec: "
27
28std::optional<Type *> LoadStoreVec::canVectorize(ArrayRef<Instruction *> Bndl,
29 Scheduler &Sched) {
30 // Check if in the same BB.
32 return std::nullopt;
33
34 // Check if instructions repeat.
36 return std::nullopt;
37
38 // Check scheduling.
39 if (!Sched.trySchedule(Bndl))
40 return std::nullopt;
41
43}
44
45void LoadStoreVec::tryEraseDeadInstrs(ArrayRef<Instruction *> Stores,
46 ArrayRef<Value *> Operands) {
47 SmallPtrSet<Instruction *, 8> DeadCandidates;
48 for (auto *SI : Stores) {
49 if (auto *PtrI =
51 DeadCandidates.insert(PtrI);
52 SI->eraseFromParent();
53 }
54 for (auto *Op : Operands) {
55 if (auto *LI = dyn_cast<LoadInst>(Op)) {
56 if (auto *PtrI =
58 DeadCandidates.insert(PtrI);
59 cast<LoadInst>(LI)->eraseFromParent();
60 }
61 }
62 for (auto *PtrI : DeadCandidates)
63 if (!PtrI->hasNUsesOrMore(1))
64 PtrI->eraseFromParent();
65}
66
68 SmallVector<Instruction *, 8> Bndl(Rgn.getAux().begin(), Rgn.getAux().end());
69 if (Bndl.size() < 2)
70 return false;
71 Function &F = *Bndl[0]->getParent()->getParent();
72 DL = &F.getParent()->getDataLayout();
73 auto &Ctx = F.getContext();
74 Scheduler Sched(A.getAA(), Ctx);
76 Bndl, A.getScalarEvolution(), *DL))
77 return false;
78 if (!canVectorize(Bndl, Sched))
79 return false;
80
81 const auto &SB = cast<RegionWithScore>(Rgn).getScoreboard();
82 InstructionCost CostBefore = SB.getAfterCost() - SB.getBeforeCost();
83
85 Operands.reserve(Bndl.size());
86 for (auto *I : Bndl) {
87 auto *Op = cast<StoreInst>(I)->getValueOperand();
88 Operands.push_back(Op);
89 }
90 BasicBlock *BB = Bndl[0]->getParent();
91 // TODO: For now we only support load operands.
92 // TODO: For now we don't cross BBs.
93 // TODO: For now don't vectorize if the loads have external uses.
94 bool AllLoads = all_of(Operands, [BB](Value *V) {
95 auto *LI = dyn_cast<LoadInst>(V);
96 if (LI == nullptr)
97 return false;
98 // TODO: For now we don't cross BBs.
99 if (LI->getParent() != BB)
100 return false;
101 if (LI->hasNUsesOrMore(2))
102 return false;
103 return true;
104 });
105 bool AllConstants =
106 all_of(Operands, [](Value *V) { return isa<Constant>(V); });
107 if (!AllLoads && !AllConstants)
108 return false;
109
110 // Vectorizing mixed floats and integers with external uses may not be
111 // profitable on some targets, so save state here.
112 Ctx.save();
113
114 Value *VecOp = nullptr;
115 if (AllLoads) {
116 // TODO: Try to avoid the extra copy to an instruction vector.
118 Loads.reserve(Operands.size());
119 for (Value *Op : Operands)
121
123 Loads, A.getScalarEvolution(), *DL);
124 if (!Consecutive) {
125 Ctx.accept();
126 return false;
127 }
128 if (!canVectorize(Loads, Sched)) {
129 Ctx.accept();
130 return false;
131 }
132
133 // Generate vector load.
135 Value *LdPtr = cast<LoadInst>(Loads[0])->getPointerOperand();
136 // TODO: Compute alignment.
137 Align LdAlign(1);
138 auto LdWhereIt = std::next(VecUtils::getLowest(Loads)->getIterator());
139 VecOp = LoadInst::create(Ty, LdPtr, LdAlign, LdWhereIt, Ctx, "VecIinitL");
140 } else if (AllConstants) {
142 Constants.reserve(Operands.size());
143 for (Value *Op : Operands) {
144 auto *COp = cast<Constant>(Op);
145 if (auto *AggrCOp = dyn_cast<ConstantAggregate>(COp)) {
146 // If the operand is a constant aggregate, then append all its elements.
147 for (Value *Elm : AggrCOp->operands())
148 Constants.push_back(cast<Constant>(Elm));
149 } else if (auto *SeqCOp = dyn_cast<ConstantDataSequential>(COp)) {
150 for (auto ElmIdx : seq<unsigned>(SeqCOp->getNumElements()))
151 Constants.push_back(SeqCOp->getElementAsConstant(ElmIdx));
152 } else if (auto *Zero = dyn_cast<ConstantAggregateZero>(COp)) {
153 auto *ZeroElm = Zero->getSequentialElement();
154 for ([[maybe_unused]] auto Cnt :
155 seq<unsigned>(Zero->getElementCount().getFixedValue()))
156 Constants.push_back(ZeroElm);
157 } else if (isa<ConstantInt>(COp) && isa<VectorType>(COp->getType())) {
158 auto *Elm = ConstantInt::get(Ctx, cast<ConstantInt>(COp)->getValue());
159 for ([[maybe_unused]] auto Cnt :
160 seq<unsigned>(cast<VectorType>(COp->getType())
161 ->getElementCount()
162 .getFixedValue()))
163 Constants.push_back(Elm);
164 } else if (isa<ConstantFP>(COp) && isa<VectorType>(COp->getType())) {
165 auto *Elm = ConstantFP::get(cast<ConstantFP>(COp)->getValue(), Ctx);
166 for ([[maybe_unused]] auto Cnt :
167 seq<unsigned>(cast<VectorType>(COp->getType())
168 ->getElementCount()
169 .getFixedValue()))
170 Constants.push_back(Elm);
171 } else {
172 Constants.push_back(COp);
173 }
174 }
175 VecOp = ConstantVector::get(Constants);
176 }
177
178 // Generate vector store.
179 Value *StPtr = cast<StoreInst>(Bndl[0])->getPointerOperand();
180 // TODO: Compute alignment.
181 Align StAlign(1);
182 auto StWhereIt = std::next(VecUtils::getLowest(Bndl)->getIterator());
183 StoreInst::create(VecOp, StPtr, StAlign, StWhereIt, Ctx);
184
185 tryEraseDeadInstrs(Bndl, Operands);
186
187 // Check the cost.
188 InstructionCost CostAfter = SB.getAfterCost() - SB.getBeforeCost();
189 InstructionCost CostGain = CostAfter - CostBefore;
190 LLVM_DEBUG(dbgs() << DEBUG_PREFIX_LOCAL << "CostGain=" << CostGain
191 << " (After=" << CostAfter << " Before=" << CostBefore
192 << ")\n");
193 if (CostGain > CostThreshold) {
194 LLVM_DEBUG(dbgs() << DEBUG_PREFIX_LOCAL << "Not profitable, reverting.\n");
195 Ctx.revert();
196 return false;
197 }
198 LLVM_DEBUG(dbgs() << DEBUG_PREFIX_LOCAL << "Profitable accepting.\n");
199 Ctx.accept();
200 return true;
201}
202
203} // namespace sandboxir
204
205} // namespace llvm
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
static constexpr Value * getValue(Ty &ValueOrUse)
#define DEBUG_PREFIX_LOCAL
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
PostRA Machine Instruction Scheduler
#define LLVM_DEBUG(...)
Definition Debug.h:119
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static LLVM_ABI Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition Constant.cpp:90
static LLVM_ABI Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition Constant.cpp:48
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
Definition Constant.cpp:176
static bool differentBlock(ArrayRef< ValueT * > Instrs)
Definition Legality.h:350
static bool areUnique(ArrayRef< ValueT * > Values)
Definition Legality.h:358
static LLVM_ABI LoadInst * create(Type *Ty, Value *Ptr, MaybeAlign Align, InsertPosition Pos, bool IsVolatile, Context &Ctx, const Twine &Name="")
bool runOnRegion(Region &Rgn, const Analyses &A) final
\Returns true if it modifies R.
const SmallVector< Instruction * > & getAux() const
\Returns the auxiliary vector.
Definition Region.h:177
The list scheduler.
Definition Scheduler.h:163
static LLVM_ABI StoreInst * create(Value *V, Value *Ptr, MaybeAlign Align, InsertPosition Pos, bool IsVolatile, Context &Ctx)
Just like llvm::Type these are immutable, unique, never get freed and can only be created via static ...
Definition Type.h:50
A SandboxIR Value has users. This is the base class.
Definition Value.h:72
static Instruction * getLowest(ArrayRef< Instruction * > Instrs)
\Returns the instruction in Instrs that is lowest in the BB.
Definition VecUtils.h:144
static Type * getCombinedVectorTypeFor(ArrayRef< Instruction * > Bndl, const DataLayout &DL)
\Returns the combined vector type for Bndl, even when the element types differ.
Definition VecUtils.h:123
static bool areConsecutive(LoadOrStoreT *I1, LoadOrStoreT *I2, ScalarEvolution &SE, const DataLayout &DL)
\Returns true if I1 and I2 are load/stores accessing consecutive memory addresses.
Definition VecUtils.h:56
BasicBlock(llvm::BasicBlock *BB, Context &SBCtx)
Definition BasicBlock.h:75
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1738
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
static cl::opt< unsigned > CostThreshold("dfa-cost-threshold", cl::desc("Maximum cost accepted for the transformation"), cl::Hidden, cl::init(50))
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39