[LLVMdev] Loop IR insertion

Reid Spencer reid at x10sys.com
Wed Jan 12 06:52:08 PST 2005


On Tue, 2005-01-11 at 07:56, Qiuyu Zhang wrote:
> Hi,
>  
> I am trying to insert a Loop IR into the existed bytecode file. 
>  
> insertion part by C code,
>  
> char *p[n]; // pointer array for storing the address of strings
> int i;
> for(i=0;i<n;i++){
>  
> (p[i])[2] = (p[i])[2] ^ 0x27;
>  
> }
>  
> My questions are 
>  
> 1. for local variable 'char *p[n]' , it is represented by IR as alloca
> ...., so could I insert local variable (pointer array) directly as
> same way as insert global variable or  I have to use new allocaInst to
> construct it.

If n is constant and you know its value then you can use alloca.
Otherwise you need to use malloc to allocate the memory for p.

>  
> 2. from instructions.h, there are some instructions class, but could I
> insert Xor instruction? or could I insert all IR instruction as I
> want? if so, would you like to give me some hints or examples.

Yes, you can insert any instructions you like. Typically the way to do
this is with a basic block so they go at the end of the basic block.
There are lots of static methods in the instruction classes to help you
do this. 

For example, look at the examples/HowToUseJIT.cpp example. Here's a
snippet from that program:

 // because of the last argument.
  BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
                                                                                
  // Get pointers to the constant `1'.
  Value *One = ConstantSInt::get(Type::IntTy, 1);
                                                                                
  // Get pointers to the integer argument of the add1 function...
  assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
  Argument *ArgX = Add1F->abegin();  // Get the arg
  ArgX->setName("AnArg");            // Give it a nice symbolic name for fun.
                                                                                
  // Create the add instruction, inserting it into the end of BB.
  Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
                                                                                
  // Create the return instruction and add it to the basic block
  new ReturnInst(Add, BB);


Note that the last two lines show two different ways of creating
instructions: one with the static BinaryOperator::createAdd instruction,
the other directly allocating  the instruction. In both cases the
BasicBlock, BB, is the last parameter which says "put the instruction at
the end of the basic block BB"

>  
> Thanks
>  
Hope this helps.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20050112/3f2d5de8/attachment.sig>


More information about the llvm-dev mailing list