blob: 962ba71dc74dae29b3d942c6bc67fddc25e51da1 [file] [log] [blame]
Chris Lattnerd2a653a2008-12-05 07:49:08 +00001//===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
Owen Andersonab6ec2e2007-07-24 17:55:58 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Owen Andersonab6ec2e2007-07-24 17:55:58 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass performs global value numbering to eliminate fully redundant
11// instructions. It also performs simple dead load elimination.
12//
John Criswell073e4d12009-03-10 15:04:53 +000013// Note that this pass does the value numbering itself; it does not use the
Matthijs Kooijman5afc2742008-06-05 07:55:49 +000014// ValueNumbering analysis passes.
15//
Owen Andersonab6ec2e2007-07-24 17:55:58 +000016//===----------------------------------------------------------------------===//
17
18#define DEBUG_TYPE "gvn"
Owen Andersonab6ec2e2007-07-24 17:55:58 +000019#include "llvm/Transforms/Scalar.h"
Owen Anderson5e5599b2007-07-25 19:57:03 +000020#include "llvm/BasicBlock.h"
Owen Andersondbf23cc2007-07-26 18:26:51 +000021#include "llvm/Constants.h"
Owen Andersonab6ec2e2007-07-24 17:55:58 +000022#include "llvm/DerivedTypes.h"
Owen Andersondbf23cc2007-07-26 18:26:51 +000023#include "llvm/Function.h"
Devang Patele8c6d312009-03-06 02:59:27 +000024#include "llvm/IntrinsicInst.h"
Owen Andersonb5618da2009-07-03 00:17:18 +000025#include "llvm/LLVMContext.h"
Owen Andersondbf23cc2007-07-26 18:26:51 +000026#include "llvm/Value.h"
Owen Andersonab6ec2e2007-07-24 17:55:58 +000027#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/DepthFirstIterator.h"
Owen Andersonbfe133e2008-12-15 02:03:00 +000029#include "llvm/ADT/PostOrderIterator.h"
Owen Andersonab6ec2e2007-07-24 17:55:58 +000030#include "llvm/ADT/SmallPtrSet.h"
31#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/Statistic.h"
Owen Anderson09b83ba2007-10-18 19:39:33 +000033#include "llvm/Analysis/Dominators.h"
34#include "llvm/Analysis/AliasAnalysis.h"
Owen Andersonab6ec2e2007-07-24 17:55:58 +000035#include "llvm/Analysis/MemoryDependenceAnalysis.h"
36#include "llvm/Support/CFG.h"
Owen Andersone780d662008-06-19 19:57:25 +000037#include "llvm/Support/CommandLine.h"
Owen Andersonab6ec2e2007-07-24 17:55:58 +000038#include "llvm/Support/Compiler.h"
Chris Lattnerd528b212008-03-29 04:36:18 +000039#include "llvm/Support/Debug.h"
Owen Andersonfdf9f162008-06-19 19:54:19 +000040#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Dale Johannesen81b64632009-06-17 20:48:23 +000041#include "llvm/Transforms/Utils/Local.h"
Duncan Sands26ff6f92008-10-08 07:23:46 +000042#include <cstdio>
Owen Andersonab6ec2e2007-07-24 17:55:58 +000043using namespace llvm;
44
Bill Wendling3c793442008-12-22 22:14:07 +000045STATISTIC(NumGVNInstr, "Number of instructions deleted");
46STATISTIC(NumGVNLoad, "Number of loads deleted");
47STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
Owen Anderson53d546e2008-07-15 16:28:06 +000048STATISTIC(NumGVNBlocks, "Number of blocks merged");
Bill Wendling3c793442008-12-22 22:14:07 +000049STATISTIC(NumPRELoad, "Number of loads PRE'd");
Chris Lattner168be762008-03-22 04:13:49 +000050
Evan Cheng9598f932008-06-20 01:01:07 +000051static cl::opt<bool> EnablePRE("enable-pre",
Owen Andersonaddbe3e2008-07-17 19:41:00 +000052 cl::init(true), cl::Hidden);
Dan Gohmana8f8a852009-06-15 18:30:15 +000053static cl::opt<bool> EnableLoadPRE("enable-load-pre", cl::init(true));
Owen Andersone780d662008-06-19 19:57:25 +000054
Owen Andersonab6ec2e2007-07-24 17:55:58 +000055//===----------------------------------------------------------------------===//
56// ValueTable Class
57//===----------------------------------------------------------------------===//
58
59/// This class holds the mapping between values and value numbers. It is used
60/// as an efficient mechanism to determine the expression-wise equivalence of
61/// two values.
62namespace {
63 struct VISIBILITY_HIDDEN Expression {
Dan Gohmana5b96452009-06-04 22:49:04 +000064 enum ExpressionOpcode { ADD, FADD, SUB, FSUB, MUL, FMUL,
65 UDIV, SDIV, FDIV, UREM, SREM,
Owen Andersonab6ec2e2007-07-24 17:55:58 +000066 FREM, SHL, LSHR, ASHR, AND, OR, XOR, ICMPEQ,
67 ICMPNE, ICMPUGT, ICMPUGE, ICMPULT, ICMPULE,
68 ICMPSGT, ICMPSGE, ICMPSLT, ICMPSLE, FCMPOEQ,
69 FCMPOGT, FCMPOGE, FCMPOLT, FCMPOLE, FCMPONE,
70 FCMPORD, FCMPUNO, FCMPUEQ, FCMPUGT, FCMPUGE,
71 FCMPULT, FCMPULE, FCMPUNE, EXTRACT, INSERT,
72 SHUFFLE, SELECT, TRUNC, ZEXT, SEXT, FPTOUI,
73 FPTOSI, UITOFP, SITOFP, FPTRUNC, FPEXT,
Owen Anderson69057b82008-05-13 08:17:22 +000074 PTRTOINT, INTTOPTR, BITCAST, GEP, CALL, CONSTANT,
Owen Anderson45d37012008-06-19 17:25:39 +000075 EMPTY, TOMBSTONE };
Owen Andersonab6ec2e2007-07-24 17:55:58 +000076
77 ExpressionOpcode opcode;
78 const Type* type;
79 uint32_t firstVN;
80 uint32_t secondVN;
81 uint32_t thirdVN;
82 SmallVector<uint32_t, 4> varargs;
Owen Anderson09b83ba2007-10-18 19:39:33 +000083 Value* function;
Owen Andersonab6ec2e2007-07-24 17:55:58 +000084
85 Expression() { }
86 Expression(ExpressionOpcode o) : opcode(o) { }
87
88 bool operator==(const Expression &other) const {
89 if (opcode != other.opcode)
90 return false;
91 else if (opcode == EMPTY || opcode == TOMBSTONE)
92 return true;
93 else if (type != other.type)
94 return false;
Owen Anderson09b83ba2007-10-18 19:39:33 +000095 else if (function != other.function)
96 return false;
Owen Andersonab6ec2e2007-07-24 17:55:58 +000097 else if (firstVN != other.firstVN)
98 return false;
99 else if (secondVN != other.secondVN)
100 return false;
101 else if (thirdVN != other.thirdVN)
102 return false;
103 else {
104 if (varargs.size() != other.varargs.size())
105 return false;
106
107 for (size_t i = 0; i < varargs.size(); ++i)
108 if (varargs[i] != other.varargs[i])
109 return false;
110
111 return true;
112 }
113 }
114
115 bool operator!=(const Expression &other) const {
Bill Wendling86f01cb2008-12-22 22:16:31 +0000116 return !(*this == other);
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000117 }
118 };
119
120 class VISIBILITY_HIDDEN ValueTable {
121 private:
122 DenseMap<Value*, uint32_t> valueNumbering;
123 DenseMap<Expression, uint32_t> expressionNumbering;
Owen Andersonf7928602008-05-12 20:15:55 +0000124 AliasAnalysis* AA;
125 MemoryDependenceAnalysis* MD;
126 DominatorTree* DT;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000127
128 uint32_t nextValueNumber;
129
130 Expression::ExpressionOpcode getOpcode(BinaryOperator* BO);
131 Expression::ExpressionOpcode getOpcode(CmpInst* C);
132 Expression::ExpressionOpcode getOpcode(CastInst* C);
133 Expression create_expression(BinaryOperator* BO);
134 Expression create_expression(CmpInst* C);
135 Expression create_expression(ShuffleVectorInst* V);
136 Expression create_expression(ExtractElementInst* C);
137 Expression create_expression(InsertElementInst* V);
138 Expression create_expression(SelectInst* V);
139 Expression create_expression(CastInst* C);
140 Expression create_expression(GetElementPtrInst* G);
Owen Anderson09b83ba2007-10-18 19:39:33 +0000141 Expression create_expression(CallInst* C);
Owen Anderson69057b82008-05-13 08:17:22 +0000142 Expression create_expression(Constant* C);
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000143 public:
Dan Gohmanc4971722009-04-01 16:37:47 +0000144 ValueTable() : nextValueNumber(1) { }
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000145 uint32_t lookup_or_add(Value* V);
146 uint32_t lookup(Value* V) const;
147 void add(Value* V, uint32_t num);
148 void clear();
149 void erase(Value* v);
150 unsigned size();
Owen Andersonf7928602008-05-12 20:15:55 +0000151 void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
Chris Lattner8541ede2008-12-01 00:40:32 +0000152 AliasAnalysis *getAliasAnalysis() const { return AA; }
Owen Andersonf7928602008-05-12 20:15:55 +0000153 void setMemDep(MemoryDependenceAnalysis* M) { MD = M; }
154 void setDomTree(DominatorTree* D) { DT = D; }
Owen Anderson3ea90a72008-07-03 17:44:33 +0000155 uint32_t getNextUnusedValueNumber() { return nextValueNumber; }
Bill Wendling6b18a392008-12-22 21:36:08 +0000156 void verifyRemoved(const Value *) const;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000157 };
158}
159
160namespace llvm {
Chris Lattner0625bd62007-09-17 18:34:04 +0000161template <> struct DenseMapInfo<Expression> {
Owen Anderson9699a6e2007-08-02 18:16:06 +0000162 static inline Expression getEmptyKey() {
163 return Expression(Expression::EMPTY);
164 }
165
166 static inline Expression getTombstoneKey() {
167 return Expression(Expression::TOMBSTONE);
168 }
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000169
170 static unsigned getHashValue(const Expression e) {
171 unsigned hash = e.opcode;
172
173 hash = e.firstVN + hash * 37;
174 hash = e.secondVN + hash * 37;
175 hash = e.thirdVN + hash * 37;
176
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +0000177 hash = ((unsigned)((uintptr_t)e.type >> 4) ^
178 (unsigned)((uintptr_t)e.type >> 9)) +
179 hash * 37;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000180
Owen Anderson9699a6e2007-08-02 18:16:06 +0000181 for (SmallVector<uint32_t, 4>::const_iterator I = e.varargs.begin(),
182 E = e.varargs.end(); I != E; ++I)
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000183 hash = *I + hash * 37;
184
Anton Korobeynikov1bfd1212008-02-20 11:26:25 +0000185 hash = ((unsigned)((uintptr_t)e.function >> 4) ^
186 (unsigned)((uintptr_t)e.function >> 9)) +
187 hash * 37;
Owen Anderson09b83ba2007-10-18 19:39:33 +0000188
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000189 return hash;
190 }
Chris Lattner0625bd62007-09-17 18:34:04 +0000191 static bool isEqual(const Expression &LHS, const Expression &RHS) {
192 return LHS == RHS;
193 }
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000194 static bool isPod() { return true; }
195};
196}
197
198//===----------------------------------------------------------------------===//
199// ValueTable Internal Functions
200//===----------------------------------------------------------------------===//
Chris Lattner2876a642008-03-21 21:14:38 +0000201Expression::ExpressionOpcode ValueTable::getOpcode(BinaryOperator* BO) {
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000202 switch(BO->getOpcode()) {
Chris Lattner2876a642008-03-21 21:14:38 +0000203 default: // THIS SHOULD NEVER HAPPEN
204 assert(0 && "Binary operator with unknown opcode?");
205 case Instruction::Add: return Expression::ADD;
Dan Gohmana5b96452009-06-04 22:49:04 +0000206 case Instruction::FAdd: return Expression::FADD;
Chris Lattner2876a642008-03-21 21:14:38 +0000207 case Instruction::Sub: return Expression::SUB;
Dan Gohmana5b96452009-06-04 22:49:04 +0000208 case Instruction::FSub: return Expression::FSUB;
Chris Lattner2876a642008-03-21 21:14:38 +0000209 case Instruction::Mul: return Expression::MUL;
Dan Gohmana5b96452009-06-04 22:49:04 +0000210 case Instruction::FMul: return Expression::FMUL;
Chris Lattner2876a642008-03-21 21:14:38 +0000211 case Instruction::UDiv: return Expression::UDIV;
212 case Instruction::SDiv: return Expression::SDIV;
213 case Instruction::FDiv: return Expression::FDIV;
214 case Instruction::URem: return Expression::UREM;
215 case Instruction::SRem: return Expression::SREM;
216 case Instruction::FRem: return Expression::FREM;
217 case Instruction::Shl: return Expression::SHL;
218 case Instruction::LShr: return Expression::LSHR;
219 case Instruction::AShr: return Expression::ASHR;
220 case Instruction::And: return Expression::AND;
221 case Instruction::Or: return Expression::OR;
222 case Instruction::Xor: return Expression::XOR;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000223 }
224}
225
226Expression::ExpressionOpcode ValueTable::getOpcode(CmpInst* C) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000227 if (isa<ICmpInst>(C)) {
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000228 switch (C->getPredicate()) {
Chris Lattner2876a642008-03-21 21:14:38 +0000229 default: // THIS SHOULD NEVER HAPPEN
230 assert(0 && "Comparison with unknown predicate?");
231 case ICmpInst::ICMP_EQ: return Expression::ICMPEQ;
232 case ICmpInst::ICMP_NE: return Expression::ICMPNE;
233 case ICmpInst::ICMP_UGT: return Expression::ICMPUGT;
234 case ICmpInst::ICMP_UGE: return Expression::ICMPUGE;
235 case ICmpInst::ICMP_ULT: return Expression::ICMPULT;
236 case ICmpInst::ICMP_ULE: return Expression::ICMPULE;
237 case ICmpInst::ICMP_SGT: return Expression::ICMPSGT;
238 case ICmpInst::ICMP_SGE: return Expression::ICMPSGE;
239 case ICmpInst::ICMP_SLT: return Expression::ICMPSLT;
240 case ICmpInst::ICMP_SLE: return Expression::ICMPSLE;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000241 }
Nick Lewyckya21d3da2009-07-08 03:04:38 +0000242 } else {
243 switch (C->getPredicate()) {
244 default: // THIS SHOULD NEVER HAPPEN
245 assert(0 && "Comparison with unknown predicate?");
246 case FCmpInst::FCMP_OEQ: return Expression::FCMPOEQ;
247 case FCmpInst::FCMP_OGT: return Expression::FCMPOGT;
248 case FCmpInst::FCMP_OGE: return Expression::FCMPOGE;
249 case FCmpInst::FCMP_OLT: return Expression::FCMPOLT;
250 case FCmpInst::FCMP_OLE: return Expression::FCMPOLE;
251 case FCmpInst::FCMP_ONE: return Expression::FCMPONE;
252 case FCmpInst::FCMP_ORD: return Expression::FCMPORD;
253 case FCmpInst::FCMP_UNO: return Expression::FCMPUNO;
254 case FCmpInst::FCMP_UEQ: return Expression::FCMPUEQ;
255 case FCmpInst::FCMP_UGT: return Expression::FCMPUGT;
256 case FCmpInst::FCMP_UGE: return Expression::FCMPUGE;
257 case FCmpInst::FCMP_ULT: return Expression::FCMPULT;
258 case FCmpInst::FCMP_ULE: return Expression::FCMPULE;
259 case FCmpInst::FCMP_UNE: return Expression::FCMPUNE;
260 }
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000261 }
262}
263
Chris Lattner2876a642008-03-21 21:14:38 +0000264Expression::ExpressionOpcode ValueTable::getOpcode(CastInst* C) {
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000265 switch(C->getOpcode()) {
Chris Lattner2876a642008-03-21 21:14:38 +0000266 default: // THIS SHOULD NEVER HAPPEN
267 assert(0 && "Cast operator with unknown opcode?");
268 case Instruction::Trunc: return Expression::TRUNC;
269 case Instruction::ZExt: return Expression::ZEXT;
270 case Instruction::SExt: return Expression::SEXT;
271 case Instruction::FPToUI: return Expression::FPTOUI;
272 case Instruction::FPToSI: return Expression::FPTOSI;
273 case Instruction::UIToFP: return Expression::UITOFP;
274 case Instruction::SIToFP: return Expression::SITOFP;
275 case Instruction::FPTrunc: return Expression::FPTRUNC;
276 case Instruction::FPExt: return Expression::FPEXT;
277 case Instruction::PtrToInt: return Expression::PTRTOINT;
278 case Instruction::IntToPtr: return Expression::INTTOPTR;
279 case Instruction::BitCast: return Expression::BITCAST;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000280 }
281}
282
Owen Anderson09b83ba2007-10-18 19:39:33 +0000283Expression ValueTable::create_expression(CallInst* C) {
284 Expression e;
285
286 e.type = C->getType();
287 e.firstVN = 0;
288 e.secondVN = 0;
289 e.thirdVN = 0;
290 e.function = C->getCalledFunction();
291 e.opcode = Expression::CALL;
292
293 for (CallInst::op_iterator I = C->op_begin()+1, E = C->op_end();
294 I != E; ++I)
Owen Anderson1e73f292008-04-11 05:11:49 +0000295 e.varargs.push_back(lookup_or_add(*I));
Owen Anderson09b83ba2007-10-18 19:39:33 +0000296
297 return e;
298}
299
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000300Expression ValueTable::create_expression(BinaryOperator* BO) {
301 Expression e;
302
Owen Anderson1e73f292008-04-11 05:11:49 +0000303 e.firstVN = lookup_or_add(BO->getOperand(0));
304 e.secondVN = lookup_or_add(BO->getOperand(1));
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000305 e.thirdVN = 0;
Owen Anderson09b83ba2007-10-18 19:39:33 +0000306 e.function = 0;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000307 e.type = BO->getType();
308 e.opcode = getOpcode(BO);
309
310 return e;
311}
312
313Expression ValueTable::create_expression(CmpInst* C) {
314 Expression e;
315
Owen Anderson1e73f292008-04-11 05:11:49 +0000316 e.firstVN = lookup_or_add(C->getOperand(0));
317 e.secondVN = lookup_or_add(C->getOperand(1));
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000318 e.thirdVN = 0;
Owen Anderson09b83ba2007-10-18 19:39:33 +0000319 e.function = 0;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000320 e.type = C->getType();
321 e.opcode = getOpcode(C);
322
323 return e;
324}
325
326Expression ValueTable::create_expression(CastInst* C) {
327 Expression e;
328
Owen Anderson1e73f292008-04-11 05:11:49 +0000329 e.firstVN = lookup_or_add(C->getOperand(0));
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000330 e.secondVN = 0;
331 e.thirdVN = 0;
Owen Anderson09b83ba2007-10-18 19:39:33 +0000332 e.function = 0;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000333 e.type = C->getType();
334 e.opcode = getOpcode(C);
335
336 return e;
337}
338
339Expression ValueTable::create_expression(ShuffleVectorInst* S) {
340 Expression e;
341
Owen Anderson1e73f292008-04-11 05:11:49 +0000342 e.firstVN = lookup_or_add(S->getOperand(0));
343 e.secondVN = lookup_or_add(S->getOperand(1));
344 e.thirdVN = lookup_or_add(S->getOperand(2));
Owen Anderson09b83ba2007-10-18 19:39:33 +0000345 e.function = 0;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000346 e.type = S->getType();
347 e.opcode = Expression::SHUFFLE;
348
349 return e;
350}
351
352Expression ValueTable::create_expression(ExtractElementInst* E) {
353 Expression e;
354
Owen Anderson1e73f292008-04-11 05:11:49 +0000355 e.firstVN = lookup_or_add(E->getOperand(0));
356 e.secondVN = lookup_or_add(E->getOperand(1));
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000357 e.thirdVN = 0;
Owen Anderson09b83ba2007-10-18 19:39:33 +0000358 e.function = 0;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000359 e.type = E->getType();
360 e.opcode = Expression::EXTRACT;
361
362 return e;
363}
364
365Expression ValueTable::create_expression(InsertElementInst* I) {
366 Expression e;
367
Owen Anderson1e73f292008-04-11 05:11:49 +0000368 e.firstVN = lookup_or_add(I->getOperand(0));
369 e.secondVN = lookup_or_add(I->getOperand(1));
370 e.thirdVN = lookup_or_add(I->getOperand(2));
Owen Anderson09b83ba2007-10-18 19:39:33 +0000371 e.function = 0;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000372 e.type = I->getType();
373 e.opcode = Expression::INSERT;
374
375 return e;
376}
377
378Expression ValueTable::create_expression(SelectInst* I) {
379 Expression e;
380
Owen Anderson1e73f292008-04-11 05:11:49 +0000381 e.firstVN = lookup_or_add(I->getCondition());
382 e.secondVN = lookup_or_add(I->getTrueValue());
383 e.thirdVN = lookup_or_add(I->getFalseValue());
Owen Anderson09b83ba2007-10-18 19:39:33 +0000384 e.function = 0;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000385 e.type = I->getType();
386 e.opcode = Expression::SELECT;
387
388 return e;
389}
390
391Expression ValueTable::create_expression(GetElementPtrInst* G) {
392 Expression e;
Owen Anderson69057b82008-05-13 08:17:22 +0000393
Owen Anderson1e73f292008-04-11 05:11:49 +0000394 e.firstVN = lookup_or_add(G->getPointerOperand());
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000395 e.secondVN = 0;
396 e.thirdVN = 0;
Owen Anderson09b83ba2007-10-18 19:39:33 +0000397 e.function = 0;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000398 e.type = G->getType();
399 e.opcode = Expression::GEP;
400
401 for (GetElementPtrInst::op_iterator I = G->idx_begin(), E = G->idx_end();
402 I != E; ++I)
Owen Anderson1e73f292008-04-11 05:11:49 +0000403 e.varargs.push_back(lookup_or_add(*I));
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000404
405 return e;
406}
407
408//===----------------------------------------------------------------------===//
409// ValueTable External Functions
410//===----------------------------------------------------------------------===//
411
Owen Anderson6a903bc2008-06-18 21:41:49 +0000412/// add - Insert a value into the table with a specified value number.
413void ValueTable::add(Value* V, uint32_t num) {
414 valueNumbering.insert(std::make_pair(V, num));
415}
416
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000417/// lookup_or_add - Returns the value number for the specified value, assigning
418/// it a new number if it did not have one before.
419uint32_t ValueTable::lookup_or_add(Value* V) {
420 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
421 if (VI != valueNumbering.end())
422 return VI->second;
423
Owen Anderson09b83ba2007-10-18 19:39:33 +0000424 if (CallInst* C = dyn_cast<CallInst>(V)) {
Owen Anderson1e73f292008-04-11 05:11:49 +0000425 if (AA->doesNotAccessMemory(C)) {
Owen Anderson09b83ba2007-10-18 19:39:33 +0000426 Expression e = create_expression(C);
427
428 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
429 if (EI != expressionNumbering.end()) {
430 valueNumbering.insert(std::make_pair(V, EI->second));
431 return EI->second;
432 } else {
433 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
434 valueNumbering.insert(std::make_pair(V, nextValueNumber));
435
436 return nextValueNumber++;
437 }
Owen Andersonf9ae76d2008-04-17 05:36:50 +0000438 } else if (AA->onlyReadsMemory(C)) {
439 Expression e = create_expression(C);
440
Owen Anderson69057b82008-05-13 08:17:22 +0000441 if (expressionNumbering.find(e) == expressionNumbering.end()) {
Owen Andersonf9ae76d2008-04-17 05:36:50 +0000442 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
443 valueNumbering.insert(std::make_pair(V, nextValueNumber));
Owen Anderson69057b82008-05-13 08:17:22 +0000444 return nextValueNumber++;
445 }
Owen Andersonf9ae76d2008-04-17 05:36:50 +0000446
Chris Lattner7f9c8a02008-11-29 02:29:27 +0000447 MemDepResult local_dep = MD->getDependency(C);
Owen Anderson17816b32008-05-13 23:18:30 +0000448
Chris Lattner0e3d6332008-12-05 21:04:20 +0000449 if (!local_dep.isDef() && !local_dep.isNonLocal()) {
Owen Anderson17816b32008-05-13 23:18:30 +0000450 valueNumbering.insert(std::make_pair(V, nextValueNumber));
451 return nextValueNumber++;
Chris Lattner80c7d812008-11-30 23:39:23 +0000452 }
Chris Lattner0e3d6332008-12-05 21:04:20 +0000453
454 if (local_dep.isDef()) {
455 CallInst* local_cdep = cast<CallInst>(local_dep.getInst());
456
457 if (local_cdep->getNumOperands() != C->getNumOperands()) {
Owen Anderson17816b32008-05-13 23:18:30 +0000458 valueNumbering.insert(std::make_pair(V, nextValueNumber));
459 return nextValueNumber++;
460 }
Chris Lattner0e3d6332008-12-05 21:04:20 +0000461
Chris Lattner80c7d812008-11-30 23:39:23 +0000462 for (unsigned i = 1; i < C->getNumOperands(); ++i) {
463 uint32_t c_vn = lookup_or_add(C->getOperand(i));
464 uint32_t cd_vn = lookup_or_add(local_cdep->getOperand(i));
465 if (c_vn != cd_vn) {
466 valueNumbering.insert(std::make_pair(V, nextValueNumber));
467 return nextValueNumber++;
468 }
469 }
470
471 uint32_t v = lookup_or_add(local_cdep);
472 valueNumbering.insert(std::make_pair(V, v));
473 return v;
Owen Anderson17816b32008-05-13 23:18:30 +0000474 }
Chris Lattner7e61daf2008-12-01 01:15:42 +0000475
Chris Lattner0e3d6332008-12-05 21:04:20 +0000476 // Non-local case.
Chris Lattner7e61daf2008-12-01 01:15:42 +0000477 const MemoryDependenceAnalysis::NonLocalDepInfo &deps =
Chris Lattner254314e2008-12-09 19:38:05 +0000478 MD->getNonLocalCallDependency(CallSite(C));
Chris Lattner0e3d6332008-12-05 21:04:20 +0000479 // FIXME: call/call dependencies for readonly calls should return def, not
480 // clobber! Move the checking logic to MemDep!
Owen Anderson8c2391d2008-05-13 13:41:23 +0000481 CallInst* cdep = 0;
Owen Anderson69057b82008-05-13 08:17:22 +0000482
Chris Lattner80c7d812008-11-30 23:39:23 +0000483 // Check to see if we have a single dominating call instruction that is
484 // identical to C.
Chris Lattner7e61daf2008-12-01 01:15:42 +0000485 for (unsigned i = 0, e = deps.size(); i != e; ++i) {
486 const MemoryDependenceAnalysis::NonLocalDepEntry *I = &deps[i];
Chris Lattner80c7d812008-11-30 23:39:23 +0000487 // Ignore non-local dependencies.
488 if (I->second.isNonLocal())
489 continue;
Owen Anderson69057b82008-05-13 08:17:22 +0000490
Chris Lattner80c7d812008-11-30 23:39:23 +0000491 // We don't handle non-depedencies. If we already have a call, reject
492 // instruction dependencies.
Chris Lattner0e3d6332008-12-05 21:04:20 +0000493 if (I->second.isClobber() || cdep != 0) {
Chris Lattner80c7d812008-11-30 23:39:23 +0000494 cdep = 0;
495 break;
Owen Anderson69057b82008-05-13 08:17:22 +0000496 }
Chris Lattner80c7d812008-11-30 23:39:23 +0000497
498 CallInst *NonLocalDepCall = dyn_cast<CallInst>(I->second.getInst());
499 // FIXME: All duplicated with non-local case.
500 if (NonLocalDepCall && DT->properlyDominates(I->first, C->getParent())){
501 cdep = NonLocalDepCall;
502 continue;
503 }
504
505 cdep = 0;
506 break;
Owen Anderson69057b82008-05-13 08:17:22 +0000507 }
508
Owen Anderson8c2391d2008-05-13 13:41:23 +0000509 if (!cdep) {
Owen Anderson69057b82008-05-13 08:17:22 +0000510 valueNumbering.insert(std::make_pair(V, nextValueNumber));
Owen Andersonf9ae76d2008-04-17 05:36:50 +0000511 return nextValueNumber++;
512 }
513
Chris Lattner0e3d6332008-12-05 21:04:20 +0000514 if (cdep->getNumOperands() != C->getNumOperands()) {
Owen Anderson69057b82008-05-13 08:17:22 +0000515 valueNumbering.insert(std::make_pair(V, nextValueNumber));
Owen Andersonf9ae76d2008-04-17 05:36:50 +0000516 return nextValueNumber++;
Owen Andersonf9ae76d2008-04-17 05:36:50 +0000517 }
Chris Lattner80c7d812008-11-30 23:39:23 +0000518 for (unsigned i = 1; i < C->getNumOperands(); ++i) {
519 uint32_t c_vn = lookup_or_add(C->getOperand(i));
520 uint32_t cd_vn = lookup_or_add(cdep->getOperand(i));
521 if (c_vn != cd_vn) {
522 valueNumbering.insert(std::make_pair(V, nextValueNumber));
523 return nextValueNumber++;
524 }
525 }
526
527 uint32_t v = lookup_or_add(cdep);
528 valueNumbering.insert(std::make_pair(V, v));
529 return v;
Owen Andersonf9ae76d2008-04-17 05:36:50 +0000530
Owen Anderson09b83ba2007-10-18 19:39:33 +0000531 } else {
532 valueNumbering.insert(std::make_pair(V, nextValueNumber));
533 return nextValueNumber++;
534 }
535 } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(V)) {
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000536 Expression e = create_expression(BO);
537
538 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
539 if (EI != expressionNumbering.end()) {
540 valueNumbering.insert(std::make_pair(V, EI->second));
541 return EI->second;
542 } else {
543 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
544 valueNumbering.insert(std::make_pair(V, nextValueNumber));
545
546 return nextValueNumber++;
547 }
548 } else if (CmpInst* C = dyn_cast<CmpInst>(V)) {
549 Expression e = create_expression(C);
550
551 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
552 if (EI != expressionNumbering.end()) {
553 valueNumbering.insert(std::make_pair(V, EI->second));
554 return EI->second;
555 } else {
556 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
557 valueNumbering.insert(std::make_pair(V, nextValueNumber));
558
559 return nextValueNumber++;
560 }
561 } else if (ShuffleVectorInst* U = dyn_cast<ShuffleVectorInst>(V)) {
562 Expression e = create_expression(U);
563
564 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
565 if (EI != expressionNumbering.end()) {
566 valueNumbering.insert(std::make_pair(V, EI->second));
567 return EI->second;
568 } else {
569 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
570 valueNumbering.insert(std::make_pair(V, nextValueNumber));
571
572 return nextValueNumber++;
573 }
574 } else if (ExtractElementInst* U = dyn_cast<ExtractElementInst>(V)) {
575 Expression e = create_expression(U);
576
577 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
578 if (EI != expressionNumbering.end()) {
579 valueNumbering.insert(std::make_pair(V, EI->second));
580 return EI->second;
581 } else {
582 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
583 valueNumbering.insert(std::make_pair(V, nextValueNumber));
584
585 return nextValueNumber++;
586 }
587 } else if (InsertElementInst* U = dyn_cast<InsertElementInst>(V)) {
588 Expression e = create_expression(U);
589
590 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
591 if (EI != expressionNumbering.end()) {
592 valueNumbering.insert(std::make_pair(V, EI->second));
593 return EI->second;
594 } else {
595 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
596 valueNumbering.insert(std::make_pair(V, nextValueNumber));
597
598 return nextValueNumber++;
599 }
600 } else if (SelectInst* U = dyn_cast<SelectInst>(V)) {
601 Expression e = create_expression(U);
602
603 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
604 if (EI != expressionNumbering.end()) {
605 valueNumbering.insert(std::make_pair(V, EI->second));
606 return EI->second;
607 } else {
608 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
609 valueNumbering.insert(std::make_pair(V, nextValueNumber));
610
611 return nextValueNumber++;
612 }
613 } else if (CastInst* U = dyn_cast<CastInst>(V)) {
614 Expression e = create_expression(U);
615
616 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
617 if (EI != expressionNumbering.end()) {
618 valueNumbering.insert(std::make_pair(V, EI->second));
619 return EI->second;
620 } else {
621 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
622 valueNumbering.insert(std::make_pair(V, nextValueNumber));
623
624 return nextValueNumber++;
625 }
626 } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(V)) {
627 Expression e = create_expression(U);
628
629 DenseMap<Expression, uint32_t>::iterator EI = expressionNumbering.find(e);
630 if (EI != expressionNumbering.end()) {
631 valueNumbering.insert(std::make_pair(V, EI->second));
632 return EI->second;
633 } else {
634 expressionNumbering.insert(std::make_pair(e, nextValueNumber));
635 valueNumbering.insert(std::make_pair(V, nextValueNumber));
636
637 return nextValueNumber++;
638 }
639 } else {
640 valueNumbering.insert(std::make_pair(V, nextValueNumber));
641 return nextValueNumber++;
642 }
643}
644
645/// lookup - Returns the value number of the specified value. Fails if
646/// the value has not yet been numbered.
647uint32_t ValueTable::lookup(Value* V) const {
648 DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
Chris Lattner2876a642008-03-21 21:14:38 +0000649 assert(VI != valueNumbering.end() && "Value not numbered?");
650 return VI->second;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000651}
652
653/// clear - Remove all entries from the ValueTable
654void ValueTable::clear() {
655 valueNumbering.clear();
656 expressionNumbering.clear();
657 nextValueNumber = 1;
658}
659
Owen Anderson10ffa862007-07-31 23:27:13 +0000660/// erase - Remove a value from the value numbering
661void ValueTable::erase(Value* V) {
662 valueNumbering.erase(V);
663}
664
Bill Wendling6b18a392008-12-22 21:36:08 +0000665/// verifyRemoved - Verify that the value is removed from all internal data
666/// structures.
667void ValueTable::verifyRemoved(const Value *V) const {
668 for (DenseMap<Value*, uint32_t>::iterator
669 I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
670 assert(I->first != V && "Inst still occurs in value numbering map!");
671 }
672}
673
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000674//===----------------------------------------------------------------------===//
Bill Wendling456e8852008-12-22 22:32:22 +0000675// GVN Pass
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000676//===----------------------------------------------------------------------===//
677
678namespace {
Owen Anderson1b3ea962008-06-20 01:15:47 +0000679 struct VISIBILITY_HIDDEN ValueNumberScope {
680 ValueNumberScope* parent;
681 DenseMap<uint32_t, Value*> table;
682
683 ValueNumberScope(ValueNumberScope* p) : parent(p) { }
684 };
685}
686
687namespace {
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000688
689 class VISIBILITY_HIDDEN GVN : public FunctionPass {
690 bool runOnFunction(Function &F);
691 public:
692 static char ID; // Pass identification, replacement for typeid
Dan Gohmana79db302008-09-04 17:05:41 +0000693 GVN() : FunctionPass(&ID) { }
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000694
695 private:
Chris Lattner8541ede2008-12-01 00:40:32 +0000696 MemoryDependenceAnalysis *MD;
697 DominatorTree *DT;
698
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000699 ValueTable VN;
Owen Anderson1b3ea962008-06-20 01:15:47 +0000700 DenseMap<BasicBlock*, ValueNumberScope*> localAvail;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000701
Owen Anderson0cc1a762007-08-07 23:12:31 +0000702 typedef DenseMap<Value*, SmallPtrSet<Instruction*, 4> > PhiMapType;
703 PhiMapType phiMap;
704
705
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000706 // This transformation requires dominator postdominator info
707 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000708 AU.addRequired<DominatorTree>();
709 AU.addRequired<MemoryDependenceAnalysis>();
Owen Anderson09b83ba2007-10-18 19:39:33 +0000710 AU.addRequired<AliasAnalysis>();
Owen Anderson54e02192008-06-23 17:49:45 +0000711
712 AU.addPreserved<DominatorTree>();
Owen Anderson09b83ba2007-10-18 19:39:33 +0000713 AU.addPreserved<AliasAnalysis>();
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000714 }
715
716 // Helper fuctions
717 // FIXME: eliminate or document these better
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000718 bool processLoad(LoadInst* L,
Chris Lattner804209d2008-03-21 22:01:16 +0000719 SmallVectorImpl<Instruction*> &toErase);
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000720 bool processInstruction(Instruction* I,
Chris Lattner804209d2008-03-21 22:01:16 +0000721 SmallVectorImpl<Instruction*> &toErase);
Owen Anderson9699a6e2007-08-02 18:16:06 +0000722 bool processNonLocalLoad(LoadInst* L,
Chris Lattner804209d2008-03-21 22:01:16 +0000723 SmallVectorImpl<Instruction*> &toErase);
Owen Andersonbfe133e2008-12-15 02:03:00 +0000724 bool processBlock(BasicBlock* BB);
Owen Andersone34c2392008-12-14 19:10:35 +0000725 Value *GetValueForBlock(BasicBlock *BB, Instruction* orig,
Owen Anderson0ac1fc82007-08-02 17:56:05 +0000726 DenseMap<BasicBlock*, Value*> &Phis,
727 bool top_level = false);
Owen Anderson6a903bc2008-06-18 21:41:49 +0000728 void dump(DenseMap<uint32_t, Value*>& d);
Owen Anderson676070d2007-08-14 18:04:11 +0000729 bool iterateOnFunction(Function &F);
Owen Andersonf5023a72007-08-16 22:51:56 +0000730 Value* CollapsePhi(PHINode* p);
Owen Anderson4cd516b2007-09-16 08:04:16 +0000731 bool isSafeReplacement(PHINode* p, Instruction* inst);
Owen Anderson6a903bc2008-06-18 21:41:49 +0000732 bool performPRE(Function& F);
Owen Anderson1b3ea962008-06-20 01:15:47 +0000733 Value* lookupNumber(BasicBlock* BB, uint32_t num);
Owen Anderson53d546e2008-07-15 16:28:06 +0000734 bool mergeBlockIntoPredecessor(BasicBlock* BB);
Owen Andersonbfe133e2008-12-15 02:03:00 +0000735 Value* AttemptRedundancyElimination(Instruction* orig, unsigned valno);
Nuno Lopese3127f32008-10-10 16:25:50 +0000736 void cleanupGlobalSets();
Bill Wendling6b18a392008-12-22 21:36:08 +0000737 void verifyRemoved(const Instruction *I) const;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000738 };
739
740 char GVN::ID = 0;
Owen Andersonab6ec2e2007-07-24 17:55:58 +0000741}
742
743// createGVNPass - The public interface to this file...
744FunctionPass *llvm::createGVNPass() { return new GVN(); }
745
746static RegisterPass<GVN> X("gvn",
747 "Global Value Numbering");
748
Owen Anderson6a903bc2008-06-18 21:41:49 +0000749void GVN::dump(DenseMap<uint32_t, Value*>& d) {
Owen Anderson5e5599b2007-07-25 19:57:03 +0000750 printf("{\n");
Owen Anderson6a903bc2008-06-18 21:41:49 +0000751 for (DenseMap<uint32_t, Value*>::iterator I = d.begin(),
Owen Anderson5e5599b2007-07-25 19:57:03 +0000752 E = d.end(); I != E; ++I) {
Owen Anderson6a903bc2008-06-18 21:41:49 +0000753 printf("%d\n", I->first);
Owen Anderson5e5599b2007-07-25 19:57:03 +0000754 I->second->dump();
755 }
756 printf("}\n");
757}
758
Owen Andersonf5023a72007-08-16 22:51:56 +0000759Value* GVN::CollapsePhi(PHINode* p) {
Owen Andersonf5023a72007-08-16 22:51:56 +0000760 Value* constVal = p->hasConstantValue();
Chris Lattner2876a642008-03-21 21:14:38 +0000761 if (!constVal) return 0;
Owen Andersonf5023a72007-08-16 22:51:56 +0000762
Chris Lattner2876a642008-03-21 21:14:38 +0000763 Instruction* inst = dyn_cast<Instruction>(constVal);
764 if (!inst)
765 return constVal;
766
Chris Lattner8541ede2008-12-01 00:40:32 +0000767 if (DT->dominates(inst, p))
Chris Lattner2876a642008-03-21 21:14:38 +0000768 if (isSafeReplacement(p, inst))
769 return inst;
Owen Andersonf5023a72007-08-16 22:51:56 +0000770 return 0;
771}
Owen Anderson5e5599b2007-07-25 19:57:03 +0000772
Owen Anderson4cd516b2007-09-16 08:04:16 +0000773bool GVN::isSafeReplacement(PHINode* p, Instruction* inst) {
774 if (!isa<PHINode>(inst))
775 return true;
776
777 for (Instruction::use_iterator UI = p->use_begin(), E = p->use_end();
778 UI != E; ++UI)
779 if (PHINode* use_phi = dyn_cast<PHINode>(UI))
780 if (use_phi->getParent() == inst->getParent())
781 return false;
782
783 return true;
784}
785
Owen Andersondbf23cc2007-07-26 18:26:51 +0000786/// GetValueForBlock - Get the value to use within the specified basic block.
787/// available values are in Phis.
Owen Andersone34c2392008-12-14 19:10:35 +0000788Value *GVN::GetValueForBlock(BasicBlock *BB, Instruction* orig,
Chris Lattner2876a642008-03-21 21:14:38 +0000789 DenseMap<BasicBlock*, Value*> &Phis,
790 bool top_level) {
Owen Andersondbf23cc2007-07-26 18:26:51 +0000791
792 // If we have already computed this value, return the previously computed val.
Owen Anderson2d19aae2007-08-03 19:59:35 +0000793 DenseMap<BasicBlock*, Value*>::iterator V = Phis.find(BB);
794 if (V != Phis.end() && !top_level) return V->second;
Owen Andersondbf23cc2007-07-26 18:26:51 +0000795
Owen Anderson6acc7822008-07-02 18:15:31 +0000796 // If the block is unreachable, just return undef, since this path
797 // can't actually occur at runtime.
Chris Lattner8541ede2008-12-01 00:40:32 +0000798 if (!DT->isReachableFromEntry(BB))
Owen Andersonb5618da2009-07-03 00:17:18 +0000799 return Phis[BB] = Context->getUndef(orig->getType());
Owen Andersonb22a6402008-07-02 17:20:16 +0000800
Chris Lattner0a5a8d52008-12-09 19:21:47 +0000801 if (BasicBlock *Pred = BB->getSinglePredecessor()) {
802 Value *ret = GetValueForBlock(Pred, orig, Phis);
Owen Anderson2d19aae2007-08-03 19:59:35 +0000803 Phis[BB] = ret;
804 return ret;
Owen Anderson774761c2007-08-03 11:03:26 +0000805 }
Chris Lattner0a5a8d52008-12-09 19:21:47 +0000806
807 // Get the number of predecessors of this block so we can reserve space later.
808 // If there is already a PHI in it, use the #preds from it, otherwise count.
809 // Getting it from the PHI is constant time.
810 unsigned NumPreds;
811 if (PHINode *ExistingPN = dyn_cast<PHINode>(BB->begin()))
812 NumPreds = ExistingPN->getNumIncomingValues();
813 else
814 NumPreds = std::distance(pred_begin(BB), pred_end(BB));
Chris Lattner2876a642008-03-21 21:14:38 +0000815
Owen Andersondbf23cc2007-07-26 18:26:51 +0000816 // Otherwise, the idom is the loop, so we need to insert a PHI node. Do so
817 // now, then get values to fill in the incoming values for the PHI.
Gabor Greife9ecc682008-04-06 20:25:17 +0000818 PHINode *PN = PHINode::Create(orig->getType(), orig->getName()+".rle",
819 BB->begin());
Chris Lattner0a5a8d52008-12-09 19:21:47 +0000820 PN->reserveOperandSpace(NumPreds);
Owen Anderson2d19aae2007-08-03 19:59:35 +0000821
Chris Lattner0a5a8d52008-12-09 19:21:47 +0000822 Phis.insert(std::make_pair(BB, PN));
Owen Andersond66e2852007-07-30 16:57:08 +0000823
Owen Andersondbf23cc2007-07-26 18:26:51 +0000824 // Fill in the incoming values for the block.
Owen Andersond58fa6b02007-07-31 17:43:14 +0000825 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
826 Value* val = GetValueForBlock(*PI, orig, Phis);
Owen Andersond58fa6b02007-07-31 17:43:14 +0000827 PN->addIncoming(val, *PI);
828 }
Chris Lattner2876a642008-03-21 21:14:38 +0000829
Chris Lattner8541ede2008-12-01 00:40:32 +0000830 VN.getAliasAnalysis()->copyValue(orig, PN);
Owen Andersond58fa6b02007-07-31 17:43:14 +0000831
Owen Anderson221a4362007-08-16 22:02:55 +0000832 // Attempt to collapse PHI nodes that are trivially redundant
Owen Andersonf5023a72007-08-16 22:51:56 +0000833 Value* v = CollapsePhi(PN);
Chris Lattner2876a642008-03-21 21:14:38 +0000834 if (!v) {
835 // Cache our phi construction results
Owen Andersone34c2392008-12-14 19:10:35 +0000836 if (LoadInst* L = dyn_cast<LoadInst>(orig))
837 phiMap[L->getPointerOperand()].insert(PN);
838 else
839 phiMap[orig].insert(PN);
840
Chris Lattner2876a642008-03-21 21:14:38 +0000841 return PN;
Owen Andersond58fa6b02007-07-31 17:43:14 +0000842 }
Owen Andersonf7928602008-05-12 20:15:55 +0000843
Chris Lattner2876a642008-03-21 21:14:38 +0000844 PN->replaceAllUsesWith(v);
Chris Lattnerfa9f99a2008-12-09 22:06:23 +0000845 if (isa<PointerType>(v->getType()))
846 MD->invalidateCachedPointerInfo(v);
Chris Lattner2876a642008-03-21 21:14:38 +0000847
848 for (DenseMap<BasicBlock*, Value*>::iterator I = Phis.begin(),
849 E = Phis.end(); I != E; ++I)
850 if (I->second == PN)
851 I->second = v;
852
Chris Lattner8541ede2008-12-01 00:40:32 +0000853 DEBUG(cerr << "GVN removed: " << *PN);
854 MD->removeInstruction(PN);
Chris Lattner2876a642008-03-21 21:14:38 +0000855 PN->eraseFromParent();
Bill Wendling6b18a392008-12-22 21:36:08 +0000856 DEBUG(verifyRemoved(PN));
Chris Lattner2876a642008-03-21 21:14:38 +0000857
858 Phis[BB] = v;
859 return v;
Owen Anderson5e5599b2007-07-25 19:57:03 +0000860}
861
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000862/// IsValueFullyAvailableInBlock - Return true if we can prove that the value
863/// we're analyzing is fully available in the specified block. As we go, keep
Chris Lattnerd2a653a2008-12-05 07:49:08 +0000864/// track of which blocks we know are fully alive in FullyAvailableBlocks. This
865/// map is actually a tri-state map with the following values:
866/// 0) we know the block *is not* fully available.
867/// 1) we know the block *is* fully available.
868/// 2) we do not know whether the block is fully available or not, but we are
869/// currently speculating that it will be.
870/// 3) we are speculating for this block and have used that to speculate for
871/// other blocks.
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000872static bool IsValueFullyAvailableInBlock(BasicBlock *BB,
Chris Lattnerd2a653a2008-12-05 07:49:08 +0000873 DenseMap<BasicBlock*, char> &FullyAvailableBlocks) {
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000874 // Optimistically assume that the block is fully available and check to see
875 // if we already know about this block in one lookup.
Chris Lattnerd2a653a2008-12-05 07:49:08 +0000876 std::pair<DenseMap<BasicBlock*, char>::iterator, char> IV =
877 FullyAvailableBlocks.insert(std::make_pair(BB, 2));
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000878
879 // If the entry already existed for this block, return the precomputed value.
Chris Lattnerd2a653a2008-12-05 07:49:08 +0000880 if (!IV.second) {
881 // If this is a speculative "available" value, mark it as being used for
882 // speculation of other blocks.
883 if (IV.first->second == 2)
884 IV.first->second = 3;
885 return IV.first->second != 0;
886 }
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000887
888 // Otherwise, see if it is fully available in all predecessors.
889 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
890
891 // If this block has no predecessors, it isn't live-in here.
892 if (PI == PE)
Chris Lattnerd2a653a2008-12-05 07:49:08 +0000893 goto SpeculationFailure;
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000894
895 for (; PI != PE; ++PI)
896 // If the value isn't fully available in one of our predecessors, then it
897 // isn't fully available in this block either. Undo our previous
898 // optimistic assumption and bail out.
899 if (!IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks))
Chris Lattnerd2a653a2008-12-05 07:49:08 +0000900 goto SpeculationFailure;
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000901
902 return true;
Chris Lattnerd2a653a2008-12-05 07:49:08 +0000903
904// SpeculationFailure - If we get here, we found out that this is not, after
905// all, a fully-available block. We have a problem if we speculated on this and
906// used the speculation to mark other blocks as available.
907SpeculationFailure:
908 char &BBVal = FullyAvailableBlocks[BB];
909
910 // If we didn't speculate on this, just return with it set to false.
911 if (BBVal == 2) {
912 BBVal = 0;
913 return false;
914 }
915
916 // If we did speculate on this value, we could have blocks set to 1 that are
917 // incorrect. Walk the (transitive) successors of this block and mark them as
918 // 0 if set to one.
919 SmallVector<BasicBlock*, 32> BBWorklist;
920 BBWorklist.push_back(BB);
921
922 while (!BBWorklist.empty()) {
923 BasicBlock *Entry = BBWorklist.pop_back_val();
924 // Note that this sets blocks to 0 (unavailable) if they happen to not
925 // already be in FullyAvailableBlocks. This is safe.
926 char &EntryVal = FullyAvailableBlocks[Entry];
927 if (EntryVal == 0) continue; // Already unavailable.
928
929 // Mark as unavailable.
930 EntryVal = 0;
931
932 for (succ_iterator I = succ_begin(Entry), E = succ_end(Entry); I != E; ++I)
933 BBWorklist.push_back(*I);
934 }
935
936 return false;
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000937}
938
Owen Anderson221a4362007-08-16 22:02:55 +0000939/// processNonLocalLoad - Attempt to eliminate a load whose dependencies are
940/// non-local by performing PHI construction.
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000941bool GVN::processNonLocalLoad(LoadInst *LI,
Chris Lattner804209d2008-03-21 22:01:16 +0000942 SmallVectorImpl<Instruction*> &toErase) {
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000943 // Find the non-local dependencies of the load.
Chris Lattnerb6fc4b82008-12-09 19:25:07 +0000944 SmallVector<MemoryDependenceAnalysis::NonLocalDepEntry, 64> Deps;
945 MD->getNonLocalPointerDependency(LI->getOperand(0), true, LI->getParent(),
946 Deps);
947 //DEBUG(cerr << "INVESTIGATING NONLOCAL LOAD: " << Deps.size() << *LI);
Owen Anderson5e5599b2007-07-25 19:57:03 +0000948
Owen Andersonb39e0de2008-08-26 22:07:42 +0000949 // If we had to process more than one hundred blocks to find the
950 // dependencies, this load isn't worth worrying about. Optimizing
951 // it will be too expensive.
Chris Lattnerb6fc4b82008-12-09 19:25:07 +0000952 if (Deps.size() > 100)
Owen Andersonb39e0de2008-08-26 22:07:42 +0000953 return false;
Chris Lattnerb6372932008-12-18 00:51:32 +0000954
955 // If we had a phi translation failure, we'll have a single entry which is a
956 // clobber in the current block. Reject this early.
Torok Edwinba93ea72009-06-17 18:48:18 +0000957 if (Deps.size() == 1 && Deps[0].second.isClobber()) {
958 DEBUG(
959 DOUT << "GVN: non-local load ";
960 WriteAsOperand(*DOUT.stream(), LI);
961 DOUT << " is clobbered by " << *Deps[0].second.getInst();
962 );
Chris Lattnerb6372932008-12-18 00:51:32 +0000963 return false;
Torok Edwinba93ea72009-06-17 18:48:18 +0000964 }
Owen Andersonb39e0de2008-08-26 22:07:42 +0000965
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000966 // Filter out useless results (non-locals, etc). Keep track of the blocks
967 // where we have a value available in repl, also keep track of whether we see
968 // dependencies that produce an unknown value for the load (such as a call
969 // that could potentially clobber the load).
970 SmallVector<std::pair<BasicBlock*, Value*>, 16> ValuesPerBlock;
971 SmallVector<BasicBlock*, 16> UnavailableBlocks;
Owen Anderson0cc1a762007-08-07 23:12:31 +0000972
Chris Lattnerb6fc4b82008-12-09 19:25:07 +0000973 for (unsigned i = 0, e = Deps.size(); i != e; ++i) {
974 BasicBlock *DepBB = Deps[i].first;
975 MemDepResult DepInfo = Deps[i].second;
Chris Lattner7e61daf2008-12-01 01:15:42 +0000976
Chris Lattner0e3d6332008-12-05 21:04:20 +0000977 if (DepInfo.isClobber()) {
978 UnavailableBlocks.push_back(DepBB);
979 continue;
980 }
981
982 Instruction *DepInst = DepInfo.getInst();
983
984 // Loading the allocation -> undef.
985 if (isa<AllocationInst>(DepInst)) {
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000986 ValuesPerBlock.push_back(std::make_pair(DepBB,
Owen Andersonb5618da2009-07-03 00:17:18 +0000987 Context->getUndef(LI->getType())));
Chris Lattner7e61daf2008-12-01 01:15:42 +0000988 continue;
989 }
Chris Lattner2876a642008-03-21 21:14:38 +0000990
Chris Lattnerb6fc4b82008-12-09 19:25:07 +0000991 if (StoreInst* S = dyn_cast<StoreInst>(DepInst)) {
Chris Lattner9ce89952008-12-01 01:31:36 +0000992 // Reject loads and stores that are to the same address but are of
993 // different types.
994 // NOTE: 403.gcc does have this case (e.g. in readonly_fields_p) because
995 // of bitfield access, it would be interesting to optimize for it at some
996 // point.
Chris Lattner1db9bbe2008-12-02 08:16:11 +0000997 if (S->getOperand(0)->getType() != LI->getType()) {
998 UnavailableBlocks.push_back(DepBB);
999 continue;
1000 }
Chris Lattner9ce89952008-12-01 01:31:36 +00001001
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001002 ValuesPerBlock.push_back(std::make_pair(DepBB, S->getOperand(0)));
Chris Lattner9ce89952008-12-01 01:31:36 +00001003
Chris Lattnerb6fc4b82008-12-09 19:25:07 +00001004 } else if (LoadInst* LD = dyn_cast<LoadInst>(DepInst)) {
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001005 if (LD->getType() != LI->getType()) {
1006 UnavailableBlocks.push_back(DepBB);
1007 continue;
1008 }
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001009 ValuesPerBlock.push_back(std::make_pair(DepBB, LD));
Owen Anderson5e5599b2007-07-25 19:57:03 +00001010 } else {
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001011 UnavailableBlocks.push_back(DepBB);
1012 continue;
Owen Anderson5e5599b2007-07-25 19:57:03 +00001013 }
Chris Lattner2876a642008-03-21 21:14:38 +00001014 }
Owen Anderson5e5599b2007-07-25 19:57:03 +00001015
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001016 // If we have no predecessors that produce a known value for this load, exit
1017 // early.
1018 if (ValuesPerBlock.empty()) return false;
1019
1020 // If all of the instructions we depend on produce a known value for this
1021 // load, then it is fully redundant and we can use PHI insertion to compute
1022 // its value. Insert PHIs and remove the fully redundant value now.
1023 if (UnavailableBlocks.empty()) {
1024 // Use cached PHI construction information from previous runs
1025 SmallPtrSet<Instruction*, 4> &p = phiMap[LI->getPointerOperand()];
Chris Lattnerb6fc4b82008-12-09 19:25:07 +00001026 // FIXME: What does phiMap do? Are we positive it isn't getting invalidated?
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001027 for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
1028 I != E; ++I) {
1029 if ((*I)->getParent() == LI->getParent()) {
1030 DEBUG(cerr << "GVN REMOVING NONLOCAL LOAD #1: " << *LI);
1031 LI->replaceAllUsesWith(*I);
Chris Lattnerfa9f99a2008-12-09 22:06:23 +00001032 if (isa<PointerType>((*I)->getType()))
1033 MD->invalidateCachedPointerInfo(*I);
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001034 toErase.push_back(LI);
1035 NumGVNLoad++;
1036 return true;
1037 }
1038
1039 ValuesPerBlock.push_back(std::make_pair((*I)->getParent(), *I));
Owen Anderson0cc1a762007-08-07 23:12:31 +00001040 }
Chris Lattner2876a642008-03-21 21:14:38 +00001041
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001042 DEBUG(cerr << "GVN REMOVING NONLOCAL LOAD: " << *LI);
1043
1044 DenseMap<BasicBlock*, Value*> BlockReplValues;
1045 BlockReplValues.insert(ValuesPerBlock.begin(), ValuesPerBlock.end());
1046 // Perform PHI construction.
1047 Value* v = GetValueForBlock(LI->getParent(), LI, BlockReplValues, true);
1048 LI->replaceAllUsesWith(v);
Chris Lattner69131fd2008-12-15 03:46:38 +00001049
Chris Lattner096f44d2009-02-12 07:00:35 +00001050 if (isa<PHINode>(v))
Chris Lattner69131fd2008-12-15 03:46:38 +00001051 v->takeName(LI);
Chris Lattnerfa9f99a2008-12-09 22:06:23 +00001052 if (isa<PointerType>(v->getType()))
1053 MD->invalidateCachedPointerInfo(v);
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001054 toErase.push_back(LI);
1055 NumGVNLoad++;
1056 return true;
1057 }
1058
1059 if (!EnablePRE || !EnableLoadPRE)
1060 return false;
1061
1062 // Okay, we have *some* definitions of the value. This means that the value
1063 // is available in some of our (transitive) predecessors. Lets think about
1064 // doing PRE of this load. This will involve inserting a new load into the
1065 // predecessor when it's not available. We could do this in general, but
1066 // prefer to not increase code size. As such, we only do this when we know
1067 // that we only have to insert *one* load (which means we're basically moving
1068 // the load, not inserting a new one).
1069
Owen Andersoncc0c75c2009-05-31 09:03:40 +00001070 SmallPtrSet<BasicBlock *, 4> Blockers;
1071 for (unsigned i = 0, e = UnavailableBlocks.size(); i != e; ++i)
1072 Blockers.insert(UnavailableBlocks[i]);
1073
1074 // Lets find first basic block with more than one predecessor. Walk backwards
1075 // through predecessors if needed.
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001076 BasicBlock *LoadBB = LI->getParent();
Owen Andersoncc0c75c2009-05-31 09:03:40 +00001077 BasicBlock *TmpBB = LoadBB;
1078
1079 bool isSinglePred = false;
Dale Johannesen81b64632009-06-17 20:48:23 +00001080 bool allSingleSucc = true;
Owen Andersoncc0c75c2009-05-31 09:03:40 +00001081 while (TmpBB->getSinglePredecessor()) {
1082 isSinglePred = true;
1083 TmpBB = TmpBB->getSinglePredecessor();
1084 if (!TmpBB) // If haven't found any, bail now.
1085 return false;
1086 if (TmpBB == LoadBB) // Infinite (unreachable) loop.
1087 return false;
1088 if (Blockers.count(TmpBB))
1089 return false;
Dale Johannesen81b64632009-06-17 20:48:23 +00001090 if (TmpBB->getTerminator()->getNumSuccessors() != 1)
1091 allSingleSucc = false;
Owen Andersoncc0c75c2009-05-31 09:03:40 +00001092 }
1093
1094 assert(TmpBB);
1095 LoadBB = TmpBB;
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001096
1097 // If we have a repl set with LI itself in it, this means we have a loop where
1098 // at least one of the values is LI. Since this means that we won't be able
1099 // to eliminate LI even if we insert uses in the other predecessors, we will
1100 // end up increasing code size. Reject this by scanning for LI.
1101 for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i)
1102 if (ValuesPerBlock[i].second == LI)
1103 return false;
1104
Owen Andersoncc0c75c2009-05-31 09:03:40 +00001105 if (isSinglePred) {
1106 bool isHot = false;
1107 for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i)
1108 if (Instruction *I = dyn_cast<Instruction>(ValuesPerBlock[i].second))
1109 // "Hot" Instruction is in some loop (because it dominates its dep.
1110 // instruction).
1111 if (DT->dominates(LI, I)) {
1112 isHot = true;
1113 break;
1114 }
1115
1116 // We are interested only in "hot" instructions. We don't want to do any
1117 // mis-optimizations here.
1118 if (!isHot)
1119 return false;
1120 }
1121
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001122 // Okay, we have some hope :). Check to see if the loaded value is fully
1123 // available in all but one predecessor.
1124 // FIXME: If we could restructure the CFG, we could make a common pred with
1125 // all the preds that don't have an available LI and insert a new load into
1126 // that one block.
1127 BasicBlock *UnavailablePred = 0;
1128
Chris Lattnerd2a653a2008-12-05 07:49:08 +00001129 DenseMap<BasicBlock*, char> FullyAvailableBlocks;
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001130 for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i)
1131 FullyAvailableBlocks[ValuesPerBlock[i].first] = true;
1132 for (unsigned i = 0, e = UnavailableBlocks.size(); i != e; ++i)
1133 FullyAvailableBlocks[UnavailableBlocks[i]] = false;
1134
1135 for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
1136 PI != E; ++PI) {
1137 if (IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks))
1138 continue;
1139
1140 // If this load is not available in multiple predecessors, reject it.
1141 if (UnavailablePred && UnavailablePred != *PI)
1142 return false;
1143 UnavailablePred = *PI;
1144 }
1145
1146 assert(UnavailablePred != 0 &&
1147 "Fully available value should be eliminated above!");
1148
1149 // If the loaded pointer is PHI node defined in this block, do PHI translation
1150 // to get its value in the predecessor.
1151 Value *LoadPtr = LI->getOperand(0)->DoPHITranslation(LoadBB, UnavailablePred);
1152
1153 // Make sure the value is live in the predecessor. If it was defined by a
1154 // non-PHI instruction in this block, we don't know how to recompute it above.
1155 if (Instruction *LPInst = dyn_cast<Instruction>(LoadPtr))
1156 if (!DT->dominates(LPInst->getParent(), UnavailablePred)) {
1157 DEBUG(cerr << "COULDN'T PRE LOAD BECAUSE PTR IS UNAVAILABLE IN PRED: "
1158 << *LPInst << *LI << "\n");
1159 return false;
1160 }
1161
1162 // We don't currently handle critical edges :(
1163 if (UnavailablePred->getTerminator()->getNumSuccessors() != 1) {
1164 DEBUG(cerr << "COULD NOT PRE LOAD BECAUSE OF CRITICAL EDGE '"
1165 << UnavailablePred->getName() << "': " << *LI);
1166 return false;
Owen Anderson0cc1a762007-08-07 23:12:31 +00001167 }
Dale Johannesen81b64632009-06-17 20:48:23 +00001168
1169 // Make sure it is valid to move this load here. We have to watch out for:
1170 // @1 = getelementptr (i8* p, ...
1171 // test p and branch if == 0
1172 // load @1
1173 // It is valid to have the getelementptr before the test, even if p can be 0,
1174 // as getelementptr only does address arithmetic.
1175 // If we are not pushing the value through any multiple-successor blocks
1176 // we do not have this case. Otherwise, check that the load is safe to
1177 // put anywhere; this can be improved, but should be conservatively safe.
1178 if (!allSingleSucc &&
1179 !isSafeToLoadUnconditionally(LoadPtr, UnavailablePred->getTerminator()))
1180 return false;
1181
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001182 // Okay, we can eliminate this load by inserting a reload in the predecessor
1183 // and using PHI construction to get the value in the other predecessors, do
1184 // it.
Chris Lattnerc1008282008-12-05 17:04:12 +00001185 DEBUG(cerr << "GVN REMOVING PRE LOAD: " << *LI);
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001186
1187 Value *NewLoad = new LoadInst(LoadPtr, LI->getName()+".pre", false,
1188 LI->getAlignment(),
1189 UnavailablePred->getTerminator());
1190
Owen Anderson04cfdd32009-05-29 05:37:54 +00001191 SmallPtrSet<Instruction*, 4> &p = phiMap[LI->getPointerOperand()];
1192 for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
1193 I != E; ++I)
1194 ValuesPerBlock.push_back(std::make_pair((*I)->getParent(), *I));
1195
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001196 DenseMap<BasicBlock*, Value*> BlockReplValues;
1197 BlockReplValues.insert(ValuesPerBlock.begin(), ValuesPerBlock.end());
1198 BlockReplValues[UnavailablePred] = NewLoad;
1199
1200 // Perform PHI construction.
1201 Value* v = GetValueForBlock(LI->getParent(), LI, BlockReplValues, true);
1202 LI->replaceAllUsesWith(v);
Chris Lattner096f44d2009-02-12 07:00:35 +00001203 if (isa<PHINode>(v))
Chris Lattner69131fd2008-12-15 03:46:38 +00001204 v->takeName(LI);
Chris Lattnerfa9f99a2008-12-09 22:06:23 +00001205 if (isa<PointerType>(v->getType()))
1206 MD->invalidateCachedPointerInfo(v);
Chris Lattner1db9bbe2008-12-02 08:16:11 +00001207 toErase.push_back(LI);
1208 NumPRELoad++;
Owen Anderson5e5599b2007-07-25 19:57:03 +00001209 return true;
1210}
1211
Owen Anderson221a4362007-08-16 22:02:55 +00001212/// processLoad - Attempt to eliminate a load, first by eliminating it
1213/// locally, and then attempting non-local elimination if that fails.
Chris Lattner0e3d6332008-12-05 21:04:20 +00001214bool GVN::processLoad(LoadInst *L, SmallVectorImpl<Instruction*> &toErase) {
1215 if (L->isVolatile())
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001216 return false;
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001217
1218 Value* pointer = L->getPointerOperand();
Chris Lattner0e3d6332008-12-05 21:04:20 +00001219
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001220 // ... to a pointer that has been loaded from before...
Chris Lattner8541ede2008-12-01 00:40:32 +00001221 MemDepResult dep = MD->getDependency(L);
Owen Andersona7b220f2007-08-14 17:59:48 +00001222
Chris Lattner0e3d6332008-12-05 21:04:20 +00001223 // If the value isn't available, don't do anything!
Torok Edwin72070282009-05-29 09:46:03 +00001224 if (dep.isClobber()) {
1225 DEBUG(
1226 // fast print dep, using operator<< on instruction would be too slow
1227 DOUT << "GVN: load ";
1228 WriteAsOperand(*DOUT.stream(), L);
1229 Instruction *I = dep.getInst();
Torok Edwin0b0ddb22009-05-29 16:58:36 +00001230 DOUT << " is clobbered by " << *I;
Torok Edwin72070282009-05-29 09:46:03 +00001231 );
Chris Lattner0e3d6332008-12-05 21:04:20 +00001232 return false;
Torok Edwin72070282009-05-29 09:46:03 +00001233 }
Chris Lattner0e3d6332008-12-05 21:04:20 +00001234
1235 // If it is defined in another block, try harder.
Chris Lattner0a5a8d52008-12-09 19:21:47 +00001236 if (dep.isNonLocal())
Chris Lattner0e3d6332008-12-05 21:04:20 +00001237 return processNonLocalLoad(L, toErase);
Eli Friedman716c10c2008-02-12 12:08:14 +00001238
Chris Lattner0e3d6332008-12-05 21:04:20 +00001239 Instruction *DepInst = dep.getInst();
1240 if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
1241 // Only forward substitute stores to loads of the same type.
1242 // FIXME: Could do better!
1243 if (DepSI->getPointerOperand()->getType() != pointer->getType())
1244 return false;
1245
1246 // Remove it!
1247 L->replaceAllUsesWith(DepSI->getOperand(0));
Chris Lattnerfa9f99a2008-12-09 22:06:23 +00001248 if (isa<PointerType>(DepSI->getOperand(0)->getType()))
1249 MD->invalidateCachedPointerInfo(DepSI->getOperand(0));
Chris Lattner0e3d6332008-12-05 21:04:20 +00001250 toErase.push_back(L);
1251 NumGVNLoad++;
1252 return true;
1253 }
1254
1255 if (LoadInst *DepLI = dyn_cast<LoadInst>(DepInst)) {
1256 // Only forward substitute stores to loads of the same type.
1257 // FIXME: Could do better! load i32 -> load i8 -> truncate on little endian.
1258 if (DepLI->getType() != L->getType())
1259 return false;
1260
1261 // Remove it!
1262 L->replaceAllUsesWith(DepLI);
Chris Lattnerfa9f99a2008-12-09 22:06:23 +00001263 if (isa<PointerType>(DepLI->getType()))
1264 MD->invalidateCachedPointerInfo(DepLI);
Chris Lattner0e3d6332008-12-05 21:04:20 +00001265 toErase.push_back(L);
1266 NumGVNLoad++;
1267 return true;
1268 }
1269
Chris Lattner3ff6d012008-11-30 01:39:32 +00001270 // If this load really doesn't depend on anything, then we must be loading an
1271 // undef value. This can happen when loading for a fresh allocation with no
1272 // intervening stores, for example.
Chris Lattner0e3d6332008-12-05 21:04:20 +00001273 if (isa<AllocationInst>(DepInst)) {
Owen Andersonb5618da2009-07-03 00:17:18 +00001274 L->replaceAllUsesWith(Context->getUndef(L->getType()));
Chris Lattner3ff6d012008-11-30 01:39:32 +00001275 toErase.push_back(L);
Chris Lattner3ff6d012008-11-30 01:39:32 +00001276 NumGVNLoad++;
Chris Lattner0e3d6332008-12-05 21:04:20 +00001277 return true;
Eli Friedman716c10c2008-02-12 12:08:14 +00001278 }
1279
Chris Lattner0e3d6332008-12-05 21:04:20 +00001280 return false;
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001281}
1282
Owen Anderson1b3ea962008-06-20 01:15:47 +00001283Value* GVN::lookupNumber(BasicBlock* BB, uint32_t num) {
Owen Anderson54e02192008-06-23 17:49:45 +00001284 DenseMap<BasicBlock*, ValueNumberScope*>::iterator I = localAvail.find(BB);
1285 if (I == localAvail.end())
1286 return 0;
1287
1288 ValueNumberScope* locals = I->second;
Owen Anderson1b3ea962008-06-20 01:15:47 +00001289
1290 while (locals) {
1291 DenseMap<uint32_t, Value*>::iterator I = locals->table.find(num);
1292 if (I != locals->table.end())
1293 return I->second;
1294 else
1295 locals = locals->parent;
1296 }
1297
1298 return 0;
1299}
1300
Owen Andersonbfe133e2008-12-15 02:03:00 +00001301/// AttemptRedundancyElimination - If the "fast path" of redundancy elimination
1302/// by inheritance from the dominator fails, see if we can perform phi
1303/// construction to eliminate the redundancy.
1304Value* GVN::AttemptRedundancyElimination(Instruction* orig, unsigned valno) {
1305 BasicBlock* BaseBlock = orig->getParent();
1306
1307 SmallPtrSet<BasicBlock*, 4> Visited;
1308 SmallVector<BasicBlock*, 8> Stack;
1309 Stack.push_back(BaseBlock);
1310
1311 DenseMap<BasicBlock*, Value*> Results;
1312
1313 // Walk backwards through our predecessors, looking for instances of the
1314 // value number we're looking for. Instances are recorded in the Results
1315 // map, which is then used to perform phi construction.
1316 while (!Stack.empty()) {
1317 BasicBlock* Current = Stack.back();
1318 Stack.pop_back();
1319
1320 // If we've walked all the way to a proper dominator, then give up. Cases
1321 // where the instance is in the dominator will have been caught by the fast
1322 // path, and any cases that require phi construction further than this are
1323 // probably not worth it anyways. Note that this is a SIGNIFICANT compile
1324 // time improvement.
1325 if (DT->properlyDominates(Current, orig->getParent())) return 0;
1326
1327 DenseMap<BasicBlock*, ValueNumberScope*>::iterator LA =
1328 localAvail.find(Current);
1329 if (LA == localAvail.end()) return 0;
Chris Lattner73d7fe52009-01-19 22:00:18 +00001330 DenseMap<uint32_t, Value*>::iterator V = LA->second->table.find(valno);
Owen Andersonbfe133e2008-12-15 02:03:00 +00001331
1332 if (V != LA->second->table.end()) {
1333 // Found an instance, record it.
1334 Results.insert(std::make_pair(Current, V->second));
1335 continue;
1336 }
1337
1338 // If we reach the beginning of the function, then give up.
1339 if (pred_begin(Current) == pred_end(Current))
1340 return 0;
1341
1342 for (pred_iterator PI = pred_begin(Current), PE = pred_end(Current);
1343 PI != PE; ++PI)
1344 if (Visited.insert(*PI))
1345 Stack.push_back(*PI);
1346 }
1347
1348 // If we didn't find instances, give up. Otherwise, perform phi construction.
1349 if (Results.size() == 0)
1350 return 0;
1351 else
1352 return GetValueForBlock(BaseBlock, orig, Results, true);
1353}
1354
Owen Anderson398602a2007-08-14 18:16:29 +00001355/// processInstruction - When calculating availability, handle an instruction
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001356/// by inserting it into the appropriate sets
Owen Andersonaccdca12008-06-12 19:25:32 +00001357bool GVN::processInstruction(Instruction *I,
Chris Lattner804209d2008-03-21 22:01:16 +00001358 SmallVectorImpl<Instruction*> &toErase) {
Owen Anderson6a903bc2008-06-18 21:41:49 +00001359 if (LoadInst* L = dyn_cast<LoadInst>(I)) {
Chris Lattner0e3d6332008-12-05 21:04:20 +00001360 bool changed = processLoad(L, toErase);
Owen Anderson6a903bc2008-06-18 21:41:49 +00001361
1362 if (!changed) {
1363 unsigned num = VN.lookup_or_add(L);
Owen Anderson1b3ea962008-06-20 01:15:47 +00001364 localAvail[I->getParent()]->table.insert(std::make_pair(num, L));
Owen Anderson6a903bc2008-06-18 21:41:49 +00001365 }
1366
1367 return changed;
1368 }
1369
Owen Anderson3ea90a72008-07-03 17:44:33 +00001370 uint32_t nextNum = VN.getNextUnusedValueNumber();
Owen Anderson6a903bc2008-06-18 21:41:49 +00001371 unsigned num = VN.lookup_or_add(I);
Chris Lattner804209d2008-03-21 22:01:16 +00001372
Owen Anderson98f912b2009-04-01 23:53:49 +00001373 if (BranchInst* BI = dyn_cast<BranchInst>(I)) {
1374 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
1375
1376 if (!BI->isConditional() || isa<Constant>(BI->getCondition()))
1377 return false;
1378
1379 Value* branchCond = BI->getCondition();
1380 uint32_t condVN = VN.lookup_or_add(branchCond);
1381
1382 BasicBlock* trueSucc = BI->getSuccessor(0);
1383 BasicBlock* falseSucc = BI->getSuccessor(1);
1384
1385 if (trueSucc->getSinglePredecessor())
Owen Andersonb5618da2009-07-03 00:17:18 +00001386 localAvail[trueSucc]->table[condVN] = Context->getConstantIntTrue();
Owen Anderson98f912b2009-04-01 23:53:49 +00001387 if (falseSucc->getSinglePredecessor())
Owen Andersonb5618da2009-07-03 00:17:18 +00001388 localAvail[falseSucc]->table[condVN] = Context->getConstantIntFalse();
Owen Anderson98f912b2009-04-01 23:53:49 +00001389
1390 return false;
1391
Owen Anderson0c1e6342008-04-07 09:59:07 +00001392 // Allocations are always uniquely numbered, so we can save time and memory
Owen Anderson98f912b2009-04-01 23:53:49 +00001393 // by fast failing them.
1394 } else if (isa<AllocationInst>(I) || isa<TerminatorInst>(I)) {
Owen Anderson1b3ea962008-06-20 01:15:47 +00001395 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
Owen Anderson0c1e6342008-04-07 09:59:07 +00001396 return false;
Owen Anderson6a903bc2008-06-18 21:41:49 +00001397 }
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001398
Owen Anderson221a4362007-08-16 22:02:55 +00001399 // Collapse PHI nodes
Owen Andersonbc271a02007-08-14 18:33:27 +00001400 if (PHINode* p = dyn_cast<PHINode>(I)) {
Owen Andersonf5023a72007-08-16 22:51:56 +00001401 Value* constVal = CollapsePhi(p);
Owen Andersonbc271a02007-08-14 18:33:27 +00001402
1403 if (constVal) {
Owen Andersonf5023a72007-08-16 22:51:56 +00001404 for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end();
1405 PI != PE; ++PI)
Chris Lattner0a5a8d52008-12-09 19:21:47 +00001406 PI->second.erase(p);
Owen Andersonbc271a02007-08-14 18:33:27 +00001407
Owen Andersonf5023a72007-08-16 22:51:56 +00001408 p->replaceAllUsesWith(constVal);
Chris Lattnerfa9f99a2008-12-09 22:06:23 +00001409 if (isa<PointerType>(constVal->getType()))
1410 MD->invalidateCachedPointerInfo(constVal);
Owen Anderson164274e2008-12-23 00:49:51 +00001411 VN.erase(p);
1412
Owen Andersonf5023a72007-08-16 22:51:56 +00001413 toErase.push_back(p);
Owen Anderson6a903bc2008-06-18 21:41:49 +00001414 } else {
Owen Anderson1b3ea962008-06-20 01:15:47 +00001415 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
Owen Andersonbc271a02007-08-14 18:33:27 +00001416 }
Owen Anderson3ea90a72008-07-03 17:44:33 +00001417
1418 // If the number we were assigned was a brand new VN, then we don't
1419 // need to do a lookup to see if the number already exists
1420 // somewhere in the domtree: it can't!
1421 } else if (num == nextNum) {
1422 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
1423
Owen Andersonbfe133e2008-12-15 02:03:00 +00001424 // Perform fast-path value-number based elimination of values inherited from
1425 // dominators.
Owen Anderson1b3ea962008-06-20 01:15:47 +00001426 } else if (Value* repl = lookupNumber(I->getParent(), num)) {
Owen Anderson086b2c42007-12-08 01:37:09 +00001427 // Remove it!
Owen Anderson10ffa862007-07-31 23:27:13 +00001428 VN.erase(I);
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001429 I->replaceAllUsesWith(repl);
Chris Lattnerfa9f99a2008-12-09 22:06:23 +00001430 if (isa<PointerType>(repl->getType()))
1431 MD->invalidateCachedPointerInfo(repl);
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001432 toErase.push_back(I);
1433 return true;
Owen Andersonbfe133e2008-12-15 02:03:00 +00001434
1435#if 0
1436 // Perform slow-pathvalue-number based elimination with phi construction.
1437 } else if (Value* repl = AttemptRedundancyElimination(I, num)) {
1438 // Remove it!
1439 VN.erase(I);
1440 I->replaceAllUsesWith(repl);
1441 if (isa<PointerType>(repl->getType()))
1442 MD->invalidateCachedPointerInfo(repl);
1443 toErase.push_back(I);
1444 return true;
1445#endif
Owen Anderson3ea90a72008-07-03 17:44:33 +00001446 } else {
Owen Anderson1b3ea962008-06-20 01:15:47 +00001447 localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001448 }
1449
1450 return false;
1451}
1452
Bill Wendling456e8852008-12-22 22:32:22 +00001453/// runOnFunction - This is the main transformation entry point for a function.
Owen Anderson676070d2007-08-14 18:04:11 +00001454bool GVN::runOnFunction(Function& F) {
Chris Lattner8541ede2008-12-01 00:40:32 +00001455 MD = &getAnalysis<MemoryDependenceAnalysis>();
1456 DT = &getAnalysis<DominatorTree>();
Owen Andersonf7928602008-05-12 20:15:55 +00001457 VN.setAliasAnalysis(&getAnalysis<AliasAnalysis>());
Chris Lattner8541ede2008-12-01 00:40:32 +00001458 VN.setMemDep(MD);
1459 VN.setDomTree(DT);
Owen Anderson09b83ba2007-10-18 19:39:33 +00001460
Owen Anderson676070d2007-08-14 18:04:11 +00001461 bool changed = false;
1462 bool shouldContinue = true;
1463
Owen Andersonac310962008-07-16 17:52:31 +00001464 // Merge unconditional branches, allowing PRE to catch more
1465 // optimization opportunities.
1466 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) {
1467 BasicBlock* BB = FI;
1468 ++FI;
Owen Andersonc0623812008-07-17 00:01:40 +00001469 bool removedBlock = MergeBlockIntoPredecessor(BB, this);
1470 if (removedBlock) NumGVNBlocks++;
1471
1472 changed |= removedBlock;
Owen Andersonac310962008-07-16 17:52:31 +00001473 }
1474
Chris Lattner0a5a8d52008-12-09 19:21:47 +00001475 unsigned Iteration = 0;
1476
Owen Anderson676070d2007-08-14 18:04:11 +00001477 while (shouldContinue) {
Chris Lattner0a5a8d52008-12-09 19:21:47 +00001478 DEBUG(cerr << "GVN iteration: " << Iteration << "\n");
Owen Anderson676070d2007-08-14 18:04:11 +00001479 shouldContinue = iterateOnFunction(F);
1480 changed |= shouldContinue;
Chris Lattner0a5a8d52008-12-09 19:21:47 +00001481 ++Iteration;
Owen Anderson676070d2007-08-14 18:04:11 +00001482 }
1483
Owen Anderson04a6e0b2008-07-18 18:03:38 +00001484 if (EnablePRE) {
Owen Anderson2fbfb702008-09-03 23:06:07 +00001485 bool PREChanged = true;
1486 while (PREChanged) {
1487 PREChanged = performPRE(F);
Owen Anderson04a6e0b2008-07-18 18:03:38 +00001488 changed |= PREChanged;
Owen Anderson2fbfb702008-09-03 23:06:07 +00001489 }
Owen Anderson04a6e0b2008-07-18 18:03:38 +00001490 }
Chris Lattner0a5a8d52008-12-09 19:21:47 +00001491 // FIXME: Should perform GVN again after PRE does something. PRE can move
1492 // computations into blocks where they become fully redundant. Note that
1493 // we can't do this until PRE's critical edge splitting updates memdep.
1494 // Actually, when this happens, we should just fully integrate PRE into GVN.
Nuno Lopese3127f32008-10-10 16:25:50 +00001495
1496 cleanupGlobalSets();
1497
Owen Anderson676070d2007-08-14 18:04:11 +00001498 return changed;
1499}
1500
1501
Owen Andersonbfe133e2008-12-15 02:03:00 +00001502bool GVN::processBlock(BasicBlock* BB) {
Chris Lattner0a5a8d52008-12-09 19:21:47 +00001503 // FIXME: Kill off toErase by doing erasing eagerly in a helper function (and
1504 // incrementing BI before processing an instruction).
Owen Andersonaccdca12008-06-12 19:25:32 +00001505 SmallVector<Instruction*, 8> toErase;
Owen Andersonaccdca12008-06-12 19:25:32 +00001506 bool changed_function = false;
Owen Anderson6a903bc2008-06-18 21:41:49 +00001507
Owen Andersonaccdca12008-06-12 19:25:32 +00001508 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
1509 BI != BE;) {
Chris Lattner0e3d6332008-12-05 21:04:20 +00001510 changed_function |= processInstruction(BI, toErase);
Owen Andersonaccdca12008-06-12 19:25:32 +00001511 if (toErase.empty()) {
1512 ++BI;
1513 continue;
1514 }
1515
1516 // If we need some instructions deleted, do it now.
1517 NumGVNInstr += toErase.size();
1518
1519 // Avoid iterator invalidation.
1520 bool AtStart = BI == BB->begin();
1521 if (!AtStart)
1522 --BI;
1523
1524 for (SmallVector<Instruction*, 4>::iterator I = toErase.begin(),
Chris Lattner8541ede2008-12-01 00:40:32 +00001525 E = toErase.end(); I != E; ++I) {
1526 DEBUG(cerr << "GVN removed: " << **I);
1527 MD->removeInstruction(*I);
Owen Andersonaccdca12008-06-12 19:25:32 +00001528 (*I)->eraseFromParent();
Bill Wendlingebb6a542008-12-22 21:57:30 +00001529 DEBUG(verifyRemoved(*I));
Chris Lattner8541ede2008-12-01 00:40:32 +00001530 }
Chris Lattner0a5a8d52008-12-09 19:21:47 +00001531 toErase.clear();
Owen Andersonaccdca12008-06-12 19:25:32 +00001532
1533 if (AtStart)
1534 BI = BB->begin();
1535 else
1536 ++BI;
Owen Andersonaccdca12008-06-12 19:25:32 +00001537 }
1538
Owen Andersonaccdca12008-06-12 19:25:32 +00001539 return changed_function;
1540}
1541
Owen Anderson6a903bc2008-06-18 21:41:49 +00001542/// performPRE - Perform a purely local form of PRE that looks for diamond
1543/// control flow patterns and attempts to perform simple PRE at the join point.
1544bool GVN::performPRE(Function& F) {
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001545 bool Changed = false;
Owen Andersonfdf9f162008-06-19 19:54:19 +00001546 SmallVector<std::pair<TerminatorInst*, unsigned>, 4> toSplit;
Chris Lattnerf00aae42008-12-01 07:29:03 +00001547 DenseMap<BasicBlock*, Value*> predMap;
Owen Anderson6a903bc2008-06-18 21:41:49 +00001548 for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
1549 DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) {
1550 BasicBlock* CurrentBlock = *DI;
1551
1552 // Nothing to PRE in the entry block.
1553 if (CurrentBlock == &F.getEntryBlock()) continue;
1554
1555 for (BasicBlock::iterator BI = CurrentBlock->begin(),
1556 BE = CurrentBlock->end(); BI != BE; ) {
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001557 Instruction *CurInst = BI++;
Duncan Sands1efabaa2009-05-06 06:49:50 +00001558
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001559 if (isa<AllocationInst>(CurInst) || isa<TerminatorInst>(CurInst) ||
John Criswell073e4d12009-03-10 15:04:53 +00001560 isa<PHINode>(CurInst) || (CurInst->getType() == Type::VoidTy) ||
Duncan Sands1efabaa2009-05-06 06:49:50 +00001561 CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
John Criswell073e4d12009-03-10 15:04:53 +00001562 isa<DbgInfoIntrinsic>(CurInst))
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001563 continue;
Duncan Sands1efabaa2009-05-06 06:49:50 +00001564
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001565 uint32_t valno = VN.lookup(CurInst);
Owen Anderson6a903bc2008-06-18 21:41:49 +00001566
1567 // Look for the predecessors for PRE opportunities. We're
1568 // only trying to solve the basic diamond case, where
1569 // a value is computed in the successor and one predecessor,
1570 // but not the other. We also explicitly disallow cases
1571 // where the successor is its own predecessor, because they're
1572 // more complicated to get right.
1573 unsigned numWith = 0;
1574 unsigned numWithout = 0;
1575 BasicBlock* PREPred = 0;
Chris Lattnerf00aae42008-12-01 07:29:03 +00001576 predMap.clear();
1577
Owen Anderson6a903bc2008-06-18 21:41:49 +00001578 for (pred_iterator PI = pred_begin(CurrentBlock),
1579 PE = pred_end(CurrentBlock); PI != PE; ++PI) {
1580 // We're not interested in PRE where the block is its
Owen Anderson1b3ea962008-06-20 01:15:47 +00001581 // own predecessor, on in blocks with predecessors
1582 // that are not reachable.
1583 if (*PI == CurrentBlock) {
Owen Anderson6a903bc2008-06-18 21:41:49 +00001584 numWithout = 2;
Owen Anderson1b3ea962008-06-20 01:15:47 +00001585 break;
1586 } else if (!localAvail.count(*PI)) {
1587 numWithout = 2;
1588 break;
1589 }
1590
1591 DenseMap<uint32_t, Value*>::iterator predV =
1592 localAvail[*PI]->table.find(valno);
1593 if (predV == localAvail[*PI]->table.end()) {
Owen Anderson6a903bc2008-06-18 21:41:49 +00001594 PREPred = *PI;
1595 numWithout++;
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001596 } else if (predV->second == CurInst) {
Owen Anderson6a903bc2008-06-18 21:41:49 +00001597 numWithout = 2;
1598 } else {
Owen Anderson1b3ea962008-06-20 01:15:47 +00001599 predMap[*PI] = predV->second;
Owen Anderson6a903bc2008-06-18 21:41:49 +00001600 numWith++;
1601 }
1602 }
1603
1604 // Don't do PRE when it might increase code size, i.e. when
1605 // we would need to insert instructions in more than one pred.
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001606 if (numWithout != 1 || numWith == 0)
Owen Anderson6a903bc2008-06-18 21:41:49 +00001607 continue;
Owen Anderson6a903bc2008-06-18 21:41:49 +00001608
Owen Andersonfdf9f162008-06-19 19:54:19 +00001609 // We can't do PRE safely on a critical edge, so instead we schedule
1610 // the edge to be split and perform the PRE the next time we iterate
1611 // on the function.
1612 unsigned succNum = 0;
1613 for (unsigned i = 0, e = PREPred->getTerminator()->getNumSuccessors();
1614 i != e; ++i)
Owen Anderson2fbfb702008-09-03 23:06:07 +00001615 if (PREPred->getTerminator()->getSuccessor(i) == CurrentBlock) {
Owen Andersonfdf9f162008-06-19 19:54:19 +00001616 succNum = i;
1617 break;
1618 }
1619
1620 if (isCriticalEdge(PREPred->getTerminator(), succNum)) {
1621 toSplit.push_back(std::make_pair(PREPred->getTerminator(), succNum));
Owen Andersonfdf9f162008-06-19 19:54:19 +00001622 continue;
1623 }
1624
Owen Anderson6a903bc2008-06-18 21:41:49 +00001625 // Instantiate the expression the in predecessor that lacked it.
1626 // Because we are going top-down through the block, all value numbers
1627 // will be available in the predecessor by the time we need them. Any
1628 // that weren't original present will have been instantiated earlier
1629 // in this loop.
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001630 Instruction* PREInstr = CurInst->clone();
Owen Anderson6a903bc2008-06-18 21:41:49 +00001631 bool success = true;
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001632 for (unsigned i = 0, e = CurInst->getNumOperands(); i != e; ++i) {
1633 Value *Op = PREInstr->getOperand(i);
1634 if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
1635 continue;
1636
1637 if (Value *V = lookupNumber(PREPred, VN.lookup(Op))) {
1638 PREInstr->setOperand(i, V);
1639 } else {
1640 success = false;
1641 break;
Owen Anderson8e462e92008-07-11 20:05:13 +00001642 }
Owen Anderson6a903bc2008-06-18 21:41:49 +00001643 }
1644
1645 // Fail out if we encounter an operand that is not available in
1646 // the PRE predecessor. This is typically because of loads which
1647 // are not value numbered precisely.
1648 if (!success) {
1649 delete PREInstr;
Bill Wendling3c793442008-12-22 22:14:07 +00001650 DEBUG(verifyRemoved(PREInstr));
Owen Anderson6a903bc2008-06-18 21:41:49 +00001651 continue;
1652 }
1653
1654 PREInstr->insertBefore(PREPred->getTerminator());
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001655 PREInstr->setName(CurInst->getName() + ".pre");
Owen Anderson1b3ea962008-06-20 01:15:47 +00001656 predMap[PREPred] = PREInstr;
Owen Anderson6a903bc2008-06-18 21:41:49 +00001657 VN.add(PREInstr, valno);
1658 NumGVNPRE++;
1659
1660 // Update the availability map to include the new instruction.
Owen Anderson1b3ea962008-06-20 01:15:47 +00001661 localAvail[PREPred]->table.insert(std::make_pair(valno, PREInstr));
Owen Anderson6a903bc2008-06-18 21:41:49 +00001662
1663 // Create a PHI to make the value available in this block.
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001664 PHINode* Phi = PHINode::Create(CurInst->getType(),
1665 CurInst->getName() + ".pre-phi",
Owen Anderson6a903bc2008-06-18 21:41:49 +00001666 CurrentBlock->begin());
1667 for (pred_iterator PI = pred_begin(CurrentBlock),
1668 PE = pred_end(CurrentBlock); PI != PE; ++PI)
Owen Anderson1b3ea962008-06-20 01:15:47 +00001669 Phi->addIncoming(predMap[*PI], *PI);
Owen Anderson6a903bc2008-06-18 21:41:49 +00001670
1671 VN.add(Phi, valno);
Owen Anderson1b3ea962008-06-20 01:15:47 +00001672 localAvail[CurrentBlock]->table[valno] = Phi;
Owen Anderson6a903bc2008-06-18 21:41:49 +00001673
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001674 CurInst->replaceAllUsesWith(Phi);
Chris Lattnerfa9f99a2008-12-09 22:06:23 +00001675 if (isa<PointerType>(Phi->getType()))
1676 MD->invalidateCachedPointerInfo(Phi);
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001677 VN.erase(CurInst);
Owen Anderson6a903bc2008-06-18 21:41:49 +00001678
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001679 DEBUG(cerr << "GVN PRE removed: " << *CurInst);
1680 MD->removeInstruction(CurInst);
1681 CurInst->eraseFromParent();
Bill Wendlingebb6a542008-12-22 21:57:30 +00001682 DEBUG(verifyRemoved(CurInst));
Chris Lattner6f5bf6a2008-12-01 07:35:54 +00001683 Changed = true;
Owen Anderson6a903bc2008-06-18 21:41:49 +00001684 }
1685 }
1686
Owen Andersonfdf9f162008-06-19 19:54:19 +00001687 for (SmallVector<std::pair<TerminatorInst*, unsigned>, 4>::iterator
Anton Korobeynikov24600bf2008-12-05 19:38:49 +00001688 I = toSplit.begin(), E = toSplit.end(); I != E; ++I)
Owen Andersonfdf9f162008-06-19 19:54:19 +00001689 SplitCriticalEdge(I->first, I->second, this);
1690
Anton Korobeynikov24600bf2008-12-05 19:38:49 +00001691 return Changed || toSplit.size();
Owen Anderson6a903bc2008-06-18 21:41:49 +00001692}
1693
Bill Wendling456e8852008-12-22 22:32:22 +00001694/// iterateOnFunction - Executes one iteration of GVN
Owen Anderson676070d2007-08-14 18:04:11 +00001695bool GVN::iterateOnFunction(Function &F) {
Nuno Lopese3127f32008-10-10 16:25:50 +00001696 cleanupGlobalSets();
Chris Lattnerbeb216d2008-03-21 21:33:23 +00001697
Owen Anderson98f912b2009-04-01 23:53:49 +00001698 for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
1699 DE = df_end(DT->getRootNode()); DI != DE; ++DI) {
1700 if (DI->getIDom())
1701 localAvail[DI->getBlock()] =
1702 new ValueNumberScope(localAvail[DI->getIDom()->getBlock()]);
1703 else
1704 localAvail[DI->getBlock()] = new ValueNumberScope(0);
1705 }
1706
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001707 // Top-down walk of the dominator tree
Owen Anderson6a903bc2008-06-18 21:41:49 +00001708 bool changed = false;
Owen Anderson03aacba2008-12-15 03:52:17 +00001709#if 0
1710 // Needed for value numbering with phi construction to work.
Owen Andersonbfe133e2008-12-15 02:03:00 +00001711 ReversePostOrderTraversal<Function*> RPOT(&F);
1712 for (ReversePostOrderTraversal<Function*>::rpo_iterator RI = RPOT.begin(),
1713 RE = RPOT.end(); RI != RE; ++RI)
1714 changed |= processBlock(*RI);
Owen Anderson03aacba2008-12-15 03:52:17 +00001715#else
1716 for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
1717 DE = df_end(DT->getRootNode()); DI != DE; ++DI)
1718 changed |= processBlock(DI->getBlock());
1719#endif
1720
Owen Andersonac310962008-07-16 17:52:31 +00001721 return changed;
Owen Andersonab6ec2e2007-07-24 17:55:58 +00001722}
Nuno Lopese3127f32008-10-10 16:25:50 +00001723
1724void GVN::cleanupGlobalSets() {
1725 VN.clear();
1726 phiMap.clear();
1727
1728 for (DenseMap<BasicBlock*, ValueNumberScope*>::iterator
1729 I = localAvail.begin(), E = localAvail.end(); I != E; ++I)
1730 delete I->second;
1731 localAvail.clear();
1732}
Bill Wendling6b18a392008-12-22 21:36:08 +00001733
1734/// verifyRemoved - Verify that the specified instruction does not occur in our
1735/// internal data structures.
Bill Wendlinge7f08e72008-12-22 22:28:56 +00001736void GVN::verifyRemoved(const Instruction *Inst) const {
1737 VN.verifyRemoved(Inst);
Bill Wendling3c793442008-12-22 22:14:07 +00001738
1739 // Walk through the PHI map to make sure the instruction isn't hiding in there
1740 // somewhere.
1741 for (PhiMapType::iterator
Bill Wendlinge7f08e72008-12-22 22:28:56 +00001742 I = phiMap.begin(), E = phiMap.end(); I != E; ++I) {
1743 assert(I->first != Inst && "Inst is still a key in PHI map!");
Bill Wendling3c793442008-12-22 22:14:07 +00001744
1745 for (SmallPtrSet<Instruction*, 4>::iterator
Bill Wendlinge7f08e72008-12-22 22:28:56 +00001746 II = I->second.begin(), IE = I->second.end(); II != IE; ++II) {
1747 assert(*II != Inst && "Inst is still a value in PHI map!");
1748 }
1749 }
1750
1751 // Walk through the value number scope to make sure the instruction isn't
1752 // ferreted away in it.
1753 for (DenseMap<BasicBlock*, ValueNumberScope*>::iterator
1754 I = localAvail.begin(), E = localAvail.end(); I != E; ++I) {
1755 const ValueNumberScope *VNS = I->second;
1756
1757 while (VNS) {
1758 for (DenseMap<uint32_t, Value*>::iterator
1759 II = VNS->table.begin(), IE = VNS->table.end(); II != IE; ++II) {
1760 assert(II->second != Inst && "Inst still in value numbering scope!");
1761 }
1762
1763 VNS = VNS->parent;
Bill Wendling3c793442008-12-22 22:14:07 +00001764 }
1765 }
Bill Wendling6b18a392008-12-22 21:36:08 +00001766}