SCCP.cpp revision f006b183e2d2bebcf6968d1dd7350397c95b0325
1cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===//
2cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
3cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//                     The LLVM Compiler Infrastructure
4cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
5cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// This file is distributed under the University of Illinois Open Source
6cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// License. See LICENSE.TXT for details.
7cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
8cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//===----------------------------------------------------------------------===//
9cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
10cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// This file implements sparse conditional constant propagation and merging:
11cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
12cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen// Specifically, this:
13cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//   * Assumes values are constant unless proven otherwise
14cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//   * Assumes BasicBlocks are dead unless proven otherwise
15cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//   * Proves values to be constant, and replaces them with constants
16dd479e9769eceee9fcc34e2173376024f3aa3c5fJakob Stoklund Olesen//   * Proves conditional branches to be unconditional
175907d863659eb972ebb2afe07bc863a4c616f0efJakob Stoklund Olesen//
18cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen// Notice that:
19f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen//   * This pass has a habit of making definitions be dead.  It is a good idea
20cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//     to to run a DCE pass sometime after running this pass.
21cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen//
22b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen//===----------------------------------------------------------------------===//
23d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen
24cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#define DEBUG_TYPE "sccp"
250db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen#include "llvm/Transforms/Scalar.h"
26cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Transforms/IPO.h"
27cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Constants.h"
28cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/DerivedTypes.h"
29cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Instructions.h"
30b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen#include "llvm/LLVMContext.h"
31cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Pass.h"
32cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Analysis/ConstantFolding.h"
33f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen#include "llvm/Analysis/MemoryBuiltins.h"
34cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Analysis/ValueTracking.h"
35cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Transforms/Utils/Local.h"
36cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/CallSite.h"
37cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/Debug.h"
38cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/ErrorHandling.h"
39cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/Support/InstVisitor.h"
4000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen#include "llvm/Support/raw_ostream.h"
41cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/ADT/DenseMap.h"
42cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/ADT/DenseSet.h"
43cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/ADT/SmallSet.h"
44533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen#include "llvm/ADT/SmallVector.h"
45cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include "llvm/ADT/Statistic.h"
4698d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen#include "llvm/ADT/STLExtras.h"
4798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen#include <algorithm>
48cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen#include <map>
49cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenusing namespace llvm;
500db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen
510db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumInstRemoved, "Number of instructions removed");
520db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund OlesenSTATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
530db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen
54a67f14bf53737f9bb0afefa28e08c4aac6ec4804Benjamin KramerSTATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP");
5500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund OlesenSTATISTIC(IPNumDeadBlocks , "Number of basic blocks unreachable by IPSCCP");
56708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund OlesenSTATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");
57708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund OlesenSTATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP");
58708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen
59708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesennamespace {
60708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen/// LatticeVal class - This class represents the different lattice values that
61708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen/// an LLVM value may occupy.  It is a simple class with value semantics.
62708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen///
63708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesenclass LatticeVal {
64708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen  enum {
65cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    /// undefined - This LLVM Value has no known value yet.
66cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    undefined,
67cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
68cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    /// constant - This LLVM Value has a specific constant value.
6992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen    constant,
7092a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
7192a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen    /// forcedconstant - This LLVM Value was thought to be undef until
7292a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen    /// ResolvedUndefsIn.  This is treated just like 'constant', but if merged
73cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    /// with another (different) constant, it goes to overdefined, instead of
74cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    /// asserting.
75cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    forcedconstant,
76cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
77b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    /// overdefined - This instruction is not known to be constant, and we know
78cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    /// it has a value.
79f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen    overdefined
80d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen  } LatticeValue;    // The current lattice position
81b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
82b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  Constant *ConstantVal; // If Constant value, the current value
83f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesenpublic:
84f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen  inline LatticeVal() : LatticeValue(undefined), ConstantVal(0) {}
85cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
86cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // markOverdefined - Return true if this is a new status to be in...
8798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  inline bool markOverdefined() {
881a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    if (LatticeValue != overdefined) {
8922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen      LatticeValue = overdefined;
9022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen      return true;
9122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen    }
9222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen    return false;
9322a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  }
9422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
9522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  // markConstant - Return true if this is a new status for us.
9622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  inline bool markConstant(Constant *V) {
9722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen    if (LatticeValue != constant) {
9822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen      if (LatticeValue == undefined) {
9922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen        LatticeValue = constant;
10022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen        assert(V && "Marking constant with NULL");
10122a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen        ConstantVal = V;
10222a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen      } else {
10322a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen        assert(LatticeValue == forcedconstant &&
104fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen               "Cannot move from overdefined to constant!");
105fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen        // Stay at forcedconstant if the constant is the same.
106fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen        if (V == ConstantVal) return false;
107fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen
108fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen        // Otherwise, we go to overdefined.  Assumptions made based on the
109fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen        // forced value are possibly wrong.  Assuming this is another constant
110fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen        // could expose a contradiction.
111fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen        LatticeValue = overdefined;
112fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen      }
11349743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen      return true;
11449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    } else {
11549743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen      assert(ConstantVal == V && "Marking constant with different value");
11649743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    }
11749743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    return false;
118fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  }
119fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen
120fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  inline void markForcedConstant(Constant *V) {
121fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    assert(LatticeValue == undefined && "Can't force a defined value!");
122fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    LatticeValue = forcedconstant;
123fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    ConstantVal = V;
12422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  }
12522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
126b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen  inline bool isUndefined() const { return LatticeValue == undefined; }
127b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen  inline bool isConstant() const {
1281a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    return LatticeValue == constant || LatticeValue == forcedconstant;
1291a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  }
1301a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  inline bool isOverdefined() const { return LatticeValue == overdefined; }
1311a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen
1321a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  inline Constant *getConstant() const {
1331a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    assert(isConstant() && "Cannot get the constant of a non-constant!");
1341a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    return ConstantVal;
1351a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  }
1361a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen};
1371a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen
1381a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen//===----------------------------------------------------------------------===//
13922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen//
14022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen/// SCCPSolver - This class is a general purpose solver for Sparse Conditional
1411a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen/// Constant Propagation.
1421a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen///
1431a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesenclass SCCPSolver : public InstVisitor<SCCPSolver> {
1441a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  LLVMContext *Context;
1451a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  DenseSet<BasicBlock*> BBExecutable;// The basic blocks that are executable
1461a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  std::map<Value*, LatticeVal> ValueState;  // The state each value is in.
14722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
14822a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  /// GlobalValue - If we are tracking any values for the contents of a global
14922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  /// variable, we keep a mapping from the constant accessor to the element of
15022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  /// the global, to the currently known value.  If the value becomes
1511a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  /// overdefined, it's entry is simply removed from this map.
152f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen  DenseMap<GlobalVariable*, LatticeVal> TrackedGlobals;
153f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen
1541a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  /// TrackedRetVals - If we are tracking arguments into and the return
1551a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  /// value out of a function, it will have an entry in this map, indicating
156f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen  /// what the known return value for the function is.
15722a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen  DenseMap<Function*, LatticeVal> TrackedRetVals;
158cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
15951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  /// TrackedMultipleRetVals - Same as TrackedRetVals, but used for functions
16051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  /// that return multiple values.
16151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  DenseMap<std::pair<Function*, unsigned>, LatticeVal> TrackedMultipleRetVals;
16251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
16351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // The reason for two worklists is that overdefined is the lowest state
16451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // on the lattice, and moving things to overdefined as fast as possible
16551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // makes SCCP converge much faster.
16651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // By having a separate worklist, we accomplish this because everything
16751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // possibly overdefined will become overdefined at the soonest possible
16851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // point.
16951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  SmallVector<Value*, 64> OverdefinedInstWorkList;
17051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  SmallVector<Value*, 64> InstWorkList;
17151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
17251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
173b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  SmallVector<BasicBlock*, 64>  BBWorkList;  // The BasicBlock work list
17422a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
175bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not
176b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  /// overdefined, despite the fact that the PHI node is overdefined.
177eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen  std::multimap<PHINode*, Instruction*> UsersOfOverdefinedPHIs;
178eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen
179eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen  /// KnownFeasibleEdges - Entries in this set are edges which have already had
1807b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen  /// PHI nodes retriggered.
18196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  typedef std::pair<BasicBlock*, BasicBlock*> Edge;
182b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  DenseSet<Edge> KnownFeasibleEdges;
18396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesenpublic:
18496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  void setContext(LLVMContext *C) { Context = C; }
18500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
18696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  /// MarkBlockExecutable - This method can be used by clients to mark all of
18700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// the blocks that are known to be intrinsically live in the processed unit.
18800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  void MarkBlockExecutable(BasicBlock *BB) {
18900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    DEBUG(errs() << "Marking Block Executable: " << BB->getName() << "\n");
19000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    BBExecutable.insert(BB);   // Basic block is executable!
19100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    BBWorkList.push_back(BB);  // Add the block to the work list!
192c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  }
19300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
19400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// TrackValueOfGlobalVariable - Clients can use this method to
19596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  /// inform the SCCPSolver that it should track loads and stores to the
1965db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  /// specified global variable if it can.  This is only legal to call if
1975db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  /// performing Interprocedural SCCP.
198c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  void TrackValueOfGlobalVariable(GlobalVariable *GV) {
1995db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen    const Type *ElTy = GV->getType()->getElementType();
20000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (ElTy->isFirstClassType()) {
201c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen      LatticeVal &IV = TrackedGlobals[GV];
2025db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen      if (!isa<UndefValue>(GV->getInitializer()))
2035db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen        IV.markConstant(GV->getInitializer());
2045db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen    }
20500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  }
20600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
20700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// AddTrackedFunction - If the SCCP solver is supposed to track calls into
20800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// and out of the specified function (which cannot have its address taken),
20900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// this method must be called.
21000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  void AddTrackedFunction(Function *F) {
21100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    assert(F->hasLocalLinkage() && "Can only track internal functions!");
21200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Add an entry, F -> undef.
21300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (const StructType *STy = dyn_cast<StructType>(F->getReturnType())) {
21400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
21500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        TrackedMultipleRetVals.insert(std::make_pair(std::make_pair(F, i),
21600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen                                                     LatticeVal()));
21796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    } else
21896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen      TrackedRetVals.insert(std::make_pair(F, LatticeVal()));
21996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  }
22096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
22196dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  /// Solve - Solve for constants and executable blocks.
22296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  ///
22396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  void Solve();
22400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
22500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// ResolvedUndefsIn - While solving the dataflow for a function, we assume
22600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// that branches on undef values cannot reach any of their successors.
22700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// However, this is not a safe assumption.  After we solve dataflow, this
22800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// method should be use to handle this.  If this returns true, the solver
22900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  /// should be rerun.
230cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  bool ResolvedUndefsIn(Function &F);
231cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
232cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  bool isBlockExecutable(BasicBlock *BB) const {
233cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    return BBExecutable.count(BB);
234cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  }
235533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen
236cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  /// getValueMapping - Once we have solved for constants, return the mapping of
237cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  /// LLVM values to LatticeVals.
238cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  std::map<Value*, LatticeVal> &getValueMapping() {
239cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    return ValueState;
240cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  }
241cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
24298d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  /// getTrackedRetVals - Get the inferred return value map.
24398d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  ///
244ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  const DenseMap<Function*, LatticeVal> &getTrackedRetVals() {
245ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    return TrackedRetVals;
246cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  }
247cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
248cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  /// getTrackedGlobals - Get and return the set of inferred initializers for
249cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  /// global variables.
250cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  const DenseMap<GlobalVariable*, LatticeVal> &getTrackedGlobals() {
251b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick    return TrackedGlobals;
252b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick  }
25392a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
2547792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  inline void markOverdefined(Value *V) {
2551d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen    markOverdefined(ValueState[V], V);
256f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen  }
25792a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
258200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesenprivate:
259f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  // markConstant - Make a value be marked as "constant".  If the value
260f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  // is not already a constant, add it to the instruction work list so that
261c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  // the users of the instruction are updated later.
262c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  //
26387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen  inline void markConstant(LatticeVal &IV, Value *V, Constant *C) {
26400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (IV.markConstant(C)) {
265034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      DEBUG(errs() << "markConstant: " << *C << ": " << *V << '\n');
26651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      InstWorkList.push_back(V);
26751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    }
26851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  }
26951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
270b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen  inline void markForcedConstant(LatticeVal &IV, Value *V, Constant *C) {
2716bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    IV.markForcedConstant(C);
2726bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    DEBUG(errs() << "markForcedConstant: " << *C << ": " << *V << '\n');
27398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen    InstWorkList.push_back(V);
2746bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  }
275b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
276b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  inline void markConstant(Value *V, Constant *C) {
277dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    markConstant(ValueState[V], V, C);
278dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen  }
279034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
280034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // markOverdefined - Make a value be marked as "overdefined". If the
281b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen  // value is not already overdefined, add it to the overdefined instruction
282b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen  // work list so that the users of the instruction are updated later.
283cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  inline void markOverdefined(LatticeVal &IV, Value *V) {
284cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    if (IV.markOverdefined()) {
285cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen      DEBUG(errs() << "markOverdefined: ";
286cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen            if (Function *F = dyn_cast<Function>(V))
287cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen              errs() << "Function '" << F->getName() << "'\n";
288b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen            else
289b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen              errs() << *V << '\n');
290fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen      // Only instructions go on the work list
291fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen      OverdefinedInstWorkList.push_back(V);
292fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    }
29349743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  }
294fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen
295fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  inline void mergeInValue(LatticeVal &IV, Value *V, LatticeVal &MergeWithV) {
296b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen    if (IV.isOverdefined() || MergeWithV.isUndefined())
297b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen      return;  // Noop.
298b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen    if (MergeWithV.isOverdefined())
299200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      markOverdefined(IV, V);
300200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    else if (IV.isUndefined())
301200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      markConstant(IV, V, MergeWithV.getConstant());
302200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    else if (IV.getConstant() != MergeWithV.getConstant())
303200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      markOverdefined(IV, V);
304cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  }
305cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
306cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  inline void mergeInValue(Value *V, LatticeVal &MergeWithV) {
307cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    return mergeInValue(ValueState[V], V, MergeWithV);
3081a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  }
309cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen
310b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
311cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // getValueState - Return the LatticeVal object that corresponds to the value.
312cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // This function is necessary because not all values should start out in the
313cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // underdefined state... Argument's should be overdefined, and
3145b220213bfe9c37c2bb41a7ae0804e06a14f1007Rafael Espindola  // constants should be marked as constants.  If a value is not known to be an
315cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // Instruction object, then use this accessor to get its value from the map.
316cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  //
317cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  inline LatticeVal &getValueState(Value *V) {
318cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    std::map<Value*, LatticeVal>::iterator I = ValueState.find(V);
319cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    if (I != ValueState.end()) return I->second;  // Common case, in the map
320b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
321b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    if (Constant *C = dyn_cast<Constant>(V)) {
322cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen      if (isa<UndefValue>(V)) {
323cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        // Nothing to do, remain undefined.
324cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen      } else {
325cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        LatticeVal &LV = ValueState[C];
326cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        LV.markConstant(C);          // Constants are constant
327cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        return LV;
328cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen      }
329b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    }
330cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    // All others are underdefined by default...
331cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen    return ValueState[V];
332cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen  }
333cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
334cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  // markEdgeExecutable - Mark a basic block as executable, adding it to the BB
33527215676c7114132a0374f7b5c9ea73d9354d329Jakob Stoklund Olesen  // work list if it is not already executable...
336cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  //
337cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
338cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
339f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen      return;  // This edge is already known to be executable!
340f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen
341cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    if (BBExecutable.count(Dest)) {
342cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen      DEBUG(errs() << "Marking Edge Executable: " << Source->getName()
343cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen            << " -> " << Dest->getName() << "\n");
344cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
345b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen      // The destination is already executable, but we just made an edge
346b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen      // feasible that wasn't before.  Revisit the PHI nodes in the block
347cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen      // because they have potentially new operands.
348cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen      for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
349cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        visitPHINode(*cast<PHINode>(I));
35092a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
35192a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen    } else {
35292a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen      MarkBlockExecutable(Dest);
35392a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen    }
35492a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  }
35592a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen
35692a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  // getFeasibleSuccessors - Return a vector of booleans to indicate which
35792a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  // successors are reachable from a given terminator instruction.
35892a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  //
35992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  void getFeasibleSuccessors(TerminatorInst &TI, SmallVector<bool, 16> &Succs);
3607792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen
3617792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
3627792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  // block to the 'To' basic block is currently feasible...
3637792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  //
3647792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  bool isEdgeFeasible(BasicBlock *From, BasicBlock *To);
3657792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen
3667792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  // OperandChangedState - This method is invoked on all of the users of an
3677792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  // instruction that was just changed state somehow....  Based on this
3687792e980c43536814ea42448db9799b4da32fef6Jakob Stoklund Olesen  // information, we need to update the specified user of this instruction.
36992a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen  //
3701d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  void OperandChangedState(User *U) {
3711d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen    // Only instructions use other variable values!
3721d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen    Instruction &I = cast<Instruction>(*U);
3731d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen    if (BBExecutable.count(I.getParent()))   // Inst is executable?
3741d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen      visit(I);
3751d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  }
3761d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen
3771d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesenprivate:
3781d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  friend class InstVisitor<SCCPSolver>;
3791d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen
3801d5b84508173b93faf513032b3847152e6060791Jakob Stoklund Olesen  // visit implementations - Something changed in this instruction... Either an
381f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen  // operand made a transition, or the instruction is newly executable.  Change
382f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen  // the value type of I to reflect these changes if appropriate.
383165e231c4295deb5cabd124d08e231b551bcc0b2Jakob Stoklund Olesen  //
384165e231c4295deb5cabd124d08e231b551bcc0b2Jakob Stoklund Olesen  void visitPHINode(PHINode &I);
385f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen
386165e231c4295deb5cabd124d08e231b551bcc0b2Jakob Stoklund Olesen  // Terminators
3871a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  void visitReturnInst(ReturnInst &I);
3881a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  void visitTerminatorInst(TerminatorInst &TI);
389f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen
390f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen  void visitCastInst(CastInst &I);
391cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  void visitSelectInst(SelectInst &I);
392cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  void visitBinaryOperator(Instruction &I);
3931a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  void visitCmpInst(CmpInst &I);
3945db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  void visitExtractElementInst(ExtractElementInst &I);
395cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  void visitInsertElementInst(InsertElementInst &I);
396cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  void visitShuffleVectorInst(ShuffleVectorInst &I);
397cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  void visitExtractValueInst(ExtractValueInst &EVI);
39898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  void visitInsertValueInst(InsertValueInst &IVI);
39998d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen
40098d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  // Instructions that cannot be folded away...
401107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  void visitStoreInst     (Instruction &I);
402107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen  void visitLoadInst      (LoadInst &I);
40398d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  void visitGetElementPtrInst(GetElementPtrInst &I);
40498d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  void visitCallInst      (CallInst &I) {
405107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen    if (isFreeCall(&I))
40690c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen      return;
4071a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    visitCallSite(CallSite::get(&I));
4081a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  }
409fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  void visitInvokeInst    (InvokeInst &II) {
410f22ca3fe5f0cfbb832cf41270f97cf5c0134fd7bJakob Stoklund Olesen    visitCallSite(CallSite::get(&II));
411cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen    visitTerminatorInst(II);
412eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen  }
413eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen  void visitCallSite      (CallSite CS);
414eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen  void visitUnwindInst    (TerminatorInst &I) { /*returns void*/ }
415cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen  void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ }
416cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen  void visitAllocaInst    (Instruction &I) { markOverdefined(&I); }
417cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen  void visitVANextInst    (Instruction &I) { markOverdefined(&I); }
418cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen  void visitVAArgInst     (Instruction &I) { markOverdefined(&I); }
419cc07e04262fe4bc35469fbadc53d2ec7bfd02fe2Jakob Stoklund Olesen
420eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen  void visitInstruction(Instruction &I) {
421eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen    // If a new instruction is added to LLVM that we don't handle...
422107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen    errs() << "SCCP: Don't know how to handle: " << I;
423d2a50734234a80893ad71da90d9f32032c47e000Jakob Stoklund Olesen    markOverdefined(&I);   // Just in case
424eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen  }
425eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen};
426eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen
427eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen} // end anonymous namespace
428107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen
429107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen
43090c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen// getFeasibleSuccessors - Return a vector of booleans to indicate which
43190c1d7ddfc65654f7efe72d56cad65d1af9e6b2aJakob Stoklund Olesen// successors are reachable from a given terminator instruction.
43298d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen//
43398d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesenvoid SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
43498d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen                                       SmallVector<bool, 16> &Succs) {
43598d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  Succs.resize(TI.getNumSuccessors());
43698d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen  if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
43798d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen    if (BI->isUnconditional()) {
43898d9648de7d571b2e6d139b65961a70d1833b0d7Jakob Stoklund Olesen      Succs[0] = true;
439770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen    } else {
4406bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen      LatticeVal &BCValue = getValueState(BI->getCondition());
4416bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen      if (BCValue.isOverdefined() ||
4426bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen          (BCValue.isConstant() && !isa<ConstantInt>(BCValue.getConstant()))) {
4436bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen        // Overdefined condition variables, and branches on unfoldable constant
4446bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen        // conditions, mean the branch could go either way.
4456bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen        Succs[0] = Succs[1] = true;
4466bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen      } else if (BCValue.isConstant()) {
4476bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen        // Constant condition variables mean the branch can only go a single way
4486bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen        Succs[BCValue.getConstant() == ConstantInt::getFalse(*Context)] = true;
4496bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen      }
4506bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    }
4516bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  } else if (isa<InvokeInst>(&TI)) {
4526bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    // Invoke instructions successors are always executable.
4536bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    Succs[0] = Succs[1] = true;
4546bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) {
4556bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    LatticeVal &SCValue = getValueState(SI->getCondition());
4566bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    if (SCValue.isOverdefined() ||   // Overdefined condition?
45751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        (SCValue.isConstant() && !isa<ConstantInt>(SCValue.getConstant()))) {
45851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // All destinations are executable!
45951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      Succs.assign(TI.getNumSuccessors(), true);
46051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    } else if (SCValue.isConstant())
46151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      Succs[SI->findCaseValue(cast<ConstantInt>(SCValue.getConstant()))] = true;
46251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  } else {
46351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    llvm_unreachable("SCCP: Don't know how to handle this terminator!");
46451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  }
46551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen}
46651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
46751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
46851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen// isEdgeFeasible - Return true if the control flow edge from the 'From' basic
46951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen// block to the 'To' basic block is currently feasible...
47051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen//
47151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesenbool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
4726bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  assert(BBExecutable.count(To) && "Dest should always be alive!");
4736bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen
4746bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  // Make sure the source basic block is executable!!
4756bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  if (!BBExecutable.count(From)) return false;
4766bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen
4776bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  // Check to make sure this edge itself is actually feasible now...
4786bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  TerminatorInst *TI = From->getTerminator();
4796bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
4806bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    if (BI->isUnconditional())
4816bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen      return true;
4826bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    else {
4836bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen      LatticeVal &BCValue = getValueState(BI->getCondition());
4846bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen      if (BCValue.isOverdefined()) {
485770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen        // Overdefined condition variables mean the branch could go either way.
48698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen        return true;
48798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen      } else if (BCValue.isConstant()) {
4882710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen        // Not branching on an evaluatable constant?
48951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        if (!isa<ConstantInt>(BCValue.getConstant())) return true;
49051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
49151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        // Constant condition variables mean the branch can only go a single way
49251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        return BI->getSuccessor(BCValue.getConstant() ==
493b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen                                       ConstantInt::getFalse(*Context)) == To;
4941a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen      }
4951a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen      return false;
49651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    }
49751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  } else if (isa<InvokeInst>(TI)) {
49851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    // Invoke instructions successors are always executable.
49951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    return true;
50051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
50151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    LatticeVal &SCValue = getValueState(SI->getCondition());
50251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    if (SCValue.isOverdefined()) {  // Overdefined condition?
50351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // All destinations are executable!
50449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen      return true;
50551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    } else if (SCValue.isConstant()) {
50651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      Constant *CPV = SCValue.getConstant();
50751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      if (!isa<ConstantInt>(CPV))
50851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        return true;  // not a foldable constant?
50951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
51051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // Make sure to skip the "default value" which isn't a value
5111a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen      for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i)
512b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen        if (SI->getSuccessorValue(i) == CPV) // Found the taken branch...
513b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen          return SI->getSuccessor(i) == To;
51451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
51551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // Constant value not equal to any of the branches... must execute
51651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      // default branch then...
51751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      return SI->getDefaultDest() == To;
51851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    }
51951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    return false;
52051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  } else {
52151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen#ifndef NDEBUG
52251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    errs() << "Unknown terminator instruction: " << *TI << '\n';
52398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen#endif
52451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    llvm_unreachable(0);
5251a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  }
5261a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen}
5271a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen
5281a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen// visit Implementations - Something changed in this instruction... Either an
5291a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen// operand made a transition, or the instruction is newly executable.  Change
5301a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen// the value type of I to reflect these changes if appropriate.  This method
5311a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen// makes sure to do the following actions:
5321a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen//
5331a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen// 1. If a phi node merges two constants in, and has conflicting value coming
5341a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen//    from different branches, or if the PHI node merges in an overdefined
5351a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen//    value, then the PHI node becomes overdefined.
53651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen// 2. If a phi node merges only constants in, and they all agree on value, the
53798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen//    PHI node becomes a constant value equal to that.
53898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen// 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant
5393f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen// 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined
54051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen// 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined
54198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen// 6. If a conditional branch has a value that is constant, make the selected
54298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen//    destination executable
5433f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen// 7. If a conditional branch has a value that is overdefined, make all
5443f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen//    successors executable.
5453f5bedf5cbde2cc2badc86b1a0b377f6efcde71cJakob Stoklund Olesen//
54698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesenvoid SCCPSolver::visitPHINode(PHINode &PN) {
54798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  LatticeVal &PNIV = getValueState(&PN);
54851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  if (PNIV.isOverdefined()) {
549fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    // There may be instructions using this PHI node that are not overdefined
5501a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    // themselves.  If so, make sure that they know that the PHI node operand
55151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    // changed.
55251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    std::multimap<PHINode*, Instruction*>::iterator I, E;
55351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    tie(I, E) = UsersOfOverdefinedPHIs.equal_range(&PN);
55451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    if (I != E) {
55551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      SmallVector<Instruction*, 16> Users;
55651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      for (; I != E; ++I) Users.push_back(I->second);
55751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      while (!Users.empty()) {
55851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        visit(Users.back());
55951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        Users.pop_back();
56051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      }
56151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    }
56251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    return;  // Quick exit
56351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  }
56451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
56551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant,
56651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // and slow us down a lot.  Just mark them overdefined.
56751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  if (PN.getNumIncomingValues() > 64) {
56851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    markOverdefined(PNIV, &PN);
56951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    return;
57051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  }
571b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen
57251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // Look at all of the executable operands of the PHI node.  If any of them
57351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // are overdefined, the PHI becomes overdefined as well.  If they are all
57476395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  // constant, and they agree with each other, the PHI becomes the identical
5752710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen  // constant.  If they are constant and don't agree, the PHI is overdefined.
5762710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen  // If there are no executable operands, the PHI remains undefined.
57751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  //
57898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  Constant *OperandVal = 0;
57998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
58098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen    LatticeVal &IV = getValueState(PN.getIncomingValue(i));
58151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    if (IV.isUndefined()) continue;  // Doesn't influence PHI node.
58251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
58351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) {
58451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      if (IV.isOverdefined()) {   // PHI node becomes overdefined!
58551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        markOverdefined(&PN);
58651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        return;
58751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      }
58851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
58951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      if (OperandVal == 0) {   // Grab the first value...
59051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        OperandVal = IV.getConstant();
59151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      } else {                // Another value is being merged in!
59251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        // There is already a reachable operand.  If we conflict with it,
59351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        // then the PHI node becomes overdefined.  If we agree with it, we
59451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        // can continue on.
59551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
59651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        // Check to see if there are two different constants merging...
59751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        if (IV.getConstant() != OperandVal) {
59851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen          // Yes there is.  This means the PHI node is not constant.
59951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen          // You must be overdefined poor PHI.
60051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen          //
60151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen          markOverdefined(&PN);    // The PHI node now becomes overdefined
60251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen          return;    // I'm done analyzing you
60351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        }
60451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      }
60551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    }
60651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  }
60751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen
60851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // If we exited the loop, this means that the PHI node only has constant
60951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // arguments that agree with each other(and OperandVal is the constant) or
61051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // OperandVal is null because there are no defined incoming arguments.  If
61198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  // this is the case, the PHI remains undefined.
61276395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  //
61376395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  if (OperandVal)
61476395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen    markConstant(&PN, OperandVal);      // Acquire operand value
61598c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen}
61698c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
6176bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesenvoid SCCPSolver::visitReturnInst(ReturnInst &I) {
6186bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  if (I.getNumOperands() == 0) return;  // Ret void
61998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
62098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  Function *F = I.getParent()->getParent();
62151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // If we are tracking the return value of this function, merge it in.
62251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  if (!F->hasLocalLinkage())
62398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen    return;
62498c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
62551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  if (!TrackedRetVals.empty() && I.getNumOperands() == 1) {
62651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    DenseMap<Function*, LatticeVal>::iterator TFRVI =
62751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      TrackedRetVals.find(F);
62851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    if (TFRVI != TrackedRetVals.end() &&
62951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        !TFRVI->second.isOverdefined()) {
63051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      LatticeVal &IV = getValueState(I.getOperand(0));
63151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      mergeInValue(TFRVI->second, F, IV);
63298c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen      return;
63398c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen    }
6346bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen  }
6356bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen
63651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  // Handle functions that return multiple values.
63751458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  if (!TrackedMultipleRetVals.empty() && I.getNumOperands() > 1) {
63851458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
63951458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      DenseMap<std::pair<Function*, unsigned>, LatticeVal>::iterator
64051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen        It = TrackedMultipleRetVals.find(std::make_pair(F, i));
64151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      if (It == TrackedMultipleRetVals.end()) break;
64251458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      mergeInValue(It->second, F, getValueState(I.getOperand(i)));
64351458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen    }
64451458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  } else if (!TrackedMultipleRetVals.empty() &&
64551458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen             I.getNumOperands() == 1 &&
64651458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen             isa<StructType>(I.getOperand(0)->getType())) {
64798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen    for (unsigned i = 0, e = I.getOperand(0)->getType()->getNumContainedTypes();
64898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen         i != e; ++i) {
64998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen      DenseMap<std::pair<Function*, unsigned>, LatticeVal>::iterator
65098c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen        It = TrackedMultipleRetVals.find(std::make_pair(F, i));
65151458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen      if (It == TrackedMultipleRetVals.end()) break;
65257f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen      if (Value *Val = FindInsertedValue(I.getOperand(0), i, I.getContext()))
65357f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen        mergeInValue(It->second, F, getValueState(Val));
65457f1e2cee06f9b57995727d786aeb1031c5376bdJakob Stoklund Olesen    }
6552710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen  }
6562710638db2eb84cd7eefb8bb9a1b7e5c49413d45Jakob Stoklund Olesen}
65798c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
65898c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesenvoid SCCPSolver::visitTerminatorInst(TerminatorInst &TI) {
65998c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen  SmallVector<bool, 16> SuccFeasible;
66051458ed09e6db0e424cd528e10b879f59915abe4Jakob Stoklund Olesen  getFeasibleSuccessors(TI, SuccFeasible);
66198c8141b6d8fcbb9bd258ebcdd4171f55c5a8e9dJakob Stoklund Olesen
662b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick  BasicBlock *BB = TI.getParent();
663b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick
664770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  // Mark all feasible successors executable...
665770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
666b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    if (SuccFeasible[i])
667b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen      markEdgeExecutable(BB, TI.getSuccessor(i));
668b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen}
6691b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen
6701b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesenvoid SCCPSolver::visitCastInst(CastInst &I) {
6711b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  Value *V = I.getOperand(0);
6721b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  LatticeVal &VState = getValueState(V);
673f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  if (VState.isOverdefined())          // Inherit overdefinedness of operand
674f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    markOverdefined(&I);
675f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  else if (VState.isConstant())        // Propagate constant value
676db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    markConstant(&I, ConstantExpr::getCast(I.getOpcode(),
677eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen                                           VState.getConstant(), I.getType()));
678b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen}
679db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
68096dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesenvoid SCCPSolver::visitExtractValueInst(ExtractValueInst &EVI) {
681db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  Value *Aggr = EVI.getAggregateOperand();
682db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
68396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  // If the operand to the extractvalue is an undef, the result is undef.
68496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  if (isa<UndefValue>(Aggr))
685f0ac26c51173a9a1d6e5b5794107dccc4c5b5792Jakob Stoklund Olesen    return;
686eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen
687db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  // Currently only handle single-index extractvalues.
688db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  if (EVI.getNumIndices() != 1) {
6895ebca793db6262386d7464d03cdaefeb5b640ad3Jakob Stoklund Olesen    markOverdefined(&EVI);
690b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    return;
691eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen  }
692eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen
693eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen  Function *F = 0;
69496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  if (CallInst *CI = dyn_cast<CallInst>(Aggr))
69596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    F = CI->getCalledFunction();
69696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  else if (InvokeInst *II = dyn_cast<InvokeInst>(Aggr))
69796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    F = II->getCalledFunction();
698eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen
6996c8afd728eb02742ce320ecd39bcf3774d8423b7Jakob Stoklund Olesen  // TODO: If IPSCCP resolves the callee of this function, we could propagate a
700db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  // result back!
701fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  if (F == 0 || TrackedMultipleRetVals.empty()) {
70296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen    markOverdefined(&EVI);
703fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen    return;
70496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  }
705a50c539b7a9e74597da34bfaea5429a48481f18bJakob Stoklund Olesen
706b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  // See if we are tracking the result of the callee.  If not tracking this
70796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  // function (for example, it is a declaration) just move to overdefined.
708eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen  if (!TrackedMultipleRetVals.count(std::make_pair(F, *EVI.idx_begin()))) {
709612f7807c581eafb7c8105e1a55c8d839033bfb3Jakob Stoklund Olesen    markOverdefined(&EVI);
710db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    return;
711fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  }
71296dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen
713fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  // Otherwise, the value will be merged in here as a result of CallSite
71496dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  // handling.
715b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen}
716b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
71796dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesenvoid SCCPSolver::visitInsertValueInst(InsertValueInst &IVI) {
71896dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  Value *Aggr = IVI.getAggregateOperand();
71996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  Value *Val = IVI.getInsertedValueOperand();
720b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
721f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  // If the operands to the insertvalue are undef, the result is undef.
722db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  if (isa<UndefValue>(Aggr) && isa<UndefValue>(Val))
7231b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    return;
7241b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen
7251b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  // Currently only handle single-index insertvalues.
726f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  if (IVI.getNumIndices() != 1) {
727f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    markOverdefined(&IVI);
728db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    return;
729db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  }
730f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen
731f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  // Currently only handle insertvalue instructions that are in a single-use
732f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  // chain that builds up a return value.
733f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  for (const InsertValueInst *TmpIVI = &IVI; ; ) {
7341b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    if (!TmpIVI->hasOneUse()) {
7351b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen      markOverdefined(&IVI);
736f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      return;
737f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    }
738db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    const Value *V = *TmpIVI->use_begin();
739f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    if (isa<ReturnInst>(V))
740f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      break;
7411b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    TmpIVI = dyn_cast<InsertValueInst>(V);
7421b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    if (!TmpIVI) {
7437b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen      markOverdefined(&IVI);
744f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      return;
745f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    }
746f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  }
74739b5abf507b43da6b92f68b86406e0015ead18e9Frits van Bommel
748f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  // See if we are tracking the result of the callee.
749f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  Function *F = IVI.getParent()->getParent();
7507b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen  DenseMap<std::pair<Function*, unsigned>, LatticeVal>::iterator
7511b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    It = TrackedMultipleRetVals.find(std::make_pair(F, *IVI.idx_begin()));
7521b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen
753f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  // Merge in the inserted member value.
754f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  if (It != TrackedMultipleRetVals.end())
755f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    mergeInValue(It->second, F, getValueState(Val));
7567b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen
7577b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen  // Mark the aggregate result of the IVI overdefined; any tracking that we do
7587b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen  // will be done on the individual member values.
7597b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen  markOverdefined(&IVI);
7607b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen}
7617b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen
7627b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesenvoid SCCPSolver::visitSelectInst(SelectInst &I) {
7637b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen  LatticeVal &CondValue = getValueState(I.getCondition());
7647b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen  if (CondValue.isUndefined())
7657b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen    return;
7667b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen  if (CondValue.isConstant()) {
7677b41fbe87234f3ceef6ae11209730cbed4b69092Jakob Stoklund Olesen    if (ConstantInt *CondCB = dyn_cast<ConstantInt>(CondValue.getConstant())){
7681b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen      mergeInValue(&I, getValueState(CondCB->getZExtValue() ? I.getTrueValue()
7691b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen                                                          : I.getFalseValue()));
7701b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen      return;
7711b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    }
7721b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  }
773db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
774db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  // Otherwise, the condition is overdefined or a constant we can't evaluate.
7751b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  // See if we can produce something better than overdefined based on the T/F
7761b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  // value.
77739b5abf507b43da6b92f68b86406e0015ead18e9Frits van Bommel  LatticeVal &TVal = getValueState(I.getTrueValue());
778b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  LatticeVal &FVal = getValueState(I.getFalseValue());
779b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
780c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  // select ?, C, C -> C.
7815db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  if (TVal.isConstant() && FVal.isConstant() &&
7825db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen      TVal.getConstant() == FVal.getConstant()) {
7835db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen    markConstant(&I, FVal.getConstant());
7845db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen    return;
785f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  }
786f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen
787f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  if (TVal.isUndefined()) {  // select ?, undef, X -> X.
7885db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen    mergeInValue(&I, FVal);
789f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  } else if (FVal.isUndefined()) {  // select ?, X, undef -> X.
790f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    mergeInValue(&I, TVal);
791f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  } else {
792f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    markOverdefined(&I);
793f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  }
794f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen}
795f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen
796f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen// Handle BinaryOperators and Shift Instructions...
797f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesenvoid SCCPSolver::visitBinaryOperator(Instruction &I) {
798f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  LatticeVal &IV = ValueState[&I];
7995db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  if (IV.isOverdefined()) return;
800f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen
8015db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  LatticeVal &V1State = getValueState(I.getOperand(0));
802f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  LatticeVal &V2State = getValueState(I.getOperand(1));
8035db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen
804f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  if (V1State.isOverdefined() || V2State.isOverdefined()) {
805f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    // If this is an AND or OR with 0 or -1, it doesn't matter that the other
806f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    // operand is overdefined.
807f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) {
808f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      LatticeVal *NonOverdefVal = 0;
809f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen      if (!V1State.isOverdefined()) {
810549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen        NonOverdefVal = &V1State;
811549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen      } else if (!V2State.isOverdefined()) {
812b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen        NonOverdefVal = &V2State;
813b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen      }
814b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen
815b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen      if (NonOverdefVal) {
816b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen        if (NonOverdefVal->isUndefined()) {
817b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen          // Could annihilate value.
818b4666364f4db4889883d7a6c02a177ebcde7c240Jakob Stoklund Olesen          if (I.getOpcode() == Instruction::And)
819b87f91b063a0ac853735f2af3bd94fb8551a11ffJakob Stoklund Olesen            markConstant(IV, &I, Constant::getNullValue(I.getType()));
820b87f91b063a0ac853735f2af3bd94fb8551a11ffJakob Stoklund Olesen          else if (const VectorType *PT = dyn_cast<VectorType>(I.getType()))
821b87f91b063a0ac853735f2af3bd94fb8551a11ffJakob Stoklund Olesen            markConstant(IV, &I, Constant::getAllOnesValue(PT));
822549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen          else
823549019792a8b14500cab093ac8f3c5f7331e86d7Jakob Stoklund Olesen            markConstant(IV, &I,
824f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen                         Constant::getAllOnesValue(I.getType()));
825f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen          return;
826f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen        } else {
827f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen          if (I.getOpcode() == Instruction::And) {
828f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen            if (NonOverdefVal->getConstant()->isNullValue()) {
82996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen              markConstant(IV, &I, NonOverdefVal->getConstant());
83087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen              return;      // X and 0 = 0
83187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen            }
83287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen          } else {
83387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen            if (ConstantInt *CI =
83487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen                     dyn_cast<ConstantInt>(NonOverdefVal->getConstant()))
83587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen              if (CI->isAllOnesValue()) {
83687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen                markConstant(IV, &I, NonOverdefVal->getConstant());
83787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen                return;    // X or -1 = -1
83887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen              }
83987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen          }
84087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen        }
84187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen      }
84287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    }
84387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
84487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
84587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    // If both operands are PHI nodes, it is possible that this instruction has
84687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    // a constant value, despite the fact that the PHI node doesn't.  Check for
84787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    // this condition now.
84887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen    if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0)))
84987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen      if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1)))
85087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen        if (PN1->getParent() == PN2->getParent()) {
85187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen          // Since the two PHI nodes are in the same basic block, they must have
85287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen          // entries for the same predecessors.  Walk the predecessor list, and
85387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen          // if all of the incoming values are constants, and the result of
85487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen          // evaluating this expression with all incoming value pairs is the
85587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen          // same, then this expression is a constant even though the PHI node
85687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen          // is not a constant!
85787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen          LatticeVal Result;
85887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen          for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
85987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen            LatticeVal &In1 = getValueState(PN1->getIncomingValue(i));
86087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen            BasicBlock *InBlock = PN1->getIncomingBlock(i);
86187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen            LatticeVal &In2 =
86287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen              getValueState(PN2->getIncomingValueForBlock(InBlock));
86387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen
86487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen            if (In1.isOverdefined() || In2.isOverdefined()) {
86587972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen              Result.markOverdefined();
86687972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen              break;  // Cannot fold this operation over the PHI nodes!
86787972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen            } else if (In1.isConstant() && In2.isConstant()) {
86887972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen              Constant *V =
86987972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen                     ConstantExpr::get(I.getOpcode(), In1.getConstant(),
87087972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen                                              In2.getConstant());
87187972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen              if (Result.isUndefined())
87287972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen                Result.markConstant(V);
87387972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen              else if (Result.isConstant() && Result.getConstant() != V) {
87487972fa63f0e2631778166e0c258c456ec12db7cJakob Stoklund Olesen                Result.markOverdefined();
875200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen                break;
876200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen              }
877200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen            }
878200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen          }
879200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen
880200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen          // If we found a constant value here, then we know the instruction is
881200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen          // constant despite the fact that the PHI nodes are overdefined.
882200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen          if (Result.isConstant()) {
883200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen            markConstant(IV, &I, Result.getConstant());
884200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen            // Remember that this instruction is virtually using the PHI node
885200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen            // operands.
886200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen            UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I));
8873f5beede1bb97ba4e06dc300e00b70e1013e7216Jakob Stoklund Olesen            UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I));
8883f5beede1bb97ba4e06dc300e00b70e1013e7216Jakob Stoklund Olesen            return;
889200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen          } else if (Result.isUndefined()) {
890200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen            return;
891200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen          }
892200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen
893b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen          // Okay, this really is overdefined now.  Since we might have
894b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen          // speculatively thought that this was not overdefined before, and
89596dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen          // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
896b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen          // make sure to clean out any entries that we put there, for
897c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen          // efficiency.
898b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen          std::multimap<PHINode*, Instruction*>::iterator It, E;
8995db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen          tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1);
900db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen          while (It != E) {
901db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen            if (It->second == &I) {
902db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen              UsersOfOverdefinedPHIs.erase(It++);
90396dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen            } else
904874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen              ++It;
905874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen          }
906874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen          tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2);
907874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen          while (It != E) {
908db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen            if (It->second == &I) {
909db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen              UsersOfOverdefinedPHIs.erase(It++);
910db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen            } else
911db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen              ++It;
912874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen          }
913874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen        }
914b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
915db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    markOverdefined(IV, &I);
9165db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen  } else if (V1State.isConstant() && V2State.isConstant()) {
9175db4289e404d76664f8aabe2675a4cc2d7b0e98eJakob Stoklund Olesen    markConstant(IV, &I,
918db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen                ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
919db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen                                           V2State.getConstant()));
9209a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen  }
9219a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen}
9229a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen
9239a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen// Handle ICmpInst instruction...
924c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesenvoid SCCPSolver::visitCmpInst(CmpInst &I) {
925c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  LatticeVal &IV = ValueState[&I];
9269a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen  if (IV.isOverdefined()) return;
9279a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen
9289a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen  LatticeVal &V1State = getValueState(I.getOperand(0));
9299a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen  LatticeVal &V2State = getValueState(I.getOperand(1));
9309a54352879e5aaac2e2c37490e5cb7844550db8bJakob Stoklund Olesen
931db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen  if (V1State.isOverdefined() || V2State.isOverdefined()) {
932b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    // If both operands are PHI nodes, it is possible that this instruction has
933b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    // a constant value, despite the fact that the PHI node doesn't.  Check for
934b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    // this condition now.
93500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0)))
93600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1)))
937ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen        if (PN1->getParent() == PN2->getParent()) {
93800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // Since the two PHI nodes are in the same basic block, they must have
93900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // entries for the same predecessors.  Walk the predecessor list, and
94000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // if all of the incoming values are constants, and the result of
94100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // evaluating this expression with all incoming value pairs is the
94200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // same, then this expression is a constant even though the PHI node
943ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen          // is not a constant!
94400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          LatticeVal Result;
94500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) {
94600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            LatticeVal &In1 = getValueState(PN1->getIncomingValue(i));
94700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            BasicBlock *InBlock = PN1->getIncomingBlock(i);
94800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            LatticeVal &In2 =
94900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen              getValueState(PN2->getIncomingValueForBlock(InBlock));
95000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
95100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            if (In1.isOverdefined() || In2.isOverdefined()) {
95200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen              Result.markOverdefined();
95300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen              break;  // Cannot fold this operation over the PHI nodes!
954ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen            } else if (In1.isConstant() && In2.isConstant()) {
9552d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen              Constant *V = ConstantExpr::getCompare(I.getPredicate(),
95669145baf36219b07a49d8ce14b4a04870e72a123Jakob Stoklund Olesen                                                     In1.getConstant(),
9572d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen                                                     In2.getConstant());
9582d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen              if (Result.isUndefined())
9592d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen                Result.markConstant(V);
9602d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen              else if (Result.isConstant() && Result.getConstant() != V) {
96187360f73ae205854f100ba5fb7eef7b90ac3bc27Jakob Stoklund Olesen                Result.markOverdefined();
962db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen                break;
963db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen              }
964db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen            }
96500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          }
96600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
96700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // If we found a constant value here, then we know the instruction is
96800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // constant despite the fact that the PHI nodes are overdefined.
96900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          if (Result.isConstant()) {
97000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            markConstant(IV, &I, Result.getConstant());
97100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            // Remember that this instruction is virtually using the PHI node
97200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            // operands.
97300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I));
97400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I));
97500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            return;
97600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          } else if (Result.isUndefined()) {
97700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            return;
97800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          }
97900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
98000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // Okay, this really is overdefined now.  Since we might have
98100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // speculatively thought that this was not overdefined before, and
98200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs,
98300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // make sure to clean out any entries that we put there, for
98400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          // efficiency.
98500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          std::multimap<PHINode*, Instruction*>::iterator It, E;
986ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen          tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1);
987fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen          while (It != E) {
98800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            if (It->second == &I) {
989fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen              UsersOfOverdefinedPHIs.erase(It++);
9902d6d86be84ee355223ccd20b7f87a0c9971c50c9Jakob Stoklund Olesen            } else
99187360f73ae205854f100ba5fb7eef7b90ac3bc27Jakob Stoklund Olesen              ++It;
992fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen          }
993fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen          tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2);
994fd5c51342a429ecab86a645282d0b36b216c0256Jakob Stoklund Olesen          while (It != E) {
99500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            if (It->second == &I) {
99600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen              UsersOfOverdefinedPHIs.erase(It++);
99700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen            } else
99800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen              ++It;
999b4ddedce599183362b0f0333922c2fe0e163a129Jakob Stoklund Olesen          }
100000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        }
1001ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1002ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    markOverdefined(IV, &I);
100300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  } else if (V1State.isConstant() && V2State.isConstant()) {
100400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    markConstant(IV, &I, ConstantExpr::getCompare(I.getPredicate(),
100500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen                                                  V1State.getConstant(),
100600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen                                                  V2State.getConstant()));
100700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  }
100800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen}
100900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
101000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesenvoid SCCPSolver::visitExtractElementInst(ExtractElementInst &I) {
101100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // FIXME : SCCP does not handle vectors properly.
101200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  markOverdefined(&I);
101300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  return;
101400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
101500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen#if 0
101600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  LatticeVal &ValState = getValueState(I.getOperand(0));
101700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  LatticeVal &IdxState = getValueState(I.getOperand(1));
101800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
101900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (ValState.isOverdefined() || IdxState.isOverdefined())
102000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    markOverdefined(&I);
102100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  else if(ValState.isConstant() && IdxState.isConstant())
102200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(),
102300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen                                                     IdxState.getConstant()));
102400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen#endif
102500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen}
102600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
102700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesenvoid SCCPSolver::visitInsertElementInst(InsertElementInst &I) {
102800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // FIXME : SCCP does not handle vectors properly.
102900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  markOverdefined(&I);
103000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  return;
103100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen#if 0
103200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  LatticeVal &ValState = getValueState(I.getOperand(0));
103300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  LatticeVal &EltState = getValueState(I.getOperand(1));
103400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  LatticeVal &IdxState = getValueState(I.getOperand(2));
103500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
103600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (ValState.isOverdefined() || EltState.isOverdefined() ||
1037db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen      IdxState.isOverdefined())
1038db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen    markOverdefined(&I);
10390db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen  else if(ValState.isConstant() && EltState.isConstant() &&
1040ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen          IdxState.isConstant())
10415928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen    markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(),
10425928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen                                                    EltState.getConstant(),
10431f8804263ffc5e6843d81f5c7bd9c739aa90fde5Jakob Stoklund Olesen                                                    IdxState.getConstant()));
1044f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen  else if (ValState.isUndefined() && EltState.isConstant() &&
10451a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen           IdxState.isConstant())
1046b2abfa0bf30edf292a27a06e091d03983e644c9bJakob Stoklund Olesen    markConstant(&I,ConstantExpr::getInsertElement(UndefValue::get(I.getType()),
10475928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen                                                   EltState.getConstant(),
10485928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen                                                   IdxState.getConstant()));
10495928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen#endif
10505928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen}
10515928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
10525928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesenvoid SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) {
10535928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  // FIXME : SCCP does not handle vectors properly.
10541a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  markOverdefined(&I);
10555928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  return;
10565928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen#if 0
10571a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  LatticeVal &V1State   = getValueState(I.getOperand(0));
10585928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  LatticeVal &V2State   = getValueState(I.getOperand(1));
10595928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  LatticeVal &MaskState = getValueState(I.getOperand(2));
10605928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
10615928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  if (MaskState.isUndefined() ||
10625928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen      (V1State.isUndefined() && V2State.isUndefined()))
1063fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen    return;  // Undefined output if mask or both inputs undefined.
10645928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen
10655928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen  if (V1State.isOverdefined() || V2State.isOverdefined() ||
10665928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen      MaskState.isOverdefined()) {
106700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    markOverdefined(&I);
106800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  } else {
106900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // A mix of constant/undef inputs.
10701a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    Constant *V1 = V1State.isConstant() ?
10719f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen        V1State.getConstant() : UndefValue::get(I.getType());
10729f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen    Constant *V2 = V2State.isConstant() ?
10739f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen        V2State.getConstant() : UndefValue::get(I.getType());
107449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    Constant *Mask = MaskState.isConstant() ?
10759f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen      MaskState.getConstant() : UndefValue::get(I.getOperand(2)->getType());
10769f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen    markConstant(&I, ConstantExpr::getShuffleVector(V1, V2, Mask));
10779f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen  }
10789f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen#endif
10799f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen}
10809f4b893b84d9c2b56aa2abc3c96ce1e5ccc465e5Jakob Stoklund Olesen
10815928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen// Handle getelementptr instructions... if all operands are constants then we
10825928046306d8bbe7db35707c294689f515f90e56Jakob Stoklund Olesen// can turn this into a getelementptr ConstantExpr.
1083eb29157d80847c207b77910bcd40a6a6c91ca5c5Jakob Stoklund Olesen//
1084ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesenvoid SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
1085ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  LatticeVal &IV = ValueState[&I];
1086ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  if (IV.isOverdefined()) return;
1087b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
1088b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  SmallVector<Constant*, 8> Operands;
1089c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  Operands.reserve(I.getNumOperands());
109000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
109100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
109200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    LatticeVal &State = getValueState(I.getOperand(i));
109300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (State.isUndefined())
109400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      return;  // Operands are not resolved yet...
109500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    else if (State.isOverdefined()) {
109600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      markOverdefined(IV, &I);
109700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      return;
109800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    }
109900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    assert(State.isConstant() && "Unknown state!");
110000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    Operands.push_back(State.getConstant());
110100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  }
110200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
110300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  Constant *Ptr = Operands[0];
110400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  Operands.erase(Operands.begin());  // Erase the pointer from idx list...
110500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
110696dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  markConstant(IV, &I, ConstantExpr::getGetElementPtr(Ptr, &Operands[0],
1107b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen                                                      Operands.size()));
1108c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen}
1109f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen
1110f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesenvoid SCCPSolver::visitStoreInst(Instruction &SI) {
1111f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen  if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1)))
1112f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen    return;
1113f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen  GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1));
1114f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen  DenseMap<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV);
111500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (I == TrackedGlobals.end() || I->second.isOverdefined()) return;
1116f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen
1117f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen  // Get the value we are storing into the global.
1118f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen  LatticeVal &PtrVal = getValueState(SI.getOperand(0));
1119f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen
1120f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen  mergeInValue(I->second, GV, PtrVal);
1121f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen  if (I->second.isOverdefined())
1122f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen    TrackedGlobals.erase(I);      // No need to keep tracking this!
1123f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen}
1124f1c709837bd11c5383fce3b8a026a7c8eaabba86Jakob Stoklund Olesen
1125c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen
1126c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen// Handle load instructions.  If the operand is a constant pointer to a constant
1127c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen// global, we can replace the load with the loaded constant value!
1128c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesenvoid SCCPSolver::visitLoadInst(LoadInst &I) {
112996dcd95a45968de6cb05864cf91aae33169cf179Jakob Stoklund Olesen  LatticeVal &IV = ValueState[&I];
1130c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  if (IV.isOverdefined()) return;
11311b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen
1132c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen  LatticeVal &PtrVal = getValueState(I.getOperand(0));
1133f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen  if (PtrVal.isUndefined()) return;   // The pointer is not resolved yet!
11341b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen  if (PtrVal.isConstant() && !I.isVolatile()) {
11351b400e840f58489c7950f815780cf08917cecaa8Jakob Stoklund Olesen    Value *Ptr = PtrVal.getConstant();
1136f4afdfc501b7185d24a0ef184fe3d0c0bbe22e0cJakob Stoklund Olesen    // TODO: Consider a target hook for valid address spaces for this xform.
1137200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    if (isa<ConstantPointerNull>(Ptr) && I.getPointerAddressSpace() == 0) {
1138200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      // load null -> null
1139200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      markConstant(IV, &I, Constant::getNullValue(I.getType()));
1140200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      return;
1141200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    }
1142200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen
1143200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    // Transform load (constant global) into the value loaded.
1144200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
1145b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen      if (GV->isConstant()) {
1146874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen        if (GV->hasDefinitiveInitializer()) {
1147c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen          markConstant(IV, &I, GV->getInitializer());
1148ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen          return;
11499efa2a263ea470caacef1c85f6ca45e32bf516d3Jakob Stoklund Olesen        }
11509efa2a263ea470caacef1c85f6ca45e32bf516d3Jakob Stoklund Olesen      } else if (!TrackedGlobals.empty()) {
1151ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen        // If we are tracking this global, merge in the known value for it.
1152c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen        DenseMap<GlobalVariable*, LatticeVal>::iterator It =
1153874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen          TrackedGlobals.find(GV);
1154ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen        if (It != TrackedGlobals.end()) {
1155874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen          mergeInValue(IV, &I, It->second);
1156ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen          return;
1157c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen        }
1158874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen      }
1159874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    }
1160c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen
1161c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen    // Transform load (constantexpr_GEP global, 0, ...) into the value loaded.
1162874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
1163874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen      if (CE->getOpcode() == Instruction::GetElementPtr)
1164874be74179b087be36a6e7869f3aa8b70732aca1Jakob Stoklund Olesen    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
1165200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen      if (GV->isConstant() && GV->hasDefinitiveInitializer())
1166c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen        if (Constant *V =
1167200729882a47535d4c2496283d26600171531fadJakob Stoklund Olesen             ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) {
1168b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen          markConstant(IV, &I, V);
1169c66a37df73f70ec3dbed06277763624f33ee3512Jakob Stoklund Olesen          return;
1170b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen        }
1171ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  }
117200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
117300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // Otherwise we cannot say for certain what value this load will produce.
1174ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  // Bail out.
1175ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  markOverdefined(IV, &I);
117600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen}
117700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
1178708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesenvoid SCCPSolver::visitCallSite(CallSite CS) {
117900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  Function *F = CS.getCalledFunction();
118000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  Instruction *I = CS.getInstruction();
118100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
118200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // The common case is that we aren't tracking the callee, either because we
118300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // are not doing interprocedural analysis or the callee is indirect, or is
118400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  // external.  Handle these cases first.
118500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen  if (F == 0 || !F->hasLocalLinkage()) {
118600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund OlesenCallOverdefined:
118700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Void return and not tracking callee, just bail.
118800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (I->getType()->isVoidTy()) return;
118900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
119000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    // Otherwise, if we have a single return value case, and if the function is
119132668ea7a290ee1cb6bfe8cd677cdd4e5df05b4dChandler Carruth    // a declaration, maybe we can constant fold it.
119200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    if (!isa<StructType>(I->getType()) && F && F->isDeclaration() &&
119300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        canConstantFoldCallTo(F)) {
119400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
119500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      SmallVector<Constant*, 8> Operands;
119600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
119700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen           AI != E; ++AI) {
119800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        LatticeVal &State = getValueState(*AI);
119900005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        if (State.isUndefined())
120000005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          return;  // Operands are not resolved yet.
120100005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        else if (State.isOverdefined()) {
120200005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          markOverdefined(I);
120300005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen          return;
120432668ea7a290ee1cb6bfe8cd677cdd4e5df05b4dChandler Carruth        }
120500005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        assert(State.isConstant() && "Unknown state!");
120600005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen        Operands.push_back(State.getConstant());
120700005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen      }
120800005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen
1209b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen      // If we can constant fold this, mark the result of the call as a
1210b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen      // constant.
1211b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen      if (Constant *C = ConstantFoldCall(F, Operands.data(), Operands.size())) {
1212ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen        markConstant(I, C);
1213ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen        return;
1214dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen      }
1215dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    }
1216dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen
1217dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    // Otherwise, we don't know anything about this call, mark it overdefined.
1218dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    markOverdefined(I);
1219dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    return;
1220dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen  }
1221dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen
1222dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen  // If this is a single/zero retval case, see if we're tracking the function.
1223dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen  DenseMap<Function*, LatticeVal>::iterator TFRVI = TrackedRetVals.find(F);
1224dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen  if (TFRVI != TrackedRetVals.end()) {
1225dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    // If so, propagate the return value of the callee into this call result.
1226708d06f7fb5dfd9c8559aea07b042a88c65645f8Jakob Stoklund Olesen    mergeInValue(I, TFRVI->second);
1227dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen  } else if (isa<StructType>(I->getType())) {
1228dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    // Check to see if we're tracking this callee, if not, handle it in the
1229dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    // common path above.
1230dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    DenseMap<std::pair<Function*, unsigned>, LatticeVal>::iterator
1231dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    TMRVI = TrackedMultipleRetVals.find(std::make_pair(F, 0));
1232dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    if (TMRVI == TrackedMultipleRetVals.end())
1233dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen      goto CallOverdefined;
1234dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen
1235dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    // Need to mark as overdefined, otherwise it stays undefined which
1236dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    // creates extractvalue undef, <idx>
1237dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    markOverdefined(I);
1238a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen    // If we are tracking this callee, propagate the return values of the call
1239a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen    // into this call site.  We do this by walking all the uses. Single-index
12401f8804263ffc5e6843d81f5c7bd9c739aa90fde5Jakob Stoklund Olesen    // ExtractValueInst uses can be tracked; anything more complicated is
12411f8804263ffc5e6843d81f5c7bd9c739aa90fde5Jakob Stoklund Olesen    // currently handled conservatively.
12421f8804263ffc5e6843d81f5c7bd9c739aa90fde5Jakob Stoklund Olesen    for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
12431f8804263ffc5e6843d81f5c7bd9c739aa90fde5Jakob Stoklund Olesen         UI != E; ++UI) {
1244a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen      if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(*UI)) {
1245a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen        if (EVI->getNumIndices() == 1) {
1246a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen          mergeInValue(EVI,
1247a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen                  TrackedMultipleRetVals[std::make_pair(F, *EVI->idx_begin())]);
1248a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen          continue;
1249a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen        }
1250a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen      }
1251a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen      // The aggregate value is used in a way not handled here. Assume nothing.
1252a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen      markOverdefined(*UI);
1253a9c41d39d1adc92107e095aca6f851aed71b6a5fJakob Stoklund Olesen    }
1254dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen  } else {
1255dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    // Otherwise we're not tracking this callee, so handle it in the
1256dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    // common path above.
1257dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen    goto CallOverdefined;
1258dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen  }
1259dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen
1260034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Finally, if this is the first call to the function hit, mark its entry
1261034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // block executable.
1262034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  if (!BBExecutable.count(F->begin()))
1263034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    MarkBlockExecutable(F->begin());
1264034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1265034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  // Propagate information from this call site into the callee.
1266034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  CallSite::arg_iterator CAI = CS.arg_begin();
1267034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1268034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen       AI != E; ++AI, ++CAI) {
1269034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    LatticeVal &IV = ValueState[AI];
1270034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    if (AI->hasByValAttr() && !F->onlyReadsMemory()) {
1271db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen      IV.markOverdefined();
1272db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen      continue;
1273034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    }
1274034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    if (!IV.isOverdefined())
1275034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      mergeInValue(IV, AI, getValueState(*CAI));
1276034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  }
1277fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen}
1278fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen
1279fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesenvoid SCCPSolver::Solve() {
1280fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  // Process the work lists until they are empty!
1281034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  while (!BBWorkList.empty() || !InstWorkList.empty() ||
1282034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen         !OverdefinedInstWorkList.empty()) {
1283034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // Process the instruction work list...
1284034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    while (!OverdefinedInstWorkList.empty()) {
1285034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      Value *I = OverdefinedInstWorkList.back();
1286034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      OverdefinedInstWorkList.pop_back();
1287034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1288034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      DEBUG(errs() << "\nPopped off OI-WL: " << *I << '\n');
1289034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1290fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen      // "I" got into the work list because it either made the transition from
1291fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen      // bottom to constant
1292034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      //
1293034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Anything on this worklist that is overdefined need not be visited
1294034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // since all of its users will have already been marked as overdefined
1295034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Update all of the users of this instruction's value...
1296034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      //
1297034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1298034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen           UI != E; ++UI)
1299034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        OperandChangedState(*UI);
1300034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    }
1301034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // Process the instruction work list...
1302034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    while (!InstWorkList.empty()) {
1303034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      Value *I = InstWorkList.back();
1304034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      InstWorkList.pop_back();
1305034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1306034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      DEBUG(errs() << "\nPopped off I-WL: " << *I << '\n');
1307034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1308034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // "I" got into the work list because it either made the transition from
1309034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // bottom to constant
1310034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      //
1311034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Anything on this worklist that is overdefined need not be visited
1312034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // since all of its users will have already been marked as overdefined.
1313034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Update all of the users of this instruction's value...
1314034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      //
1315034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (!getValueState(I).isOverdefined())
1316034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1317034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen             UI != E; ++UI)
1318034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          OperandChangedState(*UI);
1319034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    }
1320034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1321034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    // Process the basic block work list...
1322034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    while (!BBWorkList.empty()) {
1323034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      BasicBlock *BB = BBWorkList.back();
1324db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen      BBWorkList.pop_back();
1325db529a8a5d071610f3a8b467693bc40b073e68efJakob Stoklund Olesen
1326034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      DEBUG(errs() << "\nPopped off BBWL: " << *BB << '\n');
1327034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1328034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Notify all instructions in this basic block that they are newly
1329034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // executable.
1330034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      visit(BB);
1331fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen    }
1332fe62d92b7bbaf73e576bec0c0b11cfa6c191aa87Jakob Stoklund Olesen  }
1333034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen}
1334034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1335034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// ResolvedUndefsIn - While solving the dataflow for a function, we assume
1336034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// that branches on undef values cannot reach any of their successors.
1337034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// However, this is not a safe assumption.  After we solve dataflow, this
1338034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// method should be use to handle this.  If this returns true, the solver
1339034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// should be rerun.
1340034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen///
1341034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// This method handles this by finding an unresolved branch and marking it one
1342034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// of the edges from the block as being feasible, even though the condition
1343034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// doesn't say it would otherwise be.  This allows SCCP to find the rest of the
1344034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// CFG and only slightly pessimizes the analysis results (by marking one,
1345034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen/// potentially infeasible, edge feasible).  This cannot usefully modify the
1346b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen/// constraints on the condition of the branch, as that would impact other users
1347b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen/// of the value.
1348b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen///
1349b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen/// This scan also checks for values that use undefs, whose results are actually
1350b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen/// defined.  For example, 'zext i8 undef to i32' should produce all zeros
1351b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen/// conservatively, as "(zext i8 X -> i32) & 0xFF00" must always return zero,
1352b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen/// even if X isn't defined.
1353b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesenbool SCCPSolver::ResolvedUndefsIn(Function &F) {
135449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1355b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    if (!BBExecutable.count(BB))
135649743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen      continue;
1357b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
135849743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1359b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      // Look for instructions which produce undef values.
1360b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      if (I->getType()->isVoidTy()) continue;
1361b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1362b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      LatticeVal &LV = getValueState(I);
1363b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      if (!LV.isUndefined()) continue;
136449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen
1365034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // Get the lattice values of the first two operands for use below.
1366b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      LatticeVal &Op0LV = getValueState(I->getOperand(0));
1367034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      LatticeVal Op1LV;
1368034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (I->getNumOperands() == 2) {
1369034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // If this is a two-operand instruction, and if both operands are
1370034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // undefs, the result stays undef.
137140a42a2ccaaa19a109667ed7abf224cc8733cd9cJakob Stoklund Olesen        Op1LV = getValueState(I->getOperand(1));
1372034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        if (Op0LV.isUndefined() && Op1LV.isUndefined())
1373034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          continue;
1374034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1375034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1376034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // If this is an instructions whose result is defined even if the input is
1377034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      // not fully defined, propagate the information.
1378034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      const Type *ITy = I->getType();
1379034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      switch (I->getOpcode()) {
1380034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      default: break;          // Leave the instruction as an undef.
1381034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::ZExt:
1382034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // After a zero extend, we know the top part is zero.  SExt doesn't have
1383034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // to be handled here, because we don't know whether the top part is 1's
1384b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // or 0's.
1385034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        assert(Op0LV.isUndefined());
1386034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        markForcedConstant(LV, I, Constant::getNullValue(ITy));
1387034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        return true;
1388034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::Mul:
1389034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::And:
1390034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // undef * X -> 0.   X could be zero.
1391034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // undef & X -> 0.   X could be zero.
1392034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        markForcedConstant(LV, I, Constant::getNullValue(ITy));
1393034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        return true;
1394034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1395034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::Or:
1396034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // undef | X -> -1.   X could be -1.
1397034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        if (const VectorType *PTy = dyn_cast<VectorType>(ITy))
1398034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          markForcedConstant(LV, I,
1399034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen                             Constant::getAllOnesValue(PTy));
1400034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        else
1401034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          markForcedConstant(LV, I, Constant::getAllOnesValue(ITy));
1402034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        return true;
1403034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1404034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::SDiv:
1405034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::UDiv:
1406034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::SRem:
1407b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      case Instruction::URem:
1408b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // X / undef -> undef.  No change.
1409b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // X % undef -> undef.  No change.
1410b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        if (Op1LV.isUndefined()) break;
1411b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1412b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // undef / X -> 0.   X could be maxint.
1413b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // undef % X -> 0.   X could be 1.
1414b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        markForcedConstant(LV, I, Constant::getNullValue(ITy));
1415b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        return true;
1416b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1417034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::AShr:
1418b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // undef >>s X -> undef.  No change.
1419b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        if (Op0LV.isUndefined()) break;
1420b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1421b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // X >>s undef -> X.  X could be 0, X could have the high-bit known set.
1422034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        if (Op0LV.isConstant())
1423034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          markForcedConstant(LV, I, Op0LV.getConstant());
142466446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen        else
142566446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen          markOverdefined(LV, I);
1426034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        return true;
142766446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen      case Instruction::LShr:
1428034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::Shl:
1429034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // undef >> X -> undef.  No change.
143066446c803a11a26e4bb39b74091d146ac850ae4cJakob Stoklund Olesen        // undef << X -> undef.  No change.
1431034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        if (Op0LV.isUndefined()) break;
1432034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1433034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // X >> undef -> 0.  X could be 0.
1434034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // X << undef -> 0.  X could be 0.
1435034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        markForcedConstant(LV, I, Constant::getNullValue(ITy));
1436034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        return true;
1437034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::Select:
1438034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // undef ? X : Y  -> X or Y.  There could be commonality between X/Y.
1439b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        if (Op0LV.isUndefined()) {
1440034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          if (!Op1LV.isConstant())  // Pick the constant one if there is any.
1441034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            Op1LV = getValueState(I->getOperand(2));
1442034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        } else if (Op1LV.isUndefined()) {
1443034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          // c ? undef : undef -> undef.  No change.
1444034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          Op1LV = getValueState(I->getOperand(2));
1445034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          if (Op1LV.isUndefined())
1446034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen            break;
1447034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          // Otherwise, c ? undef : x -> x.
1448034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        } else {
1449034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          // Leave Op1LV as Operand(1)'s LatticeValue.
1450034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        }
1451034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1452034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        if (Op1LV.isConstant())
1453034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          markForcedConstant(LV, I, Op1LV.getConstant());
1454034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        else
1455034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen          markOverdefined(LV, I);
1456034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        return true;
1457034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      case Instruction::Call:
1458034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // If a call has an undef result, it is because it is constant foldable
1459b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen        // but one of the inputs was undef.  Just force the result to
1460034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        // overdefined.
1461034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        markOverdefined(LV, I);
1462034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        return true;
1463034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      }
1464034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    }
1465034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1466034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    TerminatorInst *TI = BB->getTerminator();
1467034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1468034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (!BI->isConditional()) continue;
1469034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      if (!getValueState(BI->getCondition()).isUndefined())
1470034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen        continue;
147192a55f4bdd120cdd3bb5a004c792d4d24a940311Jakob Stoklund Olesen    } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
1472bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen      if (SI->getNumSuccessors()<2)   // no cases
1473bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen        continue;
1474bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen      if (!getValueState(SI->getCondition()).isUndefined())
1475bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen        continue;
1476bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen    } else {
1477bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen      continue;
1478b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    }
1479b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1480f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen    // If the edge to the second successor isn't thought to be feasible yet,
1481b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    // mark it so now.  We pick the second one so that this goes to some
1482b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    // enumerated value in a switch instead of going to the default destination.
148349743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    if (KnownFeasibleEdges.count(Edge(BB, TI->getSuccessor(1))))
1484b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen      continue;
1485b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1486b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    // Otherwise, it isn't already thought to be feasible.  Mark it as such now
1487b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    // and return.  This will make other blocks reachable, which will allow new
1488b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    // values to be discovered and existing ones to be moved in the lattice.
1489b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    markEdgeExecutable(BB, TI->getSuccessor(1));
1490b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen
1491b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    // This must be a conditional branch of switch on undef.  At this point,
1492b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    // force the old terminator to branch to the first successor.  This is
149349743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen    // required because we are now influencing the dataflow of the function with
1494b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    // the assumption that this edge is taken.  If we leave the branch condition
1495b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    // as undef, then further analysis could think the undef went another way
1496b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    // leading to an inconsistent set of conclusions.
1497b3e705f88987608d4eb49668dac0e235d04df884Jakob Stoklund Olesen    if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
14980db841f9c2b9a25fb5ecb36e350d3a802c35654cJakob Stoklund Olesen      BI->setCondition(ConstantInt::getFalse(*Context));
1499034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    } else {
1500034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      SwitchInst *SI = cast<SwitchInst>(TI);
1501034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen      SI->setCondition(SI->getCaseValue(1));
1502034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen    }
1503034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1504ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    return true;
1505ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  }
1506ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1507ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  return false;
1508ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen}
1509ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1510ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1511ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesennamespace {
1512ccfa446450c9e3e0b3591343c4c5bea1e4cdc043Jakob Stoklund Olesen  //===--------------------------------------------------------------------===//
1513ccfa446450c9e3e0b3591343c4c5bea1e4cdc043Jakob Stoklund Olesen  //
1514ccfa446450c9e3e0b3591343c4c5bea1e4cdc043Jakob Stoklund Olesen  /// SCCP Class - This class uses the SCCPSolver to implement a per-function
1515ccfa446450c9e3e0b3591343c4c5bea1e4cdc043Jakob Stoklund Olesen  /// Sparse Conditional Constant Propagator.
1516034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen  ///
1517a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen  struct SCCP : public FunctionPass {
1518a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen    static char ID; // Pass identification, replacement for typeid
151922a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen    SCCP() : FunctionPass(&ID) {}
1520034a80d065358b412cdd270e08fb6f1986e65e50Jakob Stoklund Olesen
1521a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen    // runOnFunction - Run the Sparse Conditional Constant Propagation
1522a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen    // algorithm, and return true if the function was modified.
1523a2ebf60ef2c434428af7f810b13327ab50245a67Jakob Stoklund Olesen    //
1524ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    bool runOnFunction(Function &F);
152522a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen
152622a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
15277d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen      AU.setPreservesCFG();
15287d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen    }
15297d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen  };
15307d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen} // end anonymous namespace
15317d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen
15327d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesenchar SCCP::ID = 0;
1533bdda37d7fbafe6876f248341837423a4100f95a5Jakob Stoklund Olesenstatic RegisterPass<SCCP>
15347d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund OlesenX("sccp", "Sparse Conditional Constant Propagation");
15357d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen
15367d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund Olesen// createSCCPPass - This is the public interface to this file...
15377d6b6a05b549da70b4473f015c97954c2a422724Jakob Stoklund OlesenFunctionPass *llvm::createSCCPPass() {
153849743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen  return new SCCP();
153949743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen}
154049743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen
154149743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen
154249743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen// runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm,
154349743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen// and return true if the function was modified.
154449743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesen//
154549743b18f50ac0f7e065f4754a26965d4db388deJakob Stoklund Olesenbool SCCP::runOnFunction(Function &F) {
1546ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  DEBUG(errs() << "SCCP on function '" << F.getName() << "'\n");
1547dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen  SCCPSolver Solver;
1548dab35d33ae17353cb01aaaa42abbcb28b33eb98aJakob Stoklund Olesen  Solver.setContext(&F.getContext());
1549ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen
1550ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  // Mark the first block of the function as being executable.
1551ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  Solver.MarkBlockExecutable(F.begin());
1552b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
1553770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  // Mark all arguments to the function as being overdefined.
1554770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E;++AI)
1555770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen    Solver.markOverdefined(AI);
1556770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen
1557ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  // Solve for constants.
1558770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen  bool ResolvedUndefs = true;
15595f2316a3b55f88dab2190212210770180a32aa95Jakob Stoklund Olesen  while (ResolvedUndefs) {
15606bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    Solver.Solve();
15616bfba2e5af163442a1c6b11fe14aa9df9101cfd7Jakob Stoklund Olesen    DEBUG(errs() << "RESOLVING UNDEFs\n");
1562b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick    ResolvedUndefs = Solver.ResolvedUndefsIn(F);
1563b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen  }
15641a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen
15651a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  bool MadeChanges = false;
1566b8d936bc179ddf31b6350015d74900b74db6b450Jakob Stoklund Olesen
156776395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  // If we decided that there are basic blocks that are dead in this function,
1568fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  // delete their contents now.  Note that we cannot actually delete the blocks,
156976395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  // as we cannot modify the CFG of the function.
1570fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen  //
157176395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  SmallVector<Instruction*, 512> Insts;
157276395c9a31444fa86514473e0852cdd67752d0e8Jakob Stoklund Olesen  std::map<Value*, LatticeVal> &Values = Solver.getValueMapping();
1573b853e6c3702149cdbbd6fa404334e3ba0055641aAndrew Trick
1574ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1575ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    if (!Solver.isBlockExecutable(BB)) {
1576107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen      DEBUG(errs() << "  BasicBlock Dead:" << *BB);
1577107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen      ++NumDeadBlocks;
1578107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen
1579fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen      // Delete the instructions backwards, as it has a reduced likelihood of
1580fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen      // having to update as many def-use and use-def chains.
1581c1655e1a3c3a566b91b0513b56d61b58da1e36baJakob Stoklund Olesen      for (BasicBlock::iterator I = BB->begin(), E = BB->getTerminator();
1582107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen           I != E; ++I)
1583107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen        Insts.push_back(I);
1584107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen      while (!Insts.empty()) {
1585107d366df762c18294dc00f5de916f62672353ffJakob Stoklund Olesen        Instruction *I = Insts.back();
1586bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen        Insts.pop_back();
1587bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen        if (!I->use_empty())
1588fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen          I->replaceAllUsesWith(UndefValue::get(I->getType()));
1589bf4e10f2f69db24c107cb61d6fe10ed5b2047374Jakob Stoklund Olesen        BB->getInstList().erase(I);
159022a1df6bf24c188dd637a0bb2cf9a2648806b6b1Jakob Stoklund Olesen        MadeChanges = true;
159146c83c80c5737743c955ff007fa6409804a7abf0Jakob Stoklund Olesen        ++NumInstRemoved;
1592ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen      }
1593ccdb3fcef9aeb9f683cd738afbe1cd961bb0c1efJakob Stoklund Olesen    } else {
1594b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen      // Iterate over all of the instructions in a function, replacing them with
1595b64d92e29f38002e52a22fe36ea2d488968e3537Jakob Stoklund Olesen      // constants if we have found them to be of constant values.
1596770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen      //
1597770d42de3b7643b2b4f835f32e3a16275b9fbdbaJakob Stoklund Olesen      for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
159847dbf6cef761c25cfeb0aa7d624a6f98288bb96aJakob Stoklund Olesen        Instruction *Inst = BI++;
159947dbf6cef761c25cfeb0aa7d624a6f98288bb96aJakob Stoklund Olesen        if (Inst->getType()->isVoidTy() || isa<TerminatorInst>(Inst))
1600fa89a0344bba7a0ae87d3de204d18bb1ecaa5955Jakob Stoklund Olesen          continue;
1601cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1602c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen        LatticeVal &IV = Values[Inst];
1603c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen        if (!IV.isConstant() && !IV.isUndefined())
1604c46570dc05851395829bef904bb6ddb1260400d1Jakob Stoklund Olesen          continue;
1605cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1606cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        Constant *Const = IV.isConstant()
1607cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen          ? IV.getConstant() : UndefValue::get(Inst->getType());
1608cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        DEBUG(errs() << "  Constant: " << *Const << " = " << *Inst);
1609cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1610cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        // Replaces all of the uses of a variable with uses of the constant.
1611cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        Inst->replaceAllUsesWith(Const);
1612cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1613cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        // Delete the instruction.
1614cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        Inst->eraseFromParent();
1615cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
1616af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen        // Hey, we just changed something!
161789cab93fe999f6d81b4b99a71ac797b7ecfec277Jakob Stoklund Olesen        MadeChanges = true;
1618af24964251e27c2dd863239ba66ffd967b593be5Jakob Stoklund Olesen        ++NumInstRemoved;
16194680dec5fb3a1b624f13ca9b2a555ca90a07973eJakob Stoklund Olesen      }
1620b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen    }
1621f428eb6c1b09a2322b7a577b0bf2e49dd107bceaJakob Stoklund Olesen
1622f6dff84d4e44d6c4a46c4f8a18e13c78f804547cJakob Stoklund Olesen  return MadeChanges;
1623d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen}
1624b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen
1625b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesennamespace {
1626f42b66169d75301346e3685fd2b3e45e47806367Jakob Stoklund Olesen  //===--------------------------------------------------------------------===//
1627b5fa9333431673aac2ced8dea80152349a85cf6fJakob Stoklund Olesen  //
16281b847deb26b52051de39f4cbecd224c9fbd0d1c2Jakob Stoklund Olesen  /// IPSCCP Class - This class implements interprocedural Sparse Conditional
1629bece06f0c6936527e2b1c72d09f7d3a949af9a47Jakob Stoklund Olesen  /// Constant Propagation.
16301a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  ///
16311a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen  struct IPSCCP : public ModulePass {
16321a988004dba412deb5d6b8e93b955dfc837065f0Jakob Stoklund Olesen    static char ID;
1633eda0fe8d58b0aaff5f18e7f13edfda3022384e70Jakob Stoklund Olesen    IPSCCP() : ModulePass(&ID) {}
163400005782fa860f4b48b3b5261d92541c61ee2495Jakob Stoklund Olesen    bool runOnModule(Module &M);
1635d0bb5e2ca05d9c942223bf05e5940bb0c6cc9d3fJakob Stoklund Olesen  };
1636cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen} // end anonymous namespace
1637cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen
16388a61da8a689ee95874c833af4c7aa965fab5c0a9Jakob Stoklund Olesenchar IPSCCP::ID = 0;
1639cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesenstatic RegisterPass<IPSCCP>
1640cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund OlesenY("ipsccp", "Interprocedural Sparse Conditional Constant Propagation");
1641533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen
1642533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen// createIPSCCPPass - This is the public interface to this file...
1643ba05c01dabc40373760a20c874103fc58d4377f0Jakob Stoklund OlesenModulePass *llvm::createIPSCCPPass() {
1644533f58ecdd8a4732c2f0e149387c4d8d8d4142deJakob Stoklund Olesen  return new IPSCCP();
1645cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen}
1646cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen
1647c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen
1648c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesenstatic bool AddressIsTaken(GlobalValue *GV) {
1649c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen  // Delete any dead constantexpr klingons.
1650c47690264abd6ec6bdeab86ce057e99bb5d39fe4Jakob Stoklund Olesen  GV->removeDeadConstantUsers();
1651cfafc54040cc9722995558124f253d05a038176bJakob Stoklund Olesen
1652cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen  for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end();
1653cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen       UI != E; ++UI)
1654cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen    if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
1655cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen      if (SI->getOperand(0) == GV || SI->isVolatile())
1656cba2e06d525b723849cd8e1f083eb1e59a494b4eJakob Stoklund Olesen        return true;  // Storing addr of GV.
1657    } else if (isa<InvokeInst>(*UI) || isa<CallInst>(*UI)) {
1658      // Make sure we are calling the function, not passing the address.
1659      CallSite CS = CallSite::get(cast<Instruction>(*UI));
1660      if (CS.hasArgument(GV))
1661        return true;
1662    } else if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
1663      if (LI->isVolatile())
1664        return true;
1665    } else {
1666      return true;
1667    }
1668  return false;
1669}
1670
1671bool IPSCCP::runOnModule(Module &M) {
1672  LLVMContext *Context = &M.getContext();
1673
1674  SCCPSolver Solver;
1675  Solver.setContext(Context);
1676
1677  // Loop over all functions, marking arguments to those with their addresses
1678  // taken or that are external as overdefined.
1679  //
1680  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
1681    if (!F->hasLocalLinkage() || AddressIsTaken(F)) {
1682      if (!F->isDeclaration())
1683        Solver.MarkBlockExecutable(F->begin());
1684      for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1685           AI != E; ++AI)
1686        Solver.markOverdefined(AI);
1687    } else {
1688      Solver.AddTrackedFunction(F);
1689    }
1690
1691  // Loop over global variables.  We inform the solver about any internal global
1692  // variables that do not have their 'addresses taken'.  If they don't have
1693  // their addresses taken, we can propagate constants through them.
1694  for (Module::global_iterator G = M.global_begin(), E = M.global_end();
1695       G != E; ++G)
1696    if (!G->isConstant() && G->hasLocalLinkage() && !AddressIsTaken(G))
1697      Solver.TrackValueOfGlobalVariable(G);
1698
1699  // Solve for constants.
1700  bool ResolvedUndefs = true;
1701  while (ResolvedUndefs) {
1702    Solver.Solve();
1703
1704    DEBUG(errs() << "RESOLVING UNDEFS\n");
1705    ResolvedUndefs = false;
1706    for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
1707      ResolvedUndefs |= Solver.ResolvedUndefsIn(*F);
1708  }
1709
1710  bool MadeChanges = false;
1711
1712  // Iterate over all of the instructions in the module, replacing them with
1713  // constants if we have found them to be of constant values.
1714  //
1715  SmallVector<Instruction*, 512> Insts;
1716  SmallVector<BasicBlock*, 512> BlocksToErase;
1717  std::map<Value*, LatticeVal> &Values = Solver.getValueMapping();
1718
1719  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
1720    for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
1721         AI != E; ++AI)
1722      if (!AI->use_empty()) {
1723        LatticeVal &IV = Values[AI];
1724        if (IV.isConstant() || IV.isUndefined()) {
1725          Constant *CST = IV.isConstant() ?
1726            IV.getConstant() : UndefValue::get(AI->getType());
1727          DEBUG(errs() << "***  Arg " << *AI << " = " << *CST <<"\n");
1728
1729          // Replaces all of the uses of a variable with uses of the
1730          // constant.
1731          AI->replaceAllUsesWith(CST);
1732          ++IPNumArgsElimed;
1733        }
1734      }
1735
1736    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1737      if (!Solver.isBlockExecutable(BB)) {
1738        DEBUG(errs() << "  BasicBlock Dead:" << *BB);
1739        ++IPNumDeadBlocks;
1740
1741        // Delete the instructions backwards, as it has a reduced likelihood of
1742        // having to update as many def-use and use-def chains.
1743        TerminatorInst *TI = BB->getTerminator();
1744        for (BasicBlock::iterator I = BB->begin(), E = TI; I != E; ++I)
1745          Insts.push_back(I);
1746
1747        while (!Insts.empty()) {
1748          Instruction *I = Insts.back();
1749          Insts.pop_back();
1750          if (!I->use_empty())
1751            I->replaceAllUsesWith(UndefValue::get(I->getType()));
1752          BB->getInstList().erase(I);
1753          MadeChanges = true;
1754          ++IPNumInstRemoved;
1755        }
1756
1757        for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1758          BasicBlock *Succ = TI->getSuccessor(i);
1759          if (!Succ->empty() && isa<PHINode>(Succ->begin()))
1760            TI->getSuccessor(i)->removePredecessor(BB);
1761        }
1762        if (!TI->use_empty())
1763          TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
1764        BB->getInstList().erase(TI);
1765
1766        if (&*BB != &F->front())
1767          BlocksToErase.push_back(BB);
1768        else
1769          new UnreachableInst(M.getContext(), BB);
1770
1771      } else {
1772        for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
1773          Instruction *Inst = BI++;
1774          if (Inst->getType()->isVoidTy())
1775            continue;
1776
1777          LatticeVal &IV = Values[Inst];
1778          if (!IV.isConstant() && !IV.isUndefined())
1779            continue;
1780
1781          Constant *Const = IV.isConstant()
1782            ? IV.getConstant() : UndefValue::get(Inst->getType());
1783          DEBUG(errs() << "  Constant: " << *Const << " = " << *Inst);
1784
1785          // Replaces all of the uses of a variable with uses of the
1786          // constant.
1787          Inst->replaceAllUsesWith(Const);
1788
1789          // Delete the instruction.
1790          if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst))
1791            Inst->eraseFromParent();
1792
1793          // Hey, we just changed something!
1794          MadeChanges = true;
1795          ++IPNumInstRemoved;
1796        }
1797      }
1798
1799    // Now that all instructions in the function are constant folded, erase dead
1800    // blocks, because we can now use ConstantFoldTerminator to get rid of
1801    // in-edges.
1802    for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) {
1803      // If there are any PHI nodes in this successor, drop entries for BB now.
1804      BasicBlock *DeadBB = BlocksToErase[i];
1805      while (!DeadBB->use_empty()) {
1806        Instruction *I = cast<Instruction>(DeadBB->use_back());
1807        bool Folded = ConstantFoldTerminator(I->getParent());
1808        if (!Folded) {
1809          // The constant folder may not have been able to fold the terminator
1810          // if this is a branch or switch on undef.  Fold it manually as a
1811          // branch to the first successor.
1812#ifndef NDEBUG
1813          if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
1814            assert(BI->isConditional() && isa<UndefValue>(BI->getCondition()) &&
1815                   "Branch should be foldable!");
1816          } else if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
1817            assert(isa<UndefValue>(SI->getCondition()) && "Switch should fold");
1818          } else {
1819            llvm_unreachable("Didn't fold away reference to block!");
1820          }
1821#endif
1822
1823          // Make this an uncond branch to the first successor.
1824          TerminatorInst *TI = I->getParent()->getTerminator();
1825          BranchInst::Create(TI->getSuccessor(0), TI);
1826
1827          // Remove entries in successor phi nodes to remove edges.
1828          for (unsigned i = 1, e = TI->getNumSuccessors(); i != e; ++i)
1829            TI->getSuccessor(i)->removePredecessor(TI->getParent());
1830
1831          // Remove the old terminator.
1832          TI->eraseFromParent();
1833        }
1834      }
1835
1836      // Finally, delete the basic block.
1837      F->getBasicBlockList().erase(DeadBB);
1838    }
1839    BlocksToErase.clear();
1840  }
1841
1842  // If we inferred constant or undef return values for a function, we replaced
1843  // all call uses with the inferred value.  This means we don't need to bother
1844  // actually returning anything from the function.  Replace all return
1845  // instructions with return undef.
1846  // TODO: Process multiple value ret instructions also.
1847  const DenseMap<Function*, LatticeVal> &RV = Solver.getTrackedRetVals();
1848  for (DenseMap<Function*, LatticeVal>::const_iterator I = RV.begin(),
1849         E = RV.end(); I != E; ++I)
1850    if (!I->second.isOverdefined() &&
1851        !I->first->getReturnType()->isVoidTy()) {
1852      Function *F = I->first;
1853      for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1854        if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
1855          if (!isa<UndefValue>(RI->getOperand(0)))
1856            RI->setOperand(0, UndefValue::get(F->getReturnType()));
1857    }
1858
1859  // If we infered constant or undef values for globals variables, we can delete
1860  // the global and any stores that remain to it.
1861  const DenseMap<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals();
1862  for (DenseMap<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(),
1863         E = TG.end(); I != E; ++I) {
1864    GlobalVariable *GV = I->first;
1865    assert(!I->second.isOverdefined() &&
1866           "Overdefined values should have been taken out of the map!");
1867    DEBUG(errs() << "Found that GV '" << GV->getName() << "' is constant!\n");
1868    while (!GV->use_empty()) {
1869      StoreInst *SI = cast<StoreInst>(GV->use_back());
1870      SI->eraseFromParent();
1871    }
1872    M.getGlobalList().erase(GV);
1873    ++IPNumGlobalConst;
1874  }
1875
1876  return MadeChanges;
1877}
1878