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