llvm.org GIT mirror llvm / a070d2a
Change GetPointerBaseWithConstantOffset's DataLayout argument from a reference to a pointer, so that it can handle the case where DataLayout is not available and behave conservatively. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@174024 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 6 years ago
8 changed file(s) with 63 addition(s) and 64 deletion(s). Raw diff Collapse all Expand all
116116 /// it can be expressed as a base pointer plus a constant offset. Return the
117117 /// base and offset to the caller.
118118 Value *GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
119 const DataLayout &TD);
119 const DataLayout *TD);
120120 static inline const Value *
121121 GetPointerBaseWithConstantOffset(const Value *Ptr, int64_t &Offset,
122 const DataLayout &TD) {
122 const DataLayout *TD) {
123123 return GetPointerBaseWithConstantOffset(const_cast(Ptr), Offset,TD);
124124 }
125125
411411 }
412412
413413 // Check for buffer overflows and misalignment.
414 if (TD) {
415 // Only handles memory references that read/write something simple like an
416 // alloca instruction or a global variable.
417 int64_t Offset = 0;
418 if (Value *Base = GetPointerBaseWithConstantOffset(Ptr, Offset, *TD)) {
419 // OK, so the access is to a constant offset from Ptr. Check that Ptr is
420 // something we can handle and if so extract the size of this base object
421 // along with its alignment.
422 uint64_t BaseSize = AliasAnalysis::UnknownSize;
423 unsigned BaseAlign = 0;
424
425 if (AllocaInst *AI = dyn_cast(Base)) {
426 Type *ATy = AI->getAllocatedType();
427 if (!AI->isArrayAllocation() && ATy->isSized())
428 BaseSize = TD->getTypeAllocSize(ATy);
429 BaseAlign = AI->getAlignment();
430 if (BaseAlign == 0 && ATy->isSized())
431 BaseAlign = TD->getABITypeAlignment(ATy);
432 } else if (GlobalVariable *GV = dyn_cast(Base)) {
433 // If the global may be defined differently in another compilation unit
434 // then don't warn about funky memory accesses.
435 if (GV->hasDefinitiveInitializer()) {
436 Type *GTy = GV->getType()->getElementType();
437 if (GTy->isSized())
438 BaseSize = TD->getTypeAllocSize(GTy);
439 BaseAlign = GV->getAlignment();
440 if (BaseAlign == 0 && GTy->isSized())
441 BaseAlign = TD->getABITypeAlignment(GTy);
442 }
414 // Only handles memory references that read/write something simple like an
415 // alloca instruction or a global variable.
416 int64_t Offset = 0;
417 if (Value *Base = GetPointerBaseWithConstantOffset(Ptr, Offset, TD)) {
418 // OK, so the access is to a constant offset from Ptr. Check that Ptr is
419 // something we can handle and if so extract the size of this base object
420 // along with its alignment.
421 uint64_t BaseSize = AliasAnalysis::UnknownSize;
422 unsigned BaseAlign = 0;
423
424 if (AllocaInst *AI = dyn_cast(Base)) {
425 Type *ATy = AI->getAllocatedType();
426 if (TD && !AI->isArrayAllocation() && ATy->isSized())
427 BaseSize = TD->getTypeAllocSize(ATy);
428 BaseAlign = AI->getAlignment();
429 if (TD && BaseAlign == 0 && ATy->isSized())
430 BaseAlign = TD->getABITypeAlignment(ATy);
431 } else if (GlobalVariable *GV = dyn_cast(Base)) {
432 // If the global may be defined differently in another compilation unit
433 // then don't warn about funky memory accesses.
434 if (GV->hasDefinitiveInitializer()) {
435 Type *GTy = GV->getType()->getElementType();
436 if (TD && GTy->isSized())
437 BaseSize = TD->getTypeAllocSize(GTy);
438 BaseAlign = GV->getAlignment();
439 if (TD && BaseAlign == 0 && GTy->isSized())
440 BaseAlign = TD->getABITypeAlignment(GTy);
443441 }
444
445 // Accesses from before the start or after the end of the object are not
446 // defined.
447 Assert1(Size == AliasAnalysis::UnknownSize ||
448 BaseSize == AliasAnalysis::UnknownSize ||
449 (Offset >= 0 && Offset + Size <= BaseSize),
450 "Undefined behavior: Buffer overflow", &I);
451
452 // Accesses that say that the memory is more aligned than it is are not
453 // defined.
454 if (Align == 0 && Ty && Ty->isSized())
455 Align = TD->getABITypeAlignment(Ty);
456 Assert1(!BaseAlign || Align <= MinAlign(BaseAlign, Offset),
457 "Undefined behavior: Memory reference address is misaligned", &I);
458 }
442 }
443
444 // Accesses from before the start or after the end of the object are not
445 // defined.
446 Assert1(Size == AliasAnalysis::UnknownSize ||
447 BaseSize == AliasAnalysis::UnknownSize ||
448 (Offset >= 0 && Offset + Size <= BaseSize),
449 "Undefined behavior: Buffer overflow", &I);
450
451 // Accesses that say that the memory is more aligned than it is are not
452 // defined.
453 if (TD && Align == 0 && Ty && Ty->isSized())
454 Align = TD->getABITypeAlignment(Ty);
455 Assert1(!BaseAlign || Align <= MinAlign(BaseAlign, Offset),
456 "Undefined behavior: Memory reference address is misaligned", &I);
459457 }
460458 }
461459
5656 unsigned Align, const DataLayout *TD) {
5757 int64_t ByteOffset = 0;
5858 Value *Base = V;
59 if (TD)
60 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, *TD);
59 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, TD);
6160
6261 if (ByteOffset < 0) // out of bounds
6362 return false;
261261
262262 // If we haven't already computed the base/offset of MemLoc, do so now.
263263 if (MemLocBase == 0)
264 MemLocBase = GetPointerBaseWithConstantOffset(MemLoc.Ptr, MemLocOffs, *TD);
264 MemLocBase = GetPointerBaseWithConstantOffset(MemLoc.Ptr, MemLocOffs, TD);
265265
266266 unsigned Size = MemoryDependenceAnalysis::
267267 getLoadLoadClobberFullWidthSize(MemLocBase, MemLocOffs, MemLoc.Size,
286286 // Get the base of this load.
287287 int64_t LIOffs = 0;
288288 const Value *LIBase =
289 GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, TD);
289 GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, &TD);
290290
291291 // If the two pointers are not based on the same pointer, we can't tell that
292292 // they are related.
16701670 /// it can be expressed as a base pointer plus a constant offset. Return the
16711671 /// base and offset to the caller.
16721672 Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
1673 const DataLayout &TD) {
1674 unsigned BitWidth = TD.getPointerSizeInBits();
1673 const DataLayout *TD) {
1674 // Without DataLayout, conservatively assume 64-bit offsets, which is
1675 // the widest we support.
1676 unsigned BitWidth = TD ? TD->getPointerSizeInBits() : 64;
16751677 APInt ByteOffset(BitWidth, 0);
16761678 while (1) {
16771679 if (Ptr->getType()->isVectorTy())
16791681
16801682 if (GEPOperator *GEP = dyn_cast(Ptr)) {
16811683 APInt GEPOffset(BitWidth, 0);
1682 if (!GEP->accumulateConstantOffset(TD, GEPOffset))
1684 if (TD && !GEP->accumulateConstantOffset(*TD, GEPOffset))
16831685 break;
16841686 ByteOffset += GEPOffset;
16851687 Ptr = GEP->getPointerOperand();
375375 // Check to see if the later store is to the entire object (either a global,
376376 // an alloca, or a byval argument). If so, then it clearly overwrites any
377377 // other store to the same object.
378 const DataLayout &TD = *AA.getDataLayout();
379
380 const Value *UO1 = GetUnderlyingObject(P1, &TD),
381 *UO2 = GetUnderlyingObject(P2, &TD);
378 const DataLayout *TD = AA.getDataLayout();
379
380 const Value *UO1 = GetUnderlyingObject(P1, TD),
381 *UO2 = GetUnderlyingObject(P2, TD);
382382
383383 // If we can't resolve the same pointers to the same object, then we can't
384384 // analyze them at all.
848848 return -1;
849849
850850 int64_t StoreOffset = 0, LoadOffset = 0;
851 Value *StoreBase = GetPointerBaseWithConstantOffset(WritePtr, StoreOffset,TD);
852 Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, TD);
851 Value *StoreBase = GetPointerBaseWithConstantOffset(WritePtr,StoreOffset,&TD);
852 Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, &TD);
853853 if (StoreBase != LoadBase)
854854 return -1;
855855
944944 // then we should widen it!
945945 int64_t LoadOffs = 0;
946946 const Value *LoadBase =
947 GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, TD);
947 GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, &TD);
948948 unsigned LoadSize = TD.getTypeStoreSize(LoadTy);
949949
950950 unsigned Size = MemoryDependenceAnalysis::
191191 "2:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80"
192192 ":128:128-n8:16:32:64-S128");
193193 // Make sure we don't crash
194 GetPointerBaseWithConstantOffset(Gep0, Offset, TD);
195 GetPointerBaseWithConstantOffset(Gep1, Offset, TD);
196 GetPointerBaseWithConstantOffset(Gep2, Offset, TD);
197 GetPointerBaseWithConstantOffset(Gep3, Offset, TD);
194 GetPointerBaseWithConstantOffset(Gep0, Offset, &TD);
195 GetPointerBaseWithConstantOffset(Gep1, Offset, &TD);
196 GetPointerBaseWithConstantOffset(Gep2, Offset, &TD);
197 GetPointerBaseWithConstantOffset(Gep3, Offset, &TD);
198198
199199 // Gep of Geps
200200 GetElementPtrInst *GepII0 = GetElementPtrInst::Create(Gep0, C2xi32b);