#include "llvm/DerivedTypes.h"
#include "llvm/InstrTypes.h"
#include "llvm/Instructions.h"
+#include "llvm/Operator.h"
#include "llvm/Module.h"
+#include "llvm/Metadata.h"
#include "llvm/ValueSymbolTable.h"
+#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Debug.h"
+#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/LeakDetector.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/ValueHandle.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/System/RWMutex.h"
#include "llvm/System/Threading.h"
#include "llvm/ADT/DenseMap.h"
}
Value::Value(const Type *ty, unsigned scid)
- : SubclassID(scid), HasValueHandle(0), SubclassData(0), VTy(checkType(ty)),
+ : SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0),
+ SubclassData(0), VTy(checkType(ty)),
UseList(0), Name(0) {
if (isa<CallInst>(this) || isa<InvokeInst>(this))
assert((VTy->isFirstClassType() || VTy == Type::VoidTy ||
// a <badref>
//
if (!use_empty()) {
- cerr << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
+ errs() << "While deleting: " << *VTy << " %" << getNameStr() << "\n";
for (use_iterator I = use_begin(), E = use_end(); I != E; ++I)
- cerr << "Use still stuck around after Def is destroyed:"
+ errs() << "Use still stuck around after Def is destroyed:"
<< **I << "\n";
}
#endif
} else if (Argument *A = dyn_cast<Argument>(V)) {
if (Function *P = A->getParent())
ST = &P->getValueSymbolTable();
- } else {
+ } else if (NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
+ if (Module *P = N->getParent()) {
+ ST = &P->getValueSymbolTable();
+ }
+ } else if (isa<MDString>(V))
+ return true;
+ else {
assert(isa<Constant>(V) && "Unknown value type!");
return true; // no name is setable for this.
}
return false;
}
-/// getNameStart - Return a pointer to a null terminated string for this name.
-/// Note that names can have null characters within the string as well as at
-/// their end. This always returns a non-null pointer.
-const char *Value::getNameStart() const {
- if (Name == 0) return "";
- return Name->getKeyData();
-}
-
-/// getNameLen - Return the length of the string, correctly handling nul
-/// characters embedded into them.
-unsigned Value::getNameLen() const {
- return Name ? Name->getKeyLength() : 0;
-}
-
-/// isName - Return true if this value has the name specified by the provided
-/// nul terminated string.
-bool Value::isName(const char *N) const {
- unsigned InLen = strlen(N);
- return InLen == getNameLen() && memcmp(getNameStart(), N, InLen) == 0;
+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 (!Name) return StringRef("", 0);
+ return Name->getKey();
}
-
std::string Value::getNameStr() const {
- if (Name == 0) return "";
- return std::string(Name->getKeyData(),
- Name->getKeyData()+Name->getKeyLength());
+ return getName().str();
}
-void Value::setName(const std::string &name) {
- setName(&name[0], name.size());
-}
+void Value::setName(const Twine &NewName) {
+ SmallString<32> NameData;
+ NewName.toVector(NameData);
-void Value::setName(const char *Name) {
- setName(Name, Name ? strlen(Name) : 0);
-}
+ const char *NameStr = NameData.data();
+ unsigned NameLen = NameData.size();
+
+ // Name isn't changing?
+ if (getName() == StringRef(NameStr, NameLen))
+ return;
-void Value::setName(const char *NameStr, unsigned NameLen) {
- if (NameLen == 0 && !hasName()) return;
assert(getType() != Type::VoidTy && "Cannot assign a name to void values!");
// Get the symbol table to update for this object.
return;
}
- if (Name) {
- // Name isn't changing?
- if (NameLen == Name->getKeyLength() &&
- !memcmp(Name->getKeyData(), NameStr, NameLen))
- return;
+ if (Name)
Name->Destroy();
- }
// NOTE: Could optimize for the case the name is shrinking to not deallocate
// then reallocated.
// NOTE: Could optimize for the case the name is shrinking to not deallocate
// then reallocated.
if (hasName()) {
- // Name isn't changing?
- if (NameLen == Name->getKeyLength() &&
- !memcmp(Name->getKeyData(), NameStr, NameLen))
- return;
-
// Remove old name.
ST->removeValueName(Name);
Name->Destroy();
}
// Name is changing to something new.
- Name = ST->createValueName(NameStr, NameLen, this);
+ Name = ST->createValueName(StringRef(NameStr, NameLen), this);
}
if (getSymTab(this, ST)) {
// We can't set a name on this value, but we need to clear V's name if
// it has one.
- if (V->hasName()) V->setName(0, 0);
+ if (V->hasName()) V->setName("");
return; // Cannot set a name on this value (e.g. constant).
}
if (!ST) {
if (getSymTab(this, ST)) {
// Clear V's name.
- V->setName(0, 0);
+ V->setName("");
return; // Cannot set a name on this value (e.g. constant).
}
}
return this;
Value *V = this;
do {
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
- if (CE->getOpcode() == Instruction::GetElementPtr) {
- for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
- if (!CE->getOperand(i)->isNullValue())
- return V;
- V = CE->getOperand(0);
- } else if (CE->getOpcode() == Instruction::BitCast) {
- V = CE->getOperand(0);
- } else {
- return V;
- }
- } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
+ if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
if (!GEP->hasAllZeroIndices())
return V;
- V = GEP->getOperand(0);
- } else if (BitCastInst *CI = dyn_cast<BitCastInst>(V)) {
- V = CI->getOperand(0);
+ V = GEP->getPointerOperand();
+ } else if (Operator::getOpcode(V) == Instruction::BitCast) {
+ V = cast<Operator>(V)->getOperand(0);
} else {
return V;
}
Value *V = this;
unsigned MaxLookup = 6;
do {
- if (Instruction *I = dyn_cast<Instruction>(V)) {
- if (!isa<BitCastInst>(I) && !isa<GetElementPtrInst>(I))
- return V;
- V = I->getOperand(0);
- } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
- if (CE->getOpcode() != Instruction::BitCast &&
- CE->getOpcode() != Instruction::GetElementPtr)
- return V;
- V = CE->getOperand(0);
+ if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
+ V = GEP->getPointerOperand();
+ } else if (Operator::getOpcode(V) == Instruction::BitCast) {
+ V = cast<Operator>(V)->getOperand(0);
} else {
return V;
}
return this;
}
+LLVMContext &Value::getContext() const { return VTy->getContext(); }
+
//===----------------------------------------------------------------------===//
// ValueHandleBase Class
//===----------------------------------------------------------------------===//
if (VP->HasValueHandle) {
// If this value already has a ValueHandle, then it must be in the
// ValueHandles map already.
- sys::SmartScopedReader<true> Reader(&*ValueHandlesLock);
+ sys::SmartScopedReader<true> Reader(*ValueHandlesLock);
ValueHandleBase *&Entry = (*ValueHandles)[VP];
assert(Entry != 0 && "Value doesn't have any handles?");
AddToExistingUseList(&Entry);
// reallocate itself, which would invalidate all of the PrevP pointers that
// point into the old table. Handle this by checking for reallocation and
// updating the stale pointers only if needed.
- sys::SmartScopedWriter<true> Writer(&*ValueHandlesLock);
+ sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
ValueHandlesTy &Handles = *ValueHandles;
const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
// If the Next pointer was null, then it is possible that this was the last
// ValueHandle watching VP. If so, delete its entry from the ValueHandles
// map.
- sys::SmartScopedWriter<true> Writer(&*ValueHandlesLock);
+ sys::SmartScopedWriter<true> Writer(*ValueHandlesLock);
ValueHandlesTy &Handles = *ValueHandles;
if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
Handles.erase(VP);
switch (ThisNode->getKind()) {
case Assert:
#ifndef NDEBUG // Only in -g mode...
- cerr << "While deleting: " << *V->getType() << " %" << V->getNameStr()
- << "\n";
+ errs() << "While deleting: " << *V->getType() << " %" << V->getNameStr()
+ << "\n";
#endif
- cerr << "An asserting value handle still pointed to this value!\n";
- abort();
+ llvm_unreachable("An asserting value handle still pointed to this"
+ " value!");
case Weak:
// Weak just goes to null, which will unlink it from the list.
ThisNode->operator=(0);
setOperand(i, To); // Fix it now...
}
}
-