[llvm] e53bbf1 - [GVN] Add GVNOption to control load-pre more fine-grained.

Thomas Raoux via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 3 23:01:38 PST 2020


Author: Thomas Raoux
Date: 2020-02-03T23:00:58-08:00
New Revision: e53bbf12132570ee6313ef500e1c715d3eab4e6f

URL: https://github.com/llvm/llvm-project/commit/e53bbf12132570ee6313ef500e1c715d3eab4e6f
DIFF: https://github.com/llvm/llvm-project/commit/e53bbf12132570ee6313ef500e1c715d3eab4e6f.diff

LOG: [GVN] Add GVNOption to control load-pre more fine-grained.

Adds the global (cl::opt) GVNOption enable-load-in-loop-pre in order
to control whether the optimization will be performed if the load
is part of a loop.

Patch by Hendrik Greving!

Differential Revision: https://reviews.llvm.org/D73804

Added: 
    llvm/test/Transforms/GVN/PRE/pre-load-in-loop.ll

Modified: 
    llvm/include/llvm/Transforms/Scalar/GVN.h
    llvm/lib/Transforms/Scalar/GVN.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Scalar/GVN.h b/llvm/include/llvm/Transforms/Scalar/GVN.h
index 84dd708a62d8..68989f9614bf 100644
--- a/llvm/include/llvm/Transforms/Scalar/GVN.h
+++ b/llvm/include/llvm/Transforms/Scalar/GVN.h
@@ -71,6 +71,7 @@ class GVNLegacyPass;
 struct GVNOptions {
   Optional<bool> AllowPRE = None;
   Optional<bool> AllowLoadPRE = None;
+  Optional<bool> AllowLoadInLoopPRE = None;
   Optional<bool> AllowMemDep = None;
 
   GVNOptions() = default;
@@ -87,6 +88,11 @@ struct GVNOptions {
     return *this;
   }
 
+  GVNOptions &setLoadInLoopPRE(bool LoadInLoopPRE) {
+    AllowLoadInLoopPRE = LoadInLoopPRE;
+    return *this;
+  }
+
   /// Enables or disables use of MemDepAnalysis.
   GVNOptions &setMemDep(bool MemDep) {
     AllowMemDep = MemDep;
@@ -122,6 +128,7 @@ class GVN : public PassInfoMixin<GVN> {
 
   bool isPREEnabled() const;
   bool isLoadPREEnabled() const;
+  bool isLoadInLoopPREEnabled() const;
   bool isMemDepEnabled() const;
 
   /// This class holds the mapping between values and value numbers.  It is used

diff  --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index dbe26bbed1f2..abdf5ecc88d6 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -99,6 +99,8 @@ STATISTIC(NumPRELoad,   "Number of loads PRE'd");
 
 static cl::opt<bool> GVNEnablePRE("enable-pre", cl::init(true), cl::Hidden);
 static cl::opt<bool> GVNEnableLoadPRE("enable-load-pre", cl::init(true));
+static cl::opt<bool> GVNEnableLoadInLoopPRE("enable-load-in-loop-pre",
+                                            cl::init(true));
 static cl::opt<bool> GVNEnableMemDep("enable-gvn-memdep", cl::init(true));
 
 // Maximum allowed recursion depth.
@@ -616,6 +618,11 @@ bool GVN::isPREEnabled() const {
 bool GVN::isLoadPREEnabled() const {
   return Options.AllowLoadPRE.getValueOr(GVNEnableLoadPRE);
 }
+
+bool GVN::isLoadInLoopPREEnabled() const {
+  return Options.AllowLoadInLoopPRE.getValueOr(GVNEnableLoadInLoopPRE);
+}
+
 bool GVN::isMemDepEnabled() const {
   return Options.AllowMemDep.getValueOr(GVNEnableMemDep);
 }
@@ -1396,6 +1403,9 @@ bool GVN::processNonLocalLoad(LoadInst *LI) {
   // Step 4: Eliminate partial redundancy.
   if (!isPREEnabled() || !isLoadPREEnabled())
     return false;
+  if (!isLoadInLoopPREEnabled() && this->LI &&
+      this->LI->getLoopFor(LI->getParent()))
+    return false;
 
   return PerformLoadPRE(LI, ValuesPerBlock, UnavailableBlocks);
 }

diff  --git a/llvm/test/Transforms/GVN/PRE/pre-load-in-loop.ll b/llvm/test/Transforms/GVN/PRE/pre-load-in-loop.ll
new file mode 100644
index 000000000000..1c7c04f5afd2
--- /dev/null
+++ b/llvm/test/Transforms/GVN/PRE/pre-load-in-loop.ll
@@ -0,0 +1,45 @@
+; RUN: opt < %s -basicaa -gvn -enable-load-in-loop-pre=false -S | FileCheck %s
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
+
+;void test1(int N, double *G) {
+;  int j;
+;  for (j = 0; j < N - 1; j++)
+;    G[j] = G[j] + G[j+1];
+;}
+
+define void @test1(i32 %N, double* nocapture %G) nounwind ssp {
+; CHECK-LABEL: @test1(
+entry:
+  %0 = add i32 %N, -1
+  %1 = icmp sgt i32 %0, 0
+  br i1 %1, label %bb.nph, label %return
+
+bb.nph:
+  %tmp = zext i32 %0 to i64
+  br label %bb
+
+; CHECK: bb.nph:
+; CHECK-NOT: load double, double*
+; CHECK: br label %bb
+
+bb:
+  %indvar = phi i64 [ 0, %bb.nph ], [ %tmp6, %bb ]
+  %tmp6 = add i64 %indvar, 1
+  %scevgep = getelementptr double, double* %G, i64 %tmp6
+  %scevgep7 = getelementptr double, double* %G, i64 %indvar
+  %2 = load double, double* %scevgep7, align 8
+  %3 = load double, double* %scevgep, align 8
+  %4 = fadd double %2, %3
+  store double %4, double* %scevgep7, align 8
+  %exitcond = icmp eq i64 %tmp6, %tmp
+  br i1 %exitcond, label %return, label %bb
+
+; Both loads should remain in the loop.
+; CHECK: bb:
+; CHECK: load double, double*
+; CHECK: load double, double*
+; CHECK: br i1 %exitcond
+
+return:
+  ret void
+}


        


More information about the llvm-commits mailing list