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/ConstantPool.h"
11 #include "llvm/Method.h"
12 #include "llvm/Module.h"
13 #include "llvm/BasicBlock.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/ConstPoolVals.h"
18 // processModule - Driver function to call all of my subclasses virtual methods.
20 bool ModuleAnalyzer::processModule(const Module *M) {
21 // Loop over the constant pool, process all of the constants...
22 if (processConstPool(M->getConstantPool(), false))
25 return processMethods(M);
28 inline bool ModuleAnalyzer::handleType(set<const Type *> &TypeSet,
30 if (!T->isDerivedType()) return false; // Boring boring types...
31 if (TypeSet.count(T) != 0) return false; // Already found this type...
32 TypeSet.insert(T); // Add it to the set
34 // Recursively process interesting types...
35 switch (T->getPrimitiveID()) {
36 case Type::MethodTyID: {
37 const MethodType *MT = (const MethodType *)T;
38 if (handleType(TypeSet, MT->getReturnType())) return true;
39 const MethodType::ParamTypes &Params = MT->getParamTypes();
41 for (MethodType::ParamTypes::const_iterator I = Params.begin();
42 I != Params.end(); ++I)
43 if (handleType(TypeSet, *I)) return true;
48 if (handleType(TypeSet, ((const ArrayType *)T)->getElementType()))
52 case Type::StructTyID: {
53 const StructType *ST = (const StructType*)T;
54 const StructType::ElementTypes &Elements = ST->getElementTypes();
55 for (StructType::ElementTypes::const_iterator I = Elements.begin();
56 I != Elements.end(); ++I)
57 if (handleType(TypeSet, *I)) return true;
61 case Type::PointerTyID:
62 if (handleType(TypeSet, ((const PointerType *)T)->getValueType()))
67 cerr << "ModuleAnalyzer::handleType, type unknown: '"
68 << T->getName() << "'\n";
72 return processType(T);
76 bool ModuleAnalyzer::processConstPool(const ConstantPool &CP, bool isMethod) {
77 // TypeSet - Keep track of which types have already been processType'ed. We
78 // don't want to reprocess the same type more than once.
80 set<const Type *> TypeSet;
82 for (ConstantPool::plane_const_iterator PI = CP.begin();
83 PI != CP.end(); ++PI) {
84 const ConstantPool::PlaneType &Plane = **PI;
85 if (Plane.empty()) continue; // Skip empty type planes...
87 if (processConstPoolPlane(CP, Plane, isMethod)) return true;
89 for (ConstantPool::PlaneType::const_iterator CI = Plane.begin();
90 CI != Plane.end(); CI++) {
91 if ((*CI)->getType() == Type::TypeTy)
92 if (handleType(TypeSet, ((const ConstPoolType*)(*CI))->getValue()))
94 if (handleType(TypeSet, (*CI)->getType())) return true;
96 if (processConstant(*CI)) return true;
101 assert(CP.getParent()->getValueType() == Value::ModuleVal);
102 const Module *M = (const Module*)CP.getParent();
103 // Process the method types after the constant pool...
104 for (Module::MethodListType::const_iterator I = M->getMethodList().begin();
105 I != M->getMethodList().end(); I++) {
106 if (handleType(TypeSet, (*I)->getType())) return true;
107 if (visitMethod(*I)) return true;
113 bool ModuleAnalyzer::processMethods(const Module *M) {
114 for (Module::MethodListType::const_iterator I = M->getMethodList().begin();
115 I != M->getMethodList().end(); I++)
116 if (processMethod(*I)) return true;
121 bool ModuleAnalyzer::processMethod(const Method *M) {
122 // Loop over the arguments, processing them...
123 const Method::ArgumentListType &ArgList = M->getArgumentList();
124 for (Method::ArgumentListType::const_iterator AI = ArgList.begin();
125 AI != ArgList.end(); AI++)
126 if (processMethodArgument(*AI)) return true;
128 // Loop over the constant pool, adding the constants to the table...
129 processConstPool(M->getConstantPool(), true);
131 // Loop over all the basic blocks, in order...
132 Method::BasicBlocksType::const_iterator BBI = M->getBasicBlocks().begin();
133 for (; BBI != M->getBasicBlocks().end(); BBI++)
134 if (processBasicBlock(*BBI)) return true;
138 bool ModuleAnalyzer::processBasicBlock(const BasicBlock *BB) {
139 // Process all of the instructions in the basic block
140 BasicBlock::InstListType::const_iterator Inst = BB->getInstList().begin();
141 for (; Inst != BB->getInstList().end(); Inst++) {
142 if (preProcessInstruction(*Inst) || processInstruction(*Inst)) return true;
147 bool ModuleAnalyzer::preProcessInstruction(const Instruction *I) {