10b57cec5SDimitry Andric //===- GVN.cpp - Eliminate redundant values and loads ---------------------===// 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 // This pass performs global value numbering to eliminate fully redundant 100b57cec5SDimitry Andric // instructions. It also performs simple dead load elimination. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric // Note that this pass does the value numbering itself; it does not use the 130b57cec5SDimitry Andric // ValueNumbering analysis passes. 140b57cec5SDimitry Andric // 150b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "llvm/Transforms/Scalar/GVN.h" 180b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 190b57cec5SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h" 200b57cec5SDimitry Andric #include "llvm/ADT/Hashing.h" 210b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h" 220b57cec5SDimitry Andric #include "llvm/ADT/PostOrderIterator.h" 230b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 240b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 250b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 260b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 270b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 280b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h" 29e8d8bef9SDimitry Andric #include "llvm/Analysis/AssumeBundleQueries.h" 300b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h" 310b57cec5SDimitry Andric #include "llvm/Analysis/CFG.h" 320b57cec5SDimitry Andric #include "llvm/Analysis/DomTreeUpdater.h" 330b57cec5SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h" 3481ad6265SDimitry Andric #include "llvm/Analysis/InstructionPrecedenceTracking.h" 350b57cec5SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h" 36*0fca6ea1SDimitry Andric #include "llvm/Analysis/Loads.h" 370b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h" 380b57cec5SDimitry Andric #include "llvm/Analysis/MemoryBuiltins.h" 390b57cec5SDimitry Andric #include "llvm/Analysis/MemoryDependenceAnalysis.h" 40e8d8bef9SDimitry Andric #include "llvm/Analysis/MemorySSA.h" 41e8d8bef9SDimitry Andric #include "llvm/Analysis/MemorySSAUpdater.h" 420b57cec5SDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h" 430b57cec5SDimitry Andric #include "llvm/Analysis/PHITransAddr.h" 440b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 450b57cec5SDimitry Andric #include "llvm/Analysis/ValueTracking.h" 460b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 470b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 480b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 490b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 500b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 510b57cec5SDimitry Andric #include "llvm/IR/Dominators.h" 520b57cec5SDimitry Andric #include "llvm/IR/Function.h" 530b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h" 540b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 550b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 560b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 570b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 580b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 590b57cec5SDimitry Andric #include "llvm/IR/Module.h" 600b57cec5SDimitry Andric #include "llvm/IR/PassManager.h" 610b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h" 620b57cec5SDimitry Andric #include "llvm/IR/Type.h" 630b57cec5SDimitry Andric #include "llvm/IR/Use.h" 640b57cec5SDimitry Andric #include "llvm/IR/Value.h" 65480093f4SDimitry Andric #include "llvm/InitializePasses.h" 660b57cec5SDimitry Andric #include "llvm/Pass.h" 670b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 680b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 690b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 700b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 710b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 725ffd83dbSDimitry Andric #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" 730b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 740b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Local.h" 750b57cec5SDimitry Andric #include "llvm/Transforms/Utils/SSAUpdater.h" 760b57cec5SDimitry Andric #include "llvm/Transforms/Utils/VNCoercion.h" 770b57cec5SDimitry Andric #include <algorithm> 780b57cec5SDimitry Andric #include <cassert> 790b57cec5SDimitry Andric #include <cstdint> 80bdd1243dSDimitry Andric #include <optional> 810b57cec5SDimitry Andric #include <utility> 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric using namespace llvm; 840b57cec5SDimitry Andric using namespace llvm::gvn; 850b57cec5SDimitry Andric using namespace llvm::VNCoercion; 860b57cec5SDimitry Andric using namespace PatternMatch; 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric #define DEBUG_TYPE "gvn" 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric STATISTIC(NumGVNInstr, "Number of instructions deleted"); 910b57cec5SDimitry Andric STATISTIC(NumGVNLoad, "Number of loads deleted"); 920b57cec5SDimitry Andric STATISTIC(NumGVNPRE, "Number of instructions PRE'd"); 930b57cec5SDimitry Andric STATISTIC(NumGVNBlocks, "Number of blocks merged"); 940b57cec5SDimitry Andric STATISTIC(NumGVNSimpl, "Number of instructions simplified"); 950b57cec5SDimitry Andric STATISTIC(NumGVNEqProp, "Number of equalities propagated"); 960b57cec5SDimitry Andric STATISTIC(NumPRELoad, "Number of loads PRE'd"); 97fe6060f1SDimitry Andric STATISTIC(NumPRELoopLoad, "Number of loop loads PRE'd"); 9806c3fb27SDimitry Andric STATISTIC(NumPRELoadMoved2CEPred, 9906c3fb27SDimitry Andric "Number of loads moved to predecessor of a critical edge in PRE"); 1000b57cec5SDimitry Andric 101e8d8bef9SDimitry Andric STATISTIC(IsValueFullyAvailableInBlockNumSpeculationsMax, 102e8d8bef9SDimitry Andric "Number of blocks speculated as available in " 103e8d8bef9SDimitry Andric "IsValueFullyAvailableInBlock(), max"); 104e8d8bef9SDimitry Andric STATISTIC(MaxBBSpeculationCutoffReachedTimes, 105e8d8bef9SDimitry Andric "Number of times we we reached gvn-max-block-speculations cut-off " 106e8d8bef9SDimitry Andric "preventing further exploration"); 107e8d8bef9SDimitry Andric 1085ffd83dbSDimitry Andric static cl::opt<bool> GVNEnablePRE("enable-pre", cl::init(true), cl::Hidden); 1095ffd83dbSDimitry Andric static cl::opt<bool> GVNEnableLoadPRE("enable-load-pre", cl::init(true)); 1105ffd83dbSDimitry Andric static cl::opt<bool> GVNEnableLoadInLoopPRE("enable-load-in-loop-pre", 1115ffd83dbSDimitry Andric cl::init(true)); 112e8d8bef9SDimitry Andric static cl::opt<bool> 113e8d8bef9SDimitry Andric GVNEnableSplitBackedgeInLoadPRE("enable-split-backedge-in-load-pre", 11481ad6265SDimitry Andric cl::init(false)); 1155ffd83dbSDimitry Andric static cl::opt<bool> GVNEnableMemDep("enable-gvn-memdep", cl::init(true)); 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric static cl::opt<uint32_t> MaxNumDeps( 11881ad6265SDimitry Andric "gvn-max-num-deps", cl::Hidden, cl::init(100), 1190b57cec5SDimitry Andric cl::desc("Max number of dependences to attempt Load PRE (default = 100)")); 1200b57cec5SDimitry Andric 121e8d8bef9SDimitry Andric // This is based on IsValueFullyAvailableInBlockNumSpeculationsMax stat. 122e8d8bef9SDimitry Andric static cl::opt<uint32_t> MaxBBSpeculations( 12381ad6265SDimitry Andric "gvn-max-block-speculations", cl::Hidden, cl::init(600), 124e8d8bef9SDimitry Andric cl::desc("Max number of blocks we're willing to speculate on (and recurse " 125e8d8bef9SDimitry Andric "into) when deducing if a value is fully available or not in GVN " 126e8d8bef9SDimitry Andric "(default = 600)")); 127e8d8bef9SDimitry Andric 128bdd1243dSDimitry Andric static cl::opt<uint32_t> MaxNumVisitedInsts( 129bdd1243dSDimitry Andric "gvn-max-num-visited-insts", cl::Hidden, cl::init(100), 130bdd1243dSDimitry Andric cl::desc("Max number of visited instructions when trying to find " 131bdd1243dSDimitry Andric "dominating value of select dependency (default = 100)")); 132bdd1243dSDimitry Andric 13306c3fb27SDimitry Andric static cl::opt<uint32_t> MaxNumInsnsPerBlock( 13406c3fb27SDimitry Andric "gvn-max-num-insns", cl::Hidden, cl::init(100), 13506c3fb27SDimitry Andric cl::desc("Max number of instructions to scan in each basic block in GVN " 13606c3fb27SDimitry Andric "(default = 100)")); 13706c3fb27SDimitry Andric 138349cc55cSDimitry Andric struct llvm::GVNPass::Expression { 1390b57cec5SDimitry Andric uint32_t opcode; 1400b57cec5SDimitry Andric bool commutative = false; 14181ad6265SDimitry Andric // The type is not necessarily the result type of the expression, it may be 14281ad6265SDimitry Andric // any additional type needed to disambiguate the expression. 1435ffd83dbSDimitry Andric Type *type = nullptr; 1440b57cec5SDimitry Andric SmallVector<uint32_t, 4> varargs; 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric Expression(uint32_t o = ~2U) : opcode(o) {} 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric bool operator==(const Expression &other) const { 1490b57cec5SDimitry Andric if (opcode != other.opcode) 1500b57cec5SDimitry Andric return false; 1510b57cec5SDimitry Andric if (opcode == ~0U || opcode == ~1U) 1520b57cec5SDimitry Andric return true; 1530b57cec5SDimitry Andric if (type != other.type) 1540b57cec5SDimitry Andric return false; 1550b57cec5SDimitry Andric if (varargs != other.varargs) 1560b57cec5SDimitry Andric return false; 1570b57cec5SDimitry Andric return true; 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric friend hash_code hash_value(const Expression &Value) { 1610b57cec5SDimitry Andric return hash_combine( 1620b57cec5SDimitry Andric Value.opcode, Value.type, 1630b57cec5SDimitry Andric hash_combine_range(Value.varargs.begin(), Value.varargs.end())); 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric }; 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric namespace llvm { 1680b57cec5SDimitry Andric 169349cc55cSDimitry Andric template <> struct DenseMapInfo<GVNPass::Expression> { 170349cc55cSDimitry Andric static inline GVNPass::Expression getEmptyKey() { return ~0U; } 171349cc55cSDimitry Andric static inline GVNPass::Expression getTombstoneKey() { return ~1U; } 1720b57cec5SDimitry Andric 173349cc55cSDimitry Andric static unsigned getHashValue(const GVNPass::Expression &e) { 1740b57cec5SDimitry Andric using llvm::hash_value; 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric return static_cast<unsigned>(hash_value(e)); 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric 179349cc55cSDimitry Andric static bool isEqual(const GVNPass::Expression &LHS, 180349cc55cSDimitry Andric const GVNPass::Expression &RHS) { 1810b57cec5SDimitry Andric return LHS == RHS; 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric }; 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric } // end namespace llvm 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric /// Represents a particular available value that we know how to materialize. 1880b57cec5SDimitry Andric /// Materialization of an AvailableValue never fails. An AvailableValue is 1890b57cec5SDimitry Andric /// implicitly associated with a rematerialization point which is the 1900b57cec5SDimitry Andric /// location of the instruction from which it was formed. 1910b57cec5SDimitry Andric struct llvm::gvn::AvailableValue { 19281ad6265SDimitry Andric enum class ValType { 1930b57cec5SDimitry Andric SimpleVal, // A simple offsetted value that is accessed. 1940b57cec5SDimitry Andric LoadVal, // A value produced by a load. 1950b57cec5SDimitry Andric MemIntrin, // A memory intrinsic which is loaded from. 19681ad6265SDimitry Andric UndefVal, // A UndefValue representing a value from dead block (which 1970b57cec5SDimitry Andric // is not yet physically removed from the CFG). 19881ad6265SDimitry Andric SelectVal, // A pointer select which is loaded from and for which the load 19981ad6265SDimitry Andric // can be replace by a value select. 2000b57cec5SDimitry Andric }; 2010b57cec5SDimitry Andric 20281ad6265SDimitry Andric /// Val - The value that is live out of the block. 20381ad6265SDimitry Andric Value *Val; 20481ad6265SDimitry Andric /// Kind of the live-out value. 20581ad6265SDimitry Andric ValType Kind; 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric /// Offset - The byte offset in Val that is interesting for the load query. 208480093f4SDimitry Andric unsigned Offset = 0; 209bdd1243dSDimitry Andric /// V1, V2 - The dominating non-clobbered values of SelectVal. 210bdd1243dSDimitry Andric Value *V1 = nullptr, *V2 = nullptr; 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric static AvailableValue get(Value *V, unsigned Offset = 0) { 2130b57cec5SDimitry Andric AvailableValue Res; 21481ad6265SDimitry Andric Res.Val = V; 21581ad6265SDimitry Andric Res.Kind = ValType::SimpleVal; 2160b57cec5SDimitry Andric Res.Offset = Offset; 2170b57cec5SDimitry Andric return Res; 2180b57cec5SDimitry Andric } 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric static AvailableValue getMI(MemIntrinsic *MI, unsigned Offset = 0) { 2210b57cec5SDimitry Andric AvailableValue Res; 22281ad6265SDimitry Andric Res.Val = MI; 22381ad6265SDimitry Andric Res.Kind = ValType::MemIntrin; 2240b57cec5SDimitry Andric Res.Offset = Offset; 2250b57cec5SDimitry Andric return Res; 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric 228fe6060f1SDimitry Andric static AvailableValue getLoad(LoadInst *Load, unsigned Offset = 0) { 2290b57cec5SDimitry Andric AvailableValue Res; 23081ad6265SDimitry Andric Res.Val = Load; 23181ad6265SDimitry Andric Res.Kind = ValType::LoadVal; 2320b57cec5SDimitry Andric Res.Offset = Offset; 2330b57cec5SDimitry Andric return Res; 2340b57cec5SDimitry Andric } 2350b57cec5SDimitry Andric 2360b57cec5SDimitry Andric static AvailableValue getUndef() { 2370b57cec5SDimitry Andric AvailableValue Res; 23881ad6265SDimitry Andric Res.Val = nullptr; 23981ad6265SDimitry Andric Res.Kind = ValType::UndefVal; 2400b57cec5SDimitry Andric Res.Offset = 0; 2410b57cec5SDimitry Andric return Res; 2420b57cec5SDimitry Andric } 2430b57cec5SDimitry Andric 244bdd1243dSDimitry Andric static AvailableValue getSelect(SelectInst *Sel, Value *V1, Value *V2) { 24581ad6265SDimitry Andric AvailableValue Res; 24681ad6265SDimitry Andric Res.Val = Sel; 24781ad6265SDimitry Andric Res.Kind = ValType::SelectVal; 24881ad6265SDimitry Andric Res.Offset = 0; 249bdd1243dSDimitry Andric Res.V1 = V1; 250bdd1243dSDimitry Andric Res.V2 = V2; 25181ad6265SDimitry Andric return Res; 25281ad6265SDimitry Andric } 25381ad6265SDimitry Andric 25481ad6265SDimitry Andric bool isSimpleValue() const { return Kind == ValType::SimpleVal; } 25581ad6265SDimitry Andric bool isCoercedLoadValue() const { return Kind == ValType::LoadVal; } 25681ad6265SDimitry Andric bool isMemIntrinValue() const { return Kind == ValType::MemIntrin; } 25781ad6265SDimitry Andric bool isUndefValue() const { return Kind == ValType::UndefVal; } 25881ad6265SDimitry Andric bool isSelectValue() const { return Kind == ValType::SelectVal; } 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric Value *getSimpleValue() const { 2610b57cec5SDimitry Andric assert(isSimpleValue() && "Wrong accessor"); 26281ad6265SDimitry Andric return Val; 2630b57cec5SDimitry Andric } 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric LoadInst *getCoercedLoadValue() const { 2660b57cec5SDimitry Andric assert(isCoercedLoadValue() && "Wrong accessor"); 26781ad6265SDimitry Andric return cast<LoadInst>(Val); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric MemIntrinsic *getMemIntrinValue() const { 2710b57cec5SDimitry Andric assert(isMemIntrinValue() && "Wrong accessor"); 27281ad6265SDimitry Andric return cast<MemIntrinsic>(Val); 27381ad6265SDimitry Andric } 27481ad6265SDimitry Andric 27581ad6265SDimitry Andric SelectInst *getSelectValue() const { 27681ad6265SDimitry Andric assert(isSelectValue() && "Wrong accessor"); 27781ad6265SDimitry Andric return cast<SelectInst>(Val); 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric /// Emit code at the specified insertion point to adjust the value defined 2810b57cec5SDimitry Andric /// here to the specified type. This handles various coercion cases. 282fe6060f1SDimitry Andric Value *MaterializeAdjustedValue(LoadInst *Load, Instruction *InsertPt, 283349cc55cSDimitry Andric GVNPass &gvn) const; 2840b57cec5SDimitry Andric }; 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric /// Represents an AvailableValue which can be rematerialized at the end of 2870b57cec5SDimitry Andric /// the associated BasicBlock. 2880b57cec5SDimitry Andric struct llvm::gvn::AvailableValueInBlock { 2890b57cec5SDimitry Andric /// BB - The basic block in question. 290480093f4SDimitry Andric BasicBlock *BB = nullptr; 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric /// AV - The actual available value 2930b57cec5SDimitry Andric AvailableValue AV; 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric static AvailableValueInBlock get(BasicBlock *BB, AvailableValue &&AV) { 2960b57cec5SDimitry Andric AvailableValueInBlock Res; 2970b57cec5SDimitry Andric Res.BB = BB; 2980b57cec5SDimitry Andric Res.AV = std::move(AV); 2990b57cec5SDimitry Andric return Res; 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric static AvailableValueInBlock get(BasicBlock *BB, Value *V, 3030b57cec5SDimitry Andric unsigned Offset = 0) { 3040b57cec5SDimitry Andric return get(BB, AvailableValue::get(V, Offset)); 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric static AvailableValueInBlock getUndef(BasicBlock *BB) { 3080b57cec5SDimitry Andric return get(BB, AvailableValue::getUndef()); 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric 311bdd1243dSDimitry Andric static AvailableValueInBlock getSelect(BasicBlock *BB, SelectInst *Sel, 312bdd1243dSDimitry Andric Value *V1, Value *V2) { 313bdd1243dSDimitry Andric return get(BB, AvailableValue::getSelect(Sel, V1, V2)); 31481ad6265SDimitry Andric } 31581ad6265SDimitry Andric 3160b57cec5SDimitry Andric /// Emit code at the end of this block to adjust the value defined here to 3170b57cec5SDimitry Andric /// the specified type. This handles various coercion cases. 318349cc55cSDimitry Andric Value *MaterializeAdjustedValue(LoadInst *Load, GVNPass &gvn) const { 319fe6060f1SDimitry Andric return AV.MaterializeAdjustedValue(Load, BB->getTerminator(), gvn); 3200b57cec5SDimitry Andric } 3210b57cec5SDimitry Andric }; 3220b57cec5SDimitry Andric 3230b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3240b57cec5SDimitry Andric // ValueTable Internal Functions 3250b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3260b57cec5SDimitry Andric 327349cc55cSDimitry Andric GVNPass::Expression GVNPass::ValueTable::createExpr(Instruction *I) { 3280b57cec5SDimitry Andric Expression e; 3290b57cec5SDimitry Andric e.type = I->getType(); 3300b57cec5SDimitry Andric e.opcode = I->getOpcode(); 331fe6060f1SDimitry Andric if (const GCRelocateInst *GCR = dyn_cast<GCRelocateInst>(I)) { 332fe6060f1SDimitry Andric // gc.relocate is 'special' call: its second and third operands are 333fe6060f1SDimitry Andric // not real values, but indices into statepoint's argument list. 334fe6060f1SDimitry Andric // Use the refered to values for purposes of identity. 335fe6060f1SDimitry Andric e.varargs.push_back(lookupOrAdd(GCR->getOperand(0))); 336fe6060f1SDimitry Andric e.varargs.push_back(lookupOrAdd(GCR->getBasePtr())); 337fe6060f1SDimitry Andric e.varargs.push_back(lookupOrAdd(GCR->getDerivedPtr())); 338fe6060f1SDimitry Andric } else { 339fe6060f1SDimitry Andric for (Use &Op : I->operands()) 340fe6060f1SDimitry Andric e.varargs.push_back(lookupOrAdd(Op)); 341fe6060f1SDimitry Andric } 3420b57cec5SDimitry Andric if (I->isCommutative()) { 3430b57cec5SDimitry Andric // Ensure that commutative instructions that only differ by a permutation 3440b57cec5SDimitry Andric // of their operands get the same value number by sorting the operand value 345e8d8bef9SDimitry Andric // numbers. Since commutative operands are the 1st two operands it is more 3460b57cec5SDimitry Andric // efficient to sort by hand rather than using, say, std::sort. 347e8d8bef9SDimitry Andric assert(I->getNumOperands() >= 2 && "Unsupported commutative instruction!"); 3480b57cec5SDimitry Andric if (e.varargs[0] > e.varargs[1]) 3490b57cec5SDimitry Andric std::swap(e.varargs[0], e.varargs[1]); 3500b57cec5SDimitry Andric e.commutative = true; 3510b57cec5SDimitry Andric } 3520b57cec5SDimitry Andric 3535ffd83dbSDimitry Andric if (auto *C = dyn_cast<CmpInst>(I)) { 3540b57cec5SDimitry Andric // Sort the operand value numbers so x<y and y>x get the same value number. 3550b57cec5SDimitry Andric CmpInst::Predicate Predicate = C->getPredicate(); 3560b57cec5SDimitry Andric if (e.varargs[0] > e.varargs[1]) { 3570b57cec5SDimitry Andric std::swap(e.varargs[0], e.varargs[1]); 3580b57cec5SDimitry Andric Predicate = CmpInst::getSwappedPredicate(Predicate); 3590b57cec5SDimitry Andric } 3600b57cec5SDimitry Andric e.opcode = (C->getOpcode() << 8) | Predicate; 3610b57cec5SDimitry Andric e.commutative = true; 3625ffd83dbSDimitry Andric } else if (auto *E = dyn_cast<InsertValueInst>(I)) { 3635ffd83dbSDimitry Andric e.varargs.append(E->idx_begin(), E->idx_end()); 3645ffd83dbSDimitry Andric } else if (auto *SVI = dyn_cast<ShuffleVectorInst>(I)) { 3655ffd83dbSDimitry Andric ArrayRef<int> ShuffleMask = SVI->getShuffleMask(); 3665ffd83dbSDimitry Andric e.varargs.append(ShuffleMask.begin(), ShuffleMask.end()); 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric return e; 3700b57cec5SDimitry Andric } 3710b57cec5SDimitry Andric 372349cc55cSDimitry Andric GVNPass::Expression GVNPass::ValueTable::createCmpExpr( 373349cc55cSDimitry Andric unsigned Opcode, CmpInst::Predicate Predicate, Value *LHS, Value *RHS) { 3740b57cec5SDimitry Andric assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) && 3750b57cec5SDimitry Andric "Not a comparison!"); 3760b57cec5SDimitry Andric Expression e; 3770b57cec5SDimitry Andric e.type = CmpInst::makeCmpResultType(LHS->getType()); 3780b57cec5SDimitry Andric e.varargs.push_back(lookupOrAdd(LHS)); 3790b57cec5SDimitry Andric e.varargs.push_back(lookupOrAdd(RHS)); 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric // Sort the operand value numbers so x<y and y>x get the same value number. 3820b57cec5SDimitry Andric if (e.varargs[0] > e.varargs[1]) { 3830b57cec5SDimitry Andric std::swap(e.varargs[0], e.varargs[1]); 3840b57cec5SDimitry Andric Predicate = CmpInst::getSwappedPredicate(Predicate); 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric e.opcode = (Opcode << 8) | Predicate; 3870b57cec5SDimitry Andric e.commutative = true; 3880b57cec5SDimitry Andric return e; 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 391349cc55cSDimitry Andric GVNPass::Expression 392349cc55cSDimitry Andric GVNPass::ValueTable::createExtractvalueExpr(ExtractValueInst *EI) { 3930b57cec5SDimitry Andric assert(EI && "Not an ExtractValueInst?"); 3940b57cec5SDimitry Andric Expression e; 3950b57cec5SDimitry Andric e.type = EI->getType(); 3960b57cec5SDimitry Andric e.opcode = 0; 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric WithOverflowInst *WO = dyn_cast<WithOverflowInst>(EI->getAggregateOperand()); 3990b57cec5SDimitry Andric if (WO != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0) { 4000b57cec5SDimitry Andric // EI is an extract from one of our with.overflow intrinsics. Synthesize 4010b57cec5SDimitry Andric // a semantically equivalent expression instead of an extract value 4020b57cec5SDimitry Andric // expression. 4030b57cec5SDimitry Andric e.opcode = WO->getBinaryOp(); 4040b57cec5SDimitry Andric e.varargs.push_back(lookupOrAdd(WO->getLHS())); 4050b57cec5SDimitry Andric e.varargs.push_back(lookupOrAdd(WO->getRHS())); 4060b57cec5SDimitry Andric return e; 4070b57cec5SDimitry Andric } 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric // Not a recognised intrinsic. Fall back to producing an extract value 4100b57cec5SDimitry Andric // expression. 4110b57cec5SDimitry Andric e.opcode = EI->getOpcode(); 412fe6060f1SDimitry Andric for (Use &Op : EI->operands()) 413fe6060f1SDimitry Andric e.varargs.push_back(lookupOrAdd(Op)); 4140b57cec5SDimitry Andric 415e8d8bef9SDimitry Andric append_range(e.varargs, EI->indices()); 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric return e; 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 42081ad6265SDimitry Andric GVNPass::Expression GVNPass::ValueTable::createGEPExpr(GetElementPtrInst *GEP) { 42181ad6265SDimitry Andric Expression E; 42281ad6265SDimitry Andric Type *PtrTy = GEP->getType()->getScalarType(); 423*0fca6ea1SDimitry Andric const DataLayout &DL = GEP->getDataLayout(); 42481ad6265SDimitry Andric unsigned BitWidth = DL.getIndexTypeSizeInBits(PtrTy); 42581ad6265SDimitry Andric MapVector<Value *, APInt> VariableOffsets; 42681ad6265SDimitry Andric APInt ConstantOffset(BitWidth, 0); 42706c3fb27SDimitry Andric if (GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) { 42806c3fb27SDimitry Andric // Convert into offset representation, to recognize equivalent address 42906c3fb27SDimitry Andric // calculations that use different type encoding. 43081ad6265SDimitry Andric LLVMContext &Context = GEP->getContext(); 43181ad6265SDimitry Andric E.opcode = GEP->getOpcode(); 43281ad6265SDimitry Andric E.type = nullptr; 43381ad6265SDimitry Andric E.varargs.push_back(lookupOrAdd(GEP->getPointerOperand())); 43481ad6265SDimitry Andric for (const auto &Pair : VariableOffsets) { 43581ad6265SDimitry Andric E.varargs.push_back(lookupOrAdd(Pair.first)); 43681ad6265SDimitry Andric E.varargs.push_back(lookupOrAdd(ConstantInt::get(Context, Pair.second))); 43781ad6265SDimitry Andric } 43881ad6265SDimitry Andric if (!ConstantOffset.isZero()) 43981ad6265SDimitry Andric E.varargs.push_back( 44081ad6265SDimitry Andric lookupOrAdd(ConstantInt::get(Context, ConstantOffset))); 44181ad6265SDimitry Andric } else { 44206c3fb27SDimitry Andric // If converting to offset representation fails (for scalable vectors), 44306c3fb27SDimitry Andric // fall back to type-based implementation: 44481ad6265SDimitry Andric E.opcode = GEP->getOpcode(); 44581ad6265SDimitry Andric E.type = GEP->getSourceElementType(); 44681ad6265SDimitry Andric for (Use &Op : GEP->operands()) 44781ad6265SDimitry Andric E.varargs.push_back(lookupOrAdd(Op)); 44881ad6265SDimitry Andric } 44981ad6265SDimitry Andric return E; 45081ad6265SDimitry Andric } 45181ad6265SDimitry Andric 4520b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4530b57cec5SDimitry Andric // ValueTable External Functions 4540b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 4550b57cec5SDimitry Andric 456349cc55cSDimitry Andric GVNPass::ValueTable::ValueTable() = default; 457349cc55cSDimitry Andric GVNPass::ValueTable::ValueTable(const ValueTable &) = default; 458349cc55cSDimitry Andric GVNPass::ValueTable::ValueTable(ValueTable &&) = default; 459349cc55cSDimitry Andric GVNPass::ValueTable::~ValueTable() = default; 460349cc55cSDimitry Andric GVNPass::ValueTable & 461349cc55cSDimitry Andric GVNPass::ValueTable::operator=(const GVNPass::ValueTable &Arg) = default; 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric /// add - Insert a value into the table with a specified value number. 464349cc55cSDimitry Andric void GVNPass::ValueTable::add(Value *V, uint32_t num) { 4650b57cec5SDimitry Andric valueNumbering.insert(std::make_pair(V, num)); 4660b57cec5SDimitry Andric if (PHINode *PN = dyn_cast<PHINode>(V)) 4670b57cec5SDimitry Andric NumberingPhi[num] = PN; 4680b57cec5SDimitry Andric } 4690b57cec5SDimitry Andric 470349cc55cSDimitry Andric uint32_t GVNPass::ValueTable::lookupOrAddCall(CallInst *C) { 471bdd1243dSDimitry Andric // FIXME: Currently the calls which may access the thread id may 472bdd1243dSDimitry Andric // be considered as not accessing the memory. But this is 473bdd1243dSDimitry Andric // problematic for coroutines, since coroutines may resume in a 474bdd1243dSDimitry Andric // different thread. So we disable the optimization here for the 475bdd1243dSDimitry Andric // correctness. However, it may block many other correct 476bdd1243dSDimitry Andric // optimizations. Revert this one when we detect the memory 477bdd1243dSDimitry Andric // accessing kind more precisely. 47806c3fb27SDimitry Andric if (C->getFunction()->isPresplitCoroutine()) { 47906c3fb27SDimitry Andric valueNumbering[C] = nextValueNumber; 48006c3fb27SDimitry Andric return nextValueNumber++; 48106c3fb27SDimitry Andric } 48206c3fb27SDimitry Andric 48306c3fb27SDimitry Andric // Do not combine convergent calls since they implicitly depend on the set of 48406c3fb27SDimitry Andric // threads that is currently executing, and they might be in different basic 48506c3fb27SDimitry Andric // blocks. 48606c3fb27SDimitry Andric if (C->isConvergent()) { 48706c3fb27SDimitry Andric valueNumbering[C] = nextValueNumber; 48806c3fb27SDimitry Andric return nextValueNumber++; 48906c3fb27SDimitry Andric } 49006c3fb27SDimitry Andric 49106c3fb27SDimitry Andric if (AA->doesNotAccessMemory(C)) { 4920b57cec5SDimitry Andric Expression exp = createExpr(C); 4930b57cec5SDimitry Andric uint32_t e = assignExpNewValueNum(exp).first; 4940b57cec5SDimitry Andric valueNumbering[C] = e; 4950b57cec5SDimitry Andric return e; 49606c3fb27SDimitry Andric } 49706c3fb27SDimitry Andric 49806c3fb27SDimitry Andric if (MD && AA->onlyReadsMemory(C)) { 4990b57cec5SDimitry Andric Expression exp = createExpr(C); 5000b57cec5SDimitry Andric auto ValNum = assignExpNewValueNum(exp); 5010b57cec5SDimitry Andric if (ValNum.second) { 5020b57cec5SDimitry Andric valueNumbering[C] = ValNum.first; 5030b57cec5SDimitry Andric return ValNum.first; 5040b57cec5SDimitry Andric } 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric MemDepResult local_dep = MD->getDependency(C); 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric if (!local_dep.isDef() && !local_dep.isNonLocal()) { 5090b57cec5SDimitry Andric valueNumbering[C] = nextValueNumber; 5100b57cec5SDimitry Andric return nextValueNumber++; 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric if (local_dep.isDef()) { 514bdd1243dSDimitry Andric // For masked load/store intrinsics, the local_dep may actually be 515e8d8bef9SDimitry Andric // a normal load or store instruction. 516e8d8bef9SDimitry Andric CallInst *local_cdep = dyn_cast<CallInst>(local_dep.getInst()); 5170b57cec5SDimitry Andric 518349cc55cSDimitry Andric if (!local_cdep || local_cdep->arg_size() != C->arg_size()) { 5190b57cec5SDimitry Andric valueNumbering[C] = nextValueNumber; 5200b57cec5SDimitry Andric return nextValueNumber++; 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 523349cc55cSDimitry Andric for (unsigned i = 0, e = C->arg_size(); i < e; ++i) { 5240b57cec5SDimitry Andric uint32_t c_vn = lookupOrAdd(C->getArgOperand(i)); 5250b57cec5SDimitry Andric uint32_t cd_vn = lookupOrAdd(local_cdep->getArgOperand(i)); 5260b57cec5SDimitry Andric if (c_vn != cd_vn) { 5270b57cec5SDimitry Andric valueNumbering[C] = nextValueNumber; 5280b57cec5SDimitry Andric return nextValueNumber++; 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric } 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric uint32_t v = lookupOrAdd(local_cdep); 5330b57cec5SDimitry Andric valueNumbering[C] = v; 5340b57cec5SDimitry Andric return v; 5350b57cec5SDimitry Andric } 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric // Non-local case. 5380b57cec5SDimitry Andric const MemoryDependenceResults::NonLocalDepInfo &deps = 5390b57cec5SDimitry Andric MD->getNonLocalCallDependency(C); 5400b57cec5SDimitry Andric // FIXME: Move the checking logic to MemDep! 5410b57cec5SDimitry Andric CallInst* cdep = nullptr; 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric // Check to see if we have a single dominating call instruction that is 5440b57cec5SDimitry Andric // identical to C. 545bdd1243dSDimitry Andric for (const NonLocalDepEntry &I : deps) { 546bdd1243dSDimitry Andric if (I.getResult().isNonLocal()) 5470b57cec5SDimitry Andric continue; 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric // We don't handle non-definitions. If we already have a call, reject 5500b57cec5SDimitry Andric // instruction dependencies. 551bdd1243dSDimitry Andric if (!I.getResult().isDef() || cdep != nullptr) { 5520b57cec5SDimitry Andric cdep = nullptr; 5530b57cec5SDimitry Andric break; 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric 556bdd1243dSDimitry Andric CallInst *NonLocalDepCall = dyn_cast<CallInst>(I.getResult().getInst()); 5570b57cec5SDimitry Andric // FIXME: All duplicated with non-local case. 558bdd1243dSDimitry Andric if (NonLocalDepCall && DT->properlyDominates(I.getBB(), C->getParent())) { 5590b57cec5SDimitry Andric cdep = NonLocalDepCall; 5600b57cec5SDimitry Andric continue; 5610b57cec5SDimitry Andric } 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric cdep = nullptr; 5640b57cec5SDimitry Andric break; 5650b57cec5SDimitry Andric } 5660b57cec5SDimitry Andric 5670b57cec5SDimitry Andric if (!cdep) { 5680b57cec5SDimitry Andric valueNumbering[C] = nextValueNumber; 5690b57cec5SDimitry Andric return nextValueNumber++; 5700b57cec5SDimitry Andric } 5710b57cec5SDimitry Andric 572349cc55cSDimitry Andric if (cdep->arg_size() != C->arg_size()) { 5730b57cec5SDimitry Andric valueNumbering[C] = nextValueNumber; 5740b57cec5SDimitry Andric return nextValueNumber++; 5750b57cec5SDimitry Andric } 576349cc55cSDimitry Andric for (unsigned i = 0, e = C->arg_size(); i < e; ++i) { 5770b57cec5SDimitry Andric uint32_t c_vn = lookupOrAdd(C->getArgOperand(i)); 5780b57cec5SDimitry Andric uint32_t cd_vn = lookupOrAdd(cdep->getArgOperand(i)); 5790b57cec5SDimitry Andric if (c_vn != cd_vn) { 5800b57cec5SDimitry Andric valueNumbering[C] = nextValueNumber; 5810b57cec5SDimitry Andric return nextValueNumber++; 5820b57cec5SDimitry Andric } 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric 5850b57cec5SDimitry Andric uint32_t v = lookupOrAdd(cdep); 5860b57cec5SDimitry Andric valueNumbering[C] = v; 5870b57cec5SDimitry Andric return v; 58806c3fb27SDimitry Andric } 58906c3fb27SDimitry Andric 5900b57cec5SDimitry Andric valueNumbering[C] = nextValueNumber; 5910b57cec5SDimitry Andric return nextValueNumber++; 5920b57cec5SDimitry Andric } 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric /// Returns true if a value number exists for the specified value. 595349cc55cSDimitry Andric bool GVNPass::ValueTable::exists(Value *V) const { 596cb14a3feSDimitry Andric return valueNumbering.contains(V); 597349cc55cSDimitry Andric } 5980b57cec5SDimitry Andric 5990b57cec5SDimitry Andric /// lookup_or_add - Returns the value number for the specified value, assigning 6000b57cec5SDimitry Andric /// it a new number if it did not have one before. 601349cc55cSDimitry Andric uint32_t GVNPass::ValueTable::lookupOrAdd(Value *V) { 6020b57cec5SDimitry Andric DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V); 6030b57cec5SDimitry Andric if (VI != valueNumbering.end()) 6040b57cec5SDimitry Andric return VI->second; 6050b57cec5SDimitry Andric 606bdd1243dSDimitry Andric auto *I = dyn_cast<Instruction>(V); 607bdd1243dSDimitry Andric if (!I) { 6080b57cec5SDimitry Andric valueNumbering[V] = nextValueNumber; 6090b57cec5SDimitry Andric return nextValueNumber++; 6100b57cec5SDimitry Andric } 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric Expression exp; 6130b57cec5SDimitry Andric switch (I->getOpcode()) { 6140b57cec5SDimitry Andric case Instruction::Call: 6150b57cec5SDimitry Andric return lookupOrAddCall(cast<CallInst>(I)); 6160b57cec5SDimitry Andric case Instruction::FNeg: 6170b57cec5SDimitry Andric case Instruction::Add: 6180b57cec5SDimitry Andric case Instruction::FAdd: 6190b57cec5SDimitry Andric case Instruction::Sub: 6200b57cec5SDimitry Andric case Instruction::FSub: 6210b57cec5SDimitry Andric case Instruction::Mul: 6220b57cec5SDimitry Andric case Instruction::FMul: 6230b57cec5SDimitry Andric case Instruction::UDiv: 6240b57cec5SDimitry Andric case Instruction::SDiv: 6250b57cec5SDimitry Andric case Instruction::FDiv: 6260b57cec5SDimitry Andric case Instruction::URem: 6270b57cec5SDimitry Andric case Instruction::SRem: 6280b57cec5SDimitry Andric case Instruction::FRem: 6290b57cec5SDimitry Andric case Instruction::Shl: 6300b57cec5SDimitry Andric case Instruction::LShr: 6310b57cec5SDimitry Andric case Instruction::AShr: 6320b57cec5SDimitry Andric case Instruction::And: 6330b57cec5SDimitry Andric case Instruction::Or: 6340b57cec5SDimitry Andric case Instruction::Xor: 6350b57cec5SDimitry Andric case Instruction::ICmp: 6360b57cec5SDimitry Andric case Instruction::FCmp: 6370b57cec5SDimitry Andric case Instruction::Trunc: 6380b57cec5SDimitry Andric case Instruction::ZExt: 6390b57cec5SDimitry Andric case Instruction::SExt: 6400b57cec5SDimitry Andric case Instruction::FPToUI: 6410b57cec5SDimitry Andric case Instruction::FPToSI: 6420b57cec5SDimitry Andric case Instruction::UIToFP: 6430b57cec5SDimitry Andric case Instruction::SIToFP: 6440b57cec5SDimitry Andric case Instruction::FPTrunc: 6450b57cec5SDimitry Andric case Instruction::FPExt: 6460b57cec5SDimitry Andric case Instruction::PtrToInt: 6470b57cec5SDimitry Andric case Instruction::IntToPtr: 6480b57cec5SDimitry Andric case Instruction::AddrSpaceCast: 6490b57cec5SDimitry Andric case Instruction::BitCast: 6500b57cec5SDimitry Andric case Instruction::Select: 6515ffd83dbSDimitry Andric case Instruction::Freeze: 6520b57cec5SDimitry Andric case Instruction::ExtractElement: 6530b57cec5SDimitry Andric case Instruction::InsertElement: 6540b57cec5SDimitry Andric case Instruction::ShuffleVector: 6550b57cec5SDimitry Andric case Instruction::InsertValue: 6560b57cec5SDimitry Andric exp = createExpr(I); 6570b57cec5SDimitry Andric break; 65881ad6265SDimitry Andric case Instruction::GetElementPtr: 65981ad6265SDimitry Andric exp = createGEPExpr(cast<GetElementPtrInst>(I)); 66081ad6265SDimitry Andric break; 6610b57cec5SDimitry Andric case Instruction::ExtractValue: 6620b57cec5SDimitry Andric exp = createExtractvalueExpr(cast<ExtractValueInst>(I)); 6630b57cec5SDimitry Andric break; 6640b57cec5SDimitry Andric case Instruction::PHI: 6650b57cec5SDimitry Andric valueNumbering[V] = nextValueNumber; 6660b57cec5SDimitry Andric NumberingPhi[nextValueNumber] = cast<PHINode>(V); 6670b57cec5SDimitry Andric return nextValueNumber++; 6680b57cec5SDimitry Andric default: 6690b57cec5SDimitry Andric valueNumbering[V] = nextValueNumber; 6700b57cec5SDimitry Andric return nextValueNumber++; 6710b57cec5SDimitry Andric } 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric uint32_t e = assignExpNewValueNum(exp).first; 6740b57cec5SDimitry Andric valueNumbering[V] = e; 6750b57cec5SDimitry Andric return e; 6760b57cec5SDimitry Andric } 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric /// Returns the value number of the specified value. Fails if 6790b57cec5SDimitry Andric /// the value has not yet been numbered. 680349cc55cSDimitry Andric uint32_t GVNPass::ValueTable::lookup(Value *V, bool Verify) const { 6810b57cec5SDimitry Andric DenseMap<Value*, uint32_t>::const_iterator VI = valueNumbering.find(V); 6820b57cec5SDimitry Andric if (Verify) { 6830b57cec5SDimitry Andric assert(VI != valueNumbering.end() && "Value not numbered?"); 6840b57cec5SDimitry Andric return VI->second; 6850b57cec5SDimitry Andric } 6860b57cec5SDimitry Andric return (VI != valueNumbering.end()) ? VI->second : 0; 6870b57cec5SDimitry Andric } 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric /// Returns the value number of the given comparison, 6900b57cec5SDimitry Andric /// assigning it a new number if it did not have one before. Useful when 6910b57cec5SDimitry Andric /// we deduced the result of a comparison, but don't immediately have an 6920b57cec5SDimitry Andric /// instruction realizing that comparison to hand. 693349cc55cSDimitry Andric uint32_t GVNPass::ValueTable::lookupOrAddCmp(unsigned Opcode, 6940b57cec5SDimitry Andric CmpInst::Predicate Predicate, 6950b57cec5SDimitry Andric Value *LHS, Value *RHS) { 6960b57cec5SDimitry Andric Expression exp = createCmpExpr(Opcode, Predicate, LHS, RHS); 6970b57cec5SDimitry Andric return assignExpNewValueNum(exp).first; 6980b57cec5SDimitry Andric } 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric /// Remove all entries from the ValueTable. 701349cc55cSDimitry Andric void GVNPass::ValueTable::clear() { 7020b57cec5SDimitry Andric valueNumbering.clear(); 7030b57cec5SDimitry Andric expressionNumbering.clear(); 7040b57cec5SDimitry Andric NumberingPhi.clear(); 7050b57cec5SDimitry Andric PhiTranslateTable.clear(); 7060b57cec5SDimitry Andric nextValueNumber = 1; 7070b57cec5SDimitry Andric Expressions.clear(); 7080b57cec5SDimitry Andric ExprIdx.clear(); 7090b57cec5SDimitry Andric nextExprNumber = 0; 7100b57cec5SDimitry Andric } 7110b57cec5SDimitry Andric 7120b57cec5SDimitry Andric /// Remove a value from the value numbering. 713349cc55cSDimitry Andric void GVNPass::ValueTable::erase(Value *V) { 7140b57cec5SDimitry Andric uint32_t Num = valueNumbering.lookup(V); 7150b57cec5SDimitry Andric valueNumbering.erase(V); 7160b57cec5SDimitry Andric // If V is PHINode, V <--> value number is an one-to-one mapping. 7170b57cec5SDimitry Andric if (isa<PHINode>(V)) 7180b57cec5SDimitry Andric NumberingPhi.erase(Num); 7190b57cec5SDimitry Andric } 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andric /// verifyRemoved - Verify that the value is removed from all internal data 7220b57cec5SDimitry Andric /// structures. 723349cc55cSDimitry Andric void GVNPass::ValueTable::verifyRemoved(const Value *V) const { 72406c3fb27SDimitry Andric assert(!valueNumbering.contains(V) && 72506c3fb27SDimitry Andric "Inst still occurs in value numbering map!"); 7260b57cec5SDimitry Andric } 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 729*0fca6ea1SDimitry Andric // LeaderMap External Functions 730*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===// 731*0fca6ea1SDimitry Andric 732*0fca6ea1SDimitry Andric /// Push a new Value to the LeaderTable onto the list for its value number. 733*0fca6ea1SDimitry Andric void GVNPass::LeaderMap::insert(uint32_t N, Value *V, const BasicBlock *BB) { 734*0fca6ea1SDimitry Andric LeaderListNode &Curr = NumToLeaders[N]; 735*0fca6ea1SDimitry Andric if (!Curr.Entry.Val) { 736*0fca6ea1SDimitry Andric Curr.Entry.Val = V; 737*0fca6ea1SDimitry Andric Curr.Entry.BB = BB; 738*0fca6ea1SDimitry Andric return; 739*0fca6ea1SDimitry Andric } 740*0fca6ea1SDimitry Andric 741*0fca6ea1SDimitry Andric LeaderListNode *Node = TableAllocator.Allocate<LeaderListNode>(); 742*0fca6ea1SDimitry Andric Node->Entry.Val = V; 743*0fca6ea1SDimitry Andric Node->Entry.BB = BB; 744*0fca6ea1SDimitry Andric Node->Next = Curr.Next; 745*0fca6ea1SDimitry Andric Curr.Next = Node; 746*0fca6ea1SDimitry Andric } 747*0fca6ea1SDimitry Andric 748*0fca6ea1SDimitry Andric /// Scan the list of values corresponding to a given 749*0fca6ea1SDimitry Andric /// value number, and remove the given instruction if encountered. 750*0fca6ea1SDimitry Andric void GVNPass::LeaderMap::erase(uint32_t N, Instruction *I, 751*0fca6ea1SDimitry Andric const BasicBlock *BB) { 752*0fca6ea1SDimitry Andric LeaderListNode *Prev = nullptr; 753*0fca6ea1SDimitry Andric LeaderListNode *Curr = &NumToLeaders[N]; 754*0fca6ea1SDimitry Andric 755*0fca6ea1SDimitry Andric while (Curr && (Curr->Entry.Val != I || Curr->Entry.BB != BB)) { 756*0fca6ea1SDimitry Andric Prev = Curr; 757*0fca6ea1SDimitry Andric Curr = Curr->Next; 758*0fca6ea1SDimitry Andric } 759*0fca6ea1SDimitry Andric 760*0fca6ea1SDimitry Andric if (!Curr) 761*0fca6ea1SDimitry Andric return; 762*0fca6ea1SDimitry Andric 763*0fca6ea1SDimitry Andric if (Prev) { 764*0fca6ea1SDimitry Andric Prev->Next = Curr->Next; 765*0fca6ea1SDimitry Andric } else { 766*0fca6ea1SDimitry Andric if (!Curr->Next) { 767*0fca6ea1SDimitry Andric Curr->Entry.Val = nullptr; 768*0fca6ea1SDimitry Andric Curr->Entry.BB = nullptr; 769*0fca6ea1SDimitry Andric } else { 770*0fca6ea1SDimitry Andric LeaderListNode *Next = Curr->Next; 771*0fca6ea1SDimitry Andric Curr->Entry.Val = Next->Entry.Val; 772*0fca6ea1SDimitry Andric Curr->Entry.BB = Next->Entry.BB; 773*0fca6ea1SDimitry Andric Curr->Next = Next->Next; 774*0fca6ea1SDimitry Andric } 775*0fca6ea1SDimitry Andric } 776*0fca6ea1SDimitry Andric } 777*0fca6ea1SDimitry Andric 778*0fca6ea1SDimitry Andric void GVNPass::LeaderMap::verifyRemoved(const Value *V) const { 779*0fca6ea1SDimitry Andric // Walk through the value number scope to make sure the instruction isn't 780*0fca6ea1SDimitry Andric // ferreted away in it. 781*0fca6ea1SDimitry Andric for (const auto &I : NumToLeaders) { 782*0fca6ea1SDimitry Andric (void)I; 783*0fca6ea1SDimitry Andric assert(I.second.Entry.Val != V && "Inst still in value numbering scope!"); 784*0fca6ea1SDimitry Andric assert( 785*0fca6ea1SDimitry Andric std::none_of(leader_iterator(&I.second), leader_iterator(nullptr), 786*0fca6ea1SDimitry Andric [=](const LeaderTableEntry &E) { return E.Val == V; }) && 787*0fca6ea1SDimitry Andric "Inst still in value numbering scope!"); 788*0fca6ea1SDimitry Andric } 789*0fca6ea1SDimitry Andric } 790*0fca6ea1SDimitry Andric 791*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===// 7920b57cec5SDimitry Andric // GVN Pass 7930b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 7940b57cec5SDimitry Andric 795349cc55cSDimitry Andric bool GVNPass::isPREEnabled() const { 79681ad6265SDimitry Andric return Options.AllowPRE.value_or(GVNEnablePRE); 7975ffd83dbSDimitry Andric } 7985ffd83dbSDimitry Andric 799349cc55cSDimitry Andric bool GVNPass::isLoadPREEnabled() const { 80081ad6265SDimitry Andric return Options.AllowLoadPRE.value_or(GVNEnableLoadPRE); 8015ffd83dbSDimitry Andric } 8025ffd83dbSDimitry Andric 803349cc55cSDimitry Andric bool GVNPass::isLoadInLoopPREEnabled() const { 80481ad6265SDimitry Andric return Options.AllowLoadInLoopPRE.value_or(GVNEnableLoadInLoopPRE); 8055ffd83dbSDimitry Andric } 8065ffd83dbSDimitry Andric 807349cc55cSDimitry Andric bool GVNPass::isLoadPRESplitBackedgeEnabled() const { 80881ad6265SDimitry Andric return Options.AllowLoadPRESplitBackedge.value_or( 809e8d8bef9SDimitry Andric GVNEnableSplitBackedgeInLoadPRE); 810e8d8bef9SDimitry Andric } 811e8d8bef9SDimitry Andric 812349cc55cSDimitry Andric bool GVNPass::isMemDepEnabled() const { 81381ad6265SDimitry Andric return Options.AllowMemDep.value_or(GVNEnableMemDep); 8145ffd83dbSDimitry Andric } 8155ffd83dbSDimitry Andric 816349cc55cSDimitry Andric PreservedAnalyses GVNPass::run(Function &F, FunctionAnalysisManager &AM) { 8170b57cec5SDimitry Andric // FIXME: The order of evaluation of these 'getResult' calls is very 8180b57cec5SDimitry Andric // significant! Re-ordering these variables will cause GVN when run alone to 8190b57cec5SDimitry Andric // be less effective! We should fix memdep and basic-aa to not exhibit this 8200b57cec5SDimitry Andric // behavior, but until then don't change the order here. 8210b57cec5SDimitry Andric auto &AC = AM.getResult<AssumptionAnalysis>(F); 8220b57cec5SDimitry Andric auto &DT = AM.getResult<DominatorTreeAnalysis>(F); 8230b57cec5SDimitry Andric auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); 8240b57cec5SDimitry Andric auto &AA = AM.getResult<AAManager>(F); 8255ffd83dbSDimitry Andric auto *MemDep = 8265ffd83dbSDimitry Andric isMemDepEnabled() ? &AM.getResult<MemoryDependenceAnalysis>(F) : nullptr; 8275f757f3fSDimitry Andric auto &LI = AM.getResult<LoopAnalysis>(F); 828e8d8bef9SDimitry Andric auto *MSSA = AM.getCachedResult<MemorySSAAnalysis>(F); 8290b57cec5SDimitry Andric auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F); 830e8d8bef9SDimitry Andric bool Changed = runImpl(F, AC, DT, TLI, AA, MemDep, LI, &ORE, 831e8d8bef9SDimitry Andric MSSA ? &MSSA->getMSSA() : nullptr); 8320b57cec5SDimitry Andric if (!Changed) 8330b57cec5SDimitry Andric return PreservedAnalyses::all(); 8340b57cec5SDimitry Andric PreservedAnalyses PA; 8350b57cec5SDimitry Andric PA.preserve<DominatorTreeAnalysis>(); 8360b57cec5SDimitry Andric PA.preserve<TargetLibraryAnalysis>(); 837e8d8bef9SDimitry Andric if (MSSA) 838e8d8bef9SDimitry Andric PA.preserve<MemorySSAAnalysis>(); 8398bcb0991SDimitry Andric PA.preserve<LoopAnalysis>(); 8400b57cec5SDimitry Andric return PA; 8410b57cec5SDimitry Andric } 8420b57cec5SDimitry Andric 843349cc55cSDimitry Andric void GVNPass::printPipeline( 844349cc55cSDimitry Andric raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) { 845349cc55cSDimitry Andric static_cast<PassInfoMixin<GVNPass> *>(this)->printPipeline( 846349cc55cSDimitry Andric OS, MapClassName2PassName); 847349cc55cSDimitry Andric 84806c3fb27SDimitry Andric OS << '<'; 849bdd1243dSDimitry Andric if (Options.AllowPRE != std::nullopt) 850bdd1243dSDimitry Andric OS << (*Options.AllowPRE ? "" : "no-") << "pre;"; 851bdd1243dSDimitry Andric if (Options.AllowLoadPRE != std::nullopt) 852bdd1243dSDimitry Andric OS << (*Options.AllowLoadPRE ? "" : "no-") << "load-pre;"; 853bdd1243dSDimitry Andric if (Options.AllowLoadPRESplitBackedge != std::nullopt) 854bdd1243dSDimitry Andric OS << (*Options.AllowLoadPRESplitBackedge ? "" : "no-") 855349cc55cSDimitry Andric << "split-backedge-load-pre;"; 856bdd1243dSDimitry Andric if (Options.AllowMemDep != std::nullopt) 857bdd1243dSDimitry Andric OS << (*Options.AllowMemDep ? "" : "no-") << "memdep"; 85806c3fb27SDimitry Andric OS << '>'; 859349cc55cSDimitry Andric } 860349cc55cSDimitry Andric 8610b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 862349cc55cSDimitry Andric LLVM_DUMP_METHOD void GVNPass::dump(DenseMap<uint32_t, Value *> &d) const { 8630b57cec5SDimitry Andric errs() << "{\n"; 864fe6060f1SDimitry Andric for (auto &I : d) { 865fe6060f1SDimitry Andric errs() << I.first << "\n"; 866fe6060f1SDimitry Andric I.second->dump(); 8670b57cec5SDimitry Andric } 8680b57cec5SDimitry Andric errs() << "}\n"; 8690b57cec5SDimitry Andric } 8700b57cec5SDimitry Andric #endif 8710b57cec5SDimitry Andric 872e8d8bef9SDimitry Andric enum class AvailabilityState : char { 873e8d8bef9SDimitry Andric /// We know the block *is not* fully available. This is a fixpoint. 874e8d8bef9SDimitry Andric Unavailable = 0, 875e8d8bef9SDimitry Andric /// We know the block *is* fully available. This is a fixpoint. 876e8d8bef9SDimitry Andric Available = 1, 877e8d8bef9SDimitry Andric /// We do not know whether the block is fully available or not, 878e8d8bef9SDimitry Andric /// but we are currently speculating that it will be. 879e8d8bef9SDimitry Andric /// If it would have turned out that the block was, in fact, not fully 880e8d8bef9SDimitry Andric /// available, this would have been cleaned up into an Unavailable. 881e8d8bef9SDimitry Andric SpeculativelyAvailable = 2, 882e8d8bef9SDimitry Andric }; 883e8d8bef9SDimitry Andric 8840b57cec5SDimitry Andric /// Return true if we can prove that the value 8850b57cec5SDimitry Andric /// we're analyzing is fully available in the specified block. As we go, keep 8860b57cec5SDimitry Andric /// track of which blocks we know are fully alive in FullyAvailableBlocks. This 8870b57cec5SDimitry Andric /// map is actually a tri-state map with the following values: 8880b57cec5SDimitry Andric /// 0) we know the block *is not* fully available. 8890b57cec5SDimitry Andric /// 1) we know the block *is* fully available. 8900b57cec5SDimitry Andric /// 2) we do not know whether the block is fully available or not, but we are 8910b57cec5SDimitry Andric /// currently speculating that it will be. 892e8d8bef9SDimitry Andric static bool IsValueFullyAvailableInBlock( 893e8d8bef9SDimitry Andric BasicBlock *BB, 894e8d8bef9SDimitry Andric DenseMap<BasicBlock *, AvailabilityState> &FullyAvailableBlocks) { 895e8d8bef9SDimitry Andric SmallVector<BasicBlock *, 32> Worklist; 896bdd1243dSDimitry Andric std::optional<BasicBlock *> UnavailableBB; 8970b57cec5SDimitry Andric 898e8d8bef9SDimitry Andric // The number of times we didn't find an entry for a block in a map and 899e8d8bef9SDimitry Andric // optimistically inserted an entry marking block as speculatively available. 900e8d8bef9SDimitry Andric unsigned NumNewNewSpeculativelyAvailableBBs = 0; 9010b57cec5SDimitry Andric 902e8d8bef9SDimitry Andric #ifndef NDEBUG 903e8d8bef9SDimitry Andric SmallSet<BasicBlock *, 32> NewSpeculativelyAvailableBBs; 904e8d8bef9SDimitry Andric SmallVector<BasicBlock *, 32> AvailableBBs; 905e8d8bef9SDimitry Andric #endif 906e8d8bef9SDimitry Andric 907e8d8bef9SDimitry Andric Worklist.emplace_back(BB); 908e8d8bef9SDimitry Andric while (!Worklist.empty()) { 909fe6060f1SDimitry Andric BasicBlock *CurrBB = Worklist.pop_back_val(); // LoadFO - depth-first! 910e8d8bef9SDimitry Andric // Optimistically assume that the block is Speculatively Available and check 911e8d8bef9SDimitry Andric // to see if we already know about this block in one lookup. 912e8d8bef9SDimitry Andric std::pair<DenseMap<BasicBlock *, AvailabilityState>::iterator, bool> IV = 913e8d8bef9SDimitry Andric FullyAvailableBlocks.try_emplace( 914e8d8bef9SDimitry Andric CurrBB, AvailabilityState::SpeculativelyAvailable); 915e8d8bef9SDimitry Andric AvailabilityState &State = IV.first->second; 916e8d8bef9SDimitry Andric 917e8d8bef9SDimitry Andric // Did the entry already exist for this block? 9180b57cec5SDimitry Andric if (!IV.second) { 919e8d8bef9SDimitry Andric if (State == AvailabilityState::Unavailable) { 920e8d8bef9SDimitry Andric UnavailableBB = CurrBB; 921e8d8bef9SDimitry Andric break; // Backpropagate unavailability info. 9220b57cec5SDimitry Andric } 9230b57cec5SDimitry Andric 924e8d8bef9SDimitry Andric #ifndef NDEBUG 925e8d8bef9SDimitry Andric AvailableBBs.emplace_back(CurrBB); 926e8d8bef9SDimitry Andric #endif 927e8d8bef9SDimitry Andric continue; // Don't recurse further, but continue processing worklist. 9280b57cec5SDimitry Andric } 9290b57cec5SDimitry Andric 930e8d8bef9SDimitry Andric // No entry found for block. 931e8d8bef9SDimitry Andric ++NumNewNewSpeculativelyAvailableBBs; 932e8d8bef9SDimitry Andric bool OutOfBudget = NumNewNewSpeculativelyAvailableBBs > MaxBBSpeculations; 9330b57cec5SDimitry Andric 934e8d8bef9SDimitry Andric // If we have exhausted our budget, mark this block as unavailable. 935e8d8bef9SDimitry Andric // Also, if this block has no predecessors, the value isn't live-in here. 936e8d8bef9SDimitry Andric if (OutOfBudget || pred_empty(CurrBB)) { 937e8d8bef9SDimitry Andric MaxBBSpeculationCutoffReachedTimes += (int)OutOfBudget; 938e8d8bef9SDimitry Andric State = AvailabilityState::Unavailable; 939e8d8bef9SDimitry Andric UnavailableBB = CurrBB; 940e8d8bef9SDimitry Andric break; // Backpropagate unavailability info. 941e8d8bef9SDimitry Andric } 9420b57cec5SDimitry Andric 943e8d8bef9SDimitry Andric // Tentatively consider this block as speculatively available. 944e8d8bef9SDimitry Andric #ifndef NDEBUG 945e8d8bef9SDimitry Andric NewSpeculativelyAvailableBBs.insert(CurrBB); 946e8d8bef9SDimitry Andric #endif 947e8d8bef9SDimitry Andric // And further recurse into block's predecessors, in depth-first order! 948e8d8bef9SDimitry Andric Worklist.append(pred_begin(CurrBB), pred_end(CurrBB)); 949e8d8bef9SDimitry Andric } 9500b57cec5SDimitry Andric 951e8d8bef9SDimitry Andric #if LLVM_ENABLE_STATS 952e8d8bef9SDimitry Andric IsValueFullyAvailableInBlockNumSpeculationsMax.updateMax( 953e8d8bef9SDimitry Andric NumNewNewSpeculativelyAvailableBBs); 954e8d8bef9SDimitry Andric #endif 9550b57cec5SDimitry Andric 956e8d8bef9SDimitry Andric // If the block isn't marked as fixpoint yet 957e8d8bef9SDimitry Andric // (the Unavailable and Available states are fixpoints) 958e8d8bef9SDimitry Andric auto MarkAsFixpointAndEnqueueSuccessors = 959e8d8bef9SDimitry Andric [&](BasicBlock *BB, AvailabilityState FixpointState) { 960e8d8bef9SDimitry Andric auto It = FullyAvailableBlocks.find(BB); 961e8d8bef9SDimitry Andric if (It == FullyAvailableBlocks.end()) 962e8d8bef9SDimitry Andric return; // Never queried this block, leave as-is. 963e8d8bef9SDimitry Andric switch (AvailabilityState &State = It->second) { 964e8d8bef9SDimitry Andric case AvailabilityState::Unavailable: 965e8d8bef9SDimitry Andric case AvailabilityState::Available: 966e8d8bef9SDimitry Andric return; // Don't backpropagate further, continue processing worklist. 967e8d8bef9SDimitry Andric case AvailabilityState::SpeculativelyAvailable: // Fix it! 968e8d8bef9SDimitry Andric State = FixpointState; 969e8d8bef9SDimitry Andric #ifndef NDEBUG 970e8d8bef9SDimitry Andric assert(NewSpeculativelyAvailableBBs.erase(BB) && 971e8d8bef9SDimitry Andric "Found a speculatively available successor leftover?"); 972e8d8bef9SDimitry Andric #endif 973e8d8bef9SDimitry Andric // Queue successors for further processing. 974e8d8bef9SDimitry Andric Worklist.append(succ_begin(BB), succ_end(BB)); 975e8d8bef9SDimitry Andric return; 976e8d8bef9SDimitry Andric } 977e8d8bef9SDimitry Andric }; 978e8d8bef9SDimitry Andric 979e8d8bef9SDimitry Andric if (UnavailableBB) { 980e8d8bef9SDimitry Andric // Okay, we have encountered an unavailable block. 981e8d8bef9SDimitry Andric // Mark speculatively available blocks reachable from UnavailableBB as 982e8d8bef9SDimitry Andric // unavailable as well. Paths are terminated when they reach blocks not in 983e8d8bef9SDimitry Andric // FullyAvailableBlocks or they are not marked as speculatively available. 984e8d8bef9SDimitry Andric Worklist.clear(); 985e8d8bef9SDimitry Andric Worklist.append(succ_begin(*UnavailableBB), succ_end(*UnavailableBB)); 986e8d8bef9SDimitry Andric while (!Worklist.empty()) 987e8d8bef9SDimitry Andric MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(), 988e8d8bef9SDimitry Andric AvailabilityState::Unavailable); 989e8d8bef9SDimitry Andric } 990e8d8bef9SDimitry Andric 991e8d8bef9SDimitry Andric #ifndef NDEBUG 992e8d8bef9SDimitry Andric Worklist.clear(); 993e8d8bef9SDimitry Andric for (BasicBlock *AvailableBB : AvailableBBs) 994e8d8bef9SDimitry Andric Worklist.append(succ_begin(AvailableBB), succ_end(AvailableBB)); 995e8d8bef9SDimitry Andric while (!Worklist.empty()) 996e8d8bef9SDimitry Andric MarkAsFixpointAndEnqueueSuccessors(Worklist.pop_back_val(), 997e8d8bef9SDimitry Andric AvailabilityState::Available); 998e8d8bef9SDimitry Andric 999e8d8bef9SDimitry Andric assert(NewSpeculativelyAvailableBBs.empty() && 1000e8d8bef9SDimitry Andric "Must have fixed all the new speculatively available blocks."); 1001e8d8bef9SDimitry Andric #endif 1002e8d8bef9SDimitry Andric 1003e8d8bef9SDimitry Andric return !UnavailableBB; 10040b57cec5SDimitry Andric } 10050b57cec5SDimitry Andric 100606c3fb27SDimitry Andric /// If the specified OldValue exists in ValuesPerBlock, replace its value with 100706c3fb27SDimitry Andric /// NewValue. 100806c3fb27SDimitry Andric static void replaceValuesPerBlockEntry( 100906c3fb27SDimitry Andric SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock, Value *OldValue, 101006c3fb27SDimitry Andric Value *NewValue) { 101106c3fb27SDimitry Andric for (AvailableValueInBlock &V : ValuesPerBlock) { 1012b121cb00SDimitry Andric if (V.AV.Val == OldValue) 1013b121cb00SDimitry Andric V.AV.Val = NewValue; 1014b121cb00SDimitry Andric if (V.AV.isSelectValue()) { 1015b121cb00SDimitry Andric if (V.AV.V1 == OldValue) 1016b121cb00SDimitry Andric V.AV.V1 = NewValue; 1017b121cb00SDimitry Andric if (V.AV.V2 == OldValue) 1018b121cb00SDimitry Andric V.AV.V2 = NewValue; 1019b121cb00SDimitry Andric } 102006c3fb27SDimitry Andric } 102106c3fb27SDimitry Andric } 102206c3fb27SDimitry Andric 10230b57cec5SDimitry Andric /// Given a set of loads specified by ValuesPerBlock, 1024fe6060f1SDimitry Andric /// construct SSA form, allowing us to eliminate Load. This returns the value 1025fe6060f1SDimitry Andric /// that should be used at Load's definition site. 1026fe6060f1SDimitry Andric static Value * 1027fe6060f1SDimitry Andric ConstructSSAForLoadSet(LoadInst *Load, 10280b57cec5SDimitry Andric SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock, 1029349cc55cSDimitry Andric GVNPass &gvn) { 10300b57cec5SDimitry Andric // Check for the fully redundant, dominating load case. In this case, we can 10310b57cec5SDimitry Andric // just use the dominating value directly. 10320b57cec5SDimitry Andric if (ValuesPerBlock.size() == 1 && 10330b57cec5SDimitry Andric gvn.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB, 1034fe6060f1SDimitry Andric Load->getParent())) { 10350b57cec5SDimitry Andric assert(!ValuesPerBlock[0].AV.isUndefValue() && 10360b57cec5SDimitry Andric "Dead BB dominate this block"); 1037fe6060f1SDimitry Andric return ValuesPerBlock[0].MaterializeAdjustedValue(Load, gvn); 10380b57cec5SDimitry Andric } 10390b57cec5SDimitry Andric 10400b57cec5SDimitry Andric // Otherwise, we have to construct SSA form. 10410b57cec5SDimitry Andric SmallVector<PHINode*, 8> NewPHIs; 10420b57cec5SDimitry Andric SSAUpdater SSAUpdate(&NewPHIs); 1043fe6060f1SDimitry Andric SSAUpdate.Initialize(Load->getType(), Load->getName()); 10440b57cec5SDimitry Andric 10450b57cec5SDimitry Andric for (const AvailableValueInBlock &AV : ValuesPerBlock) { 10460b57cec5SDimitry Andric BasicBlock *BB = AV.BB; 10470b57cec5SDimitry Andric 1048fe6060f1SDimitry Andric if (AV.AV.isUndefValue()) 1049fe6060f1SDimitry Andric continue; 1050fe6060f1SDimitry Andric 10510b57cec5SDimitry Andric if (SSAUpdate.HasValueForBlock(BB)) 10520b57cec5SDimitry Andric continue; 10530b57cec5SDimitry Andric 10540b57cec5SDimitry Andric // If the value is the load that we will be eliminating, and the block it's 10550b57cec5SDimitry Andric // available in is the block that the load is in, then don't add it as 10560b57cec5SDimitry Andric // SSAUpdater will resolve the value to the relevant phi which may let it 10570b57cec5SDimitry Andric // avoid phi construction entirely if there's actually only one value. 1058fe6060f1SDimitry Andric if (BB == Load->getParent() && 1059fe6060f1SDimitry Andric ((AV.AV.isSimpleValue() && AV.AV.getSimpleValue() == Load) || 1060fe6060f1SDimitry Andric (AV.AV.isCoercedLoadValue() && AV.AV.getCoercedLoadValue() == Load))) 10610b57cec5SDimitry Andric continue; 10620b57cec5SDimitry Andric 1063fe6060f1SDimitry Andric SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(Load, gvn)); 10640b57cec5SDimitry Andric } 10650b57cec5SDimitry Andric 10660b57cec5SDimitry Andric // Perform PHI construction. 1067fe6060f1SDimitry Andric return SSAUpdate.GetValueInMiddleOfBlock(Load->getParent()); 10680b57cec5SDimitry Andric } 10690b57cec5SDimitry Andric 1070fe6060f1SDimitry Andric Value *AvailableValue::MaterializeAdjustedValue(LoadInst *Load, 10710b57cec5SDimitry Andric Instruction *InsertPt, 1072349cc55cSDimitry Andric GVNPass &gvn) const { 10730b57cec5SDimitry Andric Value *Res; 1074fe6060f1SDimitry Andric Type *LoadTy = Load->getType(); 1075*0fca6ea1SDimitry Andric const DataLayout &DL = Load->getDataLayout(); 10760b57cec5SDimitry Andric if (isSimpleValue()) { 10770b57cec5SDimitry Andric Res = getSimpleValue(); 10780b57cec5SDimitry Andric if (Res->getType() != LoadTy) { 107906c3fb27SDimitry Andric Res = getValueForLoad(Res, Offset, LoadTy, InsertPt, DL); 10800b57cec5SDimitry Andric 10810b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset 10820b57cec5SDimitry Andric << " " << *getSimpleValue() << '\n' 10830b57cec5SDimitry Andric << *Res << '\n' 10840b57cec5SDimitry Andric << "\n\n\n"); 10850b57cec5SDimitry Andric } 10860b57cec5SDimitry Andric } else if (isCoercedLoadValue()) { 1087fe6060f1SDimitry Andric LoadInst *CoercedLoad = getCoercedLoadValue(); 1088fe6060f1SDimitry Andric if (CoercedLoad->getType() == LoadTy && Offset == 0) { 1089fe6060f1SDimitry Andric Res = CoercedLoad; 109006c3fb27SDimitry Andric combineMetadataForCSE(CoercedLoad, Load, false); 10910b57cec5SDimitry Andric } else { 109206c3fb27SDimitry Andric Res = getValueForLoad(CoercedLoad, Offset, LoadTy, InsertPt, DL); 109306c3fb27SDimitry Andric // We are adding a new user for this load, for which the original 109406c3fb27SDimitry Andric // metadata may not hold. Additionally, the new load may have a different 109506c3fb27SDimitry Andric // size and type, so their metadata cannot be combined in any 109606c3fb27SDimitry Andric // straightforward way. 109706c3fb27SDimitry Andric // Drop all metadata that is not known to cause immediate UB on violation, 109806c3fb27SDimitry Andric // unless the load has !noundef, in which case all metadata violations 109906c3fb27SDimitry Andric // will be promoted to UB. 110006c3fb27SDimitry Andric // TODO: We can combine noalias/alias.scope metadata here, because it is 110106c3fb27SDimitry Andric // independent of the load type. 110206c3fb27SDimitry Andric if (!CoercedLoad->hasMetadata(LLVMContext::MD_noundef)) 110306c3fb27SDimitry Andric CoercedLoad->dropUnknownNonDebugMetadata( 110406c3fb27SDimitry Andric {LLVMContext::MD_dereferenceable, 110506c3fb27SDimitry Andric LLVMContext::MD_dereferenceable_or_null, 110606c3fb27SDimitry Andric LLVMContext::MD_invariant_load, LLVMContext::MD_invariant_group}); 11070b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offset 11080b57cec5SDimitry Andric << " " << *getCoercedLoadValue() << '\n' 11090b57cec5SDimitry Andric << *Res << '\n' 11100b57cec5SDimitry Andric << "\n\n\n"); 11110b57cec5SDimitry Andric } 11120b57cec5SDimitry Andric } else if (isMemIntrinValue()) { 11130b57cec5SDimitry Andric Res = getMemInstValueForLoad(getMemIntrinValue(), Offset, LoadTy, 11140b57cec5SDimitry Andric InsertPt, DL); 11150b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset 11160b57cec5SDimitry Andric << " " << *getMemIntrinValue() << '\n' 11170b57cec5SDimitry Andric << *Res << '\n' 11180b57cec5SDimitry Andric << "\n\n\n"); 111981ad6265SDimitry Andric } else if (isSelectValue()) { 112081ad6265SDimitry Andric // Introduce a new value select for a load from an eligible pointer select. 112181ad6265SDimitry Andric SelectInst *Sel = getSelectValue(); 1122bdd1243dSDimitry Andric assert(V1 && V2 && "both value operands of the select must be present"); 1123*0fca6ea1SDimitry Andric Res = 1124*0fca6ea1SDimitry Andric SelectInst::Create(Sel->getCondition(), V1, V2, "", Sel->getIterator()); 11250b57cec5SDimitry Andric } else { 1126fe6060f1SDimitry Andric llvm_unreachable("Should not materialize value from dead block"); 11270b57cec5SDimitry Andric } 11280b57cec5SDimitry Andric assert(Res && "failed to materialize?"); 11290b57cec5SDimitry Andric return Res; 11300b57cec5SDimitry Andric } 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric static bool isLifetimeStart(const Instruction *Inst) { 11330b57cec5SDimitry Andric if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst)) 11340b57cec5SDimitry Andric return II->getIntrinsicID() == Intrinsic::lifetime_start; 11350b57cec5SDimitry Andric return false; 11360b57cec5SDimitry Andric } 11370b57cec5SDimitry Andric 1138fe6060f1SDimitry Andric /// Assuming To can be reached from both From and Between, does Between lie on 1139fe6060f1SDimitry Andric /// every path from From to To? 1140fe6060f1SDimitry Andric static bool liesBetween(const Instruction *From, Instruction *Between, 1141fe6060f1SDimitry Andric const Instruction *To, DominatorTree *DT) { 1142fe6060f1SDimitry Andric if (From->getParent() == Between->getParent()) 1143fe6060f1SDimitry Andric return DT->dominates(From, Between); 1144fe6060f1SDimitry Andric SmallSet<BasicBlock *, 1> Exclusion; 1145fe6060f1SDimitry Andric Exclusion.insert(Between->getParent()); 1146fe6060f1SDimitry Andric return !isPotentiallyReachable(From, To, &Exclusion, DT); 1147fe6060f1SDimitry Andric } 1148fe6060f1SDimitry Andric 11490b57cec5SDimitry Andric /// Try to locate the three instruction involved in a missed 11500b57cec5SDimitry Andric /// load-elimination case that is due to an intervening store. 1151fe6060f1SDimitry Andric static void reportMayClobberedLoad(LoadInst *Load, MemDepResult DepInfo, 11520b57cec5SDimitry Andric DominatorTree *DT, 11530b57cec5SDimitry Andric OptimizationRemarkEmitter *ORE) { 11540b57cec5SDimitry Andric using namespace ore; 11550b57cec5SDimitry Andric 1156bdd1243dSDimitry Andric Instruction *OtherAccess = nullptr; 11570b57cec5SDimitry Andric 1158fe6060f1SDimitry Andric OptimizationRemarkMissed R(DEBUG_TYPE, "LoadClobbered", Load); 1159fe6060f1SDimitry Andric R << "load of type " << NV("Type", Load->getType()) << " not eliminated" 11600b57cec5SDimitry Andric << setExtraArgs(); 11610b57cec5SDimitry Andric 1162fe6060f1SDimitry Andric for (auto *U : Load->getPointerOperand()->users()) { 1163bdd1243dSDimitry Andric if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U))) { 1164bdd1243dSDimitry Andric auto *I = cast<Instruction>(U); 1165bdd1243dSDimitry Andric if (I->getFunction() == Load->getFunction() && DT->dominates(I, Load)) { 1166fe6060f1SDimitry Andric // Use the most immediately dominating value 1167fe6060f1SDimitry Andric if (OtherAccess) { 1168bdd1243dSDimitry Andric if (DT->dominates(OtherAccess, I)) 1169bdd1243dSDimitry Andric OtherAccess = I; 1170fe6060f1SDimitry Andric else 1171bdd1243dSDimitry Andric assert(U == OtherAccess || DT->dominates(I, OtherAccess)); 1172fe6060f1SDimitry Andric } else 1173bdd1243dSDimitry Andric OtherAccess = I; 1174bdd1243dSDimitry Andric } 1175fe6060f1SDimitry Andric } 1176fe6060f1SDimitry Andric } 1177fe6060f1SDimitry Andric 1178fe6060f1SDimitry Andric if (!OtherAccess) { 1179fe6060f1SDimitry Andric // There is no dominating use, check if we can find a closest non-dominating 1180fe6060f1SDimitry Andric // use that lies between any other potentially available use and Load. 1181fe6060f1SDimitry Andric for (auto *U : Load->getPointerOperand()->users()) { 1182bdd1243dSDimitry Andric if (U != Load && (isa<LoadInst>(U) || isa<StoreInst>(U))) { 1183bdd1243dSDimitry Andric auto *I = cast<Instruction>(U); 1184bdd1243dSDimitry Andric if (I->getFunction() == Load->getFunction() && 1185bdd1243dSDimitry Andric isPotentiallyReachable(I, Load, nullptr, DT)) { 1186fe6060f1SDimitry Andric if (OtherAccess) { 1187bdd1243dSDimitry Andric if (liesBetween(OtherAccess, I, Load, DT)) { 1188bdd1243dSDimitry Andric OtherAccess = I; 1189bdd1243dSDimitry Andric } else if (!liesBetween(I, OtherAccess, Load, DT)) { 1190fe6060f1SDimitry Andric // These uses are both partially available at Load were it not for 1191fe6060f1SDimitry Andric // the clobber, but neither lies strictly after the other. 1192fe6060f1SDimitry Andric OtherAccess = nullptr; 1193fe6060f1SDimitry Andric break; 1194fe6060f1SDimitry Andric } // else: keep current OtherAccess since it lies between U and Load 1195fe6060f1SDimitry Andric } else { 1196bdd1243dSDimitry Andric OtherAccess = I; 1197bdd1243dSDimitry Andric } 1198fe6060f1SDimitry Andric } 1199fe6060f1SDimitry Andric } 1200fe6060f1SDimitry Andric } 12010b57cec5SDimitry Andric } 12020b57cec5SDimitry Andric 12030b57cec5SDimitry Andric if (OtherAccess) 12040b57cec5SDimitry Andric R << " in favor of " << NV("OtherAccess", OtherAccess); 12050b57cec5SDimitry Andric 12060b57cec5SDimitry Andric R << " because it is clobbered by " << NV("ClobberedBy", DepInfo.getInst()); 12070b57cec5SDimitry Andric 12080b57cec5SDimitry Andric ORE->emit(R); 12090b57cec5SDimitry Andric } 12100b57cec5SDimitry Andric 1211bdd1243dSDimitry Andric // Find non-clobbered value for Loc memory location in extended basic block 1212bdd1243dSDimitry Andric // (chain of basic blocks with single predecessors) starting From instruction. 1213bdd1243dSDimitry Andric static Value *findDominatingValue(const MemoryLocation &Loc, Type *LoadTy, 1214bdd1243dSDimitry Andric Instruction *From, AAResults *AA) { 1215bdd1243dSDimitry Andric uint32_t NumVisitedInsts = 0; 1216bdd1243dSDimitry Andric BasicBlock *FromBB = From->getParent(); 1217bdd1243dSDimitry Andric BatchAAResults BatchAA(*AA); 1218bdd1243dSDimitry Andric for (BasicBlock *BB = FromBB; BB; BB = BB->getSinglePredecessor()) 12195f757f3fSDimitry Andric for (auto *Inst = BB == FromBB ? From : BB->getTerminator(); 12205f757f3fSDimitry Andric Inst != nullptr; Inst = Inst->getPrevNonDebugInstruction()) { 1221bdd1243dSDimitry Andric // Stop the search if limit is reached. 1222bdd1243dSDimitry Andric if (++NumVisitedInsts > MaxNumVisitedInsts) 1223bdd1243dSDimitry Andric return nullptr; 1224bdd1243dSDimitry Andric if (isModSet(BatchAA.getModRefInfo(Inst, Loc))) 1225bdd1243dSDimitry Andric return nullptr; 1226bdd1243dSDimitry Andric if (auto *LI = dyn_cast<LoadInst>(Inst)) 1227bdd1243dSDimitry Andric if (LI->getPointerOperand() == Loc.Ptr && LI->getType() == LoadTy) 1228bdd1243dSDimitry Andric return LI; 1229bdd1243dSDimitry Andric } 1230bdd1243dSDimitry Andric return nullptr; 123181ad6265SDimitry Andric } 123281ad6265SDimitry Andric 1233bdd1243dSDimitry Andric std::optional<AvailableValue> 1234bdd1243dSDimitry Andric GVNPass::AnalyzeLoadAvailability(LoadInst *Load, MemDepResult DepInfo, 1235bdd1243dSDimitry Andric Value *Address) { 1236fe6060f1SDimitry Andric assert(Load->isUnordered() && "rules below are incorrect for ordered access"); 1237bdd1243dSDimitry Andric assert(DepInfo.isLocal() && "expected a local dependence"); 12380b57cec5SDimitry Andric 12390b57cec5SDimitry Andric Instruction *DepInst = DepInfo.getInst(); 1240bdd1243dSDimitry Andric 1241*0fca6ea1SDimitry Andric const DataLayout &DL = Load->getDataLayout(); 12420b57cec5SDimitry Andric if (DepInfo.isClobber()) { 12430b57cec5SDimitry Andric // If the dependence is to a store that writes to a superset of the bits 12440b57cec5SDimitry Andric // read by the load, we can extract the bits we need for the load from the 12450b57cec5SDimitry Andric // stored value. 12460b57cec5SDimitry Andric if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) { 12470b57cec5SDimitry Andric // Can't forward from non-atomic to atomic without violating memory model. 1248fe6060f1SDimitry Andric if (Address && Load->isAtomic() <= DepSI->isAtomic()) { 12490b57cec5SDimitry Andric int Offset = 1250fe6060f1SDimitry Andric analyzeLoadFromClobberingStore(Load->getType(), Address, DepSI, DL); 1251bdd1243dSDimitry Andric if (Offset != -1) 1252bdd1243dSDimitry Andric return AvailableValue::get(DepSI->getValueOperand(), Offset); 12530b57cec5SDimitry Andric } 12540b57cec5SDimitry Andric } 12550b57cec5SDimitry Andric 12560b57cec5SDimitry Andric // Check to see if we have something like this: 12570b57cec5SDimitry Andric // load i32* P 12580b57cec5SDimitry Andric // load i8* (P+1) 12590b57cec5SDimitry Andric // if we have this, replace the later with an extraction from the former. 1260fe6060f1SDimitry Andric if (LoadInst *DepLoad = dyn_cast<LoadInst>(DepInst)) { 12610b57cec5SDimitry Andric // If this is a clobber and L is the first instruction in its block, then 12620b57cec5SDimitry Andric // we have the first instruction in the entry block. 12630b57cec5SDimitry Andric // Can't forward from non-atomic to atomic without violating memory model. 1264fe6060f1SDimitry Andric if (DepLoad != Load && Address && 1265fe6060f1SDimitry Andric Load->isAtomic() <= DepLoad->isAtomic()) { 1266fe6060f1SDimitry Andric Type *LoadType = Load->getType(); 1267fe6060f1SDimitry Andric int Offset = -1; 12680b57cec5SDimitry Andric 1269fe6060f1SDimitry Andric // If MD reported clobber, check it was nested. 1270fe6060f1SDimitry Andric if (DepInfo.isClobber() && 1271fe6060f1SDimitry Andric canCoerceMustAliasedValueToLoad(DepLoad, LoadType, DL)) { 1272fe6060f1SDimitry Andric const auto ClobberOff = MD->getClobberOffset(DepLoad); 1273fe6060f1SDimitry Andric // GVN has no deal with a negative offset. 1274bdd1243dSDimitry Andric Offset = (ClobberOff == std::nullopt || *ClobberOff < 0) 1275bdd1243dSDimitry Andric ? -1 1276bdd1243dSDimitry Andric : *ClobberOff; 1277fe6060f1SDimitry Andric } 1278fe6060f1SDimitry Andric if (Offset == -1) 1279fe6060f1SDimitry Andric Offset = 1280fe6060f1SDimitry Andric analyzeLoadFromClobberingLoad(LoadType, Address, DepLoad, DL); 1281bdd1243dSDimitry Andric if (Offset != -1) 1282bdd1243dSDimitry Andric return AvailableValue::getLoad(DepLoad, Offset); 12830b57cec5SDimitry Andric } 12840b57cec5SDimitry Andric } 12850b57cec5SDimitry Andric 12860b57cec5SDimitry Andric // If the clobbering value is a memset/memcpy/memmove, see if we can 12870b57cec5SDimitry Andric // forward a value on from it. 12880b57cec5SDimitry Andric if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInst)) { 1289fe6060f1SDimitry Andric if (Address && !Load->isAtomic()) { 1290fe6060f1SDimitry Andric int Offset = analyzeLoadFromClobberingMemInst(Load->getType(), Address, 12910b57cec5SDimitry Andric DepMI, DL); 1292bdd1243dSDimitry Andric if (Offset != -1) 1293bdd1243dSDimitry Andric return AvailableValue::getMI(DepMI, Offset); 12940b57cec5SDimitry Andric } 12950b57cec5SDimitry Andric } 129681ad6265SDimitry Andric 12970b57cec5SDimitry Andric // Nothing known about this clobber, have to be conservative 12980b57cec5SDimitry Andric LLVM_DEBUG( 12990b57cec5SDimitry Andric // fast print dep, using operator<< on instruction is too slow. 1300fe6060f1SDimitry Andric dbgs() << "GVN: load "; Load->printAsOperand(dbgs()); 13010b57cec5SDimitry Andric dbgs() << " is clobbered by " << *DepInst << '\n';); 13020b57cec5SDimitry Andric if (ORE->allowExtraAnalysis(DEBUG_TYPE)) 1303fe6060f1SDimitry Andric reportMayClobberedLoad(Load, DepInfo, DT, ORE); 13040b57cec5SDimitry Andric 1305bdd1243dSDimitry Andric return std::nullopt; 13060b57cec5SDimitry Andric } 13070b57cec5SDimitry Andric assert(DepInfo.isDef() && "follows from above"); 13080b57cec5SDimitry Andric 130904eeddc0SDimitry Andric // Loading the alloca -> undef. 13100b57cec5SDimitry Andric // Loading immediately after lifetime begin -> undef. 1311bdd1243dSDimitry Andric if (isa<AllocaInst>(DepInst) || isLifetimeStart(DepInst)) 1312bdd1243dSDimitry Andric return AvailableValue::get(UndefValue::get(Load->getType())); 13130b57cec5SDimitry Andric 131481ad6265SDimitry Andric if (Constant *InitVal = 1315bdd1243dSDimitry Andric getInitialValueOfAllocation(DepInst, TLI, Load->getType())) 1316bdd1243dSDimitry Andric return AvailableValue::get(InitVal); 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andric if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) { 13190b57cec5SDimitry Andric // Reject loads and stores that are to the same address but are of 1320e8d8bef9SDimitry Andric // different types if we have to. If the stored value is convertable to 13210b57cec5SDimitry Andric // the loaded value, we can reuse it. 1322fe6060f1SDimitry Andric if (!canCoerceMustAliasedValueToLoad(S->getValueOperand(), Load->getType(), 13230b57cec5SDimitry Andric DL)) 1324bdd1243dSDimitry Andric return std::nullopt; 13250b57cec5SDimitry Andric 13260b57cec5SDimitry Andric // Can't forward from non-atomic to atomic without violating memory model. 1327fe6060f1SDimitry Andric if (S->isAtomic() < Load->isAtomic()) 1328bdd1243dSDimitry Andric return std::nullopt; 13290b57cec5SDimitry Andric 1330bdd1243dSDimitry Andric return AvailableValue::get(S->getValueOperand()); 13310b57cec5SDimitry Andric } 13320b57cec5SDimitry Andric 13330b57cec5SDimitry Andric if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) { 13340b57cec5SDimitry Andric // If the types mismatch and we can't handle it, reject reuse of the load. 13350b57cec5SDimitry Andric // If the stored value is larger or equal to the loaded value, we can reuse 13360b57cec5SDimitry Andric // it. 1337fe6060f1SDimitry Andric if (!canCoerceMustAliasedValueToLoad(LD, Load->getType(), DL)) 1338bdd1243dSDimitry Andric return std::nullopt; 13390b57cec5SDimitry Andric 13400b57cec5SDimitry Andric // Can't forward from non-atomic to atomic without violating memory model. 1341fe6060f1SDimitry Andric if (LD->isAtomic() < Load->isAtomic()) 1342bdd1243dSDimitry Andric return std::nullopt; 13430b57cec5SDimitry Andric 1344bdd1243dSDimitry Andric return AvailableValue::getLoad(LD); 1345bdd1243dSDimitry Andric } 1346bdd1243dSDimitry Andric 1347bdd1243dSDimitry Andric // Check if load with Addr dependent from select can be converted to select 1348bdd1243dSDimitry Andric // between load values. There must be no instructions between the found 1349bdd1243dSDimitry Andric // loads and DepInst that may clobber the loads. 1350bdd1243dSDimitry Andric if (auto *Sel = dyn_cast<SelectInst>(DepInst)) { 1351bdd1243dSDimitry Andric assert(Sel->getType() == Load->getPointerOperandType()); 1352bdd1243dSDimitry Andric auto Loc = MemoryLocation::get(Load); 1353bdd1243dSDimitry Andric Value *V1 = 1354bdd1243dSDimitry Andric findDominatingValue(Loc.getWithNewPtr(Sel->getTrueValue()), 1355bdd1243dSDimitry Andric Load->getType(), DepInst, getAliasAnalysis()); 1356bdd1243dSDimitry Andric if (!V1) 1357bdd1243dSDimitry Andric return std::nullopt; 1358bdd1243dSDimitry Andric Value *V2 = 1359bdd1243dSDimitry Andric findDominatingValue(Loc.getWithNewPtr(Sel->getFalseValue()), 1360bdd1243dSDimitry Andric Load->getType(), DepInst, getAliasAnalysis()); 1361bdd1243dSDimitry Andric if (!V2) 1362bdd1243dSDimitry Andric return std::nullopt; 1363bdd1243dSDimitry Andric return AvailableValue::getSelect(Sel, V1, V2); 13640b57cec5SDimitry Andric } 13650b57cec5SDimitry Andric 13660b57cec5SDimitry Andric // Unknown def - must be conservative 13670b57cec5SDimitry Andric LLVM_DEBUG( 13680b57cec5SDimitry Andric // fast print dep, using operator<< on instruction is too slow. 1369fe6060f1SDimitry Andric dbgs() << "GVN: load "; Load->printAsOperand(dbgs()); 13700b57cec5SDimitry Andric dbgs() << " has unknown def " << *DepInst << '\n';); 1371bdd1243dSDimitry Andric return std::nullopt; 13720b57cec5SDimitry Andric } 13730b57cec5SDimitry Andric 1374349cc55cSDimitry Andric void GVNPass::AnalyzeLoadAvailability(LoadInst *Load, LoadDepVect &Deps, 13750b57cec5SDimitry Andric AvailValInBlkVect &ValuesPerBlock, 13760b57cec5SDimitry Andric UnavailBlkVect &UnavailableBlocks) { 13770b57cec5SDimitry Andric // Filter out useless results (non-locals, etc). Keep track of the blocks 13780b57cec5SDimitry Andric // where we have a value available in repl, also keep track of whether we see 13790b57cec5SDimitry Andric // dependencies that produce an unknown value for the load (such as a call 13800b57cec5SDimitry Andric // that could potentially clobber the load). 1381bdd1243dSDimitry Andric for (const auto &Dep : Deps) { 1382bdd1243dSDimitry Andric BasicBlock *DepBB = Dep.getBB(); 1383bdd1243dSDimitry Andric MemDepResult DepInfo = Dep.getResult(); 13840b57cec5SDimitry Andric 13850b57cec5SDimitry Andric if (DeadBlocks.count(DepBB)) { 13860b57cec5SDimitry Andric // Dead dependent mem-op disguise as a load evaluating the same value 13870b57cec5SDimitry Andric // as the load in question. 13880b57cec5SDimitry Andric ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB)); 13890b57cec5SDimitry Andric continue; 13900b57cec5SDimitry Andric } 13910b57cec5SDimitry Andric 1392bdd1243dSDimitry Andric if (!DepInfo.isLocal()) { 139381ad6265SDimitry Andric UnavailableBlocks.push_back(DepBB); 139481ad6265SDimitry Andric continue; 139581ad6265SDimitry Andric } 139681ad6265SDimitry Andric 1397bdd1243dSDimitry Andric // The address being loaded in this non-local block may not be the same as 1398bdd1243dSDimitry Andric // the pointer operand of the load if PHI translation occurs. Make sure 1399bdd1243dSDimitry Andric // to consider the right address. 1400bdd1243dSDimitry Andric if (auto AV = AnalyzeLoadAvailability(Load, DepInfo, Dep.getAddress())) { 14010b57cec5SDimitry Andric // subtlety: because we know this was a non-local dependency, we know 14020b57cec5SDimitry Andric // it's safe to materialize anywhere between the instruction within 14030b57cec5SDimitry Andric // DepInfo and the end of it's block. 1404bdd1243dSDimitry Andric ValuesPerBlock.push_back( 1405bdd1243dSDimitry Andric AvailableValueInBlock::get(DepBB, std::move(*AV))); 14060b57cec5SDimitry Andric } else { 14070b57cec5SDimitry Andric UnavailableBlocks.push_back(DepBB); 14080b57cec5SDimitry Andric } 14090b57cec5SDimitry Andric } 14100b57cec5SDimitry Andric 1411bdd1243dSDimitry Andric assert(Deps.size() == ValuesPerBlock.size() + UnavailableBlocks.size() && 14120b57cec5SDimitry Andric "post condition violation"); 14130b57cec5SDimitry Andric } 14140b57cec5SDimitry Andric 141506c3fb27SDimitry Andric /// Given the following code, v1 is partially available on some edges, but not 141606c3fb27SDimitry Andric /// available on the edge from PredBB. This function tries to find if there is 141706c3fb27SDimitry Andric /// another identical load in the other successor of PredBB. 141806c3fb27SDimitry Andric /// 141906c3fb27SDimitry Andric /// v0 = load %addr 142006c3fb27SDimitry Andric /// br %LoadBB 142106c3fb27SDimitry Andric /// 142206c3fb27SDimitry Andric /// LoadBB: 142306c3fb27SDimitry Andric /// v1 = load %addr 142406c3fb27SDimitry Andric /// ... 142506c3fb27SDimitry Andric /// 142606c3fb27SDimitry Andric /// PredBB: 142706c3fb27SDimitry Andric /// ... 142806c3fb27SDimitry Andric /// br %cond, label %LoadBB, label %SuccBB 142906c3fb27SDimitry Andric /// 143006c3fb27SDimitry Andric /// SuccBB: 143106c3fb27SDimitry Andric /// v2 = load %addr 143206c3fb27SDimitry Andric /// ... 143306c3fb27SDimitry Andric /// 143406c3fb27SDimitry Andric LoadInst *GVNPass::findLoadToHoistIntoPred(BasicBlock *Pred, BasicBlock *LoadBB, 143506c3fb27SDimitry Andric LoadInst *Load) { 143606c3fb27SDimitry Andric // For simplicity we handle a Pred has 2 successors only. 143706c3fb27SDimitry Andric auto *Term = Pred->getTerminator(); 14385f757f3fSDimitry Andric if (Term->getNumSuccessors() != 2 || Term->isSpecialTerminator()) 143906c3fb27SDimitry Andric return nullptr; 144006c3fb27SDimitry Andric auto *SuccBB = Term->getSuccessor(0); 144106c3fb27SDimitry Andric if (SuccBB == LoadBB) 144206c3fb27SDimitry Andric SuccBB = Term->getSuccessor(1); 144306c3fb27SDimitry Andric if (!SuccBB->getSinglePredecessor()) 144406c3fb27SDimitry Andric return nullptr; 144506c3fb27SDimitry Andric 144606c3fb27SDimitry Andric unsigned int NumInsts = MaxNumInsnsPerBlock; 144706c3fb27SDimitry Andric for (Instruction &Inst : *SuccBB) { 144806c3fb27SDimitry Andric if (Inst.isDebugOrPseudoInst()) 144906c3fb27SDimitry Andric continue; 145006c3fb27SDimitry Andric if (--NumInsts == 0) 145106c3fb27SDimitry Andric return nullptr; 145206c3fb27SDimitry Andric 145306c3fb27SDimitry Andric if (!Inst.isIdenticalTo(Load)) 145406c3fb27SDimitry Andric continue; 145506c3fb27SDimitry Andric 145606c3fb27SDimitry Andric MemDepResult Dep = MD->getDependency(&Inst); 145706c3fb27SDimitry Andric // If an identical load doesn't depends on any local instructions, it can 145806c3fb27SDimitry Andric // be safely moved to PredBB. 145906c3fb27SDimitry Andric // Also check for the implicit control flow instructions. See the comments 146006c3fb27SDimitry Andric // in PerformLoadPRE for details. 146106c3fb27SDimitry Andric if (Dep.isNonLocal() && !ICF->isDominatedByICFIFromSameBlock(&Inst)) 146206c3fb27SDimitry Andric return cast<LoadInst>(&Inst); 146306c3fb27SDimitry Andric 146406c3fb27SDimitry Andric // Otherwise there is something in the same BB clobbers the memory, we can't 146506c3fb27SDimitry Andric // move this and later load to PredBB. 146606c3fb27SDimitry Andric return nullptr; 146706c3fb27SDimitry Andric } 146806c3fb27SDimitry Andric 146906c3fb27SDimitry Andric return nullptr; 147006c3fb27SDimitry Andric } 147106c3fb27SDimitry Andric 1472349cc55cSDimitry Andric void GVNPass::eliminatePartiallyRedundantLoad( 1473fe6060f1SDimitry Andric LoadInst *Load, AvailValInBlkVect &ValuesPerBlock, 147406c3fb27SDimitry Andric MapVector<BasicBlock *, Value *> &AvailableLoads, 147506c3fb27SDimitry Andric MapVector<BasicBlock *, LoadInst *> *CriticalEdgePredAndLoad) { 1476fe6060f1SDimitry Andric for (const auto &AvailableLoad : AvailableLoads) { 1477fe6060f1SDimitry Andric BasicBlock *UnavailableBlock = AvailableLoad.first; 1478fe6060f1SDimitry Andric Value *LoadPtr = AvailableLoad.second; 1479fe6060f1SDimitry Andric 1480*0fca6ea1SDimitry Andric auto *NewLoad = new LoadInst( 1481*0fca6ea1SDimitry Andric Load->getType(), LoadPtr, Load->getName() + ".pre", Load->isVolatile(), 1482*0fca6ea1SDimitry Andric Load->getAlign(), Load->getOrdering(), Load->getSyncScopeID(), 1483*0fca6ea1SDimitry Andric UnavailableBlock->getTerminator()->getIterator()); 1484fe6060f1SDimitry Andric NewLoad->setDebugLoc(Load->getDebugLoc()); 1485fe6060f1SDimitry Andric if (MSSAU) { 1486fe6060f1SDimitry Andric auto *NewAccess = MSSAU->createMemoryAccessInBB( 14875f757f3fSDimitry Andric NewLoad, nullptr, NewLoad->getParent(), MemorySSA::BeforeTerminator); 1488fe6060f1SDimitry Andric if (auto *NewDef = dyn_cast<MemoryDef>(NewAccess)) 1489fe6060f1SDimitry Andric MSSAU->insertDef(NewDef, /*RenameUses=*/true); 1490fe6060f1SDimitry Andric else 1491fe6060f1SDimitry Andric MSSAU->insertUse(cast<MemoryUse>(NewAccess), /*RenameUses=*/true); 1492fe6060f1SDimitry Andric } 1493fe6060f1SDimitry Andric 1494fe6060f1SDimitry Andric // Transfer the old load's AA tags to the new load. 1495349cc55cSDimitry Andric AAMDNodes Tags = Load->getAAMetadata(); 1496fe6060f1SDimitry Andric if (Tags) 1497fe6060f1SDimitry Andric NewLoad->setAAMetadata(Tags); 1498fe6060f1SDimitry Andric 1499fe6060f1SDimitry Andric if (auto *MD = Load->getMetadata(LLVMContext::MD_invariant_load)) 1500fe6060f1SDimitry Andric NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD); 1501fe6060f1SDimitry Andric if (auto *InvGroupMD = Load->getMetadata(LLVMContext::MD_invariant_group)) 1502fe6060f1SDimitry Andric NewLoad->setMetadata(LLVMContext::MD_invariant_group, InvGroupMD); 1503fe6060f1SDimitry Andric if (auto *RangeMD = Load->getMetadata(LLVMContext::MD_range)) 1504fe6060f1SDimitry Andric NewLoad->setMetadata(LLVMContext::MD_range, RangeMD); 1505fe6060f1SDimitry Andric if (auto *AccessMD = Load->getMetadata(LLVMContext::MD_access_group)) 15065f757f3fSDimitry Andric if (LI->getLoopFor(Load->getParent()) == LI->getLoopFor(UnavailableBlock)) 1507fe6060f1SDimitry Andric NewLoad->setMetadata(LLVMContext::MD_access_group, AccessMD); 1508fe6060f1SDimitry Andric 1509fe6060f1SDimitry Andric // We do not propagate the old load's debug location, because the new 1510fe6060f1SDimitry Andric // load now lives in a different BB, and we want to avoid a jumpy line 1511fe6060f1SDimitry Andric // table. 1512fe6060f1SDimitry Andric // FIXME: How do we retain source locations without causing poor debugging 1513fe6060f1SDimitry Andric // behavior? 1514fe6060f1SDimitry Andric 1515fe6060f1SDimitry Andric // Add the newly created load. 1516fe6060f1SDimitry Andric ValuesPerBlock.push_back( 1517fe6060f1SDimitry Andric AvailableValueInBlock::get(UnavailableBlock, NewLoad)); 1518fe6060f1SDimitry Andric MD->invalidateCachedPointerInfo(LoadPtr); 1519fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n'); 152006c3fb27SDimitry Andric 152106c3fb27SDimitry Andric // For PredBB in CriticalEdgePredAndLoad we need to replace the uses of old 152206c3fb27SDimitry Andric // load instruction with the new created load instruction. 152306c3fb27SDimitry Andric if (CriticalEdgePredAndLoad) { 152406c3fb27SDimitry Andric auto I = CriticalEdgePredAndLoad->find(UnavailableBlock); 152506c3fb27SDimitry Andric if (I != CriticalEdgePredAndLoad->end()) { 152606c3fb27SDimitry Andric ++NumPRELoadMoved2CEPred; 152706c3fb27SDimitry Andric ICF->insertInstructionTo(NewLoad, UnavailableBlock); 152806c3fb27SDimitry Andric LoadInst *OldLoad = I->second; 152906c3fb27SDimitry Andric combineMetadataForCSE(NewLoad, OldLoad, false); 153006c3fb27SDimitry Andric OldLoad->replaceAllUsesWith(NewLoad); 153106c3fb27SDimitry Andric replaceValuesPerBlockEntry(ValuesPerBlock, OldLoad, NewLoad); 153206c3fb27SDimitry Andric if (uint32_t ValNo = VN.lookup(OldLoad, false)) 1533*0fca6ea1SDimitry Andric LeaderTable.erase(ValNo, OldLoad, OldLoad->getParent()); 153406c3fb27SDimitry Andric VN.erase(OldLoad); 153506c3fb27SDimitry Andric removeInstruction(OldLoad); 153606c3fb27SDimitry Andric } 153706c3fb27SDimitry Andric } 1538fe6060f1SDimitry Andric } 1539fe6060f1SDimitry Andric 1540fe6060f1SDimitry Andric // Perform PHI construction. 1541fe6060f1SDimitry Andric Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this); 154206c3fb27SDimitry Andric // ConstructSSAForLoadSet is responsible for combining metadata. 15435f757f3fSDimitry Andric ICF->removeUsersOf(Load); 1544fe6060f1SDimitry Andric Load->replaceAllUsesWith(V); 1545fe6060f1SDimitry Andric if (isa<PHINode>(V)) 1546fe6060f1SDimitry Andric V->takeName(Load); 1547fe6060f1SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(V)) 1548fe6060f1SDimitry Andric I->setDebugLoc(Load->getDebugLoc()); 1549fe6060f1SDimitry Andric if (V->getType()->isPtrOrPtrVectorTy()) 1550fe6060f1SDimitry Andric MD->invalidateCachedPointerInfo(V); 1551fe6060f1SDimitry Andric markInstructionForDeletion(Load); 1552fe6060f1SDimitry Andric ORE->emit([&]() { 1553fe6060f1SDimitry Andric return OptimizationRemark(DEBUG_TYPE, "LoadPRE", Load) 1554fe6060f1SDimitry Andric << "load eliminated by PRE"; 1555fe6060f1SDimitry Andric }); 1556fe6060f1SDimitry Andric } 1557fe6060f1SDimitry Andric 1558349cc55cSDimitry Andric bool GVNPass::PerformLoadPRE(LoadInst *Load, AvailValInBlkVect &ValuesPerBlock, 15590b57cec5SDimitry Andric UnavailBlkVect &UnavailableBlocks) { 15600b57cec5SDimitry Andric // Okay, we have *some* definitions of the value. This means that the value 15610b57cec5SDimitry Andric // is available in some of our (transitive) predecessors. Lets think about 15620b57cec5SDimitry Andric // doing PRE of this load. This will involve inserting a new load into the 15630b57cec5SDimitry Andric // predecessor when it's not available. We could do this in general, but 15640b57cec5SDimitry Andric // prefer to not increase code size. As such, we only do this when we know 15650b57cec5SDimitry Andric // that we only have to insert *one* load (which means we're basically moving 15660b57cec5SDimitry Andric // the load, not inserting a new one). 15670b57cec5SDimitry Andric 15680b57cec5SDimitry Andric SmallPtrSet<BasicBlock *, 4> Blockers(UnavailableBlocks.begin(), 15690b57cec5SDimitry Andric UnavailableBlocks.end()); 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric // Let's find the first basic block with more than one predecessor. Walk 15720b57cec5SDimitry Andric // backwards through predecessors if needed. 1573fe6060f1SDimitry Andric BasicBlock *LoadBB = Load->getParent(); 15740b57cec5SDimitry Andric BasicBlock *TmpBB = LoadBB; 15750b57cec5SDimitry Andric 15760b57cec5SDimitry Andric // Check that there is no implicit control flow instructions above our load in 15770b57cec5SDimitry Andric // its block. If there is an instruction that doesn't always pass the 15780b57cec5SDimitry Andric // execution to the following instruction, then moving through it may become 15790b57cec5SDimitry Andric // invalid. For example: 15800b57cec5SDimitry Andric // 15810b57cec5SDimitry Andric // int arr[LEN]; 15820b57cec5SDimitry Andric // int index = ???; 15830b57cec5SDimitry Andric // ... 15840b57cec5SDimitry Andric // guard(0 <= index && index < LEN); 15850b57cec5SDimitry Andric // use(arr[index]); 15860b57cec5SDimitry Andric // 15870b57cec5SDimitry Andric // It is illegal to move the array access to any point above the guard, 15880b57cec5SDimitry Andric // because if the index is out of bounds we should deoptimize rather than 15890b57cec5SDimitry Andric // access the array. 15900b57cec5SDimitry Andric // Check that there is no guard in this block above our instruction. 1591e8d8bef9SDimitry Andric bool MustEnsureSafetyOfSpeculativeExecution = 1592fe6060f1SDimitry Andric ICF->isDominatedByICFIFromSameBlock(Load); 1593e8d8bef9SDimitry Andric 15940b57cec5SDimitry Andric while (TmpBB->getSinglePredecessor()) { 15950b57cec5SDimitry Andric TmpBB = TmpBB->getSinglePredecessor(); 15960b57cec5SDimitry Andric if (TmpBB == LoadBB) // Infinite (unreachable) loop. 15970b57cec5SDimitry Andric return false; 15980b57cec5SDimitry Andric if (Blockers.count(TmpBB)) 15990b57cec5SDimitry Andric return false; 16000b57cec5SDimitry Andric 16010b57cec5SDimitry Andric // If any of these blocks has more than one successor (i.e. if the edge we 16020b57cec5SDimitry Andric // just traversed was critical), then there are other paths through this 16030b57cec5SDimitry Andric // block along which the load may not be anticipated. Hoisting the load 16040b57cec5SDimitry Andric // above this block would be adding the load to execution paths along 16050b57cec5SDimitry Andric // which it was not previously executed. 16060b57cec5SDimitry Andric if (TmpBB->getTerminator()->getNumSuccessors() != 1) 16070b57cec5SDimitry Andric return false; 16080b57cec5SDimitry Andric 16090b57cec5SDimitry Andric // Check that there is no implicit control flow in a block above. 1610e8d8bef9SDimitry Andric MustEnsureSafetyOfSpeculativeExecution = 1611e8d8bef9SDimitry Andric MustEnsureSafetyOfSpeculativeExecution || ICF->hasICF(TmpBB); 16120b57cec5SDimitry Andric } 16130b57cec5SDimitry Andric 16140b57cec5SDimitry Andric assert(TmpBB); 16150b57cec5SDimitry Andric LoadBB = TmpBB; 16160b57cec5SDimitry Andric 16170b57cec5SDimitry Andric // Check to see how many predecessors have the loaded value fully 16180b57cec5SDimitry Andric // available. 16190b57cec5SDimitry Andric MapVector<BasicBlock *, Value *> PredLoads; 1620e8d8bef9SDimitry Andric DenseMap<BasicBlock *, AvailabilityState> FullyAvailableBlocks; 16210b57cec5SDimitry Andric for (const AvailableValueInBlock &AV : ValuesPerBlock) 1622e8d8bef9SDimitry Andric FullyAvailableBlocks[AV.BB] = AvailabilityState::Available; 16230b57cec5SDimitry Andric for (BasicBlock *UnavailableBB : UnavailableBlocks) 1624e8d8bef9SDimitry Andric FullyAvailableBlocks[UnavailableBB] = AvailabilityState::Unavailable; 16250b57cec5SDimitry Andric 162606c3fb27SDimitry Andric // The edge from Pred to LoadBB is a critical edge will be splitted. 162706c3fb27SDimitry Andric SmallVector<BasicBlock *, 4> CriticalEdgePredSplit; 162806c3fb27SDimitry Andric // The edge from Pred to LoadBB is a critical edge, another successor of Pred 162906c3fb27SDimitry Andric // contains a load can be moved to Pred. This data structure maps the Pred to 163006c3fb27SDimitry Andric // the movable load. 163106c3fb27SDimitry Andric MapVector<BasicBlock *, LoadInst *> CriticalEdgePredAndLoad; 16320b57cec5SDimitry Andric for (BasicBlock *Pred : predecessors(LoadBB)) { 16330b57cec5SDimitry Andric // If any predecessor block is an EH pad that does not allow non-PHI 16340b57cec5SDimitry Andric // instructions before the terminator, we can't PRE the load. 16350b57cec5SDimitry Andric if (Pred->getTerminator()->isEHPad()) { 16360b57cec5SDimitry Andric LLVM_DEBUG( 16370b57cec5SDimitry Andric dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD PREDECESSOR '" 1638fe6060f1SDimitry Andric << Pred->getName() << "': " << *Load << '\n'); 16390b57cec5SDimitry Andric return false; 16400b57cec5SDimitry Andric } 16410b57cec5SDimitry Andric 1642e8d8bef9SDimitry Andric if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks)) { 16430b57cec5SDimitry Andric continue; 16440b57cec5SDimitry Andric } 16450b57cec5SDimitry Andric 16460b57cec5SDimitry Andric if (Pred->getTerminator()->getNumSuccessors() != 1) { 16470b57cec5SDimitry Andric if (isa<IndirectBrInst>(Pred->getTerminator())) { 16480b57cec5SDimitry Andric LLVM_DEBUG( 16490b57cec5SDimitry Andric dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '" 1650fe6060f1SDimitry Andric << Pred->getName() << "': " << *Load << '\n'); 16510b57cec5SDimitry Andric return false; 16520b57cec5SDimitry Andric } 16530b57cec5SDimitry Andric 16540b57cec5SDimitry Andric if (LoadBB->isEHPad()) { 16550b57cec5SDimitry Andric LLVM_DEBUG( 16560b57cec5SDimitry Andric dbgs() << "COULD NOT PRE LOAD BECAUSE OF AN EH PAD CRITICAL EDGE '" 1657fe6060f1SDimitry Andric << Pred->getName() << "': " << *Load << '\n'); 16580b57cec5SDimitry Andric return false; 16590b57cec5SDimitry Andric } 16600b57cec5SDimitry Andric 1661e8d8bef9SDimitry Andric // Do not split backedge as it will break the canonical loop form. 1662e8d8bef9SDimitry Andric if (!isLoadPRESplitBackedgeEnabled()) 1663e8d8bef9SDimitry Andric if (DT->dominates(LoadBB, Pred)) { 1664e8d8bef9SDimitry Andric LLVM_DEBUG( 1665e8d8bef9SDimitry Andric dbgs() 1666e8d8bef9SDimitry Andric << "COULD NOT PRE LOAD BECAUSE OF A BACKEDGE CRITICAL EDGE '" 1667fe6060f1SDimitry Andric << Pred->getName() << "': " << *Load << '\n'); 1668e8d8bef9SDimitry Andric return false; 1669e8d8bef9SDimitry Andric } 1670e8d8bef9SDimitry Andric 167106c3fb27SDimitry Andric if (LoadInst *LI = findLoadToHoistIntoPred(Pred, LoadBB, Load)) 167206c3fb27SDimitry Andric CriticalEdgePredAndLoad[Pred] = LI; 167306c3fb27SDimitry Andric else 167406c3fb27SDimitry Andric CriticalEdgePredSplit.push_back(Pred); 16750b57cec5SDimitry Andric } else { 16760b57cec5SDimitry Andric // Only add the predecessors that will not be split for now. 16770b57cec5SDimitry Andric PredLoads[Pred] = nullptr; 16780b57cec5SDimitry Andric } 16790b57cec5SDimitry Andric } 16800b57cec5SDimitry Andric 16810b57cec5SDimitry Andric // Decide whether PRE is profitable for this load. 168206c3fb27SDimitry Andric unsigned NumInsertPreds = PredLoads.size() + CriticalEdgePredSplit.size(); 168306c3fb27SDimitry Andric unsigned NumUnavailablePreds = NumInsertPreds + 168406c3fb27SDimitry Andric CriticalEdgePredAndLoad.size(); 16850b57cec5SDimitry Andric assert(NumUnavailablePreds != 0 && 16860b57cec5SDimitry Andric "Fully available value should already be eliminated!"); 168706c3fb27SDimitry Andric (void)NumUnavailablePreds; 16880b57cec5SDimitry Andric 168906c3fb27SDimitry Andric // If we need to insert new load in multiple predecessors, reject it. 16900b57cec5SDimitry Andric // FIXME: If we could restructure the CFG, we could make a common pred with 1691fe6060f1SDimitry Andric // all the preds that don't have an available Load and insert a new load into 16920b57cec5SDimitry Andric // that one block. 169306c3fb27SDimitry Andric if (NumInsertPreds > 1) 16940b57cec5SDimitry Andric return false; 16950b57cec5SDimitry Andric 1696e8d8bef9SDimitry Andric // Now we know where we will insert load. We must ensure that it is safe 1697e8d8bef9SDimitry Andric // to speculatively execute the load at that points. 1698e8d8bef9SDimitry Andric if (MustEnsureSafetyOfSpeculativeExecution) { 169906c3fb27SDimitry Andric if (CriticalEdgePredSplit.size()) 1700bdd1243dSDimitry Andric if (!isSafeToSpeculativelyExecute(Load, LoadBB->getFirstNonPHI(), AC, DT)) 1701e8d8bef9SDimitry Andric return false; 1702e8d8bef9SDimitry Andric for (auto &PL : PredLoads) 1703bdd1243dSDimitry Andric if (!isSafeToSpeculativelyExecute(Load, PL.first->getTerminator(), AC, 1704bdd1243dSDimitry Andric DT)) 1705e8d8bef9SDimitry Andric return false; 170606c3fb27SDimitry Andric for (auto &CEP : CriticalEdgePredAndLoad) 170706c3fb27SDimitry Andric if (!isSafeToSpeculativelyExecute(Load, CEP.first->getTerminator(), AC, 170806c3fb27SDimitry Andric DT)) 170906c3fb27SDimitry Andric return false; 1710e8d8bef9SDimitry Andric } 1711e8d8bef9SDimitry Andric 17120b57cec5SDimitry Andric // Split critical edges, and update the unavailable predecessors accordingly. 171306c3fb27SDimitry Andric for (BasicBlock *OrigPred : CriticalEdgePredSplit) { 17140b57cec5SDimitry Andric BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB); 17150b57cec5SDimitry Andric assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!"); 17160b57cec5SDimitry Andric PredLoads[NewPred] = nullptr; 17170b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->" 17180b57cec5SDimitry Andric << LoadBB->getName() << '\n'); 17190b57cec5SDimitry Andric } 17200b57cec5SDimitry Andric 172106c3fb27SDimitry Andric for (auto &CEP : CriticalEdgePredAndLoad) 172206c3fb27SDimitry Andric PredLoads[CEP.first] = nullptr; 172306c3fb27SDimitry Andric 17240b57cec5SDimitry Andric // Check if the load can safely be moved to all the unavailable predecessors. 17250b57cec5SDimitry Andric bool CanDoPRE = true; 1726*0fca6ea1SDimitry Andric const DataLayout &DL = Load->getDataLayout(); 17270b57cec5SDimitry Andric SmallVector<Instruction*, 8> NewInsts; 17280b57cec5SDimitry Andric for (auto &PredLoad : PredLoads) { 17290b57cec5SDimitry Andric BasicBlock *UnavailablePred = PredLoad.first; 17300b57cec5SDimitry Andric 17310b57cec5SDimitry Andric // Do PHI translation to get its value in the predecessor if necessary. The 17320b57cec5SDimitry Andric // returned pointer (if non-null) is guaranteed to dominate UnavailablePred. 1733fe6060f1SDimitry Andric // We do the translation for each edge we skipped by going from Load's block 17348bcb0991SDimitry Andric // to LoadBB, otherwise we might miss pieces needing translation. 17350b57cec5SDimitry Andric 17360b57cec5SDimitry Andric // If all preds have a single successor, then we know it is safe to insert 17370b57cec5SDimitry Andric // the load on the pred (?!?), so we can insert code to materialize the 17380b57cec5SDimitry Andric // pointer if it is not available. 1739fe6060f1SDimitry Andric Value *LoadPtr = Load->getPointerOperand(); 1740fe6060f1SDimitry Andric BasicBlock *Cur = Load->getParent(); 17418bcb0991SDimitry Andric while (Cur != LoadBB) { 17428bcb0991SDimitry Andric PHITransAddr Address(LoadPtr, DL, AC); 174306c3fb27SDimitry Andric LoadPtr = Address.translateWithInsertion(Cur, Cur->getSinglePredecessor(), 174406c3fb27SDimitry Andric *DT, NewInsts); 17458bcb0991SDimitry Andric if (!LoadPtr) { 17468bcb0991SDimitry Andric CanDoPRE = false; 17478bcb0991SDimitry Andric break; 17488bcb0991SDimitry Andric } 17498bcb0991SDimitry Andric Cur = Cur->getSinglePredecessor(); 17508bcb0991SDimitry Andric } 17510b57cec5SDimitry Andric 17528bcb0991SDimitry Andric if (LoadPtr) { 17538bcb0991SDimitry Andric PHITransAddr Address(LoadPtr, DL, AC); 175406c3fb27SDimitry Andric LoadPtr = Address.translateWithInsertion(LoadBB, UnavailablePred, *DT, 17558bcb0991SDimitry Andric NewInsts); 17568bcb0991SDimitry Andric } 17570b57cec5SDimitry Andric // If we couldn't find or insert a computation of this phi translated value, 17580b57cec5SDimitry Andric // we fail PRE. 17590b57cec5SDimitry Andric if (!LoadPtr) { 17600b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: " 1761fe6060f1SDimitry Andric << *Load->getPointerOperand() << "\n"); 17620b57cec5SDimitry Andric CanDoPRE = false; 17630b57cec5SDimitry Andric break; 17640b57cec5SDimitry Andric } 17650b57cec5SDimitry Andric 17660b57cec5SDimitry Andric PredLoad.second = LoadPtr; 17670b57cec5SDimitry Andric } 17680b57cec5SDimitry Andric 17690b57cec5SDimitry Andric if (!CanDoPRE) { 17700b57cec5SDimitry Andric while (!NewInsts.empty()) { 17718bcb0991SDimitry Andric // Erase instructions generated by the failed PHI translation before 17728bcb0991SDimitry Andric // trying to number them. PHI translation might insert instructions 17738bcb0991SDimitry Andric // in basic blocks other than the current one, and we delete them 17748bcb0991SDimitry Andric // directly, as markInstructionForDeletion only allows removing from the 17758bcb0991SDimitry Andric // current basic block. 17768bcb0991SDimitry Andric NewInsts.pop_back_val()->eraseFromParent(); 17770b57cec5SDimitry Andric } 17780b57cec5SDimitry Andric // HINT: Don't revert the edge-splitting as following transformation may 17790b57cec5SDimitry Andric // also need to split these critical edges. 178006c3fb27SDimitry Andric return !CriticalEdgePredSplit.empty(); 17810b57cec5SDimitry Andric } 17820b57cec5SDimitry Andric 17830b57cec5SDimitry Andric // Okay, we can eliminate this load by inserting a reload in the predecessor 17840b57cec5SDimitry Andric // and using PHI construction to get the value in the other predecessors, do 17850b57cec5SDimitry Andric // it. 1786fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *Load << '\n'); 1787fe6060f1SDimitry Andric LLVM_DEBUG(if (!NewInsts.empty()) dbgs() << "INSERTED " << NewInsts.size() 1788fe6060f1SDimitry Andric << " INSTS: " << *NewInsts.back() 17890b57cec5SDimitry Andric << '\n'); 17900b57cec5SDimitry Andric 17910b57cec5SDimitry Andric // Assign value numbers to the new instructions. 17920b57cec5SDimitry Andric for (Instruction *I : NewInsts) { 17930b57cec5SDimitry Andric // Instructions that have been inserted in predecessor(s) to materialize 17940b57cec5SDimitry Andric // the load address do not retain their original debug locations. Doing 17950b57cec5SDimitry Andric // so could lead to confusing (but correct) source attributions. 1796e8d8bef9SDimitry Andric I->updateLocationAfterHoist(); 17970b57cec5SDimitry Andric 17980b57cec5SDimitry Andric // FIXME: We really _ought_ to insert these value numbers into their 17990b57cec5SDimitry Andric // parent's availability map. However, in doing so, we risk getting into 18000b57cec5SDimitry Andric // ordering issues. If a block hasn't been processed yet, we would be 18010b57cec5SDimitry Andric // marking a value as AVAIL-IN, which isn't what we intend. 18020b57cec5SDimitry Andric VN.lookupOrAdd(I); 18030b57cec5SDimitry Andric } 18040b57cec5SDimitry Andric 180506c3fb27SDimitry Andric eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, PredLoads, 180606c3fb27SDimitry Andric &CriticalEdgePredAndLoad); 18070b57cec5SDimitry Andric ++NumPRELoad; 18080b57cec5SDimitry Andric return true; 18090b57cec5SDimitry Andric } 18100b57cec5SDimitry Andric 1811349cc55cSDimitry Andric bool GVNPass::performLoopLoadPRE(LoadInst *Load, 1812349cc55cSDimitry Andric AvailValInBlkVect &ValuesPerBlock, 1813fe6060f1SDimitry Andric UnavailBlkVect &UnavailableBlocks) { 1814fe6060f1SDimitry Andric const Loop *L = LI->getLoopFor(Load->getParent()); 1815fe6060f1SDimitry Andric // TODO: Generalize to other loop blocks that dominate the latch. 1816fe6060f1SDimitry Andric if (!L || L->getHeader() != Load->getParent()) 1817fe6060f1SDimitry Andric return false; 1818fe6060f1SDimitry Andric 1819fe6060f1SDimitry Andric BasicBlock *Preheader = L->getLoopPreheader(); 1820fe6060f1SDimitry Andric BasicBlock *Latch = L->getLoopLatch(); 1821fe6060f1SDimitry Andric if (!Preheader || !Latch) 1822fe6060f1SDimitry Andric return false; 1823fe6060f1SDimitry Andric 1824fe6060f1SDimitry Andric Value *LoadPtr = Load->getPointerOperand(); 1825fe6060f1SDimitry Andric // Must be available in preheader. 1826fe6060f1SDimitry Andric if (!L->isLoopInvariant(LoadPtr)) 1827fe6060f1SDimitry Andric return false; 1828fe6060f1SDimitry Andric 1829fe6060f1SDimitry Andric // We plan to hoist the load to preheader without introducing a new fault. 1830fe6060f1SDimitry Andric // In order to do it, we need to prove that we cannot side-exit the loop 1831fe6060f1SDimitry Andric // once loop header is first entered before execution of the load. 1832fe6060f1SDimitry Andric if (ICF->isDominatedByICFIFromSameBlock(Load)) 1833fe6060f1SDimitry Andric return false; 1834fe6060f1SDimitry Andric 1835fe6060f1SDimitry Andric BasicBlock *LoopBlock = nullptr; 1836fe6060f1SDimitry Andric for (auto *Blocker : UnavailableBlocks) { 1837fe6060f1SDimitry Andric // Blockers from outside the loop are handled in preheader. 1838fe6060f1SDimitry Andric if (!L->contains(Blocker)) 1839fe6060f1SDimitry Andric continue; 1840fe6060f1SDimitry Andric 1841fe6060f1SDimitry Andric // Only allow one loop block. Loop header is not less frequently executed 1842fe6060f1SDimitry Andric // than each loop block, and likely it is much more frequently executed. But 1843fe6060f1SDimitry Andric // in case of multiple loop blocks, we need extra information (such as block 1844fe6060f1SDimitry Andric // frequency info) to understand whether it is profitable to PRE into 1845fe6060f1SDimitry Andric // multiple loop blocks. 1846fe6060f1SDimitry Andric if (LoopBlock) 1847fe6060f1SDimitry Andric return false; 1848fe6060f1SDimitry Andric 1849fe6060f1SDimitry Andric // Do not sink into inner loops. This may be non-profitable. 1850fe6060f1SDimitry Andric if (L != LI->getLoopFor(Blocker)) 1851fe6060f1SDimitry Andric return false; 1852fe6060f1SDimitry Andric 1853fe6060f1SDimitry Andric // Blocks that dominate the latch execute on every single iteration, maybe 1854fe6060f1SDimitry Andric // except the last one. So PREing into these blocks doesn't make much sense 1855fe6060f1SDimitry Andric // in most cases. But the blocks that do not necessarily execute on each 1856fe6060f1SDimitry Andric // iteration are sometimes much colder than the header, and this is when 1857fe6060f1SDimitry Andric // PRE is potentially profitable. 1858fe6060f1SDimitry Andric if (DT->dominates(Blocker, Latch)) 1859fe6060f1SDimitry Andric return false; 1860fe6060f1SDimitry Andric 1861fe6060f1SDimitry Andric // Make sure that the terminator itself doesn't clobber. 1862fe6060f1SDimitry Andric if (Blocker->getTerminator()->mayWriteToMemory()) 1863fe6060f1SDimitry Andric return false; 1864fe6060f1SDimitry Andric 1865fe6060f1SDimitry Andric LoopBlock = Blocker; 1866fe6060f1SDimitry Andric } 1867fe6060f1SDimitry Andric 1868fe6060f1SDimitry Andric if (!LoopBlock) 1869fe6060f1SDimitry Andric return false; 1870fe6060f1SDimitry Andric 1871fe6060f1SDimitry Andric // Make sure the memory at this pointer cannot be freed, therefore we can 1872fe6060f1SDimitry Andric // safely reload from it after clobber. 1873fe6060f1SDimitry Andric if (LoadPtr->canBeFreed()) 1874fe6060f1SDimitry Andric return false; 1875fe6060f1SDimitry Andric 1876fe6060f1SDimitry Andric // TODO: Support critical edge splitting if blocker has more than 1 successor. 1877fe6060f1SDimitry Andric MapVector<BasicBlock *, Value *> AvailableLoads; 1878fe6060f1SDimitry Andric AvailableLoads[LoopBlock] = LoadPtr; 1879fe6060f1SDimitry Andric AvailableLoads[Preheader] = LoadPtr; 1880fe6060f1SDimitry Andric 1881fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "GVN REMOVING PRE LOOP LOAD: " << *Load << '\n'); 188206c3fb27SDimitry Andric eliminatePartiallyRedundantLoad(Load, ValuesPerBlock, AvailableLoads, 188306c3fb27SDimitry Andric /*CriticalEdgePredAndLoad*/ nullptr); 1884fe6060f1SDimitry Andric ++NumPRELoopLoad; 1885fe6060f1SDimitry Andric return true; 1886fe6060f1SDimitry Andric } 1887fe6060f1SDimitry Andric 1888fe6060f1SDimitry Andric static void reportLoadElim(LoadInst *Load, Value *AvailableValue, 18890b57cec5SDimitry Andric OptimizationRemarkEmitter *ORE) { 18900b57cec5SDimitry Andric using namespace ore; 18910b57cec5SDimitry Andric 18920b57cec5SDimitry Andric ORE->emit([&]() { 1893fe6060f1SDimitry Andric return OptimizationRemark(DEBUG_TYPE, "LoadElim", Load) 1894fe6060f1SDimitry Andric << "load of type " << NV("Type", Load->getType()) << " eliminated" 18950b57cec5SDimitry Andric << setExtraArgs() << " in favor of " 18960b57cec5SDimitry Andric << NV("InfavorOfValue", AvailableValue); 18970b57cec5SDimitry Andric }); 18980b57cec5SDimitry Andric } 18990b57cec5SDimitry Andric 19000b57cec5SDimitry Andric /// Attempt to eliminate a load whose dependencies are 19010b57cec5SDimitry Andric /// non-local by performing PHI construction. 1902349cc55cSDimitry Andric bool GVNPass::processNonLocalLoad(LoadInst *Load) { 19030b57cec5SDimitry Andric // non-local speculations are not allowed under asan. 1904fe6060f1SDimitry Andric if (Load->getParent()->getParent()->hasFnAttribute( 19050b57cec5SDimitry Andric Attribute::SanitizeAddress) || 1906fe6060f1SDimitry Andric Load->getParent()->getParent()->hasFnAttribute( 19070b57cec5SDimitry Andric Attribute::SanitizeHWAddress)) 19080b57cec5SDimitry Andric return false; 19090b57cec5SDimitry Andric 19100b57cec5SDimitry Andric // Step 1: Find the non-local dependencies of the load. 19110b57cec5SDimitry Andric LoadDepVect Deps; 1912fe6060f1SDimitry Andric MD->getNonLocalPointerDependency(Load, Deps); 19130b57cec5SDimitry Andric 19140b57cec5SDimitry Andric // If we had to process more than one hundred blocks to find the 19150b57cec5SDimitry Andric // dependencies, this load isn't worth worrying about. Optimizing 19160b57cec5SDimitry Andric // it will be too expensive. 19170b57cec5SDimitry Andric unsigned NumDeps = Deps.size(); 19180b57cec5SDimitry Andric if (NumDeps > MaxNumDeps) 19190b57cec5SDimitry Andric return false; 19200b57cec5SDimitry Andric 19210b57cec5SDimitry Andric // If we had a phi translation failure, we'll have a single entry which is a 19220b57cec5SDimitry Andric // clobber in the current block. Reject this early. 19230b57cec5SDimitry Andric if (NumDeps == 1 && 19240b57cec5SDimitry Andric !Deps[0].getResult().isDef() && !Deps[0].getResult().isClobber()) { 1925fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "GVN: non-local load "; Load->printAsOperand(dbgs()); 19260b57cec5SDimitry Andric dbgs() << " has unknown dependencies\n";); 19270b57cec5SDimitry Andric return false; 19280b57cec5SDimitry Andric } 19290b57cec5SDimitry Andric 1930e8d8bef9SDimitry Andric bool Changed = false; 19310b57cec5SDimitry Andric // If this load follows a GEP, see if we can PRE the indices before analyzing. 1932fe6060f1SDimitry Andric if (GetElementPtrInst *GEP = 1933fe6060f1SDimitry Andric dyn_cast<GetElementPtrInst>(Load->getOperand(0))) { 1934349cc55cSDimitry Andric for (Use &U : GEP->indices()) 1935349cc55cSDimitry Andric if (Instruction *I = dyn_cast<Instruction>(U.get())) 1936e8d8bef9SDimitry Andric Changed |= performScalarPRE(I); 19370b57cec5SDimitry Andric } 19380b57cec5SDimitry Andric 19390b57cec5SDimitry Andric // Step 2: Analyze the availability of the load 19400b57cec5SDimitry Andric AvailValInBlkVect ValuesPerBlock; 19410b57cec5SDimitry Andric UnavailBlkVect UnavailableBlocks; 1942fe6060f1SDimitry Andric AnalyzeLoadAvailability(Load, Deps, ValuesPerBlock, UnavailableBlocks); 19430b57cec5SDimitry Andric 19440b57cec5SDimitry Andric // If we have no predecessors that produce a known value for this load, exit 19450b57cec5SDimitry Andric // early. 19460b57cec5SDimitry Andric if (ValuesPerBlock.empty()) 1947e8d8bef9SDimitry Andric return Changed; 19480b57cec5SDimitry Andric 19490b57cec5SDimitry Andric // Step 3: Eliminate fully redundancy. 19500b57cec5SDimitry Andric // 19510b57cec5SDimitry Andric // If all of the instructions we depend on produce a known value for this 19520b57cec5SDimitry Andric // load, then it is fully redundant and we can use PHI insertion to compute 19530b57cec5SDimitry Andric // its value. Insert PHIs and remove the fully redundant value now. 19540b57cec5SDimitry Andric if (UnavailableBlocks.empty()) { 1955fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *Load << '\n'); 19560b57cec5SDimitry Andric 19570b57cec5SDimitry Andric // Perform PHI construction. 1958fe6060f1SDimitry Andric Value *V = ConstructSSAForLoadSet(Load, ValuesPerBlock, *this); 195906c3fb27SDimitry Andric // ConstructSSAForLoadSet is responsible for combining metadata. 19605f757f3fSDimitry Andric ICF->removeUsersOf(Load); 1961fe6060f1SDimitry Andric Load->replaceAllUsesWith(V); 19620b57cec5SDimitry Andric 19630b57cec5SDimitry Andric if (isa<PHINode>(V)) 1964fe6060f1SDimitry Andric V->takeName(Load); 19650b57cec5SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(V)) 19660b57cec5SDimitry Andric // If instruction I has debug info, then we should not update it. 19670b57cec5SDimitry Andric // Also, if I has a null DebugLoc, then it is still potentially incorrect 1968fe6060f1SDimitry Andric // to propagate Load's DebugLoc because Load may not post-dominate I. 1969fe6060f1SDimitry Andric if (Load->getDebugLoc() && Load->getParent() == I->getParent()) 1970fe6060f1SDimitry Andric I->setDebugLoc(Load->getDebugLoc()); 19710b57cec5SDimitry Andric if (V->getType()->isPtrOrPtrVectorTy()) 19720b57cec5SDimitry Andric MD->invalidateCachedPointerInfo(V); 1973fe6060f1SDimitry Andric markInstructionForDeletion(Load); 19740b57cec5SDimitry Andric ++NumGVNLoad; 1975fe6060f1SDimitry Andric reportLoadElim(Load, V, ORE); 19760b57cec5SDimitry Andric return true; 19770b57cec5SDimitry Andric } 19780b57cec5SDimitry Andric 19790b57cec5SDimitry Andric // Step 4: Eliminate partial redundancy. 19805ffd83dbSDimitry Andric if (!isPREEnabled() || !isLoadPREEnabled()) 1981e8d8bef9SDimitry Andric return Changed; 19825f757f3fSDimitry Andric if (!isLoadInLoopPREEnabled() && LI->getLoopFor(Load->getParent())) 1983e8d8bef9SDimitry Andric return Changed; 19840b57cec5SDimitry Andric 1985349cc55cSDimitry Andric if (performLoopLoadPRE(Load, ValuesPerBlock, UnavailableBlocks) || 1986349cc55cSDimitry Andric PerformLoadPRE(Load, ValuesPerBlock, UnavailableBlocks)) 1987349cc55cSDimitry Andric return true; 1988349cc55cSDimitry Andric 1989349cc55cSDimitry Andric return Changed; 19900b57cec5SDimitry Andric } 19910b57cec5SDimitry Andric 1992480093f4SDimitry Andric static bool impliesEquivalanceIfTrue(CmpInst* Cmp) { 1993480093f4SDimitry Andric if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_EQ) 1994480093f4SDimitry Andric return true; 1995480093f4SDimitry Andric 1996480093f4SDimitry Andric // Floating point comparisons can be equal, but not equivalent. Cases: 1997480093f4SDimitry Andric // NaNs for unordered operators 1998480093f4SDimitry Andric // +0.0 vs 0.0 for all operators 1999480093f4SDimitry Andric if (Cmp->getPredicate() == CmpInst::Predicate::FCMP_OEQ || 2000480093f4SDimitry Andric (Cmp->getPredicate() == CmpInst::Predicate::FCMP_UEQ && 2001480093f4SDimitry Andric Cmp->getFastMathFlags().noNaNs())) { 2002480093f4SDimitry Andric Value *LHS = Cmp->getOperand(0); 2003480093f4SDimitry Andric Value *RHS = Cmp->getOperand(1); 2004480093f4SDimitry Andric // If we can prove either side non-zero, then equality must imply 2005480093f4SDimitry Andric // equivalence. 2006480093f4SDimitry Andric // FIXME: We should do this optimization if 'no signed zeros' is 2007480093f4SDimitry Andric // applicable via an instruction-level fast-math-flag or some other 2008480093f4SDimitry Andric // indicator that relaxed FP semantics are being used. 2009480093f4SDimitry Andric if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero()) 2010480093f4SDimitry Andric return true; 2011480093f4SDimitry Andric if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero()) 201206c3fb27SDimitry Andric return true; 2013480093f4SDimitry Andric // TODO: Handle vector floating point constants 2014480093f4SDimitry Andric } 2015480093f4SDimitry Andric return false; 2016480093f4SDimitry Andric } 2017480093f4SDimitry Andric 2018480093f4SDimitry Andric static bool impliesEquivalanceIfFalse(CmpInst* Cmp) { 2019480093f4SDimitry Andric if (Cmp->getPredicate() == CmpInst::Predicate::ICMP_NE) 2020480093f4SDimitry Andric return true; 2021480093f4SDimitry Andric 2022480093f4SDimitry Andric // Floating point comparisons can be equal, but not equivelent. Cases: 2023480093f4SDimitry Andric // NaNs for unordered operators 2024480093f4SDimitry Andric // +0.0 vs 0.0 for all operators 2025480093f4SDimitry Andric if ((Cmp->getPredicate() == CmpInst::Predicate::FCMP_ONE && 2026480093f4SDimitry Andric Cmp->getFastMathFlags().noNaNs()) || 2027480093f4SDimitry Andric Cmp->getPredicate() == CmpInst::Predicate::FCMP_UNE) { 2028480093f4SDimitry Andric Value *LHS = Cmp->getOperand(0); 2029480093f4SDimitry Andric Value *RHS = Cmp->getOperand(1); 2030480093f4SDimitry Andric // If we can prove either side non-zero, then equality must imply 2031480093f4SDimitry Andric // equivalence. 2032480093f4SDimitry Andric // FIXME: We should do this optimization if 'no signed zeros' is 2033480093f4SDimitry Andric // applicable via an instruction-level fast-math-flag or some other 2034480093f4SDimitry Andric // indicator that relaxed FP semantics are being used. 2035480093f4SDimitry Andric if (isa<ConstantFP>(LHS) && !cast<ConstantFP>(LHS)->isZero()) 2036480093f4SDimitry Andric return true; 2037480093f4SDimitry Andric if (isa<ConstantFP>(RHS) && !cast<ConstantFP>(RHS)->isZero()) 203806c3fb27SDimitry Andric return true; 2039480093f4SDimitry Andric // TODO: Handle vector floating point constants 2040480093f4SDimitry Andric } 2041480093f4SDimitry Andric return false; 2042480093f4SDimitry Andric } 2043480093f4SDimitry Andric 2044480093f4SDimitry Andric 20458bcb0991SDimitry Andric static bool hasUsersIn(Value *V, BasicBlock *BB) { 2046bdd1243dSDimitry Andric return llvm::any_of(V->users(), [BB](User *U) { 2047bdd1243dSDimitry Andric auto *I = dyn_cast<Instruction>(U); 2048bdd1243dSDimitry Andric return I && I->getParent() == BB; 2049bdd1243dSDimitry Andric }); 20508bcb0991SDimitry Andric } 20518bcb0991SDimitry Andric 2052349cc55cSDimitry Andric bool GVNPass::processAssumeIntrinsic(AssumeInst *IntrinsicI) { 20530b57cec5SDimitry Andric Value *V = IntrinsicI->getArgOperand(0); 20540b57cec5SDimitry Andric 20550b57cec5SDimitry Andric if (ConstantInt *Cond = dyn_cast<ConstantInt>(V)) { 20560b57cec5SDimitry Andric if (Cond->isZero()) { 20570b57cec5SDimitry Andric Type *Int8Ty = Type::getInt8Ty(V->getContext()); 20585f757f3fSDimitry Andric Type *PtrTy = PointerType::get(V->getContext(), 0); 20590b57cec5SDimitry Andric // Insert a new store to null instruction before the load to indicate that 20600b57cec5SDimitry Andric // this code is not reachable. FIXME: We could insert unreachable 20610b57cec5SDimitry Andric // instruction directly because we can modify the CFG. 2062*0fca6ea1SDimitry Andric auto *NewS = 2063*0fca6ea1SDimitry Andric new StoreInst(PoisonValue::get(Int8Ty), Constant::getNullValue(PtrTy), 2064*0fca6ea1SDimitry Andric IntrinsicI->getIterator()); 2065e8d8bef9SDimitry Andric if (MSSAU) { 2066e8d8bef9SDimitry Andric const MemoryUseOrDef *FirstNonDom = nullptr; 2067e8d8bef9SDimitry Andric const auto *AL = 2068e8d8bef9SDimitry Andric MSSAU->getMemorySSA()->getBlockAccesses(IntrinsicI->getParent()); 2069e8d8bef9SDimitry Andric 2070e8d8bef9SDimitry Andric // If there are accesses in the current basic block, find the first one 2071e8d8bef9SDimitry Andric // that does not come before NewS. The new memory access is inserted 2072e8d8bef9SDimitry Andric // after the found access or before the terminator if no such access is 2073e8d8bef9SDimitry Andric // found. 2074e8d8bef9SDimitry Andric if (AL) { 2075bdd1243dSDimitry Andric for (const auto &Acc : *AL) { 2076e8d8bef9SDimitry Andric if (auto *Current = dyn_cast<MemoryUseOrDef>(&Acc)) 2077e8d8bef9SDimitry Andric if (!Current->getMemoryInst()->comesBefore(NewS)) { 2078e8d8bef9SDimitry Andric FirstNonDom = Current; 2079e8d8bef9SDimitry Andric break; 2080e8d8bef9SDimitry Andric } 2081e8d8bef9SDimitry Andric } 2082e8d8bef9SDimitry Andric } 2083e8d8bef9SDimitry Andric 2084e8d8bef9SDimitry Andric auto *NewDef = 2085e8d8bef9SDimitry Andric FirstNonDom ? MSSAU->createMemoryAccessBefore( 20865f757f3fSDimitry Andric NewS, nullptr, 2087e8d8bef9SDimitry Andric const_cast<MemoryUseOrDef *>(FirstNonDom)) 2088e8d8bef9SDimitry Andric : MSSAU->createMemoryAccessInBB( 20895f757f3fSDimitry Andric NewS, nullptr, 2090e8d8bef9SDimitry Andric NewS->getParent(), MemorySSA::BeforeTerminator); 2091e8d8bef9SDimitry Andric 2092e8d8bef9SDimitry Andric MSSAU->insertDef(cast<MemoryDef>(NewDef), /*RenameUses=*/false); 2093e8d8bef9SDimitry Andric } 20940b57cec5SDimitry Andric } 209506c3fb27SDimitry Andric if (isAssumeWithEmptyBundle(*IntrinsicI)) { 20960b57cec5SDimitry Andric markInstructionForDeletion(IntrinsicI); 209706c3fb27SDimitry Andric return true; 209806c3fb27SDimitry Andric } 20990b57cec5SDimitry Andric return false; 210006c3fb27SDimitry Andric } 210106c3fb27SDimitry Andric 210206c3fb27SDimitry Andric if (isa<Constant>(V)) { 21030b57cec5SDimitry Andric // If it's not false, and constant, it must evaluate to true. This means our 21040b57cec5SDimitry Andric // assume is assume(true), and thus, pointless, and we don't want to do 21050b57cec5SDimitry Andric // anything more here. 21060b57cec5SDimitry Andric return false; 21070b57cec5SDimitry Andric } 21080b57cec5SDimitry Andric 21090b57cec5SDimitry Andric Constant *True = ConstantInt::getTrue(V->getContext()); 21100b57cec5SDimitry Andric bool Changed = false; 21110b57cec5SDimitry Andric 21120b57cec5SDimitry Andric for (BasicBlock *Successor : successors(IntrinsicI->getParent())) { 21130b57cec5SDimitry Andric BasicBlockEdge Edge(IntrinsicI->getParent(), Successor); 21140b57cec5SDimitry Andric 21150b57cec5SDimitry Andric // This property is only true in dominated successors, propagateEquality 21160b57cec5SDimitry Andric // will check dominance for us. 21170b57cec5SDimitry Andric Changed |= propagateEquality(V, True, Edge, false); 21180b57cec5SDimitry Andric } 21190b57cec5SDimitry Andric 21200b57cec5SDimitry Andric // We can replace assume value with true, which covers cases like this: 21210b57cec5SDimitry Andric // call void @llvm.assume(i1 %cmp) 21220b57cec5SDimitry Andric // br i1 %cmp, label %bb1, label %bb2 ; will change %cmp to true 21238bcb0991SDimitry Andric ReplaceOperandsWithMap[V] = True; 21240b57cec5SDimitry Andric 2125e8d8bef9SDimitry Andric // Similarly, after assume(!NotV) we know that NotV == false. 2126e8d8bef9SDimitry Andric Value *NotV; 2127e8d8bef9SDimitry Andric if (match(V, m_Not(m_Value(NotV)))) 2128e8d8bef9SDimitry Andric ReplaceOperandsWithMap[NotV] = ConstantInt::getFalse(V->getContext()); 2129e8d8bef9SDimitry Andric 21308bcb0991SDimitry Andric // If we find an equality fact, canonicalize all dominated uses in this block 21318bcb0991SDimitry Andric // to one of the two values. We heuristically choice the "oldest" of the 21328bcb0991SDimitry Andric // two where age is determined by value number. (Note that propagateEquality 21338bcb0991SDimitry Andric // above handles the cross block case.) 21348bcb0991SDimitry Andric // 21358bcb0991SDimitry Andric // Key case to cover are: 21368bcb0991SDimitry Andric // 1) 21370b57cec5SDimitry Andric // %cmp = fcmp oeq float 3.000000e+00, %0 ; const on lhs could happen 21380b57cec5SDimitry Andric // call void @llvm.assume(i1 %cmp) 21390b57cec5SDimitry Andric // ret float %0 ; will change it to ret float 3.000000e+00 21408bcb0991SDimitry Andric // 2) 21418bcb0991SDimitry Andric // %load = load float, float* %addr 21428bcb0991SDimitry Andric // %cmp = fcmp oeq float %load, %0 21438bcb0991SDimitry Andric // call void @llvm.assume(i1 %cmp) 21448bcb0991SDimitry Andric // ret float %load ; will change it to ret float %0 21450b57cec5SDimitry Andric if (auto *CmpI = dyn_cast<CmpInst>(V)) { 2146480093f4SDimitry Andric if (impliesEquivalanceIfTrue(CmpI)) { 21470b57cec5SDimitry Andric Value *CmpLHS = CmpI->getOperand(0); 21480b57cec5SDimitry Andric Value *CmpRHS = CmpI->getOperand(1); 21498bcb0991SDimitry Andric // Heuristically pick the better replacement -- the choice of heuristic 21508bcb0991SDimitry Andric // isn't terribly important here, but the fact we canonicalize on some 21518bcb0991SDimitry Andric // replacement is for exposing other simplifications. 21528bcb0991SDimitry Andric // TODO: pull this out as a helper function and reuse w/existing 21538bcb0991SDimitry Andric // (slightly different) logic. 21548bcb0991SDimitry Andric if (isa<Constant>(CmpLHS) && !isa<Constant>(CmpRHS)) 21550b57cec5SDimitry Andric std::swap(CmpLHS, CmpRHS); 21568bcb0991SDimitry Andric if (!isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS)) 21578bcb0991SDimitry Andric std::swap(CmpLHS, CmpRHS); 21588bcb0991SDimitry Andric if ((isa<Argument>(CmpLHS) && isa<Argument>(CmpRHS)) || 21598bcb0991SDimitry Andric (isa<Instruction>(CmpLHS) && isa<Instruction>(CmpRHS))) { 21608bcb0991SDimitry Andric // Move the 'oldest' value to the right-hand side, using the value 21618bcb0991SDimitry Andric // number as a proxy for age. 21628bcb0991SDimitry Andric uint32_t LVN = VN.lookupOrAdd(CmpLHS); 21638bcb0991SDimitry Andric uint32_t RVN = VN.lookupOrAdd(CmpRHS); 21648bcb0991SDimitry Andric if (LVN < RVN) 21658bcb0991SDimitry Andric std::swap(CmpLHS, CmpRHS); 21668bcb0991SDimitry Andric } 21670b57cec5SDimitry Andric 21688bcb0991SDimitry Andric // Handle degenerate case where we either haven't pruned a dead path or a 21698bcb0991SDimitry Andric // removed a trivial assume yet. 21708bcb0991SDimitry Andric if (isa<Constant>(CmpLHS) && isa<Constant>(CmpRHS)) 21718bcb0991SDimitry Andric return Changed; 21728bcb0991SDimitry Andric 21738bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Replacing dominated uses of " 21748bcb0991SDimitry Andric << *CmpLHS << " with " 21758bcb0991SDimitry Andric << *CmpRHS << " in block " 21768bcb0991SDimitry Andric << IntrinsicI->getParent()->getName() << "\n"); 21778bcb0991SDimitry Andric 21788bcb0991SDimitry Andric 21798bcb0991SDimitry Andric // Setup the replacement map - this handles uses within the same block 21808bcb0991SDimitry Andric if (hasUsersIn(CmpLHS, IntrinsicI->getParent())) 21818bcb0991SDimitry Andric ReplaceOperandsWithMap[CmpLHS] = CmpRHS; 21828bcb0991SDimitry Andric 21838bcb0991SDimitry Andric // NOTE: The non-block local cases are handled by the call to 21848bcb0991SDimitry Andric // propagateEquality above; this block is just about handling the block 21858bcb0991SDimitry Andric // local cases. TODO: There's a bunch of logic in propagateEqualiy which 21868bcb0991SDimitry Andric // isn't duplicated for the block local case, can we share it somehow? 21870b57cec5SDimitry Andric } 21880b57cec5SDimitry Andric } 21890b57cec5SDimitry Andric return Changed; 21900b57cec5SDimitry Andric } 21910b57cec5SDimitry Andric 21920b57cec5SDimitry Andric static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) { 21930b57cec5SDimitry Andric patchReplacementInstruction(I, Repl); 21940b57cec5SDimitry Andric I->replaceAllUsesWith(Repl); 21950b57cec5SDimitry Andric } 21960b57cec5SDimitry Andric 21970b57cec5SDimitry Andric /// Attempt to eliminate a load, first by eliminating it 21980b57cec5SDimitry Andric /// locally, and then attempting non-local elimination if that fails. 2199349cc55cSDimitry Andric bool GVNPass::processLoad(LoadInst *L) { 22000b57cec5SDimitry Andric if (!MD) 22010b57cec5SDimitry Andric return false; 22020b57cec5SDimitry Andric 22030b57cec5SDimitry Andric // This code hasn't been audited for ordered or volatile memory access 22040b57cec5SDimitry Andric if (!L->isUnordered()) 22050b57cec5SDimitry Andric return false; 22060b57cec5SDimitry Andric 22070b57cec5SDimitry Andric if (L->use_empty()) { 22080b57cec5SDimitry Andric markInstructionForDeletion(L); 22090b57cec5SDimitry Andric return true; 22100b57cec5SDimitry Andric } 22110b57cec5SDimitry Andric 22120b57cec5SDimitry Andric // ... to a pointer that has been loaded from before... 22130b57cec5SDimitry Andric MemDepResult Dep = MD->getDependency(L); 22140b57cec5SDimitry Andric 22150b57cec5SDimitry Andric // If it is defined in another block, try harder. 22160b57cec5SDimitry Andric if (Dep.isNonLocal()) 22170b57cec5SDimitry Andric return processNonLocalLoad(L); 22180b57cec5SDimitry Andric 22190b57cec5SDimitry Andric // Only handle the local case below 2220bdd1243dSDimitry Andric if (!Dep.isLocal()) { 22210b57cec5SDimitry Andric // This might be a NonFuncLocal or an Unknown 22220b57cec5SDimitry Andric LLVM_DEBUG( 22230b57cec5SDimitry Andric // fast print dep, using operator<< on instruction is too slow. 22240b57cec5SDimitry Andric dbgs() << "GVN: load "; L->printAsOperand(dbgs()); 22250b57cec5SDimitry Andric dbgs() << " has unknown dependence\n";); 22260b57cec5SDimitry Andric return false; 22270b57cec5SDimitry Andric } 22280b57cec5SDimitry Andric 2229bdd1243dSDimitry Andric auto AV = AnalyzeLoadAvailability(L, Dep, L->getPointerOperand()); 2230bdd1243dSDimitry Andric if (!AV) 2231bdd1243dSDimitry Andric return false; 2232bdd1243dSDimitry Andric 2233bdd1243dSDimitry Andric Value *AvailableValue = AV->MaterializeAdjustedValue(L, L, *this); 22340b57cec5SDimitry Andric 223506c3fb27SDimitry Andric // MaterializeAdjustedValue is responsible for combining metadata. 22365f757f3fSDimitry Andric ICF->removeUsersOf(L); 223706c3fb27SDimitry Andric L->replaceAllUsesWith(AvailableValue); 22380b57cec5SDimitry Andric markInstructionForDeletion(L); 2239e8d8bef9SDimitry Andric if (MSSAU) 2240e8d8bef9SDimitry Andric MSSAU->removeMemoryAccess(L); 22410b57cec5SDimitry Andric ++NumGVNLoad; 22420b57cec5SDimitry Andric reportLoadElim(L, AvailableValue, ORE); 2243fe6060f1SDimitry Andric // Tell MDA to reexamine the reused pointer since we might have more 22440b57cec5SDimitry Andric // information after forwarding it. 22450b57cec5SDimitry Andric if (MD && AvailableValue->getType()->isPtrOrPtrVectorTy()) 22460b57cec5SDimitry Andric MD->invalidateCachedPointerInfo(AvailableValue); 22470b57cec5SDimitry Andric return true; 22480b57cec5SDimitry Andric } 22490b57cec5SDimitry Andric 22500b57cec5SDimitry Andric /// Return a pair the first field showing the value number of \p Exp and the 22510b57cec5SDimitry Andric /// second field showing whether it is a value number newly created. 22520b57cec5SDimitry Andric std::pair<uint32_t, bool> 2253349cc55cSDimitry Andric GVNPass::ValueTable::assignExpNewValueNum(Expression &Exp) { 22540b57cec5SDimitry Andric uint32_t &e = expressionNumbering[Exp]; 22550b57cec5SDimitry Andric bool CreateNewValNum = !e; 22560b57cec5SDimitry Andric if (CreateNewValNum) { 22570b57cec5SDimitry Andric Expressions.push_back(Exp); 22580b57cec5SDimitry Andric if (ExprIdx.size() < nextValueNumber + 1) 22590b57cec5SDimitry Andric ExprIdx.resize(nextValueNumber * 2); 22600b57cec5SDimitry Andric e = nextValueNumber; 22610b57cec5SDimitry Andric ExprIdx[nextValueNumber++] = nextExprNumber++; 22620b57cec5SDimitry Andric } 22630b57cec5SDimitry Andric return {e, CreateNewValNum}; 22640b57cec5SDimitry Andric } 22650b57cec5SDimitry Andric 22660b57cec5SDimitry Andric /// Return whether all the values related with the same \p num are 22670b57cec5SDimitry Andric /// defined in \p BB. 2268349cc55cSDimitry Andric bool GVNPass::ValueTable::areAllValsInBB(uint32_t Num, const BasicBlock *BB, 2269349cc55cSDimitry Andric GVNPass &Gvn) { 2270*0fca6ea1SDimitry Andric return all_of( 2271*0fca6ea1SDimitry Andric Gvn.LeaderTable.getLeaders(Num), 2272*0fca6ea1SDimitry Andric [=](const LeaderMap::LeaderTableEntry &L) { return L.BB == BB; }); 22730b57cec5SDimitry Andric } 22740b57cec5SDimitry Andric 22750b57cec5SDimitry Andric /// Wrap phiTranslateImpl to provide caching functionality. 2276349cc55cSDimitry Andric uint32_t GVNPass::ValueTable::phiTranslate(const BasicBlock *Pred, 2277349cc55cSDimitry Andric const BasicBlock *PhiBlock, 2278349cc55cSDimitry Andric uint32_t Num, GVNPass &Gvn) { 22790b57cec5SDimitry Andric auto FindRes = PhiTranslateTable.find({Num, Pred}); 22800b57cec5SDimitry Andric if (FindRes != PhiTranslateTable.end()) 22810b57cec5SDimitry Andric return FindRes->second; 22820b57cec5SDimitry Andric uint32_t NewNum = phiTranslateImpl(Pred, PhiBlock, Num, Gvn); 22830b57cec5SDimitry Andric PhiTranslateTable.insert({{Num, Pred}, NewNum}); 22840b57cec5SDimitry Andric return NewNum; 22850b57cec5SDimitry Andric } 22860b57cec5SDimitry Andric 2287c14a5a88SDimitry Andric // Return true if the value number \p Num and NewNum have equal value. 2288c14a5a88SDimitry Andric // Return false if the result is unknown. 2289349cc55cSDimitry Andric bool GVNPass::ValueTable::areCallValsEqual(uint32_t Num, uint32_t NewNum, 2290c14a5a88SDimitry Andric const BasicBlock *Pred, 2291349cc55cSDimitry Andric const BasicBlock *PhiBlock, 2292349cc55cSDimitry Andric GVNPass &Gvn) { 2293c14a5a88SDimitry Andric CallInst *Call = nullptr; 2294*0fca6ea1SDimitry Andric auto Leaders = Gvn.LeaderTable.getLeaders(Num); 2295*0fca6ea1SDimitry Andric for (const auto &Entry : Leaders) { 2296*0fca6ea1SDimitry Andric Call = dyn_cast<CallInst>(Entry.Val); 2297c14a5a88SDimitry Andric if (Call && Call->getParent() == PhiBlock) 2298c14a5a88SDimitry Andric break; 2299c14a5a88SDimitry Andric } 2300c14a5a88SDimitry Andric 2301c14a5a88SDimitry Andric if (AA->doesNotAccessMemory(Call)) 2302c14a5a88SDimitry Andric return true; 2303c14a5a88SDimitry Andric 2304c14a5a88SDimitry Andric if (!MD || !AA->onlyReadsMemory(Call)) 2305c14a5a88SDimitry Andric return false; 2306c14a5a88SDimitry Andric 2307c14a5a88SDimitry Andric MemDepResult local_dep = MD->getDependency(Call); 2308c14a5a88SDimitry Andric if (!local_dep.isNonLocal()) 2309c14a5a88SDimitry Andric return false; 2310c14a5a88SDimitry Andric 2311c14a5a88SDimitry Andric const MemoryDependenceResults::NonLocalDepInfo &deps = 2312c14a5a88SDimitry Andric MD->getNonLocalCallDependency(Call); 2313c14a5a88SDimitry Andric 2314c14a5a88SDimitry Andric // Check to see if the Call has no function local clobber. 2315fe6060f1SDimitry Andric for (const NonLocalDepEntry &D : deps) { 2316fe6060f1SDimitry Andric if (D.getResult().isNonFuncLocal()) 2317c14a5a88SDimitry Andric return true; 2318c14a5a88SDimitry Andric } 2319c14a5a88SDimitry Andric return false; 2320c14a5a88SDimitry Andric } 2321c14a5a88SDimitry Andric 23220b57cec5SDimitry Andric /// Translate value number \p Num using phis, so that it has the values of 23230b57cec5SDimitry Andric /// the phis in BB. 2324349cc55cSDimitry Andric uint32_t GVNPass::ValueTable::phiTranslateImpl(const BasicBlock *Pred, 23250b57cec5SDimitry Andric const BasicBlock *PhiBlock, 2326349cc55cSDimitry Andric uint32_t Num, GVNPass &Gvn) { 23270b57cec5SDimitry Andric if (PHINode *PN = NumberingPhi[Num]) { 23280b57cec5SDimitry Andric for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) { 23290b57cec5SDimitry Andric if (PN->getParent() == PhiBlock && PN->getIncomingBlock(i) == Pred) 23300b57cec5SDimitry Andric if (uint32_t TransVal = lookup(PN->getIncomingValue(i), false)) 23310b57cec5SDimitry Andric return TransVal; 23320b57cec5SDimitry Andric } 23330b57cec5SDimitry Andric return Num; 23340b57cec5SDimitry Andric } 23350b57cec5SDimitry Andric 23360b57cec5SDimitry Andric // If there is any value related with Num is defined in a BB other than 23370b57cec5SDimitry Andric // PhiBlock, it cannot depend on a phi in PhiBlock without going through 23380b57cec5SDimitry Andric // a backedge. We can do an early exit in that case to save compile time. 23390b57cec5SDimitry Andric if (!areAllValsInBB(Num, PhiBlock, Gvn)) 23400b57cec5SDimitry Andric return Num; 23410b57cec5SDimitry Andric 23420b57cec5SDimitry Andric if (Num >= ExprIdx.size() || ExprIdx[Num] == 0) 23430b57cec5SDimitry Andric return Num; 23440b57cec5SDimitry Andric Expression Exp = Expressions[ExprIdx[Num]]; 23450b57cec5SDimitry Andric 23460b57cec5SDimitry Andric for (unsigned i = 0; i < Exp.varargs.size(); i++) { 23470b57cec5SDimitry Andric // For InsertValue and ExtractValue, some varargs are index numbers 23480b57cec5SDimitry Andric // instead of value numbers. Those index numbers should not be 23490b57cec5SDimitry Andric // translated. 23500b57cec5SDimitry Andric if ((i > 1 && Exp.opcode == Instruction::InsertValue) || 23515ffd83dbSDimitry Andric (i > 0 && Exp.opcode == Instruction::ExtractValue) || 23525ffd83dbSDimitry Andric (i > 1 && Exp.opcode == Instruction::ShuffleVector)) 23530b57cec5SDimitry Andric continue; 23540b57cec5SDimitry Andric Exp.varargs[i] = phiTranslate(Pred, PhiBlock, Exp.varargs[i], Gvn); 23550b57cec5SDimitry Andric } 23560b57cec5SDimitry Andric 23570b57cec5SDimitry Andric if (Exp.commutative) { 2358e8d8bef9SDimitry Andric assert(Exp.varargs.size() >= 2 && "Unsupported commutative instruction!"); 23590b57cec5SDimitry Andric if (Exp.varargs[0] > Exp.varargs[1]) { 23600b57cec5SDimitry Andric std::swap(Exp.varargs[0], Exp.varargs[1]); 23610b57cec5SDimitry Andric uint32_t Opcode = Exp.opcode >> 8; 23620b57cec5SDimitry Andric if (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) 23630b57cec5SDimitry Andric Exp.opcode = (Opcode << 8) | 23640b57cec5SDimitry Andric CmpInst::getSwappedPredicate( 23650b57cec5SDimitry Andric static_cast<CmpInst::Predicate>(Exp.opcode & 255)); 23660b57cec5SDimitry Andric } 23670b57cec5SDimitry Andric } 23680b57cec5SDimitry Andric 2369c14a5a88SDimitry Andric if (uint32_t NewNum = expressionNumbering[Exp]) { 2370c14a5a88SDimitry Andric if (Exp.opcode == Instruction::Call && NewNum != Num) 2371c14a5a88SDimitry Andric return areCallValsEqual(Num, NewNum, Pred, PhiBlock, Gvn) ? NewNum : Num; 23720b57cec5SDimitry Andric return NewNum; 2373c14a5a88SDimitry Andric } 23740b57cec5SDimitry Andric return Num; 23750b57cec5SDimitry Andric } 23760b57cec5SDimitry Andric 23770b57cec5SDimitry Andric /// Erase stale entry from phiTranslate cache so phiTranslate can be computed 23780b57cec5SDimitry Andric /// again. 2379349cc55cSDimitry Andric void GVNPass::ValueTable::eraseTranslateCacheEntry( 2380349cc55cSDimitry Andric uint32_t Num, const BasicBlock &CurrBlock) { 2381e8d8bef9SDimitry Andric for (const BasicBlock *Pred : predecessors(&CurrBlock)) 2382e8d8bef9SDimitry Andric PhiTranslateTable.erase({Num, Pred}); 23830b57cec5SDimitry Andric } 23840b57cec5SDimitry Andric 23850b57cec5SDimitry Andric // In order to find a leader for a given value number at a 23860b57cec5SDimitry Andric // specific basic block, we first obtain the list of all Values for that number, 23870b57cec5SDimitry Andric // and then scan the list to find one whose block dominates the block in 23880b57cec5SDimitry Andric // question. This is fast because dominator tree queries consist of only 23890b57cec5SDimitry Andric // a few comparisons of DFS numbers. 2390349cc55cSDimitry Andric Value *GVNPass::findLeader(const BasicBlock *BB, uint32_t num) { 2391*0fca6ea1SDimitry Andric auto Leaders = LeaderTable.getLeaders(num); 2392*0fca6ea1SDimitry Andric if (Leaders.empty()) 2393*0fca6ea1SDimitry Andric return nullptr; 23940b57cec5SDimitry Andric 23950b57cec5SDimitry Andric Value *Val = nullptr; 2396*0fca6ea1SDimitry Andric for (const auto &Entry : Leaders) { 2397*0fca6ea1SDimitry Andric if (DT->dominates(Entry.BB, BB)) { 2398*0fca6ea1SDimitry Andric Val = Entry.Val; 2399*0fca6ea1SDimitry Andric if (isa<Constant>(Val)) 2400*0fca6ea1SDimitry Andric return Val; 24010b57cec5SDimitry Andric } 24020b57cec5SDimitry Andric } 24030b57cec5SDimitry Andric 24040b57cec5SDimitry Andric return Val; 24050b57cec5SDimitry Andric } 24060b57cec5SDimitry Andric 24070b57cec5SDimitry Andric /// There is an edge from 'Src' to 'Dst'. Return 24080b57cec5SDimitry Andric /// true if every path from the entry block to 'Dst' passes via this edge. In 24090b57cec5SDimitry Andric /// particular 'Dst' must not be reachable via another edge from 'Src'. 24100b57cec5SDimitry Andric static bool isOnlyReachableViaThisEdge(const BasicBlockEdge &E, 24110b57cec5SDimitry Andric DominatorTree *DT) { 24120b57cec5SDimitry Andric // While in theory it is interesting to consider the case in which Dst has 24130b57cec5SDimitry Andric // more than one predecessor, because Dst might be part of a loop which is 24140b57cec5SDimitry Andric // only reachable from Src, in practice it is pointless since at the time 24150b57cec5SDimitry Andric // GVN runs all such loops have preheaders, which means that Dst will have 24160b57cec5SDimitry Andric // been changed to have only one predecessor, namely Src. 24170b57cec5SDimitry Andric const BasicBlock *Pred = E.getEnd()->getSinglePredecessor(); 24180b57cec5SDimitry Andric assert((!Pred || Pred == E.getStart()) && 24190b57cec5SDimitry Andric "No edge between these basic blocks!"); 24200b57cec5SDimitry Andric return Pred != nullptr; 24210b57cec5SDimitry Andric } 24220b57cec5SDimitry Andric 2423349cc55cSDimitry Andric void GVNPass::assignBlockRPONumber(Function &F) { 24240b57cec5SDimitry Andric BlockRPONumber.clear(); 24250b57cec5SDimitry Andric uint32_t NextBlockNumber = 1; 24260b57cec5SDimitry Andric ReversePostOrderTraversal<Function *> RPOT(&F); 24270b57cec5SDimitry Andric for (BasicBlock *BB : RPOT) 24280b57cec5SDimitry Andric BlockRPONumber[BB] = NextBlockNumber++; 24290b57cec5SDimitry Andric InvalidBlockRPONumbers = false; 24300b57cec5SDimitry Andric } 24310b57cec5SDimitry Andric 2432349cc55cSDimitry Andric bool GVNPass::replaceOperandsForInBlockEquality(Instruction *Instr) const { 24330b57cec5SDimitry Andric bool Changed = false; 24340b57cec5SDimitry Andric for (unsigned OpNum = 0; OpNum < Instr->getNumOperands(); ++OpNum) { 24350b57cec5SDimitry Andric Value *Operand = Instr->getOperand(OpNum); 24368bcb0991SDimitry Andric auto it = ReplaceOperandsWithMap.find(Operand); 24378bcb0991SDimitry Andric if (it != ReplaceOperandsWithMap.end()) { 24380b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "GVN replacing: " << *Operand << " with " 24390b57cec5SDimitry Andric << *it->second << " in instruction " << *Instr << '\n'); 24400b57cec5SDimitry Andric Instr->setOperand(OpNum, it->second); 24410b57cec5SDimitry Andric Changed = true; 24420b57cec5SDimitry Andric } 24430b57cec5SDimitry Andric } 24440b57cec5SDimitry Andric return Changed; 24450b57cec5SDimitry Andric } 24460b57cec5SDimitry Andric 24470b57cec5SDimitry Andric /// The given values are known to be equal in every block 24480b57cec5SDimitry Andric /// dominated by 'Root'. Exploit this, for example by replacing 'LHS' with 24490b57cec5SDimitry Andric /// 'RHS' everywhere in the scope. Returns whether a change was made. 24500b57cec5SDimitry Andric /// If DominatesByEdge is false, then it means that we will propagate the RHS 24510b57cec5SDimitry Andric /// value starting from the end of Root.Start. 2452349cc55cSDimitry Andric bool GVNPass::propagateEquality(Value *LHS, Value *RHS, 2453349cc55cSDimitry Andric const BasicBlockEdge &Root, 24540b57cec5SDimitry Andric bool DominatesByEdge) { 24550b57cec5SDimitry Andric SmallVector<std::pair<Value*, Value*>, 4> Worklist; 24560b57cec5SDimitry Andric Worklist.push_back(std::make_pair(LHS, RHS)); 24570b57cec5SDimitry Andric bool Changed = false; 24580b57cec5SDimitry Andric // For speed, compute a conservative fast approximation to 24590b57cec5SDimitry Andric // DT->dominates(Root, Root.getEnd()); 24600b57cec5SDimitry Andric const bool RootDominatesEnd = isOnlyReachableViaThisEdge(Root, DT); 24610b57cec5SDimitry Andric 24620b57cec5SDimitry Andric while (!Worklist.empty()) { 24630b57cec5SDimitry Andric std::pair<Value*, Value*> Item = Worklist.pop_back_val(); 24640b57cec5SDimitry Andric LHS = Item.first; RHS = Item.second; 24650b57cec5SDimitry Andric 24660b57cec5SDimitry Andric if (LHS == RHS) 24670b57cec5SDimitry Andric continue; 24680b57cec5SDimitry Andric assert(LHS->getType() == RHS->getType() && "Equality but unequal types!"); 24690b57cec5SDimitry Andric 24700b57cec5SDimitry Andric // Don't try to propagate equalities between constants. 24710b57cec5SDimitry Andric if (isa<Constant>(LHS) && isa<Constant>(RHS)) 24720b57cec5SDimitry Andric continue; 24730b57cec5SDimitry Andric 24740b57cec5SDimitry Andric // Prefer a constant on the right-hand side, or an Argument if no constants. 24750b57cec5SDimitry Andric if (isa<Constant>(LHS) || (isa<Argument>(LHS) && !isa<Constant>(RHS))) 24760b57cec5SDimitry Andric std::swap(LHS, RHS); 24770b57cec5SDimitry Andric assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!"); 2478*0fca6ea1SDimitry Andric const DataLayout &DL = 2479*0fca6ea1SDimitry Andric isa<Argument>(LHS) 2480*0fca6ea1SDimitry Andric ? cast<Argument>(LHS)->getParent()->getDataLayout() 2481*0fca6ea1SDimitry Andric : cast<Instruction>(LHS)->getDataLayout(); 24820b57cec5SDimitry Andric 24830b57cec5SDimitry Andric // If there is no obvious reason to prefer the left-hand side over the 24840b57cec5SDimitry Andric // right-hand side, ensure the longest lived term is on the right-hand side, 24850b57cec5SDimitry Andric // so the shortest lived term will be replaced by the longest lived. 24860b57cec5SDimitry Andric // This tends to expose more simplifications. 24870b57cec5SDimitry Andric uint32_t LVN = VN.lookupOrAdd(LHS); 24880b57cec5SDimitry Andric if ((isa<Argument>(LHS) && isa<Argument>(RHS)) || 24890b57cec5SDimitry Andric (isa<Instruction>(LHS) && isa<Instruction>(RHS))) { 24900b57cec5SDimitry Andric // Move the 'oldest' value to the right-hand side, using the value number 24910b57cec5SDimitry Andric // as a proxy for age. 24920b57cec5SDimitry Andric uint32_t RVN = VN.lookupOrAdd(RHS); 24930b57cec5SDimitry Andric if (LVN < RVN) { 24940b57cec5SDimitry Andric std::swap(LHS, RHS); 24950b57cec5SDimitry Andric LVN = RVN; 24960b57cec5SDimitry Andric } 24970b57cec5SDimitry Andric } 24980b57cec5SDimitry Andric 24990b57cec5SDimitry Andric // If value numbering later sees that an instruction in the scope is equal 25000b57cec5SDimitry Andric // to 'LHS' then ensure it will be turned into 'RHS'. In order to preserve 25010b57cec5SDimitry Andric // the invariant that instructions only occur in the leader table for their 25020b57cec5SDimitry Andric // own value number (this is used by removeFromLeaderTable), do not do this 25030b57cec5SDimitry Andric // if RHS is an instruction (if an instruction in the scope is morphed into 25040b57cec5SDimitry Andric // LHS then it will be turned into RHS by the next GVN iteration anyway, so 25050b57cec5SDimitry Andric // using the leader table is about compiling faster, not optimizing better). 25060b57cec5SDimitry Andric // The leader table only tracks basic blocks, not edges. Only add to if we 25070b57cec5SDimitry Andric // have the simple case where the edge dominates the end. 2508*0fca6ea1SDimitry Andric if (RootDominatesEnd && !isa<Instruction>(RHS) && 2509*0fca6ea1SDimitry Andric canReplacePointersIfEqual(LHS, RHS, DL)) 2510*0fca6ea1SDimitry Andric LeaderTable.insert(LVN, RHS, Root.getEnd()); 25110b57cec5SDimitry Andric 25120b57cec5SDimitry Andric // Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As 25130b57cec5SDimitry Andric // LHS always has at least one use that is not dominated by Root, this will 25140b57cec5SDimitry Andric // never do anything if LHS has only one use. 25150b57cec5SDimitry Andric if (!LHS->hasOneUse()) { 2516*0fca6ea1SDimitry Andric // Create a callback that captures the DL. 2517*0fca6ea1SDimitry Andric auto canReplacePointersCallBack = [&DL](const Use &U, const Value *To) { 2518*0fca6ea1SDimitry Andric return canReplacePointersInUseIfEqual(U, To, DL); 2519*0fca6ea1SDimitry Andric }; 25200b57cec5SDimitry Andric unsigned NumReplacements = 25210b57cec5SDimitry Andric DominatesByEdge 2522*0fca6ea1SDimitry Andric ? replaceDominatedUsesWithIf(LHS, RHS, *DT, Root, 2523*0fca6ea1SDimitry Andric canReplacePointersCallBack) 2524*0fca6ea1SDimitry Andric : replaceDominatedUsesWithIf(LHS, RHS, *DT, Root.getStart(), 2525*0fca6ea1SDimitry Andric canReplacePointersCallBack); 25260b57cec5SDimitry Andric 2527*0fca6ea1SDimitry Andric if (NumReplacements > 0) { 2528*0fca6ea1SDimitry Andric Changed = true; 25290b57cec5SDimitry Andric NumGVNEqProp += NumReplacements; 25300b57cec5SDimitry Andric // Cached information for anything that uses LHS will be invalid. 25310b57cec5SDimitry Andric if (MD) 25320b57cec5SDimitry Andric MD->invalidateCachedPointerInfo(LHS); 25330b57cec5SDimitry Andric } 2534*0fca6ea1SDimitry Andric } 25350b57cec5SDimitry Andric 25360b57cec5SDimitry Andric // Now try to deduce additional equalities from this one. For example, if 25370b57cec5SDimitry Andric // the known equality was "(A != B)" == "false" then it follows that A and B 25380b57cec5SDimitry Andric // are equal in the scope. Only boolean equalities with an explicit true or 25390b57cec5SDimitry Andric // false RHS are currently supported. 25400b57cec5SDimitry Andric if (!RHS->getType()->isIntegerTy(1)) 25410b57cec5SDimitry Andric // Not a boolean equality - bail out. 25420b57cec5SDimitry Andric continue; 25430b57cec5SDimitry Andric ConstantInt *CI = dyn_cast<ConstantInt>(RHS); 25440b57cec5SDimitry Andric if (!CI) 25450b57cec5SDimitry Andric // RHS neither 'true' nor 'false' - bail out. 25460b57cec5SDimitry Andric continue; 25470b57cec5SDimitry Andric // Whether RHS equals 'true'. Otherwise it equals 'false'. 25480b57cec5SDimitry Andric bool isKnownTrue = CI->isMinusOne(); 25490b57cec5SDimitry Andric bool isKnownFalse = !isKnownTrue; 25500b57cec5SDimitry Andric 25510b57cec5SDimitry Andric // If "A && B" is known true then both A and B are known true. If "A || B" 25520b57cec5SDimitry Andric // is known false then both A and B are known false. 25530b57cec5SDimitry Andric Value *A, *B; 2554e8d8bef9SDimitry Andric if ((isKnownTrue && match(LHS, m_LogicalAnd(m_Value(A), m_Value(B)))) || 2555e8d8bef9SDimitry Andric (isKnownFalse && match(LHS, m_LogicalOr(m_Value(A), m_Value(B))))) { 25560b57cec5SDimitry Andric Worklist.push_back(std::make_pair(A, RHS)); 25570b57cec5SDimitry Andric Worklist.push_back(std::make_pair(B, RHS)); 25580b57cec5SDimitry Andric continue; 25590b57cec5SDimitry Andric } 25600b57cec5SDimitry Andric 25610b57cec5SDimitry Andric // If we are propagating an equality like "(A == B)" == "true" then also 25620b57cec5SDimitry Andric // propagate the equality A == B. When propagating a comparison such as 25630b57cec5SDimitry Andric // "(A >= B)" == "true", replace all instances of "A < B" with "false". 25640b57cec5SDimitry Andric if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) { 25650b57cec5SDimitry Andric Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1); 25660b57cec5SDimitry Andric 25670b57cec5SDimitry Andric // If "A == B" is known true, or "A != B" is known false, then replace 2568480093f4SDimitry Andric // A with B everywhere in the scope. For floating point operations, we 2569480093f4SDimitry Andric // have to be careful since equality does not always imply equivalance. 2570480093f4SDimitry Andric if ((isKnownTrue && impliesEquivalanceIfTrue(Cmp)) || 2571480093f4SDimitry Andric (isKnownFalse && impliesEquivalanceIfFalse(Cmp))) 25720b57cec5SDimitry Andric Worklist.push_back(std::make_pair(Op0, Op1)); 25730b57cec5SDimitry Andric 25740b57cec5SDimitry Andric // If "A >= B" is known true, replace "A < B" with false everywhere. 25750b57cec5SDimitry Andric CmpInst::Predicate NotPred = Cmp->getInversePredicate(); 25760b57cec5SDimitry Andric Constant *NotVal = ConstantInt::get(Cmp->getType(), isKnownFalse); 25770b57cec5SDimitry Andric // Since we don't have the instruction "A < B" immediately to hand, work 25780b57cec5SDimitry Andric // out the value number that it would have and use that to find an 25790b57cec5SDimitry Andric // appropriate instruction (if any). 25800b57cec5SDimitry Andric uint32_t NextNum = VN.getNextUnusedValueNumber(); 25810b57cec5SDimitry Andric uint32_t Num = VN.lookupOrAddCmp(Cmp->getOpcode(), NotPred, Op0, Op1); 25820b57cec5SDimitry Andric // If the number we were assigned was brand new then there is no point in 25830b57cec5SDimitry Andric // looking for an instruction realizing it: there cannot be one! 25840b57cec5SDimitry Andric if (Num < NextNum) { 25850b57cec5SDimitry Andric Value *NotCmp = findLeader(Root.getEnd(), Num); 25860b57cec5SDimitry Andric if (NotCmp && isa<Instruction>(NotCmp)) { 25870b57cec5SDimitry Andric unsigned NumReplacements = 25880b57cec5SDimitry Andric DominatesByEdge 25890b57cec5SDimitry Andric ? replaceDominatedUsesWith(NotCmp, NotVal, *DT, Root) 25900b57cec5SDimitry Andric : replaceDominatedUsesWith(NotCmp, NotVal, *DT, 25910b57cec5SDimitry Andric Root.getStart()); 25920b57cec5SDimitry Andric Changed |= NumReplacements > 0; 25930b57cec5SDimitry Andric NumGVNEqProp += NumReplacements; 25940b57cec5SDimitry Andric // Cached information for anything that uses NotCmp will be invalid. 25950b57cec5SDimitry Andric if (MD) 25960b57cec5SDimitry Andric MD->invalidateCachedPointerInfo(NotCmp); 25970b57cec5SDimitry Andric } 25980b57cec5SDimitry Andric } 25990b57cec5SDimitry Andric // Ensure that any instruction in scope that gets the "A < B" value number 26000b57cec5SDimitry Andric // is replaced with false. 26010b57cec5SDimitry Andric // The leader table only tracks basic blocks, not edges. Only add to if we 26020b57cec5SDimitry Andric // have the simple case where the edge dominates the end. 26030b57cec5SDimitry Andric if (RootDominatesEnd) 2604*0fca6ea1SDimitry Andric LeaderTable.insert(Num, NotVal, Root.getEnd()); 26050b57cec5SDimitry Andric 26060b57cec5SDimitry Andric continue; 26070b57cec5SDimitry Andric } 26080b57cec5SDimitry Andric } 26090b57cec5SDimitry Andric 26100b57cec5SDimitry Andric return Changed; 26110b57cec5SDimitry Andric } 26120b57cec5SDimitry Andric 26130b57cec5SDimitry Andric /// When calculating availability, handle an instruction 26140b57cec5SDimitry Andric /// by inserting it into the appropriate sets 2615349cc55cSDimitry Andric bool GVNPass::processInstruction(Instruction *I) { 26160b57cec5SDimitry Andric // Ignore dbg info intrinsics. 26170b57cec5SDimitry Andric if (isa<DbgInfoIntrinsic>(I)) 26180b57cec5SDimitry Andric return false; 26190b57cec5SDimitry Andric 26200b57cec5SDimitry Andric // If the instruction can be easily simplified then do so now in preference 26210b57cec5SDimitry Andric // to value numbering it. Value numbering often exposes redundancies, for 26220b57cec5SDimitry Andric // example if it determines that %y is equal to %x then the instruction 26230b57cec5SDimitry Andric // "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify. 2624*0fca6ea1SDimitry Andric const DataLayout &DL = I->getDataLayout(); 262581ad6265SDimitry Andric if (Value *V = simplifyInstruction(I, {DL, TLI, DT, AC})) { 26260b57cec5SDimitry Andric bool Changed = false; 26270b57cec5SDimitry Andric if (!I->use_empty()) { 2628fe6060f1SDimitry Andric // Simplification can cause a special instruction to become not special. 2629fe6060f1SDimitry Andric // For example, devirtualization to a willreturn function. 2630fe6060f1SDimitry Andric ICF->removeUsersOf(I); 26310b57cec5SDimitry Andric I->replaceAllUsesWith(V); 26320b57cec5SDimitry Andric Changed = true; 26330b57cec5SDimitry Andric } 26340b57cec5SDimitry Andric if (isInstructionTriviallyDead(I, TLI)) { 26350b57cec5SDimitry Andric markInstructionForDeletion(I); 26360b57cec5SDimitry Andric Changed = true; 26370b57cec5SDimitry Andric } 26380b57cec5SDimitry Andric if (Changed) { 26390b57cec5SDimitry Andric if (MD && V->getType()->isPtrOrPtrVectorTy()) 26400b57cec5SDimitry Andric MD->invalidateCachedPointerInfo(V); 26410b57cec5SDimitry Andric ++NumGVNSimpl; 26420b57cec5SDimitry Andric return true; 26430b57cec5SDimitry Andric } 26440b57cec5SDimitry Andric } 26450b57cec5SDimitry Andric 2646fe6060f1SDimitry Andric if (auto *Assume = dyn_cast<AssumeInst>(I)) 2647fe6060f1SDimitry Andric return processAssumeIntrinsic(Assume); 26480b57cec5SDimitry Andric 2649fe6060f1SDimitry Andric if (LoadInst *Load = dyn_cast<LoadInst>(I)) { 2650fe6060f1SDimitry Andric if (processLoad(Load)) 26510b57cec5SDimitry Andric return true; 26520b57cec5SDimitry Andric 2653fe6060f1SDimitry Andric unsigned Num = VN.lookupOrAdd(Load); 2654*0fca6ea1SDimitry Andric LeaderTable.insert(Num, Load, Load->getParent()); 26550b57cec5SDimitry Andric return false; 26560b57cec5SDimitry Andric } 26570b57cec5SDimitry Andric 26580b57cec5SDimitry Andric // For conditional branches, we can perform simple conditional propagation on 26590b57cec5SDimitry Andric // the condition value itself. 26600b57cec5SDimitry Andric if (BranchInst *BI = dyn_cast<BranchInst>(I)) { 26610b57cec5SDimitry Andric if (!BI->isConditional()) 26620b57cec5SDimitry Andric return false; 26630b57cec5SDimitry Andric 26640b57cec5SDimitry Andric if (isa<Constant>(BI->getCondition())) 26650b57cec5SDimitry Andric return processFoldableCondBr(BI); 26660b57cec5SDimitry Andric 26670b57cec5SDimitry Andric Value *BranchCond = BI->getCondition(); 26680b57cec5SDimitry Andric BasicBlock *TrueSucc = BI->getSuccessor(0); 26690b57cec5SDimitry Andric BasicBlock *FalseSucc = BI->getSuccessor(1); 26700b57cec5SDimitry Andric // Avoid multiple edges early. 26710b57cec5SDimitry Andric if (TrueSucc == FalseSucc) 26720b57cec5SDimitry Andric return false; 26730b57cec5SDimitry Andric 26740b57cec5SDimitry Andric BasicBlock *Parent = BI->getParent(); 26750b57cec5SDimitry Andric bool Changed = false; 26760b57cec5SDimitry Andric 26770b57cec5SDimitry Andric Value *TrueVal = ConstantInt::getTrue(TrueSucc->getContext()); 26780b57cec5SDimitry Andric BasicBlockEdge TrueE(Parent, TrueSucc); 26790b57cec5SDimitry Andric Changed |= propagateEquality(BranchCond, TrueVal, TrueE, true); 26800b57cec5SDimitry Andric 26810b57cec5SDimitry Andric Value *FalseVal = ConstantInt::getFalse(FalseSucc->getContext()); 26820b57cec5SDimitry Andric BasicBlockEdge FalseE(Parent, FalseSucc); 26830b57cec5SDimitry Andric Changed |= propagateEquality(BranchCond, FalseVal, FalseE, true); 26840b57cec5SDimitry Andric 26850b57cec5SDimitry Andric return Changed; 26860b57cec5SDimitry Andric } 26870b57cec5SDimitry Andric 26880b57cec5SDimitry Andric // For switches, propagate the case values into the case destinations. 26890b57cec5SDimitry Andric if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) { 26900b57cec5SDimitry Andric Value *SwitchCond = SI->getCondition(); 26910b57cec5SDimitry Andric BasicBlock *Parent = SI->getParent(); 26920b57cec5SDimitry Andric bool Changed = false; 26930b57cec5SDimitry Andric 26940b57cec5SDimitry Andric // Remember how many outgoing edges there are to every successor. 26950b57cec5SDimitry Andric SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges; 2696*0fca6ea1SDimitry Andric for (BasicBlock *Succ : successors(Parent)) 2697*0fca6ea1SDimitry Andric ++SwitchEdges[Succ]; 26980b57cec5SDimitry Andric 26990b57cec5SDimitry Andric for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); 27000b57cec5SDimitry Andric i != e; ++i) { 27010b57cec5SDimitry Andric BasicBlock *Dst = i->getCaseSuccessor(); 27020b57cec5SDimitry Andric // If there is only a single edge, propagate the case value into it. 27030b57cec5SDimitry Andric if (SwitchEdges.lookup(Dst) == 1) { 27040b57cec5SDimitry Andric BasicBlockEdge E(Parent, Dst); 27050b57cec5SDimitry Andric Changed |= propagateEquality(SwitchCond, i->getCaseValue(), E, true); 27060b57cec5SDimitry Andric } 27070b57cec5SDimitry Andric } 27080b57cec5SDimitry Andric return Changed; 27090b57cec5SDimitry Andric } 27100b57cec5SDimitry Andric 27110b57cec5SDimitry Andric // Instructions with void type don't return a value, so there's 27120b57cec5SDimitry Andric // no point in trying to find redundancies in them. 27130b57cec5SDimitry Andric if (I->getType()->isVoidTy()) 27140b57cec5SDimitry Andric return false; 27150b57cec5SDimitry Andric 27160b57cec5SDimitry Andric uint32_t NextNum = VN.getNextUnusedValueNumber(); 27170b57cec5SDimitry Andric unsigned Num = VN.lookupOrAdd(I); 27180b57cec5SDimitry Andric 27190b57cec5SDimitry Andric // Allocations are always uniquely numbered, so we can save time and memory 27200b57cec5SDimitry Andric // by fast failing them. 27210b57cec5SDimitry Andric if (isa<AllocaInst>(I) || I->isTerminator() || isa<PHINode>(I)) { 2722*0fca6ea1SDimitry Andric LeaderTable.insert(Num, I, I->getParent()); 27230b57cec5SDimitry Andric return false; 27240b57cec5SDimitry Andric } 27250b57cec5SDimitry Andric 27260b57cec5SDimitry Andric // If the number we were assigned was a brand new VN, then we don't 27270b57cec5SDimitry Andric // need to do a lookup to see if the number already exists 27280b57cec5SDimitry Andric // somewhere in the domtree: it can't! 27290b57cec5SDimitry Andric if (Num >= NextNum) { 2730*0fca6ea1SDimitry Andric LeaderTable.insert(Num, I, I->getParent()); 27310b57cec5SDimitry Andric return false; 27320b57cec5SDimitry Andric } 27330b57cec5SDimitry Andric 27340b57cec5SDimitry Andric // Perform fast-path value-number based elimination of values inherited from 27350b57cec5SDimitry Andric // dominators. 27360b57cec5SDimitry Andric Value *Repl = findLeader(I->getParent(), Num); 27370b57cec5SDimitry Andric if (!Repl) { 27380b57cec5SDimitry Andric // Failure, just remember this instance for future use. 2739*0fca6ea1SDimitry Andric LeaderTable.insert(Num, I, I->getParent()); 27400b57cec5SDimitry Andric return false; 274106c3fb27SDimitry Andric } 274206c3fb27SDimitry Andric 274306c3fb27SDimitry Andric if (Repl == I) { 27440b57cec5SDimitry Andric // If I was the result of a shortcut PRE, it might already be in the table 27450b57cec5SDimitry Andric // and the best replacement for itself. Nothing to do. 27460b57cec5SDimitry Andric return false; 27470b57cec5SDimitry Andric } 27480b57cec5SDimitry Andric 27490b57cec5SDimitry Andric // Remove it! 27500b57cec5SDimitry Andric patchAndReplaceAllUsesWith(I, Repl); 27510b57cec5SDimitry Andric if (MD && Repl->getType()->isPtrOrPtrVectorTy()) 27520b57cec5SDimitry Andric MD->invalidateCachedPointerInfo(Repl); 27530b57cec5SDimitry Andric markInstructionForDeletion(I); 27540b57cec5SDimitry Andric return true; 27550b57cec5SDimitry Andric } 27560b57cec5SDimitry Andric 27570b57cec5SDimitry Andric /// runOnFunction - This is the main transformation entry point for a function. 2758349cc55cSDimitry Andric bool GVNPass::runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT, 27590b57cec5SDimitry Andric const TargetLibraryInfo &RunTLI, AAResults &RunAA, 27605f757f3fSDimitry Andric MemoryDependenceResults *RunMD, LoopInfo &LI, 2761e8d8bef9SDimitry Andric OptimizationRemarkEmitter *RunORE, MemorySSA *MSSA) { 27620b57cec5SDimitry Andric AC = &RunAC; 27630b57cec5SDimitry Andric DT = &RunDT; 27640b57cec5SDimitry Andric VN.setDomTree(DT); 27650b57cec5SDimitry Andric TLI = &RunTLI; 27660b57cec5SDimitry Andric VN.setAliasAnalysis(&RunAA); 27670b57cec5SDimitry Andric MD = RunMD; 27685ffd83dbSDimitry Andric ImplicitControlFlowTracking ImplicitCFT; 27690b57cec5SDimitry Andric ICF = &ImplicitCFT; 27705f757f3fSDimitry Andric this->LI = &LI; 27710b57cec5SDimitry Andric VN.setMemDep(MD); 27720b57cec5SDimitry Andric ORE = RunORE; 27730b57cec5SDimitry Andric InvalidBlockRPONumbers = true; 2774e8d8bef9SDimitry Andric MemorySSAUpdater Updater(MSSA); 2775e8d8bef9SDimitry Andric MSSAU = MSSA ? &Updater : nullptr; 27760b57cec5SDimitry Andric 27770b57cec5SDimitry Andric bool Changed = false; 27780b57cec5SDimitry Andric bool ShouldContinue = true; 27790b57cec5SDimitry Andric 2780*0fca6ea1SDimitry Andric DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy); 27810b57cec5SDimitry Andric // Merge unconditional branches, allowing PRE to catch more 27820b57cec5SDimitry Andric // optimization opportunities. 2783349cc55cSDimitry Andric for (BasicBlock &BB : llvm::make_early_inc_range(F)) { 27845f757f3fSDimitry Andric bool removedBlock = MergeBlockIntoPredecessor(&BB, &DTU, &LI, MSSAU, MD); 27850b57cec5SDimitry Andric if (removedBlock) 27860b57cec5SDimitry Andric ++NumGVNBlocks; 27870b57cec5SDimitry Andric 27880b57cec5SDimitry Andric Changed |= removedBlock; 27890b57cec5SDimitry Andric } 2790*0fca6ea1SDimitry Andric DTU.flush(); 27910b57cec5SDimitry Andric 27920b57cec5SDimitry Andric unsigned Iteration = 0; 27930b57cec5SDimitry Andric while (ShouldContinue) { 27940b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n"); 279581ad6265SDimitry Andric (void) Iteration; 27960b57cec5SDimitry Andric ShouldContinue = iterateOnFunction(F); 27970b57cec5SDimitry Andric Changed |= ShouldContinue; 27980b57cec5SDimitry Andric ++Iteration; 27990b57cec5SDimitry Andric } 28000b57cec5SDimitry Andric 28015ffd83dbSDimitry Andric if (isPREEnabled()) { 28020b57cec5SDimitry Andric // Fabricate val-num for dead-code in order to suppress assertion in 28030b57cec5SDimitry Andric // performPRE(). 28040b57cec5SDimitry Andric assignValNumForDeadCode(); 28050b57cec5SDimitry Andric bool PREChanged = true; 28060b57cec5SDimitry Andric while (PREChanged) { 28070b57cec5SDimitry Andric PREChanged = performPRE(F); 28080b57cec5SDimitry Andric Changed |= PREChanged; 28090b57cec5SDimitry Andric } 28100b57cec5SDimitry Andric } 28110b57cec5SDimitry Andric 28120b57cec5SDimitry Andric // FIXME: Should perform GVN again after PRE does something. PRE can move 28130b57cec5SDimitry Andric // computations into blocks where they become fully redundant. Note that 28140b57cec5SDimitry Andric // we can't do this until PRE's critical edge splitting updates memdep. 28150b57cec5SDimitry Andric // Actually, when this happens, we should just fully integrate PRE into GVN. 28160b57cec5SDimitry Andric 28170b57cec5SDimitry Andric cleanupGlobalSets(); 28180b57cec5SDimitry Andric // Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each 28190b57cec5SDimitry Andric // iteration. 28200b57cec5SDimitry Andric DeadBlocks.clear(); 28210b57cec5SDimitry Andric 2822e8d8bef9SDimitry Andric if (MSSA && VerifyMemorySSA) 2823e8d8bef9SDimitry Andric MSSA->verifyMemorySSA(); 2824e8d8bef9SDimitry Andric 28250b57cec5SDimitry Andric return Changed; 28260b57cec5SDimitry Andric } 28270b57cec5SDimitry Andric 2828349cc55cSDimitry Andric bool GVNPass::processBlock(BasicBlock *BB) { 28290b57cec5SDimitry Andric // FIXME: Kill off InstrsToErase by doing erasing eagerly in a helper function 28300b57cec5SDimitry Andric // (and incrementing BI before processing an instruction). 28310b57cec5SDimitry Andric assert(InstrsToErase.empty() && 28320b57cec5SDimitry Andric "We expect InstrsToErase to be empty across iterations"); 28330b57cec5SDimitry Andric if (DeadBlocks.count(BB)) 28340b57cec5SDimitry Andric return false; 28350b57cec5SDimitry Andric 28360b57cec5SDimitry Andric // Clearing map before every BB because it can be used only for single BB. 28378bcb0991SDimitry Andric ReplaceOperandsWithMap.clear(); 28380b57cec5SDimitry Andric bool ChangedFunction = false; 28390b57cec5SDimitry Andric 2840fe6060f1SDimitry Andric // Since we may not have visited the input blocks of the phis, we can't 2841fe6060f1SDimitry Andric // use our normal hash approach for phis. Instead, simply look for 2842fe6060f1SDimitry Andric // obvious duplicates. The first pass of GVN will tend to create 2843fe6060f1SDimitry Andric // identical phis, and the second or later passes can eliminate them. 28444542f901SDimitry Andric SmallPtrSet<PHINode *, 8> PHINodesToRemove; 28454542f901SDimitry Andric ChangedFunction |= EliminateDuplicatePHINodes(BB, PHINodesToRemove); 28464542f901SDimitry Andric for (PHINode *PN : PHINodesToRemove) { 28474542f901SDimitry Andric VN.erase(PN); 28484542f901SDimitry Andric removeInstruction(PN); 28494542f901SDimitry Andric } 2850fe6060f1SDimitry Andric 28510b57cec5SDimitry Andric for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); 28520b57cec5SDimitry Andric BI != BE;) { 28538bcb0991SDimitry Andric if (!ReplaceOperandsWithMap.empty()) 28548bcb0991SDimitry Andric ChangedFunction |= replaceOperandsForInBlockEquality(&*BI); 28550b57cec5SDimitry Andric ChangedFunction |= processInstruction(&*BI); 28560b57cec5SDimitry Andric 28570b57cec5SDimitry Andric if (InstrsToErase.empty()) { 28580b57cec5SDimitry Andric ++BI; 28590b57cec5SDimitry Andric continue; 28600b57cec5SDimitry Andric } 28610b57cec5SDimitry Andric 28620b57cec5SDimitry Andric // If we need some instructions deleted, do it now. 28630b57cec5SDimitry Andric NumGVNInstr += InstrsToErase.size(); 28640b57cec5SDimitry Andric 28650b57cec5SDimitry Andric // Avoid iterator invalidation. 28660b57cec5SDimitry Andric bool AtStart = BI == BB->begin(); 28670b57cec5SDimitry Andric if (!AtStart) 28680b57cec5SDimitry Andric --BI; 28690b57cec5SDimitry Andric 28700b57cec5SDimitry Andric for (auto *I : InstrsToErase) { 28710b57cec5SDimitry Andric assert(I->getParent() == BB && "Removing instruction from wrong block?"); 28720b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "GVN removed: " << *I << '\n'); 28735ffd83dbSDimitry Andric salvageKnowledge(I, AC); 28740b57cec5SDimitry Andric salvageDebugInfo(*I); 287506c3fb27SDimitry Andric removeInstruction(I); 28760b57cec5SDimitry Andric } 28770b57cec5SDimitry Andric InstrsToErase.clear(); 28780b57cec5SDimitry Andric 28790b57cec5SDimitry Andric if (AtStart) 28800b57cec5SDimitry Andric BI = BB->begin(); 28810b57cec5SDimitry Andric else 28820b57cec5SDimitry Andric ++BI; 28830b57cec5SDimitry Andric } 28840b57cec5SDimitry Andric 28850b57cec5SDimitry Andric return ChangedFunction; 28860b57cec5SDimitry Andric } 28870b57cec5SDimitry Andric 28880b57cec5SDimitry Andric // Instantiate an expression in a predecessor that lacked it. 2889349cc55cSDimitry Andric bool GVNPass::performScalarPREInsertion(Instruction *Instr, BasicBlock *Pred, 28900b57cec5SDimitry Andric BasicBlock *Curr, unsigned int ValNo) { 28910b57cec5SDimitry Andric // Because we are going top-down through the block, all value numbers 28920b57cec5SDimitry Andric // will be available in the predecessor by the time we need them. Any 28930b57cec5SDimitry Andric // that weren't originally present will have been instantiated earlier 28940b57cec5SDimitry Andric // in this loop. 28950b57cec5SDimitry Andric bool success = true; 28960b57cec5SDimitry Andric for (unsigned i = 0, e = Instr->getNumOperands(); i != e; ++i) { 28970b57cec5SDimitry Andric Value *Op = Instr->getOperand(i); 28980b57cec5SDimitry Andric if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op)) 28990b57cec5SDimitry Andric continue; 29000b57cec5SDimitry Andric // This could be a newly inserted instruction, in which case, we won't 29010b57cec5SDimitry Andric // find a value number, and should give up before we hurt ourselves. 29020b57cec5SDimitry Andric // FIXME: Rewrite the infrastructure to let it easier to value number 29030b57cec5SDimitry Andric // and process newly inserted instructions. 29040b57cec5SDimitry Andric if (!VN.exists(Op)) { 29050b57cec5SDimitry Andric success = false; 29060b57cec5SDimitry Andric break; 29070b57cec5SDimitry Andric } 29080b57cec5SDimitry Andric uint32_t TValNo = 29090b57cec5SDimitry Andric VN.phiTranslate(Pred, Curr, VN.lookup(Op), *this); 29100b57cec5SDimitry Andric if (Value *V = findLeader(Pred, TValNo)) { 29110b57cec5SDimitry Andric Instr->setOperand(i, V); 29120b57cec5SDimitry Andric } else { 29130b57cec5SDimitry Andric success = false; 29140b57cec5SDimitry Andric break; 29150b57cec5SDimitry Andric } 29160b57cec5SDimitry Andric } 29170b57cec5SDimitry Andric 29180b57cec5SDimitry Andric // Fail out if we encounter an operand that is not available in 29190b57cec5SDimitry Andric // the PRE predecessor. This is typically because of loads which 29200b57cec5SDimitry Andric // are not value numbered precisely. 29210b57cec5SDimitry Andric if (!success) 29220b57cec5SDimitry Andric return false; 29230b57cec5SDimitry Andric 29240b57cec5SDimitry Andric Instr->insertBefore(Pred->getTerminator()); 29250b57cec5SDimitry Andric Instr->setName(Instr->getName() + ".pre"); 29260b57cec5SDimitry Andric Instr->setDebugLoc(Instr->getDebugLoc()); 29270b57cec5SDimitry Andric 2928fe6060f1SDimitry Andric ICF->insertInstructionTo(Instr, Pred); 2929fe6060f1SDimitry Andric 29300b57cec5SDimitry Andric unsigned Num = VN.lookupOrAdd(Instr); 29310b57cec5SDimitry Andric VN.add(Instr, Num); 29320b57cec5SDimitry Andric 29330b57cec5SDimitry Andric // Update the availability map to include the new instruction. 2934*0fca6ea1SDimitry Andric LeaderTable.insert(Num, Instr, Pred); 29350b57cec5SDimitry Andric return true; 29360b57cec5SDimitry Andric } 29370b57cec5SDimitry Andric 2938349cc55cSDimitry Andric bool GVNPass::performScalarPRE(Instruction *CurInst) { 29390b57cec5SDimitry Andric if (isa<AllocaInst>(CurInst) || CurInst->isTerminator() || 29400b57cec5SDimitry Andric isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() || 29410b57cec5SDimitry Andric CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() || 29420b57cec5SDimitry Andric isa<DbgInfoIntrinsic>(CurInst)) 29430b57cec5SDimitry Andric return false; 29440b57cec5SDimitry Andric 29450b57cec5SDimitry Andric // Don't do PRE on compares. The PHI would prevent CodeGenPrepare from 29460b57cec5SDimitry Andric // sinking the compare again, and it would force the code generator to 29470b57cec5SDimitry Andric // move the i1 from processor flags or predicate registers into a general 29480b57cec5SDimitry Andric // purpose register. 29490b57cec5SDimitry Andric if (isa<CmpInst>(CurInst)) 29500b57cec5SDimitry Andric return false; 29510b57cec5SDimitry Andric 29520b57cec5SDimitry Andric // Don't do PRE on GEPs. The inserted PHI would prevent CodeGenPrepare from 29530b57cec5SDimitry Andric // sinking the addressing mode computation back to its uses. Extending the 29540b57cec5SDimitry Andric // GEP's live range increases the register pressure, and therefore it can 29550b57cec5SDimitry Andric // introduce unnecessary spills. 29560b57cec5SDimitry Andric // 29570b57cec5SDimitry Andric // This doesn't prevent Load PRE. PHI translation will make the GEP available 29580b57cec5SDimitry Andric // to the load by moving it to the predecessor block if necessary. 29590b57cec5SDimitry Andric if (isa<GetElementPtrInst>(CurInst)) 29600b57cec5SDimitry Andric return false; 29610b57cec5SDimitry Andric 2962e8d8bef9SDimitry Andric if (auto *CallB = dyn_cast<CallBase>(CurInst)) { 29630b57cec5SDimitry Andric // We don't currently value number ANY inline asm calls. 29640b57cec5SDimitry Andric if (CallB->isInlineAsm()) 29650b57cec5SDimitry Andric return false; 2966e8d8bef9SDimitry Andric } 29670b57cec5SDimitry Andric 29680b57cec5SDimitry Andric uint32_t ValNo = VN.lookup(CurInst); 29690b57cec5SDimitry Andric 29700b57cec5SDimitry Andric // Look for the predecessors for PRE opportunities. We're 29710b57cec5SDimitry Andric // only trying to solve the basic diamond case, where 29720b57cec5SDimitry Andric // a value is computed in the successor and one predecessor, 29730b57cec5SDimitry Andric // but not the other. We also explicitly disallow cases 29740b57cec5SDimitry Andric // where the successor is its own predecessor, because they're 29750b57cec5SDimitry Andric // more complicated to get right. 29760b57cec5SDimitry Andric unsigned NumWith = 0; 29770b57cec5SDimitry Andric unsigned NumWithout = 0; 29780b57cec5SDimitry Andric BasicBlock *PREPred = nullptr; 29790b57cec5SDimitry Andric BasicBlock *CurrentBlock = CurInst->getParent(); 29800b57cec5SDimitry Andric 29810b57cec5SDimitry Andric // Update the RPO numbers for this function. 29820b57cec5SDimitry Andric if (InvalidBlockRPONumbers) 29830b57cec5SDimitry Andric assignBlockRPONumber(*CurrentBlock->getParent()); 29840b57cec5SDimitry Andric 29850b57cec5SDimitry Andric SmallVector<std::pair<Value *, BasicBlock *>, 8> predMap; 29860b57cec5SDimitry Andric for (BasicBlock *P : predecessors(CurrentBlock)) { 29870b57cec5SDimitry Andric // We're not interested in PRE where blocks with predecessors that are 29880b57cec5SDimitry Andric // not reachable. 29890b57cec5SDimitry Andric if (!DT->isReachableFromEntry(P)) { 29900b57cec5SDimitry Andric NumWithout = 2; 29910b57cec5SDimitry Andric break; 29920b57cec5SDimitry Andric } 2993bdd1243dSDimitry Andric // It is not safe to do PRE when P->CurrentBlock is a loop backedge. 29940b57cec5SDimitry Andric assert(BlockRPONumber.count(P) && BlockRPONumber.count(CurrentBlock) && 29950b57cec5SDimitry Andric "Invalid BlockRPONumber map."); 2996bdd1243dSDimitry Andric if (BlockRPONumber[P] >= BlockRPONumber[CurrentBlock]) { 29970b57cec5SDimitry Andric NumWithout = 2; 29980b57cec5SDimitry Andric break; 29990b57cec5SDimitry Andric } 30000b57cec5SDimitry Andric 30010b57cec5SDimitry Andric uint32_t TValNo = VN.phiTranslate(P, CurrentBlock, ValNo, *this); 30020b57cec5SDimitry Andric Value *predV = findLeader(P, TValNo); 30030b57cec5SDimitry Andric if (!predV) { 30040b57cec5SDimitry Andric predMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P)); 30050b57cec5SDimitry Andric PREPred = P; 30060b57cec5SDimitry Andric ++NumWithout; 30070b57cec5SDimitry Andric } else if (predV == CurInst) { 30080b57cec5SDimitry Andric /* CurInst dominates this predecessor. */ 30090b57cec5SDimitry Andric NumWithout = 2; 30100b57cec5SDimitry Andric break; 30110b57cec5SDimitry Andric } else { 30120b57cec5SDimitry Andric predMap.push_back(std::make_pair(predV, P)); 30130b57cec5SDimitry Andric ++NumWith; 30140b57cec5SDimitry Andric } 30150b57cec5SDimitry Andric } 30160b57cec5SDimitry Andric 30170b57cec5SDimitry Andric // Don't do PRE when it might increase code size, i.e. when 30180b57cec5SDimitry Andric // we would need to insert instructions in more than one pred. 30190b57cec5SDimitry Andric if (NumWithout > 1 || NumWith == 0) 30200b57cec5SDimitry Andric return false; 30210b57cec5SDimitry Andric 30220b57cec5SDimitry Andric // We may have a case where all predecessors have the instruction, 30230b57cec5SDimitry Andric // and we just need to insert a phi node. Otherwise, perform 30240b57cec5SDimitry Andric // insertion. 30250b57cec5SDimitry Andric Instruction *PREInstr = nullptr; 30260b57cec5SDimitry Andric 30270b57cec5SDimitry Andric if (NumWithout != 0) { 30280b57cec5SDimitry Andric if (!isSafeToSpeculativelyExecute(CurInst)) { 30290b57cec5SDimitry Andric // It is only valid to insert a new instruction if the current instruction 30300b57cec5SDimitry Andric // is always executed. An instruction with implicit control flow could 30310b57cec5SDimitry Andric // prevent us from doing it. If we cannot speculate the execution, then 30320b57cec5SDimitry Andric // PRE should be prohibited. 30330b57cec5SDimitry Andric if (ICF->isDominatedByICFIFromSameBlock(CurInst)) 30340b57cec5SDimitry Andric return false; 30350b57cec5SDimitry Andric } 30360b57cec5SDimitry Andric 30370b57cec5SDimitry Andric // Don't do PRE across indirect branch. 30380b57cec5SDimitry Andric if (isa<IndirectBrInst>(PREPred->getTerminator())) 30390b57cec5SDimitry Andric return false; 30400b57cec5SDimitry Andric 30410b57cec5SDimitry Andric // We can't do PRE safely on a critical edge, so instead we schedule 30420b57cec5SDimitry Andric // the edge to be split and perform the PRE the next time we iterate 30430b57cec5SDimitry Andric // on the function. 30440b57cec5SDimitry Andric unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock); 30450b57cec5SDimitry Andric if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) { 30460b57cec5SDimitry Andric toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum)); 30470b57cec5SDimitry Andric return false; 30480b57cec5SDimitry Andric } 30490b57cec5SDimitry Andric // We need to insert somewhere, so let's give it a shot 30500b57cec5SDimitry Andric PREInstr = CurInst->clone(); 30510b57cec5SDimitry Andric if (!performScalarPREInsertion(PREInstr, PREPred, CurrentBlock, ValNo)) { 30520b57cec5SDimitry Andric // If we failed insertion, make sure we remove the instruction. 305306c3fb27SDimitry Andric #ifndef NDEBUG 305406c3fb27SDimitry Andric verifyRemoved(PREInstr); 305506c3fb27SDimitry Andric #endif 30560b57cec5SDimitry Andric PREInstr->deleteValue(); 30570b57cec5SDimitry Andric return false; 30580b57cec5SDimitry Andric } 30590b57cec5SDimitry Andric } 30600b57cec5SDimitry Andric 30610b57cec5SDimitry Andric // Either we should have filled in the PRE instruction, or we should 30620b57cec5SDimitry Andric // not have needed insertions. 30630b57cec5SDimitry Andric assert(PREInstr != nullptr || NumWithout == 0); 30640b57cec5SDimitry Andric 30650b57cec5SDimitry Andric ++NumGVNPRE; 30660b57cec5SDimitry Andric 30670b57cec5SDimitry Andric // Create a PHI to make the value available in this block. 30685f757f3fSDimitry Andric PHINode *Phi = PHINode::Create(CurInst->getType(), predMap.size(), 30695f757f3fSDimitry Andric CurInst->getName() + ".pre-phi"); 30705f757f3fSDimitry Andric Phi->insertBefore(CurrentBlock->begin()); 30710b57cec5SDimitry Andric for (unsigned i = 0, e = predMap.size(); i != e; ++i) { 30720b57cec5SDimitry Andric if (Value *V = predMap[i].first) { 30730b57cec5SDimitry Andric // If we use an existing value in this phi, we have to patch the original 30740b57cec5SDimitry Andric // value because the phi will be used to replace a later value. 30750b57cec5SDimitry Andric patchReplacementInstruction(CurInst, V); 30760b57cec5SDimitry Andric Phi->addIncoming(V, predMap[i].second); 30770b57cec5SDimitry Andric } else 30780b57cec5SDimitry Andric Phi->addIncoming(PREInstr, PREPred); 30790b57cec5SDimitry Andric } 30800b57cec5SDimitry Andric 30810b57cec5SDimitry Andric VN.add(Phi, ValNo); 30820b57cec5SDimitry Andric // After creating a new PHI for ValNo, the phi translate result for ValNo will 30830b57cec5SDimitry Andric // be changed, so erase the related stale entries in phi translate cache. 30840b57cec5SDimitry Andric VN.eraseTranslateCacheEntry(ValNo, *CurrentBlock); 3085*0fca6ea1SDimitry Andric LeaderTable.insert(ValNo, Phi, CurrentBlock); 30860b57cec5SDimitry Andric Phi->setDebugLoc(CurInst->getDebugLoc()); 30870b57cec5SDimitry Andric CurInst->replaceAllUsesWith(Phi); 30880b57cec5SDimitry Andric if (MD && Phi->getType()->isPtrOrPtrVectorTy()) 30890b57cec5SDimitry Andric MD->invalidateCachedPointerInfo(Phi); 30900b57cec5SDimitry Andric VN.erase(CurInst); 3091*0fca6ea1SDimitry Andric LeaderTable.erase(ValNo, CurInst, CurrentBlock); 30920b57cec5SDimitry Andric 30930b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n'); 309406c3fb27SDimitry Andric removeInstruction(CurInst); 30950b57cec5SDimitry Andric ++NumGVNInstr; 30960b57cec5SDimitry Andric 30970b57cec5SDimitry Andric return true; 30980b57cec5SDimitry Andric } 30990b57cec5SDimitry Andric 31000b57cec5SDimitry Andric /// Perform a purely local form of PRE that looks for diamond 31010b57cec5SDimitry Andric /// control flow patterns and attempts to perform simple PRE at the join point. 3102349cc55cSDimitry Andric bool GVNPass::performPRE(Function &F) { 31030b57cec5SDimitry Andric bool Changed = false; 31040b57cec5SDimitry Andric for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) { 31050b57cec5SDimitry Andric // Nothing to PRE in the entry block. 31060b57cec5SDimitry Andric if (CurrentBlock == &F.getEntryBlock()) 31070b57cec5SDimitry Andric continue; 31080b57cec5SDimitry Andric 31090b57cec5SDimitry Andric // Don't perform PRE on an EH pad. 31100b57cec5SDimitry Andric if (CurrentBlock->isEHPad()) 31110b57cec5SDimitry Andric continue; 31120b57cec5SDimitry Andric 31130b57cec5SDimitry Andric for (BasicBlock::iterator BI = CurrentBlock->begin(), 31140b57cec5SDimitry Andric BE = CurrentBlock->end(); 31150b57cec5SDimitry Andric BI != BE;) { 31160b57cec5SDimitry Andric Instruction *CurInst = &*BI++; 31170b57cec5SDimitry Andric Changed |= performScalarPRE(CurInst); 31180b57cec5SDimitry Andric } 31190b57cec5SDimitry Andric } 31200b57cec5SDimitry Andric 31210b57cec5SDimitry Andric if (splitCriticalEdges()) 31220b57cec5SDimitry Andric Changed = true; 31230b57cec5SDimitry Andric 31240b57cec5SDimitry Andric return Changed; 31250b57cec5SDimitry Andric } 31260b57cec5SDimitry Andric 31270b57cec5SDimitry Andric /// Split the critical edge connecting the given two blocks, and return 31280b57cec5SDimitry Andric /// the block inserted to the critical edge. 3129349cc55cSDimitry Andric BasicBlock *GVNPass::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) { 31305ffd83dbSDimitry Andric // GVN does not require loop-simplify, do not try to preserve it if it is not 31315ffd83dbSDimitry Andric // possible. 31325ffd83dbSDimitry Andric BasicBlock *BB = SplitCriticalEdge( 31335ffd83dbSDimitry Andric Pred, Succ, 3134e8d8bef9SDimitry Andric CriticalEdgeSplittingOptions(DT, LI, MSSAU).unsetPreserveLoopSimplify()); 3135e8d8bef9SDimitry Andric if (BB) { 31360b57cec5SDimitry Andric if (MD) 31370b57cec5SDimitry Andric MD->invalidateCachedPredecessors(); 31380b57cec5SDimitry Andric InvalidBlockRPONumbers = true; 3139e8d8bef9SDimitry Andric } 31400b57cec5SDimitry Andric return BB; 31410b57cec5SDimitry Andric } 31420b57cec5SDimitry Andric 31430b57cec5SDimitry Andric /// Split critical edges found during the previous 31440b57cec5SDimitry Andric /// iteration that may enable further optimization. 3145349cc55cSDimitry Andric bool GVNPass::splitCriticalEdges() { 31460b57cec5SDimitry Andric if (toSplit.empty()) 31470b57cec5SDimitry Andric return false; 3148e8d8bef9SDimitry Andric 3149e8d8bef9SDimitry Andric bool Changed = false; 31500b57cec5SDimitry Andric do { 31510b57cec5SDimitry Andric std::pair<Instruction *, unsigned> Edge = toSplit.pop_back_val(); 3152e8d8bef9SDimitry Andric Changed |= SplitCriticalEdge(Edge.first, Edge.second, 3153e8d8bef9SDimitry Andric CriticalEdgeSplittingOptions(DT, LI, MSSAU)) != 3154e8d8bef9SDimitry Andric nullptr; 31550b57cec5SDimitry Andric } while (!toSplit.empty()); 3156e8d8bef9SDimitry Andric if (Changed) { 3157e8d8bef9SDimitry Andric if (MD) 3158e8d8bef9SDimitry Andric MD->invalidateCachedPredecessors(); 31590b57cec5SDimitry Andric InvalidBlockRPONumbers = true; 3160e8d8bef9SDimitry Andric } 3161e8d8bef9SDimitry Andric return Changed; 31620b57cec5SDimitry Andric } 31630b57cec5SDimitry Andric 31640b57cec5SDimitry Andric /// Executes one iteration of GVN 3165349cc55cSDimitry Andric bool GVNPass::iterateOnFunction(Function &F) { 31660b57cec5SDimitry Andric cleanupGlobalSets(); 31670b57cec5SDimitry Andric 31680b57cec5SDimitry Andric // Top-down walk of the dominator tree 31690b57cec5SDimitry Andric bool Changed = false; 31700b57cec5SDimitry Andric // Needed for value numbering with phi construction to work. 31710b57cec5SDimitry Andric // RPOT walks the graph in its constructor and will not be invalidated during 31720b57cec5SDimitry Andric // processBlock. 31730b57cec5SDimitry Andric ReversePostOrderTraversal<Function *> RPOT(&F); 31740b57cec5SDimitry Andric 31750b57cec5SDimitry Andric for (BasicBlock *BB : RPOT) 31760b57cec5SDimitry Andric Changed |= processBlock(BB); 31770b57cec5SDimitry Andric 31780b57cec5SDimitry Andric return Changed; 31790b57cec5SDimitry Andric } 31800b57cec5SDimitry Andric 3181349cc55cSDimitry Andric void GVNPass::cleanupGlobalSets() { 31820b57cec5SDimitry Andric VN.clear(); 31830b57cec5SDimitry Andric LeaderTable.clear(); 31840b57cec5SDimitry Andric BlockRPONumber.clear(); 31850b57cec5SDimitry Andric ICF->clear(); 31860b57cec5SDimitry Andric InvalidBlockRPONumbers = true; 31870b57cec5SDimitry Andric } 31880b57cec5SDimitry Andric 318906c3fb27SDimitry Andric void GVNPass::removeInstruction(Instruction *I) { 319006c3fb27SDimitry Andric if (MD) MD->removeInstruction(I); 319106c3fb27SDimitry Andric if (MSSAU) 319206c3fb27SDimitry Andric MSSAU->removeMemoryAccess(I); 319306c3fb27SDimitry Andric #ifndef NDEBUG 319406c3fb27SDimitry Andric verifyRemoved(I); 319506c3fb27SDimitry Andric #endif 319606c3fb27SDimitry Andric ICF->removeInstruction(I); 319706c3fb27SDimitry Andric I->eraseFromParent(); 319806c3fb27SDimitry Andric } 319906c3fb27SDimitry Andric 32000b57cec5SDimitry Andric /// Verify that the specified instruction does not occur in our 32010b57cec5SDimitry Andric /// internal data structures. 3202349cc55cSDimitry Andric void GVNPass::verifyRemoved(const Instruction *Inst) const { 32030b57cec5SDimitry Andric VN.verifyRemoved(Inst); 3204*0fca6ea1SDimitry Andric LeaderTable.verifyRemoved(Inst); 32050b57cec5SDimitry Andric } 32060b57cec5SDimitry Andric 32070b57cec5SDimitry Andric /// BB is declared dead, which implied other blocks become dead as well. This 32080b57cec5SDimitry Andric /// function is to add all these blocks to "DeadBlocks". For the dead blocks' 32090b57cec5SDimitry Andric /// live successors, update their phi nodes by replacing the operands 32100b57cec5SDimitry Andric /// corresponding to dead blocks with UndefVal. 3211349cc55cSDimitry Andric void GVNPass::addDeadBlock(BasicBlock *BB) { 32120b57cec5SDimitry Andric SmallVector<BasicBlock *, 4> NewDead; 32130b57cec5SDimitry Andric SmallSetVector<BasicBlock *, 4> DF; 32140b57cec5SDimitry Andric 32150b57cec5SDimitry Andric NewDead.push_back(BB); 32160b57cec5SDimitry Andric while (!NewDead.empty()) { 32170b57cec5SDimitry Andric BasicBlock *D = NewDead.pop_back_val(); 32180b57cec5SDimitry Andric if (DeadBlocks.count(D)) 32190b57cec5SDimitry Andric continue; 32200b57cec5SDimitry Andric 32210b57cec5SDimitry Andric // All blocks dominated by D are dead. 32220b57cec5SDimitry Andric SmallVector<BasicBlock *, 8> Dom; 32230b57cec5SDimitry Andric DT->getDescendants(D, Dom); 32240b57cec5SDimitry Andric DeadBlocks.insert(Dom.begin(), Dom.end()); 32250b57cec5SDimitry Andric 32260b57cec5SDimitry Andric // Figure out the dominance-frontier(D). 32270b57cec5SDimitry Andric for (BasicBlock *B : Dom) { 32280b57cec5SDimitry Andric for (BasicBlock *S : successors(B)) { 32290b57cec5SDimitry Andric if (DeadBlocks.count(S)) 32300b57cec5SDimitry Andric continue; 32310b57cec5SDimitry Andric 32320b57cec5SDimitry Andric bool AllPredDead = true; 32330b57cec5SDimitry Andric for (BasicBlock *P : predecessors(S)) 32340b57cec5SDimitry Andric if (!DeadBlocks.count(P)) { 32350b57cec5SDimitry Andric AllPredDead = false; 32360b57cec5SDimitry Andric break; 32370b57cec5SDimitry Andric } 32380b57cec5SDimitry Andric 32390b57cec5SDimitry Andric if (!AllPredDead) { 32400b57cec5SDimitry Andric // S could be proved dead later on. That is why we don't update phi 32410b57cec5SDimitry Andric // operands at this moment. 32420b57cec5SDimitry Andric DF.insert(S); 32430b57cec5SDimitry Andric } else { 32440b57cec5SDimitry Andric // While S is not dominated by D, it is dead by now. This could take 32450b57cec5SDimitry Andric // place if S already have a dead predecessor before D is declared 32460b57cec5SDimitry Andric // dead. 32470b57cec5SDimitry Andric NewDead.push_back(S); 32480b57cec5SDimitry Andric } 32490b57cec5SDimitry Andric } 32500b57cec5SDimitry Andric } 32510b57cec5SDimitry Andric } 32520b57cec5SDimitry Andric 32530b57cec5SDimitry Andric // For the dead blocks' live successors, update their phi nodes by replacing 32540b57cec5SDimitry Andric // the operands corresponding to dead blocks with UndefVal. 3255fe6060f1SDimitry Andric for (BasicBlock *B : DF) { 32560b57cec5SDimitry Andric if (DeadBlocks.count(B)) 32570b57cec5SDimitry Andric continue; 32580b57cec5SDimitry Andric 32598bcb0991SDimitry Andric // First, split the critical edges. This might also create additional blocks 32608bcb0991SDimitry Andric // to preserve LoopSimplify form and adjust edges accordingly. 3261e8d8bef9SDimitry Andric SmallVector<BasicBlock *, 4> Preds(predecessors(B)); 32620b57cec5SDimitry Andric for (BasicBlock *P : Preds) { 32630b57cec5SDimitry Andric if (!DeadBlocks.count(P)) 32640b57cec5SDimitry Andric continue; 32650b57cec5SDimitry Andric 3266e8d8bef9SDimitry Andric if (llvm::is_contained(successors(P), B) && 32678bcb0991SDimitry Andric isCriticalEdge(P->getTerminator(), B)) { 32680b57cec5SDimitry Andric if (BasicBlock *S = splitCriticalEdges(P, B)) 32690b57cec5SDimitry Andric DeadBlocks.insert(P = S); 32700b57cec5SDimitry Andric } 32718bcb0991SDimitry Andric } 32720b57cec5SDimitry Andric 327304eeddc0SDimitry Andric // Now poison the incoming values from the dead predecessors. 32748bcb0991SDimitry Andric for (BasicBlock *P : predecessors(B)) { 32758bcb0991SDimitry Andric if (!DeadBlocks.count(P)) 32768bcb0991SDimitry Andric continue; 32778bcb0991SDimitry Andric for (PHINode &Phi : B->phis()) { 327804eeddc0SDimitry Andric Phi.setIncomingValueForBlock(P, PoisonValue::get(Phi.getType())); 32790b57cec5SDimitry Andric if (MD) 32800b57cec5SDimitry Andric MD->invalidateCachedPointerInfo(&Phi); 32810b57cec5SDimitry Andric } 32820b57cec5SDimitry Andric } 32830b57cec5SDimitry Andric } 32840b57cec5SDimitry Andric } 32850b57cec5SDimitry Andric 32860b57cec5SDimitry Andric // If the given branch is recognized as a foldable branch (i.e. conditional 32870b57cec5SDimitry Andric // branch with constant condition), it will perform following analyses and 32880b57cec5SDimitry Andric // transformation. 32890b57cec5SDimitry Andric // 1) If the dead out-coming edge is a critical-edge, split it. Let 32900b57cec5SDimitry Andric // R be the target of the dead out-coming edge. 32910b57cec5SDimitry Andric // 1) Identify the set of dead blocks implied by the branch's dead outcoming 32920b57cec5SDimitry Andric // edge. The result of this step will be {X| X is dominated by R} 32930b57cec5SDimitry Andric // 2) Identify those blocks which haves at least one dead predecessor. The 32940b57cec5SDimitry Andric // result of this step will be dominance-frontier(R). 32950b57cec5SDimitry Andric // 3) Update the PHIs in DF(R) by replacing the operands corresponding to 32960b57cec5SDimitry Andric // dead blocks with "UndefVal" in an hope these PHIs will optimized away. 32970b57cec5SDimitry Andric // 32980b57cec5SDimitry Andric // Return true iff *NEW* dead code are found. 3299349cc55cSDimitry Andric bool GVNPass::processFoldableCondBr(BranchInst *BI) { 33000b57cec5SDimitry Andric if (!BI || BI->isUnconditional()) 33010b57cec5SDimitry Andric return false; 33020b57cec5SDimitry Andric 33030b57cec5SDimitry Andric // If a branch has two identical successors, we cannot declare either dead. 33040b57cec5SDimitry Andric if (BI->getSuccessor(0) == BI->getSuccessor(1)) 33050b57cec5SDimitry Andric return false; 33060b57cec5SDimitry Andric 33070b57cec5SDimitry Andric ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); 33080b57cec5SDimitry Andric if (!Cond) 33090b57cec5SDimitry Andric return false; 33100b57cec5SDimitry Andric 33110b57cec5SDimitry Andric BasicBlock *DeadRoot = 33120b57cec5SDimitry Andric Cond->getZExtValue() ? BI->getSuccessor(1) : BI->getSuccessor(0); 33130b57cec5SDimitry Andric if (DeadBlocks.count(DeadRoot)) 33140b57cec5SDimitry Andric return false; 33150b57cec5SDimitry Andric 33160b57cec5SDimitry Andric if (!DeadRoot->getSinglePredecessor()) 33170b57cec5SDimitry Andric DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot); 33180b57cec5SDimitry Andric 33190b57cec5SDimitry Andric addDeadBlock(DeadRoot); 33200b57cec5SDimitry Andric return true; 33210b57cec5SDimitry Andric } 33220b57cec5SDimitry Andric 33230b57cec5SDimitry Andric // performPRE() will trigger assert if it comes across an instruction without 33240b57cec5SDimitry Andric // associated val-num. As it normally has far more live instructions than dead 33250b57cec5SDimitry Andric // instructions, it makes more sense just to "fabricate" a val-number for the 33260b57cec5SDimitry Andric // dead code than checking if instruction involved is dead or not. 3327349cc55cSDimitry Andric void GVNPass::assignValNumForDeadCode() { 33280b57cec5SDimitry Andric for (BasicBlock *BB : DeadBlocks) { 33290b57cec5SDimitry Andric for (Instruction &Inst : *BB) { 33300b57cec5SDimitry Andric unsigned ValNum = VN.lookupOrAdd(&Inst); 3331*0fca6ea1SDimitry Andric LeaderTable.insert(ValNum, &Inst, BB); 33320b57cec5SDimitry Andric } 33330b57cec5SDimitry Andric } 33340b57cec5SDimitry Andric } 33350b57cec5SDimitry Andric 33360b57cec5SDimitry Andric class llvm::gvn::GVNLegacyPass : public FunctionPass { 33370b57cec5SDimitry Andric public: 33380b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 33390b57cec5SDimitry Andric 33405ffd83dbSDimitry Andric explicit GVNLegacyPass(bool NoMemDepAnalysis = !GVNEnableMemDep) 33415ffd83dbSDimitry Andric : FunctionPass(ID), Impl(GVNOptions().setMemDep(!NoMemDepAnalysis)) { 33420b57cec5SDimitry Andric initializeGVNLegacyPassPass(*PassRegistry::getPassRegistry()); 33430b57cec5SDimitry Andric } 33440b57cec5SDimitry Andric 33450b57cec5SDimitry Andric bool runOnFunction(Function &F) override { 33460b57cec5SDimitry Andric if (skipFunction(F)) 33470b57cec5SDimitry Andric return false; 33480b57cec5SDimitry Andric 3349e8d8bef9SDimitry Andric auto *MSSAWP = getAnalysisIfAvailable<MemorySSAWrapperPass>(); 33500b57cec5SDimitry Andric return Impl.runImpl( 33510b57cec5SDimitry Andric F, getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F), 33520b57cec5SDimitry Andric getAnalysis<DominatorTreeWrapperPass>().getDomTree(), 33538bcb0991SDimitry Andric getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F), 33540b57cec5SDimitry Andric getAnalysis<AAResultsWrapperPass>().getAAResults(), 33555ffd83dbSDimitry Andric Impl.isMemDepEnabled() 33565ffd83dbSDimitry Andric ? &getAnalysis<MemoryDependenceWrapperPass>().getMemDep() 33575ffd83dbSDimitry Andric : nullptr, 33585f757f3fSDimitry Andric getAnalysis<LoopInfoWrapperPass>().getLoopInfo(), 3359e8d8bef9SDimitry Andric &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(), 3360e8d8bef9SDimitry Andric MSSAWP ? &MSSAWP->getMSSA() : nullptr); 33610b57cec5SDimitry Andric } 33620b57cec5SDimitry Andric 33630b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 33640b57cec5SDimitry Andric AU.addRequired<AssumptionCacheTracker>(); 33650b57cec5SDimitry Andric AU.addRequired<DominatorTreeWrapperPass>(); 33660b57cec5SDimitry Andric AU.addRequired<TargetLibraryInfoWrapperPass>(); 33678bcb0991SDimitry Andric AU.addRequired<LoopInfoWrapperPass>(); 33685ffd83dbSDimitry Andric if (Impl.isMemDepEnabled()) 33690b57cec5SDimitry Andric AU.addRequired<MemoryDependenceWrapperPass>(); 33700b57cec5SDimitry Andric AU.addRequired<AAResultsWrapperPass>(); 33710b57cec5SDimitry Andric AU.addPreserved<DominatorTreeWrapperPass>(); 33720b57cec5SDimitry Andric AU.addPreserved<GlobalsAAWrapperPass>(); 33730b57cec5SDimitry Andric AU.addPreserved<TargetLibraryInfoWrapperPass>(); 33748bcb0991SDimitry Andric AU.addPreserved<LoopInfoWrapperPass>(); 33750b57cec5SDimitry Andric AU.addRequired<OptimizationRemarkEmitterWrapperPass>(); 3376e8d8bef9SDimitry Andric AU.addPreserved<MemorySSAWrapperPass>(); 33770b57cec5SDimitry Andric } 33780b57cec5SDimitry Andric 33790b57cec5SDimitry Andric private: 3380349cc55cSDimitry Andric GVNPass Impl; 33810b57cec5SDimitry Andric }; 33820b57cec5SDimitry Andric 33830b57cec5SDimitry Andric char GVNLegacyPass::ID = 0; 33840b57cec5SDimitry Andric 33850b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(GVNLegacyPass, "gvn", "Global Value Numbering", false, false) 33860b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 33870b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass) 33880b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 33890b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 33900b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 33910b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass) 33920b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass) 33930b57cec5SDimitry Andric INITIALIZE_PASS_END(GVNLegacyPass, "gvn", "Global Value Numbering", false, false) 33940b57cec5SDimitry Andric 33950b57cec5SDimitry Andric // The public interface to this file... 33960b57cec5SDimitry Andric FunctionPass *llvm::createGVNPass(bool NoMemDepAnalysis) { 33970b57cec5SDimitry Andric return new GVNLegacyPass(NoMemDepAnalysis); 33980b57cec5SDimitry Andric } 3399