Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 1 | //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 10 | // This file implements sparse conditional constant propagation and merging: |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 11 | // |
| 12 | // Specifically, this: |
| 13 | // * Assumes values are constant unless proven otherwise |
| 14 | // * Assumes BasicBlocks are dead unless proven otherwise |
| 15 | // * Proves values to be constant, and replaces them with constants |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 16 | // * Proves conditional branches to be unconditional |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 17 | // |
| 18 | // Notice that: |
| 19 | // * This pass has a habit of making definitions be dead. It is a good idea |
| 20 | // to to run a DCE pass sometime after running this pass. |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "sccp" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | b7a5d3e | 2004-01-12 17:43:40 +0000 | [diff] [blame] | 27 | #include "llvm/Constants.h" |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 28 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 29 | #include "llvm/Instructions.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 31 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
| 35 | #include "llvm/ADT/hash_map" |
| 36 | #include "llvm/ADT/Statistic.h" |
| 37 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 38 | #include <algorithm> |
Chris Lattner | dac58ad | 2006-01-22 23:32:06 +0000 | [diff] [blame] | 39 | #include <iostream> |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 40 | #include <set> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 42 | |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 43 | // LatticeVal class - This class represents the different lattice values that an |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 44 | // instruction may occupy. It is a simple class with value semantics. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 45 | // |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 46 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 47 | |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 48 | class LatticeVal { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 49 | enum { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 50 | undefined, // This instruction has no known value |
| 51 | constant, // This instruction has a constant value |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 52 | overdefined // This instruction has an unknown value |
| 53 | } LatticeValue; // The current lattice position |
| 54 | Constant *ConstantVal; // If Constant value, the current value |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 55 | public: |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 56 | inline LatticeVal() : LatticeValue(undefined), ConstantVal(0) {} |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 57 | |
| 58 | // markOverdefined - Return true if this is a new status to be in... |
| 59 | inline bool markOverdefined() { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 60 | if (LatticeValue != overdefined) { |
| 61 | LatticeValue = overdefined; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | // markConstant - Return true if this is a new status for us... |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 68 | inline bool markConstant(Constant *V) { |
| 69 | if (LatticeValue != constant) { |
| 70 | LatticeValue = constant; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 71 | ConstantVal = V; |
| 72 | return true; |
| 73 | } else { |
Chris Lattner | b70d82f | 2001-09-07 16:43:22 +0000 | [diff] [blame] | 74 | assert(ConstantVal == V && "Marking constant with different value"); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 79 | inline bool isUndefined() const { return LatticeValue == undefined; } |
| 80 | inline bool isConstant() const { return LatticeValue == constant; } |
| 81 | inline bool isOverdefined() const { return LatticeValue == overdefined; } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 83 | inline Constant *getConstant() const { |
| 84 | assert(isConstant() && "Cannot get the constant of a non-constant!"); |
| 85 | return ConstantVal; |
| 86 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 89 | } // end anonymous namespace |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | //===----------------------------------------------------------------------===// |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 93 | // |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 94 | /// SCCPSolver - This class is a general purpose solver for Sparse Conditional |
| 95 | /// Constant Propagation. |
| 96 | /// |
| 97 | class SCCPSolver : public InstVisitor<SCCPSolver> { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 98 | std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 99 | hash_map<Value*, LatticeVal> ValueState; // The state each value is in... |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 100 | |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 101 | /// GlobalValue - If we are tracking any values for the contents of a global |
| 102 | /// variable, we keep a mapping from the constant accessor to the element of |
| 103 | /// the global, to the currently known value. If the value becomes |
| 104 | /// overdefined, it's entry is simply removed from this map. |
| 105 | hash_map<GlobalVariable*, LatticeVal> TrackedGlobals; |
| 106 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 107 | /// TrackedFunctionRetVals - If we are tracking arguments into and the return |
| 108 | /// value out of a function, it will have an entry in this map, indicating |
| 109 | /// what the known return value for the function is. |
| 110 | hash_map<Function*, LatticeVal> TrackedFunctionRetVals; |
| 111 | |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 112 | // The reason for two worklists is that overdefined is the lowest state |
| 113 | // on the lattice, and moving things to overdefined as fast as possible |
| 114 | // makes SCCP converge much faster. |
| 115 | // By having a separate worklist, we accomplish this because everything |
| 116 | // possibly overdefined will become overdefined at the soonest possible |
| 117 | // point. |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 118 | std::vector<Value*> OverdefinedInstWorkList; |
| 119 | std::vector<Value*> InstWorkList; |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 120 | |
| 121 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 122 | std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 124 | /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not |
| 125 | /// overdefined, despite the fact that the PHI node is overdefined. |
| 126 | std::multimap<PHINode*, Instruction*> UsersOfOverdefinedPHIs; |
| 127 | |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 128 | /// KnownFeasibleEdges - Entries in this set are edges which have already had |
| 129 | /// PHI nodes retriggered. |
| 130 | typedef std::pair<BasicBlock*,BasicBlock*> Edge; |
| 131 | std::set<Edge> KnownFeasibleEdges; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 132 | public: |
| 133 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 134 | /// MarkBlockExecutable - This method can be used by clients to mark all of |
| 135 | /// the blocks that are known to be intrinsically live in the processed unit. |
| 136 | void MarkBlockExecutable(BasicBlock *BB) { |
| 137 | DEBUG(std::cerr << "Marking Block Executable: " << BB->getName() << "\n"); |
| 138 | BBExecutable.insert(BB); // Basic block is executable! |
| 139 | BBWorkList.push_back(BB); // Add the block to the work list! |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 142 | /// TrackValueOfGlobalVariable - Clients can use this method to |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 143 | /// inform the SCCPSolver that it should track loads and stores to the |
| 144 | /// specified global variable if it can. This is only legal to call if |
| 145 | /// performing Interprocedural SCCP. |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 146 | void TrackValueOfGlobalVariable(GlobalVariable *GV) { |
| 147 | const Type *ElTy = GV->getType()->getElementType(); |
| 148 | if (ElTy->isFirstClassType()) { |
| 149 | LatticeVal &IV = TrackedGlobals[GV]; |
| 150 | if (!isa<UndefValue>(GV->getInitializer())) |
| 151 | IV.markConstant(GV->getInitializer()); |
| 152 | } |
| 153 | } |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 154 | |
| 155 | /// AddTrackedFunction - If the SCCP solver is supposed to track calls into |
| 156 | /// and out of the specified function (which cannot have its address taken), |
| 157 | /// this method must be called. |
| 158 | void AddTrackedFunction(Function *F) { |
| 159 | assert(F->hasInternalLinkage() && "Can only track internal functions!"); |
| 160 | // Add an entry, F -> undef. |
| 161 | TrackedFunctionRetVals[F]; |
| 162 | } |
| 163 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 164 | /// Solve - Solve for constants and executable blocks. |
| 165 | /// |
| 166 | void Solve(); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 167 | |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 168 | /// ResolveBranchesIn - While solving the dataflow for a function, we assume |
| 169 | /// that branches on undef values cannot reach any of their successors. |
| 170 | /// However, this is not a safe assumption. After we solve dataflow, this |
| 171 | /// method should be use to handle this. If this returns true, the solver |
| 172 | /// should be rerun. |
| 173 | bool ResolveBranchesIn(Function &F); |
| 174 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 175 | /// getExecutableBlocks - Once we have solved for constants, return the set of |
| 176 | /// blocks that is known to be executable. |
| 177 | std::set<BasicBlock*> &getExecutableBlocks() { |
| 178 | return BBExecutable; |
| 179 | } |
| 180 | |
| 181 | /// getValueMapping - Once we have solved for constants, return the mapping of |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 182 | /// LLVM values to LatticeVals. |
| 183 | hash_map<Value*, LatticeVal> &getValueMapping() { |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 184 | return ValueState; |
| 185 | } |
| 186 | |
Chris Lattner | 0417feb | 2004-12-11 02:53:57 +0000 | [diff] [blame] | 187 | /// getTrackedFunctionRetVals - Get the inferred return value map. |
| 188 | /// |
| 189 | const hash_map<Function*, LatticeVal> &getTrackedFunctionRetVals() { |
| 190 | return TrackedFunctionRetVals; |
| 191 | } |
| 192 | |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 193 | /// getTrackedGlobals - Get and return the set of inferred initializers for |
| 194 | /// global variables. |
| 195 | const hash_map<GlobalVariable*, LatticeVal> &getTrackedGlobals() { |
| 196 | return TrackedGlobals; |
| 197 | } |
| 198 | |
Chris Lattner | 0417feb | 2004-12-11 02:53:57 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 200 | private: |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 201 | // markConstant - Make a value be marked as "constant". If the value |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 202 | // is not already a constant, add it to the instruction work list so that |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 203 | // the users of the instruction are updated later. |
| 204 | // |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 205 | inline void markConstant(LatticeVal &IV, Value *V, Constant *C) { |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 206 | if (IV.markConstant(C)) { |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 207 | DEBUG(std::cerr << "markConstant: " << *C << ": " << *V); |
| 208 | InstWorkList.push_back(V); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 209 | } |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 211 | inline void markConstant(Value *V, Constant *C) { |
| 212 | markConstant(ValueState[V], V, C); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 215 | // markOverdefined - Make a value be marked as "overdefined". If the |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 216 | // value is not already overdefined, add it to the overdefined instruction |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 217 | // work list so that the users of the instruction are updated later. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 219 | inline void markOverdefined(LatticeVal &IV, Value *V) { |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 220 | if (IV.markOverdefined()) { |
Chris Lattner | dade2d2 | 2004-12-11 06:05:53 +0000 | [diff] [blame] | 221 | DEBUG(std::cerr << "markOverdefined: "; |
| 222 | if (Function *F = dyn_cast<Function>(V)) |
| 223 | std::cerr << "Function '" << F->getName() << "'\n"; |
| 224 | else |
| 225 | std::cerr << *V); |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 226 | // Only instructions go on the work list |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 227 | OverdefinedInstWorkList.push_back(V); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 229 | } |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 230 | inline void markOverdefined(Value *V) { |
| 231 | markOverdefined(ValueState[V], V); |
| 232 | } |
| 233 | |
| 234 | inline void mergeInValue(LatticeVal &IV, Value *V, LatticeVal &MergeWithV) { |
| 235 | if (IV.isOverdefined() || MergeWithV.isUndefined()) |
| 236 | return; // Noop. |
| 237 | if (MergeWithV.isOverdefined()) |
| 238 | markOverdefined(IV, V); |
| 239 | else if (IV.isUndefined()) |
| 240 | markConstant(IV, V, MergeWithV.getConstant()); |
| 241 | else if (IV.getConstant() != MergeWithV.getConstant()) |
| 242 | markOverdefined(IV, V); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 243 | } |
Chris Lattner | fe243eb | 2006-02-08 02:38:11 +0000 | [diff] [blame] | 244 | |
| 245 | inline void mergeInValue(Value *V, LatticeVal &MergeWithV) { |
| 246 | return mergeInValue(ValueState[V], V, MergeWithV); |
| 247 | } |
| 248 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 249 | |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 250 | // getValueState - Return the LatticeVal object that corresponds to the value. |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 251 | // This function is necessary because not all values should start out in the |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 252 | // underdefined state... Argument's should be overdefined, and |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 253 | // constants should be marked as constants. If a value is not known to be an |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 254 | // Instruction object, then use this accessor to get its value from the map. |
| 255 | // |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 256 | inline LatticeVal &getValueState(Value *V) { |
| 257 | hash_map<Value*, LatticeVal>::iterator I = ValueState.find(V); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 258 | if (I != ValueState.end()) return I->second; // Common case, in the map |
Chris Lattner | 5d356a7 | 2004-10-16 18:09:41 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 7e529e4 | 2004-11-15 05:45:33 +0000 | [diff] [blame] | 260 | if (Constant *CPV = dyn_cast<Constant>(V)) { |
| 261 | if (isa<UndefValue>(V)) { |
| 262 | // Nothing to do, remain undefined. |
| 263 | } else { |
| 264 | ValueState[CPV].markConstant(CPV); // Constants are constant |
| 265 | } |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 266 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 267 | // All others are underdefined by default... |
| 268 | return ValueState[V]; |
| 269 | } |
| 270 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 271 | // markEdgeExecutable - Mark a basic block as executable, adding it to the BB |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 272 | // work list if it is not already executable... |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 273 | // |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 274 | void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) { |
| 275 | if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second) |
| 276 | return; // This edge is already known to be executable! |
| 277 | |
| 278 | if (BBExecutable.count(Dest)) { |
| 279 | DEBUG(std::cerr << "Marking Edge Executable: " << Source->getName() |
| 280 | << " -> " << Dest->getName() << "\n"); |
| 281 | |
| 282 | // The destination is already executable, but we just made an edge |
Chris Lattner | 929c6fb | 2003-10-08 16:56:11 +0000 | [diff] [blame] | 283 | // feasible that wasn't before. Revisit the PHI nodes in the block |
| 284 | // because they have potentially new operands. |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 285 | for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I) |
| 286 | visitPHINode(*cast<PHINode>(I)); |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 287 | |
| 288 | } else { |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 289 | MarkBlockExecutable(Dest); |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 290 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 293 | // getFeasibleSuccessors - Return a vector of booleans to indicate which |
| 294 | // successors are reachable from a given terminator instruction. |
| 295 | // |
| 296 | void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs); |
| 297 | |
| 298 | // isEdgeFeasible - Return true if the control flow edge from the 'From' basic |
| 299 | // block to the 'To' basic block is currently feasible... |
| 300 | // |
| 301 | bool isEdgeFeasible(BasicBlock *From, BasicBlock *To); |
| 302 | |
| 303 | // OperandChangedState - This method is invoked on all of the users of an |
| 304 | // instruction that was just changed state somehow.... Based on this |
| 305 | // information, we need to update the specified user of this instruction. |
| 306 | // |
| 307 | void OperandChangedState(User *U) { |
| 308 | // Only instructions use other variable values! |
| 309 | Instruction &I = cast<Instruction>(*U); |
| 310 | if (BBExecutable.count(I.getParent())) // Inst is executable? |
| 311 | visit(I); |
| 312 | } |
| 313 | |
| 314 | private: |
| 315 | friend class InstVisitor<SCCPSolver>; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 316 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 317 | // visit implementations - Something changed in this instruction... Either an |
Chris Lattner | cb056de | 2001-06-29 23:56:23 +0000 | [diff] [blame] | 318 | // operand made a transition, or the instruction is newly executable. Change |
| 319 | // the value type of I to reflect these changes if appropriate. |
| 320 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 321 | void visitPHINode(PHINode &I); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 322 | |
| 323 | // Terminators |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 324 | void visitReturnInst(ReturnInst &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 325 | void visitTerminatorInst(TerminatorInst &TI); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 326 | |
Chris Lattner | b804760 | 2002-08-14 17:53:45 +0000 | [diff] [blame] | 327 | void visitCastInst(CastInst &I); |
Chris Lattner | 6e32372 | 2004-03-12 05:52:44 +0000 | [diff] [blame] | 328 | void visitSelectInst(SelectInst &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 329 | void visitBinaryOperator(Instruction &I); |
| 330 | void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); } |
Robert Bocchino | 56107e2 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 331 | void visitExtractElementInst(ExtractElementInst &I); |
Robert Bocchino | 8fcf01e | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 332 | void visitInsertElementInst(InsertElementInst &I); |
Chris Lattner | 543abdf | 2006-04-08 01:19:12 +0000 | [diff] [blame] | 333 | void visitShuffleVectorInst(ShuffleVectorInst &I); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 334 | |
| 335 | // Instructions that cannot be folded away... |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 336 | void visitStoreInst (Instruction &I); |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 337 | void visitLoadInst (LoadInst &I); |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 338 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 339 | void visitCallInst (CallInst &I) { visitCallSite(CallSite::get(&I)); } |
| 340 | void visitInvokeInst (InvokeInst &II) { |
| 341 | visitCallSite(CallSite::get(&II)); |
| 342 | visitTerminatorInst(II); |
Chris Lattner | 99b28e6 | 2003-08-27 01:08:35 +0000 | [diff] [blame] | 343 | } |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 344 | void visitCallSite (CallSite CS); |
Chris Lattner | 36143fc | 2003-09-08 18:54:55 +0000 | [diff] [blame] | 345 | void visitUnwindInst (TerminatorInst &I) { /*returns void*/ } |
Chris Lattner | 5d356a7 | 2004-10-16 18:09:41 +0000 | [diff] [blame] | 346 | void visitUnreachableInst(TerminatorInst &I) { /*returns void*/ } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 347 | void visitAllocationInst(Instruction &I) { markOverdefined(&I); } |
Chris Lattner | cda965e | 2003-10-18 05:56:52 +0000 | [diff] [blame] | 348 | void visitVANextInst (Instruction &I) { markOverdefined(&I); } |
| 349 | void visitVAArgInst (Instruction &I) { markOverdefined(&I); } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 350 | void visitFreeInst (Instruction &I) { /*returns void*/ } |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 352 | void visitInstruction(Instruction &I) { |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 353 | // If a new instruction is added to LLVM that we don't handle... |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 354 | std::cerr << "SCCP: Don't know how to handle: " << I; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 355 | markOverdefined(&I); // Just in case |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 356 | } |
Chris Lattner | cb056de | 2001-06-29 23:56:23 +0000 | [diff] [blame] | 357 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 358 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 359 | // getFeasibleSuccessors - Return a vector of booleans to indicate which |
| 360 | // successors are reachable from a given terminator instruction. |
| 361 | // |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 362 | void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI, |
| 363 | std::vector<bool> &Succs) { |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 364 | Succs.resize(TI.getNumSuccessors()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 365 | if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) { |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 366 | if (BI->isUnconditional()) { |
| 367 | Succs[0] = true; |
| 368 | } else { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 369 | LatticeVal &BCValue = getValueState(BI->getCondition()); |
Chris Lattner | 8483164 | 2004-01-12 17:40:36 +0000 | [diff] [blame] | 370 | if (BCValue.isOverdefined() || |
| 371 | (BCValue.isConstant() && !isa<ConstantBool>(BCValue.getConstant()))) { |
| 372 | // Overdefined condition variables, and branches on unfoldable constant |
| 373 | // conditions, mean the branch could go either way. |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 374 | Succs[0] = Succs[1] = true; |
| 375 | } else if (BCValue.isConstant()) { |
| 376 | // Constant condition variables mean the branch can only go a single way |
| 377 | Succs[BCValue.getConstant() == ConstantBool::False] = true; |
| 378 | } |
| 379 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 380 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) { |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 381 | // Invoke instructions successors are always executable. |
| 382 | Succs[0] = Succs[1] = true; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 383 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 384 | LatticeVal &SCValue = getValueState(SI->getCondition()); |
Chris Lattner | 8483164 | 2004-01-12 17:40:36 +0000 | [diff] [blame] | 385 | if (SCValue.isOverdefined() || // Overdefined condition? |
| 386 | (SCValue.isConstant() && !isa<ConstantInt>(SCValue.getConstant()))) { |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 387 | // All destinations are executable! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 388 | Succs.assign(TI.getNumSuccessors(), true); |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 389 | } else if (SCValue.isConstant()) { |
| 390 | Constant *CPV = SCValue.getConstant(); |
| 391 | // Make sure to skip the "default value" which isn't a value |
| 392 | for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) { |
| 393 | if (SI->getSuccessorValue(i) == CPV) {// Found the right branch... |
| 394 | Succs[i] = true; |
| 395 | return; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Constant value not equal to any of the branches... must execute |
| 400 | // default branch then... |
| 401 | Succs[0] = true; |
| 402 | } |
| 403 | } else { |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 404 | std::cerr << "SCCP: Don't know how to handle: " << TI; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 405 | Succs.assign(TI.getNumSuccessors(), true); |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 410 | // isEdgeFeasible - Return true if the control flow edge from the 'From' basic |
| 411 | // block to the 'To' basic block is currently feasible... |
| 412 | // |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 413 | bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) { |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 414 | assert(BBExecutable.count(To) && "Dest should always be alive!"); |
| 415 | |
| 416 | // Make sure the source basic block is executable!! |
| 417 | if (!BBExecutable.count(From)) return false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 418 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 419 | // Check to make sure this edge itself is actually feasible now... |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 420 | TerminatorInst *TI = From->getTerminator(); |
| 421 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 422 | if (BI->isUnconditional()) |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 423 | return true; |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 424 | else { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 425 | LatticeVal &BCValue = getValueState(BI->getCondition()); |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 426 | if (BCValue.isOverdefined()) { |
| 427 | // Overdefined condition variables mean the branch could go either way. |
| 428 | return true; |
| 429 | } else if (BCValue.isConstant()) { |
Chris Lattner | 8483164 | 2004-01-12 17:40:36 +0000 | [diff] [blame] | 430 | // Not branching on an evaluatable constant? |
| 431 | if (!isa<ConstantBool>(BCValue.getConstant())) return true; |
| 432 | |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 433 | // Constant condition variables mean the branch can only go a single way |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 434 | return BI->getSuccessor(BCValue.getConstant() == |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 435 | ConstantBool::False) == To; |
| 436 | } |
| 437 | return false; |
| 438 | } |
| 439 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) { |
| 440 | // Invoke instructions successors are always executable. |
| 441 | return true; |
| 442 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 443 | LatticeVal &SCValue = getValueState(SI->getCondition()); |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 444 | if (SCValue.isOverdefined()) { // Overdefined condition? |
| 445 | // All destinations are executable! |
| 446 | return true; |
| 447 | } else if (SCValue.isConstant()) { |
| 448 | Constant *CPV = SCValue.getConstant(); |
Chris Lattner | 8483164 | 2004-01-12 17:40:36 +0000 | [diff] [blame] | 449 | if (!isa<ConstantInt>(CPV)) |
| 450 | return true; // not a foldable constant? |
| 451 | |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 452 | // Make sure to skip the "default value" which isn't a value |
| 453 | for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) |
| 454 | if (SI->getSuccessorValue(i) == CPV) // Found the taken branch... |
| 455 | return SI->getSuccessor(i) == To; |
| 456 | |
| 457 | // Constant value not equal to any of the branches... must execute |
| 458 | // default branch then... |
| 459 | return SI->getDefaultDest() == To; |
| 460 | } |
| 461 | return false; |
| 462 | } else { |
| 463 | std::cerr << "Unknown terminator instruction: " << *TI; |
| 464 | abort(); |
| 465 | } |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 466 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 467 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 468 | // visit Implementations - Something changed in this instruction... Either an |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 469 | // operand made a transition, or the instruction is newly executable. Change |
| 470 | // the value type of I to reflect these changes if appropriate. This method |
| 471 | // makes sure to do the following actions: |
| 472 | // |
| 473 | // 1. If a phi node merges two constants in, and has conflicting value coming |
| 474 | // from different branches, or if the PHI node merges in an overdefined |
| 475 | // value, then the PHI node becomes overdefined. |
| 476 | // 2. If a phi node merges only constants in, and they all agree on value, the |
| 477 | // PHI node becomes a constant value equal to that. |
| 478 | // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant |
| 479 | // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined |
| 480 | // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined |
| 481 | // 6. If a conditional branch has a value that is constant, make the selected |
| 482 | // destination executable |
| 483 | // 7. If a conditional branch has a value that is overdefined, make all |
| 484 | // successors executable. |
| 485 | // |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 486 | void SCCPSolver::visitPHINode(PHINode &PN) { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 487 | LatticeVal &PNIV = getValueState(&PN); |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 488 | if (PNIV.isOverdefined()) { |
| 489 | // There may be instructions using this PHI node that are not overdefined |
| 490 | // themselves. If so, make sure that they know that the PHI node operand |
| 491 | // changed. |
| 492 | std::multimap<PHINode*, Instruction*>::iterator I, E; |
| 493 | tie(I, E) = UsersOfOverdefinedPHIs.equal_range(&PN); |
| 494 | if (I != E) { |
| 495 | std::vector<Instruction*> Users; |
| 496 | Users.reserve(std::distance(I, E)); |
| 497 | for (; I != E; ++I) Users.push_back(I->second); |
| 498 | while (!Users.empty()) { |
| 499 | visit(Users.back()); |
| 500 | Users.pop_back(); |
| 501 | } |
| 502 | } |
| 503 | return; // Quick exit |
| 504 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 505 | |
Chris Lattner | a2f652d | 2004-03-16 19:49:59 +0000 | [diff] [blame] | 506 | // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant, |
| 507 | // and slow us down a lot. Just mark them overdefined. |
| 508 | if (PN.getNumIncomingValues() > 64) { |
| 509 | markOverdefined(PNIV, &PN); |
| 510 | return; |
| 511 | } |
| 512 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 513 | // Look at all of the executable operands of the PHI node. If any of them |
| 514 | // are overdefined, the PHI becomes overdefined as well. If they are all |
| 515 | // constant, and they agree with each other, the PHI becomes the identical |
| 516 | // constant. If they are constant and don't agree, the PHI is overdefined. |
| 517 | // If there are no executable operands, the PHI remains undefined. |
| 518 | // |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 519 | Constant *OperandVal = 0; |
| 520 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 521 | LatticeVal &IV = getValueState(PN.getIncomingValue(i)); |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 522 | if (IV.isUndefined()) continue; // Doesn't influence PHI node. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 524 | if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) { |
Chris Lattner | 38b5ae4 | 2003-06-24 20:29:52 +0000 | [diff] [blame] | 525 | if (IV.isOverdefined()) { // PHI node becomes overdefined! |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 526 | markOverdefined(PNIV, &PN); |
Chris Lattner | 38b5ae4 | 2003-06-24 20:29:52 +0000 | [diff] [blame] | 527 | return; |
| 528 | } |
| 529 | |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 530 | if (OperandVal == 0) { // Grab the first value... |
| 531 | OperandVal = IV.getConstant(); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 532 | } else { // Another value is being merged in! |
| 533 | // There is already a reachable operand. If we conflict with it, |
| 534 | // then the PHI node becomes overdefined. If we agree with it, we |
| 535 | // can continue on. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 537 | // Check to see if there are two different constants merging... |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 538 | if (IV.getConstant() != OperandVal) { |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 539 | // Yes there is. This means the PHI node is not constant. |
| 540 | // You must be overdefined poor PHI. |
| 541 | // |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 542 | markOverdefined(PNIV, &PN); // The PHI node now becomes overdefined |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 543 | return; // I'm done analyzing you |
Chris Lattner | 5b7d42b | 2001-11-26 18:57:38 +0000 | [diff] [blame] | 544 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 549 | // If we exited the loop, this means that the PHI node only has constant |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 550 | // arguments that agree with each other(and OperandVal is the constant) or |
| 551 | // OperandVal is null because there are no defined incoming arguments. If |
| 552 | // this is the case, the PHI remains undefined. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 553 | // |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 554 | if (OperandVal) |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 555 | markConstant(PNIV, &PN, OperandVal); // Acquire operand value |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 558 | void SCCPSolver::visitReturnInst(ReturnInst &I) { |
| 559 | if (I.getNumOperands() == 0) return; // Ret void |
| 560 | |
| 561 | // If we are tracking the return value of this function, merge it in. |
| 562 | Function *F = I.getParent()->getParent(); |
| 563 | if (F->hasInternalLinkage() && !TrackedFunctionRetVals.empty()) { |
| 564 | hash_map<Function*, LatticeVal>::iterator TFRVI = |
| 565 | TrackedFunctionRetVals.find(F); |
| 566 | if (TFRVI != TrackedFunctionRetVals.end() && |
| 567 | !TFRVI->second.isOverdefined()) { |
| 568 | LatticeVal &IV = getValueState(I.getOperand(0)); |
| 569 | mergeInValue(TFRVI->second, F, IV); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 575 | void SCCPSolver::visitTerminatorInst(TerminatorInst &TI) { |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 576 | std::vector<bool> SuccFeasible; |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 577 | getFeasibleSuccessors(TI, SuccFeasible); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 578 | |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 579 | BasicBlock *BB = TI.getParent(); |
| 580 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 581 | // Mark all feasible successors executable... |
| 582 | for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i) |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 583 | if (SuccFeasible[i]) |
| 584 | markEdgeExecutable(BB, TI.getSuccessor(i)); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 587 | void SCCPSolver::visitCastInst(CastInst &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 588 | Value *V = I.getOperand(0); |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 589 | LatticeVal &VState = getValueState(V); |
Chris Lattner | b7a5d3e | 2004-01-12 17:43:40 +0000 | [diff] [blame] | 590 | if (VState.isOverdefined()) // Inherit overdefinedness of operand |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 591 | markOverdefined(&I); |
Chris Lattner | b7a5d3e | 2004-01-12 17:43:40 +0000 | [diff] [blame] | 592 | else if (VState.isConstant()) // Propagate constant value |
| 593 | markConstant(&I, ConstantExpr::getCast(VState.getConstant(), I.getType())); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 596 | void SCCPSolver::visitSelectInst(SelectInst &I) { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 597 | LatticeVal &CondValue = getValueState(I.getCondition()); |
Chris Lattner | fe243eb | 2006-02-08 02:38:11 +0000 | [diff] [blame] | 598 | if (CondValue.isUndefined()) |
| 599 | return; |
| 600 | if (CondValue.isConstant()) { |
| 601 | Value *InVal = 0; |
Chris Lattner | 6e32372 | 2004-03-12 05:52:44 +0000 | [diff] [blame] | 602 | if (CondValue.getConstant() == ConstantBool::True) { |
Chris Lattner | fe243eb | 2006-02-08 02:38:11 +0000 | [diff] [blame] | 603 | mergeInValue(&I, getValueState(I.getTrueValue())); |
| 604 | return; |
Chris Lattner | 6e32372 | 2004-03-12 05:52:44 +0000 | [diff] [blame] | 605 | } else if (CondValue.getConstant() == ConstantBool::False) { |
Chris Lattner | fe243eb | 2006-02-08 02:38:11 +0000 | [diff] [blame] | 606 | mergeInValue(&I, getValueState(I.getFalseValue())); |
| 607 | return; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // Otherwise, the condition is overdefined or a constant we can't evaluate. |
| 612 | // See if we can produce something better than overdefined based on the T/F |
| 613 | // value. |
| 614 | LatticeVal &TVal = getValueState(I.getTrueValue()); |
| 615 | LatticeVal &FVal = getValueState(I.getFalseValue()); |
| 616 | |
| 617 | // select ?, C, C -> C. |
| 618 | if (TVal.isConstant() && FVal.isConstant() && |
| 619 | TVal.getConstant() == FVal.getConstant()) { |
| 620 | markConstant(&I, FVal.getConstant()); |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | if (TVal.isUndefined()) { // select ?, undef, X -> X. |
| 625 | mergeInValue(&I, FVal); |
| 626 | } else if (FVal.isUndefined()) { // select ?, X, undef -> X. |
| 627 | mergeInValue(&I, TVal); |
| 628 | } else { |
| 629 | markOverdefined(&I); |
Chris Lattner | 6e32372 | 2004-03-12 05:52:44 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 633 | // Handle BinaryOperators and Shift Instructions... |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 634 | void SCCPSolver::visitBinaryOperator(Instruction &I) { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 635 | LatticeVal &IV = ValueState[&I]; |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 636 | if (IV.isOverdefined()) return; |
| 637 | |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 638 | LatticeVal &V1State = getValueState(I.getOperand(0)); |
| 639 | LatticeVal &V2State = getValueState(I.getOperand(1)); |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 640 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 641 | if (V1State.isOverdefined() || V2State.isOverdefined()) { |
Chris Lattner | a177c67 | 2004-12-11 23:15:19 +0000 | [diff] [blame] | 642 | // If this is an AND or OR with 0 or -1, it doesn't matter that the other |
| 643 | // operand is overdefined. |
| 644 | if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) { |
| 645 | LatticeVal *NonOverdefVal = 0; |
| 646 | if (!V1State.isOverdefined()) { |
| 647 | NonOverdefVal = &V1State; |
| 648 | } else if (!V2State.isOverdefined()) { |
| 649 | NonOverdefVal = &V2State; |
| 650 | } |
| 651 | |
| 652 | if (NonOverdefVal) { |
| 653 | if (NonOverdefVal->isUndefined()) { |
| 654 | // Could annihilate value. |
| 655 | if (I.getOpcode() == Instruction::And) |
| 656 | markConstant(IV, &I, Constant::getNullValue(I.getType())); |
| 657 | else |
| 658 | markConstant(IV, &I, ConstantInt::getAllOnesValue(I.getType())); |
| 659 | return; |
| 660 | } else { |
| 661 | if (I.getOpcode() == Instruction::And) { |
| 662 | if (NonOverdefVal->getConstant()->isNullValue()) { |
| 663 | markConstant(IV, &I, NonOverdefVal->getConstant()); |
| 664 | return; // X or 0 = -1 |
| 665 | } |
| 666 | } else { |
| 667 | if (ConstantIntegral *CI = |
| 668 | dyn_cast<ConstantIntegral>(NonOverdefVal->getConstant())) |
| 669 | if (CI->isAllOnesValue()) { |
| 670 | markConstant(IV, &I, NonOverdefVal->getConstant()); |
| 671 | return; // X or -1 = -1 |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 679 | // If both operands are PHI nodes, it is possible that this instruction has |
| 680 | // a constant value, despite the fact that the PHI node doesn't. Check for |
| 681 | // this condition now. |
| 682 | if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0))) |
| 683 | if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1))) |
| 684 | if (PN1->getParent() == PN2->getParent()) { |
| 685 | // Since the two PHI nodes are in the same basic block, they must have |
| 686 | // entries for the same predecessors. Walk the predecessor list, and |
| 687 | // if all of the incoming values are constants, and the result of |
| 688 | // evaluating this expression with all incoming value pairs is the |
| 689 | // same, then this expression is a constant even though the PHI node |
| 690 | // is not a constant! |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 691 | LatticeVal Result; |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 692 | for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 693 | LatticeVal &In1 = getValueState(PN1->getIncomingValue(i)); |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 694 | BasicBlock *InBlock = PN1->getIncomingBlock(i); |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 695 | LatticeVal &In2 = |
| 696 | getValueState(PN2->getIncomingValueForBlock(InBlock)); |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 697 | |
| 698 | if (In1.isOverdefined() || In2.isOverdefined()) { |
| 699 | Result.markOverdefined(); |
| 700 | break; // Cannot fold this operation over the PHI nodes! |
| 701 | } else if (In1.isConstant() && In2.isConstant()) { |
Chris Lattner | b16689b | 2004-01-12 19:08:43 +0000 | [diff] [blame] | 702 | Constant *V = ConstantExpr::get(I.getOpcode(), In1.getConstant(), |
| 703 | In2.getConstant()); |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 704 | if (Result.isUndefined()) |
Chris Lattner | b16689b | 2004-01-12 19:08:43 +0000 | [diff] [blame] | 705 | Result.markConstant(V); |
| 706 | else if (Result.isConstant() && Result.getConstant() != V) { |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 707 | Result.markOverdefined(); |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | // If we found a constant value here, then we know the instruction is |
| 714 | // constant despite the fact that the PHI nodes are overdefined. |
| 715 | if (Result.isConstant()) { |
| 716 | markConstant(IV, &I, Result.getConstant()); |
| 717 | // Remember that this instruction is virtually using the PHI node |
| 718 | // operands. |
| 719 | UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I)); |
| 720 | UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I)); |
| 721 | return; |
| 722 | } else if (Result.isUndefined()) { |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | // Okay, this really is overdefined now. Since we might have |
| 727 | // speculatively thought that this was not overdefined before, and |
| 728 | // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs, |
| 729 | // make sure to clean out any entries that we put there, for |
| 730 | // efficiency. |
| 731 | std::multimap<PHINode*, Instruction*>::iterator It, E; |
| 732 | tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1); |
| 733 | while (It != E) { |
| 734 | if (It->second == &I) { |
| 735 | UsersOfOverdefinedPHIs.erase(It++); |
| 736 | } else |
| 737 | ++It; |
| 738 | } |
| 739 | tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2); |
| 740 | while (It != E) { |
| 741 | if (It->second == &I) { |
| 742 | UsersOfOverdefinedPHIs.erase(It++); |
| 743 | } else |
| 744 | ++It; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | markOverdefined(IV, &I); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 749 | } else if (V1State.isConstant() && V2State.isConstant()) { |
Chris Lattner | b16689b | 2004-01-12 19:08:43 +0000 | [diff] [blame] | 750 | markConstant(IV, &I, ConstantExpr::get(I.getOpcode(), V1State.getConstant(), |
| 751 | V2State.getConstant())); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 752 | } |
| 753 | } |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 754 | |
Robert Bocchino | 56107e2 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 755 | void SCCPSolver::visitExtractElementInst(ExtractElementInst &I) { |
| 756 | LatticeVal &ValState = getValueState(I.getOperand(0)); |
| 757 | LatticeVal &IdxState = getValueState(I.getOperand(1)); |
| 758 | |
| 759 | if (ValState.isOverdefined() || IdxState.isOverdefined()) |
| 760 | markOverdefined(&I); |
| 761 | else if(ValState.isConstant() && IdxState.isConstant()) |
| 762 | markConstant(&I, ConstantExpr::getExtractElement(ValState.getConstant(), |
| 763 | IdxState.getConstant())); |
| 764 | } |
| 765 | |
Robert Bocchino | 8fcf01e | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 766 | void SCCPSolver::visitInsertElementInst(InsertElementInst &I) { |
| 767 | LatticeVal &ValState = getValueState(I.getOperand(0)); |
| 768 | LatticeVal &EltState = getValueState(I.getOperand(1)); |
| 769 | LatticeVal &IdxState = getValueState(I.getOperand(2)); |
| 770 | |
| 771 | if (ValState.isOverdefined() || EltState.isOverdefined() || |
| 772 | IdxState.isOverdefined()) |
| 773 | markOverdefined(&I); |
| 774 | else if(ValState.isConstant() && EltState.isConstant() && |
| 775 | IdxState.isConstant()) |
| 776 | markConstant(&I, ConstantExpr::getInsertElement(ValState.getConstant(), |
| 777 | EltState.getConstant(), |
| 778 | IdxState.getConstant())); |
| 779 | else if (ValState.isUndefined() && EltState.isConstant() && |
| 780 | IdxState.isConstant()) |
| 781 | markConstant(&I, ConstantExpr::getInsertElement(UndefValue::get(I.getType()), |
| 782 | EltState.getConstant(), |
| 783 | IdxState.getConstant())); |
| 784 | } |
| 785 | |
Chris Lattner | 543abdf | 2006-04-08 01:19:12 +0000 | [diff] [blame] | 786 | void SCCPSolver::visitShuffleVectorInst(ShuffleVectorInst &I) { |
| 787 | LatticeVal &V1State = getValueState(I.getOperand(0)); |
| 788 | LatticeVal &V2State = getValueState(I.getOperand(1)); |
| 789 | LatticeVal &MaskState = getValueState(I.getOperand(2)); |
| 790 | |
| 791 | if (MaskState.isUndefined() || |
| 792 | (V1State.isUndefined() && V2State.isUndefined())) |
| 793 | return; // Undefined output if mask or both inputs undefined. |
| 794 | |
| 795 | if (V1State.isOverdefined() || V2State.isOverdefined() || |
| 796 | MaskState.isOverdefined()) { |
| 797 | markOverdefined(&I); |
| 798 | } else { |
| 799 | // A mix of constant/undef inputs. |
| 800 | Constant *V1 = V1State.isConstant() ? |
| 801 | V1State.getConstant() : UndefValue::get(I.getType()); |
| 802 | Constant *V2 = V2State.isConstant() ? |
| 803 | V2State.getConstant() : UndefValue::get(I.getType()); |
| 804 | Constant *Mask = MaskState.isConstant() ? |
| 805 | MaskState.getConstant() : UndefValue::get(I.getOperand(2)->getType()); |
| 806 | markConstant(&I, ConstantExpr::getShuffleVector(V1, V2, Mask)); |
| 807 | } |
| 808 | } |
| 809 | |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 810 | // Handle getelementptr instructions... if all operands are constants then we |
| 811 | // can turn this into a getelementptr ConstantExpr. |
| 812 | // |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 813 | void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 814 | LatticeVal &IV = ValueState[&I]; |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 815 | if (IV.isOverdefined()) return; |
| 816 | |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 817 | std::vector<Constant*> Operands; |
| 818 | Operands.reserve(I.getNumOperands()); |
| 819 | |
| 820 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 821 | LatticeVal &State = getValueState(I.getOperand(i)); |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 822 | if (State.isUndefined()) |
| 823 | return; // Operands are not resolved yet... |
| 824 | else if (State.isOverdefined()) { |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 825 | markOverdefined(IV, &I); |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 826 | return; |
| 827 | } |
| 828 | assert(State.isConstant() && "Unknown state!"); |
| 829 | Operands.push_back(State.getConstant()); |
| 830 | } |
| 831 | |
| 832 | Constant *Ptr = Operands[0]; |
| 833 | Operands.erase(Operands.begin()); // Erase the pointer from idx list... |
| 834 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 835 | markConstant(IV, &I, ConstantExpr::getGetElementPtr(Ptr, Operands)); |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 836 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 837 | |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 838 | void SCCPSolver::visitStoreInst(Instruction &SI) { |
| 839 | if (TrackedGlobals.empty() || !isa<GlobalVariable>(SI.getOperand(1))) |
| 840 | return; |
| 841 | GlobalVariable *GV = cast<GlobalVariable>(SI.getOperand(1)); |
| 842 | hash_map<GlobalVariable*, LatticeVal>::iterator I = TrackedGlobals.find(GV); |
| 843 | if (I == TrackedGlobals.end() || I->second.isOverdefined()) return; |
| 844 | |
| 845 | // Get the value we are storing into the global. |
| 846 | LatticeVal &PtrVal = getValueState(SI.getOperand(0)); |
| 847 | |
| 848 | mergeInValue(I->second, GV, PtrVal); |
| 849 | if (I->second.isOverdefined()) |
| 850 | TrackedGlobals.erase(I); // No need to keep tracking this! |
| 851 | } |
| 852 | |
| 853 | |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 854 | // Handle load instructions. If the operand is a constant pointer to a constant |
| 855 | // global, we can replace the load with the loaded constant value! |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 856 | void SCCPSolver::visitLoadInst(LoadInst &I) { |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 857 | LatticeVal &IV = ValueState[&I]; |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 858 | if (IV.isOverdefined()) return; |
| 859 | |
Chris Lattner | ef36dfd | 2004-11-15 05:03:30 +0000 | [diff] [blame] | 860 | LatticeVal &PtrVal = getValueState(I.getOperand(0)); |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 861 | if (PtrVal.isUndefined()) return; // The pointer is not resolved yet! |
| 862 | if (PtrVal.isConstant() && !I.isVolatile()) { |
| 863 | Value *Ptr = PtrVal.getConstant(); |
Chris Lattner | c76d803 | 2004-03-07 22:16:24 +0000 | [diff] [blame] | 864 | if (isa<ConstantPointerNull>(Ptr)) { |
| 865 | // load null -> null |
| 866 | markConstant(IV, &I, Constant::getNullValue(I.getType())); |
| 867 | return; |
| 868 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 869 | |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 870 | // Transform load (constant global) into the value loaded. |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 871 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) { |
| 872 | if (GV->isConstant()) { |
| 873 | if (!GV->isExternal()) { |
| 874 | markConstant(IV, &I, GV->getInitializer()); |
| 875 | return; |
| 876 | } |
| 877 | } else if (!TrackedGlobals.empty()) { |
| 878 | // If we are tracking this global, merge in the known value for it. |
| 879 | hash_map<GlobalVariable*, LatticeVal>::iterator It = |
| 880 | TrackedGlobals.find(GV); |
| 881 | if (It != TrackedGlobals.end()) { |
| 882 | mergeInValue(IV, &I, It->second); |
| 883 | return; |
| 884 | } |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 885 | } |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 886 | } |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 887 | |
| 888 | // Transform load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 889 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 890 | if (CE->getOpcode() == Instruction::GetElementPtr) |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 891 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 892 | if (GV->isConstant() && !GV->isExternal()) |
| 893 | if (Constant *V = |
Chris Lattner | ebe6120 | 2005-09-26 05:28:52 +0000 | [diff] [blame] | 894 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) { |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 895 | markConstant(IV, &I, V); |
| 896 | return; |
| 897 | } |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | // Otherwise we cannot say for certain what value this load will produce. |
| 901 | // Bail out. |
| 902 | markOverdefined(IV, &I); |
| 903 | } |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 904 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 905 | void SCCPSolver::visitCallSite(CallSite CS) { |
| 906 | Function *F = CS.getCalledFunction(); |
| 907 | |
| 908 | // If we are tracking this function, we must make sure to bind arguments as |
| 909 | // appropriate. |
| 910 | hash_map<Function*, LatticeVal>::iterator TFRVI =TrackedFunctionRetVals.end(); |
| 911 | if (F && F->hasInternalLinkage()) |
| 912 | TFRVI = TrackedFunctionRetVals.find(F); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 913 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 914 | if (TFRVI != TrackedFunctionRetVals.end()) { |
| 915 | // If this is the first call to the function hit, mark its entry block |
| 916 | // executable. |
| 917 | if (!BBExecutable.count(F->begin())) |
| 918 | MarkBlockExecutable(F->begin()); |
| 919 | |
| 920 | CallSite::arg_iterator CAI = CS.arg_begin(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 921 | for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 922 | AI != E; ++AI, ++CAI) { |
| 923 | LatticeVal &IV = ValueState[AI]; |
| 924 | if (!IV.isOverdefined()) |
| 925 | mergeInValue(IV, AI, getValueState(*CAI)); |
| 926 | } |
| 927 | } |
| 928 | Instruction *I = CS.getInstruction(); |
| 929 | if (I->getType() == Type::VoidTy) return; |
| 930 | |
| 931 | LatticeVal &IV = ValueState[I]; |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 932 | if (IV.isOverdefined()) return; |
| 933 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 934 | // Propagate the return value of the function to the value of the instruction. |
| 935 | if (TFRVI != TrackedFunctionRetVals.end()) { |
| 936 | mergeInValue(IV, I, TFRVI->second); |
| 937 | return; |
| 938 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 939 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 940 | if (F == 0 || !F->isExternal() || !canConstantFoldCallTo(F)) { |
| 941 | markOverdefined(IV, I); |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 942 | return; |
| 943 | } |
| 944 | |
| 945 | std::vector<Constant*> Operands; |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 946 | Operands.reserve(I->getNumOperands()-1); |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 947 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 948 | for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end(); |
| 949 | AI != E; ++AI) { |
| 950 | LatticeVal &State = getValueState(*AI); |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 951 | if (State.isUndefined()) |
| 952 | return; // Operands are not resolved yet... |
| 953 | else if (State.isOverdefined()) { |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 954 | markOverdefined(IV, I); |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 955 | return; |
| 956 | } |
| 957 | assert(State.isConstant() && "Unknown state!"); |
| 958 | Operands.push_back(State.getConstant()); |
| 959 | } |
| 960 | |
| 961 | if (Constant *C = ConstantFoldCall(F, Operands)) |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 962 | markConstant(IV, I, C); |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 963 | else |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 964 | markOverdefined(IV, I); |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 965 | } |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 966 | |
| 967 | |
| 968 | void SCCPSolver::Solve() { |
| 969 | // Process the work lists until they are empty! |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 970 | while (!BBWorkList.empty() || !InstWorkList.empty() || |
Jeff Cohen | 9d80930 | 2005-04-23 21:38:35 +0000 | [diff] [blame] | 971 | !OverdefinedInstWorkList.empty()) { |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 972 | // Process the instruction work list... |
| 973 | while (!OverdefinedInstWorkList.empty()) { |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 974 | Value *I = OverdefinedInstWorkList.back(); |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 975 | OverdefinedInstWorkList.pop_back(); |
| 976 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 977 | DEBUG(std::cerr << "\nPopped off OI-WL: " << *I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 978 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 979 | // "I" got into the work list because it either made the transition from |
| 980 | // bottom to constant |
| 981 | // |
| 982 | // Anything on this worklist that is overdefined need not be visited |
| 983 | // since all of its users will have already been marked as overdefined |
| 984 | // Update all of the users of this instruction's value... |
| 985 | // |
| 986 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 987 | UI != E; ++UI) |
| 988 | OperandChangedState(*UI); |
| 989 | } |
| 990 | // Process the instruction work list... |
| 991 | while (!InstWorkList.empty()) { |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 992 | Value *I = InstWorkList.back(); |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 993 | InstWorkList.pop_back(); |
| 994 | |
| 995 | DEBUG(std::cerr << "\nPopped off I-WL: " << *I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 996 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 997 | // "I" got into the work list because it either made the transition from |
| 998 | // bottom to constant |
| 999 | // |
| 1000 | // Anything on this worklist that is overdefined need not be visited |
| 1001 | // since all of its users will have already been marked as overdefined. |
| 1002 | // Update all of the users of this instruction's value... |
| 1003 | // |
| 1004 | if (!getValueState(I).isOverdefined()) |
| 1005 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 1006 | UI != E; ++UI) |
| 1007 | OperandChangedState(*UI); |
| 1008 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1010 | // Process the basic block work list... |
| 1011 | while (!BBWorkList.empty()) { |
| 1012 | BasicBlock *BB = BBWorkList.back(); |
| 1013 | BBWorkList.pop_back(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1015 | DEBUG(std::cerr << "\nPopped off BBWL: " << *BB); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1017 | // Notify all instructions in this basic block that they are newly |
| 1018 | // executable. |
| 1019 | visit(BB); |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 1024 | /// ResolveBranchesIn - While solving the dataflow for a function, we assume |
| 1025 | /// that branches on undef values cannot reach any of their successors. |
| 1026 | /// However, this is not a safe assumption. After we solve dataflow, this |
| 1027 | /// method should be use to handle this. If this returns true, the solver |
| 1028 | /// should be rerun. |
| 1029 | bool SCCPSolver::ResolveBranchesIn(Function &F) { |
| 1030 | bool BranchesResolved = false; |
Chris Lattner | dade2d2 | 2004-12-11 06:05:53 +0000 | [diff] [blame] | 1031 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 1032 | if (BBExecutable.count(BB)) { |
| 1033 | TerminatorInst *TI = BB->getTerminator(); |
| 1034 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 1035 | if (BI->isConditional()) { |
| 1036 | LatticeVal &BCValue = getValueState(BI->getCondition()); |
| 1037 | if (BCValue.isUndefined()) { |
| 1038 | BI->setCondition(ConstantBool::True); |
| 1039 | BranchesResolved = true; |
| 1040 | visit(BI); |
| 1041 | } |
| 1042 | } |
| 1043 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 1044 | LatticeVal &SCValue = getValueState(SI->getCondition()); |
| 1045 | if (SCValue.isUndefined()) { |
| 1046 | const Type *CondTy = SI->getCondition()->getType(); |
| 1047 | SI->setCondition(Constant::getNullValue(CondTy)); |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 1048 | BranchesResolved = true; |
Chris Lattner | dade2d2 | 2004-12-11 06:05:53 +0000 | [diff] [blame] | 1049 | visit(SI); |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 1050 | } |
| 1051 | } |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 1052 | } |
Chris Lattner | dade2d2 | 2004-12-11 06:05:53 +0000 | [diff] [blame] | 1053 | |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 1054 | return BranchesResolved; |
| 1055 | } |
| 1056 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1057 | |
| 1058 | namespace { |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1059 | Statistic<> NumInstRemoved("sccp", "Number of instructions removed"); |
| 1060 | Statistic<> NumDeadBlocks ("sccp", "Number of basic blocks unreachable"); |
| 1061 | |
Chris Lattner | 1405181 | 2004-11-15 07:15:04 +0000 | [diff] [blame] | 1062 | //===--------------------------------------------------------------------===// |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1063 | // |
Chris Lattner | 1405181 | 2004-11-15 07:15:04 +0000 | [diff] [blame] | 1064 | /// SCCP Class - This class uses the SCCPSolver to implement a per-function |
| 1065 | /// Sparse Conditional COnstant Propagator. |
| 1066 | /// |
| 1067 | struct SCCP : public FunctionPass { |
| 1068 | // runOnFunction - Run the Sparse Conditional Constant Propagation |
| 1069 | // algorithm, and return true if the function was modified. |
| 1070 | // |
| 1071 | bool runOnFunction(Function &F); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1072 | |
Chris Lattner | 1405181 | 2004-11-15 07:15:04 +0000 | [diff] [blame] | 1073 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 1074 | AU.setPreservesCFG(); |
| 1075 | } |
| 1076 | }; |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 1078 | RegisterPass<SCCP> X("sccp", "Sparse Conditional Constant Propagation"); |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1079 | } // end anonymous namespace |
| 1080 | |
| 1081 | |
| 1082 | // createSCCPPass - This is the public interface to this file... |
| 1083 | FunctionPass *llvm::createSCCPPass() { |
| 1084 | return new SCCP(); |
| 1085 | } |
| 1086 | |
| 1087 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1088 | // runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm, |
| 1089 | // and return true if the function was modified. |
| 1090 | // |
| 1091 | bool SCCP::runOnFunction(Function &F) { |
Chris Lattner | 7e529e4 | 2004-11-15 05:45:33 +0000 | [diff] [blame] | 1092 | DEBUG(std::cerr << "SCCP on function '" << F.getName() << "'\n"); |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1093 | SCCPSolver Solver; |
| 1094 | |
| 1095 | // Mark the first block of the function as being executable. |
| 1096 | Solver.MarkBlockExecutable(F.begin()); |
| 1097 | |
Chris Lattner | 7e529e4 | 2004-11-15 05:45:33 +0000 | [diff] [blame] | 1098 | // Mark all arguments to the function as being overdefined. |
| 1099 | hash_map<Value*, LatticeVal> &Values = Solver.getValueMapping(); |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1100 | for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end(); AI != E; ++AI) |
Chris Lattner | 7e529e4 | 2004-11-15 05:45:33 +0000 | [diff] [blame] | 1101 | Values[AI].markOverdefined(); |
| 1102 | |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1103 | // Solve for constants. |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 1104 | bool ResolvedBranches = true; |
| 1105 | while (ResolvedBranches) { |
| 1106 | Solver.Solve(); |
Chris Lattner | dade2d2 | 2004-12-11 06:05:53 +0000 | [diff] [blame] | 1107 | DEBUG(std::cerr << "RESOLVING UNDEF BRANCHES\n"); |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 1108 | ResolvedBranches = Solver.ResolveBranchesIn(F); |
| 1109 | } |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1110 | |
Chris Lattner | 7e529e4 | 2004-11-15 05:45:33 +0000 | [diff] [blame] | 1111 | bool MadeChanges = false; |
| 1112 | |
| 1113 | // If we decided that there are basic blocks that are dead in this function, |
| 1114 | // delete their contents now. Note that we cannot actually delete the blocks, |
| 1115 | // as we cannot modify the CFG of the function. |
| 1116 | // |
| 1117 | std::set<BasicBlock*> &ExecutableBBs = Solver.getExecutableBlocks(); |
| 1118 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 1119 | if (!ExecutableBBs.count(BB)) { |
| 1120 | DEBUG(std::cerr << " BasicBlock Dead:" << *BB); |
Chris Lattner | b77d5d8 | 2004-11-15 07:02:42 +0000 | [diff] [blame] | 1121 | ++NumDeadBlocks; |
| 1122 | |
Chris Lattner | 7e529e4 | 2004-11-15 05:45:33 +0000 | [diff] [blame] | 1123 | // Delete the instructions backwards, as it has a reduced likelihood of |
| 1124 | // having to update as many def-use and use-def chains. |
| 1125 | std::vector<Instruction*> Insts; |
| 1126 | for (BasicBlock::iterator I = BB->begin(), E = BB->getTerminator(); |
| 1127 | I != E; ++I) |
| 1128 | Insts.push_back(I); |
| 1129 | while (!Insts.empty()) { |
| 1130 | Instruction *I = Insts.back(); |
| 1131 | Insts.pop_back(); |
| 1132 | if (!I->use_empty()) |
| 1133 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 1134 | BB->getInstList().erase(I); |
| 1135 | MadeChanges = true; |
Chris Lattner | b77d5d8 | 2004-11-15 07:02:42 +0000 | [diff] [blame] | 1136 | ++NumInstRemoved; |
Chris Lattner | 7e529e4 | 2004-11-15 05:45:33 +0000 | [diff] [blame] | 1137 | } |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1138 | } else { |
| 1139 | // Iterate over all of the instructions in a function, replacing them with |
| 1140 | // constants if we have found them to be of constant values. |
| 1141 | // |
| 1142 | for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) { |
| 1143 | Instruction *Inst = BI++; |
| 1144 | if (Inst->getType() != Type::VoidTy) { |
| 1145 | LatticeVal &IV = Values[Inst]; |
| 1146 | if (IV.isConstant() || IV.isUndefined() && |
| 1147 | !isa<TerminatorInst>(Inst)) { |
| 1148 | Constant *Const = IV.isConstant() |
| 1149 | ? IV.getConstant() : UndefValue::get(Inst->getType()); |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1150 | DEBUG(std::cerr << " Constant: " << *Const << " = " << *Inst); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1151 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1152 | // Replaces all of the uses of a variable with uses of the constant. |
| 1153 | Inst->replaceAllUsesWith(Const); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1154 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1155 | // Delete the instruction. |
| 1156 | BB->getInstList().erase(Inst); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1157 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1158 | // Hey, we just changed something! |
| 1159 | MadeChanges = true; |
| 1160 | ++NumInstRemoved; |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1161 | } |
Chris Lattner | 82bec2c | 2004-11-15 04:44:20 +0000 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | return MadeChanges; |
| 1167 | } |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1168 | |
| 1169 | namespace { |
| 1170 | Statistic<> IPNumInstRemoved("ipsccp", "Number of instructions removed"); |
| 1171 | Statistic<> IPNumDeadBlocks ("ipsccp", "Number of basic blocks unreachable"); |
| 1172 | Statistic<> IPNumArgsElimed ("ipsccp", |
| 1173 | "Number of arguments constant propagated"); |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 1174 | Statistic<> IPNumGlobalConst("ipsccp", |
| 1175 | "Number of globals found to be constant"); |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1176 | |
| 1177 | //===--------------------------------------------------------------------===// |
| 1178 | // |
| 1179 | /// IPSCCP Class - This class implements interprocedural Sparse Conditional |
| 1180 | /// Constant Propagation. |
| 1181 | /// |
| 1182 | struct IPSCCP : public ModulePass { |
| 1183 | bool runOnModule(Module &M); |
| 1184 | }; |
| 1185 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 1186 | RegisterPass<IPSCCP> |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1187 | Y("ipsccp", "Interprocedural Sparse Conditional Constant Propagation"); |
| 1188 | } // end anonymous namespace |
| 1189 | |
| 1190 | // createIPSCCPPass - This is the public interface to this file... |
| 1191 | ModulePass *llvm::createIPSCCPPass() { |
| 1192 | return new IPSCCP(); |
| 1193 | } |
| 1194 | |
| 1195 | |
| 1196 | static bool AddressIsTaken(GlobalValue *GV) { |
Chris Lattner | 7d27fc0 | 2005-04-19 19:16:19 +0000 | [diff] [blame] | 1197 | // Delete any dead constantexpr klingons. |
| 1198 | GV->removeDeadConstantUsers(); |
| 1199 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1200 | for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); |
| 1201 | UI != E; ++UI) |
| 1202 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 1203 | if (SI->getOperand(0) == GV || SI->isVolatile()) |
| 1204 | return true; // Storing addr of GV. |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1205 | } else if (isa<InvokeInst>(*UI) || isa<CallInst>(*UI)) { |
| 1206 | // Make sure we are calling the function, not passing the address. |
| 1207 | CallSite CS = CallSite::get(cast<Instruction>(*UI)); |
| 1208 | for (CallSite::arg_iterator AI = CS.arg_begin(), |
| 1209 | E = CS.arg_end(); AI != E; ++AI) |
| 1210 | if (*AI == GV) |
| 1211 | return true; |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 1212 | } else if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) { |
| 1213 | if (LI->isVolatile()) |
| 1214 | return true; |
| 1215 | } else { |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1216 | return true; |
| 1217 | } |
| 1218 | return false; |
| 1219 | } |
| 1220 | |
| 1221 | bool IPSCCP::runOnModule(Module &M) { |
| 1222 | SCCPSolver Solver; |
| 1223 | |
| 1224 | // Loop over all functions, marking arguments to those with their addresses |
| 1225 | // taken or that are external as overdefined. |
| 1226 | // |
| 1227 | hash_map<Value*, LatticeVal> &Values = Solver.getValueMapping(); |
| 1228 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) |
| 1229 | if (!F->hasInternalLinkage() || AddressIsTaken(F)) { |
| 1230 | if (!F->isExternal()) |
| 1231 | Solver.MarkBlockExecutable(F->begin()); |
Chris Lattner | 7d27fc0 | 2005-04-19 19:16:19 +0000 | [diff] [blame] | 1232 | for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); |
| 1233 | AI != E; ++AI) |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1234 | Values[AI].markOverdefined(); |
| 1235 | } else { |
| 1236 | Solver.AddTrackedFunction(F); |
| 1237 | } |
| 1238 | |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 1239 | // Loop over global variables. We inform the solver about any internal global |
| 1240 | // variables that do not have their 'addresses taken'. If they don't have |
| 1241 | // their addresses taken, we can propagate constants through them. |
Chris Lattner | 7d27fc0 | 2005-04-19 19:16:19 +0000 | [diff] [blame] | 1242 | for (Module::global_iterator G = M.global_begin(), E = M.global_end(); |
| 1243 | G != E; ++G) |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 1244 | if (!G->isConstant() && G->hasInternalLinkage() && !AddressIsTaken(G)) |
| 1245 | Solver.TrackValueOfGlobalVariable(G); |
| 1246 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1247 | // Solve for constants. |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 1248 | bool ResolvedBranches = true; |
| 1249 | while (ResolvedBranches) { |
| 1250 | Solver.Solve(); |
| 1251 | |
Chris Lattner | dade2d2 | 2004-12-11 06:05:53 +0000 | [diff] [blame] | 1252 | DEBUG(std::cerr << "RESOLVING UNDEF BRANCHES\n"); |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 1253 | ResolvedBranches = false; |
| 1254 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) |
| 1255 | ResolvedBranches |= Solver.ResolveBranchesIn(*F); |
| 1256 | } |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1257 | |
| 1258 | bool MadeChanges = false; |
| 1259 | |
| 1260 | // Iterate over all of the instructions in the module, replacing them with |
| 1261 | // constants if we have found them to be of constant values. |
| 1262 | // |
| 1263 | std::set<BasicBlock*> &ExecutableBBs = Solver.getExecutableBlocks(); |
| 1264 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
Chris Lattner | 7d27fc0 | 2005-04-19 19:16:19 +0000 | [diff] [blame] | 1265 | for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); |
| 1266 | AI != E; ++AI) |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1267 | if (!AI->use_empty()) { |
| 1268 | LatticeVal &IV = Values[AI]; |
| 1269 | if (IV.isConstant() || IV.isUndefined()) { |
| 1270 | Constant *CST = IV.isConstant() ? |
| 1271 | IV.getConstant() : UndefValue::get(AI->getType()); |
| 1272 | DEBUG(std::cerr << "*** Arg " << *AI << " = " << *CST <<"\n"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1273 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1274 | // Replaces all of the uses of a variable with uses of the |
| 1275 | // constant. |
| 1276 | AI->replaceAllUsesWith(CST); |
| 1277 | ++IPNumArgsElimed; |
| 1278 | } |
| 1279 | } |
| 1280 | |
Chris Lattner | 5f9e8b4 | 2004-12-10 22:29:08 +0000 | [diff] [blame] | 1281 | std::vector<BasicBlock*> BlocksToErase; |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1282 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 1283 | if (!ExecutableBBs.count(BB)) { |
| 1284 | DEBUG(std::cerr << " BasicBlock Dead:" << *BB); |
| 1285 | ++IPNumDeadBlocks; |
Chris Lattner | fc6ac50 | 2004-12-10 20:41:50 +0000 | [diff] [blame] | 1286 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1287 | // Delete the instructions backwards, as it has a reduced likelihood of |
| 1288 | // having to update as many def-use and use-def chains. |
| 1289 | std::vector<Instruction*> Insts; |
Chris Lattner | 5f9e8b4 | 2004-12-10 22:29:08 +0000 | [diff] [blame] | 1290 | TerminatorInst *TI = BB->getTerminator(); |
| 1291 | for (BasicBlock::iterator I = BB->begin(), E = TI; I != E; ++I) |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1292 | Insts.push_back(I); |
Chris Lattner | 5f9e8b4 | 2004-12-10 22:29:08 +0000 | [diff] [blame] | 1293 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1294 | while (!Insts.empty()) { |
| 1295 | Instruction *I = Insts.back(); |
| 1296 | Insts.pop_back(); |
| 1297 | if (!I->use_empty()) |
| 1298 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 1299 | BB->getInstList().erase(I); |
| 1300 | MadeChanges = true; |
| 1301 | ++IPNumInstRemoved; |
| 1302 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1303 | |
Chris Lattner | 5f9e8b4 | 2004-12-10 22:29:08 +0000 | [diff] [blame] | 1304 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { |
| 1305 | BasicBlock *Succ = TI->getSuccessor(i); |
| 1306 | if (Succ->begin() != Succ->end() && isa<PHINode>(Succ->begin())) |
| 1307 | TI->getSuccessor(i)->removePredecessor(BB); |
| 1308 | } |
Chris Lattner | 0417feb | 2004-12-11 02:53:57 +0000 | [diff] [blame] | 1309 | if (!TI->use_empty()) |
| 1310 | TI->replaceAllUsesWith(UndefValue::get(TI->getType())); |
Chris Lattner | 5f9e8b4 | 2004-12-10 22:29:08 +0000 | [diff] [blame] | 1311 | BB->getInstList().erase(TI); |
| 1312 | |
Chris Lattner | 864737b | 2004-12-11 05:32:19 +0000 | [diff] [blame] | 1313 | if (&*BB != &F->front()) |
| 1314 | BlocksToErase.push_back(BB); |
| 1315 | else |
| 1316 | new UnreachableInst(BB); |
| 1317 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1318 | } else { |
| 1319 | for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) { |
| 1320 | Instruction *Inst = BI++; |
| 1321 | if (Inst->getType() != Type::VoidTy) { |
| 1322 | LatticeVal &IV = Values[Inst]; |
| 1323 | if (IV.isConstant() || IV.isUndefined() && |
| 1324 | !isa<TerminatorInst>(Inst)) { |
| 1325 | Constant *Const = IV.isConstant() |
| 1326 | ? IV.getConstant() : UndefValue::get(Inst->getType()); |
| 1327 | DEBUG(std::cerr << " Constant: " << *Const << " = " << *Inst); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1328 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1329 | // Replaces all of the uses of a variable with uses of the |
| 1330 | // constant. |
| 1331 | Inst->replaceAllUsesWith(Const); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1332 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1333 | // Delete the instruction. |
| 1334 | if (!isa<TerminatorInst>(Inst) && !isa<CallInst>(Inst)) |
| 1335 | BB->getInstList().erase(Inst); |
| 1336 | |
| 1337 | // Hey, we just changed something! |
| 1338 | MadeChanges = true; |
| 1339 | ++IPNumInstRemoved; |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | } |
Chris Lattner | 5f9e8b4 | 2004-12-10 22:29:08 +0000 | [diff] [blame] | 1344 | |
| 1345 | // Now that all instructions in the function are constant folded, erase dead |
| 1346 | // blocks, because we can now use ConstantFoldTerminator to get rid of |
| 1347 | // in-edges. |
| 1348 | for (unsigned i = 0, e = BlocksToErase.size(); i != e; ++i) { |
| 1349 | // If there are any PHI nodes in this successor, drop entries for BB now. |
| 1350 | BasicBlock *DeadBB = BlocksToErase[i]; |
| 1351 | while (!DeadBB->use_empty()) { |
| 1352 | Instruction *I = cast<Instruction>(DeadBB->use_back()); |
| 1353 | bool Folded = ConstantFoldTerminator(I->getParent()); |
| 1354 | assert(Folded && "Didn't fold away reference to block!"); |
| 1355 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1356 | |
Chris Lattner | 5f9e8b4 | 2004-12-10 22:29:08 +0000 | [diff] [blame] | 1357 | // Finally, delete the basic block. |
| 1358 | F->getBasicBlockList().erase(DeadBB); |
| 1359 | } |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1360 | } |
Chris Lattner | 0417feb | 2004-12-11 02:53:57 +0000 | [diff] [blame] | 1361 | |
| 1362 | // If we inferred constant or undef return values for a function, we replaced |
| 1363 | // all call uses with the inferred value. This means we don't need to bother |
| 1364 | // actually returning anything from the function. Replace all return |
| 1365 | // instructions with return undef. |
| 1366 | const hash_map<Function*, LatticeVal> &RV =Solver.getTrackedFunctionRetVals(); |
| 1367 | for (hash_map<Function*, LatticeVal>::const_iterator I = RV.begin(), |
| 1368 | E = RV.end(); I != E; ++I) |
| 1369 | if (!I->second.isOverdefined() && |
| 1370 | I->first->getReturnType() != Type::VoidTy) { |
| 1371 | Function *F = I->first; |
| 1372 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 1373 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) |
| 1374 | if (!isa<UndefValue>(RI->getOperand(0))) |
| 1375 | RI->setOperand(0, UndefValue::get(F->getReturnType())); |
| 1376 | } |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 1377 | |
| 1378 | // If we infered constant or undef values for globals variables, we can delete |
| 1379 | // the global and any stores that remain to it. |
| 1380 | const hash_map<GlobalVariable*, LatticeVal> &TG = Solver.getTrackedGlobals(); |
| 1381 | for (hash_map<GlobalVariable*, LatticeVal>::const_iterator I = TG.begin(), |
| 1382 | E = TG.end(); I != E; ++I) { |
| 1383 | GlobalVariable *GV = I->first; |
| 1384 | assert(!I->second.isOverdefined() && |
| 1385 | "Overdefined values should have been taken out of the map!"); |
| 1386 | DEBUG(std::cerr << "Found that GV '" << GV->getName()<< "' is constant!\n"); |
| 1387 | while (!GV->use_empty()) { |
| 1388 | StoreInst *SI = cast<StoreInst>(GV->use_back()); |
| 1389 | SI->eraseFromParent(); |
| 1390 | } |
| 1391 | M.getGlobalList().erase(GV); |
Chris Lattner | dade2d2 | 2004-12-11 06:05:53 +0000 | [diff] [blame] | 1392 | ++IPNumGlobalConst; |
Chris Lattner | dd336d1 | 2004-12-11 05:15:59 +0000 | [diff] [blame] | 1393 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1394 | |
Chris Lattner | 59acc7d | 2004-12-10 08:02:06 +0000 | [diff] [blame] | 1395 | return MadeChanges; |
| 1396 | } |