2003-10-13 05:32:08 +02:00
|
|
|
//===-- EmitFunctions.cpp - interface to insert instrumentation -----------===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-07-16 20:58:08 +02:00
|
|
|
//
|
2004-10-20 21:38:58 +02:00
|
|
|
// This inserts into the input module three new global constants containing
|
|
|
|
// mapping information pertinent to the Reoptimizer's runtime library:
|
|
|
|
// 1) a structure containing a pointer to each function;
|
|
|
|
// 2) an array containing a boolean which is true iff the corresponding
|
|
|
|
// function in 1) contains a back-edge branch suitable for the Reoptimizer's
|
|
|
|
// first-level instrumentation;
|
|
|
|
// 3) an integer containing the number of entries in 1) and 2).
|
2004-03-08 17:45:53 +01:00
|
|
|
//
|
|
|
|
// NOTE: This pass is used by the reoptimizer only.
|
2002-07-16 20:58:08 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
2003-01-14 23:39:29 +01:00
|
|
|
#include "llvm/Pass.h"
|
2003-07-18 22:55:26 +02:00
|
|
|
#include "llvm/Support/CFG.h"
|
2005-01-06 06:46:44 +01:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2004-03-08 17:45:53 +01:00
|
|
|
using namespace llvm;
|
2002-07-16 20:58:08 +02:00
|
|
|
|
2005-04-22 01:48:37 +02:00
|
|
|
namespace llvm {
|
2004-09-30 22:14:07 +02:00
|
|
|
|
2002-07-23 20:06:35 +02:00
|
|
|
namespace {
|
2004-03-08 17:45:53 +01:00
|
|
|
enum Color{
|
|
|
|
WHITE,
|
|
|
|
GREY,
|
|
|
|
BLACK
|
|
|
|
};
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2004-09-20 06:48:05 +02:00
|
|
|
struct EmitFunctionTable : public ModulePass {
|
|
|
|
bool runOnModule(Module &M);
|
2002-07-23 20:06:35 +02:00
|
|
|
};
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2004-03-08 17:45:53 +01:00
|
|
|
RegisterOpt<EmitFunctionTable>
|
|
|
|
X("emitfuncs", "Emit a function table for the reoptimizer");
|
2002-07-23 20:06:35 +02:00
|
|
|
}
|
2002-07-16 20:58:08 +02:00
|
|
|
|
2004-03-08 17:45:53 +01:00
|
|
|
static char doDFS(BasicBlock * node,std::map<BasicBlock *, Color > &color){
|
2003-07-18 22:55:26 +02:00
|
|
|
color[node] = GREY;
|
|
|
|
|
2003-09-25 00:07:33 +02:00
|
|
|
for(succ_iterator vl = succ_begin(node), ve = succ_end(node); vl != ve; ++vl){
|
2005-04-22 01:48:37 +02:00
|
|
|
|
|
|
|
BasicBlock *BB = *vl;
|
|
|
|
|
2003-07-18 22:55:26 +02:00
|
|
|
if(color[BB]!=GREY && color[BB]!=BLACK){
|
|
|
|
if(!doDFS(BB, color)){
|
2005-04-23 23:38:35 +02:00
|
|
|
return 0;
|
2003-07-18 22:55:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//if has backedge
|
|
|
|
else if(color[BB]==GREY)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
color[node] = BLACK;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-03-08 17:45:53 +01:00
|
|
|
static char hasBackEdge(Function *F){
|
2003-07-18 22:55:26 +02:00
|
|
|
std::map<BasicBlock *, Color > color;
|
|
|
|
return doDFS(F->begin(), color);
|
|
|
|
}
|
|
|
|
|
2002-07-16 20:58:08 +02:00
|
|
|
// Per Module pass for inserting function table
|
2004-09-20 06:48:05 +02:00
|
|
|
bool EmitFunctionTable::runOnModule(Module &M){
|
2003-05-23 00:00:07 +02:00
|
|
|
std::vector<const Type*> vType;
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2003-05-23 00:00:07 +02:00
|
|
|
std::vector<Constant *> vConsts;
|
2003-07-18 22:55:26 +02:00
|
|
|
std::vector<Constant *> sBCons;
|
|
|
|
|
|
|
|
unsigned int counter = 0;
|
2003-05-23 00:00:07 +02:00
|
|
|
for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
|
2002-07-16 20:58:08 +02:00
|
|
|
if (!MI->isExternal()) {
|
|
|
|
vType.push_back(MI->getType());
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2003-07-18 22:55:26 +02:00
|
|
|
//std::cerr<<MI;
|
|
|
|
|
2004-07-18 02:44:37 +02:00
|
|
|
vConsts.push_back(MI);
|
2003-07-18 22:55:26 +02:00
|
|
|
sBCons.push_back(ConstantInt::get(Type::SByteTy, hasBackEdge(MI)));
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2003-06-01 04:40:49 +02:00
|
|
|
counter++;
|
2002-07-16 20:58:08 +02:00
|
|
|
}
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-07-16 20:58:08 +02:00
|
|
|
StructType *sttype = StructType::get(vType);
|
2004-02-15 05:07:32 +01:00
|
|
|
Constant *cstruct = ConstantStruct::get(sttype, vConsts);
|
2002-07-16 20:58:08 +02:00
|
|
|
|
2003-04-16 22:28:45 +02:00
|
|
|
GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
|
2005-04-22 01:48:37 +02:00
|
|
|
GlobalValue::ExternalLinkage,
|
2002-07-16 20:58:08 +02:00
|
|
|
cstruct, "llvmFunctionTable");
|
|
|
|
M.getGlobalList().push_back(gb);
|
2003-06-01 04:40:49 +02:00
|
|
|
|
2005-04-22 01:48:37 +02:00
|
|
|
Constant *constArray = ConstantArray::get(ArrayType::get(Type::SByteTy,
|
2005-04-23 23:38:35 +02:00
|
|
|
sBCons.size()),
|
|
|
|
sBCons);
|
2003-07-18 22:55:26 +02:00
|
|
|
|
|
|
|
GlobalVariable *funcArray = new GlobalVariable(constArray->getType(), true,
|
2005-04-23 23:38:35 +02:00
|
|
|
GlobalValue::ExternalLinkage,
|
|
|
|
constArray, "llvmSimpleFunction");
|
2003-07-18 22:55:26 +02:00
|
|
|
|
|
|
|
M.getGlobalList().push_back(funcArray);
|
|
|
|
|
2005-04-22 01:48:37 +02:00
|
|
|
ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter);
|
|
|
|
GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true,
|
2005-04-23 23:38:35 +02:00
|
|
|
GlobalValue::ExternalLinkage,
|
|
|
|
cnst, "llvmFunctionCount");
|
2003-06-01 04:40:49 +02:00
|
|
|
M.getGlobalList().push_back(fnCount);
|
2002-07-16 20:58:08 +02:00
|
|
|
return true; // Always modifies program
|
|
|
|
}
|
2004-09-30 22:14:07 +02:00
|
|
|
|
|
|
|
ModulePass *createEmitFunctionTablePass () {
|
|
|
|
return new EmitFunctionTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace llvm
|