Change PHINode::hasConstantValue to have a DominatorTree argument
authorDan Gohman <gohman@apple.com>
Thu, 3 Sep 2009 15:34:35 +0000 (15:34 +0000)
committerDan Gohman <gohman@apple.com>
Thu, 3 Sep 2009 15:34:35 +0000 (15:34 +0000)
instead of a bool argument, and to do the dominator check itself.
This makes it eaiser to use when DominatorTree information is
available.

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

include/llvm/Instructions.h
lib/Transforms/Scalar/GVN.cpp
lib/Transforms/Utils/BasicBlockUtils.cpp
lib/Transforms/Utils/LoopSimplify.cpp
lib/Transforms/Utils/PromoteMemoryToRegister.cpp
lib/VMCore/Instructions.cpp
test/Transforms/GVN/2008-07-02-Unreachable.ll

index e6f1c54483a1b11d04ef9307d7775d2baa2503f2..71f4738528975e3955a533837eb62b040625e643 100644 (file)
@@ -31,6 +31,7 @@ class ConstantInt;
 class ConstantRange;
 class APInt;
 class LLVMContext;
+class DominatorTree;
 
 //===----------------------------------------------------------------------===//
 //                             AllocationInst Class
@@ -1950,7 +1951,12 @@ public:
   /// hasConstantValue - If the specified PHI node always merges together the
   /// same value, return the value, otherwise return null.
   ///
