inserting a fucntion call at the end of basic bloc

hi all,

i would like insert a fucntion call at the end of each basic bloc
the fucntion i have defined and declared at the begining of the module
ie i have written a pass derived from a module pass
virtual bool runOnModule(Module & M){
// i declared the fucntion and defined it
for (Module::iterator a = M.begin(), b = M.end(); a != b;
for (Function::iterator i = a->begin(), e = a->end(); i != e; ++i) {

//call the function
}
}

}
i tried to see an example with the demo, i saw that it instead of calling the function, it repeats all the instruction that are existing in the function
Any help?

Hi Nabila,

i would like insert a fucntion call at the end of each basic bloc

you can't, because only terminators are allowed at the end of a basic block.
However you can try to insert the call before the terminator.

...

i tried to see an example with the demo, i saw that it instead of calling the
function, it repeats all the instruction that are existing in the function
Any help?

Turn off optimization in the demo (optimization level "None").

Ciao, Duncan.

2011/4/25 Duncan Sands <baldrick@free.fr>

Hi Nabila,

i would like insert a fucntion call at the end of each basic bloc

you can’t, because only terminators are allowed at the end of a basic block.
However you can try to insert the call before the terminator.

Yes, i mean before the termininator,
My problem is how to call a method
suppose this fucntion
void A(int x)
{
x=x+1;

}

should i define this function and declare it at the beginig of the module and create for it a basic bloc?
and then how to call it before each basic bloc terminitor

Hi Nabila,

My problem is how to call a method
suppose this fucntion
void A(int x)
{
   x=x+1;

}

should i define this function and declare it at the beginig of the module and
create for it a basic bloc?

you can just declare the function (i.e. no need to give it a body), and call it.
You can then link with an object file that defines it. This is simpler than
injecting the function into each module (though that is easy to do too).

and then how to call it before each basic bloc terminitor

Use an IRBuilder. Pass the basic block terminator to SetInsertPoint. Use one
of the CreateCall IRBuilder methods to insert a call instruction.

Ciao, Duncan.

I have defined the fucntion in another object file and linked it to the
in fact the fucntion is :
void consume(int , int * );

std::vector<Value*> int32_16_params;
int32_16_params.push_back(inValue);//inValue is ConstantInt* inValue
int32_16_params.push_back(gvar_int32_y);

CallInst* int32_16 = CallInst::Create(func_consume, int32_16_params.begin(), int32_16_params.end(), “”, ii);

compilation without errors
Now when i tried this pass an error says:
Wrong type for attribute noalias
tail call void @consume(i32 noalias 3, i32* @y) nounwind

Wrong type for attribute noalias
tail call void @consume(i32 noalias 3, i32* @y) nounwind
Broken module found, compilation aborted!
0 libLLVM-2.8.so.1 0x019bc628
Stack dump:
0. Program arguments: opt -load /home/inspiron/PhdWork/llvm-2.8/Release/lib/Example.so -Example2

  1. Running pass ‘Function Pass Manager’ on module ‘’.
  2. Running pass ‘Module Verifier’ on function ‘@main
    Aborted

Any help?

2011/4/25 Duncan Sands <baldrick@free.fr>

Hi Nabila,

Now when i tried this pass an error says:
Wrong type for attribute noalias
tail call void @consume(i32 noalias 3, i32* @y) nounwind

noalias is only for arguments of pointer type. You probably meant it to
be on the second argument rather than the first. I suggest you correct
your code that adds the attribute.

Ciao, Duncan.

2011/4/26 Duncan Sands <baldrick@free.fr>

Hi Nabila,

Now when i tried this pass an error says:
Wrong type for attribute noalias
tail call void @consume(i32 noalias 3, i32* @y) nounwind

noalias is only for arguments of pointer type. You probably meant it to
be on the second argument rather than the first. I suggest you correct
your code that adds the attribute.

Ciao, Duncan.

no my fucntion is
void consume(int nb_into_bloc, int nb_total)
the second is a pointer
here is how I pass the parameters
Instruction
ii = i->getTerminator();
const int n = cast (i->size());

ConstantInt* inValue = ConstantInt::get(Type::getInt32Ty(Context), n);

std::vector<Value*> int32_16_params;
int32_16_params.push_back(inValue);
int32_16_params.push_back(gvar_int32_y);

CallInst* int32_16 = CallInst::Create(func_consume, int32_16_params.begin(), int32_16_params.end(), “”, ii);

Hi Nabila,

        Now when i tried this pass an error says:
        Wrong type for attribute noalias
        tail call void @consume(i32 noalias 3, i32* @y) nounwind

    noalias is only for arguments of pointer type. You probably meant it to
    be on the second argument rather than the first. I suggest you correct
    your code that adds the attribute.

    Ciao, Duncan.

no my fucntion is
void consume(int nb_into_bloc, int *nb_total)
the second is a pointer
here is how I pass the parameters
  Instruction* ii = i->getTerminator();
  const int n = cast <int> (i->size());
  ConstantInt* inValue = ConstantInt::get(Type::getInt32Ty(Context), n);
  std::vector<Value*> int32_16_params;
  int32_16_params.push_back(inValue);
  int32_16_params.push_back(gvar_int32_y);
  CallInst* int32_16 = CallInst::Create(func_consume, int32_16_params.begin(),
int32_16_params.end(), "", ii);

where did the "noalias" attribute and "tail call" (rather than "call") come
from? Are you setting these yourself or running some optimization pass after
your pass?

Ciao, Duncan.

2011/4/26 Duncan Sands <baldrick@free.fr>

Hi Nabila,

Now when i tried this pass an error says:
Wrong type for attribute noalias
tail call void @consume(i32 noalias 3, i32* @y) nounwind

