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