(no subject)

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

If you want to eliminate phi instructions, you can try to just use the reg2mem pass. This will do what you’re trying to accomplish here.