blob: f672cbbbfa72a8876f1f25621a603cd26fcdc316 [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>
33#include <map>
34#include <set>
Owen Anderson5fba6c12007-05-29 21:53:49 +000035using namespace llvm;
36
37namespace {
38
39 class VISIBILITY_HIDDEN GVNPRE : public FunctionPass {
40 bool runOnFunction(Function &F);
41 public:
42 static char ID; // Pass identification, replacement for typeid
43 GVNPRE() : FunctionPass((intptr_t)&ID) { nextValueNumber = 0; }
44
45 private:
46 uint32_t nextValueNumber;
47
48 struct Expression {
49 char opcode;
50 Value* value;
51 uint32_t lhs;
52 uint32_t rhs;
53
54 bool operator<(const Expression& other) const {
55 if (opcode < other.opcode)
56 return true;
57 else if (other.opcode < opcode)
58 return false;
59
60 if (opcode == 0) {
61 if (value < other.value)
62 return true;
63 else
64 return false;
65 } else {
66 if (lhs < other.lhs)
67 return true;
68 else if (other.lhs < lhs)
69 return true;
70 else if (rhs < other.rhs)
71 return true;
72 else
73 return false;
74 }
75 }
76
77 bool operator==(const Expression& other) const {
78 if (opcode != other.opcode)
79 return false;
80
81 if (value != other.value)
82 return false;
83
84 if (lhs != other.lhs)
85 return false;
86
87 if (rhs != other.rhs)
88 return false;
89
90 return true;
91 }
92 };
Owen Anderson81d156e2007-05-31 00:42:15 +000093
Owen Anderson5fba6c12007-05-29 21:53:49 +000094 typedef std::map<Expression, uint32_t> ValueTable;
Owen Anderson81d156e2007-05-31 00:42:15 +000095
Owen Anderson5fba6c12007-05-29 21:53:49 +000096 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97 AU.setPreservesCFG();
98 AU.addRequired<DominatorTree>();
99 AU.addRequired<PostDominatorTree>();
100 }
101
102 // Helper fuctions
103 // FIXME: eliminate or document these better
104 void dump(ValueTable& VN, std::set<Expression>& s);
105 void clean(ValueTable VN, std::set<Expression>& set);
106 Expression add(ValueTable& VN, std::set<Expression>& MS, Instruction* V);
107 ValueTable::iterator lookup(ValueTable& VN, Value* V);
108 Expression buildExpression(ValueTable& VN, Value* V);
109 std::set<Expression>::iterator find_leader(ValueTable VN,
110 std::set<Expression>& vals,
111 uint32_t v);
Owen Anderson81d156e2007-05-31 00:42:15 +0000112 void phi_translate(ValueTable& VN, std::set<Expression>& MS,
Owen Anderson5fba6c12007-05-29 21:53:49 +0000113 std::set<Expression>& anticIn, BasicBlock* B,
114 std::set<Expression>& out);
115
116 // 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);
212 if (VN.insert(std::make_pair(e, nextValueNumber)).second)
213 nextValueNumber++;
214 if (e.opcode != 0 || (e.opcode == 0 && isa<PHINode>(e.value)))
215 MS.insert(e);
216 return e;
217}
218
219GVNPRE::ValueTable::iterator GVNPRE::lookup(ValueTable& VN, Value* V) {
220 Expression e = buildExpression(VN, V);
221 return VN.find(e);
222}
223
224std::set<GVNPRE::Expression>::iterator GVNPRE::find_leader(GVNPRE::ValueTable VN,
225 std::set<GVNPRE::Expression>& vals,
226 uint32_t v) {
227 for (std::set<Expression>::iterator I = vals.begin(), E = vals.end();
228 I != E; ++I)
229 if (VN[*I] == v)
230 return I;
231
232 return vals.end();
233}
234
Owen Anderson81d156e2007-05-31 00:42:15 +0000235void GVNPRE::phi_translate(GVNPRE::ValueTable& VN,
236 std::set<GVNPRE::Expression>& MS,
Owen Anderson5fba6c12007-05-29 21:53:49 +0000237 std::set<GVNPRE::Expression>& anticIn, BasicBlock* B,
238 std::set<GVNPRE::Expression>& out) {
239 BasicBlock* succ = B->getTerminator()->getSuccessor(0);
240
241 for (std::set<Expression>::iterator I = anticIn.begin(), E = anticIn.end();
242 I != E; ++I) {
243 if (I->opcode == 0) {
244 Value *v = I->value;
Owen Anderson81d156e2007-05-31 00:42:15 +0000245 if (PHINode* p = dyn_cast<PHINode>(v)) {
246 if (p->getParent() == succ)
Owen Anderson5fba6c12007-05-29 21:53:49 +0000247 out.insert(buildExpression(VN, p->getIncomingValueForBlock(B)));
Owen Anderson81d156e2007-05-31 00:42:15 +0000248 } else {
249 out.insert(*I);
250 }
251 } else {
252 std::set<Expression>::iterator lhs_it = find_leader(VN, anticIn, I->lhs);
253 if (lhs_it == anticIn.end())
254 continue;
255
256 Expression lhs = *lhs_it;
257 if (lhs.value != 0)
258 if (PHINode* p = dyn_cast<PHINode>(lhs.value))
259 if (p->getParent() == succ) {
260 Expression t = buildExpression(VN, p->getIncomingValueForBlock(B));
261 lhs.opcode = t.opcode;
262 lhs.value = t.value;
263 lhs.lhs = t.lhs;
264 lhs.rhs = t.rhs;
265
266 out.insert(t);
267 }
268
269 std::set<Expression>::iterator rhs_it = find_leader(VN, anticIn, I->rhs);
270 if (rhs_it == anticIn.end())
271 continue;
272
273 Expression rhs = *rhs_it;
274 if (rhs.value != 0)
275 if (PHINode* p = dyn_cast<PHINode>(rhs.value))
276 if (p->getParent() == succ) {
277 Expression t = buildExpression(VN, p->getIncomingValueForBlock(B));
278 rhs.opcode = t.opcode;
279 rhs.value = t.value;
280 rhs.lhs = t.lhs;
281 rhs.rhs = t.rhs;
282
283 out.insert(t);
284 }
285
286 Expression e;
287 e.opcode = I->opcode;
288 e.value = 0;
289 e.lhs = VN[lhs];
290 e.rhs = VN[rhs];
291
292 if (VN.insert(std::make_pair(e, nextValueNumber)).second)
293 nextValueNumber++;
294 MS.insert(e);
Owen Anderson5fba6c12007-05-29 21:53:49 +0000295 }
Owen Anderson5fba6c12007-05-29 21:53:49 +0000296 }
297}
298
299// Remove all expressions whose operands are not themselves in the set
300void GVNPRE::clean(GVNPRE::ValueTable VN, std::set<GVNPRE::Expression>& set) {
301 unsigned size = set.size();
302 unsigned old = 0;
303
304 while (size != old) {
305 old = size;
306
307 std::vector<Expression> worklist(set.begin(), set.end());
308 while (!worklist.empty()) {
309 Expression e = worklist.back();
310 worklist.pop_back();
311
312 if (e.opcode == 0) // OPAQUE
313 continue;
314
315 bool lhsValid = false;
316 for (std::set<Expression>::iterator I = set.begin(), E = set.end();
317 I != E; ++I)
318 if (VN[*I] == e.lhs);
319 lhsValid = true;
320
321 bool rhsValid = false;
322 for (std::set<Expression>::iterator I = set.begin(), E = set.end();
323 I != E; ++I)
324 if (VN[*I] == e.rhs);
325 rhsValid = true;
326
327 if (!lhsValid || !rhsValid)
328 set.erase(e);
329 }
330
331 size = set.size();
332 }
333}
334
335void GVNPRE::dump(GVNPRE::ValueTable& VN, std::set<GVNPRE::Expression>& s) {
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000336 DOUT << "{ ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000337 for (std::set<Expression>::iterator I = s.begin(), E = s.end(); I != E; ++I) {
Owen Anderson81d156e2007-05-31 00:42:15 +0000338 DOUT << "( ";
339 DOUT << I->opcode+48;
340 DOUT << ", "
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000341 << (I->value == 0 ? "0" : I->value->getName().c_str())
342 << ", value." << I->lhs << ", value." << I->rhs << " ) ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000343 }
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000344 DOUT << "}\n\n";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000345}
346
347void GVNPRE::CalculateAvailOut(GVNPRE::ValueTable& VN, std::set<Expression>& MS,
348 DominatorTree::Node* DI,
349 std::set<Expression>& currExps,
350 std::set<PHINode*>& currPhis,
351 std::set<Expression>& currTemps,
352 std::set<Expression>& currAvail,
353 std::map<BasicBlock*, std::set<Expression> > availOut) {
354
355 BasicBlock* BB = DI->getBlock();
356
357 // A block inherits AVAIL_OUT from its dominator
358 if (DI->getIDom() != 0)
359 currAvail.insert(availOut[DI->getIDom()->getBlock()].begin(),
360 availOut[DI->getIDom()->getBlock()].end());
361
362
363 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
364 BI != BE; ++BI) {
365
366 // Handle PHI nodes...
367 if (PHINode* p = dyn_cast<PHINode>(BI)) {
368 add(VN, MS, p);
369 currPhis.insert(p);
370
371 // Handle binary ops...
372 } else if (BinaryOperator* BO = dyn_cast<BinaryOperator>(BI)) {
373 Expression leftValue = buildExpression(VN, BO->getOperand(0));
374 Expression rightValue = buildExpression(VN, BO->getOperand(1));
375
376 Expression e = add(VN, MS, BO);
377
378 currExps.insert(leftValue);
379 currExps.insert(rightValue);
380 currExps.insert(e);
381
382 currTemps.insert(e);
383
384 // Handle unsupported ops
385 } else {
386 Expression e = add(VN, MS, BI);
387 currTemps.insert(e);
388 }
389
390 currAvail.insert(buildExpression(VN, BI));
391 }
392}
393
394bool GVNPRE::runOnFunction(Function &F) {
395 ValueTable VN;
396 std::set<Expression> maximalSet;
397
398 std::map<BasicBlock*, std::set<Expression> > generatedExpressions;
399 std::map<BasicBlock*, std::set<PHINode*> > generatedPhis;
400 std::map<BasicBlock*, std::set<Expression> > generatedTemporaries;
401 std::map<BasicBlock*, std::set<Expression> > availableOut;
402 std::map<BasicBlock*, std::set<Expression> > anticipatedIn;
403
404 DominatorTree &DT = getAnalysis<DominatorTree>();
405
406 // First Phase of BuildSets - calculate AVAIL_OUT
407
408 // Top-down walk of the dominator tree
409 for (df_iterator<DominatorTree::Node*> DI = df_begin(DT.getRootNode()),
410 E = df_end(DT.getRootNode()); DI != E; ++DI) {
411
412 // Get the sets to update for this block
413 std::set<Expression>& currExps = generatedExpressions[DI->getBlock()];
414 std::set<PHINode*>& currPhis = generatedPhis[DI->getBlock()];
415 std::set<Expression>& currTemps = generatedTemporaries[DI->getBlock()];
416 std::set<Expression>& currAvail = availableOut[DI->getBlock()];
417
418 CalculateAvailOut(VN, maximalSet, *DI, currExps, currPhis,
419 currTemps, currAvail, availableOut);
420 }
421
422 PostDominatorTree &PDT = getAnalysis<PostDominatorTree>();
423
424 // Second Phase of BuildSets - calculate ANTIC_IN
425
Owen Anderson0c423072007-05-29 23:26:30 +0000426 std::set<BasicBlock*> visited;
427
Owen Anderson5fba6c12007-05-29 21:53:49 +0000428 bool changed = true;
429 unsigned iterations = 0;
430 while (changed) {
431 changed = false;
432 std::set<Expression> anticOut;
433
434 // Top-down walk of the postdominator tree
435 for (df_iterator<PostDominatorTree::Node*> PDI =
436 df_begin(PDT.getRootNode()), E = df_end(DT.getRootNode());
437 PDI != E; ++PDI) {
438 BasicBlock* BB = PDI->getBlock();
439
Owen Anderson0c423072007-05-29 23:26:30 +0000440 visited.insert(BB);
441
Owen Anderson5fba6c12007-05-29 21:53:49 +0000442 std::set<Expression>& anticIn = anticipatedIn[BB];
443 std::set<Expression> old (anticIn.begin(), anticIn.end());
444
445 if (BB->getTerminator()->getNumSuccessors() == 1) {
Owen Anderson81d156e2007-05-31 00:42:15 +0000446 if (visited.find(BB) == visited.end())
447 phi_translate(VN, maximalSet, anticIn, BB, anticOut);
448 else
449 phi_translate(VN, anticIn, anticIn, BB, anticOut);
Owen Anderson5fba6c12007-05-29 21:53:49 +0000450 } else if (BB->getTerminator()->getNumSuccessors() > 1) {
451 for (unsigned i = 0; i < BB->getTerminator()->getNumSuccessors(); ++i) {
452 BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
453 std::set<Expression> temp;
Owen Anderson4b0c1852007-05-29 23:34:14 +0000454 if (visited.find(currSucc) == visited.end())
Owen Anderson5fba6c12007-05-29 21:53:49 +0000455 temp.insert(maximalSet.begin(), maximalSet.end());
456 else
457 temp.insert(anticIn.begin(), anticIn.end());
458
459 anticIn.clear();
460 std::insert_iterator<std::set<Expression> > ai_ins(anticIn,
461 anticIn.begin());
462
463 std::set_difference(anticipatedIn[currSucc].begin(),
464 anticipatedIn[currSucc].end(),
465 temp.begin(),
466 temp.end(),
467 ai_ins);
468 }
469 }
470
471 std::set<Expression> S;
472 std::insert_iterator<std::set<Expression> > s_ins(S, S.begin());
473 std::set_union(anticOut.begin(), anticOut.end(),
474 generatedExpressions[BB].begin(),
475 generatedExpressions[BB].end(),
476 s_ins);
477
478 anticIn.clear();
479 std::insert_iterator<std::set<Expression> > antic_ins(anticIn,
480 anticIn.begin());
481 std::set_difference(S.begin(), S.end(),
482 generatedTemporaries[BB].begin(),
483 generatedTemporaries[BB].end(),
484 antic_ins);
485
486 clean(VN, anticIn);
487
488
489
490 if (old != anticIn)
491 changed = true;
492
493 anticOut.clear();
494 }
495 iterations++;
496 }
497
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000498 DOUT << "Iterations: " << iterations << "\n";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000499
500 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000501 DOUT << "Name: " << I->getName().c_str() << "\n";
502
503 DOUT << "TMP_GEN: ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000504 dump(VN, generatedTemporaries[I]);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000505 DOUT << "\n";
506
507 DOUT << "EXP_GEN: ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000508 dump(VN, generatedExpressions[I]);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000509 DOUT << "\n";
510
511 DOUT << "ANTIC_IN: ";
Owen Anderson5fba6c12007-05-29 21:53:49 +0000512 dump(VN, anticipatedIn[I]);
Owen Anderson4a6ec8f2007-05-29 23:15:21 +0000513 DOUT << "\n";
514 }
Owen Anderson5fba6c12007-05-29 21:53:49 +0000515
516 return false;
517}