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