[LLVMdev] Question about insert function or instruction.

Qiu Zhang fengy219 at yahoo.com
Wed May 19 15:11:03 PDT 2004

Hi llvmer,

I am trying to learn how to use llvm. My question is
described as the following,
there has already a testcase code, and I get bytecode
by using llvmgcc. What I want to do is to insert a
call funcation into each basic block, which is for
statistic some information. 
1) I implement call function in another c/cpp file and
can I insert the external call function to existed
bytecode ? if it is valid. Can you give me a simple
example to show how to do it or just give me a url
which can show how to do it? 

2) If I'd like to insert the function in the same
bytecode, how to do it?

3) for opt commamnd, there have -trace -tracem options
,how can I see the detailed direction to use it.

I went through a LowerInvoke.cpp and EdgeCode.cpp file
which have examples to do what I want. Although I can
learn the main flow how to insert a call funcation or
instruction, but I still cannot implement it. Shame.
Another thing is that I am not sure the method I used
is correct or not? Can you give me some suggestion?

Thanks in advance for any response.


The following code is what I do
#include "llvm/Pass.h"
#include "llvm/Function.h"
#include "llvm/BasicBlock.h"
#include "llvm/Instruction.h"
#include "llvm/iOther.h"

namespace llvm {

  struct cntPass : public FunctionPass{
    virtual bool runOnFunction(Function &F){
      std::cerr <<"funtion name : "<<
      for(Function::iterator BB = F.begin(),E=F.end();
BB != E;++BB){
	std::cerr<< "BB name : "<<BB->getName()<<"\n";
      // here, the problem is how can I get external
function pointer.
      //Instruction *callInst = new
CallInst(pCallfunction, "","");

      return false;
    // can I  insert print() function?
    void print(){
      printf("Insert test\n");
  RegisterOpt<cntPass> X("cntPass", "insert printf

Do you Yahoo!?
SBC Yahoo! - Internet access at a great low price.

More information about the llvm-dev mailing list