noalias is only for arguments of pointer type. You probably meant it to
be on the second argument rather than the first. I suggest you correct
your code that adds the attribute.

Ciao, Duncan.

no my fucntion is
void consume(int nb_into_bloc, int nb_total)
the second is a pointer
here is how I pass the parameters
Instruction
ii = i->getTerminator();
const int n = cast (i->size());
ConstantInt* inValue = ConstantInt::get(Type::getInt32Ty(Context), n);
std::vector<Value*> int32_16_params;
int32_16_params.push_back(inValue);
int32_16_params.push_back(gvar_int32_y);
CallInst* int32_16 = CallInst::Create(func_consume, int32_16_params.begin(),
int32_16_params.end(), “”, ii);

where did the “noalias” attribute and “tail call” (rather than “call”) come
from? Are you setting these yourself or running some optimization pass after
your pass?

i have written a module pass and i have compiled it and then

opt -load /home/inspiron/PhdWork/llvm-2.8/Release/lib/Example.so -Example2 < hello.bc > /dev/null

i get the error message

Wrong type for attribute noalias
tail call void @consume(i32 noalias 3, i32* @y) nounwind
Broken module found, compilation aborted!
0 libLLVM-2.8.so.1 0x019bc628
Stack dump:
0. Program arguments: opt -load /home/inspiron/PhdWork/llvm-2.8/Release/lib/Example.so -Example2

  1. Running pass ‘Function Pass Manager’ on module ‘’.
  2. Running pass ‘Module Verifier’ on function ‘@main
    Aborted

Hi Nabila,

    where did the "noalias" attribute and "tail call" (rather than "call") come
    from? Are you setting these yourself or running some optimization pass after
    your pass?

i have written a module pass and i have compiled it and then

you didn't answer my questions, you just repeated what you said before.

Ciao, Duncan.

2011/4/26 Duncan Sands <baldrick@free.fr>

Hi Nabila,

where did the “noalias” attribute and “tail call” (rather than “call”) come
from? Are you setting these yourself or running some optimization pass after
your pass?

i have written a module pass and i have compiled it and then

you didn’t answer my questions, you just repeated what you said before.

Ciao, Duncan.

opt -load /home/inspiron/PhdWork/llvm-2.8/Release/lib/Example.so -Example2 <
hello.bc > /dev/null
i get the error message
Wrong type for attribute noalias
tail call void @consume(i32 noalias 3, i32* @y) nounwind
Broken module found, compilation aborted!
0 libLLVM-2.8.so.1 0x019bc628
Stack dump:
0.Program arguments: opt -load
/home/inspiron/PhdWork/llvm-2.8/Release/lib/Example.so -Example2
1.Running pass ‘Function Pass Manager’ on module ‘’.
2.Running pass ‘Module Verifier’ on function ‘@main
Aborted

No, i’m not setting them by my self

and are you running any optimization passes? Another possibility is that the
the problem was present already in hello.bc. Did you check that hello.bc passes
the verifier? Did you build LLVM with assertions enabled (you should)? How did
you add the declaration of “consume” to the module? Finally, try running under
valgrind.

no I do not use any optimisation pass only my pass
hello.bc works and i have check it with lli hello.bc
no without assertion enabled
i don’t want to modify the original hello.bc
if would like to apply the changes found in the pass i just put instead of this
opt -load /home/inspiron/PhdWork/llvm-2.8/Release/lib/Example.so -Example2 < hello.bc > /dev/null
i put this
opt -load /home/inspiron/PhdWork/llvm-2.8/Release/lib/Example.so -Example2 < hello.bc > hello2.bc

the declaration is:

PointerType* PointerTy_0 = PointerType::get(IntegerType::get(M.getContext(), 32), 0);

std::vector<const Type*>FuncTy_3_args;
FuncTy_3_args.push_back(IntegerType::get(M.getContext(), 32));
FuncTy_3_args.push_back(PointerTy_0);
FunctionType* FuncTy_3 = FunctionType::get(
/Result=/Type::getVoidTy(M.getContext()),
/Params=/FuncTy_3_args,
/isVarArg=/false);

Constant consum = M.getOrInsertFunction(“consume”, FuncTy_3);
llvm::Function
func_consume = llvm::cast<llvm::Function > (consum);
func_consume ->setLinkage(GlobalValue::ExternalLinkage);
func_consume->setCallingConv(CallingConv::C);
AttrListPtr func_consume_PAL;
func_consume->setAttributes(func_consume_PAL);

i have tried to modify the method consume so that it is
void consume(int*)
and chaged the declaration also
and it works
now when i have modified the method consume like this void consume(int)
and modified the declaration
it doesn’t work

I don’t know valgrid:(

Sorry I always forget to clic the reply all :slight_smile:

Hi Nabila,

hello.bc works and i have check it with lli hello.bc

you should check it with "opt -verify hello.bc -disable-output" Running
lli is not sufficient if assertions are disabled.

no without assertion enabled

You should build LLVM with assertions enabled. This will almost certainly
instantly find the problem.

             AttrListPtr func_consume_PAL;
             func_consume->setAttributes(func_consume_PAL);

There is no point in setting attributes if you don't want any. That said,
it should be harmless. What I don't understand is how your call is getting
both the "nounwind" and "noalias" attributes when you say you don't set these
anywhere; and why it is marked as being a tail call when you say you didn't
set the tail call flag or run any optimization passes.

i have tried to modify the method consume so that it is
void consume(int*)
and chaged the declaration also
and it works
now when i have modified the method consume like this void consume(int)
and modified the declaration
it doesn't work

I don't know valgrid:(

Google it.

Ciao, Duncan.