[LLVMdev] Any Optimization Suggestion to Get Rid of AddrSpaceCast around PHI

David Chisnall David.Chisnall at cl.cam.ac.uk
Sat Aug 23 12:55:53 PDT 2014


On 22 Aug 2014, at 18:33, Changpeng Fang <changpeng.fang at gmail.com> wrote:

> On Fri, Aug 22, 2014 at 1:11 AM, David Chisnall <David.Chisnall at cl.cam.ac.uk> wrote:
> It's fairly simple to find all of the casts and then walk down the use chains, identifying whether the pointers escape, and if they don't rewrite all of the instructions to use the other address space.  I've done this in an (extremely hacky) pass to allow allocas to be in an address space other than 0 (which I'm slowly replacing with much less hacky code).
> But I got lost when a PHI node (as in the test case) is encountered.

When a PHI node is encountered, the easiest thing to do is add a second one with the correct type.  Keep a list of them and whenever the original phi shows up as a user of something that you're rewriting, add the value that you're visiting to the new PHI.  If, at the end, you have any missing entries in the new phi node, then you visit the corresponding predecessor block and insert an address space cast of the value that the old phi expected and add this.

For most things, you can just use replaceUsesOfWith and just walk forward along the use chain of phi nodes ptrtoint-arithmetic-inttoptr sequences, and GEPs.  

David





More information about the llvm-dev mailing list