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