LLVM 22.0.0git
NewGVN.cpp
Go to the documentation of this file.
1//===- NewGVN.cpp - Global Value Numbering Pass ---------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
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 vast majority of complexity and code
34/// in this file is devoted to tracking what value numbers could change for what
35/// instructions when various things happen. The rest of the algorithm is
36/// devoted to performing symbolic evaluation, forward propagation, and
37/// simplification of operations based on the value numbers deduced so far
38///
39/// In order to make the GVN mostly-complete, we use a technique derived from
40/// "Detection of Redundant Expressions: A Complete and Polynomial-time
41/// Algorithm in SSA" by R.R. Pai. The source of incompleteness in most SSA
42/// based GVN algorithms is related to their inability to detect equivalence
43/// between phi of ops (IE phi(a+b, c+d)) and op of phis (phi(a,c) + phi(b, d)).
44/// We resolve this issue by generating the equivalent "phi of ops" form for
45/// each op of phis we see, in a way that only takes polynomial time to resolve.
46///
47/// We also do not perform elimination by using any published algorithm. All
48/// published algorithms are O(Instructions). Instead, we use a technique that
49/// is O(number of operations with the same value number), enabling us to skip
50/// trying to eliminate things that have unique value numbers.
51//
52//===----------------------------------------------------------------------===//
53
55#include "llvm/ADT/ArrayRef.h"
56#include "llvm/ADT/BitVector.h"
57#include "llvm/ADT/DenseMap.h"
59#include "llvm/ADT/DenseSet.h"
62#include "llvm/ADT/Hashing.h"
69#include "llvm/ADT/Statistic.h"
81#include "llvm/IR/Argument.h"
82#include "llvm/IR/BasicBlock.h"
83#include "llvm/IR/Constant.h"
84#include "llvm/IR/Constants.h"
85#include "llvm/IR/DebugInfo.h"
86#include "llvm/IR/Dominators.h"
87#include "llvm/IR/Function.h"
88#include "llvm/IR/InstrTypes.h"
89#include "llvm/IR/Instruction.h"
93#include "llvm/IR/Type.h"
94#include "llvm/IR/Use.h"
95#include "llvm/IR/User.h"
96#include "llvm/IR/Value.h"
101#include "llvm/Support/Debug.h"
111#include <algorithm>
112#include <cassert>
113#include <cstdint>
114#include <iterator>
115#include <map>
116#include <memory>
117#include <set>
118#include <string>
119#include <tuple>
120#include <utility>
121#include <vector>
122
123using namespace llvm;
124using namespace llvm::GVNExpression;
125using namespace llvm::VNCoercion;
126using namespace llvm::PatternMatch;
127
128#define DEBUG_TYPE "newgvn"
129
130STATISTIC(NumGVNInstrDeleted, "Number of instructions deleted");
131STATISTIC(NumGVNBlocksDeleted, "Number of blocks deleted");
132STATISTIC(NumGVNOpsSimplified, "Number of Expressions simplified");
133STATISTIC(NumGVNPhisAllSame, "Number of PHIs whos arguments are all the same");
134STATISTIC(NumGVNMaxIterations,
135 "Maximum Number of iterations it took to converge GVN");
136STATISTIC(NumGVNLeaderChanges, "Number of leader changes");
137STATISTIC(NumGVNSortedLeaderChanges, "Number of sorted leader changes");
138STATISTIC(NumGVNAvoidedSortedLeaderChanges,
139 "Number of avoided sorted leader changes");
140STATISTIC(NumGVNDeadStores, "Number of redundant/dead stores eliminated");
141STATISTIC(NumGVNPHIOfOpsCreated, "Number of PHI of ops created");
142STATISTIC(NumGVNPHIOfOpsEliminations,
143 "Number of things eliminated using PHI of ops");
144DEBUG_COUNTER(VNCounter, "newgvn-vn",
145 "Controls which instructions are value numbered");
146DEBUG_COUNTER(PHIOfOpsCounter, "newgvn-phi",
147 "Controls which instructions we create phi of ops for");
148// Currently store defining access refinement is too slow due to basicaa being
149// egregiously slow. This flag lets us keep it working while we work on this
150// issue.
151static cl::opt<bool> EnableStoreRefinement("enable-store-refinement",
152 cl::init(false), cl::Hidden);
153
154/// Currently, the generation "phi of ops" can result in correctness issues.
155static cl::opt<bool> EnablePhiOfOps("enable-phi-of-ops", cl::init(true),
156 cl::Hidden);
157
158//===----------------------------------------------------------------------===//
159// GVN Pass
160//===----------------------------------------------------------------------===//
161
162// Anchor methods.
163namespace llvm {
164namespace GVNExpression {
165
166Expression::~Expression() = default;
173
174} // end namespace GVNExpression
175} // end namespace llvm
176
177namespace {
178
179// Tarjan's SCC finding algorithm with Nuutila's improvements
180// SCCIterator is actually fairly complex for the simple thing we want.
181// It also wants to hand us SCC's that are unrelated to the phi node we ask
182// about, and have us process them there or risk redoing work.
183// Graph traits over a filter iterator also doesn't work that well here.
184// This SCC finder is specialized to walk use-def chains, and only follows
185// instructions,
186// not generic values (arguments, etc).
187struct TarjanSCC {
188 TarjanSCC() : Components(1) {}
189
190 void Start(const Instruction *Start) {
191 if (Root.lookup(Start) == 0)
192 FindSCC(Start);
193 }
194
195 const SmallPtrSetImpl<const Value *> &getComponentFor(const Value *V) const {
196 unsigned ComponentID = ValueToComponent.lookup(V);
197
198 assert(ComponentID > 0 &&
199 "Asking for a component for a value we never processed");
200 return Components[ComponentID];
201 }
202
203private:
204 void FindSCC(const Instruction *I) {
205 Root[I] = ++DFSNum;
206 // Store the DFS Number we had before it possibly gets incremented.
207 unsigned int OurDFS = DFSNum;
208 for (const auto &Op : I->operands()) {
209 if (auto *InstOp = dyn_cast<Instruction>(Op)) {
210 if (Root.lookup(Op) == 0)
211 FindSCC(InstOp);
212 if (!InComponent.count(Op))
213 Root[I] = std::min(Root.lookup(I), Root.lookup(Op));
214 }
215 }
216 // See if we really were the root of a component, by seeing if we still have
217 // our DFSNumber. If we do, we are the root of the component, and we have
218 // completed a component. If we do not, we are not the root of a component,
219 // and belong on the component stack.
220 if (Root.lookup(I) == OurDFS) {
221 unsigned ComponentID = Components.size();
222 Components.resize(Components.size() + 1);
223 auto &Component = Components.back();
224 Component.insert(I);
225 LLVM_DEBUG(dbgs() << "Component root is " << *I << "\n");
226 InComponent.insert(I);
227 ValueToComponent[I] = ComponentID;
228 // Pop a component off the stack and label it.
229 while (!Stack.empty() && Root.lookup(Stack.back()) >= OurDFS) {
230 auto *Member = Stack.back();
231 LLVM_DEBUG(dbgs() << "Component member is " << *Member << "\n");
232 Component.insert(Member);
233 InComponent.insert(Member);
234 ValueToComponent[Member] = ComponentID;
235 Stack.pop_back();
236 }
237 } else {
238 // Part of a component, push to stack
239 Stack.push_back(I);
240 }
241 }
242
243 unsigned int DFSNum = 1;
244 SmallPtrSet<const Value *, 8> InComponent;
245 DenseMap<const Value *, unsigned int> Root;
246 SmallVector<const Value *, 8> Stack;
247
248 // Store the components as vector of ptr sets, because we need the topo order
249 // of SCC's, but not individual member order
251
252 DenseMap<const Value *, unsigned> ValueToComponent;
253};
254
255// Congruence classes represent the set of expressions/instructions
256// that are all the same *during some scope in the function*.
257// That is, because of the way we perform equality propagation, and
258// because of memory value numbering, it is not correct to assume
259// you can willy-nilly replace any member with any other at any
260// point in the function.
261//
262// For any Value in the Member set, it is valid to replace any dominated member
263// with that Value.
264//
265// Every congruence class has a leader, and the leader is used to symbolize
266// instructions in a canonical way (IE every operand of an instruction that is a
267// member of the same congruence class will always be replaced with leader
268// during symbolization). To simplify symbolization, we keep the leader as a
269// constant if class can be proved to be a constant value. Otherwise, the
270// leader is the member of the value set with the smallest DFS number. Each
271// congruence class also has a defining expression, though the expression may be
272// null. If it exists, it can be used for forward propagation and reassociation
273// of values.
274
275// For memory, we also track a representative MemoryAccess, and a set of memory
276// members for MemoryPhis (which have no real instructions). Note that for
277// memory, it seems tempting to try to split the memory members into a
278// MemoryCongruenceClass or something. Unfortunately, this does not work
279// easily. The value numbering of a given memory expression depends on the
280// leader of the memory congruence class, and the leader of memory congruence
281// class depends on the value numbering of a given memory expression. This
282// leads to wasted propagation, and in some cases, missed optimization. For
283// example: If we had value numbered two stores together before, but now do not,
284// we move them to a new value congruence class. This in turn will move at one
285// of the memorydefs to a new memory congruence class. Which in turn, affects
286// the value numbering of the stores we just value numbered (because the memory
287// congruence class is part of the value number). So while theoretically
288// possible to split them up, it turns out to be *incredibly* complicated to get
289// it to work right, because of the interdependency. While structurally
290// slightly messier, it is algorithmically much simpler and faster to do what we
291// do here, and track them both at once in the same class.
292// Note: The default iterators for this class iterate over values
293class CongruenceClass {
294public:
295 using MemberType = Value;
296 using MemberSet = SmallPtrSet<MemberType *, 4>;
297 using MemoryMemberType = MemoryPhi;
298 using MemoryMemberSet = SmallPtrSet<const MemoryMemberType *, 2>;
299
300 explicit CongruenceClass(unsigned ID) : ID(ID) {}
301 CongruenceClass(unsigned ID, std::pair<Value *, unsigned int> Leader,
302 const Expression *E)
303 : ID(ID), RepLeader(Leader), DefiningExpr(E) {}
304
305 unsigned getID() const { return ID; }
306
307 // True if this class has no members left. This is mainly used for assertion
308 // purposes, and for skipping empty classes.
309 bool isDead() const {
310 // If it's both dead from a value perspective, and dead from a memory
311 // perspective, it's really dead.
312 return empty() && memory_empty();
313 }
314
315 // Leader functions
316 Value *getLeader() const { return RepLeader.first; }
317 void setLeader(std::pair<Value *, unsigned int> Leader) {
318 RepLeader = Leader;
319 }
320 const std::pair<Value *, unsigned int> &getNextLeader() const {
321 return NextLeader;
322 }
323 void resetNextLeader() { NextLeader = {nullptr, ~0}; }
324 bool addPossibleLeader(std::pair<Value *, unsigned int> LeaderPair) {
325 if (LeaderPair.second < RepLeader.second) {
326 NextLeader = RepLeader;
327 RepLeader = LeaderPair;
328 return true;
329 } else if (LeaderPair.second < NextLeader.second) {
330 NextLeader = LeaderPair;
331 }
332 return false;
333 }
334
335 Value *getStoredValue() const { return RepStoredValue; }
336 void setStoredValue(Value *Leader) { RepStoredValue = Leader; }
337 const MemoryAccess *getMemoryLeader() const { return RepMemoryAccess; }
338 void setMemoryLeader(const MemoryAccess *Leader) { RepMemoryAccess = Leader; }
339
340 // Forward propagation info
341 const Expression *getDefiningExpr() const { return DefiningExpr; }
342
343 // Value member set
344 bool empty() const { return Members.empty(); }
345 unsigned size() const { return Members.size(); }
346 MemberSet::const_iterator begin() const { return Members.begin(); }
347 MemberSet::const_iterator end() const { return Members.end(); }
348 void insert(MemberType *M) { Members.insert(M); }
349 void erase(MemberType *M) { Members.erase(M); }
350 void swap(MemberSet &Other) { Members.swap(Other); }
351
352 // Memory member set
353 bool memory_empty() const { return MemoryMembers.empty(); }
354 unsigned memory_size() const { return MemoryMembers.size(); }
355 MemoryMemberSet::const_iterator memory_begin() const {
356 return MemoryMembers.begin();
357 }
358 MemoryMemberSet::const_iterator memory_end() const {
359 return MemoryMembers.end();
360 }
362 return make_range(memory_begin(), memory_end());
363 }
364
365 void memory_insert(const MemoryMemberType *M) { MemoryMembers.insert(M); }
366 void memory_erase(const MemoryMemberType *M) { MemoryMembers.erase(M); }
367
368 // Store count
369 unsigned getStoreCount() const { return StoreCount; }
370 void incStoreCount() { ++StoreCount; }
371 void decStoreCount() {
372 assert(StoreCount != 0 && "Store count went negative");
373 --StoreCount;
374 }
375
376 // True if this class has no memory members.
377 bool definesNoMemory() const { return StoreCount == 0 && memory_empty(); }
378
379 // Return true if two congruence classes are equivalent to each other. This
380 // means that every field but the ID number and the dead field are equivalent.
381 bool isEquivalentTo(const CongruenceClass *Other) const {
382 if (!Other)
383 return false;
384 if (this == Other)
385 return true;
386
387 if (std::tie(StoreCount, RepLeader, RepStoredValue, RepMemoryAccess) !=
388 std::tie(Other->StoreCount, Other->RepLeader, Other->RepStoredValue,
389 Other->RepMemoryAccess))
390 return false;
391 if (DefiningExpr != Other->DefiningExpr)
392 if (!DefiningExpr || !Other->DefiningExpr ||
393 *DefiningExpr != *Other->DefiningExpr)
394 return false;
395
396 if (Members.size() != Other->Members.size())
397 return false;
398
399 return llvm::set_is_subset(Members, Other->Members);
400 }
401
402private:
403 unsigned ID;
404
405 // Representative leader and its corresponding RPO number.
406 // The leader must have the lowest RPO number.
407 std::pair<Value *, unsigned int> RepLeader = {nullptr, ~0U};
408
409 // The most dominating leader after our current leader (given by the RPO
410 // number), because the member set is not sorted and is expensive to keep
411 // sorted all the time.
412 std::pair<Value *, unsigned int> NextLeader = {nullptr, ~0U};
413
414 // If this is represented by a store, the value of the store.
415 Value *RepStoredValue = nullptr;
416
417 // If this class contains MemoryDefs or MemoryPhis, this is the leading memory
418 // access.
419 const MemoryAccess *RepMemoryAccess = nullptr;
420
421 // Defining Expression.
422 const Expression *DefiningExpr = nullptr;
423
424 // Actual members of this class.
425 MemberSet Members;
426
427 // This is the set of MemoryPhis that exist in the class. MemoryDefs and
428 // MemoryUses have real instructions representing them, so we only need to
429 // track MemoryPhis here.
430 MemoryMemberSet MemoryMembers;
431
432 // Number of stores in this congruence class.
433 // This is used so we can detect store equivalence changes properly.
434 int StoreCount = 0;
435};
436
437struct ExactEqualsExpression {
438 const Expression &E;
439
440 explicit ExactEqualsExpression(const Expression &E) : E(E) {}
441
442 hash_code getComputedHash() const { return E.getComputedHash(); }
443
444 bool operator==(const Expression &Other) const {
445 return E.exactlyEquals(Other);
446 }
447};
448} // end anonymous namespace
449
450template <> struct llvm::DenseMapInfo<const Expression *> {
451 static const Expression *getEmptyKey() {
452 auto Val = static_cast<uintptr_t>(-1);
453 Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
454 return reinterpret_cast<const Expression *>(Val);
455 }
456
457 static const Expression *getTombstoneKey() {
458 auto Val = static_cast<uintptr_t>(~1U);
459 Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
460 return reinterpret_cast<const Expression *>(Val);
461 }
462
463 static unsigned getHashValue(const Expression *E) {
464 return E->getComputedHash();
465 }
466
467 static unsigned getHashValue(const ExactEqualsExpression &E) {
468 return E.getComputedHash();
469 }
470
471 static bool isEqual(const ExactEqualsExpression &LHS, const Expression *RHS) {
472 if (RHS == getTombstoneKey() || RHS == getEmptyKey())
473 return false;
474 return LHS == *RHS;
475 }
476
477 static bool isEqual(const Expression *LHS, const Expression *RHS) {
478 if (LHS == RHS)
479 return true;
480 if (LHS == getTombstoneKey() || RHS == getTombstoneKey() ||
481 LHS == getEmptyKey() || RHS == getEmptyKey())
482 return false;
483 // Compare hashes before equality. This is *not* what the hashtable does,
484 // since it is computing it modulo the number of buckets, whereas we are
485 // using the full hash keyspace. Since the hashes are precomputed, this
486 // check is *much* faster than equality.
487 if (LHS->getComputedHash() != RHS->getComputedHash())
488 return false;
489 return *LHS == *RHS;
490 }
491};
492
493namespace {
494
495class NewGVN {
496 Function &F;
497 DominatorTree *DT = nullptr;
498 const TargetLibraryInfo *TLI = nullptr;
499 AliasAnalysis *AA = nullptr;
500 MemorySSA *MSSA = nullptr;
501 MemorySSAWalker *MSSAWalker = nullptr;
502 AssumptionCache *AC = nullptr;
503 const DataLayout &DL;
504
505 // These are the only two things the create* functions should have
506 // side-effects on due to allocating memory.
507 mutable BumpPtrAllocator ExpressionAllocator;
508 mutable ArrayRecycler<Value *> ArgRecycler;
509 mutable TarjanSCC SCCFinder;
510
511 std::unique_ptr<PredicateInfo> PredInfo;
512 const SimplifyQuery SQ;
513
514 // Number of function arguments, used by ranking
515 unsigned int NumFuncArgs = 0;
516
517 // RPOOrdering of basic blocks
519
520 // Congruence class info.
521
522 // This class is called INITIAL in the paper. It is the class everything
523 // startsout in, and represents any value. Being an optimistic analysis,
524 // anything in the TOP class has the value TOP, which is indeterminate and
525 // equivalent to everything.
526 CongruenceClass *TOPClass = nullptr;
527 std::vector<CongruenceClass *> CongruenceClasses;
528 unsigned NextCongruenceNum = 0;
529
530 // Value Mappings.
533
534 // Value PHI handling, used to make equivalence between phi(op, op) and
535 // op(phi, phi).
536 // These mappings just store various data that would normally be part of the
537 // IR.
539
540 // The cached results, in general, are only valid for the specific block where
541 // they were computed. The unsigned part of the key is a unique block
542 // identifier
543 DenseMap<std::pair<const Value *, unsigned>, bool> OpSafeForPHIOfOps;
544 unsigned CacheIdx;
545
546 // Map a temporary instruction we created to a parent block.
548
549 // Map between the already in-program instructions and the temporary phis we
550 // created that they are known equivalent to.
552
553 // In order to know when we should re-process instructions that have
554 // phi-of-ops, we track the set of expressions that they needed as
555 // leaders. When we discover new leaders for those expressions, we process the
556 // associated phi-of-op instructions again in case they have changed. The
557 // other way they may change is if they had leaders, and those leaders
558 // disappear. However, at the point they have leaders, there are uses of the
559 // relevant operands in the created phi node, and so they will get reprocessed
560 // through the normal user marking we perform.
563 ExpressionToPhiOfOps;
564
565 // Map from temporary operation to MemoryAccess.
567
568 // Set of all temporary instructions we created.
569 // Note: This will include instructions that were just created during value
570 // numbering. The way to test if something is using them is to check
571 // RealToTemp.
572 DenseSet<Instruction *> AllTempInstructions;
573
574 // This is the set of instructions to revisit on a reachability change. At
575 // the end of the main iteration loop it will contain at least all the phi of
576 // ops instructions that will be changed to phis, as well as regular phis.
577 // During the iteration loop, it may contain other things, such as phi of ops
578 // instructions that used edge reachability to reach a result, and so need to
579 // be revisited when the edge changes, independent of whether the phi they
580 // depended on changes.
581 DenseMap<BasicBlock *, SparseBitVector<>> RevisitOnReachabilityChange;
582
583 // Mapping from predicate info we used to the instructions we used it with.
584 // In order to correctly ensure propagation, we must keep track of what
585 // comparisons we used, so that when the values of the comparisons change, we
586 // propagate the information to the places we used the comparison.
588 PredicateToUsers;
589
590 // the same reasoning as PredicateToUsers. When we skip MemoryAccesses for
591 // stores, we no longer can rely solely on the def-use chains of MemorySSA.
593 MemoryToUsers;
594
595 // A table storing which memorydefs/phis represent a memory state provably
596 // equivalent to another memory state.
597 // We could use the congruence class machinery, but the MemoryAccess's are
598 // abstract memory states, so they can only ever be equivalent to each other,
599 // and not to constants, etc.
601
602 // We could, if we wanted, build MemoryPhiExpressions and
603 // MemoryVariableExpressions, etc, and value number them the same way we value
604 // number phi expressions. For the moment, this seems like overkill. They
605 // can only exist in one of three states: they can be TOP (equal to
606 // everything), Equivalent to something else, or unique. Because we do not
607 // create expressions for them, we need to simulate leader change not just
608 // when they change class, but when they change state. Note: We can do the
609 // same thing for phis, and avoid having phi expressions if we wanted, We
610 // should eventually unify in one direction or the other, so this is a little
611 // bit of an experiment in which turns out easier to maintain.
612 enum MemoryPhiState { MPS_Invalid, MPS_TOP, MPS_Equivalent, MPS_Unique };
613 DenseMap<const MemoryPhi *, MemoryPhiState> MemoryPhiState;
614
615 enum InstCycleState { ICS_Unknown, ICS_CycleFree, ICS_Cycle };
616 mutable DenseMap<const Instruction *, InstCycleState> InstCycleState;
617
618 // Expression to class mapping.
619 using ExpressionClassMap = DenseMap<const Expression *, CongruenceClass *>;
620 ExpressionClassMap ExpressionToClass;
621
622 // We have a single expression that represents currently DeadExpressions.
623 // For dead expressions we can prove will stay dead, we mark them with
624 // DFS number zero. However, it's possible in the case of phi nodes
625 // for us to assume/prove all arguments are dead during fixpointing.
626 // We use DeadExpression for that case.
627 DeadExpression *SingletonDeadExpression = nullptr;
628
629 // Which values have changed as a result of leader changes.
630 SmallPtrSet<Value *, 8> LeaderChanges;
631
632 // Reachability info.
633 using BlockEdge = BasicBlockEdge;
634 DenseSet<BlockEdge> ReachableEdges;
635 SmallPtrSet<const BasicBlock *, 8> ReachableBlocks;
636
637 // This is a bitvector because, on larger functions, we may have
638 // thousands of touched instructions at once (entire blocks,
639 // instructions with hundreds of uses, etc). Even with optimization
640 // for when we mark whole blocks as touched, when this was a
641 // SmallPtrSet or DenseSet, for some functions, we spent >20% of all
642 // the time in GVN just managing this list. The bitvector, on the
643 // other hand, efficiently supports test/set/clear of both
644 // individual and ranges, as well as "find next element" This
645 // enables us to use it as a worklist with essentially 0 cost.
646 BitVector TouchedInstructions;
647
648 DenseMap<const BasicBlock *, std::pair<unsigned, unsigned>> BlockInstRange;
649 mutable DenseMap<const BitCastInst *, const Value *> PredicateSwapChoice;
650
651#ifndef NDEBUG
652 // Debugging for how many times each block and instruction got processed.
653 DenseMap<const Value *, unsigned> ProcessedCount;
654#endif
655
656 // DFS info.
657 // This contains a mapping from Instructions to DFS numbers.
658 // The numbering starts at 1. An instruction with DFS number zero
659 // means that the instruction is dead.
660 DenseMap<const Value *, unsigned> InstrDFS;
661
662 // This contains the mapping DFS numbers to instructions.
663 SmallVector<Value *, 32> DFSToInstr;
664
665 // Deletion info.
666 SmallPtrSet<Instruction *, 8> InstructionsToErase;
667
668public:
669 NewGVN(Function &F, DominatorTree *DT, AssumptionCache *AC,
670 TargetLibraryInfo *TLI, AliasAnalysis *AA, MemorySSA *MSSA,
671 const DataLayout &DL)
672 : F(F), DT(DT), TLI(TLI), AA(AA), MSSA(MSSA), AC(AC), DL(DL),
673 // Reuse ExpressionAllocator for PredicateInfo as well.
674 PredInfo(
675 std::make_unique<PredicateInfo>(F, *DT, *AC, ExpressionAllocator)),
676 SQ(DL, TLI, DT, AC, /*CtxI=*/nullptr, /*UseInstrInfo=*/false,
677 /*CanUseUndef=*/false) {}
678
679 bool runGVN();
680
681private:
682 /// Helper struct return a Expression with an optional extra dependency.
683 struct ExprResult {
684 const Expression *Expr;
685 Value *ExtraDep;
686 const PredicateBase *PredDep;
687
688 ExprResult(const Expression *Expr, Value *ExtraDep = nullptr,
689 const PredicateBase *PredDep = nullptr)
690 : Expr(Expr), ExtraDep(ExtraDep), PredDep(PredDep) {}
691 ExprResult(const ExprResult &) = delete;
692 ExprResult(ExprResult &&Other)
693 : Expr(Other.Expr), ExtraDep(Other.ExtraDep), PredDep(Other.PredDep) {
694 Other.Expr = nullptr;
695 Other.ExtraDep = nullptr;
696 Other.PredDep = nullptr;
697 }
698 ExprResult &operator=(const ExprResult &Other) = delete;
699 ExprResult &operator=(ExprResult &&Other) = delete;
700
701 ~ExprResult() { assert(!ExtraDep && "unhandled ExtraDep"); }
702
703 operator bool() const { return Expr; }
704
705 static ExprResult none() { return {nullptr, nullptr, nullptr}; }
706 static ExprResult some(const Expression *Expr, Value *ExtraDep = nullptr) {
707 return {Expr, ExtraDep, nullptr};
708 }
709 static ExprResult some(const Expression *Expr,
710 const PredicateBase *PredDep) {
711 return {Expr, nullptr, PredDep};
712 }
713 static ExprResult some(const Expression *Expr, Value *ExtraDep,
714 const PredicateBase *PredDep) {
715 return {Expr, ExtraDep, PredDep};
716 }
717 };
718
719 // Expression handling.
720 ExprResult createExpression(Instruction *) const;
721 const Expression *createBinaryExpression(unsigned, Type *, Value *, Value *,
722 Instruction *) const;
723
724 // Our canonical form for phi arguments is a pair of incoming value, incoming
725 // basic block.
726 using ValPair = std::pair<Value *, BasicBlock *>;
727
728 PHIExpression *createPHIExpression(ArrayRef<ValPair>, const Instruction *,
729 BasicBlock *, bool &HasBackEdge,
730 bool &OriginalOpsConstant) const;
731 const DeadExpression *createDeadExpression() const;
732 const VariableExpression *createVariableExpression(Value *) const;
733 const ConstantExpression *createConstantExpression(Constant *) const;
734 const Expression *createVariableOrConstant(Value *V) const;
735 const UnknownExpression *createUnknownExpression(Instruction *) const;
736 const StoreExpression *createStoreExpression(StoreInst *,
737 const MemoryAccess *) const;
738 LoadExpression *createLoadExpression(Type *, Value *, LoadInst *,
739 const MemoryAccess *) const;
740 const CallExpression *createCallExpression(CallInst *,
741 const MemoryAccess *) const;
742 const AggregateValueExpression *
743 createAggregateValueExpression(Instruction *) const;
744 bool setBasicExpressionInfo(Instruction *, BasicExpression *) const;
745
746 // Congruence class handling.
747 CongruenceClass *createCongruenceClass(Value *Leader, const Expression *E) {
748 // Set RPO to 0 for values that are always available (constants and function
749 // args). These should always be made leader.
750 unsigned LeaderDFS = 0;
751
752 // If Leader is not specified, either we have a memory class or the leader
753 // will be set later. Otherwise, if Leader is an Instruction, set LeaderDFS
754 // to its RPO number.
755 if (!Leader)
756 LeaderDFS = ~0;
757 else if (auto *I = dyn_cast<Instruction>(Leader))
758 LeaderDFS = InstrToDFSNum(I);
759 auto *result =
760 new CongruenceClass(NextCongruenceNum++, {Leader, LeaderDFS}, E);
761 CongruenceClasses.emplace_back(result);
762 return result;
763 }
764
765 CongruenceClass *createMemoryClass(MemoryAccess *MA) {
766 auto *CC = createCongruenceClass(nullptr, nullptr);
767 CC->setMemoryLeader(MA);
768 return CC;
769 }
770
771 CongruenceClass *ensureLeaderOfMemoryClass(MemoryAccess *MA) {
772 auto *CC = getMemoryClass(MA);
773 if (CC->getMemoryLeader() != MA)
774 CC = createMemoryClass(MA);
775 return CC;
776 }
777
778 CongruenceClass *createSingletonCongruenceClass(Value *Member) {
779 CongruenceClass *CClass = createCongruenceClass(Member, nullptr);
780 CClass->insert(Member);
781 ValueToClass[Member] = CClass;
782 return CClass;
783 }
784
785 void initializeCongruenceClasses(Function &F);
786 const Expression *makePossiblePHIOfOps(Instruction *,
787 SmallPtrSetImpl<Value *> &);
788 Value *findLeaderForInst(Instruction *ValueOp,
789 SmallPtrSetImpl<Value *> &Visited,
790 MemoryAccess *MemAccess, Instruction *OrigInst,
791 BasicBlock *PredBB);
792 bool OpIsSafeForPHIOfOps(Value *Op, const BasicBlock *PHIBlock,
793 SmallPtrSetImpl<const Value *> &);
794 void addPhiOfOps(PHINode *Op, BasicBlock *BB, Instruction *ExistingValue);
795 void removePhiOfOps(Instruction *I, PHINode *PHITemp);
796
797 // Value number an Instruction or MemoryPhi.
798 void valueNumberMemoryPhi(MemoryPhi *);
799 void valueNumberInstruction(Instruction *);
800
801 // Symbolic evaluation.
802 ExprResult checkExprResults(Expression *, Instruction *, Value *) const;
803 ExprResult performSymbolicEvaluation(Instruction *,
804 SmallPtrSetImpl<Value *> &) const;
805 const Expression *performSymbolicLoadCoercion(Type *, Value *, LoadInst *,
806 Instruction *,
807 MemoryAccess *) const;
808 const Expression *performSymbolicLoadEvaluation(Instruction *) const;
809 const Expression *performSymbolicStoreEvaluation(Instruction *) const;
810 ExprResult performSymbolicCallEvaluation(Instruction *) const;
811 void sortPHIOps(MutableArrayRef<ValPair> Ops) const;
812 const Expression *performSymbolicPHIEvaluation(ArrayRef<ValPair>,
813 Instruction *I,
814 BasicBlock *PHIBlock) const;
815 const Expression *performSymbolicAggrValueEvaluation(Instruction *) const;
816 ExprResult performSymbolicCmpEvaluation(Instruction *) const;
817 ExprResult performSymbolicPredicateInfoEvaluation(BitCastInst *) const;
818
819 // Congruence finding.
820 bool someEquivalentDominates(const Instruction *, const Instruction *) const;
821 Value *lookupOperandLeader(Value *) const;
822 CongruenceClass *getClassForExpression(const Expression *E) const;
823 void performCongruenceFinding(Instruction *, const Expression *);
824 void moveValueToNewCongruenceClass(Instruction *, const Expression *,
825 CongruenceClass *, CongruenceClass *);
826 void moveMemoryToNewCongruenceClass(Instruction *, MemoryAccess *,
827 CongruenceClass *, CongruenceClass *);
828 Value *getNextValueLeader(CongruenceClass *) const;
829 const MemoryAccess *getNextMemoryLeader(CongruenceClass *) const;
830 bool setMemoryClass(const MemoryAccess *From, CongruenceClass *To);
831 CongruenceClass *getMemoryClass(const MemoryAccess *MA) const;
832 const MemoryAccess *lookupMemoryLeader(const MemoryAccess *) const;
833 bool isMemoryAccessTOP(const MemoryAccess *) const;
834
835 // Ranking
836 unsigned int getRank(const Value *) const;
837 bool shouldSwapOperands(const Value *, const Value *) const;
838 bool shouldSwapOperandsForPredicate(const Value *, const Value *,
839 const BitCastInst *I) const;
840
841 // Reachability handling.
842 void updateReachableEdge(BasicBlock *, BasicBlock *);
843 void processOutgoingEdges(Instruction *, BasicBlock *);
844 Value *findConditionEquivalence(Value *) const;
845
846 // Elimination.
847 struct ValueDFS;
848 void convertClassToDFSOrdered(const CongruenceClass &,
849 SmallVectorImpl<ValueDFS> &,
850 DenseMap<const Value *, unsigned int> &,
851 SmallPtrSetImpl<Instruction *> &) const;
852 void convertClassToLoadsAndStores(const CongruenceClass &,
853 SmallVectorImpl<ValueDFS> &) const;
854
855 bool eliminateInstructions(Function &);
856 void replaceInstruction(Instruction *, Value *);
857 void markInstructionForDeletion(Instruction *);
858 void deleteInstructionsInBlock(BasicBlock *);
859 Value *findPHIOfOpsLeader(const Expression *, const Instruction *,
860 const BasicBlock *) const;
861
862 // Various instruction touch utilities
863 template <typename Map, typename KeyType>
864 void touchAndErase(Map &, const KeyType &);
865 void markUsersTouched(Value *);
866 void markMemoryUsersTouched(const MemoryAccess *);
867 void markMemoryDefTouched(const MemoryAccess *);
868 void markPredicateUsersTouched(Instruction *);
869 void markValueLeaderChangeTouched(CongruenceClass *CC);
870 void markMemoryLeaderChangeTouched(CongruenceClass *CC);
871 void markPhiOfOpsChanged(const Expression *E);
872 void addMemoryUsers(const MemoryAccess *To, MemoryAccess *U) const;
873 void addAdditionalUsers(Value *To, Value *User) const;
874 void addAdditionalUsers(ExprResult &Res, Instruction *User) const;
875
876 // Main loop of value numbering
877 void iterateTouchedInstructions();
878
879 // Utilities.
880 void cleanupTables();
881 std::pair<unsigned, unsigned> assignDFSNumbers(BasicBlock *, unsigned);
882 void updateProcessedCount(const Value *V);
883 void verifyMemoryCongruency() const;
884 void verifyIterationSettled(Function &F);
885 void verifyStoreExpressions() const;
886 bool singleReachablePHIPath(SmallPtrSet<const MemoryAccess *, 8> &,
887 const MemoryAccess *, const MemoryAccess *) const;
888 BasicBlock *getBlockForValue(Value *V) const;
889 void deleteExpression(const Expression *E) const;
890 MemoryUseOrDef *getMemoryAccess(const Instruction *) const;
891 MemoryPhi *getMemoryAccess(const BasicBlock *) const;
892 template <class T, class Range> T *getMinDFSOfRange(const Range &) const;
893
894 unsigned InstrToDFSNum(const Value *V) const {
895 assert(isa<Instruction>(V) && "This should not be used for MemoryAccesses");
896 return InstrDFS.lookup(V);
897 }
898
899 unsigned InstrToDFSNum(const MemoryAccess *MA) const {
900 return MemoryToDFSNum(MA);
901 }
902
903 Value *InstrFromDFSNum(unsigned DFSNum) { return DFSToInstr[DFSNum]; }
904
905 // Given a MemoryAccess, return the relevant instruction DFS number. Note:
906 // This deliberately takes a value so it can be used with Use's, which will
907 // auto-convert to Value's but not to MemoryAccess's.
908 unsigned MemoryToDFSNum(const Value *MA) const {
910 "This should not be used with instructions");
911 return isa<MemoryUseOrDef>(MA)
912 ? InstrToDFSNum(cast<MemoryUseOrDef>(MA)->getMemoryInst())
913 : InstrDFS.lookup(MA);
914 }
915
916 bool isCycleFree(const Instruction *) const;
917 bool isBackedge(BasicBlock *From, BasicBlock *To) const;
918
919 // Debug counter info. When verifying, we have to reset the value numbering
920 // debug counter to the same state it started in to get the same results.
921 DebugCounter::CounterState StartingVNCounter;
922};
923
924} // end anonymous namespace
925
926template <typename T>
927static bool equalsLoadStoreHelper(const T &LHS, const Expression &RHS) {
929 return false;
930 return LHS.MemoryExpression::equals(RHS);
931}
932
934 return equalsLoadStoreHelper(*this, Other);
935}
936
938 if (!equalsLoadStoreHelper(*this, Other))
939 return false;
940 // Make sure that store vs store includes the value operand.
941 if (const auto *S = dyn_cast<StoreExpression>(&Other))
942 if (getStoredValue() != S->getStoredValue())
943 return false;
944 return true;
945}
946
949 return false;
950
951 if (auto *RHS = dyn_cast<CallExpression>(&Other))
952 return Call->getAttributes()
953 .intersectWith(Call->getContext(), RHS->Call->getAttributes())
954 .has_value();
955
956 return false;
957}
958
959// Determine if the edge From->To is a backedge
960bool NewGVN::isBackedge(BasicBlock *From, BasicBlock *To) const {
961 return From == To ||
962 RPOOrdering.lookup(DT->getNode(From)) >=
963 RPOOrdering.lookup(DT->getNode(To));
964}
965
966#ifndef NDEBUG
967static std::string getBlockName(const BasicBlock *B) {
969}
970#endif
971
972// Get a MemoryAccess for an instruction, fake or real.
973MemoryUseOrDef *NewGVN::getMemoryAccess(const Instruction *I) const {
974 auto *Result = MSSA->getMemoryAccess(I);
975 return Result ? Result : TempToMemory.lookup(I);
976}
977
978// Get a MemoryPhi for a basic block. These are all real.
979MemoryPhi *NewGVN::getMemoryAccess(const BasicBlock *BB) const {
980 return MSSA->getMemoryAccess(BB);
981}
982
983// Get the basic block from an instruction/memory value.
984BasicBlock *NewGVN::getBlockForValue(Value *V) const {
985 if (auto *I = dyn_cast<Instruction>(V)) {
986 auto *Parent = I->getParent();
987 if (Parent)
988 return Parent;
989 Parent = TempToBlock.lookup(V);
990 assert(Parent && "Every fake instruction should have a block");
991 return Parent;
992 }
993
994 auto *MP = dyn_cast<MemoryPhi>(V);
995 assert(MP && "Should have been an instruction or a MemoryPhi");
996 return MP->getBlock();
997}
998
999// Delete a definitely dead expression, so it can be reused by the expression
1000// allocator. Some of these are not in creation functions, so we have to accept
1001// const versions.
1002void NewGVN::deleteExpression(const Expression *E) const {
1004 auto *BE = cast<BasicExpression>(E);
1005 const_cast<BasicExpression *>(BE)->deallocateOperands(ArgRecycler);
1006 ExpressionAllocator.Deallocate(E);
1007}
1008
1009// If V is a predicateinfo copy, get the thing it is a copy of.
1010static Value *getCopyOf(const Value *V) {
1011 if (auto *BC = dyn_cast<BitCastInst>(V))
1012 if (BC->getType() == BC->getOperand(0)->getType())
1013 return BC->getOperand(0);
1014 return nullptr;
1015}
1016
1017// Return true if V is really PN, even accounting for predicateinfo copies.
1018static bool isCopyOfPHI(const Value *V, const PHINode *PN) {
1019 return V == PN || getCopyOf(V) == PN;
1020}
1021
1022static bool isCopyOfAPHI(const Value *V) {
1023 auto *CO = getCopyOf(V);
1024 return CO && isa<PHINode>(CO);
1025}
1026
1027// Sort PHI Operands into a canonical order. What we use here is an RPO
1028// order. The BlockInstRange numbers are generated in an RPO walk of the basic
1029// blocks.
1030void NewGVN::sortPHIOps(MutableArrayRef<ValPair> Ops) const {
1031 llvm::sort(Ops, [&](const ValPair &P1, const ValPair &P2) {
1032 return BlockInstRange.lookup(P1.second).first <
1033 BlockInstRange.lookup(P2.second).first;
1034 });
1035}
1036
1037// Return true if V is a value that will always be available (IE can
1038// be placed anywhere) in the function. We don't do globals here
1039// because they are often worse to put in place.
1040static bool alwaysAvailable(Value *V) {
1041 return isa<Constant>(V) || isa<Argument>(V);
1042}
1043
1044// Create a PHIExpression from an array of {incoming edge, value} pairs. I is
1045// the original instruction we are creating a PHIExpression for (but may not be
1046// a phi node). We require, as an invariant, that all the PHIOperands in the
1047// same block are sorted the same way. sortPHIOps will sort them into a
1048// canonical order.
1049PHIExpression *NewGVN::createPHIExpression(ArrayRef<ValPair> PHIOperands,
1050 const Instruction *I,
1051 BasicBlock *PHIBlock,
1052 bool &HasBackedge,
1053 bool &OriginalOpsConstant) const {
1054 unsigned NumOps = PHIOperands.size();
1055 auto *E = new (ExpressionAllocator) PHIExpression(NumOps, PHIBlock);
1056
1057 E->allocateOperands(ArgRecycler, ExpressionAllocator);
1058 E->setType(PHIOperands.begin()->first->getType());
1059 E->setOpcode(Instruction::PHI);
1060
1061 // Filter out unreachable phi operands.
1062 auto Filtered = make_filter_range(PHIOperands, [&](const ValPair &P) {
1063 auto *BB = P.second;
1064 if (auto *PHIOp = dyn_cast<PHINode>(I))
1065 if (isCopyOfPHI(P.first, PHIOp))
1066 return false;
1067 if (!ReachableEdges.count({BB, PHIBlock}))
1068 return false;
1069 // Things in TOPClass are equivalent to everything.
1070 if (ValueToClass.lookup(P.first) == TOPClass)
1071 return false;
1072 OriginalOpsConstant = OriginalOpsConstant && isa<Constant>(P.first);
1073 HasBackedge = HasBackedge || isBackedge(BB, PHIBlock);
1074 return lookupOperandLeader(P.first) != I;
1075 });
1076 llvm::transform(Filtered, op_inserter(E), [&](const ValPair &P) -> Value * {
1077 return lookupOperandLeader(P.first);
1078 });
1079 return E;
1080}
1081
1082// Set basic expression info (Arguments, type, opcode) for Expression
1083// E from Instruction I in block B.
1084bool NewGVN::setBasicExpressionInfo(Instruction *I, BasicExpression *E) const {
1085 bool AllConstant = true;
1086 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
1087 E->setType(GEP->getSourceElementType());
1088 else
1089 E->setType(I->getType());
1090 E->setOpcode(I->getOpcode());
1091 E->allocateOperands(ArgRecycler, ExpressionAllocator);
1092
1093 // Transform the operand array into an operand leader array, and keep track of
1094 // whether all members are constant.
1095 std::transform(I->op_begin(), I->op_end(), op_inserter(E), [&](Value *O) {
1096 auto Operand = lookupOperandLeader(O);
1097 AllConstant = AllConstant && isa<Constant>(Operand);
1098 return Operand;
1099 });
1100
1101 return AllConstant;
1102}
1103
1104const Expression *NewGVN::createBinaryExpression(unsigned Opcode, Type *T,
1105 Value *Arg1, Value *Arg2,
1106 Instruction *I) const {
1107 auto *E = new (ExpressionAllocator) BasicExpression(2);
1108 // TODO: we need to remove context instruction after Value Tracking
1109 // can run without context instruction
1110 const SimplifyQuery Q = SQ.getWithInstruction(I);
1111
1112 E->setType(T);
1113 E->setOpcode(Opcode);
1114 E->allocateOperands(ArgRecycler, ExpressionAllocator);
1115 if (Instruction::isCommutative(Opcode)) {
1116 // Ensure that commutative instructions that only differ by a permutation
1117 // of their operands get the same value number by sorting the operand value
1118 // numbers. Since all commutative instructions have two operands it is more
1119 // efficient to sort by hand rather than using, say, std::sort.
1120 if (shouldSwapOperands(Arg1, Arg2))
1121 std::swap(Arg1, Arg2);
1122 }
1123 E->op_push_back(lookupOperandLeader(Arg1));
1124 E->op_push_back(lookupOperandLeader(Arg2));
1125
1126 Value *V = simplifyBinOp(Opcode, E->getOperand(0), E->getOperand(1), Q);
1127 if (auto Simplified = checkExprResults(E, I, V)) {
1128 addAdditionalUsers(Simplified, I);
1129 return Simplified.Expr;
1130 }
1131 return E;
1132}
1133
1134// Take a Value returned by simplification of Expression E/Instruction
1135// I, and see if it resulted in a simpler expression. If so, return
1136// that expression.
1137NewGVN::ExprResult NewGVN::checkExprResults(Expression *E, Instruction *I,
1138 Value *V) const {
1139 if (!V)
1140 return ExprResult::none();
1141
1142 if (auto *C = dyn_cast<Constant>(V)) {
1143 if (I)
1144 LLVM_DEBUG(dbgs() << "Simplified " << *I << " to "
1145 << " constant " << *C << "\n");
1146 NumGVNOpsSimplified++;
1148 "We should always have had a basic expression here");
1149 deleteExpression(E);
1150 return ExprResult::some(createConstantExpression(C));
1151 } else if (isa<Argument>(V) || isa<GlobalVariable>(V)) {
1152 if (I)
1153 LLVM_DEBUG(dbgs() << "Simplified " << *I << " to "
1154 << " variable " << *V << "\n");
1155 deleteExpression(E);
1156 return ExprResult::some(createVariableExpression(V));
1157 }
1158
1159 CongruenceClass *CC = ValueToClass.lookup(V);
1160 if (CC) {
1161 if (CC->getLeader() && CC->getLeader() != I) {
1162 return ExprResult::some(createVariableOrConstant(CC->getLeader()), V);
1163 }
1164 if (CC->getDefiningExpr()) {
1165 if (I)
1166 LLVM_DEBUG(dbgs() << "Simplified " << *I << " to "
1167 << " expression " << *CC->getDefiningExpr() << "\n");
1168 NumGVNOpsSimplified++;
1169 deleteExpression(E);
1170 return ExprResult::some(CC->getDefiningExpr(), V);
1171 }
1172 }
1173
1174 return ExprResult::none();
1175}
1176
1177// Create a value expression from the instruction I, replacing operands with
1178// their leaders.
1179
1180NewGVN::ExprResult NewGVN::createExpression(Instruction *I) const {
1181 auto *E = new (ExpressionAllocator) BasicExpression(I->getNumOperands());
1182 // TODO: we need to remove context instruction after Value Tracking
1183 // can run without context instruction
1184 const SimplifyQuery Q = SQ.getWithInstruction(I);
1185
1186 bool AllConstant = setBasicExpressionInfo(I, E);
1187
1188 if (I->isCommutative()) {
1189 // Ensure that commutative instructions that only differ by a permutation
1190 // of their operands get the same value number by sorting the operand value
1191 // numbers. Since all commutative instructions have two operands it is more
1192 // efficient to sort by hand rather than using, say, std::sort.
1193 assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!");
1194 if (shouldSwapOperands(E->getOperand(0), E->getOperand(1)))
1195 E->swapOperands(0, 1);
1196 }
1197 // Perform simplification.
1198 if (auto *CI = dyn_cast<CmpInst>(I)) {
1199 // Sort the operand value numbers so x<y and y>x get the same value
1200 // number.
1201 CmpInst::Predicate Predicate = CI->getPredicate();
1202 if (shouldSwapOperands(E->getOperand(0), E->getOperand(1))) {
1203 E->swapOperands(0, 1);
1205 }
1206 E->setOpcode((CI->getOpcode() << 8) | Predicate);
1207 // TODO: 25% of our time is spent in simplifyCmpInst with pointer operands
1208 assert(I->getOperand(0)->getType() == I->getOperand(1)->getType() &&
1209 "Wrong types on cmp instruction");
1210 assert((E->getOperand(0)->getType() == I->getOperand(0)->getType() &&
1211 E->getOperand(1)->getType() == I->getOperand(1)->getType()));
1212 Value *V =
1213 simplifyCmpInst(Predicate, E->getOperand(0), E->getOperand(1), Q);
1214 if (auto Simplified = checkExprResults(E, I, V))
1215 return Simplified;
1216 } else if (isa<SelectInst>(I)) {
1217 if (isa<Constant>(E->getOperand(0)) ||
1218 E->getOperand(1) == E->getOperand(2)) {
1219 assert(E->getOperand(1)->getType() == I->getOperand(1)->getType() &&
1220 E->getOperand(2)->getType() == I->getOperand(2)->getType());
1221 Value *V = simplifySelectInst(E->getOperand(0), E->getOperand(1),
1222 E->getOperand(2), Q);
1223 if (auto Simplified = checkExprResults(E, I, V))
1224 return Simplified;
1225 }
1226 } else if (I->isBinaryOp()) {
1227 Value *V =
1228 simplifyBinOp(E->getOpcode(), E->getOperand(0), E->getOperand(1), Q);
1229 if (auto Simplified = checkExprResults(E, I, V))
1230 return Simplified;
1231 } else if (auto *CI = dyn_cast<CastInst>(I)) {
1232 Value *V =
1233 simplifyCastInst(CI->getOpcode(), E->getOperand(0), CI->getType(), Q);
1234 if (auto Simplified = checkExprResults(E, I, V))
1235 return Simplified;
1236 } else if (auto *GEPI = dyn_cast<GetElementPtrInst>(I)) {
1237 Value *V = simplifyGEPInst(GEPI->getSourceElementType(), *E->op_begin(),
1238 ArrayRef(std::next(E->op_begin()), E->op_end()),
1239 GEPI->getNoWrapFlags(), Q);
1240 if (auto Simplified = checkExprResults(E, I, V))
1241 return Simplified;
1242 } else if (AllConstant) {
1243 // We don't bother trying to simplify unless all of the operands
1244 // were constant.
1245 // TODO: There are a lot of Simplify*'s we could call here, if we
1246 // wanted to. The original motivating case for this code was a
1247 // zext i1 false to i8, which we don't have an interface to
1248 // simplify (IE there is no SimplifyZExt).
1249
1251 for (Value *Arg : E->operands())
1252 C.emplace_back(cast<Constant>(Arg));
1253
1254 if (Value *V = ConstantFoldInstOperands(I, C, DL, TLI))
1255 if (auto Simplified = checkExprResults(E, I, V))
1256 return Simplified;
1257 }
1258 return ExprResult::some(E);
1259}
1260
1262NewGVN::createAggregateValueExpression(Instruction *I) const {
1263 if (auto *II = dyn_cast<InsertValueInst>(I)) {
1264 auto *E = new (ExpressionAllocator)
1265 AggregateValueExpression(I->getNumOperands(), II->getNumIndices());
1266 setBasicExpressionInfo(I, E);
1267 E->allocateIntOperands(ExpressionAllocator);
1268 llvm::copy(II->indices(), int_op_inserter(E));
1269 return E;
1270 } else if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
1271 auto *E = new (ExpressionAllocator)
1272 AggregateValueExpression(I->getNumOperands(), EI->getNumIndices());
1273 setBasicExpressionInfo(EI, E);
1274 E->allocateIntOperands(ExpressionAllocator);
1275 llvm::copy(EI->indices(), int_op_inserter(E));
1276 return E;
1277 }
1278 llvm_unreachable("Unhandled type of aggregate value operation");
1279}
1280
1281const DeadExpression *NewGVN::createDeadExpression() const {
1282 // DeadExpression has no arguments and all DeadExpression's are the same,
1283 // so we only need one of them.
1284 return SingletonDeadExpression;
1285}
1286
1287const VariableExpression *NewGVN::createVariableExpression(Value *V) const {
1288 auto *E = new (ExpressionAllocator) VariableExpression(V);
1289 E->setOpcode(V->getValueID());
1290 return E;
1291}
1292
1293const Expression *NewGVN::createVariableOrConstant(Value *V) const {
1294 if (auto *C = dyn_cast<Constant>(V))
1295 return createConstantExpression(C);
1296 return createVariableExpression(V);
1297}
1298
1299const ConstantExpression *NewGVN::createConstantExpression(Constant *C) const {
1300 auto *E = new (ExpressionAllocator) ConstantExpression(C);
1301 E->setOpcode(C->getValueID());
1302 return E;
1303}
1304
1305const UnknownExpression *NewGVN::createUnknownExpression(Instruction *I) const {
1306 auto *E = new (ExpressionAllocator) UnknownExpression(I);
1307 E->setOpcode(I->getOpcode());
1308 return E;
1309}
1310
1311const CallExpression *
1312NewGVN::createCallExpression(CallInst *CI, const MemoryAccess *MA) const {
1313 // FIXME: Add operand bundles for calls.
1314 auto *E =
1315 new (ExpressionAllocator) CallExpression(CI->getNumOperands(), CI, MA);
1316 setBasicExpressionInfo(CI, E);
1317 if (CI->isCommutative()) {
1318 // Ensure that commutative intrinsics that only differ by a permutation
1319 // of their operands get the same value number by sorting the operand value
1320 // numbers.
1321 assert(CI->getNumOperands() >= 2 && "Unsupported commutative intrinsic!");
1322 if (shouldSwapOperands(E->getOperand(0), E->getOperand(1)))
1323 E->swapOperands(0, 1);
1324 }
1325 return E;
1326}
1327
1328// Return true if some equivalent of instruction Inst dominates instruction U.
1329bool NewGVN::someEquivalentDominates(const Instruction *Inst,
1330 const Instruction *U) const {
1331 auto *CC = ValueToClass.lookup(Inst);
1332 // This must be an instruction because we are only called from phi nodes
1333 // in the case that the value it needs to check against is an instruction.
1334
1335 // The most likely candidates for dominance are the leader and the next leader.
1336 // The leader or nextleader will dominate in all cases where there is an
1337 // equivalent that is higher up in the dom tree.
1338 // We can't *only* check them, however, because the
1339 // dominator tree could have an infinite number of non-dominating siblings
1340 // with instructions that are in the right congruence class.
1341 // A
1342 // B C D E F G
1343 // |
1344 // H
1345 // Instruction U could be in H, with equivalents in every other sibling.
1346 // Depending on the rpo order picked, the leader could be the equivalent in
1347 // any of these siblings.
1348 if (!CC)
1349 return false;
1350 if (alwaysAvailable(CC->getLeader()))
1351 return true;
1352 if (DT->dominates(cast<Instruction>(CC->getLeader()), U))
1353 return true;
1354 if (CC->getNextLeader().first &&
1355 DT->dominates(cast<Instruction>(CC->getNextLeader().first), U))
1356 return true;
1357 return llvm::any_of(*CC, [&](const Value *Member) {
1358 return Member != CC->getLeader() &&
1359 DT->dominates(cast<Instruction>(Member), U);
1360 });
1361}
1362
1363// See if we have a congruence class and leader for this operand, and if so,
1364// return it. Otherwise, return the operand itself.
1365Value *NewGVN::lookupOperandLeader(Value *V) const {
1366 CongruenceClass *CC = ValueToClass.lookup(V);
1367 if (CC) {
1368 // Everything in TOP is represented by poison, as it can be any value.
1369 // We do have to make sure we get the type right though, so we can't set the
1370 // RepLeader to poison.
1371 if (CC == TOPClass)
1372 return PoisonValue::get(V->getType());
1373 return CC->getStoredValue() ? CC->getStoredValue() : CC->getLeader();
1374 }
1375
1376 return V;
1377}
1378
1379const MemoryAccess *NewGVN::lookupMemoryLeader(const MemoryAccess *MA) const {
1380 auto *CC = getMemoryClass(MA);
1381 assert(CC->getMemoryLeader() &&
1382 "Every MemoryAccess should be mapped to a congruence class with a "
1383 "representative memory access");
1384 return CC->getMemoryLeader();
1385}
1386
1387// Return true if the MemoryAccess is really equivalent to everything. This is
1388// equivalent to the lattice value "TOP" in most lattices. This is the initial
1389// state of all MemoryAccesses.
1390bool NewGVN::isMemoryAccessTOP(const MemoryAccess *MA) const {
1391 return getMemoryClass(MA) == TOPClass;
1392}
1393
1394LoadExpression *NewGVN::createLoadExpression(Type *LoadType, Value *PointerOp,
1395 LoadInst *LI,
1396 const MemoryAccess *MA) const {
1397 auto *E =
1398 new (ExpressionAllocator) LoadExpression(1, LI, lookupMemoryLeader(MA));
1399 E->allocateOperands(ArgRecycler, ExpressionAllocator);
1400 E->setType(LoadType);
1401
1402 // Give store and loads same opcode so they value number together.
1403 E->setOpcode(0);
1404 E->op_push_back(PointerOp);
1405
1406 // TODO: Value number heap versions. We may be able to discover
1407 // things alias analysis can't on it's own (IE that a store and a
1408 // load have the same value, and thus, it isn't clobbering the load).
1409 return E;
1410}
1411
1412const StoreExpression *
1413NewGVN::createStoreExpression(StoreInst *SI, const MemoryAccess *MA) const {
1414 auto *StoredValueLeader = lookupOperandLeader(SI->getValueOperand());
1415 auto *E = new (ExpressionAllocator)
1416 StoreExpression(SI->getNumOperands(), SI, StoredValueLeader, MA);
1417 E->allocateOperands(ArgRecycler, ExpressionAllocator);
1418 E->setType(SI->getValueOperand()->getType());
1419
1420 // Give store and loads same opcode so they value number together.
1421 E->setOpcode(0);
1422 E->op_push_back(lookupOperandLeader(SI->getPointerOperand()));
1423
1424 // TODO: Value number heap versions. We may be able to discover
1425 // things alias analysis can't on it's own (IE that a store and a
1426 // load have the same value, and thus, it isn't clobbering the load).
1427 return E;
1428}
1429
1430const Expression *NewGVN::performSymbolicStoreEvaluation(Instruction *I) const {
1431 // Unlike loads, we never try to eliminate stores, so we do not check if they
1432 // are simple and avoid value numbering them.
1433 auto *SI = cast<StoreInst>(I);
1434 auto *StoreAccess = getMemoryAccess(SI);
1435 // Get the expression, if any, for the RHS of the MemoryDef.
1436 const MemoryAccess *StoreRHS = StoreAccess->getDefiningAccess();
1438 StoreRHS = MSSAWalker->getClobberingMemoryAccess(StoreAccess);
1439 // If we bypassed the use-def chains, make sure we add a use.
1440 StoreRHS = lookupMemoryLeader(StoreRHS);
1441 if (StoreRHS != StoreAccess->getDefiningAccess())
1442 addMemoryUsers(StoreRHS, StoreAccess);
1443 // If we are defined by ourselves, use the live on entry def.
1444 if (StoreRHS == StoreAccess)
1445 StoreRHS = MSSA->getLiveOnEntryDef();
1446
1447 if (SI->isSimple()) {
1448 // See if we are defined by a previous store expression, it already has a
1449 // value, and it's the same value as our current store. FIXME: Right now, we
1450 // only do this for simple stores, we should expand to cover memcpys, etc.
1451 const auto *LastStore = createStoreExpression(SI, StoreRHS);
1452 const auto *LastCC = ExpressionToClass.lookup(LastStore);
1453 // We really want to check whether the expression we matched was a store. No
1454 // easy way to do that. However, we can check that the class we found has a
1455 // store, which, assuming the value numbering state is not corrupt, is
1456 // sufficient, because we must also be equivalent to that store's expression
1457 // for it to be in the same class as the load.
1458 if (LastCC && LastCC->getStoredValue() == LastStore->getStoredValue())
1459 return LastStore;
1460 // Also check if our value operand is defined by a load of the same memory
1461 // location, and the memory state is the same as it was then (otherwise, it
1462 // could have been overwritten later. See test32 in
1463 // transforms/DeadStoreElimination/simple.ll).
1464 if (auto *LI = dyn_cast<LoadInst>(LastStore->getStoredValue()))
1465 if ((lookupOperandLeader(LI->getPointerOperand()) ==
1466 LastStore->getOperand(0)) &&
1467 (lookupMemoryLeader(getMemoryAccess(LI)->getDefiningAccess()) ==
1468 StoreRHS))
1469 return LastStore;
1470 deleteExpression(LastStore);
1471 }
1472
1473 // If the store is not equivalent to anything, value number it as a store that
1474 // produces a unique memory state (instead of using it's MemoryUse, we use
1475 // it's MemoryDef).
1476 return createStoreExpression(SI, StoreAccess);
1477}
1478
1479// See if we can extract the value of a loaded pointer from a load, a store, or
1480// a memory instruction.
1481const Expression *
1482NewGVN::performSymbolicLoadCoercion(Type *LoadType, Value *LoadPtr,
1483 LoadInst *LI, Instruction *DepInst,
1484 MemoryAccess *DefiningAccess) const {
1485 assert((!LI || LI->isSimple()) && "Not a simple load");
1486 if (auto *DepSI = dyn_cast<StoreInst>(DepInst)) {
1487 // Can't forward from non-atomic to atomic without violating memory model.
1488 // Also don't need to coerce if they are the same type, we will just
1489 // propagate.
1490 if (LI->isAtomic() > DepSI->isAtomic() ||
1491 LoadType == DepSI->getValueOperand()->getType())
1492 return nullptr;
1493 int Offset = analyzeLoadFromClobberingStore(LoadType, LoadPtr, DepSI, DL);
1494 if (Offset >= 0) {
1495 if (auto *C = dyn_cast<Constant>(
1496 lookupOperandLeader(DepSI->getValueOperand()))) {
1497 if (Constant *Res = getConstantValueForLoad(C, Offset, LoadType, DL)) {
1498 LLVM_DEBUG(dbgs() << "Coercing load from store " << *DepSI
1499 << " to constant " << *Res << "\n");
1500 return createConstantExpression(Res);
1501 }
1502 }
1503 }
1504 } else if (auto *DepLI = dyn_cast<LoadInst>(DepInst)) {
1505 // Can't forward from non-atomic to atomic without violating memory model.
1506 if (LI->isAtomic() > DepLI->isAtomic())
1507 return nullptr;
1508 int Offset = analyzeLoadFromClobberingLoad(LoadType, LoadPtr, DepLI, DL);
1509 if (Offset >= 0) {
1510 // We can coerce a constant load into a load.
1511 if (auto *C = dyn_cast<Constant>(lookupOperandLeader(DepLI)))
1512 if (auto *PossibleConstant =
1513 getConstantValueForLoad(C, Offset, LoadType, DL)) {
1514 LLVM_DEBUG(dbgs() << "Coercing load from load " << *LI
1515 << " to constant " << *PossibleConstant << "\n");
1516 return createConstantExpression(PossibleConstant);
1517 }
1518 }
1519 } else if (auto *DepMI = dyn_cast<MemIntrinsic>(DepInst)) {
1520 int Offset = analyzeLoadFromClobberingMemInst(LoadType, LoadPtr, DepMI, DL);
1521 if (Offset >= 0) {
1522 if (auto *PossibleConstant =
1523 getConstantMemInstValueForLoad(DepMI, Offset, LoadType, DL)) {
1524 LLVM_DEBUG(dbgs() << "Coercing load from meminst " << *DepMI
1525 << " to constant " << *PossibleConstant << "\n");
1526 return createConstantExpression(PossibleConstant);
1527 }
1528 }
1529 }
1530
1531 if (auto *II = dyn_cast<IntrinsicInst>(DepInst)) {
1532 if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
1533 auto *LifetimePtr = II->getOperand(0);
1534 if (LoadPtr == lookupOperandLeader(LifetimePtr) ||
1535 AA->isMustAlias(LoadPtr, LifetimePtr))
1536 return createConstantExpression(UndefValue::get(LoadType));
1537 }
1538 }
1539
1540 // All of the below are only true if the loaded pointer is produced
1541 // by the dependent instruction.
1542 if (!DepInst->getType()->isPointerTy() ||
1543 (LoadPtr != lookupOperandLeader(DepInst) &&
1544 !AA->isMustAlias(LoadPtr, DepInst)))
1545 return nullptr;
1546 // If this load really doesn't depend on anything, then we must be loading an
1547 // undef value. This can happen when loading for a fresh allocation with no
1548 // intervening stores, for example. Note that this is only true in the case
1549 // that the result of the allocation is pointer equal to the load ptr.
1550 if (isa<AllocaInst>(DepInst)) {
1551 return createConstantExpression(UndefValue::get(LoadType));
1552 } else if (auto *InitVal =
1553 getInitialValueOfAllocation(DepInst, TLI, LoadType))
1554 return createConstantExpression(InitVal);
1555
1556 return nullptr;
1557}
1558
1559const Expression *NewGVN::performSymbolicLoadEvaluation(Instruction *I) const {
1560 auto *LI = cast<LoadInst>(I);
1561
1562 // We can eliminate in favor of non-simple loads, but we won't be able to
1563 // eliminate the loads themselves.
1564 if (!LI->isSimple())
1565 return nullptr;
1566
1567 Value *LoadAddressLeader = lookupOperandLeader(LI->getPointerOperand());
1568 // Load of undef is UB.
1569 if (isa<UndefValue>(LoadAddressLeader))
1570 return createConstantExpression(PoisonValue::get(LI->getType()));
1571 MemoryAccess *OriginalAccess = getMemoryAccess(I);
1572 MemoryAccess *DefiningAccess =
1573 MSSAWalker->getClobberingMemoryAccess(OriginalAccess);
1574
1575 if (!MSSA->isLiveOnEntryDef(DefiningAccess)) {
1576 if (auto *MD = dyn_cast<MemoryDef>(DefiningAccess)) {
1577 Instruction *DefiningInst = MD->getMemoryInst();
1578 // If the defining instruction is not reachable, replace with poison.
1579 if (!ReachableBlocks.count(DefiningInst->getParent()))
1580 return createConstantExpression(PoisonValue::get(LI->getType()));
1581 // This will handle stores and memory insts. We only do if it the
1582 // defining access has a different type, or it is a pointer produced by
1583 // certain memory operations that cause the memory to have a fixed value
1584 // (IE things like calloc).
1585 if (const auto *CoercionResult =
1586 performSymbolicLoadCoercion(LI->getType(), LoadAddressLeader, LI,
1587 DefiningInst, DefiningAccess))
1588 return CoercionResult;
1589 }
1590 }
1591
1592 const auto *LE = createLoadExpression(LI->getType(), LoadAddressLeader, LI,
1593 DefiningAccess);
1594 // If our MemoryLeader is not our defining access, add a use to the
1595 // MemoryLeader, so that we get reprocessed when it changes.
1596 if (LE->getMemoryLeader() != DefiningAccess)
1597 addMemoryUsers(LE->getMemoryLeader(), OriginalAccess);
1598 return LE;
1599}
1600
1601NewGVN::ExprResult
1602NewGVN::performSymbolicPredicateInfoEvaluation(BitCastInst *I) const {
1603 auto *PI = PredInfo->getPredicateInfoFor(I);
1604 if (!PI)
1605 return ExprResult::none();
1606
1607 LLVM_DEBUG(dbgs() << "Found predicate info from instruction !\n");
1608
1609 const std::optional<PredicateConstraint> &Constraint = PI->getConstraint();
1610 if (!Constraint)
1611 return ExprResult::none();
1612
1613 CmpInst::Predicate Predicate = Constraint->Predicate;
1614 Value *CmpOp0 = I->getOperand(0);
1615 Value *CmpOp1 = Constraint->OtherOp;
1616
1617 Value *FirstOp = lookupOperandLeader(CmpOp0);
1618 Value *SecondOp = lookupOperandLeader(CmpOp1);
1619 Value *AdditionallyUsedValue = CmpOp0;
1620
1621 // Sort the ops.
1622 if (shouldSwapOperandsForPredicate(FirstOp, SecondOp, I)) {
1623 std::swap(FirstOp, SecondOp);
1625 AdditionallyUsedValue = CmpOp1;
1626 }
1627
1628 if (Predicate == CmpInst::ICMP_EQ)
1629 return ExprResult::some(createVariableOrConstant(FirstOp),
1630 AdditionallyUsedValue, PI);
1631
1632 // Handle the special case of floating point.
1633 if (Predicate == CmpInst::FCMP_OEQ && isa<ConstantFP>(FirstOp) &&
1634 !cast<ConstantFP>(FirstOp)->isZero())
1635 return ExprResult::some(createConstantExpression(cast<Constant>(FirstOp)),
1636 AdditionallyUsedValue, PI);
1637
1638 return ExprResult::none();
1639}
1640
1641// Evaluate read only and pure calls, and create an expression result.
1642NewGVN::ExprResult NewGVN::performSymbolicCallEvaluation(Instruction *I) const {
1643 auto *CI = cast<CallInst>(I);
1644
1645 // FIXME: Currently the calls which may access the thread id may
1646 // be considered as not accessing the memory. But this is
1647 // problematic for coroutines, since coroutines may resume in a
1648 // different thread. So we disable the optimization here for the
1649 // correctness. However, it may block many other correct
1650 // optimizations. Revert this one when we detect the memory
1651 // accessing kind more precisely.
1652 if (CI->getFunction()->isPresplitCoroutine())
1653 return ExprResult::none();
1654
1655 // Do not combine convergent calls since they implicitly depend on the set of
1656 // threads that is currently executing, and they might be in different basic
1657 // blocks.
1658 if (CI->isConvergent())
1659 return ExprResult::none();
1660
1661 if (AA->doesNotAccessMemory(CI)) {
1662 return ExprResult::some(
1663 createCallExpression(CI, TOPClass->getMemoryLeader()));
1664 } else if (AA->onlyReadsMemory(CI)) {
1665 if (auto *MA = MSSA->getMemoryAccess(CI)) {
1666 auto *DefiningAccess = MSSAWalker->getClobberingMemoryAccess(MA);
1667 return ExprResult::some(createCallExpression(CI, DefiningAccess));
1668 } else // MSSA determined that CI does not access memory.
1669 return ExprResult::some(
1670 createCallExpression(CI, TOPClass->getMemoryLeader()));
1671 }
1672 return ExprResult::none();
1673}
1674
1675// Retrieve the memory class for a given MemoryAccess.
1676CongruenceClass *NewGVN::getMemoryClass(const MemoryAccess *MA) const {
1677 auto *Result = MemoryAccessToClass.lookup(MA);
1678 assert(Result && "Should have found memory class");
1679 return Result;
1680}
1681
1682// Update the MemoryAccess equivalence table to say that From is equal to To,
1683// and return true if this is different from what already existed in the table.
1684bool NewGVN::setMemoryClass(const MemoryAccess *From,
1685 CongruenceClass *NewClass) {
1686 assert(NewClass &&
1687 "Every MemoryAccess should be getting mapped to a non-null class");
1688 LLVM_DEBUG(dbgs() << "Setting " << *From);
1689 LLVM_DEBUG(dbgs() << " equivalent to congruence class ");
1690 LLVM_DEBUG(dbgs() << NewClass->getID()
1691 << " with current MemoryAccess leader ");
1692 LLVM_DEBUG(dbgs() << *NewClass->getMemoryLeader() << "\n");
1693
1694 auto LookupResult = MemoryAccessToClass.find(From);
1695 bool Changed = false;
1696 // If it's already in the table, see if the value changed.
1697 if (LookupResult != MemoryAccessToClass.end()) {
1698 auto *OldClass = LookupResult->second;
1699 if (OldClass != NewClass) {
1700 // If this is a phi, we have to handle memory member updates.
1701 if (auto *MP = dyn_cast<MemoryPhi>(From)) {
1702 OldClass->memory_erase(MP);
1703 NewClass->memory_insert(MP);
1704 // This may have killed the class if it had no non-memory members
1705 if (OldClass->getMemoryLeader() == From) {
1706 if (OldClass->definesNoMemory()) {
1707 OldClass->setMemoryLeader(nullptr);
1708 } else {
1709 OldClass->setMemoryLeader(getNextMemoryLeader(OldClass));
1710 LLVM_DEBUG(dbgs() << "Memory class leader change for class "
1711 << OldClass->getID() << " to "
1712 << *OldClass->getMemoryLeader()
1713 << " due to removal of a memory member " << *From
1714 << "\n");
1715 markMemoryLeaderChangeTouched(OldClass);
1716 }
1717 }
1718 }
1719 // It wasn't equivalent before, and now it is.
1720 LookupResult->second = NewClass;
1721 Changed = true;
1722 }
1723 }
1724
1725 return Changed;
1726}
1727
1728// Determine if a instruction is cycle-free. That means the values in the
1729// instruction don't depend on any expressions that can change value as a result
1730// of the instruction. For example, a non-cycle free instruction would be v =
1731// phi(0, v+1).
1732bool NewGVN::isCycleFree(const Instruction *I) const {
1733 // In order to compute cycle-freeness, we do SCC finding on the instruction,
1734 // and see what kind of SCC it ends up in. If it is a singleton, it is
1735 // cycle-free. If it is not in a singleton, it is only cycle free if the
1736 // other members are all phi nodes (as they do not compute anything, they are
1737 // copies).
1738 auto ICS = InstCycleState.lookup(I);
1739 if (ICS == ICS_Unknown) {
1740 SCCFinder.Start(I);
1741 auto &SCC = SCCFinder.getComponentFor(I);
1742 // It's cycle free if it's size 1 or the SCC is *only* phi nodes.
1743 if (SCC.size() == 1)
1744 InstCycleState.insert({I, ICS_CycleFree});
1745 else {
1746 bool AllPhis = llvm::all_of(SCC, [](const Value *V) {
1747 return isa<PHINode>(V) || isCopyOfAPHI(V);
1748 });
1749 ICS = AllPhis ? ICS_CycleFree : ICS_Cycle;
1750 for (const auto *Member : SCC)
1751 if (auto *MemberPhi = dyn_cast<PHINode>(Member))
1752 InstCycleState.insert({MemberPhi, ICS});
1753 }
1754 }
1755 if (ICS == ICS_Cycle)
1756 return false;
1757 return true;
1758}
1759
1760// Evaluate PHI nodes symbolically and create an expression result.
1761const Expression *
1762NewGVN::performSymbolicPHIEvaluation(ArrayRef<ValPair> PHIOps,
1763 Instruction *I,
1764 BasicBlock *PHIBlock) const {
1765 // True if one of the incoming phi edges is a backedge.
1766 bool HasBackedge = false;
1767 // All constant tracks the state of whether all the *original* phi operands
1768 // This is really shorthand for "this phi cannot cycle due to forward
1769 // change in value of the phi is guaranteed not to later change the value of
1770 // the phi. IE it can't be v = phi(undef, v+1)
1771 bool OriginalOpsConstant = true;
1772 auto *E = cast<PHIExpression>(createPHIExpression(
1773 PHIOps, I, PHIBlock, HasBackedge, OriginalOpsConstant));
1774 // We match the semantics of SimplifyPhiNode from InstructionSimplify here.
1775 // See if all arguments are the same.
1776 // We track if any were undef because they need special handling.
1777 bool HasUndef = false, HasPoison = false;
1778 auto Filtered = make_filter_range(E->operands(), [&](Value *Arg) {
1779 if (isa<PoisonValue>(Arg)) {
1780 HasPoison = true;
1781 return false;
1782 }
1783 if (isa<UndefValue>(Arg)) {
1784 HasUndef = true;
1785 return false;
1786 }
1787 return true;
1788 });
1789 // If we are left with no operands, it's dead.
1790 if (Filtered.empty()) {
1791 // If it has undef or poison at this point, it means there are no-non-undef
1792 // arguments, and thus, the value of the phi node must be undef.
1793 if (HasUndef) {
1794 LLVM_DEBUG(
1795 dbgs() << "PHI Node " << *I
1796 << " has no non-undef arguments, valuing it as undef\n");
1797 return createConstantExpression(UndefValue::get(I->getType()));
1798 }
1799 if (HasPoison) {
1800 LLVM_DEBUG(
1801 dbgs() << "PHI Node " << *I
1802 << " has no non-poison arguments, valuing it as poison\n");
1803 return createConstantExpression(PoisonValue::get(I->getType()));
1804 }
1805
1806 LLVM_DEBUG(dbgs() << "No arguments of PHI node " << *I << " are live\n");
1807 deleteExpression(E);
1808 return createDeadExpression();
1809 }
1810 Value *AllSameValue = *(Filtered.begin());
1811 ++Filtered.begin();
1812 // Can't use std::equal here, sadly, because filter.begin moves.
1813 if (llvm::all_of(Filtered, [&](Value *Arg) { return Arg == AllSameValue; })) {
1814 // Can't fold phi(undef, X) -> X unless X can't be poison (thus X is undef
1815 // in the worst case).
1816 if (HasUndef && !isGuaranteedNotToBePoison(AllSameValue, AC, nullptr, DT))
1817 return E;
1818
1819 // In LLVM's non-standard representation of phi nodes, it's possible to have
1820 // phi nodes with cycles (IE dependent on other phis that are .... dependent
1821 // on the original phi node), especially in weird CFG's where some arguments
1822 // are unreachable, or uninitialized along certain paths. This can cause
1823 // infinite loops during evaluation. We work around this by not trying to
1824 // really evaluate them independently, but instead using a variable
1825 // expression to say if one is equivalent to the other.
1826 // We also special case undef/poison, so that if we have an undef, we can't
1827 // use the common value unless it dominates the phi block.
1828 if (HasPoison || HasUndef) {
1829 // If we have undef and at least one other value, this is really a
1830 // multivalued phi, and we need to know if it's cycle free in order to
1831 // evaluate whether we can ignore the undef. The other parts of this are
1832 // just shortcuts. If there is no backedge, or all operands are
1833 // constants, it also must be cycle free.
1834 if (HasBackedge && !OriginalOpsConstant &&
1835 !isa<UndefValue>(AllSameValue) && !isCycleFree(I))
1836 return E;
1837
1838 // Only have to check for instructions
1839 if (auto *AllSameInst = dyn_cast<Instruction>(AllSameValue))
1840 if (!someEquivalentDominates(AllSameInst, I))
1841 return E;
1842 }
1843 // Can't simplify to something that comes later in the iteration.
1844 // Otherwise, when and if it changes congruence class, we will never catch
1845 // up. We will always be a class behind it.
1846 if (isa<Instruction>(AllSameValue) &&
1847 InstrToDFSNum(AllSameValue) > InstrToDFSNum(I))
1848 return E;
1849 NumGVNPhisAllSame++;
1850 LLVM_DEBUG(dbgs() << "Simplified PHI node " << *I << " to " << *AllSameValue
1851 << "\n");
1852 deleteExpression(E);
1853 return createVariableOrConstant(AllSameValue);
1854 }
1855 return E;
1856}
1857
1858const Expression *
1859NewGVN::performSymbolicAggrValueEvaluation(Instruction *I) const {
1860 if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
1861 auto *WO = dyn_cast<WithOverflowInst>(EI->getAggregateOperand());
1862 if (WO && EI->getNumIndices() == 1 && *EI->idx_begin() == 0)
1863 // EI is an extract from one of our with.overflow intrinsics. Synthesize
1864 // a semantically equivalent expression instead of an extract value
1865 // expression.
1866 return createBinaryExpression(WO->getBinaryOp(), EI->getType(),
1867 WO->getLHS(), WO->getRHS(), I);
1868 }
1869
1870 return createAggregateValueExpression(I);
1871}
1872
1873NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
1874 assert(isa<CmpInst>(I) && "Expected a cmp instruction.");
1875
1876 auto *CI = cast<CmpInst>(I);
1877 // See if our operands are equal to those of a previous predicate, and if so,
1878 // if it implies true or false.
1879 auto Op0 = lookupOperandLeader(CI->getOperand(0));
1880 auto Op1 = lookupOperandLeader(CI->getOperand(1));
1881 auto OurPredicate = CI->getPredicate();
1882 if (shouldSwapOperands(Op0, Op1)) {
1883 std::swap(Op0, Op1);
1884 OurPredicate = CI->getSwappedPredicate();
1885 }
1886
1887 // Avoid processing the same info twice.
1888 const PredicateBase *LastPredInfo = nullptr;
1889 // See if we know something about the comparison itself, like it is the target
1890 // of an assume.
1891 auto *CmpPI = PredInfo->getPredicateInfoFor(I);
1893 return ExprResult::some(
1894 createConstantExpression(ConstantInt::getTrue(CI->getType())));
1895
1896 if (Op0 == Op1) {
1897 // This condition does not depend on predicates, no need to add users
1898 if (CI->isTrueWhenEqual())
1899 return ExprResult::some(
1900 createConstantExpression(ConstantInt::getTrue(CI->getType())));
1901 else if (CI->isFalseWhenEqual())
1902 return ExprResult::some(
1903 createConstantExpression(ConstantInt::getFalse(CI->getType())));
1904 }
1905
1906 // NOTE: Because we are comparing both operands here and below, and using
1907 // previous comparisons, we rely on fact that predicateinfo knows to mark
1908 // comparisons that use renamed operands as users of the earlier comparisons.
1909 // It is *not* enough to just mark predicateinfo renamed operands as users of
1910 // the earlier comparisons, because the *other* operand may have changed in a
1911 // previous iteration.
1912 // Example:
1913 // icmp slt %a, %b
1914 // %b.0 = ssa.copy(%b)
1915 // false branch:
1916 // icmp slt %c, %b.0
1917
1918 // %c and %a may start out equal, and thus, the code below will say the second
1919 // %icmp is false. c may become equal to something else, and in that case the
1920 // %second icmp *must* be reexamined, but would not if only the renamed
1921 // %operands are considered users of the icmp.
1922
1923 // *Currently* we only check one level of comparisons back, and only mark one
1924 // level back as touched when changes happen. If you modify this code to look
1925 // back farther through comparisons, you *must* mark the appropriate
1926 // comparisons as users in PredicateInfo.cpp, or you will cause bugs. See if
1927 // we know something just from the operands themselves
1928
1929 // See if our operands have predicate info, so that we may be able to derive
1930 // something from a previous comparison.
1931 for (const auto &Op : CI->operands()) {
1932 auto *PI = PredInfo->getPredicateInfoFor(Op);
1933 if (const auto *PBranch = dyn_cast_or_null<PredicateBranch>(PI)) {
1934 if (PI == LastPredInfo)
1935 continue;
1936 LastPredInfo = PI;
1937 // In phi of ops cases, we may have predicate info that we are evaluating
1938 // in a different context.
1939 if (!DT->dominates(PBranch->To, I->getParent()))
1940 continue;
1941 // TODO: Along the false edge, we may know more things too, like
1942 // icmp of
1943 // same operands is false.
1944 // TODO: We only handle actual comparison conditions below, not
1945 // and/or.
1946 auto *BranchCond = dyn_cast<CmpInst>(PBranch->Condition);
1947 if (!BranchCond)
1948 continue;
1949 auto *BranchOp0 = lookupOperandLeader(BranchCond->getOperand(0));
1950 auto *BranchOp1 = lookupOperandLeader(BranchCond->getOperand(1));
1951 auto BranchPredicate = BranchCond->getPredicate();
1952 if (shouldSwapOperands(BranchOp0, BranchOp1)) {
1953 std::swap(BranchOp0, BranchOp1);
1954 BranchPredicate = BranchCond->getSwappedPredicate();
1955 }
1956 if (BranchOp0 == Op0 && BranchOp1 == Op1) {
1957 if (PBranch->TrueEdge) {
1958 // If we know the previous predicate is true and we are in the true
1959 // edge then we may be implied true or false.
1960 if (auto R = ICmpInst::isImpliedByMatchingCmp(BranchPredicate,
1961 OurPredicate)) {
1962 auto *C = ConstantInt::getBool(CI->getType(), *R);
1963 return ExprResult::some(createConstantExpression(C), PI);
1964 }
1965 } else {
1966 // Just handle the ne and eq cases, where if we have the same
1967 // operands, we may know something.
1968 if (BranchPredicate == OurPredicate) {
1969 // Same predicate, same ops,we know it was false, so this is false.
1970 return ExprResult::some(
1971 createConstantExpression(ConstantInt::getFalse(CI->getType())),
1972 PI);
1973 } else if (BranchPredicate ==
1974 CmpInst::getInversePredicate(OurPredicate)) {
1975 // Inverse predicate, we know the other was false, so this is true.
1976 return ExprResult::some(
1977 createConstantExpression(ConstantInt::getTrue(CI->getType())),
1978 PI);
1979 }
1980 }
1981 }
1982 }
1983 }
1984 // Create expression will take care of simplifyCmpInst
1985 return createExpression(I);
1986}
1987
1988// Substitute and symbolize the instruction before value numbering.
1989NewGVN::ExprResult
1990NewGVN::performSymbolicEvaluation(Instruction *I,
1991 SmallPtrSetImpl<Value *> &Visited) const {
1992
1993 const Expression *E = nullptr;
1994 // TODO: memory intrinsics.
1995 // TODO: Some day, we should do the forward propagation and reassociation
1996 // parts of the algorithm.
1997 switch (I->getOpcode()) {
1998 case Instruction::ExtractValue:
1999 case Instruction::InsertValue:
2000 E = performSymbolicAggrValueEvaluation(I);
2001 break;
2002 case Instruction::PHI: {
2004 auto *PN = cast<PHINode>(I);
2005 for (unsigned i = 0; i < PN->getNumOperands(); ++i)
2006 Ops.push_back({PN->getIncomingValue(i), PN->getIncomingBlock(i)});
2007 // Sort to ensure the invariant createPHIExpression requires is met.
2008 sortPHIOps(Ops);
2009 E = performSymbolicPHIEvaluation(Ops, I, getBlockForValue(I));
2010 } break;
2011 case Instruction::Call:
2012 return performSymbolicCallEvaluation(I);
2013 break;
2014 case Instruction::Store:
2015 E = performSymbolicStoreEvaluation(I);
2016 break;
2017 case Instruction::Load:
2018 E = performSymbolicLoadEvaluation(I);
2019 break;
2020 case Instruction::BitCast:
2021 // Intrinsics with the returned attribute are copies of arguments.
2022 if (I->getType() == I->getOperand(0)->getType())
2023 if (auto Res =
2024 performSymbolicPredicateInfoEvaluation(cast<BitCastInst>(I)))
2025 return Res;
2026 [[fallthrough]];
2027 case Instruction::AddrSpaceCast:
2028 case Instruction::Freeze:
2029 return createExpression(I);
2030 break;
2031 case Instruction::ICmp:
2032 case Instruction::FCmp:
2033 return performSymbolicCmpEvaluation(I);
2034 break;
2035 case Instruction::FNeg:
2036 case Instruction::Add:
2037 case Instruction::FAdd:
2038 case Instruction::Sub:
2039 case Instruction::FSub:
2040 case Instruction::Mul:
2041 case Instruction::FMul:
2042 case Instruction::UDiv:
2043 case Instruction::SDiv:
2044 case Instruction::FDiv:
2045 case Instruction::URem:
2046 case Instruction::SRem:
2047 case Instruction::FRem:
2048 case Instruction::Shl:
2049 case Instruction::LShr:
2050 case Instruction::AShr:
2051 case Instruction::And:
2052 case Instruction::Or:
2053 case Instruction::Xor:
2054 case Instruction::Trunc:
2055 case Instruction::ZExt:
2056 case Instruction::SExt:
2057 case Instruction::FPToUI:
2058 case Instruction::FPToSI:
2059 case Instruction::UIToFP:
2060 case Instruction::SIToFP:
2061 case Instruction::FPTrunc:
2062 case Instruction::FPExt:
2063 case Instruction::PtrToInt:
2064 case Instruction::PtrToAddr:
2065 case Instruction::IntToPtr:
2066 case Instruction::Select:
2067 case Instruction::ExtractElement:
2068 case Instruction::InsertElement:
2069 case Instruction::GetElementPtr:
2070 return createExpression(I);
2071 break;
2072 case Instruction::ShuffleVector:
2073 // FIXME: Add support for shufflevector to createExpression.
2074 return ExprResult::none();
2075 default:
2076 return ExprResult::none();
2077 }
2078 return ExprResult::some(E);
2079}
2080
2081// Look up a container of values/instructions in a map, and touch all the
2082// instructions in the container. Then erase value from the map.
2083template <typename Map, typename KeyType>
2084void NewGVN::touchAndErase(Map &M, const KeyType &Key) {
2085 const auto Result = M.find_as(Key);
2086 if (Result != M.end()) {
2087 for (const typename Map::mapped_type::value_type Mapped : Result->second)
2088 TouchedInstructions.set(InstrToDFSNum(Mapped));
2089 M.erase(Result);
2090 }
2091}
2092
2093void NewGVN::addAdditionalUsers(Value *To, Value *User) const {
2094 assert(User && To != User);
2095 if (isa<Instruction>(To))
2096 AdditionalUsers[To].insert(User);
2097}
2098
2099void NewGVN::addAdditionalUsers(ExprResult &Res, Instruction *User) const {
2100 if (Res.ExtraDep && Res.ExtraDep != User)
2101 addAdditionalUsers(Res.ExtraDep, User);
2102 Res.ExtraDep = nullptr;
2103
2104 if (Res.PredDep) {
2105 if (const auto *PBranch = dyn_cast<PredicateBranch>(Res.PredDep))
2106 PredicateToUsers[PBranch->Condition].insert(User);
2107 else if (const auto *PAssume = dyn_cast<PredicateAssume>(Res.PredDep))
2108 PredicateToUsers[PAssume->Condition].insert(User);
2109 }
2110 Res.PredDep = nullptr;
2111}
2112
2113void NewGVN::markUsersTouched(Value *V) {
2114 // Now mark the users as touched.
2115 for (auto *User : V->users()) {
2116 assert(isa<Instruction>(User) && "Use of value not within an instruction?");
2117 TouchedInstructions.set(InstrToDFSNum(User));
2118 }
2119 touchAndErase(AdditionalUsers, V);
2120}
2121
2122void NewGVN::addMemoryUsers(const MemoryAccess *To, MemoryAccess *U) const {
2123 LLVM_DEBUG(dbgs() << "Adding memory user " << *U << " to " << *To << "\n");
2124 MemoryToUsers[To].insert(U);
2125}
2126
2127void NewGVN::markMemoryDefTouched(const MemoryAccess *MA) {
2128 TouchedInstructions.set(MemoryToDFSNum(MA));
2129}
2130
2131void NewGVN::markMemoryUsersTouched(const MemoryAccess *MA) {
2132 if (isa<MemoryUse>(MA))
2133 return;
2134 for (const auto *U : MA->users())
2135 TouchedInstructions.set(MemoryToDFSNum(U));
2136 touchAndErase(MemoryToUsers, MA);
2137}
2138
2139// Touch all the predicates that depend on this instruction.
2140void NewGVN::markPredicateUsersTouched(Instruction *I) {
2141 touchAndErase(PredicateToUsers, I);
2142}
2143
2144// Mark users affected by a memory leader change.
2145void NewGVN::markMemoryLeaderChangeTouched(CongruenceClass *CC) {
2146 for (const auto *M : CC->memory())
2147 markMemoryDefTouched(M);
2148}
2149
2150// Touch the instructions that need to be updated after a congruence class has a
2151// leader change, and mark changed values.
2152void NewGVN::markValueLeaderChangeTouched(CongruenceClass *CC) {
2153 for (auto *M : *CC) {
2154 if (auto *I = dyn_cast<Instruction>(M))
2155 TouchedInstructions.set(InstrToDFSNum(I));
2156 LeaderChanges.insert(M);
2157 }
2158}
2159
2160// Give a range of things that have instruction DFS numbers, this will return
2161// the member of the range with the smallest dfs number.
2162template <class T, class Range>
2163T *NewGVN::getMinDFSOfRange(const Range &R) const {
2164 std::pair<T *, unsigned> MinDFS = {nullptr, ~0U};
2165 for (const auto X : R) {
2166 auto DFSNum = InstrToDFSNum(X);
2167 if (DFSNum < MinDFS.second)
2168 MinDFS = {X, DFSNum};
2169 }
2170 return MinDFS.first;
2171}
2172
2173// This function returns the MemoryAccess that should be the next leader of
2174// congruence class CC, under the assumption that the current leader is going to
2175// disappear.
2176const MemoryAccess *NewGVN::getNextMemoryLeader(CongruenceClass *CC) const {
2177 // TODO: If this ends up to slow, we can maintain a next memory leader like we
2178 // do for regular leaders.
2179 // Make sure there will be a leader to find.
2180 assert(!CC->definesNoMemory() && "Can't get next leader if there is none");
2181 if (CC->getStoreCount() > 0) {
2182 if (auto *NL = dyn_cast_or_null<StoreInst>(CC->getNextLeader().first))
2183 return getMemoryAccess(NL);
2184 // Find the store with the minimum DFS number.
2185 auto *V = getMinDFSOfRange<Value>(make_filter_range(
2186 *CC, [&](const Value *V) { return isa<StoreInst>(V); }));
2187 return getMemoryAccess(cast<StoreInst>(V));
2188 }
2189 assert(CC->getStoreCount() == 0);
2190
2191 // Given our assertion, hitting this part must mean
2192 // !OldClass->memory_empty()
2193 if (CC->memory_size() == 1)
2194 return *CC->memory_begin();
2195 return getMinDFSOfRange<const MemoryPhi>(CC->memory());
2196}
2197
2198// This function returns the next value leader of a congruence class, under the
2199// assumption that the current leader is going away. This should end up being
2200// the next most dominating member.
2201Value *NewGVN::getNextValueLeader(CongruenceClass *CC) const {
2202 // We don't need to sort members if there is only 1, and we don't care about
2203 // sorting the TOP class because everything either gets out of it or is
2204 // unreachable.
2205
2206 if (CC->size() == 1 || CC == TOPClass) {
2207 return *(CC->begin());
2208 } else if (CC->getNextLeader().first) {
2209 ++NumGVNAvoidedSortedLeaderChanges;
2210 return CC->getNextLeader().first;
2211 } else {
2212 ++NumGVNSortedLeaderChanges;
2213 // NOTE: If this ends up to slow, we can maintain a dual structure for
2214 // member testing/insertion, or keep things mostly sorted, and sort only
2215 // here, or use SparseBitVector or ....
2216 return getMinDFSOfRange<Value>(*CC);
2217 }
2218}
2219
2220// Move a MemoryAccess, currently in OldClass, to NewClass, including updates to
2221// the memory members, etc for the move.
2222//
2223// The invariants of this function are:
2224//
2225// - I must be moving to NewClass from OldClass
2226// - The StoreCount of OldClass and NewClass is expected to have been updated
2227// for I already if it is a store.
2228// - The OldClass memory leader has not been updated yet if I was the leader.
2229void NewGVN::moveMemoryToNewCongruenceClass(Instruction *I,
2230 MemoryAccess *InstMA,
2231 CongruenceClass *OldClass,
2232 CongruenceClass *NewClass) {
2233 // If the leader is I, and we had a representative MemoryAccess, it should
2234 // be the MemoryAccess of OldClass.
2235 assert((!InstMA || !OldClass->getMemoryLeader() ||
2236 OldClass->getLeader() != I ||
2237 MemoryAccessToClass.lookup(OldClass->getMemoryLeader()) ==
2238 MemoryAccessToClass.lookup(InstMA)) &&
2239 "Representative MemoryAccess mismatch");
2240 // First, see what happens to the new class
2241 if (!NewClass->getMemoryLeader()) {
2242 // Should be a new class, or a store becoming a leader of a new class.
2243 assert(NewClass->size() == 1 ||
2244 (isa<StoreInst>(I) && NewClass->getStoreCount() == 1));
2245 NewClass->setMemoryLeader(InstMA);
2246 // Mark it touched if we didn't just create a singleton
2247 LLVM_DEBUG(dbgs() << "Memory class leader change for class "
2248 << NewClass->getID()
2249 << " due to new memory instruction becoming leader\n");
2250 markMemoryLeaderChangeTouched(NewClass);
2251 }
2252 setMemoryClass(InstMA, NewClass);
2253 // Now, fixup the old class if necessary
2254 if (OldClass->getMemoryLeader() == InstMA) {
2255 if (!OldClass->definesNoMemory()) {
2256 OldClass->setMemoryLeader(getNextMemoryLeader(OldClass));
2257 LLVM_DEBUG(dbgs() << "Memory class leader change for class "
2258 << OldClass->getID() << " to "
2259 << *OldClass->getMemoryLeader()
2260 << " due to removal of old leader " << *InstMA << "\n");
2261 markMemoryLeaderChangeTouched(OldClass);
2262 } else
2263 OldClass->setMemoryLeader(nullptr);
2264 }
2265}
2266
2267// Move a value, currently in OldClass, to be part of NewClass
2268// Update OldClass and NewClass for the move (including changing leaders, etc).
2269void NewGVN::moveValueToNewCongruenceClass(Instruction *I, const Expression *E,
2270 CongruenceClass *OldClass,
2271 CongruenceClass *NewClass) {
2272 if (I == OldClass->getNextLeader().first)
2273 OldClass->resetNextLeader();
2274
2275 OldClass->erase(I);
2276 NewClass->insert(I);
2277
2278 // Ensure that the leader has the lowest RPO. If the leader changed notify all
2279 // members of the class.
2280 if (NewClass->getLeader() != I &&
2281 NewClass->addPossibleLeader({I, InstrToDFSNum(I)})) {
2282 markValueLeaderChangeTouched(NewClass);
2283 }
2284
2285 // Handle our special casing of stores.
2286 if (auto *SI = dyn_cast<StoreInst>(I)) {
2287 OldClass->decStoreCount();
2288 // Okay, so when do we want to make a store a leader of a class?
2289 // If we have a store defined by an earlier load, we want the earlier load
2290 // to lead the class.
2291 // If we have a store defined by something else, we want the store to lead
2292 // the class so everything else gets the "something else" as a value.
2293 // If we have a store as the single member of the class, we want the store
2294 // as the leader
2295 if (NewClass->getStoreCount() == 0 && !NewClass->getStoredValue()) {
2296 // If it's a store expression we are using, it means we are not equivalent
2297 // to something earlier.
2298 if (auto *SE = dyn_cast<StoreExpression>(E)) {
2299 NewClass->setStoredValue(SE->getStoredValue());
2300 markValueLeaderChangeTouched(NewClass);
2301 // Shift the new class leader to be the store
2302 LLVM_DEBUG(dbgs() << "Changing leader of congruence class "
2303 << NewClass->getID() << " from "
2304 << *NewClass->getLeader() << " to " << *SI
2305 << " because store joined class\n");
2306 // If we changed the leader, we have to mark it changed because we don't
2307 // know what it will do to symbolic evaluation.
2308 NewClass->setLeader({SI, InstrToDFSNum(SI)});
2309 }
2310 // We rely on the code below handling the MemoryAccess change.
2311 }
2312 NewClass->incStoreCount();
2313 }
2314 // True if there is no memory instructions left in a class that had memory
2315 // instructions before.
2316
2317 // If it's not a memory use, set the MemoryAccess equivalence
2318 auto *InstMA = dyn_cast_or_null<MemoryDef>(getMemoryAccess(I));
2319 if (InstMA)
2320 moveMemoryToNewCongruenceClass(I, InstMA, OldClass, NewClass);
2321 ValueToClass[I] = NewClass;
2322 // See if we destroyed the class or need to swap leaders.
2323 if (OldClass->empty() && OldClass != TOPClass) {
2324 if (OldClass->getDefiningExpr()) {
2325 LLVM_DEBUG(dbgs() << "Erasing expression " << *OldClass->getDefiningExpr()
2326 << " from table\n");
2327 // We erase it as an exact expression to make sure we don't just erase an
2328 // equivalent one.
2329 auto Iter = ExpressionToClass.find_as(
2330 ExactEqualsExpression(*OldClass->getDefiningExpr()));
2331 if (Iter != ExpressionToClass.end())
2332 ExpressionToClass.erase(Iter);
2333#ifdef EXPENSIVE_CHECKS
2334 assert(
2335 (*OldClass->getDefiningExpr() != *E || ExpressionToClass.lookup(E)) &&
2336 "We erased the expression we just inserted, which should not happen");
2337#endif
2338 }
2339 } else if (OldClass->getLeader() == I) {
2340 // When the leader changes, the value numbering of
2341 // everything may change due to symbolization changes, so we need to
2342 // reprocess.
2343 LLVM_DEBUG(dbgs() << "Value class leader change for class "
2344 << OldClass->getID() << "\n");
2345 ++NumGVNLeaderChanges;
2346 // Destroy the stored value if there are no more stores to represent it.
2347 // Note that this is basically clean up for the expression removal that
2348 // happens below. If we remove stores from a class, we may leave it as a
2349 // class of equivalent memory phis.
2350 if (OldClass->getStoreCount() == 0) {
2351 if (OldClass->getStoredValue())
2352 OldClass->setStoredValue(nullptr);
2353 }
2354 OldClass->setLeader({getNextValueLeader(OldClass),
2355 InstrToDFSNum(getNextValueLeader(OldClass))});
2356 OldClass->resetNextLeader();
2357 markValueLeaderChangeTouched(OldClass);
2358 }
2359}
2360
2361// For a given expression, mark the phi of ops instructions that could have
2362// changed as a result.
2363void NewGVN::markPhiOfOpsChanged(const Expression *E) {
2364 touchAndErase(ExpressionToPhiOfOps, E);
2365}
2366
2367// Perform congruence finding on a given value numbering expression.
2368void NewGVN::performCongruenceFinding(Instruction *I, const Expression *E) {
2369 // This is guaranteed to return something, since it will at least find
2370 // TOP.
2371
2372 CongruenceClass *IClass = ValueToClass.lookup(I);
2373 assert(IClass && "Should have found a IClass");
2374 // Dead classes should have been eliminated from the mapping.
2375 assert(!IClass->isDead() && "Found a dead class");
2376
2377 CongruenceClass *EClass = nullptr;
2378 if (const auto *VE = dyn_cast<VariableExpression>(E)) {
2379 EClass = ValueToClass.lookup(VE->getVariableValue());
2380 } else if (isa<DeadExpression>(E)) {
2381 EClass = TOPClass;
2382 }
2383 if (!EClass) {
2384 auto lookupResult = ExpressionToClass.try_emplace(E);
2385
2386 // If it's not in the value table, create a new congruence class.
2387 if (lookupResult.second) {
2388 CongruenceClass *NewClass = createCongruenceClass(nullptr, E);
2389 auto place = lookupResult.first;
2390 place->second = NewClass;
2391
2392 // Constants and variables should always be made the leader.
2393 if (const auto *CE = dyn_cast<ConstantExpression>(E)) {
2394 NewClass->setLeader({CE->getConstantValue(), 0});
2395 } else if (const auto *SE = dyn_cast<StoreExpression>(E)) {
2396 StoreInst *SI = SE->getStoreInst();
2397 NewClass->setLeader({SI, InstrToDFSNum(SI)});
2398 NewClass->setStoredValue(SE->getStoredValue());
2399 // The RepMemoryAccess field will be filled in properly by the
2400 // moveValueToNewCongruenceClass call.
2401 } else {
2402 NewClass->setLeader({I, InstrToDFSNum(I)});
2403 }
2405 "VariableExpression should have been handled already");
2406
2407 EClass = NewClass;
2408 LLVM_DEBUG(dbgs() << "Created new congruence class for " << *I
2409 << " using expression " << *E << " at "
2410 << NewClass->getID() << " and leader "
2411 << *(NewClass->getLeader()));
2412 if (NewClass->getStoredValue())
2413 LLVM_DEBUG(dbgs() << " and stored value "
2414 << *(NewClass->getStoredValue()));
2415 LLVM_DEBUG(dbgs() << "\n");
2416 } else {
2417 EClass = lookupResult.first->second;
2419 assert((isa<Constant>(EClass->getLeader()) ||
2420 (EClass->getStoredValue() &&
2421 isa<Constant>(EClass->getStoredValue()))) &&
2422 "Any class with a constant expression should have a "
2423 "constant leader");
2424
2425 assert(EClass && "Somehow don't have an eclass");
2426
2427 assert(!EClass->isDead() && "We accidentally looked up a dead class");
2428 }
2429 }
2430 bool ClassChanged = IClass != EClass;
2431 bool LeaderChanged = LeaderChanges.erase(I);
2432 if (ClassChanged || LeaderChanged) {
2433 LLVM_DEBUG(dbgs() << "New class " << EClass->getID() << " for expression "
2434 << *E << "\n");
2435 if (ClassChanged) {
2436 moveValueToNewCongruenceClass(I, E, IClass, EClass);
2437 markPhiOfOpsChanged(E);
2438 }
2439
2440 markUsersTouched(I);
2441 if (MemoryAccess *MA = getMemoryAccess(I))
2442 markMemoryUsersTouched(MA);
2443 if (auto *CI = dyn_cast<CmpInst>(I))
2444 markPredicateUsersTouched(CI);
2445 }
2446 // If we changed the class of the store, we want to ensure nothing finds the
2447 // old store expression. In particular, loads do not compare against stored
2448 // value, so they will find old store expressions (and associated class
2449 // mappings) if we leave them in the table.
2450 if (ClassChanged && isa<StoreInst>(I)) {
2451 auto *OldE = ValueToExpression.lookup(I);
2452 // It could just be that the old class died. We don't want to erase it if we
2453 // just moved classes.
2454 if (OldE && isa<StoreExpression>(OldE) && *E != *OldE) {
2455 // Erase this as an exact expression to ensure we don't erase expressions
2456 // equivalent to it.
2457 auto Iter = ExpressionToClass.find_as(ExactEqualsExpression(*OldE));
2458 if (Iter != ExpressionToClass.end())
2459 ExpressionToClass.erase(Iter);
2460 }
2461 }
2462 ValueToExpression[I] = E;
2463}
2464
2465// Process the fact that Edge (from, to) is reachable, including marking
2466// any newly reachable blocks and instructions for processing.
2467void NewGVN::updateReachableEdge(BasicBlock *From, BasicBlock *To) {
2468 // Check if the Edge was reachable before.
2469 if (ReachableEdges.insert({From, To}).second) {
2470 // If this block wasn't reachable before, all instructions are touched.
2471 if (ReachableBlocks.insert(To).second) {
2472 LLVM_DEBUG(dbgs() << "Block " << getBlockName(To)
2473 << " marked reachable\n");
2474 const auto &InstRange = BlockInstRange.lookup(To);
2475 TouchedInstructions.set(InstRange.first, InstRange.second);
2476 } else {
2477 LLVM_DEBUG(dbgs() << "Block " << getBlockName(To)
2478 << " was reachable, but new edge {"
2479 << getBlockName(From) << "," << getBlockName(To)
2480 << "} to it found\n");
2481
2482 // We've made an edge reachable to an existing block, which may
2483 // impact predicates. Otherwise, only mark the phi nodes as touched, as
2484 // they are the only thing that depend on new edges. Anything using their
2485 // values will get propagated to if necessary.
2486 if (MemoryAccess *MemPhi = getMemoryAccess(To))
2487 TouchedInstructions.set(InstrToDFSNum(MemPhi));
2488
2489 // FIXME: We should just add a union op on a Bitvector and
2490 // SparseBitVector. We can do it word by word faster than we are doing it
2491 // here.
2492 for (auto InstNum : RevisitOnReachabilityChange[To])
2493 TouchedInstructions.set(InstNum);
2494 }
2495 }
2496}
2497
2498// Given a predicate condition (from a switch, cmp, or whatever) and a block,
2499// see if we know some constant value for it already.
2500Value *NewGVN::findConditionEquivalence(Value *Cond) const {
2501 auto Result = lookupOperandLeader(Cond);
2502 return isa<Constant>(Result) ? Result : nullptr;
2503}
2504
2505// Process the outgoing edges of a block for reachability.
2506void NewGVN::processOutgoingEdges(Instruction *TI, BasicBlock *B) {
2507 // Evaluate reachability of terminator instruction.
2508 Value *Cond;
2509 BasicBlock *TrueSucc, *FalseSucc;
2510 if (match(TI, m_Br(m_Value(Cond), TrueSucc, FalseSucc))) {
2511 Value *CondEvaluated = findConditionEquivalence(Cond);
2512 if (!CondEvaluated) {
2513 if (auto *I = dyn_cast<Instruction>(Cond)) {
2514 SmallPtrSet<Value *, 4> Visited;
2515 auto Res = performSymbolicEvaluation(I, Visited);
2516 if (const auto *CE = dyn_cast_or_null<ConstantExpression>(Res.Expr)) {
2517 CondEvaluated = CE->getConstantValue();
2518 addAdditionalUsers(Res, I);
2519 } else {
2520 // Did not use simplification result, no need to add the extra
2521 // dependency.
2522 Res.ExtraDep = nullptr;
2523 }
2524 } else if (isa<ConstantInt>(Cond)) {
2525 CondEvaluated = Cond;
2526 }
2527 }
2528 ConstantInt *CI;
2529 if (CondEvaluated && (CI = dyn_cast<ConstantInt>(CondEvaluated))) {
2530 if (CI->isOne()) {
2531 LLVM_DEBUG(dbgs() << "Condition for Terminator " << *TI
2532 << " evaluated to true\n");
2533 updateReachableEdge(B, TrueSucc);
2534 } else if (CI->isZero()) {
2535 LLVM_DEBUG(dbgs() << "Condition for Terminator " << *TI
2536 << " evaluated to false\n");
2537 updateReachableEdge(B, FalseSucc);
2538 }
2539 } else {
2540 updateReachableEdge(B, TrueSucc);
2541 updateReachableEdge(B, FalseSucc);
2542 }
2543 } else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
2544 // For switches, propagate the case values into the case
2545 // destinations.
2546
2547 Value *SwitchCond = SI->getCondition();
2548 Value *CondEvaluated = findConditionEquivalence(SwitchCond);
2549 // See if we were able to turn this switch statement into a constant.
2550 if (CondEvaluated && isa<ConstantInt>(CondEvaluated)) {
2551 auto *CondVal = cast<ConstantInt>(CondEvaluated);
2552 // We should be able to get case value for this.
2553 auto Case = *SI->findCaseValue(CondVal);
2554 if (Case.getCaseSuccessor() == SI->getDefaultDest()) {
2555 // We proved the value is outside of the range of the case.
2556 // We can't do anything other than mark the default dest as reachable,
2557 // and go home.
2558 updateReachableEdge(B, SI->getDefaultDest());
2559 return;
2560 }
2561 // Now get where it goes and mark it reachable.
2562 BasicBlock *TargetBlock = Case.getCaseSuccessor();
2563 updateReachableEdge(B, TargetBlock);
2564 } else {
2565 for (BasicBlock *TargetBlock : successors(SI->getParent()))
2566 updateReachableEdge(B, TargetBlock);
2567 }
2568 } else {
2569 // Otherwise this is either unconditional, or a type we have no
2570 // idea about. Just mark successors as reachable.
2571 for (BasicBlock *TargetBlock : successors(TI->getParent()))
2572 updateReachableEdge(B, TargetBlock);
2573
2574 // This also may be a memory defining terminator, in which case, set it
2575 // equivalent only to itself.
2576 //
2577 auto *MA = getMemoryAccess(TI);
2578 if (MA && !isa<MemoryUse>(MA)) {
2579 auto *CC = ensureLeaderOfMemoryClass(MA);
2580 if (setMemoryClass(MA, CC))
2581 markMemoryUsersTouched(MA);
2582 }
2583 }
2584}
2585
2586// Remove the PHI of Ops PHI for I
2587void NewGVN::removePhiOfOps(Instruction *I, PHINode *PHITemp) {
2588 InstrDFS.erase(PHITemp);
2589 // It's still a temp instruction. We keep it in the array so it gets erased.
2590 // However, it's no longer used by I, or in the block
2591 TempToBlock.erase(PHITemp);
2592 RealToTemp.erase(I);
2593 // We don't remove the users from the phi node uses. This wastes a little
2594 // time, but such is life. We could use two sets to track which were there
2595 // are the start of NewGVN, and which were added, but right nowt he cost of
2596 // tracking is more than the cost of checking for more phi of ops.
2597}
2598
2599// Add PHI Op in BB as a PHI of operations version of ExistingValue.
2600void NewGVN::addPhiOfOps(PHINode *Op, BasicBlock *BB,
2601 Instruction *ExistingValue) {
2602 InstrDFS[Op] = InstrToDFSNum(ExistingValue);
2603 AllTempInstructions.insert(Op);
2604 TempToBlock[Op] = BB;
2605 RealToTemp[ExistingValue] = Op;
2606 // Add all users to phi node use, as they are now uses of the phi of ops phis
2607 // and may themselves be phi of ops.
2608 for (auto *U : ExistingValue->users())
2609 if (auto *UI = dyn_cast<Instruction>(U))
2610 PHINodeUses.insert(UI);
2611}
2612
2613static bool okayForPHIOfOps(const Instruction *I) {
2614 if (!EnablePhiOfOps)
2615 return false;
2618}
2619
2620// Return true if this operand will be safe to use for phi of ops.
2621//
2622// The reason some operands are unsafe is that we are not trying to recursively
2623// translate everything back through phi nodes. We actually expect some lookups
2624// of expressions to fail. In particular, a lookup where the expression cannot
2625// exist in the predecessor. This is true even if the expression, as shown, can
2626// be determined to be constant.
2627bool NewGVN::OpIsSafeForPHIOfOps(Value *V, const BasicBlock *PHIBlock,
2628 SmallPtrSetImpl<const Value *> &Visited) {
2629 SmallVector<Value *, 4> Worklist;
2630 Worklist.push_back(V);
2631 while (!Worklist.empty()) {
2632 auto *I = Worklist.pop_back_val();
2633 if (!isa<Instruction>(I))
2634 continue;
2635
2636 auto OISIt = OpSafeForPHIOfOps.find({I, CacheIdx});
2637 if (OISIt != OpSafeForPHIOfOps.end())
2638 return OISIt->second;
2639
2640 // Keep walking until we either dominate the phi block, or hit a phi, or run
2641 // out of things to check.
2642 if (DT->properlyDominates(getBlockForValue(I), PHIBlock)) {
2643 OpSafeForPHIOfOps.insert({{I, CacheIdx}, true});
2644 continue;
2645 }
2646 // PHI in the same block.
2647 if (isa<PHINode>(I) && getBlockForValue(I) == PHIBlock) {
2648 OpSafeForPHIOfOps.insert({{I, CacheIdx}, false});
2649 return false;
2650 }
2651
2652 auto *OrigI = cast<Instruction>(I);
2653 // When we hit an instruction that reads memory (load, call, etc), we must
2654 // consider any store that may happen in the loop. For now, we assume the
2655 // worst: there is a store in the loop that alias with this read.
2656 // The case where the load is outside the loop is already covered by the
2657 // dominator check above.
2658 // TODO: relax this condition
2659 if (OrigI->mayReadFromMemory())
2660 return false;
2661
2662 // Check the operands of the current instruction.
2663 for (auto *Op : OrigI->operand_values()) {
2664 if (!isa<Instruction>(Op))
2665 continue;
2666 // Stop now if we find an unsafe operand.
2667 auto OISIt = OpSafeForPHIOfOps.find({OrigI, CacheIdx});
2668 if (OISIt != OpSafeForPHIOfOps.end()) {
2669 if (!OISIt->second) {
2670 OpSafeForPHIOfOps.insert({{I, CacheIdx}, false});
2671 return false;
2672 }
2673 continue;
2674 }
2675 if (!Visited.insert(Op).second)
2676 continue;
2677 Worklist.push_back(cast<Instruction>(Op));
2678 }
2679 }
2680 OpSafeForPHIOfOps.insert({{V, CacheIdx}, true});
2681 return true;
2682}
2683
2684// Try to find a leader for instruction TransInst, which is a phi translated
2685// version of something in our original program. Visited is used to ensure we
2686// don't infinite loop during translations of cycles. OrigInst is the
2687// instruction in the original program, and PredBB is the predecessor we
2688// translated it through.
2689Value *NewGVN::findLeaderForInst(Instruction *TransInst,
2690 SmallPtrSetImpl<Value *> &Visited,
2691 MemoryAccess *MemAccess, Instruction *OrigInst,
2692 BasicBlock *PredBB) {
2693 unsigned IDFSNum = InstrToDFSNum(OrigInst);
2694 // Make sure it's marked as a temporary instruction.
2695 AllTempInstructions.insert(TransInst);
2696 // and make sure anything that tries to add it's DFS number is
2697 // redirected to the instruction we are making a phi of ops
2698 // for.
2699 TempToBlock.insert({TransInst, PredBB});
2700 InstrDFS.insert({TransInst, IDFSNum});
2701
2702 auto Res = performSymbolicEvaluation(TransInst, Visited);
2703 const Expression *E = Res.Expr;
2704 addAdditionalUsers(Res, OrigInst);
2705 InstrDFS.erase(TransInst);
2706 AllTempInstructions.erase(TransInst);
2707 TempToBlock.erase(TransInst);
2708 if (MemAccess)
2709 TempToMemory.erase(TransInst);
2710 if (!E)
2711 return nullptr;
2712 auto *FoundVal = findPHIOfOpsLeader(E, OrigInst, PredBB);
2713 if (!FoundVal) {
2714 ExpressionToPhiOfOps[E].insert(OrigInst);
2715 LLVM_DEBUG(dbgs() << "Cannot find phi of ops operand for " << *TransInst
2716 << " in block " << getBlockName(PredBB) << "\n");
2717 return nullptr;
2718 }
2719 if (auto *SI = dyn_cast<StoreInst>(FoundVal))
2720 FoundVal = SI->getValueOperand();
2721 return FoundVal;
2722}
2723
2724// When we see an instruction that is an op of phis, generate the equivalent phi
2725// of ops form.
2726const Expression *
2727NewGVN::makePossiblePHIOfOps(Instruction *I,
2728 SmallPtrSetImpl<Value *> &Visited) {
2729 if (!okayForPHIOfOps(I))
2730 return nullptr;
2731
2732 if (!Visited.insert(I).second)
2733 return nullptr;
2734 // For now, we require the instruction be cycle free because we don't
2735 // *always* create a phi of ops for instructions that could be done as phi
2736 // of ops, we only do it if we think it is useful. If we did do it all the
2737 // time, we could remove the cycle free check.
2738 if (!isCycleFree(I))
2739 return nullptr;
2740
2741 // TODO: We don't do phi translation on memory accesses because it's
2742 // complicated. For a load, we'd need to be able to simulate a new memoryuse,
2743 // which we don't have a good way of doing ATM.
2744 auto *MemAccess = getMemoryAccess(I);
2745 // If the memory operation is defined by a memory operation this block that
2746 // isn't a MemoryPhi, transforming the pointer backwards through a scalar phi
2747 // can't help, as it would still be killed by that memory operation.
2748 if (MemAccess && !isa<MemoryPhi>(MemAccess->getDefiningAccess()) &&
2749 MemAccess->getDefiningAccess()->getBlock() == I->getParent())
2750 return nullptr;
2751
2752 // Convert op of phis to phi of ops
2753 SmallPtrSet<const Value *, 10> VisitedOps;
2754 SmallVector<Value *, 4> Ops(I->operand_values());
2755 BasicBlock *SamePHIBlock = nullptr;
2756 PHINode *OpPHI = nullptr;
2757 if (!DebugCounter::shouldExecute(PHIOfOpsCounter))
2758 return nullptr;
2759 for (auto *Op : Ops) {
2760 if (!isa<PHINode>(Op)) {
2761 auto *ValuePHI = RealToTemp.lookup(Op);
2762 if (!ValuePHI)
2763 continue;
2764 LLVM_DEBUG(dbgs() << "Found possible dependent phi of ops\n");
2765 Op = ValuePHI;
2766 }
2767 OpPHI = cast<PHINode>(Op);
2768 if (!SamePHIBlock) {
2769 SamePHIBlock = getBlockForValue(OpPHI);
2770 } else if (SamePHIBlock != getBlockForValue(OpPHI)) {
2771 LLVM_DEBUG(
2772 dbgs()
2773 << "PHIs for operands are not all in the same block, aborting\n");
2774 return nullptr;
2775 }
2776 // No point in doing this for one-operand phis.
2777 // Since all PHIs for operands must be in the same block, then they must
2778 // have the same number of operands so we can just abort.
2779 if (OpPHI->getNumOperands() == 1)
2780 return nullptr;
2781 }
2782
2783 if (!OpPHI)
2784 return nullptr;
2785
2787 SmallPtrSet<Value *, 4> Deps;
2788 auto *PHIBlock = getBlockForValue(OpPHI);
2789 RevisitOnReachabilityChange[PHIBlock].reset(InstrToDFSNum(I));
2790 for (unsigned PredNum = 0; PredNum < OpPHI->getNumOperands(); ++PredNum) {
2791 auto *PredBB = OpPHI->getIncomingBlock(PredNum);
2792 Value *FoundVal = nullptr;
2793 SmallPtrSet<Value *, 4> CurrentDeps;
2794 // We could just skip unreachable edges entirely but it's tricky to do
2795 // with rewriting existing phi nodes.
2796 if (ReachableEdges.count({PredBB, PHIBlock})) {
2797 // Clone the instruction, create an expression from it that is
2798 // translated back into the predecessor, and see if we have a leader.
2799 Instruction *ValueOp = I->clone();
2800 // Emit the temporal instruction in the predecessor basic block where the
2801 // corresponding value is defined.
2802 ValueOp->insertBefore(PredBB->getTerminator()->getIterator());
2803 if (MemAccess)
2804 TempToMemory.insert({ValueOp, MemAccess});
2805 bool SafeForPHIOfOps = true;
2806 VisitedOps.clear();
2807 for (auto &Op : ValueOp->operands()) {
2808 auto *OrigOp = &*Op;
2809 // When these operand changes, it could change whether there is a
2810 // leader for us or not, so we have to add additional users.
2811 if (isa<PHINode>(Op)) {
2812 Op = Op->DoPHITranslation(PHIBlock, PredBB);
2813 if (Op != OrigOp && Op != I)
2814 CurrentDeps.insert(Op);
2815 } else if (auto *ValuePHI = RealToTemp.lookup(Op)) {
2816 if (getBlockForValue(ValuePHI) == PHIBlock)
2817 Op = ValuePHI->getIncomingValueForBlock(PredBB);
2818 }
2819 // If we phi-translated the op, it must be safe.
2820 SafeForPHIOfOps =
2821 SafeForPHIOfOps &&
2822 (Op != OrigOp || OpIsSafeForPHIOfOps(Op, PHIBlock, VisitedOps));
2823 }
2824 // FIXME: For those things that are not safe we could generate
2825 // expressions all the way down, and see if this comes out to a
2826 // constant. For anything where that is true, and unsafe, we should
2827 // have made a phi-of-ops (or value numbered it equivalent to something)
2828 // for the pieces already.
2829 FoundVal = !SafeForPHIOfOps ? nullptr
2830 : findLeaderForInst(ValueOp, Visited,
2831 MemAccess, I, PredBB);
2832 ValueOp->eraseFromParent();
2833 if (!FoundVal) {
2834 // We failed to find a leader for the current ValueOp, but this might
2835 // change in case of the translated operands change.
2836 if (SafeForPHIOfOps)
2837 for (auto *Dep : CurrentDeps)
2838 addAdditionalUsers(Dep, I);
2839
2840 return nullptr;
2841 }
2842 Deps.insert_range(CurrentDeps);
2843 } else {
2844 LLVM_DEBUG(dbgs() << "Skipping phi of ops operand for incoming block "
2845 << getBlockName(PredBB)
2846 << " because the block is unreachable\n");
2847 FoundVal = PoisonValue::get(I->getType());
2848 RevisitOnReachabilityChange[PHIBlock].set(InstrToDFSNum(I));
2849 }
2850
2851 PHIOps.push_back({FoundVal, PredBB});
2852 LLVM_DEBUG(dbgs() << "Found phi of ops operand " << *FoundVal << " in "
2853 << getBlockName(PredBB) << "\n");
2854 }
2855 for (auto *Dep : Deps)
2856 addAdditionalUsers(Dep, I);
2857 sortPHIOps(PHIOps);
2858 auto *E = performSymbolicPHIEvaluation(PHIOps, I, PHIBlock);
2860 LLVM_DEBUG(
2861 dbgs()
2862 << "Not creating real PHI of ops because it simplified to existing "
2863 "value or constant\n");
2864 // We have leaders for all operands, but do not create a real PHI node with
2865 // those leaders as operands, so the link between the operands and the
2866 // PHI-of-ops is not materialized in the IR. If any of those leaders
2867 // changes, the PHI-of-op may change also, so we need to add the operands as
2868 // additional users.
2869 for (auto &O : PHIOps)
2870 addAdditionalUsers(O.first, I);
2871
2872 return E;
2873 }
2874 auto *ValuePHI = RealToTemp.lookup(I);
2875 bool NewPHI = false;
2876 if (!ValuePHI) {
2877 ValuePHI =
2878 PHINode::Create(I->getType(), OpPHI->getNumOperands(), "phiofops");
2879 addPhiOfOps(ValuePHI, PHIBlock, I);
2880 NewPHI = true;
2881 NumGVNPHIOfOpsCreated++;
2882 }
2883 if (NewPHI) {
2884 for (auto PHIOp : PHIOps)
2885 ValuePHI->addIncoming(PHIOp.first, PHIOp.second);
2886 } else {
2887 TempToBlock[ValuePHI] = PHIBlock;
2888 unsigned int i = 0;
2889 for (auto PHIOp : PHIOps) {
2890 ValuePHI->setIncomingValue(i, PHIOp.first);
2891 ValuePHI->setIncomingBlock(i, PHIOp.second);
2892 ++i;
2893 }
2894 }
2895 RevisitOnReachabilityChange[PHIBlock].set(InstrToDFSNum(I));
2896 LLVM_DEBUG(dbgs() << "Created phi of ops " << *ValuePHI << " for " << *I
2897 << "\n");
2898
2899 return E;
2900}
2901
2902// The algorithm initially places the values of the routine in the TOP
2903// congruence class. The leader of TOP is the undetermined value `poison`.
2904// When the algorithm has finished, values still in TOP are unreachable.
2905void NewGVN::initializeCongruenceClasses(Function &F) {
2906 NextCongruenceNum = 0;
2907
2908 // Note that even though we use the live on entry def as a representative
2909 // MemoryAccess, it is *not* the same as the actual live on entry def. We
2910 // have no real equivalent to poison for MemoryAccesses, and so we really
2911 // should be checking whether the MemoryAccess is top if we want to know if it
2912 // is equivalent to everything. Otherwise, what this really signifies is that
2913 // the access "it reaches all the way back to the beginning of the function"
2914
2915 // Initialize all other instructions to be in TOP class.
2916 TOPClass = createCongruenceClass(nullptr, nullptr);
2917 TOPClass->setMemoryLeader(MSSA->getLiveOnEntryDef());
2918 // The live on entry def gets put into it's own class
2919 MemoryAccessToClass[MSSA->getLiveOnEntryDef()] =
2920 createMemoryClass(MSSA->getLiveOnEntryDef());
2921
2922 for (auto *DTN : nodes(DT)) {
2923 BasicBlock *BB = DTN->getBlock();
2924 // All MemoryAccesses are equivalent to live on entry to start. They must
2925 // be initialized to something so that initial changes are noticed. For
2926 // the maximal answer, we initialize them all to be the same as
2927 // liveOnEntry.
2928 auto *MemoryBlockDefs = MSSA->getBlockDefs(BB);
2929 if (MemoryBlockDefs)
2930 for (const auto &Def : *MemoryBlockDefs) {
2931 MemoryAccessToClass[&Def] = TOPClass;
2932 auto *MD = dyn_cast<MemoryDef>(&Def);
2933 // Insert the memory phis into the member list.
2934 if (!MD) {
2935 const MemoryPhi *MP = cast<MemoryPhi>(&Def);
2936 TOPClass->memory_insert(MP);
2937 MemoryPhiState.insert({MP, MPS_TOP});
2938 }
2939
2940 if (MD && isa<StoreInst>(MD->getMemoryInst()))
2941 TOPClass->incStoreCount();
2942 }
2943
2944 // FIXME: This is trying to discover which instructions are uses of phi
2945 // nodes. We should move this into one of the myriad of places that walk
2946 // all the operands already.
2947 for (auto &I : *BB) {
2948 if (isa<PHINode>(&I))
2949 for (auto *U : I.users())
2950 if (auto *UInst = dyn_cast<Instruction>(U))
2951 if (InstrToDFSNum(UInst) != 0 && okayForPHIOfOps(UInst))
2952 PHINodeUses.insert(UInst);
2953 // Don't insert void terminators into the class. We don't value number
2954 // them, and they just end up sitting in TOP.
2955 if (I.isTerminator() && I.getType()->isVoidTy())
2956 continue;
2957 TOPClass->insert(&I);
2958 ValueToClass[&I] = TOPClass;
2959 }
2960 }
2961
2962 // Initialize arguments to be in their own unique congruence classes
2963 for (auto &FA : F.args())
2964 createSingletonCongruenceClass(&FA);
2965}
2966
2967void NewGVN::cleanupTables() {
2968 for (CongruenceClass *&CC : CongruenceClasses) {
2969 LLVM_DEBUG(dbgs() << "Congruence class " << CC->getID() << " has "
2970 << CC->size() << " members\n");
2971 // Make sure we delete the congruence class (probably worth switching to
2972 // a unique_ptr at some point.
2973 delete CC;
2974 CC = nullptr;
2975 }
2976
2977 // Destroy the value expressions
2978 SmallVector<Instruction *, 8> TempInst(AllTempInstructions.begin(),
2979 AllTempInstructions.end());
2980 AllTempInstructions.clear();
2981
2982 // We have to drop all references for everything first, so there are no uses
2983 // left as we delete them.
2984 for (auto *I : TempInst) {
2985 I->dropAllReferences();
2986 }
2987
2988 while (!TempInst.empty()) {
2989 auto *I = TempInst.pop_back_val();
2990 I->deleteValue();
2991 }
2992
2993 ValueToClass.clear();
2994 ArgRecycler.clear(ExpressionAllocator);
2995 ExpressionAllocator.Reset();
2996 CongruenceClasses.clear();
2997 ExpressionToClass.clear();
2998 ValueToExpression.clear();
2999 RealToTemp.clear();
3000 AdditionalUsers.clear();
3001 ExpressionToPhiOfOps.clear();
3002 TempToBlock.clear();
3003 TempToMemory.clear();
3004 PHINodeUses.clear();
3005 OpSafeForPHIOfOps.clear();
3006 ReachableBlocks.clear();
3007 ReachableEdges.clear();
3008#ifndef NDEBUG
3009 ProcessedCount.clear();
3010#endif
3011 InstrDFS.clear();
3012 InstructionsToErase.clear();
3013 DFSToInstr.clear();
3014 BlockInstRange.clear();
3015 TouchedInstructions.clear();
3016 MemoryAccessToClass.clear();
3017 PredicateToUsers.clear();
3018 MemoryToUsers.clear();
3019 RevisitOnReachabilityChange.clear();
3020 PredicateSwapChoice.clear();
3021}
3022
3023// Assign local DFS number mapping to instructions, and leave space for Value
3024// PHI's.
3025std::pair<unsigned, unsigned> NewGVN::assignDFSNumbers(BasicBlock *B,
3026 unsigned Start) {
3027 unsigned End = Start;
3028 if (MemoryAccess *MemPhi = getMemoryAccess(B)) {
3029 InstrDFS[MemPhi] = End++;
3030 DFSToInstr.emplace_back(MemPhi);
3031 }
3032
3033 // Then the real block goes next.
3034 for (auto &I : *B) {
3035 // There's no need to call isInstructionTriviallyDead more than once on
3036 // an instruction. Therefore, once we know that an instruction is dead
3037 // we change its DFS number so that it doesn't get value numbered.
3038 if (isInstructionTriviallyDead(&I, TLI)) {
3039 InstrDFS[&I] = 0;
3040 LLVM_DEBUG(dbgs() << "Skipping trivially dead instruction " << I << "\n");
3042 markInstructionForDeletion(&I);
3043 continue;
3044 }
3045 if (isa<PHINode>(&I))
3046 RevisitOnReachabilityChange[B].set(End);
3047 InstrDFS[&I] = End++;
3048 DFSToInstr.emplace_back(&I);
3049 }
3050
3051 // All of the range functions taken half-open ranges (open on the end side).
3052 // So we do not subtract one from count, because at this point it is one
3053 // greater than the last instruction.
3054 return std::make_pair(Start, End);
3055}
3056
3057void NewGVN::updateProcessedCount(const Value *V) {
3058#ifndef NDEBUG
3059 assert(++ProcessedCount[V] < 100 &&
3060 "Seem to have processed the same Value a lot");
3061#endif
3062}
3063
3064// Evaluate MemoryPhi nodes symbolically, just like PHI nodes
3065void NewGVN::valueNumberMemoryPhi(MemoryPhi *MP) {
3066 // If all the arguments are the same, the MemoryPhi has the same value as the
3067 // argument. Filter out unreachable blocks and self phis from our operands.
3068 // TODO: We could do cycle-checking on the memory phis to allow valueizing for
3069 // self-phi checking.
3070 const BasicBlock *PHIBlock = MP->getBlock();
3071 auto Filtered = make_filter_range(MP->operands(), [&](const Use &U) {
3072 return cast<MemoryAccess>(U) != MP &&
3073 !isMemoryAccessTOP(cast<MemoryAccess>(U)) &&
3074 ReachableEdges.count({MP->getIncomingBlock(U), PHIBlock});
3075 });
3076 // If all that is left is nothing, our memoryphi is poison. We keep it as
3077 // InitialClass. Note: The only case this should happen is if we have at
3078 // least one self-argument.
3079 if (Filtered.begin() == Filtered.end()) {
3080 if (setMemoryClass(MP, TOPClass))
3081 markMemoryUsersTouched(MP);
3082 return;
3083 }
3084
3085 // Transform the remaining operands into operand leaders.
3086 // FIXME: mapped_iterator should have a range version.
3087 auto LookupFunc = [&](const Use &U) {
3088 return lookupMemoryLeader(cast<MemoryAccess>(U));
3089 };
3090 auto MappedBegin = map_iterator(Filtered.begin(), LookupFunc);
3091 auto MappedEnd = map_iterator(Filtered.end(), LookupFunc);
3092
3093 // and now check if all the elements are equal.
3094 // Sadly, we can't use std::equals since these are random access iterators.
3095 const auto *AllSameValue = *MappedBegin;
3096 ++MappedBegin;
3097 bool AllEqual = std::all_of(
3098 MappedBegin, MappedEnd,
3099 [&AllSameValue](const MemoryAccess *V) { return V == AllSameValue; });
3100
3101 if (AllEqual)
3102 LLVM_DEBUG(dbgs() << "Memory Phi value numbered to " << *AllSameValue
3103 << "\n");
3104 else
3105 LLVM_DEBUG(dbgs() << "Memory Phi value numbered to itself\n");
3106 // If it's equal to something, it's in that class. Otherwise, it has to be in
3107 // a class where it is the leader (other things may be equivalent to it, but
3108 // it needs to start off in its own class, which means it must have been the
3109 // leader, and it can't have stopped being the leader because it was never
3110 // removed).
3111 CongruenceClass *CC =
3112 AllEqual ? getMemoryClass(AllSameValue) : ensureLeaderOfMemoryClass(MP);
3113 auto OldState = MemoryPhiState.lookup(MP);
3114 assert(OldState != MPS_Invalid && "Invalid memory phi state");
3115 auto NewState = AllEqual ? MPS_Equivalent : MPS_Unique;
3116 MemoryPhiState[MP] = NewState;
3117 if (setMemoryClass(MP, CC) || OldState != NewState)
3118 markMemoryUsersTouched(MP);
3119}
3120
3121// Value number a single instruction, symbolically evaluating, performing
3122// congruence finding, and updating mappings.
3123void NewGVN::valueNumberInstruction(Instruction *I) {
3124 LLVM_DEBUG(dbgs() << "Processing instruction " << *I << "\n");
3125 if (!I->isTerminator()) {
3126 const Expression *Symbolized = nullptr;
3127 SmallPtrSet<Value *, 2> Visited;
3128 if (DebugCounter::shouldExecute(VNCounter)) {
3129 auto Res = performSymbolicEvaluation(I, Visited);
3130 Symbolized = Res.Expr;
3131 addAdditionalUsers(Res, I);
3132
3133 // Make a phi of ops if necessary
3134 if (Symbolized && !isa<ConstantExpression>(Symbolized) &&
3135 !isa<VariableExpression>(Symbolized) && PHINodeUses.count(I)) {
3136 auto *PHIE = makePossiblePHIOfOps(I, Visited);
3137 // If we created a phi of ops, use it.
3138 // If we couldn't create one, make sure we don't leave one lying around
3139 if (PHIE) {
3140 Symbolized = PHIE;
3141 } else if (auto *Op = RealToTemp.lookup(I)) {
3142 removePhiOfOps(I, Op);
3143 }
3144 }
3145 } else {
3146 // Mark the instruction as unused so we don't value number it again.
3147 InstrDFS[I] = 0;
3148 }
3149 // If we couldn't come up with a symbolic expression, use the unknown
3150 // expression
3151 if (Symbolized == nullptr)
3152 Symbolized = createUnknownExpression(I);
3153 performCongruenceFinding(I, Symbolized);
3154 } else {
3155 // Handle terminators that return values. All of them produce values we
3156 // don't currently understand. We don't place non-value producing
3157 // terminators in a class.
3158 if (!I->getType()->isVoidTy()) {
3159 auto *Symbolized = createUnknownExpression(I);
3160 performCongruenceFinding(I, Symbolized);
3161 }
3162 processOutgoingEdges(I, I->getParent());
3163 }
3164}
3165
3166// Check if there is a path, using single or equal argument phi nodes, from
3167// First to Second.
3168bool NewGVN::singleReachablePHIPath(
3169 SmallPtrSet<const MemoryAccess *, 8> &Visited, const MemoryAccess *First,
3170 const MemoryAccess *Second) const {
3171 if (First == Second)
3172 return true;
3173 if (MSSA->isLiveOnEntryDef(First))
3174 return false;
3175
3176 // This is not perfect, but as we're just verifying here, we can live with
3177 // the loss of precision. The real solution would be that of doing strongly
3178 // connected component finding in this routine, and it's probably not worth
3179 // the complexity for the time being. So, we just keep a set of visited
3180 // MemoryAccess and return true when we hit a cycle.
3181 if (!Visited.insert(First).second)
3182 return true;
3183
3184 const auto *EndDef = First;
3185 for (const auto *ChainDef : optimized_def_chain(First)) {
3186 if (ChainDef == Second)
3187 return true;
3188 if (MSSA->isLiveOnEntryDef(ChainDef))
3189 return false;
3190 EndDef = ChainDef;
3191 }
3192 auto *MP = cast<MemoryPhi>(EndDef);
3193 auto ReachableOperandPred = [&](const Use &U) {
3194 return ReachableEdges.count({MP->getIncomingBlock(U), MP->getBlock()});
3195 };
3196 auto FilteredPhiArgs =
3197 make_filter_range(MP->operands(), ReachableOperandPred);
3198 SmallVector<const Value *, 32> OperandList(FilteredPhiArgs);
3199 bool Okay = all_equal(OperandList);
3200 if (Okay)
3201 return singleReachablePHIPath(Visited, cast<MemoryAccess>(OperandList[0]),
3202 Second);
3203 return false;
3204}
3205
3206// Verify the that the memory equivalence table makes sense relative to the
3207// congruence classes. Note that this checking is not perfect, and is currently
3208// subject to very rare false negatives. It is only useful for
3209// testing/debugging.
3210void NewGVN::verifyMemoryCongruency() const {
3211#ifndef NDEBUG
3212 // Verify that the memory table equivalence and memory member set match
3213 for (const auto *CC : CongruenceClasses) {
3214 if (CC == TOPClass || CC->isDead())
3215 continue;
3216 if (CC->getStoreCount() != 0) {
3217 assert((CC->getStoredValue() || !isa<StoreInst>(CC->getLeader())) &&
3218 "Any class with a store as a leader should have a "
3219 "representative stored value");
3220 assert(CC->getMemoryLeader() &&
3221 "Any congruence class with a store should have a "
3222 "representative access");
3223 }
3224
3225 if (CC->getMemoryLeader())
3226 assert(MemoryAccessToClass.lookup(CC->getMemoryLeader()) == CC &&
3227 "Representative MemoryAccess does not appear to be reverse "
3228 "mapped properly");
3229 for (const auto *M : CC->memory())
3230 assert(MemoryAccessToClass.lookup(M) == CC &&
3231 "Memory member does not appear to be reverse mapped properly");
3232 }
3233
3234 // Anything equivalent in the MemoryAccess table should be in the same
3235 // congruence class.
3236
3237 // Filter out the unreachable and trivially dead entries, because they may
3238 // never have been updated if the instructions were not processed.
3239 auto ReachableAccessPred =
3240 [&](const std::pair<const MemoryAccess *, CongruenceClass *> Pair) {
3241 bool Result = ReachableBlocks.count(Pair.first->getBlock());
3242 if (!Result || MSSA->isLiveOnEntryDef(Pair.first) ||
3243 MemoryToDFSNum(Pair.first) == 0)
3244 return false;
3245 if (auto *MemDef = dyn_cast<MemoryDef>(Pair.first))
3246 return !isInstructionTriviallyDead(MemDef->getMemoryInst());
3247
3248 // We could have phi nodes which operands are all trivially dead,
3249 // so we don't process them.
3250 if (auto *MemPHI = dyn_cast<MemoryPhi>(Pair.first)) {
3251 for (const auto &U : MemPHI->incoming_values()) {
3252 if (auto *I = dyn_cast<Instruction>(&*U)) {
3254 return true;
3255 }
3256 }
3257 return false;
3258 }
3259
3260 return true;
3261 };
3262
3263 auto Filtered = make_filter_range(MemoryAccessToClass, ReachableAccessPred);
3264 for (auto KV : Filtered) {
3265 if (auto *FirstMUD = dyn_cast<MemoryUseOrDef>(KV.first)) {
3266 auto *SecondMUD = dyn_cast<MemoryUseOrDef>(KV.second->getMemoryLeader());
3267 if (FirstMUD && SecondMUD) {
3268 SmallPtrSet<const MemoryAccess *, 8> VisitedMAS;
3269 assert((singleReachablePHIPath(VisitedMAS, FirstMUD, SecondMUD) ||
3270 ValueToClass.lookup(FirstMUD->getMemoryInst()) ==
3271 ValueToClass.lookup(SecondMUD->getMemoryInst())) &&
3272 "The instructions for these memory operations should have "
3273 "been in the same congruence class or reachable through"
3274 "a single argument phi");
3275 }
3276 } else if (auto *FirstMP = dyn_cast<MemoryPhi>(KV.first)) {
3277 // We can only sanely verify that MemoryDefs in the operand list all have
3278 // the same class.
3279 auto ReachableOperandPred = [&](const Use &U) {
3280 return ReachableEdges.count(
3281 {FirstMP->getIncomingBlock(U), FirstMP->getBlock()}) &&
3282 isa<MemoryDef>(U);
3283 };
3284 // All arguments should in the same class, ignoring unreachable arguments
3285 auto FilteredPhiArgs =
3286 make_filter_range(FirstMP->operands(), ReachableOperandPred);
3288 std::transform(FilteredPhiArgs.begin(), FilteredPhiArgs.end(),
3289 std::back_inserter(PhiOpClasses), [&](const Use &U) {
3290 const MemoryDef *MD = cast<MemoryDef>(U);
3291 return ValueToClass.lookup(MD->getMemoryInst());
3292 });
3293 assert(all_equal(PhiOpClasses) &&
3294 "All MemoryPhi arguments should be in the same class");
3295 }
3296 }
3297#endif
3298}
3299
3300// Verify that the sparse propagation we did actually found the maximal fixpoint
3301// We do this by storing the value to class mapping, touching all instructions,
3302// and redoing the iteration to see if anything changed.
3303void NewGVN::verifyIterationSettled(Function &F) {
3304#ifndef NDEBUG
3305 LLVM_DEBUG(dbgs() << "Beginning iteration verification\n");
3306 if (DebugCounter::isCounterSet(VNCounter))
3307 DebugCounter::setCounterState(VNCounter, StartingVNCounter);
3308
3309 // Note that we have to store the actual classes, as we may change existing
3310 // classes during iteration. This is because our memory iteration propagation
3311 // is not perfect, and so may waste a little work. But it should generate
3312 // exactly the same congruence classes we have now, with different IDs.
3313 std::map<const Value *, CongruenceClass> BeforeIteration;
3314
3315 for (auto &KV : ValueToClass) {
3316 if (auto *I = dyn_cast<Instruction>(KV.first))
3317 // Skip unused/dead instructions.
3318 if (InstrToDFSNum(I) == 0)
3319 continue;
3320 BeforeIteration.insert({KV.first, *KV.second});
3321 }
3322
3323 TouchedInstructions.set();
3324 TouchedInstructions.reset(0);
3325 OpSafeForPHIOfOps.clear();
3326 CacheIdx = 0;
3327 iterateTouchedInstructions();
3328 DenseSet<std::pair<const CongruenceClass *, const CongruenceClass *>>
3329 EqualClasses;
3330 for (const auto &KV : ValueToClass) {
3331 if (auto *I = dyn_cast<Instruction>(KV.first))
3332 // Skip unused/dead instructions.
3333 if (InstrToDFSNum(I) == 0)
3334 continue;
3335 // We could sink these uses, but i think this adds a bit of clarity here as
3336 // to what we are comparing.
3337 auto *BeforeCC = &BeforeIteration.find(KV.first)->second;
3338 auto *AfterCC = KV.second;
3339 // Note that the classes can't change at this point, so we memoize the set
3340 // that are equal.
3341 if (!EqualClasses.count({BeforeCC, AfterCC})) {
3342 assert(BeforeCC->isEquivalentTo(AfterCC) &&
3343 "Value number changed after main loop completed!");
3344 EqualClasses.insert({BeforeCC, AfterCC});
3345 }
3346 }
3347#endif
3348}
3349
3350// Verify that for each store expression in the expression to class mapping,
3351// only the latest appears, and multiple ones do not appear.
3352// Because loads do not use the stored value when doing equality with stores,
3353// if we don't erase the old store expressions from the table, a load can find
3354// a no-longer valid StoreExpression.
3355void NewGVN::verifyStoreExpressions() const {
3356#ifndef NDEBUG
3357 // This is the only use of this, and it's not worth defining a complicated
3358 // densemapinfo hash/equality function for it.
3359 std::set<
3360 std::pair<const Value *,
3361 std::tuple<const Value *, const CongruenceClass *, Value *>>>
3362 StoreExpressionSet;
3363 for (const auto &KV : ExpressionToClass) {
3364 if (auto *SE = dyn_cast<StoreExpression>(KV.first)) {
3365 // Make sure a version that will conflict with loads is not already there
3366 auto Res = StoreExpressionSet.insert(
3367 {SE->getOperand(0), std::make_tuple(SE->getMemoryLeader(), KV.second,
3368 SE->getStoredValue())});
3369 bool Okay = Res.second;
3370 // It's okay to have the same expression already in there if it is
3371 // identical in nature.
3372 // This can happen when the leader of the stored value changes over time.
3373 if (!Okay)
3374 Okay = (std::get<1>(Res.first->second) == KV.second) &&
3375 (lookupOperandLeader(std::get<2>(Res.first->second)) ==
3376 lookupOperandLeader(SE->getStoredValue()));
3377 assert(Okay && "Stored expression conflict exists in expression table");
3378 auto *ValueExpr = ValueToExpression.lookup(SE->getStoreInst());
3379 assert(ValueExpr && ValueExpr->equals(*SE) &&
3380 "StoreExpression in ExpressionToClass is not latest "
3381 "StoreExpression for value");
3382 }
3383 }
3384#endif
3385}
3386
3387// This is the main value numbering loop, it iterates over the initial touched
3388// instruction set, propagating value numbers, marking things touched, etc,
3389// until the set of touched instructions is completely empty.
3390void NewGVN::iterateTouchedInstructions() {
3391 uint64_t Iterations = 0;
3392 // Figure out where touchedinstructions starts
3393 int FirstInstr = TouchedInstructions.find_first();
3394 // Nothing set, nothing to iterate, just return.
3395 if (FirstInstr == -1)
3396 return;
3397 const BasicBlock *LastBlock = getBlockForValue(InstrFromDFSNum(FirstInstr));
3398 while (TouchedInstructions.any()) {
3399 ++Iterations;
3400 // Walk through all the instructions in all the blocks in RPO.
3401 // TODO: As we hit a new block, we should push and pop equalities into a
3402 // table lookupOperandLeader can use, to catch things PredicateInfo
3403 // might miss, like edge-only equivalences.
3404 for (unsigned InstrNum : TouchedInstructions.set_bits()) {
3405
3406 // This instruction was found to be dead. We don't bother looking
3407 // at it again.
3408 if (InstrNum == 0) {
3409 TouchedInstructions.reset(InstrNum);
3410 continue;
3411 }
3412
3413 Value *V = InstrFromDFSNum(InstrNum);
3414 const BasicBlock *CurrBlock = getBlockForValue(V);
3415
3416 // If we hit a new block, do reachability processing.
3417 if (CurrBlock != LastBlock) {
3418 LastBlock = CurrBlock;
3419 bool BlockReachable = ReachableBlocks.count(CurrBlock);
3420 const auto &CurrInstRange = BlockInstRange.lookup(CurrBlock);
3421
3422 // If it's not reachable, erase any touched instructions and move on.
3423 if (!BlockReachable) {
3424 TouchedInstructions.reset(CurrInstRange.first, CurrInstRange.second);
3425 LLVM_DEBUG(dbgs() << "Skipping instructions in block "
3426 << getBlockName(CurrBlock)
3427 << " because it is unreachable\n");
3428 continue;
3429 }
3430 // Use the appropriate cache for "OpIsSafeForPHIOfOps".
3431 CacheIdx = RPOOrdering.lookup(DT->getNode(CurrBlock)) - 1;
3432 updateProcessedCount(CurrBlock);
3433 }
3434 // Reset after processing (because we may mark ourselves as touched when
3435 // we propagate equalities).
3436 TouchedInstructions.reset(InstrNum);
3437
3438 if (auto *MP = dyn_cast<MemoryPhi>(V)) {
3439 LLVM_DEBUG(dbgs() << "Processing MemoryPhi " << *MP << "\n");
3440 valueNumberMemoryPhi(MP);
3441 } else if (auto *I = dyn_cast<Instruction>(V)) {
3442 valueNumberInstruction(I);
3443 } else {
3444 llvm_unreachable("Should have been a MemoryPhi or Instruction");
3445 }
3446 updateProcessedCount(V);
3447 }
3448 }
3449 NumGVNMaxIterations = std::max(NumGVNMaxIterations.getValue(), Iterations);
3450}
3451
3452// This is the main transformation entry point.
3453bool NewGVN::runGVN() {
3454 if (DebugCounter::isCounterSet(VNCounter))
3455 StartingVNCounter = DebugCounter::getCounterState(VNCounter);
3456 bool Changed = false;
3457 NumFuncArgs = F.arg_size();
3458 MSSAWalker = MSSA->getWalker();
3459 SingletonDeadExpression = new (ExpressionAllocator) DeadExpression();
3460
3461 // Count number of instructions for sizing of hash tables, and come
3462 // up with a global dfs numbering for instructions.
3463 unsigned ICount = 1;
3464 // Add an empty instruction to account for the fact that we start at 1
3465 DFSToInstr.emplace_back(nullptr);
3466 // Note: We want ideal RPO traversal of the blocks, which is not quite the
3467 // same as dominator tree order, particularly with regard whether backedges
3468 // get visited first or second, given a block with multiple successors.
3469 // If we visit in the wrong order, we will end up performing N times as many
3470 // iterations.
3471 // The dominator tree does guarantee that, for a given dom tree node, it's
3472 // parent must occur before it in the RPO ordering. Thus, we only need to sort
3473 // the siblings.
3474 ReversePostOrderTraversal<Function *> RPOT(&F);
3475 unsigned Counter = 0;
3476 for (auto &B : RPOT) {
3477 auto *Node = DT->getNode(B);
3478 assert(Node && "RPO and Dominator tree should have same reachability");
3479 RPOOrdering[Node] = ++Counter;
3480 }
3481 // Sort dominator tree children arrays into RPO.
3482 for (auto &B : RPOT) {
3483 auto *Node = DT->getNode(B);
3484 if (Node->getNumChildren() > 1)
3485 llvm::sort(*Node, [&](const DomTreeNode *A, const DomTreeNode *B) {
3486 return RPOOrdering[A] < RPOOrdering[B];
3487 });
3488 }
3489
3490 // Now a standard depth first ordering of the domtree is equivalent to RPO.
3491 for (auto *DTN : depth_first(DT->getRootNode())) {
3492 BasicBlock *B = DTN->getBlock();
3493 const auto &BlockRange = assignDFSNumbers(B, ICount);
3494 BlockInstRange.insert({B, BlockRange});
3495 ICount += BlockRange.second - BlockRange.first;
3496 }
3497 initializeCongruenceClasses(F);
3498
3499 TouchedInstructions.resize(ICount);
3500 // Ensure we don't end up resizing the expressionToClass map, as
3501 // that can be quite expensive. At most, we have one expression per
3502 // instruction.
3503 ExpressionToClass.reserve(ICount);
3504
3505 // Initialize the touched instructions to include the entry block.
3506 const auto &InstRange = BlockInstRange.lookup(&F.getEntryBlock());
3507 TouchedInstructions.set(InstRange.first, InstRange.second);
3508 LLVM_DEBUG(dbgs() << "Block " << getBlockName(&F.getEntryBlock())
3509 << " marked reachable\n");
3510 ReachableBlocks.insert(&F.getEntryBlock());
3511 // Use index corresponding to entry block.
3512 CacheIdx = 0;
3513
3514 iterateTouchedInstructions();
3515 verifyMemoryCongruency();
3516 verifyIterationSettled(F);
3517 verifyStoreExpressions();
3518
3519 Changed |= eliminateInstructions(F);
3520
3521 // Delete all instructions marked for deletion.
3522 for (Instruction *ToErase : InstructionsToErase) {
3523 if (!ToErase->use_empty())
3524 ToErase->replaceAllUsesWith(PoisonValue::get(ToErase->getType()));
3525
3526 assert(ToErase->getParent() &&
3527 "BB containing ToErase deleted unexpectedly!");
3528 ToErase->eraseFromParent();
3529 }
3530 Changed |= !InstructionsToErase.empty();
3531
3532 // Delete all unreachable blocks.
3533 auto UnreachableBlockPred = [&](const BasicBlock &BB) {
3534 return !ReachableBlocks.count(&BB);
3535 };
3536
3537 for (auto &BB : make_filter_range(F, UnreachableBlockPred)) {
3538 LLVM_DEBUG(dbgs() << "We believe block " << getBlockName(&BB)
3539 << " is unreachable\n");
3540 deleteInstructionsInBlock(&BB);
3541 Changed = true;
3542 }
3543
3544 cleanupTables();
3545 return Changed;
3546}
3547
3549 int DFSIn = 0;
3550 int DFSOut = 0;
3551 int LocalNum = 0;
3552
3553 // Only one of Def and U will be set.
3554 // The bool in the Def tells us whether the Def is the stored value of a
3555 // store.
3557 Use *U = nullptr;
3558
3559 bool operator<(const ValueDFS &Other) const {
3560 // It's not enough that any given field be less than - we have sets
3561 // of fields that need to be evaluated together to give a proper ordering.
3562 // For example, if you have;
3563 // DFS (1, 3)
3564 // Val 0
3565 // DFS (1, 2)
3566 // Val 50
3567 // We want the second to be less than the first, but if we just go field
3568 // by field, we will get to Val 0 < Val 50 and say the first is less than
3569 // the second. We only want it to be less than if the DFS orders are equal.
3570 //
3571 // Each LLVM instruction only produces one value, and thus the lowest-level
3572 // differentiator that really matters for the stack (and what we use as a
3573 // replacement) is the local dfs number.
3574 // Everything else in the structure is instruction level, and only affects
3575 // the order in which we will replace operands of a given instruction.
3576 //
3577 // For a given instruction (IE things with equal dfsin, dfsout, localnum),
3578 // the order of replacement of uses does not matter.
3579 // IE given,
3580 // a = 5
3581 // b = a + a
3582 // When you hit b, you will have two valuedfs with the same dfsin, out, and
3583 // localnum.
3584 // The .val will be the same as well.
3585 // The .u's will be different.
3586 // You will replace both, and it does not matter what order you replace them
3587 // in (IE whether you replace operand 2, then operand 1, or operand 1, then
3588 // operand 2).
3589 // Similarly for the case of same dfsin, dfsout, localnum, but different
3590 // .val's
3591 // a = 5
3592 // b = 6
3593 // c = a + b
3594 // in c, we will a valuedfs for a, and one for b,with everything the same
3595 // but .val and .u.
3596 // It does not matter what order we replace these operands in.
3597 // You will always end up with the same IR, and this is guaranteed.
3598 return std::tie(DFSIn, DFSOut, LocalNum, Def, U) <
3599 std::tie(Other.DFSIn, Other.DFSOut, Other.LocalNum, Other.Def,
3600 Other.U);
3601 }
3602};
3603
3604// This function converts the set of members for a congruence class from values,
3605// to sets of defs and uses with associated DFS info. The total number of
3606// reachable uses for each value is stored in UseCount, and instructions that
3607// seem
3608// dead (have no non-dead uses) are stored in ProbablyDead.
3609void NewGVN::convertClassToDFSOrdered(
3610 const CongruenceClass &Dense, SmallVectorImpl<ValueDFS> &DFSOrderedSet,
3612 SmallPtrSetImpl<Instruction *> &ProbablyDead) const {
3613 for (auto *D : Dense) {
3614 // First add the value.
3615 BasicBlock *BB = getBlockForValue(D);
3616 // Constants are handled prior to ever calling this function, so
3617 // we should only be left with instructions as members.
3618 assert(BB && "Should have figured out a basic block for value");
3619 ValueDFS VDDef;
3620 DomTreeNode *DomNode = DT->getNode(BB);
3621 VDDef.DFSIn = DomNode->getDFSNumIn();
3622 VDDef.DFSOut = DomNode->getDFSNumOut();
3623 // If it's a store, use the leader of the value operand, if it's always
3624 // available, or the value operand. TODO: We could do dominance checks to
3625 // find a dominating leader, but not worth it ATM.
3626 if (auto *SI = dyn_cast<StoreInst>(D)) {
3627 auto Leader = lookupOperandLeader(SI->getValueOperand());
3628 if (alwaysAvailable(Leader)) {
3629 VDDef.Def.setPointer(Leader);
3630 } else {
3631 VDDef.Def.setPointer(SI->getValueOperand());
3632 VDDef.Def.setInt(true);
3633 }
3634 } else {
3635 VDDef.Def.setPointer(D);
3636 }
3638 "The dense set member should always be an instruction");
3640 VDDef.LocalNum = InstrToDFSNum(D);
3641 DFSOrderedSet.push_back(VDDef);
3642 // If there is a phi node equivalent, add it
3643 if (auto *PN = RealToTemp.lookup(Def)) {
3644 auto *PHIE =
3645 dyn_cast_or_null<PHIExpression>(ValueToExpression.lookup(Def));
3646 if (PHIE) {
3647 VDDef.Def.setInt(false);
3648 VDDef.Def.setPointer(PN);
3649 VDDef.LocalNum = 0;
3650 DFSOrderedSet.push_back(VDDef);
3651 }
3652 }
3653
3654 unsigned int UseCount = 0;
3655 // Now add the uses.
3656 for (auto &U : Def->uses()) {
3657 if (auto *I = dyn_cast<Instruction>(U.getUser())) {
3658 // Don't try to replace into dead uses
3659 if (InstructionsToErase.count(I))
3660 continue;
3661 ValueDFS VDUse;
3662 // Put the phi node uses in the incoming block.
3663 BasicBlock *IBlock;
3664 if (auto *P = dyn_cast<PHINode>(I)) {
3665 IBlock = P->getIncomingBlock(U);
3666 // Make phi node users appear last in the incoming block
3667 // they are from.
3668 VDUse.LocalNum = InstrDFS.size() + 1;
3669 } else {
3670 IBlock = getBlockForValue(I);
3671 VDUse.LocalNum = InstrToDFSNum(I);
3672 }
3673
3674 // Skip uses in unreachable blocks, as we're going
3675 // to delete them.
3676 if (!ReachableBlocks.contains(IBlock))
3677 continue;
3678
3679 DomTreeNode *DomNode = DT->getNode(IBlock);
3680 VDUse.DFSIn = DomNode->getDFSNumIn();
3681 VDUse.DFSOut = DomNode->getDFSNumOut();
3682 VDUse.U = &U;
3683 ++UseCount;
3684 DFSOrderedSet.emplace_back(VDUse);
3685 }
3686 }
3687
3688 // If there are no uses, it's probably dead (but it may have side-effects,
3689 // so not definitely dead. Otherwise, store the number of uses so we can
3690 // track if it becomes dead later).
3691 if (UseCount == 0)
3692 ProbablyDead.insert(Def);
3693 else
3694 UseCounts[Def] = UseCount;
3695 }
3696}
3697
3698// This function converts the set of members for a congruence class from values,
3699// to the set of defs for loads and stores, with associated DFS info.
3700void NewGVN::convertClassToLoadsAndStores(
3701 const CongruenceClass &Dense,
3702 SmallVectorImpl<ValueDFS> &LoadsAndStores) const {
3703 for (auto *D : Dense) {
3704 if (!isa<LoadInst>(D) && !isa<StoreInst>(D))
3705 continue;
3706
3707 BasicBlock *BB = getBlockForValue(D);
3708 ValueDFS VD;
3709 DomTreeNode *DomNode = DT->getNode(BB);
3710 VD.DFSIn = DomNode->getDFSNumIn();
3711 VD.DFSOut = DomNode->getDFSNumOut();
3712 VD.Def.setPointer(D);
3713
3714 // If it's an instruction, use the real local dfs number.
3715 if (auto *I = dyn_cast<Instruction>(D))
3716 VD.LocalNum = InstrToDFSNum(I);
3717 else
3718 llvm_unreachable("Should have been an instruction");
3719
3720 LoadsAndStores.emplace_back(VD);
3721 }
3722}
3723
3726 I->replaceAllUsesWith(Repl);
3727}
3728
3729void NewGVN::deleteInstructionsInBlock(BasicBlock *BB) {
3730 LLVM_DEBUG(dbgs() << " BasicBlock Dead:" << *BB);
3731 ++NumGVNBlocksDeleted;
3732
3733 // Delete the instructions backwards, as it has a reduced likelihood of having
3734 // to update as many def-use and use-def chains. Start after the terminator.
3735 auto StartPoint = BB->rbegin();
3736 ++StartPoint;
3737 // Note that we explicitly recalculate BB->rend() on each iteration,
3738 // as it may change when we remove the first instruction.
3739 for (BasicBlock::reverse_iterator I(StartPoint); I != BB->rend();) {
3740 Instruction &Inst = *I++;
3741 if (!Inst.use_empty())
3743 if (isa<LandingPadInst>(Inst))
3744 continue;
3745 salvageKnowledge(&Inst, AC);
3746
3747 Inst.eraseFromParent();
3748 ++NumGVNInstrDeleted;
3749 }
3750 // Now insert something that simplifycfg will turn into an unreachable.
3751 Type *Int8Ty = Type::getInt8Ty(BB->getContext());
3752 new StoreInst(
3753 PoisonValue::get(Int8Ty),
3755 BB->getTerminator()->getIterator());
3756}
3757
3758void NewGVN::markInstructionForDeletion(Instruction *I) {
3759 LLVM_DEBUG(dbgs() << "Marking " << *I << " for deletion\n");
3760 InstructionsToErase.insert(I);
3761}
3762
3763void NewGVN::replaceInstruction(Instruction *I, Value *V) {
3764 LLVM_DEBUG(dbgs() << "Replacing " << *I << " with " << *V << "\n");
3766 // We save the actual erasing to avoid invalidating memory
3767 // dependencies until we are done with everything.
3768 markInstructionForDeletion(I);
3769}
3770
3771namespace {
3772
3773// This is a stack that contains both the value and dfs info of where
3774// that value is valid.
3775class ValueDFSStack {
3776public:
3777 Value *back() const { return ValueStack.back(); }
3778 std::pair<int, int> dfs_back() const { return DFSStack.back(); }
3779
3780 void push_back(Value *V, int DFSIn, int DFSOut) {
3781 ValueStack.emplace_back(V);
3782 DFSStack.emplace_back(DFSIn, DFSOut);
3783 }
3784
3785 bool empty() const { return DFSStack.empty(); }
3786
3787 bool isInScope(int DFSIn, int DFSOut) const {
3788 if (empty())
3789 return false;
3790 return DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second;
3791 }
3792
3793 void popUntilDFSScope(int DFSIn, int DFSOut) {
3794
3795 // These two should always be in sync at this point.
3796 assert(ValueStack.size() == DFSStack.size() &&
3797 "Mismatch between ValueStack and DFSStack");
3798 while (
3799 !DFSStack.empty() &&
3800 !(DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second)) {
3801 DFSStack.pop_back();
3802 ValueStack.pop_back();
3803 }
3804 }
3805
3806private:
3807 SmallVector<Value *, 8> ValueStack;
3809};
3810
3811} // end anonymous namespace
3812
3813// Given an expression, get the congruence class for it.
3814CongruenceClass *NewGVN::getClassForExpression(const Expression *E) const {
3815 if (auto *VE = dyn_cast<VariableExpression>(E))
3816 return ValueToClass.lookup(VE->getVariableValue());
3817 else if (isa<DeadExpression>(E))
3818 return TOPClass;
3819 return ExpressionToClass.lookup(E);
3820}
3821
3822// Given a value and a basic block we are trying to see if it is available in,
3823// see if the value has a leader available in that block.
3824Value *NewGVN::findPHIOfOpsLeader(const Expression *E,
3825 const Instruction *OrigInst,
3826 const BasicBlock *BB) const {
3827 // It would already be constant if we could make it constant
3828 if (auto *CE = dyn_cast<ConstantExpression>(E))
3829 return CE->getConstantValue();
3830 if (auto *VE = dyn_cast<VariableExpression>(E)) {
3831 auto *V = VE->getVariableValue();
3832 if (alwaysAvailable(V) || DT->dominates(getBlockForValue(V), BB))
3833 return VE->getVariableValue();
3834 }
3835
3836 auto *CC = getClassForExpression(E);
3837 if (!CC)
3838 return nullptr;
3839 if (alwaysAvailable(CC->getLeader()))
3840 return CC->getLeader();
3841
3842 for (auto *Member : *CC) {
3843 auto *MemberInst = dyn_cast<Instruction>(Member);
3844 if (MemberInst == OrigInst)
3845 continue;
3846 // Anything that isn't an instruction is always available.
3847 if (!MemberInst)
3848 return Member;
3849 if (DT->dominates(getBlockForValue(MemberInst), BB))
3850 return Member;
3851 }
3852 return nullptr;
3853}
3854
3855bool NewGVN::eliminateInstructions(Function &F) {
3856 // This is a non-standard eliminator. The normal way to eliminate is
3857 // to walk the dominator tree in order, keeping track of available
3858 // values, and eliminating them. However, this is mildly
3859 // pointless. It requires doing lookups on every instruction,
3860 // regardless of whether we will ever eliminate it. For
3861 // instructions part of most singleton congruence classes, we know we
3862 // will never eliminate them.
3863
3864 // Instead, this eliminator looks at the congruence classes directly, sorts
3865 // them into a DFS ordering of the dominator tree, and then we just
3866 // perform elimination straight on the sets by walking the congruence
3867 // class member uses in order, and eliminate the ones dominated by the
3868 // last member. This is worst case O(E log E) where E = number of
3869 // instructions in a single congruence class. In theory, this is all
3870 // instructions. In practice, it is much faster, as most instructions are
3871 // either in singleton congruence classes or can't possibly be eliminated
3872 // anyway (if there are no overlapping DFS ranges in class).
3873 // When we find something not dominated, it becomes the new leader
3874 // for elimination purposes.
3875 // TODO: If we wanted to be faster, We could remove any members with no
3876 // overlapping ranges while sorting, as we will never eliminate anything
3877 // with those members, as they don't dominate anything else in our set.
3878
3879 bool AnythingReplaced = false;
3880
3881 // Since we are going to walk the domtree anyway, and we can't guarantee the
3882 // DFS numbers are updated, we compute some ourselves.
3883 DT->updateDFSNumbers();
3884
3885 // Go through all of our phi nodes, and kill the arguments associated with
3886 // unreachable edges.
3887 auto ReplaceUnreachablePHIArgs = [&](PHINode *PHI, BasicBlock *BB) {
3888 for (auto &Operand : PHI->incoming_values())
3889 if (!ReachableEdges.count({PHI->getIncomingBlock(Operand), BB})) {
3890 LLVM_DEBUG(dbgs() << "Replacing incoming value of " << PHI
3891 << " for block "
3892 << getBlockName(PHI->getIncomingBlock(Operand))
3893 << " with poison due to it being unreachable\n");
3894 Operand.set(PoisonValue::get(PHI->getType()));
3895 }
3896 };
3897 // Replace unreachable phi arguments.
3898 // At this point, RevisitOnReachabilityChange only contains:
3899 //
3900 // 1. PHIs
3901 // 2. Temporaries that will convert to PHIs
3902 // 3. Operations that are affected by an unreachable edge but do not fit into
3903 // 1 or 2 (rare).
3904 // So it is a slight overshoot of what we want. We could make it exact by
3905 // using two SparseBitVectors per block.
3906 DenseMap<const BasicBlock *, unsigned> ReachablePredCount;
3907 for (auto &KV : ReachableEdges)
3908 ReachablePredCount[KV.getEnd()]++;
3909 for (auto &BBPair : RevisitOnReachabilityChange) {
3910 for (auto InstNum : BBPair.second) {
3911 auto *Inst = InstrFromDFSNum(InstNum);
3912 auto *PHI = dyn_cast<PHINode>(Inst);
3913 PHI = PHI ? PHI : dyn_cast_or_null<PHINode>(RealToTemp.lookup(Inst));
3914 if (!PHI)
3915 continue;
3916 auto *BB = BBPair.first;
3917 if (ReachablePredCount.lookup(BB) != PHI->getNumIncomingValues())
3918 ReplaceUnreachablePHIArgs(PHI, BB);
3919 }
3920 }
3921
3922 // Map to store the use counts
3923 DenseMap<const Value *, unsigned int> UseCounts;
3924 for (auto *CC : reverse(CongruenceClasses)) {
3925 LLVM_DEBUG(dbgs() << "Eliminating in congruence class " << CC->getID()
3926 << "\n");
3927 // Track the equivalent store info so we can decide whether to try
3928 // dead store elimination.
3929 SmallVector<ValueDFS, 8> PossibleDeadStores;
3930 SmallPtrSet<Instruction *, 8> ProbablyDead;
3931 if (CC->isDead() || CC->empty())
3932 continue;
3933 // Everything still in the TOP class is unreachable or dead.
3934 if (CC == TOPClass) {
3935 for (auto *M : *CC) {
3936 auto *VTE = ValueToExpression.lookup(M);
3937 if (VTE && isa<DeadExpression>(VTE))
3938 markInstructionForDeletion(cast<Instruction>(M));
3939 assert((!ReachableBlocks.count(cast<Instruction>(M)->getParent()) ||
3940 InstructionsToErase.count(cast<Instruction>(M))) &&
3941 "Everything in TOP should be unreachable or dead at this "
3942 "point");
3943 }
3944 continue;
3945 }
3946
3947 assert(CC->getLeader() && "We should have had a leader");
3948 // If this is a leader that is always available, and it's a
3949 // constant or has no equivalences, just replace everything with
3950 // it. We then update the congruence class with whatever members
3951 // are left.
3952 Value *Leader =
3953 CC->getStoredValue() ? CC->getStoredValue() : CC->getLeader();
3954 if (alwaysAvailable(Leader)) {
3955 CongruenceClass::MemberSet MembersLeft;
3956 for (auto *M : *CC) {
3957 Value *Member = M;
3958 // Void things have no uses we can replace.
3959 if (Member == Leader || !isa<Instruction>(Member) ||
3960 Member->getType()->isVoidTy()) {
3961 MembersLeft.insert(Member);
3962 continue;
3963 }
3964
3965 LLVM_DEBUG(dbgs() << "Found replacement " << *(Leader) << " for "
3966 << *Member << "\n");
3967 auto *I = cast<Instruction>(Member);
3968 assert(Leader != I && "About to accidentally remove our leader");
3969 replaceInstruction(I, Leader);
3970 AnythingReplaced = true;
3971 }
3972 CC->swap(MembersLeft);
3973 } else {
3974 // If this is a singleton, we can skip it.
3975 if (CC->size() != 1 || RealToTemp.count(Leader)) {
3976 // This is a stack because equality replacement/etc may place
3977 // constants in the middle of the member list, and we want to use
3978 // those constant values in preference to the current leader, over
3979 // the scope of those constants.
3980 ValueDFSStack EliminationStack;
3981
3982 // Convert the members to DFS ordered sets and then merge them.
3983 SmallVector<ValueDFS, 8> DFSOrderedSet;
3984 convertClassToDFSOrdered(*CC, DFSOrderedSet, UseCounts, ProbablyDead);
3985
3986 // Sort the whole thing.
3987 llvm::sort(DFSOrderedSet);
3988 for (auto &VD : DFSOrderedSet) {
3989 int MemberDFSIn = VD.DFSIn;
3990 int MemberDFSOut = VD.DFSOut;
3991 Value *Def = VD.Def.getPointer();
3992 bool FromStore = VD.Def.getInt();
3993 Use *U = VD.U;
3994 // We ignore void things because we can't get a value from them.
3995 if (Def && Def->getType()->isVoidTy())
3996 continue;
3997 auto *DefInst = dyn_cast_or_null<Instruction>(Def);
3998 if (DefInst && AllTempInstructions.count(DefInst)) {
3999 auto *PN = cast<PHINode>(DefInst);
4000
4001 // If this is a value phi and that's the expression we used, insert
4002 // it into the program
4003 // remove from temp instruction list.
4004 AllTempInstructions.erase(PN);
4005 auto *DefBlock = getBlockForValue(Def);
4006 LLVM_DEBUG(dbgs() << "Inserting fully real phi of ops" << *Def
4007 << " into block "
4008 << getBlockName(getBlockForValue(Def)) << "\n");
4009 PN->insertBefore(DefBlock->begin());
4010 Def = PN;
4011 NumGVNPHIOfOpsEliminations++;
4012 }
4013
4014 if (EliminationStack.empty()) {
4015 LLVM_DEBUG(dbgs() << "Elimination Stack is empty\n");
4016 } else {
4017 LLVM_DEBUG(dbgs() << "Elimination Stack Top DFS numbers are ("
4018 << EliminationStack.dfs_back().first << ","
4019 << EliminationStack.dfs_back().second << ")\n");
4020 }
4021
4022 LLVM_DEBUG(dbgs() << "Current DFS numbers are (" << MemberDFSIn << ","
4023 << MemberDFSOut << ")\n");
4024 // First, we see if we are out of scope or empty. If so,
4025 // and there equivalences, we try to replace the top of
4026 // stack with equivalences (if it's on the stack, it must
4027 // not have been eliminated yet).
4028 // Then we synchronize to our current scope, by
4029 // popping until we are back within a DFS scope that
4030 // dominates the current member.
4031 // Then, what happens depends on a few factors
4032 // If the stack is now empty, we need to push
4033 // If we have a constant or a local equivalence we want to
4034 // start using, we also push.
4035 // Otherwise, we walk along, processing members who are
4036 // dominated by this scope, and eliminate them.
4037 bool ShouldPush = Def && EliminationStack.empty();
4038 bool OutOfScope =
4039 !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut);
4040
4041 if (OutOfScope || ShouldPush) {
4042 // Sync to our current scope.
4043 EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
4044 bool ShouldPush = Def && EliminationStack.empty();
4045 if (ShouldPush) {
4046 EliminationStack.push_back(Def, MemberDFSIn, MemberDFSOut);
4047 }
4048 }
4049
4050 // Skip the Def's, we only want to eliminate on their uses. But mark
4051 // dominated defs as dead.
4052 if (Def) {
4053 // For anything in this case, what and how we value number
4054 // guarantees that any side-effects that would have occurred (ie
4055 // throwing, etc) can be proven to either still occur (because it's
4056 // dominated by something that has the same side-effects), or never
4057 // occur. Otherwise, we would not have been able to prove it value
4058 // equivalent to something else. For these things, we can just mark
4059 // it all dead. Note that this is different from the "ProbablyDead"
4060 // set, which may not be dominated by anything, and thus, are only
4061 // easy to prove dead if they are also side-effect free. Note that
4062 // because stores are put in terms of the stored value, we skip
4063 // stored values here. If the stored value is really dead, it will
4064 // still be marked for deletion when we process it in its own class.
4065 auto *DefI = dyn_cast<Instruction>(Def);
4066 if (!EliminationStack.empty() && DefI && !FromStore) {
4067 Value *DominatingLeader = EliminationStack.back();
4068 if (DominatingLeader != Def) {
4069 // Even if the instruction is removed, we still need to update
4070 // flags/metadata due to downstreams users of the leader.
4071 patchReplacementInstruction(DefI, DominatingLeader);
4072
4074 findDbgUsers(DefI, DVRUsers);
4075
4076 for (auto *DVR : DVRUsers)
4077 DVR->replaceVariableLocationOp(DefI, DominatingLeader);
4078
4079 markInstructionForDeletion(DefI);
4080 }
4081 }
4082 continue;
4083 }
4084 // At this point, we know it is a Use we are trying to possibly
4085 // replace.
4086
4087 assert(isa<Instruction>(U->get()) &&
4088 "Current def should have been an instruction");
4089 assert(isa<Instruction>(U->getUser()) &&
4090 "Current user should have been an instruction");
4091
4092 // If the thing we are replacing into is already marked to be dead,
4093 // this use is dead. Note that this is true regardless of whether
4094 // we have anything dominating the use or not. We do this here
4095 // because we are already walking all the uses anyway.
4096 Instruction *InstUse = cast<Instruction>(U->getUser());
4097 if (InstructionsToErase.count(InstUse)) {
4098 auto &UseCount = UseCounts[U->get()];
4099 if (--UseCount == 0) {
4100 ProbablyDead.insert(cast<Instruction>(U->get()));
4101 }
4102 }
4103
4104 // If we get to this point, and the stack is empty we must have a use
4105 // with nothing we can use to eliminate this use, so just skip it.
4106 if (EliminationStack.empty())
4107 continue;
4108
4109 Value *DominatingLeader = EliminationStack.back();
4110
4111 Instruction *SSACopy = nullptr;
4112 if (auto *BC = dyn_cast<BitCastInst>(DominatingLeader)) {
4113 if (BC->getType() == BC->getOperand(0)->getType() &&
4114 PredInfo->getPredicateInfoFor(DominatingLeader)) {
4115 SSACopy = BC;
4116 DominatingLeader = BC->getOperand(0);
4117 }
4118 }
4119
4120 // Don't replace our existing users with ourselves.
4121 if (U->get() == DominatingLeader)
4122 continue;
4123
4124 // If we replaced something in an instruction, handle the patching of
4125 // metadata. Skip this if we are replacing predicateinfo with its
4126 // original operand, as we already know we can just drop it.
4127 auto *ReplacedInst = cast<Instruction>(U->get());
4128 auto *PI = PredInfo->getPredicateInfoFor(ReplacedInst);
4129 if (!PI || DominatingLeader != PI->OriginalOp)
4130 patchReplacementInstruction(ReplacedInst, DominatingLeader);
4131
4133 << "Found replacement " << *DominatingLeader << " for "
4134 << *U->get() << " in " << *(U->getUser()) << "\n");
4135 U->set(DominatingLeader);
4136 // This is now a use of the dominating leader, which means if the
4137 // dominating leader was dead, it's now live!
4138 auto &LeaderUseCount = UseCounts[DominatingLeader];
4139 // It's about to be alive again.
4140 if (LeaderUseCount == 0 && isa<Instruction>(DominatingLeader))
4141 ProbablyDead.erase(cast<Instruction>(DominatingLeader));
4142 // For copy instructions, we use their operand as a leader,
4143 // which means we remove a user of the copy and it may become dead.
4144 if (SSACopy) {
4145 auto It = UseCounts.find(SSACopy);
4146 if (It != UseCounts.end()) {
4147 unsigned &IIUseCount = It->second;
4148 if (--IIUseCount == 0)
4149 ProbablyDead.insert(SSACopy);
4150 }
4151 }
4152 ++LeaderUseCount;
4153 AnythingReplaced = true;
4154 }
4155 }
4156 }
4157
4158 // At this point, anything still in the ProbablyDead set is actually dead if
4159 // would be trivially dead.
4160 for (auto *I : ProbablyDead)
4162 markInstructionForDeletion(I);
4163
4164 // Cleanup the congruence class.
4165 CongruenceClass::MemberSet MembersLeft;
4166 for (auto *Member : *CC)
4167 if (!isa<Instruction>(Member) ||
4168 !InstructionsToErase.count(cast<Instruction>(Member)))
4169 MembersLeft.insert(Member);
4170 CC->swap(MembersLeft);
4171
4172 // If we have possible dead stores to look at, try to eliminate them.
4173 if (CC->getStoreCount() > 0) {
4174 convertClassToLoadsAndStores(*CC, PossibleDeadStores);
4175 llvm::sort(PossibleDeadStores);
4176 ValueDFSStack EliminationStack;
4177 for (auto &VD : PossibleDeadStores) {
4178 int MemberDFSIn = VD.DFSIn;
4179 int MemberDFSOut = VD.DFSOut;
4180 Instruction *Member = cast<Instruction>(VD.Def.getPointer());
4181 if (EliminationStack.empty() ||
4182 !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut)) {
4183 // Sync to our current scope.
4184 EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
4185 if (EliminationStack.empty()) {
4186 EliminationStack.push_back(Member, MemberDFSIn, MemberDFSOut);
4187 continue;
4188 }
4189 }
4190 // We already did load elimination, so nothing to do here.
4191 if (isa<LoadInst>(Member))
4192 continue;
4193 assert(!EliminationStack.empty());
4194 Instruction *Leader = cast<Instruction>(EliminationStack.back());
4195 (void)Leader;
4196 assert(DT->dominates(Leader->getParent(), Member->getParent()));
4197 // Member is dominater by Leader, and thus dead
4198 LLVM_DEBUG(dbgs() << "Marking dead store " << *Member
4199 << " that is dominated by " << *Leader << "\n");
4200 markInstructionForDeletion(Member);
4201 CC->erase(Member);
4202 ++NumGVNDeadStores;
4203 }
4204 }
4205 }
4206 return AnythingReplaced;
4207}
4208
4209// This function provides global ranking of operations so that we can place them
4210// in a canonical order. Note that rank alone is not necessarily enough for a
4211// complete ordering, as constants all have the same rank. However, generally,
4212// we will simplify an operation with all constants so that it doesn't matter
4213// what order they appear in.
4214unsigned int NewGVN::getRank(const Value *V) const {
4215 // Prefer constants to undef to anything else
4216 // Undef is a constant, have to check it first.
4217 // Prefer poison to undef as it's less defined.
4218 // Prefer smaller constants to constantexprs
4219 // Note that the order here matters because of class inheritance
4220 if (isa<ConstantExpr>(V))
4221 return 3;
4222 if (isa<PoisonValue>(V))
4223 return 1;
4224 if (isa<UndefValue>(V))
4225 return 2;
4226 if (isa<Constant>(V))
4227 return 0;
4228 if (auto *A = dyn_cast<Argument>(V))
4229 return 4 + A->getArgNo();
4230
4231 // Need to shift the instruction DFS by number of arguments + 5 to account for
4232 // the constant and argument ranking above.
4233 unsigned Result = InstrToDFSNum(V);
4234 if (Result > 0)
4235 return 5 + NumFuncArgs + Result;
4236 // Unreachable or something else, just return a really large number.
4237 return ~0;
4238}
4239
4240// This is a function that says whether two commutative operations should
4241// have their order swapped when canonicalizing.
4242bool NewGVN::shouldSwapOperands(const Value *A, const Value *B) const {
4243 // Because we only care about a total ordering, and don't rewrite expressions
4244 // in this order, we order by rank, which will give a strict weak ordering to
4245 // everything but constants, and then we order by pointer address.
4246 return std::make_pair(getRank(A), A) > std::make_pair(getRank(B), B);
4247}
4248
4249bool NewGVN::shouldSwapOperandsForPredicate(const Value *A, const Value *B,
4250 const BitCastInst *I) const {
4251 if (shouldSwapOperands(A, B)) {
4252 PredicateSwapChoice[I] = B;
4253 return true;
4254 }
4255
4256 auto LookupResult = PredicateSwapChoice.find(I);
4257 if (LookupResult != PredicateSwapChoice.end()) {
4258 auto *SeenPredicate = LookupResult->second;
4259 if (SeenPredicate) {
4260 // We previously decided to swap B to the left. Keep that choice.
4261 if (SeenPredicate == B)
4262 return true;
4263 else
4264 LookupResult->second = nullptr;
4265 }
4266 }
4267 return false;
4268}
4269
4271 // Apparently the order in which we get these results matter for
4272 // the old GVN (see Chandler's comment in GVN.cpp). I'll keep
4273 // the same order here, just in case.
4274 auto &AC = AM.getResult<AssumptionAnalysis>(F);
4275 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
4276 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
4277 auto &AA = AM.getResult<AAManager>(F);
4278 auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
4279 bool Changed =
4280 NewGVN(F, &DT, &AC, &TLI, &AA, &MSSA, F.getDataLayout())
4281 .runGVN();
4282 if (!Changed)
4283 return PreservedAnalyses::all();
4286 return PA;
4287}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
Rewrite undef for PHI
Unify divergent function exit nodes
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis false
This file defines the BumpPtrAllocator interface.
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
This file defines DenseMapInfo traits for DenseMap.
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
early cse Early CSE w MemorySSA
The header file for the GVN pass that contains expression handling classes.
static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl)
Definition GVN.cpp:2144
This is the interface for a simple mod/ref and alias analysis over globals.
This file defines the little GraphTraits<X> template class that should be specialized by classes that...
Hexagon Common GEP
This defines the Use class.
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
Branch Probability Basic Block static false std::string getBlockName(const MachineBasicBlock *BB)
Helper to print the name of a MBB.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
static bool alwaysAvailable(Value *V)
Definition NewGVN.cpp:1040
static Value * getCopyOf(const Value *V)
Definition NewGVN.cpp:1010
static bool isCopyOfPHI(const Value *V, const PHINode *PN)
Definition NewGVN.cpp:1018
static bool isCopyOfAPHI(const Value *V)
Definition NewGVN.cpp:1022
static bool okayForPHIOfOps(const Instruction *I)
Definition NewGVN.cpp:2613
static cl::opt< bool > EnableStoreRefinement("enable-store-refinement", cl::init(false), cl::Hidden)
static bool equalsLoadStoreHelper(const T &LHS, const Expression &RHS)
Definition NewGVN.cpp:927
static cl::opt< bool > EnablePhiOfOps("enable-phi-of-ops", cl::init(true), cl::Hidden)
Currently, the generation "phi of ops" can result in correctness issues.
This file provides the interface for LLVM's Global Value Numbering pass.
#define P(N)
if(PassOpts->AAPipeline)
This file defines the PointerIntPair class.
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file implements the PredicateInfo analysis, which creates an Extended SSA form for operations us...
const SmallVectorImpl< MachineOperand > & Cond
bool isDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
This file defines generic set operations that may be used on set's of different types,...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the SparseBitVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
Value * RHS
Value * LHS
A manager for alias analyses.
bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
A trivial helper function to check to see if the specified pointers are must-alias.
bool doesNotAccessMemory(const CallBase *Call)
Checks if the specified call is known to never read or write memory.
bool onlyReadsMemory(const CallBase *Call)
Checks if the specified call is known to only read from non-volatile memory (or not access memory at ...
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Recycle small arrays allocated from a BumpPtrAllocator.
void clear(AllocatorType &Allocator)
Release all the tracked allocations to the allocator.
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
iterator begin() const
Definition ArrayRef.h:135
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
reverse_iterator rbegin()
Definition BasicBlock.h:475
InstListType::reverse_iterator reverse_iterator
Definition BasicBlock.h:172
reverse_iterator rend()
Definition BasicBlock.h:477
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
BitVector & reset()
Definition BitVector.h:411
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
Definition BitVector.h:319
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool any() const
any - Returns true if any bit is set.
Definition BitVector.h:189
iterator_range< const_set_bits_iterator > set_bits() const
Definition BitVector.h:159
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
Definition Allocator.h:124
void Deallocate(const void *Ptr, size_t Size, size_t)
Definition Allocator.h:226
bool isConvergent() const
Determine if the invoke is convergent.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:679
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:827
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:789
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition Constants.h:220
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:214
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
static CounterState getCounterState(unsigned ID)
static bool isCounterSet(unsigned ID)
static bool shouldExecute(unsigned CounterName)
static void setCounterState(unsigned ID, CounterState State)
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:194
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:167
bool erase(const KeyT &Val)
Definition DenseMap.h:311
unsigned size() const
Definition DenseMap.h:110
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:163
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:222
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
unsigned getDFSNumIn() const
getDFSNumIn/getDFSNumOut - These return the DFS visitation order for nodes in the dominator tree.
unsigned getDFSNumOut() const
Analysis pass which computes a DominatorTree.
Definition Dominators.h:284
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
void updateDFSNumbers() const
updateDFSNumbers - Assign In and Out numbers to the nodes while walking dominator tree in dfs order.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Class representing an expression and its matching format.
bool isPresplitCoroutine() const
Determine if the function is presplit coroutine.
Definition Function.h:539
void allocateOperands(RecyclerType &Recycler, BumpPtrAllocator &Allocator)
bool equals(const Expression &Other) const override
Definition NewGVN.cpp:947
void setOpcode(unsigned opcode)
bool equals(const Expression &Other) const override
Definition NewGVN.cpp:933
bool equals(const Expression &Other) const override
bool equals(const Expression &Other) const override
Definition NewGVN.cpp:937
static LLVM_ABI std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
LLVM_ABI bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
Value * getPointerOperand()
bool isSimple() const
BasicBlock * getBlock() const
Definition MemorySSA.h:162
BasicBlock * getIncomingBlock(unsigned I) const
Return incoming basic block number i.
Definition MemorySSA.h:542
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:936
This is the generic walker interface for walkers of MemorySSA.
Definition MemorySSA.h:1024
MemoryAccess * getClobberingMemoryAccess(const Instruction *I, BatchAAResults &AA)
Given a memory Mod/Ref/ModRef'ing instruction, calling this will give you the nearest dominating Memo...
Definition MemorySSA.h:1053
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition MemorySSA.h:702
LLVM_ABI MemorySSAWalker * getWalker()
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition MemorySSA.h:720
MemoryAccess * getLiveOnEntryDef() const
Definition MemorySSA.h:744
const DefsList * getBlockDefs(const BasicBlock *BB) const
Return the list of MemoryDef's and MemoryPhi's for a given basic block.
Definition MemorySSA.h:768
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
Definition MemorySSA.h:740
PreservedAnalyses run(Function &F, AnalysisManager< Function > &AM)
Run the pass over the function.
Definition NewGVN.cpp:4270
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
PointerIntPair - This class implements a pair of a pointer and small integer.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
SmallPtrSetIterator< PtrType > const_iterator
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:295
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_range operands()
Definition User.h:292
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
iterator_range< user_iterator > users()
Definition Value.h:426
bool use_empty() const
Definition Value.h:346
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool erase(const ValueT &V)
Definition DenseSet.h:100
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:180
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
Changed
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
bool match(Val *V, const Pattern &P)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
int analyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr, StoreInst *DepSI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the store at D...
Constant * getConstantValueForLoad(Constant *SrcVal, unsigned Offset, Type *LoadTy, const DataLayout &DL)
int analyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr, LoadInst *DepLI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the load at De...
Constant * getConstantMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset, Type *LoadTy, const DataLayout &DL)
int analyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr, MemIntrinsic *DepMI, const DataLayout &DL)
This function determines whether a value for the pointer LoadPtr can be extracted from the memory int...
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:48
initializer< Ty > init(const Ty &Val)
std::vector< std::optional< ExecutorSymbolDef > > LookupResult
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
bool empty() const
Definition BasicBlock.h:101
iterator end() const
Definition BasicBlock.h:89
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
LLVM_ABI Instruction & back() const
LLVM_ABI iterator begin() const
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
LLVM_ABI Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, GEPNoWrapFlags NW, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
LLVM_ABI Constant * getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty)
If this is a call to an allocation function that initializes memory to a fixed value,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1655
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
Definition Utils.cpp:1725
auto successors(const MachineBasicBlock *BB)
SDValue getStoredValue(SDValue Op)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
mapped_iterator< ItTy, FuncTy > map_iterator(ItTy I, FuncTy F)
Definition STLExtras.h:359
bool set_is_subset(const S1Ty &S1, const S2Ty &S2)
set_is_subset(A, B) - Return true iff A in B
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:677
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI Value * simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:95
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:754
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition STLExtras.h:2128
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
Definition STLExtras.h:1968
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1732
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition Local.cpp:402
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI bool wouldInstructionBeTriviallyDead(const Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction would have no side effects if it was not used.
Definition Local.cpp:421
LLVM_ABI void patchReplacementInstruction(Instruction *I, Value *Repl)
Patch the replacement so that it is not more restrictive than the value being replaced.
Definition Local.cpp:3156
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition STLExtras.h:550
BumpPtrAllocatorImpl BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
LLVM_ABI bool salvageKnowledge(Instruction *I, AssumptionCache *AC=nullptr, DominatorTree *DT=nullptr)
Calls BuildAssumeFromInst and if the resulting llvm.assume is valid insert if before I.
LLVM_ABI Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1835
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
iterator_range< df_iterator< T > > depth_first(const T &G)
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2108
LLVM_ABI Value * simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI Constant * ConstantFoldInstOperands(const Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
LLVM_ABI Value * simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const SimplifyQuery &Q)
Given operands for a SelectInst, fold the result or return null.
LLVM_ABI void findDbgUsers(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the debug info records describing a value.
iterator_range< def_chain_iterator< T, true > > optimized_def_chain(T MA)
Definition MemorySSA.h:1372
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
PointerIntPair< Value *, 1, bool > Def
Definition NewGVN.cpp:3556
bool operator<(const ValueDFS &Other) const
Definition NewGVN.cpp:3559
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
static unsigned getHashValue(const ExactEqualsExpression &E)
Definition NewGVN.cpp:467
static unsigned getHashValue(const Expression *E)
Definition NewGVN.cpp:463
static const Expression * getTombstoneKey()
Definition NewGVN.cpp:457
static bool isEqual(const Expression *LHS, const Expression *RHS)
Definition NewGVN.cpp:477
static const Expression * getEmptyKey()
Definition NewGVN.cpp:451
static bool isEqual(const ExactEqualsExpression &LHS, const Expression *RHS)
Definition NewGVN.cpp:471
An information struct used to provide DenseMap with the various necessary components for a given valu...
SimplifyQuery getWithInstruction(const Instruction *I) const
unsigned int LocalNum