Created
September 3, 2012 23:31
-
-
Save bploeckelman/3614825 to your computer and use it in GitHub Desktop.
Ex2. Dynamic BasicBlock Count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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