void fsm_transition(int& currentState, int newState)
{
#ifdef _DEBUG
std::cerr << "Transition from " << currentState << " to " << newState << std::endl;
#endif
currentState = newState;
}
// Example usage of the FSM_TRANSITION macro
int main()
{
int currentState = 0;
while(currentState < 10){
//FSM_TRANSITION(currentState);
fsm_transition(currentState, currentState + 1);
std::cout << "Current state is: " << currentState << std::endl;
if(currentState>0){
std::cout <<"Checking inside if condition\n";
}
currentState += 1;
}
return 0;
}
From this program, I just want to get the condition(currentState>0) from this program. I wrote a pass to get the conditions like following,
for (auto &F:M){
errs() << "Function: " << F.getName() << "\n";
for (BasicBlock &BB : F) {
for (Instruction &I : BB) {
if (ICmpInst *CI = dyn_cast<ICmpInst>(&I)) {
// Get the operands of the icmp instruction
Value *Op1 = CI->getOperand(0);
Value *Op2 = CI->getOperand(1);
errs()<<"Op1:"<<*Op1<<"\nOp2:"<<*Op2<<"\n";
StringRef Op1Name = Op1->hasName() ? Op1->getName() : "unnamed";
StringRef Op2Name = Op2->hasName() ? Op2->getName() : "unnamed";
// Print the name of the operands and the condition
errs() << "If Condition:\n\top1name: " << Op1Name << "\n\tcondition:" << CI->getPredicateName(CI->getPredicate())
<< "\n\top2name:" << Op2Name << "\n";
}
//if (BranchInst *BI = dyn_cast<BranchInst>(&I)) {
// if (BI->isConditional()) {
// errs()<<"Inside Branch Conditional\n";
// Value *condition = BI->getCondition();
// errs() << " If Condition: " << *condition << "\n";
// }
//}
}
}
}
I got the operands, the load instructions, you can see the result following,
Op1: %0 = load i32, i32* %currentState, align 4, !dbg !860
Op2:i32 10
If Condition:
op1name: unnamed
condition:slt
op2name:unnamed
Op1: %3 = load i32, i32* %currentState, align 4, !dbg !870
Op2:i32 0
If Condition:
op1name: unnamed
condition:sgt
But I am not getting the names, I don’t understand why it is unnamed. Is it possible to get the variable name from these instructions?