Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 1 | //===- SCCP.cpp - Sparse Conditional Constant Propagation -----------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 10 | // This file implements sparse conditional constant propagation and merging: |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 11 | // |
| 12 | // Specifically, this: |
| 13 | // * Assumes values are constant unless proven otherwise |
| 14 | // * Assumes BasicBlocks are dead unless proven otherwise |
| 15 | // * Proves values to be constant, and replaces them with constants |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 16 | // * Proves conditional branches to be unconditional |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 17 | // |
| 18 | // Notice that: |
| 19 | // * This pass has a habit of making definitions be dead. It is a good idea |
| 20 | // to to run a DCE pass sometime after running this pass. |
| 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | b7a5d3e | 2004-01-12 17:43:40 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 26 | #include "llvm/Function.h" |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 27 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 28 | #include "llvm/Instructions.h" |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 29 | #include "llvm/Pass.h" |
Chris Lattner | b7a5d3e | 2004-01-12 17:43:40 +0000 | [diff] [blame] | 30 | #include "llvm/Type.h" |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 31 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/Local.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" |
| 34 | #include "llvm/ADT/hash_map" |
| 35 | #include "llvm/ADT/Statistic.h" |
| 36 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 38 | #include <set> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 41 | // InstVal class - This class represents the different lattice values that an |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 42 | // instruction may occupy. It is a simple class with value semantics. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 43 | // |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 44 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 45 | Statistic<> NumInstRemoved("sccp", "Number of instructions removed"); |
| 46 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 47 | class InstVal { |
| 48 | enum { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 49 | undefined, // This instruction has no known value |
| 50 | constant, // This instruction has a constant value |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 51 | overdefined // This instruction has an unknown value |
| 52 | } LatticeValue; // The current lattice position |
| 53 | Constant *ConstantVal; // If Constant value, the current value |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 54 | public: |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 55 | inline InstVal() : LatticeValue(undefined), ConstantVal(0) {} |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 56 | |
| 57 | // markOverdefined - Return true if this is a new status to be in... |
| 58 | inline bool markOverdefined() { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 59 | if (LatticeValue != overdefined) { |
| 60 | LatticeValue = overdefined; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | // markConstant - Return true if this is a new status for us... |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 67 | inline bool markConstant(Constant *V) { |
| 68 | if (LatticeValue != constant) { |
| 69 | LatticeValue = constant; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 70 | ConstantVal = V; |
| 71 | return true; |
| 72 | } else { |
Chris Lattner | b70d82f | 2001-09-07 16:43:22 +0000 | [diff] [blame] | 73 | assert(ConstantVal == V && "Marking constant with different value"); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 78 | inline bool isUndefined() const { return LatticeValue == undefined; } |
| 79 | inline bool isConstant() const { return LatticeValue == constant; } |
| 80 | inline bool isOverdefined() const { return LatticeValue == overdefined; } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 82 | inline Constant *getConstant() const { |
| 83 | assert(isConstant() && "Cannot get the constant of a non-constant!"); |
| 84 | return ConstantVal; |
| 85 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 88 | } // end anonymous namespace |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | // SCCP Class |
| 93 | // |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 94 | // This class does all of the work of Sparse Conditional Constant Propagation. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 95 | // |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 96 | namespace { |
| 97 | class SCCP : public FunctionPass, public InstVisitor<SCCP> { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 98 | std::set<BasicBlock*> BBExecutable;// The basic blocks that are executable |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 99 | hash_map<Value*, InstVal> ValueState; // The state each value is in... |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 101 | // The reason for two worklists is that overdefined is the lowest state |
| 102 | // on the lattice, and moving things to overdefined as fast as possible |
| 103 | // makes SCCP converge much faster. |
| 104 | // By having a separate worklist, we accomplish this because everything |
| 105 | // possibly overdefined will become overdefined at the soonest possible |
| 106 | // point. |
| 107 | std::vector<Instruction*> OverdefinedInstWorkList;// The overdefined |
| 108 | // instruction work list |
Chris Lattner | 071d0ad | 2002-05-07 04:29:32 +0000 | [diff] [blame] | 109 | std::vector<Instruction*> InstWorkList;// The instruction work list |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 110 | |
| 111 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 112 | std::vector<BasicBlock*> BBWorkList; // The BasicBlock work list |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 114 | /// UsersOfOverdefinedPHIs - Keep track of any users of PHI nodes that are not |
| 115 | /// overdefined, despite the fact that the PHI node is overdefined. |
| 116 | std::multimap<PHINode*, Instruction*> UsersOfOverdefinedPHIs; |
| 117 | |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 118 | /// KnownFeasibleEdges - Entries in this set are edges which have already had |
| 119 | /// PHI nodes retriggered. |
| 120 | typedef std::pair<BasicBlock*,BasicBlock*> Edge; |
| 121 | std::set<Edge> KnownFeasibleEdges; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 122 | public: |
| 123 | |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 124 | // runOnFunction - Run the Sparse Conditional Constant Propagation algorithm, |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 125 | // and return true if the function was modified. |
| 126 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 127 | bool runOnFunction(Function &F); |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 128 | |
| 129 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cb2610e | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 130 | AU.setPreservesCFG(); |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 133 | |
| 134 | //===--------------------------------------------------------------------===// |
| 135 | // The implementation of this class |
| 136 | // |
| 137 | private: |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 138 | friend class InstVisitor<SCCP>; // Allow callbacks from visitor |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 140 | // markConstant - Make a value be marked as "constant". If the value |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 141 | // is not already a constant, add it to the instruction work list so that |
| 142 | // the users of the instruction are updated later. |
| 143 | // |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 144 | inline void markConstant(InstVal &IV, Instruction *I, Constant *C) { |
| 145 | if (IV.markConstant(C)) { |
| 146 | DEBUG(std::cerr << "markConstant: " << *C << ": " << *I); |
Chris Lattner | 071d0ad | 2002-05-07 04:29:32 +0000 | [diff] [blame] | 147 | InstWorkList.push_back(I); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 148 | } |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 149 | } |
| 150 | inline void markConstant(Instruction *I, Constant *C) { |
| 151 | markConstant(ValueState[I], I, C); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 154 | // markOverdefined - Make a value be marked as "overdefined". If the |
| 155 | // value is not already overdefined, add it to the overdefined instruction |
| 156 | // work list so that the users of the instruction are updated later. |
| 157 | |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 158 | inline void markOverdefined(InstVal &IV, Instruction *I) { |
| 159 | if (IV.markOverdefined()) { |
| 160 | DEBUG(std::cerr << "markOverdefined: " << *I); |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 161 | OverdefinedInstWorkList.push_back(I); // Only instructions go on the work list |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 162 | } |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 163 | } |
| 164 | inline void markOverdefined(Instruction *I) { |
| 165 | markOverdefined(ValueState[I], I); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // getValueState - Return the InstVal object that corresponds to the value. |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 169 | // This function is necessary because not all values should start out in the |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 170 | // underdefined state... Argument's should be overdefined, and |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 171 | // constants should be marked as constants. If a value is not known to be an |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 172 | // Instruction object, then use this accessor to get its value from the map. |
| 173 | // |
| 174 | inline InstVal &getValueState(Value *V) { |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 175 | hash_map<Value*, InstVal>::iterator I = ValueState.find(V); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 176 | if (I != ValueState.end()) return I->second; // Common case, in the map |
| 177 | |
Reid Spencer | c88920d | 2004-07-18 08:34:52 +0000 | [diff] [blame] | 178 | if (Constant *CPV = dyn_cast<Constant>(V)) { // Constants are constant |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 179 | ValueState[CPV].markConstant(CPV); |
Chris Lattner | 73e2142 | 2002-04-09 19:48:49 +0000 | [diff] [blame] | 180 | } else if (isa<Argument>(V)) { // Arguments are overdefined |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 181 | ValueState[V].markOverdefined(); |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 182 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 183 | // All others are underdefined by default... |
| 184 | return ValueState[V]; |
| 185 | } |
| 186 | |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 187 | // markEdgeExecutable - Mark a basic block as executable, adding it to the BB |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 188 | // work list if it is not already executable... |
| 189 | // |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 190 | void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) { |
| 191 | if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second) |
| 192 | return; // This edge is already known to be executable! |
| 193 | |
| 194 | if (BBExecutable.count(Dest)) { |
| 195 | DEBUG(std::cerr << "Marking Edge Executable: " << Source->getName() |
| 196 | << " -> " << Dest->getName() << "\n"); |
| 197 | |
| 198 | // The destination is already executable, but we just made an edge |
Chris Lattner | 929c6fb | 2003-10-08 16:56:11 +0000 | [diff] [blame] | 199 | // feasible that wasn't before. Revisit the PHI nodes in the block |
| 200 | // because they have potentially new operands. |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 201 | for (BasicBlock::iterator I = Dest->begin(); |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 202 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
Chris Lattner | bceb2b0 | 2003-04-25 03:35:10 +0000 | [diff] [blame] | 203 | visitPHINode(*PN); |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 204 | |
| 205 | } else { |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 206 | DEBUG(std::cerr << "Marking Block Executable: " << Dest->getName()<<"\n"); |
| 207 | BBExecutable.insert(Dest); // Basic block is executable! |
| 208 | BBWorkList.push_back(Dest); // Add the block to the work list! |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 209 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 213 | // visit implementations - Something changed in this instruction... Either an |
Chris Lattner | cb056de | 2001-06-29 23:56:23 +0000 | [diff] [blame] | 214 | // operand made a transition, or the instruction is newly executable. Change |
| 215 | // the value type of I to reflect these changes if appropriate. |
| 216 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 217 | void visitPHINode(PHINode &I); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 218 | |
| 219 | // Terminators |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 220 | void visitReturnInst(ReturnInst &I) { /*does not have an effect*/ } |
| 221 | void visitTerminatorInst(TerminatorInst &TI); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 222 | |
Chris Lattner | b804760 | 2002-08-14 17:53:45 +0000 | [diff] [blame] | 223 | void visitCastInst(CastInst &I); |
Chris Lattner | 6e32372 | 2004-03-12 05:52:44 +0000 | [diff] [blame] | 224 | void visitSelectInst(SelectInst &I); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 225 | void visitBinaryOperator(Instruction &I); |
| 226 | void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); } |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 227 | |
| 228 | // Instructions that cannot be folded away... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 229 | void visitStoreInst (Instruction &I) { /*returns void*/ } |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 230 | void visitLoadInst (LoadInst &I); |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 231 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 232 | void visitCallInst (CallInst &I); |
Chris Lattner | 99b28e6 | 2003-08-27 01:08:35 +0000 | [diff] [blame] | 233 | void visitInvokeInst (TerminatorInst &I) { |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 234 | if (I.getType() != Type::VoidTy) markOverdefined(&I); |
Chris Lattner | 99b28e6 | 2003-08-27 01:08:35 +0000 | [diff] [blame] | 235 | visitTerminatorInst(I); |
| 236 | } |
Chris Lattner | 36143fc | 2003-09-08 18:54:55 +0000 | [diff] [blame] | 237 | void visitUnwindInst (TerminatorInst &I) { /*returns void*/ } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 238 | void visitAllocationInst(Instruction &I) { markOverdefined(&I); } |
Chris Lattner | cda965e | 2003-10-18 05:56:52 +0000 | [diff] [blame] | 239 | void visitVANextInst (Instruction &I) { markOverdefined(&I); } |
| 240 | void visitVAArgInst (Instruction &I) { markOverdefined(&I); } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 241 | void visitFreeInst (Instruction &I) { /*returns void*/ } |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 243 | void visitInstruction(Instruction &I) { |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 244 | // If a new instruction is added to LLVM that we don't handle... |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 245 | std::cerr << "SCCP: Don't know how to handle: " << I; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 246 | markOverdefined(&I); // Just in case |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 247 | } |
Chris Lattner | cb056de | 2001-06-29 23:56:23 +0000 | [diff] [blame] | 248 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 249 | // getFeasibleSuccessors - Return a vector of booleans to indicate which |
| 250 | // successors are reachable from a given terminator instruction. |
| 251 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 252 | void getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs); |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 254 | // isEdgeFeasible - Return true if the control flow edge from the 'From' basic |
| 255 | // block to the 'To' basic block is currently feasible... |
| 256 | // |
| 257 | bool isEdgeFeasible(BasicBlock *From, BasicBlock *To); |
| 258 | |
Chris Lattner | cb056de | 2001-06-29 23:56:23 +0000 | [diff] [blame] | 259 | // OperandChangedState - This method is invoked on all of the users of an |
| 260 | // instruction that was just changed state somehow.... Based on this |
| 261 | // information, we need to update the specified user of this instruction. |
| 262 | // |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 263 | void OperandChangedState(User *U) { |
| 264 | // Only instructions use other variable values! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 265 | Instruction &I = cast<Instruction>(*U); |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 266 | if (BBExecutable.count(I.getParent())) // Inst is executable? |
| 267 | visit(I); |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | cb056de | 2001-06-29 23:56:23 +0000 | [diff] [blame] | 269 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 271 | RegisterOpt<SCCP> X("sccp", "Sparse Conditional Constant Propagation"); |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 272 | } // end anonymous namespace |
| 273 | |
| 274 | |
| 275 | // createSCCPPass - This is the public interface to this file... |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 276 | Pass *llvm::createSCCPPass() { |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 277 | return new SCCP(); |
| 278 | } |
| 279 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 281 | //===----------------------------------------------------------------------===// |
| 282 | // SCCP Class Implementation |
| 283 | |
| 284 | |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 285 | // runOnFunction() - Run the Sparse Conditional Constant Propagation algorithm, |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 286 | // and return true if the function was modified. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 287 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 288 | bool SCCP::runOnFunction(Function &F) { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 289 | // Mark the first block of the function as being executable... |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 290 | BBExecutable.insert(F.begin()); // Basic block is executable! |
| 291 | BBWorkList.push_back(F.begin()); // Add the block to the work list! |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 293 | // Process the work lists until they are empty! |
| 294 | while (!BBWorkList.empty() || !InstWorkList.empty() || |
| 295 | !OverdefinedInstWorkList.empty()) { |
| 296 | // Process the instruction work list... |
| 297 | while (!OverdefinedInstWorkList.empty()) { |
| 298 | Instruction *I = OverdefinedInstWorkList.back(); |
| 299 | OverdefinedInstWorkList.pop_back(); |
| 300 | |
| 301 | DEBUG(std::cerr << "\nPopped off OI-WL: " << I); |
| 302 | |
| 303 | // "I" got into the work list because it either made the transition from |
| 304 | // bottom to constant |
| 305 | // |
| 306 | // Anything on this worklist that is overdefined need not be visited |
| 307 | // since all of its users will have already been marked as overdefined |
| 308 | // Update all of the users of this instruction's value... |
| 309 | // |
| 310 | for_each(I->use_begin(), I->use_end(), |
| 311 | bind_obj(this, &SCCP::OperandChangedState)); |
| 312 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 313 | // Process the instruction work list... |
| 314 | while (!InstWorkList.empty()) { |
Chris Lattner | 071d0ad | 2002-05-07 04:29:32 +0000 | [diff] [blame] | 315 | Instruction *I = InstWorkList.back(); |
| 316 | InstWorkList.pop_back(); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 2fc1230 | 2004-07-15 01:50:47 +0000 | [diff] [blame] | 318 | DEBUG(std::cerr << "\nPopped off I-WL: " << *I); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 319 | |
| 320 | // "I" got into the work list because it either made the transition from |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 321 | // bottom to constant |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 322 | // |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 323 | // Anything on this worklist that is overdefined need not be visited |
| 324 | // since all of its users will have already been marked as overdefined. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 325 | // Update all of the users of this instruction's value... |
| 326 | // |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 327 | InstVal &Ival = getValueState (I); |
| 328 | if (!Ival.isOverdefined()) |
| 329 | for_each(I->use_begin(), I->use_end(), |
| 330 | bind_obj(this, &SCCP::OperandChangedState)); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // Process the basic block work list... |
| 334 | while (!BBWorkList.empty()) { |
| 335 | BasicBlock *BB = BBWorkList.back(); |
| 336 | BBWorkList.pop_back(); |
| 337 | |
Chris Lattner | 2fc1230 | 2004-07-15 01:50:47 +0000 | [diff] [blame] | 338 | DEBUG(std::cerr << "\nPopped off BBWL: " << *BB); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 340 | // Notify all instructions in this basic block that they are newly |
| 341 | // executable. |
| 342 | visit(BB); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 346 | if (DebugFlag) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 347 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 348 | if (!BBExecutable.count(I)) |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 349 | std::cerr << "BasicBlock Dead:" << *I; |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 350 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 351 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 352 | // Iterate over all of the instructions in a function, replacing them with |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 353 | // constants if we have found them to be of constant values. |
| 354 | // |
| 355 | bool MadeChanges = false; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 356 | for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 357 | for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 358 | Instruction &Inst = *BI; |
| 359 | InstVal &IV = ValueState[&Inst]; |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 360 | if (IV.isConstant()) { |
| 361 | Constant *Const = IV.getConstant(); |
Chris Lattner | 2fc1230 | 2004-07-15 01:50:47 +0000 | [diff] [blame] | 362 | DEBUG(std::cerr << "Constant: " << *Const << " = " << Inst); |
Chris Lattner | 5b7d42b | 2001-11-26 18:57:38 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 364 | // Replaces all of the uses of a variable with uses of the constant. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 365 | Inst.replaceAllUsesWith(Const); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 0e9c515 | 2002-05-02 20:32:51 +0000 | [diff] [blame] | 367 | // Remove the operator from the list of definitions... and delete it. |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 368 | BI = BB->getInstList().erase(BI); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 370 | // Hey, we just changed something! |
| 371 | MadeChanges = true; |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 372 | ++NumInstRemoved; |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 373 | } else { |
| 374 | ++BI; |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 375 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 376 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 377 | |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 378 | // Reset state so that the next invocation will have empty data structures |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 379 | BBExecutable.clear(); |
| 380 | ValueState.clear(); |
Chris Lattner | 80b2d6c | 2004-07-15 23:36:43 +0000 | [diff] [blame] | 381 | std::vector<Instruction*>().swap(OverdefinedInstWorkList); |
Chris Lattner | af66346 | 2002-11-04 02:54:22 +0000 | [diff] [blame] | 382 | std::vector<Instruction*>().swap(InstWorkList); |
| 383 | std::vector<BasicBlock*>().swap(BBWorkList); |
Chris Lattner | 0dbfc05 | 2002-04-29 21:26:08 +0000 | [diff] [blame] | 384 | |
Chris Lattner | b70d82f | 2001-09-07 16:43:22 +0000 | [diff] [blame] | 385 | return MadeChanges; |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 388 | |
| 389 | // getFeasibleSuccessors - Return a vector of booleans to indicate which |
| 390 | // successors are reachable from a given terminator instruction. |
| 391 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 392 | void SCCP::getFeasibleSuccessors(TerminatorInst &TI, std::vector<bool> &Succs) { |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 393 | Succs.resize(TI.getNumSuccessors()); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 394 | if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) { |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 395 | if (BI->isUnconditional()) { |
| 396 | Succs[0] = true; |
| 397 | } else { |
| 398 | InstVal &BCValue = getValueState(BI->getCondition()); |
Chris Lattner | 8483164 | 2004-01-12 17:40:36 +0000 | [diff] [blame] | 399 | if (BCValue.isOverdefined() || |
| 400 | (BCValue.isConstant() && !isa<ConstantBool>(BCValue.getConstant()))) { |
| 401 | // Overdefined condition variables, and branches on unfoldable constant |
| 402 | // conditions, mean the branch could go either way. |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 403 | Succs[0] = Succs[1] = true; |
| 404 | } else if (BCValue.isConstant()) { |
| 405 | // Constant condition variables mean the branch can only go a single way |
| 406 | Succs[BCValue.getConstant() == ConstantBool::False] = true; |
| 407 | } |
| 408 | } |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 409 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(&TI)) { |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 410 | // Invoke instructions successors are always executable. |
| 411 | Succs[0] = Succs[1] = true; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 412 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(&TI)) { |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 413 | InstVal &SCValue = getValueState(SI->getCondition()); |
Chris Lattner | 8483164 | 2004-01-12 17:40:36 +0000 | [diff] [blame] | 414 | if (SCValue.isOverdefined() || // Overdefined condition? |
| 415 | (SCValue.isConstant() && !isa<ConstantInt>(SCValue.getConstant()))) { |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 416 | // All destinations are executable! |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 417 | Succs.assign(TI.getNumSuccessors(), true); |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 418 | } else if (SCValue.isConstant()) { |
| 419 | Constant *CPV = SCValue.getConstant(); |
| 420 | // Make sure to skip the "default value" which isn't a value |
| 421 | for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) { |
| 422 | if (SI->getSuccessorValue(i) == CPV) {// Found the right branch... |
| 423 | Succs[i] = true; |
| 424 | return; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Constant value not equal to any of the branches... must execute |
| 429 | // default branch then... |
| 430 | Succs[0] = true; |
| 431 | } |
| 432 | } else { |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 433 | std::cerr << "SCCP: Don't know how to handle: " << TI; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 434 | Succs.assign(TI.getNumSuccessors(), true); |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 439 | // isEdgeFeasible - Return true if the control flow edge from the 'From' basic |
| 440 | // block to the 'To' basic block is currently feasible... |
| 441 | // |
| 442 | bool SCCP::isEdgeFeasible(BasicBlock *From, BasicBlock *To) { |
| 443 | assert(BBExecutable.count(To) && "Dest should always be alive!"); |
| 444 | |
| 445 | // Make sure the source basic block is executable!! |
| 446 | if (!BBExecutable.count(From)) return false; |
| 447 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 448 | // Check to make sure this edge itself is actually feasible now... |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 449 | TerminatorInst *TI = From->getTerminator(); |
| 450 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 451 | if (BI->isUnconditional()) |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 452 | return true; |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 453 | else { |
| 454 | InstVal &BCValue = getValueState(BI->getCondition()); |
| 455 | if (BCValue.isOverdefined()) { |
| 456 | // Overdefined condition variables mean the branch could go either way. |
| 457 | return true; |
| 458 | } else if (BCValue.isConstant()) { |
Chris Lattner | 8483164 | 2004-01-12 17:40:36 +0000 | [diff] [blame] | 459 | // Not branching on an evaluatable constant? |
| 460 | if (!isa<ConstantBool>(BCValue.getConstant())) return true; |
| 461 | |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 462 | // Constant condition variables mean the branch can only go a single way |
| 463 | return BI->getSuccessor(BCValue.getConstant() == |
| 464 | ConstantBool::False) == To; |
| 465 | } |
| 466 | return false; |
| 467 | } |
| 468 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) { |
| 469 | // Invoke instructions successors are always executable. |
| 470 | return true; |
| 471 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 472 | InstVal &SCValue = getValueState(SI->getCondition()); |
| 473 | if (SCValue.isOverdefined()) { // Overdefined condition? |
| 474 | // All destinations are executable! |
| 475 | return true; |
| 476 | } else if (SCValue.isConstant()) { |
| 477 | Constant *CPV = SCValue.getConstant(); |
Chris Lattner | 8483164 | 2004-01-12 17:40:36 +0000 | [diff] [blame] | 478 | if (!isa<ConstantInt>(CPV)) |
| 479 | return true; // not a foldable constant? |
| 480 | |
Chris Lattner | 7d275f4 | 2003-10-08 15:47:41 +0000 | [diff] [blame] | 481 | // Make sure to skip the "default value" which isn't a value |
| 482 | for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) |
| 483 | if (SI->getSuccessorValue(i) == CPV) // Found the taken branch... |
| 484 | return SI->getSuccessor(i) == To; |
| 485 | |
| 486 | // Constant value not equal to any of the branches... must execute |
| 487 | // default branch then... |
| 488 | return SI->getDefaultDest() == To; |
| 489 | } |
| 490 | return false; |
| 491 | } else { |
| 492 | std::cerr << "Unknown terminator instruction: " << *TI; |
| 493 | abort(); |
| 494 | } |
Chris Lattner | 59f0ce2 | 2002-05-02 21:18:01 +0000 | [diff] [blame] | 495 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 497 | // visit Implementations - Something changed in this instruction... Either an |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 498 | // operand made a transition, or the instruction is newly executable. Change |
| 499 | // the value type of I to reflect these changes if appropriate. This method |
| 500 | // makes sure to do the following actions: |
| 501 | // |
| 502 | // 1. If a phi node merges two constants in, and has conflicting value coming |
| 503 | // from different branches, or if the PHI node merges in an overdefined |
| 504 | // value, then the PHI node becomes overdefined. |
| 505 | // 2. If a phi node merges only constants in, and they all agree on value, the |
| 506 | // PHI node becomes a constant value equal to that. |
| 507 | // 3. If V <- x (op) y && isConstant(x) && isConstant(y) V = Constant |
| 508 | // 4. If V <- x (op) y && (isOverdefined(x) || isOverdefined(y)) V = Overdefined |
| 509 | // 5. If V <- MEM or V <- CALL or V <- (unknown) then V = Overdefined |
| 510 | // 6. If a conditional branch has a value that is constant, make the selected |
| 511 | // destination executable |
| 512 | // 7. If a conditional branch has a value that is overdefined, make all |
| 513 | // successors executable. |
| 514 | // |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 515 | void SCCP::visitPHINode(PHINode &PN) { |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 516 | InstVal &PNIV = getValueState(&PN); |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 517 | if (PNIV.isOverdefined()) { |
| 518 | // There may be instructions using this PHI node that are not overdefined |
| 519 | // themselves. If so, make sure that they know that the PHI node operand |
| 520 | // changed. |
| 521 | std::multimap<PHINode*, Instruction*>::iterator I, E; |
| 522 | tie(I, E) = UsersOfOverdefinedPHIs.equal_range(&PN); |
| 523 | if (I != E) { |
| 524 | std::vector<Instruction*> Users; |
| 525 | Users.reserve(std::distance(I, E)); |
| 526 | for (; I != E; ++I) Users.push_back(I->second); |
| 527 | while (!Users.empty()) { |
| 528 | visit(Users.back()); |
| 529 | Users.pop_back(); |
| 530 | } |
| 531 | } |
| 532 | return; // Quick exit |
| 533 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 534 | |
Chris Lattner | a2f652d | 2004-03-16 19:49:59 +0000 | [diff] [blame] | 535 | // Super-extra-high-degree PHI nodes are unlikely to ever be marked constant, |
| 536 | // and slow us down a lot. Just mark them overdefined. |
| 537 | if (PN.getNumIncomingValues() > 64) { |
| 538 | markOverdefined(PNIV, &PN); |
| 539 | return; |
| 540 | } |
| 541 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 542 | // Look at all of the executable operands of the PHI node. If any of them |
| 543 | // are overdefined, the PHI becomes overdefined as well. If they are all |
| 544 | // constant, and they agree with each other, the PHI becomes the identical |
| 545 | // constant. If they are constant and don't agree, the PHI is overdefined. |
| 546 | // If there are no executable operands, the PHI remains undefined. |
| 547 | // |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 548 | Constant *OperandVal = 0; |
| 549 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 550 | InstVal &IV = getValueState(PN.getIncomingValue(i)); |
| 551 | if (IV.isUndefined()) continue; // Doesn't influence PHI node. |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 552 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 553 | if (isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent())) { |
Chris Lattner | 38b5ae4 | 2003-06-24 20:29:52 +0000 | [diff] [blame] | 554 | if (IV.isOverdefined()) { // PHI node becomes overdefined! |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 555 | markOverdefined(PNIV, &PN); |
Chris Lattner | 38b5ae4 | 2003-06-24 20:29:52 +0000 | [diff] [blame] | 556 | return; |
| 557 | } |
| 558 | |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 559 | if (OperandVal == 0) { // Grab the first value... |
| 560 | OperandVal = IV.getConstant(); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 561 | } else { // Another value is being merged in! |
| 562 | // There is already a reachable operand. If we conflict with it, |
| 563 | // then the PHI node becomes overdefined. If we agree with it, we |
| 564 | // can continue on. |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 565 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 566 | // Check to see if there are two different constants merging... |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 567 | if (IV.getConstant() != OperandVal) { |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 568 | // Yes there is. This means the PHI node is not constant. |
| 569 | // You must be overdefined poor PHI. |
| 570 | // |
Chris Lattner | 3d405b0 | 2003-10-08 16:21:03 +0000 | [diff] [blame] | 571 | markOverdefined(PNIV, &PN); // The PHI node now becomes overdefined |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 572 | return; // I'm done analyzing you |
Chris Lattner | 5b7d42b | 2001-11-26 18:57:38 +0000 | [diff] [blame] | 573 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 578 | // If we exited the loop, this means that the PHI node only has constant |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 579 | // arguments that agree with each other(and OperandVal is the constant) or |
| 580 | // OperandVal is null because there are no defined incoming arguments. If |
| 581 | // this is the case, the PHI remains undefined. |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 582 | // |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 583 | if (OperandVal) |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 584 | markConstant(PNIV, &PN, OperandVal); // Acquire operand value |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 587 | void SCCP::visitTerminatorInst(TerminatorInst &TI) { |
Chris Lattner | 9de2828 | 2003-04-25 02:50:03 +0000 | [diff] [blame] | 588 | std::vector<bool> SuccFeasible; |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 589 | getFeasibleSuccessors(TI, SuccFeasible); |
Chris Lattner | 138a124 | 2001-06-27 23:38:11 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 591 | BasicBlock *BB = TI.getParent(); |
| 592 | |
Chris Lattner | b9a6634 | 2002-05-02 21:44:00 +0000 | [diff] [blame] | 593 | // Mark all feasible successors executable... |
| 594 | for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i) |
Chris Lattner | 16b18fd | 2003-10-08 16:55:34 +0000 | [diff] [blame] | 595 | if (SuccFeasible[i]) |
| 596 | markEdgeExecutable(BB, TI.getSuccessor(i)); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Chris Lattner | b804760 | 2002-08-14 17:53:45 +0000 | [diff] [blame] | 599 | void SCCP::visitCastInst(CastInst &I) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 600 | Value *V = I.getOperand(0); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 601 | InstVal &VState = getValueState(V); |
Chris Lattner | b7a5d3e | 2004-01-12 17:43:40 +0000 | [diff] [blame] | 602 | if (VState.isOverdefined()) // Inherit overdefinedness of operand |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 603 | markOverdefined(&I); |
Chris Lattner | b7a5d3e | 2004-01-12 17:43:40 +0000 | [diff] [blame] | 604 | else if (VState.isConstant()) // Propagate constant value |
| 605 | markConstant(&I, ConstantExpr::getCast(VState.getConstant(), I.getType())); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Chris Lattner | 6e32372 | 2004-03-12 05:52:44 +0000 | [diff] [blame] | 608 | void SCCP::visitSelectInst(SelectInst &I) { |
| 609 | InstVal &CondValue = getValueState(I.getCondition()); |
| 610 | if (CondValue.isOverdefined()) |
| 611 | markOverdefined(&I); |
| 612 | else if (CondValue.isConstant()) { |
| 613 | if (CondValue.getConstant() == ConstantBool::True) { |
| 614 | InstVal &Val = getValueState(I.getTrueValue()); |
| 615 | if (Val.isOverdefined()) |
| 616 | markOverdefined(&I); |
| 617 | else if (Val.isConstant()) |
| 618 | markConstant(&I, Val.getConstant()); |
| 619 | } else if (CondValue.getConstant() == ConstantBool::False) { |
| 620 | InstVal &Val = getValueState(I.getFalseValue()); |
| 621 | if (Val.isOverdefined()) |
| 622 | markOverdefined(&I); |
| 623 | else if (Val.isConstant()) |
| 624 | markConstant(&I, Val.getConstant()); |
| 625 | } else |
| 626 | markOverdefined(&I); |
| 627 | } |
| 628 | } |
| 629 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 630 | // Handle BinaryOperators and Shift Instructions... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 631 | void SCCP::visitBinaryOperator(Instruction &I) { |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 632 | InstVal &IV = ValueState[&I]; |
| 633 | if (IV.isOverdefined()) return; |
| 634 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 635 | InstVal &V1State = getValueState(I.getOperand(0)); |
| 636 | InstVal &V2State = getValueState(I.getOperand(1)); |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 638 | if (V1State.isOverdefined() || V2State.isOverdefined()) { |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 639 | // If both operands are PHI nodes, it is possible that this instruction has |
| 640 | // a constant value, despite the fact that the PHI node doesn't. Check for |
| 641 | // this condition now. |
| 642 | if (PHINode *PN1 = dyn_cast<PHINode>(I.getOperand(0))) |
| 643 | if (PHINode *PN2 = dyn_cast<PHINode>(I.getOperand(1))) |
| 644 | if (PN1->getParent() == PN2->getParent()) { |
| 645 | // Since the two PHI nodes are in the same basic block, they must have |
| 646 | // entries for the same predecessors. Walk the predecessor list, and |
| 647 | // if all of the incoming values are constants, and the result of |
| 648 | // evaluating this expression with all incoming value pairs is the |
| 649 | // same, then this expression is a constant even though the PHI node |
| 650 | // is not a constant! |
| 651 | InstVal Result; |
| 652 | for (unsigned i = 0, e = PN1->getNumIncomingValues(); i != e; ++i) { |
| 653 | InstVal &In1 = getValueState(PN1->getIncomingValue(i)); |
| 654 | BasicBlock *InBlock = PN1->getIncomingBlock(i); |
| 655 | InstVal &In2 =getValueState(PN2->getIncomingValueForBlock(InBlock)); |
| 656 | |
| 657 | if (In1.isOverdefined() || In2.isOverdefined()) { |
| 658 | Result.markOverdefined(); |
| 659 | break; // Cannot fold this operation over the PHI nodes! |
| 660 | } else if (In1.isConstant() && In2.isConstant()) { |
Chris Lattner | b16689b | 2004-01-12 19:08:43 +0000 | [diff] [blame] | 661 | Constant *V = ConstantExpr::get(I.getOpcode(), In1.getConstant(), |
| 662 | In2.getConstant()); |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 663 | if (Result.isUndefined()) |
Chris Lattner | b16689b | 2004-01-12 19:08:43 +0000 | [diff] [blame] | 664 | Result.markConstant(V); |
| 665 | else if (Result.isConstant() && Result.getConstant() != V) { |
Chris Lattner | 1daee8b | 2004-01-12 03:57:30 +0000 | [diff] [blame] | 666 | Result.markOverdefined(); |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | // If we found a constant value here, then we know the instruction is |
| 673 | // constant despite the fact that the PHI nodes are overdefined. |
| 674 | if (Result.isConstant()) { |
| 675 | markConstant(IV, &I, Result.getConstant()); |
| 676 | // Remember that this instruction is virtually using the PHI node |
| 677 | // operands. |
| 678 | UsersOfOverdefinedPHIs.insert(std::make_pair(PN1, &I)); |
| 679 | UsersOfOverdefinedPHIs.insert(std::make_pair(PN2, &I)); |
| 680 | return; |
| 681 | } else if (Result.isUndefined()) { |
| 682 | return; |
| 683 | } |
| 684 | |
| 685 | // Okay, this really is overdefined now. Since we might have |
| 686 | // speculatively thought that this was not overdefined before, and |
| 687 | // added ourselves to the UsersOfOverdefinedPHIs list for the PHIs, |
| 688 | // make sure to clean out any entries that we put there, for |
| 689 | // efficiency. |
| 690 | std::multimap<PHINode*, Instruction*>::iterator It, E; |
| 691 | tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN1); |
| 692 | while (It != E) { |
| 693 | if (It->second == &I) { |
| 694 | UsersOfOverdefinedPHIs.erase(It++); |
| 695 | } else |
| 696 | ++It; |
| 697 | } |
| 698 | tie(It, E) = UsersOfOverdefinedPHIs.equal_range(PN2); |
| 699 | while (It != E) { |
| 700 | if (It->second == &I) { |
| 701 | UsersOfOverdefinedPHIs.erase(It++); |
| 702 | } else |
| 703 | ++It; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | markOverdefined(IV, &I); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 708 | } else if (V1State.isConstant() && V2State.isConstant()) { |
Chris Lattner | b16689b | 2004-01-12 19:08:43 +0000 | [diff] [blame] | 709 | markConstant(IV, &I, ConstantExpr::get(I.getOpcode(), V1State.getConstant(), |
| 710 | V2State.getConstant())); |
Chris Lattner | 2a63255 | 2002-04-18 15:13:15 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 713 | |
| 714 | // Handle getelementptr instructions... if all operands are constants then we |
| 715 | // can turn this into a getelementptr ConstantExpr. |
| 716 | // |
| 717 | void SCCP::visitGetElementPtrInst(GetElementPtrInst &I) { |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 718 | InstVal &IV = ValueState[&I]; |
| 719 | if (IV.isOverdefined()) return; |
| 720 | |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 721 | std::vector<Constant*> Operands; |
| 722 | Operands.reserve(I.getNumOperands()); |
| 723 | |
| 724 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 725 | InstVal &State = getValueState(I.getOperand(i)); |
| 726 | if (State.isUndefined()) |
| 727 | return; // Operands are not resolved yet... |
| 728 | else if (State.isOverdefined()) { |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 729 | markOverdefined(IV, &I); |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 730 | return; |
| 731 | } |
| 732 | assert(State.isConstant() && "Unknown state!"); |
| 733 | Operands.push_back(State.getConstant()); |
| 734 | } |
| 735 | |
| 736 | Constant *Ptr = Operands[0]; |
| 737 | Operands.erase(Operands.begin()); // Erase the pointer from idx list... |
| 738 | |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 739 | markConstant(IV, &I, ConstantExpr::getGetElementPtr(Ptr, Operands)); |
Chris Lattner | 2a88bb7 | 2002-08-30 23:39:00 +0000 | [diff] [blame] | 740 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 741 | |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 742 | /// GetGEPGlobalInitializer - Given a constant and a getelementptr constantexpr, |
| 743 | /// return the constant value being addressed by the constant expression, or |
| 744 | /// null if something is funny. |
| 745 | /// |
| 746 | static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) { |
Chris Lattner | 28977af | 2004-04-05 01:30:19 +0000 | [diff] [blame] | 747 | if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 748 | return 0; // Do not allow stepping over the value! |
| 749 | |
| 750 | // Loop over all of the operands, tracking down which value we are |
| 751 | // addressing... |
| 752 | for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i) |
| 753 | if (ConstantUInt *CU = dyn_cast<ConstantUInt>(CE->getOperand(i))) { |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 754 | ConstantStruct *CS = dyn_cast<ConstantStruct>(C); |
| 755 | if (CS == 0) return 0; |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 756 | if (CU->getValue() >= CS->getNumOperands()) return 0; |
| 757 | C = CS->getOperand(CU->getValue()); |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 758 | } else if (ConstantSInt *CS = dyn_cast<ConstantSInt>(CE->getOperand(i))) { |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 759 | ConstantArray *CA = dyn_cast<ConstantArray>(C); |
| 760 | if (CA == 0) return 0; |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 761 | if ((uint64_t)CS->getValue() >= CA->getNumOperands()) return 0; |
| 762 | C = CA->getOperand(CS->getValue()); |
Chris Lattner | de512b5 | 2004-02-15 05:55:15 +0000 | [diff] [blame] | 763 | } else |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 764 | return 0; |
| 765 | return C; |
| 766 | } |
| 767 | |
| 768 | // Handle load instructions. If the operand is a constant pointer to a constant |
| 769 | // global, we can replace the load with the loaded constant value! |
| 770 | void SCCP::visitLoadInst(LoadInst &I) { |
| 771 | InstVal &IV = ValueState[&I]; |
| 772 | if (IV.isOverdefined()) return; |
| 773 | |
| 774 | InstVal &PtrVal = getValueState(I.getOperand(0)); |
| 775 | if (PtrVal.isUndefined()) return; // The pointer is not resolved yet! |
| 776 | if (PtrVal.isConstant() && !I.isVolatile()) { |
| 777 | Value *Ptr = PtrVal.getConstant(); |
Chris Lattner | c76d803 | 2004-03-07 22:16:24 +0000 | [diff] [blame] | 778 | if (isa<ConstantPointerNull>(Ptr)) { |
| 779 | // load null -> null |
| 780 | markConstant(IV, &I, Constant::getNullValue(I.getType())); |
| 781 | return; |
| 782 | } |
| 783 | |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 784 | // Transform load (constant global) into the value loaded. |
| 785 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) |
| 786 | if (GV->isConstant() && !GV->isExternal()) { |
| 787 | markConstant(IV, &I, GV->getInitializer()); |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | // Transform load (constantexpr_GEP global, 0, ...) into the value loaded. |
| 792 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 793 | if (CE->getOpcode() == Instruction::GetElementPtr) |
Reid Spencer | 21cb67e | 2004-07-18 00:31:05 +0000 | [diff] [blame] | 794 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 795 | if (GV->isConstant() && !GV->isExternal()) |
| 796 | if (Constant *V = |
| 797 | GetGEPGlobalInitializer(GV->getInitializer(), CE)) { |
| 798 | markConstant(IV, &I, V); |
| 799 | return; |
| 800 | } |
Chris Lattner | c6a4d6a | 2004-01-12 04:29:41 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | // Otherwise we cannot say for certain what value this load will produce. |
| 804 | // Bail out. |
| 805 | markOverdefined(IV, &I); |
| 806 | } |
Chris Lattner | 58b7b08 | 2004-04-13 19:43:54 +0000 | [diff] [blame] | 807 | |
| 808 | void SCCP::visitCallInst(CallInst &I) { |
| 809 | InstVal &IV = ValueState[&I]; |
| 810 | if (IV.isOverdefined()) return; |
| 811 | |
| 812 | Function *F = I.getCalledFunction(); |
| 813 | if (F == 0 || !canConstantFoldCallTo(F)) { |
| 814 | markOverdefined(IV, &I); |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | std::vector<Constant*> Operands; |
| 819 | Operands.reserve(I.getNumOperands()-1); |
| 820 | |
| 821 | for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { |
| 822 | InstVal &State = getValueState(I.getOperand(i)); |
| 823 | if (State.isUndefined()) |
| 824 | return; // Operands are not resolved yet... |
| 825 | else if (State.isOverdefined()) { |
| 826 | markOverdefined(IV, &I); |
| 827 | return; |
| 828 | } |
| 829 | assert(State.isConstant() && "Unknown state!"); |
| 830 | Operands.push_back(State.getConstant()); |
| 831 | } |
| 832 | |
| 833 | if (Constant *C = ConstantFoldCall(F, Operands)) |
| 834 | markConstant(IV, &I, C); |
| 835 | else |
| 836 | markOverdefined(IV, &I); |
| 837 | } |