-/// isDereferenceablePointer - Test if this value is always a pointer to
-/// allocated and suitably aligned memory for a simple load or store.
-static bool isDereferenceablePointer(const Value *V, const DataLayout *DL,
- SmallPtrSet<const Value *, 32> &Visited) {
- // Note that it is not safe to speculate into a malloc'd region because
- // malloc may return null.
-
- // These are obviously ok.
- if (isa<AllocaInst>(V)) return true;
-
- // It's not always safe to follow a bitcast, for example:
- // bitcast i8* (alloca i8) to i32*
- // would result in a 4-byte load from a 1-byte alloca. However,
- // if we're casting from a pointer from a type of larger size
- // to a type of smaller size (or the same size), and the alignment
- // is at least as large as for the resulting pointer type, then
- // we can look through the bitcast.
- if (DL)
- if (const BitCastInst* BC = dyn_cast<BitCastInst>(V)) {
- Type *STy = BC->getSrcTy()->getPointerElementType(),
- *DTy = BC->getDestTy()->getPointerElementType();
- if (STy->isSized() && DTy->isSized() &&
- (DL->getTypeStoreSize(STy) >=
- DL->getTypeStoreSize(DTy)) &&
- (DL->getABITypeAlignment(STy) >=
- DL->getABITypeAlignment(DTy)))
- return isDereferenceablePointer(BC->getOperand(0), DL, Visited);
- }
-
- // Global variables which can't collapse to null are ok.
- if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
- return !GV->hasExternalWeakLinkage();
-
- // byval arguments are okay. Arguments specifically marked as
- // dereferenceable are okay too.
- if (const Argument *A = dyn_cast<Argument>(V)) {
- if (A->hasByValAttr())
- return true;
- else if (uint64_t Bytes = A->getDereferenceableBytes()) {
- Type *Ty = V->getType()->getPointerElementType();
- if (Ty->isSized() && DL && DL->getTypeStoreSize(Ty) <= Bytes)
- return true;
- }
-
- return false;
- }
-
- // Return values from call sites specifically marked as dereferenceable are
- // also okay.
- if (ImmutableCallSite CS = V) {
- if (uint64_t Bytes = CS.getDereferenceableBytes(0)) {
- Type *Ty = V->getType()->getPointerElementType();
- if (Ty->isSized() && DL && DL->getTypeStoreSize(Ty) <= Bytes)
- return true;
- }
- }
-
- // For GEPs, determine if the indexing lands within the allocated object.
- if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
- // Conservatively require that the base pointer be fully dereferenceable.
- if (!Visited.insert(GEP->getOperand(0)))
- return false;
- if (!isDereferenceablePointer(GEP->getOperand(0), DL, Visited))
- return false;
- // Check the indices.
- gep_type_iterator GTI = gep_type_begin(GEP);
- for (User::const_op_iterator I = GEP->op_begin()+1,
- E = GEP->op_end(); I != E; ++I) {
- Value *Index = *I;
- Type *Ty = *GTI++;
- // Struct indices can't be out of bounds.
- if (isa<StructType>(Ty))
- continue;
- ConstantInt *CI = dyn_cast<ConstantInt>(Index);
- if (!CI)
- return false;
- // Zero is always ok.
- if (CI->isZero())
- continue;
- // Check to see that it's within the bounds of an array.
- ArrayType *ATy = dyn_cast<ArrayType>(Ty);
- if (!ATy)
- return false;
- if (CI->getValue().getActiveBits() > 64)
- return false;
- if (CI->getZExtValue() >= ATy->getNumElements())
- return false;
- }
- // Indices check out; this is dereferenceable.
- return true;
- }
-
- if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
- return isDereferenceablePointer(ASC->getOperand(0), DL, Visited);
-
- // If we don't know, assume the worst.
- return false;
-}
-
-/// isDereferenceablePointer - Test if this value is always a pointer to
-/// allocated and suitably aligned memory for a simple load or store.
-bool Value::isDereferenceablePointer(const DataLayout *DL) const {
- // When dereferenceability information is provided by a dereferenceable
- // attribute, we know exactly how many bytes are dereferenceable. If we can
- // determine the exact offset to the attributed variable, we can use that
- // information here.
- Type *Ty = getType()->getPointerElementType();
- if (Ty->isSized() && DL) {
- APInt Offset(DL->getTypeStoreSizeInBits(getType()), 0);
- const Value *BV = stripAndAccumulateInBoundsConstantOffsets(*DL, Offset);
-
- APInt DerefBytes(Offset.getBitWidth(), 0);
- if (const Argument *A = dyn_cast<Argument>(BV))
- DerefBytes = A->getDereferenceableBytes();
- else if (ImmutableCallSite CS = BV)
- DerefBytes = CS.getDereferenceableBytes(0);
-
- if (DerefBytes.getBoolValue() && Offset.isNonNegative()) {
- if (DerefBytes.uge(Offset + DL->getTypeStoreSize(Ty)))
- return true;
- }
- }
-
- SmallPtrSet<const Value *, 32> Visited;
- return ::isDereferenceablePointer(this, DL, Visited);
-}
-
-/// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
-/// return the value in the PHI node corresponding to PredBB. If not, return
-/// ourself. This is useful if you want to know the value something has in a
-/// predecessor block.