[PATCH] D35294: [CGP] Allow cycles during Phi traversal in OptimizaMemoryInst

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 12 02:11:58 PDT 2017


skatkov created this revision.

Allowing cycles in Phi traversal increases the scope of optimize memory instruction
in case we are in loop.

The added test shows an example of enabling optimization inside a loop.


https://reviews.llvm.org/D35294

Files:
  lib/CodeGen/CodeGenPrepare.cpp
  test/Transforms/CodeGenPrepare/X86/sink-addrmode.ll


Index: test/Transforms/CodeGenPrepare/X86/sink-addrmode.ll
===================================================================
--- test/Transforms/CodeGenPrepare/X86/sink-addrmode.ll
+++ test/Transforms/CodeGenPrepare/X86/sink-addrmode.ll
@@ -192,5 +192,38 @@
   br label %fallthrough
 }
 
-
 declare void @slowpath(i32, i32*)
+
+; Make sure we can sink address computation even
+; if there is a cycle in phi nodes.
+define void @test8(i1 %cond, i64* %base) {
+; CHECK-LABEL: @test8
+entry:
+  %addr = getelementptr inbounds i64, i64* %base, i64 5
+  %casted = bitcast i64* %addr to i32*
+  br label %header
+
+header:
+  %iv = phi i32 [0, %entry], [%iv.inc, %backedge]
+  %casted.loop = phi i32* [%casted, %entry], [%casted.merged, %backedge]
+  br i1 %cond, label %if.then, label %backedge
+
+if.then:
+  call void @foo(i32 %iv)
+  %addr.1 = getelementptr inbounds i64, i64* %base, i64 5
+  %casted.1 = bitcast i64* %addr.1 to i32*
+  br label %backedge
+
+backedge:
+; CHECK-LABEL: backedge:
+; CHECK: getelementptr i8, {{.+}} 40
+  %casted.merged = phi i32* [%casted.loop, %header], [%casted.1, %if.then]
+  %v = load i32, i32* %casted.merged, align 4
+  call void @foo(i32 %v)
+  %iv.inc = add i32 %iv, 1
+  %cmp = icmp slt i32 %iv.inc, 1000
+  br i1 %cmp, label %header, label %exit
+
+exit:
+  ret void
+}
Index: lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- lib/CodeGen/CodeGenPrepare.cpp
+++ lib/CodeGen/CodeGenPrepare.cpp
@@ -4281,10 +4281,8 @@
     worklist.pop_back();
 
     // Break use-def graph loops.
-    if (!Visited.insert(V).second) {
-      Consensus = nullptr;
-      break;
-    }
+    if (!Visited.insert(V).second)
+      continue;
 
     // For a PHI node, push all of its incoming values.
     if (PHINode *P = dyn_cast<PHINode>(V)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35294.106153.patch
Type: text/x-patch
Size: 1820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170712/6b7b6e34/attachment.bin>


More information about the llvm-commits mailing list