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