我有一个IR,看起来像这样:
%5=icmp eq i32 %4,0
我想检查icmp指令的第二个操作数是否为0或其他。我使用了getOperand(1)
,它以Value *
格式返回结果。如何将它与常数0进行比较?
答案 0 :(得分:4)
以下是您可以运行的示例传递来找到它:
class DetectZeroValuePass : public FunctionPass {
public:
static char ID;
DetectZeroValuePass()
: FunctionPass(ID)
{}
virtual bool runOnFunction(Function &F) {
for (Function::iterator bb = F.begin(), bb_e = F.end(); bb != bb_e; ++bb) {
for (BasicBlock::iterator ii = bb->begin(), ii_e = bb->end(); ii != ii_e; ++ii) {
if (CmpInst *cmpInst = dyn_cast<CmpInst>(&*ii)) {
handle_cmp(cmpInst);
}
}
}
}
void handle_cmp(CmpInst *cmpInst) {
// Detect cmp instructions with the second operand being 0
if (cmpInst->getNumOperands() >= 2) {
Value *secondOperand = cmpInst->getOperand(1);
if (ConstantInt *CI = dyn_cast<ConstantInt>(secondOperand))
if (CI->isZero()) {
errs() << "In the following instruction, second operand is 0\n";
cmpInst->dump();
}
}
}
};