I'm working towards analyzing the used instructions. Given I have found an instruction of interest, I want to backtrack all the used operations and variables. Given for example the following loop in LLVM IR.
%i.01 = phi i32 [ 0, %0 ], [ %4, %2 ]
%3 = icmp eq i32 %i.01, 0
%bar.foobar.i = select i1 %3, void ()* @bar, void ()* @foobar
tail call void %bar.foobar.i() #2
%4 = add nuw nsw i32 %i.01, 1
%exitcond = icmp eq i32 %4, 10
br i1 %exitcond, label %1, label %2
I'm interested for example in all involved data in the last branch. The condition is computed one instruction before using an ICompInst
which involves variable %4
and a constant 10
. %4
is computed one step before using an addition. And so on and so forth...
Given I found the instruction of interest, in this case the BranchInst
, I want to start my backtracking.
if(BranchInst* BI = dyn_cast<BranchInst>(&I)) {
if(BI->isConditional()) {
backtrack(BI, &I);
}
}
To implement such an algorithm, I have two questions. Having the branch instruction, how can I get the condition to start the backtracking? Second, how can I determine if the condition is used in a previous instruction? To understand it easier, I have the following pseudo code:
void backtrack(BranchInst* BI, Instruction* I) {
Value* condition = BI->getCondition();
Instruction* prevInst = I;
do {
prevInst = prev(prevInst);
if(prevInst->prevInst(condition)) {
backTrackOperand(prevInst->getOperand(0), prevInst);
backTrackOperand(prevInst->getOperand(1), prevInst);
break;
}
} while(1);
}
void backTrackOperand(Value* operand, Instruction* I) {
Instruction* prevInst = I;
do {
prevInst = prev(prevInst);
if(prevInst->usesOperand(operand)) {
...
}
}
}
So how could I implement the function getCondition()
, computesCondition()
, and usesOperand()
?