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