[llvm-dev] Problems in adding compare instruction after function call

archanaa@vt.edu via llvm-dev llvm-dev at lists.llvm.org
Fri Feb 28 09:52:06 PST 2020


Dear all,

I am trying to build a pass which compares the output of all function 
calls with a predefined value. I encountered two issues.

For example, the following IR

.....
%before = alloca i32, i32 55
%fooRet  = call i32 @foo(i32 10)
.....

should be transformed to the following IR after the pass

....
%before = alloca i32, i32 55
%fooRet = call i32 @foo(i32 10)
%check = icmp eq i32 %call, i32 %before
br i1 %check label %3, label %4
...

1) I am not able to add icmp instruction using IRbuilder after the 
function call instruction (I was able to add icmp instruction using 
IRbuilder after other instructions, e.g. mul instruction): I wrote a 
pass(snippet below) to do the above instrumentation which also adds 
other new instructions, running the pass with opt does not throw any 
error when there is a CreateIcmpEQ with two constant operands, but only 
the icmp instruction after the function call is not present in the 
instrumented IR.

2) How to pass the output of a function call to CreateIcmpEQ() as a 
argument? I understand that fooRet is the return value of my call 
instruction. When I use check = builder.CreateICmpEQ(call, newConst, 
"check"), I get a Type mismatch assert. /[ICmpInst::AssertOK(): 
Assertion 'getOperand(0)->getType() == getOperand(1)->getType() && "Both 
operands to Icmp instruction are not of the same type!"]/. I guess if I 
can make the types match, I will be able to pass the return value as 
argument. How can I achieve that?

A snippet of the pass:
if(CallInst *call = dyn_cast<CallInst>(&I))
{
     call->setName("fooRet");
     IRBuilder<> builder(call);
     Value* newConst = ConstantInt::get(Int32Ty,55);
     Value* before = builder.CreateAlloca(Int32Ty, newConst,"before");
     builder.SetInsertPoint(&bb, ++builder.GetInsertPoint());
*Value* check = builder.CreateICmpEQ(call, newConst, "check");*
     auto * branch = builder.CreateCondBr(check, true, false);

}

Any help is appreciated. Thank you.

Regards,
Archanaa

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200228/2ba19b4d/attachment.html>


More information about the llvm-dev mailing list