1 //===-- llvm/Analysis/ModuleAnalyzer.cpp - Module analysis driver ----------==//
3 // This class provides a nice interface to traverse a module in a predictable
4 // way. This is used by the AssemblyWriter, BytecodeWriter, and SlotCalculator
5 // to do analysis of a module.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/ModuleAnalyzer.h"
10 #include "llvm/Method.h"
11 #include "llvm/Module.h"
12 #include "llvm/BasicBlock.h"
13 #include "llvm/DerivedTypes.h"
14 #include "llvm/ConstPoolVals.h"
15 #include "llvm/Support/STLExtras.h"
18 // processModule - Driver function to call all of my subclasses virtual methods.
20 bool ModuleAnalyzer::processModule(const Module *M) {
21 return processMethods(M);
24 inline bool ModuleAnalyzer::handleType(set<const Type *> &TypeSet,
26 if (!T->isDerivedType()) return false; // Boring boring types...
27 if (TypeSet.count(T) != 0) return false; // Already found this type...
28 TypeSet.insert(T); // Add it to the set
30 // Recursively process interesting types...
31 switch (T->getPrimitiveID()) {
32 case Type::MethodTyID: {
33 const MethodType *MT = (const MethodType *)T;
34 if (handleType(TypeSet, MT->getReturnType())) return true;
35 const MethodType::ParamTypes &Params = MT->getParamTypes();
37 for (MethodType::ParamTypes::const_iterator I = Params.begin();
38 I != Params.end(); ++I)
39 if (handleType(TypeSet, *I)) return true;
44 if (handleType(TypeSet, ((const ArrayType *)T)->getElementType()))
48 case Type::StructTyID: {
49 const StructType *ST = (const StructType*)T;
50 const StructType::ElementTypes &Elements = ST->getElementTypes();
51 for (StructType::ElementTypes::const_iterator I = Elements.begin();
52 I != Elements.end(); ++I)
53 if (handleType(TypeSet, *I)) return true;
57 case Type::PointerTyID:
58 if (handleType(TypeSet, ((const PointerType *)T)->getValueType()))
63 cerr << "ModuleAnalyzer::handleType, type unknown: '"
64 << T->getName() << "'\n";
68 return processType(T);
72 bool ModuleAnalyzer::processMethods(const Module *M) {
73 return apply_until(M->begin(), M->end(),
74 bind_obj(this, &ModuleAnalyzer::processMethod));
77 bool ModuleAnalyzer::processMethod(const Method *M) {
78 // Loop over the arguments, processing them...
79 if (apply_until(M->getArgumentList().begin(), M->getArgumentList().end(),
80 bind_obj(this, &ModuleAnalyzer::processMethodArgument)))
83 // Loop over all the basic blocks, in order...
84 return apply_until(M->begin(), M->end(),
85 bind_obj(this, &ModuleAnalyzer::processBasicBlock));
88 bool ModuleAnalyzer::processBasicBlock(const BasicBlock *BB) {
89 // Process all of the instructions in the basic block
90 BasicBlock::const_iterator Inst = BB->begin();
91 for (; Inst != BB->end(); Inst++) {
92 if (preProcessInstruction(*Inst) || processInstruction(*Inst)) return true;
97 bool ModuleAnalyzer::preProcessInstruction(const Instruction *I) {