|
21 | 21 | #include "llvm/IR/LLVMContext.h"
|
22 | 22 | #include "llvm/IR/Module.h"
|
23 | 23 | #include "llvm/IR/Operator.h"
|
| 24 | +#include "llvm/IR/Statepoint.h" |
| 25 | + |
24 | 26 | using namespace llvm;
|
25 | 27 |
|
| 28 | +static bool isDereferenceableFromAttribute(const Value *BV, APInt Offset, |
| 29 | + Type *Ty, const DataLayout &DL, |
| 30 | + const Instruction *CtxI, |
| 31 | + const DominatorTree *DT, |
| 32 | + const TargetLibraryInfo *TLI) { |
| 33 | + assert(Offset.isNonNegative() && "offset can't be negative"); |
| 34 | + assert(Ty->isSized() && "must be sized"); |
| 35 | + |
| 36 | + APInt DerefBytes(Offset.getBitWidth(), 0); |
| 37 | + bool CheckForNonNull = false; |
| 38 | + if (const Argument *A = dyn_cast<Argument>(BV)) { |
| 39 | + DerefBytes = A->getDereferenceableBytes(); |
| 40 | + if (!DerefBytes.getBoolValue()) { |
| 41 | + DerefBytes = A->getDereferenceableOrNullBytes(); |
| 42 | + CheckForNonNull = true; |
| 43 | + } |
| 44 | + } else if (auto CS = ImmutableCallSite(BV)) { |
| 45 | + DerefBytes = CS.getDereferenceableBytes(0); |
| 46 | + if (!DerefBytes.getBoolValue()) { |
| 47 | + DerefBytes = CS.getDereferenceableOrNullBytes(0); |
| 48 | + CheckForNonNull = true; |
| 49 | + } |
| 50 | + } else if (const LoadInst *LI = dyn_cast<LoadInst>(BV)) { |
| 51 | + if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { |
| 52 | + ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); |
| 53 | + DerefBytes = CI->getLimitedValue(); |
| 54 | + } |
| 55 | + if (!DerefBytes.getBoolValue()) { |
| 56 | + if (MDNode *MD = |
| 57 | + LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { |
| 58 | + ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); |
| 59 | + DerefBytes = CI->getLimitedValue(); |
| 60 | + } |
| 61 | + CheckForNonNull = true; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (DerefBytes.getBoolValue()) |
| 66 | + if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty))) |
| 67 | + if (!CheckForNonNull || isKnownNonNullAt(BV, CtxI, DT, TLI)) |
| 68 | + return true; |
| 69 | + |
| 70 | + return false; |
| 71 | +} |
| 72 | + |
| 73 | +static bool isDereferenceableFromAttribute(const Value *V, const DataLayout &DL, |
| 74 | + const Instruction *CtxI, |
| 75 | + const DominatorTree *DT, |
| 76 | + const TargetLibraryInfo *TLI) { |
| 77 | + Type *VTy = V->getType(); |
| 78 | + Type *Ty = VTy->getPointerElementType(); |
| 79 | + if (!Ty->isSized()) |
| 80 | + return false; |
| 81 | + |
| 82 | + APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0); |
| 83 | + return isDereferenceableFromAttribute(V, Offset, Ty, DL, CtxI, DT, TLI); |
| 84 | +} |
| 85 | + |
| 86 | +static bool isAligned(const Value *Base, APInt Offset, unsigned Align, |
| 87 | + const DataLayout &DL) { |
| 88 | + APInt BaseAlign(Offset.getBitWidth(), Base->getPointerAlignment(DL)); |
| 89 | + |
| 90 | + if (!BaseAlign) { |
| 91 | + Type *Ty = Base->getType()->getPointerElementType(); |
| 92 | + if (!Ty->isSized()) |
| 93 | + return false; |
| 94 | + BaseAlign = DL.getABITypeAlignment(Ty); |
| 95 | + } |
| 96 | + |
| 97 | + APInt Alignment(Offset.getBitWidth(), Align); |
| 98 | + |
| 99 | + assert(Alignment.isPowerOf2() && "must be a power of 2!"); |
| 100 | + return BaseAlign.uge(Alignment) && !(Offset & (Alignment-1)); |
| 101 | +} |
| 102 | + |
| 103 | +static bool isAligned(const Value *Base, unsigned Align, const DataLayout &DL) { |
| 104 | + Type *Ty = Base->getType(); |
| 105 | + assert(Ty->isSized() && "must be sized"); |
| 106 | + APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0); |
| 107 | + return isAligned(Base, Offset, Align, DL); |
| 108 | +} |
| 109 | + |
| 110 | +/// Test if V is always a pointer to allocated and suitably aligned memory for |
| 111 | +/// a simple load or store. |
| 112 | +static bool isDereferenceableAndAlignedPointer( |
| 113 | + const Value *V, unsigned Align, const DataLayout &DL, |
| 114 | + const Instruction *CtxI, const DominatorTree *DT, |
| 115 | + const TargetLibraryInfo *TLI, SmallPtrSetImpl<const Value *> &Visited) { |
| 116 | + // Note that it is not safe to speculate into a malloc'd region because |
| 117 | + // malloc may return null. |
| 118 | + |
| 119 | + // These are obviously ok if aligned. |
| 120 | + if (isa<AllocaInst>(V)) |
| 121 | + return isAligned(V, Align, DL); |
| 122 | + |
| 123 | + // It's not always safe to follow a bitcast, for example: |
| 124 | + // bitcast i8* (alloca i8) to i32* |
| 125 | + // would result in a 4-byte load from a 1-byte alloca. However, |
| 126 | + // if we're casting from a pointer from a type of larger size |
| 127 | + // to a type of smaller size (or the same size), and the alignment |
| 128 | + // is at least as large as for the resulting pointer type, then |
| 129 | + // we can look through the bitcast. |
| 130 | + if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) { |
| 131 | + Type *STy = BC->getSrcTy()->getPointerElementType(), |
| 132 | + *DTy = BC->getDestTy()->getPointerElementType(); |
| 133 | + if (STy->isSized() && DTy->isSized() && |
| 134 | + (DL.getTypeStoreSize(STy) >= DL.getTypeStoreSize(DTy)) && |
| 135 | + (DL.getABITypeAlignment(STy) >= DL.getABITypeAlignment(DTy))) |
| 136 | + return isDereferenceableAndAlignedPointer(BC->getOperand(0), Align, DL, |
| 137 | + CtxI, DT, TLI, Visited); |
| 138 | + } |
| 139 | + |
| 140 | + // Global variables which can't collapse to null are ok. |
| 141 | + if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 142 | + if (!GV->hasExternalWeakLinkage()) |
| 143 | + return isAligned(V, Align, DL); |
| 144 | + |
| 145 | + // byval arguments are okay. |
| 146 | + if (const Argument *A = dyn_cast<Argument>(V)) |
| 147 | + if (A->hasByValAttr()) |
| 148 | + return isAligned(V, Align, DL); |
| 149 | + |
| 150 | + if (isDereferenceableFromAttribute(V, DL, CtxI, DT, TLI)) |
| 151 | + return isAligned(V, Align, DL); |
| 152 | + |
| 153 | + // For GEPs, determine if the indexing lands within the allocated object. |
| 154 | + if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
| 155 | + Type *Ty = GEP->getResultElementType(); |
| 156 | + const Value *Base = GEP->getPointerOperand(); |
| 157 | + |
| 158 | + // Conservatively require that the base pointer be fully dereferenceable |
| 159 | + // and aligned. |
| 160 | + if (!Visited.insert(Base).second) |
| 161 | + return false; |
| 162 | + if (!isDereferenceableAndAlignedPointer(Base, Align, DL, CtxI, DT, TLI, |
| 163 | + Visited)) |
| 164 | + return false; |
| 165 | + |
| 166 | + APInt Offset(DL.getPointerTypeSizeInBits(GEP->getType()), 0); |
| 167 | + if (!GEP->accumulateConstantOffset(DL, Offset)) |
| 168 | + return false; |
| 169 | + |
| 170 | + // Check if the load is within the bounds of the underlying object |
| 171 | + // and offset is aligned. |
| 172 | + uint64_t LoadSize = DL.getTypeStoreSize(Ty); |
| 173 | + Type *BaseType = GEP->getSourceElementType(); |
| 174 | + assert(isPowerOf2_32(Align) && "must be a power of 2!"); |
| 175 | + return (Offset + LoadSize).ule(DL.getTypeAllocSize(BaseType)) && |
| 176 | + !(Offset & APInt(Offset.getBitWidth(), Align-1)); |
| 177 | + } |
| 178 | + |
| 179 | + // For gc.relocate, look through relocations |
| 180 | + if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V)) |
| 181 | + return isDereferenceableAndAlignedPointer( |
| 182 | + RelocateInst->getDerivedPtr(), Align, DL, CtxI, DT, TLI, Visited); |
| 183 | + |
| 184 | + if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V)) |
| 185 | + return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, DL, |
| 186 | + CtxI, DT, TLI, Visited); |
| 187 | + |
| 188 | + // If we don't know, assume the worst. |
| 189 | + return false; |
| 190 | +} |
| 191 | + |
| 192 | +bool llvm::isDereferenceableAndAlignedPointer(const Value *V, unsigned Align, |
| 193 | + const DataLayout &DL, |
| 194 | + const Instruction *CtxI, |
| 195 | + const DominatorTree *DT, |
| 196 | + const TargetLibraryInfo *TLI) { |
| 197 | + // When dereferenceability information is provided by a dereferenceable |
| 198 | + // attribute, we know exactly how many bytes are dereferenceable. If we can |
| 199 | + // determine the exact offset to the attributed variable, we can use that |
| 200 | + // information here. |
| 201 | + Type *VTy = V->getType(); |
| 202 | + Type *Ty = VTy->getPointerElementType(); |
| 203 | + |
| 204 | + // Require ABI alignment for loads without alignment specification |
| 205 | + if (Align == 0) |
| 206 | + Align = DL.getABITypeAlignment(Ty); |
| 207 | + |
| 208 | + if (Ty->isSized()) { |
| 209 | + APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0); |
| 210 | + const Value *BV = V->stripAndAccumulateInBoundsConstantOffsets(DL, Offset); |
| 211 | + |
| 212 | + if (Offset.isNonNegative()) |
| 213 | + if (isDereferenceableFromAttribute(BV, Offset, Ty, DL, CtxI, DT, TLI) && |
| 214 | + isAligned(BV, Offset, Align, DL)) |
| 215 | + return true; |
| 216 | + } |
| 217 | + |
| 218 | + SmallPtrSet<const Value *, 32> Visited; |
| 219 | + return ::isDereferenceableAndAlignedPointer(V, Align, DL, CtxI, DT, TLI, |
| 220 | + Visited); |
| 221 | +} |
| 222 | + |
| 223 | +bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL, |
| 224 | + const Instruction *CtxI, |
| 225 | + const DominatorTree *DT, |
| 226 | + const TargetLibraryInfo *TLI) { |
| 227 | + return isDereferenceableAndAlignedPointer(V, 1, DL, CtxI, DT, TLI); |
| 228 | +} |
| 229 | + |
26 | 230 | /// \brief Test if A and B will obviously have the same value.
|
27 | 231 | ///
|
28 | 232 | /// This includes recognizing that %t0 and %t1 will have the same
|
|
0 commit comments