blob: 0fa7181c489ac8b3123bab72a353024e831cc7f7 [file] [log] [blame]
Davide Italiano7e274e02016-12-22 16:03:48 +00001//===---- NewGVN.cpp - Global Value Numbering Pass --------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file implements the new LLVM's Global Value Numbering pass.
11/// GVN partitions values computed by a function into congruence classes.
12/// Values ending up in the same congruence class are guaranteed to be the same
13/// for every execution of the program. In that respect, congruency is a
14/// compile-time approximation of equivalence of values at runtime.
15/// The algorithm implemented here uses a sparse formulation and it's based
16/// on the ideas described in the paper:
17/// "A Sparse Algorithm for Predicated Global Value Numbering" from
18/// Karthik Gargi.
19///
20//===----------------------------------------------------------------------===//
21
22#include "llvm/Transforms/Scalar/NewGVN.h"
23#include "llvm/ADT/BitVector.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/DenseSet.h"
26#include "llvm/ADT/DepthFirstIterator.h"
27#include "llvm/ADT/Hashing.h"
28#include "llvm/ADT/MapVector.h"
29#include "llvm/ADT/PostOrderIterator.h"
30#include "llvm/ADT/SmallPtrSet.h"
31#include "llvm/ADT/SmallSet.h"
32#include "llvm/ADT/SparseBitVector.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/TinyPtrVector.h"
35#include "llvm/Analysis/AliasAnalysis.h"
36#include "llvm/Analysis/AssumptionCache.h"
37#include "llvm/Analysis/CFG.h"
38#include "llvm/Analysis/CFGPrinter.h"
39#include "llvm/Analysis/ConstantFolding.h"
40#include "llvm/Analysis/GlobalsModRef.h"
41#include "llvm/Analysis/InstructionSimplify.h"
42#include "llvm/Analysis/Loads.h"
43#include "llvm/Analysis/MemoryBuiltins.h"
44#include "llvm/Analysis/MemoryDependenceAnalysis.h"
45#include "llvm/Analysis/MemoryLocation.h"
46#include "llvm/Analysis/PHITransAddr.h"
47#include "llvm/Analysis/TargetLibraryInfo.h"
48#include "llvm/Analysis/ValueTracking.h"
49#include "llvm/IR/DataLayout.h"
50#include "llvm/IR/Dominators.h"
51#include "llvm/IR/GlobalVariable.h"
52#include "llvm/IR/IRBuilder.h"
53#include "llvm/IR/IntrinsicInst.h"
54#include "llvm/IR/LLVMContext.h"
55#include "llvm/IR/Metadata.h"
56#include "llvm/IR/PatternMatch.h"
57#include "llvm/IR/PredIteratorCache.h"
58#include "llvm/IR/Type.h"
59#include "llvm/Support/Allocator.h"
60#include "llvm/Support/CommandLine.h"
61#include "llvm/Support/Debug.h"
62#include "llvm/Transforms/Scalar.h"
63#include "llvm/Transforms/Scalar/GVNExpression.h"
64#include "llvm/Transforms/Utils/BasicBlockUtils.h"
65#include "llvm/Transforms/Utils/Local.h"
66#include "llvm/Transforms/Utils/MemorySSA.h"
67#include "llvm/Transforms/Utils/SSAUpdater.h"
68#include <unordered_map>
69#include <utility>
70#include <vector>
71using namespace llvm;
72using namespace PatternMatch;
73using namespace llvm::GVNExpression;
74
75#define DEBUG_TYPE "newgvn"
76
77STATISTIC(NumGVNInstrDeleted, "Number of instructions deleted");
78STATISTIC(NumGVNBlocksDeleted, "Number of blocks deleted");
79STATISTIC(NumGVNOpsSimplified, "Number of Expressions simplified");
80STATISTIC(NumGVNPhisAllSame, "Number of PHIs whos arguments are all the same");
81
82//===----------------------------------------------------------------------===//
83// GVN Pass
84//===----------------------------------------------------------------------===//
85
86// Anchor methods.
87namespace llvm {
88namespace GVNExpression {
89 Expression::~Expression() = default;
90 BasicExpression::~BasicExpression() = default;
91 CallExpression::~CallExpression() = default;
92 LoadExpression::~LoadExpression() = default;
93 StoreExpression::~StoreExpression() = default;
94 AggregateValueExpression::~AggregateValueExpression() = default;
95 PHIExpression::~PHIExpression() = default;
96}
97}
98
99// Congruence classes represent the set of expressions/instructions
100// that are all the same *during some scope in the function*.
101// That is, because of the way we perform equality propagation, and
102// because of memory value numbering, it is not correct to assume
103// you can willy-nilly replace any member with any other at any
104// point in the function.
105//
106// For any Value in the Member set, it is valid to replace any dominated member
107// with that Value.
108//
109// Every congruence class has a leader, and the leader is used to
110// symbolize instructions in a canonical way (IE every operand of an
111// instruction that is a member of the same congruence class will
112// always be replaced with leader during symbolization).
113// To simplify symbolization, we keep the leader as a constant if class can be
114// proved to be a constant value.
115// Otherwise, the leader is a randomly chosen member of the value set, it does
116// not matter which one is chosen.
117// Each congruence class also has a defining expression,
118// though the expression may be null. If it exists, it can be used for forward
119// propagation and reassociation of values.
120//
121struct CongruenceClass {
122 typedef SmallPtrSet<Value *, 4> MemberSet;
123 unsigned ID;
124 // Representative leader.
125 Value *RepLeader;
126 // Defining Expression.
127 const Expression *DefiningExpr;
128 // Actual members of this class.
129 MemberSet Members;
130
131 // True if this class has no members left. This is mainly used for assertion
132 // purposes, and for skipping empty classes.
133 bool Dead;
134
135 explicit CongruenceClass(unsigned ID)
136 : ID(ID), RepLeader(0), DefiningExpr(0), Dead(false) {}
137 CongruenceClass(unsigned ID, Value *Leader, const Expression *E)
138 : ID(ID), RepLeader(Leader), DefiningExpr(E), Dead(false) {}
139};
140
141namespace llvm {
142 template <> struct DenseMapInfo<const Expression *> {
143 static const Expression *getEmptyKey() {
144 uintptr_t Val = static_cast<uintptr_t>(-1);
145 Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
146 return reinterpret_cast<const Expression *>(Val);
147 }
148 static const Expression *getTombstoneKey() {
149 uintptr_t Val = static_cast<uintptr_t>(~1U);
150 Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
151 return reinterpret_cast<const Expression *>(Val);
152 }
153 static unsigned getHashValue(const Expression *V) {
154 return static_cast<unsigned>(V->getHashValue());
155 }
156 static bool isEqual(const Expression *LHS, const Expression *RHS) {
157 if (LHS == RHS)
158 return true;
159 if (LHS == getTombstoneKey() || RHS == getTombstoneKey() ||
160 LHS == getEmptyKey() || RHS == getEmptyKey())
161 return false;
162 return *LHS == *RHS;
163 }
164 };
165} // end namespace llvm
166
167class NewGVN : public FunctionPass {
168 DominatorTree *DT;
169 const DataLayout *DL;
170 const TargetLibraryInfo *TLI;
171 AssumptionCache *AC;
172 AliasAnalysis *AA;
173 MemorySSA *MSSA;
174 MemorySSAWalker *MSSAWalker;
175 BumpPtrAllocator ExpressionAllocator;
176 ArrayRecycler<Value *> ArgRecycler;
177
178 // Congruence class info.
179 CongruenceClass *InitialClass;
180 std::vector<CongruenceClass *> CongruenceClasses;
181 unsigned NextCongruenceNum;
182
183 // Value Mappings.
184 DenseMap<Value *, CongruenceClass *> ValueToClass;
185 DenseMap<Value *, const Expression *> ValueToExpression;
186
187 // Expression to class mapping.
188 typedef DenseMap<const Expression *, CongruenceClass *> ExpressionClassMap;
189 ExpressionClassMap ExpressionToClass;
190
191 // Which values have changed as a result of leader changes.
192 SmallPtrSet<Value *, 8> ChangedValues;
193
194 // Reachability info.
195 typedef BasicBlockEdge BlockEdge;
196 DenseSet<BlockEdge> ReachableEdges;
197 SmallPtrSet<const BasicBlock *, 8> ReachableBlocks;
198
199 // This is a bitvector because, on larger functions, we may have
200 // thousands of touched instructions at once (entire blocks,
201 // instructions with hundreds of uses, etc). Even with optimization
202 // for when we mark whole blocks as touched, when this was a
203 // SmallPtrSet or DenseSet, for some functions, we spent >20% of all
204 // the time in GVN just managing this list. The bitvector, on the
205 // other hand, efficiently supports test/set/clear of both
206 // individual and ranges, as well as "find next element" This
207 // enables us to use it as a worklist with essentially 0 cost.
208 BitVector TouchedInstructions;
209
210 DenseMap<const BasicBlock *, std::pair<unsigned, unsigned>> BlockInstRange;
211 DenseMap<const DomTreeNode *, std::pair<unsigned, unsigned>>
212 DominatedInstRange;
213
214#ifndef NDEBUG
215 // Debugging for how many times each block and instruction got processed.
216 DenseMap<const Value *, unsigned> ProcessedCount;
217#endif
218
219 // DFS info.
220 DenseMap<const BasicBlock *, std::pair<int, int>> DFSDomMap;
221 DenseMap<const Value *, unsigned> InstrDFS;
222 std::vector<Instruction *> DFSToInstr;
223
224 // Deletion info.
225 SmallPtrSet<Instruction *, 8> InstructionsToErase;
226
227public:
228 static char ID; // Pass identification, replacement for typeid.
229 NewGVN() : FunctionPass(ID) {
230 initializeNewGVNPass(*PassRegistry::getPassRegistry());
231 }
232
233 bool runOnFunction(Function &F) override;
234 bool runGVN(Function &F, DominatorTree *DT, AssumptionCache *AC,
235 TargetLibraryInfo *TLI, AliasAnalysis *AA,
236 MemorySSA *MSSA);
237
238private:
239 // This transformation requires dominator postdominator info.
240 void getAnalysisUsage(AnalysisUsage &AU) const override {
241 AU.addRequired<AssumptionCacheTracker>();
242 AU.addRequired<DominatorTreeWrapperPass>();
243 AU.addRequired<TargetLibraryInfoWrapperPass>();
244 AU.addRequired<MemorySSAWrapperPass>();
245 AU.addRequired<AAResultsWrapperPass>();
246
247 AU.addPreserved<DominatorTreeWrapperPass>();
248 AU.addPreserved<GlobalsAAWrapperPass>();
249 }
250
251 // Expression handling.
252 const Expression *createExpression(Instruction *, const BasicBlock *);
253 const Expression *createBinaryExpression(unsigned, Type *, Value *, Value *,
254 const BasicBlock *);
255 PHIExpression *createPHIExpression(Instruction *);
256 const VariableExpression *createVariableExpression(Value *);
257 const ConstantExpression *createConstantExpression(Constant *);
258 const Expression *createVariableOrConstant(Value *V, const BasicBlock *B);
259 const StoreExpression *createStoreExpression(StoreInst *, MemoryAccess *,
260 const BasicBlock *);
261 LoadExpression *createLoadExpression(Type *, Value *, LoadInst *,
262 MemoryAccess *, const BasicBlock *);
263
264 const CallExpression *createCallExpression(CallInst *, MemoryAccess *,
265 const BasicBlock *);
266 const AggregateValueExpression *
267 createAggregateValueExpression(Instruction *, const BasicBlock *);
268 bool setBasicExpressionInfo(Instruction *, BasicExpression *,
269 const BasicBlock *);
270
271 // Congruence class handling.
272 CongruenceClass *createCongruenceClass(Value *Leader, const Expression *E) {
273 CongruenceClass *result =
274 new CongruenceClass(NextCongruenceNum++, Leader, E);
275 CongruenceClasses.emplace_back(result);
276 return result;
277 }
278
279 CongruenceClass *createSingletonCongruenceClass(Value *Member) {
280 CongruenceClass *CClass = createCongruenceClass(Member, NULL);
281 CClass->Members.insert(Member);
282 ValueToClass[Member] = CClass;
283 return CClass;
284 }
285 void initializeCongruenceClasses(Function &F);
286
287 // Symbolic evaluation.
288 const Expression *checkSimplificationResults(Expression *, Instruction *,
289 Value *);
290 const Expression *performSymbolicEvaluation(Value *, const BasicBlock *);
291 const Expression *performSymbolicLoadEvaluation(Instruction *,
292 const BasicBlock *);
293 const Expression *performSymbolicStoreEvaluation(Instruction *,
294 const BasicBlock *);
295 const Expression *performSymbolicCallEvaluation(Instruction *,
296 const BasicBlock *);
297 const Expression *performSymbolicPHIEvaluation(Instruction *,
298 const BasicBlock *);
299 const Expression *performSymbolicAggrValueEvaluation(Instruction *,
300 const BasicBlock *);
301
302 // Congruence finding.
303 // Templated to allow them to work both on BB's and BB-edges.
304 template <class T>
305 Value *lookupOperandLeader(Value *, const User *, const T &) const;
306 void performCongruenceFinding(Value *, const Expression *);
307
308 // Reachability handling.
309 void updateReachableEdge(BasicBlock *, BasicBlock *);
310 void processOutgoingEdges(TerminatorInst *, BasicBlock *);
Daniel Berlin8a6a8612016-12-24 00:04:07 +0000311 bool isOnlyReachableViaThisEdge(const BasicBlockEdge &) const;
Davide Italiano7e274e02016-12-22 16:03:48 +0000312 Value *findConditionEquivalence(Value *, BasicBlock *) const;
313
314 // Elimination.
315 struct ValueDFS;
316 void convertDenseToDFSOrdered(CongruenceClass::MemberSet &,
317 std::vector<ValueDFS> &);
318
319 bool eliminateInstructions(Function &);
320 void replaceInstruction(Instruction *, Value *);
321 void markInstructionForDeletion(Instruction *);
322 void deleteInstructionsInBlock(BasicBlock *);
323
324 // New instruction creation.
325 void handleNewInstruction(Instruction *){};
326 void markUsersTouched(Value *);
327 void markMemoryUsersTouched(MemoryAccess *);
328
329 // Utilities.
330 void cleanupTables();
331 std::pair<unsigned, unsigned> assignDFSNumbers(BasicBlock *, unsigned);
332 void updateProcessedCount(Value *V);
333};
334
335char NewGVN::ID = 0;
336
337// createGVNPass - The public interface to this file.
338FunctionPass *llvm::createNewGVNPass() { return new NewGVN(); }
339
340bool LoadExpression::equals(const Expression &Other) const {
341 if (!isa<LoadExpression>(Other) && !isa<StoreExpression>(Other))
342 return false;
343 if (!this->BasicExpression::equals(Other))
344 return false;
345 if (const auto *OtherL = dyn_cast<LoadExpression>(&Other)) {
346 if (DefiningAccess != OtherL->getDefiningAccess())
347 return false;
348 } else if (const auto *OtherS = dyn_cast<StoreExpression>(&Other)) {
349 if (DefiningAccess != OtherS->getDefiningAccess())
350 return false;
351 }
352
353 return true;
354}
355
356bool StoreExpression::equals(const Expression &Other) const {
357 if (!isa<LoadExpression>(Other) && !isa<StoreExpression>(Other))
358 return false;
359 if (!this->BasicExpression::equals(Other))
360 return false;
361 if (const auto *OtherL = dyn_cast<LoadExpression>(&Other)) {
362 if (DefiningAccess != OtherL->getDefiningAccess())
363 return false;
364 } else if (const auto *OtherS = dyn_cast<StoreExpression>(&Other)) {
365 if (DefiningAccess != OtherS->getDefiningAccess())
366 return false;
367 }
368
369 return true;
370}
371
372#ifndef NDEBUG
373static std::string getBlockName(const BasicBlock *B) {
374 return DOTGraphTraits<const Function *>::getSimpleNodeLabel(B, NULL);
375}
376#endif
377
378INITIALIZE_PASS_BEGIN(NewGVN, "newgvn", "Global Value Numbering", false, false)
379INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
380INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
381INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
382INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
383INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
384INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
385INITIALIZE_PASS_END(NewGVN, "newgvn", "Global Value Numbering", false, false)
386
387PHIExpression *NewGVN::createPHIExpression(Instruction *I) {
388 BasicBlock *PhiBlock = I->getParent();
389 PHINode *PN = cast<PHINode>(I);
390 PHIExpression *E = new (ExpressionAllocator)
391 PHIExpression(PN->getNumOperands(), I->getParent());
392
393 E->allocateOperands(ArgRecycler, ExpressionAllocator);
394 E->setType(I->getType());
395 E->setOpcode(I->getOpcode());
396 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
397 BasicBlock *B = PN->getIncomingBlock(i);
398 if (!ReachableBlocks.count(B)) {
399 DEBUG(dbgs() << "Skipping unreachable block " << getBlockName(B)
400 << " in PHI node " << *PN << "\n");
401 continue;
402 }
403 if (I->getOperand(i) != I) {
404 const BasicBlockEdge BBE(B, PhiBlock);
405 auto Operand = lookupOperandLeader(I->getOperand(i), I, BBE);
406 E->ops_push_back(Operand);
407 } else {
408 E->ops_push_back(I->getOperand(i));
409 }
410 }
411 return E;
412}
413
414// Set basic expression info (Arguments, type, opcode) for Expression
415// E from Instruction I in block B.
416bool NewGVN::setBasicExpressionInfo(Instruction *I, BasicExpression *E,
417 const BasicBlock *B) {
418 bool AllConstant = true;
419 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
420 E->setType(GEP->getSourceElementType());
421 else
422 E->setType(I->getType());
423 E->setOpcode(I->getOpcode());
424 E->allocateOperands(ArgRecycler, ExpressionAllocator);
425
426 for (auto &O : I->operands()) {
427 auto Operand = lookupOperandLeader(O, I, B);
428 if (!isa<Constant>(Operand))
429 AllConstant = false;
430 E->ops_push_back(Operand);
431 }
432 return AllConstant;
433}
434
435const Expression *NewGVN::createBinaryExpression(unsigned Opcode, Type *T,
436 Value *Arg1, Value *Arg2,
437 const BasicBlock *B) {
438 BasicExpression *E = new (ExpressionAllocator) BasicExpression(2);
439
440 E->setType(T);
441 E->setOpcode(Opcode);
442 E->allocateOperands(ArgRecycler, ExpressionAllocator);
443 if (Instruction::isCommutative(Opcode)) {
444 // Ensure that commutative instructions that only differ by a permutation
445 // of their operands get the same value number by sorting the operand value
446 // numbers. Since all commutative instructions have two operands it is more
447 // efficient to sort by hand rather than using, say, std::sort.
448 if (Arg1 > Arg2)
449 std::swap(Arg1, Arg2);
450 }
451 E->ops_push_back(lookupOperandLeader(Arg1, nullptr, B));
452 E->ops_push_back(lookupOperandLeader(Arg2, nullptr, B));
453
454 Value *V = SimplifyBinOp(Opcode, E->getOperand(0), E->getOperand(1), *DL, TLI,
455 DT, AC);
456 if (const Expression *SimplifiedE = checkSimplificationResults(E, nullptr, V))
457 return SimplifiedE;
458 return E;
459}
460
461// Take a Value returned by simplification of Expression E/Instruction
462// I, and see if it resulted in a simpler expression. If so, return
463// that expression.
464// TODO: Once finished, this should not take an Instruction, we only
465// use it for printing.
466const Expression *NewGVN::checkSimplificationResults(Expression *E,
467 Instruction *I, Value *V) {
468 if (!V)
469 return nullptr;
470 if (auto *C = dyn_cast<Constant>(V)) {
471 if (I)
472 DEBUG(dbgs() << "Simplified " << *I << " to "
473 << " constant " << *C << "\n");
474 NumGVNOpsSimplified++;
475 assert(isa<BasicExpression>(E) &&
476 "We should always have had a basic expression here");
477
478 cast<BasicExpression>(E)->deallocateOperands(ArgRecycler);
479 ExpressionAllocator.Deallocate(E);
480 return createConstantExpression(C);
481 } else if (isa<Argument>(V) || isa<GlobalVariable>(V)) {
482 if (I)
483 DEBUG(dbgs() << "Simplified " << *I << " to "
484 << " variable " << *V << "\n");
485 cast<BasicExpression>(E)->deallocateOperands(ArgRecycler);
486 ExpressionAllocator.Deallocate(E);
487 return createVariableExpression(V);
488 }
489
490 CongruenceClass *CC = ValueToClass.lookup(V);
491 if (CC && CC->DefiningExpr) {
492 if (I)
493 DEBUG(dbgs() << "Simplified " << *I << " to "
494 << " expression " << *V << "\n");
495 NumGVNOpsSimplified++;
496 assert(isa<BasicExpression>(E) &&
497 "We should always have had a basic expression here");
498 cast<BasicExpression>(E)->deallocateOperands(ArgRecycler);
499 ExpressionAllocator.Deallocate(E);
500 return CC->DefiningExpr;
501 }
502 return nullptr;
503}
504
505const Expression *NewGVN::createExpression(Instruction *I,
506 const BasicBlock *B) {
507
508 BasicExpression *E =
509 new (ExpressionAllocator) BasicExpression(I->getNumOperands());
510
511 bool AllConstant = setBasicExpressionInfo(I, E, B);
512
513 if (I->isCommutative()) {
514 // Ensure that commutative instructions that only differ by a permutation
515 // of their operands get the same value number by sorting the operand value
516 // numbers. Since all commutative instructions have two operands it is more
517 // efficient to sort by hand rather than using, say, std::sort.
518 assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!");
519 if (E->getOperand(0) > E->getOperand(1))
520 E->swapOperands(0, 1);
521 }
522
523 // Perform simplificaiton
524 // TODO: Right now we only check to see if we get a constant result.
525 // We may get a less than constant, but still better, result for
526 // some operations.
527 // IE
528 // add 0, x -> x
529 // and x, x -> x
530 // We should handle this by simply rewriting the expression.
531 if (auto *CI = dyn_cast<CmpInst>(I)) {
532 // Sort the operand value numbers so x<y and y>x get the same value
533 // number.
534 CmpInst::Predicate Predicate = CI->getPredicate();
535 if (E->getOperand(0) > E->getOperand(1)) {
536 E->swapOperands(0, 1);
537 Predicate = CmpInst::getSwappedPredicate(Predicate);
538 }
539 E->setOpcode((CI->getOpcode() << 8) | Predicate);
540 // TODO: 25% of our time is spent in SimplifyCmpInst with pointer operands
541 // TODO: Since we noop bitcasts, we may need to check types before
542 // simplifying, so that we don't end up simplifying based on a wrong
543 // type assumption. We should clean this up so we can use constants of the
544 // wrong type
545
546 assert(I->getOperand(0)->getType() == I->getOperand(1)->getType() &&
547 "Wrong types on cmp instruction");
548 if ((E->getOperand(0)->getType() == I->getOperand(0)->getType() &&
549 E->getOperand(1)->getType() == I->getOperand(1)->getType())) {
550 Value *V = SimplifyCmpInst(Predicate, E->getOperand(0), E->getOperand(1),
551 *DL, TLI, DT, AC);
552 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
553 return SimplifiedE;
554 }
555 } else if (isa<SelectInst>(I)) {
556 if (isa<Constant>(E->getOperand(0)) ||
557 (E->getOperand(1)->getType() == I->getOperand(1)->getType() &&
558 E->getOperand(2)->getType() == I->getOperand(2)->getType())) {
559 Value *V = SimplifySelectInst(E->getOperand(0), E->getOperand(1),
560 E->getOperand(2), *DL, TLI, DT, AC);
561 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
562 return SimplifiedE;
563 }
564 } else if (I->isBinaryOp()) {
565 Value *V = SimplifyBinOp(E->getOpcode(), E->getOperand(0), E->getOperand(1),
566 *DL, TLI, DT, AC);
567 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
568 return SimplifiedE;
569 } else if (auto *BI = dyn_cast<BitCastInst>(I)) {
570 Value *V = SimplifyInstruction(BI, *DL, TLI, DT, AC);
571 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
572 return SimplifiedE;
573 } else if (isa<GetElementPtrInst>(I)) {
574 Value *V = SimplifyGEPInst(E->getType(),
575 ArrayRef<Value *>(E->ops_begin(), E->ops_end()),
576 *DL, TLI, DT, AC);
577 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
578 return SimplifiedE;
579 } else if (AllConstant) {
580 // We don't bother trying to simplify unless all of the operands
581 // were constant.
582 // TODO: There are a lot of Simplify*'s we could call here, if we
583 // wanted to. The original motivating case for this code was a
584 // zext i1 false to i8, which we don't have an interface to
585 // simplify (IE there is no SimplifyZExt).
586
587 SmallVector<Constant *, 8> C;
588 for (Value *Arg : E->operands())
589 C.emplace_back(cast<Constant>(Arg));
590
591 if (Value *V = ConstantFoldInstOperands(I, C, *DL, TLI))
592 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
593 return SimplifiedE;
594 }
595 return E;
596}
597
598const AggregateValueExpression *
599NewGVN::createAggregateValueExpression(Instruction *I, const BasicBlock *B) {
600 if (auto *II = dyn_cast<InsertValueInst>(I)) {
601 AggregateValueExpression *E = new (ExpressionAllocator)
602 AggregateValueExpression(I->getNumOperands(), II->getNumIndices());
603 setBasicExpressionInfo(I, E, B);
604 E->allocateIntOperands(ExpressionAllocator);
605
606 for (auto &Index : II->indices())
607 E->int_ops_push_back(Index);
608 return E;
609
610 } else if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
611 AggregateValueExpression *E = new (ExpressionAllocator)
612 AggregateValueExpression(I->getNumOperands(), EI->getNumIndices());
613 setBasicExpressionInfo(EI, E, B);
614 E->allocateIntOperands(ExpressionAllocator);
615
616 for (auto &Index : EI->indices())
617 E->int_ops_push_back(Index);
618 return E;
619 }
620 llvm_unreachable("Unhandled type of aggregate value operation");
621}
622
623const VariableExpression *
624NewGVN::createVariableExpression(Value *V) {
625 VariableExpression *E = new (ExpressionAllocator) VariableExpression(V);
626 E->setOpcode(V->getValueID());
627 return E;
628}
629
630const Expression *NewGVN::createVariableOrConstant(Value *V,
631 const BasicBlock *B) {
632 auto Leader = lookupOperandLeader(V, nullptr, B);
633 if (auto *C = dyn_cast<Constant>(Leader))
634 return createConstantExpression(C);
635 return createVariableExpression(Leader);
636}
637
638const ConstantExpression *
639NewGVN::createConstantExpression(Constant *C) {
640 ConstantExpression *E = new (ExpressionAllocator) ConstantExpression(C);
641 E->setOpcode(C->getValueID());
642 return E;
643}
644
645const CallExpression *NewGVN::createCallExpression(CallInst *CI,
646 MemoryAccess *HV,
647 const BasicBlock *B) {
648 // FIXME: Add operand bundles for calls.
649 CallExpression *E =
650 new (ExpressionAllocator) CallExpression(CI->getNumOperands(), CI, HV);
651 setBasicExpressionInfo(CI, E, B);
652 return E;
653}
654
655// See if we have a congruence class and leader for this operand, and if so,
656// return it. Otherwise, return the operand itself.
657template <class T>
658Value *NewGVN::lookupOperandLeader(Value *V, const User *U,
659 const T &B) const {
660 CongruenceClass *CC = ValueToClass.lookup(V);
661 if (CC && (CC != InitialClass))
662 return CC->RepLeader;
663 return V;
664}
665
666LoadExpression *NewGVN::createLoadExpression(Type *LoadType, Value *PointerOp,
667 LoadInst *LI, MemoryAccess *DA,
668 const BasicBlock *B) {
669 LoadExpression *E = new (ExpressionAllocator) LoadExpression(1, LI, DA);
670 E->allocateOperands(ArgRecycler, ExpressionAllocator);
671 E->setType(LoadType);
672
673 // Give store and loads same opcode so they value number together.
674 E->setOpcode(0);
675 auto Operand = lookupOperandLeader(PointerOp, LI, B);
676 E->ops_push_back(Operand);
677 if (LI)
678 E->setAlignment(LI->getAlignment());
679
680 // TODO: Value number heap versions. We may be able to discover
681 // things alias analysis can't on it's own (IE that a store and a
682 // load have the same value, and thus, it isn't clobbering the load).
683 return E;
684}
685
686const StoreExpression *NewGVN::createStoreExpression(StoreInst *SI,
687 MemoryAccess *DA,
688 const BasicBlock *B) {
689 StoreExpression *E =
690 new (ExpressionAllocator) StoreExpression(SI->getNumOperands(), SI, DA);
691 E->allocateOperands(ArgRecycler, ExpressionAllocator);
692 E->setType(SI->getValueOperand()->getType());
693
694 // Give store and loads same opcode so they value number together.
695 E->setOpcode(0);
696 E->ops_push_back(lookupOperandLeader(SI->getPointerOperand(), SI, B));
697
698 // TODO: Value number heap versions. We may be able to discover
699 // things alias analysis can't on it's own (IE that a store and a
700 // load have the same value, and thus, it isn't clobbering the load).
701 return E;
702}
703
704const Expression *NewGVN::performSymbolicStoreEvaluation(Instruction *I,
705 const BasicBlock *B) {
706 StoreInst *SI = cast<StoreInst>(I);
707 const Expression *E = createStoreExpression(SI, MSSA->getMemoryAccess(SI), B);
708 return E;
709}
710
711const Expression *NewGVN::performSymbolicLoadEvaluation(Instruction *I,
712 const BasicBlock *B) {
713 LoadInst *LI = cast<LoadInst>(I);
714
715 // We can eliminate in favor of non-simple loads, but we won't be able to
716 // eliminate them.
717 if (!LI->isSimple())
718 return nullptr;
719
720 Value *LoadAddressLeader =
721 lookupOperandLeader(LI->getPointerOperand(), I, B);
722 // Load of undef is undef.
723 if (isa<UndefValue>(LoadAddressLeader))
724 return createConstantExpression(UndefValue::get(LI->getType()));
725
726 MemoryAccess *DefiningAccess = MSSAWalker->getClobberingMemoryAccess(I);
727
728 if (!MSSA->isLiveOnEntryDef(DefiningAccess)) {
729 if (auto *MD = dyn_cast<MemoryDef>(DefiningAccess)) {
730 Instruction *DefiningInst = MD->getMemoryInst();
731 // If the defining instruction is not reachable, replace with undef.
732 if (!ReachableBlocks.count(DefiningInst->getParent()))
733 return createConstantExpression(UndefValue::get(LI->getType()));
734 }
735 }
736
737 const Expression *E = createLoadExpression(
738 LI->getType(), LI->getPointerOperand(), LI, DefiningAccess, B);
739 return E;
740}
741
742// Evaluate read only and pure calls, and create an expression result.
743const Expression *NewGVN::performSymbolicCallEvaluation(Instruction *I,
744 const BasicBlock *B) {
745 CallInst *CI = cast<CallInst>(I);
746 if (AA->doesNotAccessMemory(CI))
747 return createCallExpression(CI, nullptr, B);
748 else if (AA->onlyReadsMemory(CI))
749 return createCallExpression(CI, MSSAWalker->getClobberingMemoryAccess(CI),
750 B);
751 else
752 return nullptr;
753}
754
755// Evaluate PHI nodes symbolically, and create an expression result.
756const Expression *NewGVN::performSymbolicPHIEvaluation(Instruction *I,
757 const BasicBlock *B) {
758 PHIExpression *E = cast<PHIExpression>(createPHIExpression(I));
759 if (E->ops_empty()) {
760 DEBUG(dbgs() << "Simplified PHI node " << *I << " to undef"
761 << "\n");
762 E->deallocateOperands(ArgRecycler);
763 ExpressionAllocator.Deallocate(E);
764 return createConstantExpression(UndefValue::get(I->getType()));
765 }
766
767 Value *AllSameValue = E->getOperand(0);
768
769 // See if all arguments are the same, ignoring undef arguments, because we can
770 // choose a value that is the same for them.
771 for (const Value *Arg : E->operands())
772 if (Arg != AllSameValue && !isa<UndefValue>(Arg)) {
773 AllSameValue = NULL;
774 break;
775 }
776
777 if (AllSameValue) {
778 // It's possible to have phi nodes with cycles (IE dependent on
779 // other phis that are .... dependent on the original phi node),
780 // especially in weird CFG's where some arguments are unreachable, or
781 // uninitialized along certain paths.
782 // This can cause infinite loops during evaluation (even if you disable
783 // the recursion below, you will simply ping-pong between congruence
784 // classes). If a phi node symbolically evaluates to another phi node,
785 // just leave it alone. If they are really the same, we will still
786 // eliminate them in favor of each other.
787 if (isa<PHINode>(AllSameValue))
788 return E;
789 NumGVNPhisAllSame++;
790 DEBUG(dbgs() << "Simplified PHI node " << *I << " to " << *AllSameValue
791 << "\n");
792 E->deallocateOperands(ArgRecycler);
793 ExpressionAllocator.Deallocate(E);
794 if (auto *C = dyn_cast<Constant>(AllSameValue))
795 return createConstantExpression(C);
796 return createVariableExpression(AllSameValue);
797 }
798 return E;
799}
800
801const Expression *
802NewGVN::performSymbolicAggrValueEvaluation(Instruction *I,
803 const BasicBlock *B) {
804 if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
805 auto *II = dyn_cast<IntrinsicInst>(EI->getAggregateOperand());
806 if (II && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
807 unsigned Opcode = 0;
808 // EI might be an extract from one of our recognised intrinsics. If it
809 // is we'll synthesize a semantically equivalent expression instead on
810 // an extract value expression.
811 switch (II->getIntrinsicID()) {
812 case Intrinsic::sadd_with_overflow:
813 case Intrinsic::uadd_with_overflow:
814 Opcode = Instruction::Add;
815 break;
816 case Intrinsic::ssub_with_overflow:
817 case Intrinsic::usub_with_overflow:
818 Opcode = Instruction::Sub;
819 break;
820 case Intrinsic::smul_with_overflow:
821 case Intrinsic::umul_with_overflow:
822 Opcode = Instruction::Mul;
823 break;
824 default:
825 break;
826 }
827
828 if (Opcode != 0) {
829 // Intrinsic recognized. Grab its args to finish building the
830 // expression.
831 assert(II->getNumArgOperands() == 2 &&
832 "Expect two args for recognised intrinsics.");
833 return createBinaryExpression(Opcode, EI->getType(),
834 II->getArgOperand(0),
835 II->getArgOperand(1), B);
836 }
837 }
838 }
839
840 return createAggregateValueExpression(I, B);
841}
842
843// Substitute and symbolize the value before value numbering.
844const Expression *NewGVN::performSymbolicEvaluation(Value *V,
845 const BasicBlock *B) {
846 const Expression *E = NULL;
847 if (auto *C = dyn_cast<Constant>(V))
848 E = createConstantExpression(C);
849 else if (isa<Argument>(V) || isa<GlobalVariable>(V)) {
850 E = createVariableExpression(V);
851 } else {
852 // TODO: memory intrinsics.
853 // TODO: Some day, we should do the forward propagation and reassociation
854 // parts of the algorithm.
855 Instruction *I = cast<Instruction>(V);
856 switch (I->getOpcode()) {
857 case Instruction::ExtractValue:
858 case Instruction::InsertValue:
859 E = performSymbolicAggrValueEvaluation(I, B);
860 break;
861 case Instruction::PHI:
862 E = performSymbolicPHIEvaluation(I, B);
863 break;
864 case Instruction::Call:
865 E = performSymbolicCallEvaluation(I, B);
866 break;
867 case Instruction::Store:
868 E = performSymbolicStoreEvaluation(I, B);
869 break;
870 case Instruction::Load:
871 E = performSymbolicLoadEvaluation(I, B);
872 break;
873 case Instruction::BitCast: {
874 E = createExpression(I, B);
875 } break;
876
877 case Instruction::Add:
878 case Instruction::FAdd:
879 case Instruction::Sub:
880 case Instruction::FSub:
881 case Instruction::Mul:
882 case Instruction::FMul:
883 case Instruction::UDiv:
884 case Instruction::SDiv:
885 case Instruction::FDiv:
886 case Instruction::URem:
887 case Instruction::SRem:
888 case Instruction::FRem:
889 case Instruction::Shl:
890 case Instruction::LShr:
891 case Instruction::AShr:
892 case Instruction::And:
893 case Instruction::Or:
894 case Instruction::Xor:
895 case Instruction::ICmp:
896 case Instruction::FCmp:
897 case Instruction::Trunc:
898 case Instruction::ZExt:
899 case Instruction::SExt:
900 case Instruction::FPToUI:
901 case Instruction::FPToSI:
902 case Instruction::UIToFP:
903 case Instruction::SIToFP:
904 case Instruction::FPTrunc:
905 case Instruction::FPExt:
906 case Instruction::PtrToInt:
907 case Instruction::IntToPtr:
908 case Instruction::Select:
909 case Instruction::ExtractElement:
910 case Instruction::InsertElement:
911 case Instruction::ShuffleVector:
912 case Instruction::GetElementPtr:
913 E = createExpression(I, B);
914 break;
915 default:
916 return nullptr;
917 }
918 }
919 if (!E)
920 return nullptr;
921 return E;
922}
923
924// There is an edge from 'Src' to 'Dst'. Return true if every path from
925// the entry block to 'Dst' passes via this edge. In particular 'Dst'
926// must not be reachable via another edge from 'Src'.
Daniel Berlin8a6a8612016-12-24 00:04:07 +0000927bool NewGVN::isOnlyReachableViaThisEdge(const BasicBlockEdge &E) const {
Davide Italiano7e274e02016-12-22 16:03:48 +0000928
929 // While in theory it is interesting to consider the case in which Dst has
930 // more than one predecessor, because Dst might be part of a loop which is
931 // only reachable from Src, in practice it is pointless since at the time
932 // GVN runs all such loops have preheaders, which means that Dst will have
933 // been changed to have only one predecessor, namely Src.
934 const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
935 const BasicBlock *Src = E.getStart();
936 assert((!Pred || Pred == Src) && "No edge between these basic blocks!");
937 (void)Src;
938 return Pred != nullptr;
939}
940
941void NewGVN::markUsersTouched(Value *V) {
942 // Now mark the users as touched.
943 for (auto &U : V->uses()) {
944 auto *User = dyn_cast<Instruction>(U.getUser());
945 assert(User && "Use of value not within an instruction?");
946 TouchedInstructions.set(InstrDFS[User]);
947 }
948}
949
950void NewGVN::markMemoryUsersTouched(MemoryAccess *MA) {
951 for (auto U : MA->users()) {
952 if (auto *MUD = dyn_cast<MemoryUseOrDef>(U))
953 TouchedInstructions.set(InstrDFS[MUD->getMemoryInst()]);
954 else
955 TouchedInstructions.set(InstrDFS[MA]);
956 }
957}
958
959// Perform congruence finding on a given value numbering expression.
960void NewGVN::performCongruenceFinding(Value *V, const Expression *E) {
961
962 ValueToExpression[V] = E;
963 // This is guaranteed to return something, since it will at least find
964 // INITIAL.
965 CongruenceClass *VClass = ValueToClass[V];
966 assert(VClass && "Should have found a vclass");
967 // Dead classes should have been eliminated from the mapping.
968 assert(!VClass->Dead && "Found a dead class");
969
970 CongruenceClass *EClass;
971 // Expressions we can't symbolize are always in their own unique
972 // congruence class.
973 if (E == NULL) {
974 // We may have already made a unique class.
975 if (VClass->Members.size() != 1 || VClass->RepLeader != V) {
976 CongruenceClass *NewClass = createCongruenceClass(V, NULL);
977 // We should always be adding the member in the below code.
978 EClass = NewClass;
979 DEBUG(dbgs() << "Created new congruence class for " << *V
980 << " due to NULL expression\n");
981 } else {
982 EClass = VClass;
983 }
984 } else if (const auto *VE = dyn_cast<VariableExpression>(E)) {
985 EClass = ValueToClass[VE->getVariableValue()];
986 } else {
987 auto lookupResult = ExpressionToClass.insert({E, nullptr});
988
989 // If it's not in the value table, create a new congruence class.
990 if (lookupResult.second) {
991 CongruenceClass *NewClass = createCongruenceClass(NULL, E);
992 auto place = lookupResult.first;
993 place->second = NewClass;
994
995 // Constants and variables should always be made the leader.
996 if (const auto *CE = dyn_cast<ConstantExpression>(E))
997 NewClass->RepLeader = CE->getConstantValue();
998 else if (const auto *VE = dyn_cast<VariableExpression>(E))
999 NewClass->RepLeader = VE->getVariableValue();
1000 else if (const auto *SE = dyn_cast<StoreExpression>(E))
1001 NewClass->RepLeader = SE->getStoreInst()->getValueOperand();
1002 else
1003 NewClass->RepLeader = V;
1004
1005 EClass = NewClass;
1006 DEBUG(dbgs() << "Created new congruence class for " << *V
1007 << " using expression " << *E << " at " << NewClass->ID
1008 << "\n");
1009 DEBUG(dbgs() << "Hash value was " << E->getHashValue() << "\n");
1010 } else {
1011 EClass = lookupResult.first->second;
1012 assert(EClass && "Somehow don't have an eclass");
1013
1014 assert(!EClass->Dead && "We accidentally looked up a dead class");
1015 }
1016 }
1017 bool WasInChanged = ChangedValues.erase(V);
1018 if (VClass != EClass || WasInChanged) {
1019 DEBUG(dbgs() << "Found class " << EClass->ID << " for expression " << E
1020 << "\n");
1021
1022 if (VClass != EClass) {
1023 DEBUG(dbgs() << "New congruence class for " << V << " is " << EClass->ID
1024 << "\n");
1025
1026 VClass->Members.erase(V);
1027 EClass->Members.insert(V);
1028 ValueToClass[V] = EClass;
1029 // See if we destroyed the class or need to swap leaders.
1030 if (VClass->Members.empty() && VClass != InitialClass) {
1031 if (VClass->DefiningExpr) {
1032 VClass->Dead = true;
1033 DEBUG(dbgs() << "Erasing expression " << *E << " from table\n");
1034 ExpressionToClass.erase(VClass->DefiningExpr);
1035 }
1036 } else if (VClass->RepLeader == V) {
1037 // FIXME: When the leader changes, the value numbering of
1038 // everything may change, so we need to reprocess.
1039 VClass->RepLeader = *(VClass->Members.begin());
1040 for (auto M : VClass->Members) {
1041 if (auto *I = dyn_cast<Instruction>(M))
1042 TouchedInstructions.set(InstrDFS[I]);
1043 ChangedValues.insert(M);
1044 }
1045 }
1046 }
1047 markUsersTouched(V);
Davide Italiano463c32e2016-12-24 17:17:21 +00001048 if (auto *I = dyn_cast<Instruction>(V))
Davide Italiano7e274e02016-12-22 16:03:48 +00001049 if (MemoryAccess *MA = MSSA->getMemoryAccess(I))
1050 markMemoryUsersTouched(MA);
1051 }
1052}
1053
1054// Process the fact that Edge (from, to) is reachable, including marking
1055// any newly reachable blocks and instructions for processing.
1056void NewGVN::updateReachableEdge(BasicBlock *From, BasicBlock *To) {
1057 // Check if the Edge was reachable before.
1058 if (ReachableEdges.insert({From, To}).second) {
1059 // If this block wasn't reachable before, all instructions are touched.
1060 if (ReachableBlocks.insert(To).second) {
1061 DEBUG(dbgs() << "Block " << getBlockName(To) << " marked reachable\n");
1062 const auto &InstRange = BlockInstRange.lookup(To);
1063 TouchedInstructions.set(InstRange.first, InstRange.second);
1064 } else {
1065 DEBUG(dbgs() << "Block " << getBlockName(To)
1066 << " was reachable, but new edge {" << getBlockName(From)
1067 << "," << getBlockName(To) << "} to it found\n");
1068
1069 // We've made an edge reachable to an existing block, which may
1070 // impact predicates. Otherwise, only mark the phi nodes as touched, as
1071 // they are the only thing that depend on new edges. Anything using their
1072 // values will get propagated to if necessary.
1073 auto BI = To->begin();
1074 while (isa<PHINode>(BI)) {
1075 TouchedInstructions.set(InstrDFS[&*BI]);
1076 ++BI;
1077 }
1078 }
1079 }
1080}
1081
1082// Given a predicate condition (from a switch, cmp, or whatever) and a block,
1083// see if we know some constant value for it already.
1084Value *NewGVN::findConditionEquivalence(Value *Cond, BasicBlock *B) const {
1085 auto Result = lookupOperandLeader(Cond, nullptr, B);
1086 if (isa<Constant>(Result))
1087 return Result;
1088 return nullptr;
1089}
1090
1091// Process the outgoing edges of a block for reachability.
1092void NewGVN::processOutgoingEdges(TerminatorInst *TI, BasicBlock *B) {
1093 // Evaluate reachability of terminator instruction.
1094 BranchInst *BR;
1095 if ((BR = dyn_cast<BranchInst>(TI)) && BR->isConditional()) {
1096 Value *Cond = BR->getCondition();
1097 Value *CondEvaluated = findConditionEquivalence(Cond, B);
1098 if (!CondEvaluated) {
1099 if (auto *I = dyn_cast<Instruction>(Cond)) {
1100 const Expression *E = createExpression(I, B);
1101 if (const auto *CE = dyn_cast<ConstantExpression>(E)) {
1102 CondEvaluated = CE->getConstantValue();
1103 }
1104 } else if (isa<ConstantInt>(Cond)) {
1105 CondEvaluated = Cond;
1106 }
1107 }
1108 ConstantInt *CI;
1109 BasicBlock *TrueSucc = BR->getSuccessor(0);
1110 BasicBlock *FalseSucc = BR->getSuccessor(1);
1111 if (CondEvaluated && (CI = dyn_cast<ConstantInt>(CondEvaluated))) {
1112 if (CI->isOne()) {
1113 DEBUG(dbgs() << "Condition for Terminator " << *TI
1114 << " evaluated to true\n");
1115 updateReachableEdge(B, TrueSucc);
1116 } else if (CI->isZero()) {
1117 DEBUG(dbgs() << "Condition for Terminator " << *TI
1118 << " evaluated to false\n");
1119 updateReachableEdge(B, FalseSucc);
1120 }
1121 } else {
1122 updateReachableEdge(B, TrueSucc);
1123 updateReachableEdge(B, FalseSucc);
1124 }
1125 } else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
1126 // For switches, propagate the case values into the case
1127 // destinations.
1128
1129 // Remember how many outgoing edges there are to every successor.
1130 SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
1131
Davide Italiano7e274e02016-12-22 16:03:48 +00001132 Value *SwitchCond = SI->getCondition();
1133 Value *CondEvaluated = findConditionEquivalence(SwitchCond, B);
1134 // See if we were able to turn this switch statement into a constant.
1135 if (CondEvaluated && isa<ConstantInt>(CondEvaluated)) {
1136 ConstantInt *CondVal = cast<ConstantInt>(CondEvaluated);
1137 // We should be able to get case value for this.
1138 auto CaseVal = SI->findCaseValue(CondVal);
1139 if (CaseVal.getCaseSuccessor() == SI->getDefaultDest()) {
1140 // We proved the value is outside of the range of the case.
1141 // We can't do anything other than mark the default dest as reachable,
1142 // and go home.
1143 updateReachableEdge(B, SI->getDefaultDest());
1144 return;
1145 }
1146 // Now get where it goes and mark it reachable.
1147 BasicBlock *TargetBlock = CaseVal.getCaseSuccessor();
1148 updateReachableEdge(B, TargetBlock);
Davide Italiano7e274e02016-12-22 16:03:48 +00001149 } else {
1150 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
1151 BasicBlock *TargetBlock = SI->getSuccessor(i);
1152 ++SwitchEdges[TargetBlock];
1153 updateReachableEdge(B, TargetBlock);
1154 }
1155 }
1156 } else {
1157 // Otherwise this is either unconditional, or a type we have no
1158 // idea about. Just mark successors as reachable.
1159 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1160 BasicBlock *TargetBlock = TI->getSuccessor(i);
1161 updateReachableEdge(B, TargetBlock);
1162 }
1163 }
1164}
1165
1166// The algorithm initially places the values of the routine in the INITIAL congruence
1167// class. The leader of INITIAL is the undetermined value `TOP`.
1168// When the algorithm has finished, values still in INITIAL are unreachable.
1169void NewGVN::initializeCongruenceClasses(Function &F) {
1170 // FIXME now i can't remember why this is 2
1171 NextCongruenceNum = 2;
1172 // Initialize all other instructions to be in INITIAL class.
1173 CongruenceClass::MemberSet InitialValues;
1174 for (auto &B : F)
1175 for (auto &I : B)
1176 InitialValues.insert(&I);
1177
1178 InitialClass = createCongruenceClass(NULL, NULL);
1179 for (auto L : InitialValues)
1180 ValueToClass[L] = InitialClass;
1181 InitialClass->Members.swap(InitialValues);
1182
1183 // Initialize arguments to be in their own unique congruence classes
1184 for (auto &FA : F.args())
1185 createSingletonCongruenceClass(&FA);
1186}
1187
1188void NewGVN::cleanupTables() {
1189 for (unsigned i = 0, e = CongruenceClasses.size(); i != e; ++i) {
1190 DEBUG(dbgs() << "Congruence class " << CongruenceClasses[i]->ID << " has "
1191 << CongruenceClasses[i]->Members.size() << " members\n");
1192 // Make sure we delete the congruence class (probably worth switching to
1193 // a unique_ptr at some point.
1194 delete CongruenceClasses[i];
1195 CongruenceClasses[i] = NULL;
1196 }
1197
1198 ValueToClass.clear();
1199 ArgRecycler.clear(ExpressionAllocator);
1200 ExpressionAllocator.Reset();
1201 CongruenceClasses.clear();
1202 ExpressionToClass.clear();
1203 ValueToExpression.clear();
1204 ReachableBlocks.clear();
1205 ReachableEdges.clear();
1206#ifndef NDEBUG
1207 ProcessedCount.clear();
1208#endif
1209 DFSDomMap.clear();
1210 InstrDFS.clear();
1211 InstructionsToErase.clear();
1212
1213 DFSToInstr.clear();
1214 BlockInstRange.clear();
1215 TouchedInstructions.clear();
1216 DominatedInstRange.clear();
1217}
1218
1219std::pair<unsigned, unsigned> NewGVN::assignDFSNumbers(BasicBlock *B,
1220 unsigned Start) {
1221 unsigned End = Start;
1222 for (auto &I : *B) {
1223 InstrDFS[&I] = End++;
1224 DFSToInstr.emplace_back(&I);
1225 }
1226
1227 // All of the range functions taken half-open ranges (open on the end side).
1228 // So we do not subtract one from count, because at this point it is one
1229 // greater than the last instruction.
1230 return std::make_pair(Start, End);
1231}
1232
1233void NewGVN::updateProcessedCount(Value *V) {
1234#ifndef NDEBUG
1235 if (ProcessedCount.count(V) == 0) {
1236 ProcessedCount.insert({V, 1});
1237 } else {
1238 ProcessedCount[V] += 1;
1239 assert(ProcessedCount[V] < 100 &&
1240 "Seem to have processed the same Value a lot\n");
1241 }
1242#endif
1243}
1244
1245// This is the main transformation entry point.
1246bool NewGVN::runGVN(Function &F, DominatorTree *_DT, AssumptionCache *_AC,
1247 TargetLibraryInfo *_TLI, AliasAnalysis *_AA,
1248 MemorySSA *_MSSA) {
1249 bool Changed = false;
1250 DT = _DT;
1251 AC = _AC;
1252 TLI = _TLI;
1253 AA = _AA;
1254 MSSA = _MSSA;
1255 DL = &F.getParent()->getDataLayout();
1256 MSSAWalker = MSSA->getWalker();
1257
1258 // Count number of instructions for sizing of hash tables, and come
1259 // up with a global dfs numbering for instructions.
1260 unsigned ICount = 0;
1261 SmallPtrSet<BasicBlock *, 16> VisitedBlocks;
1262
1263 // Note: We want RPO traversal of the blocks, which is not quite the same as
1264 // dominator tree order, particularly with regard whether backedges get
1265 // visited first or second, given a block with multiple successors.
1266 // If we visit in the wrong order, we will end up performing N times as many
1267 // iterations.
1268 ReversePostOrderTraversal<Function *> RPOT(&F);
1269 for (auto &B : RPOT) {
1270 VisitedBlocks.insert(B);
1271 const auto &BlockRange = assignDFSNumbers(B, ICount);
1272 BlockInstRange.insert({B, BlockRange});
1273 ICount += BlockRange.second - BlockRange.first;
1274 }
1275
1276 // Handle forward unreachable blocks and figure out which blocks
1277 // have single preds.
1278 for (auto &B : F) {
1279 // Assign numbers to unreachable blocks.
1280 if (!VisitedBlocks.count(&B)) {
1281 const auto &BlockRange = assignDFSNumbers(&B, ICount);
1282 BlockInstRange.insert({&B, BlockRange});
1283 ICount += BlockRange.second - BlockRange.first;
1284 }
1285 }
1286
1287 TouchedInstructions.resize(ICount + 1);
1288 DominatedInstRange.reserve(F.size());
1289 // Ensure we don't end up resizing the expressionToClass map, as
1290 // that can be quite expensive. At most, we have one expression per
1291 // instruction.
1292 ExpressionToClass.reserve(ICount + 1);
1293
1294 // Initialize the touched instructions to include the entry block.
1295 const auto &InstRange = BlockInstRange.lookup(&F.getEntryBlock());
1296 TouchedInstructions.set(InstRange.first, InstRange.second);
1297 ReachableBlocks.insert(&F.getEntryBlock());
1298
1299 initializeCongruenceClasses(F);
1300
1301 // We start out in the entry block.
1302 BasicBlock *LastBlock = &F.getEntryBlock();
1303 while (TouchedInstructions.any()) {
1304 // Walk through all the instructions in all the blocks in RPO.
1305 for (int InstrNum = TouchedInstructions.find_first(); InstrNum != -1;
1306 InstrNum = TouchedInstructions.find_next(InstrNum)) {
1307 Instruction *I = DFSToInstr[InstrNum];
1308 BasicBlock *CurrBlock = I->getParent();
1309
1310 // If we hit a new block, do reachability processing.
1311 if (CurrBlock != LastBlock) {
1312 LastBlock = CurrBlock;
1313 bool BlockReachable = ReachableBlocks.count(CurrBlock);
1314 const auto &CurrInstRange = BlockInstRange.lookup(CurrBlock);
1315
1316 // If it's not reachable, erase any touched instructions and move on.
1317 if (!BlockReachable) {
1318 TouchedInstructions.reset(CurrInstRange.first, CurrInstRange.second);
1319 DEBUG(dbgs() << "Skipping instructions in block "
1320 << getBlockName(CurrBlock)
1321 << " because it is unreachable\n");
1322 continue;
1323 }
1324 updateProcessedCount(CurrBlock);
1325 }
1326 DEBUG(dbgs() << "Processing instruction " << *I << "\n");
1327 if (I->use_empty() && !I->getType()->isVoidTy()) {
1328 DEBUG(dbgs() << "Skipping unused instruction\n");
1329 if (isInstructionTriviallyDead(I, TLI))
1330 markInstructionForDeletion(I);
1331 TouchedInstructions.reset(InstrNum);
1332 continue;
1333 }
1334 updateProcessedCount(I);
1335
1336 if (!I->isTerminator()) {
1337 const Expression *Symbolized = performSymbolicEvaluation(I, CurrBlock);
1338 performCongruenceFinding(I, Symbolized);
1339 } else {
1340 processOutgoingEdges(dyn_cast<TerminatorInst>(I), CurrBlock);
1341 }
1342 // Reset after processing (because we may mark ourselves as touched when
1343 // we propagate equalities).
1344 TouchedInstructions.reset(InstrNum);
1345 }
1346 }
1347
1348 Changed |= eliminateInstructions(F);
1349
1350 // Delete all instructions marked for deletion.
1351 for (Instruction *ToErase : InstructionsToErase) {
1352 if (!ToErase->use_empty())
1353 ToErase->replaceAllUsesWith(UndefValue::get(ToErase->getType()));
1354
1355 ToErase->eraseFromParent();
1356 }
1357
1358 // Delete all unreachable blocks.
1359 for (auto &B : F) {
1360 BasicBlock *BB = &B;
1361 if (!ReachableBlocks.count(BB)) {
1362 DEBUG(dbgs() << "We believe block " << getBlockName(BB)
1363 << " is unreachable\n");
1364 deleteInstructionsInBlock(BB);
1365 Changed = true;
1366 }
1367 }
1368
1369 cleanupTables();
1370 return Changed;
1371}
1372
1373bool NewGVN::runOnFunction(Function &F) {
1374 if (skipFunction(F))
1375 return false;
1376 return runGVN(F, &getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
1377 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
1378 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
1379 &getAnalysis<AAResultsWrapperPass>().getAAResults(),
1380 &getAnalysis<MemorySSAWrapperPass>().getMSSA());
1381}
1382
1383PreservedAnalyses NewGVNPass::run(Function &F,
1384 AnalysisManager<Function> &AM) {
1385 NewGVN Impl;
1386
1387 // Apparently the order in which we get these results matter for
1388 // the old GVN (see Chandler's comment in GVN.cpp). I'll keep
1389 // the same order here, just in case.
1390 auto &AC = AM.getResult<AssumptionAnalysis>(F);
1391 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1392 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1393 auto &AA = AM.getResult<AAManager>(F);
1394 auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
1395 bool Changed = Impl.runGVN(F, &DT, &AC, &TLI, &AA, &MSSA);
1396 if (!Changed)
1397 return PreservedAnalyses::all();
1398 PreservedAnalyses PA;
1399 PA.preserve<DominatorTreeAnalysis>();
1400 PA.preserve<GlobalsAA>();
1401 return PA;
1402}
1403
1404// Return true if V is a value that will always be available (IE can
1405// be placed anywhere) in the function. We don't do globals here
1406// because they are often worse to put in place.
1407// TODO: Separate cost from availability
1408static bool alwaysAvailable(Value *V) {
1409 return isa<Constant>(V) || isa<Argument>(V);
1410}
1411
1412// Get the basic block from an instruction/value.
1413static BasicBlock *getBlockForValue(Value *V) {
1414 if (auto *I = dyn_cast<Instruction>(V))
1415 return I->getParent();
1416 return nullptr;
1417}
1418
1419struct NewGVN::ValueDFS {
1420 int DFSIn;
1421 int DFSOut;
1422 int LocalNum;
1423 // Only one of these will be set.
1424 Value *Val;
1425 Use *U;
1426 ValueDFS()
1427 : DFSIn(0), DFSOut(0), LocalNum(0), Val(nullptr), U(nullptr) {}
1428
1429 bool operator<(const ValueDFS &Other) const {
1430 // It's not enough that any given field be less than - we have sets
1431 // of fields that need to be evaluated together to give a proper ordering.
1432 // For example, if you have;
1433 // DFS (1, 3)
1434 // Val 0
1435 // DFS (1, 2)
1436 // Val 50
1437 // We want the second to be less than the first, but if we just go field
1438 // by field, we will get to Val 0 < Val 50 and say the first is less than
1439 // the second. We only want it to be less than if the DFS orders are equal.
1440 //
1441 // Each LLVM instruction only produces one value, and thus the lowest-level
1442 // differentiator that really matters for the stack (and what we use as as a
1443 // replacement) is the local dfs number.
1444 // Everything else in the structure is instruction level, and only affects the
1445 // order in which we will replace operands of a given instruction.
1446 //
1447 // For a given instruction (IE things with equal dfsin, dfsout, localnum),
1448 // the order of replacement of uses does not matter.
1449 // IE given,
1450 // a = 5
1451 // b = a + a
1452 // When you hit b, you will have two valuedfs with the same dfsin, out, and localnum.
1453 // The .val will be the same as well.
1454 // The .u's will be different.
1455 // You will replace both, and it does not matter what order you replace them in
1456 // (IE whether you replace operand 2, then operand 1, or operand 1, then operand 2).
1457 // Similarly for the case of same dfsin, dfsout, localnum, but different .val's
1458 // a = 5
1459 // b = 6
1460 // c = a + b
1461 // in c, we will a valuedfs for a, and one for b,with everything the same but
1462 // .val and .u.
1463 // It does not matter what order we replace these operands in.
1464 // You will always end up with the same IR, and this is guaranteed.
1465 return std::tie(DFSIn, DFSOut, LocalNum, Val, U) <
1466 std::tie(Other.DFSIn, Other.DFSOut, Other.LocalNum, Other.Val,
1467 Other.U);
1468 }
1469};
1470
1471void NewGVN::convertDenseToDFSOrdered(CongruenceClass::MemberSet &Dense,
1472 std::vector<ValueDFS> &DFSOrderedSet) {
1473 for (auto D : Dense) {
1474 // First add the value.
1475 BasicBlock *BB = getBlockForValue(D);
1476 // Constants are handled prior to ever calling this function, so
1477 // we should only be left with instructions as members.
Chandler Carruthee086762016-12-23 01:38:06 +00001478 assert(BB && "Should have figured out a basic block for value");
Davide Italiano7e274e02016-12-22 16:03:48 +00001479 ValueDFS VD;
1480
1481 std::pair<int, int> DFSPair = DFSDomMap[BB];
1482 assert(DFSPair.first != -1 && DFSPair.second != -1 && "Invalid DFS Pair");
1483 VD.DFSIn = DFSPair.first;
1484 VD.DFSOut = DFSPair.second;
1485 VD.Val = D;
1486 // If it's an instruction, use the real local dfs number.
1487 if (auto *I = dyn_cast<Instruction>(D))
1488 VD.LocalNum = InstrDFS[I];
1489 else
1490 llvm_unreachable("Should have been an instruction");
1491
1492 DFSOrderedSet.emplace_back(VD);
1493
1494 // Now add the users.
1495 for (auto &U : D->uses()) {
1496 if (auto *I = dyn_cast<Instruction>(U.getUser())) {
1497 ValueDFS VD;
1498 // Put the phi node uses in the incoming block.
1499 BasicBlock *IBlock;
1500 if (auto *P = dyn_cast<PHINode>(I)) {
1501 IBlock = P->getIncomingBlock(U);
1502 // Make phi node users appear last in the incoming block
1503 // they are from.
1504 VD.LocalNum = InstrDFS.size() + 1;
1505 } else {
1506 IBlock = I->getParent();
1507 VD.LocalNum = InstrDFS[I];
1508 }
1509 std::pair<int, int> DFSPair = DFSDomMap[IBlock];
1510 VD.DFSIn = DFSPair.first;
1511 VD.DFSOut = DFSPair.second;
1512 VD.U = &U;
1513 DFSOrderedSet.emplace_back(VD);
1514 }
1515 }
1516 }
1517}
1518
1519static void patchReplacementInstruction(Instruction *I, Value *Repl) {
1520 // Patch the replacement so that it is not more restrictive than the value
1521 // being replaced.
1522 auto *Op = dyn_cast<BinaryOperator>(I);
1523 auto *ReplOp = dyn_cast<BinaryOperator>(Repl);
1524
1525 if (Op && ReplOp)
1526 ReplOp->andIRFlags(Op);
1527
1528 if (auto *ReplInst = dyn_cast<Instruction>(Repl)) {
1529 // FIXME: If both the original and replacement value are part of the
1530 // same control-flow region (meaning that the execution of one
1531 // guarentees the executation of the other), then we can combine the
1532 // noalias scopes here and do better than the general conservative
1533 // answer used in combineMetadata().
1534
1535 // In general, GVN unifies expressions over different control-flow
1536 // regions, and so we need a conservative combination of the noalias
1537 // scopes.
1538 unsigned KnownIDs[] = {
1539 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
1540 LLVMContext::MD_noalias, LLVMContext::MD_range,
1541 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
1542 LLVMContext::MD_invariant_group};
1543 combineMetadata(ReplInst, I, KnownIDs);
1544 }
1545}
1546
1547static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
1548 patchReplacementInstruction(I, Repl);
1549 I->replaceAllUsesWith(Repl);
1550}
1551
1552void NewGVN::deleteInstructionsInBlock(BasicBlock *BB) {
1553 DEBUG(dbgs() << " BasicBlock Dead:" << *BB);
1554 ++NumGVNBlocksDeleted;
1555
1556 // Check to see if there are non-terminating instructions to delete.
1557 if (isa<TerminatorInst>(BB->begin()))
1558 return;
1559
1560 // Delete the instructions backwards, as it has a reduced likelihood of having
1561 // to update as many def-use and use-def chains. Start after the terminator.
1562 auto StartPoint = BB->rbegin();
1563 ++StartPoint;
1564 // Note that we explicitly recalculate BB->rend() on each iteration,
1565 // as it may change when we remove the first instruction.
1566 for (BasicBlock::reverse_iterator I(StartPoint); I != BB->rend();) {
1567 Instruction &Inst = *I++;
1568 if (!Inst.use_empty())
1569 Inst.replaceAllUsesWith(UndefValue::get(Inst.getType()));
1570 if (isa<LandingPadInst>(Inst))
1571 continue;
1572
1573 Inst.eraseFromParent();
1574 ++NumGVNInstrDeleted;
1575 }
1576}
1577
1578void NewGVN::markInstructionForDeletion(Instruction *I) {
1579 DEBUG(dbgs() << "Marking " << *I << " for deletion\n");
1580 InstructionsToErase.insert(I);
1581}
1582
1583void NewGVN::replaceInstruction(Instruction *I, Value *V) {
1584
1585 DEBUG(dbgs() << "Replacing " << *I << " with " << *V << "\n");
1586 patchAndReplaceAllUsesWith(I, V);
1587 // We save the actual erasing to avoid invalidating memory
1588 // dependencies until we are done with everything.
1589 markInstructionForDeletion(I);
1590}
1591
1592namespace {
1593
1594// This is a stack that contains both the value and dfs info of where
1595// that value is valid.
1596class ValueDFSStack {
1597public:
1598 Value *back() const { return ValueStack.back(); }
1599 std::pair<int, int> dfs_back() const { return DFSStack.back(); }
1600
1601 void push_back(Value *V, int DFSIn, int DFSOut) {
1602 ValueStack.emplace_back(V);
1603 DFSStack.emplace_back(DFSIn, DFSOut);
1604 }
1605 bool empty() const { return DFSStack.empty(); }
1606 bool isInScope(int DFSIn, int DFSOut) const {
1607 if (empty())
1608 return false;
1609 return DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second;
1610 }
1611
1612 void popUntilDFSScope(int DFSIn, int DFSOut) {
1613
1614 // These two should always be in sync at this point.
1615 assert(ValueStack.size() == DFSStack.size() &&
1616 "Mismatch between ValueStack and DFSStack");
1617 while (
1618 !DFSStack.empty() &&
1619 !(DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second)) {
1620 DFSStack.pop_back();
1621 ValueStack.pop_back();
1622 }
1623 }
1624
1625private:
1626 SmallVector<Value *, 8> ValueStack;
1627 SmallVector<std::pair<int, int>, 8> DFSStack;
1628};
1629}
1630
1631bool NewGVN::eliminateInstructions(Function &F) {
1632 // This is a non-standard eliminator. The normal way to eliminate is
1633 // to walk the dominator tree in order, keeping track of available
1634 // values, and eliminating them. However, this is mildly
1635 // pointless. It requires doing lookups on every instruction,
1636 // regardless of whether we will ever eliminate it. For
1637 // instructions part of most singleton congruence class, we know we
1638 // will never eliminate it.
1639
1640 // Instead, this eliminator looks at the congruence classes directly, sorts
1641 // them into a DFS ordering of the dominator tree, and then we just
1642 // perform eliminate straight on the sets by walking the congruence
1643 // class member uses in order, and eliminate the ones dominated by the
1644 // last member. This is technically O(N log N) where N = number of
1645 // instructions (since in theory all instructions may be in the same
1646 // congruence class).
1647 // When we find something not dominated, it becomes the new leader
1648 // for elimination purposes
1649
1650 bool AnythingReplaced = false;
1651
1652 // Since we are going to walk the domtree anyway, and we can't guarantee the
1653 // DFS numbers are updated, we compute some ourselves.
1654 DT->updateDFSNumbers();
1655
1656 for (auto &B : F) {
1657 if (!ReachableBlocks.count(&B)) {
1658 for (const auto S : successors(&B)) {
1659 for (auto II = S->begin(); isa<PHINode>(II); ++II) {
1660 PHINode &Phi = cast<PHINode>(*II);
1661 DEBUG(dbgs() << "Replacing incoming value of " << *II << " for block "
1662 << getBlockName(&B)
1663 << " with undef due to it being unreachable\n");
1664 for (auto &Operand : Phi.incoming_values())
1665 if (Phi.getIncomingBlock(Operand) == &B)
1666 Operand.set(UndefValue::get(Phi.getType()));
1667 }
1668 }
1669 }
1670 DomTreeNode *Node = DT->getNode(&B);
1671 if (Node)
1672 DFSDomMap[&B] = {Node->getDFSNumIn(), Node->getDFSNumOut()};
1673 }
1674
1675 for (CongruenceClass *CC : CongruenceClasses) {
1676 // FIXME: We should eventually be able to replace everything still
1677 // in the initial class with undef, as they should be unreachable.
1678 // Right now, initial still contains some things we skip value
1679 // numbering of (UNREACHABLE's, for example).
1680 if (CC == InitialClass || CC->Dead)
1681 continue;
1682 assert(CC->RepLeader && "We should have had a leader");
1683
1684 // If this is a leader that is always available, and it's a
1685 // constant or has no equivalences, just replace everything with
1686 // it. We then update the congruence class with whatever members
1687 // are left.
1688 if (alwaysAvailable(CC->RepLeader)) {
1689 SmallPtrSet<Value *, 4> MembersLeft;
1690 for (auto M : CC->Members) {
1691
1692 Value *Member = M;
1693
1694 // Void things have no uses we can replace.
1695 if (Member == CC->RepLeader || Member->getType()->isVoidTy()) {
1696 MembersLeft.insert(Member);
1697 continue;
1698 }
1699
1700 DEBUG(dbgs() << "Found replacement " << *(CC->RepLeader) << " for "
1701 << *Member << "\n");
1702 // Due to equality propagation, these may not always be
1703 // instructions, they may be real values. We don't really
1704 // care about trying to replace the non-instructions.
1705 if (auto *I = dyn_cast<Instruction>(Member)) {
1706 assert(CC->RepLeader != I &&
1707 "About to accidentally remove our leader");
1708 replaceInstruction(I, CC->RepLeader);
1709 AnythingReplaced = true;
1710
1711 continue;
1712 } else {
1713 MembersLeft.insert(I);
1714 }
1715 }
1716 CC->Members.swap(MembersLeft);
1717
1718 } else {
1719 DEBUG(dbgs() << "Eliminating in congruence class " << CC->ID << "\n");
1720 // If this is a singleton, we can skip it.
1721 if (CC->Members.size() != 1) {
1722
1723 // This is a stack because equality replacement/etc may place
1724 // constants in the middle of the member list, and we want to use
1725 // those constant values in preference to the current leader, over
1726 // the scope of those constants.
1727 ValueDFSStack EliminationStack;
1728
1729 // Convert the members to DFS ordered sets and then merge them.
1730 std::vector<ValueDFS> DFSOrderedSet;
1731 convertDenseToDFSOrdered(CC->Members, DFSOrderedSet);
1732
1733 // Sort the whole thing.
1734 sort(DFSOrderedSet.begin(), DFSOrderedSet.end());
1735
1736 for (auto &C : DFSOrderedSet) {
1737 int MemberDFSIn = C.DFSIn;
1738 int MemberDFSOut = C.DFSOut;
1739 Value *Member = C.Val;
1740 Use *MemberUse = C.U;
1741
1742 // We ignore void things because we can't get a value from them.
1743 if (Member && Member->getType()->isVoidTy())
1744 continue;
1745
1746 if (EliminationStack.empty()) {
1747 DEBUG(dbgs() << "Elimination Stack is empty\n");
1748 } else {
1749 DEBUG(dbgs() << "Elimination Stack Top DFS numbers are ("
1750 << EliminationStack.dfs_back().first << ","
1751 << EliminationStack.dfs_back().second << ")\n");
1752 }
1753 if (Member && isa<Constant>(Member))
1754 assert(isa<Constant>(CC->RepLeader));
1755
1756 DEBUG(dbgs() << "Current DFS numbers are (" << MemberDFSIn << ","
1757 << MemberDFSOut << ")\n");
1758 // First, we see if we are out of scope or empty. If so,
1759 // and there equivalences, we try to replace the top of
1760 // stack with equivalences (if it's on the stack, it must
1761 // not have been eliminated yet).
1762 // Then we synchronize to our current scope, by
1763 // popping until we are back within a DFS scope that
1764 // dominates the current member.
1765 // Then, what happens depends on a few factors
1766 // If the stack is now empty, we need to push
1767 // If we have a constant or a local equivalence we want to
1768 // start using, we also push.
1769 // Otherwise, we walk along, processing members who are
1770 // dominated by this scope, and eliminate them.
1771 bool ShouldPush =
1772 Member && (EliminationStack.empty() || isa<Constant>(Member));
1773 bool OutOfScope =
1774 !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut);
1775
1776 if (OutOfScope || ShouldPush) {
1777 // Sync to our current scope.
1778 EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
1779 ShouldPush |= Member && EliminationStack.empty();
1780 if (ShouldPush) {
1781 EliminationStack.push_back(Member, MemberDFSIn, MemberDFSOut);
1782 }
1783 }
1784
1785 // If we get to this point, and the stack is empty we must have a use
1786 // with nothing we can use to eliminate it, just skip it.
1787 if (EliminationStack.empty())
1788 continue;
1789
1790 // Skip the Value's, we only want to eliminate on their uses.
1791 if (Member)
1792 continue;
1793 Value *Result = EliminationStack.back();
1794
1795 // Don't replace our existing users with ourselves.
1796 if (MemberUse->get() == Result)
1797 continue;
1798
1799 DEBUG(dbgs() << "Found replacement " << *Result << " for "
1800 << *MemberUse->get() << " in " << *(MemberUse->getUser())
1801 << "\n");
1802
1803 // If we replaced something in an instruction, handle the patching of
1804 // metadata.
1805 if (auto *ReplacedInst =
1806 dyn_cast<Instruction>(MemberUse->get()))
1807 patchReplacementInstruction(ReplacedInst, Result);
1808
1809 assert(isa<Instruction>(MemberUse->getUser()));
1810 MemberUse->set(Result);
1811 AnythingReplaced = true;
1812 }
1813 }
1814 }
1815
1816 // Cleanup the congruence class.
1817 SmallPtrSet<Value *, 4> MembersLeft;
1818 for (auto MI = CC->Members.begin(), ME = CC->Members.end(); MI != ME;) {
1819 auto CurrIter = MI;
1820 ++MI;
1821 Value *Member = *CurrIter;
1822 if (Member->getType()->isVoidTy()) {
1823 MembersLeft.insert(Member);
1824 continue;
1825 }
1826
1827 if (auto *MemberInst = dyn_cast<Instruction>(Member)) {
1828 if (isInstructionTriviallyDead(MemberInst)) {
1829 // TODO: Don't mark loads of undefs.
1830 markInstructionForDeletion(MemberInst);
1831 continue;
1832 }
1833 }
1834 MembersLeft.insert(Member);
1835 }
1836 CC->Members.swap(MembersLeft);
1837 }
1838
1839 return AnythingReplaced;
1840}
1841