I want to insert a zero extension instruction and a multiply instruction to a basic block. The input is,
define void @DriverInit() {
EntryBlock:
%abc = call i32 @cuInit(i32 0)
ret void
}
I want to transform it to,
define void @DriverInit() {
EntryBlock:
%abc = call i32 @cuInit(i32 0)
%2 = zext i32 1 to i64
%3 = mul i64 %2, ptrtoint (i1** getelementptr (i1** null, i32 1) to i64)
ret void
}
How to do it using LLVM C++ APIs? I use the below code to create the zero extension instruction, but i am unable to do it.
IRBuilder<> builder(BB);
Value *One = builder.getInt32(1);
Value *zer=builder.CreateZExt(One, IntegerType::getInt64Ty(M.getContext()),"1");
The second argument to CreateZExt is the destination type to which I want to zero extend,correct me if I am wrong.
I am a beginner in LLVM and find it difficult to get info on what functions to use in the passes. What resources are available except the doxygen documentation for the source code?
Once you have some experience with LLVM, you know where to look in the code. Until you gain that experience, you can use the C++ backend to generate API calls equivalent to the given IR for you.
One way to do that is to compile the IR with
llcusing the C++ backend. For example, I take this simplified IR:Save it to a file named
z.lland run:You need to have LLVM installed or built somewhere to have access to
llc. It producesz.cppwhich has the C++ API calls to create the whole module. The relevant part for theEntryBlockbasic block is:In which you see how to use the
zExtInstrconstructor and later theBinaryOperator::Createcall correctly to generate that IR.