[llvm-branch-commits] [llvm-branch] r323738 - Merging r322006:

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Jan 30 01:52:39 PST 2018


Author: hans
Date: Tue Jan 30 01:52:39 2018
New Revision: 323738

URL: http://llvm.org/viewvc/llvm-project?rev=323738&view=rev
Log:
Merging r322006:
------------------------------------------------------------------------
r322006 | davide | 2018-01-08 17:34:06 +0100 (Mon, 08 Jan 2018) | 19 lines

[CVP] Replace incoming values from unreachable blocks with undef.

This is an attempt of fixing PR35807.
Due to the non-standard definition of dominance in LLVM, where uses in
unreachable blocks are dominated by anything, you can have, in an
unreachable block:

  %patatino = OP1 %patatino, CONSTANT

When `SimplifyInstruction` receives a PHI where an incoming value is of
the aforementioned form, in some cases, loops indefinitely.

What I propose here instead is keeping track of the incoming values
from unreachable blocks, and replacing them with undef. It fixes this
case, and it seems to be good regardless (even if we can't prove that
the value is constant, as it's coming from an unreachable block, we
can ignore it).

Differential Revision:  https://reviews.llvm.org/D41812
------------------------------------------------------------------------

Added:
    llvm/branches/release_60/test/Transforms/CorrelatedValuePropagation/pr35807.ll
      - copied unchanged from r322006, llvm/trunk/test/Transforms/CorrelatedValuePropagation/pr35807.ll
Modified:
    llvm/branches/release_60/   (props changed)
    llvm/branches/release_60/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Propchange: llvm/branches/release_60/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Jan 30 01:52:39 2018
@@ -1,3 +1,3 @@
 /llvm/branches/Apple/Pertwee:110850,110961
 /llvm/branches/type-system-rewrite:133420-134817
-/llvm/trunk:155241,321751,321789,321791,321806,321862,321870,321872,321878,321980,321991,321993-321994,322003,322053,322056,322103,322106,322108,322123,322131,322223,322272,322313,322372,322473,322623,322644,322724,322767,322875,322878-322879,322900,322904-322905,322973,322993,323034,323190,323307,323331,323369,323371,323384
+/llvm/trunk:155241,321751,321789,321791,321806,321862,321870,321872,321878,321980,321991,321993-321994,322003,322006,322053,322056,322103,322106,322108,322123,322131,322223,322272,322313,322372,322473,322623,322644,322724,322767,322875,322878-322879,322900,322904-322905,322973,322993,323034,323190,323307,323331,323369,323371,323384

Modified: llvm/branches/release_60/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_60/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp?rev=323738&r1=323737&r2=323738&view=diff
==============================================================================
--- llvm/branches/release_60/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp (original)
+++ llvm/branches/release_60/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp Tue Jan 30 01:52:39 2018
@@ -14,6 +14,7 @@
 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
 #include "llvm/ADT/DepthFirstIterator.h"
 #include "llvm/ADT/Optional.h"
+#include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Analysis/GlobalsModRef.h"
@@ -120,8 +121,8 @@ static bool processSelect(SelectInst *S,
   return true;
 }
 
-static bool processPHI(PHINode *P, LazyValueInfo *LVI,
-                       const SimplifyQuery &SQ) {
+static bool processPHI(PHINode *P, LazyValueInfo *LVI, const SimplifyQuery &SQ,
+                       DenseSet<BasicBlock *> &ReachableBlocks) {
   bool Changed = false;
 
   BasicBlock *BB = P->getParent();
@@ -129,7 +130,18 @@ static bool processPHI(PHINode *P, LazyV
     Value *Incoming = P->getIncomingValue(i);
     if (isa<Constant>(Incoming)) continue;
 
-    Value *V = LVI->getConstantOnEdge(Incoming, P->getIncomingBlock(i), BB, P);
+    // If the incoming value is coming from an unreachable block, replace
+    // it with undef and go on. This is good for two reasons:
+    // 1) We skip an LVI query for an unreachable block
+    // 2) We transform the incoming value so that the code below doesn't
+    //    mess around with IR in unreachable blocks.
+    BasicBlock *IncomingBB = P->getIncomingBlock(i);
+    if (!ReachableBlocks.count(IncomingBB)) {
+      P->setIncomingValue(i, UndefValue::get(P->getType()));
+      continue;
+    }
+
+    Value *V = LVI->getConstantOnEdge(Incoming, IncomingBB, BB, P);
 
     // Look if the incoming value is a select with a scalar condition for which
     // LVI can tells us the value. In that case replace the incoming value with
@@ -561,6 +573,14 @@ static Constant *getConstantAt(Value *V,
 
 static bool runImpl(Function &F, LazyValueInfo *LVI, const SimplifyQuery &SQ) {
   bool FnChanged = false;
+
+  // Compute reachability from the entry block of this function via an RPO
+  // walk. We use this information when processing PHIs.
+  DenseSet<BasicBlock *> ReachableBlocks;
+  ReversePostOrderTraversal<Function *> RPOT(&F);
+  for (BasicBlock *BB : RPOT)
+    ReachableBlocks.insert(BB);
+
   // Visiting in a pre-order depth-first traversal causes us to simplify early
   // blocks before querying later blocks (which require us to analyze early
   // blocks).  Eagerly simplifying shallow blocks means there is strictly less
@@ -575,7 +595,7 @@ static bool runImpl(Function &F, LazyVal
         BBChanged |= processSelect(cast<SelectInst>(II), LVI);
         break;
       case Instruction::PHI:
-        BBChanged |= processPHI(cast<PHINode>(II), LVI, SQ);
+        BBChanged |= processPHI(cast<PHINode>(II), LVI, SQ, ReachableBlocks);
         break;
       case Instruction::ICmp:
       case Instruction::FCmp:




More information about the llvm-branch-commits mailing list