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