2001-06-06 20:29:01 +00:00
|
|
|
//===-- llvm/Analysis/ModuleAnalyzer.cpp - Module analysis driver ----------==//
|
|
|
|
//
|
|
|
|
// This class provides a nice interface to traverse a module in a predictable
|
|
|
|
// way. This is used by the AssemblyWriter, BytecodeWriter, and SlotCalculator
|
|
|
|
// to do analysis of a module.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/ModuleAnalyzer.h"
|
|
|
|
#include "llvm/Method.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/STLExtras.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <map>
|
2002-01-20 22:54:45 +00:00
|
|
|
#include <iostream>
|
|
|
|
using std::set;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// processModule - Driver function to call all of my subclasses virtual methods.
|
|
|
|
//
|
|
|
|
bool ModuleAnalyzer::processModule(const Module *M) {
|
|
|
|
return processMethods(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ModuleAnalyzer::handleType(set<const Type *> &TypeSet,
|
|
|
|
const Type *T) {
|
|
|
|
if (!T->isDerivedType()) return false; // Boring boring types...
|
|
|
|
if (TypeSet.count(T) != 0) return false; // Already found this type...
|
|
|
|
TypeSet.insert(T); // Add it to the set
|
|
|
|
|
|
|
|
// Recursively process interesting types...
|
|
|
|
switch (T->getPrimitiveID()) {
|
|
|
|
case Type::MethodTyID: {
|
|
|
|
const MethodType *MT = (const MethodType *)T;
|
|
|
|
if (handleType(TypeSet, MT->getReturnType())) return true;
|
|
|
|
const MethodType::ParamTypes &Params = MT->getParamTypes();
|
|
|
|
|
|
|
|
for (MethodType::ParamTypes::const_iterator I = Params.begin();
|
|
|
|
I != Params.end(); ++I)
|
|
|
|
if (handleType(TypeSet, *I)) return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::ArrayTyID:
|
|
|
|
if (handleType(TypeSet, ((const ArrayType *)T)->getElementType()))
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::StructTyID: {
|
2001-10-02 03:41:24 +00:00
|
|
|
const StructType *ST = cast<const StructType>(T);
|
2001-06-06 20:29:01 +00:00
|
|
|
const StructType::ElementTypes &Elements = ST->getElementTypes();
|
|
|
|
for (StructType::ElementTypes::const_iterator I = Elements.begin();
|
|
|
|
I != Elements.end(); ++I)
|
|
|
|
if (handleType(TypeSet, *I)) return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::PointerTyID:
|
2001-12-04 00:03:30 +00:00
|
|
|
if (handleType(TypeSet, cast<const PointerType>(T)->getElementType()))
|
2001-06-06 20:29:01 +00:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2002-01-20 22:54:45 +00:00
|
|
|
std::cerr << "ModuleAnalyzer::handleType, type unknown: '"
|
|
|
|
<< T->getName() << "'\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return processType(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ModuleAnalyzer::processMethods(const Module *M) {
|
2001-06-27 23:41:11 +00:00
|
|
|
return apply_until(M->begin(), M->end(),
|
|
|
|
bind_obj(this, &ModuleAnalyzer::processMethod));
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleAnalyzer::processMethod(const Method *M) {
|
|
|
|
// Loop over the arguments, processing them...
|
2001-06-27 23:41:11 +00:00
|
|
|
if (apply_until(M->getArgumentList().begin(), M->getArgumentList().end(),
|
|
|
|
bind_obj(this, &ModuleAnalyzer::processMethodArgument)))
|
|
|
|
return true;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Loop over all the basic blocks, in order...
|
2001-06-27 23:41:11 +00:00
|
|
|
return apply_until(M->begin(), M->end(),
|
|
|
|
bind_obj(this, &ModuleAnalyzer::processBasicBlock));
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleAnalyzer::processBasicBlock(const BasicBlock *BB) {
|
|
|
|
// Process all of the instructions in the basic block
|
2001-06-27 23:41:11 +00:00
|
|
|
BasicBlock::const_iterator Inst = BB->begin();
|
|
|
|
for (; Inst != BB->end(); Inst++) {
|
2001-06-06 20:29:01 +00:00
|
|
|
if (preProcessInstruction(*Inst) || processInstruction(*Inst)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ModuleAnalyzer::preProcessInstruction(const Instruction *I) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|