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