blob: f7040b03e170d40ba638dd751ce3d4e6f482d543 [file] [log] [blame]
Owen Anderson5fba6c12007-05-29 21:53:49 +00001//===- GVNPRE.cpp - Eliminate redundant values and expressions ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the Owen Anderson and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass performs a hybrid of global value numbering and partial redundancy
11// elimination, known as GVN-PRE. It performs partial redundancy elimination on
12// values, rather than lexical expressions, allowing a more comprehensive view
13// the optimization. It replaces redundant values with uses of earlier
14// occurences of the same value. While this is beneficial in that it eliminates
15// unneeded computation, it also increases register pressure by creating large
16// live ranges, and should be used with caution on platforms that a very
17// sensitive to register pressure.
18//
19//===----------------------------------------------------------------------===//
20
21#define DEBUG_TYPE "gvnpre"
22#include "llvm/Value.h"
23#include "llvm/Transforms/Scalar.h"
24#include "llvm/Instructions.h"
25#include "llvm/Function.h"
26#include "llvm/Analysis/Dominators.h"
27#include "llvm/Analysis/PostDominators.h"
28#include "llvm/ADT/DepthFirstIterator.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/Support/Compiler.h"
Owen Anderson4a6ec8f2007-05-29 23:15:21 +000031#include "llvm/Support/Debug.h"
Owen Anderson5fba6c12007-05-29 21:53:49 +000032#include <algorithm>
Owen Anderson331bf6a2007-05-31 22:44:11 +000033#include <deque>
Owen Anderson5fba6c12007-05-29 21:53:49 +000034#include <map>
Owen Anderson331bf6a2007-05-31 22:44:11 +000035#include <vector>
Owen Anderson5fba6c12007-05-29 21:53:49 +000036#include <set>
Owen Anderson5fba6c12007-05-29 21:53:49 +000037using namespace llvm;
38
39namespace {
40
41 class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
42 bool runOnFunction(Function &F);
43 public:
44 static char ID; // Pass identification, replacement for typeid
45 GVNPRE() : FunctionPass((intptr_t)&ID) { nextValueNumber = 0; }
46
47 private:
48 uint32_t nextValueNumber;
49
50 struct Expression {
51 char opcode;
52 Value* value;
53 uint32_t lhs;
54 uint32_t rhs;
55
56 bool operator<(const Expression& other) const {
57 if (opcode < other.opcode)
58 return true;
59 else if (other.opcode < opcode)
60 return false;
61
62 if (opcode == 0) {
Owen Anderson4c891422007-06-01 17:34:47 +000063 return value < other.value;
Owen Anderson5fba6c12007-05-29 21:53:49 +000064 } else {
65 if (lhs < other.lhs)
66 return true;
67 else if (other.lhs < lhs)
Owen Anderson5fba6c12007-05-29 21:53:49 +000068 return false;
Owen Anderson4c891422007-06-01 17:34:47 +000069 else
70 return rhs < other.rhs;
Owen Anderson5fba6c12007-05-29 21:53:49 +000071 }
72 }
73
74 bool operator==(const Expression& other) const {
75 if (opcode != other.opcode)
76 return false;
77
78 if (value != other.value)
79 return false;
80
81 if (lhs != other.lhs)
82 return false;
83
84 if (rhs != other.rhs)
85 return false;
86
87 return true;
88 }
89 };
Owen Anderson81d156e2007-05-31 00:42:15 +000090
Owen Anderson5fba6c12007-05-29 21:53:49 +000091 typedef std::map<Expression, uint32_t> ValueTable;
Owen Anderson81d156e2007-05-31 00:42:15 +000092
Owen Anderson5fba6c12007-05-29 21:53:49 +000093 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
94 AU.setPreservesCFG();
95 AU.addRequired<DominatorTree>();
96 AU.addRequired<PostDominatorTree>();
97 }
98
99 // Helper fuctions
100 // FIXME: eliminate or document these better
101 void dump(ValueTable& VN, std::set<Expression>& s);
102 void clean(ValueTable VN, std::set<Expression>& set);
103 Expression add(ValueTable& VN, std::set<Expression>& MS, Instruction* V);
104 ValueTable::iterator lookup(ValueTable& VN, Value* V);
105 Expression buildExpression(ValueTable& VN, Value* V);
106 std::set<Expression>::iterator find_leader(ValueTable VN,
107 std::set<Expression>& vals,
108 uint32_t v);
Owen Anderson81d156e2007-05-31 00:42:15 +0000109 void phi_translate(ValueTable& VN, std::set<Expression>& MS,
Owen Anderson5fba6c12007-05-29 21:53:49 +0000110 std::set<Expression>& anticIn, BasicBlock* B,
111 std::set<Expression>& out);
112
Owen Anderson331bf6a2007-05-31 22:44:11 +0000113 void topo_sort(ValueTable& VN, std::set<Expression>& set,
114 std::vector<Expression>& vec);
115
Owen Anderson5fba6c12007-05-29 21:53:49 +0000116 // For a given block, calculate the generated expressions, temporaries,
117 // and the AVAIL_OUT set
118 void CalculateAvailOut(ValueTable& VN, std::set<Expression>& MS,
119 DominatorTree::Node* DI,
120 std::set<Expression>& currExps,
121 std::set<PHINode*>& currPhis,
122 std::set<Expression>& currTemps,
123 std::set<Expression>& currAvail,
124 std::map<BasicBlock*, std::set<Expression> > availOut);
125
126 };
127
128 char GVNPRE::ID = 0;
129
130}
131
132FunctionPass *llvm::createGVNPREPass() { return new GVNPRE(); }
133
134RegisterPass<GVNPRE> X("gvnpre",
135 "Global Value Numbering/Partial Redundancy Elimination");
136
137// Given a Value, build an Expression to represent it
138GVNPRE::Expression GVNPRE::buildExpression(ValueTable& VN, Value* V) {
139 if (Instruction* I = dyn_cast<Instruction>(V)) {
140 Expression e;
141
142 switch (I->getOpcode()) {
143 case 7:
144 e.opcode = 1; // ADD
145 break;
146 case 8:
147 e.opcode = 2; // SUB
148 break;
149 case 9:
150 e.opcode = 3; // MUL
151 break;
152 case 10:
153 e.opcode = 4; // UDIV
154 break;
155 case 11:
156 e.opcode = 5; // SDIV
157 break;
158 case 12:
159 e.opcode = 6; // FDIV
160 break;
161 case 13:
162 e.opcode = 7; // UREM
163 break;
164 case 14:
165 e.opcode = 8; // SREM
166 break;
167 case 15:
168 e.opcode = 9; // FREM
169 break;
170 default:
171 e.opcode = 0; // OPAQUE
172 e.lhs = 0;
173 e.rhs = 0;
174 e.value = V;
175 return e;
176 }
177
178 e.value = 0;
179
180 ValueTable::iterator lhs = lookup(VN, I->getOperand(0));
181 if (lhs == VN.end()) {
182 Expression lhsExp = buildExpression(VN, I->getOperand(0));
183 VN.insert(std::make_pair(lhsExp, nextValueNumber));
184 e.lhs = nextValueNumber;
185 nextValueNumber++;
186 } else
187 e.lhs = lhs->second;
188 ValueTable::iterator rhs = lookup(VN, I->getOperand(1));
189 if (rhs == VN.end()) {
190 Expression rhsExp = buildExpression(VN, I->getOperand(1));
191 VN.insert(std::make_pair(rhsExp, nextValueNumber));
192 e.rhs = nextValueNumber;
193 nextValueNumber++;
194 } else
195 e.rhs = rhs->second;
196
197 return e;
198 } else {
199 Expression e;
200 e.opcode = 0;
201 e.value = V;
202 e.lhs = 0;
203 e.rhs = 0;
204
205 return e;
206 }
207}
208
209GVNPRE::Expression GVNPRE::add(ValueTable& VN, std::set<Expression>& MS,
210 Instruction* V) {
211 Expression e = buildExpression(VN, V);
Owen Anderson4c891422007-06-01 17:34:47 +0000212 std::pair<ValueTable::iterator, bool> ret = VN.insert(std::make_pair(e, nextValueNumber));
213 if (ret.second)
Owen Anderson5fba6c12007-05-29 21:53:49 +0000214 nextValueNumber++;
215 if (e.opcode != 0 || (e.opcode == 0 && isa<PHINode>(e.value)))
216 MS.insert(e);
217 return e;
218}
219
220GVNPRE::ValueTable::iterator GVNPRE::lookup(ValueTable& VN, Value* V) {
221 Expression e = buildExpression(VN, V);
222 return VN.find(e);
223}
224
225std::set<GVNPRE::Expression>::iterator GVNPRE::find_leader(GVNPRE::ValueTable VN,
226 std::set<GVNPRE::Expression>& vals,
227 uint32_t v) {
228 for (std::set<Expression>::iterator I = vals.begin(), E = vals.end();
229 I != E; ++I)
230 if (VN[*I] == v)
231 return I;
232
233 return vals.end();
234}
235
Owen Anderson81d156e2007-05-31 00:42:15 +0000236void GVNPRE::phi_translate(GVNPRE::ValueTable& VN,
237 std::set<GVNPRE::Expression>& MS,
Owen Anderson5fba6c12007-05-29 21:53:49 +0000238 std::set<GVNPRE::Expression>& anticIn, BasicBlock* B,
239 std::set<GVNPRE::Expression>& out) {
240 BasicBlock* succ = B->getTerminator()->getSuccessor(0);
241
242 for (std::set<Expression>::iterator I = anticIn.begin(), E = anticIn.end();
243 I != E; ++I) {
244 if (I->opcode == 0) {
245 Value *v = I->value;
Owen Anderson81d156e2007-05-31 00:42:15 +0000246 if (PHINode* p = dyn_cast<PHINode>(v)) {
247 if (p->getParent() == succ)
Owen Anderson5fba6c12007-05-29 21:53:49 +0000248 out.insert(buildExpression(VN, p->getIncomingValueForBlock(B)));
Owen Anderson81d156e2007-05-31 00:42:15 +0000249 } else {
250 out.insert(*I);
251 }
252 } else {
253 std::set<Expression>::iterator lhs_it = find_leader(VN, anticIn, I->lhs);
254 if (lhs_it == anticIn.end())
255 continue;
256
257 Expression lhs = *lhs_it;
258 if (lhs.value != 0)
259 if (PHINode* p = dyn_cast<PHINode>(lhs.value))
260 if (p->getParent() == succ) {
261 Expression t = buildExpression(VN, p->getIncomingValueForBlock(B));
262 lhs.opcode = t.opcode;
263 lhs.value = t.value;
264 lhs.lhs = t.lhs;
265 lhs.rhs = t.rhs;
266
267 out.insert(t);
268 }
269
270 std::set<Expression>::iterator rhs_it = find_leader(VN, anticIn, I->rhs);
271 if (rhs_it == anticIn.end())
272 continue;
273
274 Expression rhs = *rhs_it;
275 if (rhs.value != 0)
276 if (PHINode* p = dyn_cast<PHINode>(rhs.value))
277 if (p->getParent() == succ) {
278 Expression t = buildExpression(VN, p->getIncomingValueForBlock(B));
279 rhs.opcode = t.opcode;
280 rhs.value = t.value;
281 rhs.lhs = t.lhs;
282 rhs.rhs = t.rhs;
283
284 out.insert(t);
285 }
286
287 Expression e;
288 e.opcode = I->opcode;
289 e.value = 0;
290 e.lhs = VN[lhs];
291 e.rhs = VN[rhs];
292
293 if (VN.insert(std::make_pair(e, nextValueNumber)).second)
294 nextValueNumber++;
295 MS.insert(e);
Owen Anderson5fba6c12007-05-29 21:53:49 +0000296 }
Owen Anderson5fba6c12007-05-29 21:53:49 +0000297 }
298}
299
300// Remove all expressions whose operands are not themselves in the set
301void GVNPRE::clean(GVNPRE::ValueTable VN, std::set<GVNPRE::Expression>& set) {
302 unsigned size = set.size();
303 unsigned old = 0;
304
305 while (size != old) {
306 old = size;
307
308 std::vector<Expression> worklist(set.begin(), set.end());
309 while (!worklist.empty()) {
310 Expression e = worklist.back();
311 worklist.pop_back();
312
313 if (e.opcode == 0) // OPAQUE
314 continue;
315
316 bool lhsValid = false;
317 for (std::set<Expression>::iterator I = set.begin(), E = set.end();
318 I != E; ++I)
319 if (VN[*I] == e.lhs);
320 lhsValid = true;
321
322 bool rhsValid = false;
323 for (std::set<Expression>::iterator I = set.begin(), E = set.end();
324 I != E; ++I)
325 if (VN[*I] == e.rhs);
326 rhsValid = true;
327
328 if (!lhsValid || !rhsValid)
329 set.erase(e);
330 }
331
332 size = set.size();
333 }
334}
335
Owen Anderson331bf6a2007-05-31 22:44:11 +0000336void GVNPRE::topo_sort(GVNPRE::ValueTable& VN,
337 std::set<GVNPRE::Expression>& set,
338 std::vector<GVNPRE::Expression>& vec) {
339 std::set<Expression> toErase;
340 for (std::set<Expression>::iterator I = set.begin(), E = set.end();
341 I != E; ++I) {
342 for (std::set<Expression>::iterator SI = set.begin(); SI != E; ++SI) {
343 if (I->lhs == VN[*SI] || I->rhs == VN[*SI]) {
344 toErase.insert(*SI);
345 }
346 }
347 }
348
349 std::vector<Expression> Q;
350 std::insert_iterator<std::vector<Expression> > q_ins(Q, Q.begin());
351 std::set_difference(set.begin(), set.end(),
352 toErase.begin(), toErase.end(),
353 q_ins);
354
355 std::set<Expression> visited;
356 while (!Q.empty()) {
357 Expression e = Q.back();
358
359 if (e.opcode == 0) {
360 visited.insert(e);
361 vec.push_back(e);
362 Q.pop_back();
363 continue;
364 }
365
366 std::set<Expression>::iterator l = find_leader(VN, set, e.lhs);
367 std::set<Expression>::iterator r = find_leader(VN, set, e.rhs);
368
369 if (l != set.end() && visited.find(*l) == visited.end())
370 Q.push_back(*l);
371 else if (r != set.end() && visited.find(*r) == visited.end())
372 Q.push_back(*r);
373 else {
374 vec.push_back(e);
375 visited.insert(e);
376 Q.pop_back();
377 }
378 }
379}
380
Owen Anderson5fba6c12007-05-29 21:53:49 +0000381void GVNPRE::dump(GVNPRE::ValueTable& VN, std::set<GVNPRE::Expression>& s) {
Owen Anderson331bf6a2007-05-31 22:44:11 +0000382 std::vector<Expression> sorted;
383 topo_sort(VN, s, sorted);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000384 DOUT << "{ ";
Owen Anderson331bf6a2007-05-31 22:44:11 +0000385 for (std::vector<Expression>::iterator I = sorted.begin(), E = sorted.end();
386 I != E; ++I) {
387 DOUT << VN[*I] << ": ";
Owen Anderson81d156e2007-05-31 00:42:15 +0000388 DOUT << "( ";
Owen Anderson331bf6a2007-05-31 22:44:11 +0000389 DOUT << (char)(I->opcode+48);
Owen Anderson4c891422007-06-01 17:34:47 +0000390 DOUT << ", ";
391 if (I->value == 0)
392 DOUT << "0";
393 else
394 DEBUG(I->value->dump());
395 DOUT << ", value." << I->lhs << ", value." << I->rhs << " ) ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000396 }
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000397 DOUT << "}\n\n";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000398}
399
400void GVNPRE::CalculateAvailOut(GVNPRE::ValueTable& VN, std::set<Expression>& MS,
401 DominatorTree::Node* DI,
402 std::set<Expression>& currExps,
403 std::set<PHINode*>& currPhis,
404 std::set<Expression>& currTemps,
405 std::set<Expression>& currAvail,
406 std::map<BasicBlock*, std::set<Expression> > availOut) {
407
408 BasicBlock* BB = DI->getBlock();
409
410 // A block inherits AVAIL_OUT from its dominator
411 if (DI->getIDom() != 0)
412 currAvail.insert(availOut[DI->getIDom()->getBlock()].begin(),
413 availOut[DI->getIDom()->getBlock()].end());
414
415
416 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
417 BI != BE; ++BI) {
418
419 // Handle PHI nodes...
420 if (PHINode* p = dyn_cast<PHINode>(BI)) {
421 add(VN, MS, p);
422 currPhis.insert(p);
423
424 // Handle binary ops...
425 } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(BI)) {
426 Expression leftValue = buildExpression(VN, BO->getOperand(0));
427 Expression rightValue = buildExpression(VN, BO->getOperand(1));
428
429 Expression e = add(VN, MS, BO);
430
431 currExps.insert(leftValue);
432 currExps.insert(rightValue);
433 currExps.insert(e);
434
435 currTemps.insert(e);
436
437 // Handle unsupported ops
438 } else {
439 Expression e = add(VN, MS, BI);
440 currTemps.insert(e);
441 }
442
443 currAvail.insert(buildExpression(VN, BI));
444 }
445}
446
447bool GVNPRE::runOnFunction(Function &F) {
448 ValueTable VN;
449 std::set<Expression> maximalSet;
450
451 std::map<BasicBlock*, std::set<Expression> > generatedExpressions;
452 std::map<BasicBlock*, std::set<PHINode*> > generatedPhis;
453 std::map<BasicBlock*, std::set<Expression> > generatedTemporaries;
454 std::map<BasicBlock*, std::set<Expression> > availableOut;
455 std::map<BasicBlock*, std::set<Expression> > anticipatedIn;
456
457 DominatorTree &DT = getAnalysis<DominatorTree>();
458
459 // First Phase of BuildSets - calculate AVAIL_OUT
460
461 // Top-down walk of the dominator tree
462 for (df_iterator<DominatorTree::Node*> DI = df_begin(DT.getRootNode()),
463 E = df_end(DT.getRootNode()); DI != E; ++DI) {
464
465 // Get the sets to update for this block
466 std::set<Expression>& currExps = generatedExpressions[DI->getBlock()];
467 std::set<PHINode*>& currPhis = generatedPhis[DI->getBlock()];
468 std::set<Expression>& currTemps = generatedTemporaries[DI->getBlock()];
469 std::set<Expression>& currAvail = availableOut[DI->getBlock()];
470
471 CalculateAvailOut(VN, maximalSet, *DI, currExps, currPhis,
472 currTemps, currAvail, availableOut);
473 }
474
475 PostDominatorTree &PDT = getAnalysis<PostDominatorTree>();
476
477 // Second Phase of BuildSets - calculate ANTIC_IN
478
Owen Anderson0c423072007-05-29 23:26:30 +0000479 std::set<BasicBlock*> visited;
480
Owen Anderson5fba6c12007-05-29 21:53:49 +0000481 bool changed = true;
482 unsigned iterations = 0;
483 while (changed) {
484 changed = false;
485 std::set<Expression> anticOut;
486
487 // Top-down walk of the postdominator tree
488 for (df_iterator<PostDominatorTree::Node*> PDI =
489 df_begin(PDT.getRootNode()), E = df_end(DT.getRootNode());
490 PDI != E; ++PDI) {
491 BasicBlock* BB = PDI->getBlock();
492
Owen Anderson0c423072007-05-29 23:26:30 +0000493 visited.insert(BB);
494
Owen Anderson5fba6c12007-05-29 21:53:49 +0000495 std::set<Expression>& anticIn = anticipatedIn[BB];
496 std::set<Expression> old (anticIn.begin(), anticIn.end());
497
498 if (BB->getTerminator()->getNumSuccessors() == 1) {
Owen Anderson81d156e2007-05-31 00:42:15 +0000499 if (visited.find(BB) == visited.end())
500 phi_translate(VN, maximalSet, anticIn, BB, anticOut);
501 else
502 phi_translate(VN, anticIn, anticIn, BB, anticOut);
Owen Anderson5fba6c12007-05-29 21:53:49 +0000503 } else if (BB->getTerminator()->getNumSuccessors() > 1) {
504 for (unsigned i = 0; i < BB->getTerminator()->getNumSuccessors(); ++i) {
505 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
506 std::set<Expression> temp;
Owen Anderson4b0c1852007-05-29 23:34:14 +0000507 if (visited.find(currSucc) == visited.end())
Owen Anderson5fba6c12007-05-29 21:53:49 +0000508 temp.insert(maximalSet.begin(), maximalSet.end());
509 else
510 temp.insert(anticIn.begin(), anticIn.end());
511
512 anticIn.clear();
513 std::insert_iterator<std::set<Expression> > ai_ins(anticIn,
514 anticIn.begin());
515
516 std::set_difference(anticipatedIn[currSucc].begin(),
517 anticipatedIn[currSucc].end(),
518 temp.begin(),
519 temp.end(),
520 ai_ins);
521 }
522 }
523
524 std::set<Expression> S;
525 std::insert_iterator<std::set<Expression> > s_ins(S, S.begin());
526 std::set_union(anticOut.begin(), anticOut.end(),
527 generatedExpressions[BB].begin(),
528 generatedExpressions[BB].end(),
529 s_ins);
530
531 anticIn.clear();
532 std::insert_iterator<std::set<Expression> > antic_ins(anticIn,
533 anticIn.begin());
534 std::set_difference(S.begin(), S.end(),
535 generatedTemporaries[BB].begin(),
536 generatedTemporaries[BB].end(),
537 antic_ins);
538
539 clean(VN, anticIn);
540
541
542
543 if (old != anticIn)
544 changed = true;
545
546 anticOut.clear();
547 }
548 iterations++;
549 }
550
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000551 DOUT << "Iterations: " << iterations << "\n";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000552
553 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000554 DOUT << "Name: " << I->getName().c_str() << "\n";
555
556 DOUT << "TMP_GEN: ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000557 dump(VN, generatedTemporaries[I]);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000558 DOUT << "\n";
559
560 DOUT << "EXP_GEN: ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000561 dump(VN, generatedExpressions[I]);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000562 DOUT << "\n";
563
564 DOUT << "ANTIC_IN: ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000565 dump(VN, anticipatedIn[I]);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000566 DOUT << "\n";
567 }
Owen Anderson5fba6c12007-05-29 21:53:49 +0000568
569 return false;
570}