Skip to content

Instantly share code, notes, and snippets.

@bploeckelman
Created September 3, 2012 23:31
Show Gist options
  • Save bploeckelman/3614825 to your computer and use it in GitHub Desktop.
Save bploeckelman/3614825 to your computer and use it in GitHub Desktop.
Ex2. Dynamic BasicBlock Count
bool CountBasicBlocks::runOnBasicBlock(BasicBlock &BB, Module &M) {
// Get the bbCounter GlobalVariable
GlobalVariable *bbCounter = M.getGlobalVariable("bbCounter");
assert(bbCounter && "Error: unable to get BasicBlock counter");
// Insert instructions at the end of this BasicBlock
TerminatorInst *InsertPos = BB.getTerminator();
// Load, increment and store the value back
Value *OldVal = new LoadInst(bbCounter, "old.bb.count", InsertPos);
Value *NewVal = BinaryOperator::Create(
Instruction::Add
, OldVal
, ConstantInt::get(Type::getInt64Ty(BB.getContext()), 1)
, "new.bb.count"
, InsertPos);
new StoreInst(NewVal, bbCounter, InsertPos);
return true; // IR modified
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment