Mercurial > hg > CbC > CbC_llvm
comparison lib/Analysis/Loads.cpp @ 147:c2174574ed3a
LLVM 10
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 14 Aug 2019 16:55:33 +0900 |
parents | 3a76565eade5 |
children |
comparison
equal
deleted
inserted
replaced
134:3a76565eade5 | 147:c2174574ed3a |
---|---|
1 //===- Loads.cpp - Local load analysis ------------------------------------===// | 1 //===- Loads.cpp - Local load analysis ------------------------------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 // | 4 // See https://llvm.org/LICENSE.txt for license information. |
5 // This file is distributed under the University of Illinois Open Source | 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 // License. See LICENSE.TXT for details. | |
7 // | 6 // |
8 //===----------------------------------------------------------------------===// | 7 //===----------------------------------------------------------------------===// |
9 // | 8 // |
10 // This file defines simple local analyses for load instructions. | 9 // This file defines simple local analyses for load instructions. |
11 // | 10 // |
105 | 104 |
106 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V)) | 105 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V)) |
107 return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, Size, | 106 return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, Size, |
108 DL, CtxI, DT, Visited); | 107 DL, CtxI, DT, Visited); |
109 | 108 |
110 if (auto CS = ImmutableCallSite(V)) | 109 if (const auto *Call = dyn_cast<CallBase>(V)) |
111 if (const Value *RV = CS.getReturnedArgOperand()) | 110 if (auto *RP = getArgumentAliasingToReturnedPointer(Call)) |
112 return isDereferenceableAndAlignedPointer(RV, Align, Size, DL, CtxI, DT, | 111 return isDereferenceableAndAlignedPointer(RP, Align, Size, DL, CtxI, DT, |
113 Visited); | 112 Visited); |
114 | 113 |
115 // If we don't know, assume the worst. | 114 // If we don't know, assume the worst. |
116 return false; | 115 return false; |
117 } | 116 } |
124 SmallPtrSet<const Value *, 32> Visited; | 123 SmallPtrSet<const Value *, 32> Visited; |
125 return ::isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT, | 124 return ::isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT, |
126 Visited); | 125 Visited); |
127 } | 126 } |
128 | 127 |
129 bool llvm::isDereferenceableAndAlignedPointer(const Value *V, unsigned Align, | 128 bool llvm::isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, |
129 unsigned Align, | |
130 const DataLayout &DL, | 130 const DataLayout &DL, |
131 const Instruction *CtxI, | 131 const Instruction *CtxI, |
132 const DominatorTree *DT) { | 132 const DominatorTree *DT) { |
133 // When dereferenceability information is provided by a dereferenceable | 133 // When dereferenceability information is provided by a dereferenceable |
134 // attribute, we know exactly how many bytes are dereferenceable. If we can | 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 | 135 // determine the exact offset to the attributed variable, we can use that |
136 // information here. | 136 // information here. |
137 Type *VTy = V->getType(); | |
138 Type *Ty = VTy->getPointerElementType(); | |
139 | 137 |
140 // Require ABI alignment for loads without alignment specification | 138 // Require ABI alignment for loads without alignment specification |
141 if (Align == 0) | 139 if (Align == 0) |
142 Align = DL.getABITypeAlignment(Ty); | 140 Align = DL.getABITypeAlignment(Ty); |
143 | 141 |
144 if (!Ty->isSized()) | 142 if (!Ty->isSized()) |
145 return false; | 143 return false; |
146 | 144 |
147 SmallPtrSet<const Value *, 32> Visited; | 145 SmallPtrSet<const Value *, 32> Visited; |
148 return ::isDereferenceableAndAlignedPointer( | 146 return ::isDereferenceableAndAlignedPointer( |
149 V, Align, APInt(DL.getIndexTypeSizeInBits(VTy), DL.getTypeStoreSize(Ty)), DL, | 147 V, Align, |
150 CtxI, DT, Visited); | 148 APInt(DL.getIndexTypeSizeInBits(V->getType()), DL.getTypeStoreSize(Ty)), |
151 } | 149 DL, CtxI, DT, Visited); |
152 | 150 } |
153 bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL, | 151 |
152 bool llvm::isDereferenceablePointer(const Value *V, Type *Ty, | |
153 const DataLayout &DL, | |
154 const Instruction *CtxI, | 154 const Instruction *CtxI, |
155 const DominatorTree *DT) { | 155 const DominatorTree *DT) { |
156 return isDereferenceableAndAlignedPointer(V, 1, DL, CtxI, DT); | 156 return isDereferenceableAndAlignedPointer(V, Ty, 1, DL, CtxI, DT); |
157 } | 157 } |
158 | 158 |
159 /// \brief Test if A and B will obviously have the same value. | 159 /// Test if A and B will obviously have the same value. |
160 /// | 160 /// |
161 /// This includes recognizing that %t0 and %t1 will have the same | 161 /// This includes recognizing that %t0 and %t1 will have the same |
162 /// value in code like this: | 162 /// value in code like this: |
163 /// \code | 163 /// \code |
164 /// %t0 = getelementptr \@a, 0, 3 | 164 /// %t0 = getelementptr \@a, 0, 3 |
185 | 185 |
186 // Otherwise they may not be equivalent. | 186 // Otherwise they may not be equivalent. |
187 return false; | 187 return false; |
188 } | 188 } |
189 | 189 |
190 /// \brief Check if executing a load of this pointer value cannot trap. | 190 /// Check if executing a load of this pointer value cannot trap. |
191 /// | 191 /// |
192 /// If DT and ScanFrom are specified this method performs context-sensitive | 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. | 193 /// analysis and returns true if it is safe to load immediately before ScanFrom. |
194 /// | 194 /// |
195 /// If it is not obviously safe to load from the specified pointer, we do | 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 | 196 /// a quick local scan of the basic block containing \c ScanFrom, to determine |
197 /// if the address is already accessed. | 197 /// if the address is already accessed. |
198 /// | 198 /// |
199 /// This uses the pointee type to determine how many bytes need to be safe to | 199 /// This uses the pointee type to determine how many bytes need to be safe to |
200 /// load from the pointer. | 200 /// load from the pointer. |
201 bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align, | 201 bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align, APInt &Size, |
202 const DataLayout &DL, | 202 const DataLayout &DL, |
203 Instruction *ScanFrom, | 203 Instruction *ScanFrom, |
204 const DominatorTree *DT) { | 204 const DominatorTree *DT) { |
205 // Zero alignment means that the load has the ABI alignment for the target | 205 // Zero alignment means that the load has the ABI alignment for the target |
206 if (Align == 0) | 206 if (Align == 0) |
207 Align = DL.getABITypeAlignment(V->getType()->getPointerElementType()); | 207 Align = DL.getABITypeAlignment(V->getType()->getPointerElementType()); |
208 assert(isPowerOf2_32(Align)); | 208 assert(isPowerOf2_32(Align)); |
209 | 209 |
210 // If DT is not specified we can't make context-sensitive query | 210 // If DT is not specified we can't make context-sensitive query |
211 const Instruction* CtxI = DT ? ScanFrom : nullptr; | 211 const Instruction* CtxI = DT ? ScanFrom : nullptr; |
212 if (isDereferenceableAndAlignedPointer(V, Align, DL, CtxI, DT)) | 212 if (isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT)) |
213 return true; | 213 return true; |
214 | 214 |
215 int64_t ByteOffset = 0; | 215 int64_t ByteOffset = 0; |
216 Value *Base = V; | 216 Value *Base = V; |
217 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL); | 217 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL); |
279 return false; | 279 return false; |
280 | 280 |
281 Value *AccessedPtr; | 281 Value *AccessedPtr; |
282 unsigned AccessedAlign; | 282 unsigned AccessedAlign; |
283 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { | 283 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
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; | |
284 AccessedPtr = LI->getPointerOperand(); | 289 AccessedPtr = LI->getPointerOperand(); |
285 AccessedAlign = LI->getAlignment(); | 290 AccessedAlign = LI->getAlignment(); |
286 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { | 291 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) { |
292 // Ignore volatile stores (see comment for loads). | |
293 if (SI->isVolatile()) | |
294 continue; | |
287 AccessedPtr = SI->getPointerOperand(); | 295 AccessedPtr = SI->getPointerOperand(); |
288 AccessedAlign = SI->getAlignment(); | 296 AccessedAlign = SI->getAlignment(); |
289 } else | 297 } else |
290 continue; | 298 continue; |
291 | 299 |
304 return true; | 312 return true; |
305 } | 313 } |
306 return false; | 314 return false; |
307 } | 315 } |
308 | 316 |
309 /// DefMaxInstsToScan - the default number of maximum instructions | 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 | |
310 /// to scan in the block, used by FindAvailableLoadedValue(). | 326 /// to scan in the block, used by FindAvailableLoadedValue(). |
311 /// FindAvailableLoadedValue() was introduced in r60148, to improve jump | 327 /// FindAvailableLoadedValue() was introduced in r60148, to improve jump |
312 /// threading in part by eliminating partially redundant loads. | 328 /// threading in part by eliminating partially redundant loads. |
313 /// At that point, the value of MaxInstsToScan was already set to '6' | 329 /// At that point, the value of MaxInstsToScan was already set to '6' |
314 /// without documented explanation. | 330 /// without documented explanation. |
343 MaxInstsToScan = ~0U; | 359 MaxInstsToScan = ~0U; |
344 | 360 |
345 const DataLayout &DL = ScanBB->getModule()->getDataLayout(); | 361 const DataLayout &DL = ScanBB->getModule()->getDataLayout(); |
346 | 362 |
347 // Try to get the store size for the type. | 363 // Try to get the store size for the type. |
348 uint64_t AccessSize = DL.getTypeStoreSize(AccessTy); | 364 auto AccessSize = LocationSize::precise(DL.getTypeStoreSize(AccessTy)); |
349 | 365 |
350 Value *StrippedPtr = Ptr->stripPointerCasts(); | 366 Value *StrippedPtr = Ptr->stripPointerCasts(); |
351 | 367 |
352 while (ScanFrom != ScanBB->begin()) { | 368 while (ScanFrom != ScanBB->begin()) { |
353 // We must ignore debug info directives when counting (otherwise they | 369 // We must ignore debug info directives when counting (otherwise they |