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