Bug Summary

File:lib/Transforms/Scalar/NewGVN.cpp
Warning:line 188, column 7
Forming reference to null pointer

Annotated Source Code

1//===---- 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/// A brief overview of the algorithm: The algorithm is essentially the same as
21/// the standard RPO value numbering algorithm (a good reference is the paper
22/// "SCC based value numbering" by L. Taylor Simpson) with one major difference:
23/// The RPO algorithm proceeds, on every iteration, to process every reachable
24/// block and every instruction in that block. This is because the standard RPO
25/// algorithm does not track what things have the same value number, it only
26/// tracks what the value number of a given operation is (the mapping is
27/// operation -> value number). Thus, when a value number of an operation
28/// changes, it must reprocess everything to ensure all uses of a value number
29/// get updated properly. In constrast, the sparse algorithm we use *also*
30/// tracks what operations have a given value number (IE it also tracks the
31/// reverse mapping from value number -> operations with that value number), so
32/// that it only needs to reprocess the instructions that are affected when
33/// something's value number changes. The rest of the algorithm is devoted to
34/// performing symbolic evaluation, forward propagation, and simplification of
35/// operations based on the value numbers deduced so far.
36///
37/// We also do not perform elimination by using any published algorithm. All
38/// published algorithms are O(Instructions). Instead, we use a technique that
39/// is O(number of operations with the same value number), enabling us to skip
40/// trying to eliminate things that have unique value numbers.
41//===----------------------------------------------------------------------===//
42
43#include "llvm/Transforms/Scalar/NewGVN.h"
44#include "llvm/ADT/BitVector.h"
45#include "llvm/ADT/DenseMap.h"
46#include "llvm/ADT/DenseSet.h"
47#include "llvm/ADT/DepthFirstIterator.h"
48#include "llvm/ADT/Hashing.h"
49#include "llvm/ADT/MapVector.h"
50#include "llvm/ADT/PostOrderIterator.h"
51#include "llvm/ADT/STLExtras.h"
52#include "llvm/ADT/SmallPtrSet.h"
53#include "llvm/ADT/SmallSet.h"
54#include "llvm/ADT/SparseBitVector.h"
55#include "llvm/ADT/Statistic.h"
56#include "llvm/ADT/TinyPtrVector.h"
57#include "llvm/Analysis/AliasAnalysis.h"
58#include "llvm/Analysis/AssumptionCache.h"
59#include "llvm/Analysis/CFG.h"
60#include "llvm/Analysis/CFGPrinter.h"
61#include "llvm/Analysis/ConstantFolding.h"
62#include "llvm/Analysis/GlobalsModRef.h"
63#include "llvm/Analysis/InstructionSimplify.h"
64#include "llvm/Analysis/MemoryBuiltins.h"
65#include "llvm/Analysis/MemoryLocation.h"
66#include "llvm/Analysis/TargetLibraryInfo.h"
67#include "llvm/IR/DataLayout.h"
68#include "llvm/IR/Dominators.h"
69#include "llvm/IR/GlobalVariable.h"
70#include "llvm/IR/IRBuilder.h"
71#include "llvm/IR/IntrinsicInst.h"
72#include "llvm/IR/LLVMContext.h"
73#include "llvm/IR/Metadata.h"
74#include "llvm/IR/PatternMatch.h"
75#include "llvm/IR/Type.h"
76#include "llvm/Support/Allocator.h"
77#include "llvm/Support/CommandLine.h"
78#include "llvm/Support/Debug.h"
79#include "llvm/Support/DebugCounter.h"
80#include "llvm/Transforms/Scalar.h"
81#include "llvm/Transforms/Scalar/GVNExpression.h"
82#include "llvm/Transforms/Utils/BasicBlockUtils.h"
83#include "llvm/Transforms/Utils/Local.h"
84#include "llvm/Transforms/Utils/MemorySSA.h"
85#include "llvm/Transforms/Utils/PredicateInfo.h"
86#include <unordered_map>
87#include <utility>
88#include <vector>
89using namespace llvm;
90using namespace PatternMatch;
91using namespace llvm::GVNExpression;
92#define DEBUG_TYPE"newgvn" "newgvn"
93
94STATISTIC(NumGVNInstrDeleted, "Number of instructions deleted")static llvm::Statistic NumGVNInstrDeleted = {"newgvn", "NumGVNInstrDeleted"
, "Number of instructions deleted", {0}, false}
;
95STATISTIC(NumGVNBlocksDeleted, "Number of blocks deleted")static llvm::Statistic NumGVNBlocksDeleted = {"newgvn", "NumGVNBlocksDeleted"
, "Number of blocks deleted", {0}, false}
;
96STATISTIC(NumGVNOpsSimplified, "Number of Expressions simplified")static llvm::Statistic NumGVNOpsSimplified = {"newgvn", "NumGVNOpsSimplified"
, "Number of Expressions simplified", {0}, false}
;
97STATISTIC(NumGVNPhisAllSame, "Number of PHIs whos arguments are all the same")static llvm::Statistic NumGVNPhisAllSame = {"newgvn", "NumGVNPhisAllSame"
, "Number of PHIs whos arguments are all the same", {0}, false
}
;
98STATISTIC(NumGVNMaxIterations,static llvm::Statistic NumGVNMaxIterations = {"newgvn", "NumGVNMaxIterations"
, "Maximum Number of iterations it took to converge GVN", {0}
, false}
99 "Maximum Number of iterations it took to converge GVN")static llvm::Statistic NumGVNMaxIterations = {"newgvn", "NumGVNMaxIterations"
, "Maximum Number of iterations it took to converge GVN", {0}
, false}
;
100STATISTIC(NumGVNLeaderChanges, "Number of leader changes")static llvm::Statistic NumGVNLeaderChanges = {"newgvn", "NumGVNLeaderChanges"
, "Number of leader changes", {0}, false}
;
101STATISTIC(NumGVNSortedLeaderChanges, "Number of sorted leader changes")static llvm::Statistic NumGVNSortedLeaderChanges = {"newgvn",
"NumGVNSortedLeaderChanges", "Number of sorted leader changes"
, {0}, false}
;
102STATISTIC(NumGVNAvoidedSortedLeaderChanges,static llvm::Statistic NumGVNAvoidedSortedLeaderChanges = {"newgvn"
, "NumGVNAvoidedSortedLeaderChanges", "Number of avoided sorted leader changes"
, {0}, false}
103 "Number of avoided sorted leader changes")static llvm::Statistic NumGVNAvoidedSortedLeaderChanges = {"newgvn"
, "NumGVNAvoidedSortedLeaderChanges", "Number of avoided sorted leader changes"
, {0}, false}
;
104STATISTIC(NumGVNNotMostDominatingLeader,static llvm::Statistic NumGVNNotMostDominatingLeader = {"newgvn"
, "NumGVNNotMostDominatingLeader", "Number of times a member dominated it's new classes' leader"
, {0}, false}
105 "Number of times a member dominated it's new classes' leader")static llvm::Statistic NumGVNNotMostDominatingLeader = {"newgvn"
, "NumGVNNotMostDominatingLeader", "Number of times a member dominated it's new classes' leader"
, {0}, false}
;
106STATISTIC(NumGVNDeadStores, "Number of redundant/dead stores eliminated")static llvm::Statistic NumGVNDeadStores = {"newgvn", "NumGVNDeadStores"
, "Number of redundant/dead stores eliminated", {0}, false}
;
107DEBUG_COUNTER(VNCounter, "newgvn-vn",static const unsigned VNCounter = DebugCounter::registerCounter
("newgvn-vn", "Controls which instructions are value numbered"
);
108 "Controls which instructions are value numbered")static const unsigned VNCounter = DebugCounter::registerCounter
("newgvn-vn", "Controls which instructions are value numbered"
);
109//===----------------------------------------------------------------------===//
110// GVN Pass
111//===----------------------------------------------------------------------===//
112
113// Anchor methods.
114namespace llvm {
115namespace GVNExpression {
116Expression::~Expression() = default;
117BasicExpression::~BasicExpression() = default;
118CallExpression::~CallExpression() = default;
119LoadExpression::~LoadExpression() = default;
120StoreExpression::~StoreExpression() = default;
121AggregateValueExpression::~AggregateValueExpression() = default;
122PHIExpression::~PHIExpression() = default;
123}
124}
125
126// Congruence classes represent the set of expressions/instructions
127// that are all the same *during some scope in the function*.
128// That is, because of the way we perform equality propagation, and
129// because of memory value numbering, it is not correct to assume
130// you can willy-nilly replace any member with any other at any
131// point in the function.
132//
133// For any Value in the Member set, it is valid to replace any dominated member
134// with that Value.
135//
136// Every congruence class has a leader, and the leader is used to
137// symbolize instructions in a canonical way (IE every operand of an
138// instruction that is a member of the same congruence class will
139// always be replaced with leader during symbolization).
140// To simplify symbolization, we keep the leader as a constant if class can be
141// proved to be a constant value.
142// Otherwise, the leader is a randomly chosen member of the value set, it does
143// not matter which one is chosen.
144// Each congruence class also has a defining expression,
145// though the expression may be null. If it exists, it can be used for forward
146// propagation and reassociation of values.
147//
148struct CongruenceClass {
149 using MemberSet = SmallPtrSet<Value *, 4>;
150 unsigned ID;
151 // Representative leader.
152 Value *RepLeader = nullptr;
153 // If this is represented by a store, the value.
154 Value *RepStoredValue = nullptr;
155 // If this class contains MemoryDefs, what is the represented memory state.
156 MemoryAccess *RepMemoryAccess = nullptr;
157 // Defining Expression.
158 const Expression *DefiningExpr = nullptr;
159 // Actual members of this class.
160 MemberSet Members;
161
162 // True if this class has no members left. This is mainly used for assertion
163 // purposes, and for skipping empty classes.
164 bool Dead = false;
165
166 // Number of stores in this congruence class.
167 // This is used so we can detect store equivalence changes properly.
168 int StoreCount = 0;
169
170 // The most dominating leader after our current leader, because the member set
171 // is not sorted and is expensive to keep sorted all the time.
172 std::pair<Value *, unsigned int> NextLeader = {nullptr, ~0U};
173
174 explicit CongruenceClass(unsigned ID) : ID(ID) {}
175 CongruenceClass(unsigned ID, Value *Leader, const Expression *E)
176 : ID(ID), RepLeader(Leader), DefiningExpr(E) {}
177};
178
179// Return true if two congruence classes are equivalent to each other. This
180// means
181// that every field but the ID number and the dead field are equivalent.
182bool areClassesEquivalent(const CongruenceClass *A, const CongruenceClass *B) {
183 if (A == B)
7
Taking false branch
184 return true;
185 if ((A && !B) || (B && !A))
8
Assuming 'A' is null
9
Assuming 'B' is null
186 return false;
187
188 if (std::tie(A->StoreCount, A->RepLeader, A->RepStoredValue,
10
Forming reference to null pointer
189 A->RepMemoryAccess) != std::tie(B->StoreCount, B->RepLeader,
190 B->RepStoredValue,
191 B->RepMemoryAccess))
192 return false;
193 if (A->DefiningExpr != B->DefiningExpr)
194 if (!A->DefiningExpr || !B->DefiningExpr ||
195 *A->DefiningExpr != *B->DefiningExpr)
196 return false;
197 // We need some ordered set
198 std::set<Value *> AMembers(A->Members.begin(), A->Members.end());
199 std::set<Value *> BMembers(B->Members.begin(), B->Members.end());
200 return AMembers == BMembers;
201}
202
203namespace llvm {
204template <> struct DenseMapInfo<const Expression *> {
205 static const Expression *getEmptyKey() {
206 auto Val = static_cast<uintptr_t>(-1);
207 Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
208 return reinterpret_cast<const Expression *>(Val);
209 }
210 static const Expression *getTombstoneKey() {
211 auto Val = static_cast<uintptr_t>(~1U);
212 Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
213 return reinterpret_cast<const Expression *>(Val);
214 }
215 static unsigned getHashValue(const Expression *V) {
216 return static_cast<unsigned>(V->getHashValue());
217 }
218 static bool isEqual(const Expression *LHS, const Expression *RHS) {
219 if (LHS == RHS)
220 return true;
221 if (LHS == getTombstoneKey() || RHS == getTombstoneKey() ||
222 LHS == getEmptyKey() || RHS == getEmptyKey())
223 return false;
224 return *LHS == *RHS;
225 }
226};
227} // end namespace llvm
228
229namespace {
230class NewGVN {
231 Function &F;
232 DominatorTree *DT;
233 AssumptionCache *AC;
234 const TargetLibraryInfo *TLI;
235 AliasAnalysis *AA;
236 MemorySSA *MSSA;
237 MemorySSAWalker *MSSAWalker;
238 const DataLayout &DL;
239 std::unique_ptr<PredicateInfo> PredInfo;
240 BumpPtrAllocator ExpressionAllocator;
241 ArrayRecycler<Value *> ArgRecycler;
242
243 // Number of function arguments, used by ranking
244 unsigned int NumFuncArgs;
245
246 // Congruence class info.
247
248 // This class is called INITIAL in the paper. It is the class everything
249 // startsout in, and represents any value. Being an optimistic analysis,
250 // anything in the TOP class has the value TOP, which is indeterminate and
251 // equivalent to everything.
252 CongruenceClass *TOPClass;
253 std::vector<CongruenceClass *> CongruenceClasses;
254 unsigned NextCongruenceNum;
255
256 // Value Mappings.
257 DenseMap<Value *, CongruenceClass *> ValueToClass;
258 DenseMap<Value *, const Expression *> ValueToExpression;
259
260 // Mapping from predicate info we used to the instructions we used it with.
261 // In order to correctly ensure propagation, we must keep track of what
262 // comparisons we used, so that when the values of the comparisons change, we
263 // propagate the information to the places we used the comparison.
264 DenseMap<const Value *, SmallPtrSet<Instruction *, 2>> PredicateToUsers;
265
266 // A table storing which memorydefs/phis represent a memory state provably
267 // equivalent to another memory state.
268 // We could use the congruence class machinery, but the MemoryAccess's are
269 // abstract memory states, so they can only ever be equivalent to each other,
270 // and not to constants, etc.
271 DenseMap<const MemoryAccess *, CongruenceClass *> MemoryAccessToClass;
272
273 // Expression to class mapping.
274 using ExpressionClassMap = DenseMap<const Expression *, CongruenceClass *>;
275 ExpressionClassMap ExpressionToClass;
276
277 // Which values have changed as a result of leader changes.
278 SmallPtrSet<Value *, 8> LeaderChanges;
279
280 // Reachability info.
281 using BlockEdge = BasicBlockEdge;
282 DenseSet<BlockEdge> ReachableEdges;
283 SmallPtrSet<const BasicBlock *, 8> ReachableBlocks;
284
285 // This is a bitvector because, on larger functions, we may have
286 // thousands of touched instructions at once (entire blocks,
287 // instructions with hundreds of uses, etc). Even with optimization
288 // for when we mark whole blocks as touched, when this was a
289 // SmallPtrSet or DenseSet, for some functions, we spent >20% of all
290 // the time in GVN just managing this list. The bitvector, on the
291 // other hand, efficiently supports test/set/clear of both
292 // individual and ranges, as well as "find next element" This
293 // enables us to use it as a worklist with essentially 0 cost.
294 BitVector TouchedInstructions;
295
296 DenseMap<const BasicBlock *, std::pair<unsigned, unsigned>> BlockInstRange;
297
298#ifndef NDEBUG
299 // Debugging for how many times each block and instruction got processed.
300 DenseMap<const Value *, unsigned> ProcessedCount;
301#endif
302
303 // DFS info.
304 // This contains a mapping from Instructions to DFS numbers.
305 // The numbering starts at 1. An instruction with DFS number zero
306 // means that the instruction is dead.
307 DenseMap<const Value *, unsigned> InstrDFS;
308
309 // This contains the mapping DFS numbers to instructions.
310 SmallVector<Value *, 32> DFSToInstr;
311
312 // Deletion info.
313 SmallPtrSet<Instruction *, 8> InstructionsToErase;
314
315public:
316 NewGVN(Function &F, DominatorTree *DT, AssumptionCache *AC,
317 TargetLibraryInfo *TLI, AliasAnalysis *AA, MemorySSA *MSSA,
318 const DataLayout &DL)
319 : F(F), DT(DT), AC(AC), TLI(TLI), AA(AA), MSSA(MSSA), DL(DL),
320 PredInfo(make_unique<PredicateInfo>(F, *DT, *AC)) {}
321 bool runGVN();
322
323private:
324 // Expression handling.
325 const Expression *createExpression(Instruction *);
326 const Expression *createBinaryExpression(unsigned, Type *, Value *, Value *);
327 PHIExpression *createPHIExpression(Instruction *);
328 const VariableExpression *createVariableExpression(Value *);
329 const ConstantExpression *createConstantExpression(Constant *);
330 const Expression *createVariableOrConstant(Value *V);
331 const UnknownExpression *createUnknownExpression(Instruction *);
332 const StoreExpression *createStoreExpression(StoreInst *, MemoryAccess *);
333 LoadExpression *createLoadExpression(Type *, Value *, LoadInst *,
334 MemoryAccess *);
335 const CallExpression *createCallExpression(CallInst *, MemoryAccess *);
336 const AggregateValueExpression *createAggregateValueExpression(Instruction *);
337 bool setBasicExpressionInfo(Instruction *, BasicExpression *);
338
339 // Congruence class handling.
340 CongruenceClass *createCongruenceClass(Value *Leader, const Expression *E) {
341 auto *result = new CongruenceClass(NextCongruenceNum++, Leader, E);
342 CongruenceClasses.emplace_back(result);
343 return result;
344 }
345
346 CongruenceClass *createSingletonCongruenceClass(Value *Member) {
347 CongruenceClass *CClass = createCongruenceClass(Member, nullptr);
348 CClass->Members.insert(Member);
349 ValueToClass[Member] = CClass;
350 return CClass;
351 }
352 void initializeCongruenceClasses(Function &F);
353
354 // Value number an Instruction or MemoryPhi.
355 void valueNumberMemoryPhi(MemoryPhi *);
356 void valueNumberInstruction(Instruction *);
357
358 // Symbolic evaluation.
359 const Expression *checkSimplificationResults(Expression *, Instruction *,
360 Value *);
361 const Expression *performSymbolicEvaluation(Value *);
362 const Expression *performSymbolicLoadEvaluation(Instruction *);
363 const Expression *performSymbolicStoreEvaluation(Instruction *);
364 const Expression *performSymbolicCallEvaluation(Instruction *);
365 const Expression *performSymbolicPHIEvaluation(Instruction *);
366 const Expression *performSymbolicAggrValueEvaluation(Instruction *);
367 const Expression *performSymbolicCmpEvaluation(Instruction *);
368 const Expression *performSymbolicPredicateInfoEvaluation(Instruction *);
369
370 // Congruence finding.
371 Value *lookupOperandLeader(Value *) const;
372 void performCongruenceFinding(Instruction *, const Expression *);
373 void moveValueToNewCongruenceClass(Instruction *, CongruenceClass *,
374 CongruenceClass *);
375 bool setMemoryAccessEquivTo(MemoryAccess *From, CongruenceClass *To);
376 MemoryAccess *lookupMemoryAccessEquiv(MemoryAccess *) const;
377 bool isMemoryAccessTop(const MemoryAccess *) const;
378 // Ranking
379 unsigned int getRank(const Value *) const;
380 bool shouldSwapOperands(const Value *, const Value *) const;
381
382 // Reachability handling.
383 void updateReachableEdge(BasicBlock *, BasicBlock *);
384 void processOutgoingEdges(TerminatorInst *, BasicBlock *);
385 Value *findConditionEquivalence(Value *) const;
386
387 // Elimination.
388 struct ValueDFS;
389 void convertClassToDFSOrdered(const CongruenceClass::MemberSet &,
390 SmallVectorImpl<ValueDFS> &,
391 DenseMap<const Value *, unsigned int> &,
392 SmallPtrSetImpl<Instruction *> &);
393 void convertClassToLoadsAndStores(const CongruenceClass::MemberSet &,
394 SmallVectorImpl<ValueDFS> &);
395
396 bool eliminateInstructions(Function &);
397 void replaceInstruction(Instruction *, Value *);
398 void markInstructionForDeletion(Instruction *);
399 void deleteInstructionsInBlock(BasicBlock *);
400
401 // New instruction creation.
402 void handleNewInstruction(Instruction *){};
403
404 // Various instruction touch utilities
405 void markUsersTouched(Value *);
406 void markMemoryUsersTouched(MemoryAccess *);
407 void markPredicateUsersTouched(Instruction *);
408 void markLeaderChangeTouched(CongruenceClass *CC);
409 void addPredicateUsers(const PredicateBase *, Instruction *);
410
411 // Main loop of value numbering
412 void iterateTouchedInstructions();
413
414 // Utilities.
415 void cleanupTables();
416 std::pair<unsigned, unsigned> assignDFSNumbers(BasicBlock *, unsigned);
417 void updateProcessedCount(Value *V);
418 void verifyMemoryCongruency() const;
419 void verifyIterationSettled(Function &F);
420 bool singleReachablePHIPath(const MemoryAccess *, const MemoryAccess *) const;
421 BasicBlock *getBlockForValue(Value *V) const;
422 // Debug counter info. When verifying, we have to reset the value numbering
423 // debug counter to the same state it started in to get the same results.
424 std::pair<int, int> StartingVNCounter;
425};
426} // end anonymous namespace
427
428template <typename T>
429static bool equalsLoadStoreHelper(const T &LHS, const Expression &RHS) {
430 if ((!isa<LoadExpression>(RHS) && !isa<StoreExpression>(RHS)) ||
431 !LHS.BasicExpression::equals(RHS)) {
432 return false;
433 } else if (const auto *L = dyn_cast<LoadExpression>(&RHS)) {
434 if (LHS.getDefiningAccess() != L->getDefiningAccess())
435 return false;
436 } else if (const auto *S = dyn_cast<StoreExpression>(&RHS)) {
437 if (LHS.getDefiningAccess() != S->getDefiningAccess())
438 return false;
439 }
440 return true;
441}
442
443bool LoadExpression::equals(const Expression &Other) const {
444 return equalsLoadStoreHelper(*this, Other);
445}
446
447bool StoreExpression::equals(const Expression &Other) const {
448 bool Result = equalsLoadStoreHelper(*this, Other);
449 // Make sure that store vs store includes the value operand.
450 if (Result)
451 if (const auto *S = dyn_cast<StoreExpression>(&Other))
452 if (getStoredValue() != S->getStoredValue())
453 return false;
454 return Result;
455}
456
457#ifndef NDEBUG
458static std::string getBlockName(const BasicBlock *B) {
459 return DOTGraphTraits<const Function *>::getSimpleNodeLabel(B, nullptr);
460}
461#endif
462
463// Get the basic block from an instruction/memory value.
464BasicBlock *NewGVN::getBlockForValue(Value *V) const {
465 if (auto *I = dyn_cast<Instruction>(V))
466 return I->getParent();
467 else if (auto *MP = dyn_cast<MemoryPhi>(V))
468 return MP->getBlock();
469 llvm_unreachable("Should have been able to figure out a block for our value")::llvm::llvm_unreachable_internal("Should have been able to figure out a block for our value"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 469)
;
470 return nullptr;
471}
472
473PHIExpression *NewGVN::createPHIExpression(Instruction *I) {
474 BasicBlock *PHIBlock = I->getParent();
475 auto *PN = cast<PHINode>(I);
476 auto *E =
477 new (ExpressionAllocator) PHIExpression(PN->getNumOperands(), PHIBlock);
478
479 E->allocateOperands(ArgRecycler, ExpressionAllocator);
480 E->setType(I->getType());
481 E->setOpcode(I->getOpcode());
482
483 // Filter out unreachable phi operands.
484 auto Filtered = make_filter_range(PN->operands(), [&](const Use &U) {
485 return ReachableEdges.count({PN->getIncomingBlock(U), PHIBlock});
486 });
487
488 std::transform(Filtered.begin(), Filtered.end(), op_inserter(E),
489 [&](const Use &U) -> Value * {
490 // Don't try to transform self-defined phis.
491 if (U == PN)
492 return PN;
493 return lookupOperandLeader(U);
494 });
495 return E;
496}
497
498// Set basic expression info (Arguments, type, opcode) for Expression
499// E from Instruction I in block B.
500bool NewGVN::setBasicExpressionInfo(Instruction *I, BasicExpression *E) {
501 bool AllConstant = true;
502 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
503 E->setType(GEP->getSourceElementType());
504 else
505 E->setType(I->getType());
506 E->setOpcode(I->getOpcode());
507 E->allocateOperands(ArgRecycler, ExpressionAllocator);
508
509 // Transform the operand array into an operand leader array, and keep track of
510 // whether all members are constant.
511 std::transform(I->op_begin(), I->op_end(), op_inserter(E), [&](Value *O) {
512 auto Operand = lookupOperandLeader(O);
513 AllConstant &= isa<Constant>(Operand);
514 return Operand;
515 });
516
517 return AllConstant;
518}
519
520const Expression *NewGVN::createBinaryExpression(unsigned Opcode, Type *T,
521 Value *Arg1, Value *Arg2) {
522 auto *E = new (ExpressionAllocator) BasicExpression(2);
523
524 E->setType(T);
525 E->setOpcode(Opcode);
526 E->allocateOperands(ArgRecycler, ExpressionAllocator);
527 if (Instruction::isCommutative(Opcode)) {
528 // Ensure that commutative instructions that only differ by a permutation
529 // of their operands get the same value number by sorting the operand value
530 // numbers. Since all commutative instructions have two operands it is more
531 // efficient to sort by hand rather than using, say, std::sort.
532 if (shouldSwapOperands(Arg1, Arg2))
533 std::swap(Arg1, Arg2);
534 }
535 E->op_push_back(lookupOperandLeader(Arg1));
536 E->op_push_back(lookupOperandLeader(Arg2));
537
538 Value *V = SimplifyBinOp(Opcode, E->getOperand(0), E->getOperand(1), DL, TLI,
539 DT, AC);
540 if (const Expression *SimplifiedE = checkSimplificationResults(E, nullptr, V))
541 return SimplifiedE;
542 return E;
543}
544
545// Take a Value returned by simplification of Expression E/Instruction
546// I, and see if it resulted in a simpler expression. If so, return
547// that expression.
548// TODO: Once finished, this should not take an Instruction, we only
549// use it for printing.
550const Expression *NewGVN::checkSimplificationResults(Expression *E,
551 Instruction *I, Value *V) {
552 if (!V)
553 return nullptr;
554 if (auto *C = dyn_cast<Constant>(V)) {
555 if (I)
556 DEBUG(dbgs() << "Simplified " << *I << " to "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Simplified " << *I <<
" to " << " constant " << *C << "\n"; } } while
(false)
557 << " constant " << *C << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Simplified " << *I <<
" to " << " constant " << *C << "\n"; } } while
(false)
;
558 NumGVNOpsSimplified++;
559 assert(isa<BasicExpression>(E) &&((isa<BasicExpression>(E) && "We should always have had a basic expression here"
) ? static_cast<void> (0) : __assert_fail ("isa<BasicExpression>(E) && \"We should always have had a basic expression here\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 560, __PRETTY_FUNCTION__))
560 "We should always have had a basic expression here")((isa<BasicExpression>(E) && "We should always have had a basic expression here"
) ? static_cast<void> (0) : __assert_fail ("isa<BasicExpression>(E) && \"We should always have had a basic expression here\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 560, __PRETTY_FUNCTION__))
;
561
562 cast<BasicExpression>(E)->deallocateOperands(ArgRecycler);
563 ExpressionAllocator.Deallocate(E);
564 return createConstantExpression(C);
565 } else if (isa<Argument>(V) || isa<GlobalVariable>(V)) {
566 if (I)
567 DEBUG(dbgs() << "Simplified " << *I << " to "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Simplified " << *I <<
" to " << " variable " << *V << "\n"; } } while
(false)
568 << " variable " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Simplified " << *I <<
" to " << " variable " << *V << "\n"; } } while
(false)
;
569 cast<BasicExpression>(E)->deallocateOperands(ArgRecycler);
570 ExpressionAllocator.Deallocate(E);
571 return createVariableExpression(V);
572 }
573
574 CongruenceClass *CC = ValueToClass.lookup(V);
575 if (CC && CC->DefiningExpr) {
576 if (I)
577 DEBUG(dbgs() << "Simplified " << *I << " to "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Simplified " << *I <<
" to " << " expression " << *V << "\n"; } }
while (false)
578 << " expression " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Simplified " << *I <<
" to " << " expression " << *V << "\n"; } }
while (false)
;
579 NumGVNOpsSimplified++;
580 assert(isa<BasicExpression>(E) &&((isa<BasicExpression>(E) && "We should always have had a basic expression here"
) ? static_cast<void> (0) : __assert_fail ("isa<BasicExpression>(E) && \"We should always have had a basic expression here\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 581, __PRETTY_FUNCTION__))
581 "We should always have had a basic expression here")((isa<BasicExpression>(E) && "We should always have had a basic expression here"
) ? static_cast<void> (0) : __assert_fail ("isa<BasicExpression>(E) && \"We should always have had a basic expression here\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 581, __PRETTY_FUNCTION__))
;
582 cast<BasicExpression>(E)->deallocateOperands(ArgRecycler);
583 ExpressionAllocator.Deallocate(E);
584 return CC->DefiningExpr;
585 }
586 return nullptr;
587}
588
589const Expression *NewGVN::createExpression(Instruction *I) {
590 auto *E = new (ExpressionAllocator) BasicExpression(I->getNumOperands());
591
592 bool AllConstant = setBasicExpressionInfo(I, E);
593
594 if (I->isCommutative()) {
595 // Ensure that commutative instructions that only differ by a permutation
596 // of their operands get the same value number by sorting the operand value
597 // numbers. Since all commutative instructions have two operands it is more
598 // efficient to sort by hand rather than using, say, std::sort.
599 assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!")((I->getNumOperands() == 2 && "Unsupported commutative instruction!"
) ? static_cast<void> (0) : __assert_fail ("I->getNumOperands() == 2 && \"Unsupported commutative instruction!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 599, __PRETTY_FUNCTION__))
;
600 if (shouldSwapOperands(E->getOperand(0), E->getOperand(1)))
601 E->swapOperands(0, 1);
602 }
603
604 // Perform simplificaiton
605 // TODO: Right now we only check to see if we get a constant result.
606 // We may get a less than constant, but still better, result for
607 // some operations.
608 // IE
609 // add 0, x -> x
610 // and x, x -> x
611 // We should handle this by simply rewriting the expression.
612 if (auto *CI = dyn_cast<CmpInst>(I)) {
613 // Sort the operand value numbers so x<y and y>x get the same value
614 // number.
615 CmpInst::Predicate Predicate = CI->getPredicate();
616 if (shouldSwapOperands(E->getOperand(0), E->getOperand(1))) {
617 E->swapOperands(0, 1);
618 Predicate = CmpInst::getSwappedPredicate(Predicate);
619 }
620 E->setOpcode((CI->getOpcode() << 8) | Predicate);
621 // TODO: 25% of our time is spent in SimplifyCmpInst with pointer operands
622 assert(I->getOperand(0)->getType() == I->getOperand(1)->getType() &&((I->getOperand(0)->getType() == I->getOperand(1)->
getType() && "Wrong types on cmp instruction") ? static_cast
<void> (0) : __assert_fail ("I->getOperand(0)->getType() == I->getOperand(1)->getType() && \"Wrong types on cmp instruction\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 623, __PRETTY_FUNCTION__))
623 "Wrong types on cmp instruction")((I->getOperand(0)->getType() == I->getOperand(1)->
getType() && "Wrong types on cmp instruction") ? static_cast
<void> (0) : __assert_fail ("I->getOperand(0)->getType() == I->getOperand(1)->getType() && \"Wrong types on cmp instruction\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 623, __PRETTY_FUNCTION__))
;
624 assert((E->getOperand(0)->getType() == I->getOperand(0)->getType() &&(((E->getOperand(0)->getType() == I->getOperand(0)->
getType() && E->getOperand(1)->getType() == I->
getOperand(1)->getType())) ? static_cast<void> (0) :
__assert_fail ("(E->getOperand(0)->getType() == I->getOperand(0)->getType() && E->getOperand(1)->getType() == I->getOperand(1)->getType())"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 625, __PRETTY_FUNCTION__))
625 E->getOperand(1)->getType() == I->getOperand(1)->getType()))(((E->getOperand(0)->getType() == I->getOperand(0)->
getType() && E->getOperand(1)->getType() == I->
getOperand(1)->getType())) ? static_cast<void> (0) :
__assert_fail ("(E->getOperand(0)->getType() == I->getOperand(0)->getType() && E->getOperand(1)->getType() == I->getOperand(1)->getType())"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 625, __PRETTY_FUNCTION__))
;
626 Value *V = SimplifyCmpInst(Predicate, E->getOperand(0), E->getOperand(1),
627 DL, TLI, DT, AC);
628 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
629 return SimplifiedE;
630 } else if (isa<SelectInst>(I)) {
631 if (isa<Constant>(E->getOperand(0)) ||
632 E->getOperand(0) == E->getOperand(1)) {
633 assert(E->getOperand(1)->getType() == I->getOperand(1)->getType() &&((E->getOperand(1)->getType() == I->getOperand(1)->
getType() && E->getOperand(2)->getType() == I->
getOperand(2)->getType()) ? static_cast<void> (0) : __assert_fail
("E->getOperand(1)->getType() == I->getOperand(1)->getType() && E->getOperand(2)->getType() == I->getOperand(2)->getType()"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 634, __PRETTY_FUNCTION__))
634 E->getOperand(2)->getType() == I->getOperand(2)->getType())((E->getOperand(1)->getType() == I->getOperand(1)->
getType() && E->getOperand(2)->getType() == I->
getOperand(2)->getType()) ? static_cast<void> (0) : __assert_fail
("E->getOperand(1)->getType() == I->getOperand(1)->getType() && E->getOperand(2)->getType() == I->getOperand(2)->getType()"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 634, __PRETTY_FUNCTION__))
;
635 Value *V = SimplifySelectInst(E->getOperand(0), E->getOperand(1),
636 E->getOperand(2), DL, TLI, DT, AC);
637 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
638 return SimplifiedE;
639 }
640 } else if (I->isBinaryOp()) {
641 Value *V = SimplifyBinOp(E->getOpcode(), E->getOperand(0), E->getOperand(1),
642 DL, TLI, DT, AC);
643 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
644 return SimplifiedE;
645 } else if (auto *BI = dyn_cast<BitCastInst>(I)) {
646 Value *V = SimplifyInstruction(BI, DL, TLI, DT, AC);
647 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
648 return SimplifiedE;
649 } else if (isa<GetElementPtrInst>(I)) {
650 Value *V = SimplifyGEPInst(E->getType(),
651 ArrayRef<Value *>(E->op_begin(), E->op_end()),
652 DL, TLI, DT, AC);
653 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
654 return SimplifiedE;
655 } else if (AllConstant) {
656 // We don't bother trying to simplify unless all of the operands
657 // were constant.
658 // TODO: There are a lot of Simplify*'s we could call here, if we
659 // wanted to. The original motivating case for this code was a
660 // zext i1 false to i8, which we don't have an interface to
661 // simplify (IE there is no SimplifyZExt).
662
663 SmallVector<Constant *, 8> C;
664 for (Value *Arg : E->operands())
665 C.emplace_back(cast<Constant>(Arg));
666
667 if (Value *V = ConstantFoldInstOperands(I, C, DL, TLI))
668 if (const Expression *SimplifiedE = checkSimplificationResults(E, I, V))
669 return SimplifiedE;
670 }
671 return E;
672}
673
674const AggregateValueExpression *
675NewGVN::createAggregateValueExpression(Instruction *I) {
676 if (auto *II = dyn_cast<InsertValueInst>(I)) {
677 auto *E = new (ExpressionAllocator)
678 AggregateValueExpression(I->getNumOperands(), II->getNumIndices());
679 setBasicExpressionInfo(I, E);
680 E->allocateIntOperands(ExpressionAllocator);
681 std::copy(II->idx_begin(), II->idx_end(), int_op_inserter(E));
682 return E;
683 } else if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
684 auto *E = new (ExpressionAllocator)
685 AggregateValueExpression(I->getNumOperands(), EI->getNumIndices());
686 setBasicExpressionInfo(EI, E);
687 E->allocateIntOperands(ExpressionAllocator);
688 std::copy(EI->idx_begin(), EI->idx_end(), int_op_inserter(E));
689 return E;
690 }
691 llvm_unreachable("Unhandled type of aggregate value operation")::llvm::llvm_unreachable_internal("Unhandled type of aggregate value operation"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 691)
;
692}
693
694const VariableExpression *NewGVN::createVariableExpression(Value *V) {
695 auto *E = new (ExpressionAllocator) VariableExpression(V);
696 E->setOpcode(V->getValueID());
697 return E;
698}
699
700const Expression *NewGVN::createVariableOrConstant(Value *V) {
701 if (auto *C = dyn_cast<Constant>(V))
702 return createConstantExpression(C);
703 return createVariableExpression(V);
704}
705
706const ConstantExpression *NewGVN::createConstantExpression(Constant *C) {
707 auto *E = new (ExpressionAllocator) ConstantExpression(C);
708 E->setOpcode(C->getValueID());
709 return E;
710}
711
712const UnknownExpression *NewGVN::createUnknownExpression(Instruction *I) {
713 auto *E = new (ExpressionAllocator) UnknownExpression(I);
714 E->setOpcode(I->getOpcode());
715 return E;
716}
717
718const CallExpression *NewGVN::createCallExpression(CallInst *CI,
719 MemoryAccess *HV) {
720 // FIXME: Add operand bundles for calls.
721 auto *E =
722 new (ExpressionAllocator) CallExpression(CI->getNumOperands(), CI, HV);
723 setBasicExpressionInfo(CI, E);
724 return E;
725}
726
727// See if we have a congruence class and leader for this operand, and if so,
728// return it. Otherwise, return the operand itself.
729Value *NewGVN::lookupOperandLeader(Value *V) const {
730 CongruenceClass *CC = ValueToClass.lookup(V);
731 if (CC) {
732 // Everything in TOP is represneted by undef, as it can be any value.
733 // We do have to make sure we get the type right though, so we can't set the
734 // RepLeader to undef.
735 if (CC == TOPClass)
736 return UndefValue::get(V->getType());
737 return CC->RepStoredValue ? CC->RepStoredValue : CC->RepLeader;
738 }
739
740 return V;
741}
742
743MemoryAccess *NewGVN::lookupMemoryAccessEquiv(MemoryAccess *MA) const {
744 auto *CC = MemoryAccessToClass.lookup(MA);
745 if (CC && CC->RepMemoryAccess)
746 return CC->RepMemoryAccess;
747 // FIXME: We need to audit all the places that current set a nullptr To, and
748 // fix them. There should always be *some* congruence class, even if it is
749 // singular. Right now, we don't bother setting congruence classes for
750 // anything but stores, which means we have to return the original access
751 // here. Otherwise, this should be unreachable.
752 return MA;
753}
754
755// Return true if the MemoryAccess is really equivalent to everything. This is
756// equivalent to the lattice value "TOP" in most lattices. This is the initial
757// state of all memory accesses.
758bool NewGVN::isMemoryAccessTop(const MemoryAccess *MA) const {
759 return MemoryAccessToClass.lookup(MA) == TOPClass;
760}
761
762LoadExpression *NewGVN::createLoadExpression(Type *LoadType, Value *PointerOp,
763 LoadInst *LI, MemoryAccess *DA) {
764 auto *E = new (ExpressionAllocator) LoadExpression(1, LI, DA);
765 E->allocateOperands(ArgRecycler, ExpressionAllocator);
766 E->setType(LoadType);
767
768 // Give store and loads same opcode so they value number together.
769 E->setOpcode(0);
770 E->op_push_back(lookupOperandLeader(PointerOp));
771 if (LI)
772 E->setAlignment(LI->getAlignment());
773
774 // TODO: Value number heap versions. We may be able to discover
775 // things alias analysis can't on it's own (IE that a store and a
776 // load have the same value, and thus, it isn't clobbering the load).
777 return E;
778}
779
780const StoreExpression *NewGVN::createStoreExpression(StoreInst *SI,
781 MemoryAccess *DA) {
782 auto *StoredValueLeader = lookupOperandLeader(SI->getValueOperand());
783 auto *E = new (ExpressionAllocator)
784 StoreExpression(SI->getNumOperands(), SI, StoredValueLeader, DA);
785 E->allocateOperands(ArgRecycler, ExpressionAllocator);
786 E->setType(SI->getValueOperand()->getType());
787
788 // Give store and loads same opcode so they value number together.
789 E->setOpcode(0);
790 E->op_push_back(lookupOperandLeader(SI->getPointerOperand()));
791
792 // TODO: Value number heap versions. We may be able to discover
793 // things alias analysis can't on it's own (IE that a store and a
794 // load have the same value, and thus, it isn't clobbering the load).
795 return E;
796}
797
798const Expression *NewGVN::performSymbolicStoreEvaluation(Instruction *I) {
799 // Unlike loads, we never try to eliminate stores, so we do not check if they
800 // are simple and avoid value numbering them.
801 auto *SI = cast<StoreInst>(I);
802 MemoryAccess *StoreAccess = MSSA->getMemoryAccess(SI);
803 // Get the expression, if any, for the RHS of the MemoryDef.
804 MemoryAccess *StoreRHS = lookupMemoryAccessEquiv(
805 cast<MemoryDef>(StoreAccess)->getDefiningAccess());
806 // If we are defined by ourselves, use the live on entry def.
807 if (StoreRHS == StoreAccess)
808 StoreRHS = MSSA->getLiveOnEntryDef();
809
810 if (SI->isSimple()) {
811 // See if we are defined by a previous store expression, it already has a
812 // value, and it's the same value as our current store. FIXME: Right now, we
813 // only do this for simple stores, we should expand to cover memcpys, etc.
814 const Expression *OldStore = createStoreExpression(SI, StoreRHS);
815 CongruenceClass *CC = ExpressionToClass.lookup(OldStore);
816 // Basically, check if the congruence class the store is in is defined by a
817 // store that isn't us, and has the same value. MemorySSA takes care of
818 // ensuring the store has the same memory state as us already.
819 // The RepStoredValue gets nulled if all the stores disappear in a class, so
820 // we don't need to check if the class contains a store besides us.
821 if (CC && CC->RepStoredValue == lookupOperandLeader(SI->getValueOperand()))
822 return createStoreExpression(SI, StoreRHS);
823 // Also check if our value operand is defined by a load of the same memory
824 // location, and the memory state is the same as it was then
825 // (otherwise, it could have been overwritten later. See test32 in
826 // transforms/DeadStoreElimination/simple.ll)
827 if (LoadInst *LI = dyn_cast<LoadInst>(SI->getValueOperand())) {
828 if ((lookupOperandLeader(LI->getPointerOperand()) ==
829 lookupOperandLeader(SI->getPointerOperand())) &&
830 (lookupMemoryAccessEquiv(
831 MSSA->getMemoryAccess(LI)->getDefiningAccess()) == StoreRHS))
832 return createVariableExpression(LI);
833 }
834 }
835 return createStoreExpression(SI, StoreAccess);
836}
837
838const Expression *NewGVN::performSymbolicLoadEvaluation(Instruction *I) {
839 auto *LI = cast<LoadInst>(I);
840
841 // We can eliminate in favor of non-simple loads, but we won't be able to
842 // eliminate the loads themselves.
843 if (!LI->isSimple())
844 return nullptr;
845
846 Value *LoadAddressLeader = lookupOperandLeader(LI->getPointerOperand());
847 // Load of undef is undef.
848 if (isa<UndefValue>(LoadAddressLeader))
849 return createConstantExpression(UndefValue::get(LI->getType()));
850
851 MemoryAccess *DefiningAccess = MSSAWalker->getClobberingMemoryAccess(I);
852
853 if (!MSSA->isLiveOnEntryDef(DefiningAccess)) {
854 if (auto *MD = dyn_cast<MemoryDef>(DefiningAccess)) {
855 Instruction *DefiningInst = MD->getMemoryInst();
856 // If the defining instruction is not reachable, replace with undef.
857 if (!ReachableBlocks.count(DefiningInst->getParent()))
858 return createConstantExpression(UndefValue::get(LI->getType()));
859 }
860 }
861
862 const Expression *E =
863 createLoadExpression(LI->getType(), LI->getPointerOperand(), LI,
864 lookupMemoryAccessEquiv(DefiningAccess));
865 return E;
866}
867
868const Expression *
869NewGVN::performSymbolicPredicateInfoEvaluation(Instruction *I) {
870 auto *PI = PredInfo->getPredicateInfoFor(I);
871 if (!PI)
872 return nullptr;
873
874 DEBUG(dbgs() << "Found predicate info from instruction !\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Found predicate info from instruction !\n"
; } } while (false)
;
875
876 auto *PWC = dyn_cast<PredicateWithCondition>(PI);
877 if (!PWC)
878 return nullptr;
879
880 auto *CopyOf = I->getOperand(0);
881 auto *Cond = PWC->Condition;
882
883 // If this a copy of the condition, it must be either true or false depending
884 // on the predicate info type and edge
885 if (CopyOf == Cond) {
886 // We should not need to add predicate users because the predicate info is
887 // already a use of this operand.
888 if (isa<PredicateAssume>(PI))
889 return createConstantExpression(ConstantInt::getTrue(Cond->getType()));
890 if (auto *PBranch = dyn_cast<PredicateBranch>(PI)) {
891 if (PBranch->TrueEdge)
892 return createConstantExpression(ConstantInt::getTrue(Cond->getType()));
893 return createConstantExpression(ConstantInt::getFalse(Cond->getType()));
894 }
895 if (auto *PSwitch = dyn_cast<PredicateSwitch>(PI))
896 return createConstantExpression(cast<Constant>(PSwitch->CaseValue));
897 }
898
899 // Not a copy of the condition, so see what the predicates tell us about this
900 // value. First, though, we check to make sure the value is actually a copy
901 // of one of the condition operands. It's possible, in certain cases, for it
902 // to be a copy of a predicateinfo copy. In particular, if two branch
903 // operations use the same condition, and one branch dominates the other, we
904 // will end up with a copy of a copy. This is currently a small deficiency in
905 // predicateinfo. What will end up happening here is that we will value
906 // number both copies the same anyway.
907
908 // Everything below relies on the condition being a comparison.
909 auto *Cmp = dyn_cast<CmpInst>(Cond);
910 if (!Cmp)
911 return nullptr;
912
913 if (CopyOf != Cmp->getOperand(0) && CopyOf != Cmp->getOperand(1)) {
914 DEBUG(dbgs() << "Copy is not of any condition operands!")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Copy is not of any condition operands!"
; } } while (false)
;
915 return nullptr;
916 }
917 Value *FirstOp = lookupOperandLeader(Cmp->getOperand(0));
918 Value *SecondOp = lookupOperandLeader(Cmp->getOperand(1));
919 bool SwappedOps = false;
920 // Sort the ops
921 if (shouldSwapOperands(FirstOp, SecondOp)) {
922 std::swap(FirstOp, SecondOp);
923 SwappedOps = true;
924 }
925 CmpInst::Predicate Predicate =
926 SwappedOps ? Cmp->getSwappedPredicate() : Cmp->getPredicate();
927
928 if (isa<PredicateAssume>(PI)) {
929 // If the comparison is true when the operands are equal, then we know the
930 // operands are equal, because assumes must always be true.
931 if (CmpInst::isTrueWhenEqual(Predicate)) {
932 addPredicateUsers(PI, I);
933 return createVariableOrConstant(FirstOp);
934 }
935 }
936 if (const auto *PBranch = dyn_cast<PredicateBranch>(PI)) {
937 // If we are *not* a copy of the comparison, we may equal to the other
938 // operand when the predicate implies something about equality of
939 // operations. In particular, if the comparison is true/false when the
940 // operands are equal, and we are on the right edge, we know this operation
941 // is equal to something.
942 if ((PBranch->TrueEdge && Predicate == CmpInst::ICMP_EQ) ||
943 (!PBranch->TrueEdge && Predicate == CmpInst::ICMP_NE)) {
944 addPredicateUsers(PI, I);
945 return createVariableOrConstant(FirstOp);
946 }
947 // Handle the special case of floating point.
948 if (((PBranch->TrueEdge && Predicate == CmpInst::FCMP_OEQ) ||
949 (!PBranch->TrueEdge && Predicate == CmpInst::FCMP_UNE)) &&
950 isa<ConstantFP>(FirstOp) && !cast<ConstantFP>(FirstOp)->isZero()) {
951 addPredicateUsers(PI, I);
952 return createConstantExpression(cast<Constant>(FirstOp));
953 }
954 }
955 return nullptr;
956}
957
958// Evaluate read only and pure calls, and create an expression result.
959const Expression *NewGVN::performSymbolicCallEvaluation(Instruction *I) {
960 auto *CI = cast<CallInst>(I);
961 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
962 // Instrinsics with the returned attribute are copies of arguments.
963 if (auto *ReturnedValue = II->getReturnedArgOperand()) {
964 if (II->getIntrinsicID() == Intrinsic::ssa_copy)
965 if (const auto *Result = performSymbolicPredicateInfoEvaluation(I))
966 return Result;
967 return createVariableOrConstant(ReturnedValue);
968 }
969 }
970 if (AA->doesNotAccessMemory(CI)) {
971 return createCallExpression(CI, nullptr);
972 } else if (AA->onlyReadsMemory(CI)) {
973 MemoryAccess *DefiningAccess = MSSAWalker->getClobberingMemoryAccess(CI);
974 return createCallExpression(CI, lookupMemoryAccessEquiv(DefiningAccess));
975 }
976 return nullptr;
977}
978
979// Update the memory access equivalence table to say that From is equal to To,
980// and return true if this is different from what already existed in the table.
981// FIXME: We need to audit all the places that current set a nullptr To, and fix
982// them. There should always be *some* congruence class, even if it is singular.
983bool NewGVN::setMemoryAccessEquivTo(MemoryAccess *From, CongruenceClass *To) {
984 DEBUG(dbgs() << "Setting " << *From)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Setting " << *From; } } while
(false)
;
985 if (To) {
986 DEBUG(dbgs() << " equivalent to congruence class ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << " equivalent to congruence class "
; } } while (false)
;
987 DEBUG(dbgs() << To->ID << " with current memory access leader ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << To->ID << " with current memory access leader "
; } } while (false)
;
988 DEBUG(dbgs() << *To->RepMemoryAccess)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << *To->RepMemoryAccess; } } while
(false)
;
989 } else {
990 DEBUG(dbgs() << " equivalent to itself")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << " equivalent to itself"; } } while
(false)
;
991 }
992 DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "\n"; } } while (false)
;
993
994 auto LookupResult = MemoryAccessToClass.find(From);
995 bool Changed = false;
996 // If it's already in the table, see if the value changed.
997 if (LookupResult != MemoryAccessToClass.end()) {
998 if (To && LookupResult->second != To) {
999 // It wasn't equivalent before, and now it is.
1000 LookupResult->second = To;
1001 Changed = true;
1002 } else if (!To) {
1003 // It used to be equivalent to something, and now it's not.
1004 MemoryAccessToClass.erase(LookupResult);
1005 Changed = true;
1006 }
1007 } else {
1008 assert(!To &&((!To && "Memory equivalence should never change from nothing to something"
) ? static_cast<void> (0) : __assert_fail ("!To && \"Memory equivalence should never change from nothing to something\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1009, __PRETTY_FUNCTION__))
1009 "Memory equivalence should never change from nothing to something")((!To && "Memory equivalence should never change from nothing to something"
) ? static_cast<void> (0) : __assert_fail ("!To && \"Memory equivalence should never change from nothing to something\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1009, __PRETTY_FUNCTION__))
;
1010 }
1011
1012 return Changed;
1013}
1014// Evaluate PHI nodes symbolically, and create an expression result.
1015const Expression *NewGVN::performSymbolicPHIEvaluation(Instruction *I) {
1016 auto *E = cast<PHIExpression>(createPHIExpression(I));
1017 // We match the semantics of SimplifyPhiNode from InstructionSimplify here.
1018
1019 // See if all arguaments are the same.
1020 // We track if any were undef because they need special handling.
1021 bool HasUndef = false;
1022 auto Filtered = make_filter_range(E->operands(), [&](const Value *Arg) {
1023 if (Arg == I)
1024 return false;
1025 if (isa<UndefValue>(Arg)) {
1026 HasUndef = true;
1027 return false;
1028 }
1029 return true;
1030 });
1031 // If we are left with no operands, it's undef
1032 if (Filtered.begin() == Filtered.end()) {
1033 DEBUG(dbgs() << "Simplified PHI node " << *I << " to undef"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Simplified PHI node " <<
*I << " to undef" << "\n"; } } while (false)
1034 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Simplified PHI node " <<
*I << " to undef" << "\n"; } } while (false)
;
1035 E->deallocateOperands(ArgRecycler);
1036 ExpressionAllocator.Deallocate(E);
1037 return createConstantExpression(UndefValue::get(I->getType()));
1038 }
1039 Value *AllSameValue = *(Filtered.begin());
1040 ++Filtered.begin();
1041 // Can't use std::equal here, sadly, because filter.begin moves.
1042 if (llvm::all_of(Filtered, [AllSameValue](const Value *V) {
1043 return V == AllSameValue;
1044 })) {
1045 // In LLVM's non-standard representation of phi nodes, it's possible to have
1046 // phi nodes with cycles (IE dependent on other phis that are .... dependent
1047 // on the original phi node), especially in weird CFG's where some arguments
1048 // are unreachable, or uninitialized along certain paths. This can cause
1049 // infinite loops during evaluation. We work around this by not trying to
1050 // really evaluate them independently, but instead using a variable
1051 // expression to say if one is equivalent to the other.
1052 // We also special case undef, so that if we have an undef, we can't use the
1053 // common value unless it dominates the phi block.
1054 if (HasUndef) {
1055 // Only have to check for instructions
1056 if (auto *AllSameInst = dyn_cast<Instruction>(AllSameValue))
1057 if (!DT->dominates(AllSameInst, I))
1058 return E;
1059 }
1060
1061 NumGVNPhisAllSame++;
1062 DEBUG(dbgs() << "Simplified PHI node " << *I << " to " << *AllSameValuedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Simplified PHI node " <<
*I << " to " << *AllSameValue << "\n"; } }
while (false)
1063 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Simplified PHI node " <<
*I << " to " << *AllSameValue << "\n"; } }
while (false)
;
1064 E->deallocateOperands(ArgRecycler);
1065 ExpressionAllocator.Deallocate(E);
1066 return createVariableOrConstant(AllSameValue);
1067 }
1068 return E;
1069}
1070
1071const Expression *NewGVN::performSymbolicAggrValueEvaluation(Instruction *I) {
1072 if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
1073 auto *II = dyn_cast<IntrinsicInst>(EI->getAggregateOperand());
1074 if (II && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) {
1075 unsigned Opcode = 0;
1076 // EI might be an extract from one of our recognised intrinsics. If it
1077 // is we'll synthesize a semantically equivalent expression instead on
1078 // an extract value expression.
1079 switch (II->getIntrinsicID()) {
1080 case Intrinsic::sadd_with_overflow:
1081 case Intrinsic::uadd_with_overflow:
1082 Opcode = Instruction::Add;
1083 break;
1084 case Intrinsic::ssub_with_overflow:
1085 case Intrinsic::usub_with_overflow:
1086 Opcode = Instruction::Sub;
1087 break;
1088 case Intrinsic::smul_with_overflow:
1089 case Intrinsic::umul_with_overflow:
1090 Opcode = Instruction::Mul;
1091 break;
1092 default:
1093 break;
1094 }
1095
1096 if (Opcode != 0) {
1097 // Intrinsic recognized. Grab its args to finish building the
1098 // expression.
1099 assert(II->getNumArgOperands() == 2 &&((II->getNumArgOperands() == 2 && "Expect two args for recognised intrinsics."
) ? static_cast<void> (0) : __assert_fail ("II->getNumArgOperands() == 2 && \"Expect two args for recognised intrinsics.\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1100, __PRETTY_FUNCTION__))
1100 "Expect two args for recognised intrinsics.")((II->getNumArgOperands() == 2 && "Expect two args for recognised intrinsics."
) ? static_cast<void> (0) : __assert_fail ("II->getNumArgOperands() == 2 && \"Expect two args for recognised intrinsics.\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1100, __PRETTY_FUNCTION__))
;
1101 return createBinaryExpression(
1102 Opcode, EI->getType(), II->getArgOperand(0), II->getArgOperand(1));
1103 }
1104 }
1105 }
1106
1107 return createAggregateValueExpression(I);
1108}
1109const Expression *NewGVN::performSymbolicCmpEvaluation(Instruction *I) {
1110 auto *CI = dyn_cast<CmpInst>(I);
1111 // See if our operands are equal to those of a previous predicate, and if so,
1112 // if it implies true or false.
1113 auto Op0 = lookupOperandLeader(CI->getOperand(0));
1114 auto Op1 = lookupOperandLeader(CI->getOperand(1));
1115 auto OurPredicate = CI->getPredicate();
1116 if (shouldSwapOperands(Op0, Op1)) {
1117 std::swap(Op0, Op1);
1118 OurPredicate = CI->getSwappedPredicate();
1119 }
1120
1121 // Avoid processing the same info twice
1122 const PredicateBase *LastPredInfo = nullptr;
1123 // See if we know something about the comparison itself, like it is the target
1124 // of an assume.
1125 auto *CmpPI = PredInfo->getPredicateInfoFor(I);
1126 if (dyn_cast_or_null<PredicateAssume>(CmpPI))
1127 return createConstantExpression(ConstantInt::getTrue(CI->getType()));
1128
1129 if (Op0 == Op1) {
1130 // This condition does not depend on predicates, no need to add users
1131 if (CI->isTrueWhenEqual())
1132 return createConstantExpression(ConstantInt::getTrue(CI->getType()));
1133 else if (CI->isFalseWhenEqual())
1134 return createConstantExpression(ConstantInt::getFalse(CI->getType()));
1135 }
1136
1137 // NOTE: Because we are comparing both operands here and below, and using
1138 // previous comparisons, we rely on fact that predicateinfo knows to mark
1139 // comparisons that use renamed operands as users of the earlier comparisons.
1140 // It is *not* enough to just mark predicateinfo renamed operands as users of
1141 // the earlier comparisons, because the *other* operand may have changed in a
1142 // previous iteration.
1143 // Example:
1144 // icmp slt %a, %b
1145 // %b.0 = ssa.copy(%b)
1146 // false branch:
1147 // icmp slt %c, %b.0
1148
1149 // %c and %a may start out equal, and thus, the code below will say the second
1150 // %icmp is false. c may become equal to something else, and in that case the
1151 // %second icmp *must* be reexamined, but would not if only the renamed
1152 // %operands are considered users of the icmp.
1153
1154 // *Currently* we only check one level of comparisons back, and only mark one
1155 // level back as touched when changes appen . If you modify this code to look
1156 // back farther through comparisons, you *must* mark the appropriate
1157 // comparisons as users in PredicateInfo.cpp, or you will cause bugs. See if
1158 // we know something just from the operands themselves
1159
1160 // See if our operands have predicate info, so that we may be able to derive
1161 // something from a previous comparison.
1162 for (const auto &Op : CI->operands()) {
1163 auto *PI = PredInfo->getPredicateInfoFor(Op);
1164 if (const auto *PBranch = dyn_cast_or_null<PredicateBranch>(PI)) {
1165 if (PI == LastPredInfo)
1166 continue;
1167 LastPredInfo = PI;
1168
1169 // TODO: Along the false edge, we may know more things too, like icmp of
1170 // same operands is false.
1171 // TODO: We only handle actual comparison conditions below, not and/or.
1172 auto *BranchCond = dyn_cast<CmpInst>(PBranch->Condition);
1173 if (!BranchCond)
1174 continue;
1175 auto *BranchOp0 = lookupOperandLeader(BranchCond->getOperand(0));
1176 auto *BranchOp1 = lookupOperandLeader(BranchCond->getOperand(1));
1177 auto BranchPredicate = BranchCond->getPredicate();
1178 if (shouldSwapOperands(BranchOp0, BranchOp1)) {
1179 std::swap(BranchOp0, BranchOp1);
1180 BranchPredicate = BranchCond->getSwappedPredicate();
1181 }
1182 if (BranchOp0 == Op0 && BranchOp1 == Op1) {
1183 if (PBranch->TrueEdge) {
1184 // If we know the previous predicate is true and we are in the true
1185 // edge then we may be implied true or false.
1186 if (CmpInst::isImpliedTrueByMatchingCmp(OurPredicate,
1187 BranchPredicate)) {
1188 addPredicateUsers(PI, I);
1189 return createConstantExpression(
1190 ConstantInt::getTrue(CI->getType()));
1191 }
1192
1193 if (CmpInst::isImpliedFalseByMatchingCmp(OurPredicate,
1194 BranchPredicate)) {
1195 addPredicateUsers(PI, I);
1196 return createConstantExpression(
1197 ConstantInt::getFalse(CI->getType()));
1198 }
1199
1200 } else {
1201 // Just handle the ne and eq cases, where if we have the same
1202 // operands, we may know something.
1203 if (BranchPredicate == OurPredicate) {
1204 addPredicateUsers(PI, I);
1205 // Same predicate, same ops,we know it was false, so this is false.
1206 return createConstantExpression(
1207 ConstantInt::getFalse(CI->getType()));
1208 } else if (BranchPredicate ==
1209 CmpInst::getInversePredicate(OurPredicate)) {
1210 addPredicateUsers(PI, I);
1211 // Inverse predicate, we know the other was false, so this is true.
1212 // FIXME: Double check this
1213 return createConstantExpression(
1214 ConstantInt::getTrue(CI->getType()));
1215 }
1216 }
1217 }
1218 }
1219 }
1220 // Create expression will take care of simplifyCmpInst
1221 return createExpression(I);
1222}
1223
1224// Substitute and symbolize the value before value numbering.
1225const Expression *NewGVN::performSymbolicEvaluation(Value *V) {
1226 const Expression *E = nullptr;
1227 if (auto *C = dyn_cast<Constant>(V))
1228 E = createConstantExpression(C);
1229 else if (isa<Argument>(V) || isa<GlobalVariable>(V)) {
1230 E = createVariableExpression(V);
1231 } else {
1232 // TODO: memory intrinsics.
1233 // TODO: Some day, we should do the forward propagation and reassociation
1234 // parts of the algorithm.
1235 auto *I = cast<Instruction>(V);
1236 switch (I->getOpcode()) {
1237 case Instruction::ExtractValue:
1238 case Instruction::InsertValue:
1239 E = performSymbolicAggrValueEvaluation(I);
1240 break;
1241 case Instruction::PHI:
1242 E = performSymbolicPHIEvaluation(I);
1243 break;
1244 case Instruction::Call:
1245 E = performSymbolicCallEvaluation(I);
1246 break;
1247 case Instruction::Store:
1248 E = performSymbolicStoreEvaluation(I);
1249 break;
1250 case Instruction::Load:
1251 E = performSymbolicLoadEvaluation(I);
1252 break;
1253 case Instruction::BitCast: {
1254 E = createExpression(I);
1255 } break;
1256 case Instruction::ICmp:
1257 case Instruction::FCmp: {
1258 E = performSymbolicCmpEvaluation(I);
1259 } break;
1260 case Instruction::Add:
1261 case Instruction::FAdd:
1262 case Instruction::Sub:
1263 case Instruction::FSub:
1264 case Instruction::Mul:
1265 case Instruction::FMul:
1266 case Instruction::UDiv:
1267 case Instruction::SDiv:
1268 case Instruction::FDiv:
1269 case Instruction::URem:
1270 case Instruction::SRem:
1271 case Instruction::FRem:
1272 case Instruction::Shl:
1273 case Instruction::LShr:
1274 case Instruction::AShr:
1275 case Instruction::And:
1276 case Instruction::Or:
1277 case Instruction::Xor:
1278 case Instruction::Trunc:
1279 case Instruction::ZExt:
1280 case Instruction::SExt:
1281 case Instruction::FPToUI:
1282 case Instruction::FPToSI:
1283 case Instruction::UIToFP:
1284 case Instruction::SIToFP:
1285 case Instruction::FPTrunc:
1286 case Instruction::FPExt:
1287 case Instruction::PtrToInt:
1288 case Instruction::IntToPtr:
1289 case Instruction::Select:
1290 case Instruction::ExtractElement:
1291 case Instruction::InsertElement:
1292 case Instruction::ShuffleVector:
1293 case Instruction::GetElementPtr:
1294 E = createExpression(I);
1295 break;
1296 default:
1297 return nullptr;
1298 }
1299 }
1300 return E;
1301}
1302
1303void NewGVN::markUsersTouched(Value *V) {
1304 // Now mark the users as touched.
1305 for (auto *User : V->users()) {
1306 assert(isa<Instruction>(User) && "Use of value not within an instruction?")((isa<Instruction>(User) && "Use of value not within an instruction?"
) ? static_cast<void> (0) : __assert_fail ("isa<Instruction>(User) && \"Use of value not within an instruction?\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1306, __PRETTY_FUNCTION__))
;
1307 TouchedInstructions.set(InstrDFS.lookup(User));
1308 }
1309}
1310
1311void NewGVN::markMemoryUsersTouched(MemoryAccess *MA) {
1312 for (auto U : MA->users()) {
1313 if (auto *MUD = dyn_cast<MemoryUseOrDef>(U))
1314 TouchedInstructions.set(InstrDFS.lookup(MUD->getMemoryInst()));
1315 else
1316 TouchedInstructions.set(InstrDFS.lookup(U));
1317 }
1318}
1319
1320// Add I to the set of users of a given predicate.
1321void NewGVN::addPredicateUsers(const PredicateBase *PB, Instruction *I) {
1322 if (auto *PBranch = dyn_cast<PredicateBranch>(PB))
1323 PredicateToUsers[PBranch->Condition].insert(I);
1324 else if (auto *PAssume = dyn_cast<PredicateBranch>(PB))
1325 PredicateToUsers[PAssume->Condition].insert(I);
1326}
1327
1328// Touch all the predicates that depend on this instruction.
1329void NewGVN::markPredicateUsersTouched(Instruction *I) {
1330 const auto Result = PredicateToUsers.find(I);
1331 if (Result != PredicateToUsers.end()) {
1332 for (auto *User : Result->second)
1333 TouchedInstructions.set(InstrDFS.lookup(User));
1334 PredicateToUsers.erase(Result);
1335 }
1336}
1337
1338// Touch the instructions that need to be updated after a congruence class has a
1339// leader change, and mark changed values.
1340void NewGVN::markLeaderChangeTouched(CongruenceClass *CC) {
1341 for (auto M : CC->Members) {
1342 if (auto *I = dyn_cast<Instruction>(M))
1343 TouchedInstructions.set(InstrDFS.lookup(I));
1344 LeaderChanges.insert(M);
1345 }
1346}
1347
1348// Move a value, currently in OldClass, to be part of NewClass
1349// Update OldClass for the move (including changing leaders, etc)
1350void NewGVN::moveValueToNewCongruenceClass(Instruction *I,
1351 CongruenceClass *OldClass,
1352 CongruenceClass *NewClass) {
1353 DEBUG(dbgs() << "New congruence class for " << I << " is " << NewClass->IDdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "New congruence class for " <<
I << " is " << NewClass->ID << "\n"; } }
while (false)
1354 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "New congruence class for " <<
I << " is " << NewClass->ID << "\n"; } }
while (false)
;
1355
1356 if (I == OldClass->NextLeader.first)
1357 OldClass->NextLeader = {nullptr, ~0U};
1358
1359 // It's possible, though unlikely, for us to discover equivalences such
1360 // that the current leader does not dominate the old one.
1361 // This statistic tracks how often this happens.
1362 // We assert on phi nodes when this happens, currently, for debugging, because
1363 // we want to make sure we name phi node cycles properly.
1364 if (isa<Instruction>(NewClass->RepLeader) && NewClass->RepLeader &&
1365 I != NewClass->RepLeader &&
1366 DT->properlyDominates(
1367 I->getParent(),
1368 cast<Instruction>(NewClass->RepLeader)->getParent())) {
1369 ++NumGVNNotMostDominatingLeader;
1370 assert(!isa<PHINode>(I) &&((!isa<PHINode>(I) && "New class for instruction should not be dominated by instruction"
) ? static_cast<void> (0) : __assert_fail ("!isa<PHINode>(I) && \"New class for instruction should not be dominated by instruction\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1371, __PRETTY_FUNCTION__))
1371 "New class for instruction should not be dominated by instruction")((!isa<PHINode>(I) && "New class for instruction should not be dominated by instruction"
) ? static_cast<void> (0) : __assert_fail ("!isa<PHINode>(I) && \"New class for instruction should not be dominated by instruction\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1371, __PRETTY_FUNCTION__))
;
1372 }
1373
1374 if (NewClass->RepLeader != I) {
1375 auto DFSNum = InstrDFS.lookup(I);
1376 if (DFSNum < NewClass->NextLeader.second)
1377 NewClass->NextLeader = {I, DFSNum};
1378 }
1379
1380 OldClass->Members.erase(I);
1381 NewClass->Members.insert(I);
1382 MemoryAccess *StoreAccess = nullptr;
1383 if (auto *SI = dyn_cast<StoreInst>(I)) {
1384 StoreAccess = MSSA->getMemoryAccess(SI);
1385 --OldClass->StoreCount;
1386 assert(OldClass->StoreCount >= 0)((OldClass->StoreCount >= 0) ? static_cast<void> (
0) : __assert_fail ("OldClass->StoreCount >= 0", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1386, __PRETTY_FUNCTION__))
;
1387 ++NewClass->StoreCount;
1388 assert(NewClass->StoreCount > 0)((NewClass->StoreCount > 0) ? static_cast<void> (
0) : __assert_fail ("NewClass->StoreCount > 0", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1388, __PRETTY_FUNCTION__))
;
1389 if (!NewClass->RepMemoryAccess) {
1390 // If we don't have a representative memory access, it better be the only
1391 // store in there.
1392 assert(NewClass->StoreCount == 1)((NewClass->StoreCount == 1) ? static_cast<void> (0)
: __assert_fail ("NewClass->StoreCount == 1", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1392, __PRETTY_FUNCTION__))
;
1393 NewClass->RepMemoryAccess = StoreAccess;
1394 }
1395 setMemoryAccessEquivTo(StoreAccess, NewClass);
1396 }
1397
1398 ValueToClass[I] = NewClass;
1399 // See if we destroyed the class or need to swap leaders.
1400 if (OldClass->Members.empty() && OldClass != TOPClass) {
1401 if (OldClass->DefiningExpr) {
1402 OldClass->Dead = true;
1403 DEBUG(dbgs() << "Erasing expression " << OldClass->DefiningExprdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Erasing expression " << OldClass
->DefiningExpr << " from table\n"; } } while (false)
1404 << " from table\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Erasing expression " << OldClass
->DefiningExpr << " from table\n"; } } while (false)
;
1405 ExpressionToClass.erase(OldClass->DefiningExpr);
1406 }
1407 } else if (OldClass->RepLeader == I) {
1408 // When the leader changes, the value numbering of
1409 // everything may change due to symbolization changes, so we need to
1410 // reprocess.
1411 DEBUG(dbgs() << "Leader change!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Leader change!\n"; } } while (
false)
;
1412 ++NumGVNLeaderChanges;
1413 // Destroy the stored value if there are no more stores to represent it.
1414 if (OldClass->StoreCount == 0) {
1415 if (OldClass->RepStoredValue != nullptr)
1416 OldClass->RepStoredValue = nullptr;
1417 if (OldClass->RepMemoryAccess != nullptr)
1418 OldClass->RepMemoryAccess = nullptr;
1419 }
1420
1421 // If we destroy the old access leader, we have to effectively destroy the
1422 // congruence class. When it comes to scalars, anything with the same value
1423 // is as good as any other. That means that one leader is as good as
1424 // another, and as long as you have some leader for the value, you are
1425 // good.. When it comes to *memory states*, only one particular thing really
1426 // represents the definition of a given memory state. Once it goes away, we
1427 // need to re-evaluate which pieces of memory are really still
1428 // equivalent. The best way to do this is to re-value number things. The
1429 // only way to really make that happen is to destroy the rest of the class.
1430 // In order to effectively destroy the class, we reset ExpressionToClass for
1431 // each by using the ValueToExpression mapping. The members later get
1432 // marked as touched due to the leader change. We will create new
1433 // congruence classes, and the pieces that are still equivalent will end
1434 // back together in a new class. If this becomes too expensive, it is
1435 // possible to use a versioning scheme for the congruence classes to avoid
1436 // the expressions finding this old class.
1437 if (OldClass->StoreCount > 0 && OldClass->RepMemoryAccess == StoreAccess) {
1438 DEBUG(dbgs() << "Kicking everything out of class " << OldClass->IDdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Kicking everything out of class "
<< OldClass->ID << " because memory access leader changed"
; } } while (false)
1439 << " because memory access leader changed")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Kicking everything out of class "
<< OldClass->ID << " because memory access leader changed"
; } } while (false)
;
1440 for (auto Member : OldClass->Members)
1441 ExpressionToClass.erase(ValueToExpression.lookup(Member));
1442 }
1443
1444 // We don't need to sort members if there is only 1, and we don't care about
1445 // sorting the TOP class because everything either gets out of it or is
1446 // unreachable.
1447 if (OldClass->Members.size() == 1 || OldClass == TOPClass) {
1448 OldClass->RepLeader = *(OldClass->Members.begin());
1449 } else if (OldClass->NextLeader.first) {
1450 ++NumGVNAvoidedSortedLeaderChanges;
1451 OldClass->RepLeader = OldClass->NextLeader.first;
1452 OldClass->NextLeader = {nullptr, ~0U};
1453 } else {
1454 ++NumGVNSortedLeaderChanges;
1455 // TODO: If this ends up to slow, we can maintain a dual structure for
1456 // member testing/insertion, or keep things mostly sorted, and sort only
1457 // here, or ....
1458 std::pair<Value *, unsigned> MinDFS = {nullptr, ~0U};
1459 for (const auto X : OldClass->Members) {
1460 auto DFSNum = InstrDFS.lookup(X);
1461 if (DFSNum < MinDFS.second)
1462 MinDFS = {X, DFSNum};
1463 }
1464 OldClass->RepLeader = MinDFS.first;
1465 }
1466 markLeaderChangeTouched(OldClass);
1467 }
1468}
1469
1470// Perform congruence finding on a given value numbering expression.
1471void NewGVN::performCongruenceFinding(Instruction *I, const Expression *E) {
1472 ValueToExpression[I] = E;
1473 // This is guaranteed to return something, since it will at least find
1474 // TOP.
1475
1476 CongruenceClass *IClass = ValueToClass[I];
1477 assert(IClass && "Should have found a IClass")((IClass && "Should have found a IClass") ? static_cast
<void> (0) : __assert_fail ("IClass && \"Should have found a IClass\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1477, __PRETTY_FUNCTION__))
;
1478 // Dead classes should have been eliminated from the mapping.
1479 assert(!IClass->Dead && "Found a dead class")((!IClass->Dead && "Found a dead class") ? static_cast
<void> (0) : __assert_fail ("!IClass->Dead && \"Found a dead class\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1479, __PRETTY_FUNCTION__))
;
1480
1481 CongruenceClass *EClass;
1482 if (const auto *VE = dyn_cast<VariableExpression>(E)) {
1483 EClass = ValueToClass[VE->getVariableValue()];
1484 } else {
1485 auto lookupResult = ExpressionToClass.insert({E, nullptr});
1486
1487 // If it's not in the value table, create a new congruence class.
1488 if (lookupResult.second) {
1489 CongruenceClass *NewClass = createCongruenceClass(nullptr, E);
1490 auto place = lookupResult.first;
1491 place->second = NewClass;
1492
1493 // Constants and variables should always be made the leader.
1494 if (const auto *CE = dyn_cast<ConstantExpression>(E)) {
1495 NewClass->RepLeader = CE->getConstantValue();
1496 } else if (const auto *SE = dyn_cast<StoreExpression>(E)) {
1497 StoreInst *SI = SE->getStoreInst();
1498 NewClass->RepLeader = SI;
1499 NewClass->RepStoredValue = lookupOperandLeader(SI->getValueOperand());
1500 // The RepMemoryAccess field will be filled in properly by the
1501 // moveValueToNewCongruenceClass call.
1502 } else {
1503 NewClass->RepLeader = I;
1504 }
1505 assert(!isa<VariableExpression>(E) &&((!isa<VariableExpression>(E) && "VariableExpression should have been handled already"
) ? static_cast<void> (0) : __assert_fail ("!isa<VariableExpression>(E) && \"VariableExpression should have been handled already\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1506, __PRETTY_FUNCTION__))
1506 "VariableExpression should have been handled already")((!isa<VariableExpression>(E) && "VariableExpression should have been handled already"
) ? static_cast<void> (0) : __assert_fail ("!isa<VariableExpression>(E) && \"VariableExpression should have been handled already\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1506, __PRETTY_FUNCTION__))
;
1507
1508 EClass = NewClass;
1509 DEBUG(dbgs() << "Created new congruence class for " << *Ido { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Created new congruence class for "
<< *I << " using expression " << *E <<
" at " << NewClass->ID << " and leader " <<
*(NewClass->RepLeader); } } while (false)
1510 << " using expression " << *E << " at " << NewClass->IDdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Created new congruence class for "
<< *I << " using expression " << *E <<
" at " << NewClass->ID << " and leader " <<
*(NewClass->RepLeader); } } while (false)
1511 << " and leader " << *(NewClass->RepLeader))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Created new congruence class for "
<< *I << " using expression " << *E <<
" at " << NewClass->ID << " and leader " <<
*(NewClass->RepLeader); } } while (false)
;
1512 if (NewClass->RepStoredValue)
1513 DEBUG(dbgs() << " and stored value " << *(NewClass->RepStoredValue))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << " and stored value " << *
(NewClass->RepStoredValue); } } while (false)
;
1514 DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "\n"; } } while (false)
;
1515 DEBUG(dbgs() << "Hash value was " << E->getHashValue() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Hash value was " << E->
getHashValue() << "\n"; } } while (false)
;
1516 } else {
1517 EClass = lookupResult.first->second;
1518 if (isa<ConstantExpression>(E))
1519 assert(isa<Constant>(EClass->RepLeader) &&((isa<Constant>(EClass->RepLeader) && "Any class with a constant expression should have a "
"constant leader") ? static_cast<void> (0) : __assert_fail
("isa<Constant>(EClass->RepLeader) && \"Any class with a constant expression should have a \" \"constant leader\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1521, __PRETTY_FUNCTION__))
1520 "Any class with a constant expression should have a "((isa<Constant>(EClass->RepLeader) && "Any class with a constant expression should have a "
"constant leader") ? static_cast<void> (0) : __assert_fail
("isa<Constant>(EClass->RepLeader) && \"Any class with a constant expression should have a \" \"constant leader\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1521, __PRETTY_FUNCTION__))
1521 "constant leader")((isa<Constant>(EClass->RepLeader) && "Any class with a constant expression should have a "
"constant leader") ? static_cast<void> (0) : __assert_fail
("isa<Constant>(EClass->RepLeader) && \"Any class with a constant expression should have a \" \"constant leader\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1521, __PRETTY_FUNCTION__))
;
1522
1523 assert(EClass && "Somehow don't have an eclass")((EClass && "Somehow don't have an eclass") ? static_cast
<void> (0) : __assert_fail ("EClass && \"Somehow don't have an eclass\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1523, __PRETTY_FUNCTION__))
;
1524
1525 assert(!EClass->Dead && "We accidentally looked up a dead class")((!EClass->Dead && "We accidentally looked up a dead class"
) ? static_cast<void> (0) : __assert_fail ("!EClass->Dead && \"We accidentally looked up a dead class\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1525, __PRETTY_FUNCTION__))
;
1526 }
1527 }
1528 bool ClassChanged = IClass != EClass;
1529 bool LeaderChanged = LeaderChanges.erase(I);
1530 if (ClassChanged || LeaderChanged) {
1531 DEBUG(dbgs() << "Found class " << EClass->ID << " for expression " << Edo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Found class " << EClass->
ID << " for expression " << E << "\n"; } } while
(false)
1532 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Found class " << EClass->
ID << " for expression " << E << "\n"; } } while
(false)
;
1533
1534 if (ClassChanged)
1535 moveValueToNewCongruenceClass(I, IClass, EClass);
1536 markUsersTouched(I);
1537 if (MemoryAccess *MA = MSSA->getMemoryAccess(I))
1538 markMemoryUsersTouched(MA);
1539 if (auto *CI = dyn_cast<CmpInst>(I))
1540 markPredicateUsersTouched(CI);
1541 }
1542}
1543
1544// Process the fact that Edge (from, to) is reachable, including marking
1545// any newly reachable blocks and instructions for processing.
1546void NewGVN::updateReachableEdge(BasicBlock *From, BasicBlock *To) {
1547 // Check if the Edge was reachable before.
1548 if (ReachableEdges.insert({From, To}).second) {
1549 // If this block wasn't reachable before, all instructions are touched.
1550 if (ReachableBlocks.insert(To).second) {
1551 DEBUG(dbgs() << "Block " << getBlockName(To) << " marked reachable\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Block " << getBlockName(
To) << " marked reachable\n"; } } while (false)
;
1552 const auto &InstRange = BlockInstRange.lookup(To);
1553 TouchedInstructions.set(InstRange.first, InstRange.second);
1554 } else {
1555 DEBUG(dbgs() << "Block " << getBlockName(To)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Block " << getBlockName(
To) << " was reachable, but new edge {" << getBlockName
(From) << "," << getBlockName(To) << "} to it found\n"
; } } while (false)
1556 << " was reachable, but new edge {" << getBlockName(From)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Block " << getBlockName(
To) << " was reachable, but new edge {" << getBlockName
(From) << "," << getBlockName(To) << "} to it found\n"
; } } while (false)
1557 << "," << getBlockName(To) << "} to it found\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Block " << getBlockName(
To) << " was reachable, but new edge {" << getBlockName
(From) << "," << getBlockName(To) << "} to it found\n"
; } } while (false)
;
1558
1559 // We've made an edge reachable to an existing block, which may
1560 // impact predicates. Otherwise, only mark the phi nodes as touched, as
1561 // they are the only thing that depend on new edges. Anything using their
1562 // values will get propagated to if necessary.
1563 if (MemoryAccess *MemPhi = MSSA->getMemoryAccess(To))
1564 TouchedInstructions.set(InstrDFS.lookup(MemPhi));
1565
1566 auto BI = To->begin();
1567 while (isa<PHINode>(BI)) {
1568 TouchedInstructions.set(InstrDFS.lookup(&*BI));
1569 ++BI;
1570 }
1571 }
1572 }
1573}
1574
1575// Given a predicate condition (from a switch, cmp, or whatever) and a block,
1576// see if we know some constant value for it already.
1577Value *NewGVN::findConditionEquivalence(Value *Cond) const {
1578 auto Result = lookupOperandLeader(Cond);
1579 if (isa<Constant>(Result))
1580 return Result;
1581 return nullptr;
1582}
1583
1584// Process the outgoing edges of a block for reachability.
1585void NewGVN::processOutgoingEdges(TerminatorInst *TI, BasicBlock *B) {
1586 // Evaluate reachability of terminator instruction.
1587 BranchInst *BR;
1588 if ((BR = dyn_cast<BranchInst>(TI)) && BR->isConditional()) {
1589 Value *Cond = BR->getCondition();
1590 Value *CondEvaluated = findConditionEquivalence(Cond);
1591 if (!CondEvaluated) {
1592 if (auto *I = dyn_cast<Instruction>(Cond)) {
1593 const Expression *E = createExpression(I);
1594 if (const auto *CE = dyn_cast<ConstantExpression>(E)) {
1595 CondEvaluated = CE->getConstantValue();
1596 }
1597 } else if (isa<ConstantInt>(Cond)) {
1598 CondEvaluated = Cond;
1599 }
1600 }
1601 ConstantInt *CI;
1602 BasicBlock *TrueSucc = BR->getSuccessor(0);
1603 BasicBlock *FalseSucc = BR->getSuccessor(1);
1604 if (CondEvaluated && (CI = dyn_cast<ConstantInt>(CondEvaluated))) {
1605 if (CI->isOne()) {
1606 DEBUG(dbgs() << "Condition for Terminator " << *TIdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Condition for Terminator " <<
*TI << " evaluated to true\n"; } } while (false)
1607 << " evaluated to true\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Condition for Terminator " <<
*TI << " evaluated to true\n"; } } while (false)
;
1608 updateReachableEdge(B, TrueSucc);
1609 } else if (CI->isZero()) {
1610 DEBUG(dbgs() << "Condition for Terminator " << *TIdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Condition for Terminator " <<
*TI << " evaluated to false\n"; } } while (false)
1611 << " evaluated to false\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Condition for Terminator " <<
*TI << " evaluated to false\n"; } } while (false)
;
1612 updateReachableEdge(B, FalseSucc);
1613 }
1614 } else {
1615 updateReachableEdge(B, TrueSucc);
1616 updateReachableEdge(B, FalseSucc);
1617 }
1618 } else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
1619 // For switches, propagate the case values into the case
1620 // destinations.
1621
1622 // Remember how many outgoing edges there are to every successor.
1623 SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
1624
1625 Value *SwitchCond = SI->getCondition();
1626 Value *CondEvaluated = findConditionEquivalence(SwitchCond);
1627 // See if we were able to turn this switch statement into a constant.
1628 if (CondEvaluated && isa<ConstantInt>(CondEvaluated)) {
1629 auto *CondVal = cast<ConstantInt>(CondEvaluated);
1630 // We should be able to get case value for this.
1631 auto CaseVal = SI->findCaseValue(CondVal);
1632 if (CaseVal.getCaseSuccessor() == SI->getDefaultDest()) {
1633 // We proved the value is outside of the range of the case.
1634 // We can't do anything other than mark the default dest as reachable,
1635 // and go home.
1636 updateReachableEdge(B, SI->getDefaultDest());
1637 return;
1638 }
1639 // Now get where it goes and mark it reachable.
1640 BasicBlock *TargetBlock = CaseVal.getCaseSuccessor();
1641 updateReachableEdge(B, TargetBlock);
1642 } else {
1643 for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
1644 BasicBlock *TargetBlock = SI->getSuccessor(i);
1645 ++SwitchEdges[TargetBlock];
1646 updateReachableEdge(B, TargetBlock);
1647 }
1648 }
1649 } else {
1650 // Otherwise this is either unconditional, or a type we have no
1651 // idea about. Just mark successors as reachable.
1652 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1653 BasicBlock *TargetBlock = TI->getSuccessor(i);
1654 updateReachableEdge(B, TargetBlock);
1655 }
1656
1657 // This also may be a memory defining terminator, in which case, set it
1658 // equivalent to nothing.
1659 if (MemoryAccess *MA = MSSA->getMemoryAccess(TI))
1660 setMemoryAccessEquivTo(MA, nullptr);
1661 }
1662}
1663
1664// The algorithm initially places the values of the routine in the TOP
1665// congruence class. The leader of TOP is the undetermined value `undef`.
1666// When the algorithm has finished, values still in TOP are unreachable.
1667void NewGVN::initializeCongruenceClasses(Function &F) {
1668 // FIXME now i can't remember why this is 2
1669 NextCongruenceNum = 2;
1670 // Initialize all other instructions to be in TOP class.
1671 CongruenceClass::MemberSet InitialValues;
1672 TOPClass = createCongruenceClass(nullptr, nullptr);
1673 TOPClass->RepMemoryAccess = MSSA->getLiveOnEntryDef();
1674 for (auto &B : F) {
1675 if (auto *MP = MSSA->getMemoryAccess(&B))
1676 MemoryAccessToClass[MP] = TOPClass;
1677
1678 for (auto &I : B) {
1679 // Don't insert void terminators into the class. We don't value number
1680 // them, and they just end up sitting in TOP.
1681 if (isa<TerminatorInst>(I) && I.getType()->isVoidTy())
1682 continue;
1683 InitialValues.insert(&I);
1684 ValueToClass[&I] = TOPClass;
1685
1686 // All memory accesses are equivalent to live on entry to start. They must
1687 // be initialized to something so that initial changes are noticed. For
1688 // the maximal answer, we initialize them all to be the same as
1689 // liveOnEntry. Note that to save time, we only initialize the
1690 // MemoryDef's for stores and all MemoryPhis to be equal. Right now, no
1691 // other expression can generate a memory equivalence. If we start
1692 // handling memcpy/etc, we can expand this.
1693 if (isa<StoreInst>(&I)) {
1694 MemoryAccessToClass[MSSA->getMemoryAccess(&I)] = TOPClass;
1695 ++TOPClass->StoreCount;
1696 assert(TOPClass->StoreCount > 0)((TOPClass->StoreCount > 0) ? static_cast<void> (
0) : __assert_fail ("TOPClass->StoreCount > 0", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1696, __PRETTY_FUNCTION__))
;
1697 }
1698 }
1699 }
1700 TOPClass->Members.swap(InitialValues);
1701
1702 // Initialize arguments to be in their own unique congruence classes
1703 for (auto &FA : F.args())
1704 createSingletonCongruenceClass(&FA);
1705}
1706
1707void NewGVN::cleanupTables() {
1708 for (unsigned i = 0, e = CongruenceClasses.size(); i != e; ++i) {
1709 DEBUG(dbgs() << "Congruence class " << CongruenceClasses[i]->ID << " has "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Congruence class " << CongruenceClasses
[i]->ID << " has " << CongruenceClasses[i]->
Members.size() << " members\n"; } } while (false)
1710 << CongruenceClasses[i]->Members.size() << " members\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Congruence class " << CongruenceClasses
[i]->ID << " has " << CongruenceClasses[i]->
Members.size() << " members\n"; } } while (false)
;
1711 // Make sure we delete the congruence class (probably worth switching to
1712 // a unique_ptr at some point.
1713 delete CongruenceClasses[i];
1714 CongruenceClasses[i] = nullptr;
1715 }
1716
1717 ValueToClass.clear();
1718 ArgRecycler.clear(ExpressionAllocator);
1719 ExpressionAllocator.Reset();
1720 CongruenceClasses.clear();
1721 ExpressionToClass.clear();
1722 ValueToExpression.clear();
1723 ReachableBlocks.clear();
1724 ReachableEdges.clear();
1725#ifndef NDEBUG
1726 ProcessedCount.clear();
1727#endif
1728 InstrDFS.clear();
1729 InstructionsToErase.clear();
1730 DFSToInstr.clear();
1731 BlockInstRange.clear();
1732 TouchedInstructions.clear();
1733 MemoryAccessToClass.clear();
1734 PredicateToUsers.clear();
1735}
1736
1737std::pair<unsigned, unsigned> NewGVN::assignDFSNumbers(BasicBlock *B,
1738 unsigned Start) {
1739 unsigned End = Start;
1740 if (MemoryAccess *MemPhi = MSSA->getMemoryAccess(B)) {
1741 InstrDFS[MemPhi] = End++;
1742 DFSToInstr.emplace_back(MemPhi);
1743 }
1744
1745 for (auto &I : *B) {
1746 // There's no need to call isInstructionTriviallyDead more than once on
1747 // an instruction. Therefore, once we know that an instruction is dead
1748 // we change its DFS number so that it doesn't get value numbered.
1749 if (isInstructionTriviallyDead(&I, TLI)) {
1750 InstrDFS[&I] = 0;
1751 DEBUG(dbgs() << "Skipping trivially dead instruction " << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Skipping trivially dead instruction "
<< I << "\n"; } } while (false)
;
1752 markInstructionForDeletion(&I);
1753 continue;
1754 }
1755
1756 InstrDFS[&I] = End++;
1757 DFSToInstr.emplace_back(&I);
1758 }
1759
1760 // All of the range functions taken half-open ranges (open on the end side).
1761 // So we do not subtract one from count, because at this point it is one
1762 // greater than the last instruction.
1763 return std::make_pair(Start, End);
1764}
1765
1766void NewGVN::updateProcessedCount(Value *V) {
1767#ifndef NDEBUG
1768 if (ProcessedCount.count(V) == 0) {
1769 ProcessedCount.insert({V, 1});
1770 } else {
1771 ++ProcessedCount[V];
1772 assert(ProcessedCount[V] < 100 &&((ProcessedCount[V] < 100 && "Seem to have processed the same Value a lot"
) ? static_cast<void> (0) : __assert_fail ("ProcessedCount[V] < 100 && \"Seem to have processed the same Value a lot\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1773, __PRETTY_FUNCTION__))
1773 "Seem to have processed the same Value a lot")((ProcessedCount[V] < 100 && "Seem to have processed the same Value a lot"
) ? static_cast<void> (0) : __assert_fail ("ProcessedCount[V] < 100 && \"Seem to have processed the same Value a lot\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1773, __PRETTY_FUNCTION__))
;
1774 }
1775#endif
1776}
1777// Evaluate MemoryPhi nodes symbolically, just like PHI nodes
1778void NewGVN::valueNumberMemoryPhi(MemoryPhi *MP) {
1779 // If all the arguments are the same, the MemoryPhi has the same value as the
1780 // argument.
1781 // Filter out unreachable blocks and self phis from our operands.
1782 const BasicBlock *PHIBlock = MP->getBlock();
1783 auto Filtered = make_filter_range(MP->operands(), [&](const Use &U) {
1784 return lookupMemoryAccessEquiv(cast<MemoryAccess>(U)) != MP &&
1785 !isMemoryAccessTop(cast<MemoryAccess>(U)) &&
1786 ReachableEdges.count({MP->getIncomingBlock(U), PHIBlock});
1787 });
1788 // If all that is left is nothing, our memoryphi is undef. We keep it as
1789 // InitialClass. Note: The only case this should happen is if we have at
1790 // least one self-argument.
1791 if (Filtered.begin() == Filtered.end()) {
1792 if (setMemoryAccessEquivTo(MP, TOPClass))
1793 markMemoryUsersTouched(MP);
1794 return;
1795 }
1796
1797 // Transform the remaining operands into operand leaders.
1798 // FIXME: mapped_iterator should have a range version.
1799 auto LookupFunc = [&](const Use &U) {
1800 return lookupMemoryAccessEquiv(cast<MemoryAccess>(U));
1801 };
1802 auto MappedBegin = map_iterator(Filtered.begin(), LookupFunc);
1803 auto MappedEnd = map_iterator(Filtered.end(), LookupFunc);
1804
1805 // and now check if all the elements are equal.
1806 // Sadly, we can't use std::equals since these are random access iterators.
1807 MemoryAccess *AllSameValue = *MappedBegin;
1808 ++MappedBegin;
1809 bool AllEqual = std::all_of(
1810 MappedBegin, MappedEnd,
1811 [&AllSameValue](const MemoryAccess *V) { return V == AllSameValue; });
1812
1813 if (AllEqual)
1814 DEBUG(dbgs() << "Memory Phi value numbered to " << *AllSameValue << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Memory Phi value numbered to "
<< *AllSameValue << "\n"; } } while (false)
;
1815 else
1816 DEBUG(dbgs() << "Memory Phi value numbered to itself\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Memory Phi value numbered to itself\n"
; } } while (false)
;
1817
1818 if (setMemoryAccessEquivTo(
1819 MP, AllEqual ? MemoryAccessToClass.lookup(AllSameValue) : nullptr))
1820 markMemoryUsersTouched(MP);
1821}
1822
1823// Value number a single instruction, symbolically evaluating, performing
1824// congruence finding, and updating mappings.
1825void NewGVN::valueNumberInstruction(Instruction *I) {
1826 DEBUG(dbgs() << "Processing instruction " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Processing instruction " <<
*I << "\n"; } } while (false)
;
1827 if (!I->isTerminator()) {
1828 const Expression *Symbolized = nullptr;
1829 if (DebugCounter::shouldExecute(VNCounter)) {
1830 Symbolized = performSymbolicEvaluation(I);
1831 } else {
1832 // Mark the instruction as unused so we don't value number it again.
1833 InstrDFS[I] = 0;
1834 }
1835 // If we couldn't come up with a symbolic expression, use the unknown
1836 // expression
1837 if (Symbolized == nullptr)
1838 Symbolized = createUnknownExpression(I);
1839 performCongruenceFinding(I, Symbolized);
1840 } else {
1841 // Handle terminators that return values. All of them produce values we
1842 // don't currently understand. We don't place non-value producing
1843 // terminators in a class.
1844 if (!I->getType()->isVoidTy()) {
1845 auto *Symbolized = createUnknownExpression(I);
1846 performCongruenceFinding(I, Symbolized);
1847 }
1848 processOutgoingEdges(dyn_cast<TerminatorInst>(I), I->getParent());
1849 }
1850}
1851
1852// Check if there is a path, using single or equal argument phi nodes, from
1853// First to Second.
1854bool NewGVN::singleReachablePHIPath(const MemoryAccess *First,
1855 const MemoryAccess *Second) const {
1856 if (First == Second)
1857 return true;
1858
1859 if (auto *FirstDef = dyn_cast<MemoryUseOrDef>(First)) {
1860 auto *DefAccess = FirstDef->getDefiningAccess();
1861 return singleReachablePHIPath(DefAccess, Second);
1862 } else {
1863 auto *MP = cast<MemoryPhi>(First);
1864 auto ReachableOperandPred = [&](const Use &U) {
1865 return ReachableEdges.count({MP->getIncomingBlock(U), MP->getBlock()});
1866 };
1867 auto FilteredPhiArgs =
1868 make_filter_range(MP->operands(), ReachableOperandPred);
1869 SmallVector<const Value *, 32> OperandList;
1870 std::copy(FilteredPhiArgs.begin(), FilteredPhiArgs.end(),
1871 std::back_inserter(OperandList));
1872 bool Okay = OperandList.size() == 1;
1873 if (!Okay)
1874 Okay = std::equal(OperandList.begin(), OperandList.end(),
1875 OperandList.begin());
1876 if (Okay)
1877 return singleReachablePHIPath(cast<MemoryAccess>(OperandList[0]), Second);
1878 return false;
1879 }
1880}
1881
1882// Verify the that the memory equivalence table makes sense relative to the
1883// congruence classes. Note that this checking is not perfect, and is currently
1884// subject to very rare false negatives. It is only useful for
1885// testing/debugging.
1886void NewGVN::verifyMemoryCongruency() const {
1887 // Anything equivalent in the memory access table should be in the same
1888 // congruence class.
1889
1890 // Filter out the unreachable and trivially dead entries, because they may
1891 // never have been updated if the instructions were not processed.
1892 auto ReachableAccessPred =
1893 [&](const std::pair<const MemoryAccess *, CongruenceClass *> Pair) {
1894 bool Result = ReachableBlocks.count(Pair.first->getBlock());
1895 if (!Result)
1896 return false;
1897 if (auto *MemDef = dyn_cast<MemoryDef>(Pair.first))
1898 return !isInstructionTriviallyDead(MemDef->getMemoryInst());
1899 return true;
1900 };
1901
1902 auto Filtered = make_filter_range(MemoryAccessToClass, ReachableAccessPred);
1903 for (auto KV : Filtered) {
1904 // Unreachable instructions may not have changed because we never process
1905 // them.
1906 if (!ReachableBlocks.count(KV.first->getBlock()))
1907 continue;
1908 if (auto *FirstMUD = dyn_cast<MemoryUseOrDef>(KV.first)) {
1909 auto *SecondMUD = dyn_cast<MemoryUseOrDef>(KV.second->RepMemoryAccess);
1910 if (FirstMUD && SecondMUD)
1911 assert((singleReachablePHIPath(FirstMUD, SecondMUD) ||(((singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass
.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(
SecondMUD->getMemoryInst())) && "The instructions for these memory operations should have "
"been in the same congruence class or reachable through" "a single argument phi"
) ? static_cast<void> (0) : __assert_fail ("(singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(SecondMUD->getMemoryInst())) && \"The instructions for these memory operations should have \" \"been in the same congruence class or reachable through\" \"a single argument phi\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1916, __PRETTY_FUNCTION__))
1912 ValueToClass.lookup(FirstMUD->getMemoryInst()) ==(((singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass
.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(
SecondMUD->getMemoryInst())) && "The instructions for these memory operations should have "
"been in the same congruence class or reachable through" "a single argument phi"
) ? static_cast<void> (0) : __assert_fail ("(singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(SecondMUD->getMemoryInst())) && \"The instructions for these memory operations should have \" \"been in the same congruence class or reachable through\" \"a single argument phi\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1916, __PRETTY_FUNCTION__))
1913 ValueToClass.lookup(SecondMUD->getMemoryInst())) &&(((singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass
.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(
SecondMUD->getMemoryInst())) && "The instructions for these memory operations should have "
"been in the same congruence class or reachable through" "a single argument phi"
) ? static_cast<void> (0) : __assert_fail ("(singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(SecondMUD->getMemoryInst())) && \"The instructions for these memory operations should have \" \"been in the same congruence class or reachable through\" \"a single argument phi\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1916, __PRETTY_FUNCTION__))
1914 "The instructions for these memory operations should have "(((singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass
.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(
SecondMUD->getMemoryInst())) && "The instructions for these memory operations should have "
"been in the same congruence class or reachable through" "a single argument phi"
) ? static_cast<void> (0) : __assert_fail ("(singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(SecondMUD->getMemoryInst())) && \"The instructions for these memory operations should have \" \"been in the same congruence class or reachable through\" \"a single argument phi\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1916, __PRETTY_FUNCTION__))
1915 "been in the same congruence class or reachable through"(((singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass
.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(
SecondMUD->getMemoryInst())) && "The instructions for these memory operations should have "
"been in the same congruence class or reachable through" "a single argument phi"
) ? static_cast<void> (0) : __assert_fail ("(singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(SecondMUD->getMemoryInst())) && \"The instructions for these memory operations should have \" \"been in the same congruence class or reachable through\" \"a single argument phi\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1916, __PRETTY_FUNCTION__))
1916 "a single argument phi")(((singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass
.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(
SecondMUD->getMemoryInst())) && "The instructions for these memory operations should have "
"been in the same congruence class or reachable through" "a single argument phi"
) ? static_cast<void> (0) : __assert_fail ("(singleReachablePHIPath(FirstMUD, SecondMUD) || ValueToClass.lookup(FirstMUD->getMemoryInst()) == ValueToClass.lookup(SecondMUD->getMemoryInst())) && \"The instructions for these memory operations should have \" \"been in the same congruence class or reachable through\" \"a single argument phi\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1916, __PRETTY_FUNCTION__))
;
1917 } else if (auto *FirstMP = dyn_cast<MemoryPhi>(KV.first)) {
1918
1919 // We can only sanely verify that MemoryDefs in the operand list all have
1920 // the same class.
1921 auto ReachableOperandPred = [&](const Use &U) {
1922 return ReachableEdges.count(
1923 {FirstMP->getIncomingBlock(U), FirstMP->getBlock()}) &&
1924 isa<MemoryDef>(U);
1925
1926 };
1927 // All arguments should in the same class, ignoring unreachable arguments
1928 auto FilteredPhiArgs =
1929 make_filter_range(FirstMP->operands(), ReachableOperandPred);
1930 SmallVector<const CongruenceClass *, 16> PhiOpClasses;
1931 std::transform(FilteredPhiArgs.begin(), FilteredPhiArgs.end(),
1932 std::back_inserter(PhiOpClasses), [&](const Use &U) {
1933 const MemoryDef *MD = cast<MemoryDef>(U);
1934 return ValueToClass.lookup(MD->getMemoryInst());
1935 });
1936 assert(std::equal(PhiOpClasses.begin(), PhiOpClasses.end(),((std::equal(PhiOpClasses.begin(), PhiOpClasses.end(), PhiOpClasses
.begin()) && "All MemoryPhi arguments should be in the same class"
) ? static_cast<void> (0) : __assert_fail ("std::equal(PhiOpClasses.begin(), PhiOpClasses.end(), PhiOpClasses.begin()) && \"All MemoryPhi arguments should be in the same class\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1938, __PRETTY_FUNCTION__))
1937 PhiOpClasses.begin()) &&((std::equal(PhiOpClasses.begin(), PhiOpClasses.end(), PhiOpClasses
.begin()) && "All MemoryPhi arguments should be in the same class"
) ? static_cast<void> (0) : __assert_fail ("std::equal(PhiOpClasses.begin(), PhiOpClasses.end(), PhiOpClasses.begin()) && \"All MemoryPhi arguments should be in the same class\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1938, __PRETTY_FUNCTION__))
1938 "All MemoryPhi arguments should be in the same class")((std::equal(PhiOpClasses.begin(), PhiOpClasses.end(), PhiOpClasses
.begin()) && "All MemoryPhi arguments should be in the same class"
) ? static_cast<void> (0) : __assert_fail ("std::equal(PhiOpClasses.begin(), PhiOpClasses.end(), PhiOpClasses.begin()) && \"All MemoryPhi arguments should be in the same class\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1938, __PRETTY_FUNCTION__))
;
1939 }
1940 }
1941}
1942
1943// Verify that the sparse propagation we did actually found the maximal fixpoint
1944// We do this by storing the value to class mapping, touching all instructions,
1945// and redoing the iteration to see if anything changed.
1946void NewGVN::verifyIterationSettled(Function &F) {
1947#ifndef NDEBUG
1948 if (DebugCounter::isCounterSet(VNCounter))
1
Assuming the condition is false
2
Taking false branch
1949 DebugCounter::setCounterValue(VNCounter, StartingVNCounter);
1950
1951 // Note that we have to store the actual classes, as we may change existing
1952 // classes during iteration. This is because our memory iteration propagation
1953 // is not perfect, and so may waste a little work. But it should generate
1954 // exactly the same congruence classes we have now, with different IDs.
1955 std::map<const Value *, CongruenceClass> BeforeIteration;
1956
1957 for (auto &KV : ValueToClass) {
1958 if (auto *I = dyn_cast<Instruction>(KV.first))
1959 // Skip unused/dead instructions.
1960 if (InstrDFS.lookup(I) == 0)
1961 continue;
1962 BeforeIteration.insert({KV.first, *KV.second});
1963 }
1964
1965 TouchedInstructions.set();
1966 TouchedInstructions.reset(0);
1967 iterateTouchedInstructions();
1968 DenseSet<std::pair<const CongruenceClass *, const CongruenceClass *>>
1969 EqualClasses;
1970 for (const auto &KV : ValueToClass) {
1971 if (auto *I = dyn_cast<Instruction>(KV.first))
3
Taking false branch
1972 // Skip unused/dead instructions.
1973 if (InstrDFS.lookup(I) == 0)
1974 continue;
1975 // We could sink these uses, but i think this adds a bit of clarity here as
1976 // to what we are comparing.
1977 auto *BeforeCC = &BeforeIteration.find(KV.first)->second;
1978 auto *AfterCC = KV.second;
1979 // Note that the classes can't change at this point, so we memoize the set
1980 // that are equal.
1981 if (!EqualClasses.count({BeforeCC, AfterCC})) {
4
Assuming the condition is true
5
Taking true branch
1982 assert(areClassesEquivalent(BeforeCC, AfterCC) &&((areClassesEquivalent(BeforeCC, AfterCC) && "Value number changed after main loop completed!"
) ? static_cast<void> (0) : __assert_fail ("areClassesEquivalent(BeforeCC, AfterCC) && \"Value number changed after main loop completed!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1983, __PRETTY_FUNCTION__))
6
Within the expansion of the macro 'assert':
a
Passing value via 1st parameter 'A'
b
Calling 'areClassesEquivalent'
1983 "Value number changed after main loop completed!")((areClassesEquivalent(BeforeCC, AfterCC) && "Value number changed after main loop completed!"
) ? static_cast<void> (0) : __assert_fail ("areClassesEquivalent(BeforeCC, AfterCC) && \"Value number changed after main loop completed!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 1983, __PRETTY_FUNCTION__))
;
1984 EqualClasses.insert({BeforeCC, AfterCC});
1985 }
1986 }
1987#endif
1988}
1989
1990// This is the main value numbering loop, it iterates over the initial touched
1991// instruction set, propagating value numbers, marking things touched, etc,
1992// until the set of touched instructions is completely empty.
1993void NewGVN::iterateTouchedInstructions() {
1994 unsigned int Iterations = 0;
1995 // Figure out where touchedinstructions starts
1996 int FirstInstr = TouchedInstructions.find_first();
1997 // Nothing set, nothing to iterate, just return.
1998 if (FirstInstr == -1)
1999 return;
2000 BasicBlock *LastBlock = getBlockForValue(DFSToInstr[FirstInstr]);
2001 while (TouchedInstructions.any()) {
2002 ++Iterations;
2003 // Walk through all the instructions in all the blocks in RPO.
2004 // TODO: As we hit a new block, we should push and pop equalities into a
2005 // table lookupOperandLeader can use, to catch things PredicateInfo
2006 // might miss, like edge-only equivalences.
2007 for (int InstrNum = TouchedInstructions.find_first(); InstrNum != -1;
2008 InstrNum = TouchedInstructions.find_next(InstrNum)) {
2009
2010 // This instruction was found to be dead. We don't bother looking
2011 // at it again.
2012 if (InstrNum == 0) {
2013 TouchedInstructions.reset(InstrNum);
2014 continue;
2015 }
2016
2017 Value *V = DFSToInstr[InstrNum];
2018 BasicBlock *CurrBlock = getBlockForValue(V);
2019
2020 // If we hit a new block, do reachability processing.
2021 if (CurrBlock != LastBlock) {
2022 LastBlock = CurrBlock;
2023 bool BlockReachable = ReachableBlocks.count(CurrBlock);
2024 const auto &CurrInstRange = BlockInstRange.lookup(CurrBlock);
2025
2026 // If it's not reachable, erase any touched instructions and move on.
2027 if (!BlockReachable) {
2028 TouchedInstructions.reset(CurrInstRange.first, CurrInstRange.second);
2029 DEBUG(dbgs() << "Skipping instructions in block "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Skipping instructions in block "
<< getBlockName(CurrBlock) << " because it is unreachable\n"
; } } while (false)
2030 << getBlockName(CurrBlock)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Skipping instructions in block "
<< getBlockName(CurrBlock) << " because it is unreachable\n"
; } } while (false)
2031 << " because it is unreachable\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Skipping instructions in block "
<< getBlockName(CurrBlock) << " because it is unreachable\n"
; } } while (false)
;
2032 continue;
2033 }
2034 updateProcessedCount(CurrBlock);
2035 }
2036
2037 if (auto *MP = dyn_cast<MemoryPhi>(V)) {
2038 DEBUG(dbgs() << "Processing MemoryPhi " << *MP << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Processing MemoryPhi " <<
*MP << "\n"; } } while (false)
;
2039 valueNumberMemoryPhi(MP);
2040 } else if (auto *I = dyn_cast<Instruction>(V)) {
2041 valueNumberInstruction(I);
2042 } else {
2043 llvm_unreachable("Should have been a MemoryPhi or Instruction")::llvm::llvm_unreachable_internal("Should have been a MemoryPhi or Instruction"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2043)
;
2044 }
2045 updateProcessedCount(V);
2046 // Reset after processing (because we may mark ourselves as touched when
2047 // we propagate equalities).
2048 TouchedInstructions.reset(InstrNum);
2049 }
2050 }
2051 NumGVNMaxIterations = std::max(NumGVNMaxIterations.getValue(), Iterations);
2052}
2053
2054// This is the main transformation entry point.
2055bool NewGVN::runGVN() {
2056 if (DebugCounter::isCounterSet(VNCounter))
2057 StartingVNCounter = DebugCounter::getCounterValue(VNCounter);
2058 bool Changed = false;
2059 NumFuncArgs = F.arg_size();
2060 MSSAWalker = MSSA->getWalker();
2061
2062 // Count number of instructions for sizing of hash tables, and come
2063 // up with a global dfs numbering for instructions.
2064 unsigned ICount = 1;
2065 // Add an empty instruction to account for the fact that we start at 1
2066 DFSToInstr.emplace_back(nullptr);
2067 // Note: We want ideal RPO traversal of the blocks, which is not quite the
2068 // same as dominator tree order, particularly with regard whether backedges
2069 // get visited first or second, given a block with multiple successors.
2070 // If we visit in the wrong order, we will end up performing N times as many
2071 // iterations.
2072 // The dominator tree does guarantee that, for a given dom tree node, it's
2073 // parent must occur before it in the RPO ordering. Thus, we only need to sort
2074 // the siblings.
2075 DenseMap<const DomTreeNode *, unsigned> RPOOrdering;
2076 ReversePostOrderTraversal<Function *> RPOT(&F);
2077 unsigned Counter = 0;
2078 for (auto &B : RPOT) {
2079 auto *Node = DT->getNode(B);
2080 assert(Node && "RPO and Dominator tree should have same reachability")((Node && "RPO and Dominator tree should have same reachability"
) ? static_cast<void> (0) : __assert_fail ("Node && \"RPO and Dominator tree should have same reachability\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2080, __PRETTY_FUNCTION__))
;
2081 RPOOrdering[Node] = ++Counter;
2082 }
2083 // Sort dominator tree children arrays into RPO.
2084 for (auto &B : RPOT) {
2085 auto *Node = DT->getNode(B);
2086 if (Node->getChildren().size() > 1)
2087 std::sort(Node->begin(), Node->end(),
2088 [&RPOOrdering](const DomTreeNode *A, const DomTreeNode *B) {
2089 return RPOOrdering[A] < RPOOrdering[B];
2090 });
2091 }
2092
2093 // Now a standard depth first ordering of the domtree is equivalent to RPO.
2094 auto DFI = df_begin(DT->getRootNode());
2095 for (auto DFE = df_end(DT->getRootNode()); DFI != DFE; ++DFI) {
2096 BasicBlock *B = DFI->getBlock();
2097 const auto &BlockRange = assignDFSNumbers(B, ICount);
2098 BlockInstRange.insert({B, BlockRange});
2099 ICount += BlockRange.second - BlockRange.first;
2100 }
2101
2102 // Handle forward unreachable blocks and figure out which blocks
2103 // have single preds.
2104 for (auto &B : F) {
2105 // Assign numbers to unreachable blocks.
2106 if (!DFI.nodeVisited(DT->getNode(&B))) {
2107 const auto &BlockRange = assignDFSNumbers(&B, ICount);
2108 BlockInstRange.insert({&B, BlockRange});
2109 ICount += BlockRange.second - BlockRange.first;
2110 }
2111 }
2112
2113 TouchedInstructions.resize(ICount);
2114 // Ensure we don't end up resizing the expressionToClass map, as
2115 // that can be quite expensive. At most, we have one expression per
2116 // instruction.
2117 ExpressionToClass.reserve(ICount);
2118
2119 // Initialize the touched instructions to include the entry block.
2120 const auto &InstRange = BlockInstRange.lookup(&F.getEntryBlock());
2121 TouchedInstructions.set(InstRange.first, InstRange.second);
2122 ReachableBlocks.insert(&F.getEntryBlock());
2123
2124 initializeCongruenceClasses(F);
2125 iterateTouchedInstructions();
2126#ifndef NDEBUG
2127 verifyMemoryCongruency();
2128 verifyIterationSettled(F);
2129#endif
2130
2131 Changed |= eliminateInstructions(F);
2132
2133 // Delete all instructions marked for deletion.
2134 for (Instruction *ToErase : InstructionsToErase) {
2135 if (!ToErase->use_empty())
2136 ToErase->replaceAllUsesWith(UndefValue::get(ToErase->getType()));
2137
2138 ToErase->eraseFromParent();
2139 }
2140
2141 // Delete all unreachable blocks.
2142 auto UnreachableBlockPred = [&](const BasicBlock &BB) {
2143 return !ReachableBlocks.count(&BB);
2144 };
2145
2146 for (auto &BB : make_filter_range(F, UnreachableBlockPred)) {
2147 DEBUG(dbgs() << "We believe block " << getBlockName(&BB)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "We believe block " << getBlockName
(&BB) << " is unreachable\n"; } } while (false)
2148 << " is unreachable\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "We believe block " << getBlockName
(&BB) << " is unreachable\n"; } } while (false)
;
2149 deleteInstructionsInBlock(&BB);
2150 Changed = true;
2151 }
2152
2153 cleanupTables();
2154 return Changed;
2155}
2156
2157// Return true if V is a value that will always be available (IE can
2158// be placed anywhere) in the function. We don't do globals here
2159// because they are often worse to put in place.
2160// TODO: Separate cost from availability
2161static bool alwaysAvailable(Value *V) {
2162 return isa<Constant>(V) || isa<Argument>(V);
2163}
2164
2165struct NewGVN::ValueDFS {
2166 int DFSIn = 0;
2167 int DFSOut = 0;
2168 int LocalNum = 0;
2169 // Only one of Def and U will be set.
2170 Value *Def = nullptr;
2171 Use *U = nullptr;
2172 bool operator<(const ValueDFS &Other) const {
2173 // It's not enough that any given field be less than - we have sets
2174 // of fields that need to be evaluated together to give a proper ordering.
2175 // For example, if you have;
2176 // DFS (1, 3)
2177 // Val 0
2178 // DFS (1, 2)
2179 // Val 50
2180 // We want the second to be less than the first, but if we just go field
2181 // by field, we will get to Val 0 < Val 50 and say the first is less than
2182 // the second. We only want it to be less than if the DFS orders are equal.
2183 //
2184 // Each LLVM instruction only produces one value, and thus the lowest-level
2185 // differentiator that really matters for the stack (and what we use as as a
2186 // replacement) is the local dfs number.
2187 // Everything else in the structure is instruction level, and only affects
2188 // the order in which we will replace operands of a given instruction.
2189 //
2190 // For a given instruction (IE things with equal dfsin, dfsout, localnum),
2191 // the order of replacement of uses does not matter.
2192 // IE given,
2193 // a = 5
2194 // b = a + a
2195 // When you hit b, you will have two valuedfs with the same dfsin, out, and
2196 // localnum.
2197 // The .val will be the same as well.
2198 // The .u's will be different.
2199 // You will replace both, and it does not matter what order you replace them
2200 // in (IE whether you replace operand 2, then operand 1, or operand 1, then
2201 // operand 2).
2202 // Similarly for the case of same dfsin, dfsout, localnum, but different
2203 // .val's
2204 // a = 5
2205 // b = 6
2206 // c = a + b
2207 // in c, we will a valuedfs for a, and one for b,with everything the same
2208 // but .val and .u.
2209 // It does not matter what order we replace these operands in.
2210 // You will always end up with the same IR, and this is guaranteed.
2211 return std::tie(DFSIn, DFSOut, LocalNum, Def, U) <
2212 std::tie(Other.DFSIn, Other.DFSOut, Other.LocalNum, Other.Def,
2213 Other.U);
2214 }
2215};
2216
2217// This function converts the set of members for a congruence class from values,
2218// to sets of defs and uses with associated DFS info. The total number of
2219// reachable uses for each value is stored in UseCount, and instructions that
2220// seem
2221// dead (have no non-dead uses) are stored in ProbablyDead.
2222void NewGVN::convertClassToDFSOrdered(
2223 const CongruenceClass::MemberSet &Dense,
2224 SmallVectorImpl<ValueDFS> &DFSOrderedSet,
2225 DenseMap<const Value *, unsigned int> &UseCounts,
2226 SmallPtrSetImpl<Instruction *> &ProbablyDead) {
2227 for (auto D : Dense) {
2228 // First add the value.
2229 BasicBlock *BB = getBlockForValue(D);
2230 // Constants are handled prior to ever calling this function, so
2231 // we should only be left with instructions as members.
2232 assert(BB && "Should have figured out a basic block for value")((BB && "Should have figured out a basic block for value"
) ? static_cast<void> (0) : __assert_fail ("BB && \"Should have figured out a basic block for value\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2232, __PRETTY_FUNCTION__))
;
2233 ValueDFS VDDef;
2234 DomTreeNode *DomNode = DT->getNode(BB);
2235 VDDef.DFSIn = DomNode->getDFSNumIn();
2236 VDDef.DFSOut = DomNode->getDFSNumOut();
2237 // If it's a store, use the leader of the value operand.
2238 if (auto *SI = dyn_cast<StoreInst>(D)) {
2239 auto Leader = lookupOperandLeader(SI->getValueOperand());
2240 VDDef.Def = alwaysAvailable(Leader) ? Leader : SI->getValueOperand();
2241 } else {
2242 VDDef.Def = D;
2243 }
2244 assert(isa<Instruction>(D) &&((isa<Instruction>(D) && "The dense set member should always be an instruction"
) ? static_cast<void> (0) : __assert_fail ("isa<Instruction>(D) && \"The dense set member should always be an instruction\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2245, __PRETTY_FUNCTION__))
2245 "The dense set member should always be an instruction")((isa<Instruction>(D) && "The dense set member should always be an instruction"
) ? static_cast<void> (0) : __assert_fail ("isa<Instruction>(D) && \"The dense set member should always be an instruction\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2245, __PRETTY_FUNCTION__))
;
2246 VDDef.LocalNum = InstrDFS.lookup(D);
2247 DFSOrderedSet.emplace_back(VDDef);
2248 Instruction *Def = cast<Instruction>(D);
2249 unsigned int UseCount = 0;
2250 // Now add the uses.
2251 for (auto &U : Def->uses()) {
2252 if (auto *I = dyn_cast<Instruction>(U.getUser())) {
2253 // Don't try to replace into dead uses
2254 if (InstructionsToErase.count(I))
2255 continue;
2256 ValueDFS VDUse;
2257 // Put the phi node uses in the incoming block.
2258 BasicBlock *IBlock;
2259 if (auto *P = dyn_cast<PHINode>(I)) {
2260 IBlock = P->getIncomingBlock(U);
2261 // Make phi node users appear last in the incoming block
2262 // they are from.
2263 VDUse.LocalNum = InstrDFS.size() + 1;
2264 } else {
2265 IBlock = I->getParent();
2266 VDUse.LocalNum = InstrDFS.lookup(I);
2267 }
2268
2269 // Skip uses in unreachable blocks, as we're going
2270 // to delete them.
2271 if (ReachableBlocks.count(IBlock) == 0)
2272 continue;
2273
2274 DomTreeNode *DomNode = DT->getNode(IBlock);
2275 VDUse.DFSIn = DomNode->getDFSNumIn();
2276 VDUse.DFSOut = DomNode->getDFSNumOut();
2277 VDUse.U = &U;
2278 ++UseCount;
2279 DFSOrderedSet.emplace_back(VDUse);
2280 }
2281 }
2282
2283 // If there are no uses, it's probably dead (but it may have side-effects,
2284 // so not definitely dead. Otherwise, store the number of uses so we can
2285 // track if it becomes dead later).
2286 if (UseCount == 0)
2287 ProbablyDead.insert(Def);
2288 else
2289 UseCounts[Def] = UseCount;
2290 }
2291}
2292
2293// This function converts the set of members for a congruence class from values,
2294// to the set of defs for loads and stores, with associated DFS info.
2295void NewGVN::convertClassToLoadsAndStores(
2296 const CongruenceClass::MemberSet &Dense,
2297 SmallVectorImpl<ValueDFS> &LoadsAndStores) {
2298 for (auto D : Dense) {
2299 if (!isa<LoadInst>(D) && !isa<StoreInst>(D))
2300 continue;
2301
2302 BasicBlock *BB = getBlockForValue(D);
2303 ValueDFS VD;
2304 DomTreeNode *DomNode = DT->getNode(BB);
2305 VD.DFSIn = DomNode->getDFSNumIn();
2306 VD.DFSOut = DomNode->getDFSNumOut();
2307 VD.Def = D;
2308
2309 // If it's an instruction, use the real local dfs number.
2310 if (auto *I = dyn_cast<Instruction>(D))
2311 VD.LocalNum = InstrDFS.lookup(I);
2312 else
2313 llvm_unreachable("Should have been an instruction")::llvm::llvm_unreachable_internal("Should have been an instruction"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2313)
;
2314
2315 LoadsAndStores.emplace_back(VD);
2316 }
2317}
2318
2319static void patchReplacementInstruction(Instruction *I, Value *Repl) {
2320 auto *ReplInst = dyn_cast<Instruction>(Repl);
2321 if (!ReplInst)
2322 return;
2323
2324 // Patch the replacement so that it is not more restrictive than the value
2325 // being replaced.
2326 // Note that if 'I' is a load being replaced by some operation,
2327 // for example, by an arithmetic operation, then andIRFlags()
2328 // would just erase all math flags from the original arithmetic
2329 // operation, which is clearly not wanted and not needed.
2330 if (!isa<LoadInst>(I))
2331 ReplInst->andIRFlags(I);
2332
2333 // FIXME: If both the original and replacement value are part of the
2334 // same control-flow region (meaning that the execution of one
2335 // guarantees the execution of the other), then we can combine the
2336 // noalias scopes here and do better than the general conservative
2337 // answer used in combineMetadata().
2338
2339 // In general, GVN unifies expressions over different control-flow
2340 // regions, and so we need a conservative combination of the noalias
2341 // scopes.
2342 static const unsigned KnownIDs[] = {
2343 LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
2344 LLVMContext::MD_noalias, LLVMContext::MD_range,
2345 LLVMContext::MD_fpmath, LLVMContext::MD_invariant_load,
2346 LLVMContext::MD_invariant_group};
2347 combineMetadata(ReplInst, I, KnownIDs);
2348}
2349
2350static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
2351 patchReplacementInstruction(I, Repl);
2352 I->replaceAllUsesWith(Repl);
2353}
2354
2355void NewGVN::deleteInstructionsInBlock(BasicBlock *BB) {
2356 DEBUG(dbgs() << " BasicBlock Dead:" << *BB)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << " BasicBlock Dead:" << *
BB; } } while (false)
;
2357 ++NumGVNBlocksDeleted;
2358
2359 // Delete the instructions backwards, as it has a reduced likelihood of having
2360 // to update as many def-use and use-def chains. Start after the terminator.
2361 auto StartPoint = BB->rbegin();
2362 ++StartPoint;
2363 // Note that we explicitly recalculate BB->rend() on each iteration,
2364 // as it may change when we remove the first instruction.
2365 for (BasicBlock::reverse_iterator I(StartPoint); I != BB->rend();) {
2366 Instruction &Inst = *I++;
2367 if (!Inst.use_empty())
2368 Inst.replaceAllUsesWith(UndefValue::get(Inst.getType()));
2369 if (isa<LandingPadInst>(Inst))
2370 continue;
2371
2372 Inst.eraseFromParent();
2373 ++NumGVNInstrDeleted;
2374 }
2375 // Now insert something that simplifycfg will turn into an unreachable.
2376 Type *Int8Ty = Type::getInt8Ty(BB->getContext());
2377 new StoreInst(UndefValue::get(Int8Ty),
2378 Constant::getNullValue(Int8Ty->getPointerTo()),
2379 BB->getTerminator());
2380}
2381
2382void NewGVN::markInstructionForDeletion(Instruction *I) {
2383 DEBUG(dbgs() << "Marking " << *I << " for deletion\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Marking " << *I <<
" for deletion\n"; } } while (false)
;
2384 InstructionsToErase.insert(I);
2385}
2386
2387void NewGVN::replaceInstruction(Instruction *I, Value *V) {
2388
2389 DEBUG(dbgs() << "Replacing " << *I << " with " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Replacing " << *I <<
" with " << *V << "\n"; } } while (false)
;
2390 patchAndReplaceAllUsesWith(I, V);
2391 // We save the actual erasing to avoid invalidating memory
2392 // dependencies until we are done with everything.
2393 markInstructionForDeletion(I);
2394}
2395
2396namespace {
2397
2398// This is a stack that contains both the value and dfs info of where
2399// that value is valid.
2400class ValueDFSStack {
2401public:
2402 Value *back() const { return ValueStack.back(); }
2403 std::pair<int, int> dfs_back() const { return DFSStack.back(); }
2404
2405 void push_back(Value *V, int DFSIn, int DFSOut) {
2406 ValueStack.emplace_back(V);
2407 DFSStack.emplace_back(DFSIn, DFSOut);
2408 }
2409 bool empty() const { return DFSStack.empty(); }
2410 bool isInScope(int DFSIn, int DFSOut) const {
2411 if (empty())
2412 return false;
2413 return DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second;
2414 }
2415
2416 void popUntilDFSScope(int DFSIn, int DFSOut) {
2417
2418 // These two should always be in sync at this point.
2419 assert(ValueStack.size() == DFSStack.size() &&((ValueStack.size() == DFSStack.size() && "Mismatch between ValueStack and DFSStack"
) ? static_cast<void> (0) : __assert_fail ("ValueStack.size() == DFSStack.size() && \"Mismatch between ValueStack and DFSStack\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2420, __PRETTY_FUNCTION__))
2420 "Mismatch between ValueStack and DFSStack")((ValueStack.size() == DFSStack.size() && "Mismatch between ValueStack and DFSStack"
) ? static_cast<void> (0) : __assert_fail ("ValueStack.size() == DFSStack.size() && \"Mismatch between ValueStack and DFSStack\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2420, __PRETTY_FUNCTION__))
;
2421 while (
2422 !DFSStack.empty() &&
2423 !(DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second)) {
2424 DFSStack.pop_back();
2425 ValueStack.pop_back();
2426 }
2427 }
2428
2429private:
2430 SmallVector<Value *, 8> ValueStack;
2431 SmallVector<std::pair<int, int>, 8> DFSStack;
2432};
2433}
2434
2435bool NewGVN::eliminateInstructions(Function &F) {
2436 // This is a non-standard eliminator. The normal way to eliminate is
2437 // to walk the dominator tree in order, keeping track of available
2438 // values, and eliminating them. However, this is mildly
2439 // pointless. It requires doing lookups on every instruction,
2440 // regardless of whether we will ever eliminate it. For
2441 // instructions part of most singleton congruence classes, we know we
2442 // will never eliminate them.
2443
2444 // Instead, this eliminator looks at the congruence classes directly, sorts
2445 // them into a DFS ordering of the dominator tree, and then we just
2446 // perform elimination straight on the sets by walking the congruence
2447 // class member uses in order, and eliminate the ones dominated by the
2448 // last member. This is worst case O(E log E) where E = number of
2449 // instructions in a single congruence class. In theory, this is all
2450 // instructions. In practice, it is much faster, as most instructions are
2451 // either in singleton congruence classes or can't possibly be eliminated
2452 // anyway (if there are no overlapping DFS ranges in class).
2453 // When we find something not dominated, it becomes the new leader
2454 // for elimination purposes.
2455 // TODO: If we wanted to be faster, We could remove any members with no
2456 // overlapping ranges while sorting, as we will never eliminate anything
2457 // with those members, as they don't dominate anything else in our set.
2458
2459 bool AnythingReplaced = false;
2460
2461 // Since we are going to walk the domtree anyway, and we can't guarantee the
2462 // DFS numbers are updated, we compute some ourselves.
2463 DT->updateDFSNumbers();
2464
2465 for (auto &B : F) {
2466 if (!ReachableBlocks.count(&B)) {
2467 for (const auto S : successors(&B)) {
2468 for (auto II = S->begin(); isa<PHINode>(II); ++II) {
2469 auto &Phi = cast<PHINode>(*II);
2470 DEBUG(dbgs() << "Replacing incoming value of " << *II << " for block "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Replacing incoming value of " <<
*II << " for block " << getBlockName(&B) <<
" with undef due to it being unreachable\n"; } } while (false
)
2471 << getBlockName(&B)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Replacing incoming value of " <<
*II << " for block " << getBlockName(&B) <<
" with undef due to it being unreachable\n"; } } while (false
)
2472 << " with undef due to it being unreachable\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Replacing incoming value of " <<
*II << " for block " << getBlockName(&B) <<
" with undef due to it being unreachable\n"; } } while (false
)
;
2473 for (auto &Operand : Phi.incoming_values())
2474 if (Phi.getIncomingBlock(Operand) == &B)
2475 Operand.set(UndefValue::get(Phi.getType()));
2476 }
2477 }
2478 }
2479 }
2480
2481 // Map to store the use counts
2482 DenseMap<const Value *, unsigned int> UseCounts;
2483 for (CongruenceClass *CC : reverse(CongruenceClasses)) {
2484 // Track the equivalent store info so we can decide whether to try
2485 // dead store elimination.
2486 SmallVector<ValueDFS, 8> PossibleDeadStores;
2487 SmallPtrSet<Instruction *, 8> ProbablyDead;
2488 if (CC->Dead)
2489 continue;
2490 // Everything still in the TOP class is unreachable or dead.
2491 if (CC == TOPClass) {
2492#ifndef NDEBUG
2493 for (auto M : CC->Members)
2494 assert((!ReachableBlocks.count(cast<Instruction>(M)->getParent()) ||(((!ReachableBlocks.count(cast<Instruction>(M)->getParent
()) || InstructionsToErase.count(cast<Instruction>(M)))
&& "Everything in TOP should be unreachable or dead at this "
"point") ? static_cast<void> (0) : __assert_fail ("(!ReachableBlocks.count(cast<Instruction>(M)->getParent()) || InstructionsToErase.count(cast<Instruction>(M))) && \"Everything in TOP should be unreachable or dead at this \" \"point\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2497, __PRETTY_FUNCTION__))
2495 InstructionsToErase.count(cast<Instruction>(M))) &&(((!ReachableBlocks.count(cast<Instruction>(M)->getParent
()) || InstructionsToErase.count(cast<Instruction>(M)))
&& "Everything in TOP should be unreachable or dead at this "
"point") ? static_cast<void> (0) : __assert_fail ("(!ReachableBlocks.count(cast<Instruction>(M)->getParent()) || InstructionsToErase.count(cast<Instruction>(M))) && \"Everything in TOP should be unreachable or dead at this \" \"point\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2497, __PRETTY_FUNCTION__))
2496 "Everything in TOP should be unreachable or dead at this "(((!ReachableBlocks.count(cast<Instruction>(M)->getParent
()) || InstructionsToErase.count(cast<Instruction>(M)))
&& "Everything in TOP should be unreachable or dead at this "
"point") ? static_cast<void> (0) : __assert_fail ("(!ReachableBlocks.count(cast<Instruction>(M)->getParent()) || InstructionsToErase.count(cast<Instruction>(M))) && \"Everything in TOP should be unreachable or dead at this \" \"point\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2497, __PRETTY_FUNCTION__))
2497 "point")(((!ReachableBlocks.count(cast<Instruction>(M)->getParent
()) || InstructionsToErase.count(cast<Instruction>(M)))
&& "Everything in TOP should be unreachable or dead at this "
"point") ? static_cast<void> (0) : __assert_fail ("(!ReachableBlocks.count(cast<Instruction>(M)->getParent()) || InstructionsToErase.count(cast<Instruction>(M))) && \"Everything in TOP should be unreachable or dead at this \" \"point\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2497, __PRETTY_FUNCTION__))
;
2498#endif
2499 continue;
2500 }
2501
2502 assert(CC->RepLeader && "We should have had a leader")((CC->RepLeader && "We should have had a leader") ?
static_cast<void> (0) : __assert_fail ("CC->RepLeader && \"We should have had a leader\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2502, __PRETTY_FUNCTION__))
;
2503
2504 // If this is a leader that is always available, and it's a
2505 // constant or has no equivalences, just replace everything with
2506 // it. We then update the congruence class with whatever members
2507 // are left.
2508 Value *Leader = CC->RepStoredValue ? CC->RepStoredValue : CC->RepLeader;
2509 if (alwaysAvailable(Leader)) {
2510 SmallPtrSet<Value *, 4> MembersLeft;
2511 for (auto M : CC->Members) {
2512 Value *Member = M;
2513 // Void things have no uses we can replace.
2514 if (Member == Leader || Member->getType()->isVoidTy()) {
2515 MembersLeft.insert(Member);
2516 continue;
2517 }
2518 DEBUG(dbgs() << "Found replacement " << *(Leader) << " for " << *Memberdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Found replacement " << *
(Leader) << " for " << *Member << "\n"; } }
while (false)
2519 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Found replacement " << *
(Leader) << " for " << *Member << "\n"; } }
while (false)
;
2520 // Due to equality propagation, these may not always be
2521 // instructions, they may be real values. We don't really
2522 // care about trying to replace the non-instructions.
2523 if (auto *I = dyn_cast<Instruction>(Member)) {
2524 assert(Leader != I && "About to accidentally remove our leader")((Leader != I && "About to accidentally remove our leader"
) ? static_cast<void> (0) : __assert_fail ("Leader != I && \"About to accidentally remove our leader\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2524, __PRETTY_FUNCTION__))
;
2525 replaceInstruction(I, Leader);
2526 AnythingReplaced = true;
2527 continue;
2528 } else {
2529 MembersLeft.insert(I);
2530 }
2531 }
2532 CC->Members.swap(MembersLeft);
2533 } else {
2534 DEBUG(dbgs() << "Eliminating in congruence class " << CC->ID << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Eliminating in congruence class "
<< CC->ID << "\n"; } } while (false)
;
2535 // If this is a singleton, we can skip it.
2536 if (CC->Members.size() != 1) {
2537
2538 // This is a stack because equality replacement/etc may place
2539 // constants in the middle of the member list, and we want to use
2540 // those constant values in preference to the current leader, over
2541 // the scope of those constants.
2542 ValueDFSStack EliminationStack;
2543
2544 // Convert the members to DFS ordered sets and then merge them.
2545 SmallVector<ValueDFS, 8> DFSOrderedSet;
2546 convertClassToDFSOrdered(CC->Members, DFSOrderedSet, UseCounts,
2547 ProbablyDead);
2548
2549 // Sort the whole thing.
2550 std::sort(DFSOrderedSet.begin(), DFSOrderedSet.end());
2551 for (auto &VD : DFSOrderedSet) {
2552 int MemberDFSIn = VD.DFSIn;
2553 int MemberDFSOut = VD.DFSOut;
2554 Value *Def = VD.Def;
2555 Use *U = VD.U;
2556 // We ignore void things because we can't get a value from them.
2557 if (Def && Def->getType()->isVoidTy())
2558 continue;
2559
2560 if (EliminationStack.empty()) {
2561 DEBUG(dbgs() << "Elimination Stack is empty\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Elimination Stack is empty\n";
} } while (false)
;
2562 } else {
2563 DEBUG(dbgs() << "Elimination Stack Top DFS numbers are ("do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Elimination Stack Top DFS numbers are ("
<< EliminationStack.dfs_back().first << "," <<
EliminationStack.dfs_back().second << ")\n"; } } while
(false)
2564 << EliminationStack.dfs_back().first << ","do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Elimination Stack Top DFS numbers are ("
<< EliminationStack.dfs_back().first << "," <<
EliminationStack.dfs_back().second << ")\n"; } } while
(false)
2565 << EliminationStack.dfs_back().second << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Elimination Stack Top DFS numbers are ("
<< EliminationStack.dfs_back().first << "," <<
EliminationStack.dfs_back().second << ")\n"; } } while
(false)
;
2566 }
2567
2568 DEBUG(dbgs() << "Current DFS numbers are (" << MemberDFSIn << ","do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Current DFS numbers are (" <<
MemberDFSIn << "," << MemberDFSOut << ")\n"
; } } while (false)
2569 << MemberDFSOut << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Current DFS numbers are (" <<
MemberDFSIn << "," << MemberDFSOut << ")\n"
; } } while (false)
;
2570 // First, we see if we are out of scope or empty. If so,
2571 // and there equivalences, we try to replace the top of
2572 // stack with equivalences (if it's on the stack, it must
2573 // not have been eliminated yet).
2574 // Then we synchronize to our current scope, by
2575 // popping until we are back within a DFS scope that
2576 // dominates the current member.
2577 // Then, what happens depends on a few factors
2578 // If the stack is now empty, we need to push
2579 // If we have a constant or a local equivalence we want to
2580 // start using, we also push.
2581 // Otherwise, we walk along, processing members who are
2582 // dominated by this scope, and eliminate them.
2583 bool ShouldPush = Def && EliminationStack.empty();
2584 bool OutOfScope =
2585 !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut);
2586
2587 if (OutOfScope || ShouldPush) {
2588 // Sync to our current scope.
2589 EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
2590 bool ShouldPush = Def && EliminationStack.empty();
2591 if (ShouldPush) {
2592 EliminationStack.push_back(Def, MemberDFSIn, MemberDFSOut);
2593 }
2594 }
2595
2596 // Skip the Def's, we only want to eliminate on their uses. But mark
2597 // dominated defs as dead.
2598 if (Def) {
2599 // For anything in this case, what and how we value number
2600 // guarantees that any side-effets that would have occurred (ie
2601 // throwing, etc) can be proven to either still occur (because it's
2602 // dominated by something that has the same side-effects), or never
2603 // occur. Otherwise, we would not have been able to prove it value
2604 // equivalent to something else. For these things, we can just mark
2605 // it all dead. Note that this is different from the "ProbablyDead"
2606 // set, which may not be dominated by anything, and thus, are only
2607 // easy to prove dead if they are also side-effect free.
2608 if (!EliminationStack.empty() && Def != EliminationStack.back() &&
2609 isa<Instruction>(Def))
2610 markInstructionForDeletion(cast<Instruction>(Def));
2611 continue;
2612 }
2613 // At this point, we know it is a Use we are trying to possibly
2614 // replace.
2615
2616 assert(isa<Instruction>(U->get()) &&((isa<Instruction>(U->get()) && "Current def should have been an instruction"
) ? static_cast<void> (0) : __assert_fail ("isa<Instruction>(U->get()) && \"Current def should have been an instruction\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2617, __PRETTY_FUNCTION__))
2617 "Current def should have been an instruction")((isa<Instruction>(U->get()) && "Current def should have been an instruction"
) ? static_cast<void> (0) : __assert_fail ("isa<Instruction>(U->get()) && \"Current def should have been an instruction\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2617, __PRETTY_FUNCTION__))
;
2618 assert(isa<Instruction>(U->getUser()) &&((isa<Instruction>(U->getUser()) && "Current user should have been an instruction"
) ? static_cast<void> (0) : __assert_fail ("isa<Instruction>(U->getUser()) && \"Current user should have been an instruction\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2619, __PRETTY_FUNCTION__))
2619 "Current user should have been an instruction")((isa<Instruction>(U->getUser()) && "Current user should have been an instruction"
) ? static_cast<void> (0) : __assert_fail ("isa<Instruction>(U->getUser()) && \"Current user should have been an instruction\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2619, __PRETTY_FUNCTION__))
;
2620
2621 // If the thing we are replacing into is already marked to be dead,
2622 // this use is dead. Note that this is true regardless of whether
2623 // we have anything dominating the use or not. We do this here
2624 // because we are already walking all the uses anyway.
2625 Instruction *InstUse = cast<Instruction>(U->getUser());
2626 if (InstructionsToErase.count(InstUse)) {
2627 auto &UseCount = UseCounts[U->get()];
2628 if (--UseCount == 0) {
2629 ProbablyDead.insert(cast<Instruction>(U->get()));
2630 }
2631 }
2632
2633 // If we get to this point, and the stack is empty we must have a use
2634 // with nothing we can use to eliminate this use, so just skip it.
2635 if (EliminationStack.empty())
2636 continue;
2637
2638 Value *DominatingLeader = EliminationStack.back();
2639
2640 // Don't replace our existing users with ourselves.
2641 if (U->get() == DominatingLeader)
2642 continue;
2643 DEBUG(dbgs() << "Found replacement " << *DominatingLeader << " for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Found replacement " << *
DominatingLeader << " for " << *U->get() <<
" in " << *(U->getUser()) << "\n"; } } while (
false)
2644 << *U->get() << " in " << *(U->getUser()) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Found replacement " << *
DominatingLeader << " for " << *U->get() <<
" in " << *(U->getUser()) << "\n"; } } while (
false)
;
2645
2646 // If we replaced something in an instruction, handle the patching of
2647 // metadata. Skip this if we are replacing predicateinfo with its
2648 // original operand, as we already know we can just drop it.
2649 auto *ReplacedInst = cast<Instruction>(U->get());
2650 auto *PI = PredInfo->getPredicateInfoFor(ReplacedInst);
2651 if (!PI || DominatingLeader != PI->OriginalOp)
2652 patchReplacementInstruction(ReplacedInst, DominatingLeader);
2653 U->set(DominatingLeader);
2654 // This is now a use of the dominating leader, which means if the
2655 // dominating leader was dead, it's now live!
2656 auto &LeaderUseCount = UseCounts[DominatingLeader];
2657 // It's about to be alive again.
2658 if (LeaderUseCount == 0 && isa<Instruction>(DominatingLeader))
2659 ProbablyDead.erase(cast<Instruction>(DominatingLeader));
2660 ++LeaderUseCount;
2661 AnythingReplaced = true;
2662 }
2663 }
2664 }
2665
2666 // At this point, anything still in the ProbablyDead set is actually dead if
2667 // would be trivially dead.
2668 for (auto *I : ProbablyDead)
2669 if (wouldInstructionBeTriviallyDead(I))
2670 markInstructionForDeletion(I);
2671
2672 // Cleanup the congruence class.
2673 SmallPtrSet<Value *, 4> MembersLeft;
2674 for (Value *Member : CC->Members) {
2675 if (Member->getType()->isVoidTy()) {
2676 MembersLeft.insert(Member);
2677 continue;
2678 }
2679
2680 MembersLeft.insert(Member);
2681 }
2682 CC->Members.swap(MembersLeft);
2683
2684 // If we have possible dead stores to look at, try to eliminate them.
2685 if (CC->StoreCount > 0) {
2686 convertClassToLoadsAndStores(CC->Members, PossibleDeadStores);
2687 std::sort(PossibleDeadStores.begin(), PossibleDeadStores.end());
2688 ValueDFSStack EliminationStack;
2689 for (auto &VD : PossibleDeadStores) {
2690 int MemberDFSIn = VD.DFSIn;
2691 int MemberDFSOut = VD.DFSOut;
2692 Instruction *Member = cast<Instruction>(VD.Def);
2693 if (EliminationStack.empty() ||
2694 !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut)) {
2695 // Sync to our current scope.
2696 EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
2697 if (EliminationStack.empty()) {
2698 EliminationStack.push_back(Member, MemberDFSIn, MemberDFSOut);
2699 continue;
2700 }
2701 }
2702 // We already did load elimination, so nothing to do here.
2703 if (isa<LoadInst>(Member))
2704 continue;
2705 assert(!EliminationStack.empty())((!EliminationStack.empty()) ? static_cast<void> (0) : __assert_fail
("!EliminationStack.empty()", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2705, __PRETTY_FUNCTION__))
;
2706 Instruction *Leader = cast<Instruction>(EliminationStack.back());
2707 (void)Leader;
2708 assert(DT->dominates(Leader->getParent(), Member->getParent()))((DT->dominates(Leader->getParent(), Member->getParent
())) ? static_cast<void> (0) : __assert_fail ("DT->dominates(Leader->getParent(), Member->getParent())"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/lib/Transforms/Scalar/NewGVN.cpp"
, 2708, __PRETTY_FUNCTION__))
;
2709 // Member is dominater by Leader, and thus dead
2710 DEBUG(dbgs() << "Marking dead store " << *Memberdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Marking dead store " << *
Member << " that is dominated by " << *Leader <<
"\n"; } } while (false)
2711 << " that is dominated by " << *Leader << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("newgvn")) { dbgs() << "Marking dead store " << *
Member << " that is dominated by " << *Leader <<
"\n"; } } while (false)
;
2712 markInstructionForDeletion(Member);
2713 CC->Members.erase(Member);
2714 ++NumGVNDeadStores;
2715 }
2716 }
2717 }
2718
2719 return AnythingReplaced;
2720}
2721
2722// This function provides global ranking of operations so that we can place them
2723// in a canonical order. Note that rank alone is not necessarily enough for a
2724// complete ordering, as constants all have the same rank. However, generally,
2725// we will simplify an operation with all constants so that it doesn't matter
2726// what order they appear in.
2727unsigned int NewGVN::getRank(const Value *V) const {
2728 // Prefer undef to anything else
2729 if (isa<UndefValue>(V))
2730 return 0;
2731 if (isa<Constant>(V))
2732 return 1;
2733 else if (auto *A = dyn_cast<Argument>(V))
2734 return 2 + A->getArgNo();
2735
2736 // Need to shift the instruction DFS by number of arguments + 3 to account for
2737 // the constant and argument ranking above.
2738 unsigned Result = InstrDFS.lookup(V);
2739 if (Result > 0)
2740 return 3 + NumFuncArgs + Result;
2741 // Unreachable or something else, just return a really large number.
2742 return ~0;
2743}
2744
2745// This is a function that says whether two commutative operations should
2746// have their order swapped when canonicalizing.
2747bool NewGVN::shouldSwapOperands(const Value *A, const Value *B) const {
2748 // Because we only care about a total ordering, and don't rewrite expressions
2749 // in this order, we order by rank, which will give a strict weak ordering to
2750 // everything but constants, and then we order by pointer address.
2751 return std::make_pair(getRank(A), A) > std::make_pair(getRank(B), B);
2752}
2753
2754class NewGVNLegacyPass : public FunctionPass {
2755public:
2756 static char ID; // Pass identification, replacement for typeid.
2757 NewGVNLegacyPass() : FunctionPass(ID) {
2758 initializeNewGVNLegacyPassPass(*PassRegistry::getPassRegistry());
2759 }
2760 bool runOnFunction(Function &F) override;
2761
2762private:
2763 void getAnalysisUsage(AnalysisUsage &AU) const override {
2764 AU.addRequired<AssumptionCacheTracker>();
2765 AU.addRequired<DominatorTreeWrapperPass>();
2766 AU.addRequired<TargetLibraryInfoWrapperPass>();
2767 AU.addRequired<MemorySSAWrapperPass>();
2768 AU.addRequired<AAResultsWrapperPass>();
2769 AU.addPreserved<DominatorTreeWrapperPass>();
2770 AU.addPreserved<GlobalsAAWrapperPass>();
2771 }
2772};
2773
2774bool NewGVNLegacyPass::runOnFunction(Function &F) {
2775 if (skipFunction(F))
2776 return false;
2777 return NewGVN(F, &getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
2778 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
2779 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
2780 &getAnalysis<AAResultsWrapperPass>().getAAResults(),
2781 &getAnalysis<MemorySSAWrapperPass>().getMSSA(),
2782 F.getParent()->getDataLayout())
2783 .runGVN();
2784}
2785
2786INITIALIZE_PASS_BEGIN(NewGVNLegacyPass, "newgvn", "Global Value Numbering",static void *initializeNewGVNLegacyPassPassOnce(PassRegistry &
Registry) {
2787 false, false)static void *initializeNewGVNLegacyPassPassOnce(PassRegistry &
Registry) {
2788INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)initializeAssumptionCacheTrackerPass(Registry);
2789INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)initializeMemorySSAWrapperPassPass(Registry);
2790INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)initializeDominatorTreeWrapperPassPass(Registry);
2791INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry);
2792INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)initializeAAResultsWrapperPassPass(Registry);
2793INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)initializeGlobalsAAWrapperPassPass(Registry);
2794INITIALIZE_PASS_END(NewGVNLegacyPass, "newgvn", "Global Value Numbering", false,PassInfo *PI = new PassInfo( "Global Value Numbering", "newgvn"
, &NewGVNLegacyPass::ID, PassInfo::NormalCtor_t(callDefaultCtor
<NewGVNLegacyPass>), false, false); Registry.registerPass
(*PI, true); return PI; } static llvm::once_flag InitializeNewGVNLegacyPassPassFlag
; void llvm::initializeNewGVNLegacyPassPass(PassRegistry &
Registry) { llvm::call_once(InitializeNewGVNLegacyPassPassFlag
, initializeNewGVNLegacyPassPassOnce, std::ref(Registry)); }
2795 false)PassInfo *PI = new PassInfo( "Global Value Numbering", "newgvn"
, &NewGVNLegacyPass::ID, PassInfo::NormalCtor_t(callDefaultCtor
<NewGVNLegacyPass>), false, false); Registry.registerPass
(*PI, true); return PI; } static llvm::once_flag InitializeNewGVNLegacyPassPassFlag
; void llvm::initializeNewGVNLegacyPassPass(PassRegistry &
Registry) { llvm::call_once(InitializeNewGVNLegacyPassPassFlag
, initializeNewGVNLegacyPassPassOnce, std::ref(Registry)); }
2796
2797char NewGVNLegacyPass::ID = 0;
2798
2799// createGVNPass - The public interface to this file.
2800FunctionPass *llvm::createNewGVNPass() { return new NewGVNLegacyPass(); }
2801
2802PreservedAnalyses NewGVNPass::run(Function &F, AnalysisManager<Function> &AM) {
2803 // Apparently the order in which we get these results matter for
2804 // the old GVN (see Chandler's comment in GVN.cpp). I'll keep
2805 // the same order here, just in case.
2806 auto &AC = AM.getResult<AssumptionAnalysis>(F);
2807 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
2808 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
2809 auto &AA = AM.getResult<AAManager>(F);
2810 auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
2811 bool Changed =
2812 NewGVN(F, &DT, &AC, &TLI, &AA, &MSSA, F.getParent()->getDataLayout())
2813 .runGVN();
2814 if (!Changed)
2815 return PreservedAnalyses::all();
2816 PreservedAnalyses PA;
2817 PA.preserve<DominatorTreeAnalysis>();
2818 PA.preserve<GlobalsAA>();
2819 return PA;
2820}