[PATCH] D49385: [IPSCCP] Run Solve each time we resolved an undef in a function.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 16 09:50:14 PDT 2018


fhahn created this revision.
fhahn added reviewers: efriedma, mssimpso, davide.

Once we resolved an undef in a function we can run Solve, which could
lead to finding a constant return value for the function, which in turn
could turn undefs into constants in other functions that call it, before
resolving undefs there.

Computationally the amount of work we are doing stays the same, just the
order we process things is slightly different and potentially there are
a few less undefs to resolve.

We are still relying on the order of functions in the IR, which means
depending on the order, we are able to resolve the optimal undef first
or not. For example, if @test1 comes before @testf, we find the constant
return value of @testf too late and we cannot use it while solving
@test1.

This on its own does not lead to more constants removed in the
test-suite, probably because currently we have to be very lucky to visit
applicable functions in the right order.

Maybe it would make sense to resolve undefs depending on the call graph,
e.g. leaf functions first, but I am not sure how/if that would be doable
in a lightweight fashion.


https://reviews.llvm.org/D49385

Files:
  lib/Transforms/Scalar/SCCP.cpp
  test/Transforms/IPConstantProp/solve-after-each-resolving-undefs-for-function.ll
  test/Transforms/SCCP/ipsccp-basic.ll


Index: test/Transforms/SCCP/ipsccp-basic.ll
===================================================================
--- test/Transforms/SCCP/ipsccp-basic.ll
+++ test/Transforms/SCCP/ipsccp-basic.ll
@@ -253,7 +253,7 @@
   ret void
 ; CHECK-LABEL: define void @test11b
 ; CHECK: %[[call1:.*]] = call i64 @test11a()
-; CHECK: %[[call2:.*]] = call i64 @llvm.ctpop.i64(i64 0)
+; CHECK-NOT: call i64 @llvm.ctpop.i64
 }
 
 declare i64 @llvm.ctpop.i64(i64)
Index: test/Transforms/IPConstantProp/solve-after-each-resolving-undefs-for-function.ll
===================================================================
--- /dev/null
+++ test/Transforms/IPConstantProp/solve-after-each-resolving-undefs-for-function.ll
@@ -0,0 +1,39 @@
+; RUN: opt < %s -S -ipsccp | FileCheck %s
+
+; We re-run the solver each time we resolved an undef in a function. This allows
+; us to find the constant return value of @testf before resolving undefs in
+; @test1.
+define internal i1 @testf() {
+; CHECK-LABEL: define internal i1 @testf(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[IF_END3:%.*]]
+; CHECK:       if.end3:
+; CHECK-NEXT:    ret i1 undef
+;
+entry:
+  br i1 undef, label %if.then1, label %if.end3
+
+if.then1:                                         ; preds = %if.end
+  br label %if.end3
+
+if.end3:                                          ; preds = %if.then1, %entry
+  ret i1 true
+}
+
+define void @test1() {
+; CHECK-LABEL: @test1(
+; CHECK-LABEL: if.then:
+; CHECK:         call i1 @testf()
+; CHECK-NEXT:    br i1 true, label %if.end, label %if.then
+;
+entry:
+  br label %if.then
+if.then:                                          ; preds = %entry, %if.then
+  %foo = phi i32 [ 0, %entry], [ %next, %if.then]
+  %next = add i32 %foo, 1
+  %call = call i1 @testf()
+  br i1 %call, label %if.end, label %if.then
+
+if.end:                                           ; preds = %if.then, %entry
+  ret void
+}
Index: lib/Transforms/Scalar/SCCP.cpp
===================================================================
--- lib/Transforms/Scalar/SCCP.cpp
+++ lib/Transforms/Scalar/SCCP.cpp
@@ -1902,13 +1902,15 @@
 
   // Solve for constants.
   bool ResolvedUndefs = true;
+  Solver.Solve();
   while (ResolvedUndefs) {
-    Solver.Solve();
-
     LLVM_DEBUG(dbgs() << "RESOLVING UNDEFS\n");
     ResolvedUndefs = false;
     for (Function &F : M)
-      ResolvedUndefs |= Solver.ResolvedUndefsIn(F);
+      if (Solver.ResolvedUndefsIn(F)) {
+        Solver.Solve();
+        ResolvedUndefs = true;
+      }
   }
 
   bool MadeChanges = false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49385.155710.patch
Type: text/x-patch
Size: 2536 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180716/df6ce0f0/attachment.bin>


More information about the llvm-commits mailing list