A few beginner level questions..

1. If we run a few passes in a sequence ..is the bytecode file
transformed after each pass in sequence i.e

if we have
a) opt -pass1 -pass2 -pass3 < in.bc > out.bc
b)opt -pass1 -pass2 < in.bc > tmp.bc
  opt -pass3 < tmp.bc > out.bc

are the above two equivalent ?
what I basically want is to run my pass on an optimised bytecode , so
should i optimize it and get a new bytecode file first or I can just
put my pass at the end of all the other optimization passes ?

2. For an application , for every instruction if its a call
instruction I try to print out the called function as below ..

void pass01a::instruct_show(Instruction* I){
   if ( isa<CallInst>(*I) ){
       const CallInst *CI = cast<CallInst>(I);
       const Function *Func = CI->getCalledFunction() ;
       std::cerr<<":calledFunction:"<<Func->getName();
   }
}

Now it works fine for some application but for one of the benchmark in
MIBENCH , i am getting the following error ..

1. If we run a few passes in a sequence ..is the bytecode file
transformed after each pass in sequence i.e

if we have
a) opt -pass1 -pass2 -pass3 < in.bc > out.bc
b)opt -pass1 -pass2 < in.bc > tmp.bc
  opt -pass3 < tmp.bc > out.bc

are the above two equivalent ?

Yes, they're equivalent.

what I basically want is to run my pass on an optimised bytecode , so
should i optimize it and get a new bytecode file first or I can just
put my pass at the end of all the other optimization passes ?

Just put yours at the end. You'll need to create a loadable module
(shared library/dll) and use the --load option to op.

2. For an application , for every instruction if its a call
instruction I try to print out the called function as below ..

void pass01a::instruct_show(Instruction* I){
   if ( isa<CallInst>(*I) ){
       const CallInst *CI = cast<CallInst>(I);
       const Function *Func = CI->getCalledFunction() ;
       std::cerr<<":calledFunction:"<<Func->getName();
   }
}

Now it works fine for some application but for one of the benchmark in
MIBENCH , i am getting the following error ..

++++++++++++++++++++++++
:calledFunction:opt((anonymous namespace)::PrintStackTrace()+0x1a)[0x86b74e6]
opt((anonymous namespace)::SignalHandler(int)+0xcb)[0x86b7759]
/lib/libc.so.6[0x401532b8]
/home/ary/work/llvm/lib/Debug/libpass01.so((anonymous
namespace)::pass01a::instruct_show(llvm::Instruction*)+0x281)[0x4001a425]
/home/ary/work/llvm/lib/Debug/libpass01.so((anonymous
namespace)::pass01a::runOnFunction(llvm::Function&)+0x18c)[0x4001b57c]
opt(llvm::PassManagerTraits<llvm::Function>::runPass(llvm::FunctionPass*,
llvm::Function*)+0x1f)[0x8679cbf]
opt(llvm::PassManagerT<llvm::Function>::runOnUnit(llvm::Function*)+0x5d3)[0x8675117]
opt(llvm::PassManagerTraits<llvm::Function>::runOnFunction(llvm::Function&)+0x1f)[0x8672f1d]
opt(llvm::FunctionPass::runOnModule(llvm::Module&)+0xa7)[0x862234b]
opt(llvm::PassManagerTraits<llvm::Module>::runPass(llvm::ModulePass*,
llvm::Module*)+0x1f)[0x8679d55]
opt(llvm::PassManagerT<llvm::Module>::runOnUnit(llvm::Module*)+0x5d3)[0x8673653]
opt(llvm::PassManagerTraits<llvm::Module>::runOnModule(llvm::Module&)+0x1f)[0x8672ce1]
opt(llvm::PassManager::run(llvm::Module&)+0x23)[0x862174d]
opt(main+0x913)[0x83c0bab]
/lib/libc.so.6(__libc_start_main+0xcb)[0x4013f90b]
opt(dlopen+0x41)[0x83c0201]
Segmentation fault
+++++++++++++++++++++++++++

Is there something wrong with my coding or llvm itself .? Is there any
simpler way to extract the function names?

Based on the above, it looks like "Func" is null. I'm not sure why the
getCalledFunction would return a null, but it looks like it is. Possibly
some transformation invalidated it? What happens in runOnFunction before
you call instruct_show?

Reid.

In a debugger like gdb, you can use the function ->dump() on any Value*
to print it out to std::err, so with some breakpoints, you could look at
what CI is, and if it's OK (i.e., valid), step to the next statement and
look at what Func is, before you call Func->getName().

As Reid pointed out, it could be that CI or Func is null or invalid, it
could be your bug if you have modified them somehow, or if it is a bug
in an LLVM pass, please narrow it down (using bugpoint) and file it as
such.

> 2. For an application , for every instruction if its a call
> instruction I try to print out the called function as below ..
>
> void pass01a::instruct_show(Instruction* I){
> if ( isa<CallInst>(*I) ){
> const CallInst *CI = cast<CallInst>(I);
> const Function *Func = CI->getCalledFunction() ;
> std::cerr<<":calledFunction:"<<Func->getName();
> }
> }

As a general comment, the above could be rewritten like this:

void pass01a::instruct_show(Instruction* I){
    if (const CallInst *CI = dyn_cast<CallInst>(*I) ){
        const Function *Func = CI->getCalledFunction() ;
        std::cerr<<":calledFunction:"<<Func->getName();
    }

... saving you a line of code ...

> Now it works fine for some application but for one of the benchmark in
> MIBENCH , i am getting the following error .. Is there something wrong
> with my coding or llvm itself .? Is there any simpler way to extract
> the function names?

There is a problem with your code, though I don't blame you. I've updated
the comment on the getCalledFunction() method to read this:

  /// getCalledFunction - Return the function being called by this instruction
  /// if it is a direct call. If it is a call through a function pointer,
  /// return null.

I hope that helps,

-Chris