Skip to content

Commit

Permalink
refs #26, temp submit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tengfei1010 committed Jan 18, 2018
1 parent aae2dc1 commit e55f8c8
Show file tree
Hide file tree
Showing 8 changed files with 63,938 additions and 2 deletions.
17 changes: 17 additions & 0 deletions include/Common/Constant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef COMAIR_CONSTANT_H
#define COMAIR_CONSTANT_H

#define APROF_INSERT_FLAG "aprof_insert_flag"

enum {
INIT = 0,
WRITE = 1,
READ = 2,
CALL_BEFORE = 3,
RETURN = 4,
INCREMENT_RMS = 5,
COST_UPDATE = 6,
};


#endif //COMAIR_CONSTANT_H
32 changes: 32 additions & 0 deletions include/MarkFlagForAprof/MarkFlagForAprof.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef COMAIR_MARKFLAGFORAPROF_H
#define COMAIR_MARKFLAGFORAPROF_H

#include "llvm/Pass.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/GlobalVariable.h"


using namespace llvm;

struct MarkFlagForAprof : public ModulePass {
static char ID;

MarkFlagForAprof();

Module *pModule;

virtual void getAnalysisUsage(AnalysisUsage &AU) const;

virtual bool runOnModule(Module &M);

void markInstFlag(Instruction *, int);

void setupInit(Module *M);
void setupTypes();

// Type
Type *IntType;
};

#endif //COMAIR_MARKFLAGFORAPROF_H
3 changes: 2 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(AProf)
add_subdirectory(AprofHook)
add_subdirectory(IDAssigner)
add_subdirectory(IDAssigner)
add_subdirectory(MarkFlagForAprof)
13 changes: 13 additions & 0 deletions lib/MarkFlagForAprof/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
add_library(MarkFlagForAprofPass MODULE
# List your source files here.
MarkFlagForAprof.cpp
)

# Use C++11 to compile our pass (i.e., supply -std=c++11).
target_compile_features(MarkFlagForAprofPass PRIVATE cxx_range_for cxx_auto_type)

# LLVM is (typically) built with no C++ RTTI. We need to match that;
# otherwise, we'll get linker errors about missing RTTI data.
set_target_properties(MarkFlagForAprofPass PROPERTIES
COMPILE_FLAGS "-fno-rtti"
)
173 changes: 173 additions & 0 deletions lib/MarkFlagForAprof/MarkFlagForAprof.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#include "llvm/Analysis/PostDominators.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/Support/raw_ostream.h"

#include "Common/Constant.h"

#include "MarkFlagForAprof/MarkFlagForAprof.h"


using namespace std;


static RegisterPass<MarkFlagForAprof> X(
"mark-flag-aprof", "mark flags for insert aprof functions", true, true);


/* local function */

int GetFunctionID(Function *F) {

if (F->begin() == F->end()) {
return -1;
}

Instruction *I = &*(F->begin()->begin());

MDNode *Node = I->getMetadata("func_id");
if (!Node) {
return -1;
}

assert(Node->getNumOperands() == 1);
const Metadata *MD = Node->getOperand(0);
if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
Value *V = MDV->getValue();
ConstantInt *CI = dyn_cast<ConstantInt>(V);
assert(CI);
return CI->getZExtValue();
}

return -1;
}

int GetBasicBlockID(BasicBlock *BB) {
if (BB->begin() == BB->end()) {
return -1;
}

Instruction *I = &*(BB->begin());

MDNode *Node = I->getMetadata("bb_id");
if (!Node) {
return -1;
}

assert(Node->getNumOperands() == 1);
const Metadata *MD = Node->getOperand(0);
if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
Value *V = MDV->getValue();
ConstantInt *CI = dyn_cast<ConstantInt>(V);
assert(CI);
return CI->getZExtValue();
}

return -1;
}

int GetInstructionID(Instruction *II) {

MDNode *Node = II->getMetadata("ins_id");
if (!Node) {
return -1;
}

assert(Node->getNumOperands() == 1);
const Metadata *MD = Node->getOperand(0);
if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
Value *V = MDV->getValue();
ConstantInt *CI = dyn_cast<ConstantInt>(V);
assert(CI);
return CI->getZExtValue();
}

return -1;
}

bool isIgnoreFunc(Function *F) {

if (F->getSection().str() == ".text.startup") {
return true;
}

int FuncID = GetFunctionID(F);

if (FuncID < 0) {
return true;
}

return false;
}

/* */

char MarkFlagForAprof::ID = 0;

void MarkFlagForAprof::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<PostDominatorTreeWrapperPass>();

}

MarkFlagForAprof::MarkFlagForAprof() : ModulePass(ID) {
initializePostDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
}

void MarkFlagForAprof::setupTypes() {
this->IntType = IntegerType::get(this->pModule->getContext(), 32);
}

void MarkFlagForAprof::setupInit(Module *M) {
this->pModule = M;
setupTypes();
}

void MarkFlagForAprof::markInstFlag(Instruction *Inst, int Flag) {
MDBuilder MDHelper(this->pModule->getContext());
Constant *InsID = ConstantInt::get(this->IntType, Flag);
SmallVector<Metadata *, 1> Vals;
Vals.push_back(MDHelper.createConstant(InsID));
Inst->setMetadata(
APROF_INSERT_FLAG,
MDNode::get(this->pModule->getContext(), Vals)
);
}

bool MarkFlagForAprof::runOnModule(Module &M) {

setupInit(&M);

for (Module::iterator FI = this->pModule->begin();
FI != this->pModule->end(); FI++) {

Function *Func = &*FI;

if (isIgnoreFunc(Func)) {
continue;
}

PostDominatorTree *PDT = &getAnalysis<PostDominatorTreeWrapperPass>(
*Func).getPostDomTree();

for (Function::iterator BI = Func->begin(); BI != Func->end(); BI++) {

errs() << "===============" << "\n";
BasicBlock *BB = &*BI;
BB->dump();

DomTreeNodeBase<BasicBlock> *tree = PDT->getNode(BB);

for (auto TI = tree->begin(); TI != tree->end(); TI++) {
BasicBlock *BB = (*TI)->getBlock();
BB->dump();
}
errs() << "===============" << "\n";
}

}

return false;
}
Binary file modified scripts/apache34464/a.out
Binary file not shown.
Loading

0 comments on commit e55f8c8

Please sign in to comment.