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