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