1 //===- GVNPRE.cpp - Eliminate redundant values and expressions ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 are very
17 // sensitive to register pressure.
19 // Note that this pass does the value numbering itself, it does not use the
20 // ValueNumbering analysis passes.
22 //===----------------------------------------------------------------------===//
24 #define DEBUG_TYPE "gvnpre"
25 #include "llvm/Value.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/Function.h"
29 #include "llvm/DerivedTypes.h"
30 #include "llvm/Analysis/Dominators.h"
31 #include "llvm/ADT/BitVector.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/DepthFirstIterator.h"
34 #include "llvm/ADT/PostOrderIterator.h"
35 #include "llvm/ADT/SmallPtrSet.h"
36 #include "llvm/ADT/SmallVector.h"
37 #include "llvm/ADT/Statistic.h"
38 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
39 #include "llvm/Support/CFG.h"
40 #include "llvm/Support/Compiler.h"
41 #include "llvm/Support/Debug.h"
47 //===----------------------------------------------------------------------===//
49 //===----------------------------------------------------------------------===//
53 /// This class holds the mapping between values and value numbers. It is used
54 /// as an efficient mechanism to determine the expression-wise equivalence of
58 enum ExpressionOpcode
{ ADD
, SUB
, MUL
, UDIV
, SDIV
, FDIV
, UREM
, SREM
,
59 FREM
, SHL
, LSHR
, ASHR
, AND
, OR
, XOR
, ICMPEQ
,
60 ICMPNE
, ICMPUGT
, ICMPUGE
, ICMPULT
, ICMPULE
,
61 ICMPSGT
, ICMPSGE
, ICMPSLT
, ICMPSLE
, FCMPOEQ
,
62 FCMPOGT
, FCMPOGE
, FCMPOLT
, FCMPOLE
, FCMPONE
,
63 FCMPORD
, FCMPUNO
, FCMPUEQ
, FCMPUGT
, FCMPUGE
,
64 FCMPULT
, FCMPULE
, FCMPUNE
, EXTRACT
, INSERT
,
65 SHUFFLE
, SELECT
, TRUNC
, ZEXT
, SEXT
, FPTOUI
,
66 FPTOSI
, UITOFP
, SITOFP
, FPTRUNC
, FPEXT
,
67 PTRTOINT
, INTTOPTR
, BITCAST
, GEP
, EMPTY
,
70 ExpressionOpcode opcode
;
75 SmallVector
<uint32_t, 4> varargs
;
78 explicit Expression(ExpressionOpcode o
) : opcode(o
) { }
80 bool operator==(const Expression
&other
) const {
81 if (opcode
!= other
.opcode
)
83 else if (opcode
== EMPTY
|| opcode
== TOMBSTONE
)
85 else if (type
!= other
.type
)
87 else if (firstVN
!= other
.firstVN
)
89 else if (secondVN
!= other
.secondVN
)
91 else if (thirdVN
!= other
.thirdVN
)
94 if (varargs
.size() != other
.varargs
.size())
97 for (size_t i
= 0; i
< varargs
.size(); ++i
)
98 if (varargs
[i
] != other
.varargs
[i
])
105 bool operator!=(const Expression
&other
) const {
106 if (opcode
!= other
.opcode
)
108 else if (opcode
== EMPTY
|| opcode
== TOMBSTONE
)
110 else if (type
!= other
.type
)
112 else if (firstVN
!= other
.firstVN
)
114 else if (secondVN
!= other
.secondVN
)
116 else if (thirdVN
!= other
.thirdVN
)
119 if (varargs
.size() != other
.varargs
.size())
122 for (size_t i
= 0; i
< varargs
.size(); ++i
)
123 if (varargs
[i
] != other
.varargs
[i
])
134 class VISIBILITY_HIDDEN ValueTable
{
136 DenseMap
<Value
*, uint32_t> valueNumbering
;
137 DenseMap
<Expression
, uint32_t> expressionNumbering
;
139 uint32_t nextValueNumber
;
141 Expression::ExpressionOpcode
getOpcode(BinaryOperator
* BO
);
142 Expression::ExpressionOpcode
getOpcode(CmpInst
* C
);
143 Expression::ExpressionOpcode
getOpcode(CastInst
* C
);
144 Expression
create_expression(BinaryOperator
* BO
);
145 Expression
create_expression(CmpInst
* C
);
146 Expression
create_expression(ShuffleVectorInst
* V
);
147 Expression
create_expression(ExtractElementInst
* C
);
148 Expression
create_expression(InsertElementInst
* V
);
149 Expression
create_expression(SelectInst
* V
);
150 Expression
create_expression(CastInst
* C
);
151 Expression
create_expression(GetElementPtrInst
* G
);
153 ValueTable() { nextValueNumber
= 1; }
154 uint32_t lookup_or_add(Value
* V
);
155 uint32_t lookup(Value
* V
) const;
156 void add(Value
* V
, uint32_t num
);
158 void erase(Value
* v
);
164 template <> struct DenseMapInfo
<Expression
> {
165 static inline Expression
getEmptyKey() {
166 return Expression(Expression::EMPTY
);
169 static inline Expression
getTombstoneKey() {
170 return Expression(Expression::TOMBSTONE
);
173 static unsigned getHashValue(const Expression e
) {
174 unsigned hash
= e
.opcode
;
176 hash
= e
.firstVN
+ hash
* 37;
177 hash
= e
.secondVN
+ hash
* 37;
178 hash
= e
.thirdVN
+ hash
* 37;
180 hash
= ((unsigned)((uintptr_t)e
.type
>> 4) ^
181 (unsigned)((uintptr_t)e
.type
>> 9)) +
184 for (SmallVector
<uint32_t, 4>::const_iterator I
= e
.varargs
.begin(),
185 E
= e
.varargs
.end(); I
!= E
; ++I
)
186 hash
= *I
+ hash
* 37;
190 static bool isEqual(const Expression
&LHS
, const Expression
&RHS
) {
193 static bool isPod() { return true; }
197 //===----------------------------------------------------------------------===//
198 // ValueTable Internal Functions
199 //===----------------------------------------------------------------------===//
200 Expression::ExpressionOpcode
201 ValueTable::getOpcode(BinaryOperator
* BO
) {
202 switch(BO
->getOpcode()) {
203 case Instruction::Add
:
204 return Expression::ADD
;
205 case Instruction::Sub
:
206 return Expression::SUB
;
207 case Instruction::Mul
:
208 return Expression::MUL
;
209 case Instruction::UDiv
:
210 return Expression::UDIV
;
211 case Instruction::SDiv
:
212 return Expression::SDIV
;
213 case Instruction::FDiv
:
214 return Expression::FDIV
;
215 case Instruction::URem
:
216 return Expression::UREM
;
217 case Instruction::SRem
:
218 return Expression::SREM
;
219 case Instruction::FRem
:
220 return Expression::FREM
;
221 case Instruction::Shl
:
222 return Expression::SHL
;
223 case Instruction::LShr
:
224 return Expression::LSHR
;
225 case Instruction::AShr
:
226 return Expression::ASHR
;
227 case Instruction::And
:
228 return Expression::AND
;
229 case Instruction::Or
:
230 return Expression::OR
;
231 case Instruction::Xor
:
232 return Expression::XOR
;
234 // THIS SHOULD NEVER HAPPEN
236 assert(0 && "Binary operator with unknown opcode?");
237 return Expression::ADD
;
241 Expression::ExpressionOpcode
ValueTable::getOpcode(CmpInst
* C
) {
242 if (C
->getOpcode() == Instruction::ICmp
) {
243 switch (C
->getPredicate()) {
244 case ICmpInst::ICMP_EQ
:
245 return Expression::ICMPEQ
;
246 case ICmpInst::ICMP_NE
:
247 return Expression::ICMPNE
;
248 case ICmpInst::ICMP_UGT
:
249 return Expression::ICMPUGT
;
250 case ICmpInst::ICMP_UGE
:
251 return Expression::ICMPUGE
;
252 case ICmpInst::ICMP_ULT
:
253 return Expression::ICMPULT
;
254 case ICmpInst::ICMP_ULE
:
255 return Expression::ICMPULE
;
256 case ICmpInst::ICMP_SGT
:
257 return Expression::ICMPSGT
;
258 case ICmpInst::ICMP_SGE
:
259 return Expression::ICMPSGE
;
260 case ICmpInst::ICMP_SLT
:
261 return Expression::ICMPSLT
;
262 case ICmpInst::ICMP_SLE
:
263 return Expression::ICMPSLE
;
265 // THIS SHOULD NEVER HAPPEN
267 assert(0 && "Comparison with unknown predicate?");
268 return Expression::ICMPEQ
;
271 switch (C
->getPredicate()) {
272 case FCmpInst::FCMP_OEQ
:
273 return Expression::FCMPOEQ
;
274 case FCmpInst::FCMP_OGT
:
275 return Expression::FCMPOGT
;
276 case FCmpInst::FCMP_OGE
:
277 return Expression::FCMPOGE
;
278 case FCmpInst::FCMP_OLT
:
279 return Expression::FCMPOLT
;
280 case FCmpInst::FCMP_OLE
:
281 return Expression::FCMPOLE
;
282 case FCmpInst::FCMP_ONE
:
283 return Expression::FCMPONE
;
284 case FCmpInst::FCMP_ORD
:
285 return Expression::FCMPORD
;
286 case FCmpInst::FCMP_UNO
:
287 return Expression::FCMPUNO
;
288 case FCmpInst::FCMP_UEQ
:
289 return Expression::FCMPUEQ
;
290 case FCmpInst::FCMP_UGT
:
291 return Expression::FCMPUGT
;
292 case FCmpInst::FCMP_UGE
:
293 return Expression::FCMPUGE
;
294 case FCmpInst::FCMP_ULT
:
295 return Expression::FCMPULT
;
296 case FCmpInst::FCMP_ULE
:
297 return Expression::FCMPULE
;
298 case FCmpInst::FCMP_UNE
:
299 return Expression::FCMPUNE
;
301 // THIS SHOULD NEVER HAPPEN
303 assert(0 && "Comparison with unknown predicate?");
304 return Expression::FCMPOEQ
;
309 Expression::ExpressionOpcode
310 ValueTable::getOpcode(CastInst
* C
) {
311 switch(C
->getOpcode()) {
312 case Instruction::Trunc
:
313 return Expression::TRUNC
;
314 case Instruction::ZExt
:
315 return Expression::ZEXT
;
316 case Instruction::SExt
:
317 return Expression::SEXT
;
318 case Instruction::FPToUI
:
319 return Expression::FPTOUI
;
320 case Instruction::FPToSI
:
321 return Expression::FPTOSI
;
322 case Instruction::UIToFP
:
323 return Expression::UITOFP
;
324 case Instruction::SIToFP
:
325 return Expression::SITOFP
;
326 case Instruction::FPTrunc
:
327 return Expression::FPTRUNC
;
328 case Instruction::FPExt
:
329 return Expression::FPEXT
;
330 case Instruction::PtrToInt
:
331 return Expression::PTRTOINT
;
332 case Instruction::IntToPtr
:
333 return Expression::INTTOPTR
;
334 case Instruction::BitCast
:
335 return Expression::BITCAST
;
337 // THIS SHOULD NEVER HAPPEN
339 assert(0 && "Cast operator with unknown opcode?");
340 return Expression::BITCAST
;
344 Expression
ValueTable::create_expression(BinaryOperator
* BO
) {
347 e
.firstVN
= lookup_or_add(BO
->getOperand(0));
348 e
.secondVN
= lookup_or_add(BO
->getOperand(1));
350 e
.type
= BO
->getType();
351 e
.opcode
= getOpcode(BO
);
356 Expression
ValueTable::create_expression(CmpInst
* C
) {
359 e
.firstVN
= lookup_or_add(C
->getOperand(0));
360 e
.secondVN
= lookup_or_add(C
->getOperand(1));
362 e
.type
= C
->getType();
363 e
.opcode
= getOpcode(C
);
368 Expression
ValueTable::create_expression(CastInst
* C
) {
371 e
.firstVN
= lookup_or_add(C
->getOperand(0));
374 e
.type
= C
->getType();
375 e
.opcode
= getOpcode(C
);
380 Expression
ValueTable::create_expression(ShuffleVectorInst
* S
) {
383 e
.firstVN
= lookup_or_add(S
->getOperand(0));
384 e
.secondVN
= lookup_or_add(S
->getOperand(1));
385 e
.thirdVN
= lookup_or_add(S
->getOperand(2));
386 e
.type
= S
->getType();
387 e
.opcode
= Expression::SHUFFLE
;
392 Expression
ValueTable::create_expression(ExtractElementInst
* E
) {
395 e
.firstVN
= lookup_or_add(E
->getOperand(0));
396 e
.secondVN
= lookup_or_add(E
->getOperand(1));
398 e
.type
= E
->getType();
399 e
.opcode
= Expression::EXTRACT
;
404 Expression
ValueTable::create_expression(InsertElementInst
* I
) {
407 e
.firstVN
= lookup_or_add(I
->getOperand(0));
408 e
.secondVN
= lookup_or_add(I
->getOperand(1));
409 e
.thirdVN
= lookup_or_add(I
->getOperand(2));
410 e
.type
= I
->getType();
411 e
.opcode
= Expression::INSERT
;
416 Expression
ValueTable::create_expression(SelectInst
* I
) {
419 e
.firstVN
= lookup_or_add(I
->getCondition());
420 e
.secondVN
= lookup_or_add(I
->getTrueValue());
421 e
.thirdVN
= lookup_or_add(I
->getFalseValue());
422 e
.type
= I
->getType();
423 e
.opcode
= Expression::SELECT
;
428 Expression
ValueTable::create_expression(GetElementPtrInst
* G
) {
431 e
.firstVN
= lookup_or_add(G
->getPointerOperand());
434 e
.type
= G
->getType();
435 e
.opcode
= Expression::GEP
;
437 for (GetElementPtrInst::op_iterator I
= G
->idx_begin(), E
= G
->idx_end();
439 e
.varargs
.push_back(lookup_or_add(*I
));
444 //===----------------------------------------------------------------------===//
445 // ValueTable External Functions
446 //===----------------------------------------------------------------------===//
448 /// lookup_or_add - Returns the value number for the specified value, assigning
449 /// it a new number if it did not have one before.
450 uint32_t ValueTable::lookup_or_add(Value
* V
) {
451 DenseMap
<Value
*, uint32_t>::iterator VI
= valueNumbering
.find(V
);
452 if (VI
!= valueNumbering
.end())
456 if (BinaryOperator
* BO
= dyn_cast
<BinaryOperator
>(V
)) {
457 Expression e
= create_expression(BO
);
459 DenseMap
<Expression
, uint32_t>::iterator EI
= expressionNumbering
.find(e
);
460 if (EI
!= expressionNumbering
.end()) {
461 valueNumbering
.insert(std::make_pair(V
, EI
->second
));
464 expressionNumbering
.insert(std::make_pair(e
, nextValueNumber
));
465 valueNumbering
.insert(std::make_pair(V
, nextValueNumber
));
467 return nextValueNumber
++;
469 } else if (CmpInst
* C
= dyn_cast
<CmpInst
>(V
)) {
470 Expression e
= create_expression(C
);
472 DenseMap
<Expression
, uint32_t>::iterator EI
= expressionNumbering
.find(e
);
473 if (EI
!= expressionNumbering
.end()) {
474 valueNumbering
.insert(std::make_pair(V
, EI
->second
));
477 expressionNumbering
.insert(std::make_pair(e
, nextValueNumber
));
478 valueNumbering
.insert(std::make_pair(V
, nextValueNumber
));
480 return nextValueNumber
++;
482 } else if (ShuffleVectorInst
* U
= dyn_cast
<ShuffleVectorInst
>(V
)) {
483 Expression e
= create_expression(U
);
485 DenseMap
<Expression
, uint32_t>::iterator EI
= expressionNumbering
.find(e
);
486 if (EI
!= expressionNumbering
.end()) {
487 valueNumbering
.insert(std::make_pair(V
, EI
->second
));
490 expressionNumbering
.insert(std::make_pair(e
, nextValueNumber
));
491 valueNumbering
.insert(std::make_pair(V
, nextValueNumber
));
493 return nextValueNumber
++;
495 } else if (ExtractElementInst
* U
= dyn_cast
<ExtractElementInst
>(V
)) {
496 Expression e
= create_expression(U
);
498 DenseMap
<Expression
, uint32_t>::iterator EI
= expressionNumbering
.find(e
);
499 if (EI
!= expressionNumbering
.end()) {
500 valueNumbering
.insert(std::make_pair(V
, EI
->second
));
503 expressionNumbering
.insert(std::make_pair(e
, nextValueNumber
));
504 valueNumbering
.insert(std::make_pair(V
, nextValueNumber
));
506 return nextValueNumber
++;
508 } else if (InsertElementInst
* U
= dyn_cast
<InsertElementInst
>(V
)) {
509 Expression e
= create_expression(U
);
511 DenseMap
<Expression
, uint32_t>::iterator EI
= expressionNumbering
.find(e
);
512 if (EI
!= expressionNumbering
.end()) {
513 valueNumbering
.insert(std::make_pair(V
, EI
->second
));
516 expressionNumbering
.insert(std::make_pair(e
, nextValueNumber
));
517 valueNumbering
.insert(std::make_pair(V
, nextValueNumber
));
519 return nextValueNumber
++;
521 } else if (SelectInst
* U
= dyn_cast
<SelectInst
>(V
)) {
522 Expression e
= create_expression(U
);
524 DenseMap
<Expression
, uint32_t>::iterator EI
= expressionNumbering
.find(e
);
525 if (EI
!= expressionNumbering
.end()) {
526 valueNumbering
.insert(std::make_pair(V
, EI
->second
));
529 expressionNumbering
.insert(std::make_pair(e
, nextValueNumber
));
530 valueNumbering
.insert(std::make_pair(V
, nextValueNumber
));
532 return nextValueNumber
++;
534 } else if (CastInst
* U
= dyn_cast
<CastInst
>(V
)) {
535 Expression e
= create_expression(U
);
537 DenseMap
<Expression
, uint32_t>::iterator EI
= expressionNumbering
.find(e
);
538 if (EI
!= expressionNumbering
.end()) {
539 valueNumbering
.insert(std::make_pair(V
, EI
->second
));
542 expressionNumbering
.insert(std::make_pair(e
, nextValueNumber
));
543 valueNumbering
.insert(std::make_pair(V
, nextValueNumber
));
545 return nextValueNumber
++;
547 } else if (GetElementPtrInst
* U
= dyn_cast
<GetElementPtrInst
>(V
)) {
548 Expression e
= create_expression(U
);
550 DenseMap
<Expression
, uint32_t>::iterator EI
= expressionNumbering
.find(e
);
551 if (EI
!= expressionNumbering
.end()) {
552 valueNumbering
.insert(std::make_pair(V
, EI
->second
));
555 expressionNumbering
.insert(std::make_pair(e
, nextValueNumber
));
556 valueNumbering
.insert(std::make_pair(V
, nextValueNumber
));
558 return nextValueNumber
++;
561 valueNumbering
.insert(std::make_pair(V
, nextValueNumber
));
562 return nextValueNumber
++;
566 /// lookup - Returns the value number of the specified value. Fails if
567 /// the value has not yet been numbered.
568 uint32_t ValueTable::lookup(Value
* V
) const {
569 DenseMap
<Value
*, uint32_t>::iterator VI
= valueNumbering
.find(V
);
570 if (VI
!= valueNumbering
.end())
573 assert(0 && "Value not numbered?");
578 /// add - Add the specified value with the given value number, removing
579 /// its old number, if any
580 void ValueTable::add(Value
* V
, uint32_t num
) {
581 DenseMap
<Value
*, uint32_t>::iterator VI
= valueNumbering
.find(V
);
582 if (VI
!= valueNumbering
.end())
583 valueNumbering
.erase(VI
);
584 valueNumbering
.insert(std::make_pair(V
, num
));
587 /// clear - Remove all entries from the ValueTable
588 void ValueTable::clear() {
589 valueNumbering
.clear();
590 expressionNumbering
.clear();
594 /// erase - Remove a value from the value numbering
595 void ValueTable::erase(Value
* V
) {
596 valueNumbering
.erase(V
);
599 /// size - Return the number of assigned value numbers
600 unsigned ValueTable::size() {
601 // NOTE: zero is never assigned
602 return nextValueNumber
;
607 //===----------------------------------------------------------------------===//
608 // ValueNumberedSet Class
609 //===----------------------------------------------------------------------===//
611 class ValueNumberedSet
{
613 SmallPtrSet
<Value
*, 8> contents
;
616 ValueNumberedSet() { numbers
.resize(1); }
617 ValueNumberedSet(const ValueNumberedSet
& other
) {
618 numbers
= other
.numbers
;
619 contents
= other
.contents
;
622 typedef SmallPtrSet
<Value
*, 8>::iterator iterator
;
624 iterator
begin() { return contents
.begin(); }
625 iterator
end() { return contents
.end(); }
627 bool insert(Value
* v
) { return contents
.insert(v
); }
628 void insert(iterator I
, iterator E
) { contents
.insert(I
, E
); }
629 void erase(Value
* v
) { contents
.erase(v
); }
630 unsigned count(Value
* v
) { return contents
.count(v
); }
631 size_t size() { return contents
.size(); }
633 void set(unsigned i
) {
634 if (i
>= numbers
.size())
640 void operator=(const ValueNumberedSet
& other
) {
641 contents
= other
.contents
;
642 numbers
= other
.numbers
;
645 void reset(unsigned i
) {
646 if (i
< numbers
.size())
650 bool test(unsigned i
) {
651 if (i
>= numbers
.size())
654 return numbers
.test(i
);
665 //===----------------------------------------------------------------------===//
667 //===----------------------------------------------------------------------===//
671 class VISIBILITY_HIDDEN GVNPRE
: public FunctionPass
{
672 bool runOnFunction(Function
&F
);
674 static char ID
; // Pass identification, replacement for typeid
675 GVNPRE() : FunctionPass(&ID
) {}
679 SmallVector
<Instruction
*, 8> createdExpressions
;
681 DenseMap
<BasicBlock
*, ValueNumberedSet
> availableOut
;
682 DenseMap
<BasicBlock
*, ValueNumberedSet
> anticipatedIn
;
683 DenseMap
<BasicBlock
*, ValueNumberedSet
> generatedPhis
;
685 // This transformation requires dominator postdominator info
686 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
687 AU
.setPreservesCFG();
688 AU
.addRequiredID(BreakCriticalEdgesID
);
689 AU
.addRequired
<UnifyFunctionExitNodes
>();
690 AU
.addRequired
<DominatorTree
>();
694 // FIXME: eliminate or document these better
695 void dump(ValueNumberedSet
& s
) const ;
696 void clean(ValueNumberedSet
& set
) ;
697 Value
* find_leader(ValueNumberedSet
& vals
, uint32_t v
) ;
698 Value
* phi_translate(Value
* V
, BasicBlock
* pred
, BasicBlock
* succ
) ;
699 void phi_translate_set(ValueNumberedSet
& anticIn
, BasicBlock
* pred
,
700 BasicBlock
* succ
, ValueNumberedSet
& out
) ;
702 void topo_sort(ValueNumberedSet
& set
,
703 SmallVector
<Value
*, 8>& vec
) ;
708 void val_insert(ValueNumberedSet
& s
, Value
* v
) ;
709 void val_replace(ValueNumberedSet
& s
, Value
* v
) ;
710 bool dependsOnInvoke(Value
* V
) ;
711 void buildsets_availout(BasicBlock::iterator I
,
712 ValueNumberedSet
& currAvail
,
713 ValueNumberedSet
& currPhis
,
714 ValueNumberedSet
& currExps
,
715 SmallPtrSet
<Value
*, 16>& currTemps
);
716 bool buildsets_anticout(BasicBlock
* BB
,
717 ValueNumberedSet
& anticOut
,
718 SmallPtrSet
<BasicBlock
*, 8>& visited
);
719 unsigned buildsets_anticin(BasicBlock
* BB
,
720 ValueNumberedSet
& anticOut
,
721 ValueNumberedSet
& currExps
,
722 SmallPtrSet
<Value
*, 16>& currTemps
,
723 SmallPtrSet
<BasicBlock
*, 8>& visited
);
724 void buildsets(Function
& F
) ;
726 void insertion_pre(Value
* e
, BasicBlock
* BB
,
727 DenseMap
<BasicBlock
*, Value
*>& avail
,
728 std::map
<BasicBlock
*,ValueNumberedSet
>& new_set
);
729 unsigned insertion_mergepoint(SmallVector
<Value
*, 8>& workList
,
730 df_iterator
<DomTreeNode
*>& D
,
731 std::map
<BasicBlock
*, ValueNumberedSet
>& new_set
);
732 bool insertion(Function
& F
) ;
740 // createGVNPREPass - The public interface to this file...
741 FunctionPass
*llvm::createGVNPREPass() { return new GVNPRE(); }
743 static RegisterPass
<GVNPRE
> X("gvnpre",
744 "Global Value Numbering/Partial Redundancy Elimination");
747 STATISTIC(NumInsertedVals
, "Number of values inserted");
748 STATISTIC(NumInsertedPhis
, "Number of PHI nodes inserted");
749 STATISTIC(NumEliminated
, "Number of redundant instructions eliminated");
751 /// find_leader - Given a set and a value number, return the first
752 /// element of the set with that value number, or 0 if no such element
754 Value
* GVNPRE::find_leader(ValueNumberedSet
& vals
, uint32_t v
) {
758 for (ValueNumberedSet::iterator I
= vals
.begin(), E
= vals
.end();
760 if (v
== VN
.lookup(*I
))
763 assert(0 && "No leader found, but present bit is set?");
767 /// val_insert - Insert a value into a set only if there is not a value
768 /// with the same value number already in the set
769 void GVNPRE::val_insert(ValueNumberedSet
& s
, Value
* v
) {
770 uint32_t num
= VN
.lookup(v
);
775 /// val_replace - Insert a value into a set, replacing any values already in
776 /// the set that have the same value number
777 void GVNPRE::val_replace(ValueNumberedSet
& s
, Value
* v
) {
778 if (s
.count(v
)) return;
780 uint32_t num
= VN
.lookup(v
);
781 Value
* leader
= find_leader(s
, num
);
788 /// phi_translate - Given a value, its parent block, and a predecessor of its
789 /// parent, translate the value into legal for the predecessor block. This
790 /// means translating its operands (and recursively, their operands) through
791 /// any phi nodes in the parent into values available in the predecessor
792 Value
* GVNPRE::phi_translate(Value
* V
, BasicBlock
* pred
, BasicBlock
* succ
) {
797 if (CastInst
* U
= dyn_cast
<CastInst
>(V
)) {
799 if (isa
<Instruction
>(U
->getOperand(0)))
800 newOp1
= phi_translate(U
->getOperand(0), pred
, succ
);
802 newOp1
= U
->getOperand(0);
807 if (newOp1
!= U
->getOperand(0)) {
808 Instruction
* newVal
= 0;
809 if (CastInst
* C
= dyn_cast
<CastInst
>(U
))
810 newVal
= CastInst::Create(C
->getOpcode(),
811 newOp1
, C
->getType(),
812 C
->getName()+".expr");
814 uint32_t v
= VN
.lookup_or_add(newVal
);
816 Value
* leader
= find_leader(availableOut
[pred
], v
);
818 createdExpressions
.push_back(newVal
);
828 } if (isa
<BinaryOperator
>(V
) || isa
<CmpInst
>(V
) ||
829 isa
<ExtractElementInst
>(V
)) {
830 User
* U
= cast
<User
>(V
);
833 if (isa
<Instruction
>(U
->getOperand(0)))
834 newOp1
= phi_translate(U
->getOperand(0), pred
, succ
);
836 newOp1
= U
->getOperand(0);
842 if (isa
<Instruction
>(U
->getOperand(1)))
843 newOp2
= phi_translate(U
->getOperand(1), pred
, succ
);
845 newOp2
= U
->getOperand(1);
850 if (newOp1
!= U
->getOperand(0) || newOp2
!= U
->getOperand(1)) {
851 Instruction
* newVal
= 0;
852 if (BinaryOperator
* BO
= dyn_cast
<BinaryOperator
>(U
))
853 newVal
= BinaryOperator::Create(BO
->getOpcode(),
855 BO
->getName()+".expr");
856 else if (CmpInst
* C
= dyn_cast
<CmpInst
>(U
))
857 newVal
= CmpInst::Create(C
->getOpcode(),
860 C
->getName()+".expr");
861 else if (ExtractElementInst
* E
= dyn_cast
<ExtractElementInst
>(U
))
862 newVal
= new ExtractElementInst(newOp1
, newOp2
, E
->getName()+".expr");
864 uint32_t v
= VN
.lookup_or_add(newVal
);
866 Value
* leader
= find_leader(availableOut
[pred
], v
);
868 createdExpressions
.push_back(newVal
);
877 // Ternary Operations
878 } else if (isa
<ShuffleVectorInst
>(V
) || isa
<InsertElementInst
>(V
) ||
879 isa
<SelectInst
>(V
)) {
880 User
* U
= cast
<User
>(V
);
883 if (isa
<Instruction
>(U
->getOperand(0)))
884 newOp1
= phi_translate(U
->getOperand(0), pred
, succ
);
886 newOp1
= U
->getOperand(0);
892 if (isa
<Instruction
>(U
->getOperand(1)))
893 newOp2
= phi_translate(U
->getOperand(1), pred
, succ
);
895 newOp2
= U
->getOperand(1);
901 if (isa
<Instruction
>(U
->getOperand(2)))
902 newOp3
= phi_translate(U
->getOperand(2), pred
, succ
);
904 newOp3
= U
->getOperand(2);
909 if (newOp1
!= U
->getOperand(0) ||
910 newOp2
!= U
->getOperand(1) ||
911 newOp3
!= U
->getOperand(2)) {
912 Instruction
* newVal
= 0;
913 if (ShuffleVectorInst
* S
= dyn_cast
<ShuffleVectorInst
>(U
))
914 newVal
= new ShuffleVectorInst(newOp1
, newOp2
, newOp3
,
915 S
->getName() + ".expr");
916 else if (InsertElementInst
* I
= dyn_cast
<InsertElementInst
>(U
))
917 newVal
= InsertElementInst::Create(newOp1
, newOp2
, newOp3
,
918 I
->getName() + ".expr");
919 else if (SelectInst
* I
= dyn_cast
<SelectInst
>(U
))
920 newVal
= SelectInst::Create(newOp1
, newOp2
, newOp3
,
921 I
->getName() + ".expr");
923 uint32_t v
= VN
.lookup_or_add(newVal
);
925 Value
* leader
= find_leader(availableOut
[pred
], v
);
927 createdExpressions
.push_back(newVal
);
937 } else if (GetElementPtrInst
* U
= dyn_cast
<GetElementPtrInst
>(V
)) {
939 if (isa
<Instruction
>(U
->getPointerOperand()))
940 newOp1
= phi_translate(U
->getPointerOperand(), pred
, succ
);
942 newOp1
= U
->getPointerOperand();
947 bool changed_idx
= false;
948 SmallVector
<Value
*, 4> newIdx
;
949 for (GetElementPtrInst::op_iterator I
= U
->idx_begin(), E
= U
->idx_end();
951 if (isa
<Instruction
>(*I
)) {
952 Value
* newVal
= phi_translate(*I
, pred
, succ
);
953 newIdx
.push_back(newVal
);
957 newIdx
.push_back(*I
);
960 if (newOp1
!= U
->getPointerOperand() || changed_idx
) {
961 Instruction
* newVal
=
962 GetElementPtrInst::Create(newOp1
,
963 newIdx
.begin(), newIdx
.end(),
964 U
->getName()+".expr");
966 uint32_t v
= VN
.lookup_or_add(newVal
);
968 Value
* leader
= find_leader(availableOut
[pred
], v
);
970 createdExpressions
.push_back(newVal
);
980 } else if (PHINode
* P
= dyn_cast
<PHINode
>(V
)) {
981 if (P
->getParent() == succ
)
982 return P
->getIncomingValueForBlock(pred
);
988 /// phi_translate_set - Perform phi translation on every element of a set
989 void GVNPRE::phi_translate_set(ValueNumberedSet
& anticIn
,
990 BasicBlock
* pred
, BasicBlock
* succ
,
991 ValueNumberedSet
& out
) {
992 for (ValueNumberedSet::iterator I
= anticIn
.begin(),
993 E
= anticIn
.end(); I
!= E
; ++I
) {
994 Value
* V
= phi_translate(*I
, pred
, succ
);
995 if (V
!= 0 && !out
.test(VN
.lookup_or_add(V
))) {
997 out
.set(VN
.lookup(V
));
1002 /// dependsOnInvoke - Test if a value has an phi node as an operand, any of
1003 /// whose inputs is an invoke instruction. If this is true, we cannot safely
1004 /// PRE the instruction or anything that depends on it.
1005 bool GVNPRE::dependsOnInvoke(Value
* V
) {
1006 if (PHINode
* p
= dyn_cast
<PHINode
>(V
)) {
1007 for (PHINode::op_iterator I
= p
->op_begin(), E
= p
->op_end(); I
!= E
; ++I
)
1008 if (isa
<InvokeInst
>(*I
))
1016 /// clean - Remove all non-opaque values from the set whose operands are not
1017 /// themselves in the set, as well as all values that depend on invokes (see
1019 void GVNPRE::clean(ValueNumberedSet
& set
) {
1020 SmallVector
<Value
*, 8> worklist
;
1021 worklist
.reserve(set
.size());
1022 topo_sort(set
, worklist
);
1024 for (unsigned i
= 0; i
< worklist
.size(); ++i
) {
1025 Value
* v
= worklist
[i
];
1028 if (CastInst
* U
= dyn_cast
<CastInst
>(v
)) {
1029 bool lhsValid
= !isa
<Instruction
>(U
->getOperand(0));
1030 lhsValid
|= set
.test(VN
.lookup(U
->getOperand(0)));
1032 lhsValid
= !dependsOnInvoke(U
->getOperand(0));
1036 set
.reset(VN
.lookup(U
));
1039 // Handle binary ops
1040 } else if (isa
<BinaryOperator
>(v
) || isa
<CmpInst
>(v
) ||
1041 isa
<ExtractElementInst
>(v
)) {
1042 User
* U
= cast
<User
>(v
);
1044 bool lhsValid
= !isa
<Instruction
>(U
->getOperand(0));
1045 lhsValid
|= set
.test(VN
.lookup(U
->getOperand(0)));
1047 lhsValid
= !dependsOnInvoke(U
->getOperand(0));
1049 bool rhsValid
= !isa
<Instruction
>(U
->getOperand(1));
1050 rhsValid
|= set
.test(VN
.lookup(U
->getOperand(1)));
1052 rhsValid
= !dependsOnInvoke(U
->getOperand(1));
1054 if (!lhsValid
|| !rhsValid
) {
1056 set
.reset(VN
.lookup(U
));
1059 // Handle ternary ops
1060 } else if (isa
<ShuffleVectorInst
>(v
) || isa
<InsertElementInst
>(v
) ||
1061 isa
<SelectInst
>(v
)) {
1062 User
* U
= cast
<User
>(v
);
1064 bool lhsValid
= !isa
<Instruction
>(U
->getOperand(0));
1065 lhsValid
|= set
.test(VN
.lookup(U
->getOperand(0)));
1067 lhsValid
= !dependsOnInvoke(U
->getOperand(0));
1069 bool rhsValid
= !isa
<Instruction
>(U
->getOperand(1));
1070 rhsValid
|= set
.test(VN
.lookup(U
->getOperand(1)));
1072 rhsValid
= !dependsOnInvoke(U
->getOperand(1));
1074 bool thirdValid
= !isa
<Instruction
>(U
->getOperand(2));
1075 thirdValid
|= set
.test(VN
.lookup(U
->getOperand(2)));
1077 thirdValid
= !dependsOnInvoke(U
->getOperand(2));
1079 if (!lhsValid
|| !rhsValid
|| !thirdValid
) {
1081 set
.reset(VN
.lookup(U
));
1084 // Handle varargs ops
1085 } else if (GetElementPtrInst
* U
= dyn_cast
<GetElementPtrInst
>(v
)) {
1086 bool ptrValid
= !isa
<Instruction
>(U
->getPointerOperand());
1087 ptrValid
|= set
.test(VN
.lookup(U
->getPointerOperand()));
1089 ptrValid
= !dependsOnInvoke(U
->getPointerOperand());
1091 bool varValid
= true;
1092 for (GetElementPtrInst::op_iterator I
= U
->idx_begin(), E
= U
->idx_end();
1095 varValid
&= !isa
<Instruction
>(*I
) || set
.test(VN
.lookup(*I
));
1096 varValid
&= !dependsOnInvoke(*I
);
1099 if (!ptrValid
|| !varValid
) {
1101 set
.reset(VN
.lookup(U
));
1107 /// topo_sort - Given a set of values, sort them by topological
1108 /// order into the provided vector.
1109 void GVNPRE::topo_sort(ValueNumberedSet
& set
, SmallVector
<Value
*, 8>& vec
) {
1110 SmallPtrSet
<Value
*, 16> visited
;
1111 SmallVector
<Value
*, 8> stack
;
1112 for (ValueNumberedSet::iterator I
= set
.begin(), E
= set
.end();
1114 if (visited
.count(*I
) == 0)
1115 stack
.push_back(*I
);
1117 while (!stack
.empty()) {
1118 Value
* e
= stack
.back();
1121 if (CastInst
* U
= dyn_cast
<CastInst
>(e
)) {
1122 Value
* l
= find_leader(set
, VN
.lookup(U
->getOperand(0)));
1124 if (l
!= 0 && isa
<Instruction
>(l
) &&
1125 visited
.count(l
) == 0)
1133 // Handle binary ops
1134 } else if (isa
<BinaryOperator
>(e
) || isa
<CmpInst
>(e
) ||
1135 isa
<ExtractElementInst
>(e
)) {
1136 User
* U
= cast
<User
>(e
);
1137 Value
* l
= find_leader(set
, VN
.lookup(U
->getOperand(0)));
1138 Value
* r
= find_leader(set
, VN
.lookup(U
->getOperand(1)));
1140 if (l
!= 0 && isa
<Instruction
>(l
) &&
1141 visited
.count(l
) == 0)
1143 else if (r
!= 0 && isa
<Instruction
>(r
) &&
1144 visited
.count(r
) == 0)
1152 // Handle ternary ops
1153 } else if (isa
<InsertElementInst
>(e
) || isa
<ShuffleVectorInst
>(e
) ||
1154 isa
<SelectInst
>(e
)) {
1155 User
* U
= cast
<User
>(e
);
1156 Value
* l
= find_leader(set
, VN
.lookup(U
->getOperand(0)));
1157 Value
* r
= find_leader(set
, VN
.lookup(U
->getOperand(1)));
1158 Value
* m
= find_leader(set
, VN
.lookup(U
->getOperand(2)));
1160 if (l
!= 0 && isa
<Instruction
>(l
) &&
1161 visited
.count(l
) == 0)
1163 else if (r
!= 0 && isa
<Instruction
>(r
) &&
1164 visited
.count(r
) == 0)
1166 else if (m
!= 0 && isa
<Instruction
>(m
) &&
1167 visited
.count(m
) == 0)
1175 // Handle vararg ops
1176 } else if (GetElementPtrInst
* U
= dyn_cast
<GetElementPtrInst
>(e
)) {
1177 Value
* p
= find_leader(set
, VN
.lookup(U
->getPointerOperand()));
1179 if (p
!= 0 && isa
<Instruction
>(p
) &&
1180 visited
.count(p
) == 0)
1183 bool push_va
= false;
1184 for (GetElementPtrInst::op_iterator I
= U
->idx_begin(),
1185 E
= U
->idx_end(); I
!= E
; ++I
) {
1186 Value
* v
= find_leader(set
, VN
.lookup(*I
));
1187 if (v
!= 0 && isa
<Instruction
>(v
) && visited
.count(v
) == 0) {
1200 // Handle opaque ops
1212 /// dump - Dump a set of values to standard error
1213 void GVNPRE::dump(ValueNumberedSet
& s
) const {
1215 for (ValueNumberedSet::iterator I
= s
.begin(), E
= s
.end();
1217 DOUT
<< "" << VN
.lookup(*I
) << ": ";
1218 DEBUG((*I
)->dump());
1223 /// elimination - Phase 3 of the main algorithm. Perform full redundancy
1224 /// elimination by walking the dominator tree and removing any instruction that
1225 /// is dominated by another instruction with the same value number.
1226 bool GVNPRE::elimination() {
1227 bool changed_function
= false;
1229 SmallVector
<std::pair
<Instruction
*, Value
*>, 8> replace
;
1230 SmallVector
<Instruction
*, 8> erase
;
1232 DominatorTree
& DT
= getAnalysis
<DominatorTree
>();
1234 for (df_iterator
<DomTreeNode
*> DI
= df_begin(DT
.getRootNode()),
1235 E
= df_end(DT
.getRootNode()); DI
!= E
; ++DI
) {
1236 BasicBlock
* BB
= DI
->getBlock();
1238 for (BasicBlock::iterator BI
= BB
->begin(), BE
= BB
->end();
1241 if (isa
<BinaryOperator
>(BI
) || isa
<CmpInst
>(BI
) ||
1242 isa
<ShuffleVectorInst
>(BI
) || isa
<InsertElementInst
>(BI
) ||
1243 isa
<ExtractElementInst
>(BI
) || isa
<SelectInst
>(BI
) ||
1244 isa
<CastInst
>(BI
) || isa
<GetElementPtrInst
>(BI
)) {
1246 if (availableOut
[BB
].test(VN
.lookup(BI
)) &&
1247 !availableOut
[BB
].count(BI
)) {
1248 Value
*leader
= find_leader(availableOut
[BB
], VN
.lookup(BI
));
1249 if (Instruction
* Instr
= dyn_cast
<Instruction
>(leader
))
1250 if (Instr
->getParent() != 0 && Instr
!= BI
) {
1251 replace
.push_back(std::make_pair(BI
, leader
));
1252 erase
.push_back(BI
);
1260 while (!replace
.empty()) {
1261 std::pair
<Instruction
*, Value
*> rep
= replace
.back();
1263 rep
.first
->replaceAllUsesWith(rep
.second
);
1264 changed_function
= true;
1267 for (SmallVector
<Instruction
*, 8>::iterator I
= erase
.begin(),
1268 E
= erase
.end(); I
!= E
; ++I
)
1269 (*I
)->eraseFromParent();
1271 return changed_function
;
1274 /// cleanup - Delete any extraneous values that were created to represent
1275 /// expressions without leaders.
1276 void GVNPRE::cleanup() {
1277 while (!createdExpressions
.empty()) {
1278 Instruction
* I
= createdExpressions
.back();
1279 createdExpressions
.pop_back();
1285 /// buildsets_availout - When calculating availability, handle an instruction
1286 /// by inserting it into the appropriate sets
1287 void GVNPRE::buildsets_availout(BasicBlock::iterator I
,
1288 ValueNumberedSet
& currAvail
,
1289 ValueNumberedSet
& currPhis
,
1290 ValueNumberedSet
& currExps
,
1291 SmallPtrSet
<Value
*, 16>& currTemps
) {
1293 if (PHINode
* p
= dyn_cast
<PHINode
>(I
)) {
1294 unsigned num
= VN
.lookup_or_add(p
);
1300 } else if (CastInst
* U
= dyn_cast
<CastInst
>(I
)) {
1301 Value
* leftValue
= U
->getOperand(0);
1303 unsigned num
= VN
.lookup_or_add(U
);
1305 if (isa
<Instruction
>(leftValue
))
1306 if (!currExps
.test(VN
.lookup(leftValue
))) {
1307 currExps
.insert(leftValue
);
1308 currExps
.set(VN
.lookup(leftValue
));
1311 if (!currExps
.test(num
)) {
1316 // Handle binary ops
1317 } else if (isa
<BinaryOperator
>(I
) || isa
<CmpInst
>(I
) ||
1318 isa
<ExtractElementInst
>(I
)) {
1319 User
* U
= cast
<User
>(I
);
1320 Value
* leftValue
= U
->getOperand(0);
1321 Value
* rightValue
= U
->getOperand(1);
1323 unsigned num
= VN
.lookup_or_add(U
);
1325 if (isa
<Instruction
>(leftValue
))
1326 if (!currExps
.test(VN
.lookup(leftValue
))) {
1327 currExps
.insert(leftValue
);
1328 currExps
.set(VN
.lookup(leftValue
));
1331 if (isa
<Instruction
>(rightValue
))
1332 if (!currExps
.test(VN
.lookup(rightValue
))) {
1333 currExps
.insert(rightValue
);
1334 currExps
.set(VN
.lookup(rightValue
));
1337 if (!currExps
.test(num
)) {
1342 // Handle ternary ops
1343 } else if (isa
<InsertElementInst
>(I
) || isa
<ShuffleVectorInst
>(I
) ||
1344 isa
<SelectInst
>(I
)) {
1345 User
* U
= cast
<User
>(I
);
1346 Value
* leftValue
= U
->getOperand(0);
1347 Value
* rightValue
= U
->getOperand(1);
1348 Value
* thirdValue
= U
->getOperand(2);
1350 VN
.lookup_or_add(U
);
1352 unsigned num
= VN
.lookup_or_add(U
);
1354 if (isa
<Instruction
>(leftValue
))
1355 if (!currExps
.test(VN
.lookup(leftValue
))) {
1356 currExps
.insert(leftValue
);
1357 currExps
.set(VN
.lookup(leftValue
));
1359 if (isa
<Instruction
>(rightValue
))
1360 if (!currExps
.test(VN
.lookup(rightValue
))) {
1361 currExps
.insert(rightValue
);
1362 currExps
.set(VN
.lookup(rightValue
));
1364 if (isa
<Instruction
>(thirdValue
))
1365 if (!currExps
.test(VN
.lookup(thirdValue
))) {
1366 currExps
.insert(thirdValue
);
1367 currExps
.set(VN
.lookup(thirdValue
));
1370 if (!currExps
.test(num
)) {
1375 // Handle vararg ops
1376 } else if (GetElementPtrInst
* U
= dyn_cast
<GetElementPtrInst
>(I
)) {
1377 Value
* ptrValue
= U
->getPointerOperand();
1379 VN
.lookup_or_add(U
);
1381 unsigned num
= VN
.lookup_or_add(U
);
1383 if (isa
<Instruction
>(ptrValue
))
1384 if (!currExps
.test(VN
.lookup(ptrValue
))) {
1385 currExps
.insert(ptrValue
);
1386 currExps
.set(VN
.lookup(ptrValue
));
1389 for (GetElementPtrInst::op_iterator OI
= U
->idx_begin(), OE
= U
->idx_end();
1391 if (isa
<Instruction
>(*OI
) && !currExps
.test(VN
.lookup(*OI
))) {
1392 currExps
.insert(*OI
);
1393 currExps
.set(VN
.lookup(*OI
));
1396 if (!currExps
.test(VN
.lookup(U
))) {
1401 // Handle opaque ops
1402 } else if (!I
->isTerminator()){
1403 VN
.lookup_or_add(I
);
1405 currTemps
.insert(I
);
1408 if (!I
->isTerminator())
1409 if (!currAvail
.test(VN
.lookup(I
))) {
1410 currAvail
.insert(I
);
1411 currAvail
.set(VN
.lookup(I
));
1415 /// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT
1416 /// set as a function of the ANTIC_IN set of the block's predecessors
1417 bool GVNPRE::buildsets_anticout(BasicBlock
* BB
,
1418 ValueNumberedSet
& anticOut
,
1419 SmallPtrSet
<BasicBlock
*, 8>& visited
) {
1420 if (BB
->getTerminator()->getNumSuccessors() == 1) {
1421 if (BB
->getTerminator()->getSuccessor(0) != BB
&&
1422 visited
.count(BB
->getTerminator()->getSuccessor(0)) == 0) {
1426 phi_translate_set(anticipatedIn
[BB
->getTerminator()->getSuccessor(0)],
1427 BB
, BB
->getTerminator()->getSuccessor(0), anticOut
);
1429 } else if (BB
->getTerminator()->getNumSuccessors() > 1) {
1430 BasicBlock
* first
= BB
->getTerminator()->getSuccessor(0);
1431 for (ValueNumberedSet::iterator I
= anticipatedIn
[first
].begin(),
1432 E
= anticipatedIn
[first
].end(); I
!= E
; ++I
) {
1433 anticOut
.insert(*I
);
1434 anticOut
.set(VN
.lookup(*I
));
1437 for (unsigned i
= 1; i
< BB
->getTerminator()->getNumSuccessors(); ++i
) {
1438 BasicBlock
* currSucc
= BB
->getTerminator()->getSuccessor(i
);
1439 ValueNumberedSet
& succAnticIn
= anticipatedIn
[currSucc
];
1441 SmallVector
<Value
*, 16> temp
;
1443 for (ValueNumberedSet::iterator I
= anticOut
.begin(),
1444 E
= anticOut
.end(); I
!= E
; ++I
)
1445 if (!succAnticIn
.test(VN
.lookup(*I
)))
1448 for (SmallVector
<Value
*, 16>::iterator I
= temp
.begin(), E
= temp
.end();
1451 anticOut
.reset(VN
.lookup(*I
));
1459 /// buildsets_anticin - Walk the postdom tree, calculating ANTIC_OUT for
1460 /// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN
1461 /// sets populated in buildsets_availout
1462 unsigned GVNPRE::buildsets_anticin(BasicBlock
* BB
,
1463 ValueNumberedSet
& anticOut
,
1464 ValueNumberedSet
& currExps
,
1465 SmallPtrSet
<Value
*, 16>& currTemps
,
1466 SmallPtrSet
<BasicBlock
*, 8>& visited
) {
1467 ValueNumberedSet
& anticIn
= anticipatedIn
[BB
];
1468 unsigned old
= anticIn
.size();
1470 bool defer
= buildsets_anticout(BB
, anticOut
, visited
);
1476 for (ValueNumberedSet::iterator I
= anticOut
.begin(),
1477 E
= anticOut
.end(); I
!= E
; ++I
) {
1479 anticIn
.set(VN
.lookup(*I
));
1481 for (ValueNumberedSet::iterator I
= currExps
.begin(),
1482 E
= currExps
.end(); I
!= E
; ++I
) {
1483 if (!anticIn
.test(VN
.lookup(*I
))) {
1485 anticIn
.set(VN
.lookup(*I
));
1489 for (SmallPtrSet
<Value
*, 16>::iterator I
= currTemps
.begin(),
1490 E
= currTemps
.end(); I
!= E
; ++I
) {
1492 anticIn
.reset(VN
.lookup(*I
));
1498 if (old
!= anticIn
.size())
1504 /// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT
1505 /// and the ANTIC_IN sets.
1506 void GVNPRE::buildsets(Function
& F
) {
1507 DenseMap
<BasicBlock
*, ValueNumberedSet
> generatedExpressions
;
1508 DenseMap
<BasicBlock
*, SmallPtrSet
<Value
*, 16> > generatedTemporaries
;
1510 DominatorTree
&DT
= getAnalysis
<DominatorTree
>();
1512 // Phase 1, Part 1: calculate AVAIL_OUT
1514 // Top-down walk of the dominator tree
1515 for (df_iterator
<DomTreeNode
*> DI
= df_begin(DT
.getRootNode()),
1516 E
= df_end(DT
.getRootNode()); DI
!= E
; ++DI
) {
1518 // Get the sets to update for this block
1519 ValueNumberedSet
& currExps
= generatedExpressions
[DI
->getBlock()];
1520 ValueNumberedSet
& currPhis
= generatedPhis
[DI
->getBlock()];
1521 SmallPtrSet
<Value
*, 16>& currTemps
= generatedTemporaries
[DI
->getBlock()];
1522 ValueNumberedSet
& currAvail
= availableOut
[DI
->getBlock()];
1524 BasicBlock
* BB
= DI
->getBlock();
1526 // A block inherits AVAIL_OUT from its dominator
1527 if (DI
->getIDom() != 0)
1528 currAvail
= availableOut
[DI
->getIDom()->getBlock()];
1530 for (BasicBlock::iterator BI
= BB
->begin(), BE
= BB
->end();
1532 buildsets_availout(BI
, currAvail
, currPhis
, currExps
,
1537 // Phase 1, Part 2: calculate ANTIC_IN
1539 SmallPtrSet
<BasicBlock
*, 8> visited
;
1540 SmallPtrSet
<BasicBlock
*, 4> block_changed
;
1541 for (Function::iterator FI
= F
.begin(), FE
= F
.end(); FI
!= FE
; ++FI
)
1542 block_changed
.insert(FI
);
1544 bool changed
= true;
1545 unsigned iterations
= 0;
1549 ValueNumberedSet anticOut
;
1551 // Postorder walk of the CFG
1552 for (po_iterator
<BasicBlock
*> BBI
= po_begin(&F
.getEntryBlock()),
1553 BBE
= po_end(&F
.getEntryBlock()); BBI
!= BBE
; ++BBI
) {
1554 BasicBlock
* BB
= *BBI
;
1556 if (block_changed
.count(BB
) != 0) {
1557 unsigned ret
= buildsets_anticin(BB
, anticOut
,generatedExpressions
[BB
],
1558 generatedTemporaries
[BB
], visited
);
1567 for (pred_iterator PI
= pred_begin(BB
), PE
= pred_end(BB
);
1569 block_changed
.insert(*PI
);
1572 block_changed
.erase(BB
);
1574 changed
|= (ret
== 2);
1583 /// insertion_pre - When a partial redundancy has been identified, eliminate it
1584 /// by inserting appropriate values into the predecessors and a phi node in
1586 void GVNPRE::insertion_pre(Value
* e
, BasicBlock
* BB
,
1587 DenseMap
<BasicBlock
*, Value
*>& avail
,
1588 std::map
<BasicBlock
*, ValueNumberedSet
>& new_sets
) {
1589 for (pred_iterator PI
= pred_begin(BB
), PE
= pred_end(BB
); PI
!= PE
; ++PI
) {
1590 Value
* e2
= avail
[*PI
];
1591 if (!availableOut
[*PI
].test(VN
.lookup(e2
))) {
1592 User
* U
= cast
<User
>(e2
);
1595 if (isa
<BinaryOperator
>(U
->getOperand(0)) ||
1596 isa
<CmpInst
>(U
->getOperand(0)) ||
1597 isa
<ShuffleVectorInst
>(U
->getOperand(0)) ||
1598 isa
<ExtractElementInst
>(U
->getOperand(0)) ||
1599 isa
<InsertElementInst
>(U
->getOperand(0)) ||
1600 isa
<SelectInst
>(U
->getOperand(0)) ||
1601 isa
<CastInst
>(U
->getOperand(0)) ||
1602 isa
<GetElementPtrInst
>(U
->getOperand(0)))
1603 s1
= find_leader(availableOut
[*PI
], VN
.lookup(U
->getOperand(0)));
1605 s1
= U
->getOperand(0);
1609 if (isa
<BinaryOperator
>(U
) ||
1611 isa
<ShuffleVectorInst
>(U
) ||
1612 isa
<ExtractElementInst
>(U
) ||
1613 isa
<InsertElementInst
>(U
) ||
1614 isa
<SelectInst
>(U
)) {
1615 if (isa
<BinaryOperator
>(U
->getOperand(1)) ||
1616 isa
<CmpInst
>(U
->getOperand(1)) ||
1617 isa
<ShuffleVectorInst
>(U
->getOperand(1)) ||
1618 isa
<ExtractElementInst
>(U
->getOperand(1)) ||
1619 isa
<InsertElementInst
>(U
->getOperand(1)) ||
1620 isa
<SelectInst
>(U
->getOperand(1)) ||
1621 isa
<CastInst
>(U
->getOperand(1)) ||
1622 isa
<GetElementPtrInst
>(U
->getOperand(1))) {
1623 s2
= find_leader(availableOut
[*PI
], VN
.lookup(U
->getOperand(1)));
1625 s2
= U
->getOperand(1);
1629 // Ternary Operators
1631 if (isa
<ShuffleVectorInst
>(U
) ||
1632 isa
<InsertElementInst
>(U
) ||
1633 isa
<SelectInst
>(U
)) {
1634 if (isa
<BinaryOperator
>(U
->getOperand(2)) ||
1635 isa
<CmpInst
>(U
->getOperand(2)) ||
1636 isa
<ShuffleVectorInst
>(U
->getOperand(2)) ||
1637 isa
<ExtractElementInst
>(U
->getOperand(2)) ||
1638 isa
<InsertElementInst
>(U
->getOperand(2)) ||
1639 isa
<SelectInst
>(U
->getOperand(2)) ||
1640 isa
<CastInst
>(U
->getOperand(2)) ||
1641 isa
<GetElementPtrInst
>(U
->getOperand(2))) {
1642 s3
= find_leader(availableOut
[*PI
], VN
.lookup(U
->getOperand(2)));
1644 s3
= U
->getOperand(2);
1649 SmallVector
<Value
*, 4> sVarargs
;
1650 if (GetElementPtrInst
* G
= dyn_cast
<GetElementPtrInst
>(U
)) {
1651 for (GetElementPtrInst::op_iterator OI
= G
->idx_begin(),
1652 OE
= G
->idx_end(); OI
!= OE
; ++OI
) {
1653 if (isa
<BinaryOperator
>(*OI
) ||
1654 isa
<CmpInst
>(*OI
) ||
1655 isa
<ShuffleVectorInst
>(*OI
) ||
1656 isa
<ExtractElementInst
>(*OI
) ||
1657 isa
<InsertElementInst
>(*OI
) ||
1658 isa
<SelectInst
>(*OI
) ||
1659 isa
<CastInst
>(*OI
) ||
1660 isa
<GetElementPtrInst
>(*OI
)) {
1661 sVarargs
.push_back(find_leader(availableOut
[*PI
],
1664 sVarargs
.push_back(*OI
);
1670 if (BinaryOperator
* BO
= dyn_cast
<BinaryOperator
>(U
))
1671 newVal
= BinaryOperator::Create(BO
->getOpcode(), s1
, s2
,
1672 BO
->getName()+".gvnpre",
1673 (*PI
)->getTerminator());
1674 else if (CmpInst
* C
= dyn_cast
<CmpInst
>(U
))
1675 newVal
= CmpInst::Create(C
->getOpcode(), C
->getPredicate(), s1
, s2
,
1676 C
->getName()+".gvnpre",
1677 (*PI
)->getTerminator());
1678 else if (ShuffleVectorInst
* S
= dyn_cast
<ShuffleVectorInst
>(U
))
1679 newVal
= new ShuffleVectorInst(s1
, s2
, s3
, S
->getName()+".gvnpre",
1680 (*PI
)->getTerminator());
1681 else if (InsertElementInst
* S
= dyn_cast
<InsertElementInst
>(U
))
1682 newVal
= InsertElementInst::Create(s1
, s2
, s3
, S
->getName()+".gvnpre",
1683 (*PI
)->getTerminator());
1684 else if (ExtractElementInst
* S
= dyn_cast
<ExtractElementInst
>(U
))
1685 newVal
= new ExtractElementInst(s1
, s2
, S
->getName()+".gvnpre",
1686 (*PI
)->getTerminator());
1687 else if (SelectInst
* S
= dyn_cast
<SelectInst
>(U
))
1688 newVal
= SelectInst::Create(s1
, s2
, s3
, S
->getName()+".gvnpre",
1689 (*PI
)->getTerminator());
1690 else if (CastInst
* C
= dyn_cast
<CastInst
>(U
))
1691 newVal
= CastInst::Create(C
->getOpcode(), s1
, C
->getType(),
1692 C
->getName()+".gvnpre",
1693 (*PI
)->getTerminator());
1694 else if (GetElementPtrInst
* G
= dyn_cast
<GetElementPtrInst
>(U
))
1695 newVal
= GetElementPtrInst::Create(s1
, sVarargs
.begin(), sVarargs
.end(),
1696 G
->getName()+".gvnpre",
1697 (*PI
)->getTerminator());
1699 VN
.add(newVal
, VN
.lookup(U
));
1701 ValueNumberedSet
& predAvail
= availableOut
[*PI
];
1702 val_replace(predAvail
, newVal
);
1703 val_replace(new_sets
[*PI
], newVal
);
1704 predAvail
.set(VN
.lookup(newVal
));
1706 DenseMap
<BasicBlock
*, Value
*>::iterator av
= avail
.find(*PI
);
1707 if (av
!= avail
.end())
1709 avail
.insert(std::make_pair(*PI
, newVal
));
1717 for (pred_iterator PI
= pred_begin(BB
), PE
= pred_end(BB
); PI
!= PE
; ++PI
) {
1719 p
= PHINode::Create(avail
[*PI
]->getType(), "gvnpre-join", BB
->begin());
1721 p
->addIncoming(avail
[*PI
], *PI
);
1724 VN
.add(p
, VN
.lookup(e
));
1725 val_replace(availableOut
[BB
], p
);
1726 availableOut
[BB
].set(VN
.lookup(e
));
1727 generatedPhis
[BB
].insert(p
);
1728 generatedPhis
[BB
].set(VN
.lookup(e
));
1729 new_sets
[BB
].insert(p
);
1730 new_sets
[BB
].set(VN
.lookup(e
));
1735 /// insertion_mergepoint - When walking the dom tree, check at each merge
1736 /// block for the possibility of a partial redundancy. If present, eliminate it
1737 unsigned GVNPRE::insertion_mergepoint(SmallVector
<Value
*, 8>& workList
,
1738 df_iterator
<DomTreeNode
*>& D
,
1739 std::map
<BasicBlock
*, ValueNumberedSet
>& new_sets
) {
1740 bool changed_function
= false;
1741 bool new_stuff
= false;
1743 BasicBlock
* BB
= D
->getBlock();
1744 for (unsigned i
= 0; i
< workList
.size(); ++i
) {
1745 Value
* e
= workList
[i
];
1747 if (isa
<BinaryOperator
>(e
) || isa
<CmpInst
>(e
) ||
1748 isa
<ExtractElementInst
>(e
) || isa
<InsertElementInst
>(e
) ||
1749 isa
<ShuffleVectorInst
>(e
) || isa
<SelectInst
>(e
) || isa
<CastInst
>(e
) ||
1750 isa
<GetElementPtrInst
>(e
)) {
1751 if (availableOut
[D
->getIDom()->getBlock()].test(VN
.lookup(e
)))
1754 DenseMap
<BasicBlock
*, Value
*> avail
;
1755 bool by_some
= false;
1756 bool all_same
= true;
1757 Value
* first_s
= 0;
1759 for (pred_iterator PI
= pred_begin(BB
), PE
= pred_end(BB
); PI
!= PE
;
1761 Value
*e2
= phi_translate(e
, *PI
, BB
);
1762 Value
*e3
= find_leader(availableOut
[*PI
], VN
.lookup(e2
));
1765 DenseMap
<BasicBlock
*, Value
*>::iterator av
= avail
.find(*PI
);
1766 if (av
!= avail
.end())
1768 avail
.insert(std::make_pair(*PI
, e2
));
1771 DenseMap
<BasicBlock
*, Value
*>::iterator av
= avail
.find(*PI
);
1772 if (av
!= avail
.end())
1774 avail
.insert(std::make_pair(*PI
, e3
));
1779 else if (first_s
!= e3
)
1784 if (by_some
&& !all_same
&&
1785 !generatedPhis
[BB
].test(VN
.lookup(e
))) {
1786 insertion_pre(e
, BB
, avail
, new_sets
);
1788 changed_function
= true;
1794 unsigned retval
= 0;
1795 if (changed_function
)
1803 /// insert - Phase 2 of the main algorithm. Walk the dominator tree looking for
1804 /// merge points. When one is found, check for a partial redundancy. If one is
1805 /// present, eliminate it. Repeat this walk until no changes are made.
1806 bool GVNPRE::insertion(Function
& F
) {
1807 bool changed_function
= false;
1809 DominatorTree
&DT
= getAnalysis
<DominatorTree
>();
1811 std::map
<BasicBlock
*, ValueNumberedSet
> new_sets
;
1812 bool new_stuff
= true;
1815 for (df_iterator
<DomTreeNode
*> DI
= df_begin(DT
.getRootNode()),
1816 E
= df_end(DT
.getRootNode()); DI
!= E
; ++DI
) {
1817 BasicBlock
* BB
= DI
->getBlock();
1822 ValueNumberedSet
& availOut
= availableOut
[BB
];
1823 ValueNumberedSet
& anticIn
= anticipatedIn
[BB
];
1825 // Replace leaders with leaders inherited from dominator
1826 if (DI
->getIDom() != 0) {
1827 ValueNumberedSet
& dom_set
= new_sets
[DI
->getIDom()->getBlock()];
1828 for (ValueNumberedSet::iterator I
= dom_set
.begin(),
1829 E
= dom_set
.end(); I
!= E
; ++I
) {
1830 val_replace(new_sets
[BB
], *I
);
1831 val_replace(availOut
, *I
);
1835 // If there is more than one predecessor...
1836 if (pred_begin(BB
) != pred_end(BB
) && ++pred_begin(BB
) != pred_end(BB
)) {
1837 SmallVector
<Value
*, 8> workList
;
1838 workList
.reserve(anticIn
.size());
1839 topo_sort(anticIn
, workList
);
1841 unsigned result
= insertion_mergepoint(workList
, DI
, new_sets
);
1843 changed_function
= true;
1850 return changed_function
;
1853 // GVNPRE::runOnFunction - This is the main transformation entry point for a
1856 bool GVNPRE::runOnFunction(Function
&F
) {
1857 // Clean out global sets from any previous functions
1859 createdExpressions
.clear();
1860 availableOut
.clear();
1861 anticipatedIn
.clear();
1862 generatedPhis
.clear();
1864 bool changed_function
= false;
1866 // Phase 1: BuildSets
1867 // This phase calculates the AVAIL_OUT and ANTIC_IN sets
1871 // This phase inserts values to make partially redundant values
1873 changed_function
|= insertion(F
);
1875 // Phase 3: Eliminate
1876 // This phase performs trivial full redundancy elimination
1877 changed_function
|= elimination();
1880 // This phase cleans up values that were created solely
1881 // as leaders for expressions
1884 return changed_function
;