-  Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
+  /// If the PHI has undef operands, but all the rest of the operands are
+  /// some unique value, return that value if it can be proved that the
+  /// value dominates the PHI. If DT is null, use a conservative check,
+  /// otherwise use DT to test for dominance.
+  ///
+  Value *hasConstantValue(DominatorTree *DT = 0) const;
 
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const PHINode *) { return true; }
index 56bc816c4f49fb21d503ac6657db6453102e6a92..36c90f519f2fb57e19406209e1827c2a7ce4246f 100644 (file)
@@ -769,7 +769,7 @@ static bool isSafeReplacement(PHINode* p, Instruction* inst) {
 }
 
 Value* GVN::CollapsePhi(PHINode* p) {
-  Value* constVal = p->hasConstantValue();
+  Value* constVal = p->hasConstantValue(DT);
   if (!constVal) return 0;
   
   Instruction* inst = dyn_cast<Instruction>(constVal);
index c3d6194801d23426ed4555ae70204d7da301ea14..c165e04fb8adb52ccd8cb4cd6b70fb5954b59da3 100644 (file)
@@ -429,13 +429,10 @@ BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
     PN->addIncoming(InVal, NewBB);
     
     // Check to see if we can eliminate this phi node.
-    if (Value *V = PN->hasConstantValue(DT != 0)) {
-      Instruction *I = dyn_cast<Instruction>(V);
-      if (!I || DT == 0 || DT->dominates(I, PN)) {
-        PN->replaceAllUsesWith(V);
-        if (AA) AA->deleteValue(PN);
-        PN->eraseFromParent();
-      }
+    if (Value *V = PN->hasConstantValue(DT)) {
+      PN->replaceAllUsesWith(V);
+      if (AA) AA->deleteValue(PN);
+      PN->eraseFromParent();
     }
   }
   
index c981a014cb302ebfad8836b1c9ac256c60ad8ca8..56e5a46cb7859803922a7f86a6638813830694ab 100644 (file)
@@ -255,7 +255,7 @@ ReprocessLoop:
   PHINode *PN;
   for (BasicBlock::iterator I = L->getHeader()->begin();
        (PN = dyn_cast<PHINode>(I++)); )
-    if (Value *V = PN->hasConstantValue()) {
+    if (Value *V = PN->hasConstantValue(DT)) {
       if (AA) AA->deleteValue(PN);
       PN->replaceAllUsesWith(V);
       PN->eraseFromParent();
@@ -417,14 +417,13 @@ static PHINode *FindPHIToPartitionLoops(Loop *L, DominatorTree *DT,
   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ) {
     PHINode *PN = cast<PHINode>(I);
     ++I;
-    if (Value *V = PN->hasConstantValue())
-      if (!isa<Instruction>(V) || DT->dominates(cast<Instruction>(V), PN)) {
-        // This is a degenerate PHI already, don't modify it!
-        PN->replaceAllUsesWith(V);
-        if (AA) AA->deleteValue(PN);
-        PN->eraseFromParent();
-        continue;
-      }
+    if (Value *V = PN->hasConstantValue(DT)) {
+      // This is a degenerate PHI already, don't modify it!
+      PN->replaceAllUsesWith(V);
+      if (AA) AA->deleteValue(PN);
+      PN->eraseFromParent();
+      continue;
+    }
 
     // Scan this PHI node looking for a use of the PHI node by itself.
     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
index 6a56a8d8245461659d512dc7e170cf5ea2343c3d..8274e5aeb619b7857513bcc10dccd8de33cc7387 100644 (file)
@@ -494,17 +494,14 @@ void PromoteMem2Reg::run() {
       PHINode *PN = I->second;
       
       // If this PHI node merges one value and/or undefs, get the value.
-      if (Value *V = PN->hasConstantValue(true)) {
-        if (!isa<Instruction>(V) ||
-            properlyDominates(cast<Instruction>(V), PN)) {
-          if (AST && isa<PointerType>(PN->getType()))
-            AST->deleteValue(PN);
-          PN->replaceAllUsesWith(V);
-          PN->eraseFromParent();
-          NewPhiNodes.erase(I++);
-          EliminatedAPHI = true;
-          continue;
-        }
+      if (Value *V = PN->hasConstantValue(&DT)) {
+        if (AST && isa<PointerType>(PN->getType()))
+          AST->deleteValue(PN);
+        PN->replaceAllUsesWith(V);
+        PN->eraseFromParent();
+        NewPhiNodes.erase(I++);
+        EliminatedAPHI = true;
+        continue;
       }
       ++I;
     }
index a12075fe0d9ff5c752bef9770a7bbc92efc46405..2d4ab557217653e107b91a27571f266d549a59c6 100644 (file)
@@ -17,6 +17,7 @@
 #include "llvm/Function.h"
 #include "llvm/Instructions.h"
 #include "llvm/Operator.h"
+#include "llvm/Analysis/Dominators.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/CallSite.h"
 #include "llvm/Support/ConstantRange.h"
@@ -226,7 +227,12 @@ void PHINode::resizeOperands(unsigned NumOps) {
 /// hasConstantValue - If the specified PHI node always merges together the same
 /// value, return the value, otherwise return null.
 ///
-Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
+/// If the PHI has undef operands, but all the rest of the operands are
+/// some unique value, return that value if it can be proved that the
+/// value dominates the PHI. If DT is null, use a conservative check,
+/// otherwise use DT to test for dominance.
+///
+Value *PHINode::hasConstantValue(DominatorTree *DT) const {
   // If the PHI node only has one incoming value, eliminate the PHI node...
   if (getNumIncomingValues() == 1) {
     if (getIncomingValue(0) != this)   // not  X = phi X
@@ -260,12 +266,19 @@ Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
   // instruction, we cannot always return X as the result of the PHI node.  Only
   // do this if X is not an instruction (thus it must dominate the PHI block),
   // or if the client is prepared to deal with this possibility.
-  if (HasUndefInput && !AllowNonDominatingInstruction)
-    if (Instruction *IV = dyn_cast<Instruction>(InVal))
-      // If it's in the entry block, it dominates everything.
-      if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
-          isa<InvokeInst>(IV))
-        return 0;   // Cannot guarantee that InVal dominates this PHINode.
+  if (HasUndefInput)
+    if (Instruction *IV = dyn_cast<Instruction>(InVal)) {
+      if (DT) {
+        // We have a DominatorTree. Do a precise test.
+        if (!DT->dominates(IV, this))
+          return 0;
+      } else {
+        // If it's in the entry block, it dominates everything.
+        if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
+            isa<InvokeInst>(IV))
+          return 0;   // Cannot guarantee that InVal dominates this PHINode.
+      }
+    }
 
   // All of the incoming values are the same, return the value now.
   return InVal;
index 15667d2bfb21e84bbacd46224cc3dd1ef644a653..fc273c30f7f69373a2f391a9df655374b6474b60 100644 (file)
@@ -1,4 +1,4 @@
-; RUN: llvm-as < %s | opt -gvn | llvm-dis | grep undef
+; RUN: llvm-as < %s | opt -gvn | llvm-dis | grep {ret i8 \[%\]tmp3}
 ; PR2503
 
 @g_3 = external global i8              ; <i8*> [#uses=2]