[LLVMdev] How do I insert a printf call in the IR?

Shah, Mrunal J mrunal.shah at gatech.edu
Sat Nov 15 18:18:52 PST 2008


I figured it!

Instead of creating the function, I use module.getFunction if it already exists.
Or I could use getOrInsertFunction().

I did it and it worked.

Thanks again!
Mrunal

----- Original Message -----
From: "Mrunal J Shah" <mrunal.shah at gatech.edu>
To: "LLVM Developers Mailing List" <llvmdev at cs.uiuc.edu>
Sent: Saturday, November 15, 2008 9:10:20 PM GMT -05:00 US/Canada Eastern
Subject: Re: [LLVMdev] How do I insert a printf call in the IR?

Thanks a lot Nick

-march=cpp was very helpful.
But I still have a small problem, I am trying to insert a printf in a transformation pass. 
So when the original program already has a printf, on executing the transformation pass, it tries to create function "printf1" instead of "printf"

Am I missing something here?

Thanks again!
Mrunal

----- Original Message -----
From: "Nick Lewycky" <nicholas at mxc.ca>
To: "LLVM Developers Mailing List" <llvmdev at cs.uiuc.edu>
Sent: Saturday, November 15, 2008 7:49:09 PM GMT -05:00 US/Canada Eastern
Subject: Re: [LLVMdev] How do I insert a printf call in the IR?

Shah, Mrunal J wrote:
> Once I have the function
> to make a call to this function I use
> 
> CallInst::Create(myPrint, args.begin(), args.end(),"", B);
> 
> But I am not being able to pass a string into the argument vector.
> should I define args as
> std::vector<const Type*> args;
> or
> std::vector<const PointerType*> args;
> or 
> std::vector<Value*> args;
> 
> and how do I convert a constant string to either of these types, so that I can push it on the args list.

You need to put the string constant into a GlobalVariable (which is a 
Value).

This is a bit tricky if you're using the direct API. I suggest using 
"llc -march=cpp" on a program that does printf("Hello world!");

Using IRBuilder is easier. Starting with LLVM 2.4, there's a 
CreateGlobalString function that takes a const char *String and returns 
a Value*.

Nick

> Thanks!
> Mrunal
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
> 

_______________________________________________
LLVM Developers mailing list
LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
_______________________________________________
LLVM Developers mailing list
LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev



More information about the llvm-dev mailing list