blob: 4f5205434df971109a495845009ead4e9b671377 [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) {
Owen Anderson48e93f22007-06-01 22:00:37 +0000302 std::vector<Expression> worklist;
303 topo_sort(VN, set, worklist);
Owen Anderson5fba6c12007-05-29 21:53:49 +0000304
Owen Anderson48e93f22007-06-01 22:00:37 +0000305 while (!worklist.empty()) {
306 Expression e = worklist.back();
307 worklist.pop_back();
Owen Anderson5fba6c12007-05-29 21:53:49 +0000308
Owen Anderson48e93f22007-06-01 22:00:37 +0000309 if (e.opcode == 0) // OPAQUE
310 continue;
Owen Anderson5fba6c12007-05-29 21:53:49 +0000311
Owen Anderson48e93f22007-06-01 22:00:37 +0000312 bool lhsValid = false;
313 for (std::set<Expression>::iterator I = set.begin(), E = set.end();
314 I != E; ++I)
315 if (VN[*I] == e.lhs);
316 lhsValid = true;
Owen Anderson5fba6c12007-05-29 21:53:49 +0000317
Owen Anderson48e93f22007-06-01 22:00:37 +0000318 bool rhsValid = false;
319 for (std::set<Expression>::iterator I = set.begin(), E = set.end();
320 I != E; ++I)
321 if (VN[*I] == e.rhs);
322 rhsValid = true;
Owen Anderson5fba6c12007-05-29 21:53:49 +0000323
Owen Anderson48e93f22007-06-01 22:00:37 +0000324 if (!lhsValid || !rhsValid)
325 set.erase(e);
Owen Anderson5fba6c12007-05-29 21:53:49 +0000326 }
327}
328
Owen Anderson331bf6a2007-05-31 22:44:11 +0000329void GVNPRE::topo_sort(GVNPRE::ValueTable& VN,
330 std::set<GVNPRE::Expression>& set,
331 std::vector<GVNPRE::Expression>& vec) {
332 std::set<Expression> toErase;
333 for (std::set<Expression>::iterator I = set.begin(), E = set.end();
334 I != E; ++I) {
335 for (std::set<Expression>::iterator SI = set.begin(); SI != E; ++SI) {
336 if (I->lhs == VN[*SI] || I->rhs == VN[*SI]) {
337 toErase.insert(*SI);
338 }
339 }
340 }
341
342 std::vector<Expression> Q;
343 std::insert_iterator<std::vector<Expression> > q_ins(Q, Q.begin());
344 std::set_difference(set.begin(), set.end(),
345 toErase.begin(), toErase.end(),
346 q_ins);
347
348 std::set<Expression> visited;
349 while (!Q.empty()) {
350 Expression e = Q.back();
351
352 if (e.opcode == 0) {
353 visited.insert(e);
354 vec.push_back(e);
355 Q.pop_back();
356 continue;
357 }
358
359 std::set<Expression>::iterator l = find_leader(VN, set, e.lhs);
360 std::set<Expression>::iterator r = find_leader(VN, set, e.rhs);
361
362 if (l != set.end() && visited.find(*l) == visited.end())
363 Q.push_back(*l);
364 else if (r != set.end() && visited.find(*r) == visited.end())
365 Q.push_back(*r);
366 else {
367 vec.push_back(e);
368 visited.insert(e);
369 Q.pop_back();
370 }
371 }
372}
373
Owen Anderson5fba6c12007-05-29 21:53:49 +0000374void GVNPRE::dump(GVNPRE::ValueTable& VN, std::set<GVNPRE::Expression>& s) {
Owen Anderson331bf6a2007-05-31 22:44:11 +0000375 std::vector<Expression> sorted;
376 topo_sort(VN, s, sorted);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000377 DOUT << "{ ";
Owen Anderson331bf6a2007-05-31 22:44:11 +0000378 for (std::vector<Expression>::iterator I = sorted.begin(), E = sorted.end();
379 I != E; ++I) {
380 DOUT << VN[*I] << ": ";
Owen Anderson81d156e2007-05-31 00:42:15 +0000381 DOUT << "( ";
Owen Anderson331bf6a2007-05-31 22:44:11 +0000382 DOUT << (char)(I->opcode+48);
Owen Anderson4c891422007-06-01 17:34:47 +0000383 DOUT << ", ";
384 if (I->value == 0)
385 DOUT << "0";
386 else
387 DEBUG(I->value->dump());
388 DOUT << ", value." << I->lhs << ", value." << I->rhs << " ) ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000389 }
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000390 DOUT << "}\n\n";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000391}
392
393void GVNPRE::CalculateAvailOut(GVNPRE::ValueTable& VN, std::set<Expression>& MS,
394 DominatorTree::Node* DI,
395 std::set<Expression>& currExps,
396 std::set<PHINode*>& currPhis,
397 std::set<Expression>& currTemps,
398 std::set<Expression>& currAvail,
399 std::map<BasicBlock*, std::set<Expression> > availOut) {
400
401 BasicBlock* BB = DI->getBlock();
402
403 // A block inherits AVAIL_OUT from its dominator
404 if (DI->getIDom() != 0)
405 currAvail.insert(availOut[DI->getIDom()->getBlock()].begin(),
406 availOut[DI->getIDom()->getBlock()].end());
407
408
409 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
410 BI != BE; ++BI) {
411
412 // Handle PHI nodes...
413 if (PHINode* p = dyn_cast<PHINode>(BI)) {
414 add(VN, MS, p);
415 currPhis.insert(p);
416
417 // Handle binary ops...
418 } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(BI)) {
419 Expression leftValue = buildExpression(VN, BO->getOperand(0));
420 Expression rightValue = buildExpression(VN, BO->getOperand(1));
421
422 Expression e = add(VN, MS, BO);
423
424 currExps.insert(leftValue);
425 currExps.insert(rightValue);
426 currExps.insert(e);
427
428 currTemps.insert(e);
429
430 // Handle unsupported ops
431 } else {
432 Expression e = add(VN, MS, BI);
433 currTemps.insert(e);
434 }
435
436 currAvail.insert(buildExpression(VN, BI));
437 }
438}
439
440bool GVNPRE::runOnFunction(Function &F) {
441 ValueTable VN;
442 std::set<Expression> maximalSet;
443
444 std::map<BasicBlock*, std::set<Expression> > generatedExpressions;
445 std::map<BasicBlock*, std::set<PHINode*> > generatedPhis;
446 std::map<BasicBlock*, std::set<Expression> > generatedTemporaries;
447 std::map<BasicBlock*, std::set<Expression> > availableOut;
448 std::map<BasicBlock*, std::set<Expression> > anticipatedIn;
449
450 DominatorTree &DT = getAnalysis<DominatorTree>();
451
452 // First Phase of BuildSets - calculate AVAIL_OUT
453
454 // Top-down walk of the dominator tree
455 for (df_iterator<DominatorTree::Node*> DI = df_begin(DT.getRootNode()),
456 E = df_end(DT.getRootNode()); DI != E; ++DI) {
457
458 // Get the sets to update for this block
459 std::set<Expression>& currExps = generatedExpressions[DI->getBlock()];
460 std::set<PHINode*>& currPhis = generatedPhis[DI->getBlock()];
461 std::set<Expression>& currTemps = generatedTemporaries[DI->getBlock()];
462 std::set<Expression>& currAvail = availableOut[DI->getBlock()];
463
464 CalculateAvailOut(VN, maximalSet, *DI, currExps, currPhis,
465 currTemps, currAvail, availableOut);
466 }
467
468 PostDominatorTree &PDT = getAnalysis<PostDominatorTree>();
469
470 // Second Phase of BuildSets - calculate ANTIC_IN
471
Owen Anderson0c423072007-05-29 23:26:30 +0000472 std::set<BasicBlock*> visited;
473
Owen Anderson5fba6c12007-05-29 21:53:49 +0000474 bool changed = true;
475 unsigned iterations = 0;
476 while (changed) {
477 changed = false;
478 std::set<Expression> anticOut;
479
480 // Top-down walk of the postdominator tree
481 for (df_iterator<PostDominatorTree::Node*> PDI =
482 df_begin(PDT.getRootNode()), E = df_end(DT.getRootNode());
483 PDI != E; ++PDI) {
484 BasicBlock* BB = PDI->getBlock();
485
Owen Anderson0c423072007-05-29 23:26:30 +0000486 visited.insert(BB);
487
Owen Anderson5fba6c12007-05-29 21:53:49 +0000488 std::set<Expression>& anticIn = anticipatedIn[BB];
489 std::set<Expression> old (anticIn.begin(), anticIn.end());
490
491 if (BB->getTerminator()->getNumSuccessors() == 1) {
Owen Anderson81d156e2007-05-31 00:42:15 +0000492 if (visited.find(BB) == visited.end())
493 phi_translate(VN, maximalSet, anticIn, BB, anticOut);
494 else
495 phi_translate(VN, anticIn, anticIn, BB, anticOut);
Owen Anderson5fba6c12007-05-29 21:53:49 +0000496 } else if (BB->getTerminator()->getNumSuccessors() > 1) {
497 for (unsigned i = 0; i < BB->getTerminator()->getNumSuccessors(); ++i) {
498 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
499 std::set<Expression> temp;
Owen Anderson4b0c1852007-05-29 23:34:14 +0000500 if (visited.find(currSucc) == visited.end())
Owen Anderson5fba6c12007-05-29 21:53:49 +0000501 temp.insert(maximalSet.begin(), maximalSet.end());
502 else
503 temp.insert(anticIn.begin(), anticIn.end());
504
505 anticIn.clear();
506 std::insert_iterator<std::set<Expression> > ai_ins(anticIn,
507 anticIn.begin());
508
509 std::set_difference(anticipatedIn[currSucc].begin(),
510 anticipatedIn[currSucc].end(),
511 temp.begin(),
512 temp.end(),
513 ai_ins);
514 }
515 }
516
517 std::set<Expression> S;
518 std::insert_iterator<std::set<Expression> > s_ins(S, S.begin());
519 std::set_union(anticOut.begin(), anticOut.end(),
520 generatedExpressions[BB].begin(),
521 generatedExpressions[BB].end(),
522 s_ins);
523
524 anticIn.clear();
525 std::insert_iterator<std::set<Expression> > antic_ins(anticIn,
526 anticIn.begin());
527 std::set_difference(S.begin(), S.end(),
528 generatedTemporaries[BB].begin(),
529 generatedTemporaries[BB].end(),
530 antic_ins);
531
532 clean(VN, anticIn);
533
534
535
536 if (old != anticIn)
537 changed = true;
538
539 anticOut.clear();
540 }
541 iterations++;
542 }
543
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000544 DOUT << "Iterations: " << iterations << "\n";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000545
546 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000547 DOUT << "Name: " << I->getName().c_str() << "\n";
548
549 DOUT << "TMP_GEN: ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000550 dump(VN, generatedTemporaries[I]);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000551 DOUT << "\n";
552
553 DOUT << "EXP_GEN: ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000554 dump(VN, generatedExpressions[I]);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000555 DOUT << "\n";
556
557 DOUT << "ANTIC_IN: ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000558 dump(VN, anticipatedIn[I]);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000559 DOUT << "\n";
560 }
Owen Anderson5fba6c12007-05-29 21:53:49 +0000561
562 return false;
563}