0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===- Loads.cpp - Local load analysis ------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
147
|
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
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 // This file defines simple local analyses for load instructions.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 #include "llvm/Analysis/Loads.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 #include "llvm/Analysis/AliasAnalysis.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 #include "llvm/Analysis/ValueTracking.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 #include "llvm/IR/DataLayout.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 #include "llvm/IR/GlobalAlias.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 #include "llvm/IR/GlobalVariable.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 #include "llvm/IR/IntrinsicInst.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 #include "llvm/IR/LLVMContext.h"
|
95
|
21 #include "llvm/IR/Module.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 #include "llvm/IR/Operator.h"
|
120
|
23 #include "llvm/IR/Statepoint.h"
|
|
24
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26
|
120
|
27 static bool isAligned(const Value *Base, const APInt &Offset, unsigned Align,
|
|
28 const DataLayout &DL) {
|
|
29 APInt BaseAlign(Offset.getBitWidth(), Base->getPointerAlignment(DL));
|
|
30
|
|
31 if (!BaseAlign) {
|
|
32 Type *Ty = Base->getType()->getPointerElementType();
|
|
33 if (!Ty->isSized())
|
|
34 return false;
|
|
35 BaseAlign = DL.getABITypeAlignment(Ty);
|
|
36 }
|
|
37
|
|
38 APInt Alignment(Offset.getBitWidth(), Align);
|
|
39
|
|
40 assert(Alignment.isPowerOf2() && "must be a power of 2!");
|
|
41 return BaseAlign.uge(Alignment) && !(Offset & (Alignment-1));
|
|
42 }
|
|
43
|
|
44 static bool isAligned(const Value *Base, unsigned Align, const DataLayout &DL) {
|
|
45 Type *Ty = Base->getType();
|
|
46 assert(Ty->isSized() && "must be sized");
|
|
47 APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0);
|
|
48 return isAligned(Base, Offset, Align, DL);
|
|
49 }
|
|
50
|
|
51 /// Test if V is always a pointer to allocated and suitably aligned memory for
|
|
52 /// a simple load or store.
|
|
53 static bool isDereferenceableAndAlignedPointer(
|
|
54 const Value *V, unsigned Align, const APInt &Size, const DataLayout &DL,
|
|
55 const Instruction *CtxI, const DominatorTree *DT,
|
|
56 SmallPtrSetImpl<const Value *> &Visited) {
|
|
57 // Already visited? Bail out, we've likely hit unreachable code.
|
|
58 if (!Visited.insert(V).second)
|
|
59 return false;
|
|
60
|
|
61 // Note that it is not safe to speculate into a malloc'd region because
|
|
62 // malloc may return null.
|
|
63
|
|
64 // bitcast instructions are no-ops as far as dereferenceability is concerned.
|
|
65 if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V))
|
|
66 return isDereferenceableAndAlignedPointer(BC->getOperand(0), Align, Size,
|
|
67 DL, CtxI, DT, Visited);
|
|
68
|
|
69 bool CheckForNonNull = false;
|
|
70 APInt KnownDerefBytes(Size.getBitWidth(),
|
|
71 V->getPointerDereferenceableBytes(DL, CheckForNonNull));
|
|
72 if (KnownDerefBytes.getBoolValue()) {
|
|
73 if (KnownDerefBytes.uge(Size))
|
121
|
74 if (!CheckForNonNull || isKnownNonZero(V, DL, 0, nullptr, CtxI, DT))
|
120
|
75 return isAligned(V, Align, DL);
|
|
76 }
|
|
77
|
|
78 // For GEPs, determine if the indexing lands within the allocated object.
|
|
79 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
|
|
80 const Value *Base = GEP->getPointerOperand();
|
|
81
|
134
|
82 APInt Offset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
|
120
|
83 if (!GEP->accumulateConstantOffset(DL, Offset) || Offset.isNegative() ||
|
|
84 !Offset.urem(APInt(Offset.getBitWidth(), Align)).isMinValue())
|
|
85 return false;
|
|
86
|
|
87 // If the base pointer is dereferenceable for Offset+Size bytes, then the
|
|
88 // GEP (== Base + Offset) is dereferenceable for Size bytes. If the base
|
|
89 // pointer is aligned to Align bytes, and the Offset is divisible by Align
|
|
90 // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also
|
|
91 // aligned to Align bytes.
|
|
92
|
|
93 // Offset and Size may have different bit widths if we have visited an
|
|
94 // addrspacecast, so we can't do arithmetic directly on the APInt values.
|
|
95 return isDereferenceableAndAlignedPointer(
|
|
96 Base, Align, Offset + Size.sextOrTrunc(Offset.getBitWidth()),
|
|
97 DL, CtxI, DT, Visited);
|
|
98 }
|
|
99
|
|
100 // For gc.relocate, look through relocations
|
|
101 if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V))
|
|
102 return isDereferenceableAndAlignedPointer(
|
|
103 RelocateInst->getDerivedPtr(), Align, Size, DL, CtxI, DT, Visited);
|
|
104
|
|
105 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
|
|
106 return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, Size,
|
|
107 DL, CtxI, DT, Visited);
|
|
108
|
147
|
109 if (const auto *Call = dyn_cast<CallBase>(V))
|
|
110 if (auto *RP = getArgumentAliasingToReturnedPointer(Call))
|
|
111 return isDereferenceableAndAlignedPointer(RP, Align, Size, DL, CtxI, DT,
|
120
|
112 Visited);
|
|
113
|
|
114 // If we don't know, assume the worst.
|
|
115 return false;
|
|
116 }
|
|
117
|
|
118 bool llvm::isDereferenceableAndAlignedPointer(const Value *V, unsigned Align,
|
121
|
119 const APInt &Size,
|
|
120 const DataLayout &DL,
|
|
121 const Instruction *CtxI,
|
|
122 const DominatorTree *DT) {
|
|
123 SmallPtrSet<const Value *, 32> Visited;
|
|
124 return ::isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT,
|
|
125 Visited);
|
|
126 }
|
|
127
|
147
|
128 bool llvm::isDereferenceableAndAlignedPointer(const Value *V, Type *Ty,
|
|
129 unsigned Align,
|
120
|
130 const DataLayout &DL,
|
|
131 const Instruction *CtxI,
|
|
132 const DominatorTree *DT) {
|
|
133 // When dereferenceability information is provided by a dereferenceable
|
|
134 // attribute, we know exactly how many bytes are dereferenceable. If we can
|
|
135 // determine the exact offset to the attributed variable, we can use that
|
|
136 // information here.
|
|
137
|
|
138 // Require ABI alignment for loads without alignment specification
|
|
139 if (Align == 0)
|
|
140 Align = DL.getABITypeAlignment(Ty);
|
|
141
|
|
142 if (!Ty->isSized())
|
|
143 return false;
|
|
144
|
|
145 SmallPtrSet<const Value *, 32> Visited;
|
|
146 return ::isDereferenceableAndAlignedPointer(
|
147
|
147 V, Align,
|
|
148 APInt(DL.getIndexTypeSizeInBits(V->getType()), DL.getTypeStoreSize(Ty)),
|
|
149 DL, CtxI, DT, Visited);
|
120
|
150 }
|
|
151
|
147
|
152 bool llvm::isDereferenceablePointer(const Value *V, Type *Ty,
|
|
153 const DataLayout &DL,
|
120
|
154 const Instruction *CtxI,
|
|
155 const DominatorTree *DT) {
|
147
|
156 return isDereferenceableAndAlignedPointer(V, Ty, 1, DL, CtxI, DT);
|
120
|
157 }
|
|
158
|
147
|
159 /// Test if A and B will obviously have the same value.
|
83
|
160 ///
|
|
161 /// This includes recognizing that %t0 and %t1 will have the same
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
162 /// value in code like this:
|
83
|
163 /// \code
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
164 /// %t0 = getelementptr \@a, 0, 3
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
165 /// store i32 0, i32* %t0
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
166 /// %t1 = getelementptr \@a, 0, 3
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
167 /// %t2 = load i32* %t1
|
83
|
168 /// \endcode
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169 ///
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
170 static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
171 // Test if the values are trivially equivalent.
|
83
|
172 if (A == B)
|
|
173 return true;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
174
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
175 // Test if the values come from identical arithmetic instructions.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
176 // Use isIdenticalToWhenDefined instead of isIdenticalTo because
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
177 // this function is only used when one address use dominates the
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
178 // other, which means that they'll always either have the same
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
179 // value or one of them will have an undefined value.
|
83
|
180 if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
|
|
181 isa<GetElementPtrInst>(A))
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
182 if (const Instruction *BI = dyn_cast<Instruction>(B))
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
183 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
184 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
185
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186 // Otherwise they may not be equivalent.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
187 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
188 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
189
|
147
|
190 /// Check if executing a load of this pointer value cannot trap.
|
83
|
191 ///
|
120
|
192 /// If DT and ScanFrom are specified this method performs context-sensitive
|
|
193 /// analysis and returns true if it is safe to load immediately before ScanFrom.
|
|
194 ///
|
83
|
195 /// If it is not obviously safe to load from the specified pointer, we do
|
|
196 /// a quick local scan of the basic block containing \c ScanFrom, to determine
|
|
197 /// if the address is already accessed.
|
|
198 ///
|
|
199 /// This uses the pointee type to determine how many bytes need to be safe to
|
|
200 /// load from the pointer.
|
147
|
201 bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align, APInt &Size,
|
120
|
202 const DataLayout &DL,
|
|
203 Instruction *ScanFrom,
|
|
204 const DominatorTree *DT) {
|
95
|
205 // Zero alignment means that the load has the ABI alignment for the target
|
|
206 if (Align == 0)
|
|
207 Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
|
|
208 assert(isPowerOf2_32(Align));
|
|
209
|
120
|
210 // If DT is not specified we can't make context-sensitive query
|
|
211 const Instruction* CtxI = DT ? ScanFrom : nullptr;
|
147
|
212 if (isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT))
|
100
|
213 return true;
|
|
214
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
215 int64_t ByteOffset = 0;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
216 Value *Base = V;
|
83
|
217 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
218
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
219 if (ByteOffset < 0) // out of bounds
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
220 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
221
|
77
|
222 Type *BaseType = nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
223 unsigned BaseAlign = 0;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
224 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
225 // An alloca is safe to load from as load as it is suitably aligned.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
226 BaseType = AI->getAllocatedType();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
227 BaseAlign = AI->getAlignment();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
228 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
|
83
|
229 // Global variables are not necessarily safe to load from if they are
|
120
|
230 // interposed arbitrarily. Their size may change or they may be weak and
|
|
231 // require a test to determine if they were in fact provided.
|
|
232 if (!GV->isInterposable()) {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
233 BaseType = GV->getType()->getElementType();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
234 BaseAlign = GV->getAlignment();
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
235 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
236 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
237
|
83
|
238 PointerType *AddrTy = cast<PointerType>(V->getType());
|
95
|
239 uint64_t LoadSize = DL.getTypeStoreSize(AddrTy->getElementType());
|
83
|
240
|
|
241 // If we found a base allocated type from either an alloca or global variable,
|
|
242 // try to see if we are definitively within the allocated region. We need to
|
|
243 // know the size of the base type and the loaded type to do anything in this
|
95
|
244 // case.
|
|
245 if (BaseType && BaseType->isSized()) {
|
83
|
246 if (BaseAlign == 0)
|
95
|
247 BaseAlign = DL.getPrefTypeAlignment(BaseType);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
248
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
249 if (Align <= BaseAlign) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
250 // Check if the load is within the bounds of the underlying object.
|
95
|
251 if (ByteOffset + LoadSize <= DL.getTypeAllocSize(BaseType) &&
|
|
252 ((ByteOffset % Align) == 0))
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
253 return true;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
254 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
255 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
256
|
120
|
257 if (!ScanFrom)
|
|
258 return false;
|
|
259
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
260 // Otherwise, be a little bit aggressive by scanning the local block where we
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
261 // want to check to see if the pointer is already being loaded or stored
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
262 // from/to. If so, the previous load or store would have already trapped,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
263 // so there is no harm doing an extra load (also, CSE will later eliminate
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
264 // the load entirely).
|
95
|
265 BasicBlock::iterator BBI = ScanFrom->getIterator(),
|
|
266 E = ScanFrom->getParent()->begin();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
267
|
83
|
268 // We can at least always strip pointer casts even though we can't use the
|
|
269 // base here.
|
|
270 V = V->stripPointerCasts();
|
|
271
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
272 while (BBI != E) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
273 --BBI;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
274
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
275 // If we see a free or a call which may write to memory (i.e. which might do
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
276 // a free) the pointer could be marked invalid.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
277 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
278 !isa<DbgInfoIntrinsic>(BBI))
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
279 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
280
|
83
|
281 Value *AccessedPtr;
|
95
|
282 unsigned AccessedAlign;
|
|
283 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
|
147
|
284 // Ignore volatile loads. The execution of a volatile load cannot
|
|
285 // be used to prove an address is backed by regular memory; it can,
|
|
286 // for example, point to an MMIO register.
|
|
287 if (LI->isVolatile())
|
|
288 continue;
|
83
|
289 AccessedPtr = LI->getPointerOperand();
|
95
|
290 AccessedAlign = LI->getAlignment();
|
|
291 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
|
147
|
292 // Ignore volatile stores (see comment for loads).
|
|
293 if (SI->isVolatile())
|
|
294 continue;
|
83
|
295 AccessedPtr = SI->getPointerOperand();
|
95
|
296 AccessedAlign = SI->getAlignment();
|
|
297 } else
|
83
|
298 continue;
|
|
299
|
95
|
300 Type *AccessedTy = AccessedPtr->getType()->getPointerElementType();
|
|
301 if (AccessedAlign == 0)
|
|
302 AccessedAlign = DL.getABITypeAlignment(AccessedTy);
|
|
303 if (AccessedAlign < Align)
|
|
304 continue;
|
|
305
|
|
306 // Handle trivial cases.
|
83
|
307 if (AccessedPtr == V)
|
|
308 return true;
|
|
309
|
|
310 if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
|
95
|
311 LoadSize <= DL.getTypeStoreSize(AccessedTy))
|
83
|
312 return true;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
313 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
314 return false;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
315 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
316
|
147
|
317 bool llvm::isSafeToLoadUnconditionally(Value *V, Type *Ty, unsigned Align,
|
|
318 const DataLayout &DL,
|
|
319 Instruction *ScanFrom,
|
|
320 const DominatorTree *DT) {
|
|
321 APInt Size(DL.getIndexTypeSizeInBits(V->getType()), DL.getTypeStoreSize(Ty));
|
|
322 return isSafeToLoadUnconditionally(V, Align, Size, DL, ScanFrom, DT);
|
|
323 }
|
|
324
|
|
325 /// DefMaxInstsToScan - the default number of maximum instructions
|
95
|
326 /// to scan in the block, used by FindAvailableLoadedValue().
|
|
327 /// FindAvailableLoadedValue() was introduced in r60148, to improve jump
|
|
328 /// threading in part by eliminating partially redundant loads.
|
|
329 /// At that point, the value of MaxInstsToScan was already set to '6'
|
|
330 /// without documented explanation.
|
|
331 cl::opt<unsigned>
|
|
332 llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
|
|
333 cl::desc("Use this to specify the default maximum number of instructions "
|
|
334 "to scan backward from a given instruction, when searching for "
|
|
335 "available loaded value"));
|
|
336
|
120
|
337 Value *llvm::FindAvailableLoadedValue(LoadInst *Load,
|
|
338 BasicBlock *ScanBB,
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
339 BasicBlock::iterator &ScanFrom,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
340 unsigned MaxInstsToScan,
|
121
|
341 AliasAnalysis *AA, bool *IsLoad,
|
|
342 unsigned *NumScanedInst) {
|
|
343 // Don't CSE load that is volatile or anything stronger than unordered.
|
|
344 if (!Load->isUnordered())
|
|
345 return nullptr;
|
|
346
|
|
347 return FindAvailablePtrLoadStore(
|
|
348 Load->getPointerOperand(), Load->getType(), Load->isAtomic(), ScanBB,
|
|
349 ScanFrom, MaxInstsToScan, AA, IsLoad, NumScanedInst);
|
|
350 }
|
|
351
|
|
352 Value *llvm::FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy,
|
|
353 bool AtLeastAtomic, BasicBlock *ScanBB,
|
|
354 BasicBlock::iterator &ScanFrom,
|
|
355 unsigned MaxInstsToScan,
|
|
356 AliasAnalysis *AA, bool *IsLoadCSE,
|
|
357 unsigned *NumScanedInst) {
|
83
|
358 if (MaxInstsToScan == 0)
|
|
359 MaxInstsToScan = ~0U;
|
|
360
|
95
|
361 const DataLayout &DL = ScanBB->getModule()->getDataLayout();
|
83
|
362
|
|
363 // Try to get the store size for the type.
|
147
|
364 auto AccessSize = LocationSize::precise(DL.getTypeStoreSize(AccessTy));
|
83
|
365
|
|
366 Value *StrippedPtr = Ptr->stripPointerCasts();
|
|
367
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
368 while (ScanFrom != ScanBB->begin()) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
369 // We must ignore debug info directives when counting (otherwise they
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
370 // would affect codegen).
|
95
|
371 Instruction *Inst = &*--ScanFrom;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
372 if (isa<DbgInfoIntrinsic>(Inst))
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
373 continue;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
374
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
375 // Restore ScanFrom to expected value in case next test succeeds
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
376 ScanFrom++;
|
83
|
377
|
121
|
378 if (NumScanedInst)
|
|
379 ++(*NumScanedInst);
|
|
380
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
381 // Don't scan huge blocks.
|
83
|
382 if (MaxInstsToScan-- == 0)
|
|
383 return nullptr;
|
|
384
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
385 --ScanFrom;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
386 // If this is a load of Ptr, the loaded value is available.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
387 // (This is true even if the load is volatile or atomic, although
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
388 // those cases are unlikely.)
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
389 if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
|
83
|
390 if (AreEquivalentAddressValues(
|
|
391 LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) &&
|
|
392 CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
|
120
|
393
|
|
394 // We can value forward from an atomic to a non-atomic, but not the
|
|
395 // other way around.
|
121
|
396 if (LI->isAtomic() < AtLeastAtomic)
|
120
|
397 return nullptr;
|
|
398
|
|
399 if (IsLoadCSE)
|
|
400 *IsLoadCSE = true;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
401 return LI;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
402 }
|
83
|
403
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
404 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
83
|
405 Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
406 // If this is a store through Ptr, the value is available!
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
407 // (This is true even if the store is volatile or atomic, although
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
408 // those cases are unlikely.)
|
83
|
409 if (AreEquivalentAddressValues(StorePtr, StrippedPtr) &&
|
|
410 CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(),
|
|
411 AccessTy, DL)) {
|
120
|
412
|
|
413 // We can value forward from an atomic to a non-atomic, but not the
|
|
414 // other way around.
|
121
|
415 if (SI->isAtomic() < AtLeastAtomic)
|
120
|
416 return nullptr;
|
|
417
|
|
418 if (IsLoadCSE)
|
|
419 *IsLoadCSE = false;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
420 return SI->getOperand(0);
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
421 }
|
83
|
422
|
|
423 // If both StrippedPtr and StorePtr reach all the way to an alloca or
|
|
424 // global and they are different, ignore the store. This is a trivial form
|
|
425 // of alias analysis that is important for reg2mem'd code.
|
|
426 if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
|
|
427 (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
|
|
428 StrippedPtr != StorePtr)
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
429 continue;
|
83
|
430
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
431 // If we have alias analysis and it says the store won't modify the loaded
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
432 // value, ignore the store.
|
134
|
433 if (AA && !isModSet(AA->getModRefInfo(SI, StrippedPtr, AccessSize)))
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
434 continue;
|
83
|
435
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
436 // Otherwise the store that may or may not alias the pointer, bail out.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
437 ++ScanFrom;
|
77
|
438 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
439 }
|
83
|
440
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
441 // If this is some other instruction that may clobber Ptr, bail out.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
442 if (Inst->mayWriteToMemory()) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
443 // If alias analysis claims that it really won't modify the load,
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
444 // ignore it.
|
134
|
445 if (AA && !isModSet(AA->getModRefInfo(Inst, StrippedPtr, AccessSize)))
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
446 continue;
|
83
|
447
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
448 // May modify the pointer, bail out.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
449 ++ScanFrom;
|
77
|
450 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
451 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
452 }
|
83
|
453
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
454 // Got to the start of the block, we didn't find it, but are done for this
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
455 // block.
|
77
|
456 return nullptr;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
457 }
|