Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 1 | //===- GVN.cpp - Eliminate redundant values and loads ---------------------===// |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass performs global value numbering to eliminate fully redundant |
| 11 | // instructions. It also performs simple dead load elimination. |
| 12 | // |
John Criswell | 090c0a2 | 2009-03-10 15:04:53 +0000 | [diff] [blame] | 13 | // Note that this pass does the value numbering itself; it does not use the |
Matthijs Kooijman | 845f524 | 2008-06-05 07:55:49 +0000 | [diff] [blame] | 14 | // ValueNumbering analysis passes. |
| 15 | // |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #define DEBUG_TYPE "gvn" |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | a53cfd1 | 2009-12-28 21:28:46 +0000 | [diff] [blame] | 20 | #include "llvm/GlobalVariable.h" |
Devang Patel | c64bc16 | 2009-03-06 02:59:27 +0000 | [diff] [blame] | 21 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | f4177aa | 2010-12-15 23:53:55 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" |
Owen Anderson | b388ca9 | 2007-10-18 19:39:33 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | bc9a28d | 2009-12-06 05:29:56 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/ConstantFolding.h" |
| 25 | #include "llvm/Analysis/Dominators.h" |
Duncan Sands | 88c3df7 | 2010-11-12 21:10:24 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/InstructionSimplify.h" |
Dan Gohman | dd9344f | 2010-05-28 16:19:17 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/Loads.h" |
Victor Hernandez | f006b18 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/MemoryBuiltins.h" |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Chris Lattner | 05e15f8 | 2009-12-09 01:59:31 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/PHITransAddr.h" |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | 9fc5cdf | 2011-01-02 22:09:33 +0000 | [diff] [blame] | 32 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetData.h" |
| 34 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
| 36 | #include "llvm/ADT/DenseMap.h" |
| 37 | #include "llvm/ADT/DepthFirstIterator.h" |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallPtrSet.h" |
| 39 | #include "llvm/ADT/Statistic.h" |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Allocator.h" |
Owen Anderson | aa0b634 | 2008-06-19 19:57:25 +0000 | [diff] [blame] | 41 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 9f8a6a7 | 2008-03-29 04:36:18 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Debug.h" |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 43 | #include "llvm/Support/IRBuilder.h" |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
Bill Wendling | 70ded19 | 2008-12-22 22:14:07 +0000 | [diff] [blame] | 46 | STATISTIC(NumGVNInstr, "Number of instructions deleted"); |
| 47 | STATISTIC(NumGVNLoad, "Number of loads deleted"); |
| 48 | STATISTIC(NumGVNPRE, "Number of instructions PRE'd"); |
Owen Anderson | 961edc8 | 2008-07-15 16:28:06 +0000 | [diff] [blame] | 49 | STATISTIC(NumGVNBlocks, "Number of blocks merged"); |
Bill Wendling | 70ded19 | 2008-12-22 22:14:07 +0000 | [diff] [blame] | 50 | STATISTIC(NumPRELoad, "Number of loads PRE'd"); |
Chris Lattner | d27290d | 2008-03-22 04:13:49 +0000 | [diff] [blame] | 51 | |
Evan Cheng | 88d11c0 | 2008-06-20 01:01:07 +0000 | [diff] [blame] | 52 | static cl::opt<bool> EnablePRE("enable-pre", |
Owen Anderson | c2b856e | 2008-07-17 19:41:00 +0000 | [diff] [blame] | 53 | cl::init(true), cl::Hidden); |
Dan Gohman | c915c95 | 2009-06-15 18:30:15 +0000 | [diff] [blame] | 54 | static cl::opt<bool> EnableLoadPRE("enable-load-pre", cl::init(true)); |
Owen Anderson | aa0b634 | 2008-06-19 19:57:25 +0000 | [diff] [blame] | 55 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===// |
| 57 | // ValueTable Class |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | |
| 60 | /// This class holds the mapping between values and value numbers. It is used |
| 61 | /// as an efficient mechanism to determine the expression-wise equivalence of |
| 62 | /// two values. |
| 63 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 64 | struct Expression { |
Owen Anderson | 30f4a55 | 2011-01-03 19:00:11 +0000 | [diff] [blame] | 65 | uint32_t opcode; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 66 | const Type* type; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 67 | SmallVector<uint32_t, 4> varargs; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 68 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 69 | Expression() { } |
Owen Anderson | 30f4a55 | 2011-01-03 19:00:11 +0000 | [diff] [blame] | 70 | Expression(uint32_t o) : opcode(o) { } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 71 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 72 | bool operator==(const Expression &other) const { |
| 73 | if (opcode != other.opcode) |
| 74 | return false; |
Owen Anderson | 30f4a55 | 2011-01-03 19:00:11 +0000 | [diff] [blame] | 75 | else if (opcode == ~0U || opcode == ~1U) |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 76 | return true; |
| 77 | else if (type != other.type) |
| 78 | return false; |
Benjamin Kramer | aad94aa | 2010-12-21 21:30:19 +0000 | [diff] [blame] | 79 | else if (varargs != other.varargs) |
| 80 | return false; |
| 81 | return true; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 82 | } |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 83 | }; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 85 | class ValueTable { |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 86 | private: |
| 87 | DenseMap<Value*, uint32_t> valueNumbering; |
| 88 | DenseMap<Expression, uint32_t> expressionNumbering; |
Owen Anderson | a472c4a | 2008-05-12 20:15:55 +0000 | [diff] [blame] | 89 | AliasAnalysis* AA; |
| 90 | MemoryDependenceAnalysis* MD; |
| 91 | DominatorTree* DT; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 92 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 93 | uint32_t nextValueNumber; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 94 | |
Owen Anderson | 30f4a55 | 2011-01-03 19:00:11 +0000 | [diff] [blame] | 95 | Expression create_expression(Instruction* I); |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 96 | uint32_t lookup_or_add_call(CallInst* C); |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 97 | public: |
Dan Gohman | e63c4a2 | 2009-04-01 16:37:47 +0000 | [diff] [blame] | 98 | ValueTable() : nextValueNumber(1) { } |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 99 | uint32_t lookup_or_add(Value *V); |
| 100 | uint32_t lookup(Value *V) const; |
| 101 | void add(Value *V, uint32_t num); |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 102 | void clear(); |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 103 | void erase(Value *v); |
Owen Anderson | a472c4a | 2008-05-12 20:15:55 +0000 | [diff] [blame] | 104 | void setAliasAnalysis(AliasAnalysis* A) { AA = A; } |
Chris Lattner | 663e441 | 2008-12-01 00:40:32 +0000 | [diff] [blame] | 105 | AliasAnalysis *getAliasAnalysis() const { return AA; } |
Owen Anderson | a472c4a | 2008-05-12 20:15:55 +0000 | [diff] [blame] | 106 | void setMemDep(MemoryDependenceAnalysis* M) { MD = M; } |
| 107 | void setDomTree(DominatorTree* D) { DT = D; } |
Owen Anderson | 0ae33ef | 2008-07-03 17:44:33 +0000 | [diff] [blame] | 108 | uint32_t getNextUnusedValueNumber() { return nextValueNumber; } |
Bill Wendling | 246dbbb | 2008-12-22 21:36:08 +0000 | [diff] [blame] | 109 | void verifyRemoved(const Value *) const; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 110 | }; |
| 111 | } |
| 112 | |
| 113 | namespace llvm { |
Chris Lattner | 76c1b97 | 2007-09-17 18:34:04 +0000 | [diff] [blame] | 114 | template <> struct DenseMapInfo<Expression> { |
Owen Anderson | 830db6a | 2007-08-02 18:16:06 +0000 | [diff] [blame] | 115 | static inline Expression getEmptyKey() { |
Owen Anderson | 30f4a55 | 2011-01-03 19:00:11 +0000 | [diff] [blame] | 116 | return ~0U; |
Owen Anderson | 830db6a | 2007-08-02 18:16:06 +0000 | [diff] [blame] | 117 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 118 | |
Owen Anderson | 830db6a | 2007-08-02 18:16:06 +0000 | [diff] [blame] | 119 | static inline Expression getTombstoneKey() { |
Owen Anderson | 30f4a55 | 2011-01-03 19:00:11 +0000 | [diff] [blame] | 120 | return ~1U; |
Owen Anderson | 830db6a | 2007-08-02 18:16:06 +0000 | [diff] [blame] | 121 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 122 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 123 | static unsigned getHashValue(const Expression e) { |
| 124 | unsigned hash = e.opcode; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 125 | |
Anton Korobeynikov | 07e6e56 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 126 | hash = ((unsigned)((uintptr_t)e.type >> 4) ^ |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 127 | (unsigned)((uintptr_t)e.type >> 9)); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 128 | |
Owen Anderson | 830db6a | 2007-08-02 18:16:06 +0000 | [diff] [blame] | 129 | for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(), |
| 130 | E = e.varargs.end(); I != E; ++I) |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 131 | hash = *I + hash * 37; |
Owen Anderson | 30f4a55 | 2011-01-03 19:00:11 +0000 | [diff] [blame] | 132 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 133 | return hash; |
| 134 | } |
Chris Lattner | 76c1b97 | 2007-09-17 18:34:04 +0000 | [diff] [blame] | 135 | static bool isEqual(const Expression &LHS, const Expression &RHS) { |
| 136 | return LHS == RHS; |
| 137 | } |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 138 | }; |
Chris Lattner | 4bbf4ee | 2009-12-15 07:26:43 +0000 | [diff] [blame] | 139 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | // ValueTable Internal Functions |
| 144 | //===----------------------------------------------------------------------===// |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 145 | |
Owen Anderson | 30f4a55 | 2011-01-03 19:00:11 +0000 | [diff] [blame] | 146 | |
| 147 | Expression ValueTable::create_expression(Instruction *I) { |
| 148 | Expression e; |
| 149 | e.type = I->getType(); |
| 150 | e.opcode = I->getOpcode(); |
| 151 | for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); |
| 152 | OI != OE; ++OI) |
| 153 | e.varargs.push_back(lookup_or_add(*OI)); |
| 154 | |
| 155 | if (CmpInst *C = dyn_cast<CmpInst>(I)) |
| 156 | e.opcode = (C->getOpcode() << 8) | C->getPredicate(); |
| 157 | else if (ExtractValueInst *E = dyn_cast<ExtractValueInst>(I)) { |
| 158 | for (ExtractValueInst::idx_iterator II = E->idx_begin(), IE = E->idx_end(); |
| 159 | II != IE; ++II) |
| 160 | e.varargs.push_back(*II); |
| 161 | } else if (InsertValueInst *E = dyn_cast<InsertValueInst>(I)) { |
| 162 | for (InsertValueInst::idx_iterator II = E->idx_begin(), IE = E->idx_end(); |
| 163 | II != IE; ++II) |
| 164 | e.varargs.push_back(*II); |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 165 | } |
Owen Anderson | 30f4a55 | 2011-01-03 19:00:11 +0000 | [diff] [blame] | 166 | |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 167 | return e; |
| 168 | } |
| 169 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 170 | //===----------------------------------------------------------------------===// |
| 171 | // ValueTable External Functions |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 174 | /// add - Insert a value into the table with a specified value number. |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 175 | void ValueTable::add(Value *V, uint32_t num) { |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 176 | valueNumbering.insert(std::make_pair(V, num)); |
| 177 | } |
| 178 | |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 179 | uint32_t ValueTable::lookup_or_add_call(CallInst* C) { |
| 180 | if (AA->doesNotAccessMemory(C)) { |
| 181 | Expression exp = create_expression(C); |
| 182 | uint32_t& e = expressionNumbering[exp]; |
| 183 | if (!e) e = nextValueNumber++; |
| 184 | valueNumbering[C] = e; |
| 185 | return e; |
| 186 | } else if (AA->onlyReadsMemory(C)) { |
| 187 | Expression exp = create_expression(C); |
| 188 | uint32_t& e = expressionNumbering[exp]; |
| 189 | if (!e) { |
| 190 | e = nextValueNumber++; |
| 191 | valueNumbering[C] = e; |
| 192 | return e; |
| 193 | } |
Dan Gohman | 4ec01b2 | 2009-11-14 02:27:51 +0000 | [diff] [blame] | 194 | if (!MD) { |
| 195 | e = nextValueNumber++; |
| 196 | valueNumbering[C] = e; |
| 197 | return e; |
| 198 | } |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 199 | |
| 200 | MemDepResult local_dep = MD->getDependency(C); |
| 201 | |
| 202 | if (!local_dep.isDef() && !local_dep.isNonLocal()) { |
| 203 | valueNumbering[C] = nextValueNumber; |
| 204 | return nextValueNumber++; |
| 205 | } |
| 206 | |
| 207 | if (local_dep.isDef()) { |
| 208 | CallInst* local_cdep = cast<CallInst>(local_dep.getInst()); |
| 209 | |
Gabor Greif | 237e1da | 2010-06-30 09:17:53 +0000 | [diff] [blame] | 210 | if (local_cdep->getNumArgOperands() != C->getNumArgOperands()) { |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 211 | valueNumbering[C] = nextValueNumber; |
| 212 | return nextValueNumber++; |
| 213 | } |
| 214 | |
Gabor Greif | d883a9d | 2010-06-24 10:17:17 +0000 | [diff] [blame] | 215 | for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) { |
| 216 | uint32_t c_vn = lookup_or_add(C->getArgOperand(i)); |
| 217 | uint32_t cd_vn = lookup_or_add(local_cdep->getArgOperand(i)); |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 218 | if (c_vn != cd_vn) { |
| 219 | valueNumbering[C] = nextValueNumber; |
| 220 | return nextValueNumber++; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | uint32_t v = lookup_or_add(local_cdep); |
| 225 | valueNumbering[C] = v; |
| 226 | return v; |
| 227 | } |
| 228 | |
| 229 | // Non-local case. |
| 230 | const MemoryDependenceAnalysis::NonLocalDepInfo &deps = |
| 231 | MD->getNonLocalCallDependency(CallSite(C)); |
| 232 | // FIXME: call/call dependencies for readonly calls should return def, not |
| 233 | // clobber! Move the checking logic to MemDep! |
| 234 | CallInst* cdep = 0; |
| 235 | |
| 236 | // Check to see if we have a single dominating call instruction that is |
| 237 | // identical to C. |
| 238 | for (unsigned i = 0, e = deps.size(); i != e; ++i) { |
Chris Lattner | e18b971 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 239 | const NonLocalDepEntry *I = &deps[i]; |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 240 | // Ignore non-local dependencies. |
Chris Lattner | e18b971 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 241 | if (I->getResult().isNonLocal()) |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 242 | continue; |
| 243 | |
| 244 | // We don't handle non-depedencies. If we already have a call, reject |
| 245 | // instruction dependencies. |
Chris Lattner | e18b971 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 246 | if (I->getResult().isClobber() || cdep != 0) { |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 247 | cdep = 0; |
| 248 | break; |
| 249 | } |
| 250 | |
Chris Lattner | e18b971 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 251 | CallInst *NonLocalDepCall = dyn_cast<CallInst>(I->getResult().getInst()); |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 252 | // FIXME: All duplicated with non-local case. |
Chris Lattner | e18b971 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 253 | if (NonLocalDepCall && DT->properlyDominates(I->getBB(), C->getParent())){ |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 254 | cdep = NonLocalDepCall; |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | cdep = 0; |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | if (!cdep) { |
| 263 | valueNumbering[C] = nextValueNumber; |
| 264 | return nextValueNumber++; |
| 265 | } |
| 266 | |
Gabor Greif | 237e1da | 2010-06-30 09:17:53 +0000 | [diff] [blame] | 267 | if (cdep->getNumArgOperands() != C->getNumArgOperands()) { |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 268 | valueNumbering[C] = nextValueNumber; |
| 269 | return nextValueNumber++; |
| 270 | } |
Gabor Greif | d883a9d | 2010-06-24 10:17:17 +0000 | [diff] [blame] | 271 | for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) { |
| 272 | uint32_t c_vn = lookup_or_add(C->getArgOperand(i)); |
| 273 | uint32_t cd_vn = lookup_or_add(cdep->getArgOperand(i)); |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 274 | if (c_vn != cd_vn) { |
| 275 | valueNumbering[C] = nextValueNumber; |
| 276 | return nextValueNumber++; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | uint32_t v = lookup_or_add(cdep); |
| 281 | valueNumbering[C] = v; |
| 282 | return v; |
| 283 | |
| 284 | } else { |
| 285 | valueNumbering[C] = nextValueNumber; |
| 286 | return nextValueNumber++; |
| 287 | } |
| 288 | } |
| 289 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 290 | /// lookup_or_add - Returns the value number for the specified value, assigning |
| 291 | /// it a new number if it did not have one before. |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 292 | uint32_t ValueTable::lookup_or_add(Value *V) { |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 293 | DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V); |
| 294 | if (VI != valueNumbering.end()) |
| 295 | return VI->second; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 296 | |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 297 | if (!isa<Instruction>(V)) { |
Owen Anderson | 158d86e | 2009-10-19 21:14:57 +0000 | [diff] [blame] | 298 | valueNumbering[V] = nextValueNumber; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 299 | return nextValueNumber++; |
| 300 | } |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 301 | |
| 302 | Instruction* I = cast<Instruction>(V); |
| 303 | Expression exp; |
| 304 | switch (I->getOpcode()) { |
| 305 | case Instruction::Call: |
| 306 | return lookup_or_add_call(cast<CallInst>(I)); |
| 307 | case Instruction::Add: |
| 308 | case Instruction::FAdd: |
| 309 | case Instruction::Sub: |
| 310 | case Instruction::FSub: |
| 311 | case Instruction::Mul: |
| 312 | case Instruction::FMul: |
| 313 | case Instruction::UDiv: |
| 314 | case Instruction::SDiv: |
| 315 | case Instruction::FDiv: |
| 316 | case Instruction::URem: |
| 317 | case Instruction::SRem: |
| 318 | case Instruction::FRem: |
| 319 | case Instruction::Shl: |
| 320 | case Instruction::LShr: |
| 321 | case Instruction::AShr: |
| 322 | case Instruction::And: |
| 323 | case Instruction::Or : |
| 324 | case Instruction::Xor: |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 325 | case Instruction::ICmp: |
| 326 | case Instruction::FCmp: |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 327 | case Instruction::Trunc: |
| 328 | case Instruction::ZExt: |
| 329 | case Instruction::SExt: |
| 330 | case Instruction::FPToUI: |
| 331 | case Instruction::FPToSI: |
| 332 | case Instruction::UIToFP: |
| 333 | case Instruction::SIToFP: |
| 334 | case Instruction::FPTrunc: |
| 335 | case Instruction::FPExt: |
| 336 | case Instruction::PtrToInt: |
| 337 | case Instruction::IntToPtr: |
| 338 | case Instruction::BitCast: |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 339 | case Instruction::Select: |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 340 | case Instruction::ExtractElement: |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 341 | case Instruction::InsertElement: |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 342 | case Instruction::ShuffleVector: |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 343 | case Instruction::ExtractValue: |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 344 | case Instruction::InsertValue: |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 345 | case Instruction::GetElementPtr: |
Owen Anderson | 30f4a55 | 2011-01-03 19:00:11 +0000 | [diff] [blame] | 346 | exp = create_expression(I); |
Owen Anderson | d41ed4e | 2009-10-19 22:14:22 +0000 | [diff] [blame] | 347 | break; |
| 348 | default: |
| 349 | valueNumbering[V] = nextValueNumber; |
| 350 | return nextValueNumber++; |
| 351 | } |
| 352 | |
| 353 | uint32_t& e = expressionNumbering[exp]; |
| 354 | if (!e) e = nextValueNumber++; |
| 355 | valueNumbering[V] = e; |
| 356 | return e; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /// lookup - Returns the value number of the specified value. Fails if |
| 360 | /// the value has not yet been numbered. |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 361 | uint32_t ValueTable::lookup(Value *V) const { |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 362 | DenseMap<Value*, uint32_t>::const_iterator VI = valueNumbering.find(V); |
Chris Lattner | 88365bb | 2008-03-21 21:14:38 +0000 | [diff] [blame] | 363 | assert(VI != valueNumbering.end() && "Value not numbered?"); |
| 364 | return VI->second; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /// clear - Remove all entries from the ValueTable |
| 368 | void ValueTable::clear() { |
| 369 | valueNumbering.clear(); |
| 370 | expressionNumbering.clear(); |
| 371 | nextValueNumber = 1; |
| 372 | } |
| 373 | |
Owen Anderson | bf7d0bc | 2007-07-31 23:27:13 +0000 | [diff] [blame] | 374 | /// erase - Remove a value from the value numbering |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 375 | void ValueTable::erase(Value *V) { |
Owen Anderson | bf7d0bc | 2007-07-31 23:27:13 +0000 | [diff] [blame] | 376 | valueNumbering.erase(V); |
| 377 | } |
| 378 | |
Bill Wendling | 246dbbb | 2008-12-22 21:36:08 +0000 | [diff] [blame] | 379 | /// verifyRemoved - Verify that the value is removed from all internal data |
| 380 | /// structures. |
| 381 | void ValueTable::verifyRemoved(const Value *V) const { |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 382 | for (DenseMap<Value*, uint32_t>::const_iterator |
Bill Wendling | 246dbbb | 2008-12-22 21:36:08 +0000 | [diff] [blame] | 383 | I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) { |
| 384 | assert(I->first != V && "Inst still occurs in value numbering map!"); |
| 385 | } |
| 386 | } |
| 387 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 388 | //===----------------------------------------------------------------------===// |
Bill Wendling | 30788b8 | 2008-12-22 22:32:22 +0000 | [diff] [blame] | 389 | // GVN Pass |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 390 | //===----------------------------------------------------------------------===// |
| 391 | |
| 392 | namespace { |
| 393 | |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 394 | class GVN : public FunctionPass { |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 395 | bool runOnFunction(Function &F); |
| 396 | public: |
| 397 | static char ID; // Pass identification, replacement for typeid |
Bob Wilson | b29d7d2 | 2010-02-28 05:34:05 +0000 | [diff] [blame] | 398 | explicit GVN(bool noloads = false) |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 399 | : FunctionPass(ID), NoLoads(noloads), MD(0) { |
| 400 | initializeGVNPass(*PassRegistry::getPassRegistry()); |
| 401 | } |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 402 | |
| 403 | private: |
Dan Gohman | 4ec01b2 | 2009-11-14 02:27:51 +0000 | [diff] [blame] | 404 | bool NoLoads; |
Chris Lattner | 663e441 | 2008-12-01 00:40:32 +0000 | [diff] [blame] | 405 | MemoryDependenceAnalysis *MD; |
| 406 | DominatorTree *DT; |
Duncan Sands | 88c3df7 | 2010-11-12 21:10:24 +0000 | [diff] [blame] | 407 | const TargetData* TD; |
Chris Lattner | 663e441 | 2008-12-01 00:40:32 +0000 | [diff] [blame] | 408 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 409 | ValueTable VN; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 410 | |
Owen Anderson | b1602ab | 2011-01-04 19:29:46 +0000 | [diff] [blame] | 411 | /// LeaderTable - A mapping from value numbers to lists of Value*'s that |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 412 | /// have that value number. Use findLeader to query it. |
| 413 | struct LeaderTableEntry { |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 414 | Value *Val; |
| 415 | BasicBlock *BB; |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 416 | LeaderTableEntry *Next; |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 417 | }; |
Owen Anderson | b1602ab | 2011-01-04 19:29:46 +0000 | [diff] [blame] | 418 | DenseMap<uint32_t, LeaderTableEntry> LeaderTable; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 419 | BumpPtrAllocator TableAllocator; |
Owen Anderson | 68c2639 | 2010-11-19 22:48:40 +0000 | [diff] [blame] | 420 | |
Owen Anderson | b1602ab | 2011-01-04 19:29:46 +0000 | [diff] [blame] | 421 | /// addToLeaderTable - Push a new Value to the LeaderTable onto the list for |
Owen Anderson | 68c2639 | 2010-11-19 22:48:40 +0000 | [diff] [blame] | 422 | /// its value number. |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 423 | void addToLeaderTable(uint32_t N, Value *V, BasicBlock *BB) { |
Owen Anderson | b1602ab | 2011-01-04 19:29:46 +0000 | [diff] [blame] | 424 | LeaderTableEntry& Curr = LeaderTable[N]; |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 425 | if (!Curr.Val) { |
| 426 | Curr.Val = V; |
| 427 | Curr.BB = BB; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 428 | return; |
| 429 | } |
| 430 | |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 431 | LeaderTableEntry* Node = TableAllocator.Allocate<LeaderTableEntry>(); |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 432 | Node->Val = V; |
| 433 | Node->BB = BB; |
| 434 | Node->Next = Curr.Next; |
| 435 | Curr.Next = Node; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Owen Anderson | b1602ab | 2011-01-04 19:29:46 +0000 | [diff] [blame] | 438 | /// removeFromLeaderTable - Scan the list of values corresponding to a given |
| 439 | /// value number, and remove the given value if encountered. |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 440 | void removeFromLeaderTable(uint32_t N, Value *V, BasicBlock *BB) { |
| 441 | LeaderTableEntry* Prev = 0; |
Owen Anderson | b1602ab | 2011-01-04 19:29:46 +0000 | [diff] [blame] | 442 | LeaderTableEntry* Curr = &LeaderTable[N]; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 443 | |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 444 | while (Curr->Val != V || Curr->BB != BB) { |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 445 | Prev = Curr; |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 446 | Curr = Curr->Next; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (Prev) { |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 450 | Prev->Next = Curr->Next; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 451 | } else { |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 452 | if (!Curr->Next) { |
| 453 | Curr->Val = 0; |
| 454 | Curr->BB = 0; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 455 | } else { |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 456 | LeaderTableEntry* Next = Curr->Next; |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 457 | Curr->Val = Next->Val; |
| 458 | Curr->BB = Next->BB; |
Owen Anderson | 680ac4f | 2011-01-04 19:10:54 +0000 | [diff] [blame] | 459 | Curr->Next = Next->Next; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 463 | |
Bob Wilson | 484d4a3 | 2010-02-16 19:51:59 +0000 | [diff] [blame] | 464 | // List of critical edges to be split between iterations. |
| 465 | SmallVector<std::pair<TerminatorInst*, unsigned>, 4> toSplit; |
| 466 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 467 | // This transformation requires dominator postdominator info |
| 468 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 469 | AU.addRequired<DominatorTree>(); |
Dan Gohman | 4ec01b2 | 2009-11-14 02:27:51 +0000 | [diff] [blame] | 470 | if (!NoLoads) |
| 471 | AU.addRequired<MemoryDependenceAnalysis>(); |
Owen Anderson | b388ca9 | 2007-10-18 19:39:33 +0000 | [diff] [blame] | 472 | AU.addRequired<AliasAnalysis>(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 473 | |
Owen Anderson | b70a571 | 2008-06-23 17:49:45 +0000 | [diff] [blame] | 474 | AU.addPreserved<DominatorTree>(); |
Owen Anderson | b388ca9 | 2007-10-18 19:39:33 +0000 | [diff] [blame] | 475 | AU.addPreserved<AliasAnalysis>(); |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 476 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 477 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 478 | // Helper fuctions |
| 479 | // FIXME: eliminate or document these better |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 480 | bool processLoad(LoadInst* L, |
Chris Lattner | 8e1e95c | 2008-03-21 22:01:16 +0000 | [diff] [blame] | 481 | SmallVectorImpl<Instruction*> &toErase); |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 482 | bool processInstruction(Instruction *I, |
Chris Lattner | 8e1e95c | 2008-03-21 22:01:16 +0000 | [diff] [blame] | 483 | SmallVectorImpl<Instruction*> &toErase); |
Owen Anderson | 830db6a | 2007-08-02 18:16:06 +0000 | [diff] [blame] | 484 | bool processNonLocalLoad(LoadInst* L, |
Chris Lattner | 8e1e95c | 2008-03-21 22:01:16 +0000 | [diff] [blame] | 485 | SmallVectorImpl<Instruction*> &toErase); |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 486 | bool processBlock(BasicBlock *BB); |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 487 | void dump(DenseMap<uint32_t, Value*>& d); |
Owen Anderson | 3e75a42 | 2007-08-14 18:04:11 +0000 | [diff] [blame] | 488 | bool iterateOnFunction(Function &F); |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 489 | bool performPRE(Function& F); |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 490 | Value *findLeader(BasicBlock *BB, uint32_t num); |
Nuno Lopes | 7cdd9ee | 2008-10-10 16:25:50 +0000 | [diff] [blame] | 491 | void cleanupGlobalSets(); |
Bill Wendling | 246dbbb | 2008-12-22 21:36:08 +0000 | [diff] [blame] | 492 | void verifyRemoved(const Instruction *I) const; |
Bob Wilson | 484d4a3 | 2010-02-16 19:51:59 +0000 | [diff] [blame] | 493 | bool splitCriticalEdges(); |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 494 | }; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 495 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 496 | char GVN::ID = 0; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // createGVNPass - The public interface to this file... |
Bob Wilson | b29d7d2 | 2010-02-28 05:34:05 +0000 | [diff] [blame] | 500 | FunctionPass *llvm::createGVNPass(bool NoLoads) { |
| 501 | return new GVN(NoLoads); |
Dan Gohman | 4ec01b2 | 2009-11-14 02:27:51 +0000 | [diff] [blame] | 502 | } |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 503 | |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 504 | INITIALIZE_PASS_BEGIN(GVN, "gvn", "Global Value Numbering", false, false) |
| 505 | INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis) |
| 506 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 507 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 508 | INITIALIZE_PASS_END(GVN, "gvn", "Global Value Numbering", false, false) |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 509 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 510 | void GVN::dump(DenseMap<uint32_t, Value*>& d) { |
Dan Gohman | ad12b26 | 2009-12-18 03:25:51 +0000 | [diff] [blame] | 511 | errs() << "{\n"; |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 512 | for (DenseMap<uint32_t, Value*>::iterator I = d.begin(), |
Owen Anderson | 0cd3203 | 2007-07-25 19:57:03 +0000 | [diff] [blame] | 513 | E = d.end(); I != E; ++I) { |
Dan Gohman | ad12b26 | 2009-12-18 03:25:51 +0000 | [diff] [blame] | 514 | errs() << I->first << "\n"; |
Owen Anderson | 0cd3203 | 2007-07-25 19:57:03 +0000 | [diff] [blame] | 515 | I->second->dump(); |
| 516 | } |
Dan Gohman | ad12b26 | 2009-12-18 03:25:51 +0000 | [diff] [blame] | 517 | errs() << "}\n"; |
Owen Anderson | 0cd3203 | 2007-07-25 19:57:03 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 520 | /// IsValueFullyAvailableInBlock - Return true if we can prove that the value |
| 521 | /// we're analyzing is fully available in the specified block. As we go, keep |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 522 | /// track of which blocks we know are fully alive in FullyAvailableBlocks. This |
| 523 | /// map is actually a tri-state map with the following values: |
| 524 | /// 0) we know the block *is not* fully available. |
| 525 | /// 1) we know the block *is* fully available. |
| 526 | /// 2) we do not know whether the block is fully available or not, but we are |
| 527 | /// currently speculating that it will be. |
| 528 | /// 3) we are speculating for this block and have used that to speculate for |
| 529 | /// other blocks. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 530 | static bool IsValueFullyAvailableInBlock(BasicBlock *BB, |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 531 | DenseMap<BasicBlock*, char> &FullyAvailableBlocks) { |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 532 | // Optimistically assume that the block is fully available and check to see |
| 533 | // if we already know about this block in one lookup. |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 534 | std::pair<DenseMap<BasicBlock*, char>::iterator, char> IV = |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 535 | FullyAvailableBlocks.insert(std::make_pair(BB, 2)); |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 536 | |
| 537 | // If the entry already existed for this block, return the precomputed value. |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 538 | if (!IV.second) { |
| 539 | // If this is a speculative "available" value, mark it as being used for |
| 540 | // speculation of other blocks. |
| 541 | if (IV.first->second == 2) |
| 542 | IV.first->second = 3; |
| 543 | return IV.first->second != 0; |
| 544 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 545 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 546 | // Otherwise, see if it is fully available in all predecessors. |
| 547 | pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 548 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 549 | // If this block has no predecessors, it isn't live-in here. |
| 550 | if (PI == PE) |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 551 | goto SpeculationFailure; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 552 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 553 | for (; PI != PE; ++PI) |
| 554 | // If the value isn't fully available in one of our predecessors, then it |
| 555 | // isn't fully available in this block either. Undo our previous |
| 556 | // optimistic assumption and bail out. |
| 557 | if (!IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks)) |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 558 | goto SpeculationFailure; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 559 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 560 | return true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 561 | |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 562 | // SpeculationFailure - If we get here, we found out that this is not, after |
| 563 | // all, a fully-available block. We have a problem if we speculated on this and |
| 564 | // used the speculation to mark other blocks as available. |
| 565 | SpeculationFailure: |
| 566 | char &BBVal = FullyAvailableBlocks[BB]; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 568 | // If we didn't speculate on this, just return with it set to false. |
| 569 | if (BBVal == 2) { |
| 570 | BBVal = 0; |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | // If we did speculate on this value, we could have blocks set to 1 that are |
| 575 | // incorrect. Walk the (transitive) successors of this block and mark them as |
| 576 | // 0 if set to one. |
| 577 | SmallVector<BasicBlock*, 32> BBWorklist; |
| 578 | BBWorklist.push_back(BB); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 579 | |
Dan Gohman | 321a813 | 2010-01-05 16:27:25 +0000 | [diff] [blame] | 580 | do { |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 581 | BasicBlock *Entry = BBWorklist.pop_back_val(); |
| 582 | // Note that this sets blocks to 0 (unavailable) if they happen to not |
| 583 | // already be in FullyAvailableBlocks. This is safe. |
| 584 | char &EntryVal = FullyAvailableBlocks[Entry]; |
| 585 | if (EntryVal == 0) continue; // Already unavailable. |
| 586 | |
| 587 | // Mark as unavailable. |
| 588 | EntryVal = 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 590 | for (succ_iterator I = succ_begin(Entry), E = succ_end(Entry); I != E; ++I) |
| 591 | BBWorklist.push_back(*I); |
Dan Gohman | 321a813 | 2010-01-05 16:27:25 +0000 | [diff] [blame] | 592 | } while (!BBWorklist.empty()); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 594 | return false; |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 8b2bc3d | 2009-09-21 17:24:04 +0000 | [diff] [blame] | 598 | /// CanCoerceMustAliasedValueToLoad - Return true if |
| 599 | /// CoerceAvailableValueToLoadType will succeed. |
| 600 | static bool CanCoerceMustAliasedValueToLoad(Value *StoredVal, |
| 601 | const Type *LoadTy, |
| 602 | const TargetData &TD) { |
| 603 | // If the loaded or stored value is an first class array or struct, don't try |
| 604 | // to transform them. We need to be able to bitcast to integer. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 605 | if (LoadTy->isStructTy() || LoadTy->isArrayTy() || |
| 606 | StoredVal->getType()->isStructTy() || |
| 607 | StoredVal->getType()->isArrayTy()) |
Chris Lattner | 8b2bc3d | 2009-09-21 17:24:04 +0000 | [diff] [blame] | 608 | return false; |
| 609 | |
| 610 | // The store has to be at least as big as the load. |
| 611 | if (TD.getTypeSizeInBits(StoredVal->getType()) < |
| 612 | TD.getTypeSizeInBits(LoadTy)) |
| 613 | return false; |
| 614 | |
| 615 | return true; |
| 616 | } |
| 617 | |
| 618 | |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 619 | /// CoerceAvailableValueToLoadType - If we saw a store of a value to memory, and |
| 620 | /// then a load from a must-aliased pointer of a different type, try to coerce |
| 621 | /// the stored value. LoadedTy is the type of the load we want to replace and |
| 622 | /// InsertPt is the place to insert new instructions. |
| 623 | /// |
| 624 | /// If we can't do it, return null. |
| 625 | static Value *CoerceAvailableValueToLoadType(Value *StoredVal, |
| 626 | const Type *LoadedTy, |
| 627 | Instruction *InsertPt, |
| 628 | const TargetData &TD) { |
Chris Lattner | 8b2bc3d | 2009-09-21 17:24:04 +0000 | [diff] [blame] | 629 | if (!CanCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, TD)) |
| 630 | return 0; |
| 631 | |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 632 | const Type *StoredValTy = StoredVal->getType(); |
| 633 | |
Chris Lattner | 7944c21 | 2010-05-08 20:01:44 +0000 | [diff] [blame] | 634 | uint64_t StoreSize = TD.getTypeStoreSizeInBits(StoredValTy); |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 635 | uint64_t LoadSize = TD.getTypeSizeInBits(LoadedTy); |
| 636 | |
| 637 | // If the store and reload are the same size, we can always reuse it. |
| 638 | if (StoreSize == LoadSize) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 639 | if (StoredValTy->isPointerTy() && LoadedTy->isPointerTy()) { |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 640 | // Pointer to Pointer -> use bitcast. |
| 641 | return new BitCastInst(StoredVal, LoadedTy, "", InsertPt); |
| 642 | } |
| 643 | |
| 644 | // Convert source pointers to integers, which can be bitcast. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 645 | if (StoredValTy->isPointerTy()) { |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 646 | StoredValTy = TD.getIntPtrType(StoredValTy->getContext()); |
| 647 | StoredVal = new PtrToIntInst(StoredVal, StoredValTy, "", InsertPt); |
| 648 | } |
| 649 | |
| 650 | const Type *TypeToCastTo = LoadedTy; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 651 | if (TypeToCastTo->isPointerTy()) |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 652 | TypeToCastTo = TD.getIntPtrType(StoredValTy->getContext()); |
| 653 | |
| 654 | if (StoredValTy != TypeToCastTo) |
| 655 | StoredVal = new BitCastInst(StoredVal, TypeToCastTo, "", InsertPt); |
| 656 | |
| 657 | // Cast to pointer if the load needs a pointer type. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 658 | if (LoadedTy->isPointerTy()) |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 659 | StoredVal = new IntToPtrInst(StoredVal, LoadedTy, "", InsertPt); |
| 660 | |
| 661 | return StoredVal; |
| 662 | } |
| 663 | |
| 664 | // If the loaded value is smaller than the available value, then we can |
| 665 | // extract out a piece from it. If the available value is too small, then we |
| 666 | // can't do anything. |
Chris Lattner | 8b2bc3d | 2009-09-21 17:24:04 +0000 | [diff] [blame] | 667 | assert(StoreSize >= LoadSize && "CanCoerceMustAliasedValueToLoad fail"); |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 668 | |
| 669 | // Convert source pointers to integers, which can be manipulated. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 670 | if (StoredValTy->isPointerTy()) { |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 671 | StoredValTy = TD.getIntPtrType(StoredValTy->getContext()); |
| 672 | StoredVal = new PtrToIntInst(StoredVal, StoredValTy, "", InsertPt); |
| 673 | } |
| 674 | |
| 675 | // Convert vectors and fp to integer, which can be manipulated. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 676 | if (!StoredValTy->isIntegerTy()) { |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 677 | StoredValTy = IntegerType::get(StoredValTy->getContext(), StoreSize); |
| 678 | StoredVal = new BitCastInst(StoredVal, StoredValTy, "", InsertPt); |
| 679 | } |
| 680 | |
| 681 | // If this is a big-endian system, we need to shift the value down to the low |
| 682 | // bits so that a truncate will work. |
| 683 | if (TD.isBigEndian()) { |
| 684 | Constant *Val = ConstantInt::get(StoredVal->getType(), StoreSize-LoadSize); |
| 685 | StoredVal = BinaryOperator::CreateLShr(StoredVal, Val, "tmp", InsertPt); |
| 686 | } |
| 687 | |
| 688 | // Truncate the integer to the right size now. |
| 689 | const Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadSize); |
| 690 | StoredVal = new TruncInst(StoredVal, NewIntTy, "trunc", InsertPt); |
| 691 | |
| 692 | if (LoadedTy == NewIntTy) |
| 693 | return StoredVal; |
| 694 | |
| 695 | // If the result is a pointer, inttoptr. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 696 | if (LoadedTy->isPointerTy()) |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 697 | return new IntToPtrInst(StoredVal, LoadedTy, "inttoptr", InsertPt); |
| 698 | |
| 699 | // Otherwise, bitcast. |
| 700 | return new BitCastInst(StoredVal, LoadedTy, "bitcast", InsertPt); |
| 701 | } |
| 702 | |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 703 | /// AnalyzeLoadFromClobberingWrite - This function is called when we have a |
| 704 | /// memdep query of a load that ends up being a clobbering memory write (store, |
| 705 | /// memset, memcpy, memmove). This means that the write *may* provide bits used |
| 706 | /// by the load but we can't be sure because the pointers don't mustalias. |
| 707 | /// |
| 708 | /// Check this case to see if there is anything more we can do before we give |
| 709 | /// up. This returns -1 if we have to give up, or a byte number in the stored |
| 710 | /// value of the piece that feeds the load. |
Chris Lattner | 03f17da | 2009-12-09 07:34:10 +0000 | [diff] [blame] | 711 | static int AnalyzeLoadFromClobberingWrite(const Type *LoadTy, Value *LoadPtr, |
| 712 | Value *WritePtr, |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 713 | uint64_t WriteSizeInBits, |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 714 | const TargetData &TD) { |
Chris Lattner | 8b2bc3d | 2009-09-21 17:24:04 +0000 | [diff] [blame] | 715 | // If the loaded or stored value is an first class array or struct, don't try |
| 716 | // to transform them. We need to be able to bitcast to integer. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 717 | if (LoadTy->isStructTy() || LoadTy->isArrayTy()) |
Chris Lattner | 8b2bc3d | 2009-09-21 17:24:04 +0000 | [diff] [blame] | 718 | return -1; |
| 719 | |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 720 | int64_t StoreOffset = 0, LoadOffset = 0; |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 721 | Value *StoreBase = GetPointerBaseWithConstantOffset(WritePtr, StoreOffset,TD); |
| 722 | Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, TD); |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 723 | if (StoreBase != LoadBase) |
| 724 | return -1; |
| 725 | |
| 726 | // If the load and store are to the exact same address, they should have been |
| 727 | // a must alias. AA must have gotten confused. |
Chris Lattner | 219d774 | 2010-03-25 05:58:19 +0000 | [diff] [blame] | 728 | // FIXME: Study to see if/when this happens. One case is forwarding a memset |
| 729 | // to a load from the base of the memset. |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 730 | #if 0 |
Chris Lattner | 219d774 | 2010-03-25 05:58:19 +0000 | [diff] [blame] | 731 | if (LoadOffset == StoreOffset) { |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 732 | dbgs() << "STORE/LOAD DEP WITH COMMON POINTER MISSED:\n" |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 733 | << "Base = " << *StoreBase << "\n" |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 734 | << "Store Ptr = " << *WritePtr << "\n" |
| 735 | << "Store Offs = " << StoreOffset << "\n" |
Chris Lattner | b6760b4 | 2009-12-10 00:04:46 +0000 | [diff] [blame] | 736 | << "Load Ptr = " << *LoadPtr << "\n"; |
Chris Lattner | b3f927f | 2009-12-09 02:41:54 +0000 | [diff] [blame] | 737 | abort(); |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | 219d774 | 2010-03-25 05:58:19 +0000 | [diff] [blame] | 739 | #endif |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 740 | |
| 741 | // If the load and store don't overlap at all, the store doesn't provide |
| 742 | // anything to the load. In this case, they really don't alias at all, AA |
| 743 | // must have gotten confused. |
Chris Lattner | 03f17da | 2009-12-09 07:34:10 +0000 | [diff] [blame] | 744 | uint64_t LoadSize = TD.getTypeSizeInBits(LoadTy); |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 745 | |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 746 | if ((WriteSizeInBits & 7) | (LoadSize & 7)) |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 747 | return -1; |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 748 | uint64_t StoreSize = WriteSizeInBits >> 3; // Convert to bytes. |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 749 | LoadSize >>= 3; |
| 750 | |
| 751 | |
| 752 | bool isAAFailure = false; |
Chris Lattner | 219d774 | 2010-03-25 05:58:19 +0000 | [diff] [blame] | 753 | if (StoreOffset < LoadOffset) |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 754 | isAAFailure = StoreOffset+int64_t(StoreSize) <= LoadOffset; |
Chris Lattner | 219d774 | 2010-03-25 05:58:19 +0000 | [diff] [blame] | 755 | else |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 756 | isAAFailure = LoadOffset+int64_t(LoadSize) <= StoreOffset; |
Chris Lattner | 219d774 | 2010-03-25 05:58:19 +0000 | [diff] [blame] | 757 | |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 758 | if (isAAFailure) { |
| 759 | #if 0 |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 760 | dbgs() << "STORE LOAD DEP WITH COMMON BASE:\n" |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 761 | << "Base = " << *StoreBase << "\n" |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 762 | << "Store Ptr = " << *WritePtr << "\n" |
| 763 | << "Store Offs = " << StoreOffset << "\n" |
Chris Lattner | b6760b4 | 2009-12-10 00:04:46 +0000 | [diff] [blame] | 764 | << "Load Ptr = " << *LoadPtr << "\n"; |
Chris Lattner | b3f927f | 2009-12-09 02:41:54 +0000 | [diff] [blame] | 765 | abort(); |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 766 | #endif |
| 767 | return -1; |
| 768 | } |
| 769 | |
| 770 | // If the Load isn't completely contained within the stored bits, we don't |
| 771 | // have all the bits to feed it. We could do something crazy in the future |
| 772 | // (issue a smaller load then merge the bits in) but this seems unlikely to be |
| 773 | // valuable. |
| 774 | if (StoreOffset > LoadOffset || |
| 775 | StoreOffset+StoreSize < LoadOffset+LoadSize) |
| 776 | return -1; |
| 777 | |
| 778 | // Okay, we can do this transformation. Return the number of bytes into the |
| 779 | // store that the load is. |
| 780 | return LoadOffset-StoreOffset; |
| 781 | } |
| 782 | |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 783 | /// AnalyzeLoadFromClobberingStore - This function is called when we have a |
| 784 | /// memdep query of a load that ends up being a clobbering store. |
Chris Lattner | 4ca70fe | 2009-12-09 07:37:07 +0000 | [diff] [blame] | 785 | static int AnalyzeLoadFromClobberingStore(const Type *LoadTy, Value *LoadPtr, |
| 786 | StoreInst *DepSI, |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 787 | const TargetData &TD) { |
| 788 | // Cannot handle reading from store of first-class aggregate yet. |
Dan Gohman | 3355c4e | 2010-11-10 19:03:33 +0000 | [diff] [blame] | 789 | if (DepSI->getValueOperand()->getType()->isStructTy() || |
| 790 | DepSI->getValueOperand()->getType()->isArrayTy()) |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 791 | return -1; |
| 792 | |
| 793 | Value *StorePtr = DepSI->getPointerOperand(); |
Dan Gohman | 3355c4e | 2010-11-10 19:03:33 +0000 | [diff] [blame] | 794 | uint64_t StoreSize =TD.getTypeSizeInBits(DepSI->getValueOperand()->getType()); |
Chris Lattner | 4ca70fe | 2009-12-09 07:37:07 +0000 | [diff] [blame] | 795 | return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, |
Chris Lattner | 03f17da | 2009-12-09 07:34:10 +0000 | [diff] [blame] | 796 | StorePtr, StoreSize, TD); |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Chris Lattner | 4ca70fe | 2009-12-09 07:37:07 +0000 | [diff] [blame] | 799 | static int AnalyzeLoadFromClobberingMemInst(const Type *LoadTy, Value *LoadPtr, |
| 800 | MemIntrinsic *MI, |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 801 | const TargetData &TD) { |
| 802 | // If the mem operation is a non-constant size, we can't handle it. |
| 803 | ConstantInt *SizeCst = dyn_cast<ConstantInt>(MI->getLength()); |
| 804 | if (SizeCst == 0) return -1; |
| 805 | uint64_t MemSizeInBits = SizeCst->getZExtValue()*8; |
Chris Lattner | bc9a28d | 2009-12-06 05:29:56 +0000 | [diff] [blame] | 806 | |
| 807 | // If this is memset, we just need to see if the offset is valid in the size |
| 808 | // of the memset.. |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 809 | if (MI->getIntrinsicID() == Intrinsic::memset) |
Chris Lattner | 4ca70fe | 2009-12-09 07:37:07 +0000 | [diff] [blame] | 810 | return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(), |
| 811 | MemSizeInBits, TD); |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 812 | |
Chris Lattner | bc9a28d | 2009-12-06 05:29:56 +0000 | [diff] [blame] | 813 | // If we have a memcpy/memmove, the only case we can handle is if this is a |
| 814 | // copy from constant memory. In that case, we can read directly from the |
| 815 | // constant memory. |
| 816 | MemTransferInst *MTI = cast<MemTransferInst>(MI); |
| 817 | |
| 818 | Constant *Src = dyn_cast<Constant>(MTI->getSource()); |
| 819 | if (Src == 0) return -1; |
| 820 | |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 821 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Src)); |
Chris Lattner | bc9a28d | 2009-12-06 05:29:56 +0000 | [diff] [blame] | 822 | if (GV == 0 || !GV->isConstant()) return -1; |
| 823 | |
| 824 | // See if the access is within the bounds of the transfer. |
Chris Lattner | 4ca70fe | 2009-12-09 07:37:07 +0000 | [diff] [blame] | 825 | int Offset = AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, |
| 826 | MI->getDest(), MemSizeInBits, TD); |
Chris Lattner | bc9a28d | 2009-12-06 05:29:56 +0000 | [diff] [blame] | 827 | if (Offset == -1) |
| 828 | return Offset; |
| 829 | |
| 830 | // Otherwise, see if we can constant fold a load from the constant with the |
| 831 | // offset applied as appropriate. |
| 832 | Src = ConstantExpr::getBitCast(Src, |
| 833 | llvm::Type::getInt8PtrTy(Src->getContext())); |
| 834 | Constant *OffsetCst = |
| 835 | ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset); |
| 836 | Src = ConstantExpr::getGetElementPtr(Src, &OffsetCst, 1); |
Chris Lattner | 4ca70fe | 2009-12-09 07:37:07 +0000 | [diff] [blame] | 837 | Src = ConstantExpr::getBitCast(Src, PointerType::getUnqual(LoadTy)); |
Chris Lattner | bc9a28d | 2009-12-06 05:29:56 +0000 | [diff] [blame] | 838 | if (ConstantFoldLoadFromConstPtr(Src, &TD)) |
| 839 | return Offset; |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 840 | return -1; |
| 841 | } |
| 842 | |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 843 | |
| 844 | /// GetStoreValueForLoad - This function is called when we have a |
| 845 | /// memdep query of a load that ends up being a clobbering store. This means |
| 846 | /// that the store *may* provide bits used by the load but we can't be sure |
| 847 | /// because the pointers don't mustalias. Check this case to see if there is |
| 848 | /// anything more we can do before we give up. |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 849 | static Value *GetStoreValueForLoad(Value *SrcVal, unsigned Offset, |
| 850 | const Type *LoadTy, |
| 851 | Instruction *InsertPt, const TargetData &TD){ |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 852 | LLVMContext &Ctx = SrcVal->getType()->getContext(); |
| 853 | |
Chris Lattner | 7944c21 | 2010-05-08 20:01:44 +0000 | [diff] [blame] | 854 | uint64_t StoreSize = (TD.getTypeSizeInBits(SrcVal->getType()) + 7) / 8; |
| 855 | uint64_t LoadSize = (TD.getTypeSizeInBits(LoadTy) + 7) / 8; |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 856 | |
Chris Lattner | b2c6ae8 | 2009-12-09 18:13:28 +0000 | [diff] [blame] | 857 | IRBuilder<> Builder(InsertPt->getParent(), InsertPt); |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 858 | |
| 859 | // Compute which bits of the stored value are being used by the load. Convert |
| 860 | // to an integer type to start with. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 861 | if (SrcVal->getType()->isPointerTy()) |
Chris Lattner | b2c6ae8 | 2009-12-09 18:13:28 +0000 | [diff] [blame] | 862 | SrcVal = Builder.CreatePtrToInt(SrcVal, TD.getIntPtrType(Ctx), "tmp"); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 863 | if (!SrcVal->getType()->isIntegerTy()) |
Chris Lattner | b2c6ae8 | 2009-12-09 18:13:28 +0000 | [diff] [blame] | 864 | SrcVal = Builder.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize*8), |
| 865 | "tmp"); |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 866 | |
| 867 | // Shift the bits to the least significant depending on endianness. |
| 868 | unsigned ShiftAmt; |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 869 | if (TD.isLittleEndian()) |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 870 | ShiftAmt = Offset*8; |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 871 | else |
Chris Lattner | 19ad784 | 2009-09-21 17:55:47 +0000 | [diff] [blame] | 872 | ShiftAmt = (StoreSize-LoadSize-Offset)*8; |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 873 | |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 874 | if (ShiftAmt) |
Chris Lattner | b2c6ae8 | 2009-12-09 18:13:28 +0000 | [diff] [blame] | 875 | SrcVal = Builder.CreateLShr(SrcVal, ShiftAmt, "tmp"); |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 876 | |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 877 | if (LoadSize != StoreSize) |
Chris Lattner | b2c6ae8 | 2009-12-09 18:13:28 +0000 | [diff] [blame] | 878 | SrcVal = Builder.CreateTrunc(SrcVal, IntegerType::get(Ctx, LoadSize*8), |
| 879 | "tmp"); |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 880 | |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 881 | return CoerceAvailableValueToLoadType(SrcVal, LoadTy, InsertPt, TD); |
Chris Lattner | ca74940 | 2009-09-21 06:24:16 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 884 | /// GetMemInstValueForLoad - This function is called when we have a |
| 885 | /// memdep query of a load that ends up being a clobbering mem intrinsic. |
| 886 | static Value *GetMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, |
| 887 | const Type *LoadTy, Instruction *InsertPt, |
| 888 | const TargetData &TD){ |
| 889 | LLVMContext &Ctx = LoadTy->getContext(); |
| 890 | uint64_t LoadSize = TD.getTypeSizeInBits(LoadTy)/8; |
| 891 | |
| 892 | IRBuilder<> Builder(InsertPt->getParent(), InsertPt); |
| 893 | |
| 894 | // We know that this method is only called when the mem transfer fully |
| 895 | // provides the bits for the load. |
| 896 | if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) { |
| 897 | // memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and |
| 898 | // independently of what the offset is. |
| 899 | Value *Val = MSI->getValue(); |
| 900 | if (LoadSize != 1) |
| 901 | Val = Builder.CreateZExt(Val, IntegerType::get(Ctx, LoadSize*8)); |
| 902 | |
| 903 | Value *OneElt = Val; |
| 904 | |
| 905 | // Splat the value out to the right number of bits. |
| 906 | for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize; ) { |
| 907 | // If we can double the number of bytes set, do it. |
| 908 | if (NumBytesSet*2 <= LoadSize) { |
| 909 | Value *ShVal = Builder.CreateShl(Val, NumBytesSet*8); |
| 910 | Val = Builder.CreateOr(Val, ShVal); |
| 911 | NumBytesSet <<= 1; |
| 912 | continue; |
| 913 | } |
| 914 | |
| 915 | // Otherwise insert one byte at a time. |
| 916 | Value *ShVal = Builder.CreateShl(Val, 1*8); |
| 917 | Val = Builder.CreateOr(OneElt, ShVal); |
| 918 | ++NumBytesSet; |
| 919 | } |
| 920 | |
| 921 | return CoerceAvailableValueToLoadType(Val, LoadTy, InsertPt, TD); |
| 922 | } |
Chris Lattner | bc9a28d | 2009-12-06 05:29:56 +0000 | [diff] [blame] | 923 | |
| 924 | // Otherwise, this is a memcpy/memmove from a constant global. |
| 925 | MemTransferInst *MTI = cast<MemTransferInst>(SrcInst); |
| 926 | Constant *Src = cast<Constant>(MTI->getSource()); |
| 927 | |
| 928 | // Otherwise, see if we can constant fold a load from the constant with the |
| 929 | // offset applied as appropriate. |
| 930 | Src = ConstantExpr::getBitCast(Src, |
| 931 | llvm::Type::getInt8PtrTy(Src->getContext())); |
| 932 | Constant *OffsetCst = |
| 933 | ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset); |
| 934 | Src = ConstantExpr::getGetElementPtr(Src, &OffsetCst, 1); |
| 935 | Src = ConstantExpr::getBitCast(Src, PointerType::getUnqual(LoadTy)); |
| 936 | return ConstantFoldLoadFromConstPtr(Src, &TD); |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 939 | namespace { |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 940 | |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 941 | struct AvailableValueInBlock { |
| 942 | /// BB - The basic block in question. |
| 943 | BasicBlock *BB; |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 944 | enum ValType { |
| 945 | SimpleVal, // A simple offsetted value that is accessed. |
| 946 | MemIntrin // A memory intrinsic which is loaded from. |
| 947 | }; |
| 948 | |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 949 | /// V - The value that is live out of the block. |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 950 | PointerIntPair<Value *, 1, ValType> Val; |
| 951 | |
| 952 | /// Offset - The byte offset in Val that is interesting for the load query. |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 953 | unsigned Offset; |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 954 | |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 955 | static AvailableValueInBlock get(BasicBlock *BB, Value *V, |
| 956 | unsigned Offset = 0) { |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 957 | AvailableValueInBlock Res; |
| 958 | Res.BB = BB; |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 959 | Res.Val.setPointer(V); |
| 960 | Res.Val.setInt(SimpleVal); |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 961 | Res.Offset = Offset; |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 962 | return Res; |
| 963 | } |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 964 | |
| 965 | static AvailableValueInBlock getMI(BasicBlock *BB, MemIntrinsic *MI, |
| 966 | unsigned Offset = 0) { |
| 967 | AvailableValueInBlock Res; |
| 968 | Res.BB = BB; |
| 969 | Res.Val.setPointer(MI); |
| 970 | Res.Val.setInt(MemIntrin); |
| 971 | Res.Offset = Offset; |
| 972 | return Res; |
| 973 | } |
| 974 | |
| 975 | bool isSimpleValue() const { return Val.getInt() == SimpleVal; } |
| 976 | Value *getSimpleValue() const { |
| 977 | assert(isSimpleValue() && "Wrong accessor"); |
| 978 | return Val.getPointer(); |
| 979 | } |
| 980 | |
| 981 | MemIntrinsic *getMemIntrinValue() const { |
| 982 | assert(!isSimpleValue() && "Wrong accessor"); |
| 983 | return cast<MemIntrinsic>(Val.getPointer()); |
| 984 | } |
Chris Lattner | 5362c54 | 2009-12-21 23:04:33 +0000 | [diff] [blame] | 985 | |
| 986 | /// MaterializeAdjustedValue - Emit code into this block to adjust the value |
| 987 | /// defined here to the specified type. This handles various coercion cases. |
| 988 | Value *MaterializeAdjustedValue(const Type *LoadTy, |
| 989 | const TargetData *TD) const { |
| 990 | Value *Res; |
| 991 | if (isSimpleValue()) { |
| 992 | Res = getSimpleValue(); |
| 993 | if (Res->getType() != LoadTy) { |
| 994 | assert(TD && "Need target data to handle type mismatch case"); |
| 995 | Res = GetStoreValueForLoad(Res, Offset, LoadTy, BB->getTerminator(), |
| 996 | *TD); |
| 997 | |
| 998 | DEBUG(errs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset << " " |
| 999 | << *getSimpleValue() << '\n' |
| 1000 | << *Res << '\n' << "\n\n\n"); |
| 1001 | } |
| 1002 | } else { |
| 1003 | Res = GetMemInstValueForLoad(getMemIntrinValue(), Offset, |
| 1004 | LoadTy, BB->getTerminator(), *TD); |
| 1005 | DEBUG(errs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset |
| 1006 | << " " << *getMemIntrinValue() << '\n' |
| 1007 | << *Res << '\n' << "\n\n\n"); |
| 1008 | } |
| 1009 | return Res; |
| 1010 | } |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 1011 | }; |
| 1012 | |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Chris Lattner | a09fbf0 | 2009-10-10 23:50:30 +0000 | [diff] [blame] | 1015 | /// ConstructSSAForLoadSet - Given a set of loads specified by ValuesPerBlock, |
| 1016 | /// construct SSA form, allowing us to eliminate LI. This returns the value |
| 1017 | /// that should be used at LI's definition site. |
| 1018 | static Value *ConstructSSAForLoadSet(LoadInst *LI, |
| 1019 | SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock, |
| 1020 | const TargetData *TD, |
Chris Lattner | d2191e5 | 2009-12-21 23:15:48 +0000 | [diff] [blame] | 1021 | const DominatorTree &DT, |
Chris Lattner | a09fbf0 | 2009-10-10 23:50:30 +0000 | [diff] [blame] | 1022 | AliasAnalysis *AA) { |
Chris Lattner | d2191e5 | 2009-12-21 23:15:48 +0000 | [diff] [blame] | 1023 | // Check for the fully redundant, dominating load case. In this case, we can |
| 1024 | // just use the dominating value directly. |
| 1025 | if (ValuesPerBlock.size() == 1 && |
| 1026 | DT.properlyDominates(ValuesPerBlock[0].BB, LI->getParent())) |
| 1027 | return ValuesPerBlock[0].MaterializeAdjustedValue(LI->getType(), TD); |
| 1028 | |
| 1029 | // Otherwise, we have to construct SSA form. |
Chris Lattner | a09fbf0 | 2009-10-10 23:50:30 +0000 | [diff] [blame] | 1030 | SmallVector<PHINode*, 8> NewPHIs; |
| 1031 | SSAUpdater SSAUpdate(&NewPHIs); |
Duncan Sands | fc6e29d | 2010-09-02 08:14:03 +0000 | [diff] [blame] | 1032 | SSAUpdate.Initialize(LI->getType(), LI->getName()); |
Chris Lattner | a09fbf0 | 2009-10-10 23:50:30 +0000 | [diff] [blame] | 1033 | |
| 1034 | const Type *LoadTy = LI->getType(); |
| 1035 | |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1036 | for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i) { |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 1037 | const AvailableValueInBlock &AV = ValuesPerBlock[i]; |
| 1038 | BasicBlock *BB = AV.BB; |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | a09fbf0 | 2009-10-10 23:50:30 +0000 | [diff] [blame] | 1040 | if (SSAUpdate.HasValueForBlock(BB)) |
| 1041 | continue; |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 1042 | |
Chris Lattner | 5362c54 | 2009-12-21 23:04:33 +0000 | [diff] [blame] | 1043 | SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(LoadTy, TD)); |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1044 | } |
Chris Lattner | a09fbf0 | 2009-10-10 23:50:30 +0000 | [diff] [blame] | 1045 | |
| 1046 | // Perform PHI construction. |
| 1047 | Value *V = SSAUpdate.GetValueInMiddleOfBlock(LI->getParent()); |
| 1048 | |
| 1049 | // If new PHI nodes were created, notify alias analysis. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1050 | if (V->getType()->isPointerTy()) |
Chris Lattner | a09fbf0 | 2009-10-10 23:50:30 +0000 | [diff] [blame] | 1051 | for (unsigned i = 0, e = NewPHIs.size(); i != e; ++i) |
| 1052 | AA->copyValue(LI, NewPHIs[i]); |
Owen Anderson | 392249f | 2011-01-03 23:51:43 +0000 | [diff] [blame] | 1053 | |
| 1054 | // Now that we've copied information to the new PHIs, scan through |
| 1055 | // them again and inform alias analysis that we've added potentially |
| 1056 | // escaping uses to any values that are operands to these PHIs. |
| 1057 | for (unsigned i = 0, e = NewPHIs.size(); i != e; ++i) { |
| 1058 | PHINode *P = NewPHIs[i]; |
| 1059 | for (unsigned ii = 0, ee = P->getNumIncomingValues(); ii != ee; ++ii) |
| 1060 | AA->addEscapingUse(P->getOperandUse(2*ii)); |
| 1061 | } |
Chris Lattner | a09fbf0 | 2009-10-10 23:50:30 +0000 | [diff] [blame] | 1062 | |
| 1063 | return V; |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Gabor Greif | ea3eec9 | 2010-04-09 10:57:00 +0000 | [diff] [blame] | 1066 | static bool isLifetimeStart(const Instruction *Inst) { |
| 1067 | if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst)) |
Owen Anderson | 9ff5a23 | 2009-12-02 07:35:19 +0000 | [diff] [blame] | 1068 | return II->getIntrinsicID() == Intrinsic::lifetime_start; |
Chris Lattner | 720e790 | 2009-12-02 06:44:58 +0000 | [diff] [blame] | 1069 | return false; |
| 1070 | } |
| 1071 | |
Owen Anderson | 62bc33c | 2007-08-16 22:02:55 +0000 | [diff] [blame] | 1072 | /// processNonLocalLoad - Attempt to eliminate a load whose dependencies are |
| 1073 | /// non-local by performing PHI construction. |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1074 | bool GVN::processNonLocalLoad(LoadInst *LI, |
Chris Lattner | 8e1e95c | 2008-03-21 22:01:16 +0000 | [diff] [blame] | 1075 | SmallVectorImpl<Instruction*> &toErase) { |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1076 | // Find the non-local dependencies of the load. |
Chris Lattner | 0ee443d | 2009-12-22 04:25:02 +0000 | [diff] [blame] | 1077 | SmallVector<NonLocalDepResult, 64> Deps; |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 1078 | AliasAnalysis::Location Loc = VN.getAliasAnalysis()->getLocation(LI); |
| 1079 | MD->getNonLocalPointerDependency(Loc, true, LI->getParent(), Deps); |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1080 | //DEBUG(dbgs() << "INVESTIGATING NONLOCAL LOAD: " |
Dan Gohman | 2a29899 | 2009-07-31 20:24:18 +0000 | [diff] [blame] | 1081 | // << Deps.size() << *LI << '\n'); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1082 | |
Owen Anderson | 516eb1c | 2008-08-26 22:07:42 +0000 | [diff] [blame] | 1083 | // If we had to process more than one hundred blocks to find the |
| 1084 | // dependencies, this load isn't worth worrying about. Optimizing |
| 1085 | // it will be too expensive. |
Chris Lattner | 91bcf64 | 2008-12-09 19:25:07 +0000 | [diff] [blame] | 1086 | if (Deps.size() > 100) |
Owen Anderson | 516eb1c | 2008-08-26 22:07:42 +0000 | [diff] [blame] | 1087 | return false; |
Chris Lattner | 5f4f84b | 2008-12-18 00:51:32 +0000 | [diff] [blame] | 1088 | |
| 1089 | // If we had a phi translation failure, we'll have a single entry which is a |
| 1090 | // clobber in the current block. Reject this early. |
Chris Lattner | e18b971 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 1091 | if (Deps.size() == 1 && Deps[0].getResult().isClobber()) { |
Torok Edwin | 4306b1a | 2009-06-17 18:48:18 +0000 | [diff] [blame] | 1092 | DEBUG( |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1093 | dbgs() << "GVN: non-local load "; |
| 1094 | WriteAsOperand(dbgs(), LI); |
| 1095 | dbgs() << " is clobbered by " << *Deps[0].getResult().getInst() << '\n'; |
Torok Edwin | 4306b1a | 2009-06-17 18:48:18 +0000 | [diff] [blame] | 1096 | ); |
Chris Lattner | 5f4f84b | 2008-12-18 00:51:32 +0000 | [diff] [blame] | 1097 | return false; |
Torok Edwin | 4306b1a | 2009-06-17 18:48:18 +0000 | [diff] [blame] | 1098 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1100 | // Filter out useless results (non-locals, etc). Keep track of the blocks |
| 1101 | // where we have a value available in repl, also keep track of whether we see |
| 1102 | // dependencies that produce an unknown value for the load (such as a call |
| 1103 | // that could potentially clobber the load). |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 1104 | SmallVector<AvailableValueInBlock, 16> ValuesPerBlock; |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1105 | SmallVector<BasicBlock*, 16> UnavailableBlocks; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1106 | |
Chris Lattner | 91bcf64 | 2008-12-09 19:25:07 +0000 | [diff] [blame] | 1107 | for (unsigned i = 0, e = Deps.size(); i != e; ++i) { |
Chris Lattner | e18b971 | 2009-12-09 07:08:01 +0000 | [diff] [blame] | 1108 | BasicBlock *DepBB = Deps[i].getBB(); |
| 1109 | MemDepResult DepInfo = Deps[i].getResult(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1110 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1111 | if (DepInfo.isClobber()) { |
Chris Lattner | af064ae | 2009-12-09 18:21:46 +0000 | [diff] [blame] | 1112 | // The address being loaded in this non-local block may not be the same as |
| 1113 | // the pointer operand of the load if PHI translation occurs. Make sure |
| 1114 | // to consider the right address. |
| 1115 | Value *Address = Deps[i].getAddress(); |
| 1116 | |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 1117 | // If the dependence is to a store that writes to a superset of the bits |
| 1118 | // read by the load, we can extract the bits we need for the load from the |
| 1119 | // stored value. |
| 1120 | if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInfo.getInst())) { |
Chris Lattner | af064ae | 2009-12-09 18:21:46 +0000 | [diff] [blame] | 1121 | if (TD && Address) { |
| 1122 | int Offset = AnalyzeLoadFromClobberingStore(LI->getType(), Address, |
Chris Lattner | 4ca70fe | 2009-12-09 07:37:07 +0000 | [diff] [blame] | 1123 | DepSI, *TD); |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 1124 | if (Offset != -1) { |
| 1125 | ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB, |
Dan Gohman | 3355c4e | 2010-11-10 19:03:33 +0000 | [diff] [blame] | 1126 | DepSI->getValueOperand(), |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 1127 | Offset)); |
| 1128 | continue; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1133 | // If the clobbering value is a memset/memcpy/memmove, see if we can |
| 1134 | // forward a value on from it. |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 1135 | if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInfo.getInst())) { |
Chris Lattner | af064ae | 2009-12-09 18:21:46 +0000 | [diff] [blame] | 1136 | if (TD && Address) { |
| 1137 | int Offset = AnalyzeLoadFromClobberingMemInst(LI->getType(), Address, |
Chris Lattner | 4ca70fe | 2009-12-09 07:37:07 +0000 | [diff] [blame] | 1138 | DepMI, *TD); |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 1139 | if (Offset != -1) { |
| 1140 | ValuesPerBlock.push_back(AvailableValueInBlock::getMI(DepBB, DepMI, |
| 1141 | Offset)); |
| 1142 | continue; |
| 1143 | } |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1144 | } |
| 1145 | } |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 1146 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1147 | UnavailableBlocks.push_back(DepBB); |
| 1148 | continue; |
| 1149 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1151 | Instruction *DepInst = DepInfo.getInst(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1152 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1153 | // Loading the allocation -> undef. |
Chris Lattner | 720e790 | 2009-12-02 06:44:58 +0000 | [diff] [blame] | 1154 | if (isa<AllocaInst>(DepInst) || isMalloc(DepInst) || |
Owen Anderson | 9ff5a23 | 2009-12-02 07:35:19 +0000 | [diff] [blame] | 1155 | // Loading immediately after lifetime begin -> undef. |
| 1156 | isLifetimeStart(DepInst)) { |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 1157 | ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB, |
| 1158 | UndefValue::get(LI->getType()))); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 1159 | continue; |
| 1160 | } |
Owen Anderson | b62f792 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 1161 | |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 1162 | if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) { |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1163 | // Reject loads and stores that are to the same address but are of |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1164 | // different types if we have to. |
Dan Gohman | 3355c4e | 2010-11-10 19:03:33 +0000 | [diff] [blame] | 1165 | if (S->getValueOperand()->getType() != LI->getType()) { |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1166 | // If the stored value is larger or equal to the loaded value, we can |
| 1167 | // reuse it. |
Dan Gohman | 3355c4e | 2010-11-10 19:03:33 +0000 | [diff] [blame] | 1168 | if (TD == 0 || !CanCoerceMustAliasedValueToLoad(S->getValueOperand(), |
Chris Lattner | 8b2bc3d | 2009-09-21 17:24:04 +0000 | [diff] [blame] | 1169 | LI->getType(), *TD)) { |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1170 | UnavailableBlocks.push_back(DepBB); |
| 1171 | continue; |
| 1172 | } |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1173 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1174 | |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 1175 | ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB, |
Dan Gohman | 3355c4e | 2010-11-10 19:03:33 +0000 | [diff] [blame] | 1176 | S->getValueOperand())); |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 1177 | continue; |
| 1178 | } |
| 1179 | |
| 1180 | if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) { |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1181 | // If the types mismatch and we can't handle it, reject reuse of the load. |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1182 | if (LD->getType() != LI->getType()) { |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1183 | // If the stored value is larger or equal to the loaded value, we can |
| 1184 | // reuse it. |
Chris Lattner | 8b2bc3d | 2009-09-21 17:24:04 +0000 | [diff] [blame] | 1185 | if (TD == 0 || !CanCoerceMustAliasedValueToLoad(LD, LI->getType(),*TD)){ |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1186 | UnavailableBlocks.push_back(DepBB); |
| 1187 | continue; |
| 1188 | } |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1189 | } |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 1190 | ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB, LD)); |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1191 | continue; |
Owen Anderson | 0cd3203 | 2007-07-25 19:57:03 +0000 | [diff] [blame] | 1192 | } |
Chris Lattner | 4fbd14e | 2009-09-21 06:48:08 +0000 | [diff] [blame] | 1193 | |
| 1194 | UnavailableBlocks.push_back(DepBB); |
| 1195 | continue; |
Chris Lattner | 88365bb | 2008-03-21 21:14:38 +0000 | [diff] [blame] | 1196 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1198 | // If we have no predecessors that produce a known value for this load, exit |
| 1199 | // early. |
| 1200 | if (ValuesPerBlock.empty()) return false; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1201 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1202 | // If all of the instructions we depend on produce a known value for this |
| 1203 | // load, then it is fully redundant and we can use PHI insertion to compute |
| 1204 | // its value. Insert PHIs and remove the fully redundant value now. |
| 1205 | if (UnavailableBlocks.empty()) { |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1206 | DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *LI << '\n'); |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1207 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1208 | // Perform PHI construction. |
Chris Lattner | d2191e5 | 2009-12-21 23:15:48 +0000 | [diff] [blame] | 1209 | Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, TD, *DT, |
Chris Lattner | a09fbf0 | 2009-10-10 23:50:30 +0000 | [diff] [blame] | 1210 | VN.getAliasAnalysis()); |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1211 | LI->replaceAllUsesWith(V); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1212 | |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1213 | if (isa<PHINode>(V)) |
| 1214 | V->takeName(LI); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1215 | if (V->getType()->isPointerTy()) |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1216 | MD->invalidateCachedPointerInfo(V); |
Bob Wilson | 74175c2 | 2010-02-22 21:39:41 +0000 | [diff] [blame] | 1217 | VN.erase(LI); |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1218 | toErase.push_back(LI); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1219 | ++NumGVNLoad; |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1220 | return true; |
| 1221 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1222 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1223 | if (!EnablePRE || !EnableLoadPRE) |
| 1224 | return false; |
| 1225 | |
| 1226 | // Okay, we have *some* definitions of the value. This means that the value |
| 1227 | // is available in some of our (transitive) predecessors. Lets think about |
| 1228 | // doing PRE of this load. This will involve inserting a new load into the |
| 1229 | // predecessor when it's not available. We could do this in general, but |
| 1230 | // prefer to not increase code size. As such, we only do this when we know |
| 1231 | // that we only have to insert *one* load (which means we're basically moving |
| 1232 | // the load, not inserting a new one). |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1233 | |
Owen Anderson | 88554df | 2009-05-31 09:03:40 +0000 | [diff] [blame] | 1234 | SmallPtrSet<BasicBlock *, 4> Blockers; |
| 1235 | for (unsigned i = 0, e = UnavailableBlocks.size(); i != e; ++i) |
| 1236 | Blockers.insert(UnavailableBlocks[i]); |
| 1237 | |
| 1238 | // Lets find first basic block with more than one predecessor. Walk backwards |
| 1239 | // through predecessors if needed. |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1240 | BasicBlock *LoadBB = LI->getParent(); |
Owen Anderson | 88554df | 2009-05-31 09:03:40 +0000 | [diff] [blame] | 1241 | BasicBlock *TmpBB = LoadBB; |
| 1242 | |
| 1243 | bool isSinglePred = false; |
Dale Johannesen | 42c3f55 | 2009-06-17 20:48:23 +0000 | [diff] [blame] | 1244 | bool allSingleSucc = true; |
Owen Anderson | 88554df | 2009-05-31 09:03:40 +0000 | [diff] [blame] | 1245 | while (TmpBB->getSinglePredecessor()) { |
| 1246 | isSinglePred = true; |
| 1247 | TmpBB = TmpBB->getSinglePredecessor(); |
Owen Anderson | 88554df | 2009-05-31 09:03:40 +0000 | [diff] [blame] | 1248 | if (TmpBB == LoadBB) // Infinite (unreachable) loop. |
| 1249 | return false; |
| 1250 | if (Blockers.count(TmpBB)) |
| 1251 | return false; |
Owen Anderson | b0ba0f4 | 2010-09-25 05:26:18 +0000 | [diff] [blame] | 1252 | |
| 1253 | // If any of these blocks has more than one successor (i.e. if the edge we |
| 1254 | // just traversed was critical), then there are other paths through this |
| 1255 | // block along which the load may not be anticipated. Hoisting the load |
| 1256 | // above this block would be adding the load to execution paths along |
| 1257 | // which it was not previously executed. |
Dale Johannesen | 42c3f55 | 2009-06-17 20:48:23 +0000 | [diff] [blame] | 1258 | if (TmpBB->getTerminator()->getNumSuccessors() != 1) |
Owen Anderson | b0ba0f4 | 2010-09-25 05:26:18 +0000 | [diff] [blame] | 1259 | return false; |
Owen Anderson | 88554df | 2009-05-31 09:03:40 +0000 | [diff] [blame] | 1260 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1261 | |
Owen Anderson | 88554df | 2009-05-31 09:03:40 +0000 | [diff] [blame] | 1262 | assert(TmpBB); |
| 1263 | LoadBB = TmpBB; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1264 | |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 1265 | // FIXME: It is extremely unclear what this loop is doing, other than |
| 1266 | // artificially restricting loadpre. |
Owen Anderson | 88554df | 2009-05-31 09:03:40 +0000 | [diff] [blame] | 1267 | if (isSinglePred) { |
| 1268 | bool isHot = false; |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 1269 | for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i) { |
| 1270 | const AvailableValueInBlock &AV = ValuesPerBlock[i]; |
| 1271 | if (AV.isSimpleValue()) |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1272 | // "Hot" Instruction is in some loop (because it dominates its dep. |
| 1273 | // instruction). |
Chris Lattner | cb9cbc4 | 2009-12-06 04:54:31 +0000 | [diff] [blame] | 1274 | if (Instruction *I = dyn_cast<Instruction>(AV.getSimpleValue())) |
| 1275 | if (DT->dominates(LI, I)) { |
| 1276 | isHot = true; |
| 1277 | break; |
| 1278 | } |
| 1279 | } |
Owen Anderson | 88554df | 2009-05-31 09:03:40 +0000 | [diff] [blame] | 1280 | |
| 1281 | // We are interested only in "hot" instructions. We don't want to do any |
| 1282 | // mis-optimizations here. |
| 1283 | if (!isHot) |
| 1284 | return false; |
| 1285 | } |
| 1286 | |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1287 | // Check to see how many predecessors have the loaded value fully |
| 1288 | // available. |
| 1289 | DenseMap<BasicBlock*, Value*> PredLoads; |
Chris Lattner | 72bc70d | 2008-12-05 07:49:08 +0000 | [diff] [blame] | 1290 | DenseMap<BasicBlock*, char> FullyAvailableBlocks; |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1291 | for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i) |
Chris Lattner | 8791351 | 2009-09-21 06:30:24 +0000 | [diff] [blame] | 1292 | FullyAvailableBlocks[ValuesPerBlock[i].BB] = true; |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1293 | for (unsigned i = 0, e = UnavailableBlocks.size(); i != e; ++i) |
| 1294 | FullyAvailableBlocks[UnavailableBlocks[i]] = false; |
| 1295 | |
Bob Wilson | 34414a6 | 2010-05-04 20:03:21 +0000 | [diff] [blame] | 1296 | SmallVector<std::pair<TerminatorInst*, unsigned>, 4> NeedToSplit; |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1297 | for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB); |
| 1298 | PI != E; ++PI) { |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1299 | BasicBlock *Pred = *PI; |
| 1300 | if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks)) { |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1301 | continue; |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1302 | } |
| 1303 | PredLoads[Pred] = 0; |
Bob Wilson | 484d4a3 | 2010-02-16 19:51:59 +0000 | [diff] [blame] | 1304 | |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1305 | if (Pred->getTerminator()->getNumSuccessors() != 1) { |
Bob Wilson | 484d4a3 | 2010-02-16 19:51:59 +0000 | [diff] [blame] | 1306 | if (isa<IndirectBrInst>(Pred->getTerminator())) { |
| 1307 | DEBUG(dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '" |
| 1308 | << Pred->getName() << "': " << *LI << '\n'); |
| 1309 | return false; |
| 1310 | } |
Bob Wilson | ae23daf | 2010-02-16 21:06:42 +0000 | [diff] [blame] | 1311 | unsigned SuccNum = GetSuccessorNumber(Pred, LoadBB); |
Bob Wilson | 34414a6 | 2010-05-04 20:03:21 +0000 | [diff] [blame] | 1312 | NeedToSplit.push_back(std::make_pair(Pred->getTerminator(), SuccNum)); |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1313 | } |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1314 | } |
Bob Wilson | 34414a6 | 2010-05-04 20:03:21 +0000 | [diff] [blame] | 1315 | if (!NeedToSplit.empty()) { |
Bob Wilson | bc78653 | 2010-05-05 20:44:15 +0000 | [diff] [blame] | 1316 | toSplit.append(NeedToSplit.begin(), NeedToSplit.end()); |
Bob Wilson | 7070497 | 2010-03-01 23:37:32 +0000 | [diff] [blame] | 1317 | return false; |
Bob Wilson | 34414a6 | 2010-05-04 20:03:21 +0000 | [diff] [blame] | 1318 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1319 | |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1320 | // Decide whether PRE is profitable for this load. |
| 1321 | unsigned NumUnavailablePreds = PredLoads.size(); |
| 1322 | assert(NumUnavailablePreds != 0 && |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1323 | "Fully available value should be eliminated above!"); |
Owen Anderson | 7267e14 | 2010-10-01 20:02:55 +0000 | [diff] [blame] | 1324 | |
| 1325 | // If this load is unavailable in multiple predecessors, reject it. |
| 1326 | // FIXME: If we could restructure the CFG, we could make a common pred with |
| 1327 | // all the preds that don't have an available LI and insert a new load into |
| 1328 | // that one block. |
| 1329 | if (NumUnavailablePreds != 1) |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1330 | return false; |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1331 | |
| 1332 | // Check if the load can safely be moved to all the unavailable predecessors. |
| 1333 | bool CanDoPRE = true; |
Chris Lattner | dd69605 | 2009-11-28 15:39:14 +0000 | [diff] [blame] | 1334 | SmallVector<Instruction*, 8> NewInsts; |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1335 | for (DenseMap<BasicBlock*, Value*>::iterator I = PredLoads.begin(), |
| 1336 | E = PredLoads.end(); I != E; ++I) { |
| 1337 | BasicBlock *UnavailablePred = I->first; |
| 1338 | |
| 1339 | // Do PHI translation to get its value in the predecessor if necessary. The |
| 1340 | // returned pointer (if non-null) is guaranteed to dominate UnavailablePred. |
| 1341 | |
| 1342 | // If all preds have a single successor, then we know it is safe to insert |
| 1343 | // the load on the pred (?!?), so we can insert code to materialize the |
| 1344 | // pointer if it is not available. |
Dan Gohman | 3355c4e | 2010-11-10 19:03:33 +0000 | [diff] [blame] | 1345 | PHITransAddr Address(LI->getPointerOperand(), TD); |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1346 | Value *LoadPtr = 0; |
| 1347 | if (allSingleSucc) { |
| 1348 | LoadPtr = Address.PHITranslateWithInsertion(LoadBB, UnavailablePred, |
| 1349 | *DT, NewInsts); |
| 1350 | } else { |
Daniel Dunbar | 6d8f2ca | 2010-02-24 08:48:04 +0000 | [diff] [blame] | 1351 | Address.PHITranslateValue(LoadBB, UnavailablePred, DT); |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1352 | LoadPtr = Address.getAddr(); |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | // If we couldn't find or insert a computation of this phi translated value, |
| 1356 | // we fail PRE. |
| 1357 | if (LoadPtr == 0) { |
| 1358 | DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: " |
Dan Gohman | 3355c4e | 2010-11-10 19:03:33 +0000 | [diff] [blame] | 1359 | << *LI->getPointerOperand() << "\n"); |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1360 | CanDoPRE = false; |
| 1361 | break; |
| 1362 | } |
| 1363 | |
| 1364 | // Make sure it is valid to move this load here. We have to watch out for: |
| 1365 | // @1 = getelementptr (i8* p, ... |
| 1366 | // test p and branch if == 0 |
| 1367 | // load @1 |
Owen Anderson | b1602ab | 2011-01-04 19:29:46 +0000 | [diff] [blame] | 1368 | // It is valid to have the getelementptr before the test, even if p can |
| 1369 | // be 0, as getelementptr only does address arithmetic. |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1370 | // If we are not pushing the value through any multiple-successor blocks |
| 1371 | // we do not have this case. Otherwise, check that the load is safe to |
| 1372 | // put anywhere; this can be improved, but should be conservatively safe. |
| 1373 | if (!allSingleSucc && |
| 1374 | // FIXME: REEVALUTE THIS. |
| 1375 | !isSafeToLoadUnconditionally(LoadPtr, |
| 1376 | UnavailablePred->getTerminator(), |
| 1377 | LI->getAlignment(), TD)) { |
| 1378 | CanDoPRE = false; |
| 1379 | break; |
| 1380 | } |
| 1381 | |
| 1382 | I->second = LoadPtr; |
Chris Lattner | 05e15f8 | 2009-12-09 01:59:31 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1385 | if (!CanDoPRE) { |
Chris Lattner | 3077ca9 | 2011-01-11 08:19:16 +0000 | [diff] [blame^] | 1386 | while (!NewInsts.empty()) { |
| 1387 | Instruction *I = NewInsts.pop_back_val(); |
| 1388 | if (MD) MD->removeInstruction(I); |
| 1389 | I->eraseFromParent(); |
| 1390 | } |
Dale Johannesen | 42c3f55 | 2009-06-17 20:48:23 +0000 | [diff] [blame] | 1391 | return false; |
Chris Lattner | 0c264b1 | 2009-11-28 16:08:18 +0000 | [diff] [blame] | 1392 | } |
Dale Johannesen | 42c3f55 | 2009-06-17 20:48:23 +0000 | [diff] [blame] | 1393 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1394 | // Okay, we can eliminate this load by inserting a reload in the predecessor |
| 1395 | // and using PHI construction to get the value in the other predecessors, do |
| 1396 | // it. |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1397 | DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *LI << '\n'); |
Chris Lattner | 0c264b1 | 2009-11-28 16:08:18 +0000 | [diff] [blame] | 1398 | DEBUG(if (!NewInsts.empty()) |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1399 | dbgs() << "INSERTED " << NewInsts.size() << " INSTS: " |
Chris Lattner | 0c264b1 | 2009-11-28 16:08:18 +0000 | [diff] [blame] | 1400 | << *NewInsts.back() << '\n'); |
| 1401 | |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1402 | // Assign value numbers to the new instructions. |
| 1403 | for (unsigned i = 0, e = NewInsts.size(); i != e; ++i) { |
| 1404 | // FIXME: We really _ought_ to insert these value numbers into their |
| 1405 | // parent's availability map. However, in doing so, we risk getting into |
| 1406 | // ordering issues. If a block hasn't been processed yet, we would be |
| 1407 | // marking a value as AVAIL-IN, which isn't what we intend. |
| 1408 | VN.lookup_or_add(NewInsts[i]); |
| 1409 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1410 | |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1411 | for (DenseMap<BasicBlock*, Value*>::iterator I = PredLoads.begin(), |
| 1412 | E = PredLoads.end(); I != E; ++I) { |
| 1413 | BasicBlock *UnavailablePred = I->first; |
| 1414 | Value *LoadPtr = I->second; |
| 1415 | |
Dan Gohman | f4177aa | 2010-12-15 23:53:55 +0000 | [diff] [blame] | 1416 | Instruction *NewLoad = new LoadInst(LoadPtr, LI->getName()+".pre", false, |
| 1417 | LI->getAlignment(), |
| 1418 | UnavailablePred->getTerminator()); |
| 1419 | |
| 1420 | // Transfer the old load's TBAA tag to the new load. |
| 1421 | if (MDNode *Tag = LI->getMetadata(LLVMContext::MD_tbaa)) |
| 1422 | NewLoad->setMetadata(LLVMContext::MD_tbaa, Tag); |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1423 | |
| 1424 | // Add the newly created load. |
| 1425 | ValuesPerBlock.push_back(AvailableValueInBlock::get(UnavailablePred, |
| 1426 | NewLoad)); |
Bob Wilson | 188f428 | 2010-02-23 05:55:00 +0000 | [diff] [blame] | 1427 | MD->invalidateCachedPointerInfo(LoadPtr); |
| 1428 | DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n'); |
Bob Wilson | 6cad417 | 2010-02-01 21:17:14 +0000 | [diff] [blame] | 1429 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1430 | |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1431 | // Perform PHI construction. |
Chris Lattner | d2191e5 | 2009-12-21 23:15:48 +0000 | [diff] [blame] | 1432 | Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, TD, *DT, |
Chris Lattner | a09fbf0 | 2009-10-10 23:50:30 +0000 | [diff] [blame] | 1433 | VN.getAliasAnalysis()); |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1434 | LI->replaceAllUsesWith(V); |
| 1435 | if (isa<PHINode>(V)) |
| 1436 | V->takeName(LI); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1437 | if (V->getType()->isPointerTy()) |
Chris Lattner | 771a542 | 2009-09-20 20:09:34 +0000 | [diff] [blame] | 1438 | MD->invalidateCachedPointerInfo(V); |
Bob Wilson | 74175c2 | 2010-02-22 21:39:41 +0000 | [diff] [blame] | 1439 | VN.erase(LI); |
Chris Lattner | c89c6a9 | 2008-12-02 08:16:11 +0000 | [diff] [blame] | 1440 | toErase.push_back(LI); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1441 | ++NumPRELoad; |
Owen Anderson | 0cd3203 | 2007-07-25 19:57:03 +0000 | [diff] [blame] | 1442 | return true; |
| 1443 | } |
| 1444 | |
Owen Anderson | 62bc33c | 2007-08-16 22:02:55 +0000 | [diff] [blame] | 1445 | /// processLoad - Attempt to eliminate a load, first by eliminating it |
| 1446 | /// locally, and then attempting non-local elimination if that fails. |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1447 | bool GVN::processLoad(LoadInst *L, SmallVectorImpl<Instruction*> &toErase) { |
Dan Gohman | 4ec01b2 | 2009-11-14 02:27:51 +0000 | [diff] [blame] | 1448 | if (!MD) |
| 1449 | return false; |
| 1450 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1451 | if (L->isVolatile()) |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 1452 | return false; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1453 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 1454 | // ... to a pointer that has been loaded from before... |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1455 | MemDepResult Dep = MD->getDependency(L); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1457 | // If the value isn't available, don't do anything! |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1458 | if (Dep.isClobber()) { |
Chris Lattner | eed919b | 2009-09-21 05:57:11 +0000 | [diff] [blame] | 1459 | // Check to see if we have something like this: |
Chris Lattner | bb6495c | 2009-09-20 19:03:47 +0000 | [diff] [blame] | 1460 | // store i32 123, i32* %P |
| 1461 | // %A = bitcast i32* %P to i8* |
| 1462 | // %B = gep i8* %A, i32 1 |
| 1463 | // %C = load i8* %B |
| 1464 | // |
| 1465 | // We could do that by recognizing if the clobber instructions are obviously |
| 1466 | // a common base + constant offset, and if the previous store (or memset) |
| 1467 | // completely covers this load. This sort of thing can happen in bitfield |
| 1468 | // access code. |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1469 | Value *AvailVal = 0; |
Chris Lattner | eed919b | 2009-09-21 05:57:11 +0000 | [diff] [blame] | 1470 | if (StoreInst *DepSI = dyn_cast<StoreInst>(Dep.getInst())) |
Duncan Sands | 88c3df7 | 2010-11-12 21:10:24 +0000 | [diff] [blame] | 1471 | if (TD) { |
Chris Lattner | 4ca70fe | 2009-12-09 07:37:07 +0000 | [diff] [blame] | 1472 | int Offset = AnalyzeLoadFromClobberingStore(L->getType(), |
| 1473 | L->getPointerOperand(), |
| 1474 | DepSI, *TD); |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1475 | if (Offset != -1) |
Dan Gohman | 3355c4e | 2010-11-10 19:03:33 +0000 | [diff] [blame] | 1476 | AvailVal = GetStoreValueForLoad(DepSI->getValueOperand(), Offset, |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1477 | L->getType(), L, *TD); |
Chris Lattner | 1ce0829 | 2009-09-21 06:22:46 +0000 | [diff] [blame] | 1478 | } |
Chris Lattner | eed919b | 2009-09-21 05:57:11 +0000 | [diff] [blame] | 1479 | |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1480 | // If the clobbering value is a memset/memcpy/memmove, see if we can forward |
| 1481 | // a value on from it. |
| 1482 | if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(Dep.getInst())) { |
Duncan Sands | 88c3df7 | 2010-11-12 21:10:24 +0000 | [diff] [blame] | 1483 | if (TD) { |
Chris Lattner | 4ca70fe | 2009-12-09 07:37:07 +0000 | [diff] [blame] | 1484 | int Offset = AnalyzeLoadFromClobberingMemInst(L->getType(), |
| 1485 | L->getPointerOperand(), |
| 1486 | DepMI, *TD); |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1487 | if (Offset != -1) |
| 1488 | AvailVal = GetMemInstValueForLoad(DepMI, Offset, L->getType(), L,*TD); |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | if (AvailVal) { |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1493 | DEBUG(dbgs() << "GVN COERCED INST:\n" << *Dep.getInst() << '\n' |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1494 | << *AvailVal << '\n' << *L << "\n\n\n"); |
| 1495 | |
| 1496 | // Replace the load! |
| 1497 | L->replaceAllUsesWith(AvailVal); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1498 | if (AvailVal->getType()->isPointerTy()) |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1499 | MD->invalidateCachedPointerInfo(AvailVal); |
Bob Wilson | 74175c2 | 2010-02-22 21:39:41 +0000 | [diff] [blame] | 1500 | VN.erase(L); |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1501 | toErase.push_back(L); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1502 | ++NumGVNLoad; |
Chris Lattner | faf815b | 2009-12-06 01:57:02 +0000 | [diff] [blame] | 1503 | return true; |
| 1504 | } |
| 1505 | |
Torok Edwin | 3f3c6d4 | 2009-05-29 09:46:03 +0000 | [diff] [blame] | 1506 | DEBUG( |
| 1507 | // fast print dep, using operator<< on instruction would be too slow |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1508 | dbgs() << "GVN: load "; |
| 1509 | WriteAsOperand(dbgs(), L); |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1510 | Instruction *I = Dep.getInst(); |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1511 | dbgs() << " is clobbered by " << *I << '\n'; |
Torok Edwin | 3f3c6d4 | 2009-05-29 09:46:03 +0000 | [diff] [blame] | 1512 | ); |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1513 | return false; |
Torok Edwin | 3f3c6d4 | 2009-05-29 09:46:03 +0000 | [diff] [blame] | 1514 | } |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1515 | |
| 1516 | // If it is defined in another block, try harder. |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1517 | if (Dep.isNonLocal()) |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1518 | return processNonLocalLoad(L, toErase); |
Eli Friedman | b6c36e4 | 2008-02-12 12:08:14 +0000 | [diff] [blame] | 1519 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1520 | Instruction *DepInst = Dep.getInst(); |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1521 | if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) { |
Dan Gohman | 3355c4e | 2010-11-10 19:03:33 +0000 | [diff] [blame] | 1522 | Value *StoredVal = DepSI->getValueOperand(); |
Chris Lattner | bb6495c | 2009-09-20 19:03:47 +0000 | [diff] [blame] | 1523 | |
| 1524 | // The store and load are to a must-aliased pointer, but they may not |
| 1525 | // actually have the same type. See if we know how to reuse the stored |
| 1526 | // value (depending on its type). |
Chris Lattner | a52fce4 | 2009-10-21 04:11:19 +0000 | [diff] [blame] | 1527 | if (StoredVal->getType() != L->getType()) { |
Duncan Sands | 88c3df7 | 2010-11-12 21:10:24 +0000 | [diff] [blame] | 1528 | if (TD) { |
Chris Lattner | a52fce4 | 2009-10-21 04:11:19 +0000 | [diff] [blame] | 1529 | StoredVal = CoerceAvailableValueToLoadType(StoredVal, L->getType(), |
| 1530 | L, *TD); |
| 1531 | if (StoredVal == 0) |
| 1532 | return false; |
| 1533 | |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1534 | DEBUG(dbgs() << "GVN COERCED STORE:\n" << *DepSI << '\n' << *StoredVal |
Chris Lattner | a52fce4 | 2009-10-21 04:11:19 +0000 | [diff] [blame] | 1535 | << '\n' << *L << "\n\n\n"); |
| 1536 | } |
| 1537 | else |
Chris Lattner | bb6495c | 2009-09-20 19:03:47 +0000 | [diff] [blame] | 1538 | return false; |
Chris Lattner | bb6495c | 2009-09-20 19:03:47 +0000 | [diff] [blame] | 1539 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1540 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1541 | // Remove it! |
Chris Lattner | bb6495c | 2009-09-20 19:03:47 +0000 | [diff] [blame] | 1542 | L->replaceAllUsesWith(StoredVal); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1543 | if (StoredVal->getType()->isPointerTy()) |
Chris Lattner | bb6495c | 2009-09-20 19:03:47 +0000 | [diff] [blame] | 1544 | MD->invalidateCachedPointerInfo(StoredVal); |
Bob Wilson | 74175c2 | 2010-02-22 21:39:41 +0000 | [diff] [blame] | 1545 | VN.erase(L); |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1546 | toErase.push_back(L); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1547 | ++NumGVNLoad; |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1548 | return true; |
| 1549 | } |
| 1550 | |
| 1551 | if (LoadInst *DepLI = dyn_cast<LoadInst>(DepInst)) { |
Chris Lattner | bb6495c | 2009-09-20 19:03:47 +0000 | [diff] [blame] | 1552 | Value *AvailableVal = DepLI; |
| 1553 | |
| 1554 | // The loads are of a must-aliased pointer, but they may not actually have |
| 1555 | // the same type. See if we know how to reuse the previously loaded value |
| 1556 | // (depending on its type). |
Chris Lattner | a52fce4 | 2009-10-21 04:11:19 +0000 | [diff] [blame] | 1557 | if (DepLI->getType() != L->getType()) { |
Duncan Sands | 88c3df7 | 2010-11-12 21:10:24 +0000 | [diff] [blame] | 1558 | if (TD) { |
Chris Lattner | a52fce4 | 2009-10-21 04:11:19 +0000 | [diff] [blame] | 1559 | AvailableVal = CoerceAvailableValueToLoadType(DepLI, L->getType(), L,*TD); |
| 1560 | if (AvailableVal == 0) |
| 1561 | return false; |
Chris Lattner | bb6495c | 2009-09-20 19:03:47 +0000 | [diff] [blame] | 1562 | |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1563 | DEBUG(dbgs() << "GVN COERCED LOAD:\n" << *DepLI << "\n" << *AvailableVal |
Chris Lattner | a52fce4 | 2009-10-21 04:11:19 +0000 | [diff] [blame] | 1564 | << "\n" << *L << "\n\n\n"); |
| 1565 | } |
| 1566 | else |
| 1567 | return false; |
Chris Lattner | bb6495c | 2009-09-20 19:03:47 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1570 | // Remove it! |
Chris Lattner | bb6495c | 2009-09-20 19:03:47 +0000 | [diff] [blame] | 1571 | L->replaceAllUsesWith(AvailableVal); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1572 | if (DepLI->getType()->isPointerTy()) |
Chris Lattner | bc99be1 | 2008-12-09 22:06:23 +0000 | [diff] [blame] | 1573 | MD->invalidateCachedPointerInfo(DepLI); |
Bob Wilson | 74175c2 | 2010-02-22 21:39:41 +0000 | [diff] [blame] | 1574 | VN.erase(L); |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1575 | toErase.push_back(L); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1576 | ++NumGVNLoad; |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1577 | return true; |
| 1578 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1579 | |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 1580 | // If this load really doesn't depend on anything, then we must be loading an |
| 1581 | // undef value. This can happen when loading for a fresh allocation with no |
| 1582 | // intervening stores, for example. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1583 | if (isa<AllocaInst>(DepInst) || isMalloc(DepInst)) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1584 | L->replaceAllUsesWith(UndefValue::get(L->getType())); |
Bob Wilson | 74175c2 | 2010-02-22 21:39:41 +0000 | [diff] [blame] | 1585 | VN.erase(L); |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 1586 | toErase.push_back(L); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1587 | ++NumGVNLoad; |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1588 | return true; |
Eli Friedman | b6c36e4 | 2008-02-12 12:08:14 +0000 | [diff] [blame] | 1589 | } |
Owen Anderson | b62f792 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 1590 | |
Owen Anderson | 9ff5a23 | 2009-12-02 07:35:19 +0000 | [diff] [blame] | 1591 | // If this load occurs either right after a lifetime begin, |
Owen Anderson | b62f792 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 1592 | // then the loaded value is undefined. |
| 1593 | if (IntrinsicInst* II = dyn_cast<IntrinsicInst>(DepInst)) { |
Owen Anderson | 9ff5a23 | 2009-12-02 07:35:19 +0000 | [diff] [blame] | 1594 | if (II->getIntrinsicID() == Intrinsic::lifetime_start) { |
Owen Anderson | b62f792 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 1595 | L->replaceAllUsesWith(UndefValue::get(L->getType())); |
Bob Wilson | 74175c2 | 2010-02-22 21:39:41 +0000 | [diff] [blame] | 1596 | VN.erase(L); |
Owen Anderson | b62f792 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 1597 | toErase.push_back(L); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1598 | ++NumGVNLoad; |
Owen Anderson | b62f792 | 2009-10-28 07:05:35 +0000 | [diff] [blame] | 1599 | return true; |
| 1600 | } |
| 1601 | } |
Eli Friedman | b6c36e4 | 2008-02-12 12:08:14 +0000 | [diff] [blame] | 1602 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 1603 | return false; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1606 | // findLeader - In order to find a leader for a given value number at a |
Owen Anderson | 68c2639 | 2010-11-19 22:48:40 +0000 | [diff] [blame] | 1607 | // specific basic block, we first obtain the list of all Values for that number, |
| 1608 | // and then scan the list to find one whose block dominates the block in |
| 1609 | // question. This is fast because dominator tree queries consist of only |
| 1610 | // a few comparisons of DFS numbers. |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1611 | Value *GVN::findLeader(BasicBlock *BB, uint32_t num) { |
Owen Anderson | b1602ab | 2011-01-04 19:29:46 +0000 | [diff] [blame] | 1612 | LeaderTableEntry Vals = LeaderTable[num]; |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 1613 | if (!Vals.Val) return 0; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 1614 | |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 1615 | Value *Val = 0; |
| 1616 | if (DT->dominates(Vals.BB, BB)) { |
| 1617 | Val = Vals.Val; |
| 1618 | if (isa<Constant>(Val)) return Val; |
| 1619 | } |
| 1620 | |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1621 | LeaderTableEntry* Next = Vals.Next; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 1622 | while (Next) { |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 1623 | if (DT->dominates(Next->BB, BB)) { |
| 1624 | if (isa<Constant>(Next->Val)) return Next->Val; |
| 1625 | if (!Val) Val = Next->Val; |
| 1626 | } |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 1627 | |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 1628 | Next = Next->Next; |
Owen Anderson | 6fafe84 | 2008-06-20 01:15:47 +0000 | [diff] [blame] | 1629 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1630 | |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 1631 | return Val; |
Owen Anderson | 6fafe84 | 2008-06-20 01:15:47 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Owen Anderson | 255dafc | 2008-12-15 02:03:00 +0000 | [diff] [blame] | 1634 | |
Owen Anderson | 36057c7 | 2007-08-14 18:16:29 +0000 | [diff] [blame] | 1635 | /// processInstruction - When calculating availability, handle an instruction |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 1636 | /// by inserting it into the appropriate sets |
Owen Anderson | af4240a | 2008-06-12 19:25:32 +0000 | [diff] [blame] | 1637 | bool GVN::processInstruction(Instruction *I, |
Chris Lattner | 8e1e95c | 2008-03-21 22:01:16 +0000 | [diff] [blame] | 1638 | SmallVectorImpl<Instruction*> &toErase) { |
Devang Patel | be905e2 | 2010-02-11 00:20:49 +0000 | [diff] [blame] | 1639 | // Ignore dbg info intrinsics. |
| 1640 | if (isa<DbgInfoIntrinsic>(I)) |
| 1641 | return false; |
| 1642 | |
Duncan Sands | 88c3df7 | 2010-11-12 21:10:24 +0000 | [diff] [blame] | 1643 | // If the instruction can be easily simplified then do so now in preference |
| 1644 | // to value numbering it. Value numbering often exposes redundancies, for |
| 1645 | // example if it determines that %y is equal to %x then the instruction |
| 1646 | // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify. |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 1647 | if (Value *V = SimplifyInstruction(I, TD, DT)) { |
Duncan Sands | 88c3df7 | 2010-11-12 21:10:24 +0000 | [diff] [blame] | 1648 | I->replaceAllUsesWith(V); |
| 1649 | if (MD && V->getType()->isPointerTy()) |
| 1650 | MD->invalidateCachedPointerInfo(V); |
| 1651 | VN.erase(I); |
| 1652 | toErase.push_back(I); |
| 1653 | return true; |
| 1654 | } |
| 1655 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1656 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 1657 | bool Changed = processLoad(LI, toErase); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1658 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1659 | if (!Changed) { |
| 1660 | unsigned Num = VN.lookup_or_add(LI); |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1661 | addToLeaderTable(Num, LI, LI->getParent()); |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1662 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1664 | return Changed; |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1665 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1666 | |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 1667 | // For conditions branches, we can perform simple conditional propagation on |
| 1668 | // the condition value itself. |
| 1669 | if (BranchInst *BI = dyn_cast<BranchInst>(I)) { |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 1670 | if (!BI->isConditional() || isa<Constant>(BI->getCondition())) |
| 1671 | return false; |
| 1672 | |
| 1673 | Value *BranchCond = BI->getCondition(); |
| 1674 | uint32_t CondVN = VN.lookup_or_add(BranchCond); |
| 1675 | |
| 1676 | BasicBlock *TrueSucc = BI->getSuccessor(0); |
| 1677 | BasicBlock *FalseSucc = BI->getSuccessor(1); |
| 1678 | |
| 1679 | if (TrueSucc->getSinglePredecessor()) |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1680 | addToLeaderTable(CondVN, |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 1681 | ConstantInt::getTrue(TrueSucc->getContext()), |
| 1682 | TrueSucc); |
| 1683 | if (FalseSucc->getSinglePredecessor()) |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1684 | addToLeaderTable(CondVN, |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 1685 | ConstantInt::getFalse(TrueSucc->getContext()), |
| 1686 | FalseSucc); |
| 1687 | |
| 1688 | return false; |
| 1689 | } |
Owen Anderson | 680ac4f | 2011-01-04 19:10:54 +0000 | [diff] [blame] | 1690 | |
Owen Anderson | 2cf7537 | 2011-01-04 22:15:21 +0000 | [diff] [blame] | 1691 | // Instructions with void type don't return a value, so there's |
| 1692 | // no point in trying to find redudancies in them. |
| 1693 | if (I->getType()->isVoidTy()) return false; |
| 1694 | |
Owen Anderson | c2146a6 | 2011-01-04 18:54:18 +0000 | [diff] [blame] | 1695 | uint32_t NextNum = VN.getNextUnusedValueNumber(); |
| 1696 | unsigned Num = VN.lookup_or_add(I); |
| 1697 | |
Owen Anderson | e5ffa90 | 2008-04-07 09:59:07 +0000 | [diff] [blame] | 1698 | // Allocations are always uniquely numbered, so we can save time and memory |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1699 | // by fast failing them. |
Chris Lattner | 459f4f8 | 2010-12-19 20:24:28 +0000 | [diff] [blame] | 1700 | if (isa<AllocaInst>(I) || isa<TerminatorInst>(I) || isa<PHINode>(I)) { |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1701 | addToLeaderTable(Num, I, I->getParent()); |
Owen Anderson | e5ffa90 | 2008-04-07 09:59:07 +0000 | [diff] [blame] | 1702 | return false; |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1703 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1704 | |
Owen Anderson | 0ae33ef | 2008-07-03 17:44:33 +0000 | [diff] [blame] | 1705 | // If the number we were assigned was a brand new VN, then we don't |
| 1706 | // need to do a lookup to see if the number already exists |
| 1707 | // somewhere in the domtree: it can't! |
Chris Lattner | 459f4f8 | 2010-12-19 20:24:28 +0000 | [diff] [blame] | 1708 | if (Num == NextNum) { |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1709 | addToLeaderTable(Num, I, I->getParent()); |
Chris Lattner | 459f4f8 | 2010-12-19 20:24:28 +0000 | [diff] [blame] | 1710 | return false; |
| 1711 | } |
| 1712 | |
Owen Anderson | 255dafc | 2008-12-15 02:03:00 +0000 | [diff] [blame] | 1713 | // Perform fast-path value-number based elimination of values inherited from |
| 1714 | // dominators. |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1715 | Value *repl = findLeader(I->getParent(), Num); |
Chris Lattner | 459f4f8 | 2010-12-19 20:24:28 +0000 | [diff] [blame] | 1716 | if (repl == 0) { |
| 1717 | // Failure, just remember this instance for future use. |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1718 | addToLeaderTable(Num, I, I->getParent()); |
Chris Lattner | 459f4f8 | 2010-12-19 20:24:28 +0000 | [diff] [blame] | 1719 | return false; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 1720 | } |
Chris Lattner | 459f4f8 | 2010-12-19 20:24:28 +0000 | [diff] [blame] | 1721 | |
| 1722 | // Remove it! |
| 1723 | VN.erase(I); |
| 1724 | I->replaceAllUsesWith(repl); |
| 1725 | if (MD && repl->getType()->isPointerTy()) |
| 1726 | MD->invalidateCachedPointerInfo(repl); |
| 1727 | toErase.push_back(I); |
| 1728 | return true; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Bill Wendling | 30788b8 | 2008-12-22 22:32:22 +0000 | [diff] [blame] | 1731 | /// runOnFunction - This is the main transformation entry point for a function. |
Owen Anderson | 3e75a42 | 2007-08-14 18:04:11 +0000 | [diff] [blame] | 1732 | bool GVN::runOnFunction(Function& F) { |
Dan Gohman | 4ec01b2 | 2009-11-14 02:27:51 +0000 | [diff] [blame] | 1733 | if (!NoLoads) |
| 1734 | MD = &getAnalysis<MemoryDependenceAnalysis>(); |
Chris Lattner | 663e441 | 2008-12-01 00:40:32 +0000 | [diff] [blame] | 1735 | DT = &getAnalysis<DominatorTree>(); |
Duncan Sands | 88c3df7 | 2010-11-12 21:10:24 +0000 | [diff] [blame] | 1736 | TD = getAnalysisIfAvailable<TargetData>(); |
Owen Anderson | a472c4a | 2008-05-12 20:15:55 +0000 | [diff] [blame] | 1737 | VN.setAliasAnalysis(&getAnalysis<AliasAnalysis>()); |
Chris Lattner | 663e441 | 2008-12-01 00:40:32 +0000 | [diff] [blame] | 1738 | VN.setMemDep(MD); |
| 1739 | VN.setDomTree(DT); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1740 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1741 | bool Changed = false; |
| 1742 | bool ShouldContinue = true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1743 | |
Owen Anderson | 5d0af03 | 2008-07-16 17:52:31 +0000 | [diff] [blame] | 1744 | // Merge unconditional branches, allowing PRE to catch more |
| 1745 | // optimization opportunities. |
| 1746 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) { |
Chris Lattner | b5b7997 | 2011-01-11 08:13:40 +0000 | [diff] [blame] | 1747 | BasicBlock *BB = FI++; |
| 1748 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 1749 | bool removedBlock = MergeBlockIntoPredecessor(BB, this); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1750 | if (removedBlock) ++NumGVNBlocks; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1751 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1752 | Changed |= removedBlock; |
Owen Anderson | 5d0af03 | 2008-07-16 17:52:31 +0000 | [diff] [blame] | 1753 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1754 | |
Chris Lattner | ae19931 | 2008-12-09 19:21:47 +0000 | [diff] [blame] | 1755 | unsigned Iteration = 0; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1756 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1757 | while (ShouldContinue) { |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1758 | DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n"); |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1759 | ShouldContinue = iterateOnFunction(F); |
Bob Wilson | 484d4a3 | 2010-02-16 19:51:59 +0000 | [diff] [blame] | 1760 | if (splitCriticalEdges()) |
| 1761 | ShouldContinue = true; |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1762 | Changed |= ShouldContinue; |
Chris Lattner | ae19931 | 2008-12-09 19:21:47 +0000 | [diff] [blame] | 1763 | ++Iteration; |
Owen Anderson | 3e75a42 | 2007-08-14 18:04:11 +0000 | [diff] [blame] | 1764 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1765 | |
Owen Anderson | e98c54c | 2008-07-18 18:03:38 +0000 | [diff] [blame] | 1766 | if (EnablePRE) { |
Owen Anderson | 0c7f91c | 2008-09-03 23:06:07 +0000 | [diff] [blame] | 1767 | bool PREChanged = true; |
| 1768 | while (PREChanged) { |
| 1769 | PREChanged = performPRE(F); |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1770 | Changed |= PREChanged; |
Owen Anderson | 0c7f91c | 2008-09-03 23:06:07 +0000 | [diff] [blame] | 1771 | } |
Owen Anderson | e98c54c | 2008-07-18 18:03:38 +0000 | [diff] [blame] | 1772 | } |
Chris Lattner | ae19931 | 2008-12-09 19:21:47 +0000 | [diff] [blame] | 1773 | // FIXME: Should perform GVN again after PRE does something. PRE can move |
| 1774 | // computations into blocks where they become fully redundant. Note that |
| 1775 | // we can't do this until PRE's critical edge splitting updates memdep. |
| 1776 | // Actually, when this happens, we should just fully integrate PRE into GVN. |
Nuno Lopes | 7cdd9ee | 2008-10-10 16:25:50 +0000 | [diff] [blame] | 1777 | |
| 1778 | cleanupGlobalSets(); |
| 1779 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1780 | return Changed; |
Owen Anderson | 3e75a42 | 2007-08-14 18:04:11 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1784 | bool GVN::processBlock(BasicBlock *BB) { |
Chris Lattner | ae19931 | 2008-12-09 19:21:47 +0000 | [diff] [blame] | 1785 | // FIXME: Kill off toErase by doing erasing eagerly in a helper function (and |
| 1786 | // incrementing BI before processing an instruction). |
Owen Anderson | af4240a | 2008-06-12 19:25:32 +0000 | [diff] [blame] | 1787 | SmallVector<Instruction*, 8> toErase; |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1788 | bool ChangedFunction = false; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1789 | |
Owen Anderson | af4240a | 2008-06-12 19:25:32 +0000 | [diff] [blame] | 1790 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); |
| 1791 | BI != BE;) { |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1792 | ChangedFunction |= processInstruction(BI, toErase); |
Owen Anderson | af4240a | 2008-06-12 19:25:32 +0000 | [diff] [blame] | 1793 | if (toErase.empty()) { |
| 1794 | ++BI; |
| 1795 | continue; |
| 1796 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1797 | |
Owen Anderson | af4240a | 2008-06-12 19:25:32 +0000 | [diff] [blame] | 1798 | // If we need some instructions deleted, do it now. |
| 1799 | NumGVNInstr += toErase.size(); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1800 | |
Owen Anderson | af4240a | 2008-06-12 19:25:32 +0000 | [diff] [blame] | 1801 | // Avoid iterator invalidation. |
| 1802 | bool AtStart = BI == BB->begin(); |
| 1803 | if (!AtStart) |
| 1804 | --BI; |
| 1805 | |
| 1806 | for (SmallVector<Instruction*, 4>::iterator I = toErase.begin(), |
Chris Lattner | 663e441 | 2008-12-01 00:40:32 +0000 | [diff] [blame] | 1807 | E = toErase.end(); I != E; ++I) { |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1808 | DEBUG(dbgs() << "GVN removed: " << **I << '\n'); |
Dan Gohman | 4ec01b2 | 2009-11-14 02:27:51 +0000 | [diff] [blame] | 1809 | if (MD) MD->removeInstruction(*I); |
Owen Anderson | af4240a | 2008-06-12 19:25:32 +0000 | [diff] [blame] | 1810 | (*I)->eraseFromParent(); |
Bill Wendling | ec40d50 | 2008-12-22 21:57:30 +0000 | [diff] [blame] | 1811 | DEBUG(verifyRemoved(*I)); |
Chris Lattner | 663e441 | 2008-12-01 00:40:32 +0000 | [diff] [blame] | 1812 | } |
Chris Lattner | ae19931 | 2008-12-09 19:21:47 +0000 | [diff] [blame] | 1813 | toErase.clear(); |
Owen Anderson | af4240a | 2008-06-12 19:25:32 +0000 | [diff] [blame] | 1814 | |
| 1815 | if (AtStart) |
| 1816 | BI = BB->begin(); |
| 1817 | else |
| 1818 | ++BI; |
Owen Anderson | af4240a | 2008-06-12 19:25:32 +0000 | [diff] [blame] | 1819 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1820 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1821 | return ChangedFunction; |
Owen Anderson | af4240a | 2008-06-12 19:25:32 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1824 | /// performPRE - Perform a purely local form of PRE that looks for diamond |
| 1825 | /// control flow patterns and attempts to perform simple PRE at the join point. |
Chris Lattner | fb6e701 | 2009-10-31 22:11:15 +0000 | [diff] [blame] | 1826 | bool GVN::performPRE(Function &F) { |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1827 | bool Changed = false; |
Chris Lattner | 0971379 | 2008-12-01 07:29:03 +0000 | [diff] [blame] | 1828 | DenseMap<BasicBlock*, Value*> predMap; |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1829 | for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()), |
| 1830 | DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) { |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1831 | BasicBlock *CurrentBlock = *DI; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1832 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1833 | // Nothing to PRE in the entry block. |
| 1834 | if (CurrentBlock == &F.getEntryBlock()) continue; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1835 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1836 | for (BasicBlock::iterator BI = CurrentBlock->begin(), |
| 1837 | BE = CurrentBlock->end(); BI != BE; ) { |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1838 | Instruction *CurInst = BI++; |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 1839 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1840 | if (isa<AllocaInst>(CurInst) || |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 1841 | isa<TerminatorInst>(CurInst) || isa<PHINode>(CurInst) || |
Devang Patel | 9674d15 | 2009-10-14 17:29:00 +0000 | [diff] [blame] | 1842 | CurInst->getType()->isVoidTy() || |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 1843 | CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() || |
John Criswell | 090c0a2 | 2009-03-10 15:04:53 +0000 | [diff] [blame] | 1844 | isa<DbgInfoIntrinsic>(CurInst)) |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1845 | continue; |
Owen Anderson | 5015b34 | 2010-08-07 00:20:35 +0000 | [diff] [blame] | 1846 | |
| 1847 | // We don't currently value number ANY inline asm calls. |
| 1848 | if (CallInst *CallI = dyn_cast<CallInst>(CurInst)) |
| 1849 | if (CallI->isInlineAsm()) |
| 1850 | continue; |
Duncan Sands | 7af1c78 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 1851 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1852 | uint32_t ValNo = VN.lookup(CurInst); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1853 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1854 | // Look for the predecessors for PRE opportunities. We're |
| 1855 | // only trying to solve the basic diamond case, where |
| 1856 | // a value is computed in the successor and one predecessor, |
| 1857 | // but not the other. We also explicitly disallow cases |
| 1858 | // where the successor is its own predecessor, because they're |
| 1859 | // more complicated to get right. |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1860 | unsigned NumWith = 0; |
| 1861 | unsigned NumWithout = 0; |
| 1862 | BasicBlock *PREPred = 0; |
Chris Lattner | 0971379 | 2008-12-01 07:29:03 +0000 | [diff] [blame] | 1863 | predMap.clear(); |
| 1864 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1865 | for (pred_iterator PI = pred_begin(CurrentBlock), |
| 1866 | PE = pred_end(CurrentBlock); PI != PE; ++PI) { |
Gabor Greif | 0814985 | 2010-07-09 14:36:49 +0000 | [diff] [blame] | 1867 | BasicBlock *P = *PI; |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1868 | // We're not interested in PRE where the block is its |
Bob Wilson | e7b635f | 2010-02-03 00:33:21 +0000 | [diff] [blame] | 1869 | // own predecessor, or in blocks with predecessors |
Owen Anderson | 6fafe84 | 2008-06-20 01:15:47 +0000 | [diff] [blame] | 1870 | // that are not reachable. |
Gabor Greif | 0814985 | 2010-07-09 14:36:49 +0000 | [diff] [blame] | 1871 | if (P == CurrentBlock) { |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1872 | NumWithout = 2; |
Owen Anderson | 6fafe84 | 2008-06-20 01:15:47 +0000 | [diff] [blame] | 1873 | break; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 1874 | } else if (!DT->dominates(&F.getEntryBlock(), P)) { |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1875 | NumWithout = 2; |
Owen Anderson | 6fafe84 | 2008-06-20 01:15:47 +0000 | [diff] [blame] | 1876 | break; |
| 1877 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1878 | |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1879 | Value* predV = findLeader(P, ValNo); |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 1880 | if (predV == 0) { |
Gabor Greif | 0814985 | 2010-07-09 14:36:49 +0000 | [diff] [blame] | 1881 | PREPred = P; |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1882 | ++NumWithout; |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 1883 | } else if (predV == CurInst) { |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1884 | NumWithout = 2; |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1885 | } else { |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 1886 | predMap[P] = predV; |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1887 | ++NumWith; |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1888 | } |
| 1889 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1890 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1891 | // Don't do PRE when it might increase code size, i.e. when |
| 1892 | // we would need to insert instructions in more than one pred. |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1893 | if (NumWithout != 1 || NumWith == 0) |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1894 | continue; |
Chris Lattner | fb6e701 | 2009-10-31 22:11:15 +0000 | [diff] [blame] | 1895 | |
| 1896 | // Don't do PRE across indirect branch. |
| 1897 | if (isa<IndirectBrInst>(PREPred->getTerminator())) |
| 1898 | continue; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1899 | |
Owen Anderson | 5c274ee | 2008-06-19 19:54:19 +0000 | [diff] [blame] | 1900 | // We can't do PRE safely on a critical edge, so instead we schedule |
| 1901 | // the edge to be split and perform the PRE the next time we iterate |
| 1902 | // on the function. |
Bob Wilson | ae23daf | 2010-02-16 21:06:42 +0000 | [diff] [blame] | 1903 | unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock); |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1904 | if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) { |
| 1905 | toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum)); |
Owen Anderson | 5c274ee | 2008-06-19 19:54:19 +0000 | [diff] [blame] | 1906 | continue; |
| 1907 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1908 | |
Bob Wilson | e7b635f | 2010-02-03 00:33:21 +0000 | [diff] [blame] | 1909 | // Instantiate the expression in the predecessor that lacked it. |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1910 | // Because we are going top-down through the block, all value numbers |
| 1911 | // will be available in the predecessor by the time we need them. Any |
Bob Wilson | e7b635f | 2010-02-03 00:33:21 +0000 | [diff] [blame] | 1912 | // that weren't originally present will have been instantiated earlier |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1913 | // in this loop. |
Nick Lewycky | 6776064 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 1914 | Instruction *PREInstr = CurInst->clone(); |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1915 | bool success = true; |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1916 | for (unsigned i = 0, e = CurInst->getNumOperands(); i != e; ++i) { |
| 1917 | Value *Op = PREInstr->getOperand(i); |
| 1918 | if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op)) |
| 1919 | continue; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1920 | |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1921 | if (Value *V = findLeader(PREPred, VN.lookup(Op))) { |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1922 | PREInstr->setOperand(i, V); |
| 1923 | } else { |
| 1924 | success = false; |
| 1925 | break; |
Owen Anderson | c45996b | 2008-07-11 20:05:13 +0000 | [diff] [blame] | 1926 | } |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1927 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1928 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1929 | // Fail out if we encounter an operand that is not available in |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1930 | // the PRE predecessor. This is typically because of loads which |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1931 | // are not value numbered precisely. |
| 1932 | if (!success) { |
| 1933 | delete PREInstr; |
Bill Wendling | 70ded19 | 2008-12-22 22:14:07 +0000 | [diff] [blame] | 1934 | DEBUG(verifyRemoved(PREInstr)); |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1935 | continue; |
| 1936 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1937 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1938 | PREInstr->insertBefore(PREPred->getTerminator()); |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1939 | PREInstr->setName(CurInst->getName() + ".pre"); |
Owen Anderson | 6fafe84 | 2008-06-20 01:15:47 +0000 | [diff] [blame] | 1940 | predMap[PREPred] = PREInstr; |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1941 | VN.add(PREInstr, ValNo); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1942 | ++NumGVNPRE; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1943 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1944 | // Update the availability map to include the new instruction. |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1945 | addToLeaderTable(ValNo, PREInstr, PREPred); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1946 | |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1947 | // Create a PHI to make the value available in this block. |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1948 | PHINode* Phi = PHINode::Create(CurInst->getType(), |
| 1949 | CurInst->getName() + ".pre-phi", |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1950 | CurrentBlock->begin()); |
| 1951 | for (pred_iterator PI = pred_begin(CurrentBlock), |
Gabor Greif | 1d3ae02 | 2010-07-09 14:48:08 +0000 | [diff] [blame] | 1952 | PE = pred_end(CurrentBlock); PI != PE; ++PI) { |
| 1953 | BasicBlock *P = *PI; |
| 1954 | Phi->addIncoming(predMap[P], P); |
| 1955 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1956 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 1957 | VN.add(Phi, ValNo); |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1958 | addToLeaderTable(ValNo, Phi, CurrentBlock); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1959 | |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1960 | CurInst->replaceAllUsesWith(Phi); |
Owen Anderson | 392249f | 2011-01-03 23:51:43 +0000 | [diff] [blame] | 1961 | if (Phi->getType()->isPointerTy()) { |
| 1962 | // Because we have added a PHI-use of the pointer value, it has now |
| 1963 | // "escaped" from alias analysis' perspective. We need to inform |
| 1964 | // AA of this. |
| 1965 | for (unsigned ii = 0, ee = Phi->getNumIncomingValues(); ii != ee; ++ii) |
| 1966 | VN.getAliasAnalysis()->addEscapingUse(Phi->getOperandUse(2*ii)); |
| 1967 | |
| 1968 | if (MD) |
| 1969 | MD->invalidateCachedPointerInfo(Phi); |
| 1970 | } |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1971 | VN.erase(CurInst); |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 1972 | removeFromLeaderTable(ValNo, CurInst, CurrentBlock); |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1973 | |
David Greene | bf7f78e | 2010-01-05 01:27:17 +0000 | [diff] [blame] | 1974 | DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n'); |
Dan Gohman | 4ec01b2 | 2009-11-14 02:27:51 +0000 | [diff] [blame] | 1975 | if (MD) MD->removeInstruction(CurInst); |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1976 | CurInst->eraseFromParent(); |
Bill Wendling | ec40d50 | 2008-12-22 21:57:30 +0000 | [diff] [blame] | 1977 | DEBUG(verifyRemoved(CurInst)); |
Chris Lattner | d0f5bfc | 2008-12-01 07:35:54 +0000 | [diff] [blame] | 1978 | Changed = true; |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1979 | } |
| 1980 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1981 | |
Bob Wilson | 484d4a3 | 2010-02-16 19:51:59 +0000 | [diff] [blame] | 1982 | if (splitCriticalEdges()) |
| 1983 | Changed = true; |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 1984 | |
Bob Wilson | 484d4a3 | 2010-02-16 19:51:59 +0000 | [diff] [blame] | 1985 | return Changed; |
| 1986 | } |
| 1987 | |
| 1988 | /// splitCriticalEdges - Split critical edges found during the previous |
| 1989 | /// iteration that may enable further optimization. |
| 1990 | bool GVN::splitCriticalEdges() { |
| 1991 | if (toSplit.empty()) |
| 1992 | return false; |
| 1993 | do { |
| 1994 | std::pair<TerminatorInst*, unsigned> Edge = toSplit.pop_back_val(); |
| 1995 | SplitCriticalEdge(Edge.first, Edge.second, this); |
| 1996 | } while (!toSplit.empty()); |
Evan Cheng | 19d417c | 2010-03-01 22:23:12 +0000 | [diff] [blame] | 1997 | if (MD) MD->invalidateCachedPredecessors(); |
Bob Wilson | 484d4a3 | 2010-02-16 19:51:59 +0000 | [diff] [blame] | 1998 | return true; |
Owen Anderson | b230372 | 2008-06-18 21:41:49 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Bill Wendling | 30788b8 | 2008-12-22 22:32:22 +0000 | [diff] [blame] | 2001 | /// iterateOnFunction - Executes one iteration of GVN |
Owen Anderson | 3e75a42 | 2007-08-14 18:04:11 +0000 | [diff] [blame] | 2002 | bool GVN::iterateOnFunction(Function &F) { |
Nuno Lopes | 7cdd9ee | 2008-10-10 16:25:50 +0000 | [diff] [blame] | 2003 | cleanupGlobalSets(); |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 2004 | |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 2005 | // Top-down walk of the dominator tree |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 2006 | bool Changed = false; |
Owen Anderson | c34d112 | 2008-12-15 03:52:17 +0000 | [diff] [blame] | 2007 | #if 0 |
| 2008 | // Needed for value numbering with phi construction to work. |
Owen Anderson | 255dafc | 2008-12-15 02:03:00 +0000 | [diff] [blame] | 2009 | ReversePostOrderTraversal<Function*> RPOT(&F); |
| 2010 | for (ReversePostOrderTraversal<Function*>::rpo_iterator RI = RPOT.begin(), |
| 2011 | RE = RPOT.end(); RI != RE; ++RI) |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 2012 | Changed |= processBlock(*RI); |
Owen Anderson | c34d112 | 2008-12-15 03:52:17 +0000 | [diff] [blame] | 2013 | #else |
| 2014 | for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()), |
| 2015 | DE = df_end(DT->getRootNode()); DI != DE; ++DI) |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 2016 | Changed |= processBlock(DI->getBlock()); |
Owen Anderson | c34d112 | 2008-12-15 03:52:17 +0000 | [diff] [blame] | 2017 | #endif |
| 2018 | |
Chris Lattner | b2412a8 | 2009-09-21 02:42:51 +0000 | [diff] [blame] | 2019 | return Changed; |
Owen Anderson | 1ad2cb7 | 2007-07-24 17:55:58 +0000 | [diff] [blame] | 2020 | } |
Nuno Lopes | 7cdd9ee | 2008-10-10 16:25:50 +0000 | [diff] [blame] | 2021 | |
| 2022 | void GVN::cleanupGlobalSets() { |
| 2023 | VN.clear(); |
Owen Anderson | b1602ab | 2011-01-04 19:29:46 +0000 | [diff] [blame] | 2024 | LeaderTable.clear(); |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 2025 | TableAllocator.Reset(); |
Nuno Lopes | 7cdd9ee | 2008-10-10 16:25:50 +0000 | [diff] [blame] | 2026 | } |
Bill Wendling | 246dbbb | 2008-12-22 21:36:08 +0000 | [diff] [blame] | 2027 | |
| 2028 | /// verifyRemoved - Verify that the specified instruction does not occur in our |
| 2029 | /// internal data structures. |
Bill Wendling | 6d463f2 | 2008-12-22 22:28:56 +0000 | [diff] [blame] | 2030 | void GVN::verifyRemoved(const Instruction *Inst) const { |
| 2031 | VN.verifyRemoved(Inst); |
Bill Wendling | 70ded19 | 2008-12-22 22:14:07 +0000 | [diff] [blame] | 2032 | |
Bill Wendling | 6d463f2 | 2008-12-22 22:28:56 +0000 | [diff] [blame] | 2033 | // Walk through the value number scope to make sure the instruction isn't |
| 2034 | // ferreted away in it. |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 2035 | for (DenseMap<uint32_t, LeaderTableEntry>::const_iterator |
Owen Anderson | b1602ab | 2011-01-04 19:29:46 +0000 | [diff] [blame] | 2036 | I = LeaderTable.begin(), E = LeaderTable.end(); I != E; ++I) { |
Owen Anderson | 7a75d61 | 2011-01-04 19:13:25 +0000 | [diff] [blame] | 2037 | const LeaderTableEntry *Node = &I->second; |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 2038 | assert(Node->Val != Inst && "Inst still in value numbering scope!"); |
Owen Anderson | a04a064 | 2010-11-18 18:32:40 +0000 | [diff] [blame] | 2039 | |
Owen Anderson | f056838 | 2010-12-21 23:54:34 +0000 | [diff] [blame] | 2040 | while (Node->Next) { |
| 2041 | Node = Node->Next; |
| 2042 | assert(Node->Val != Inst && "Inst still in value numbering scope!"); |
Bill Wendling | 70ded19 | 2008-12-22 22:14:07 +0000 | [diff] [blame] | 2043 | } |
| 2044 | } |
Bill Wendling | 246dbbb | 2008-12-22 21:36:08 +0000 | [diff] [blame] | 2045 | } |