[LLVMdev] Phi translation (original) (raw)

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 funcbblockitr = func->begin(); for(; funcbblockitr != func->end(); ++funcbblockitr) { BasicBlock::iterator bblockinstitr = funcbblockitr->begin(); for(; bblockinstitr != funcbblockitr->end(); ++bblockinstitr) { Then, I look for PHI instructions: if(bblockinstitr->getOpcode() == Instruction::PHI) { If I find one, I allocate space: string name = "phi"; name.insert(3,tostring(phi)); Twine tname= Twine(name); AllocaInst alloca = AllocaInst(bblockinstitr->getOperand(0)->getType(),tname,funcbblockitr->begin()); and then i go through the operands one by one to find where to put store instructions: for(unsigned int i = 0; i <_ _bblockinstitr->getNumOperands();i++) { Value *v = bblockinstitr->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?, funcbblockitr?); } } } 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