0
比较IR操作我有一个IR看起来像这样:具有恒定的LLVM
%5=icmp eq i32 %4,0
我要检查,如果ICMP指令的第二个操作数为0或别的东西。我使用了getOperand(1)
,它返回格式为Value *
的结果。我怎样才能比较它与常数0?
比较IR操作我有一个IR看起来像这样:具有恒定的LLVM
%5=icmp eq i32 %4,0
我要检查,如果ICMP指令的第二个操作数为0或别的东西。我使用了getOperand(1)
,它返回格式为Value *
的结果。我怎样才能比较它与常数0?
这里有一个抽样合格可以运行发现了这一点:
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();
}
}
}
};
专为0,则是'val-> isZero()',多一点棘手的其他常量。我建议使用'match',参见InstructionSimplify.cpp了解大量示例。 – 2012-02-14 11:59:24
@ SK逻辑:'Value'本身没有'isZero',这是'ConstantInt'的一种方法。查看我的答案获取更多详情 – 2012-02-14 13:59:17
@EliBendersky,当然,向ConstantInt提供动态演员是非常成功的。 – 2012-02-14 14:26:22