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