[LLVMdev] strange code behavior when non-prototyped user function are called

Chuck Zhao czhao at eecg.toronto.edu
Wed Sep 29 17:21:00 PDT 2010


  I noticed a strange behavior when a non-prototyped user-defined 
function is called.

E.g. for the following test.c code segment:

void foo(void);
void bar(void);

void foo(void){
   bar();
}

void bar(void){
   printf("inside bar()");
}


LLVM-GCC 2.7 -O0 will generate the following code for foo(), which is 
all fine.

define void @foo() nounwind {
entry:
*  call void @bar() nounwind*
   br label %return

return:                                           ; preds = %entry
   ret void
}

However, if I comment out the first 2 lines (the prototype 
declarations), llvm-gcc will generate the following code for foo() instead:
define void @foo() nounwind {
entry:
   %0 = call i32 (...)* bitcast (void ()* @bar to i32 (...)*)() nounwind 
; <i32> [#uses=0]
   br label %return

return:                                           ; preds = %entry
   ret void
}

Notice there is now a bitcast operator embedded inside the call instruction.

The problem, however, is that the 2nd flavor of LLVM IR breaks my code, 
which tries to detect all Call Instructions inside all Functions:
...
     if (BitCastInst *BCI = dyn_cast<BitCastInst>(II)){
       errs() <<"find BitCastInst\n";
     ...
     }

     if (CallInst *CI = dyn_cast<CallInst>(II)){
       errs() <<"find CallInst\n";
     ...
     }
...

As a result, neither of the above two cases can trigger.

I am asking how I can detect all such function calls as CallInsts.

Thank you very much

Chuck







-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100929/837022d0/attachment.html>


More information about the llvm-dev mailing list