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