X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FIR%2FValue.cpp;h=dcf0ad50190f63c10dde574d7841c48e3be02de4;hb=838271c858c35f4aa0622c52b549074318e18f4e;hp=f6eb4273a53eace3b693d2d0a84d8daaf67f1401;hpb=e749325463058c3df21185715cc1287029c3124b;p=oota-llvm.git diff --git a/lib/IR/Value.cpp b/lib/IR/Value.cpp index f6eb4273a53..dcf0ad50190 100644 --- a/lib/IR/Value.cpp +++ b/lib/IR/Value.cpp @@ -46,8 +46,9 @@ static inline Type *checkType(Type *Ty) { } Value::Value(Type *ty, unsigned scid) - : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0), - SubclassOptionalData(0), SubclassData(0), NumOperands(0) { + : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), + HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), + NumOperands(0), IsUsedByMD(false), HasName(false) { // FIXME: Why isn't this in the subclass gunk?? // Note, we cannot call isa before the CallInst has been // constructed. @@ -157,16 +158,44 @@ static bool getSymTab(Value *V, ValueSymbolTable *&ST) { return false; } +ValueName *Value::getValueName() const { + if (!HasName) return nullptr; + + LLVMContext &Ctx = getContext(); + auto I = Ctx.pImpl->ValueNames.find(this); + assert(I != Ctx.pImpl->ValueNames.end() && + "No name entry found!"); + + return I->second; +} + +void Value::setValueName(ValueName *VN) { + LLVMContext &Ctx = getContext(); + + assert(HasName == Ctx.pImpl->ValueNames.count(this) && + "HasName bit out of sync!"); + + if (!VN) { + if (HasName) + Ctx.pImpl->ValueNames.erase(this); + HasName = false; + return; + } + + HasName = true; + Ctx.pImpl->ValueNames[this] = VN; +} + StringRef Value::getName() const { // Make sure the empty string is still a C string. For historical reasons, // some clients want to call .data() on the result and expect it to be null // terminated. - if (!getValueName()) + if (!hasName()) return StringRef("", 0); return getValueName()->getKey(); } -void Value::setName(const Twine &NewName) { +void Value::setNameImpl(const Twine &NewName) { // Fast path for common IRBuilder case of setName("") when there is no name. if (NewName.isTriviallyEmpty() && !hasName()) return; @@ -187,9 +216,6 @@ void Value::setName(const Twine &NewName) { if (getSymTab(this, ST)) return; // Cannot set a name on this value (e.g. constant). - if (Function *F = dyn_cast(this)) - getContext().pImpl->IntrinsicIDCache.erase(F); - if (!ST) { // No symbol table to update? Just do the change. if (NameRef.empty()) { // Free the name for this value. @@ -222,6 +248,12 @@ void Value::setName(const Twine &NewName) { setValueName(ST->createValueName(NameRef, this)); } +void Value::setName(const Twine &NewName) { + setNameImpl(NewName); + if (Function *F = dyn_cast(this)) + F->recalculateIntrinsicID(); +} + void Value::takeName(Value *V) { ValueSymbolTable *ST = nullptr; // If this value has a name, drop it. @@ -477,137 +509,6 @@ Value *Value::stripInBoundsOffsets() { return stripPointerCastsAndOffsets(this); } -/// \brief Check if Value is always a dereferenceable pointer. -/// -/// Test if V 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, - SmallPtrSetImpl &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(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 (const BitCastOperator *BC = dyn_cast(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(V)) - return !GV->hasExternalWeakLinkage(); - - // byval arguments are okay. Arguments specifically marked as - // dereferenceable are okay too. - if (const Argument *A = dyn_cast(V)) { - if (A->hasByValAttr()) - return true; - else if (uint64_t Bytes = A->getDereferenceableBytes()) { - Type *Ty = V->getType()->getPointerElementType(); - if (Ty->isSized() && DL.getTypeStoreSize(Ty) <= Bytes) - return true; - } - - return false; - } - - // Return values from call sites specifically marked as dereferenceable are - // also okay. - if (auto CS = ImmutableCallSite(V)) { - if (uint64_t Bytes = CS.getDereferenceableBytes(0)) { - Type *Ty = V->getType()->getPointerElementType(); - if (Ty->isSized() && DL.getTypeStoreSize(Ty) <= Bytes) - return true; - } - } - - // For GEPs, determine if the indexing lands within the allocated object. - if (const GEPOperator *GEP = dyn_cast(V)) { - // Conservatively require that the base pointer be fully dereferenceable. - if (!Visited.insert(GEP->getOperand(0)).second) - 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(Ty)) - continue; - ConstantInt *CI = dyn_cast(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(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; - } - - // For gc.relocate, look through relocations - if (const IntrinsicInst *I = dyn_cast(V)) - if (I->getIntrinsicID() == Intrinsic::experimental_gc_relocate) { - GCRelocateOperands RelocateInst(I); - return isDereferenceablePointer(RelocateInst.derivedPtr(), DL, Visited); - } - - if (const AddrSpaceCastInst *ASC = dyn_cast(V)) - return isDereferenceablePointer(ASC->getOperand(0), DL, Visited); - - // If we don't know, assume the worst. - return false; -} - -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()) { - APInt Offset(DL.getTypeStoreSizeInBits(getType()), 0); - const Value *BV = stripAndAccumulateInBoundsConstantOffsets(DL, Offset); - - APInt DerefBytes(Offset.getBitWidth(), 0); - if (const Argument *A = dyn_cast(BV)) - DerefBytes = A->getDereferenceableBytes(); - else if (auto CS = ImmutableCallSite(BV)) - DerefBytes = CS.getDereferenceableBytes(0); - - if (DerefBytes.getBoolValue() && Offset.isNonNegative()) { - if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty))) - return true; - } - } - - SmallPtrSet Visited; - return ::isDereferenceablePointer(this, DL, Visited); -} - Value *Value::DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) { PHINode *PN = dyn_cast(this);