diff options
Diffstat (limited to 'lib/Analysis')
-rw-r--r-- | lib/Analysis/DbgInfoPrinter.cpp | 48 | ||||
-rw-r--r-- | lib/Analysis/DebugInfo.cpp | 75 | ||||
-rw-r--r-- | lib/Analysis/ValueTracking.cpp | 120 |
3 files changed, 101 insertions, 142 deletions
diff --git a/lib/Analysis/DbgInfoPrinter.cpp b/lib/Analysis/DbgInfoPrinter.cpp index 6346a90a68..e43bc81ffa 100644 --- a/lib/Analysis/DbgInfoPrinter.cpp +++ b/lib/Analysis/DbgInfoPrinter.cpp @@ -33,29 +33,31 @@ static cl::opt<bool> PrintDirectory("print-fullpath", cl::desc("Print fullpath when printing debug info"), cl::Hidden); namespace { - struct VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass { + struct VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass { private: raw_ostream &Out; void printStopPoint(const DbgStopPointInst *DSI); void printFuncStart(const DbgFuncStartInst *FS); void printVariableDeclaration(const Value *V); - public: - static char ID; // Pass identification - PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {} + public: + static char ID; // Pass identification + PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {} - virtual bool runOnFunction(Function &F); - virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.setPreservesAll(); - } - }; - char PrintDbgInfo::ID = 0; - static RegisterPass<PrintDbgInfo> X("print-dbginfo", - "Print debug info in human readable form"); + virtual bool runOnFunction(Function &F); + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); + } + + }; + char PrintDbgInfo::ID = 0; + static RegisterPass<PrintDbgInfo> X("print-dbginfo", + "Print debug info in human readable form"); } FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); } -void PrintDbgInfo::printVariableDeclaration(const Value *V) { +void PrintDbgInfo::printVariableDeclaration(const Value *V) +{ std::string DisplayName, File, Directory, Type; unsigned LineNo; if (getLocationInfo(V, DisplayName, Type, LineNo, File, Directory)) { @@ -73,22 +75,24 @@ void PrintDbgInfo::printVariableDeclaration(const Value *V) { void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI) { if (PrintDirectory) { - const char *Dir = GetConstantStringInfo(DSI->getDirectory()); - Out << (Dir ? Dir : "") << "/"; + std::string dir; + GetConstantStringInfo(DSI->getDirectory(), dir); + Out << dir << "/"; } - - const char *FN = GetConstantStringInfo(DSI->getFileName()); - Out << (FN ? FN : "") << ":" << DSI->getLine(); - - if (unsigned Col = DSI->getColumn()) + std::string file; + GetConstantStringInfo(DSI->getFileName(), file); + Out << file << ":" << DSI->getLine(); + if (unsigned Col = DSI->getColumn()) { Out << ":" << Col; + } } void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS) { DISubprogram Subprogram(cast<GlobalVariable>(FS->getSubprogram())); - Out << ";fully qualified function name: " << Subprogram.getDisplayName() - << " return type: " << Subprogram.getType().getName() + std::string Res1, Res2; + Out << ";fully qualified function name: " << Subprogram.getDisplayName(Res1) + << " return type: " << Subprogram.getType().getName(Res2) << " at line " << Subprogram.getLineNumber() << "\n\n"; } diff --git a/lib/Analysis/DebugInfo.cpp b/lib/Analysis/DebugInfo.cpp index 901455700e..d9f0aa53e9 100644 --- a/lib/Analysis/DebugInfo.cpp +++ b/lib/Analysis/DebugInfo.cpp @@ -35,16 +35,24 @@ DIDescriptor::DIDescriptor(GlobalVariable *gv, unsigned RequiredTag) { GV = 0; } -const char *DIDescriptor::getStringField(unsigned Elt) const { - if (GV == 0) - return 0; +const std::string & +DIDescriptor::getStringField(unsigned Elt, std::string &Result) const { + if (GV == 0) { + Result.clear(); + return Result; + } Constant *C = GV->getInitializer(); - if (C == 0 || Elt >= C->getNumOperands()) - return 0; + if (C == 0 || Elt >= C->getNumOperands()) { + Result.clear(); + return Result; + } // Fills in the string if it succeeds - return GetConstantStringInfo(C->getOperand(Elt)); + if (!GetConstantStringInfo(C->getOperand(Elt), Result)) + Result.clear(); + + return Result; } uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const { @@ -182,9 +190,11 @@ unsigned DIArray::getNumElements() const { bool DICompileUnit::Verify() const { if (isNull()) return false; - + std::string Res; + if (getFilename(Res).empty()) + return false; // It is possible that directory and produce string is empty. - return getFilename(); + return true; } /// Verify - Verify that a type descriptor is well formed. @@ -495,7 +505,7 @@ DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){ /// CreateBasicType - Create a basic type like int, float, etc. DIBasicType DIFactory::CreateBasicType(DIDescriptor Context, - const std::string &Name, + const std::string &Name, DICompileUnit CompileUnit, unsigned LineNumber, uint64_t SizeInBits, @@ -884,7 +894,8 @@ namespace llvm { } bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type, - unsigned &LineNo, std::string &File, std::string &Dir) { + unsigned &LineNo, std::string &File, std::string &Dir) + { DICompileUnit Unit; DIType TypeD; if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) { @@ -892,11 +903,7 @@ namespace llvm { if (!DIGV) return false; DIGlobalVariable Var(cast<GlobalVariable>(DIGV)); - const char *DN = Var.getDisplayName(); - if (DN) - DisplayName = DN; - else - DisplayName.clear(); + Var.getDisplayName(DisplayName); LineNo = Var.getLineNumber(); Unit = Var.getCompileUnit(); TypeD = Var.getType(); @@ -905,24 +912,14 @@ namespace llvm { if (!DDI) return false; DIVariable Var(cast<GlobalVariable>(DDI->getVariable())); - const char *DN = Var.getName(); - if (DN) - DisplayName = DN; - else - DisplayName.clear(); + Var.getName(DisplayName); LineNo = Var.getLineNumber(); Unit = Var.getCompileUnit(); TypeD = Var.getType(); } - Type.clear(); - File.clear(); - Dir.clear(); - const char *Str = TypeD.getName(); - if (Str) Type = Str; - Str = Unit.getFilename(); - if (Str) File = Str; - Str = Unit.getDirectory(); - if (Str) Dir = Str; + TypeD.getName(Type); + Unit.getFilename(File); + Unit.getDirectory(Dir); return true; } } @@ -932,17 +929,17 @@ void DICompileUnit::dump() const { if (getLanguage()) cerr << " [" << dwarf::LanguageString(getLanguage()) << "] "; - const char *Dir = getDirectory(); - const char *FN = getFilename(); - cerr << " [" << (Dir ? Dir : "") << "/" << (FN ? FN : "") << " ]"; + std::string Res1, Res2; + cerr << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]"; } /// dump - print type. void DIType::dump() const { if (isNull()) return; - if (const char *N = getName()) - cerr << " [" << N << "] "; + std::string Res; + if (!getName(Res).empty()) + cerr << " [" << Res << "] "; unsigned Tag = getTag(); cerr << " [" << dwarf::TagString(Tag) << "] "; @@ -999,8 +996,9 @@ void DICompositeType::dump() const { /// dump - print global. void DIGlobal::dump() const { - if (const char *N = getName()) - cerr << " [" << N << "] "; + std::string Res; + if (!getName(Res).empty()) + cerr << " [" << Res << "] "; unsigned Tag = getTag(); cerr << " [" << dwarf::TagString(Tag) << "] "; @@ -1033,8 +1031,9 @@ void DIGlobalVariable::dump() const { /// dump - print variable. void DIVariable::dump() const { - if (const char *N = getName()) - cerr << " [" << N << "] "; + std::string Res; + if (!getName(Res).empty()) + cerr << " [" << Res << "] "; getCompileUnit().dump(); cerr << " [" << getLineNumber() << "] "; diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index d3a7d1bfeb..20fa69ea24 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -17,10 +17,9 @@ #include "llvm/Instructions.h" #include "llvm/GlobalVariable.h" #include "llvm/IntrinsicInst.h" -#include "llvm/ADT/DenseMap.h" +#include "llvm/Target/TargetData.h" #include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/MathExtras.h" -#include "llvm/Target/TargetData.h" #include <cstring> using namespace llvm; @@ -929,7 +928,6 @@ Value *llvm::FindInsertedValue(Value *V, const unsigned *idx_begin, return FindInsertedValue(I->getAggregateOperand(), Idxs.begin(), Idxs.end(), InsertBefore); } - // Otherwise, we don't know (such as, extracting from a function return value // or load instruction) return 0; @@ -938,86 +936,55 @@ Value *llvm::FindInsertedValue(Value *V, const unsigned *idx_begin, /// GetConstantStringInfo - This function computes the length of a /// null-terminated C string pointed to by V. If successful, it returns true /// and returns the string in Str. If unsuccessful, it returns false. -const char *llvm::GetConstantStringInfo(Value *V, uint64_t Offset, - bool StopAtNul) { - static DenseMap<Value*, std::string> StringInfoMap; - static DenseMap<Value*, bool> NulMap; - - // If we've already determined that the Value is NUL, then return 0. - if (NulMap[V]) - return 0; - - // Check to see if we've already calculated the string info. - if (StringInfoMap.find(V) != StringInfoMap.end()) - return StringInfoMap.lookup(V).c_str(); - - // If V is NULL then return nul. - if (V == 0) { - NulMap[V] = true; - return 0; - } - - std::string *Str = &StringInfoMap.FindAndConstruct(V).second; - Str->clear(); +bool llvm::GetConstantStringInfo(Value *V, std::string &Str, uint64_t Offset, + bool StopAtNul) { + // If V is NULL then return false; + if (V == NULL) return false; // Look through bitcast instructions. if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) - return GetConstantStringInfo(BCI->getOperand(0), Offset, StopAtNul); - + return GetConstantStringInfo(BCI->getOperand(0), Str, Offset, StopAtNul); + // If the value is not a GEP instruction nor a constant expression with a // GEP instruction, then return false because ConstantArray can't occur // any other way User *GEP = 0; - if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) { GEP = GEPI; } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { if (CE->getOpcode() == Instruction::BitCast) - return GetConstantStringInfo(CE->getOperand(0), Offset, StopAtNul); - - if (CE->getOpcode() != Instruction::GetElementPtr) { - NulMap[V] = true; - return 0; - } - + return GetConstantStringInfo(CE->getOperand(0), Str, Offset, StopAtNul); + if (CE->getOpcode() != Instruction::GetElementPtr) + return false; GEP = CE; } if (GEP) { // Make sure the GEP has exactly three arguments. - if (GEP->getNumOperands() != 3) { - NulMap[V] = true; - return 0; - } - + if (GEP->getNumOperands() != 3) + return false; + // Make sure the index-ee is a pointer to array of i8. const PointerType *PT = cast<PointerType>(GEP->getOperand(0)->getType()); const ArrayType *AT = dyn_cast<ArrayType>(PT->getElementType()); - if (AT == 0 || AT->getElementType() != Type::Int8Ty) { - NulMap[V] = true; - return 0; - } + if (AT == 0 || AT->getElementType() != Type::Int8Ty) + return false; // Check to make sure that the first operand of the GEP is an integer and // has value 0 so that we are sure we're indexing into the initializer. ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1)); - if (FirstIdx == 0 || !FirstIdx->isZero()) { - NulMap[V] = true; - return 0; - } + if (FirstIdx == 0 || !FirstIdx->isZero()) + return false; // If the second index isn't a ConstantInt, then this is a variable index // into the array. If this occurs, we can't say anything meaningful about // the string. uint64_t StartIdx = 0; - if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) { + if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) StartIdx = CI->getZExtValue(); - } else { - NulMap[V] = true; - return 0; - } - - return GetConstantStringInfo(GEP->getOperand(0), StartIdx + Offset, + else + return false; + return GetConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset, StopAtNul); } @@ -1025,53 +992,42 @@ const char *llvm::GetConstantStringInfo(Value *V, uint64_t Offset, // variable that is a constant and is initialized. The referenced constant // initializer is the array that we'll use for optimization. GlobalVariable* GV = dyn_cast<GlobalVariable>(V); - if (!GV || !GV->isConstant() || !GV->hasInitializer()) { - NulMap[V] = true; - return 0; - } + if (!GV || !GV->isConstant() || !GV->hasInitializer()) + return false; Constant *GlobalInit = GV->getInitializer(); // Handle the ConstantAggregateZero case - if (isa<ConstantAggregateZero>(GlobalInit)) + if (isa<ConstantAggregateZero>(GlobalInit)) { // This is a degenerate case. The initializer is constant zero so the // length of the string must be zero. - return ""; + Str.clear(); + return true; + } // Must be a Constant Array ConstantArray *Array = dyn_cast<ConstantArray>(GlobalInit); - if (Array == 0 || Array->getType()->getElementType() != Type::Int8Ty) { - NulMap[V] = true; - return 0; - } + if (Array == 0 || Array->getType()->getElementType() != Type::Int8Ty) + return false; // Get the number of elements in the array uint64_t NumElts = Array->getType()->getNumElements(); - if (Offset > NumElts) { - NulMap[V] = true; - return 0; - } + if (Offset > NumElts) + return false; // Traverse the constant array from 'Offset' which is the place the GEP refers // to in the array. - Str->reserve(NumElts - Offset); - + Str.reserve(NumElts-Offset); for (unsigned i = Offset; i != NumElts; ++i) { Constant *Elt = Array->getOperand(i); ConstantInt *CI = dyn_cast<ConstantInt>(Elt); - - if (!CI) { // This array isn't suitable, non-int initializer. - StringInfoMap.erase(V); - NulMap[V] = true; - return 0; - } - + if (!CI) // This array isn't suitable, non-int initializer. + return false; if (StopAtNul && CI->isZero()) - return Str->c_str(); // we found end of string, success! - - Str->operator+=((char)CI->getZExtValue()); + return true; // we found end of string, success! + Str += (char)CI->getZExtValue(); } - + // The array isn't null terminated, but maybe this is a memcpy, not a strcpy. - return Str->c_str(); + return true; } |