PHINode in entry block

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

PHINode in entry block

William Moses
All,

Consider the case where the entry block to a function acts like a loop (e.g. it ends with a conditional break to itself and somewhere else). How would one create a PHINode (representing an index perhaps) which has a constant index (say 0) when entering the function, and (oldval+1) when looping.

I understand how to do this if the loop were not in the entry block (by simply using node->addIncoming(ConstantInt(...), entry), node->addIncoming(builder.CreateAdd(node,...),loopBlock) ).

My question is how would one do this using the C++ API when there is no predecessor since it is the entry block.

Thanks,
Billy Moses

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: PHINode in entry block

Tim Northover-2
Hi,

On 15 July 2014 15:55, William Moses <[hidden email]> wrote:
> Consider the case where the entry block to a function acts like a loop (e.g.
> it ends with a conditional break to itself and somewhere else). How would
> one create a PHINode (representing an index perhaps) which has a constant
> index (say 0) when entering the function, and (oldval+1) when looping.

I don't believe you can. The entry block isn't allowed to be part of a
loop, neatly sidestepping the question. Usually people just put an
unconditional branch to the real loop start when it matters.

Cheers.

Tim.
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: PHINode in entry block

John Criswell-5
In reply to this post by William Moses
On 7/15/14, 9:55 AM, William Moses wrote:
All,

Consider the case where the entry block to a function acts like a loop (e.g. it ends with a conditional break to itself and somewhere else). How would one create a PHINode (representing an index perhaps) which has a constant index (say 0) when entering the function, and (oldval+1) when looping.

I understand how to do this if the loop were not in the entry block (by simply using node->addIncoming(ConstantInt(...), entry), node->addIncoming(builder.CreateAdd(node,...),loopBlock) ).

My question is how would one do this using the C++ API when there is no predecessor since it is the entry block.

An entry block cannot have any phi-nodes.  If you have a function that starts with a loop, then your entry block will be a single unconditional branch to a basic block which is the first basic block of the loop.

Regards,

John Criswell


Thanks,
Billy Moses


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: PHINode in entry block

Duncan Sands
In reply to this post by William Moses
Hi William,

On 15/07/14 16:55, William Moses wrote:

> All,
>
> Consider the case where the entry block to a function acts like a loop (e.g. it
> ends with a conditional break to itself and somewhere else). How would one
> create a PHINode (representing an index perhaps) which has a constant index (say
> 0) when entering the function, and (oldval+1) when looping.
>
> I understand how to do this if the loop were not in the entry block (by simply
> using node->addIncoming(ConstantInt(...), entry),
> node->addIncoming(builder.CreateAdd(node,...),loopBlock) ).
>
> My question is how would one do this using the C++ API when there is no
> predecessor since it is the entry block.

phi nodes are not allowed in the entry block.  You need to create a block to put
the phi node in, and have the entry block just be a branch to this block.

Ciao, Duncan.

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: PHINode in entry block

Caldarale, Charles R
In reply to this post by William Moses
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of William Moses
> Subject: [LLVMdev] PHINode in entry block

> Consider the case where the entry block to a function acts like a loop (e.g. it
> ends with a conditional break to itself and somewhere else). How would one create
> a PHINode (representing an index perhaps) which has a constant index (say 0) when
> entering the function, and (oldval+1) when looping.

> My question is how would one do this using the C++ API when there is no predecessor
> since it is the entry block.

In this case you need to create an essentially empty entry block as a predecessor containing nothing but a branch to the starting block of the loop; its sole purpose is to provide a block for the PHI node to reference.

 - Chuck


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev