xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Scalar/NewGVN.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- NewGVN.cpp - Global Value Numbering Pass ---------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file implements the new LLVM's Global Value Numbering pass.
110b57cec5SDimitry Andric /// GVN partitions values computed by a function into congruence classes.
120b57cec5SDimitry Andric /// Values ending up in the same congruence class are guaranteed to be the same
130b57cec5SDimitry Andric /// for every execution of the program. In that respect, congruency is a
140b57cec5SDimitry Andric /// compile-time approximation of equivalence of values at runtime.
150b57cec5SDimitry Andric /// The algorithm implemented here uses a sparse formulation and it's based
160b57cec5SDimitry Andric /// on the ideas described in the paper:
170b57cec5SDimitry Andric /// "A Sparse Algorithm for Predicated Global Value Numbering" from
180b57cec5SDimitry Andric /// Karthik Gargi.
190b57cec5SDimitry Andric ///
200b57cec5SDimitry Andric /// A brief overview of the algorithm: The algorithm is essentially the same as
210b57cec5SDimitry Andric /// the standard RPO value numbering algorithm (a good reference is the paper
220b57cec5SDimitry Andric /// "SCC based value numbering" by L. Taylor Simpson) with one major difference:
230b57cec5SDimitry Andric /// The RPO algorithm proceeds, on every iteration, to process every reachable
240b57cec5SDimitry Andric /// block and every instruction in that block.  This is because the standard RPO
250b57cec5SDimitry Andric /// algorithm does not track what things have the same value number, it only
260b57cec5SDimitry Andric /// tracks what the value number of a given operation is (the mapping is
270b57cec5SDimitry Andric /// operation -> value number).  Thus, when a value number of an operation
280b57cec5SDimitry Andric /// changes, it must reprocess everything to ensure all uses of a value number
290b57cec5SDimitry Andric /// get updated properly.  In constrast, the sparse algorithm we use *also*
300b57cec5SDimitry Andric /// tracks what operations have a given value number (IE it also tracks the
310b57cec5SDimitry Andric /// reverse mapping from value number -> operations with that value number), so
320b57cec5SDimitry Andric /// that it only needs to reprocess the instructions that are affected when
330b57cec5SDimitry Andric /// something's value number changes.  The vast majority of complexity and code
340b57cec5SDimitry Andric /// in this file is devoted to tracking what value numbers could change for what
350b57cec5SDimitry Andric /// instructions when various things happen.  The rest of the algorithm is
360b57cec5SDimitry Andric /// devoted to performing symbolic evaluation, forward propagation, and
370b57cec5SDimitry Andric /// simplification of operations based on the value numbers deduced so far
380b57cec5SDimitry Andric ///
390b57cec5SDimitry Andric /// In order to make the GVN mostly-complete, we use a technique derived from
400b57cec5SDimitry Andric /// "Detection of Redundant Expressions: A Complete and Polynomial-time
410b57cec5SDimitry Andric /// Algorithm in SSA" by R.R. Pai.  The source of incompleteness in most SSA
420b57cec5SDimitry Andric /// based GVN algorithms is related to their inability to detect equivalence
430b57cec5SDimitry Andric /// between phi of ops (IE phi(a+b, c+d)) and op of phis (phi(a,c) + phi(b, d)).
440b57cec5SDimitry Andric /// We resolve this issue by generating the equivalent "phi of ops" form for
450b57cec5SDimitry Andric /// each op of phis we see, in a way that only takes polynomial time to resolve.
460b57cec5SDimitry Andric ///
470b57cec5SDimitry Andric /// We also do not perform elimination by using any published algorithm.  All
480b57cec5SDimitry Andric /// published algorithms are O(Instructions). Instead, we use a technique that
490b57cec5SDimitry Andric /// is O(number of operations with the same value number), enabling us to skip
500b57cec5SDimitry Andric /// trying to eliminate things that have unique value numbers.
510b57cec5SDimitry Andric //
520b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/NewGVN.h"
550b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
560b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
570b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
580b57cec5SDimitry Andric #include "llvm/ADT/DenseMapInfo.h"
590b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
600b57cec5SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h"
610b57cec5SDimitry Andric #include "llvm/ADT/GraphTraits.h"
620b57cec5SDimitry Andric #include "llvm/ADT/Hashing.h"
630b57cec5SDimitry Andric #include "llvm/ADT/PointerIntPair.h"
640b57cec5SDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
65fe6060f1SDimitry Andric #include "llvm/ADT/SetOperations.h"
660b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
670b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
680b57cec5SDimitry Andric #include "llvm/ADT/SparseBitVector.h"
690b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
700b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
710b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
720b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
730b57cec5SDimitry Andric #include "llvm/Analysis/CFGPrinter.h"
740b57cec5SDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
750b57cec5SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
760b57cec5SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
770b57cec5SDimitry Andric #include "llvm/Analysis/MemoryBuiltins.h"
780b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSA.h"
790b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
801fd87a68SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
810b57cec5SDimitry Andric #include "llvm/IR/Argument.h"
820b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
830b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
840b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
850b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
860b57cec5SDimitry Andric #include "llvm/IR/Function.h"
870b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
880b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
890b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
900b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
918bcb0991SDimitry Andric #include "llvm/IR/PatternMatch.h"
920b57cec5SDimitry Andric #include "llvm/IR/Type.h"
930b57cec5SDimitry Andric #include "llvm/IR/Use.h"
940b57cec5SDimitry Andric #include "llvm/IR/User.h"
950b57cec5SDimitry Andric #include "llvm/IR/Value.h"
960b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
970b57cec5SDimitry Andric #include "llvm/Support/ArrayRecycler.h"
980b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
990b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
1000b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
1010b57cec5SDimitry Andric #include "llvm/Support/DebugCounter.h"
1020b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
1030b57cec5SDimitry Andric #include "llvm/Support/PointerLikeTypeTraits.h"
1040b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
1050b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/GVNExpression.h"
1065ffd83dbSDimitry Andric #include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
107480093f4SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
1080b57cec5SDimitry Andric #include "llvm/Transforms/Utils/PredicateInfo.h"
1090b57cec5SDimitry Andric #include "llvm/Transforms/Utils/VNCoercion.h"
1100b57cec5SDimitry Andric #include <algorithm>
1110b57cec5SDimitry Andric #include <cassert>
1120b57cec5SDimitry Andric #include <cstdint>
1130b57cec5SDimitry Andric #include <iterator>
1140b57cec5SDimitry Andric #include <map>
1150b57cec5SDimitry Andric #include <memory>
1160b57cec5SDimitry Andric #include <set>
1170b57cec5SDimitry Andric #include <string>
1180b57cec5SDimitry Andric #include <tuple>
1190b57cec5SDimitry Andric #include <utility>
1200b57cec5SDimitry Andric #include <vector>
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric using namespace llvm;
1230b57cec5SDimitry Andric using namespace llvm::GVNExpression;
1240b57cec5SDimitry Andric using namespace llvm::VNCoercion;
1258bcb0991SDimitry Andric using namespace llvm::PatternMatch;
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric #define DEBUG_TYPE "newgvn"
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric STATISTIC(NumGVNInstrDeleted, "Number of instructions deleted");
1300b57cec5SDimitry Andric STATISTIC(NumGVNBlocksDeleted, "Number of blocks deleted");
1310b57cec5SDimitry Andric STATISTIC(NumGVNOpsSimplified, "Number of Expressions simplified");
1320b57cec5SDimitry Andric STATISTIC(NumGVNPhisAllSame, "Number of PHIs whos arguments are all the same");
1330b57cec5SDimitry Andric STATISTIC(NumGVNMaxIterations,
1340b57cec5SDimitry Andric           "Maximum Number of iterations it took to converge GVN");
1350b57cec5SDimitry Andric STATISTIC(NumGVNLeaderChanges, "Number of leader changes");
1360b57cec5SDimitry Andric STATISTIC(NumGVNSortedLeaderChanges, "Number of sorted leader changes");
1370b57cec5SDimitry Andric STATISTIC(NumGVNAvoidedSortedLeaderChanges,
1380b57cec5SDimitry Andric           "Number of avoided sorted leader changes");
1390b57cec5SDimitry Andric STATISTIC(NumGVNDeadStores, "Number of redundant/dead stores eliminated");
1400b57cec5SDimitry Andric STATISTIC(NumGVNPHIOfOpsCreated, "Number of PHI of ops created");
1410b57cec5SDimitry Andric STATISTIC(NumGVNPHIOfOpsEliminations,
1420b57cec5SDimitry Andric           "Number of things eliminated using PHI of ops");
1430b57cec5SDimitry Andric DEBUG_COUNTER(VNCounter, "newgvn-vn",
1440b57cec5SDimitry Andric               "Controls which instructions are value numbered");
1450b57cec5SDimitry Andric DEBUG_COUNTER(PHIOfOpsCounter, "newgvn-phi",
1460b57cec5SDimitry Andric               "Controls which instructions we create phi of ops for");
1470b57cec5SDimitry Andric // Currently store defining access refinement is too slow due to basicaa being
1480b57cec5SDimitry Andric // egregiously slow.  This flag lets us keep it working while we work on this
1490b57cec5SDimitry Andric // issue.
1500b57cec5SDimitry Andric static cl::opt<bool> EnableStoreRefinement("enable-store-refinement",
1510b57cec5SDimitry Andric                                            cl::init(false), cl::Hidden);
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric /// Currently, the generation "phi of ops" can result in correctness issues.
1540b57cec5SDimitry Andric static cl::opt<bool> EnablePhiOfOps("enable-phi-of-ops", cl::init(true),
1550b57cec5SDimitry Andric                                     cl::Hidden);
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1580b57cec5SDimitry Andric //                                GVN Pass
1590b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric // Anchor methods.
1620b57cec5SDimitry Andric namespace llvm {
1630b57cec5SDimitry Andric namespace GVNExpression {
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric Expression::~Expression() = default;
1660b57cec5SDimitry Andric BasicExpression::~BasicExpression() = default;
1670b57cec5SDimitry Andric CallExpression::~CallExpression() = default;
1680b57cec5SDimitry Andric LoadExpression::~LoadExpression() = default;
1690b57cec5SDimitry Andric StoreExpression::~StoreExpression() = default;
1700b57cec5SDimitry Andric AggregateValueExpression::~AggregateValueExpression() = default;
1710b57cec5SDimitry Andric PHIExpression::~PHIExpression() = default;
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric } // end namespace GVNExpression
1740b57cec5SDimitry Andric } // end namespace llvm
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric namespace {
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric // Tarjan's SCC finding algorithm with Nuutila's improvements
1790b57cec5SDimitry Andric // SCCIterator is actually fairly complex for the simple thing we want.
1800b57cec5SDimitry Andric // It also wants to hand us SCC's that are unrelated to the phi node we ask
1810b57cec5SDimitry Andric // about, and have us process them there or risk redoing work.
1820b57cec5SDimitry Andric // Graph traits over a filter iterator also doesn't work that well here.
1830b57cec5SDimitry Andric // This SCC finder is specialized to walk use-def chains, and only follows
1840b57cec5SDimitry Andric // instructions,
1850b57cec5SDimitry Andric // not generic values (arguments, etc).
1860b57cec5SDimitry Andric struct TarjanSCC {
TarjanSCC__anonb3555ec80111::TarjanSCC1870b57cec5SDimitry Andric   TarjanSCC() : Components(1) {}
1880b57cec5SDimitry Andric 
Start__anonb3555ec80111::TarjanSCC1890b57cec5SDimitry Andric   void Start(const Instruction *Start) {
1900b57cec5SDimitry Andric     if (Root.lookup(Start) == 0)
1910b57cec5SDimitry Andric       FindSCC(Start);
1920b57cec5SDimitry Andric   }
1930b57cec5SDimitry Andric 
getComponentFor__anonb3555ec80111::TarjanSCC1940b57cec5SDimitry Andric   const SmallPtrSetImpl<const Value *> &getComponentFor(const Value *V) const {
1950b57cec5SDimitry Andric     unsigned ComponentID = ValueToComponent.lookup(V);
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric     assert(ComponentID > 0 &&
1980b57cec5SDimitry Andric            "Asking for a component for a value we never processed");
1990b57cec5SDimitry Andric     return Components[ComponentID];
2000b57cec5SDimitry Andric   }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric private:
FindSCC__anonb3555ec80111::TarjanSCC2030b57cec5SDimitry Andric   void FindSCC(const Instruction *I) {
2040b57cec5SDimitry Andric     Root[I] = ++DFSNum;
2050b57cec5SDimitry Andric     // Store the DFS Number we had before it possibly gets incremented.
2060b57cec5SDimitry Andric     unsigned int OurDFS = DFSNum;
207bdd1243dSDimitry Andric     for (const auto &Op : I->operands()) {
2080b57cec5SDimitry Andric       if (auto *InstOp = dyn_cast<Instruction>(Op)) {
2090b57cec5SDimitry Andric         if (Root.lookup(Op) == 0)
2100b57cec5SDimitry Andric           FindSCC(InstOp);
2110b57cec5SDimitry Andric         if (!InComponent.count(Op))
2120b57cec5SDimitry Andric           Root[I] = std::min(Root.lookup(I), Root.lookup(Op));
2130b57cec5SDimitry Andric       }
2140b57cec5SDimitry Andric     }
2150b57cec5SDimitry Andric     // See if we really were the root of a component, by seeing if we still have
2160b57cec5SDimitry Andric     // our DFSNumber.  If we do, we are the root of the component, and we have
2170b57cec5SDimitry Andric     // completed a component. If we do not, we are not the root of a component,
2180b57cec5SDimitry Andric     // and belong on the component stack.
2190b57cec5SDimitry Andric     if (Root.lookup(I) == OurDFS) {
2200b57cec5SDimitry Andric       unsigned ComponentID = Components.size();
2210b57cec5SDimitry Andric       Components.resize(Components.size() + 1);
2220b57cec5SDimitry Andric       auto &Component = Components.back();
2230b57cec5SDimitry Andric       Component.insert(I);
2240b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Component root is " << *I << "\n");
2250b57cec5SDimitry Andric       InComponent.insert(I);
2260b57cec5SDimitry Andric       ValueToComponent[I] = ComponentID;
2270b57cec5SDimitry Andric       // Pop a component off the stack and label it.
2280b57cec5SDimitry Andric       while (!Stack.empty() && Root.lookup(Stack.back()) >= OurDFS) {
2290b57cec5SDimitry Andric         auto *Member = Stack.back();
2300b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Component member is " << *Member << "\n");
2310b57cec5SDimitry Andric         Component.insert(Member);
2320b57cec5SDimitry Andric         InComponent.insert(Member);
2330b57cec5SDimitry Andric         ValueToComponent[Member] = ComponentID;
2340b57cec5SDimitry Andric         Stack.pop_back();
2350b57cec5SDimitry Andric       }
2360b57cec5SDimitry Andric     } else {
2370b57cec5SDimitry Andric       // Part of a component, push to stack
2380b57cec5SDimitry Andric       Stack.push_back(I);
2390b57cec5SDimitry Andric     }
2400b57cec5SDimitry Andric   }
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   unsigned int DFSNum = 1;
2430b57cec5SDimitry Andric   SmallPtrSet<const Value *, 8> InComponent;
2440b57cec5SDimitry Andric   DenseMap<const Value *, unsigned int> Root;
2450b57cec5SDimitry Andric   SmallVector<const Value *, 8> Stack;
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   // Store the components as vector of ptr sets, because we need the topo order
2480b57cec5SDimitry Andric   // of SCC's, but not individual member order
2490b57cec5SDimitry Andric   SmallVector<SmallPtrSet<const Value *, 8>, 8> Components;
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric   DenseMap<const Value *, unsigned> ValueToComponent;
2520b57cec5SDimitry Andric };
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric // Congruence classes represent the set of expressions/instructions
2550b57cec5SDimitry Andric // that are all the same *during some scope in the function*.
2560b57cec5SDimitry Andric // That is, because of the way we perform equality propagation, and
2570b57cec5SDimitry Andric // because of memory value numbering, it is not correct to assume
2580b57cec5SDimitry Andric // you can willy-nilly replace any member with any other at any
2590b57cec5SDimitry Andric // point in the function.
2600b57cec5SDimitry Andric //
2610b57cec5SDimitry Andric // For any Value in the Member set, it is valid to replace any dominated member
2620b57cec5SDimitry Andric // with that Value.
2630b57cec5SDimitry Andric //
2640b57cec5SDimitry Andric // Every congruence class has a leader, and the leader is used to symbolize
2650b57cec5SDimitry Andric // instructions in a canonical way (IE every operand of an instruction that is a
2660b57cec5SDimitry Andric // member of the same congruence class will always be replaced with leader
2670b57cec5SDimitry Andric // during symbolization).  To simplify symbolization, we keep the leader as a
2680b57cec5SDimitry Andric // constant if class can be proved to be a constant value.  Otherwise, the
2690b57cec5SDimitry Andric // leader is the member of the value set with the smallest DFS number.  Each
2700b57cec5SDimitry Andric // congruence class also has a defining expression, though the expression may be
2710b57cec5SDimitry Andric // null.  If it exists, it can be used for forward propagation and reassociation
2720b57cec5SDimitry Andric // of values.
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric // For memory, we also track a representative MemoryAccess, and a set of memory
2750b57cec5SDimitry Andric // members for MemoryPhis (which have no real instructions). Note that for
2760b57cec5SDimitry Andric // memory, it seems tempting to try to split the memory members into a
2770b57cec5SDimitry Andric // MemoryCongruenceClass or something.  Unfortunately, this does not work
2780b57cec5SDimitry Andric // easily.  The value numbering of a given memory expression depends on the
2790b57cec5SDimitry Andric // leader of the memory congruence class, and the leader of memory congruence
2800b57cec5SDimitry Andric // class depends on the value numbering of a given memory expression.  This
2810b57cec5SDimitry Andric // leads to wasted propagation, and in some cases, missed optimization.  For
2820b57cec5SDimitry Andric // example: If we had value numbered two stores together before, but now do not,
2830b57cec5SDimitry Andric // we move them to a new value congruence class.  This in turn will move at one
2840b57cec5SDimitry Andric // of the memorydefs to a new memory congruence class.  Which in turn, affects
2850b57cec5SDimitry Andric // the value numbering of the stores we just value numbered (because the memory
2860b57cec5SDimitry Andric // congruence class is part of the value number).  So while theoretically
2870b57cec5SDimitry Andric // possible to split them up, it turns out to be *incredibly* complicated to get
2880b57cec5SDimitry Andric // it to work right, because of the interdependency.  While structurally
2890b57cec5SDimitry Andric // slightly messier, it is algorithmically much simpler and faster to do what we
2900b57cec5SDimitry Andric // do here, and track them both at once in the same class.
2910b57cec5SDimitry Andric // Note: The default iterators for this class iterate over values
2920b57cec5SDimitry Andric class CongruenceClass {
2930b57cec5SDimitry Andric public:
2940b57cec5SDimitry Andric   using MemberType = Value;
2950b57cec5SDimitry Andric   using MemberSet = SmallPtrSet<MemberType *, 4>;
2960b57cec5SDimitry Andric   using MemoryMemberType = MemoryPhi;
2970b57cec5SDimitry Andric   using MemoryMemberSet = SmallPtrSet<const MemoryMemberType *, 2>;
2980b57cec5SDimitry Andric 
CongruenceClass(unsigned ID)2990b57cec5SDimitry Andric   explicit CongruenceClass(unsigned ID) : ID(ID) {}
CongruenceClass(unsigned ID,Value * Leader,const Expression * E)3000b57cec5SDimitry Andric   CongruenceClass(unsigned ID, Value *Leader, const Expression *E)
3010b57cec5SDimitry Andric       : ID(ID), RepLeader(Leader), DefiningExpr(E) {}
3020b57cec5SDimitry Andric 
getID() const3030b57cec5SDimitry Andric   unsigned getID() const { return ID; }
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric   // True if this class has no members left.  This is mainly used for assertion
3060b57cec5SDimitry Andric   // purposes, and for skipping empty classes.
isDead() const3070b57cec5SDimitry Andric   bool isDead() const {
3080b57cec5SDimitry Andric     // If it's both dead from a value perspective, and dead from a memory
3090b57cec5SDimitry Andric     // perspective, it's really dead.
3100b57cec5SDimitry Andric     return empty() && memory_empty();
3110b57cec5SDimitry Andric   }
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   // Leader functions
getLeader() const3140b57cec5SDimitry Andric   Value *getLeader() const { return RepLeader; }
setLeader(Value * Leader)3150b57cec5SDimitry Andric   void setLeader(Value *Leader) { RepLeader = Leader; }
getNextLeader() const3160b57cec5SDimitry Andric   const std::pair<Value *, unsigned int> &getNextLeader() const {
3170b57cec5SDimitry Andric     return NextLeader;
3180b57cec5SDimitry Andric   }
resetNextLeader()3190b57cec5SDimitry Andric   void resetNextLeader() { NextLeader = {nullptr, ~0}; }
addPossibleNextLeader(std::pair<Value *,unsigned int> LeaderPair)3200b57cec5SDimitry Andric   void addPossibleNextLeader(std::pair<Value *, unsigned int> LeaderPair) {
3210b57cec5SDimitry Andric     if (LeaderPair.second < NextLeader.second)
3220b57cec5SDimitry Andric       NextLeader = LeaderPair;
3230b57cec5SDimitry Andric   }
3240b57cec5SDimitry Andric 
getStoredValue() const3250b57cec5SDimitry Andric   Value *getStoredValue() const { return RepStoredValue; }
setStoredValue(Value * Leader)3260b57cec5SDimitry Andric   void setStoredValue(Value *Leader) { RepStoredValue = Leader; }
getMemoryLeader() const3270b57cec5SDimitry Andric   const MemoryAccess *getMemoryLeader() const { return RepMemoryAccess; }
setMemoryLeader(const MemoryAccess * Leader)3280b57cec5SDimitry Andric   void setMemoryLeader(const MemoryAccess *Leader) { RepMemoryAccess = Leader; }
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   // Forward propagation info
getDefiningExpr() const3310b57cec5SDimitry Andric   const Expression *getDefiningExpr() const { return DefiningExpr; }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   // Value member set
empty() const3340b57cec5SDimitry Andric   bool empty() const { return Members.empty(); }
size() const3350b57cec5SDimitry Andric   unsigned size() const { return Members.size(); }
begin() const3360b57cec5SDimitry Andric   MemberSet::const_iterator begin() const { return Members.begin(); }
end() const3370b57cec5SDimitry Andric   MemberSet::const_iterator end() const { return Members.end(); }
insert(MemberType * M)3380b57cec5SDimitry Andric   void insert(MemberType *M) { Members.insert(M); }
erase(MemberType * M)3390b57cec5SDimitry Andric   void erase(MemberType *M) { Members.erase(M); }
swap(MemberSet & Other)3400b57cec5SDimitry Andric   void swap(MemberSet &Other) { Members.swap(Other); }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   // Memory member set
memory_empty() const3430b57cec5SDimitry Andric   bool memory_empty() const { return MemoryMembers.empty(); }
memory_size() const3440b57cec5SDimitry Andric   unsigned memory_size() const { return MemoryMembers.size(); }
memory_begin() const3450b57cec5SDimitry Andric   MemoryMemberSet::const_iterator memory_begin() const {
3460b57cec5SDimitry Andric     return MemoryMembers.begin();
3470b57cec5SDimitry Andric   }
memory_end() const3480b57cec5SDimitry Andric   MemoryMemberSet::const_iterator memory_end() const {
3490b57cec5SDimitry Andric     return MemoryMembers.end();
3500b57cec5SDimitry Andric   }
memory() const3510b57cec5SDimitry Andric   iterator_range<MemoryMemberSet::const_iterator> memory() const {
3520b57cec5SDimitry Andric     return make_range(memory_begin(), memory_end());
3530b57cec5SDimitry Andric   }
3540b57cec5SDimitry Andric 
memory_insert(const MemoryMemberType * M)3550b57cec5SDimitry Andric   void memory_insert(const MemoryMemberType *M) { MemoryMembers.insert(M); }
memory_erase(const MemoryMemberType * M)3560b57cec5SDimitry Andric   void memory_erase(const MemoryMemberType *M) { MemoryMembers.erase(M); }
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric   // Store count
getStoreCount() const3590b57cec5SDimitry Andric   unsigned getStoreCount() const { return StoreCount; }
incStoreCount()3600b57cec5SDimitry Andric   void incStoreCount() { ++StoreCount; }
decStoreCount()3610b57cec5SDimitry Andric   void decStoreCount() {
3620b57cec5SDimitry Andric     assert(StoreCount != 0 && "Store count went negative");
3630b57cec5SDimitry Andric     --StoreCount;
3640b57cec5SDimitry Andric   }
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric   // True if this class has no memory members.
definesNoMemory() const3670b57cec5SDimitry Andric   bool definesNoMemory() const { return StoreCount == 0 && memory_empty(); }
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric   // Return true if two congruence classes are equivalent to each other. This
3700b57cec5SDimitry Andric   // means that every field but the ID number and the dead field are equivalent.
isEquivalentTo(const CongruenceClass * Other) const3710b57cec5SDimitry Andric   bool isEquivalentTo(const CongruenceClass *Other) const {
3720b57cec5SDimitry Andric     if (!Other)
3730b57cec5SDimitry Andric       return false;
3740b57cec5SDimitry Andric     if (this == Other)
3750b57cec5SDimitry Andric       return true;
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric     if (std::tie(StoreCount, RepLeader, RepStoredValue, RepMemoryAccess) !=
3780b57cec5SDimitry Andric         std::tie(Other->StoreCount, Other->RepLeader, Other->RepStoredValue,
3790b57cec5SDimitry Andric                  Other->RepMemoryAccess))
3800b57cec5SDimitry Andric       return false;
3810b57cec5SDimitry Andric     if (DefiningExpr != Other->DefiningExpr)
3820b57cec5SDimitry Andric       if (!DefiningExpr || !Other->DefiningExpr ||
3830b57cec5SDimitry Andric           *DefiningExpr != *Other->DefiningExpr)
3840b57cec5SDimitry Andric         return false;
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric     if (Members.size() != Other->Members.size())
3870b57cec5SDimitry Andric       return false;
3880b57cec5SDimitry Andric 
389fe6060f1SDimitry Andric     return llvm::set_is_subset(Members, Other->Members);
3900b57cec5SDimitry Andric   }
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric private:
3930b57cec5SDimitry Andric   unsigned ID;
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric   // Representative leader.
3960b57cec5SDimitry Andric   Value *RepLeader = nullptr;
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric   // The most dominating leader after our current leader, because the member set
3990b57cec5SDimitry Andric   // is not sorted and is expensive to keep sorted all the time.
4000b57cec5SDimitry Andric   std::pair<Value *, unsigned int> NextLeader = {nullptr, ~0U};
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric   // If this is represented by a store, the value of the store.
4030b57cec5SDimitry Andric   Value *RepStoredValue = nullptr;
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric   // If this class contains MemoryDefs or MemoryPhis, this is the leading memory
4060b57cec5SDimitry Andric   // access.
4070b57cec5SDimitry Andric   const MemoryAccess *RepMemoryAccess = nullptr;
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   // Defining Expression.
4100b57cec5SDimitry Andric   const Expression *DefiningExpr = nullptr;
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric   // Actual members of this class.
4130b57cec5SDimitry Andric   MemberSet Members;
4140b57cec5SDimitry Andric 
4150b57cec5SDimitry Andric   // This is the set of MemoryPhis that exist in the class. MemoryDefs and
4160b57cec5SDimitry Andric   // MemoryUses have real instructions representing them, so we only need to
4170b57cec5SDimitry Andric   // track MemoryPhis here.
4180b57cec5SDimitry Andric   MemoryMemberSet MemoryMembers;
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric   // Number of stores in this congruence class.
4210b57cec5SDimitry Andric   // This is used so we can detect store equivalence changes properly.
4220b57cec5SDimitry Andric   int StoreCount = 0;
4230b57cec5SDimitry Andric };
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric } // end anonymous namespace
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric namespace llvm {
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric struct ExactEqualsExpression {
4300b57cec5SDimitry Andric   const Expression &E;
4310b57cec5SDimitry Andric 
ExactEqualsExpressionllvm::ExactEqualsExpression4320b57cec5SDimitry Andric   explicit ExactEqualsExpression(const Expression &E) : E(E) {}
4330b57cec5SDimitry Andric 
getComputedHashllvm::ExactEqualsExpression4340b57cec5SDimitry Andric   hash_code getComputedHash() const { return E.getComputedHash(); }
4350b57cec5SDimitry Andric 
operator ==llvm::ExactEqualsExpression4360b57cec5SDimitry Andric   bool operator==(const Expression &Other) const {
4370b57cec5SDimitry Andric     return E.exactlyEquals(Other);
4380b57cec5SDimitry Andric   }
4390b57cec5SDimitry Andric };
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric template <> struct DenseMapInfo<const Expression *> {
getEmptyKeyllvm::DenseMapInfo4420b57cec5SDimitry Andric   static const Expression *getEmptyKey() {
4430b57cec5SDimitry Andric     auto Val = static_cast<uintptr_t>(-1);
4440b57cec5SDimitry Andric     Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
4450b57cec5SDimitry Andric     return reinterpret_cast<const Expression *>(Val);
4460b57cec5SDimitry Andric   }
4470b57cec5SDimitry Andric 
getTombstoneKeyllvm::DenseMapInfo4480b57cec5SDimitry Andric   static const Expression *getTombstoneKey() {
4490b57cec5SDimitry Andric     auto Val = static_cast<uintptr_t>(~1U);
4500b57cec5SDimitry Andric     Val <<= PointerLikeTypeTraits<const Expression *>::NumLowBitsAvailable;
4510b57cec5SDimitry Andric     return reinterpret_cast<const Expression *>(Val);
4520b57cec5SDimitry Andric   }
4530b57cec5SDimitry Andric 
getHashValuellvm::DenseMapInfo4540b57cec5SDimitry Andric   static unsigned getHashValue(const Expression *E) {
4550b57cec5SDimitry Andric     return E->getComputedHash();
4560b57cec5SDimitry Andric   }
4570b57cec5SDimitry Andric 
getHashValuellvm::DenseMapInfo4580b57cec5SDimitry Andric   static unsigned getHashValue(const ExactEqualsExpression &E) {
4590b57cec5SDimitry Andric     return E.getComputedHash();
4600b57cec5SDimitry Andric   }
4610b57cec5SDimitry Andric 
isEqualllvm::DenseMapInfo4620b57cec5SDimitry Andric   static bool isEqual(const ExactEqualsExpression &LHS, const Expression *RHS) {
4630b57cec5SDimitry Andric     if (RHS == getTombstoneKey() || RHS == getEmptyKey())
4640b57cec5SDimitry Andric       return false;
4650b57cec5SDimitry Andric     return LHS == *RHS;
4660b57cec5SDimitry Andric   }
4670b57cec5SDimitry Andric 
isEqualllvm::DenseMapInfo4680b57cec5SDimitry Andric   static bool isEqual(const Expression *LHS, const Expression *RHS) {
4690b57cec5SDimitry Andric     if (LHS == RHS)
4700b57cec5SDimitry Andric       return true;
4710b57cec5SDimitry Andric     if (LHS == getTombstoneKey() || RHS == getTombstoneKey() ||
4720b57cec5SDimitry Andric         LHS == getEmptyKey() || RHS == getEmptyKey())
4730b57cec5SDimitry Andric       return false;
4740b57cec5SDimitry Andric     // Compare hashes before equality.  This is *not* what the hashtable does,
4750b57cec5SDimitry Andric     // since it is computing it modulo the number of buckets, whereas we are
4760b57cec5SDimitry Andric     // using the full hash keyspace.  Since the hashes are precomputed, this
4770b57cec5SDimitry Andric     // check is *much* faster than equality.
4780b57cec5SDimitry Andric     if (LHS->getComputedHash() != RHS->getComputedHash())
4790b57cec5SDimitry Andric       return false;
4800b57cec5SDimitry Andric     return *LHS == *RHS;
4810b57cec5SDimitry Andric   }
4820b57cec5SDimitry Andric };
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric } // end namespace llvm
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric namespace {
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric class NewGVN {
4890b57cec5SDimitry Andric   Function &F;
490480093f4SDimitry Andric   DominatorTree *DT = nullptr;
491480093f4SDimitry Andric   const TargetLibraryInfo *TLI = nullptr;
492480093f4SDimitry Andric   AliasAnalysis *AA = nullptr;
493480093f4SDimitry Andric   MemorySSA *MSSA = nullptr;
494480093f4SDimitry Andric   MemorySSAWalker *MSSAWalker = nullptr;
4955ffd83dbSDimitry Andric   AssumptionCache *AC = nullptr;
4960b57cec5SDimitry Andric   const DataLayout &DL;
4970b57cec5SDimitry Andric   std::unique_ptr<PredicateInfo> PredInfo;
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric   // These are the only two things the create* functions should have
5000b57cec5SDimitry Andric   // side-effects on due to allocating memory.
5010b57cec5SDimitry Andric   mutable BumpPtrAllocator ExpressionAllocator;
5020b57cec5SDimitry Andric   mutable ArrayRecycler<Value *> ArgRecycler;
5030b57cec5SDimitry Andric   mutable TarjanSCC SCCFinder;
5040b57cec5SDimitry Andric   const SimplifyQuery SQ;
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric   // Number of function arguments, used by ranking
507480093f4SDimitry Andric   unsigned int NumFuncArgs = 0;
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   // RPOOrdering of basic blocks
5100b57cec5SDimitry Andric   DenseMap<const DomTreeNode *, unsigned> RPOOrdering;
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric   // Congruence class info.
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric   // This class is called INITIAL in the paper. It is the class everything
5150b57cec5SDimitry Andric   // startsout in, and represents any value. Being an optimistic analysis,
5160b57cec5SDimitry Andric   // anything in the TOP class has the value TOP, which is indeterminate and
5170b57cec5SDimitry Andric   // equivalent to everything.
518480093f4SDimitry Andric   CongruenceClass *TOPClass = nullptr;
5190b57cec5SDimitry Andric   std::vector<CongruenceClass *> CongruenceClasses;
520480093f4SDimitry Andric   unsigned NextCongruenceNum = 0;
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   // Value Mappings.
5230b57cec5SDimitry Andric   DenseMap<Value *, CongruenceClass *> ValueToClass;
5240b57cec5SDimitry Andric   DenseMap<Value *, const Expression *> ValueToExpression;
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric   // Value PHI handling, used to make equivalence between phi(op, op) and
5270b57cec5SDimitry Andric   // op(phi, phi).
5280b57cec5SDimitry Andric   // These mappings just store various data that would normally be part of the
5290b57cec5SDimitry Andric   // IR.
5300b57cec5SDimitry Andric   SmallPtrSet<const Instruction *, 8> PHINodeUses;
5310b57cec5SDimitry Andric 
532*0fca6ea1SDimitry Andric   // The cached results, in general, are only valid for the specific block where
533*0fca6ea1SDimitry Andric   // they were computed. The unsigned part of the key is a unique block
534*0fca6ea1SDimitry Andric   // identifier
535*0fca6ea1SDimitry Andric   DenseMap<std::pair<const Value *, unsigned>, bool> OpSafeForPHIOfOps;
536*0fca6ea1SDimitry Andric   unsigned CacheIdx;
5370b57cec5SDimitry Andric 
5380b57cec5SDimitry Andric   // Map a temporary instruction we created to a parent block.
5390b57cec5SDimitry Andric   DenseMap<const Value *, BasicBlock *> TempToBlock;
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric   // Map between the already in-program instructions and the temporary phis we
5420b57cec5SDimitry Andric   // created that they are known equivalent to.
5430b57cec5SDimitry Andric   DenseMap<const Value *, PHINode *> RealToTemp;
5440b57cec5SDimitry Andric 
5450b57cec5SDimitry Andric   // In order to know when we should re-process instructions that have
5460b57cec5SDimitry Andric   // phi-of-ops, we track the set of expressions that they needed as
5470b57cec5SDimitry Andric   // leaders. When we discover new leaders for those expressions, we process the
5480b57cec5SDimitry Andric   // associated phi-of-op instructions again in case they have changed.  The
5490b57cec5SDimitry Andric   // other way they may change is if they had leaders, and those leaders
5500b57cec5SDimitry Andric   // disappear.  However, at the point they have leaders, there are uses of the
5510b57cec5SDimitry Andric   // relevant operands in the created phi node, and so they will get reprocessed
5520b57cec5SDimitry Andric   // through the normal user marking we perform.
5530b57cec5SDimitry Andric   mutable DenseMap<const Value *, SmallPtrSet<Value *, 2>> AdditionalUsers;
5540b57cec5SDimitry Andric   DenseMap<const Expression *, SmallPtrSet<Instruction *, 2>>
5550b57cec5SDimitry Andric       ExpressionToPhiOfOps;
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric   // Map from temporary operation to MemoryAccess.
5580b57cec5SDimitry Andric   DenseMap<const Instruction *, MemoryUseOrDef *> TempToMemory;
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric   // Set of all temporary instructions we created.
5610b57cec5SDimitry Andric   // Note: This will include instructions that were just created during value
5620b57cec5SDimitry Andric   // numbering.  The way to test if something is using them is to check
5630b57cec5SDimitry Andric   // RealToTemp.
5640b57cec5SDimitry Andric   DenseSet<Instruction *> AllTempInstructions;
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric   // This is the set of instructions to revisit on a reachability change.  At
5670b57cec5SDimitry Andric   // the end of the main iteration loop it will contain at least all the phi of
5680b57cec5SDimitry Andric   // ops instructions that will be changed to phis, as well as regular phis.
5690b57cec5SDimitry Andric   // During the iteration loop, it may contain other things, such as phi of ops
5700b57cec5SDimitry Andric   // instructions that used edge reachability to reach a result, and so need to
5710b57cec5SDimitry Andric   // be revisited when the edge changes, independent of whether the phi they
5720b57cec5SDimitry Andric   // depended on changes.
5730b57cec5SDimitry Andric   DenseMap<BasicBlock *, SparseBitVector<>> RevisitOnReachabilityChange;
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric   // Mapping from predicate info we used to the instructions we used it with.
5760b57cec5SDimitry Andric   // In order to correctly ensure propagation, we must keep track of what
5770b57cec5SDimitry Andric   // comparisons we used, so that when the values of the comparisons change, we
5780b57cec5SDimitry Andric   // propagate the information to the places we used the comparison.
5790b57cec5SDimitry Andric   mutable DenseMap<const Value *, SmallPtrSet<Instruction *, 2>>
5800b57cec5SDimitry Andric       PredicateToUsers;
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric   // the same reasoning as PredicateToUsers.  When we skip MemoryAccesses for
5830b57cec5SDimitry Andric   // stores, we no longer can rely solely on the def-use chains of MemorySSA.
5840b57cec5SDimitry Andric   mutable DenseMap<const MemoryAccess *, SmallPtrSet<MemoryAccess *, 2>>
5850b57cec5SDimitry Andric       MemoryToUsers;
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric   // A table storing which memorydefs/phis represent a memory state provably
5880b57cec5SDimitry Andric   // equivalent to another memory state.
5890b57cec5SDimitry Andric   // We could use the congruence class machinery, but the MemoryAccess's are
5900b57cec5SDimitry Andric   // abstract memory states, so they can only ever be equivalent to each other,
5910b57cec5SDimitry Andric   // and not to constants, etc.
5920b57cec5SDimitry Andric   DenseMap<const MemoryAccess *, CongruenceClass *> MemoryAccessToClass;
5930b57cec5SDimitry Andric 
5940b57cec5SDimitry Andric   // We could, if we wanted, build MemoryPhiExpressions and
5950b57cec5SDimitry Andric   // MemoryVariableExpressions, etc, and value number them the same way we value
5960b57cec5SDimitry Andric   // number phi expressions.  For the moment, this seems like overkill.  They
5970b57cec5SDimitry Andric   // can only exist in one of three states: they can be TOP (equal to
5980b57cec5SDimitry Andric   // everything), Equivalent to something else, or unique.  Because we do not
5990b57cec5SDimitry Andric   // create expressions for them, we need to simulate leader change not just
6000b57cec5SDimitry Andric   // when they change class, but when they change state.  Note: We can do the
6010b57cec5SDimitry Andric   // same thing for phis, and avoid having phi expressions if we wanted, We
6020b57cec5SDimitry Andric   // should eventually unify in one direction or the other, so this is a little
6030b57cec5SDimitry Andric   // bit of an experiment in which turns out easier to maintain.
6040b57cec5SDimitry Andric   enum MemoryPhiState { MPS_Invalid, MPS_TOP, MPS_Equivalent, MPS_Unique };
6050b57cec5SDimitry Andric   DenseMap<const MemoryPhi *, MemoryPhiState> MemoryPhiState;
6060b57cec5SDimitry Andric 
6070b57cec5SDimitry Andric   enum InstCycleState { ICS_Unknown, ICS_CycleFree, ICS_Cycle };
6080b57cec5SDimitry Andric   mutable DenseMap<const Instruction *, InstCycleState> InstCycleState;
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric   // Expression to class mapping.
6110b57cec5SDimitry Andric   using ExpressionClassMap = DenseMap<const Expression *, CongruenceClass *>;
6120b57cec5SDimitry Andric   ExpressionClassMap ExpressionToClass;
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric   // We have a single expression that represents currently DeadExpressions.
6150b57cec5SDimitry Andric   // For dead expressions we can prove will stay dead, we mark them with
6160b57cec5SDimitry Andric   // DFS number zero.  However, it's possible in the case of phi nodes
6170b57cec5SDimitry Andric   // for us to assume/prove all arguments are dead during fixpointing.
6180b57cec5SDimitry Andric   // We use DeadExpression for that case.
6190b57cec5SDimitry Andric   DeadExpression *SingletonDeadExpression = nullptr;
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric   // Which values have changed as a result of leader changes.
6220b57cec5SDimitry Andric   SmallPtrSet<Value *, 8> LeaderChanges;
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric   // Reachability info.
6250b57cec5SDimitry Andric   using BlockEdge = BasicBlockEdge;
6260b57cec5SDimitry Andric   DenseSet<BlockEdge> ReachableEdges;
6270b57cec5SDimitry Andric   SmallPtrSet<const BasicBlock *, 8> ReachableBlocks;
6280b57cec5SDimitry Andric 
6290b57cec5SDimitry Andric   // This is a bitvector because, on larger functions, we may have
6300b57cec5SDimitry Andric   // thousands of touched instructions at once (entire blocks,
6310b57cec5SDimitry Andric   // instructions with hundreds of uses, etc).  Even with optimization
6320b57cec5SDimitry Andric   // for when we mark whole blocks as touched, when this was a
6330b57cec5SDimitry Andric   // SmallPtrSet or DenseSet, for some functions, we spent >20% of all
6340b57cec5SDimitry Andric   // the time in GVN just managing this list.  The bitvector, on the
6350b57cec5SDimitry Andric   // other hand, efficiently supports test/set/clear of both
6360b57cec5SDimitry Andric   // individual and ranges, as well as "find next element" This
6370b57cec5SDimitry Andric   // enables us to use it as a worklist with essentially 0 cost.
6380b57cec5SDimitry Andric   BitVector TouchedInstructions;
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric   DenseMap<const BasicBlock *, std::pair<unsigned, unsigned>> BlockInstRange;
6410eae32dcSDimitry Andric   mutable DenseMap<const IntrinsicInst *, const Value *> IntrinsicInstPred;
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric #ifndef NDEBUG
6440b57cec5SDimitry Andric   // Debugging for how many times each block and instruction got processed.
6450b57cec5SDimitry Andric   DenseMap<const Value *, unsigned> ProcessedCount;
6460b57cec5SDimitry Andric #endif
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric   // DFS info.
6490b57cec5SDimitry Andric   // This contains a mapping from Instructions to DFS numbers.
6500b57cec5SDimitry Andric   // The numbering starts at 1. An instruction with DFS number zero
6510b57cec5SDimitry Andric   // means that the instruction is dead.
6520b57cec5SDimitry Andric   DenseMap<const Value *, unsigned> InstrDFS;
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric   // This contains the mapping DFS numbers to instructions.
6550b57cec5SDimitry Andric   SmallVector<Value *, 32> DFSToInstr;
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric   // Deletion info.
6580b57cec5SDimitry Andric   SmallPtrSet<Instruction *, 8> InstructionsToErase;
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric public:
NewGVN(Function & F,DominatorTree * DT,AssumptionCache * AC,TargetLibraryInfo * TLI,AliasAnalysis * AA,MemorySSA * MSSA,const DataLayout & DL)6610b57cec5SDimitry Andric   NewGVN(Function &F, DominatorTree *DT, AssumptionCache *AC,
6620b57cec5SDimitry Andric          TargetLibraryInfo *TLI, AliasAnalysis *AA, MemorySSA *MSSA,
6630b57cec5SDimitry Andric          const DataLayout &DL)
6645ffd83dbSDimitry Andric       : F(F), DT(DT), TLI(TLI), AA(AA), MSSA(MSSA), AC(AC), DL(DL),
6658bcb0991SDimitry Andric         PredInfo(std::make_unique<PredicateInfo>(F, *DT, *AC)),
666e8d8bef9SDimitry Andric         SQ(DL, TLI, DT, AC, /*CtxI=*/nullptr, /*UseInstrInfo=*/false,
667e8d8bef9SDimitry Andric            /*CanUseUndef=*/false) {}
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric   bool runGVN();
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric private:
672fe6060f1SDimitry Andric   /// Helper struct return a Expression with an optional extra dependency.
673fe6060f1SDimitry Andric   struct ExprResult {
674fe6060f1SDimitry Andric     const Expression *Expr;
675fe6060f1SDimitry Andric     Value *ExtraDep;
676fe6060f1SDimitry Andric     const PredicateBase *PredDep;
677fe6060f1SDimitry Andric 
ExprResult__anonb3555ec80211::NewGVN::ExprResult678fe6060f1SDimitry Andric     ExprResult(const Expression *Expr, Value *ExtraDep = nullptr,
679fe6060f1SDimitry Andric                const PredicateBase *PredDep = nullptr)
680fe6060f1SDimitry Andric         : Expr(Expr), ExtraDep(ExtraDep), PredDep(PredDep) {}
681fe6060f1SDimitry Andric     ExprResult(const ExprResult &) = delete;
ExprResult__anonb3555ec80211::NewGVN::ExprResult682fe6060f1SDimitry Andric     ExprResult(ExprResult &&Other)
683fe6060f1SDimitry Andric         : Expr(Other.Expr), ExtraDep(Other.ExtraDep), PredDep(Other.PredDep) {
684fe6060f1SDimitry Andric       Other.Expr = nullptr;
685fe6060f1SDimitry Andric       Other.ExtraDep = nullptr;
686fe6060f1SDimitry Andric       Other.PredDep = nullptr;
687fe6060f1SDimitry Andric     }
688fe6060f1SDimitry Andric     ExprResult &operator=(const ExprResult &Other) = delete;
689fe6060f1SDimitry Andric     ExprResult &operator=(ExprResult &&Other) = delete;
690fe6060f1SDimitry Andric 
~ExprResult__anonb3555ec80211::NewGVN::ExprResult691fe6060f1SDimitry Andric     ~ExprResult() { assert(!ExtraDep && "unhandled ExtraDep"); }
692fe6060f1SDimitry Andric 
operator bool__anonb3555ec80211::NewGVN::ExprResult693fe6060f1SDimitry Andric     operator bool() const { return Expr; }
694fe6060f1SDimitry Andric 
none__anonb3555ec80211::NewGVN::ExprResult695fe6060f1SDimitry Andric     static ExprResult none() { return {nullptr, nullptr, nullptr}; }
some__anonb3555ec80211::NewGVN::ExprResult696fe6060f1SDimitry Andric     static ExprResult some(const Expression *Expr, Value *ExtraDep = nullptr) {
697fe6060f1SDimitry Andric       return {Expr, ExtraDep, nullptr};
698fe6060f1SDimitry Andric     }
some__anonb3555ec80211::NewGVN::ExprResult699fe6060f1SDimitry Andric     static ExprResult some(const Expression *Expr,
700fe6060f1SDimitry Andric                            const PredicateBase *PredDep) {
701fe6060f1SDimitry Andric       return {Expr, nullptr, PredDep};
702fe6060f1SDimitry Andric     }
some__anonb3555ec80211::NewGVN::ExprResult703fe6060f1SDimitry Andric     static ExprResult some(const Expression *Expr, Value *ExtraDep,
704fe6060f1SDimitry Andric                            const PredicateBase *PredDep) {
705fe6060f1SDimitry Andric       return {Expr, ExtraDep, PredDep};
706fe6060f1SDimitry Andric     }
707fe6060f1SDimitry Andric   };
708fe6060f1SDimitry Andric 
7090b57cec5SDimitry Andric   // Expression handling.
710fe6060f1SDimitry Andric   ExprResult createExpression(Instruction *) const;
7110b57cec5SDimitry Andric   const Expression *createBinaryExpression(unsigned, Type *, Value *, Value *,
7120b57cec5SDimitry Andric                                            Instruction *) const;
7130b57cec5SDimitry Andric 
7140b57cec5SDimitry Andric   // Our canonical form for phi arguments is a pair of incoming value, incoming
7150b57cec5SDimitry Andric   // basic block.
7160b57cec5SDimitry Andric   using ValPair = std::pair<Value *, BasicBlock *>;
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric   PHIExpression *createPHIExpression(ArrayRef<ValPair>, const Instruction *,
7190b57cec5SDimitry Andric                                      BasicBlock *, bool &HasBackEdge,
7200b57cec5SDimitry Andric                                      bool &OriginalOpsConstant) const;
7210b57cec5SDimitry Andric   const DeadExpression *createDeadExpression() const;
7220b57cec5SDimitry Andric   const VariableExpression *createVariableExpression(Value *) const;
7230b57cec5SDimitry Andric   const ConstantExpression *createConstantExpression(Constant *) const;
7240b57cec5SDimitry Andric   const Expression *createVariableOrConstant(Value *V) const;
7250b57cec5SDimitry Andric   const UnknownExpression *createUnknownExpression(Instruction *) const;
7260b57cec5SDimitry Andric   const StoreExpression *createStoreExpression(StoreInst *,
7270b57cec5SDimitry Andric                                                const MemoryAccess *) const;
7280b57cec5SDimitry Andric   LoadExpression *createLoadExpression(Type *, Value *, LoadInst *,
7290b57cec5SDimitry Andric                                        const MemoryAccess *) const;
7300b57cec5SDimitry Andric   const CallExpression *createCallExpression(CallInst *,
7310b57cec5SDimitry Andric                                              const MemoryAccess *) const;
7320b57cec5SDimitry Andric   const AggregateValueExpression *
7330b57cec5SDimitry Andric   createAggregateValueExpression(Instruction *) const;
7340b57cec5SDimitry Andric   bool setBasicExpressionInfo(Instruction *, BasicExpression *) const;
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric   // Congruence class handling.
createCongruenceClass(Value * Leader,const Expression * E)7370b57cec5SDimitry Andric   CongruenceClass *createCongruenceClass(Value *Leader, const Expression *E) {
7380b57cec5SDimitry Andric     auto *result = new CongruenceClass(NextCongruenceNum++, Leader, E);
7390b57cec5SDimitry Andric     CongruenceClasses.emplace_back(result);
7400b57cec5SDimitry Andric     return result;
7410b57cec5SDimitry Andric   }
7420b57cec5SDimitry Andric 
createMemoryClass(MemoryAccess * MA)7430b57cec5SDimitry Andric   CongruenceClass *createMemoryClass(MemoryAccess *MA) {
7440b57cec5SDimitry Andric     auto *CC = createCongruenceClass(nullptr, nullptr);
7450b57cec5SDimitry Andric     CC->setMemoryLeader(MA);
7460b57cec5SDimitry Andric     return CC;
7470b57cec5SDimitry Andric   }
7480b57cec5SDimitry Andric 
ensureLeaderOfMemoryClass(MemoryAccess * MA)7490b57cec5SDimitry Andric   CongruenceClass *ensureLeaderOfMemoryClass(MemoryAccess *MA) {
7500b57cec5SDimitry Andric     auto *CC = getMemoryClass(MA);
7510b57cec5SDimitry Andric     if (CC->getMemoryLeader() != MA)
7520b57cec5SDimitry Andric       CC = createMemoryClass(MA);
7530b57cec5SDimitry Andric     return CC;
7540b57cec5SDimitry Andric   }
7550b57cec5SDimitry Andric 
createSingletonCongruenceClass(Value * Member)7560b57cec5SDimitry Andric   CongruenceClass *createSingletonCongruenceClass(Value *Member) {
7570b57cec5SDimitry Andric     CongruenceClass *CClass = createCongruenceClass(Member, nullptr);
7580b57cec5SDimitry Andric     CClass->insert(Member);
7590b57cec5SDimitry Andric     ValueToClass[Member] = CClass;
7600b57cec5SDimitry Andric     return CClass;
7610b57cec5SDimitry Andric   }
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric   void initializeCongruenceClasses(Function &F);
7640b57cec5SDimitry Andric   const Expression *makePossiblePHIOfOps(Instruction *,
7650b57cec5SDimitry Andric                                          SmallPtrSetImpl<Value *> &);
7660b57cec5SDimitry Andric   Value *findLeaderForInst(Instruction *ValueOp,
7670b57cec5SDimitry Andric                            SmallPtrSetImpl<Value *> &Visited,
7680b57cec5SDimitry Andric                            MemoryAccess *MemAccess, Instruction *OrigInst,
7690b57cec5SDimitry Andric                            BasicBlock *PredBB);
7700b57cec5SDimitry Andric   bool OpIsSafeForPHIOfOps(Value *Op, const BasicBlock *PHIBlock,
7710b57cec5SDimitry Andric                            SmallPtrSetImpl<const Value *> &);
7720b57cec5SDimitry Andric   void addPhiOfOps(PHINode *Op, BasicBlock *BB, Instruction *ExistingValue);
7730b57cec5SDimitry Andric   void removePhiOfOps(Instruction *I, PHINode *PHITemp);
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric   // Value number an Instruction or MemoryPhi.
7760b57cec5SDimitry Andric   void valueNumberMemoryPhi(MemoryPhi *);
7770b57cec5SDimitry Andric   void valueNumberInstruction(Instruction *);
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric   // Symbolic evaluation.
780fe6060f1SDimitry Andric   ExprResult checkExprResults(Expression *, Instruction *, Value *) const;
7815f757f3fSDimitry Andric   ExprResult performSymbolicEvaluation(Instruction *,
7820b57cec5SDimitry Andric                                        SmallPtrSetImpl<Value *> &) const;
7830b57cec5SDimitry Andric   const Expression *performSymbolicLoadCoercion(Type *, Value *, LoadInst *,
7840b57cec5SDimitry Andric                                                 Instruction *,
7850b57cec5SDimitry Andric                                                 MemoryAccess *) const;
7860b57cec5SDimitry Andric   const Expression *performSymbolicLoadEvaluation(Instruction *) const;
7870b57cec5SDimitry Andric   const Expression *performSymbolicStoreEvaluation(Instruction *) const;
788fe6060f1SDimitry Andric   ExprResult performSymbolicCallEvaluation(Instruction *) const;
7890b57cec5SDimitry Andric   void sortPHIOps(MutableArrayRef<ValPair> Ops) const;
7900b57cec5SDimitry Andric   const Expression *performSymbolicPHIEvaluation(ArrayRef<ValPair>,
7910b57cec5SDimitry Andric                                                  Instruction *I,
7920b57cec5SDimitry Andric                                                  BasicBlock *PHIBlock) const;
7930b57cec5SDimitry Andric   const Expression *performSymbolicAggrValueEvaluation(Instruction *) const;
794fe6060f1SDimitry Andric   ExprResult performSymbolicCmpEvaluation(Instruction *) const;
7950eae32dcSDimitry Andric   ExprResult performSymbolicPredicateInfoEvaluation(IntrinsicInst *) const;
7960b57cec5SDimitry Andric 
7970b57cec5SDimitry Andric   // Congruence finding.
7980b57cec5SDimitry Andric   bool someEquivalentDominates(const Instruction *, const Instruction *) const;
7990b57cec5SDimitry Andric   Value *lookupOperandLeader(Value *) const;
8000b57cec5SDimitry Andric   CongruenceClass *getClassForExpression(const Expression *E) const;
8010b57cec5SDimitry Andric   void performCongruenceFinding(Instruction *, const Expression *);
8020b57cec5SDimitry Andric   void moveValueToNewCongruenceClass(Instruction *, const Expression *,
8030b57cec5SDimitry Andric                                      CongruenceClass *, CongruenceClass *);
8040b57cec5SDimitry Andric   void moveMemoryToNewCongruenceClass(Instruction *, MemoryAccess *,
8050b57cec5SDimitry Andric                                       CongruenceClass *, CongruenceClass *);
8060b57cec5SDimitry Andric   Value *getNextValueLeader(CongruenceClass *) const;
8070b57cec5SDimitry Andric   const MemoryAccess *getNextMemoryLeader(CongruenceClass *) const;
8080b57cec5SDimitry Andric   bool setMemoryClass(const MemoryAccess *From, CongruenceClass *To);
8090b57cec5SDimitry Andric   CongruenceClass *getMemoryClass(const MemoryAccess *MA) const;
8100b57cec5SDimitry Andric   const MemoryAccess *lookupMemoryLeader(const MemoryAccess *) const;
8110b57cec5SDimitry Andric   bool isMemoryAccessTOP(const MemoryAccess *) const;
8120b57cec5SDimitry Andric 
8130b57cec5SDimitry Andric   // Ranking
8140b57cec5SDimitry Andric   unsigned int getRank(const Value *) const;
8150b57cec5SDimitry Andric   bool shouldSwapOperands(const Value *, const Value *) const;
8160eae32dcSDimitry Andric   bool shouldSwapOperandsForIntrinsic(const Value *, const Value *,
8170eae32dcSDimitry Andric                                       const IntrinsicInst *I) const;
8180b57cec5SDimitry Andric 
8190b57cec5SDimitry Andric   // Reachability handling.
8200b57cec5SDimitry Andric   void updateReachableEdge(BasicBlock *, BasicBlock *);
8210b57cec5SDimitry Andric   void processOutgoingEdges(Instruction *, BasicBlock *);
8220b57cec5SDimitry Andric   Value *findConditionEquivalence(Value *) const;
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric   // Elimination.
8250b57cec5SDimitry Andric   struct ValueDFS;
8260b57cec5SDimitry Andric   void convertClassToDFSOrdered(const CongruenceClass &,
8270b57cec5SDimitry Andric                                 SmallVectorImpl<ValueDFS> &,
8280b57cec5SDimitry Andric                                 DenseMap<const Value *, unsigned int> &,
8290b57cec5SDimitry Andric                                 SmallPtrSetImpl<Instruction *> &) const;
8300b57cec5SDimitry Andric   void convertClassToLoadsAndStores(const CongruenceClass &,
8310b57cec5SDimitry Andric                                     SmallVectorImpl<ValueDFS> &) const;
8320b57cec5SDimitry Andric 
8330b57cec5SDimitry Andric   bool eliminateInstructions(Function &);
8340b57cec5SDimitry Andric   void replaceInstruction(Instruction *, Value *);
8350b57cec5SDimitry Andric   void markInstructionForDeletion(Instruction *);
8360b57cec5SDimitry Andric   void deleteInstructionsInBlock(BasicBlock *);
8370b57cec5SDimitry Andric   Value *findPHIOfOpsLeader(const Expression *, const Instruction *,
8380b57cec5SDimitry Andric                             const BasicBlock *) const;
8390b57cec5SDimitry Andric 
8400b57cec5SDimitry Andric   // Various instruction touch utilities
8410b57cec5SDimitry Andric   template <typename Map, typename KeyType>
8420b57cec5SDimitry Andric   void touchAndErase(Map &, const KeyType &);
8430b57cec5SDimitry Andric   void markUsersTouched(Value *);
8440b57cec5SDimitry Andric   void markMemoryUsersTouched(const MemoryAccess *);
8450b57cec5SDimitry Andric   void markMemoryDefTouched(const MemoryAccess *);
8460b57cec5SDimitry Andric   void markPredicateUsersTouched(Instruction *);
8470b57cec5SDimitry Andric   void markValueLeaderChangeTouched(CongruenceClass *CC);
8480b57cec5SDimitry Andric   void markMemoryLeaderChangeTouched(CongruenceClass *CC);
8490b57cec5SDimitry Andric   void markPhiOfOpsChanged(const Expression *E);
8500b57cec5SDimitry Andric   void addMemoryUsers(const MemoryAccess *To, MemoryAccess *U) const;
8510b57cec5SDimitry Andric   void addAdditionalUsers(Value *To, Value *User) const;
852fe6060f1SDimitry Andric   void addAdditionalUsers(ExprResult &Res, Instruction *User) const;
8530b57cec5SDimitry Andric 
8540b57cec5SDimitry Andric   // Main loop of value numbering
8550b57cec5SDimitry Andric   void iterateTouchedInstructions();
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric   // Utilities.
8580b57cec5SDimitry Andric   void cleanupTables();
8590b57cec5SDimitry Andric   std::pair<unsigned, unsigned> assignDFSNumbers(BasicBlock *, unsigned);
8600b57cec5SDimitry Andric   void updateProcessedCount(const Value *V);
8610b57cec5SDimitry Andric   void verifyMemoryCongruency() const;
8620b57cec5SDimitry Andric   void verifyIterationSettled(Function &F);
8630b57cec5SDimitry Andric   void verifyStoreExpressions() const;
8640b57cec5SDimitry Andric   bool singleReachablePHIPath(SmallPtrSet<const MemoryAccess *, 8> &,
8650b57cec5SDimitry Andric                               const MemoryAccess *, const MemoryAccess *) const;
8660b57cec5SDimitry Andric   BasicBlock *getBlockForValue(Value *V) const;
8670b57cec5SDimitry Andric   void deleteExpression(const Expression *E) const;
8680b57cec5SDimitry Andric   MemoryUseOrDef *getMemoryAccess(const Instruction *) const;
8690b57cec5SDimitry Andric   MemoryPhi *getMemoryAccess(const BasicBlock *) const;
8700b57cec5SDimitry Andric   template <class T, class Range> T *getMinDFSOfRange(const Range &) const;
8710b57cec5SDimitry Andric 
InstrToDFSNum(const Value * V) const8720b57cec5SDimitry Andric   unsigned InstrToDFSNum(const Value *V) const {
8730b57cec5SDimitry Andric     assert(isa<Instruction>(V) && "This should not be used for MemoryAccesses");
8740b57cec5SDimitry Andric     return InstrDFS.lookup(V);
8750b57cec5SDimitry Andric   }
8760b57cec5SDimitry Andric 
InstrToDFSNum(const MemoryAccess * MA) const8770b57cec5SDimitry Andric   unsigned InstrToDFSNum(const MemoryAccess *MA) const {
8780b57cec5SDimitry Andric     return MemoryToDFSNum(MA);
8790b57cec5SDimitry Andric   }
8800b57cec5SDimitry Andric 
InstrFromDFSNum(unsigned DFSNum)8810b57cec5SDimitry Andric   Value *InstrFromDFSNum(unsigned DFSNum) { return DFSToInstr[DFSNum]; }
8820b57cec5SDimitry Andric 
8830b57cec5SDimitry Andric   // Given a MemoryAccess, return the relevant instruction DFS number.  Note:
8840b57cec5SDimitry Andric   // This deliberately takes a value so it can be used with Use's, which will
8850b57cec5SDimitry Andric   // auto-convert to Value's but not to MemoryAccess's.
MemoryToDFSNum(const Value * MA) const8860b57cec5SDimitry Andric   unsigned MemoryToDFSNum(const Value *MA) const {
8870b57cec5SDimitry Andric     assert(isa<MemoryAccess>(MA) &&
8880b57cec5SDimitry Andric            "This should not be used with instructions");
8890b57cec5SDimitry Andric     return isa<MemoryUseOrDef>(MA)
8900b57cec5SDimitry Andric                ? InstrToDFSNum(cast<MemoryUseOrDef>(MA)->getMemoryInst())
8910b57cec5SDimitry Andric                : InstrDFS.lookup(MA);
8920b57cec5SDimitry Andric   }
8930b57cec5SDimitry Andric 
8940b57cec5SDimitry Andric   bool isCycleFree(const Instruction *) const;
8950b57cec5SDimitry Andric   bool isBackedge(BasicBlock *From, BasicBlock *To) const;
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric   // Debug counter info.  When verifying, we have to reset the value numbering
8980b57cec5SDimitry Andric   // debug counter to the same state it started in to get the same results.
899*0fca6ea1SDimitry Andric   DebugCounter::CounterState StartingVNCounter;
9000b57cec5SDimitry Andric };
9010b57cec5SDimitry Andric 
9020b57cec5SDimitry Andric } // end anonymous namespace
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric template <typename T>
equalsLoadStoreHelper(const T & LHS,const Expression & RHS)9050b57cec5SDimitry Andric static bool equalsLoadStoreHelper(const T &LHS, const Expression &RHS) {
9060b57cec5SDimitry Andric   if (!isa<LoadExpression>(RHS) && !isa<StoreExpression>(RHS))
9070b57cec5SDimitry Andric     return false;
9080b57cec5SDimitry Andric   return LHS.MemoryExpression::equals(RHS);
9090b57cec5SDimitry Andric }
9100b57cec5SDimitry Andric 
equals(const Expression & Other) const9110b57cec5SDimitry Andric bool LoadExpression::equals(const Expression &Other) const {
9120b57cec5SDimitry Andric   return equalsLoadStoreHelper(*this, Other);
9130b57cec5SDimitry Andric }
9140b57cec5SDimitry Andric 
equals(const Expression & Other) const9150b57cec5SDimitry Andric bool StoreExpression::equals(const Expression &Other) const {
9160b57cec5SDimitry Andric   if (!equalsLoadStoreHelper(*this, Other))
9170b57cec5SDimitry Andric     return false;
9180b57cec5SDimitry Andric   // Make sure that store vs store includes the value operand.
9190b57cec5SDimitry Andric   if (const auto *S = dyn_cast<StoreExpression>(&Other))
9200b57cec5SDimitry Andric     if (getStoredValue() != S->getStoredValue())
9210b57cec5SDimitry Andric       return false;
9220b57cec5SDimitry Andric   return true;
9230b57cec5SDimitry Andric }
9240b57cec5SDimitry Andric 
9250b57cec5SDimitry Andric // Determine if the edge From->To is a backedge
isBackedge(BasicBlock * From,BasicBlock * To) const9260b57cec5SDimitry Andric bool NewGVN::isBackedge(BasicBlock *From, BasicBlock *To) const {
9270b57cec5SDimitry Andric   return From == To ||
9280b57cec5SDimitry Andric          RPOOrdering.lookup(DT->getNode(From)) >=
9290b57cec5SDimitry Andric              RPOOrdering.lookup(DT->getNode(To));
9300b57cec5SDimitry Andric }
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric #ifndef NDEBUG
getBlockName(const BasicBlock * B)9330b57cec5SDimitry Andric static std::string getBlockName(const BasicBlock *B) {
9345ffd83dbSDimitry Andric   return DOTGraphTraits<DOTFuncInfo *>::getSimpleNodeLabel(B, nullptr);
9350b57cec5SDimitry Andric }
9360b57cec5SDimitry Andric #endif
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric // Get a MemoryAccess for an instruction, fake or real.
getMemoryAccess(const Instruction * I) const9390b57cec5SDimitry Andric MemoryUseOrDef *NewGVN::getMemoryAccess(const Instruction *I) const {
9400b57cec5SDimitry Andric   auto *Result = MSSA->getMemoryAccess(I);
9410b57cec5SDimitry Andric   return Result ? Result : TempToMemory.lookup(I);
9420b57cec5SDimitry Andric }
9430b57cec5SDimitry Andric 
9440b57cec5SDimitry Andric // Get a MemoryPhi for a basic block. These are all real.
getMemoryAccess(const BasicBlock * BB) const9450b57cec5SDimitry Andric MemoryPhi *NewGVN::getMemoryAccess(const BasicBlock *BB) const {
9460b57cec5SDimitry Andric   return MSSA->getMemoryAccess(BB);
9470b57cec5SDimitry Andric }
9480b57cec5SDimitry Andric 
9490b57cec5SDimitry Andric // Get the basic block from an instruction/memory value.
getBlockForValue(Value * V) const9500b57cec5SDimitry Andric BasicBlock *NewGVN::getBlockForValue(Value *V) const {
9510b57cec5SDimitry Andric   if (auto *I = dyn_cast<Instruction>(V)) {
9520b57cec5SDimitry Andric     auto *Parent = I->getParent();
9530b57cec5SDimitry Andric     if (Parent)
9540b57cec5SDimitry Andric       return Parent;
9550b57cec5SDimitry Andric     Parent = TempToBlock.lookup(V);
9560b57cec5SDimitry Andric     assert(Parent && "Every fake instruction should have a block");
9570b57cec5SDimitry Andric     return Parent;
9580b57cec5SDimitry Andric   }
9590b57cec5SDimitry Andric 
9600b57cec5SDimitry Andric   auto *MP = dyn_cast<MemoryPhi>(V);
9610b57cec5SDimitry Andric   assert(MP && "Should have been an instruction or a MemoryPhi");
9620b57cec5SDimitry Andric   return MP->getBlock();
9630b57cec5SDimitry Andric }
9640b57cec5SDimitry Andric 
9650b57cec5SDimitry Andric // Delete a definitely dead expression, so it can be reused by the expression
9660b57cec5SDimitry Andric // allocator.  Some of these are not in creation functions, so we have to accept
9670b57cec5SDimitry Andric // const versions.
deleteExpression(const Expression * E) const9680b57cec5SDimitry Andric void NewGVN::deleteExpression(const Expression *E) const {
9690b57cec5SDimitry Andric   assert(isa<BasicExpression>(E));
9700b57cec5SDimitry Andric   auto *BE = cast<BasicExpression>(E);
9710b57cec5SDimitry Andric   const_cast<BasicExpression *>(BE)->deallocateOperands(ArgRecycler);
9720b57cec5SDimitry Andric   ExpressionAllocator.Deallocate(E);
9730b57cec5SDimitry Andric }
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric // If V is a predicateinfo copy, get the thing it is a copy of.
getCopyOf(const Value * V)9760b57cec5SDimitry Andric static Value *getCopyOf(const Value *V) {
9770b57cec5SDimitry Andric   if (auto *II = dyn_cast<IntrinsicInst>(V))
9780b57cec5SDimitry Andric     if (II->getIntrinsicID() == Intrinsic::ssa_copy)
9790b57cec5SDimitry Andric       return II->getOperand(0);
9800b57cec5SDimitry Andric   return nullptr;
9810b57cec5SDimitry Andric }
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric // Return true if V is really PN, even accounting for predicateinfo copies.
isCopyOfPHI(const Value * V,const PHINode * PN)9840b57cec5SDimitry Andric static bool isCopyOfPHI(const Value *V, const PHINode *PN) {
9850b57cec5SDimitry Andric   return V == PN || getCopyOf(V) == PN;
9860b57cec5SDimitry Andric }
9870b57cec5SDimitry Andric 
isCopyOfAPHI(const Value * V)9880b57cec5SDimitry Andric static bool isCopyOfAPHI(const Value *V) {
9890b57cec5SDimitry Andric   auto *CO = getCopyOf(V);
9900b57cec5SDimitry Andric   return CO && isa<PHINode>(CO);
9910b57cec5SDimitry Andric }
9920b57cec5SDimitry Andric 
9930b57cec5SDimitry Andric // Sort PHI Operands into a canonical order.  What we use here is an RPO
9940b57cec5SDimitry Andric // order. The BlockInstRange numbers are generated in an RPO walk of the basic
9950b57cec5SDimitry Andric // blocks.
sortPHIOps(MutableArrayRef<ValPair> Ops) const9960b57cec5SDimitry Andric void NewGVN::sortPHIOps(MutableArrayRef<ValPair> Ops) const {
9970b57cec5SDimitry Andric   llvm::sort(Ops, [&](const ValPair &P1, const ValPair &P2) {
9980b57cec5SDimitry Andric     return BlockInstRange.lookup(P1.second).first <
9990b57cec5SDimitry Andric            BlockInstRange.lookup(P2.second).first;
10000b57cec5SDimitry Andric   });
10010b57cec5SDimitry Andric }
10020b57cec5SDimitry Andric 
10030b57cec5SDimitry Andric // Return true if V is a value that will always be available (IE can
10040b57cec5SDimitry Andric // be placed anywhere) in the function.  We don't do globals here
10050b57cec5SDimitry Andric // because they are often worse to put in place.
alwaysAvailable(Value * V)10060b57cec5SDimitry Andric static bool alwaysAvailable(Value *V) {
10070b57cec5SDimitry Andric   return isa<Constant>(V) || isa<Argument>(V);
10080b57cec5SDimitry Andric }
10090b57cec5SDimitry Andric 
10100b57cec5SDimitry Andric // Create a PHIExpression from an array of {incoming edge, value} pairs.  I is
10110b57cec5SDimitry Andric // the original instruction we are creating a PHIExpression for (but may not be
10120b57cec5SDimitry Andric // a phi node). We require, as an invariant, that all the PHIOperands in the
10130b57cec5SDimitry Andric // same block are sorted the same way. sortPHIOps will sort them into a
10140b57cec5SDimitry Andric // canonical order.
createPHIExpression(ArrayRef<ValPair> PHIOperands,const Instruction * I,BasicBlock * PHIBlock,bool & HasBackedge,bool & OriginalOpsConstant) const10150b57cec5SDimitry Andric PHIExpression *NewGVN::createPHIExpression(ArrayRef<ValPair> PHIOperands,
10160b57cec5SDimitry Andric                                            const Instruction *I,
10170b57cec5SDimitry Andric                                            BasicBlock *PHIBlock,
10180b57cec5SDimitry Andric                                            bool &HasBackedge,
10190b57cec5SDimitry Andric                                            bool &OriginalOpsConstant) const {
10200b57cec5SDimitry Andric   unsigned NumOps = PHIOperands.size();
10210b57cec5SDimitry Andric   auto *E = new (ExpressionAllocator) PHIExpression(NumOps, PHIBlock);
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric   E->allocateOperands(ArgRecycler, ExpressionAllocator);
10240b57cec5SDimitry Andric   E->setType(PHIOperands.begin()->first->getType());
10250b57cec5SDimitry Andric   E->setOpcode(Instruction::PHI);
10260b57cec5SDimitry Andric 
10270b57cec5SDimitry Andric   // Filter out unreachable phi operands.
10280b57cec5SDimitry Andric   auto Filtered = make_filter_range(PHIOperands, [&](const ValPair &P) {
10290b57cec5SDimitry Andric     auto *BB = P.second;
10300b57cec5SDimitry Andric     if (auto *PHIOp = dyn_cast<PHINode>(I))
10310b57cec5SDimitry Andric       if (isCopyOfPHI(P.first, PHIOp))
10320b57cec5SDimitry Andric         return false;
10330b57cec5SDimitry Andric     if (!ReachableEdges.count({BB, PHIBlock}))
10340b57cec5SDimitry Andric       return false;
10350b57cec5SDimitry Andric     // Things in TOPClass are equivalent to everything.
10360b57cec5SDimitry Andric     if (ValueToClass.lookup(P.first) == TOPClass)
10370b57cec5SDimitry Andric       return false;
10380b57cec5SDimitry Andric     OriginalOpsConstant = OriginalOpsConstant && isa<Constant>(P.first);
10390b57cec5SDimitry Andric     HasBackedge = HasBackedge || isBackedge(BB, PHIBlock);
10400b57cec5SDimitry Andric     return lookupOperandLeader(P.first) != I;
10410b57cec5SDimitry Andric   });
10420b57cec5SDimitry Andric   std::transform(Filtered.begin(), Filtered.end(), op_inserter(E),
10430b57cec5SDimitry Andric                  [&](const ValPair &P) -> Value * {
10440b57cec5SDimitry Andric                    return lookupOperandLeader(P.first);
10450b57cec5SDimitry Andric                  });
10460b57cec5SDimitry Andric   return E;
10470b57cec5SDimitry Andric }
10480b57cec5SDimitry Andric 
10490b57cec5SDimitry Andric // Set basic expression info (Arguments, type, opcode) for Expression
10500b57cec5SDimitry Andric // E from Instruction I in block B.
setBasicExpressionInfo(Instruction * I,BasicExpression * E) const10510b57cec5SDimitry Andric bool NewGVN::setBasicExpressionInfo(Instruction *I, BasicExpression *E) const {
10520b57cec5SDimitry Andric   bool AllConstant = true;
10530b57cec5SDimitry Andric   if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
10540b57cec5SDimitry Andric     E->setType(GEP->getSourceElementType());
10550b57cec5SDimitry Andric   else
10560b57cec5SDimitry Andric     E->setType(I->getType());
10570b57cec5SDimitry Andric   E->setOpcode(I->getOpcode());
10580b57cec5SDimitry Andric   E->allocateOperands(ArgRecycler, ExpressionAllocator);
10590b57cec5SDimitry Andric 
10600b57cec5SDimitry Andric   // Transform the operand array into an operand leader array, and keep track of
10610b57cec5SDimitry Andric   // whether all members are constant.
10620b57cec5SDimitry Andric   std::transform(I->op_begin(), I->op_end(), op_inserter(E), [&](Value *O) {
10630b57cec5SDimitry Andric     auto Operand = lookupOperandLeader(O);
10640b57cec5SDimitry Andric     AllConstant = AllConstant && isa<Constant>(Operand);
10650b57cec5SDimitry Andric     return Operand;
10660b57cec5SDimitry Andric   });
10670b57cec5SDimitry Andric 
10680b57cec5SDimitry Andric   return AllConstant;
10690b57cec5SDimitry Andric }
10700b57cec5SDimitry Andric 
createBinaryExpression(unsigned Opcode,Type * T,Value * Arg1,Value * Arg2,Instruction * I) const10710b57cec5SDimitry Andric const Expression *NewGVN::createBinaryExpression(unsigned Opcode, Type *T,
10720b57cec5SDimitry Andric                                                  Value *Arg1, Value *Arg2,
10730b57cec5SDimitry Andric                                                  Instruction *I) const {
10740b57cec5SDimitry Andric   auto *E = new (ExpressionAllocator) BasicExpression(2);
107581ad6265SDimitry Andric   // TODO: we need to remove context instruction after Value Tracking
107681ad6265SDimitry Andric   // can run without context instruction
107781ad6265SDimitry Andric   const SimplifyQuery Q = SQ.getWithInstruction(I);
10780b57cec5SDimitry Andric 
10790b57cec5SDimitry Andric   E->setType(T);
10800b57cec5SDimitry Andric   E->setOpcode(Opcode);
10810b57cec5SDimitry Andric   E->allocateOperands(ArgRecycler, ExpressionAllocator);
10820b57cec5SDimitry Andric   if (Instruction::isCommutative(Opcode)) {
10830b57cec5SDimitry Andric     // Ensure that commutative instructions that only differ by a permutation
10840b57cec5SDimitry Andric     // of their operands get the same value number by sorting the operand value
10850b57cec5SDimitry Andric     // numbers.  Since all commutative instructions have two operands it is more
10860b57cec5SDimitry Andric     // efficient to sort by hand rather than using, say, std::sort.
10870b57cec5SDimitry Andric     if (shouldSwapOperands(Arg1, Arg2))
10880b57cec5SDimitry Andric       std::swap(Arg1, Arg2);
10890b57cec5SDimitry Andric   }
10900b57cec5SDimitry Andric   E->op_push_back(lookupOperandLeader(Arg1));
10910b57cec5SDimitry Andric   E->op_push_back(lookupOperandLeader(Arg2));
10920b57cec5SDimitry Andric 
109381ad6265SDimitry Andric   Value *V = simplifyBinOp(Opcode, E->getOperand(0), E->getOperand(1), Q);
1094fe6060f1SDimitry Andric   if (auto Simplified = checkExprResults(E, I, V)) {
1095fe6060f1SDimitry Andric     addAdditionalUsers(Simplified, I);
1096fe6060f1SDimitry Andric     return Simplified.Expr;
1097fe6060f1SDimitry Andric   }
10980b57cec5SDimitry Andric   return E;
10990b57cec5SDimitry Andric }
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric // Take a Value returned by simplification of Expression E/Instruction
11020b57cec5SDimitry Andric // I, and see if it resulted in a simpler expression. If so, return
11030b57cec5SDimitry Andric // that expression.
checkExprResults(Expression * E,Instruction * I,Value * V) const1104fe6060f1SDimitry Andric NewGVN::ExprResult NewGVN::checkExprResults(Expression *E, Instruction *I,
11050b57cec5SDimitry Andric                                             Value *V) const {
11060b57cec5SDimitry Andric   if (!V)
1107fe6060f1SDimitry Andric     return ExprResult::none();
1108fe6060f1SDimitry Andric 
11090b57cec5SDimitry Andric   if (auto *C = dyn_cast<Constant>(V)) {
11100b57cec5SDimitry Andric     if (I)
11110b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Simplified " << *I << " to "
11120b57cec5SDimitry Andric                         << " constant " << *C << "\n");
11130b57cec5SDimitry Andric     NumGVNOpsSimplified++;
11140b57cec5SDimitry Andric     assert(isa<BasicExpression>(E) &&
11150b57cec5SDimitry Andric            "We should always have had a basic expression here");
11160b57cec5SDimitry Andric     deleteExpression(E);
1117fe6060f1SDimitry Andric     return ExprResult::some(createConstantExpression(C));
11180b57cec5SDimitry Andric   } else if (isa<Argument>(V) || isa<GlobalVariable>(V)) {
11190b57cec5SDimitry Andric     if (I)
11200b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Simplified " << *I << " to "
11210b57cec5SDimitry Andric                         << " variable " << *V << "\n");
11220b57cec5SDimitry Andric     deleteExpression(E);
1123fe6060f1SDimitry Andric     return ExprResult::some(createVariableExpression(V));
11240b57cec5SDimitry Andric   }
11250b57cec5SDimitry Andric 
11260b57cec5SDimitry Andric   CongruenceClass *CC = ValueToClass.lookup(V);
11270b57cec5SDimitry Andric   if (CC) {
11280b57cec5SDimitry Andric     if (CC->getLeader() && CC->getLeader() != I) {
1129fe6060f1SDimitry Andric       return ExprResult::some(createVariableOrConstant(CC->getLeader()), V);
11300b57cec5SDimitry Andric     }
11310b57cec5SDimitry Andric     if (CC->getDefiningExpr()) {
11320b57cec5SDimitry Andric       if (I)
11330b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Simplified " << *I << " to "
11340b57cec5SDimitry Andric                           << " expression " << *CC->getDefiningExpr() << "\n");
11350b57cec5SDimitry Andric       NumGVNOpsSimplified++;
11360b57cec5SDimitry Andric       deleteExpression(E);
1137fe6060f1SDimitry Andric       return ExprResult::some(CC->getDefiningExpr(), V);
11380b57cec5SDimitry Andric     }
11390b57cec5SDimitry Andric   }
11400b57cec5SDimitry Andric 
1141fe6060f1SDimitry Andric   return ExprResult::none();
11420b57cec5SDimitry Andric }
11430b57cec5SDimitry Andric 
11440b57cec5SDimitry Andric // Create a value expression from the instruction I, replacing operands with
11450b57cec5SDimitry Andric // their leaders.
11460b57cec5SDimitry Andric 
createExpression(Instruction * I) const1147fe6060f1SDimitry Andric NewGVN::ExprResult NewGVN::createExpression(Instruction *I) const {
11480b57cec5SDimitry Andric   auto *E = new (ExpressionAllocator) BasicExpression(I->getNumOperands());
114981ad6265SDimitry Andric   // TODO: we need to remove context instruction after Value Tracking
115081ad6265SDimitry Andric   // can run without context instruction
115181ad6265SDimitry Andric   const SimplifyQuery Q = SQ.getWithInstruction(I);
11520b57cec5SDimitry Andric 
11530b57cec5SDimitry Andric   bool AllConstant = setBasicExpressionInfo(I, E);
11540b57cec5SDimitry Andric 
11550b57cec5SDimitry Andric   if (I->isCommutative()) {
11560b57cec5SDimitry Andric     // Ensure that commutative instructions that only differ by a permutation
11570b57cec5SDimitry Andric     // of their operands get the same value number by sorting the operand value
11580b57cec5SDimitry Andric     // numbers.  Since all commutative instructions have two operands it is more
11590b57cec5SDimitry Andric     // efficient to sort by hand rather than using, say, std::sort.
11600b57cec5SDimitry Andric     assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!");
11610b57cec5SDimitry Andric     if (shouldSwapOperands(E->getOperand(0), E->getOperand(1)))
11620b57cec5SDimitry Andric       E->swapOperands(0, 1);
11630b57cec5SDimitry Andric   }
11640b57cec5SDimitry Andric   // Perform simplification.
11650b57cec5SDimitry Andric   if (auto *CI = dyn_cast<CmpInst>(I)) {
11660b57cec5SDimitry Andric     // Sort the operand value numbers so x<y and y>x get the same value
11670b57cec5SDimitry Andric     // number.
11680b57cec5SDimitry Andric     CmpInst::Predicate Predicate = CI->getPredicate();
11690b57cec5SDimitry Andric     if (shouldSwapOperands(E->getOperand(0), E->getOperand(1))) {
11700b57cec5SDimitry Andric       E->swapOperands(0, 1);
11710b57cec5SDimitry Andric       Predicate = CmpInst::getSwappedPredicate(Predicate);
11720b57cec5SDimitry Andric     }
11730b57cec5SDimitry Andric     E->setOpcode((CI->getOpcode() << 8) | Predicate);
117481ad6265SDimitry Andric     // TODO: 25% of our time is spent in simplifyCmpInst with pointer operands
11750b57cec5SDimitry Andric     assert(I->getOperand(0)->getType() == I->getOperand(1)->getType() &&
11760b57cec5SDimitry Andric            "Wrong types on cmp instruction");
11770b57cec5SDimitry Andric     assert((E->getOperand(0)->getType() == I->getOperand(0)->getType() &&
11780b57cec5SDimitry Andric             E->getOperand(1)->getType() == I->getOperand(1)->getType()));
11790b57cec5SDimitry Andric     Value *V =
118081ad6265SDimitry Andric         simplifyCmpInst(Predicate, E->getOperand(0), E->getOperand(1), Q);
1181fe6060f1SDimitry Andric     if (auto Simplified = checkExprResults(E, I, V))
1182fe6060f1SDimitry Andric       return Simplified;
11830b57cec5SDimitry Andric   } else if (isa<SelectInst>(I)) {
11840b57cec5SDimitry Andric     if (isa<Constant>(E->getOperand(0)) ||
11850b57cec5SDimitry Andric         E->getOperand(1) == E->getOperand(2)) {
11860b57cec5SDimitry Andric       assert(E->getOperand(1)->getType() == I->getOperand(1)->getType() &&
11870b57cec5SDimitry Andric              E->getOperand(2)->getType() == I->getOperand(2)->getType());
118881ad6265SDimitry Andric       Value *V = simplifySelectInst(E->getOperand(0), E->getOperand(1),
118981ad6265SDimitry Andric                                     E->getOperand(2), Q);
1190fe6060f1SDimitry Andric       if (auto Simplified = checkExprResults(E, I, V))
1191fe6060f1SDimitry Andric         return Simplified;
11920b57cec5SDimitry Andric     }
11930b57cec5SDimitry Andric   } else if (I->isBinaryOp()) {
11940b57cec5SDimitry Andric     Value *V =
119581ad6265SDimitry Andric         simplifyBinOp(E->getOpcode(), E->getOperand(0), E->getOperand(1), Q);
1196fe6060f1SDimitry Andric     if (auto Simplified = checkExprResults(E, I, V))
1197fe6060f1SDimitry Andric       return Simplified;
11980b57cec5SDimitry Andric   } else if (auto *CI = dyn_cast<CastInst>(I)) {
11990b57cec5SDimitry Andric     Value *V =
120081ad6265SDimitry Andric         simplifyCastInst(CI->getOpcode(), E->getOperand(0), CI->getType(), Q);
1201fe6060f1SDimitry Andric     if (auto Simplified = checkExprResults(E, I, V))
1202fe6060f1SDimitry Andric       return Simplified;
1203349cc55cSDimitry Andric   } else if (auto *GEPI = dyn_cast<GetElementPtrInst>(I)) {
1204bdd1243dSDimitry Andric     Value *V = simplifyGEPInst(GEPI->getSourceElementType(), *E->op_begin(),
1205bdd1243dSDimitry Andric                                ArrayRef(std::next(E->op_begin()), E->op_end()),
1206*0fca6ea1SDimitry Andric                                GEPI->getNoWrapFlags(), Q);
1207fe6060f1SDimitry Andric     if (auto Simplified = checkExprResults(E, I, V))
1208fe6060f1SDimitry Andric       return Simplified;
12090b57cec5SDimitry Andric   } else if (AllConstant) {
12100b57cec5SDimitry Andric     // We don't bother trying to simplify unless all of the operands
12110b57cec5SDimitry Andric     // were constant.
12120b57cec5SDimitry Andric     // TODO: There are a lot of Simplify*'s we could call here, if we
12130b57cec5SDimitry Andric     // wanted to.  The original motivating case for this code was a
12140b57cec5SDimitry Andric     // zext i1 false to i8, which we don't have an interface to
12150b57cec5SDimitry Andric     // simplify (IE there is no SimplifyZExt).
12160b57cec5SDimitry Andric 
12170b57cec5SDimitry Andric     SmallVector<Constant *, 8> C;
12180b57cec5SDimitry Andric     for (Value *Arg : E->operands())
12190b57cec5SDimitry Andric       C.emplace_back(cast<Constant>(Arg));
12200b57cec5SDimitry Andric 
12210b57cec5SDimitry Andric     if (Value *V = ConstantFoldInstOperands(I, C, DL, TLI))
1222fe6060f1SDimitry Andric       if (auto Simplified = checkExprResults(E, I, V))
1223fe6060f1SDimitry Andric         return Simplified;
12240b57cec5SDimitry Andric   }
1225fe6060f1SDimitry Andric   return ExprResult::some(E);
12260b57cec5SDimitry Andric }
12270b57cec5SDimitry Andric 
12280b57cec5SDimitry Andric const AggregateValueExpression *
createAggregateValueExpression(Instruction * I) const12290b57cec5SDimitry Andric NewGVN::createAggregateValueExpression(Instruction *I) const {
12300b57cec5SDimitry Andric   if (auto *II = dyn_cast<InsertValueInst>(I)) {
12310b57cec5SDimitry Andric     auto *E = new (ExpressionAllocator)
12320b57cec5SDimitry Andric         AggregateValueExpression(I->getNumOperands(), II->getNumIndices());
12330b57cec5SDimitry Andric     setBasicExpressionInfo(I, E);
12340b57cec5SDimitry Andric     E->allocateIntOperands(ExpressionAllocator);
12350b57cec5SDimitry Andric     std::copy(II->idx_begin(), II->idx_end(), int_op_inserter(E));
12360b57cec5SDimitry Andric     return E;
12370b57cec5SDimitry Andric   } else if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
12380b57cec5SDimitry Andric     auto *E = new (ExpressionAllocator)
12390b57cec5SDimitry Andric         AggregateValueExpression(I->getNumOperands(), EI->getNumIndices());
12400b57cec5SDimitry Andric     setBasicExpressionInfo(EI, E);
12410b57cec5SDimitry Andric     E->allocateIntOperands(ExpressionAllocator);
12420b57cec5SDimitry Andric     std::copy(EI->idx_begin(), EI->idx_end(), int_op_inserter(E));
12430b57cec5SDimitry Andric     return E;
12440b57cec5SDimitry Andric   }
12450b57cec5SDimitry Andric   llvm_unreachable("Unhandled type of aggregate value operation");
12460b57cec5SDimitry Andric }
12470b57cec5SDimitry Andric 
createDeadExpression() const12480b57cec5SDimitry Andric const DeadExpression *NewGVN::createDeadExpression() const {
12490b57cec5SDimitry Andric   // DeadExpression has no arguments and all DeadExpression's are the same,
12500b57cec5SDimitry Andric   // so we only need one of them.
12510b57cec5SDimitry Andric   return SingletonDeadExpression;
12520b57cec5SDimitry Andric }
12530b57cec5SDimitry Andric 
createVariableExpression(Value * V) const12540b57cec5SDimitry Andric const VariableExpression *NewGVN::createVariableExpression(Value *V) const {
12550b57cec5SDimitry Andric   auto *E = new (ExpressionAllocator) VariableExpression(V);
12560b57cec5SDimitry Andric   E->setOpcode(V->getValueID());
12570b57cec5SDimitry Andric   return E;
12580b57cec5SDimitry Andric }
12590b57cec5SDimitry Andric 
createVariableOrConstant(Value * V) const12600b57cec5SDimitry Andric const Expression *NewGVN::createVariableOrConstant(Value *V) const {
12610b57cec5SDimitry Andric   if (auto *C = dyn_cast<Constant>(V))
12620b57cec5SDimitry Andric     return createConstantExpression(C);
12630b57cec5SDimitry Andric   return createVariableExpression(V);
12640b57cec5SDimitry Andric }
12650b57cec5SDimitry Andric 
createConstantExpression(Constant * C) const12660b57cec5SDimitry Andric const ConstantExpression *NewGVN::createConstantExpression(Constant *C) const {
12670b57cec5SDimitry Andric   auto *E = new (ExpressionAllocator) ConstantExpression(C);
12680b57cec5SDimitry Andric   E->setOpcode(C->getValueID());
12690b57cec5SDimitry Andric   return E;
12700b57cec5SDimitry Andric }
12710b57cec5SDimitry Andric 
createUnknownExpression(Instruction * I) const12720b57cec5SDimitry Andric const UnknownExpression *NewGVN::createUnknownExpression(Instruction *I) const {
12730b57cec5SDimitry Andric   auto *E = new (ExpressionAllocator) UnknownExpression(I);
12740b57cec5SDimitry Andric   E->setOpcode(I->getOpcode());
12750b57cec5SDimitry Andric   return E;
12760b57cec5SDimitry Andric }
12770b57cec5SDimitry Andric 
12780b57cec5SDimitry Andric const CallExpression *
createCallExpression(CallInst * CI,const MemoryAccess * MA) const12790b57cec5SDimitry Andric NewGVN::createCallExpression(CallInst *CI, const MemoryAccess *MA) const {
12800b57cec5SDimitry Andric   // FIXME: Add operand bundles for calls.
12810b57cec5SDimitry Andric   auto *E =
12820b57cec5SDimitry Andric       new (ExpressionAllocator) CallExpression(CI->getNumOperands(), CI, MA);
12830b57cec5SDimitry Andric   setBasicExpressionInfo(CI, E);
128406c3fb27SDimitry Andric   if (CI->isCommutative()) {
128506c3fb27SDimitry Andric     // Ensure that commutative intrinsics that only differ by a permutation
128606c3fb27SDimitry Andric     // of their operands get the same value number by sorting the operand value
128706c3fb27SDimitry Andric     // numbers.
128806c3fb27SDimitry Andric     assert(CI->getNumOperands() >= 2 && "Unsupported commutative intrinsic!");
128906c3fb27SDimitry Andric     if (shouldSwapOperands(E->getOperand(0), E->getOperand(1)))
129006c3fb27SDimitry Andric       E->swapOperands(0, 1);
129106c3fb27SDimitry Andric   }
12920b57cec5SDimitry Andric   return E;
12930b57cec5SDimitry Andric }
12940b57cec5SDimitry Andric 
12950b57cec5SDimitry Andric // Return true if some equivalent of instruction Inst dominates instruction U.
someEquivalentDominates(const Instruction * Inst,const Instruction * U) const12960b57cec5SDimitry Andric bool NewGVN::someEquivalentDominates(const Instruction *Inst,
12970b57cec5SDimitry Andric                                      const Instruction *U) const {
12980b57cec5SDimitry Andric   auto *CC = ValueToClass.lookup(Inst);
12990b57cec5SDimitry Andric    // This must be an instruction because we are only called from phi nodes
13000b57cec5SDimitry Andric   // in the case that the value it needs to check against is an instruction.
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric   // The most likely candidates for dominance are the leader and the next leader.
13030b57cec5SDimitry Andric   // The leader or nextleader will dominate in all cases where there is an
13040b57cec5SDimitry Andric   // equivalent that is higher up in the dom tree.
13050b57cec5SDimitry Andric   // We can't *only* check them, however, because the
13060b57cec5SDimitry Andric   // dominator tree could have an infinite number of non-dominating siblings
13070b57cec5SDimitry Andric   // with instructions that are in the right congruence class.
13080b57cec5SDimitry Andric   //       A
13090b57cec5SDimitry Andric   // B C D E F G
13100b57cec5SDimitry Andric   // |
13110b57cec5SDimitry Andric   // H
13120b57cec5SDimitry Andric   // Instruction U could be in H,  with equivalents in every other sibling.
13130b57cec5SDimitry Andric   // Depending on the rpo order picked, the leader could be the equivalent in
13140b57cec5SDimitry Andric   // any of these siblings.
13150b57cec5SDimitry Andric   if (!CC)
13160b57cec5SDimitry Andric     return false;
13170b57cec5SDimitry Andric   if (alwaysAvailable(CC->getLeader()))
13180b57cec5SDimitry Andric     return true;
13190b57cec5SDimitry Andric   if (DT->dominates(cast<Instruction>(CC->getLeader()), U))
13200b57cec5SDimitry Andric     return true;
13210b57cec5SDimitry Andric   if (CC->getNextLeader().first &&
13220b57cec5SDimitry Andric       DT->dominates(cast<Instruction>(CC->getNextLeader().first), U))
13230b57cec5SDimitry Andric     return true;
13240b57cec5SDimitry Andric   return llvm::any_of(*CC, [&](const Value *Member) {
13250b57cec5SDimitry Andric     return Member != CC->getLeader() &&
13260b57cec5SDimitry Andric            DT->dominates(cast<Instruction>(Member), U);
13270b57cec5SDimitry Andric   });
13280b57cec5SDimitry Andric }
13290b57cec5SDimitry Andric 
13300b57cec5SDimitry Andric // See if we have a congruence class and leader for this operand, and if so,
13310b57cec5SDimitry Andric // return it. Otherwise, return the operand itself.
lookupOperandLeader(Value * V) const13320b57cec5SDimitry Andric Value *NewGVN::lookupOperandLeader(Value *V) const {
13330b57cec5SDimitry Andric   CongruenceClass *CC = ValueToClass.lookup(V);
13340b57cec5SDimitry Andric   if (CC) {
133504eeddc0SDimitry Andric     // Everything in TOP is represented by poison, as it can be any value.
13360b57cec5SDimitry Andric     // We do have to make sure we get the type right though, so we can't set the
133704eeddc0SDimitry Andric     // RepLeader to poison.
13380b57cec5SDimitry Andric     if (CC == TOPClass)
133904eeddc0SDimitry Andric       return PoisonValue::get(V->getType());
13400b57cec5SDimitry Andric     return CC->getStoredValue() ? CC->getStoredValue() : CC->getLeader();
13410b57cec5SDimitry Andric   }
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric   return V;
13440b57cec5SDimitry Andric }
13450b57cec5SDimitry Andric 
lookupMemoryLeader(const MemoryAccess * MA) const13460b57cec5SDimitry Andric const MemoryAccess *NewGVN::lookupMemoryLeader(const MemoryAccess *MA) const {
13470b57cec5SDimitry Andric   auto *CC = getMemoryClass(MA);
13480b57cec5SDimitry Andric   assert(CC->getMemoryLeader() &&
13490b57cec5SDimitry Andric          "Every MemoryAccess should be mapped to a congruence class with a "
13500b57cec5SDimitry Andric          "representative memory access");
13510b57cec5SDimitry Andric   return CC->getMemoryLeader();
13520b57cec5SDimitry Andric }
13530b57cec5SDimitry Andric 
13540b57cec5SDimitry Andric // Return true if the MemoryAccess is really equivalent to everything. This is
13550b57cec5SDimitry Andric // equivalent to the lattice value "TOP" in most lattices.  This is the initial
13560b57cec5SDimitry Andric // state of all MemoryAccesses.
isMemoryAccessTOP(const MemoryAccess * MA) const13570b57cec5SDimitry Andric bool NewGVN::isMemoryAccessTOP(const MemoryAccess *MA) const {
13580b57cec5SDimitry Andric   return getMemoryClass(MA) == TOPClass;
13590b57cec5SDimitry Andric }
13600b57cec5SDimitry Andric 
createLoadExpression(Type * LoadType,Value * PointerOp,LoadInst * LI,const MemoryAccess * MA) const13610b57cec5SDimitry Andric LoadExpression *NewGVN::createLoadExpression(Type *LoadType, Value *PointerOp,
13620b57cec5SDimitry Andric                                              LoadInst *LI,
13630b57cec5SDimitry Andric                                              const MemoryAccess *MA) const {
13640b57cec5SDimitry Andric   auto *E =
13650b57cec5SDimitry Andric       new (ExpressionAllocator) LoadExpression(1, LI, lookupMemoryLeader(MA));
13660b57cec5SDimitry Andric   E->allocateOperands(ArgRecycler, ExpressionAllocator);
13670b57cec5SDimitry Andric   E->setType(LoadType);
13680b57cec5SDimitry Andric 
13690b57cec5SDimitry Andric   // Give store and loads same opcode so they value number together.
13700b57cec5SDimitry Andric   E->setOpcode(0);
13710b57cec5SDimitry Andric   E->op_push_back(PointerOp);
13720b57cec5SDimitry Andric 
13730b57cec5SDimitry Andric   // TODO: Value number heap versions. We may be able to discover
13740b57cec5SDimitry Andric   // things alias analysis can't on it's own (IE that a store and a
13750b57cec5SDimitry Andric   // load have the same value, and thus, it isn't clobbering the load).
13760b57cec5SDimitry Andric   return E;
13770b57cec5SDimitry Andric }
13780b57cec5SDimitry Andric 
13790b57cec5SDimitry Andric const StoreExpression *
createStoreExpression(StoreInst * SI,const MemoryAccess * MA) const13800b57cec5SDimitry Andric NewGVN::createStoreExpression(StoreInst *SI, const MemoryAccess *MA) const {
13810b57cec5SDimitry Andric   auto *StoredValueLeader = lookupOperandLeader(SI->getValueOperand());
13820b57cec5SDimitry Andric   auto *E = new (ExpressionAllocator)
13830b57cec5SDimitry Andric       StoreExpression(SI->getNumOperands(), SI, StoredValueLeader, MA);
13840b57cec5SDimitry Andric   E->allocateOperands(ArgRecycler, ExpressionAllocator);
13850b57cec5SDimitry Andric   E->setType(SI->getValueOperand()->getType());
13860b57cec5SDimitry Andric 
13870b57cec5SDimitry Andric   // Give store and loads same opcode so they value number together.
13880b57cec5SDimitry Andric   E->setOpcode(0);
13890b57cec5SDimitry Andric   E->op_push_back(lookupOperandLeader(SI->getPointerOperand()));
13900b57cec5SDimitry Andric 
13910b57cec5SDimitry Andric   // TODO: Value number heap versions. We may be able to discover
13920b57cec5SDimitry Andric   // things alias analysis can't on it's own (IE that a store and a
13930b57cec5SDimitry Andric   // load have the same value, and thus, it isn't clobbering the load).
13940b57cec5SDimitry Andric   return E;
13950b57cec5SDimitry Andric }
13960b57cec5SDimitry Andric 
performSymbolicStoreEvaluation(Instruction * I) const13970b57cec5SDimitry Andric const Expression *NewGVN::performSymbolicStoreEvaluation(Instruction *I) const {
13980b57cec5SDimitry Andric   // Unlike loads, we never try to eliminate stores, so we do not check if they
13990b57cec5SDimitry Andric   // are simple and avoid value numbering them.
14000b57cec5SDimitry Andric   auto *SI = cast<StoreInst>(I);
14010b57cec5SDimitry Andric   auto *StoreAccess = getMemoryAccess(SI);
14020b57cec5SDimitry Andric   // Get the expression, if any, for the RHS of the MemoryDef.
14030b57cec5SDimitry Andric   const MemoryAccess *StoreRHS = StoreAccess->getDefiningAccess();
14040b57cec5SDimitry Andric   if (EnableStoreRefinement)
14050b57cec5SDimitry Andric     StoreRHS = MSSAWalker->getClobberingMemoryAccess(StoreAccess);
14060b57cec5SDimitry Andric   // If we bypassed the use-def chains, make sure we add a use.
14070b57cec5SDimitry Andric   StoreRHS = lookupMemoryLeader(StoreRHS);
14080b57cec5SDimitry Andric   if (StoreRHS != StoreAccess->getDefiningAccess())
14090b57cec5SDimitry Andric     addMemoryUsers(StoreRHS, StoreAccess);
14100b57cec5SDimitry Andric   // If we are defined by ourselves, use the live on entry def.
14110b57cec5SDimitry Andric   if (StoreRHS == StoreAccess)
14120b57cec5SDimitry Andric     StoreRHS = MSSA->getLiveOnEntryDef();
14130b57cec5SDimitry Andric 
14140b57cec5SDimitry Andric   if (SI->isSimple()) {
14150b57cec5SDimitry Andric     // See if we are defined by a previous store expression, it already has a
14160b57cec5SDimitry Andric     // value, and it's the same value as our current store. FIXME: Right now, we
14170b57cec5SDimitry Andric     // only do this for simple stores, we should expand to cover memcpys, etc.
14180b57cec5SDimitry Andric     const auto *LastStore = createStoreExpression(SI, StoreRHS);
14190b57cec5SDimitry Andric     const auto *LastCC = ExpressionToClass.lookup(LastStore);
14200b57cec5SDimitry Andric     // We really want to check whether the expression we matched was a store. No
14210b57cec5SDimitry Andric     // easy way to do that. However, we can check that the class we found has a
14220b57cec5SDimitry Andric     // store, which, assuming the value numbering state is not corrupt, is
14230b57cec5SDimitry Andric     // sufficient, because we must also be equivalent to that store's expression
14240b57cec5SDimitry Andric     // for it to be in the same class as the load.
14250b57cec5SDimitry Andric     if (LastCC && LastCC->getStoredValue() == LastStore->getStoredValue())
14260b57cec5SDimitry Andric       return LastStore;
14270b57cec5SDimitry Andric     // Also check if our value operand is defined by a load of the same memory
14280b57cec5SDimitry Andric     // location, and the memory state is the same as it was then (otherwise, it
14290b57cec5SDimitry Andric     // could have been overwritten later. See test32 in
14300b57cec5SDimitry Andric     // transforms/DeadStoreElimination/simple.ll).
14310b57cec5SDimitry Andric     if (auto *LI = dyn_cast<LoadInst>(LastStore->getStoredValue()))
14320b57cec5SDimitry Andric       if ((lookupOperandLeader(LI->getPointerOperand()) ==
14330b57cec5SDimitry Andric            LastStore->getOperand(0)) &&
14340b57cec5SDimitry Andric           (lookupMemoryLeader(getMemoryAccess(LI)->getDefiningAccess()) ==
14350b57cec5SDimitry Andric            StoreRHS))
14360b57cec5SDimitry Andric         return LastStore;
14370b57cec5SDimitry Andric     deleteExpression(LastStore);
14380b57cec5SDimitry Andric   }
14390b57cec5SDimitry Andric 
14400b57cec5SDimitry Andric   // If the store is not equivalent to anything, value number it as a store that
14410b57cec5SDimitry Andric   // produces a unique memory state (instead of using it's MemoryUse, we use
14420b57cec5SDimitry Andric   // it's MemoryDef).
14430b57cec5SDimitry Andric   return createStoreExpression(SI, StoreAccess);
14440b57cec5SDimitry Andric }
14450b57cec5SDimitry Andric 
14460b57cec5SDimitry Andric // See if we can extract the value of a loaded pointer from a load, a store, or
14470b57cec5SDimitry Andric // a memory instruction.
14480b57cec5SDimitry Andric const Expression *
performSymbolicLoadCoercion(Type * LoadType,Value * LoadPtr,LoadInst * LI,Instruction * DepInst,MemoryAccess * DefiningAccess) const14490b57cec5SDimitry Andric NewGVN::performSymbolicLoadCoercion(Type *LoadType, Value *LoadPtr,
14500b57cec5SDimitry Andric                                     LoadInst *LI, Instruction *DepInst,
14510b57cec5SDimitry Andric                                     MemoryAccess *DefiningAccess) const {
14520b57cec5SDimitry Andric   assert((!LI || LI->isSimple()) && "Not a simple load");
14530b57cec5SDimitry Andric   if (auto *DepSI = dyn_cast<StoreInst>(DepInst)) {
14540b57cec5SDimitry Andric     // Can't forward from non-atomic to atomic without violating memory model.
14550b57cec5SDimitry Andric     // Also don't need to coerce if they are the same type, we will just
14560b57cec5SDimitry Andric     // propagate.
14570b57cec5SDimitry Andric     if (LI->isAtomic() > DepSI->isAtomic() ||
14580b57cec5SDimitry Andric         LoadType == DepSI->getValueOperand()->getType())
14590b57cec5SDimitry Andric       return nullptr;
14600b57cec5SDimitry Andric     int Offset = analyzeLoadFromClobberingStore(LoadType, LoadPtr, DepSI, DL);
14610b57cec5SDimitry Andric     if (Offset >= 0) {
14620b57cec5SDimitry Andric       if (auto *C = dyn_cast<Constant>(
14630b57cec5SDimitry Andric               lookupOperandLeader(DepSI->getValueOperand()))) {
146406c3fb27SDimitry Andric         if (Constant *Res = getConstantValueForLoad(C, Offset, LoadType, DL)) {
14650b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "Coercing load from store " << *DepSI
146681ad6265SDimitry Andric                             << " to constant " << *Res << "\n");
146781ad6265SDimitry Andric           return createConstantExpression(Res);
146881ad6265SDimitry Andric         }
14690b57cec5SDimitry Andric       }
14700b57cec5SDimitry Andric     }
14710b57cec5SDimitry Andric   } else if (auto *DepLI = dyn_cast<LoadInst>(DepInst)) {
14720b57cec5SDimitry Andric     // Can't forward from non-atomic to atomic without violating memory model.
14730b57cec5SDimitry Andric     if (LI->isAtomic() > DepLI->isAtomic())
14740b57cec5SDimitry Andric       return nullptr;
14750b57cec5SDimitry Andric     int Offset = analyzeLoadFromClobberingLoad(LoadType, LoadPtr, DepLI, DL);
14760b57cec5SDimitry Andric     if (Offset >= 0) {
14770b57cec5SDimitry Andric       // We can coerce a constant load into a load.
14780b57cec5SDimitry Andric       if (auto *C = dyn_cast<Constant>(lookupOperandLeader(DepLI)))
14790b57cec5SDimitry Andric         if (auto *PossibleConstant =
148006c3fb27SDimitry Andric                 getConstantValueForLoad(C, Offset, LoadType, DL)) {
14810b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "Coercing load from load " << *LI
14820b57cec5SDimitry Andric                             << " to constant " << *PossibleConstant << "\n");
14830b57cec5SDimitry Andric           return createConstantExpression(PossibleConstant);
14840b57cec5SDimitry Andric         }
14850b57cec5SDimitry Andric     }
14860b57cec5SDimitry Andric   } else if (auto *DepMI = dyn_cast<MemIntrinsic>(DepInst)) {
14870b57cec5SDimitry Andric     int Offset = analyzeLoadFromClobberingMemInst(LoadType, LoadPtr, DepMI, DL);
14880b57cec5SDimitry Andric     if (Offset >= 0) {
14890b57cec5SDimitry Andric       if (auto *PossibleConstant =
14900b57cec5SDimitry Andric               getConstantMemInstValueForLoad(DepMI, Offset, LoadType, DL)) {
14910b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Coercing load from meminst " << *DepMI
14920b57cec5SDimitry Andric                           << " to constant " << *PossibleConstant << "\n");
14930b57cec5SDimitry Andric         return createConstantExpression(PossibleConstant);
14940b57cec5SDimitry Andric       }
14950b57cec5SDimitry Andric     }
14960b57cec5SDimitry Andric   }
14970b57cec5SDimitry Andric 
14980b57cec5SDimitry Andric   // All of the below are only true if the loaded pointer is produced
14990b57cec5SDimitry Andric   // by the dependent instruction.
15000b57cec5SDimitry Andric   if (LoadPtr != lookupOperandLeader(DepInst) &&
15010b57cec5SDimitry Andric       !AA->isMustAlias(LoadPtr, DepInst))
15020b57cec5SDimitry Andric     return nullptr;
15030b57cec5SDimitry Andric   // If this load really doesn't depend on anything, then we must be loading an
15040b57cec5SDimitry Andric   // undef value.  This can happen when loading for a fresh allocation with no
15050b57cec5SDimitry Andric   // intervening stores, for example.  Note that this is only true in the case
15060b57cec5SDimitry Andric   // that the result of the allocation is pointer equal to the load ptr.
150704eeddc0SDimitry Andric   if (isa<AllocaInst>(DepInst)) {
15080b57cec5SDimitry Andric     return createConstantExpression(UndefValue::get(LoadType));
15090b57cec5SDimitry Andric   }
15100b57cec5SDimitry Andric   // If this load occurs either right after a lifetime begin,
15110b57cec5SDimitry Andric   // then the loaded value is undefined.
15120b57cec5SDimitry Andric   else if (auto *II = dyn_cast<IntrinsicInst>(DepInst)) {
15130b57cec5SDimitry Andric     if (II->getIntrinsicID() == Intrinsic::lifetime_start)
15140b57cec5SDimitry Andric       return createConstantExpression(UndefValue::get(LoadType));
151581ad6265SDimitry Andric   } else if (auto *InitVal =
151681ad6265SDimitry Andric                  getInitialValueOfAllocation(DepInst, TLI, LoadType))
151704eeddc0SDimitry Andric       return createConstantExpression(InitVal);
15180b57cec5SDimitry Andric 
15190b57cec5SDimitry Andric   return nullptr;
15200b57cec5SDimitry Andric }
15210b57cec5SDimitry Andric 
performSymbolicLoadEvaluation(Instruction * I) const15220b57cec5SDimitry Andric const Expression *NewGVN::performSymbolicLoadEvaluation(Instruction *I) const {
15230b57cec5SDimitry Andric   auto *LI = cast<LoadInst>(I);
15240b57cec5SDimitry Andric 
15250b57cec5SDimitry Andric   // We can eliminate in favor of non-simple loads, but we won't be able to
15260b57cec5SDimitry Andric   // eliminate the loads themselves.
15270b57cec5SDimitry Andric   if (!LI->isSimple())
15280b57cec5SDimitry Andric     return nullptr;
15290b57cec5SDimitry Andric 
15300b57cec5SDimitry Andric   Value *LoadAddressLeader = lookupOperandLeader(LI->getPointerOperand());
153104eeddc0SDimitry Andric   // Load of undef is UB.
15320b57cec5SDimitry Andric   if (isa<UndefValue>(LoadAddressLeader))
153304eeddc0SDimitry Andric     return createConstantExpression(PoisonValue::get(LI->getType()));
15340b57cec5SDimitry Andric   MemoryAccess *OriginalAccess = getMemoryAccess(I);
15350b57cec5SDimitry Andric   MemoryAccess *DefiningAccess =
15360b57cec5SDimitry Andric       MSSAWalker->getClobberingMemoryAccess(OriginalAccess);
15370b57cec5SDimitry Andric 
15380b57cec5SDimitry Andric   if (!MSSA->isLiveOnEntryDef(DefiningAccess)) {
15390b57cec5SDimitry Andric     if (auto *MD = dyn_cast<MemoryDef>(DefiningAccess)) {
15400b57cec5SDimitry Andric       Instruction *DefiningInst = MD->getMemoryInst();
154104eeddc0SDimitry Andric       // If the defining instruction is not reachable, replace with poison.
15420b57cec5SDimitry Andric       if (!ReachableBlocks.count(DefiningInst->getParent()))
154304eeddc0SDimitry Andric         return createConstantExpression(PoisonValue::get(LI->getType()));
15440b57cec5SDimitry Andric       // This will handle stores and memory insts.  We only do if it the
15450b57cec5SDimitry Andric       // defining access has a different type, or it is a pointer produced by
15460b57cec5SDimitry Andric       // certain memory operations that cause the memory to have a fixed value
15470b57cec5SDimitry Andric       // (IE things like calloc).
15480b57cec5SDimitry Andric       if (const auto *CoercionResult =
15490b57cec5SDimitry Andric               performSymbolicLoadCoercion(LI->getType(), LoadAddressLeader, LI,
15500b57cec5SDimitry Andric                                           DefiningInst, DefiningAccess))
15510b57cec5SDimitry Andric         return CoercionResult;
15520b57cec5SDimitry Andric     }
15530b57cec5SDimitry Andric   }
15540b57cec5SDimitry Andric 
15550b57cec5SDimitry Andric   const auto *LE = createLoadExpression(LI->getType(), LoadAddressLeader, LI,
15560b57cec5SDimitry Andric                                         DefiningAccess);
15570b57cec5SDimitry Andric   // If our MemoryLeader is not our defining access, add a use to the
15580b57cec5SDimitry Andric   // MemoryLeader, so that we get reprocessed when it changes.
15590b57cec5SDimitry Andric   if (LE->getMemoryLeader() != DefiningAccess)
15600b57cec5SDimitry Andric     addMemoryUsers(LE->getMemoryLeader(), OriginalAccess);
15610b57cec5SDimitry Andric   return LE;
15620b57cec5SDimitry Andric }
15630b57cec5SDimitry Andric 
1564fe6060f1SDimitry Andric NewGVN::ExprResult
performSymbolicPredicateInfoEvaluation(IntrinsicInst * I) const15650eae32dcSDimitry Andric NewGVN::performSymbolicPredicateInfoEvaluation(IntrinsicInst *I) const {
15660b57cec5SDimitry Andric   auto *PI = PredInfo->getPredicateInfoFor(I);
15670b57cec5SDimitry Andric   if (!PI)
1568fe6060f1SDimitry Andric     return ExprResult::none();
15690b57cec5SDimitry Andric 
15700b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Found predicate info from instruction !\n");
15710b57cec5SDimitry Andric 
1572bdd1243dSDimitry Andric   const std::optional<PredicateConstraint> &Constraint = PI->getConstraint();
1573e8d8bef9SDimitry Andric   if (!Constraint)
1574fe6060f1SDimitry Andric     return ExprResult::none();
15750b57cec5SDimitry Andric 
1576e8d8bef9SDimitry Andric   CmpInst::Predicate Predicate = Constraint->Predicate;
1577e8d8bef9SDimitry Andric   Value *CmpOp0 = I->getOperand(0);
1578e8d8bef9SDimitry Andric   Value *CmpOp1 = Constraint->OtherOp;
15790b57cec5SDimitry Andric 
1580e8d8bef9SDimitry Andric   Value *FirstOp = lookupOperandLeader(CmpOp0);
1581e8d8bef9SDimitry Andric   Value *SecondOp = lookupOperandLeader(CmpOp1);
1582e8d8bef9SDimitry Andric   Value *AdditionallyUsedValue = CmpOp0;
15830b57cec5SDimitry Andric 
15840b57cec5SDimitry Andric   // Sort the ops.
15850eae32dcSDimitry Andric   if (shouldSwapOperandsForIntrinsic(FirstOp, SecondOp, I)) {
15860b57cec5SDimitry Andric     std::swap(FirstOp, SecondOp);
1587e8d8bef9SDimitry Andric     Predicate = CmpInst::getSwappedPredicate(Predicate);
1588e8d8bef9SDimitry Andric     AdditionallyUsedValue = CmpOp1;
15890b57cec5SDimitry Andric   }
15900b57cec5SDimitry Andric 
1591fe6060f1SDimitry Andric   if (Predicate == CmpInst::ICMP_EQ)
1592fe6060f1SDimitry Andric     return ExprResult::some(createVariableOrConstant(FirstOp),
1593fe6060f1SDimitry Andric                             AdditionallyUsedValue, PI);
1594e8d8bef9SDimitry Andric 
15950b57cec5SDimitry Andric   // Handle the special case of floating point.
1596e8d8bef9SDimitry Andric   if (Predicate == CmpInst::FCMP_OEQ && isa<ConstantFP>(FirstOp) &&
1597fe6060f1SDimitry Andric       !cast<ConstantFP>(FirstOp)->isZero())
1598fe6060f1SDimitry Andric     return ExprResult::some(createConstantExpression(cast<Constant>(FirstOp)),
1599fe6060f1SDimitry Andric                             AdditionallyUsedValue, PI);
1600e8d8bef9SDimitry Andric 
1601fe6060f1SDimitry Andric   return ExprResult::none();
16020b57cec5SDimitry Andric }
16030b57cec5SDimitry Andric 
16040b57cec5SDimitry Andric // Evaluate read only and pure calls, and create an expression result.
performSymbolicCallEvaluation(Instruction * I) const1605fe6060f1SDimitry Andric NewGVN::ExprResult NewGVN::performSymbolicCallEvaluation(Instruction *I) const {
16060b57cec5SDimitry Andric   auto *CI = cast<CallInst>(I);
16070b57cec5SDimitry Andric   if (auto *II = dyn_cast<IntrinsicInst>(I)) {
16080b57cec5SDimitry Andric     // Intrinsics with the returned attribute are copies of arguments.
16090b57cec5SDimitry Andric     if (auto *ReturnedValue = II->getReturnedArgOperand()) {
16100b57cec5SDimitry Andric       if (II->getIntrinsicID() == Intrinsic::ssa_copy)
16110eae32dcSDimitry Andric         if (auto Res = performSymbolicPredicateInfoEvaluation(II))
1612fe6060f1SDimitry Andric           return Res;
1613fe6060f1SDimitry Andric       return ExprResult::some(createVariableOrConstant(ReturnedValue));
16140b57cec5SDimitry Andric     }
16150b57cec5SDimitry Andric   }
1616bdd1243dSDimitry Andric 
1617bdd1243dSDimitry Andric   // FIXME: Currently the calls which may access the thread id may
1618bdd1243dSDimitry Andric   // be considered as not accessing the memory. But this is
1619bdd1243dSDimitry Andric   // problematic for coroutines, since coroutines may resume in a
1620bdd1243dSDimitry Andric   // different thread. So we disable the optimization here for the
1621bdd1243dSDimitry Andric   // correctness. However, it may block many other correct
1622bdd1243dSDimitry Andric   // optimizations. Revert this one when we detect the memory
1623bdd1243dSDimitry Andric   // accessing kind more precisely.
1624bdd1243dSDimitry Andric   if (CI->getFunction()->isPresplitCoroutine())
1625bdd1243dSDimitry Andric     return ExprResult::none();
1626bdd1243dSDimitry Andric 
162706c3fb27SDimitry Andric   // Do not combine convergent calls since they implicitly depend on the set of
162806c3fb27SDimitry Andric   // threads that is currently executing, and they might be in different basic
162906c3fb27SDimitry Andric   // blocks.
163006c3fb27SDimitry Andric   if (CI->isConvergent())
163106c3fb27SDimitry Andric     return ExprResult::none();
163206c3fb27SDimitry Andric 
16330b57cec5SDimitry Andric   if (AA->doesNotAccessMemory(CI)) {
1634fe6060f1SDimitry Andric     return ExprResult::some(
1635fe6060f1SDimitry Andric         createCallExpression(CI, TOPClass->getMemoryLeader()));
16360b57cec5SDimitry Andric   } else if (AA->onlyReadsMemory(CI)) {
16378bcb0991SDimitry Andric     if (auto *MA = MSSA->getMemoryAccess(CI)) {
16388bcb0991SDimitry Andric       auto *DefiningAccess = MSSAWalker->getClobberingMemoryAccess(MA);
1639fe6060f1SDimitry Andric       return ExprResult::some(createCallExpression(CI, DefiningAccess));
16408bcb0991SDimitry Andric     } else // MSSA determined that CI does not access memory.
1641fe6060f1SDimitry Andric       return ExprResult::some(
1642fe6060f1SDimitry Andric           createCallExpression(CI, TOPClass->getMemoryLeader()));
16430b57cec5SDimitry Andric   }
1644fe6060f1SDimitry Andric   return ExprResult::none();
16450b57cec5SDimitry Andric }
16460b57cec5SDimitry Andric 
16470b57cec5SDimitry Andric // Retrieve the memory class for a given MemoryAccess.
getMemoryClass(const MemoryAccess * MA) const16480b57cec5SDimitry Andric CongruenceClass *NewGVN::getMemoryClass(const MemoryAccess *MA) const {
16490b57cec5SDimitry Andric   auto *Result = MemoryAccessToClass.lookup(MA);
16500b57cec5SDimitry Andric   assert(Result && "Should have found memory class");
16510b57cec5SDimitry Andric   return Result;
16520b57cec5SDimitry Andric }
16530b57cec5SDimitry Andric 
16540b57cec5SDimitry Andric // Update the MemoryAccess equivalence table to say that From is equal to To,
16550b57cec5SDimitry Andric // and return true if this is different from what already existed in the table.
setMemoryClass(const MemoryAccess * From,CongruenceClass * NewClass)16560b57cec5SDimitry Andric bool NewGVN::setMemoryClass(const MemoryAccess *From,
16570b57cec5SDimitry Andric                             CongruenceClass *NewClass) {
16580b57cec5SDimitry Andric   assert(NewClass &&
16590b57cec5SDimitry Andric          "Every MemoryAccess should be getting mapped to a non-null class");
16600b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Setting " << *From);
16610b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << " equivalent to congruence class ");
16620b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << NewClass->getID()
16630b57cec5SDimitry Andric                     << " with current MemoryAccess leader ");
16640b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << *NewClass->getMemoryLeader() << "\n");
16650b57cec5SDimitry Andric 
16660b57cec5SDimitry Andric   auto LookupResult = MemoryAccessToClass.find(From);
16670b57cec5SDimitry Andric   bool Changed = false;
16680b57cec5SDimitry Andric   // If it's already in the table, see if the value changed.
16690b57cec5SDimitry Andric   if (LookupResult != MemoryAccessToClass.end()) {
16700b57cec5SDimitry Andric     auto *OldClass = LookupResult->second;
16710b57cec5SDimitry Andric     if (OldClass != NewClass) {
16720b57cec5SDimitry Andric       // If this is a phi, we have to handle memory member updates.
16730b57cec5SDimitry Andric       if (auto *MP = dyn_cast<MemoryPhi>(From)) {
16740b57cec5SDimitry Andric         OldClass->memory_erase(MP);
16750b57cec5SDimitry Andric         NewClass->memory_insert(MP);
16760b57cec5SDimitry Andric         // This may have killed the class if it had no non-memory members
16770b57cec5SDimitry Andric         if (OldClass->getMemoryLeader() == From) {
16780b57cec5SDimitry Andric           if (OldClass->definesNoMemory()) {
16790b57cec5SDimitry Andric             OldClass->setMemoryLeader(nullptr);
16800b57cec5SDimitry Andric           } else {
16810b57cec5SDimitry Andric             OldClass->setMemoryLeader(getNextMemoryLeader(OldClass));
16820b57cec5SDimitry Andric             LLVM_DEBUG(dbgs() << "Memory class leader change for class "
16830b57cec5SDimitry Andric                               << OldClass->getID() << " to "
16840b57cec5SDimitry Andric                               << *OldClass->getMemoryLeader()
16850b57cec5SDimitry Andric                               << " due to removal of a memory member " << *From
16860b57cec5SDimitry Andric                               << "\n");
16870b57cec5SDimitry Andric             markMemoryLeaderChangeTouched(OldClass);
16880b57cec5SDimitry Andric           }
16890b57cec5SDimitry Andric         }
16900b57cec5SDimitry Andric       }
16910b57cec5SDimitry Andric       // It wasn't equivalent before, and now it is.
16920b57cec5SDimitry Andric       LookupResult->second = NewClass;
16930b57cec5SDimitry Andric       Changed = true;
16940b57cec5SDimitry Andric     }
16950b57cec5SDimitry Andric   }
16960b57cec5SDimitry Andric 
16970b57cec5SDimitry Andric   return Changed;
16980b57cec5SDimitry Andric }
16990b57cec5SDimitry Andric 
17000b57cec5SDimitry Andric // Determine if a instruction is cycle-free.  That means the values in the
17010b57cec5SDimitry Andric // instruction don't depend on any expressions that can change value as a result
17020b57cec5SDimitry Andric // of the instruction.  For example, a non-cycle free instruction would be v =
17030b57cec5SDimitry Andric // phi(0, v+1).
isCycleFree(const Instruction * I) const17040b57cec5SDimitry Andric bool NewGVN::isCycleFree(const Instruction *I) const {
17050b57cec5SDimitry Andric   // In order to compute cycle-freeness, we do SCC finding on the instruction,
17060b57cec5SDimitry Andric   // and see what kind of SCC it ends up in.  If it is a singleton, it is
17070b57cec5SDimitry Andric   // cycle-free.  If it is not in a singleton, it is only cycle free if the
17080b57cec5SDimitry Andric   // other members are all phi nodes (as they do not compute anything, they are
17090b57cec5SDimitry Andric   // copies).
17100b57cec5SDimitry Andric   auto ICS = InstCycleState.lookup(I);
17110b57cec5SDimitry Andric   if (ICS == ICS_Unknown) {
17120b57cec5SDimitry Andric     SCCFinder.Start(I);
17130b57cec5SDimitry Andric     auto &SCC = SCCFinder.getComponentFor(I);
17140b57cec5SDimitry Andric     // It's cycle free if it's size 1 or the SCC is *only* phi nodes.
17150b57cec5SDimitry Andric     if (SCC.size() == 1)
17160b57cec5SDimitry Andric       InstCycleState.insert({I, ICS_CycleFree});
17170b57cec5SDimitry Andric     else {
17180b57cec5SDimitry Andric       bool AllPhis = llvm::all_of(SCC, [](const Value *V) {
17190b57cec5SDimitry Andric         return isa<PHINode>(V) || isCopyOfAPHI(V);
17200b57cec5SDimitry Andric       });
17210b57cec5SDimitry Andric       ICS = AllPhis ? ICS_CycleFree : ICS_Cycle;
1722bdd1243dSDimitry Andric       for (const auto *Member : SCC)
17230b57cec5SDimitry Andric         if (auto *MemberPhi = dyn_cast<PHINode>(Member))
17240b57cec5SDimitry Andric           InstCycleState.insert({MemberPhi, ICS});
17250b57cec5SDimitry Andric     }
17260b57cec5SDimitry Andric   }
17270b57cec5SDimitry Andric   if (ICS == ICS_Cycle)
17280b57cec5SDimitry Andric     return false;
17290b57cec5SDimitry Andric   return true;
17300b57cec5SDimitry Andric }
17310b57cec5SDimitry Andric 
17320b57cec5SDimitry Andric // Evaluate PHI nodes symbolically and create an expression result.
17330b57cec5SDimitry Andric const Expression *
performSymbolicPHIEvaluation(ArrayRef<ValPair> PHIOps,Instruction * I,BasicBlock * PHIBlock) const17340b57cec5SDimitry Andric NewGVN::performSymbolicPHIEvaluation(ArrayRef<ValPair> PHIOps,
17350b57cec5SDimitry Andric                                      Instruction *I,
17360b57cec5SDimitry Andric                                      BasicBlock *PHIBlock) const {
17370b57cec5SDimitry Andric   // True if one of the incoming phi edges is a backedge.
17380b57cec5SDimitry Andric   bool HasBackedge = false;
17390b57cec5SDimitry Andric   // All constant tracks the state of whether all the *original* phi operands
17400b57cec5SDimitry Andric   // This is really shorthand for "this phi cannot cycle due to forward
17410b57cec5SDimitry Andric   // change in value of the phi is guaranteed not to later change the value of
17420b57cec5SDimitry Andric   // the phi. IE it can't be v = phi(undef, v+1)
17430b57cec5SDimitry Andric   bool OriginalOpsConstant = true;
17440b57cec5SDimitry Andric   auto *E = cast<PHIExpression>(createPHIExpression(
17450b57cec5SDimitry Andric       PHIOps, I, PHIBlock, HasBackedge, OriginalOpsConstant));
17460b57cec5SDimitry Andric   // We match the semantics of SimplifyPhiNode from InstructionSimplify here.
17470b57cec5SDimitry Andric   // See if all arguments are the same.
17480b57cec5SDimitry Andric   // We track if any were undef because they need special handling.
174904eeddc0SDimitry Andric   bool HasUndef = false, HasPoison = false;
17500b57cec5SDimitry Andric   auto Filtered = make_filter_range(E->operands(), [&](Value *Arg) {
175104eeddc0SDimitry Andric     if (isa<PoisonValue>(Arg)) {
175204eeddc0SDimitry Andric       HasPoison = true;
175304eeddc0SDimitry Andric       return false;
175404eeddc0SDimitry Andric     }
17550b57cec5SDimitry Andric     if (isa<UndefValue>(Arg)) {
17560b57cec5SDimitry Andric       HasUndef = true;
17570b57cec5SDimitry Andric       return false;
17580b57cec5SDimitry Andric     }
17590b57cec5SDimitry Andric     return true;
17600b57cec5SDimitry Andric   });
17610b57cec5SDimitry Andric   // If we are left with no operands, it's dead.
17628bcb0991SDimitry Andric   if (Filtered.empty()) {
176304eeddc0SDimitry Andric     // If it has undef or poison at this point, it means there are no-non-undef
176404eeddc0SDimitry Andric     // arguments, and thus, the value of the phi node must be undef.
17650b57cec5SDimitry Andric     if (HasUndef) {
17660b57cec5SDimitry Andric       LLVM_DEBUG(
17670b57cec5SDimitry Andric           dbgs() << "PHI Node " << *I
17680b57cec5SDimitry Andric                  << " has no non-undef arguments, valuing it as undef\n");
17690b57cec5SDimitry Andric       return createConstantExpression(UndefValue::get(I->getType()));
17700b57cec5SDimitry Andric     }
17711fd87a68SDimitry Andric     if (HasPoison) {
17721fd87a68SDimitry Andric       LLVM_DEBUG(
17731fd87a68SDimitry Andric           dbgs() << "PHI Node " << *I
17741fd87a68SDimitry Andric                  << " has no non-poison arguments, valuing it as poison\n");
17751fd87a68SDimitry Andric       return createConstantExpression(PoisonValue::get(I->getType()));
17761fd87a68SDimitry Andric     }
17770b57cec5SDimitry Andric 
17780b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "No arguments of PHI node " << *I << " are live\n");
17790b57cec5SDimitry Andric     deleteExpression(E);
17800b57cec5SDimitry Andric     return createDeadExpression();
17810b57cec5SDimitry Andric   }
17820b57cec5SDimitry Andric   Value *AllSameValue = *(Filtered.begin());
17830b57cec5SDimitry Andric   ++Filtered.begin();
17840b57cec5SDimitry Andric   // Can't use std::equal here, sadly, because filter.begin moves.
17850b57cec5SDimitry Andric   if (llvm::all_of(Filtered, [&](Value *Arg) { return Arg == AllSameValue; })) {
17861fd87a68SDimitry Andric     // Can't fold phi(undef, X) -> X unless X can't be poison (thus X is undef
17871fd87a68SDimitry Andric     // in the worst case).
17881fd87a68SDimitry Andric     if (HasUndef && !isGuaranteedNotToBePoison(AllSameValue, AC, nullptr, DT))
17891fd87a68SDimitry Andric       return E;
17901fd87a68SDimitry Andric 
17910b57cec5SDimitry Andric     // In LLVM's non-standard representation of phi nodes, it's possible to have
17920b57cec5SDimitry Andric     // phi nodes with cycles (IE dependent on other phis that are .... dependent
17930b57cec5SDimitry Andric     // on the original phi node), especially in weird CFG's where some arguments
17940b57cec5SDimitry Andric     // are unreachable, or uninitialized along certain paths.  This can cause
17950b57cec5SDimitry Andric     // infinite loops during evaluation. We work around this by not trying to
17960b57cec5SDimitry Andric     // really evaluate them independently, but instead using a variable
17970b57cec5SDimitry Andric     // expression to say if one is equivalent to the other.
17981fd87a68SDimitry Andric     // We also special case undef/poison, so that if we have an undef, we can't
17991fd87a68SDimitry Andric     // use the common value unless it dominates the phi block.
180004eeddc0SDimitry Andric     if (HasPoison || HasUndef) {
18010b57cec5SDimitry Andric       // If we have undef and at least one other value, this is really a
18020b57cec5SDimitry Andric       // multivalued phi, and we need to know if it's cycle free in order to
18030b57cec5SDimitry Andric       // evaluate whether we can ignore the undef.  The other parts of this are
18040b57cec5SDimitry Andric       // just shortcuts.  If there is no backedge, or all operands are
18050b57cec5SDimitry Andric       // constants, it also must be cycle free.
18060b57cec5SDimitry Andric       if (HasBackedge && !OriginalOpsConstant &&
18070b57cec5SDimitry Andric           !isa<UndefValue>(AllSameValue) && !isCycleFree(I))
18080b57cec5SDimitry Andric         return E;
18090b57cec5SDimitry Andric 
18100b57cec5SDimitry Andric       // Only have to check for instructions
18110b57cec5SDimitry Andric       if (auto *AllSameInst = dyn_cast<Instruction>(AllSameValue))
18120b57cec5SDimitry Andric         if (!someEquivalentDominates(AllSameInst, I))
18130b57cec5SDimitry Andric           return E;
18140b57cec5SDimitry Andric     }
18150b57cec5SDimitry Andric     // Can't simplify to something that comes later in the iteration.
18160b57cec5SDimitry Andric     // Otherwise, when and if it changes congruence class, we will never catch
18170b57cec5SDimitry Andric     // up. We will always be a class behind it.
18180b57cec5SDimitry Andric     if (isa<Instruction>(AllSameValue) &&
18190b57cec5SDimitry Andric         InstrToDFSNum(AllSameValue) > InstrToDFSNum(I))
18200b57cec5SDimitry Andric       return E;
18210b57cec5SDimitry Andric     NumGVNPhisAllSame++;
18220b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Simplified PHI node " << *I << " to " << *AllSameValue
18230b57cec5SDimitry Andric                       << "\n");
18240b57cec5SDimitry Andric     deleteExpression(E);
18250b57cec5SDimitry Andric     return createVariableOrConstant(AllSameValue);
18260b57cec5SDimitry Andric   }
18270b57cec5SDimitry Andric   return E;
18280b57cec5SDimitry Andric }
18290b57cec5SDimitry Andric 
18300b57cec5SDimitry Andric const Expression *
performSymbolicAggrValueEvaluation(Instruction * I) const18310b57cec5SDimitry Andric NewGVN::performSymbolicAggrValueEvaluation(Instruction *I) const {
18320b57cec5SDimitry Andric   if (auto *EI = dyn_cast<ExtractValueInst>(I)) {
18330b57cec5SDimitry Andric     auto *WO = dyn_cast<WithOverflowInst>(EI->getAggregateOperand());
18340b57cec5SDimitry Andric     if (WO && EI->getNumIndices() == 1 && *EI->idx_begin() == 0)
18350b57cec5SDimitry Andric       // EI is an extract from one of our with.overflow intrinsics. Synthesize
18360b57cec5SDimitry Andric       // a semantically equivalent expression instead of an extract value
18370b57cec5SDimitry Andric       // expression.
18380b57cec5SDimitry Andric       return createBinaryExpression(WO->getBinaryOp(), EI->getType(),
18390b57cec5SDimitry Andric                                     WO->getLHS(), WO->getRHS(), I);
18400b57cec5SDimitry Andric   }
18410b57cec5SDimitry Andric 
18420b57cec5SDimitry Andric   return createAggregateValueExpression(I);
18430b57cec5SDimitry Andric }
18440b57cec5SDimitry Andric 
performSymbolicCmpEvaluation(Instruction * I) const1845fe6060f1SDimitry Andric NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
18460b57cec5SDimitry Andric   assert(isa<CmpInst>(I) && "Expected a cmp instruction.");
18470b57cec5SDimitry Andric 
18480b57cec5SDimitry Andric   auto *CI = cast<CmpInst>(I);
18490b57cec5SDimitry Andric   // See if our operands are equal to those of a previous predicate, and if so,
18500b57cec5SDimitry Andric   // if it implies true or false.
18510b57cec5SDimitry Andric   auto Op0 = lookupOperandLeader(CI->getOperand(0));
18520b57cec5SDimitry Andric   auto Op1 = lookupOperandLeader(CI->getOperand(1));
18530b57cec5SDimitry Andric   auto OurPredicate = CI->getPredicate();
18540b57cec5SDimitry Andric   if (shouldSwapOperands(Op0, Op1)) {
18550b57cec5SDimitry Andric     std::swap(Op0, Op1);
18560b57cec5SDimitry Andric     OurPredicate = CI->getSwappedPredicate();
18570b57cec5SDimitry Andric   }
18580b57cec5SDimitry Andric 
18590b57cec5SDimitry Andric   // Avoid processing the same info twice.
18600b57cec5SDimitry Andric   const PredicateBase *LastPredInfo = nullptr;
18610b57cec5SDimitry Andric   // See if we know something about the comparison itself, like it is the target
18620b57cec5SDimitry Andric   // of an assume.
18630b57cec5SDimitry Andric   auto *CmpPI = PredInfo->getPredicateInfoFor(I);
1864349cc55cSDimitry Andric   if (isa_and_nonnull<PredicateAssume>(CmpPI))
1865fe6060f1SDimitry Andric     return ExprResult::some(
1866fe6060f1SDimitry Andric         createConstantExpression(ConstantInt::getTrue(CI->getType())));
18670b57cec5SDimitry Andric 
18680b57cec5SDimitry Andric   if (Op0 == Op1) {
18690b57cec5SDimitry Andric     // This condition does not depend on predicates, no need to add users
18700b57cec5SDimitry Andric     if (CI->isTrueWhenEqual())
1871fe6060f1SDimitry Andric       return ExprResult::some(
1872fe6060f1SDimitry Andric           createConstantExpression(ConstantInt::getTrue(CI->getType())));
18730b57cec5SDimitry Andric     else if (CI->isFalseWhenEqual())
1874fe6060f1SDimitry Andric       return ExprResult::some(
1875fe6060f1SDimitry Andric           createConstantExpression(ConstantInt::getFalse(CI->getType())));
18760b57cec5SDimitry Andric   }
18770b57cec5SDimitry Andric 
18780b57cec5SDimitry Andric   // NOTE: Because we are comparing both operands here and below, and using
18790b57cec5SDimitry Andric   // previous comparisons, we rely on fact that predicateinfo knows to mark
18800b57cec5SDimitry Andric   // comparisons that use renamed operands as users of the earlier comparisons.
18810b57cec5SDimitry Andric   // It is *not* enough to just mark predicateinfo renamed operands as users of
18820b57cec5SDimitry Andric   // the earlier comparisons, because the *other* operand may have changed in a
18830b57cec5SDimitry Andric   // previous iteration.
18840b57cec5SDimitry Andric   // Example:
18850b57cec5SDimitry Andric   // icmp slt %a, %b
18860b57cec5SDimitry Andric   // %b.0 = ssa.copy(%b)
18870b57cec5SDimitry Andric   // false branch:
18880b57cec5SDimitry Andric   // icmp slt %c, %b.0
18890b57cec5SDimitry Andric 
18900b57cec5SDimitry Andric   // %c and %a may start out equal, and thus, the code below will say the second
18910b57cec5SDimitry Andric   // %icmp is false.  c may become equal to something else, and in that case the
18920b57cec5SDimitry Andric   // %second icmp *must* be reexamined, but would not if only the renamed
18930b57cec5SDimitry Andric   // %operands are considered users of the icmp.
18940b57cec5SDimitry Andric 
18950b57cec5SDimitry Andric   // *Currently* we only check one level of comparisons back, and only mark one
18960b57cec5SDimitry Andric   // level back as touched when changes happen.  If you modify this code to look
18970b57cec5SDimitry Andric   // back farther through comparisons, you *must* mark the appropriate
18980b57cec5SDimitry Andric   // comparisons as users in PredicateInfo.cpp, or you will cause bugs.  See if
18990b57cec5SDimitry Andric   // we know something just from the operands themselves
19000b57cec5SDimitry Andric 
19010b57cec5SDimitry Andric   // See if our operands have predicate info, so that we may be able to derive
19020b57cec5SDimitry Andric   // something from a previous comparison.
19030b57cec5SDimitry Andric   for (const auto &Op : CI->operands()) {
19040b57cec5SDimitry Andric     auto *PI = PredInfo->getPredicateInfoFor(Op);
19050b57cec5SDimitry Andric     if (const auto *PBranch = dyn_cast_or_null<PredicateBranch>(PI)) {
19060b57cec5SDimitry Andric       if (PI == LastPredInfo)
19070b57cec5SDimitry Andric         continue;
19080b57cec5SDimitry Andric       LastPredInfo = PI;
19090b57cec5SDimitry Andric       // In phi of ops cases, we may have predicate info that we are evaluating
19100b57cec5SDimitry Andric       // in a different context.
19115f757f3fSDimitry Andric       if (!DT->dominates(PBranch->To, I->getParent()))
19120b57cec5SDimitry Andric         continue;
19130b57cec5SDimitry Andric       // TODO: Along the false edge, we may know more things too, like
19140b57cec5SDimitry Andric       // icmp of
19150b57cec5SDimitry Andric       // same operands is false.
19160b57cec5SDimitry Andric       // TODO: We only handle actual comparison conditions below, not
19170b57cec5SDimitry Andric       // and/or.
19180b57cec5SDimitry Andric       auto *BranchCond = dyn_cast<CmpInst>(PBranch->Condition);
19190b57cec5SDimitry Andric       if (!BranchCond)
19200b57cec5SDimitry Andric         continue;
19210b57cec5SDimitry Andric       auto *BranchOp0 = lookupOperandLeader(BranchCond->getOperand(0));
19220b57cec5SDimitry Andric       auto *BranchOp1 = lookupOperandLeader(BranchCond->getOperand(1));
19230b57cec5SDimitry Andric       auto BranchPredicate = BranchCond->getPredicate();
19240b57cec5SDimitry Andric       if (shouldSwapOperands(BranchOp0, BranchOp1)) {
19250b57cec5SDimitry Andric         std::swap(BranchOp0, BranchOp1);
19260b57cec5SDimitry Andric         BranchPredicate = BranchCond->getSwappedPredicate();
19270b57cec5SDimitry Andric       }
19280b57cec5SDimitry Andric       if (BranchOp0 == Op0 && BranchOp1 == Op1) {
19290b57cec5SDimitry Andric         if (PBranch->TrueEdge) {
19300b57cec5SDimitry Andric           // If we know the previous predicate is true and we are in the true
19310b57cec5SDimitry Andric           // edge then we may be implied true or false.
19320b57cec5SDimitry Andric           if (CmpInst::isImpliedTrueByMatchingCmp(BranchPredicate,
19330b57cec5SDimitry Andric                                                   OurPredicate)) {
1934fe6060f1SDimitry Andric             return ExprResult::some(
1935fe6060f1SDimitry Andric                 createConstantExpression(ConstantInt::getTrue(CI->getType())),
1936fe6060f1SDimitry Andric                 PI);
19370b57cec5SDimitry Andric           }
19380b57cec5SDimitry Andric 
19390b57cec5SDimitry Andric           if (CmpInst::isImpliedFalseByMatchingCmp(BranchPredicate,
19400b57cec5SDimitry Andric                                                    OurPredicate)) {
1941fe6060f1SDimitry Andric             return ExprResult::some(
1942fe6060f1SDimitry Andric                 createConstantExpression(ConstantInt::getFalse(CI->getType())),
1943fe6060f1SDimitry Andric                 PI);
19440b57cec5SDimitry Andric           }
19450b57cec5SDimitry Andric         } else {
19460b57cec5SDimitry Andric           // Just handle the ne and eq cases, where if we have the same
19470b57cec5SDimitry Andric           // operands, we may know something.
19480b57cec5SDimitry Andric           if (BranchPredicate == OurPredicate) {
19490b57cec5SDimitry Andric             // Same predicate, same ops,we know it was false, so this is false.
1950fe6060f1SDimitry Andric             return ExprResult::some(
1951fe6060f1SDimitry Andric                 createConstantExpression(ConstantInt::getFalse(CI->getType())),
1952fe6060f1SDimitry Andric                 PI);
19530b57cec5SDimitry Andric           } else if (BranchPredicate ==
19540b57cec5SDimitry Andric                      CmpInst::getInversePredicate(OurPredicate)) {
19550b57cec5SDimitry Andric             // Inverse predicate, we know the other was false, so this is true.
1956fe6060f1SDimitry Andric             return ExprResult::some(
1957fe6060f1SDimitry Andric                 createConstantExpression(ConstantInt::getTrue(CI->getType())),
1958fe6060f1SDimitry Andric                 PI);
19590b57cec5SDimitry Andric           }
19600b57cec5SDimitry Andric         }
19610b57cec5SDimitry Andric       }
19620b57cec5SDimitry Andric     }
19630b57cec5SDimitry Andric   }
19640b57cec5SDimitry Andric   // Create expression will take care of simplifyCmpInst
19650b57cec5SDimitry Andric   return createExpression(I);
19660b57cec5SDimitry Andric }
19670b57cec5SDimitry Andric 
19685f757f3fSDimitry Andric // Substitute and symbolize the instruction before value numbering.
1969fe6060f1SDimitry Andric NewGVN::ExprResult
performSymbolicEvaluation(Instruction * I,SmallPtrSetImpl<Value * > & Visited) const19705f757f3fSDimitry Andric NewGVN::performSymbolicEvaluation(Instruction *I,
19710b57cec5SDimitry Andric                                   SmallPtrSetImpl<Value *> &Visited) const {
1972fe6060f1SDimitry Andric 
19730b57cec5SDimitry Andric   const Expression *E = nullptr;
19740b57cec5SDimitry Andric   // TODO: memory intrinsics.
19750b57cec5SDimitry Andric   // TODO: Some day, we should do the forward propagation and reassociation
19760b57cec5SDimitry Andric   // parts of the algorithm.
19770b57cec5SDimitry Andric   switch (I->getOpcode()) {
19780b57cec5SDimitry Andric   case Instruction::ExtractValue:
19790b57cec5SDimitry Andric   case Instruction::InsertValue:
19800b57cec5SDimitry Andric     E = performSymbolicAggrValueEvaluation(I);
19810b57cec5SDimitry Andric     break;
19820b57cec5SDimitry Andric   case Instruction::PHI: {
19830b57cec5SDimitry Andric     SmallVector<ValPair, 3> Ops;
19840b57cec5SDimitry Andric     auto *PN = cast<PHINode>(I);
19850b57cec5SDimitry Andric     for (unsigned i = 0; i < PN->getNumOperands(); ++i)
19860b57cec5SDimitry Andric       Ops.push_back({PN->getIncomingValue(i), PN->getIncomingBlock(i)});
19870b57cec5SDimitry Andric     // Sort to ensure the invariant createPHIExpression requires is met.
19880b57cec5SDimitry Andric     sortPHIOps(Ops);
19890b57cec5SDimitry Andric     E = performSymbolicPHIEvaluation(Ops, I, getBlockForValue(I));
19900b57cec5SDimitry Andric   } break;
19910b57cec5SDimitry Andric   case Instruction::Call:
1992fe6060f1SDimitry Andric     return performSymbolicCallEvaluation(I);
19930b57cec5SDimitry Andric     break;
19940b57cec5SDimitry Andric   case Instruction::Store:
19950b57cec5SDimitry Andric     E = performSymbolicStoreEvaluation(I);
19960b57cec5SDimitry Andric     break;
19970b57cec5SDimitry Andric   case Instruction::Load:
19980b57cec5SDimitry Andric     E = performSymbolicLoadEvaluation(I);
19990b57cec5SDimitry Andric     break;
20000b57cec5SDimitry Andric   case Instruction::BitCast:
20010b57cec5SDimitry Andric   case Instruction::AddrSpaceCast:
200206c3fb27SDimitry Andric   case Instruction::Freeze:
2003fe6060f1SDimitry Andric     return createExpression(I);
20040b57cec5SDimitry Andric     break;
20050b57cec5SDimitry Andric   case Instruction::ICmp:
20060b57cec5SDimitry Andric   case Instruction::FCmp:
2007fe6060f1SDimitry Andric     return performSymbolicCmpEvaluation(I);
20080b57cec5SDimitry Andric     break;
20090b57cec5SDimitry Andric   case Instruction::FNeg:
20100b57cec5SDimitry Andric   case Instruction::Add:
20110b57cec5SDimitry Andric   case Instruction::FAdd:
20120b57cec5SDimitry Andric   case Instruction::Sub:
20130b57cec5SDimitry Andric   case Instruction::FSub:
20140b57cec5SDimitry Andric   case Instruction::Mul:
20150b57cec5SDimitry Andric   case Instruction::FMul:
20160b57cec5SDimitry Andric   case Instruction::UDiv:
20170b57cec5SDimitry Andric   case Instruction::SDiv:
20180b57cec5SDimitry Andric   case Instruction::FDiv:
20190b57cec5SDimitry Andric   case Instruction::URem:
20200b57cec5SDimitry Andric   case Instruction::SRem:
20210b57cec5SDimitry Andric   case Instruction::FRem:
20220b57cec5SDimitry Andric   case Instruction::Shl:
20230b57cec5SDimitry Andric   case Instruction::LShr:
20240b57cec5SDimitry Andric   case Instruction::AShr:
20250b57cec5SDimitry Andric   case Instruction::And:
20260b57cec5SDimitry Andric   case Instruction::Or:
20270b57cec5SDimitry Andric   case Instruction::Xor:
20280b57cec5SDimitry Andric   case Instruction::Trunc:
20290b57cec5SDimitry Andric   case Instruction::ZExt:
20300b57cec5SDimitry Andric   case Instruction::SExt:
20310b57cec5SDimitry Andric   case Instruction::FPToUI:
20320b57cec5SDimitry Andric   case Instruction::FPToSI:
20330b57cec5SDimitry Andric   case Instruction::UIToFP:
20340b57cec5SDimitry Andric   case Instruction::SIToFP:
20350b57cec5SDimitry Andric   case Instruction::FPTrunc:
20360b57cec5SDimitry Andric   case Instruction::FPExt:
20370b57cec5SDimitry Andric   case Instruction::PtrToInt:
20380b57cec5SDimitry Andric   case Instruction::IntToPtr:
20390b57cec5SDimitry Andric   case Instruction::Select:
20400b57cec5SDimitry Andric   case Instruction::ExtractElement:
20410b57cec5SDimitry Andric   case Instruction::InsertElement:
20420b57cec5SDimitry Andric   case Instruction::GetElementPtr:
2043fe6060f1SDimitry Andric     return createExpression(I);
20440b57cec5SDimitry Andric     break;
20455ffd83dbSDimitry Andric   case Instruction::ShuffleVector:
20465ffd83dbSDimitry Andric     // FIXME: Add support for shufflevector to createExpression.
2047fe6060f1SDimitry Andric     return ExprResult::none();
20480b57cec5SDimitry Andric   default:
2049fe6060f1SDimitry Andric     return ExprResult::none();
20500b57cec5SDimitry Andric   }
2051fe6060f1SDimitry Andric   return ExprResult::some(E);
20520b57cec5SDimitry Andric }
20530b57cec5SDimitry Andric 
20540b57cec5SDimitry Andric // Look up a container of values/instructions in a map, and touch all the
20550b57cec5SDimitry Andric // instructions in the container.  Then erase value from the map.
20560b57cec5SDimitry Andric template <typename Map, typename KeyType>
touchAndErase(Map & M,const KeyType & Key)20570b57cec5SDimitry Andric void NewGVN::touchAndErase(Map &M, const KeyType &Key) {
20580b57cec5SDimitry Andric   const auto Result = M.find_as(Key);
20590b57cec5SDimitry Andric   if (Result != M.end()) {
20600b57cec5SDimitry Andric     for (const typename Map::mapped_type::value_type Mapped : Result->second)
20610b57cec5SDimitry Andric       TouchedInstructions.set(InstrToDFSNum(Mapped));
20620b57cec5SDimitry Andric     M.erase(Result);
20630b57cec5SDimitry Andric   }
20640b57cec5SDimitry Andric }
20650b57cec5SDimitry Andric 
addAdditionalUsers(Value * To,Value * User) const20660b57cec5SDimitry Andric void NewGVN::addAdditionalUsers(Value *To, Value *User) const {
20670b57cec5SDimitry Andric   assert(User && To != User);
20680b57cec5SDimitry Andric   if (isa<Instruction>(To))
20690b57cec5SDimitry Andric     AdditionalUsers[To].insert(User);
20700b57cec5SDimitry Andric }
20710b57cec5SDimitry Andric 
addAdditionalUsers(ExprResult & Res,Instruction * User) const2072fe6060f1SDimitry Andric void NewGVN::addAdditionalUsers(ExprResult &Res, Instruction *User) const {
2073fe6060f1SDimitry Andric   if (Res.ExtraDep && Res.ExtraDep != User)
2074fe6060f1SDimitry Andric     addAdditionalUsers(Res.ExtraDep, User);
2075fe6060f1SDimitry Andric   Res.ExtraDep = nullptr;
2076fe6060f1SDimitry Andric 
2077fe6060f1SDimitry Andric   if (Res.PredDep) {
2078fe6060f1SDimitry Andric     if (const auto *PBranch = dyn_cast<PredicateBranch>(Res.PredDep))
2079fe6060f1SDimitry Andric       PredicateToUsers[PBranch->Condition].insert(User);
2080fe6060f1SDimitry Andric     else if (const auto *PAssume = dyn_cast<PredicateAssume>(Res.PredDep))
2081fe6060f1SDimitry Andric       PredicateToUsers[PAssume->Condition].insert(User);
2082fe6060f1SDimitry Andric   }
2083fe6060f1SDimitry Andric   Res.PredDep = nullptr;
2084fe6060f1SDimitry Andric }
2085fe6060f1SDimitry Andric 
markUsersTouched(Value * V)20860b57cec5SDimitry Andric void NewGVN::markUsersTouched(Value *V) {
20870b57cec5SDimitry Andric   // Now mark the users as touched.
20880b57cec5SDimitry Andric   for (auto *User : V->users()) {
20890b57cec5SDimitry Andric     assert(isa<Instruction>(User) && "Use of value not within an instruction?");
20900b57cec5SDimitry Andric     TouchedInstructions.set(InstrToDFSNum(User));
20910b57cec5SDimitry Andric   }
20920b57cec5SDimitry Andric   touchAndErase(AdditionalUsers, V);
20930b57cec5SDimitry Andric }
20940b57cec5SDimitry Andric 
addMemoryUsers(const MemoryAccess * To,MemoryAccess * U) const20950b57cec5SDimitry Andric void NewGVN::addMemoryUsers(const MemoryAccess *To, MemoryAccess *U) const {
20960b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Adding memory user " << *U << " to " << *To << "\n");
20970b57cec5SDimitry Andric   MemoryToUsers[To].insert(U);
20980b57cec5SDimitry Andric }
20990b57cec5SDimitry Andric 
markMemoryDefTouched(const MemoryAccess * MA)21000b57cec5SDimitry Andric void NewGVN::markMemoryDefTouched(const MemoryAccess *MA) {
21010b57cec5SDimitry Andric   TouchedInstructions.set(MemoryToDFSNum(MA));
21020b57cec5SDimitry Andric }
21030b57cec5SDimitry Andric 
markMemoryUsersTouched(const MemoryAccess * MA)21040b57cec5SDimitry Andric void NewGVN::markMemoryUsersTouched(const MemoryAccess *MA) {
21050b57cec5SDimitry Andric   if (isa<MemoryUse>(MA))
21060b57cec5SDimitry Andric     return;
2107bdd1243dSDimitry Andric   for (const auto *U : MA->users())
21080b57cec5SDimitry Andric     TouchedInstructions.set(MemoryToDFSNum(U));
21090b57cec5SDimitry Andric   touchAndErase(MemoryToUsers, MA);
21100b57cec5SDimitry Andric }
21110b57cec5SDimitry Andric 
21120b57cec5SDimitry Andric // Touch all the predicates that depend on this instruction.
markPredicateUsersTouched(Instruction * I)21130b57cec5SDimitry Andric void NewGVN::markPredicateUsersTouched(Instruction *I) {
21140b57cec5SDimitry Andric   touchAndErase(PredicateToUsers, I);
21150b57cec5SDimitry Andric }
21160b57cec5SDimitry Andric 
21170b57cec5SDimitry Andric // Mark users affected by a memory leader change.
markMemoryLeaderChangeTouched(CongruenceClass * CC)21180b57cec5SDimitry Andric void NewGVN::markMemoryLeaderChangeTouched(CongruenceClass *CC) {
2119bdd1243dSDimitry Andric   for (const auto *M : CC->memory())
21200b57cec5SDimitry Andric     markMemoryDefTouched(M);
21210b57cec5SDimitry Andric }
21220b57cec5SDimitry Andric 
21230b57cec5SDimitry Andric // Touch the instructions that need to be updated after a congruence class has a
21240b57cec5SDimitry Andric // leader change, and mark changed values.
markValueLeaderChangeTouched(CongruenceClass * CC)21250b57cec5SDimitry Andric void NewGVN::markValueLeaderChangeTouched(CongruenceClass *CC) {
2126bdd1243dSDimitry Andric   for (auto *M : *CC) {
21270b57cec5SDimitry Andric     if (auto *I = dyn_cast<Instruction>(M))
21280b57cec5SDimitry Andric       TouchedInstructions.set(InstrToDFSNum(I));
21290b57cec5SDimitry Andric     LeaderChanges.insert(M);
21300b57cec5SDimitry Andric   }
21310b57cec5SDimitry Andric }
21320b57cec5SDimitry Andric 
21330b57cec5SDimitry Andric // Give a range of things that have instruction DFS numbers, this will return
21340b57cec5SDimitry Andric // the member of the range with the smallest dfs number.
21350b57cec5SDimitry Andric template <class T, class Range>
getMinDFSOfRange(const Range & R) const21360b57cec5SDimitry Andric T *NewGVN::getMinDFSOfRange(const Range &R) const {
21370b57cec5SDimitry Andric   std::pair<T *, unsigned> MinDFS = {nullptr, ~0U};
21380b57cec5SDimitry Andric   for (const auto X : R) {
21390b57cec5SDimitry Andric     auto DFSNum = InstrToDFSNum(X);
21400b57cec5SDimitry Andric     if (DFSNum < MinDFS.second)
21410b57cec5SDimitry Andric       MinDFS = {X, DFSNum};
21420b57cec5SDimitry Andric   }
21430b57cec5SDimitry Andric   return MinDFS.first;
21440b57cec5SDimitry Andric }
21450b57cec5SDimitry Andric 
21460b57cec5SDimitry Andric // This function returns the MemoryAccess that should be the next leader of
21470b57cec5SDimitry Andric // congruence class CC, under the assumption that the current leader is going to
21480b57cec5SDimitry Andric // disappear.
getNextMemoryLeader(CongruenceClass * CC) const21490b57cec5SDimitry Andric const MemoryAccess *NewGVN::getNextMemoryLeader(CongruenceClass *CC) const {
21500b57cec5SDimitry Andric   // TODO: If this ends up to slow, we can maintain a next memory leader like we
21510b57cec5SDimitry Andric   // do for regular leaders.
21520b57cec5SDimitry Andric   // Make sure there will be a leader to find.
21530b57cec5SDimitry Andric   assert(!CC->definesNoMemory() && "Can't get next leader if there is none");
21540b57cec5SDimitry Andric   if (CC->getStoreCount() > 0) {
21550b57cec5SDimitry Andric     if (auto *NL = dyn_cast_or_null<StoreInst>(CC->getNextLeader().first))
21560b57cec5SDimitry Andric       return getMemoryAccess(NL);
21570b57cec5SDimitry Andric     // Find the store with the minimum DFS number.
21580b57cec5SDimitry Andric     auto *V = getMinDFSOfRange<Value>(make_filter_range(
21590b57cec5SDimitry Andric         *CC, [&](const Value *V) { return isa<StoreInst>(V); }));
21600b57cec5SDimitry Andric     return getMemoryAccess(cast<StoreInst>(V));
21610b57cec5SDimitry Andric   }
21620b57cec5SDimitry Andric   assert(CC->getStoreCount() == 0);
21630b57cec5SDimitry Andric 
21640b57cec5SDimitry Andric   // Given our assertion, hitting this part must mean
21650b57cec5SDimitry Andric   // !OldClass->memory_empty()
21660b57cec5SDimitry Andric   if (CC->memory_size() == 1)
21670b57cec5SDimitry Andric     return *CC->memory_begin();
21680b57cec5SDimitry Andric   return getMinDFSOfRange<const MemoryPhi>(CC->memory());
21690b57cec5SDimitry Andric }
21700b57cec5SDimitry Andric 
21710b57cec5SDimitry Andric // This function returns the next value leader of a congruence class, under the
21720b57cec5SDimitry Andric // assumption that the current leader is going away.  This should end up being
21730b57cec5SDimitry Andric // the next most dominating member.
getNextValueLeader(CongruenceClass * CC) const21740b57cec5SDimitry Andric Value *NewGVN::getNextValueLeader(CongruenceClass *CC) const {
21750b57cec5SDimitry Andric   // We don't need to sort members if there is only 1, and we don't care about
21760b57cec5SDimitry Andric   // sorting the TOP class because everything either gets out of it or is
21770b57cec5SDimitry Andric   // unreachable.
21780b57cec5SDimitry Andric 
21790b57cec5SDimitry Andric   if (CC->size() == 1 || CC == TOPClass) {
21800b57cec5SDimitry Andric     return *(CC->begin());
21810b57cec5SDimitry Andric   } else if (CC->getNextLeader().first) {
21820b57cec5SDimitry Andric     ++NumGVNAvoidedSortedLeaderChanges;
21830b57cec5SDimitry Andric     return CC->getNextLeader().first;
21840b57cec5SDimitry Andric   } else {
21850b57cec5SDimitry Andric     ++NumGVNSortedLeaderChanges;
21860b57cec5SDimitry Andric     // NOTE: If this ends up to slow, we can maintain a dual structure for
21870b57cec5SDimitry Andric     // member testing/insertion, or keep things mostly sorted, and sort only
21880b57cec5SDimitry Andric     // here, or use SparseBitVector or ....
21890b57cec5SDimitry Andric     return getMinDFSOfRange<Value>(*CC);
21900b57cec5SDimitry Andric   }
21910b57cec5SDimitry Andric }
21920b57cec5SDimitry Andric 
21930b57cec5SDimitry Andric // Move a MemoryAccess, currently in OldClass, to NewClass, including updates to
21940b57cec5SDimitry Andric // the memory members, etc for the move.
21950b57cec5SDimitry Andric //
21960b57cec5SDimitry Andric // The invariants of this function are:
21970b57cec5SDimitry Andric //
21980b57cec5SDimitry Andric // - I must be moving to NewClass from OldClass
21990b57cec5SDimitry Andric // - The StoreCount of OldClass and NewClass is expected to have been updated
22000b57cec5SDimitry Andric //   for I already if it is a store.
22010b57cec5SDimitry Andric // - The OldClass memory leader has not been updated yet if I was the leader.
moveMemoryToNewCongruenceClass(Instruction * I,MemoryAccess * InstMA,CongruenceClass * OldClass,CongruenceClass * NewClass)22020b57cec5SDimitry Andric void NewGVN::moveMemoryToNewCongruenceClass(Instruction *I,
22030b57cec5SDimitry Andric                                             MemoryAccess *InstMA,
22040b57cec5SDimitry Andric                                             CongruenceClass *OldClass,
22050b57cec5SDimitry Andric                                             CongruenceClass *NewClass) {
22060b57cec5SDimitry Andric   // If the leader is I, and we had a representative MemoryAccess, it should
22070b57cec5SDimitry Andric   // be the MemoryAccess of OldClass.
22080b57cec5SDimitry Andric   assert((!InstMA || !OldClass->getMemoryLeader() ||
22090b57cec5SDimitry Andric           OldClass->getLeader() != I ||
22100b57cec5SDimitry Andric           MemoryAccessToClass.lookup(OldClass->getMemoryLeader()) ==
22110b57cec5SDimitry Andric               MemoryAccessToClass.lookup(InstMA)) &&
22120b57cec5SDimitry Andric          "Representative MemoryAccess mismatch");
22130b57cec5SDimitry Andric   // First, see what happens to the new class
22140b57cec5SDimitry Andric   if (!NewClass->getMemoryLeader()) {
22150b57cec5SDimitry Andric     // Should be a new class, or a store becoming a leader of a new class.
22160b57cec5SDimitry Andric     assert(NewClass->size() == 1 ||
22170b57cec5SDimitry Andric            (isa<StoreInst>(I) && NewClass->getStoreCount() == 1));
22180b57cec5SDimitry Andric     NewClass->setMemoryLeader(InstMA);
22190b57cec5SDimitry Andric     // Mark it touched if we didn't just create a singleton
22200b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Memory class leader change for class "
22210b57cec5SDimitry Andric                       << NewClass->getID()
22220b57cec5SDimitry Andric                       << " due to new memory instruction becoming leader\n");
22230b57cec5SDimitry Andric     markMemoryLeaderChangeTouched(NewClass);
22240b57cec5SDimitry Andric   }
22250b57cec5SDimitry Andric   setMemoryClass(InstMA, NewClass);
22260b57cec5SDimitry Andric   // Now, fixup the old class if necessary
22270b57cec5SDimitry Andric   if (OldClass->getMemoryLeader() == InstMA) {
22280b57cec5SDimitry Andric     if (!OldClass->definesNoMemory()) {
22290b57cec5SDimitry Andric       OldClass->setMemoryLeader(getNextMemoryLeader(OldClass));
22300b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Memory class leader change for class "
22310b57cec5SDimitry Andric                         << OldClass->getID() << " to "
22320b57cec5SDimitry Andric                         << *OldClass->getMemoryLeader()
22330b57cec5SDimitry Andric                         << " due to removal of old leader " << *InstMA << "\n");
22340b57cec5SDimitry Andric       markMemoryLeaderChangeTouched(OldClass);
22350b57cec5SDimitry Andric     } else
22360b57cec5SDimitry Andric       OldClass->setMemoryLeader(nullptr);
22370b57cec5SDimitry Andric   }
22380b57cec5SDimitry Andric }
22390b57cec5SDimitry Andric 
22400b57cec5SDimitry Andric // Move a value, currently in OldClass, to be part of NewClass
22410b57cec5SDimitry Andric // Update OldClass and NewClass for the move (including changing leaders, etc).
moveValueToNewCongruenceClass(Instruction * I,const Expression * E,CongruenceClass * OldClass,CongruenceClass * NewClass)22420b57cec5SDimitry Andric void NewGVN::moveValueToNewCongruenceClass(Instruction *I, const Expression *E,
22430b57cec5SDimitry Andric                                            CongruenceClass *OldClass,
22440b57cec5SDimitry Andric                                            CongruenceClass *NewClass) {
22450b57cec5SDimitry Andric   if (I == OldClass->getNextLeader().first)
22460b57cec5SDimitry Andric     OldClass->resetNextLeader();
22470b57cec5SDimitry Andric 
22480b57cec5SDimitry Andric   OldClass->erase(I);
22490b57cec5SDimitry Andric   NewClass->insert(I);
22500b57cec5SDimitry Andric 
22510b57cec5SDimitry Andric   if (NewClass->getLeader() != I)
22520b57cec5SDimitry Andric     NewClass->addPossibleNextLeader({I, InstrToDFSNum(I)});
22530b57cec5SDimitry Andric   // Handle our special casing of stores.
22540b57cec5SDimitry Andric   if (auto *SI = dyn_cast<StoreInst>(I)) {
22550b57cec5SDimitry Andric     OldClass->decStoreCount();
22560b57cec5SDimitry Andric     // Okay, so when do we want to make a store a leader of a class?
22570b57cec5SDimitry Andric     // If we have a store defined by an earlier load, we want the earlier load
22580b57cec5SDimitry Andric     // to lead the class.
22590b57cec5SDimitry Andric     // If we have a store defined by something else, we want the store to lead
22600b57cec5SDimitry Andric     // the class so everything else gets the "something else" as a value.
22610b57cec5SDimitry Andric     // If we have a store as the single member of the class, we want the store
22620b57cec5SDimitry Andric     // as the leader
22630b57cec5SDimitry Andric     if (NewClass->getStoreCount() == 0 && !NewClass->getStoredValue()) {
22640b57cec5SDimitry Andric       // If it's a store expression we are using, it means we are not equivalent
22650b57cec5SDimitry Andric       // to something earlier.
22660b57cec5SDimitry Andric       if (auto *SE = dyn_cast<StoreExpression>(E)) {
22670b57cec5SDimitry Andric         NewClass->setStoredValue(SE->getStoredValue());
22680b57cec5SDimitry Andric         markValueLeaderChangeTouched(NewClass);
22690b57cec5SDimitry Andric         // Shift the new class leader to be the store
22700b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Changing leader of congruence class "
22710b57cec5SDimitry Andric                           << NewClass->getID() << " from "
22720b57cec5SDimitry Andric                           << *NewClass->getLeader() << " to  " << *SI
22730b57cec5SDimitry Andric                           << " because store joined class\n");
22740b57cec5SDimitry Andric         // If we changed the leader, we have to mark it changed because we don't
22750b57cec5SDimitry Andric         // know what it will do to symbolic evaluation.
22760b57cec5SDimitry Andric         NewClass->setLeader(SI);
22770b57cec5SDimitry Andric       }
22780b57cec5SDimitry Andric       // We rely on the code below handling the MemoryAccess change.
22790b57cec5SDimitry Andric     }
22800b57cec5SDimitry Andric     NewClass->incStoreCount();
22810b57cec5SDimitry Andric   }
22820b57cec5SDimitry Andric   // True if there is no memory instructions left in a class that had memory
22830b57cec5SDimitry Andric   // instructions before.
22840b57cec5SDimitry Andric 
22850b57cec5SDimitry Andric   // If it's not a memory use, set the MemoryAccess equivalence
22860b57cec5SDimitry Andric   auto *InstMA = dyn_cast_or_null<MemoryDef>(getMemoryAccess(I));
22870b57cec5SDimitry Andric   if (InstMA)
22880b57cec5SDimitry Andric     moveMemoryToNewCongruenceClass(I, InstMA, OldClass, NewClass);
22890b57cec5SDimitry Andric   ValueToClass[I] = NewClass;
22900b57cec5SDimitry Andric   // See if we destroyed the class or need to swap leaders.
22910b57cec5SDimitry Andric   if (OldClass->empty() && OldClass != TOPClass) {
22920b57cec5SDimitry Andric     if (OldClass->getDefiningExpr()) {
22930b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Erasing expression " << *OldClass->getDefiningExpr()
22940b57cec5SDimitry Andric                         << " from table\n");
22950b57cec5SDimitry Andric       // We erase it as an exact expression to make sure we don't just erase an
22960b57cec5SDimitry Andric       // equivalent one.
22970b57cec5SDimitry Andric       auto Iter = ExpressionToClass.find_as(
22980b57cec5SDimitry Andric           ExactEqualsExpression(*OldClass->getDefiningExpr()));
22990b57cec5SDimitry Andric       if (Iter != ExpressionToClass.end())
23000b57cec5SDimitry Andric         ExpressionToClass.erase(Iter);
23010b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS
23020b57cec5SDimitry Andric       assert(
23030b57cec5SDimitry Andric           (*OldClass->getDefiningExpr() != *E || ExpressionToClass.lookup(E)) &&
23040b57cec5SDimitry Andric           "We erased the expression we just inserted, which should not happen");
23050b57cec5SDimitry Andric #endif
23060b57cec5SDimitry Andric     }
23070b57cec5SDimitry Andric   } else if (OldClass->getLeader() == I) {
23080b57cec5SDimitry Andric     // When the leader changes, the value numbering of
23090b57cec5SDimitry Andric     // everything may change due to symbolization changes, so we need to
23100b57cec5SDimitry Andric     // reprocess.
23110b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Value class leader change for class "
23120b57cec5SDimitry Andric                       << OldClass->getID() << "\n");
23130b57cec5SDimitry Andric     ++NumGVNLeaderChanges;
23140b57cec5SDimitry Andric     // Destroy the stored value if there are no more stores to represent it.
23150b57cec5SDimitry Andric     // Note that this is basically clean up for the expression removal that
23160b57cec5SDimitry Andric     // happens below.  If we remove stores from a class, we may leave it as a
23170b57cec5SDimitry Andric     // class of equivalent memory phis.
23180b57cec5SDimitry Andric     if (OldClass->getStoreCount() == 0) {
23190b57cec5SDimitry Andric       if (OldClass->getStoredValue())
23200b57cec5SDimitry Andric         OldClass->setStoredValue(nullptr);
23210b57cec5SDimitry Andric     }
23220b57cec5SDimitry Andric     OldClass->setLeader(getNextValueLeader(OldClass));
23230b57cec5SDimitry Andric     OldClass->resetNextLeader();
23240b57cec5SDimitry Andric     markValueLeaderChangeTouched(OldClass);
23250b57cec5SDimitry Andric   }
23260b57cec5SDimitry Andric }
23270b57cec5SDimitry Andric 
23280b57cec5SDimitry Andric // For a given expression, mark the phi of ops instructions that could have
23290b57cec5SDimitry Andric // changed as a result.
markPhiOfOpsChanged(const Expression * E)23300b57cec5SDimitry Andric void NewGVN::markPhiOfOpsChanged(const Expression *E) {
23310b57cec5SDimitry Andric   touchAndErase(ExpressionToPhiOfOps, E);
23320b57cec5SDimitry Andric }
23330b57cec5SDimitry Andric 
23340b57cec5SDimitry Andric // Perform congruence finding on a given value numbering expression.
performCongruenceFinding(Instruction * I,const Expression * E)23350b57cec5SDimitry Andric void NewGVN::performCongruenceFinding(Instruction *I, const Expression *E) {
23360b57cec5SDimitry Andric   // This is guaranteed to return something, since it will at least find
23370b57cec5SDimitry Andric   // TOP.
23380b57cec5SDimitry Andric 
23390b57cec5SDimitry Andric   CongruenceClass *IClass = ValueToClass.lookup(I);
23400b57cec5SDimitry Andric   assert(IClass && "Should have found a IClass");
23410b57cec5SDimitry Andric   // Dead classes should have been eliminated from the mapping.
23420b57cec5SDimitry Andric   assert(!IClass->isDead() && "Found a dead class");
23430b57cec5SDimitry Andric 
23440b57cec5SDimitry Andric   CongruenceClass *EClass = nullptr;
23450b57cec5SDimitry Andric   if (const auto *VE = dyn_cast<VariableExpression>(E)) {
23460b57cec5SDimitry Andric     EClass = ValueToClass.lookup(VE->getVariableValue());
23470b57cec5SDimitry Andric   } else if (isa<DeadExpression>(E)) {
23480b57cec5SDimitry Andric     EClass = TOPClass;
23490b57cec5SDimitry Andric   }
23500b57cec5SDimitry Andric   if (!EClass) {
23510b57cec5SDimitry Andric     auto lookupResult = ExpressionToClass.insert({E, nullptr});
23520b57cec5SDimitry Andric 
23530b57cec5SDimitry Andric     // If it's not in the value table, create a new congruence class.
23540b57cec5SDimitry Andric     if (lookupResult.second) {
23550b57cec5SDimitry Andric       CongruenceClass *NewClass = createCongruenceClass(nullptr, E);
23560b57cec5SDimitry Andric       auto place = lookupResult.first;
23570b57cec5SDimitry Andric       place->second = NewClass;
23580b57cec5SDimitry Andric 
23590b57cec5SDimitry Andric       // Constants and variables should always be made the leader.
23600b57cec5SDimitry Andric       if (const auto *CE = dyn_cast<ConstantExpression>(E)) {
23610b57cec5SDimitry Andric         NewClass->setLeader(CE->getConstantValue());
23620b57cec5SDimitry Andric       } else if (const auto *SE = dyn_cast<StoreExpression>(E)) {
23630b57cec5SDimitry Andric         StoreInst *SI = SE->getStoreInst();
23640b57cec5SDimitry Andric         NewClass->setLeader(SI);
23650b57cec5SDimitry Andric         NewClass->setStoredValue(SE->getStoredValue());
23660b57cec5SDimitry Andric         // The RepMemoryAccess field will be filled in properly by the
23670b57cec5SDimitry Andric         // moveValueToNewCongruenceClass call.
23680b57cec5SDimitry Andric       } else {
23690b57cec5SDimitry Andric         NewClass->setLeader(I);
23700b57cec5SDimitry Andric       }
23710b57cec5SDimitry Andric       assert(!isa<VariableExpression>(E) &&
23720b57cec5SDimitry Andric              "VariableExpression should have been handled already");
23730b57cec5SDimitry Andric 
23740b57cec5SDimitry Andric       EClass = NewClass;
23750b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Created new congruence class for " << *I
23760b57cec5SDimitry Andric                         << " using expression " << *E << " at "
23770b57cec5SDimitry Andric                         << NewClass->getID() << " and leader "
23780b57cec5SDimitry Andric                         << *(NewClass->getLeader()));
23790b57cec5SDimitry Andric       if (NewClass->getStoredValue())
23800b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << " and stored value "
23810b57cec5SDimitry Andric                           << *(NewClass->getStoredValue()));
23820b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "\n");
23830b57cec5SDimitry Andric     } else {
23840b57cec5SDimitry Andric       EClass = lookupResult.first->second;
23850b57cec5SDimitry Andric       if (isa<ConstantExpression>(E))
23860b57cec5SDimitry Andric         assert((isa<Constant>(EClass->getLeader()) ||
23870b57cec5SDimitry Andric                 (EClass->getStoredValue() &&
23880b57cec5SDimitry Andric                  isa<Constant>(EClass->getStoredValue()))) &&
23890b57cec5SDimitry Andric                "Any class with a constant expression should have a "
23900b57cec5SDimitry Andric                "constant leader");
23910b57cec5SDimitry Andric 
23920b57cec5SDimitry Andric       assert(EClass && "Somehow don't have an eclass");
23930b57cec5SDimitry Andric 
23940b57cec5SDimitry Andric       assert(!EClass->isDead() && "We accidentally looked up a dead class");
23950b57cec5SDimitry Andric     }
23960b57cec5SDimitry Andric   }
23970b57cec5SDimitry Andric   bool ClassChanged = IClass != EClass;
23980b57cec5SDimitry Andric   bool LeaderChanged = LeaderChanges.erase(I);
23990b57cec5SDimitry Andric   if (ClassChanged || LeaderChanged) {
24000b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "New class " << EClass->getID() << " for expression "
24010b57cec5SDimitry Andric                       << *E << "\n");
24020b57cec5SDimitry Andric     if (ClassChanged) {
24030b57cec5SDimitry Andric       moveValueToNewCongruenceClass(I, E, IClass, EClass);
24040b57cec5SDimitry Andric       markPhiOfOpsChanged(E);
24050b57cec5SDimitry Andric     }
24060b57cec5SDimitry Andric 
24070b57cec5SDimitry Andric     markUsersTouched(I);
24080b57cec5SDimitry Andric     if (MemoryAccess *MA = getMemoryAccess(I))
24090b57cec5SDimitry Andric       markMemoryUsersTouched(MA);
24100b57cec5SDimitry Andric     if (auto *CI = dyn_cast<CmpInst>(I))
24110b57cec5SDimitry Andric       markPredicateUsersTouched(CI);
24120b57cec5SDimitry Andric   }
24130b57cec5SDimitry Andric   // If we changed the class of the store, we want to ensure nothing finds the
24140b57cec5SDimitry Andric   // old store expression.  In particular, loads do not compare against stored
24150b57cec5SDimitry Andric   // value, so they will find old store expressions (and associated class
24160b57cec5SDimitry Andric   // mappings) if we leave them in the table.
24170b57cec5SDimitry Andric   if (ClassChanged && isa<StoreInst>(I)) {
24180b57cec5SDimitry Andric     auto *OldE = ValueToExpression.lookup(I);
24190b57cec5SDimitry Andric     // It could just be that the old class died. We don't want to erase it if we
24200b57cec5SDimitry Andric     // just moved classes.
24210b57cec5SDimitry Andric     if (OldE && isa<StoreExpression>(OldE) && *E != *OldE) {
24220b57cec5SDimitry Andric       // Erase this as an exact expression to ensure we don't erase expressions
24230b57cec5SDimitry Andric       // equivalent to it.
24240b57cec5SDimitry Andric       auto Iter = ExpressionToClass.find_as(ExactEqualsExpression(*OldE));
24250b57cec5SDimitry Andric       if (Iter != ExpressionToClass.end())
24260b57cec5SDimitry Andric         ExpressionToClass.erase(Iter);
24270b57cec5SDimitry Andric     }
24280b57cec5SDimitry Andric   }
24290b57cec5SDimitry Andric   ValueToExpression[I] = E;
24300b57cec5SDimitry Andric }
24310b57cec5SDimitry Andric 
24320b57cec5SDimitry Andric // Process the fact that Edge (from, to) is reachable, including marking
24330b57cec5SDimitry Andric // any newly reachable blocks and instructions for processing.
updateReachableEdge(BasicBlock * From,BasicBlock * To)24340b57cec5SDimitry Andric void NewGVN::updateReachableEdge(BasicBlock *From, BasicBlock *To) {
24350b57cec5SDimitry Andric   // Check if the Edge was reachable before.
24360b57cec5SDimitry Andric   if (ReachableEdges.insert({From, To}).second) {
24370b57cec5SDimitry Andric     // If this block wasn't reachable before, all instructions are touched.
24380b57cec5SDimitry Andric     if (ReachableBlocks.insert(To).second) {
24390b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Block " << getBlockName(To)
24400b57cec5SDimitry Andric                         << " marked reachable\n");
24410b57cec5SDimitry Andric       const auto &InstRange = BlockInstRange.lookup(To);
24420b57cec5SDimitry Andric       TouchedInstructions.set(InstRange.first, InstRange.second);
24430b57cec5SDimitry Andric     } else {
24440b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Block " << getBlockName(To)
24450b57cec5SDimitry Andric                         << " was reachable, but new edge {"
24460b57cec5SDimitry Andric                         << getBlockName(From) << "," << getBlockName(To)
24470b57cec5SDimitry Andric                         << "} to it found\n");
24480b57cec5SDimitry Andric 
24490b57cec5SDimitry Andric       // We've made an edge reachable to an existing block, which may
24500b57cec5SDimitry Andric       // impact predicates. Otherwise, only mark the phi nodes as touched, as
24510b57cec5SDimitry Andric       // they are the only thing that depend on new edges. Anything using their
24520b57cec5SDimitry Andric       // values will get propagated to if necessary.
24530b57cec5SDimitry Andric       if (MemoryAccess *MemPhi = getMemoryAccess(To))
24540b57cec5SDimitry Andric         TouchedInstructions.set(InstrToDFSNum(MemPhi));
24550b57cec5SDimitry Andric 
24560b57cec5SDimitry Andric       // FIXME: We should just add a union op on a Bitvector and
24570b57cec5SDimitry Andric       // SparseBitVector.  We can do it word by word faster than we are doing it
24580b57cec5SDimitry Andric       // here.
24590b57cec5SDimitry Andric       for (auto InstNum : RevisitOnReachabilityChange[To])
24600b57cec5SDimitry Andric         TouchedInstructions.set(InstNum);
24610b57cec5SDimitry Andric     }
24620b57cec5SDimitry Andric   }
24630b57cec5SDimitry Andric }
24640b57cec5SDimitry Andric 
24650b57cec5SDimitry Andric // Given a predicate condition (from a switch, cmp, or whatever) and a block,
24660b57cec5SDimitry Andric // see if we know some constant value for it already.
findConditionEquivalence(Value * Cond) const24670b57cec5SDimitry Andric Value *NewGVN::findConditionEquivalence(Value *Cond) const {
24680b57cec5SDimitry Andric   auto Result = lookupOperandLeader(Cond);
24690b57cec5SDimitry Andric   return isa<Constant>(Result) ? Result : nullptr;
24700b57cec5SDimitry Andric }
24710b57cec5SDimitry Andric 
24720b57cec5SDimitry Andric // Process the outgoing edges of a block for reachability.
processOutgoingEdges(Instruction * TI,BasicBlock * B)24730b57cec5SDimitry Andric void NewGVN::processOutgoingEdges(Instruction *TI, BasicBlock *B) {
24740b57cec5SDimitry Andric   // Evaluate reachability of terminator instruction.
24758bcb0991SDimitry Andric   Value *Cond;
24768bcb0991SDimitry Andric   BasicBlock *TrueSucc, *FalseSucc;
24778bcb0991SDimitry Andric   if (match(TI, m_Br(m_Value(Cond), TrueSucc, FalseSucc))) {
24780b57cec5SDimitry Andric     Value *CondEvaluated = findConditionEquivalence(Cond);
24790b57cec5SDimitry Andric     if (!CondEvaluated) {
24800b57cec5SDimitry Andric       if (auto *I = dyn_cast<Instruction>(Cond)) {
2481fe6060f1SDimitry Andric         SmallPtrSet<Value *, 4> Visited;
2482fe6060f1SDimitry Andric         auto Res = performSymbolicEvaluation(I, Visited);
2483fe6060f1SDimitry Andric         if (const auto *CE = dyn_cast_or_null<ConstantExpression>(Res.Expr)) {
24840b57cec5SDimitry Andric           CondEvaluated = CE->getConstantValue();
2485fe6060f1SDimitry Andric           addAdditionalUsers(Res, I);
2486fe6060f1SDimitry Andric         } else {
2487fe6060f1SDimitry Andric           // Did not use simplification result, no need to add the extra
2488fe6060f1SDimitry Andric           // dependency.
2489fe6060f1SDimitry Andric           Res.ExtraDep = nullptr;
24900b57cec5SDimitry Andric         }
24910b57cec5SDimitry Andric       } else if (isa<ConstantInt>(Cond)) {
24920b57cec5SDimitry Andric         CondEvaluated = Cond;
24930b57cec5SDimitry Andric       }
24940b57cec5SDimitry Andric     }
24950b57cec5SDimitry Andric     ConstantInt *CI;
24960b57cec5SDimitry Andric     if (CondEvaluated && (CI = dyn_cast<ConstantInt>(CondEvaluated))) {
24970b57cec5SDimitry Andric       if (CI->isOne()) {
24980b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Condition for Terminator " << *TI
24990b57cec5SDimitry Andric                           << " evaluated to true\n");
25000b57cec5SDimitry Andric         updateReachableEdge(B, TrueSucc);
25010b57cec5SDimitry Andric       } else if (CI->isZero()) {
25020b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Condition for Terminator " << *TI
25030b57cec5SDimitry Andric                           << " evaluated to false\n");
25040b57cec5SDimitry Andric         updateReachableEdge(B, FalseSucc);
25050b57cec5SDimitry Andric       }
25060b57cec5SDimitry Andric     } else {
25070b57cec5SDimitry Andric       updateReachableEdge(B, TrueSucc);
25080b57cec5SDimitry Andric       updateReachableEdge(B, FalseSucc);
25090b57cec5SDimitry Andric     }
25100b57cec5SDimitry Andric   } else if (auto *SI = dyn_cast<SwitchInst>(TI)) {
25110b57cec5SDimitry Andric     // For switches, propagate the case values into the case
25120b57cec5SDimitry Andric     // destinations.
25130b57cec5SDimitry Andric 
25140b57cec5SDimitry Andric     Value *SwitchCond = SI->getCondition();
25150b57cec5SDimitry Andric     Value *CondEvaluated = findConditionEquivalence(SwitchCond);
25160b57cec5SDimitry Andric     // See if we were able to turn this switch statement into a constant.
25170b57cec5SDimitry Andric     if (CondEvaluated && isa<ConstantInt>(CondEvaluated)) {
25180b57cec5SDimitry Andric       auto *CondVal = cast<ConstantInt>(CondEvaluated);
25190b57cec5SDimitry Andric       // We should be able to get case value for this.
25200b57cec5SDimitry Andric       auto Case = *SI->findCaseValue(CondVal);
25210b57cec5SDimitry Andric       if (Case.getCaseSuccessor() == SI->getDefaultDest()) {
25220b57cec5SDimitry Andric         // We proved the value is outside of the range of the case.
25230b57cec5SDimitry Andric         // We can't do anything other than mark the default dest as reachable,
25240b57cec5SDimitry Andric         // and go home.
25250b57cec5SDimitry Andric         updateReachableEdge(B, SI->getDefaultDest());
25260b57cec5SDimitry Andric         return;
25270b57cec5SDimitry Andric       }
25280b57cec5SDimitry Andric       // Now get where it goes and mark it reachable.
25290b57cec5SDimitry Andric       BasicBlock *TargetBlock = Case.getCaseSuccessor();
25300b57cec5SDimitry Andric       updateReachableEdge(B, TargetBlock);
25310b57cec5SDimitry Andric     } else {
2532*0fca6ea1SDimitry Andric       for (BasicBlock *TargetBlock : successors(SI->getParent()))
25330b57cec5SDimitry Andric         updateReachableEdge(B, TargetBlock);
25340b57cec5SDimitry Andric     }
25350b57cec5SDimitry Andric   } else {
25360b57cec5SDimitry Andric     // Otherwise this is either unconditional, or a type we have no
25370b57cec5SDimitry Andric     // idea about. Just mark successors as reachable.
2538*0fca6ea1SDimitry Andric     for (BasicBlock *TargetBlock : successors(TI->getParent()))
25390b57cec5SDimitry Andric       updateReachableEdge(B, TargetBlock);
25400b57cec5SDimitry Andric 
25410b57cec5SDimitry Andric     // This also may be a memory defining terminator, in which case, set it
25420b57cec5SDimitry Andric     // equivalent only to itself.
25430b57cec5SDimitry Andric     //
25440b57cec5SDimitry Andric     auto *MA = getMemoryAccess(TI);
25450b57cec5SDimitry Andric     if (MA && !isa<MemoryUse>(MA)) {
25460b57cec5SDimitry Andric       auto *CC = ensureLeaderOfMemoryClass(MA);
25470b57cec5SDimitry Andric       if (setMemoryClass(MA, CC))
25480b57cec5SDimitry Andric         markMemoryUsersTouched(MA);
25490b57cec5SDimitry Andric     }
25500b57cec5SDimitry Andric   }
25510b57cec5SDimitry Andric }
25520b57cec5SDimitry Andric 
25530b57cec5SDimitry Andric // Remove the PHI of Ops PHI for I
removePhiOfOps(Instruction * I,PHINode * PHITemp)25540b57cec5SDimitry Andric void NewGVN::removePhiOfOps(Instruction *I, PHINode *PHITemp) {
25550b57cec5SDimitry Andric   InstrDFS.erase(PHITemp);
25560b57cec5SDimitry Andric   // It's still a temp instruction. We keep it in the array so it gets erased.
25570b57cec5SDimitry Andric   // However, it's no longer used by I, or in the block
25580b57cec5SDimitry Andric   TempToBlock.erase(PHITemp);
25590b57cec5SDimitry Andric   RealToTemp.erase(I);
25600b57cec5SDimitry Andric   // We don't remove the users from the phi node uses. This wastes a little
25610b57cec5SDimitry Andric   // time, but such is life.  We could use two sets to track which were there
25620b57cec5SDimitry Andric   // are the start of NewGVN, and which were added, but right nowt he cost of
25630b57cec5SDimitry Andric   // tracking is more than the cost of checking for more phi of ops.
25640b57cec5SDimitry Andric }
25650b57cec5SDimitry Andric 
25660b57cec5SDimitry Andric // Add PHI Op in BB as a PHI of operations version of ExistingValue.
addPhiOfOps(PHINode * Op,BasicBlock * BB,Instruction * ExistingValue)25670b57cec5SDimitry Andric void NewGVN::addPhiOfOps(PHINode *Op, BasicBlock *BB,
25680b57cec5SDimitry Andric                          Instruction *ExistingValue) {
25690b57cec5SDimitry Andric   InstrDFS[Op] = InstrToDFSNum(ExistingValue);
25700b57cec5SDimitry Andric   AllTempInstructions.insert(Op);
25710b57cec5SDimitry Andric   TempToBlock[Op] = BB;
25720b57cec5SDimitry Andric   RealToTemp[ExistingValue] = Op;
25730b57cec5SDimitry Andric   // Add all users to phi node use, as they are now uses of the phi of ops phis
25740b57cec5SDimitry Andric   // and may themselves be phi of ops.
25750b57cec5SDimitry Andric   for (auto *U : ExistingValue->users())
25760b57cec5SDimitry Andric     if (auto *UI = dyn_cast<Instruction>(U))
25770b57cec5SDimitry Andric       PHINodeUses.insert(UI);
25780b57cec5SDimitry Andric }
25790b57cec5SDimitry Andric 
okayForPHIOfOps(const Instruction * I)25800b57cec5SDimitry Andric static bool okayForPHIOfOps(const Instruction *I) {
25810b57cec5SDimitry Andric   if (!EnablePhiOfOps)
25820b57cec5SDimitry Andric     return false;
25830b57cec5SDimitry Andric   return isa<BinaryOperator>(I) || isa<SelectInst>(I) || isa<CmpInst>(I) ||
25840b57cec5SDimitry Andric          isa<LoadInst>(I);
25850b57cec5SDimitry Andric }
25860b57cec5SDimitry Andric 
25870b57cec5SDimitry Andric // Return true if this operand will be safe to use for phi of ops.
25880b57cec5SDimitry Andric //
25890b57cec5SDimitry Andric // The reason some operands are unsafe is that we are not trying to recursively
25900b57cec5SDimitry Andric // translate everything back through phi nodes.  We actually expect some lookups
25910b57cec5SDimitry Andric // of expressions to fail.  In particular, a lookup where the expression cannot
25920b57cec5SDimitry Andric // exist in the predecessor.  This is true even if the expression, as shown, can
25930b57cec5SDimitry Andric // be determined to be constant.
OpIsSafeForPHIOfOps(Value * V,const BasicBlock * PHIBlock,SmallPtrSetImpl<const Value * > & Visited)25940b57cec5SDimitry Andric bool NewGVN::OpIsSafeForPHIOfOps(Value *V, const BasicBlock *PHIBlock,
25950b57cec5SDimitry Andric                                  SmallPtrSetImpl<const Value *> &Visited) {
2596bdd1243dSDimitry Andric   SmallVector<Value *, 4> Worklist;
2597bdd1243dSDimitry Andric   Worklist.push_back(V);
25980b57cec5SDimitry Andric   while (!Worklist.empty()) {
25990b57cec5SDimitry Andric     auto *I = Worklist.pop_back_val();
2600bdd1243dSDimitry Andric     if (!isa<Instruction>(I))
2601bdd1243dSDimitry Andric       continue;
2602bdd1243dSDimitry Andric 
2603*0fca6ea1SDimitry Andric     auto OISIt = OpSafeForPHIOfOps.find({I, CacheIdx});
2604bdd1243dSDimitry Andric     if (OISIt != OpSafeForPHIOfOps.end())
2605bdd1243dSDimitry Andric       return OISIt->second;
2606bdd1243dSDimitry Andric 
2607bdd1243dSDimitry Andric     // Keep walking until we either dominate the phi block, or hit a phi, or run
2608bdd1243dSDimitry Andric     // out of things to check.
2609bdd1243dSDimitry Andric     if (DT->properlyDominates(getBlockForValue(I), PHIBlock)) {
2610*0fca6ea1SDimitry Andric       OpSafeForPHIOfOps.insert({{I, CacheIdx}, true});
2611bdd1243dSDimitry Andric       continue;
2612bdd1243dSDimitry Andric     }
2613bdd1243dSDimitry Andric     // PHI in the same block.
2614bdd1243dSDimitry Andric     if (isa<PHINode>(I) && getBlockForValue(I) == PHIBlock) {
2615*0fca6ea1SDimitry Andric       OpSafeForPHIOfOps.insert({{I, CacheIdx}, false});
26160b57cec5SDimitry Andric       return false;
26170b57cec5SDimitry Andric     }
2618bdd1243dSDimitry Andric 
2619bdd1243dSDimitry Andric     auto *OrigI = cast<Instruction>(I);
2620bdd1243dSDimitry Andric     // When we hit an instruction that reads memory (load, call, etc), we must
2621bdd1243dSDimitry Andric     // consider any store that may happen in the loop. For now, we assume the
2622bdd1243dSDimitry Andric     // worst: there is a store in the loop that alias with this read.
2623bdd1243dSDimitry Andric     // The case where the load is outside the loop is already covered by the
2624bdd1243dSDimitry Andric     // dominator check above.
2625bdd1243dSDimitry Andric     // TODO: relax this condition
2626bdd1243dSDimitry Andric     if (OrigI->mayReadFromMemory())
2627bdd1243dSDimitry Andric       return false;
2628bdd1243dSDimitry Andric 
2629bdd1243dSDimitry Andric     // Check the operands of the current instruction.
2630bdd1243dSDimitry Andric     for (auto *Op : OrigI->operand_values()) {
2631bdd1243dSDimitry Andric       if (!isa<Instruction>(Op))
2632bdd1243dSDimitry Andric         continue;
2633bdd1243dSDimitry Andric       // Stop now if we find an unsafe operand.
2634*0fca6ea1SDimitry Andric       auto OISIt = OpSafeForPHIOfOps.find({OrigI, CacheIdx});
2635bdd1243dSDimitry Andric       if (OISIt != OpSafeForPHIOfOps.end()) {
2636bdd1243dSDimitry Andric         if (!OISIt->second) {
2637*0fca6ea1SDimitry Andric           OpSafeForPHIOfOps.insert({{I, CacheIdx}, false});
2638bdd1243dSDimitry Andric           return false;
2639bdd1243dSDimitry Andric         }
2640bdd1243dSDimitry Andric         continue;
2641bdd1243dSDimitry Andric       }
2642bdd1243dSDimitry Andric       if (!Visited.insert(Op).second)
2643bdd1243dSDimitry Andric         continue;
2644bdd1243dSDimitry Andric       Worklist.push_back(cast<Instruction>(Op));
2645bdd1243dSDimitry Andric     }
2646bdd1243dSDimitry Andric   }
2647*0fca6ea1SDimitry Andric   OpSafeForPHIOfOps.insert({{V, CacheIdx}, true});
26480b57cec5SDimitry Andric   return true;
26490b57cec5SDimitry Andric }
26500b57cec5SDimitry Andric 
26510b57cec5SDimitry Andric // Try to find a leader for instruction TransInst, which is a phi translated
26520b57cec5SDimitry Andric // version of something in our original program.  Visited is used to ensure we
26530b57cec5SDimitry Andric // don't infinite loop during translations of cycles.  OrigInst is the
26540b57cec5SDimitry Andric // instruction in the original program, and PredBB is the predecessor we
26550b57cec5SDimitry Andric // translated it through.
findLeaderForInst(Instruction * TransInst,SmallPtrSetImpl<Value * > & Visited,MemoryAccess * MemAccess,Instruction * OrigInst,BasicBlock * PredBB)26560b57cec5SDimitry Andric Value *NewGVN::findLeaderForInst(Instruction *TransInst,
26570b57cec5SDimitry Andric                                  SmallPtrSetImpl<Value *> &Visited,
26580b57cec5SDimitry Andric                                  MemoryAccess *MemAccess, Instruction *OrigInst,
26590b57cec5SDimitry Andric                                  BasicBlock *PredBB) {
26600b57cec5SDimitry Andric   unsigned IDFSNum = InstrToDFSNum(OrigInst);
26610b57cec5SDimitry Andric   // Make sure it's marked as a temporary instruction.
26620b57cec5SDimitry Andric   AllTempInstructions.insert(TransInst);
26630b57cec5SDimitry Andric   // and make sure anything that tries to add it's DFS number is
26640b57cec5SDimitry Andric   // redirected to the instruction we are making a phi of ops
26650b57cec5SDimitry Andric   // for.
26660b57cec5SDimitry Andric   TempToBlock.insert({TransInst, PredBB});
26670b57cec5SDimitry Andric   InstrDFS.insert({TransInst, IDFSNum});
26680b57cec5SDimitry Andric 
2669fe6060f1SDimitry Andric   auto Res = performSymbolicEvaluation(TransInst, Visited);
2670fe6060f1SDimitry Andric   const Expression *E = Res.Expr;
2671fe6060f1SDimitry Andric   addAdditionalUsers(Res, OrigInst);
26720b57cec5SDimitry Andric   InstrDFS.erase(TransInst);
26730b57cec5SDimitry Andric   AllTempInstructions.erase(TransInst);
26740b57cec5SDimitry Andric   TempToBlock.erase(TransInst);
26750b57cec5SDimitry Andric   if (MemAccess)
26760b57cec5SDimitry Andric     TempToMemory.erase(TransInst);
26770b57cec5SDimitry Andric   if (!E)
26780b57cec5SDimitry Andric     return nullptr;
26790b57cec5SDimitry Andric   auto *FoundVal = findPHIOfOpsLeader(E, OrigInst, PredBB);
26800b57cec5SDimitry Andric   if (!FoundVal) {
26810b57cec5SDimitry Andric     ExpressionToPhiOfOps[E].insert(OrigInst);
26820b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Cannot find phi of ops operand for " << *TransInst
26830b57cec5SDimitry Andric                       << " in block " << getBlockName(PredBB) << "\n");
26840b57cec5SDimitry Andric     return nullptr;
26850b57cec5SDimitry Andric   }
26860b57cec5SDimitry Andric   if (auto *SI = dyn_cast<StoreInst>(FoundVal))
26870b57cec5SDimitry Andric     FoundVal = SI->getValueOperand();
26880b57cec5SDimitry Andric   return FoundVal;
26890b57cec5SDimitry Andric }
26900b57cec5SDimitry Andric 
26910b57cec5SDimitry Andric // When we see an instruction that is an op of phis, generate the equivalent phi
26920b57cec5SDimitry Andric // of ops form.
26930b57cec5SDimitry Andric const Expression *
makePossiblePHIOfOps(Instruction * I,SmallPtrSetImpl<Value * > & Visited)26940b57cec5SDimitry Andric NewGVN::makePossiblePHIOfOps(Instruction *I,
26950b57cec5SDimitry Andric                              SmallPtrSetImpl<Value *> &Visited) {
26960b57cec5SDimitry Andric   if (!okayForPHIOfOps(I))
26970b57cec5SDimitry Andric     return nullptr;
26980b57cec5SDimitry Andric 
26990b57cec5SDimitry Andric   if (!Visited.insert(I).second)
27000b57cec5SDimitry Andric     return nullptr;
27010b57cec5SDimitry Andric   // For now, we require the instruction be cycle free because we don't
27020b57cec5SDimitry Andric   // *always* create a phi of ops for instructions that could be done as phi
27030b57cec5SDimitry Andric   // of ops, we only do it if we think it is useful.  If we did do it all the
27040b57cec5SDimitry Andric   // time, we could remove the cycle free check.
27050b57cec5SDimitry Andric   if (!isCycleFree(I))
27060b57cec5SDimitry Andric     return nullptr;
27070b57cec5SDimitry Andric 
27080b57cec5SDimitry Andric   SmallPtrSet<const Value *, 8> ProcessedPHIs;
27090b57cec5SDimitry Andric   // TODO: We don't do phi translation on memory accesses because it's
27100b57cec5SDimitry Andric   // complicated. For a load, we'd need to be able to simulate a new memoryuse,
27110b57cec5SDimitry Andric   // which we don't have a good way of doing ATM.
27120b57cec5SDimitry Andric   auto *MemAccess = getMemoryAccess(I);
27130b57cec5SDimitry Andric   // If the memory operation is defined by a memory operation this block that
27140b57cec5SDimitry Andric   // isn't a MemoryPhi, transforming the pointer backwards through a scalar phi
27150b57cec5SDimitry Andric   // can't help, as it would still be killed by that memory operation.
27160b57cec5SDimitry Andric   if (MemAccess && !isa<MemoryPhi>(MemAccess->getDefiningAccess()) &&
27170b57cec5SDimitry Andric       MemAccess->getDefiningAccess()->getBlock() == I->getParent())
27180b57cec5SDimitry Andric     return nullptr;
27190b57cec5SDimitry Andric 
27200b57cec5SDimitry Andric   // Convert op of phis to phi of ops
27210b57cec5SDimitry Andric   SmallPtrSet<const Value *, 10> VisitedOps;
27220b57cec5SDimitry Andric   SmallVector<Value *, 4> Ops(I->operand_values());
27230b57cec5SDimitry Andric   BasicBlock *SamePHIBlock = nullptr;
27240b57cec5SDimitry Andric   PHINode *OpPHI = nullptr;
27250b57cec5SDimitry Andric   if (!DebugCounter::shouldExecute(PHIOfOpsCounter))
27260b57cec5SDimitry Andric     return nullptr;
27270b57cec5SDimitry Andric   for (auto *Op : Ops) {
27280b57cec5SDimitry Andric     if (!isa<PHINode>(Op)) {
27290b57cec5SDimitry Andric       auto *ValuePHI = RealToTemp.lookup(Op);
27300b57cec5SDimitry Andric       if (!ValuePHI)
27310b57cec5SDimitry Andric         continue;
27320b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Found possible dependent phi of ops\n");
27330b57cec5SDimitry Andric       Op = ValuePHI;
27340b57cec5SDimitry Andric     }
27350b57cec5SDimitry Andric     OpPHI = cast<PHINode>(Op);
27360b57cec5SDimitry Andric     if (!SamePHIBlock) {
27370b57cec5SDimitry Andric       SamePHIBlock = getBlockForValue(OpPHI);
27380b57cec5SDimitry Andric     } else if (SamePHIBlock != getBlockForValue(OpPHI)) {
27390b57cec5SDimitry Andric       LLVM_DEBUG(
27400b57cec5SDimitry Andric           dbgs()
27410b57cec5SDimitry Andric           << "PHIs for operands are not all in the same block, aborting\n");
27420b57cec5SDimitry Andric       return nullptr;
27430b57cec5SDimitry Andric     }
27440b57cec5SDimitry Andric     // No point in doing this for one-operand phis.
274506c3fb27SDimitry Andric     // Since all PHIs for operands must be in the same block, then they must
274606c3fb27SDimitry Andric     // have the same number of operands so we can just abort.
274706c3fb27SDimitry Andric     if (OpPHI->getNumOperands() == 1)
274806c3fb27SDimitry Andric       return nullptr;
27490b57cec5SDimitry Andric   }
27500b57cec5SDimitry Andric 
27510b57cec5SDimitry Andric   if (!OpPHI)
27520b57cec5SDimitry Andric     return nullptr;
27530b57cec5SDimitry Andric 
27540b57cec5SDimitry Andric   SmallVector<ValPair, 4> PHIOps;
27550b57cec5SDimitry Andric   SmallPtrSet<Value *, 4> Deps;
27560b57cec5SDimitry Andric   auto *PHIBlock = getBlockForValue(OpPHI);
27570b57cec5SDimitry Andric   RevisitOnReachabilityChange[PHIBlock].reset(InstrToDFSNum(I));
27580b57cec5SDimitry Andric   for (unsigned PredNum = 0; PredNum < OpPHI->getNumOperands(); ++PredNum) {
27590b57cec5SDimitry Andric     auto *PredBB = OpPHI->getIncomingBlock(PredNum);
27600b57cec5SDimitry Andric     Value *FoundVal = nullptr;
27610b57cec5SDimitry Andric     SmallPtrSet<Value *, 4> CurrentDeps;
27620b57cec5SDimitry Andric     // We could just skip unreachable edges entirely but it's tricky to do
27630b57cec5SDimitry Andric     // with rewriting existing phi nodes.
27640b57cec5SDimitry Andric     if (ReachableEdges.count({PredBB, PHIBlock})) {
27650b57cec5SDimitry Andric       // Clone the instruction, create an expression from it that is
27660b57cec5SDimitry Andric       // translated back into the predecessor, and see if we have a leader.
27670b57cec5SDimitry Andric       Instruction *ValueOp = I->clone();
27685f757f3fSDimitry Andric       // Emit the temporal instruction in the predecessor basic block where the
27695f757f3fSDimitry Andric       // corresponding value is defined.
27705f757f3fSDimitry Andric       ValueOp->insertBefore(PredBB->getTerminator());
27710b57cec5SDimitry Andric       if (MemAccess)
27720b57cec5SDimitry Andric         TempToMemory.insert({ValueOp, MemAccess});
27730b57cec5SDimitry Andric       bool SafeForPHIOfOps = true;
27740b57cec5SDimitry Andric       VisitedOps.clear();
27750b57cec5SDimitry Andric       for (auto &Op : ValueOp->operands()) {
27760b57cec5SDimitry Andric         auto *OrigOp = &*Op;
27770b57cec5SDimitry Andric         // When these operand changes, it could change whether there is a
27780b57cec5SDimitry Andric         // leader for us or not, so we have to add additional users.
27790b57cec5SDimitry Andric         if (isa<PHINode>(Op)) {
27800b57cec5SDimitry Andric           Op = Op->DoPHITranslation(PHIBlock, PredBB);
27810b57cec5SDimitry Andric           if (Op != OrigOp && Op != I)
27820b57cec5SDimitry Andric             CurrentDeps.insert(Op);
27830b57cec5SDimitry Andric         } else if (auto *ValuePHI = RealToTemp.lookup(Op)) {
27840b57cec5SDimitry Andric           if (getBlockForValue(ValuePHI) == PHIBlock)
27850b57cec5SDimitry Andric             Op = ValuePHI->getIncomingValueForBlock(PredBB);
27860b57cec5SDimitry Andric         }
27870b57cec5SDimitry Andric         // If we phi-translated the op, it must be safe.
27880b57cec5SDimitry Andric         SafeForPHIOfOps =
27890b57cec5SDimitry Andric             SafeForPHIOfOps &&
27900b57cec5SDimitry Andric             (Op != OrigOp || OpIsSafeForPHIOfOps(Op, PHIBlock, VisitedOps));
27910b57cec5SDimitry Andric       }
27920b57cec5SDimitry Andric       // FIXME: For those things that are not safe we could generate
27930b57cec5SDimitry Andric       // expressions all the way down, and see if this comes out to a
27940b57cec5SDimitry Andric       // constant.  For anything where that is true, and unsafe, we should
27950b57cec5SDimitry Andric       // have made a phi-of-ops (or value numbered it equivalent to something)
27960b57cec5SDimitry Andric       // for the pieces already.
27970b57cec5SDimitry Andric       FoundVal = !SafeForPHIOfOps ? nullptr
27980b57cec5SDimitry Andric                                   : findLeaderForInst(ValueOp, Visited,
27990b57cec5SDimitry Andric                                                       MemAccess, I, PredBB);
28005f757f3fSDimitry Andric       ValueOp->eraseFromParent();
28010b57cec5SDimitry Andric       if (!FoundVal) {
28020b57cec5SDimitry Andric         // We failed to find a leader for the current ValueOp, but this might
28030b57cec5SDimitry Andric         // change in case of the translated operands change.
28040b57cec5SDimitry Andric         if (SafeForPHIOfOps)
2805bdd1243dSDimitry Andric           for (auto *Dep : CurrentDeps)
28060b57cec5SDimitry Andric             addAdditionalUsers(Dep, I);
28070b57cec5SDimitry Andric 
28080b57cec5SDimitry Andric         return nullptr;
28090b57cec5SDimitry Andric       }
28100b57cec5SDimitry Andric       Deps.insert(CurrentDeps.begin(), CurrentDeps.end());
28110b57cec5SDimitry Andric     } else {
28120b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Skipping phi of ops operand for incoming block "
28130b57cec5SDimitry Andric                         << getBlockName(PredBB)
28140b57cec5SDimitry Andric                         << " because the block is unreachable\n");
281504eeddc0SDimitry Andric       FoundVal = PoisonValue::get(I->getType());
28160b57cec5SDimitry Andric       RevisitOnReachabilityChange[PHIBlock].set(InstrToDFSNum(I));
28170b57cec5SDimitry Andric     }
28180b57cec5SDimitry Andric 
28190b57cec5SDimitry Andric     PHIOps.push_back({FoundVal, PredBB});
28200b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Found phi of ops operand " << *FoundVal << " in "
28210b57cec5SDimitry Andric                       << getBlockName(PredBB) << "\n");
28220b57cec5SDimitry Andric   }
2823bdd1243dSDimitry Andric   for (auto *Dep : Deps)
28240b57cec5SDimitry Andric     addAdditionalUsers(Dep, I);
28250b57cec5SDimitry Andric   sortPHIOps(PHIOps);
28260b57cec5SDimitry Andric   auto *E = performSymbolicPHIEvaluation(PHIOps, I, PHIBlock);
28270b57cec5SDimitry Andric   if (isa<ConstantExpression>(E) || isa<VariableExpression>(E)) {
28280b57cec5SDimitry Andric     LLVM_DEBUG(
28290b57cec5SDimitry Andric         dbgs()
28300b57cec5SDimitry Andric         << "Not creating real PHI of ops because it simplified to existing "
28310b57cec5SDimitry Andric            "value or constant\n");
2832fe6060f1SDimitry Andric     // We have leaders for all operands, but do not create a real PHI node with
2833fe6060f1SDimitry Andric     // those leaders as operands, so the link between the operands and the
2834fe6060f1SDimitry Andric     // PHI-of-ops is not materialized in the IR. If any of those leaders
2835fe6060f1SDimitry Andric     // changes, the PHI-of-op may change also, so we need to add the operands as
2836fe6060f1SDimitry Andric     // additional users.
2837fe6060f1SDimitry Andric     for (auto &O : PHIOps)
2838fe6060f1SDimitry Andric       addAdditionalUsers(O.first, I);
2839fe6060f1SDimitry Andric 
28400b57cec5SDimitry Andric     return E;
28410b57cec5SDimitry Andric   }
28420b57cec5SDimitry Andric   auto *ValuePHI = RealToTemp.lookup(I);
28430b57cec5SDimitry Andric   bool NewPHI = false;
28440b57cec5SDimitry Andric   if (!ValuePHI) {
28450b57cec5SDimitry Andric     ValuePHI =
28460b57cec5SDimitry Andric         PHINode::Create(I->getType(), OpPHI->getNumOperands(), "phiofops");
28470b57cec5SDimitry Andric     addPhiOfOps(ValuePHI, PHIBlock, I);
28480b57cec5SDimitry Andric     NewPHI = true;
28490b57cec5SDimitry Andric     NumGVNPHIOfOpsCreated++;
28500b57cec5SDimitry Andric   }
28510b57cec5SDimitry Andric   if (NewPHI) {
28520b57cec5SDimitry Andric     for (auto PHIOp : PHIOps)
28530b57cec5SDimitry Andric       ValuePHI->addIncoming(PHIOp.first, PHIOp.second);
28540b57cec5SDimitry Andric   } else {
28550b57cec5SDimitry Andric     TempToBlock[ValuePHI] = PHIBlock;
28560b57cec5SDimitry Andric     unsigned int i = 0;
28570b57cec5SDimitry Andric     for (auto PHIOp : PHIOps) {
28580b57cec5SDimitry Andric       ValuePHI->setIncomingValue(i, PHIOp.first);
28590b57cec5SDimitry Andric       ValuePHI->setIncomingBlock(i, PHIOp.second);
28600b57cec5SDimitry Andric       ++i;
28610b57cec5SDimitry Andric     }
28620b57cec5SDimitry Andric   }
28630b57cec5SDimitry Andric   RevisitOnReachabilityChange[PHIBlock].set(InstrToDFSNum(I));
28640b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Created phi of ops " << *ValuePHI << " for " << *I
28650b57cec5SDimitry Andric                     << "\n");
28660b57cec5SDimitry Andric 
28670b57cec5SDimitry Andric   return E;
28680b57cec5SDimitry Andric }
28690b57cec5SDimitry Andric 
28700b57cec5SDimitry Andric // The algorithm initially places the values of the routine in the TOP
28711fd87a68SDimitry Andric // congruence class. The leader of TOP is the undetermined value `poison`.
28720b57cec5SDimitry Andric // When the algorithm has finished, values still in TOP are unreachable.
initializeCongruenceClasses(Function & F)28730b57cec5SDimitry Andric void NewGVN::initializeCongruenceClasses(Function &F) {
28740b57cec5SDimitry Andric   NextCongruenceNum = 0;
28750b57cec5SDimitry Andric 
28760b57cec5SDimitry Andric   // Note that even though we use the live on entry def as a representative
28770b57cec5SDimitry Andric   // MemoryAccess, it is *not* the same as the actual live on entry def. We
28781fd87a68SDimitry Andric   // have no real equivalent to poison for MemoryAccesses, and so we really
28790b57cec5SDimitry Andric   // should be checking whether the MemoryAccess is top if we want to know if it
28800b57cec5SDimitry Andric   // is equivalent to everything.  Otherwise, what this really signifies is that
28810b57cec5SDimitry Andric   // the access "it reaches all the way back to the beginning of the function"
28820b57cec5SDimitry Andric 
28830b57cec5SDimitry Andric   // Initialize all other instructions to be in TOP class.
28840b57cec5SDimitry Andric   TOPClass = createCongruenceClass(nullptr, nullptr);
28850b57cec5SDimitry Andric   TOPClass->setMemoryLeader(MSSA->getLiveOnEntryDef());
28860b57cec5SDimitry Andric   //  The live on entry def gets put into it's own class
28870b57cec5SDimitry Andric   MemoryAccessToClass[MSSA->getLiveOnEntryDef()] =
28880b57cec5SDimitry Andric       createMemoryClass(MSSA->getLiveOnEntryDef());
28890b57cec5SDimitry Andric 
2890bdd1243dSDimitry Andric   for (auto *DTN : nodes(DT)) {
28910b57cec5SDimitry Andric     BasicBlock *BB = DTN->getBlock();
28920b57cec5SDimitry Andric     // All MemoryAccesses are equivalent to live on entry to start. They must
28930b57cec5SDimitry Andric     // be initialized to something so that initial changes are noticed. For
28940b57cec5SDimitry Andric     // the maximal answer, we initialize them all to be the same as
28950b57cec5SDimitry Andric     // liveOnEntry.
28960b57cec5SDimitry Andric     auto *MemoryBlockDefs = MSSA->getBlockDefs(BB);
28970b57cec5SDimitry Andric     if (MemoryBlockDefs)
28980b57cec5SDimitry Andric       for (const auto &Def : *MemoryBlockDefs) {
28990b57cec5SDimitry Andric         MemoryAccessToClass[&Def] = TOPClass;
29000b57cec5SDimitry Andric         auto *MD = dyn_cast<MemoryDef>(&Def);
29010b57cec5SDimitry Andric         // Insert the memory phis into the member list.
29020b57cec5SDimitry Andric         if (!MD) {
29030b57cec5SDimitry Andric           const MemoryPhi *MP = cast<MemoryPhi>(&Def);
29040b57cec5SDimitry Andric           TOPClass->memory_insert(MP);
29050b57cec5SDimitry Andric           MemoryPhiState.insert({MP, MPS_TOP});
29060b57cec5SDimitry Andric         }
29070b57cec5SDimitry Andric 
29080b57cec5SDimitry Andric         if (MD && isa<StoreInst>(MD->getMemoryInst()))
29090b57cec5SDimitry Andric           TOPClass->incStoreCount();
29100b57cec5SDimitry Andric       }
29110b57cec5SDimitry Andric 
29120b57cec5SDimitry Andric     // FIXME: This is trying to discover which instructions are uses of phi
29130b57cec5SDimitry Andric     // nodes.  We should move this into one of the myriad of places that walk
29140b57cec5SDimitry Andric     // all the operands already.
29150b57cec5SDimitry Andric     for (auto &I : *BB) {
29160b57cec5SDimitry Andric       if (isa<PHINode>(&I))
29170b57cec5SDimitry Andric         for (auto *U : I.users())
29180b57cec5SDimitry Andric           if (auto *UInst = dyn_cast<Instruction>(U))
29190b57cec5SDimitry Andric             if (InstrToDFSNum(UInst) != 0 && okayForPHIOfOps(UInst))
29200b57cec5SDimitry Andric               PHINodeUses.insert(UInst);
29210b57cec5SDimitry Andric       // Don't insert void terminators into the class. We don't value number
29220b57cec5SDimitry Andric       // them, and they just end up sitting in TOP.
29230b57cec5SDimitry Andric       if (I.isTerminator() && I.getType()->isVoidTy())
29240b57cec5SDimitry Andric         continue;
29250b57cec5SDimitry Andric       TOPClass->insert(&I);
29260b57cec5SDimitry Andric       ValueToClass[&I] = TOPClass;
29270b57cec5SDimitry Andric     }
29280b57cec5SDimitry Andric   }
29290b57cec5SDimitry Andric 
29300b57cec5SDimitry Andric   // Initialize arguments to be in their own unique congruence classes
29310b57cec5SDimitry Andric   for (auto &FA : F.args())
29320b57cec5SDimitry Andric     createSingletonCongruenceClass(&FA);
29330b57cec5SDimitry Andric }
29340b57cec5SDimitry Andric 
cleanupTables()29350b57cec5SDimitry Andric void NewGVN::cleanupTables() {
2936bdd1243dSDimitry Andric   for (CongruenceClass *&CC : CongruenceClasses) {
2937bdd1243dSDimitry Andric     LLVM_DEBUG(dbgs() << "Congruence class " << CC->getID() << " has "
2938bdd1243dSDimitry Andric                       << CC->size() << " members\n");
29390b57cec5SDimitry Andric     // Make sure we delete the congruence class (probably worth switching to
29400b57cec5SDimitry Andric     // a unique_ptr at some point.
2941bdd1243dSDimitry Andric     delete CC;
2942bdd1243dSDimitry Andric     CC = nullptr;
29430b57cec5SDimitry Andric   }
29440b57cec5SDimitry Andric 
29450b57cec5SDimitry Andric   // Destroy the value expressions
29460b57cec5SDimitry Andric   SmallVector<Instruction *, 8> TempInst(AllTempInstructions.begin(),
29470b57cec5SDimitry Andric                                          AllTempInstructions.end());
29480b57cec5SDimitry Andric   AllTempInstructions.clear();
29490b57cec5SDimitry Andric 
29500b57cec5SDimitry Andric   // We have to drop all references for everything first, so there are no uses
29510b57cec5SDimitry Andric   // left as we delete them.
29520b57cec5SDimitry Andric   for (auto *I : TempInst) {
29530b57cec5SDimitry Andric     I->dropAllReferences();
29540b57cec5SDimitry Andric   }
29550b57cec5SDimitry Andric 
29560b57cec5SDimitry Andric   while (!TempInst.empty()) {
2957e8d8bef9SDimitry Andric     auto *I = TempInst.pop_back_val();
29580b57cec5SDimitry Andric     I->deleteValue();
29590b57cec5SDimitry Andric   }
29600b57cec5SDimitry Andric 
29610b57cec5SDimitry Andric   ValueToClass.clear();
29620b57cec5SDimitry Andric   ArgRecycler.clear(ExpressionAllocator);
29630b57cec5SDimitry Andric   ExpressionAllocator.Reset();
29640b57cec5SDimitry Andric   CongruenceClasses.clear();
29650b57cec5SDimitry Andric   ExpressionToClass.clear();
29660b57cec5SDimitry Andric   ValueToExpression.clear();
29670b57cec5SDimitry Andric   RealToTemp.clear();
29680b57cec5SDimitry Andric   AdditionalUsers.clear();
29690b57cec5SDimitry Andric   ExpressionToPhiOfOps.clear();
29700b57cec5SDimitry Andric   TempToBlock.clear();
29710b57cec5SDimitry Andric   TempToMemory.clear();
29720b57cec5SDimitry Andric   PHINodeUses.clear();
29730b57cec5SDimitry Andric   OpSafeForPHIOfOps.clear();
29740b57cec5SDimitry Andric   ReachableBlocks.clear();
29750b57cec5SDimitry Andric   ReachableEdges.clear();
29760b57cec5SDimitry Andric #ifndef NDEBUG
29770b57cec5SDimitry Andric   ProcessedCount.clear();
29780b57cec5SDimitry Andric #endif
29790b57cec5SDimitry Andric   InstrDFS.clear();
29800b57cec5SDimitry Andric   InstructionsToErase.clear();
29810b57cec5SDimitry Andric   DFSToInstr.clear();
29820b57cec5SDimitry Andric   BlockInstRange.clear();
29830b57cec5SDimitry Andric   TouchedInstructions.clear();
29840b57cec5SDimitry Andric   MemoryAccessToClass.clear();
29850b57cec5SDimitry Andric   PredicateToUsers.clear();
29860b57cec5SDimitry Andric   MemoryToUsers.clear();
29870b57cec5SDimitry Andric   RevisitOnReachabilityChange.clear();
29880eae32dcSDimitry Andric   IntrinsicInstPred.clear();
29890b57cec5SDimitry Andric }
29900b57cec5SDimitry Andric 
29910b57cec5SDimitry Andric // Assign local DFS number mapping to instructions, and leave space for Value
29920b57cec5SDimitry Andric // PHI's.
assignDFSNumbers(BasicBlock * B,unsigned Start)29930b57cec5SDimitry Andric std::pair<unsigned, unsigned> NewGVN::assignDFSNumbers(BasicBlock *B,
29940b57cec5SDimitry Andric                                                        unsigned Start) {
29950b57cec5SDimitry Andric   unsigned End = Start;
29960b57cec5SDimitry Andric   if (MemoryAccess *MemPhi = getMemoryAccess(B)) {
29970b57cec5SDimitry Andric     InstrDFS[MemPhi] = End++;
29980b57cec5SDimitry Andric     DFSToInstr.emplace_back(MemPhi);
29990b57cec5SDimitry Andric   }
30000b57cec5SDimitry Andric 
30010b57cec5SDimitry Andric   // Then the real block goes next.
30020b57cec5SDimitry Andric   for (auto &I : *B) {
30030b57cec5SDimitry Andric     // There's no need to call isInstructionTriviallyDead more than once on
30040b57cec5SDimitry Andric     // an instruction. Therefore, once we know that an instruction is dead
30050b57cec5SDimitry Andric     // we change its DFS number so that it doesn't get value numbered.
30060b57cec5SDimitry Andric     if (isInstructionTriviallyDead(&I, TLI)) {
30070b57cec5SDimitry Andric       InstrDFS[&I] = 0;
30080b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Skipping trivially dead instruction " << I << "\n");
30090b57cec5SDimitry Andric       markInstructionForDeletion(&I);
30100b57cec5SDimitry Andric       continue;
30110b57cec5SDimitry Andric     }
30120b57cec5SDimitry Andric     if (isa<PHINode>(&I))
30130b57cec5SDimitry Andric       RevisitOnReachabilityChange[B].set(End);
30140b57cec5SDimitry Andric     InstrDFS[&I] = End++;
30150b57cec5SDimitry Andric     DFSToInstr.emplace_back(&I);
30160b57cec5SDimitry Andric   }
30170b57cec5SDimitry Andric 
30180b57cec5SDimitry Andric   // All of the range functions taken half-open ranges (open on the end side).
30190b57cec5SDimitry Andric   // So we do not subtract one from count, because at this point it is one
30200b57cec5SDimitry Andric   // greater than the last instruction.
30210b57cec5SDimitry Andric   return std::make_pair(Start, End);
30220b57cec5SDimitry Andric }
30230b57cec5SDimitry Andric 
updateProcessedCount(const Value * V)30240b57cec5SDimitry Andric void NewGVN::updateProcessedCount(const Value *V) {
30250b57cec5SDimitry Andric #ifndef NDEBUG
30260b57cec5SDimitry Andric   if (ProcessedCount.count(V) == 0) {
30270b57cec5SDimitry Andric     ProcessedCount.insert({V, 1});
30280b57cec5SDimitry Andric   } else {
30290b57cec5SDimitry Andric     ++ProcessedCount[V];
30300b57cec5SDimitry Andric     assert(ProcessedCount[V] < 100 &&
30310b57cec5SDimitry Andric            "Seem to have processed the same Value a lot");
30320b57cec5SDimitry Andric   }
30330b57cec5SDimitry Andric #endif
30340b57cec5SDimitry Andric }
30350b57cec5SDimitry Andric 
30360b57cec5SDimitry Andric // Evaluate MemoryPhi nodes symbolically, just like PHI nodes
valueNumberMemoryPhi(MemoryPhi * MP)30370b57cec5SDimitry Andric void NewGVN::valueNumberMemoryPhi(MemoryPhi *MP) {
30380b57cec5SDimitry Andric   // If all the arguments are the same, the MemoryPhi has the same value as the
30390b57cec5SDimitry Andric   // argument.  Filter out unreachable blocks and self phis from our operands.
30400b57cec5SDimitry Andric   // TODO: We could do cycle-checking on the memory phis to allow valueizing for
30410b57cec5SDimitry Andric   // self-phi checking.
30420b57cec5SDimitry Andric   const BasicBlock *PHIBlock = MP->getBlock();
30430b57cec5SDimitry Andric   auto Filtered = make_filter_range(MP->operands(), [&](const Use &U) {
30440b57cec5SDimitry Andric     return cast<MemoryAccess>(U) != MP &&
30450b57cec5SDimitry Andric            !isMemoryAccessTOP(cast<MemoryAccess>(U)) &&
30460b57cec5SDimitry Andric            ReachableEdges.count({MP->getIncomingBlock(U), PHIBlock});
30470b57cec5SDimitry Andric   });
30481fd87a68SDimitry Andric   // If all that is left is nothing, our memoryphi is poison. We keep it as
30490b57cec5SDimitry Andric   // InitialClass.  Note: The only case this should happen is if we have at
30500b57cec5SDimitry Andric   // least one self-argument.
30510b57cec5SDimitry Andric   if (Filtered.begin() == Filtered.end()) {
30520b57cec5SDimitry Andric     if (setMemoryClass(MP, TOPClass))
30530b57cec5SDimitry Andric       markMemoryUsersTouched(MP);
30540b57cec5SDimitry Andric     return;
30550b57cec5SDimitry Andric   }
30560b57cec5SDimitry Andric 
30570b57cec5SDimitry Andric   // Transform the remaining operands into operand leaders.
30580b57cec5SDimitry Andric   // FIXME: mapped_iterator should have a range version.
30590b57cec5SDimitry Andric   auto LookupFunc = [&](const Use &U) {
30600b57cec5SDimitry Andric     return lookupMemoryLeader(cast<MemoryAccess>(U));
30610b57cec5SDimitry Andric   };
30620b57cec5SDimitry Andric   auto MappedBegin = map_iterator(Filtered.begin(), LookupFunc);
30630b57cec5SDimitry Andric   auto MappedEnd = map_iterator(Filtered.end(), LookupFunc);
30640b57cec5SDimitry Andric 
30650b57cec5SDimitry Andric   // and now check if all the elements are equal.
30660b57cec5SDimitry Andric   // Sadly, we can't use std::equals since these are random access iterators.
30670b57cec5SDimitry Andric   const auto *AllSameValue = *MappedBegin;
30680b57cec5SDimitry Andric   ++MappedBegin;
30690b57cec5SDimitry Andric   bool AllEqual = std::all_of(
30700b57cec5SDimitry Andric       MappedBegin, MappedEnd,
30710b57cec5SDimitry Andric       [&AllSameValue](const MemoryAccess *V) { return V == AllSameValue; });
30720b57cec5SDimitry Andric 
30730b57cec5SDimitry Andric   if (AllEqual)
30740b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Memory Phi value numbered to " << *AllSameValue
30750b57cec5SDimitry Andric                       << "\n");
30760b57cec5SDimitry Andric   else
30770b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Memory Phi value numbered to itself\n");
30780b57cec5SDimitry Andric   // If it's equal to something, it's in that class. Otherwise, it has to be in
30790b57cec5SDimitry Andric   // a class where it is the leader (other things may be equivalent to it, but
30800b57cec5SDimitry Andric   // it needs to start off in its own class, which means it must have been the
30810b57cec5SDimitry Andric   // leader, and it can't have stopped being the leader because it was never
30820b57cec5SDimitry Andric   // removed).
30830b57cec5SDimitry Andric   CongruenceClass *CC =
30840b57cec5SDimitry Andric       AllEqual ? getMemoryClass(AllSameValue) : ensureLeaderOfMemoryClass(MP);
30850b57cec5SDimitry Andric   auto OldState = MemoryPhiState.lookup(MP);
30860b57cec5SDimitry Andric   assert(OldState != MPS_Invalid && "Invalid memory phi state");
30870b57cec5SDimitry Andric   auto NewState = AllEqual ? MPS_Equivalent : MPS_Unique;
30880b57cec5SDimitry Andric   MemoryPhiState[MP] = NewState;
30890b57cec5SDimitry Andric   if (setMemoryClass(MP, CC) || OldState != NewState)
30900b57cec5SDimitry Andric     markMemoryUsersTouched(MP);
30910b57cec5SDimitry Andric }
30920b57cec5SDimitry Andric 
30930b57cec5SDimitry Andric // Value number a single instruction, symbolically evaluating, performing
30940b57cec5SDimitry Andric // congruence finding, and updating mappings.
valueNumberInstruction(Instruction * I)30950b57cec5SDimitry Andric void NewGVN::valueNumberInstruction(Instruction *I) {
30960b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Processing instruction " << *I << "\n");
30970b57cec5SDimitry Andric   if (!I->isTerminator()) {
30980b57cec5SDimitry Andric     const Expression *Symbolized = nullptr;
30990b57cec5SDimitry Andric     SmallPtrSet<Value *, 2> Visited;
31000b57cec5SDimitry Andric     if (DebugCounter::shouldExecute(VNCounter)) {
3101fe6060f1SDimitry Andric       auto Res = performSymbolicEvaluation(I, Visited);
3102fe6060f1SDimitry Andric       Symbolized = Res.Expr;
3103fe6060f1SDimitry Andric       addAdditionalUsers(Res, I);
3104fe6060f1SDimitry Andric 
31050b57cec5SDimitry Andric       // Make a phi of ops if necessary
31060b57cec5SDimitry Andric       if (Symbolized && !isa<ConstantExpression>(Symbolized) &&
31070b57cec5SDimitry Andric           !isa<VariableExpression>(Symbolized) && PHINodeUses.count(I)) {
31080b57cec5SDimitry Andric         auto *PHIE = makePossiblePHIOfOps(I, Visited);
31090b57cec5SDimitry Andric         // If we created a phi of ops, use it.
31100b57cec5SDimitry Andric         // If we couldn't create one, make sure we don't leave one lying around
31110b57cec5SDimitry Andric         if (PHIE) {
31120b57cec5SDimitry Andric           Symbolized = PHIE;
31130b57cec5SDimitry Andric         } else if (auto *Op = RealToTemp.lookup(I)) {
31140b57cec5SDimitry Andric           removePhiOfOps(I, Op);
31150b57cec5SDimitry Andric         }
31160b57cec5SDimitry Andric       }
31170b57cec5SDimitry Andric     } else {
31180b57cec5SDimitry Andric       // Mark the instruction as unused so we don't value number it again.
31190b57cec5SDimitry Andric       InstrDFS[I] = 0;
31200b57cec5SDimitry Andric     }
31210b57cec5SDimitry Andric     // If we couldn't come up with a symbolic expression, use the unknown
31220b57cec5SDimitry Andric     // expression
31230b57cec5SDimitry Andric     if (Symbolized == nullptr)
31240b57cec5SDimitry Andric       Symbolized = createUnknownExpression(I);
31250b57cec5SDimitry Andric     performCongruenceFinding(I, Symbolized);
31260b57cec5SDimitry Andric   } else {
31270b57cec5SDimitry Andric     // Handle terminators that return values. All of them produce values we
31280b57cec5SDimitry Andric     // don't currently understand.  We don't place non-value producing
31290b57cec5SDimitry Andric     // terminators in a class.
31300b57cec5SDimitry Andric     if (!I->getType()->isVoidTy()) {
31310b57cec5SDimitry Andric       auto *Symbolized = createUnknownExpression(I);
31320b57cec5SDimitry Andric       performCongruenceFinding(I, Symbolized);
31330b57cec5SDimitry Andric     }
31340b57cec5SDimitry Andric     processOutgoingEdges(I, I->getParent());
31350b57cec5SDimitry Andric   }
31360b57cec5SDimitry Andric }
31370b57cec5SDimitry Andric 
31380b57cec5SDimitry Andric // Check if there is a path, using single or equal argument phi nodes, from
31390b57cec5SDimitry Andric // First to Second.
singleReachablePHIPath(SmallPtrSet<const MemoryAccess *,8> & Visited,const MemoryAccess * First,const MemoryAccess * Second) const31400b57cec5SDimitry Andric bool NewGVN::singleReachablePHIPath(
31410b57cec5SDimitry Andric     SmallPtrSet<const MemoryAccess *, 8> &Visited, const MemoryAccess *First,
31420b57cec5SDimitry Andric     const MemoryAccess *Second) const {
31430b57cec5SDimitry Andric   if (First == Second)
31440b57cec5SDimitry Andric     return true;
31450b57cec5SDimitry Andric   if (MSSA->isLiveOnEntryDef(First))
31460b57cec5SDimitry Andric     return false;
31470b57cec5SDimitry Andric 
31480b57cec5SDimitry Andric   // This is not perfect, but as we're just verifying here, we can live with
31490b57cec5SDimitry Andric   // the loss of precision. The real solution would be that of doing strongly
31500b57cec5SDimitry Andric   // connected component finding in this routine, and it's probably not worth
31510b57cec5SDimitry Andric   // the complexity for the time being. So, we just keep a set of visited
31520b57cec5SDimitry Andric   // MemoryAccess and return true when we hit a cycle.
315381ad6265SDimitry Andric   if (!Visited.insert(First).second)
31540b57cec5SDimitry Andric     return true;
31550b57cec5SDimitry Andric 
31560b57cec5SDimitry Andric   const auto *EndDef = First;
3157bdd1243dSDimitry Andric   for (const auto *ChainDef : optimized_def_chain(First)) {
31580b57cec5SDimitry Andric     if (ChainDef == Second)
31590b57cec5SDimitry Andric       return true;
31600b57cec5SDimitry Andric     if (MSSA->isLiveOnEntryDef(ChainDef))
31610b57cec5SDimitry Andric       return false;
31620b57cec5SDimitry Andric     EndDef = ChainDef;
31630b57cec5SDimitry Andric   }
31640b57cec5SDimitry Andric   auto *MP = cast<MemoryPhi>(EndDef);
31650b57cec5SDimitry Andric   auto ReachableOperandPred = [&](const Use &U) {
31660b57cec5SDimitry Andric     return ReachableEdges.count({MP->getIncomingBlock(U), MP->getBlock()});
31670b57cec5SDimitry Andric   };
31680b57cec5SDimitry Andric   auto FilteredPhiArgs =
31690b57cec5SDimitry Andric       make_filter_range(MP->operands(), ReachableOperandPred);
31700b57cec5SDimitry Andric   SmallVector<const Value *, 32> OperandList;
31710b57cec5SDimitry Andric   llvm::copy(FilteredPhiArgs, std::back_inserter(OperandList));
3172bdd1243dSDimitry Andric   bool Okay = all_equal(OperandList);
31730b57cec5SDimitry Andric   if (Okay)
31740b57cec5SDimitry Andric     return singleReachablePHIPath(Visited, cast<MemoryAccess>(OperandList[0]),
31750b57cec5SDimitry Andric                                   Second);
31760b57cec5SDimitry Andric   return false;
31770b57cec5SDimitry Andric }
31780b57cec5SDimitry Andric 
31790b57cec5SDimitry Andric // Verify the that the memory equivalence table makes sense relative to the
31800b57cec5SDimitry Andric // congruence classes.  Note that this checking is not perfect, and is currently
31810b57cec5SDimitry Andric // subject to very rare false negatives. It is only useful for
31820b57cec5SDimitry Andric // testing/debugging.
verifyMemoryCongruency() const31830b57cec5SDimitry Andric void NewGVN::verifyMemoryCongruency() const {
31840b57cec5SDimitry Andric #ifndef NDEBUG
31850b57cec5SDimitry Andric   // Verify that the memory table equivalence and memory member set match
31860b57cec5SDimitry Andric   for (const auto *CC : CongruenceClasses) {
31870b57cec5SDimitry Andric     if (CC == TOPClass || CC->isDead())
31880b57cec5SDimitry Andric       continue;
31890b57cec5SDimitry Andric     if (CC->getStoreCount() != 0) {
31900b57cec5SDimitry Andric       assert((CC->getStoredValue() || !isa<StoreInst>(CC->getLeader())) &&
31910b57cec5SDimitry Andric              "Any class with a store as a leader should have a "
31920b57cec5SDimitry Andric              "representative stored value");
31930b57cec5SDimitry Andric       assert(CC->getMemoryLeader() &&
31940b57cec5SDimitry Andric              "Any congruence class with a store should have a "
31950b57cec5SDimitry Andric              "representative access");
31960b57cec5SDimitry Andric     }
31970b57cec5SDimitry Andric 
31980b57cec5SDimitry Andric     if (CC->getMemoryLeader())
31990b57cec5SDimitry Andric       assert(MemoryAccessToClass.lookup(CC->getMemoryLeader()) == CC &&
32000b57cec5SDimitry Andric              "Representative MemoryAccess does not appear to be reverse "
32010b57cec5SDimitry Andric              "mapped properly");
3202bdd1243dSDimitry Andric     for (const auto *M : CC->memory())
32030b57cec5SDimitry Andric       assert(MemoryAccessToClass.lookup(M) == CC &&
32040b57cec5SDimitry Andric              "Memory member does not appear to be reverse mapped properly");
32050b57cec5SDimitry Andric   }
32060b57cec5SDimitry Andric 
32070b57cec5SDimitry Andric   // Anything equivalent in the MemoryAccess table should be in the same
32080b57cec5SDimitry Andric   // congruence class.
32090b57cec5SDimitry Andric 
32100b57cec5SDimitry Andric   // Filter out the unreachable and trivially dead entries, because they may
32110b57cec5SDimitry Andric   // never have been updated if the instructions were not processed.
32120b57cec5SDimitry Andric   auto ReachableAccessPred =
32130b57cec5SDimitry Andric       [&](const std::pair<const MemoryAccess *, CongruenceClass *> Pair) {
32140b57cec5SDimitry Andric         bool Result = ReachableBlocks.count(Pair.first->getBlock());
32150b57cec5SDimitry Andric         if (!Result || MSSA->isLiveOnEntryDef(Pair.first) ||
32160b57cec5SDimitry Andric             MemoryToDFSNum(Pair.first) == 0)
32170b57cec5SDimitry Andric           return false;
32180b57cec5SDimitry Andric         if (auto *MemDef = dyn_cast<MemoryDef>(Pair.first))
32190b57cec5SDimitry Andric           return !isInstructionTriviallyDead(MemDef->getMemoryInst());
32200b57cec5SDimitry Andric 
32210b57cec5SDimitry Andric         // We could have phi nodes which operands are all trivially dead,
32220b57cec5SDimitry Andric         // so we don't process them.
32230b57cec5SDimitry Andric         if (auto *MemPHI = dyn_cast<MemoryPhi>(Pair.first)) {
3224bdd1243dSDimitry Andric           for (const auto &U : MemPHI->incoming_values()) {
32250b57cec5SDimitry Andric             if (auto *I = dyn_cast<Instruction>(&*U)) {
32260b57cec5SDimitry Andric               if (!isInstructionTriviallyDead(I))
32270b57cec5SDimitry Andric                 return true;
32280b57cec5SDimitry Andric             }
32290b57cec5SDimitry Andric           }
32300b57cec5SDimitry Andric           return false;
32310b57cec5SDimitry Andric         }
32320b57cec5SDimitry Andric 
32330b57cec5SDimitry Andric         return true;
32340b57cec5SDimitry Andric       };
32350b57cec5SDimitry Andric 
32360b57cec5SDimitry Andric   auto Filtered = make_filter_range(MemoryAccessToClass, ReachableAccessPred);
32370b57cec5SDimitry Andric   for (auto KV : Filtered) {
32380b57cec5SDimitry Andric     if (auto *FirstMUD = dyn_cast<MemoryUseOrDef>(KV.first)) {
32390b57cec5SDimitry Andric       auto *SecondMUD = dyn_cast<MemoryUseOrDef>(KV.second->getMemoryLeader());
32400b57cec5SDimitry Andric       if (FirstMUD && SecondMUD) {
32410b57cec5SDimitry Andric         SmallPtrSet<const MemoryAccess *, 8> VisitedMAS;
32420b57cec5SDimitry Andric         assert((singleReachablePHIPath(VisitedMAS, FirstMUD, SecondMUD) ||
32430b57cec5SDimitry Andric                 ValueToClass.lookup(FirstMUD->getMemoryInst()) ==
32440b57cec5SDimitry Andric                     ValueToClass.lookup(SecondMUD->getMemoryInst())) &&
32450b57cec5SDimitry Andric                "The instructions for these memory operations should have "
32460b57cec5SDimitry Andric                "been in the same congruence class or reachable through"
32470b57cec5SDimitry Andric                "a single argument phi");
32480b57cec5SDimitry Andric       }
32490b57cec5SDimitry Andric     } else if (auto *FirstMP = dyn_cast<MemoryPhi>(KV.first)) {
32500b57cec5SDimitry Andric       // We can only sanely verify that MemoryDefs in the operand list all have
32510b57cec5SDimitry Andric       // the same class.
32520b57cec5SDimitry Andric       auto ReachableOperandPred = [&](const Use &U) {
32530b57cec5SDimitry Andric         return ReachableEdges.count(
32540b57cec5SDimitry Andric                    {FirstMP->getIncomingBlock(U), FirstMP->getBlock()}) &&
32550b57cec5SDimitry Andric                isa<MemoryDef>(U);
32560b57cec5SDimitry Andric 
32570b57cec5SDimitry Andric       };
32580b57cec5SDimitry Andric       // All arguments should in the same class, ignoring unreachable arguments
32590b57cec5SDimitry Andric       auto FilteredPhiArgs =
32600b57cec5SDimitry Andric           make_filter_range(FirstMP->operands(), ReachableOperandPred);
32610b57cec5SDimitry Andric       SmallVector<const CongruenceClass *, 16> PhiOpClasses;
32620b57cec5SDimitry Andric       std::transform(FilteredPhiArgs.begin(), FilteredPhiArgs.end(),
32630b57cec5SDimitry Andric                      std::back_inserter(PhiOpClasses), [&](const Use &U) {
32640b57cec5SDimitry Andric                        const MemoryDef *MD = cast<MemoryDef>(U);
32650b57cec5SDimitry Andric                        return ValueToClass.lookup(MD->getMemoryInst());
32660b57cec5SDimitry Andric                      });
3267bdd1243dSDimitry Andric       assert(all_equal(PhiOpClasses) &&
32680b57cec5SDimitry Andric              "All MemoryPhi arguments should be in the same class");
32690b57cec5SDimitry Andric     }
32700b57cec5SDimitry Andric   }
32710b57cec5SDimitry Andric #endif
32720b57cec5SDimitry Andric }
32730b57cec5SDimitry Andric 
32740b57cec5SDimitry Andric // Verify that the sparse propagation we did actually found the maximal fixpoint
32750b57cec5SDimitry Andric // We do this by storing the value to class mapping, touching all instructions,
32760b57cec5SDimitry Andric // and redoing the iteration to see if anything changed.
verifyIterationSettled(Function & F)32770b57cec5SDimitry Andric void NewGVN::verifyIterationSettled(Function &F) {
32780b57cec5SDimitry Andric #ifndef NDEBUG
32790b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Beginning iteration verification\n");
32800b57cec5SDimitry Andric   if (DebugCounter::isCounterSet(VNCounter))
3281*0fca6ea1SDimitry Andric     DebugCounter::setCounterState(VNCounter, StartingVNCounter);
32820b57cec5SDimitry Andric 
32830b57cec5SDimitry Andric   // Note that we have to store the actual classes, as we may change existing
32840b57cec5SDimitry Andric   // classes during iteration.  This is because our memory iteration propagation
32850b57cec5SDimitry Andric   // is not perfect, and so may waste a little work.  But it should generate
32860b57cec5SDimitry Andric   // exactly the same congruence classes we have now, with different IDs.
32870b57cec5SDimitry Andric   std::map<const Value *, CongruenceClass> BeforeIteration;
32880b57cec5SDimitry Andric 
32890b57cec5SDimitry Andric   for (auto &KV : ValueToClass) {
32900b57cec5SDimitry Andric     if (auto *I = dyn_cast<Instruction>(KV.first))
32910b57cec5SDimitry Andric       // Skip unused/dead instructions.
32920b57cec5SDimitry Andric       if (InstrToDFSNum(I) == 0)
32930b57cec5SDimitry Andric         continue;
32940b57cec5SDimitry Andric     BeforeIteration.insert({KV.first, *KV.second});
32950b57cec5SDimitry Andric   }
32960b57cec5SDimitry Andric 
32970b57cec5SDimitry Andric   TouchedInstructions.set();
32980b57cec5SDimitry Andric   TouchedInstructions.reset(0);
3299bdd1243dSDimitry Andric   OpSafeForPHIOfOps.clear();
3300*0fca6ea1SDimitry Andric   CacheIdx = 0;
33010b57cec5SDimitry Andric   iterateTouchedInstructions();
33020b57cec5SDimitry Andric   DenseSet<std::pair<const CongruenceClass *, const CongruenceClass *>>
33030b57cec5SDimitry Andric       EqualClasses;
33040b57cec5SDimitry Andric   for (const auto &KV : ValueToClass) {
33050b57cec5SDimitry Andric     if (auto *I = dyn_cast<Instruction>(KV.first))
33060b57cec5SDimitry Andric       // Skip unused/dead instructions.
33070b57cec5SDimitry Andric       if (InstrToDFSNum(I) == 0)
33080b57cec5SDimitry Andric         continue;
33090b57cec5SDimitry Andric     // We could sink these uses, but i think this adds a bit of clarity here as
33100b57cec5SDimitry Andric     // to what we are comparing.
33110b57cec5SDimitry Andric     auto *BeforeCC = &BeforeIteration.find(KV.first)->second;
33120b57cec5SDimitry Andric     auto *AfterCC = KV.second;
33130b57cec5SDimitry Andric     // Note that the classes can't change at this point, so we memoize the set
33140b57cec5SDimitry Andric     // that are equal.
33150b57cec5SDimitry Andric     if (!EqualClasses.count({BeforeCC, AfterCC})) {
33160b57cec5SDimitry Andric       assert(BeforeCC->isEquivalentTo(AfterCC) &&
33170b57cec5SDimitry Andric              "Value number changed after main loop completed!");
33180b57cec5SDimitry Andric       EqualClasses.insert({BeforeCC, AfterCC});
33190b57cec5SDimitry Andric     }
33200b57cec5SDimitry Andric   }
33210b57cec5SDimitry Andric #endif
33220b57cec5SDimitry Andric }
33230b57cec5SDimitry Andric 
33240b57cec5SDimitry Andric // Verify that for each store expression in the expression to class mapping,
33250b57cec5SDimitry Andric // only the latest appears, and multiple ones do not appear.
33260b57cec5SDimitry Andric // Because loads do not use the stored value when doing equality with stores,
33270b57cec5SDimitry Andric // if we don't erase the old store expressions from the table, a load can find
33280b57cec5SDimitry Andric // a no-longer valid StoreExpression.
verifyStoreExpressions() const33290b57cec5SDimitry Andric void NewGVN::verifyStoreExpressions() const {
33300b57cec5SDimitry Andric #ifndef NDEBUG
33310b57cec5SDimitry Andric   // This is the only use of this, and it's not worth defining a complicated
33320b57cec5SDimitry Andric   // densemapinfo hash/equality function for it.
33330b57cec5SDimitry Andric   std::set<
33340b57cec5SDimitry Andric       std::pair<const Value *,
33350b57cec5SDimitry Andric                 std::tuple<const Value *, const CongruenceClass *, Value *>>>
33360b57cec5SDimitry Andric       StoreExpressionSet;
33370b57cec5SDimitry Andric   for (const auto &KV : ExpressionToClass) {
33380b57cec5SDimitry Andric     if (auto *SE = dyn_cast<StoreExpression>(KV.first)) {
33390b57cec5SDimitry Andric       // Make sure a version that will conflict with loads is not already there
33400b57cec5SDimitry Andric       auto Res = StoreExpressionSet.insert(
33410b57cec5SDimitry Andric           {SE->getOperand(0), std::make_tuple(SE->getMemoryLeader(), KV.second,
33420b57cec5SDimitry Andric                                               SE->getStoredValue())});
33430b57cec5SDimitry Andric       bool Okay = Res.second;
33440b57cec5SDimitry Andric       // It's okay to have the same expression already in there if it is
33450b57cec5SDimitry Andric       // identical in nature.
33460b57cec5SDimitry Andric       // This can happen when the leader of the stored value changes over time.
33470b57cec5SDimitry Andric       if (!Okay)
33480b57cec5SDimitry Andric         Okay = (std::get<1>(Res.first->second) == KV.second) &&
33490b57cec5SDimitry Andric                (lookupOperandLeader(std::get<2>(Res.first->second)) ==
33500b57cec5SDimitry Andric                 lookupOperandLeader(SE->getStoredValue()));
33510b57cec5SDimitry Andric       assert(Okay && "Stored expression conflict exists in expression table");
33520b57cec5SDimitry Andric       auto *ValueExpr = ValueToExpression.lookup(SE->getStoreInst());
33530b57cec5SDimitry Andric       assert(ValueExpr && ValueExpr->equals(*SE) &&
33540b57cec5SDimitry Andric              "StoreExpression in ExpressionToClass is not latest "
33550b57cec5SDimitry Andric              "StoreExpression for value");
33560b57cec5SDimitry Andric     }
33570b57cec5SDimitry Andric   }
33580b57cec5SDimitry Andric #endif
33590b57cec5SDimitry Andric }
33600b57cec5SDimitry Andric 
33610b57cec5SDimitry Andric // This is the main value numbering loop, it iterates over the initial touched
33620b57cec5SDimitry Andric // instruction set, propagating value numbers, marking things touched, etc,
33630b57cec5SDimitry Andric // until the set of touched instructions is completely empty.
iterateTouchedInstructions()33640b57cec5SDimitry Andric void NewGVN::iterateTouchedInstructions() {
336581ad6265SDimitry Andric   uint64_t Iterations = 0;
33660b57cec5SDimitry Andric   // Figure out where touchedinstructions starts
33670b57cec5SDimitry Andric   int FirstInstr = TouchedInstructions.find_first();
33680b57cec5SDimitry Andric   // Nothing set, nothing to iterate, just return.
33690b57cec5SDimitry Andric   if (FirstInstr == -1)
33700b57cec5SDimitry Andric     return;
33710b57cec5SDimitry Andric   const BasicBlock *LastBlock = getBlockForValue(InstrFromDFSNum(FirstInstr));
33720b57cec5SDimitry Andric   while (TouchedInstructions.any()) {
33730b57cec5SDimitry Andric     ++Iterations;
33740b57cec5SDimitry Andric     // Walk through all the instructions in all the blocks in RPO.
33750b57cec5SDimitry Andric     // TODO: As we hit a new block, we should push and pop equalities into a
33760b57cec5SDimitry Andric     // table lookupOperandLeader can use, to catch things PredicateInfo
33770b57cec5SDimitry Andric     // might miss, like edge-only equivalences.
33780b57cec5SDimitry Andric     for (unsigned InstrNum : TouchedInstructions.set_bits()) {
33790b57cec5SDimitry Andric 
33800b57cec5SDimitry Andric       // This instruction was found to be dead. We don't bother looking
33810b57cec5SDimitry Andric       // at it again.
33820b57cec5SDimitry Andric       if (InstrNum == 0) {
33830b57cec5SDimitry Andric         TouchedInstructions.reset(InstrNum);
33840b57cec5SDimitry Andric         continue;
33850b57cec5SDimitry Andric       }
33860b57cec5SDimitry Andric 
33870b57cec5SDimitry Andric       Value *V = InstrFromDFSNum(InstrNum);
33880b57cec5SDimitry Andric       const BasicBlock *CurrBlock = getBlockForValue(V);
33890b57cec5SDimitry Andric 
33900b57cec5SDimitry Andric       // If we hit a new block, do reachability processing.
33910b57cec5SDimitry Andric       if (CurrBlock != LastBlock) {
33920b57cec5SDimitry Andric         LastBlock = CurrBlock;
33930b57cec5SDimitry Andric         bool BlockReachable = ReachableBlocks.count(CurrBlock);
33940b57cec5SDimitry Andric         const auto &CurrInstRange = BlockInstRange.lookup(CurrBlock);
33950b57cec5SDimitry Andric 
33960b57cec5SDimitry Andric         // If it's not reachable, erase any touched instructions and move on.
33970b57cec5SDimitry Andric         if (!BlockReachable) {
33980b57cec5SDimitry Andric           TouchedInstructions.reset(CurrInstRange.first, CurrInstRange.second);
33990b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "Skipping instructions in block "
34000b57cec5SDimitry Andric                             << getBlockName(CurrBlock)
34010b57cec5SDimitry Andric                             << " because it is unreachable\n");
34020b57cec5SDimitry Andric           continue;
34030b57cec5SDimitry Andric         }
3404*0fca6ea1SDimitry Andric         // Use the appropriate cache for "OpIsSafeForPHIOfOps".
3405*0fca6ea1SDimitry Andric         CacheIdx = RPOOrdering.lookup(DT->getNode(CurrBlock)) - 1;
34060b57cec5SDimitry Andric         updateProcessedCount(CurrBlock);
34070b57cec5SDimitry Andric       }
34080b57cec5SDimitry Andric       // Reset after processing (because we may mark ourselves as touched when
34090b57cec5SDimitry Andric       // we propagate equalities).
34100b57cec5SDimitry Andric       TouchedInstructions.reset(InstrNum);
34110b57cec5SDimitry Andric 
34120b57cec5SDimitry Andric       if (auto *MP = dyn_cast<MemoryPhi>(V)) {
34130b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Processing MemoryPhi " << *MP << "\n");
34140b57cec5SDimitry Andric         valueNumberMemoryPhi(MP);
34150b57cec5SDimitry Andric       } else if (auto *I = dyn_cast<Instruction>(V)) {
34160b57cec5SDimitry Andric         valueNumberInstruction(I);
34170b57cec5SDimitry Andric       } else {
34180b57cec5SDimitry Andric         llvm_unreachable("Should have been a MemoryPhi or Instruction");
34190b57cec5SDimitry Andric       }
34200b57cec5SDimitry Andric       updateProcessedCount(V);
34210b57cec5SDimitry Andric     }
34220b57cec5SDimitry Andric   }
34230b57cec5SDimitry Andric   NumGVNMaxIterations = std::max(NumGVNMaxIterations.getValue(), Iterations);
34240b57cec5SDimitry Andric }
34250b57cec5SDimitry Andric 
34260b57cec5SDimitry Andric // This is the main transformation entry point.
runGVN()34270b57cec5SDimitry Andric bool NewGVN::runGVN() {
34280b57cec5SDimitry Andric   if (DebugCounter::isCounterSet(VNCounter))
3429*0fca6ea1SDimitry Andric     StartingVNCounter = DebugCounter::getCounterState(VNCounter);
34300b57cec5SDimitry Andric   bool Changed = false;
34310b57cec5SDimitry Andric   NumFuncArgs = F.arg_size();
34320b57cec5SDimitry Andric   MSSAWalker = MSSA->getWalker();
34330b57cec5SDimitry Andric   SingletonDeadExpression = new (ExpressionAllocator) DeadExpression();
34340b57cec5SDimitry Andric 
34350b57cec5SDimitry Andric   // Count number of instructions for sizing of hash tables, and come
34360b57cec5SDimitry Andric   // up with a global dfs numbering for instructions.
34370b57cec5SDimitry Andric   unsigned ICount = 1;
34380b57cec5SDimitry Andric   // Add an empty instruction to account for the fact that we start at 1
34390b57cec5SDimitry Andric   DFSToInstr.emplace_back(nullptr);
34400b57cec5SDimitry Andric   // Note: We want ideal RPO traversal of the blocks, which is not quite the
34410b57cec5SDimitry Andric   // same as dominator tree order, particularly with regard whether backedges
34420b57cec5SDimitry Andric   // get visited first or second, given a block with multiple successors.
34430b57cec5SDimitry Andric   // If we visit in the wrong order, we will end up performing N times as many
34440b57cec5SDimitry Andric   // iterations.
34450b57cec5SDimitry Andric   // The dominator tree does guarantee that, for a given dom tree node, it's
34460b57cec5SDimitry Andric   // parent must occur before it in the RPO ordering. Thus, we only need to sort
34470b57cec5SDimitry Andric   // the siblings.
34480b57cec5SDimitry Andric   ReversePostOrderTraversal<Function *> RPOT(&F);
34490b57cec5SDimitry Andric   unsigned Counter = 0;
34500b57cec5SDimitry Andric   for (auto &B : RPOT) {
34510b57cec5SDimitry Andric     auto *Node = DT->getNode(B);
34520b57cec5SDimitry Andric     assert(Node && "RPO and Dominator tree should have same reachability");
34530b57cec5SDimitry Andric     RPOOrdering[Node] = ++Counter;
34540b57cec5SDimitry Andric   }
34550b57cec5SDimitry Andric   // Sort dominator tree children arrays into RPO.
34560b57cec5SDimitry Andric   for (auto &B : RPOT) {
34570b57cec5SDimitry Andric     auto *Node = DT->getNode(B);
34585ffd83dbSDimitry Andric     if (Node->getNumChildren() > 1)
3459e8d8bef9SDimitry Andric       llvm::sort(*Node, [&](const DomTreeNode *A, const DomTreeNode *B) {
34600b57cec5SDimitry Andric         return RPOOrdering[A] < RPOOrdering[B];
34610b57cec5SDimitry Andric       });
34620b57cec5SDimitry Andric   }
34630b57cec5SDimitry Andric 
34640b57cec5SDimitry Andric   // Now a standard depth first ordering of the domtree is equivalent to RPO.
3465bdd1243dSDimitry Andric   for (auto *DTN : depth_first(DT->getRootNode())) {
34660b57cec5SDimitry Andric     BasicBlock *B = DTN->getBlock();
34670b57cec5SDimitry Andric     const auto &BlockRange = assignDFSNumbers(B, ICount);
34680b57cec5SDimitry Andric     BlockInstRange.insert({B, BlockRange});
34690b57cec5SDimitry Andric     ICount += BlockRange.second - BlockRange.first;
34700b57cec5SDimitry Andric   }
34710b57cec5SDimitry Andric   initializeCongruenceClasses(F);
34720b57cec5SDimitry Andric 
34730b57cec5SDimitry Andric   TouchedInstructions.resize(ICount);
34740b57cec5SDimitry Andric   // Ensure we don't end up resizing the expressionToClass map, as
34750b57cec5SDimitry Andric   // that can be quite expensive. At most, we have one expression per
34760b57cec5SDimitry Andric   // instruction.
34770b57cec5SDimitry Andric   ExpressionToClass.reserve(ICount);
34780b57cec5SDimitry Andric 
34790b57cec5SDimitry Andric   // Initialize the touched instructions to include the entry block.
34800b57cec5SDimitry Andric   const auto &InstRange = BlockInstRange.lookup(&F.getEntryBlock());
34810b57cec5SDimitry Andric   TouchedInstructions.set(InstRange.first, InstRange.second);
34820b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Block " << getBlockName(&F.getEntryBlock())
34830b57cec5SDimitry Andric                     << " marked reachable\n");
34840b57cec5SDimitry Andric   ReachableBlocks.insert(&F.getEntryBlock());
3485*0fca6ea1SDimitry Andric   // Use index corresponding to entry block.
3486*0fca6ea1SDimitry Andric   CacheIdx = 0;
34870b57cec5SDimitry Andric 
34880b57cec5SDimitry Andric   iterateTouchedInstructions();
34890b57cec5SDimitry Andric   verifyMemoryCongruency();
34900b57cec5SDimitry Andric   verifyIterationSettled(F);
34910b57cec5SDimitry Andric   verifyStoreExpressions();
34920b57cec5SDimitry Andric 
34930b57cec5SDimitry Andric   Changed |= eliminateInstructions(F);
34940b57cec5SDimitry Andric 
34950b57cec5SDimitry Andric   // Delete all instructions marked for deletion.
34960b57cec5SDimitry Andric   for (Instruction *ToErase : InstructionsToErase) {
34970b57cec5SDimitry Andric     if (!ToErase->use_empty())
349804eeddc0SDimitry Andric       ToErase->replaceAllUsesWith(PoisonValue::get(ToErase->getType()));
34990b57cec5SDimitry Andric 
35000b57cec5SDimitry Andric     assert(ToErase->getParent() &&
35010b57cec5SDimitry Andric            "BB containing ToErase deleted unexpectedly!");
35020b57cec5SDimitry Andric     ToErase->eraseFromParent();
35030b57cec5SDimitry Andric   }
35040b57cec5SDimitry Andric   Changed |= !InstructionsToErase.empty();
35050b57cec5SDimitry Andric 
35060b57cec5SDimitry Andric   // Delete all unreachable blocks.
35070b57cec5SDimitry Andric   auto UnreachableBlockPred = [&](const BasicBlock &BB) {
35080b57cec5SDimitry Andric     return !ReachableBlocks.count(&BB);
35090b57cec5SDimitry Andric   };
35100b57cec5SDimitry Andric 
35110b57cec5SDimitry Andric   for (auto &BB : make_filter_range(F, UnreachableBlockPred)) {
35120b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "We believe block " << getBlockName(&BB)
35130b57cec5SDimitry Andric                       << " is unreachable\n");
35140b57cec5SDimitry Andric     deleteInstructionsInBlock(&BB);
35150b57cec5SDimitry Andric     Changed = true;
35160b57cec5SDimitry Andric   }
35170b57cec5SDimitry Andric 
35180b57cec5SDimitry Andric   cleanupTables();
35190b57cec5SDimitry Andric   return Changed;
35200b57cec5SDimitry Andric }
35210b57cec5SDimitry Andric 
35220b57cec5SDimitry Andric struct NewGVN::ValueDFS {
35230b57cec5SDimitry Andric   int DFSIn = 0;
35240b57cec5SDimitry Andric   int DFSOut = 0;
35250b57cec5SDimitry Andric   int LocalNum = 0;
35260b57cec5SDimitry Andric 
35270b57cec5SDimitry Andric   // Only one of Def and U will be set.
35280b57cec5SDimitry Andric   // The bool in the Def tells us whether the Def is the stored value of a
35290b57cec5SDimitry Andric   // store.
35300b57cec5SDimitry Andric   PointerIntPair<Value *, 1, bool> Def;
35310b57cec5SDimitry Andric   Use *U = nullptr;
35320b57cec5SDimitry Andric 
operator <NewGVN::ValueDFS35330b57cec5SDimitry Andric   bool operator<(const ValueDFS &Other) const {
35340b57cec5SDimitry Andric     // It's not enough that any given field be less than - we have sets
35350b57cec5SDimitry Andric     // of fields that need to be evaluated together to give a proper ordering.
35360b57cec5SDimitry Andric     // For example, if you have;
35370b57cec5SDimitry Andric     // DFS (1, 3)
35380b57cec5SDimitry Andric     // Val 0
35390b57cec5SDimitry Andric     // DFS (1, 2)
35400b57cec5SDimitry Andric     // Val 50
35410b57cec5SDimitry Andric     // We want the second to be less than the first, but if we just go field
35420b57cec5SDimitry Andric     // by field, we will get to Val 0 < Val 50 and say the first is less than
35430b57cec5SDimitry Andric     // the second. We only want it to be less than if the DFS orders are equal.
35440b57cec5SDimitry Andric     //
35450b57cec5SDimitry Andric     // Each LLVM instruction only produces one value, and thus the lowest-level
35465f757f3fSDimitry Andric     // differentiator that really matters for the stack (and what we use as a
35470b57cec5SDimitry Andric     // replacement) is the local dfs number.
35480b57cec5SDimitry Andric     // Everything else in the structure is instruction level, and only affects
35490b57cec5SDimitry Andric     // the order in which we will replace operands of a given instruction.
35500b57cec5SDimitry Andric     //
35510b57cec5SDimitry Andric     // For a given instruction (IE things with equal dfsin, dfsout, localnum),
35520b57cec5SDimitry Andric     // the order of replacement of uses does not matter.
35530b57cec5SDimitry Andric     // IE given,
35540b57cec5SDimitry Andric     //  a = 5
35550b57cec5SDimitry Andric     //  b = a + a
35560b57cec5SDimitry Andric     // When you hit b, you will have two valuedfs with the same dfsin, out, and
35570b57cec5SDimitry Andric     // localnum.
35580b57cec5SDimitry Andric     // The .val will be the same as well.
35590b57cec5SDimitry Andric     // The .u's will be different.
35600b57cec5SDimitry Andric     // You will replace both, and it does not matter what order you replace them
35610b57cec5SDimitry Andric     // in (IE whether you replace operand 2, then operand 1, or operand 1, then
35620b57cec5SDimitry Andric     // operand 2).
35630b57cec5SDimitry Andric     // Similarly for the case of same dfsin, dfsout, localnum, but different
35640b57cec5SDimitry Andric     // .val's
35650b57cec5SDimitry Andric     //  a = 5
35660b57cec5SDimitry Andric     //  b  = 6
35670b57cec5SDimitry Andric     //  c = a + b
35680b57cec5SDimitry Andric     // in c, we will a valuedfs for a, and one for b,with everything the same
35690b57cec5SDimitry Andric     // but .val  and .u.
35700b57cec5SDimitry Andric     // It does not matter what order we replace these operands in.
35710b57cec5SDimitry Andric     // You will always end up with the same IR, and this is guaranteed.
35720b57cec5SDimitry Andric     return std::tie(DFSIn, DFSOut, LocalNum, Def, U) <
35730b57cec5SDimitry Andric            std::tie(Other.DFSIn, Other.DFSOut, Other.LocalNum, Other.Def,
35740b57cec5SDimitry Andric                     Other.U);
35750b57cec5SDimitry Andric   }
35760b57cec5SDimitry Andric };
35770b57cec5SDimitry Andric 
35780b57cec5SDimitry Andric // This function converts the set of members for a congruence class from values,
35790b57cec5SDimitry Andric // to sets of defs and uses with associated DFS info.  The total number of
35800b57cec5SDimitry Andric // reachable uses for each value is stored in UseCount, and instructions that
35810b57cec5SDimitry Andric // seem
35820b57cec5SDimitry Andric // dead (have no non-dead uses) are stored in ProbablyDead.
convertClassToDFSOrdered(const CongruenceClass & Dense,SmallVectorImpl<ValueDFS> & DFSOrderedSet,DenseMap<const Value *,unsigned int> & UseCounts,SmallPtrSetImpl<Instruction * > & ProbablyDead) const35830b57cec5SDimitry Andric void NewGVN::convertClassToDFSOrdered(
35840b57cec5SDimitry Andric     const CongruenceClass &Dense, SmallVectorImpl<ValueDFS> &DFSOrderedSet,
35850b57cec5SDimitry Andric     DenseMap<const Value *, unsigned int> &UseCounts,
35860b57cec5SDimitry Andric     SmallPtrSetImpl<Instruction *> &ProbablyDead) const {
3587bdd1243dSDimitry Andric   for (auto *D : Dense) {
35880b57cec5SDimitry Andric     // First add the value.
35890b57cec5SDimitry Andric     BasicBlock *BB = getBlockForValue(D);
35900b57cec5SDimitry Andric     // Constants are handled prior to ever calling this function, so
35910b57cec5SDimitry Andric     // we should only be left with instructions as members.
35920b57cec5SDimitry Andric     assert(BB && "Should have figured out a basic block for value");
35930b57cec5SDimitry Andric     ValueDFS VDDef;
35940b57cec5SDimitry Andric     DomTreeNode *DomNode = DT->getNode(BB);
35950b57cec5SDimitry Andric     VDDef.DFSIn = DomNode->getDFSNumIn();
35960b57cec5SDimitry Andric     VDDef.DFSOut = DomNode->getDFSNumOut();
35970b57cec5SDimitry Andric     // If it's a store, use the leader of the value operand, if it's always
35980b57cec5SDimitry Andric     // available, or the value operand.  TODO: We could do dominance checks to
35990b57cec5SDimitry Andric     // find a dominating leader, but not worth it ATM.
36000b57cec5SDimitry Andric     if (auto *SI = dyn_cast<StoreInst>(D)) {
36010b57cec5SDimitry Andric       auto Leader = lookupOperandLeader(SI->getValueOperand());
36020b57cec5SDimitry Andric       if (alwaysAvailable(Leader)) {
36030b57cec5SDimitry Andric         VDDef.Def.setPointer(Leader);
36040b57cec5SDimitry Andric       } else {
36050b57cec5SDimitry Andric         VDDef.Def.setPointer(SI->getValueOperand());
36060b57cec5SDimitry Andric         VDDef.Def.setInt(true);
36070b57cec5SDimitry Andric       }
36080b57cec5SDimitry Andric     } else {
36090b57cec5SDimitry Andric       VDDef.Def.setPointer(D);
36100b57cec5SDimitry Andric     }
36110b57cec5SDimitry Andric     assert(isa<Instruction>(D) &&
36120b57cec5SDimitry Andric            "The dense set member should always be an instruction");
36130b57cec5SDimitry Andric     Instruction *Def = cast<Instruction>(D);
36140b57cec5SDimitry Andric     VDDef.LocalNum = InstrToDFSNum(D);
36150b57cec5SDimitry Andric     DFSOrderedSet.push_back(VDDef);
36160b57cec5SDimitry Andric     // If there is a phi node equivalent, add it
36170b57cec5SDimitry Andric     if (auto *PN = RealToTemp.lookup(Def)) {
36180b57cec5SDimitry Andric       auto *PHIE =
36190b57cec5SDimitry Andric           dyn_cast_or_null<PHIExpression>(ValueToExpression.lookup(Def));
36200b57cec5SDimitry Andric       if (PHIE) {
36210b57cec5SDimitry Andric         VDDef.Def.setInt(false);
36220b57cec5SDimitry Andric         VDDef.Def.setPointer(PN);
36230b57cec5SDimitry Andric         VDDef.LocalNum = 0;
36240b57cec5SDimitry Andric         DFSOrderedSet.push_back(VDDef);
36250b57cec5SDimitry Andric       }
36260b57cec5SDimitry Andric     }
36270b57cec5SDimitry Andric 
36280b57cec5SDimitry Andric     unsigned int UseCount = 0;
36290b57cec5SDimitry Andric     // Now add the uses.
36300b57cec5SDimitry Andric     for (auto &U : Def->uses()) {
36310b57cec5SDimitry Andric       if (auto *I = dyn_cast<Instruction>(U.getUser())) {
36320b57cec5SDimitry Andric         // Don't try to replace into dead uses
36330b57cec5SDimitry Andric         if (InstructionsToErase.count(I))
36340b57cec5SDimitry Andric           continue;
36350b57cec5SDimitry Andric         ValueDFS VDUse;
36360b57cec5SDimitry Andric         // Put the phi node uses in the incoming block.
36370b57cec5SDimitry Andric         BasicBlock *IBlock;
36380b57cec5SDimitry Andric         if (auto *P = dyn_cast<PHINode>(I)) {
36390b57cec5SDimitry Andric           IBlock = P->getIncomingBlock(U);
36400b57cec5SDimitry Andric           // Make phi node users appear last in the incoming block
36410b57cec5SDimitry Andric           // they are from.
36420b57cec5SDimitry Andric           VDUse.LocalNum = InstrDFS.size() + 1;
36430b57cec5SDimitry Andric         } else {
36440b57cec5SDimitry Andric           IBlock = getBlockForValue(I);
36450b57cec5SDimitry Andric           VDUse.LocalNum = InstrToDFSNum(I);
36460b57cec5SDimitry Andric         }
36470b57cec5SDimitry Andric 
36480b57cec5SDimitry Andric         // Skip uses in unreachable blocks, as we're going
36490b57cec5SDimitry Andric         // to delete them.
3650349cc55cSDimitry Andric         if (!ReachableBlocks.contains(IBlock))
36510b57cec5SDimitry Andric           continue;
36520b57cec5SDimitry Andric 
36530b57cec5SDimitry Andric         DomTreeNode *DomNode = DT->getNode(IBlock);
36540b57cec5SDimitry Andric         VDUse.DFSIn = DomNode->getDFSNumIn();
36550b57cec5SDimitry Andric         VDUse.DFSOut = DomNode->getDFSNumOut();
36560b57cec5SDimitry Andric         VDUse.U = &U;
36570b57cec5SDimitry Andric         ++UseCount;
36580b57cec5SDimitry Andric         DFSOrderedSet.emplace_back(VDUse);
36590b57cec5SDimitry Andric       }
36600b57cec5SDimitry Andric     }
36610b57cec5SDimitry Andric 
36620b57cec5SDimitry Andric     // If there are no uses, it's probably dead (but it may have side-effects,
36630b57cec5SDimitry Andric     // so not definitely dead. Otherwise, store the number of uses so we can
36640b57cec5SDimitry Andric     // track if it becomes dead later).
36650b57cec5SDimitry Andric     if (UseCount == 0)
36660b57cec5SDimitry Andric       ProbablyDead.insert(Def);
36670b57cec5SDimitry Andric     else
36680b57cec5SDimitry Andric       UseCounts[Def] = UseCount;
36690b57cec5SDimitry Andric   }
36700b57cec5SDimitry Andric }
36710b57cec5SDimitry Andric 
36720b57cec5SDimitry Andric // This function converts the set of members for a congruence class from values,
36730b57cec5SDimitry Andric // to the set of defs for loads and stores, with associated DFS info.
convertClassToLoadsAndStores(const CongruenceClass & Dense,SmallVectorImpl<ValueDFS> & LoadsAndStores) const36740b57cec5SDimitry Andric void NewGVN::convertClassToLoadsAndStores(
36750b57cec5SDimitry Andric     const CongruenceClass &Dense,
36760b57cec5SDimitry Andric     SmallVectorImpl<ValueDFS> &LoadsAndStores) const {
3677bdd1243dSDimitry Andric   for (auto *D : Dense) {
36780b57cec5SDimitry Andric     if (!isa<LoadInst>(D) && !isa<StoreInst>(D))
36790b57cec5SDimitry Andric       continue;
36800b57cec5SDimitry Andric 
36810b57cec5SDimitry Andric     BasicBlock *BB = getBlockForValue(D);
36820b57cec5SDimitry Andric     ValueDFS VD;
36830b57cec5SDimitry Andric     DomTreeNode *DomNode = DT->getNode(BB);
36840b57cec5SDimitry Andric     VD.DFSIn = DomNode->getDFSNumIn();
36850b57cec5SDimitry Andric     VD.DFSOut = DomNode->getDFSNumOut();
36860b57cec5SDimitry Andric     VD.Def.setPointer(D);
36870b57cec5SDimitry Andric 
36880b57cec5SDimitry Andric     // If it's an instruction, use the real local dfs number.
36890b57cec5SDimitry Andric     if (auto *I = dyn_cast<Instruction>(D))
36900b57cec5SDimitry Andric       VD.LocalNum = InstrToDFSNum(I);
36910b57cec5SDimitry Andric     else
36920b57cec5SDimitry Andric       llvm_unreachable("Should have been an instruction");
36930b57cec5SDimitry Andric 
36940b57cec5SDimitry Andric     LoadsAndStores.emplace_back(VD);
36950b57cec5SDimitry Andric   }
36960b57cec5SDimitry Andric }
36970b57cec5SDimitry Andric 
patchAndReplaceAllUsesWith(Instruction * I,Value * Repl)36980b57cec5SDimitry Andric static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
36990b57cec5SDimitry Andric   patchReplacementInstruction(I, Repl);
37000b57cec5SDimitry Andric   I->replaceAllUsesWith(Repl);
37010b57cec5SDimitry Andric }
37020b57cec5SDimitry Andric 
deleteInstructionsInBlock(BasicBlock * BB)37030b57cec5SDimitry Andric void NewGVN::deleteInstructionsInBlock(BasicBlock *BB) {
37040b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "  BasicBlock Dead:" << *BB);
37050b57cec5SDimitry Andric   ++NumGVNBlocksDeleted;
37060b57cec5SDimitry Andric 
37070b57cec5SDimitry Andric   // Delete the instructions backwards, as it has a reduced likelihood of having
37080b57cec5SDimitry Andric   // to update as many def-use and use-def chains. Start after the terminator.
37090b57cec5SDimitry Andric   auto StartPoint = BB->rbegin();
37100b57cec5SDimitry Andric   ++StartPoint;
37110b57cec5SDimitry Andric   // Note that we explicitly recalculate BB->rend() on each iteration,
37120b57cec5SDimitry Andric   // as it may change when we remove the first instruction.
37130b57cec5SDimitry Andric   for (BasicBlock::reverse_iterator I(StartPoint); I != BB->rend();) {
37140b57cec5SDimitry Andric     Instruction &Inst = *I++;
37150b57cec5SDimitry Andric     if (!Inst.use_empty())
371604eeddc0SDimitry Andric       Inst.replaceAllUsesWith(PoisonValue::get(Inst.getType()));
37170b57cec5SDimitry Andric     if (isa<LandingPadInst>(Inst))
37180b57cec5SDimitry Andric       continue;
37195ffd83dbSDimitry Andric     salvageKnowledge(&Inst, AC);
37200b57cec5SDimitry Andric 
37210b57cec5SDimitry Andric     Inst.eraseFromParent();
37220b57cec5SDimitry Andric     ++NumGVNInstrDeleted;
37230b57cec5SDimitry Andric   }
37240b57cec5SDimitry Andric   // Now insert something that simplifycfg will turn into an unreachable.
37250b57cec5SDimitry Andric   Type *Int8Ty = Type::getInt8Ty(BB->getContext());
372606c3fb27SDimitry Andric   new StoreInst(
372706c3fb27SDimitry Andric       PoisonValue::get(Int8Ty),
372806c3fb27SDimitry Andric       Constant::getNullValue(PointerType::getUnqual(BB->getContext())),
3729*0fca6ea1SDimitry Andric       BB->getTerminator()->getIterator());
37300b57cec5SDimitry Andric }
37310b57cec5SDimitry Andric 
markInstructionForDeletion(Instruction * I)37320b57cec5SDimitry Andric void NewGVN::markInstructionForDeletion(Instruction *I) {
37330b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Marking " << *I << " for deletion\n");
37340b57cec5SDimitry Andric   InstructionsToErase.insert(I);
37350b57cec5SDimitry Andric }
37360b57cec5SDimitry Andric 
replaceInstruction(Instruction * I,Value * V)37370b57cec5SDimitry Andric void NewGVN::replaceInstruction(Instruction *I, Value *V) {
37380b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Replacing " << *I << " with " << *V << "\n");
37390b57cec5SDimitry Andric   patchAndReplaceAllUsesWith(I, V);
37400b57cec5SDimitry Andric   // We save the actual erasing to avoid invalidating memory
37410b57cec5SDimitry Andric   // dependencies until we are done with everything.
37420b57cec5SDimitry Andric   markInstructionForDeletion(I);
37430b57cec5SDimitry Andric }
37440b57cec5SDimitry Andric 
37450b57cec5SDimitry Andric namespace {
37460b57cec5SDimitry Andric 
37470b57cec5SDimitry Andric // This is a stack that contains both the value and dfs info of where
37480b57cec5SDimitry Andric // that value is valid.
37490b57cec5SDimitry Andric class ValueDFSStack {
37500b57cec5SDimitry Andric public:
back() const37510b57cec5SDimitry Andric   Value *back() const { return ValueStack.back(); }
dfs_back() const37520b57cec5SDimitry Andric   std::pair<int, int> dfs_back() const { return DFSStack.back(); }
37530b57cec5SDimitry Andric 
push_back(Value * V,int DFSIn,int DFSOut)37540b57cec5SDimitry Andric   void push_back(Value *V, int DFSIn, int DFSOut) {
37550b57cec5SDimitry Andric     ValueStack.emplace_back(V);
37560b57cec5SDimitry Andric     DFSStack.emplace_back(DFSIn, DFSOut);
37570b57cec5SDimitry Andric   }
37580b57cec5SDimitry Andric 
empty() const37590b57cec5SDimitry Andric   bool empty() const { return DFSStack.empty(); }
37600b57cec5SDimitry Andric 
isInScope(int DFSIn,int DFSOut) const37610b57cec5SDimitry Andric   bool isInScope(int DFSIn, int DFSOut) const {
37620b57cec5SDimitry Andric     if (empty())
37630b57cec5SDimitry Andric       return false;
37640b57cec5SDimitry Andric     return DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second;
37650b57cec5SDimitry Andric   }
37660b57cec5SDimitry Andric 
popUntilDFSScope(int DFSIn,int DFSOut)37670b57cec5SDimitry Andric   void popUntilDFSScope(int DFSIn, int DFSOut) {
37680b57cec5SDimitry Andric 
37690b57cec5SDimitry Andric     // These two should always be in sync at this point.
37700b57cec5SDimitry Andric     assert(ValueStack.size() == DFSStack.size() &&
37710b57cec5SDimitry Andric            "Mismatch between ValueStack and DFSStack");
37720b57cec5SDimitry Andric     while (
37730b57cec5SDimitry Andric         !DFSStack.empty() &&
37740b57cec5SDimitry Andric         !(DFSIn >= DFSStack.back().first && DFSOut <= DFSStack.back().second)) {
37750b57cec5SDimitry Andric       DFSStack.pop_back();
37760b57cec5SDimitry Andric       ValueStack.pop_back();
37770b57cec5SDimitry Andric     }
37780b57cec5SDimitry Andric   }
37790b57cec5SDimitry Andric 
37800b57cec5SDimitry Andric private:
37810b57cec5SDimitry Andric   SmallVector<Value *, 8> ValueStack;
37820b57cec5SDimitry Andric   SmallVector<std::pair<int, int>, 8> DFSStack;
37830b57cec5SDimitry Andric };
37840b57cec5SDimitry Andric 
37850b57cec5SDimitry Andric } // end anonymous namespace
37860b57cec5SDimitry Andric 
37870b57cec5SDimitry Andric // Given an expression, get the congruence class for it.
getClassForExpression(const Expression * E) const37880b57cec5SDimitry Andric CongruenceClass *NewGVN::getClassForExpression(const Expression *E) const {
37890b57cec5SDimitry Andric   if (auto *VE = dyn_cast<VariableExpression>(E))
37900b57cec5SDimitry Andric     return ValueToClass.lookup(VE->getVariableValue());
37910b57cec5SDimitry Andric   else if (isa<DeadExpression>(E))
37920b57cec5SDimitry Andric     return TOPClass;
37930b57cec5SDimitry Andric   return ExpressionToClass.lookup(E);
37940b57cec5SDimitry Andric }
37950b57cec5SDimitry Andric 
37960b57cec5SDimitry Andric // Given a value and a basic block we are trying to see if it is available in,
37970b57cec5SDimitry Andric // see if the value has a leader available in that block.
findPHIOfOpsLeader(const Expression * E,const Instruction * OrigInst,const BasicBlock * BB) const37980b57cec5SDimitry Andric Value *NewGVN::findPHIOfOpsLeader(const Expression *E,
37990b57cec5SDimitry Andric                                   const Instruction *OrigInst,
38000b57cec5SDimitry Andric                                   const BasicBlock *BB) const {
38010b57cec5SDimitry Andric   // It would already be constant if we could make it constant
38020b57cec5SDimitry Andric   if (auto *CE = dyn_cast<ConstantExpression>(E))
38030b57cec5SDimitry Andric     return CE->getConstantValue();
38040b57cec5SDimitry Andric   if (auto *VE = dyn_cast<VariableExpression>(E)) {
38050b57cec5SDimitry Andric     auto *V = VE->getVariableValue();
38060b57cec5SDimitry Andric     if (alwaysAvailable(V) || DT->dominates(getBlockForValue(V), BB))
38070b57cec5SDimitry Andric       return VE->getVariableValue();
38080b57cec5SDimitry Andric   }
38090b57cec5SDimitry Andric 
38100b57cec5SDimitry Andric   auto *CC = getClassForExpression(E);
38110b57cec5SDimitry Andric   if (!CC)
38120b57cec5SDimitry Andric     return nullptr;
38130b57cec5SDimitry Andric   if (alwaysAvailable(CC->getLeader()))
38140b57cec5SDimitry Andric     return CC->getLeader();
38150b57cec5SDimitry Andric 
3816bdd1243dSDimitry Andric   for (auto *Member : *CC) {
38170b57cec5SDimitry Andric     auto *MemberInst = dyn_cast<Instruction>(Member);
38180b57cec5SDimitry Andric     if (MemberInst == OrigInst)
38190b57cec5SDimitry Andric       continue;
38200b57cec5SDimitry Andric     // Anything that isn't an instruction is always available.
38210b57cec5SDimitry Andric     if (!MemberInst)
38220b57cec5SDimitry Andric       return Member;
38230b57cec5SDimitry Andric     if (DT->dominates(getBlockForValue(MemberInst), BB))
38240b57cec5SDimitry Andric       return Member;
38250b57cec5SDimitry Andric   }
38260b57cec5SDimitry Andric   return nullptr;
38270b57cec5SDimitry Andric }
38280b57cec5SDimitry Andric 
eliminateInstructions(Function & F)38290b57cec5SDimitry Andric bool NewGVN::eliminateInstructions(Function &F) {
38300b57cec5SDimitry Andric   // This is a non-standard eliminator. The normal way to eliminate is
38310b57cec5SDimitry Andric   // to walk the dominator tree in order, keeping track of available
38320b57cec5SDimitry Andric   // values, and eliminating them.  However, this is mildly
38330b57cec5SDimitry Andric   // pointless. It requires doing lookups on every instruction,
38340b57cec5SDimitry Andric   // regardless of whether we will ever eliminate it.  For
38350b57cec5SDimitry Andric   // instructions part of most singleton congruence classes, we know we
38360b57cec5SDimitry Andric   // will never eliminate them.
38370b57cec5SDimitry Andric 
38380b57cec5SDimitry Andric   // Instead, this eliminator looks at the congruence classes directly, sorts
38390b57cec5SDimitry Andric   // them into a DFS ordering of the dominator tree, and then we just
38400b57cec5SDimitry Andric   // perform elimination straight on the sets by walking the congruence
38410b57cec5SDimitry Andric   // class member uses in order, and eliminate the ones dominated by the
38420b57cec5SDimitry Andric   // last member.   This is worst case O(E log E) where E = number of
38430b57cec5SDimitry Andric   // instructions in a single congruence class.  In theory, this is all
38440b57cec5SDimitry Andric   // instructions.   In practice, it is much faster, as most instructions are
38450b57cec5SDimitry Andric   // either in singleton congruence classes or can't possibly be eliminated
38460b57cec5SDimitry Andric   // anyway (if there are no overlapping DFS ranges in class).
38470b57cec5SDimitry Andric   // When we find something not dominated, it becomes the new leader
38480b57cec5SDimitry Andric   // for elimination purposes.
38490b57cec5SDimitry Andric   // TODO: If we wanted to be faster, We could remove any members with no
38500b57cec5SDimitry Andric   // overlapping ranges while sorting, as we will never eliminate anything
38510b57cec5SDimitry Andric   // with those members, as they don't dominate anything else in our set.
38520b57cec5SDimitry Andric 
38530b57cec5SDimitry Andric   bool AnythingReplaced = false;
38540b57cec5SDimitry Andric 
38550b57cec5SDimitry Andric   // Since we are going to walk the domtree anyway, and we can't guarantee the
38560b57cec5SDimitry Andric   // DFS numbers are updated, we compute some ourselves.
38570b57cec5SDimitry Andric   DT->updateDFSNumbers();
38580b57cec5SDimitry Andric 
38590b57cec5SDimitry Andric   // Go through all of our phi nodes, and kill the arguments associated with
38600b57cec5SDimitry Andric   // unreachable edges.
38610b57cec5SDimitry Andric   auto ReplaceUnreachablePHIArgs = [&](PHINode *PHI, BasicBlock *BB) {
38620b57cec5SDimitry Andric     for (auto &Operand : PHI->incoming_values())
38630b57cec5SDimitry Andric       if (!ReachableEdges.count({PHI->getIncomingBlock(Operand), BB})) {
38640b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Replacing incoming value of " << PHI
38650b57cec5SDimitry Andric                           << " for block "
38660b57cec5SDimitry Andric                           << getBlockName(PHI->getIncomingBlock(Operand))
386704eeddc0SDimitry Andric                           << " with poison due to it being unreachable\n");
386804eeddc0SDimitry Andric         Operand.set(PoisonValue::get(PHI->getType()));
38690b57cec5SDimitry Andric       }
38700b57cec5SDimitry Andric   };
38710b57cec5SDimitry Andric   // Replace unreachable phi arguments.
38720b57cec5SDimitry Andric   // At this point, RevisitOnReachabilityChange only contains:
38730b57cec5SDimitry Andric   //
38740b57cec5SDimitry Andric   // 1. PHIs
38750b57cec5SDimitry Andric   // 2. Temporaries that will convert to PHIs
38760b57cec5SDimitry Andric   // 3. Operations that are affected by an unreachable edge but do not fit into
38770b57cec5SDimitry Andric   // 1 or 2 (rare).
38780b57cec5SDimitry Andric   // So it is a slight overshoot of what we want. We could make it exact by
38790b57cec5SDimitry Andric   // using two SparseBitVectors per block.
38800b57cec5SDimitry Andric   DenseMap<const BasicBlock *, unsigned> ReachablePredCount;
38810b57cec5SDimitry Andric   for (auto &KV : ReachableEdges)
38820b57cec5SDimitry Andric     ReachablePredCount[KV.getEnd()]++;
38830b57cec5SDimitry Andric   for (auto &BBPair : RevisitOnReachabilityChange) {
38840b57cec5SDimitry Andric     for (auto InstNum : BBPair.second) {
38850b57cec5SDimitry Andric       auto *Inst = InstrFromDFSNum(InstNum);
38860b57cec5SDimitry Andric       auto *PHI = dyn_cast<PHINode>(Inst);
38870b57cec5SDimitry Andric       PHI = PHI ? PHI : dyn_cast_or_null<PHINode>(RealToTemp.lookup(Inst));
38880b57cec5SDimitry Andric       if (!PHI)
38890b57cec5SDimitry Andric         continue;
38900b57cec5SDimitry Andric       auto *BB = BBPair.first;
38910b57cec5SDimitry Andric       if (ReachablePredCount.lookup(BB) != PHI->getNumIncomingValues())
38920b57cec5SDimitry Andric         ReplaceUnreachablePHIArgs(PHI, BB);
38930b57cec5SDimitry Andric     }
38940b57cec5SDimitry Andric   }
38950b57cec5SDimitry Andric 
38960b57cec5SDimitry Andric   // Map to store the use counts
38970b57cec5SDimitry Andric   DenseMap<const Value *, unsigned int> UseCounts;
38980b57cec5SDimitry Andric   for (auto *CC : reverse(CongruenceClasses)) {
38990b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Eliminating in congruence class " << CC->getID()
39000b57cec5SDimitry Andric                       << "\n");
39010b57cec5SDimitry Andric     // Track the equivalent store info so we can decide whether to try
39020b57cec5SDimitry Andric     // dead store elimination.
39030b57cec5SDimitry Andric     SmallVector<ValueDFS, 8> PossibleDeadStores;
39040b57cec5SDimitry Andric     SmallPtrSet<Instruction *, 8> ProbablyDead;
39050b57cec5SDimitry Andric     if (CC->isDead() || CC->empty())
39060b57cec5SDimitry Andric       continue;
39070b57cec5SDimitry Andric     // Everything still in the TOP class is unreachable or dead.
39080b57cec5SDimitry Andric     if (CC == TOPClass) {
3909bdd1243dSDimitry Andric       for (auto *M : *CC) {
39100b57cec5SDimitry Andric         auto *VTE = ValueToExpression.lookup(M);
39110b57cec5SDimitry Andric         if (VTE && isa<DeadExpression>(VTE))
39120b57cec5SDimitry Andric           markInstructionForDeletion(cast<Instruction>(M));
39130b57cec5SDimitry Andric         assert((!ReachableBlocks.count(cast<Instruction>(M)->getParent()) ||
39140b57cec5SDimitry Andric                 InstructionsToErase.count(cast<Instruction>(M))) &&
39150b57cec5SDimitry Andric                "Everything in TOP should be unreachable or dead at this "
39160b57cec5SDimitry Andric                "point");
39170b57cec5SDimitry Andric       }
39180b57cec5SDimitry Andric       continue;
39190b57cec5SDimitry Andric     }
39200b57cec5SDimitry Andric 
39210b57cec5SDimitry Andric     assert(CC->getLeader() && "We should have had a leader");
39220b57cec5SDimitry Andric     // If this is a leader that is always available, and it's a
39230b57cec5SDimitry Andric     // constant or has no equivalences, just replace everything with
39240b57cec5SDimitry Andric     // it. We then update the congruence class with whatever members
39250b57cec5SDimitry Andric     // are left.
39260b57cec5SDimitry Andric     Value *Leader =
39270b57cec5SDimitry Andric         CC->getStoredValue() ? CC->getStoredValue() : CC->getLeader();
39280b57cec5SDimitry Andric     if (alwaysAvailable(Leader)) {
39290b57cec5SDimitry Andric       CongruenceClass::MemberSet MembersLeft;
3930bdd1243dSDimitry Andric       for (auto *M : *CC) {
39310b57cec5SDimitry Andric         Value *Member = M;
39320b57cec5SDimitry Andric         // Void things have no uses we can replace.
39330b57cec5SDimitry Andric         if (Member == Leader || !isa<Instruction>(Member) ||
39340b57cec5SDimitry Andric             Member->getType()->isVoidTy()) {
39350b57cec5SDimitry Andric           MembersLeft.insert(Member);
39360b57cec5SDimitry Andric           continue;
39370b57cec5SDimitry Andric         }
39380b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Found replacement " << *(Leader) << " for "
39390b57cec5SDimitry Andric                           << *Member << "\n");
39400b57cec5SDimitry Andric         auto *I = cast<Instruction>(Member);
39410b57cec5SDimitry Andric         assert(Leader != I && "About to accidentally remove our leader");
39420b57cec5SDimitry Andric         replaceInstruction(I, Leader);
39430b57cec5SDimitry Andric         AnythingReplaced = true;
39440b57cec5SDimitry Andric       }
39450b57cec5SDimitry Andric       CC->swap(MembersLeft);
39460b57cec5SDimitry Andric     } else {
39470b57cec5SDimitry Andric       // If this is a singleton, we can skip it.
39480b57cec5SDimitry Andric       if (CC->size() != 1 || RealToTemp.count(Leader)) {
39490b57cec5SDimitry Andric         // This is a stack because equality replacement/etc may place
39500b57cec5SDimitry Andric         // constants in the middle of the member list, and we want to use
39510b57cec5SDimitry Andric         // those constant values in preference to the current leader, over
39520b57cec5SDimitry Andric         // the scope of those constants.
39530b57cec5SDimitry Andric         ValueDFSStack EliminationStack;
39540b57cec5SDimitry Andric 
39550b57cec5SDimitry Andric         // Convert the members to DFS ordered sets and then merge them.
39560b57cec5SDimitry Andric         SmallVector<ValueDFS, 8> DFSOrderedSet;
39570b57cec5SDimitry Andric         convertClassToDFSOrdered(*CC, DFSOrderedSet, UseCounts, ProbablyDead);
39580b57cec5SDimitry Andric 
39590b57cec5SDimitry Andric         // Sort the whole thing.
39600b57cec5SDimitry Andric         llvm::sort(DFSOrderedSet);
39610b57cec5SDimitry Andric         for (auto &VD : DFSOrderedSet) {
39620b57cec5SDimitry Andric           int MemberDFSIn = VD.DFSIn;
39630b57cec5SDimitry Andric           int MemberDFSOut = VD.DFSOut;
39640b57cec5SDimitry Andric           Value *Def = VD.Def.getPointer();
39650b57cec5SDimitry Andric           bool FromStore = VD.Def.getInt();
39660b57cec5SDimitry Andric           Use *U = VD.U;
39670b57cec5SDimitry Andric           // We ignore void things because we can't get a value from them.
39680b57cec5SDimitry Andric           if (Def && Def->getType()->isVoidTy())
39690b57cec5SDimitry Andric             continue;
39700b57cec5SDimitry Andric           auto *DefInst = dyn_cast_or_null<Instruction>(Def);
39710b57cec5SDimitry Andric           if (DefInst && AllTempInstructions.count(DefInst)) {
39720b57cec5SDimitry Andric             auto *PN = cast<PHINode>(DefInst);
39730b57cec5SDimitry Andric 
39740b57cec5SDimitry Andric             // If this is a value phi and that's the expression we used, insert
39750b57cec5SDimitry Andric             // it into the program
39760b57cec5SDimitry Andric             // remove from temp instruction list.
39770b57cec5SDimitry Andric             AllTempInstructions.erase(PN);
39780b57cec5SDimitry Andric             auto *DefBlock = getBlockForValue(Def);
39790b57cec5SDimitry Andric             LLVM_DEBUG(dbgs() << "Inserting fully real phi of ops" << *Def
39800b57cec5SDimitry Andric                               << " into block "
39810b57cec5SDimitry Andric                               << getBlockName(getBlockForValue(Def)) << "\n");
39820b57cec5SDimitry Andric             PN->insertBefore(&DefBlock->front());
39830b57cec5SDimitry Andric             Def = PN;
39840b57cec5SDimitry Andric             NumGVNPHIOfOpsEliminations++;
39850b57cec5SDimitry Andric           }
39860b57cec5SDimitry Andric 
39870b57cec5SDimitry Andric           if (EliminationStack.empty()) {
39880b57cec5SDimitry Andric             LLVM_DEBUG(dbgs() << "Elimination Stack is empty\n");
39890b57cec5SDimitry Andric           } else {
39900b57cec5SDimitry Andric             LLVM_DEBUG(dbgs() << "Elimination Stack Top DFS numbers are ("
39910b57cec5SDimitry Andric                               << EliminationStack.dfs_back().first << ","
39920b57cec5SDimitry Andric                               << EliminationStack.dfs_back().second << ")\n");
39930b57cec5SDimitry Andric           }
39940b57cec5SDimitry Andric 
39950b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "Current DFS numbers are (" << MemberDFSIn << ","
39960b57cec5SDimitry Andric                             << MemberDFSOut << ")\n");
39970b57cec5SDimitry Andric           // First, we see if we are out of scope or empty.  If so,
39980b57cec5SDimitry Andric           // and there equivalences, we try to replace the top of
39990b57cec5SDimitry Andric           // stack with equivalences (if it's on the stack, it must
40000b57cec5SDimitry Andric           // not have been eliminated yet).
40010b57cec5SDimitry Andric           // Then we synchronize to our current scope, by
40020b57cec5SDimitry Andric           // popping until we are back within a DFS scope that
40030b57cec5SDimitry Andric           // dominates the current member.
40040b57cec5SDimitry Andric           // Then, what happens depends on a few factors
40050b57cec5SDimitry Andric           // If the stack is now empty, we need to push
40060b57cec5SDimitry Andric           // If we have a constant or a local equivalence we want to
40070b57cec5SDimitry Andric           // start using, we also push.
40080b57cec5SDimitry Andric           // Otherwise, we walk along, processing members who are
40090b57cec5SDimitry Andric           // dominated by this scope, and eliminate them.
40100b57cec5SDimitry Andric           bool ShouldPush = Def && EliminationStack.empty();
40110b57cec5SDimitry Andric           bool OutOfScope =
40120b57cec5SDimitry Andric               !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut);
40130b57cec5SDimitry Andric 
40140b57cec5SDimitry Andric           if (OutOfScope || ShouldPush) {
40150b57cec5SDimitry Andric             // Sync to our current scope.
40160b57cec5SDimitry Andric             EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
40170b57cec5SDimitry Andric             bool ShouldPush = Def && EliminationStack.empty();
40180b57cec5SDimitry Andric             if (ShouldPush) {
40190b57cec5SDimitry Andric               EliminationStack.push_back(Def, MemberDFSIn, MemberDFSOut);
40200b57cec5SDimitry Andric             }
40210b57cec5SDimitry Andric           }
40220b57cec5SDimitry Andric 
40230b57cec5SDimitry Andric           // Skip the Def's, we only want to eliminate on their uses.  But mark
40240b57cec5SDimitry Andric           // dominated defs as dead.
40250b57cec5SDimitry Andric           if (Def) {
40260b57cec5SDimitry Andric             // For anything in this case, what and how we value number
4027*0fca6ea1SDimitry Andric             // guarantees that any side-effects that would have occurred (ie
40280b57cec5SDimitry Andric             // throwing, etc) can be proven to either still occur (because it's
40290b57cec5SDimitry Andric             // dominated by something that has the same side-effects), or never
40300b57cec5SDimitry Andric             // occur.  Otherwise, we would not have been able to prove it value
40310b57cec5SDimitry Andric             // equivalent to something else. For these things, we can just mark
40320b57cec5SDimitry Andric             // it all dead.  Note that this is different from the "ProbablyDead"
40330b57cec5SDimitry Andric             // set, which may not be dominated by anything, and thus, are only
40340b57cec5SDimitry Andric             // easy to prove dead if they are also side-effect free. Note that
40350b57cec5SDimitry Andric             // because stores are put in terms of the stored value, we skip
40360b57cec5SDimitry Andric             // stored values here. If the stored value is really dead, it will
40370b57cec5SDimitry Andric             // still be marked for deletion when we process it in its own class.
40385f757f3fSDimitry Andric             auto *DefI = dyn_cast<Instruction>(Def);
40395f757f3fSDimitry Andric             if (!EliminationStack.empty() && DefI && !FromStore) {
40405f757f3fSDimitry Andric               Value *DominatingLeader = EliminationStack.back();
40415f757f3fSDimitry Andric               if (DominatingLeader != Def) {
40425f757f3fSDimitry Andric                 // Even if the instruction is removed, we still need to update
40435f757f3fSDimitry Andric                 // flags/metadata due to downstreams users of the leader.
40445f757f3fSDimitry Andric                 if (!match(DefI, m_Intrinsic<Intrinsic::ssa_copy>()))
40455f757f3fSDimitry Andric                   patchReplacementInstruction(DefI, DominatingLeader);
40465f757f3fSDimitry Andric 
40475f757f3fSDimitry Andric                 markInstructionForDeletion(DefI);
40485f757f3fSDimitry Andric               }
40495f757f3fSDimitry Andric             }
40500b57cec5SDimitry Andric             continue;
40510b57cec5SDimitry Andric           }
40520b57cec5SDimitry Andric           // At this point, we know it is a Use we are trying to possibly
40530b57cec5SDimitry Andric           // replace.
40540b57cec5SDimitry Andric 
40550b57cec5SDimitry Andric           assert(isa<Instruction>(U->get()) &&
40560b57cec5SDimitry Andric                  "Current def should have been an instruction");
40570b57cec5SDimitry Andric           assert(isa<Instruction>(U->getUser()) &&
40580b57cec5SDimitry Andric                  "Current user should have been an instruction");
40590b57cec5SDimitry Andric 
40600b57cec5SDimitry Andric           // If the thing we are replacing into is already marked to be dead,
40610b57cec5SDimitry Andric           // this use is dead.  Note that this is true regardless of whether
40620b57cec5SDimitry Andric           // we have anything dominating the use or not.  We do this here
40630b57cec5SDimitry Andric           // because we are already walking all the uses anyway.
40640b57cec5SDimitry Andric           Instruction *InstUse = cast<Instruction>(U->getUser());
40650b57cec5SDimitry Andric           if (InstructionsToErase.count(InstUse)) {
40660b57cec5SDimitry Andric             auto &UseCount = UseCounts[U->get()];
40670b57cec5SDimitry Andric             if (--UseCount == 0) {
40680b57cec5SDimitry Andric               ProbablyDead.insert(cast<Instruction>(U->get()));
40690b57cec5SDimitry Andric             }
40700b57cec5SDimitry Andric           }
40710b57cec5SDimitry Andric 
40720b57cec5SDimitry Andric           // If we get to this point, and the stack is empty we must have a use
40730b57cec5SDimitry Andric           // with nothing we can use to eliminate this use, so just skip it.
40740b57cec5SDimitry Andric           if (EliminationStack.empty())
40750b57cec5SDimitry Andric             continue;
40760b57cec5SDimitry Andric 
40770b57cec5SDimitry Andric           Value *DominatingLeader = EliminationStack.back();
40780b57cec5SDimitry Andric 
40790b57cec5SDimitry Andric           auto *II = dyn_cast<IntrinsicInst>(DominatingLeader);
40800b57cec5SDimitry Andric           bool isSSACopy = II && II->getIntrinsicID() == Intrinsic::ssa_copy;
40810b57cec5SDimitry Andric           if (isSSACopy)
40820b57cec5SDimitry Andric             DominatingLeader = II->getOperand(0);
40830b57cec5SDimitry Andric 
40840b57cec5SDimitry Andric           // Don't replace our existing users with ourselves.
40850b57cec5SDimitry Andric           if (U->get() == DominatingLeader)
40860b57cec5SDimitry Andric             continue;
40870b57cec5SDimitry Andric           LLVM_DEBUG(dbgs()
40880b57cec5SDimitry Andric                      << "Found replacement " << *DominatingLeader << " for "
40890b57cec5SDimitry Andric                      << *U->get() << " in " << *(U->getUser()) << "\n");
40900b57cec5SDimitry Andric 
40910b57cec5SDimitry Andric           // If we replaced something in an instruction, handle the patching of
40920b57cec5SDimitry Andric           // metadata.  Skip this if we are replacing predicateinfo with its
40930b57cec5SDimitry Andric           // original operand, as we already know we can just drop it.
40940b57cec5SDimitry Andric           auto *ReplacedInst = cast<Instruction>(U->get());
40950b57cec5SDimitry Andric           auto *PI = PredInfo->getPredicateInfoFor(ReplacedInst);
40960b57cec5SDimitry Andric           if (!PI || DominatingLeader != PI->OriginalOp)
40970b57cec5SDimitry Andric             patchReplacementInstruction(ReplacedInst, DominatingLeader);
40980b57cec5SDimitry Andric           U->set(DominatingLeader);
40990b57cec5SDimitry Andric           // This is now a use of the dominating leader, which means if the
41000b57cec5SDimitry Andric           // dominating leader was dead, it's now live!
41010b57cec5SDimitry Andric           auto &LeaderUseCount = UseCounts[DominatingLeader];
41020b57cec5SDimitry Andric           // It's about to be alive again.
41030b57cec5SDimitry Andric           if (LeaderUseCount == 0 && isa<Instruction>(DominatingLeader))
41040b57cec5SDimitry Andric             ProbablyDead.erase(cast<Instruction>(DominatingLeader));
41050b57cec5SDimitry Andric           // For copy instructions, we use their operand as a leader,
41060b57cec5SDimitry Andric           // which means we remove a user of the copy and it may become dead.
41070b57cec5SDimitry Andric           if (isSSACopy) {
41085f757f3fSDimitry Andric             auto It = UseCounts.find(II);
41095f757f3fSDimitry Andric             if (It != UseCounts.end()) {
41105f757f3fSDimitry Andric               unsigned &IIUseCount = It->second;
41110b57cec5SDimitry Andric               if (--IIUseCount == 0)
41120b57cec5SDimitry Andric                 ProbablyDead.insert(II);
41130b57cec5SDimitry Andric             }
41145f757f3fSDimitry Andric           }
41150b57cec5SDimitry Andric           ++LeaderUseCount;
41160b57cec5SDimitry Andric           AnythingReplaced = true;
41170b57cec5SDimitry Andric         }
41180b57cec5SDimitry Andric       }
41190b57cec5SDimitry Andric     }
41200b57cec5SDimitry Andric 
41210b57cec5SDimitry Andric     // At this point, anything still in the ProbablyDead set is actually dead if
41220b57cec5SDimitry Andric     // would be trivially dead.
41230b57cec5SDimitry Andric     for (auto *I : ProbablyDead)
41240b57cec5SDimitry Andric       if (wouldInstructionBeTriviallyDead(I))
41250b57cec5SDimitry Andric         markInstructionForDeletion(I);
41260b57cec5SDimitry Andric 
41270b57cec5SDimitry Andric     // Cleanup the congruence class.
41280b57cec5SDimitry Andric     CongruenceClass::MemberSet MembersLeft;
41290b57cec5SDimitry Andric     for (auto *Member : *CC)
41300b57cec5SDimitry Andric       if (!isa<Instruction>(Member) ||
41310b57cec5SDimitry Andric           !InstructionsToErase.count(cast<Instruction>(Member)))
41320b57cec5SDimitry Andric         MembersLeft.insert(Member);
41330b57cec5SDimitry Andric     CC->swap(MembersLeft);
41340b57cec5SDimitry Andric 
41350b57cec5SDimitry Andric     // If we have possible dead stores to look at, try to eliminate them.
41360b57cec5SDimitry Andric     if (CC->getStoreCount() > 0) {
41370b57cec5SDimitry Andric       convertClassToLoadsAndStores(*CC, PossibleDeadStores);
41380b57cec5SDimitry Andric       llvm::sort(PossibleDeadStores);
41390b57cec5SDimitry Andric       ValueDFSStack EliminationStack;
41400b57cec5SDimitry Andric       for (auto &VD : PossibleDeadStores) {
41410b57cec5SDimitry Andric         int MemberDFSIn = VD.DFSIn;
41420b57cec5SDimitry Andric         int MemberDFSOut = VD.DFSOut;
41430b57cec5SDimitry Andric         Instruction *Member = cast<Instruction>(VD.Def.getPointer());
41440b57cec5SDimitry Andric         if (EliminationStack.empty() ||
41450b57cec5SDimitry Andric             !EliminationStack.isInScope(MemberDFSIn, MemberDFSOut)) {
41460b57cec5SDimitry Andric           // Sync to our current scope.
41470b57cec5SDimitry Andric           EliminationStack.popUntilDFSScope(MemberDFSIn, MemberDFSOut);
41480b57cec5SDimitry Andric           if (EliminationStack.empty()) {
41490b57cec5SDimitry Andric             EliminationStack.push_back(Member, MemberDFSIn, MemberDFSOut);
41500b57cec5SDimitry Andric             continue;
41510b57cec5SDimitry Andric           }
41520b57cec5SDimitry Andric         }
41530b57cec5SDimitry Andric         // We already did load elimination, so nothing to do here.
41540b57cec5SDimitry Andric         if (isa<LoadInst>(Member))
41550b57cec5SDimitry Andric           continue;
41560b57cec5SDimitry Andric         assert(!EliminationStack.empty());
41570b57cec5SDimitry Andric         Instruction *Leader = cast<Instruction>(EliminationStack.back());
41580b57cec5SDimitry Andric         (void)Leader;
41590b57cec5SDimitry Andric         assert(DT->dominates(Leader->getParent(), Member->getParent()));
41600b57cec5SDimitry Andric         // Member is dominater by Leader, and thus dead
41610b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Marking dead store " << *Member
41620b57cec5SDimitry Andric                           << " that is dominated by " << *Leader << "\n");
41630b57cec5SDimitry Andric         markInstructionForDeletion(Member);
41640b57cec5SDimitry Andric         CC->erase(Member);
41650b57cec5SDimitry Andric         ++NumGVNDeadStores;
41660b57cec5SDimitry Andric       }
41670b57cec5SDimitry Andric     }
41680b57cec5SDimitry Andric   }
41690b57cec5SDimitry Andric   return AnythingReplaced;
41700b57cec5SDimitry Andric }
41710b57cec5SDimitry Andric 
41720b57cec5SDimitry Andric // This function provides global ranking of operations so that we can place them
41730b57cec5SDimitry Andric // in a canonical order.  Note that rank alone is not necessarily enough for a
41740b57cec5SDimitry Andric // complete ordering, as constants all have the same rank.  However, generally,
41750b57cec5SDimitry Andric // we will simplify an operation with all constants so that it doesn't matter
41760b57cec5SDimitry Andric // what order they appear in.
getRank(const Value * V) const41770b57cec5SDimitry Andric unsigned int NewGVN::getRank(const Value *V) const {
41780b57cec5SDimitry Andric   // Prefer constants to undef to anything else
41790b57cec5SDimitry Andric   // Undef is a constant, have to check it first.
418004eeddc0SDimitry Andric   // Prefer poison to undef as it's less defined.
41810b57cec5SDimitry Andric   // Prefer smaller constants to constantexprs
418204eeddc0SDimitry Andric   // Note that the order here matters because of class inheritance
41830b57cec5SDimitry Andric   if (isa<ConstantExpr>(V))
418404eeddc0SDimitry Andric     return 3;
418504eeddc0SDimitry Andric   if (isa<PoisonValue>(V))
41860b57cec5SDimitry Andric     return 1;
418704eeddc0SDimitry Andric   if (isa<UndefValue>(V))
418804eeddc0SDimitry Andric     return 2;
41890b57cec5SDimitry Andric   if (isa<Constant>(V))
41900b57cec5SDimitry Andric     return 0;
419104eeddc0SDimitry Andric   if (auto *A = dyn_cast<Argument>(V))
419204eeddc0SDimitry Andric     return 4 + A->getArgNo();
41930b57cec5SDimitry Andric 
419404eeddc0SDimitry Andric   // Need to shift the instruction DFS by number of arguments + 5 to account for
41950b57cec5SDimitry Andric   // the constant and argument ranking above.
41960b57cec5SDimitry Andric   unsigned Result = InstrToDFSNum(V);
41970b57cec5SDimitry Andric   if (Result > 0)
419804eeddc0SDimitry Andric     return 5 + NumFuncArgs + Result;
41990b57cec5SDimitry Andric   // Unreachable or something else, just return a really large number.
42000b57cec5SDimitry Andric   return ~0;
42010b57cec5SDimitry Andric }
42020b57cec5SDimitry Andric 
42030b57cec5SDimitry Andric // This is a function that says whether two commutative operations should
42040b57cec5SDimitry Andric // have their order swapped when canonicalizing.
shouldSwapOperands(const Value * A,const Value * B) const42050b57cec5SDimitry Andric bool NewGVN::shouldSwapOperands(const Value *A, const Value *B) const {
42060b57cec5SDimitry Andric   // Because we only care about a total ordering, and don't rewrite expressions
42070b57cec5SDimitry Andric   // in this order, we order by rank, which will give a strict weak ordering to
42080b57cec5SDimitry Andric   // everything but constants, and then we order by pointer address.
42090b57cec5SDimitry Andric   return std::make_pair(getRank(A), A) > std::make_pair(getRank(B), B);
42100b57cec5SDimitry Andric }
42110b57cec5SDimitry Andric 
shouldSwapOperandsForIntrinsic(const Value * A,const Value * B,const IntrinsicInst * I) const42120eae32dcSDimitry Andric bool NewGVN::shouldSwapOperandsForIntrinsic(const Value *A, const Value *B,
42130eae32dcSDimitry Andric                                             const IntrinsicInst *I) const {
42140eae32dcSDimitry Andric   auto LookupResult = IntrinsicInstPred.find(I);
42150eae32dcSDimitry Andric   if (shouldSwapOperands(A, B)) {
42160eae32dcSDimitry Andric     if (LookupResult == IntrinsicInstPred.end())
42170eae32dcSDimitry Andric       IntrinsicInstPred.insert({I, B});
42180eae32dcSDimitry Andric     else
42190eae32dcSDimitry Andric       LookupResult->second = B;
42200eae32dcSDimitry Andric     return true;
42210eae32dcSDimitry Andric   }
42220eae32dcSDimitry Andric 
42230eae32dcSDimitry Andric   if (LookupResult != IntrinsicInstPred.end()) {
42240eae32dcSDimitry Andric     auto *SeenPredicate = LookupResult->second;
42250eae32dcSDimitry Andric     if (SeenPredicate) {
42260eae32dcSDimitry Andric       if (SeenPredicate == B)
42270eae32dcSDimitry Andric         return true;
42280eae32dcSDimitry Andric       else
42290eae32dcSDimitry Andric         LookupResult->second = nullptr;
42300eae32dcSDimitry Andric     }
42310eae32dcSDimitry Andric   }
42320eae32dcSDimitry Andric   return false;
42330eae32dcSDimitry Andric }
42340eae32dcSDimitry Andric 
run(Function & F,AnalysisManager<Function> & AM)42350b57cec5SDimitry Andric PreservedAnalyses NewGVNPass::run(Function &F, AnalysisManager<Function> &AM) {
42360b57cec5SDimitry Andric   // Apparently the order in which we get these results matter for
42370b57cec5SDimitry Andric   // the old GVN (see Chandler's comment in GVN.cpp). I'll keep
42380b57cec5SDimitry Andric   // the same order here, just in case.
42390b57cec5SDimitry Andric   auto &AC = AM.getResult<AssumptionAnalysis>(F);
42400b57cec5SDimitry Andric   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
42410b57cec5SDimitry Andric   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
42420b57cec5SDimitry Andric   auto &AA = AM.getResult<AAManager>(F);
42430b57cec5SDimitry Andric   auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
42440b57cec5SDimitry Andric   bool Changed =
4245*0fca6ea1SDimitry Andric       NewGVN(F, &DT, &AC, &TLI, &AA, &MSSA, F.getDataLayout())
42460b57cec5SDimitry Andric           .runGVN();
42470b57cec5SDimitry Andric   if (!Changed)
42480b57cec5SDimitry Andric     return PreservedAnalyses::all();
42490b57cec5SDimitry Andric   PreservedAnalyses PA;
42500b57cec5SDimitry Andric   PA.preserve<DominatorTreeAnalysis>();
42510b57cec5SDimitry Andric   return PA;
42520b57cec5SDimitry Andric }
4253