[PATCH] D127628: [NewGVN][LoadCoercion][3/3] Replace load with a phi node

Konstantina Mitropoulou via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 13 03:04:46 PDT 2022


kmitropoulou created this revision.
Herald added subscribers: nlopes, hiraditya.
Herald added a project: All.
kmitropoulou requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In the following example, the load can either read the value 100 or the value

500. Therefore, it is straightforward that we can replace the load with a phi

node as it is shown below:

Example 1:
Before Load Coercion:

         BB1:                        BB2:
          store i32 100, i32* %P      store i32 500, i32* %P
  	                      \    /
  			      BB3:
  			       %V = load i32, i32* %P

After Load Coercion:

         BB1:                        BB2:
          store i32 100, i32* %P      store i32 500, i32* %P
  	                      \    /
  	                      BB3:
                                 %phi = phi i32 [ 100, %BB1], [ 500, %BB2 ]

In the following example, the load has only one dependency in BB1. To eliminate
the load we add an artifical dependency in BB2 (%V1) and we replace the load of
BB3 with a phi node. In the worst case scenario the load of BB2 (%V1) will be
executed as many times as the load in BB3.

Example 2:
Before Load Coercion:

         BB1:                        BB2:
          store i32 100, i32* %P      |
  	                      \    /
  			      BB3:
  			       %V = load i32, i32* %P

After Load Coercion:

         BB1:                        BB2:
          store i32 100, i32* %P      %V1 = load i32, i32* %P
  	                      \    /
  	                      BB3:
                                 %phi = phi i32 [ 100, %BB1], [ %V1, %BB2 ]

The algorithm has two main steps:

1. Collection of loads: We first check if the incoming values of the memory phis of the function can be used as the incoming values of the new phi node. In the following example, we can use the memory phi as a reference to create the incoming values of the new phi:

  Example 3: BB1:                        BB2: 1 = MemoryDef(liveOnEntry)  2 = MemoryDef(liveOnEntry) store i32 100, i32* %P      store i32 500, i32* %P \    / 	          BB3:

  		           3 = MemoryPhi({T,1},{F,2})
  			   %V = load i32, i32* %P
  
     Next, we check if there are any live-in loads that can be replaced by a phi
     node. In the following example, we can add a fake dependency in BB2. In this
     way, we can replace the load of BB3 with a phi node.
  
     Example 4:
     BB1:                              BB2:
      0 = MemoryDef(liveOnEntry)        |
      %V1 = load <2xi32>, <2xi32>* %P   |
                                \      /
          	             BB3:
  			      %V = load i32, i32* %P

2. Code generation: As it is shown in Examples 1 and 2, there are two code genearion scenarios: i. If the load has as many dependencies as its predecessors, then we just replace the load with a phi as it is shown in Example 1. ii. If one of the predecessors of the basic block of the load does not have a dependency, then we emit a fake dependency in this predecessor. Next, we can replace the load with a phi node as it is shown in Example 2.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127628

Files:
  llvm/lib/Transforms/Scalar/NewGVN.cpp
  llvm/test/Transforms/NewGVN/basic-cyclic-opt.ll
  llvm/test/Transforms/NewGVN/load_coercion_replace_load_with_phi.ll
  llvm/test/Transforms/NewGVN/pr31483.ll
  llvm/test/Transforms/NewGVN/pr31613.ll
  llvm/test/Transforms/NewGVN/pr32836.ll
  llvm/test/Transforms/NewGVN/pr32934.ll
  llvm/test/Transforms/NewGVN/refine-stores.ll
  llvm/test/Transforms/NewGVN/rle-nonlocal.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127628.436325.patch
Type: text/x-patch
Size: 50380 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220613/29da5a5a/attachment-0001.bin>


More information about the llvm-commits mailing list