aboutsummaryrefslogtreecommitdiffstats
path: root/lib/VMCore/Module.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/VMCore/Module.cpp')
-rw-r--r--lib/VMCore/Module.cpp102
1 files changed, 58 insertions, 44 deletions
diff --git a/lib/VMCore/Module.cpp b/lib/VMCore/Module.cpp
index 3c67191..8ea3665 100644
--- a/lib/VMCore/Module.cpp
+++ b/lib/VMCore/Module.cpp
@@ -65,20 +65,20 @@ Module::~Module() {
Module::Endianness Module::getEndianness() const {
StringRef temp = DataLayout;
Module::Endianness ret = AnyEndianness;
-
+
while (!temp.empty()) {
std::pair<StringRef, StringRef> P = getToken(temp, "-");
-
+
StringRef token = P.first;
temp = P.second;
-
+
if (token[0] == 'e') {
ret = LittleEndian;
} else if (token[0] == 'E') {
ret = BigEndian;
}
}
-
+
return ret;
}
@@ -86,13 +86,13 @@ Module::Endianness Module::getEndianness() const {
Module::PointerSize Module::getPointerSize() const {
StringRef temp = DataLayout;
Module::PointerSize ret = AnyPointerSize;
-
+
while (!temp.empty()) {
std::pair<StringRef, StringRef> TmpP = getToken(temp, "-");
temp = TmpP.second;
TmpP = getToken(TmpP.first, ":");
StringRef token = TmpP.second, signalToken = TmpP.first;
-
+
if (signalToken[0] == 'p') {
int size = 0;
getToken(token, ":").first.getAsInteger(10, size);
@@ -102,7 +102,7 @@ Module::PointerSize Module::getPointerSize() const {
ret = Pointer64;
}
}
-
+
return ret;
}
@@ -164,9 +164,9 @@ Constant *Module::getOrInsertFunction(StringRef Name,
// right type.
if (F->getType() != PointerType::getUnqual(Ty))
return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
-
+
// Otherwise, we just found the existing function or a prototype.
- return F;
+ return F;
}
Constant *Module::getOrInsertTargetIntrinsic(StringRef Name,
@@ -183,13 +183,12 @@ Constant *Module::getOrInsertTargetIntrinsic(StringRef Name,
}
// Otherwise, we just found the existing function or a prototype.
- return F;
+ return F;
}
Constant *Module::getOrInsertFunction(StringRef Name,
FunctionType *Ty) {
- AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0);
- return getOrInsertFunction(Name, Ty, AttributeList);
+ return getOrInsertFunction(Name, Ty, AttrListPtr());
}
// getOrInsertFunction - Look up the specified function in the module symbol
@@ -229,9 +228,9 @@ Constant *Module::getOrInsertFunction(StringRef Name,
va_end(Args);
// Build the function type and chain to the other getOrInsertFunction...
- return getOrInsertFunction(Name,
+ return getOrInsertFunction(Name,
FunctionType::get(RetTy, ArgTys, false),
- AttrListPtr::get((AttributeWithIndex *)0, 0));
+ AttrListPtr());
}
// getFunction - Look up the specified function in the module symbol table.
@@ -254,7 +253,7 @@ Function *Module::getFunction(StringRef Name) const {
///
GlobalVariable *Module::getGlobalVariable(StringRef Name,
bool AllowLocal) const {
- if (GlobalVariable *Result =
+ if (GlobalVariable *Result =
dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
if (AllowLocal || !Result->hasLocalLinkage())
return Result;
@@ -282,7 +281,7 @@ Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
// right type.
if (GV->getType() != PointerType::getUnqual(Ty))
return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
-
+
// Otherwise, we just found the existing function or a prototype.
return GV;
}
@@ -299,7 +298,7 @@ GlobalAlias *Module::getNamedAlias(StringRef Name) const {
}
/// getNamedMetadata - Return the first NamedMDNode in the module with the
-/// specified name. This method returns null if a NamedMDNode with the
+/// specified name. This method returns null if a NamedMDNode with the
/// specified name is not found.
NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
SmallString<256> NameData;
@@ -307,8 +306,8 @@ NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
}
-/// getOrInsertNamedMetadata - Return the first named MDNode in the module
-/// with the specified name. This method returns a new NamedMDNode if a
+/// getOrInsertNamedMetadata - Return the first named MDNode in the module
+/// with the specified name. This method returns a new NamedMDNode if a
/// NamedMDNode with the specified name is not found.
NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
NamedMDNode *&NMD =
@@ -481,12 +480,13 @@ namespace {
// objects, we keep several helper maps.
DenseSet<const Value*> VisitedConstants;
DenseSet<Type*> VisitedTypes;
-
+
std::vector<StructType*> &StructTypes;
+ bool OnlyNamed;
public:
- TypeFinder(std::vector<StructType*> &structTypes)
- : StructTypes(structTypes) {}
-
+ TypeFinder(std::vector<StructType*> &structTypes, bool onlyNamed)
+ : StructTypes(structTypes), OnlyNamed(onlyNamed) {}
+
void run(const Module &M) {
// Get types from global variables.
for (Module::const_global_iterator I = M.global_begin(),
@@ -495,7 +495,7 @@ namespace {
if (I->hasInitializer())
incorporateValue(I->getInitializer());
}
-
+
// Get types from aliases.
for (Module::const_alias_iterator I = M.alias_begin(),
E = M.alias_end(); I != E; ++I) {
@@ -503,24 +503,32 @@ namespace {
if (const Value *Aliasee = I->getAliasee())
incorporateValue(Aliasee);
}
-
- SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
// Get types from functions.
+ SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst;
for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
incorporateType(FI->getType());
-
+
+ // First incorporate the arguments.
+ for (Function::const_arg_iterator AI = FI->arg_begin(),
+ AE = FI->arg_end(); AI != AE; ++AI)
+ incorporateValue(AI);
+
for (Function::const_iterator BB = FI->begin(), E = FI->end();
BB != E;++BB)
for (BasicBlock::const_iterator II = BB->begin(),
E = BB->end(); II != E; ++II) {
const Instruction &I = *II;
- // Incorporate the type of the instruction and all its operands.
+ // Incorporate the type of the instruction.
incorporateType(I.getType());
+
+ // Incorporate non-instruction operand types. (We are incorporating
+ // all instructions with this loop.)
for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
OI != OE; ++OI)
- incorporateValue(*OI);
-
+ if (!isa<Instruction>(OI))
+ incorporateValue(*OI);
+
// Incorporate types hiding in metadata.
I.getAllMetadataOtherThanDebugLoc(MDForInst);
for (unsigned i = 0, e = MDForInst.size(); i != e; ++i)
@@ -528,7 +536,7 @@ namespace {
MDForInst.clear();
}
}
-
+
for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
E = M.named_metadata_end(); I != E; ++I) {
const NamedMDNode *NMD = I;
@@ -536,23 +544,24 @@ namespace {
incorporateMDNode(NMD->getOperand(i));
}
}
-
+
private:
void incorporateType(Type *Ty) {
// Check to see if we're already visited this type.
if (!VisitedTypes.insert(Ty).second)
return;
-
+
// If this is a structure or opaque type, add a name for the type.
if (StructType *STy = dyn_cast<StructType>(Ty))
- StructTypes.push_back(STy);
-
+ if (!OnlyNamed || STy->hasName())
+ StructTypes.push_back(STy);
+
// Recursively walk all contained types.
for (Type::subtype_iterator I = Ty->subtype_begin(),
E = Ty->subtype_end(); I != E; ++I)
incorporateType(*I);
}
-
+
/// incorporateValue - This method is used to walk operand lists finding
/// types hiding in constant expressions and other operands that won't be
/// walked in other ways. GlobalValues, basic blocks, instructions, and
@@ -561,27 +570,31 @@ namespace {
if (const MDNode *M = dyn_cast<MDNode>(V))
return incorporateMDNode(M);
if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;
-
+
// Already visited?
if (!VisitedConstants.insert(V).second)
return;
-
+
// Check this type.
incorporateType(V->getType());
-
+
+ // If this is an instruction, we incorporate it separately.
+ if (isa<Instruction>(V))
+ return;
+
// Look in operands for types.
const User *U = cast<User>(V);
for (Constant::const_op_iterator I = U->op_begin(),
E = U->op_end(); I != E;++I)
incorporateValue(*I);
}
-
+
void incorporateMDNode(const MDNode *V) {
-
+
// Already visited?
if (!VisitedConstants.insert(V).second)
return;
-
+
// Look in operands for types.
for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i)
if (Value *Op = V->getOperand(i))
@@ -590,6 +603,7 @@ namespace {
};
} // end anonymous namespace
-void Module::findUsedStructTypes(std::vector<StructType*> &StructTypes) const {
- TypeFinder(StructTypes).run(*this);
+void Module::findUsedStructTypes(std::vector<StructType*> &StructTypes,
+ bool OnlyNamed) const {
+ TypeFinder(StructTypes, OnlyNamed).run(*this);
}