add support for caching pointer dependence queries. Nothing uses this yet
[oota-llvm.git] / lib / Analysis / SparsePropagation.cpp
index 9c18751ed6f855787393fa52b9f6c7f3654de758..8f042c278392f6645bab6954233de8be3200b361 100644 (file)
 #include "llvm/Constants.h"
 #include "llvm/Function.h"
 #include "llvm/Instructions.h"
-#include "llvm/Module.h"
-#include "llvm/Pass.h"
 #include "llvm/Support/Debug.h"
-#include "llvm/Support/Streams.h"
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -60,8 +57,10 @@ SparseSolver::LatticeVal SparseSolver::getOrInitValueState(Value *V) {
     return LatticeFunc->getUntrackedVal();
   else if (Constant *C = dyn_cast<Constant>(V))
     LV = LatticeFunc->ComputeConstant(C);
+  else if (Argument *A = dyn_cast<Argument>(V))
+    LV = LatticeFunc->ComputeArgument(A);
   else if (!isa<Instruction>(V))
-    // Non-instructions (e.g. formal arguments) are overdefined.
+    // All other non-instructions are overdefined.
     LV = LatticeFunc->getOverdefinedVal();
   else
     // All instructions are underdefined by default.
@@ -99,10 +98,10 @@ void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
   if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
     return;  // This edge is already known to be executable!
   
+  DOUT << "Marking Edge Executable: " << Source->getNameStart()
+       << " -> " << Dest->getNameStart() << "\n";
+
   if (BBExecutable.count(Dest)) {
-    DOUT << "Marking Edge Executable: " << Source->getNameStart()
-    << " -> " << Dest->getNameStart() << "\n";
-    
     // The destination is already executable, but we just made an edge
     // feasible that wasn't before.  Revisit the PHI nodes in the block
     // because they have potentially new operands.
@@ -118,7 +117,8 @@ void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
 /// getFeasibleSuccessors - Return a vector of booleans to indicate which
 /// successors are reachable from a given terminator instruction.
 void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
-                                         SmallVectorImpl<bool> &Succs) {
+                                         SmallVectorImpl<bool> &Succs,
+                                         bool AggressiveUndef) {
   Succs.resize(TI.getNumSuccessors());
   if (TI.getNumSuccessors() == 0) return;
   
@@ -128,7 +128,12 @@ void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
       return;
     }
     
-    LatticeVal BCValue = getOrInitValueState(BI->getCondition());
+    LatticeVal BCValue;
+    if (AggressiveUndef)
+      BCValue = getOrInitValueState(BI->getCondition());
+    else
+      BCValue = getLatticeState(BI->getCondition());
+    
     if (BCValue == LatticeFunc->getOverdefinedVal() ||
         BCValue == LatticeFunc->getUntrackedVal()) {
       // Overdefined condition variables can branch either way.
@@ -160,7 +165,12 @@ void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
   }
   
   SwitchInst &SI = cast<SwitchInst>(TI);
-  LatticeVal SCValue = getOrInitValueState(SI.getCondition());
+  LatticeVal SCValue;
+  if (AggressiveUndef)
+    SCValue = getOrInitValueState(SI.getCondition());
+  else
+    SCValue = getLatticeState(SI.getCondition());
+  
   if (SCValue == LatticeFunc->getOverdefinedVal() ||
       SCValue == LatticeFunc->getUntrackedVal()) {
     // All destinations are executable!
@@ -185,10 +195,11 @@ void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
 
 /// isEdgeFeasible - Return true if the control flow edge from the 'From'
 /// basic block to the 'To' basic block is currently feasible...
-bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
+bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To,
+                                  bool AggressiveUndef) {
   SmallVector<bool, 16> SuccFeasible;
   TerminatorInst *TI = From->getTerminator();
-  getFeasibleSuccessors(*TI, SuccFeasible);
+  getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
   
   for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
     if (TI->getSuccessor(i) == To && SuccFeasible[i])
@@ -199,7 +210,7 @@ bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
 
 void SparseSolver::visitTerminatorInst(TerminatorInst &TI) {
   SmallVector<bool, 16> SuccFeasible;
-  getFeasibleSuccessors(TI, SuccFeasible);
+  getFeasibleSuccessors(TI, SuccFeasible, true);
   
   BasicBlock *BB = TI.getParent();
   
@@ -229,7 +240,7 @@ void SparseSolver::visitPHINode(PHINode &PN) {
   // transfer function to give us the merge of the incoming values.
   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
     // If the edge is not yet known to be feasible, it doesn't impact the PHI.
-    if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent()))
+    if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true))
       continue;
     
     // Merge in this value.
@@ -263,7 +274,7 @@ void SparseSolver::visitInst(Instruction &I) {
 }
 
 void SparseSolver::Solve(Function &F) {
-  MarkBlockExecutable(F.begin());
+  MarkBlockExecutable(&F.getEntryBlock());
   
   // Process the work lists until they are empty!
   while (!BBWorkList.empty() || !InstWorkList.empty()) {