Skip to content

Instantly share code, notes, and snippets.

@bploeckelman
Created September 3, 2012 23:17
Show Gist options
  • Save bploeckelman/3614671 to your computer and use it in GitHub Desktop.
Save bploeckelman/3614671 to your computer and use it in GitHub Desktop.
Ex2. Dynamic BasicBlock Count
bool CountBasicBlocks::initialize(Module &M) {
Function *Main = M.getFunction("main");
assert(Main && "Error: count-bb requires a main function");
Type *I64Ty = Type::getInt64Ty(M.getContext());
// Create and insert a new GlobalVariable to track the number
// of basic blocks that are executed during the program's run
// int64 bbCounter = 0;
new GlobalVariable(
M // Module
, I64Ty // Type
, false // isConstant
, GlobalValue::CommonLinkage // Linkage
, ConstantInt::get(I64Ty, 0) // Initializer
, "bbCounter"); // Name
// Create a declaration for the setupAtExit function
Type *VoidTy = Type::getVoidTy(M.getContext());
FunctionType *FTy = FunctionType::get(VoidTy, VoidTy);
Constant *setupAtExitFunc = M.getOrInsertFunction("setupAtExit", FTy);
// Insert a call to setupAtExit() at the start of main
BasicBlock *BB = &Main->front();
Instruction *I = &BB->front();
CallInst::Create(setupAtExitFunc, "", I);
return true; // modified IR
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment