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