1*0b57cec5SDimitry Andric //===- MergeFunctions.cpp - Merge identical functions ---------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This pass looks for equivalent functions that are mergable and folds them. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric // Order relation is defined on set of functions. It was made through 12*0b57cec5SDimitry Andric // special function comparison procedure that returns 13*0b57cec5SDimitry Andric // 0 when functions are equal, 14*0b57cec5SDimitry Andric // -1 when Left function is less than right function, and 15*0b57cec5SDimitry Andric // 1 for opposite case. We need total-ordering, so we need to maintain 16*0b57cec5SDimitry Andric // four properties on the functions set: 17*0b57cec5SDimitry Andric // a <= a (reflexivity) 18*0b57cec5SDimitry Andric // if a <= b and b <= a then a = b (antisymmetry) 19*0b57cec5SDimitry Andric // if a <= b and b <= c then a <= c (transitivity). 20*0b57cec5SDimitry Andric // for all a and b: a <= b or b <= a (totality). 21*0b57cec5SDimitry Andric // 22*0b57cec5SDimitry Andric // Comparison iterates through each instruction in each basic block. 23*0b57cec5SDimitry Andric // Functions are kept on binary tree. For each new function F we perform 24*0b57cec5SDimitry Andric // lookup in binary tree. 25*0b57cec5SDimitry Andric // In practice it works the following way: 26*0b57cec5SDimitry Andric // -- We define Function* container class with custom "operator<" (FunctionPtr). 27*0b57cec5SDimitry Andric // -- "FunctionPtr" instances are stored in std::set collection, so every 28*0b57cec5SDimitry Andric // std::set::insert operation will give you result in log(N) time. 29*0b57cec5SDimitry Andric // 30*0b57cec5SDimitry Andric // As an optimization, a hash of the function structure is calculated first, and 31*0b57cec5SDimitry Andric // two functions are only compared if they have the same hash. This hash is 32*0b57cec5SDimitry Andric // cheap to compute, and has the property that if function F == G according to 33*0b57cec5SDimitry Andric // the comparison function, then hash(F) == hash(G). This consistency property 34*0b57cec5SDimitry Andric // is critical to ensuring all possible merging opportunities are exploited. 35*0b57cec5SDimitry Andric // Collisions in the hash affect the speed of the pass but not the correctness 36*0b57cec5SDimitry Andric // or determinism of the resulting transformation. 37*0b57cec5SDimitry Andric // 38*0b57cec5SDimitry Andric // When a match is found the functions are folded. If both functions are 39*0b57cec5SDimitry Andric // overridable, we move the functionality into a new internal function and 40*0b57cec5SDimitry Andric // leave two overridable thunks to it. 41*0b57cec5SDimitry Andric // 42*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 43*0b57cec5SDimitry Andric // 44*0b57cec5SDimitry Andric // Future work: 45*0b57cec5SDimitry Andric // 46*0b57cec5SDimitry Andric // * virtual functions. 47*0b57cec5SDimitry Andric // 48*0b57cec5SDimitry Andric // Many functions have their address taken by the virtual function table for 49*0b57cec5SDimitry Andric // the object they belong to. However, as long as it's only used for a lookup 50*0b57cec5SDimitry Andric // and call, this is irrelevant, and we'd like to fold such functions. 51*0b57cec5SDimitry Andric // 52*0b57cec5SDimitry Andric // * be smarter about bitcasts. 53*0b57cec5SDimitry Andric // 54*0b57cec5SDimitry Andric // In order to fold functions, we will sometimes add either bitcast instructions 55*0b57cec5SDimitry Andric // or bitcast constant expressions. Unfortunately, this can confound further 56*0b57cec5SDimitry Andric // analysis since the two functions differ where one has a bitcast and the 57*0b57cec5SDimitry Andric // other doesn't. We should learn to look through bitcasts. 58*0b57cec5SDimitry Andric // 59*0b57cec5SDimitry Andric // * Compare complex types with pointer types inside. 60*0b57cec5SDimitry Andric // * Compare cross-reference cases. 61*0b57cec5SDimitry Andric // * Compare complex expressions. 62*0b57cec5SDimitry Andric // 63*0b57cec5SDimitry Andric // All the three issues above could be described as ability to prove that 64*0b57cec5SDimitry Andric // fA == fB == fC == fE == fF == fG in example below: 65*0b57cec5SDimitry Andric // 66*0b57cec5SDimitry Andric // void fA() { 67*0b57cec5SDimitry Andric // fB(); 68*0b57cec5SDimitry Andric // } 69*0b57cec5SDimitry Andric // void fB() { 70*0b57cec5SDimitry Andric // fA(); 71*0b57cec5SDimitry Andric // } 72*0b57cec5SDimitry Andric // 73*0b57cec5SDimitry Andric // void fE() { 74*0b57cec5SDimitry Andric // fF(); 75*0b57cec5SDimitry Andric // } 76*0b57cec5SDimitry Andric // void fF() { 77*0b57cec5SDimitry Andric // fG(); 78*0b57cec5SDimitry Andric // } 79*0b57cec5SDimitry Andric // void fG() { 80*0b57cec5SDimitry Andric // fE(); 81*0b57cec5SDimitry Andric // } 82*0b57cec5SDimitry Andric // 83*0b57cec5SDimitry Andric // Simplest cross-reference case (fA <--> fB) was implemented in previous 84*0b57cec5SDimitry Andric // versions of MergeFunctions, though it presented only in two function pairs 85*0b57cec5SDimitry Andric // in test-suite (that counts >50k functions) 86*0b57cec5SDimitry Andric // Though possibility to detect complex cross-referencing (e.g.: A->B->C->D->A) 87*0b57cec5SDimitry Andric // could cover much more cases. 88*0b57cec5SDimitry Andric // 89*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 90*0b57cec5SDimitry Andric 9181ad6265SDimitry Andric #include "llvm/Transforms/IPO/MergeFunctions.h" 92*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 93*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 94*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 95*0b57cec5SDimitry Andric #include "llvm/IR/Argument.h" 96*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 97*0b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 98*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 99*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 100*0b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h" 101*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 102*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 103*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 104*0b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h" 105*0b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h" 106*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 107*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 108*0b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 109*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 110*0b57cec5SDimitry Andric #include "llvm/IR/Type.h" 111*0b57cec5SDimitry Andric #include "llvm/IR/Use.h" 112*0b57cec5SDimitry Andric #include "llvm/IR/User.h" 113*0b57cec5SDimitry Andric #include "llvm/IR/Value.h" 114*0b57cec5SDimitry Andric #include "llvm/IR/ValueHandle.h" 115480093f4SDimitry Andric #include "llvm/InitializePasses.h" 116*0b57cec5SDimitry Andric #include "llvm/Pass.h" 117*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 118*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 119*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 120*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 121*0b57cec5SDimitry Andric #include "llvm/Transforms/IPO.h" 122*0b57cec5SDimitry Andric #include "llvm/Transforms/Utils/FunctionComparator.h" 12381ad6265SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h" 124*0b57cec5SDimitry Andric #include <algorithm> 125*0b57cec5SDimitry Andric #include <cassert> 126*0b57cec5SDimitry Andric #include <iterator> 127*0b57cec5SDimitry Andric #include <set> 128*0b57cec5SDimitry Andric #include <utility> 129*0b57cec5SDimitry Andric #include <vector> 130*0b57cec5SDimitry Andric 131*0b57cec5SDimitry Andric using namespace llvm; 132*0b57cec5SDimitry Andric 133*0b57cec5SDimitry Andric #define DEBUG_TYPE "mergefunc" 134*0b57cec5SDimitry Andric 135*0b57cec5SDimitry Andric STATISTIC(NumFunctionsMerged, "Number of functions merged"); 136*0b57cec5SDimitry Andric STATISTIC(NumThunksWritten, "Number of thunks generated"); 137*0b57cec5SDimitry Andric STATISTIC(NumAliasesWritten, "Number of aliases generated"); 138*0b57cec5SDimitry Andric STATISTIC(NumDoubleWeak, "Number of new functions created"); 139*0b57cec5SDimitry Andric 14081ad6265SDimitry Andric static cl::opt<unsigned> NumFunctionsForVerificationCheck( 14181ad6265SDimitry Andric "mergefunc-verify", 14281ad6265SDimitry Andric cl::desc("How many functions in a module could be used for " 14381ad6265SDimitry Andric "MergeFunctions to pass a basic correctness check. " 144*0b57cec5SDimitry Andric "'0' disables this check. Works only with '-debug' key."), 145*0b57cec5SDimitry Andric cl::init(0), cl::Hidden); 146*0b57cec5SDimitry Andric 147*0b57cec5SDimitry Andric // Under option -mergefunc-preserve-debug-info we: 148*0b57cec5SDimitry Andric // - Do not create a new function for a thunk. 149*0b57cec5SDimitry Andric // - Retain the debug info for a thunk's parameters (and associated 150*0b57cec5SDimitry Andric // instructions for the debug info) from the entry block. 151*0b57cec5SDimitry Andric // Note: -debug will display the algorithm at work. 152*0b57cec5SDimitry Andric // - Create debug-info for the call (to the shared implementation) made by 153*0b57cec5SDimitry Andric // a thunk and its return value. 154*0b57cec5SDimitry Andric // - Erase the rest of the function, retaining the (minimally sized) entry 155*0b57cec5SDimitry Andric // block to create a thunk. 156*0b57cec5SDimitry Andric // - Preserve a thunk's call site to point to the thunk even when both occur 157*0b57cec5SDimitry Andric // within the same translation unit, to aid debugability. Note that this 158*0b57cec5SDimitry Andric // behaviour differs from the underlying -mergefunc implementation which 159*0b57cec5SDimitry Andric // modifies the thunk's call site to point to the shared implementation 160*0b57cec5SDimitry Andric // when both occur within the same translation unit. 161*0b57cec5SDimitry Andric static cl::opt<bool> 162*0b57cec5SDimitry Andric MergeFunctionsPDI("mergefunc-preserve-debug-info", cl::Hidden, 163*0b57cec5SDimitry Andric cl::init(false), 164*0b57cec5SDimitry Andric cl::desc("Preserve debug info in thunk when mergefunc " 165*0b57cec5SDimitry Andric "transformations are made.")); 166*0b57cec5SDimitry Andric 167*0b57cec5SDimitry Andric static cl::opt<bool> 168*0b57cec5SDimitry Andric MergeFunctionsAliases("mergefunc-use-aliases", cl::Hidden, 169*0b57cec5SDimitry Andric cl::init(false), 170*0b57cec5SDimitry Andric cl::desc("Allow mergefunc to create aliases")); 171*0b57cec5SDimitry Andric 172*0b57cec5SDimitry Andric namespace { 173*0b57cec5SDimitry Andric 174*0b57cec5SDimitry Andric class FunctionNode { 175*0b57cec5SDimitry Andric mutable AssertingVH<Function> F; 176*0b57cec5SDimitry Andric FunctionComparator::FunctionHash Hash; 177*0b57cec5SDimitry Andric 178*0b57cec5SDimitry Andric public: 179*0b57cec5SDimitry Andric // Note the hash is recalculated potentially multiple times, but it is cheap. 180*0b57cec5SDimitry Andric FunctionNode(Function *F) 181*0b57cec5SDimitry Andric : F(F), Hash(FunctionComparator::functionHash(*F)) {} 182*0b57cec5SDimitry Andric 183*0b57cec5SDimitry Andric Function *getFunc() const { return F; } 184*0b57cec5SDimitry Andric FunctionComparator::FunctionHash getHash() const { return Hash; } 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric /// Replace the reference to the function F by the function G, assuming their 187*0b57cec5SDimitry Andric /// implementations are equal. 188*0b57cec5SDimitry Andric void replaceBy(Function *G) const { 189*0b57cec5SDimitry Andric F = G; 190*0b57cec5SDimitry Andric } 191*0b57cec5SDimitry Andric }; 192*0b57cec5SDimitry Andric 193*0b57cec5SDimitry Andric /// MergeFunctions finds functions which will generate identical machine code, 194*0b57cec5SDimitry Andric /// by considering all pointer types to be equivalent. Once identified, 195*0b57cec5SDimitry Andric /// MergeFunctions will fold them by replacing a call to one to a call to a 196*0b57cec5SDimitry Andric /// bitcast of the other. 197480093f4SDimitry Andric class MergeFunctions { 198*0b57cec5SDimitry Andric public: 199480093f4SDimitry Andric MergeFunctions() : FnTree(FunctionNodeCmp(&GlobalNumbers)) { 200*0b57cec5SDimitry Andric } 201*0b57cec5SDimitry Andric 202480093f4SDimitry Andric bool runOnModule(Module &M); 203*0b57cec5SDimitry Andric 204*0b57cec5SDimitry Andric private: 205*0b57cec5SDimitry Andric // The function comparison operator is provided here so that FunctionNodes do 206*0b57cec5SDimitry Andric // not need to become larger with another pointer. 207*0b57cec5SDimitry Andric class FunctionNodeCmp { 208*0b57cec5SDimitry Andric GlobalNumberState* GlobalNumbers; 209*0b57cec5SDimitry Andric 210*0b57cec5SDimitry Andric public: 211*0b57cec5SDimitry Andric FunctionNodeCmp(GlobalNumberState* GN) : GlobalNumbers(GN) {} 212*0b57cec5SDimitry Andric 213*0b57cec5SDimitry Andric bool operator()(const FunctionNode &LHS, const FunctionNode &RHS) const { 214*0b57cec5SDimitry Andric // Order first by hashes, then full function comparison. 215*0b57cec5SDimitry Andric if (LHS.getHash() != RHS.getHash()) 216*0b57cec5SDimitry Andric return LHS.getHash() < RHS.getHash(); 217*0b57cec5SDimitry Andric FunctionComparator FCmp(LHS.getFunc(), RHS.getFunc(), GlobalNumbers); 218bdd1243dSDimitry Andric return FCmp.compare() < 0; 219*0b57cec5SDimitry Andric } 220*0b57cec5SDimitry Andric }; 221*0b57cec5SDimitry Andric using FnTreeType = std::set<FunctionNode, FunctionNodeCmp>; 222*0b57cec5SDimitry Andric 223*0b57cec5SDimitry Andric GlobalNumberState GlobalNumbers; 224*0b57cec5SDimitry Andric 225*0b57cec5SDimitry Andric /// A work queue of functions that may have been modified and should be 226*0b57cec5SDimitry Andric /// analyzed again. 227*0b57cec5SDimitry Andric std::vector<WeakTrackingVH> Deferred; 228*0b57cec5SDimitry Andric 22981ad6265SDimitry Andric /// Set of values marked as used in llvm.used and llvm.compiler.used. 23081ad6265SDimitry Andric SmallPtrSet<GlobalValue *, 4> Used; 23181ad6265SDimitry Andric 232*0b57cec5SDimitry Andric #ifndef NDEBUG 233*0b57cec5SDimitry Andric /// Checks the rules of order relation introduced among functions set. 23481ad6265SDimitry Andric /// Returns true, if check has been passed, and false if failed. 23581ad6265SDimitry Andric bool doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist); 236*0b57cec5SDimitry Andric #endif 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric /// Insert a ComparableFunction into the FnTree, or merge it away if it's 239*0b57cec5SDimitry Andric /// equal to one that's already present. 240*0b57cec5SDimitry Andric bool insert(Function *NewFunction); 241*0b57cec5SDimitry Andric 242*0b57cec5SDimitry Andric /// Remove a Function from the FnTree and queue it up for a second sweep of 243*0b57cec5SDimitry Andric /// analysis. 244*0b57cec5SDimitry Andric void remove(Function *F); 245*0b57cec5SDimitry Andric 246*0b57cec5SDimitry Andric /// Find the functions that use this Value and remove them from FnTree and 247*0b57cec5SDimitry Andric /// queue the functions. 248*0b57cec5SDimitry Andric void removeUsers(Value *V); 249*0b57cec5SDimitry Andric 250*0b57cec5SDimitry Andric /// Replace all direct calls of Old with calls of New. Will bitcast New if 251*0b57cec5SDimitry Andric /// necessary to make types match. 252*0b57cec5SDimitry Andric void replaceDirectCallers(Function *Old, Function *New); 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric /// Merge two equivalent functions. Upon completion, G may be deleted, or may 255*0b57cec5SDimitry Andric /// be converted into a thunk. In either case, it should never be visited 256*0b57cec5SDimitry Andric /// again. 257*0b57cec5SDimitry Andric void mergeTwoFunctions(Function *F, Function *G); 258*0b57cec5SDimitry Andric 259*0b57cec5SDimitry Andric /// Fill PDIUnrelatedWL with instructions from the entry block that are 260*0b57cec5SDimitry Andric /// unrelated to parameter related debug info. 261*0b57cec5SDimitry Andric void filterInstsUnrelatedToPDI(BasicBlock *GEntryBlock, 262*0b57cec5SDimitry Andric std::vector<Instruction *> &PDIUnrelatedWL); 263*0b57cec5SDimitry Andric 264*0b57cec5SDimitry Andric /// Erase the rest of the CFG (i.e. barring the entry block). 265*0b57cec5SDimitry Andric void eraseTail(Function *G); 266*0b57cec5SDimitry Andric 267*0b57cec5SDimitry Andric /// Erase the instructions in PDIUnrelatedWL as they are unrelated to the 268*0b57cec5SDimitry Andric /// parameter debug info, from the entry block. 269*0b57cec5SDimitry Andric void eraseInstsUnrelatedToPDI(std::vector<Instruction *> &PDIUnrelatedWL); 270*0b57cec5SDimitry Andric 271*0b57cec5SDimitry Andric /// Replace G with a simple tail call to bitcast(F). Also (unless 272*0b57cec5SDimitry Andric /// MergeFunctionsPDI holds) replace direct uses of G with bitcast(F), 273*0b57cec5SDimitry Andric /// delete G. 274*0b57cec5SDimitry Andric void writeThunk(Function *F, Function *G); 275*0b57cec5SDimitry Andric 276*0b57cec5SDimitry Andric // Replace G with an alias to F (deleting function G) 277*0b57cec5SDimitry Andric void writeAlias(Function *F, Function *G); 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric // Replace G with an alias to F if possible, or a thunk to F if possible. 280*0b57cec5SDimitry Andric // Returns false if neither is the case. 281*0b57cec5SDimitry Andric bool writeThunkOrAlias(Function *F, Function *G); 282*0b57cec5SDimitry Andric 283*0b57cec5SDimitry Andric /// Replace function F with function G in the function tree. 284*0b57cec5SDimitry Andric void replaceFunctionInTree(const FunctionNode &FN, Function *G); 285*0b57cec5SDimitry Andric 286*0b57cec5SDimitry Andric /// The set of all distinct functions. Use the insert() and remove() methods 287*0b57cec5SDimitry Andric /// to modify it. The map allows efficient lookup and deferring of Functions. 288*0b57cec5SDimitry Andric FnTreeType FnTree; 289*0b57cec5SDimitry Andric 290*0b57cec5SDimitry Andric // Map functions to the iterators of the FunctionNode which contains them 291*0b57cec5SDimitry Andric // in the FnTree. This must be updated carefully whenever the FnTree is 292*0b57cec5SDimitry Andric // modified, i.e. in insert(), remove(), and replaceFunctionInTree(), to avoid 293*0b57cec5SDimitry Andric // dangling iterators into FnTree. The invariant that preserves this is that 294*0b57cec5SDimitry Andric // there is exactly one mapping F -> FN for each FunctionNode FN in FnTree. 295*0b57cec5SDimitry Andric DenseMap<AssertingVH<Function>, FnTreeType::iterator> FNodesInTree; 296*0b57cec5SDimitry Andric }; 297*0b57cec5SDimitry Andric 298480093f4SDimitry Andric class MergeFunctionsLegacyPass : public ModulePass { 299480093f4SDimitry Andric public: 300480093f4SDimitry Andric static char ID; 301480093f4SDimitry Andric 302480093f4SDimitry Andric MergeFunctionsLegacyPass(): ModulePass(ID) { 303480093f4SDimitry Andric initializeMergeFunctionsLegacyPassPass(*PassRegistry::getPassRegistry()); 304480093f4SDimitry Andric } 305480093f4SDimitry Andric 306480093f4SDimitry Andric bool runOnModule(Module &M) override { 307480093f4SDimitry Andric if (skipModule(M)) 308480093f4SDimitry Andric return false; 309480093f4SDimitry Andric 310480093f4SDimitry Andric MergeFunctions MF; 311480093f4SDimitry Andric return MF.runOnModule(M); 312480093f4SDimitry Andric } 313480093f4SDimitry Andric }; 314480093f4SDimitry Andric 315*0b57cec5SDimitry Andric } // end anonymous namespace 316*0b57cec5SDimitry Andric 317480093f4SDimitry Andric char MergeFunctionsLegacyPass::ID = 0; 318480093f4SDimitry Andric INITIALIZE_PASS(MergeFunctionsLegacyPass, "mergefunc", 319480093f4SDimitry Andric "Merge Functions", false, false) 320*0b57cec5SDimitry Andric 321*0b57cec5SDimitry Andric ModulePass *llvm::createMergeFunctionsPass() { 322480093f4SDimitry Andric return new MergeFunctionsLegacyPass(); 323480093f4SDimitry Andric } 324480093f4SDimitry Andric 325480093f4SDimitry Andric PreservedAnalyses MergeFunctionsPass::run(Module &M, 326480093f4SDimitry Andric ModuleAnalysisManager &AM) { 327480093f4SDimitry Andric MergeFunctions MF; 328480093f4SDimitry Andric if (!MF.runOnModule(M)) 329480093f4SDimitry Andric return PreservedAnalyses::all(); 330480093f4SDimitry Andric return PreservedAnalyses::none(); 331*0b57cec5SDimitry Andric } 332*0b57cec5SDimitry Andric 333*0b57cec5SDimitry Andric #ifndef NDEBUG 33481ad6265SDimitry Andric bool MergeFunctions::doFunctionalCheck(std::vector<WeakTrackingVH> &Worklist) { 33581ad6265SDimitry Andric if (const unsigned Max = NumFunctionsForVerificationCheck) { 336*0b57cec5SDimitry Andric unsigned TripleNumber = 0; 337*0b57cec5SDimitry Andric bool Valid = true; 338*0b57cec5SDimitry Andric 33981ad6265SDimitry Andric dbgs() << "MERGEFUNC-VERIFY: Started for first " << Max << " functions.\n"; 340*0b57cec5SDimitry Andric 341*0b57cec5SDimitry Andric unsigned i = 0; 342*0b57cec5SDimitry Andric for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(), 343*0b57cec5SDimitry Andric E = Worklist.end(); 344*0b57cec5SDimitry Andric I != E && i < Max; ++I, ++i) { 345*0b57cec5SDimitry Andric unsigned j = i; 346*0b57cec5SDimitry Andric for (std::vector<WeakTrackingVH>::iterator J = I; J != E && j < Max; 347*0b57cec5SDimitry Andric ++J, ++j) { 348*0b57cec5SDimitry Andric Function *F1 = cast<Function>(*I); 349*0b57cec5SDimitry Andric Function *F2 = cast<Function>(*J); 350*0b57cec5SDimitry Andric int Res1 = FunctionComparator(F1, F2, &GlobalNumbers).compare(); 351*0b57cec5SDimitry Andric int Res2 = FunctionComparator(F2, F1, &GlobalNumbers).compare(); 352*0b57cec5SDimitry Andric 353*0b57cec5SDimitry Andric // If F1 <= F2, then F2 >= F1, otherwise report failure. 354*0b57cec5SDimitry Andric if (Res1 != -Res2) { 35581ad6265SDimitry Andric dbgs() << "MERGEFUNC-VERIFY: Non-symmetric; triple: " << TripleNumber 356*0b57cec5SDimitry Andric << "\n"; 357*0b57cec5SDimitry Andric dbgs() << *F1 << '\n' << *F2 << '\n'; 358*0b57cec5SDimitry Andric Valid = false; 359*0b57cec5SDimitry Andric } 360*0b57cec5SDimitry Andric 361*0b57cec5SDimitry Andric if (Res1 == 0) 362*0b57cec5SDimitry Andric continue; 363*0b57cec5SDimitry Andric 364*0b57cec5SDimitry Andric unsigned k = j; 365*0b57cec5SDimitry Andric for (std::vector<WeakTrackingVH>::iterator K = J; K != E && k < Max; 366*0b57cec5SDimitry Andric ++k, ++K, ++TripleNumber) { 367*0b57cec5SDimitry Andric if (K == J) 368*0b57cec5SDimitry Andric continue; 369*0b57cec5SDimitry Andric 370*0b57cec5SDimitry Andric Function *F3 = cast<Function>(*K); 371*0b57cec5SDimitry Andric int Res3 = FunctionComparator(F1, F3, &GlobalNumbers).compare(); 372*0b57cec5SDimitry Andric int Res4 = FunctionComparator(F2, F3, &GlobalNumbers).compare(); 373*0b57cec5SDimitry Andric 374*0b57cec5SDimitry Andric bool Transitive = true; 375*0b57cec5SDimitry Andric 376*0b57cec5SDimitry Andric if (Res1 != 0 && Res1 == Res4) { 377*0b57cec5SDimitry Andric // F1 > F2, F2 > F3 => F1 > F3 378*0b57cec5SDimitry Andric Transitive = Res3 == Res1; 379*0b57cec5SDimitry Andric } else if (Res3 != 0 && Res3 == -Res4) { 380*0b57cec5SDimitry Andric // F1 > F3, F3 > F2 => F1 > F2 381*0b57cec5SDimitry Andric Transitive = Res3 == Res1; 382*0b57cec5SDimitry Andric } else if (Res4 != 0 && -Res3 == Res4) { 383*0b57cec5SDimitry Andric // F2 > F3, F3 > F1 => F2 > F1 384*0b57cec5SDimitry Andric Transitive = Res4 == -Res1; 385*0b57cec5SDimitry Andric } 386*0b57cec5SDimitry Andric 387*0b57cec5SDimitry Andric if (!Transitive) { 38881ad6265SDimitry Andric dbgs() << "MERGEFUNC-VERIFY: Non-transitive; triple: " 389*0b57cec5SDimitry Andric << TripleNumber << "\n"; 390*0b57cec5SDimitry Andric dbgs() << "Res1, Res3, Res4: " << Res1 << ", " << Res3 << ", " 391*0b57cec5SDimitry Andric << Res4 << "\n"; 392*0b57cec5SDimitry Andric dbgs() << *F1 << '\n' << *F2 << '\n' << *F3 << '\n'; 393*0b57cec5SDimitry Andric Valid = false; 394*0b57cec5SDimitry Andric } 395*0b57cec5SDimitry Andric } 396*0b57cec5SDimitry Andric } 397*0b57cec5SDimitry Andric } 398*0b57cec5SDimitry Andric 39981ad6265SDimitry Andric dbgs() << "MERGEFUNC-VERIFY: " << (Valid ? "Passed." : "Failed.") << "\n"; 400*0b57cec5SDimitry Andric return Valid; 401*0b57cec5SDimitry Andric } 402*0b57cec5SDimitry Andric return true; 403*0b57cec5SDimitry Andric } 404*0b57cec5SDimitry Andric #endif 405*0b57cec5SDimitry Andric 406*0b57cec5SDimitry Andric /// Check whether \p F is eligible for function merging. 407*0b57cec5SDimitry Andric static bool isEligibleForMerging(Function &F) { 408*0b57cec5SDimitry Andric return !F.isDeclaration() && !F.hasAvailableExternallyLinkage(); 409*0b57cec5SDimitry Andric } 410*0b57cec5SDimitry Andric 411*0b57cec5SDimitry Andric bool MergeFunctions::runOnModule(Module &M) { 412*0b57cec5SDimitry Andric bool Changed = false; 413*0b57cec5SDimitry Andric 41481ad6265SDimitry Andric SmallVector<GlobalValue *, 4> UsedV; 41581ad6265SDimitry Andric collectUsedGlobalVariables(M, UsedV, /*CompilerUsed=*/false); 41681ad6265SDimitry Andric collectUsedGlobalVariables(M, UsedV, /*CompilerUsed=*/true); 41781ad6265SDimitry Andric Used.insert(UsedV.begin(), UsedV.end()); 41881ad6265SDimitry Andric 419*0b57cec5SDimitry Andric // All functions in the module, ordered by hash. Functions with a unique 420*0b57cec5SDimitry Andric // hash value are easily eliminated. 421*0b57cec5SDimitry Andric std::vector<std::pair<FunctionComparator::FunctionHash, Function *>> 422*0b57cec5SDimitry Andric HashedFuncs; 423*0b57cec5SDimitry Andric for (Function &Func : M) { 424*0b57cec5SDimitry Andric if (isEligibleForMerging(Func)) { 425*0b57cec5SDimitry Andric HashedFuncs.push_back({FunctionComparator::functionHash(Func), &Func}); 426*0b57cec5SDimitry Andric } 427*0b57cec5SDimitry Andric } 428*0b57cec5SDimitry Andric 429*0b57cec5SDimitry Andric llvm::stable_sort(HashedFuncs, less_first()); 430*0b57cec5SDimitry Andric 431*0b57cec5SDimitry Andric auto S = HashedFuncs.begin(); 432*0b57cec5SDimitry Andric for (auto I = HashedFuncs.begin(), IE = HashedFuncs.end(); I != IE; ++I) { 433*0b57cec5SDimitry Andric // If the hash value matches the previous value or the next one, we must 434*0b57cec5SDimitry Andric // consider merging it. Otherwise it is dropped and never considered again. 435*0b57cec5SDimitry Andric if ((I != S && std::prev(I)->first == I->first) || 436*0b57cec5SDimitry Andric (std::next(I) != IE && std::next(I)->first == I->first) ) { 437*0b57cec5SDimitry Andric Deferred.push_back(WeakTrackingVH(I->second)); 438*0b57cec5SDimitry Andric } 439*0b57cec5SDimitry Andric } 440*0b57cec5SDimitry Andric 441*0b57cec5SDimitry Andric do { 442*0b57cec5SDimitry Andric std::vector<WeakTrackingVH> Worklist; 443*0b57cec5SDimitry Andric Deferred.swap(Worklist); 444*0b57cec5SDimitry Andric 44581ad6265SDimitry Andric LLVM_DEBUG(doFunctionalCheck(Worklist)); 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "size of module: " << M.size() << '\n'); 448*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n'); 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric // Insert functions and merge them. 451*0b57cec5SDimitry Andric for (WeakTrackingVH &I : Worklist) { 452*0b57cec5SDimitry Andric if (!I) 453*0b57cec5SDimitry Andric continue; 454*0b57cec5SDimitry Andric Function *F = cast<Function>(I); 455*0b57cec5SDimitry Andric if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage()) { 456*0b57cec5SDimitry Andric Changed |= insert(F); 457*0b57cec5SDimitry Andric } 458*0b57cec5SDimitry Andric } 459*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "size of FnTree: " << FnTree.size() << '\n'); 460*0b57cec5SDimitry Andric } while (!Deferred.empty()); 461*0b57cec5SDimitry Andric 462*0b57cec5SDimitry Andric FnTree.clear(); 463*0b57cec5SDimitry Andric FNodesInTree.clear(); 464*0b57cec5SDimitry Andric GlobalNumbers.clear(); 46581ad6265SDimitry Andric Used.clear(); 466*0b57cec5SDimitry Andric 467*0b57cec5SDimitry Andric return Changed; 468*0b57cec5SDimitry Andric } 469*0b57cec5SDimitry Andric 470*0b57cec5SDimitry Andric // Replace direct callers of Old with New. 471*0b57cec5SDimitry Andric void MergeFunctions::replaceDirectCallers(Function *Old, Function *New) { 472*0b57cec5SDimitry Andric Constant *BitcastNew = ConstantExpr::getBitCast(New, Old->getType()); 473349cc55cSDimitry Andric for (Use &U : llvm::make_early_inc_range(Old->uses())) { 474349cc55cSDimitry Andric CallBase *CB = dyn_cast<CallBase>(U.getUser()); 475349cc55cSDimitry Andric if (CB && CB->isCallee(&U)) { 476480093f4SDimitry Andric // Do not copy attributes from the called function to the call-site. 477480093f4SDimitry Andric // Function comparison ensures that the attributes are the same up to 478480093f4SDimitry Andric // type congruences in byval(), in which case we need to keep the byval 479480093f4SDimitry Andric // type of the call-site, not the callee function. 4805ffd83dbSDimitry Andric remove(CB->getFunction()); 481349cc55cSDimitry Andric U.set(BitcastNew); 482*0b57cec5SDimitry Andric } 483*0b57cec5SDimitry Andric } 484*0b57cec5SDimitry Andric } 485*0b57cec5SDimitry Andric 486*0b57cec5SDimitry Andric // Helper for writeThunk, 487*0b57cec5SDimitry Andric // Selects proper bitcast operation, 488*0b57cec5SDimitry Andric // but a bit simpler then CastInst::getCastOpcode. 489*0b57cec5SDimitry Andric static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) { 490*0b57cec5SDimitry Andric Type *SrcTy = V->getType(); 491*0b57cec5SDimitry Andric if (SrcTy->isStructTy()) { 492*0b57cec5SDimitry Andric assert(DestTy->isStructTy()); 493*0b57cec5SDimitry Andric assert(SrcTy->getStructNumElements() == DestTy->getStructNumElements()); 49481ad6265SDimitry Andric Value *Result = PoisonValue::get(DestTy); 495*0b57cec5SDimitry Andric for (unsigned int I = 0, E = SrcTy->getStructNumElements(); I < E; ++I) { 496bdd1243dSDimitry Andric Value *Element = 497bdd1243dSDimitry Andric createCast(Builder, Builder.CreateExtractValue(V, ArrayRef(I)), 498*0b57cec5SDimitry Andric DestTy->getStructElementType(I)); 499*0b57cec5SDimitry Andric 500bdd1243dSDimitry Andric Result = Builder.CreateInsertValue(Result, Element, ArrayRef(I)); 501*0b57cec5SDimitry Andric } 502*0b57cec5SDimitry Andric return Result; 503*0b57cec5SDimitry Andric } 504*0b57cec5SDimitry Andric assert(!DestTy->isStructTy()); 505*0b57cec5SDimitry Andric if (SrcTy->isIntegerTy() && DestTy->isPointerTy()) 506*0b57cec5SDimitry Andric return Builder.CreateIntToPtr(V, DestTy); 507*0b57cec5SDimitry Andric else if (SrcTy->isPointerTy() && DestTy->isIntegerTy()) 508*0b57cec5SDimitry Andric return Builder.CreatePtrToInt(V, DestTy); 509*0b57cec5SDimitry Andric else 510*0b57cec5SDimitry Andric return Builder.CreateBitCast(V, DestTy); 511*0b57cec5SDimitry Andric } 512*0b57cec5SDimitry Andric 513*0b57cec5SDimitry Andric // Erase the instructions in PDIUnrelatedWL as they are unrelated to the 514*0b57cec5SDimitry Andric // parameter debug info, from the entry block. 515*0b57cec5SDimitry Andric void MergeFunctions::eraseInstsUnrelatedToPDI( 516*0b57cec5SDimitry Andric std::vector<Instruction *> &PDIUnrelatedWL) { 517*0b57cec5SDimitry Andric LLVM_DEBUG( 518*0b57cec5SDimitry Andric dbgs() << " Erasing instructions (in reverse order of appearance in " 519*0b57cec5SDimitry Andric "entry block) unrelated to parameter debug info from entry " 520*0b57cec5SDimitry Andric "block: {\n"); 521*0b57cec5SDimitry Andric while (!PDIUnrelatedWL.empty()) { 522*0b57cec5SDimitry Andric Instruction *I = PDIUnrelatedWL.back(); 523*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Deleting Instruction: "); 524*0b57cec5SDimitry Andric LLVM_DEBUG(I->print(dbgs())); 525*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 526*0b57cec5SDimitry Andric I->eraseFromParent(); 527*0b57cec5SDimitry Andric PDIUnrelatedWL.pop_back(); 528*0b57cec5SDimitry Andric } 529*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " } // Done erasing instructions unrelated to parameter " 530*0b57cec5SDimitry Andric "debug info from entry block. \n"); 531*0b57cec5SDimitry Andric } 532*0b57cec5SDimitry Andric 533*0b57cec5SDimitry Andric // Reduce G to its entry block. 534*0b57cec5SDimitry Andric void MergeFunctions::eraseTail(Function *G) { 535*0b57cec5SDimitry Andric std::vector<BasicBlock *> WorklistBB; 536fe6060f1SDimitry Andric for (BasicBlock &BB : drop_begin(*G)) { 537fe6060f1SDimitry Andric BB.dropAllReferences(); 538fe6060f1SDimitry Andric WorklistBB.push_back(&BB); 539*0b57cec5SDimitry Andric } 540*0b57cec5SDimitry Andric while (!WorklistBB.empty()) { 541*0b57cec5SDimitry Andric BasicBlock *BB = WorklistBB.back(); 542*0b57cec5SDimitry Andric BB->eraseFromParent(); 543*0b57cec5SDimitry Andric WorklistBB.pop_back(); 544*0b57cec5SDimitry Andric } 545*0b57cec5SDimitry Andric } 546*0b57cec5SDimitry Andric 547*0b57cec5SDimitry Andric // We are interested in the following instructions from the entry block as being 548*0b57cec5SDimitry Andric // related to parameter debug info: 549*0b57cec5SDimitry Andric // - @llvm.dbg.declare 550*0b57cec5SDimitry Andric // - stores from the incoming parameters to locations on the stack-frame 551*0b57cec5SDimitry Andric // - allocas that create these locations on the stack-frame 552*0b57cec5SDimitry Andric // - @llvm.dbg.value 553*0b57cec5SDimitry Andric // - the entry block's terminator 554*0b57cec5SDimitry Andric // The rest are unrelated to debug info for the parameters; fill up 555*0b57cec5SDimitry Andric // PDIUnrelatedWL with such instructions. 556*0b57cec5SDimitry Andric void MergeFunctions::filterInstsUnrelatedToPDI( 557*0b57cec5SDimitry Andric BasicBlock *GEntryBlock, std::vector<Instruction *> &PDIUnrelatedWL) { 558*0b57cec5SDimitry Andric std::set<Instruction *> PDIRelated; 559*0b57cec5SDimitry Andric for (BasicBlock::iterator BI = GEntryBlock->begin(), BIE = GEntryBlock->end(); 560*0b57cec5SDimitry Andric BI != BIE; ++BI) { 561*0b57cec5SDimitry Andric if (auto *DVI = dyn_cast<DbgValueInst>(&*BI)) { 562*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Deciding: "); 563*0b57cec5SDimitry Andric LLVM_DEBUG(BI->print(dbgs())); 564*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 565*0b57cec5SDimitry Andric DILocalVariable *DILocVar = DVI->getVariable(); 566*0b57cec5SDimitry Andric if (DILocVar->isParameter()) { 567*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Include (parameter): "); 568*0b57cec5SDimitry Andric LLVM_DEBUG(BI->print(dbgs())); 569*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 570*0b57cec5SDimitry Andric PDIRelated.insert(&*BI); 571*0b57cec5SDimitry Andric } else { 572*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Delete (!parameter): "); 573*0b57cec5SDimitry Andric LLVM_DEBUG(BI->print(dbgs())); 574*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 575*0b57cec5SDimitry Andric } 576*0b57cec5SDimitry Andric } else if (auto *DDI = dyn_cast<DbgDeclareInst>(&*BI)) { 577*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Deciding: "); 578*0b57cec5SDimitry Andric LLVM_DEBUG(BI->print(dbgs())); 579*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 580*0b57cec5SDimitry Andric DILocalVariable *DILocVar = DDI->getVariable(); 581*0b57cec5SDimitry Andric if (DILocVar->isParameter()) { 582*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Parameter: "); 583*0b57cec5SDimitry Andric LLVM_DEBUG(DILocVar->print(dbgs())); 584*0b57cec5SDimitry Andric AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress()); 585*0b57cec5SDimitry Andric if (AI) { 586*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Processing alloca users: "); 587*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 588*0b57cec5SDimitry Andric for (User *U : AI->users()) { 589*0b57cec5SDimitry Andric if (StoreInst *SI = dyn_cast<StoreInst>(U)) { 590*0b57cec5SDimitry Andric if (Value *Arg = SI->getValueOperand()) { 591fe6060f1SDimitry Andric if (isa<Argument>(Arg)) { 592*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Include: "); 593*0b57cec5SDimitry Andric LLVM_DEBUG(AI->print(dbgs())); 594*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 595*0b57cec5SDimitry Andric PDIRelated.insert(AI); 596*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Include (parameter): "); 597*0b57cec5SDimitry Andric LLVM_DEBUG(SI->print(dbgs())); 598*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 599*0b57cec5SDimitry Andric PDIRelated.insert(SI); 600*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Include: "); 601*0b57cec5SDimitry Andric LLVM_DEBUG(BI->print(dbgs())); 602*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 603*0b57cec5SDimitry Andric PDIRelated.insert(&*BI); 604*0b57cec5SDimitry Andric } else { 605*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Delete (!parameter): "); 606*0b57cec5SDimitry Andric LLVM_DEBUG(SI->print(dbgs())); 607*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 608*0b57cec5SDimitry Andric } 609*0b57cec5SDimitry Andric } 610*0b57cec5SDimitry Andric } else { 611*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Defer: "); 612*0b57cec5SDimitry Andric LLVM_DEBUG(U->print(dbgs())); 613*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 614*0b57cec5SDimitry Andric } 615*0b57cec5SDimitry Andric } 616*0b57cec5SDimitry Andric } else { 617*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Delete (alloca NULL): "); 618*0b57cec5SDimitry Andric LLVM_DEBUG(BI->print(dbgs())); 619*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 620*0b57cec5SDimitry Andric } 621*0b57cec5SDimitry Andric } else { 622*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Delete (!parameter): "); 623*0b57cec5SDimitry Andric LLVM_DEBUG(BI->print(dbgs())); 624*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 625*0b57cec5SDimitry Andric } 626*0b57cec5SDimitry Andric } else if (BI->isTerminator() && &*BI == GEntryBlock->getTerminator()) { 627*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Will Include Terminator: "); 628*0b57cec5SDimitry Andric LLVM_DEBUG(BI->print(dbgs())); 629*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 630*0b57cec5SDimitry Andric PDIRelated.insert(&*BI); 631*0b57cec5SDimitry Andric } else { 632*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Defer: "); 633*0b57cec5SDimitry Andric LLVM_DEBUG(BI->print(dbgs())); 634*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 635*0b57cec5SDimitry Andric } 636*0b57cec5SDimitry Andric } 637*0b57cec5SDimitry Andric LLVM_DEBUG( 638*0b57cec5SDimitry Andric dbgs() 639*0b57cec5SDimitry Andric << " Report parameter debug info related/related instructions: {\n"); 640fe6060f1SDimitry Andric for (Instruction &I : *GEntryBlock) { 641fe6060f1SDimitry Andric if (PDIRelated.find(&I) == PDIRelated.end()) { 642*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " !PDIRelated: "); 643fe6060f1SDimitry Andric LLVM_DEBUG(I.print(dbgs())); 644*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 645fe6060f1SDimitry Andric PDIUnrelatedWL.push_back(&I); 646*0b57cec5SDimitry Andric } else { 647*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " PDIRelated: "); 648fe6060f1SDimitry Andric LLVM_DEBUG(I.print(dbgs())); 649*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 650*0b57cec5SDimitry Andric } 651*0b57cec5SDimitry Andric } 652*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " }\n"); 653*0b57cec5SDimitry Andric } 654*0b57cec5SDimitry Andric 655*0b57cec5SDimitry Andric /// Whether this function may be replaced by a forwarding thunk. 656*0b57cec5SDimitry Andric static bool canCreateThunkFor(Function *F) { 657*0b57cec5SDimitry Andric if (F->isVarArg()) 658*0b57cec5SDimitry Andric return false; 659*0b57cec5SDimitry Andric 660*0b57cec5SDimitry Andric // Don't merge tiny functions using a thunk, since it can just end up 661*0b57cec5SDimitry Andric // making the function larger. 662*0b57cec5SDimitry Andric if (F->size() == 1) { 663*0b57cec5SDimitry Andric if (F->front().size() <= 2) { 664*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "canCreateThunkFor: " << F->getName() 665*0b57cec5SDimitry Andric << " is too small to bother creating a thunk for\n"); 666*0b57cec5SDimitry Andric return false; 667*0b57cec5SDimitry Andric } 668*0b57cec5SDimitry Andric } 669*0b57cec5SDimitry Andric return true; 670*0b57cec5SDimitry Andric } 671*0b57cec5SDimitry Andric 672*0b57cec5SDimitry Andric // Replace G with a simple tail call to bitcast(F). Also (unless 673*0b57cec5SDimitry Andric // MergeFunctionsPDI holds) replace direct uses of G with bitcast(F), 674*0b57cec5SDimitry Andric // delete G. Under MergeFunctionsPDI, we use G itself for creating 675*0b57cec5SDimitry Andric // the thunk as we preserve the debug info (and associated instructions) 676*0b57cec5SDimitry Andric // from G's entry block pertaining to G's incoming arguments which are 677*0b57cec5SDimitry Andric // passed on as corresponding arguments in the call that G makes to F. 678*0b57cec5SDimitry Andric // For better debugability, under MergeFunctionsPDI, we do not modify G's 679*0b57cec5SDimitry Andric // call sites to point to F even when within the same translation unit. 680*0b57cec5SDimitry Andric void MergeFunctions::writeThunk(Function *F, Function *G) { 681*0b57cec5SDimitry Andric BasicBlock *GEntryBlock = nullptr; 682*0b57cec5SDimitry Andric std::vector<Instruction *> PDIUnrelatedWL; 683*0b57cec5SDimitry Andric BasicBlock *BB = nullptr; 684*0b57cec5SDimitry Andric Function *NewG = nullptr; 685*0b57cec5SDimitry Andric if (MergeFunctionsPDI) { 686*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "writeThunk: (MergeFunctionsPDI) Do not create a new " 687*0b57cec5SDimitry Andric "function as thunk; retain original: " 688*0b57cec5SDimitry Andric << G->getName() << "()\n"); 689*0b57cec5SDimitry Andric GEntryBlock = &G->getEntryBlock(); 690*0b57cec5SDimitry Andric LLVM_DEBUG( 691*0b57cec5SDimitry Andric dbgs() << "writeThunk: (MergeFunctionsPDI) filter parameter related " 692*0b57cec5SDimitry Andric "debug info for " 693*0b57cec5SDimitry Andric << G->getName() << "() {\n"); 694*0b57cec5SDimitry Andric filterInstsUnrelatedToPDI(GEntryBlock, PDIUnrelatedWL); 695*0b57cec5SDimitry Andric GEntryBlock->getTerminator()->eraseFromParent(); 696*0b57cec5SDimitry Andric BB = GEntryBlock; 697*0b57cec5SDimitry Andric } else { 698*0b57cec5SDimitry Andric NewG = Function::Create(G->getFunctionType(), G->getLinkage(), 699*0b57cec5SDimitry Andric G->getAddressSpace(), "", G->getParent()); 700*0b57cec5SDimitry Andric NewG->setComdat(G->getComdat()); 701*0b57cec5SDimitry Andric BB = BasicBlock::Create(F->getContext(), "", NewG); 702*0b57cec5SDimitry Andric } 703*0b57cec5SDimitry Andric 704*0b57cec5SDimitry Andric IRBuilder<> Builder(BB); 705*0b57cec5SDimitry Andric Function *H = MergeFunctionsPDI ? G : NewG; 706*0b57cec5SDimitry Andric SmallVector<Value *, 16> Args; 707*0b57cec5SDimitry Andric unsigned i = 0; 708*0b57cec5SDimitry Andric FunctionType *FFTy = F->getFunctionType(); 709*0b57cec5SDimitry Andric for (Argument &AI : H->args()) { 710*0b57cec5SDimitry Andric Args.push_back(createCast(Builder, &AI, FFTy->getParamType(i))); 711*0b57cec5SDimitry Andric ++i; 712*0b57cec5SDimitry Andric } 713*0b57cec5SDimitry Andric 714*0b57cec5SDimitry Andric CallInst *CI = Builder.CreateCall(F, Args); 715*0b57cec5SDimitry Andric ReturnInst *RI = nullptr; 716fe6060f1SDimitry Andric bool isSwiftTailCall = F->getCallingConv() == CallingConv::SwiftTail && 717fe6060f1SDimitry Andric G->getCallingConv() == CallingConv::SwiftTail; 718fe6060f1SDimitry Andric CI->setTailCallKind(isSwiftTailCall ? llvm::CallInst::TCK_MustTail 719fe6060f1SDimitry Andric : llvm::CallInst::TCK_Tail); 720*0b57cec5SDimitry Andric CI->setCallingConv(F->getCallingConv()); 721*0b57cec5SDimitry Andric CI->setAttributes(F->getAttributes()); 722*0b57cec5SDimitry Andric if (H->getReturnType()->isVoidTy()) { 723*0b57cec5SDimitry Andric RI = Builder.CreateRetVoid(); 724*0b57cec5SDimitry Andric } else { 725*0b57cec5SDimitry Andric RI = Builder.CreateRet(createCast(Builder, CI, H->getReturnType())); 726*0b57cec5SDimitry Andric } 727*0b57cec5SDimitry Andric 728*0b57cec5SDimitry Andric if (MergeFunctionsPDI) { 729*0b57cec5SDimitry Andric DISubprogram *DIS = G->getSubprogram(); 730*0b57cec5SDimitry Andric if (DIS) { 731e8d8bef9SDimitry Andric DebugLoc CIDbgLoc = 732e8d8bef9SDimitry Andric DILocation::get(DIS->getContext(), DIS->getScopeLine(), 0, DIS); 733e8d8bef9SDimitry Andric DebugLoc RIDbgLoc = 734e8d8bef9SDimitry Andric DILocation::get(DIS->getContext(), DIS->getScopeLine(), 0, DIS); 735*0b57cec5SDimitry Andric CI->setDebugLoc(CIDbgLoc); 736*0b57cec5SDimitry Andric RI->setDebugLoc(RIDbgLoc); 737*0b57cec5SDimitry Andric } else { 738*0b57cec5SDimitry Andric LLVM_DEBUG( 739*0b57cec5SDimitry Andric dbgs() << "writeThunk: (MergeFunctionsPDI) No DISubprogram for " 740*0b57cec5SDimitry Andric << G->getName() << "()\n"); 741*0b57cec5SDimitry Andric } 742*0b57cec5SDimitry Andric eraseTail(G); 743*0b57cec5SDimitry Andric eraseInstsUnrelatedToPDI(PDIUnrelatedWL); 744*0b57cec5SDimitry Andric LLVM_DEBUG( 745*0b57cec5SDimitry Andric dbgs() << "} // End of parameter related debug info filtering for: " 746*0b57cec5SDimitry Andric << G->getName() << "()\n"); 747*0b57cec5SDimitry Andric } else { 748*0b57cec5SDimitry Andric NewG->copyAttributesFrom(G); 749*0b57cec5SDimitry Andric NewG->takeName(G); 750*0b57cec5SDimitry Andric removeUsers(G); 751*0b57cec5SDimitry Andric G->replaceAllUsesWith(NewG); 752*0b57cec5SDimitry Andric G->eraseFromParent(); 753*0b57cec5SDimitry Andric } 754*0b57cec5SDimitry Andric 755*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "writeThunk: " << H->getName() << '\n'); 756*0b57cec5SDimitry Andric ++NumThunksWritten; 757*0b57cec5SDimitry Andric } 758*0b57cec5SDimitry Andric 759*0b57cec5SDimitry Andric // Whether this function may be replaced by an alias 760*0b57cec5SDimitry Andric static bool canCreateAliasFor(Function *F) { 761*0b57cec5SDimitry Andric if (!MergeFunctionsAliases || !F->hasGlobalUnnamedAddr()) 762*0b57cec5SDimitry Andric return false; 763*0b57cec5SDimitry Andric 764*0b57cec5SDimitry Andric // We should only see linkages supported by aliases here 765*0b57cec5SDimitry Andric assert(F->hasLocalLinkage() || F->hasExternalLinkage() 766*0b57cec5SDimitry Andric || F->hasWeakLinkage() || F->hasLinkOnceLinkage()); 767*0b57cec5SDimitry Andric return true; 768*0b57cec5SDimitry Andric } 769*0b57cec5SDimitry Andric 770*0b57cec5SDimitry Andric // Replace G with an alias to F (deleting function G) 771*0b57cec5SDimitry Andric void MergeFunctions::writeAlias(Function *F, Function *G) { 772*0b57cec5SDimitry Andric Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); 773*0b57cec5SDimitry Andric PointerType *PtrType = G->getType(); 774fe6060f1SDimitry Andric auto *GA = GlobalAlias::create(G->getValueType(), PtrType->getAddressSpace(), 775*0b57cec5SDimitry Andric G->getLinkage(), "", BitcastF, G->getParent()); 776*0b57cec5SDimitry Andric 777bdd1243dSDimitry Andric const MaybeAlign FAlign = F->getAlign(); 778bdd1243dSDimitry Andric const MaybeAlign GAlign = G->getAlign(); 779bdd1243dSDimitry Andric if (FAlign || GAlign) 780bdd1243dSDimitry Andric F->setAlignment(std::max(FAlign.valueOrOne(), GAlign.valueOrOne())); 781bdd1243dSDimitry Andric else 782bdd1243dSDimitry Andric F->setAlignment(std::nullopt); 783*0b57cec5SDimitry Andric GA->takeName(G); 784*0b57cec5SDimitry Andric GA->setVisibility(G->getVisibility()); 785*0b57cec5SDimitry Andric GA->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); 786*0b57cec5SDimitry Andric 787*0b57cec5SDimitry Andric removeUsers(G); 788*0b57cec5SDimitry Andric G->replaceAllUsesWith(GA); 789*0b57cec5SDimitry Andric G->eraseFromParent(); 790*0b57cec5SDimitry Andric 791*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "writeAlias: " << GA->getName() << '\n'); 792*0b57cec5SDimitry Andric ++NumAliasesWritten; 793*0b57cec5SDimitry Andric } 794*0b57cec5SDimitry Andric 795*0b57cec5SDimitry Andric // Replace G with an alias to F if possible, or a thunk to F if 796*0b57cec5SDimitry Andric // profitable. Returns false if neither is the case. 797*0b57cec5SDimitry Andric bool MergeFunctions::writeThunkOrAlias(Function *F, Function *G) { 798*0b57cec5SDimitry Andric if (canCreateAliasFor(G)) { 799*0b57cec5SDimitry Andric writeAlias(F, G); 800*0b57cec5SDimitry Andric return true; 801*0b57cec5SDimitry Andric } 802*0b57cec5SDimitry Andric if (canCreateThunkFor(F)) { 803*0b57cec5SDimitry Andric writeThunk(F, G); 804*0b57cec5SDimitry Andric return true; 805*0b57cec5SDimitry Andric } 806*0b57cec5SDimitry Andric return false; 807*0b57cec5SDimitry Andric } 808*0b57cec5SDimitry Andric 809*0b57cec5SDimitry Andric // Merge two equivalent functions. Upon completion, Function G is deleted. 810*0b57cec5SDimitry Andric void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) { 811*0b57cec5SDimitry Andric if (F->isInterposable()) { 812*0b57cec5SDimitry Andric assert(G->isInterposable()); 813*0b57cec5SDimitry Andric 814*0b57cec5SDimitry Andric // Both writeThunkOrAlias() calls below must succeed, either because we can 815*0b57cec5SDimitry Andric // create aliases for G and NewF, or because a thunk for F is profitable. 816*0b57cec5SDimitry Andric // F here has the same signature as NewF below, so that's what we check. 817*0b57cec5SDimitry Andric if (!canCreateThunkFor(F) && 818*0b57cec5SDimitry Andric (!canCreateAliasFor(F) || !canCreateAliasFor(G))) 819*0b57cec5SDimitry Andric return; 820*0b57cec5SDimitry Andric 821*0b57cec5SDimitry Andric // Make them both thunks to the same internal function. 822*0b57cec5SDimitry Andric Function *NewF = Function::Create(F->getFunctionType(), F->getLinkage(), 823*0b57cec5SDimitry Andric F->getAddressSpace(), "", F->getParent()); 824*0b57cec5SDimitry Andric NewF->copyAttributesFrom(F); 825*0b57cec5SDimitry Andric NewF->takeName(F); 826*0b57cec5SDimitry Andric removeUsers(F); 827*0b57cec5SDimitry Andric F->replaceAllUsesWith(NewF); 828*0b57cec5SDimitry Andric 829bdd1243dSDimitry Andric // We collect alignment before writeThunkOrAlias that overwrites NewF and 830bdd1243dSDimitry Andric // G's content. 831bdd1243dSDimitry Andric const MaybeAlign NewFAlign = NewF->getAlign(); 832bdd1243dSDimitry Andric const MaybeAlign GAlign = G->getAlign(); 833*0b57cec5SDimitry Andric 834*0b57cec5SDimitry Andric writeThunkOrAlias(F, G); 835*0b57cec5SDimitry Andric writeThunkOrAlias(F, NewF); 836*0b57cec5SDimitry Andric 837bdd1243dSDimitry Andric if (NewFAlign || GAlign) 838bdd1243dSDimitry Andric F->setAlignment(std::max(NewFAlign.valueOrOne(), GAlign.valueOrOne())); 839bdd1243dSDimitry Andric else 840bdd1243dSDimitry Andric F->setAlignment(std::nullopt); 841*0b57cec5SDimitry Andric F->setLinkage(GlobalValue::PrivateLinkage); 842*0b57cec5SDimitry Andric ++NumDoubleWeak; 843*0b57cec5SDimitry Andric ++NumFunctionsMerged; 844*0b57cec5SDimitry Andric } else { 845*0b57cec5SDimitry Andric // For better debugability, under MergeFunctionsPDI, we do not modify G's 846*0b57cec5SDimitry Andric // call sites to point to F even when within the same translation unit. 847*0b57cec5SDimitry Andric if (!G->isInterposable() && !MergeFunctionsPDI) { 84881ad6265SDimitry Andric // Functions referred to by llvm.used/llvm.compiler.used are special: 84981ad6265SDimitry Andric // there are uses of the symbol name that are not visible to LLVM, 85081ad6265SDimitry Andric // usually from inline asm. 85181ad6265SDimitry Andric if (G->hasGlobalUnnamedAddr() && !Used.contains(G)) { 852*0b57cec5SDimitry Andric // G might have been a key in our GlobalNumberState, and it's illegal 853*0b57cec5SDimitry Andric // to replace a key in ValueMap<GlobalValue *> with a non-global. 854*0b57cec5SDimitry Andric GlobalNumbers.erase(G); 855*0b57cec5SDimitry Andric // If G's address is not significant, replace it entirely. 856*0b57cec5SDimitry Andric Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); 857*0b57cec5SDimitry Andric removeUsers(G); 858*0b57cec5SDimitry Andric G->replaceAllUsesWith(BitcastF); 859*0b57cec5SDimitry Andric } else { 860*0b57cec5SDimitry Andric // Redirect direct callers of G to F. (See note on MergeFunctionsPDI 861*0b57cec5SDimitry Andric // above). 862*0b57cec5SDimitry Andric replaceDirectCallers(G, F); 863*0b57cec5SDimitry Andric } 864*0b57cec5SDimitry Andric } 865*0b57cec5SDimitry Andric 866*0b57cec5SDimitry Andric // If G was internal then we may have replaced all uses of G with F. If so, 867*0b57cec5SDimitry Andric // stop here and delete G. There's no need for a thunk. (See note on 868*0b57cec5SDimitry Andric // MergeFunctionsPDI above). 869*0b57cec5SDimitry Andric if (G->isDiscardableIfUnused() && G->use_empty() && !MergeFunctionsPDI) { 870*0b57cec5SDimitry Andric G->eraseFromParent(); 871*0b57cec5SDimitry Andric ++NumFunctionsMerged; 872*0b57cec5SDimitry Andric return; 873*0b57cec5SDimitry Andric } 874*0b57cec5SDimitry Andric 875*0b57cec5SDimitry Andric if (writeThunkOrAlias(F, G)) { 876*0b57cec5SDimitry Andric ++NumFunctionsMerged; 877*0b57cec5SDimitry Andric } 878*0b57cec5SDimitry Andric } 879*0b57cec5SDimitry Andric } 880*0b57cec5SDimitry Andric 881*0b57cec5SDimitry Andric /// Replace function F by function G. 882*0b57cec5SDimitry Andric void MergeFunctions::replaceFunctionInTree(const FunctionNode &FN, 883*0b57cec5SDimitry Andric Function *G) { 884*0b57cec5SDimitry Andric Function *F = FN.getFunc(); 885*0b57cec5SDimitry Andric assert(FunctionComparator(F, G, &GlobalNumbers).compare() == 0 && 886*0b57cec5SDimitry Andric "The two functions must be equal"); 887*0b57cec5SDimitry Andric 888*0b57cec5SDimitry Andric auto I = FNodesInTree.find(F); 889*0b57cec5SDimitry Andric assert(I != FNodesInTree.end() && "F should be in FNodesInTree"); 890*0b57cec5SDimitry Andric assert(FNodesInTree.count(G) == 0 && "FNodesInTree should not contain G"); 891*0b57cec5SDimitry Andric 892*0b57cec5SDimitry Andric FnTreeType::iterator IterToFNInFnTree = I->second; 893*0b57cec5SDimitry Andric assert(&(*IterToFNInFnTree) == &FN && "F should map to FN in FNodesInTree."); 894*0b57cec5SDimitry Andric // Remove F -> FN and insert G -> FN 895*0b57cec5SDimitry Andric FNodesInTree.erase(I); 896*0b57cec5SDimitry Andric FNodesInTree.insert({G, IterToFNInFnTree}); 897*0b57cec5SDimitry Andric // Replace F with G in FN, which is stored inside the FnTree. 898*0b57cec5SDimitry Andric FN.replaceBy(G); 899*0b57cec5SDimitry Andric } 900*0b57cec5SDimitry Andric 901*0b57cec5SDimitry Andric // Ordering for functions that are equal under FunctionComparator 902*0b57cec5SDimitry Andric static bool isFuncOrderCorrect(const Function *F, const Function *G) { 903*0b57cec5SDimitry Andric if (F->isInterposable() != G->isInterposable()) { 904*0b57cec5SDimitry Andric // Strong before weak, because the weak function may call the strong 905*0b57cec5SDimitry Andric // one, but not the other way around. 906*0b57cec5SDimitry Andric return !F->isInterposable(); 907*0b57cec5SDimitry Andric } 908*0b57cec5SDimitry Andric if (F->hasLocalLinkage() != G->hasLocalLinkage()) { 909*0b57cec5SDimitry Andric // External before local, because we definitely have to keep the external 910*0b57cec5SDimitry Andric // function, but may be able to drop the local one. 911*0b57cec5SDimitry Andric return !F->hasLocalLinkage(); 912*0b57cec5SDimitry Andric } 913*0b57cec5SDimitry Andric // Impose a total order (by name) on the replacement of functions. This is 914*0b57cec5SDimitry Andric // important when operating on more than one module independently to prevent 915*0b57cec5SDimitry Andric // cycles of thunks calling each other when the modules are linked together. 916*0b57cec5SDimitry Andric return F->getName() <= G->getName(); 917*0b57cec5SDimitry Andric } 918*0b57cec5SDimitry Andric 919*0b57cec5SDimitry Andric // Insert a ComparableFunction into the FnTree, or merge it away if equal to one 920*0b57cec5SDimitry Andric // that was already inserted. 921*0b57cec5SDimitry Andric bool MergeFunctions::insert(Function *NewFunction) { 922*0b57cec5SDimitry Andric std::pair<FnTreeType::iterator, bool> Result = 923*0b57cec5SDimitry Andric FnTree.insert(FunctionNode(NewFunction)); 924*0b57cec5SDimitry Andric 925*0b57cec5SDimitry Andric if (Result.second) { 926*0b57cec5SDimitry Andric assert(FNodesInTree.count(NewFunction) == 0); 927*0b57cec5SDimitry Andric FNodesInTree.insert({NewFunction, Result.first}); 928*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Inserting as unique: " << NewFunction->getName() 929*0b57cec5SDimitry Andric << '\n'); 930*0b57cec5SDimitry Andric return false; 931*0b57cec5SDimitry Andric } 932*0b57cec5SDimitry Andric 933*0b57cec5SDimitry Andric const FunctionNode &OldF = *Result.first; 934*0b57cec5SDimitry Andric 935*0b57cec5SDimitry Andric if (!isFuncOrderCorrect(OldF.getFunc(), NewFunction)) { 936*0b57cec5SDimitry Andric // Swap the two functions. 937*0b57cec5SDimitry Andric Function *F = OldF.getFunc(); 938*0b57cec5SDimitry Andric replaceFunctionInTree(*Result.first, NewFunction); 939*0b57cec5SDimitry Andric NewFunction = F; 940*0b57cec5SDimitry Andric assert(OldF.getFunc() != F && "Must have swapped the functions."); 941*0b57cec5SDimitry Andric } 942*0b57cec5SDimitry Andric 943*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " " << OldF.getFunc()->getName() 944*0b57cec5SDimitry Andric << " == " << NewFunction->getName() << '\n'); 945*0b57cec5SDimitry Andric 946*0b57cec5SDimitry Andric Function *DeleteF = NewFunction; 947*0b57cec5SDimitry Andric mergeTwoFunctions(OldF.getFunc(), DeleteF); 948*0b57cec5SDimitry Andric return true; 949*0b57cec5SDimitry Andric } 950*0b57cec5SDimitry Andric 951*0b57cec5SDimitry Andric // Remove a function from FnTree. If it was already in FnTree, add 952*0b57cec5SDimitry Andric // it to Deferred so that we'll look at it in the next round. 953*0b57cec5SDimitry Andric void MergeFunctions::remove(Function *F) { 954*0b57cec5SDimitry Andric auto I = FNodesInTree.find(F); 955*0b57cec5SDimitry Andric if (I != FNodesInTree.end()) { 956*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Deferred " << F->getName() << ".\n"); 957*0b57cec5SDimitry Andric FnTree.erase(I->second); 958*0b57cec5SDimitry Andric // I->second has been invalidated, remove it from the FNodesInTree map to 959*0b57cec5SDimitry Andric // preserve the invariant. 960*0b57cec5SDimitry Andric FNodesInTree.erase(I); 961*0b57cec5SDimitry Andric Deferred.emplace_back(F); 962*0b57cec5SDimitry Andric } 963*0b57cec5SDimitry Andric } 964*0b57cec5SDimitry Andric 965*0b57cec5SDimitry Andric // For each instruction used by the value, remove() the function that contains 966*0b57cec5SDimitry Andric // the instruction. This should happen right before a call to RAUW. 967*0b57cec5SDimitry Andric void MergeFunctions::removeUsers(Value *V) { 968*0b57cec5SDimitry Andric for (User *U : V->users()) 969*0b57cec5SDimitry Andric if (auto *I = dyn_cast<Instruction>(U)) 970*0b57cec5SDimitry Andric remove(I->getFunction()); 971*0b57cec5SDimitry Andric } 972