reenable load address insertion in load pre. This allows us to
authorChris Lattner <sabre@nondot.org>
Sat, 28 Nov 2009 16:08:18 +0000 (16:08 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 28 Nov 2009 16:08:18 +0000 (16:08 +0000)
handle cases like this:
void test(int N, double* G) {
  long j;
  for (j = 1; j < N - 1; j++)
      G[j+1] = G[j] + G[j+1];
}

where G[1] isn't live into the loop.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@90041 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/GVN.cpp
test/Transforms/GVN/pre-load.ll

index 6311491c7d3f8ec664aab7f3624ce2fcb9c48735..6f1c32c004e85b41f5622bba971bdf3301d82348 100644 (file)
@@ -1438,16 +1438,19 @@ bool GVN::processNonLocalLoad(LoadInst *LI,
   // FIXME: This may insert a computation, but we don't tell scalar GVN
   // optimization stuff about it.  How do we do this?
   SmallVector<Instruction*, 8> NewInsts;
-#if 0
-  Value *LoadPtr =
-    MD->InsertPHITranslatedPointer(LI->getOperand(0), LoadBB,
-                                   UnavailablePred, TD, *DT, NewInsts);
-#else
-  Value *LoadPtr =
-    MD->GetAvailablePHITranslatedValue(LI->getOperand(0), LoadBB,
-                                       UnavailablePred, TD, *DT);
-#endif  
+  Value *LoadPtr = 0;
   
+  // If all preds have a single successor, then we know it is safe to insert the
+  // load on the pred (?!?), so we can insert code to materialize the pointer if
+  // it is not available.
+  if (allSingleSucc) {
+    LoadPtr = MD->InsertPHITranslatedPointer(LI->getOperand(0), LoadBB,
+                                             UnavailablePred, TD, *DT,NewInsts);
+  } else {
+    LoadPtr = MD->GetAvailablePHITranslatedValue(LI->getOperand(0), LoadBB,
+                                                 UnavailablePred, TD, *DT);
+  }
+    
   // If we couldn't find or insert a computation of this phi translated value,
   // we fail PRE.
   if (LoadPtr == 0) {
@@ -1467,14 +1470,19 @@ bool GVN::processNonLocalLoad(LoadInst *LI,
   // put anywhere; this can be improved, but should be conservatively safe.
   if (!allSingleSucc &&
       // FIXME: REEVALUTE THIS.
-      !isSafeToLoadUnconditionally(LoadPtr, UnavailablePred->getTerminator()))
+      !isSafeToLoadUnconditionally(LoadPtr, UnavailablePred->getTerminator())) {
+    assert(NewInsts.empty() && "Should not have inserted instructions");
     return false;
+  }
 
   // Okay, we can eliminate this load by inserting a reload in the predecessor
   // and using PHI construction to get the value in the other predecessors, do
   // it.
   DEBUG(errs() << "GVN REMOVING PRE LOAD: " << *LI << '\n');
-
+  DEBUG(if (!NewInsts.empty())
+          errs() << "INSERTED " << NewInsts.size() << " INSTS: "
+                 << *NewInsts.back() << '\n');
+  
   Value *NewLoad = new LoadInst(LoadPtr, LI->getName()+".pre", false,
                                 LI->getAlignment(),
                                 UnavailablePred->getTerminator());
index c055e86b4250d2651f7031925ae7f12925f8a86f..aa9e2b0cb085d4c65ee4f7e2ce9d94ea3146867d 100644 (file)
@@ -86,9 +86,9 @@ block1:
 
 block2:
  br label %block4
-; HECK: block2:
-; HECK:   load i32*
-; HECK:   br label %block4
+; CHECK: block2:
+; CHECK:   load i32*
+; CHECK:   br label %block4
 
 block3:
   %B = getelementptr i32* %q, i32 1
@@ -103,10 +103,10 @@ block4:
   %P3 = getelementptr i32* %P2, i32 1
   %PRE = load i32* %P3
   ret i32 %PRE
-; HECK: block4:
-; HECK-NEXT: phi i32 [
-; HECK-NOT: load
-; HECK: ret i32
+; CHECK: block4:
+; CHECK-NEXT: phi i32 [
+; CHECK-NOT: load
+; CHECK: ret i32
 }
 
 ;void test5(int N, double *G) {
@@ -248,9 +248,9 @@ block1:
 
 block2:
  br label %block4
-; HECK: block2:
-; HECK:   load i32*
-; HECK:   br label %block4
+; CHECK: block2:
+; CHECK:   load i32*
+; CHECK:   br label %block4
 
 block3:
   %A = getelementptr i32* %p, i32 1
@@ -262,10 +262,10 @@ block4:
   %P3 = getelementptr i32* %P2, i32 1
   %PRE = load i32* %P3
   ret i32 %PRE
-; HECK: block4:
-; HECK-NEXT: phi i32 [
-; HECK-NOT: load
-; HECK: ret i32
+; CHECK: block4:
+; CHECK-NEXT: phi i32 [
+; CHECK-NOT: load
+; CHECK: ret i32
 }