[LLVMdev] Phi translation

Daniel Berlin dberlin at dberlin.org
Thu Jul 19 21:35:20 PDT 2012


So, this will generally work, but you actually don't need to write
this code at all.
If you look in lib/Transforms/Utils/DemoteRegToStack.cpp, there is a
function called "DemotePHIToStack" that does exactly what you want to
do below.



On Wed, Jul 18, 2012 at 2:07 PM, ryan baird <ryanrbaird at gmail.com> wrote:
> I just accidently sent a partially complete email, so this contains the rest
> (sorry!)
> I'm working on translating llvm's optimized intermediate code to another
> compiler's intermediate code, and I'm working on the PHI instruction.
>
> Here's an example phi instruction to help explain what I'm trying to do:
> %inc25 = phi i32 [ 1, %entry ], [ %inc, %for.body ]
>
> What I would want to do here is allocate some memory memory (i'm trying to
> use %phi1 = alloca  i32, allign 4 ). Then, I want to go to the %entry block
> and say "store i32  1, i32* %phi1", and in the %for.body I want to add
> "store i32  1, i32* %phi1".
>
> So first, I'm iterating through my function and basicblock:
>     int phi = 0;
>     Function::iterator func_bblock_itr = func->begin();
>     for(; func_bblock_itr != func->end(); ++func_bblock_itr)
>     {
>         BasicBlock::iterator bblock_inst_itr = func_bblock_itr->begin();
>         for(; bblock_inst_itr != func_bblock_itr->end(); ++bblock_inst_itr)
>         {
> Then, I look for PHI instructions:
>             if(bblock_inst_itr->getOpcode() == Instruction::PHI) {
> If I find one, I allocate space:
>                 string name = "phi";
>                 name.insert(3,to_string(phi));
>                 Twine tname= Twine(name);
>                 AllocaInst alloca =
> AllocaInst(bblock_inst_itr->getOperand(0)->getType(),tname,func_bblock_itr->begin());
> and then i go through the operands one by one to find where to put store
> instructions:
>                 for(unsigned int i = 0; i <
> bblock_inst_itr->getNumOperands();i++) {
>                     Value *v = bblock_inst_itr->getOperand(i);
> Here's where I don't know what to do: I want to create a store instruction
> in that place:
>                     StoreInst(v, alloca?, func_bblock_itr?);
>                }
>           }
> }
>
> Is this a decent approach to getting the instructions inserted? and what do
> I put to get the pointer in the store instruction and the basicblock, the
> current code gives me a bunch of errors.  Any help would be appreciated,
> thanks!
>
> _______________________________________________
> 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