LCOV - code coverage report
Current view: top level - lib/CodeGen - CodeGenPrepare.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 2426 2533 95.8 %
Date: 2017-09-14 15:23:50 Functions: 137 148 92.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- CodeGenPrepare.cpp - Prepare a function for code generation --------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This pass munges the code in the input function to better prepare it for
      11             : // SelectionDAG-based code generation. This works around limitations in it's
      12             : // basic-block-at-a-time approach. It should eventually be removed.
      13             : //
      14             : //===----------------------------------------------------------------------===//
      15             : 
      16             : #include "llvm/ADT/APInt.h"
      17             : #include "llvm/ADT/ArrayRef.h"
      18             : #include "llvm/ADT/DenseMap.h"
      19             : #include "llvm/ADT/PointerIntPair.h"
      20             : #include "llvm/ADT/STLExtras.h"
      21             : #include "llvm/ADT/SetVector.h"
      22             : #include "llvm/ADT/SmallPtrSet.h"
      23             : #include "llvm/ADT/SmallVector.h"
      24             : #include "llvm/ADT/Statistic.h"
      25             : #include "llvm/Analysis/BlockFrequencyInfo.h"
      26             : #include "llvm/Analysis/BranchProbabilityInfo.h"
      27             : #include "llvm/Analysis/ConstantFolding.h"
      28             : #include "llvm/Analysis/InstructionSimplify.h"
      29             : #include "llvm/Analysis/LoopInfo.h"
      30             : #include "llvm/Analysis/MemoryBuiltins.h"
      31             : #include "llvm/Analysis/ProfileSummaryInfo.h"
      32             : #include "llvm/Analysis/TargetLibraryInfo.h"
      33             : #include "llvm/Analysis/TargetTransformInfo.h"
      34             : #include "llvm/Analysis/ValueTracking.h"
      35             : #include "llvm/CodeGen/Analysis.h"
      36             : #include "llvm/CodeGen/ISDOpcodes.h"
      37             : #include "llvm/CodeGen/MachineValueType.h"
      38             : #include "llvm/CodeGen/SelectionDAGNodes.h"
      39             : #include "llvm/CodeGen/TargetPassConfig.h"
      40             : #include "llvm/CodeGen/ValueTypes.h"
      41             : #include "llvm/IR/Argument.h"
      42             : #include "llvm/IR/Attributes.h"
      43             : #include "llvm/IR/BasicBlock.h"
      44             : #include "llvm/IR/CallSite.h"
      45             : #include "llvm/IR/Constant.h"
      46             : #include "llvm/IR/Constants.h"
      47             : #include "llvm/IR/DataLayout.h"
      48             : #include "llvm/IR/DerivedTypes.h"
      49             : #include "llvm/IR/Dominators.h"
      50             : #include "llvm/IR/Function.h"
      51             : #include "llvm/IR/GetElementPtrTypeIterator.h"
      52             : #include "llvm/IR/GlobalValue.h"
      53             : #include "llvm/IR/GlobalVariable.h"
      54             : #include "llvm/IR/IRBuilder.h"
      55             : #include "llvm/IR/InlineAsm.h"
      56             : #include "llvm/IR/InstrTypes.h"
      57             : #include "llvm/IR/Instruction.h"
      58             : #include "llvm/IR/Instructions.h"
      59             : #include "llvm/IR/IntrinsicInst.h"
      60             : #include "llvm/IR/Intrinsics.h"
      61             : #include "llvm/IR/LLVMContext.h"
      62             : #include "llvm/IR/MDBuilder.h"
      63             : #include "llvm/IR/Module.h"
      64             : #include "llvm/IR/Operator.h"
      65             : #include "llvm/IR/PatternMatch.h"
      66             : #include "llvm/IR/Statepoint.h"
      67             : #include "llvm/IR/Type.h"
      68             : #include "llvm/IR/Use.h"
      69             : #include "llvm/IR/User.h"
      70             : #include "llvm/IR/Value.h"
      71             : #include "llvm/IR/ValueHandle.h"
      72             : #include "llvm/IR/ValueMap.h"
      73             : #include "llvm/Pass.h"
      74             : #include "llvm/Support/BlockFrequency.h"
      75             : #include "llvm/Support/BranchProbability.h"
      76             : #include "llvm/Support/Casting.h"
      77             : #include "llvm/Support/CommandLine.h"
      78             : #include "llvm/Support/Compiler.h"
      79             : #include "llvm/Support/Debug.h"
      80             : #include "llvm/Support/ErrorHandling.h"
      81             : #include "llvm/Support/MathExtras.h"
      82             : #include "llvm/Support/raw_ostream.h"
      83             : #include "llvm/Target/TargetLowering.h"
      84             : #include "llvm/Target/TargetMachine.h"
      85             : #include "llvm/Target/TargetOptions.h"
      86             : #include "llvm/Target/TargetSubtargetInfo.h"
      87             : #include "llvm/Transforms/Utils/BasicBlockUtils.h"
      88             : #include "llvm/Transforms/Utils/BypassSlowDivision.h"
      89             : #include "llvm/Transforms/Utils/Cloning.h"
      90             : #include "llvm/Transforms/Utils/Local.h"
      91             : #include "llvm/Transforms/Utils/SimplifyLibCalls.h"
      92             : #include "llvm/Transforms/Utils/ValueMapper.h"
      93             : #include <algorithm>
      94             : #include <cassert>
      95             : #include <cstdint>
      96             : #include <iterator>
      97             : #include <limits>
      98             : #include <memory>
      99             : #include <utility>
     100             : #include <vector>
     101             : 
     102             : using namespace llvm;
     103             : using namespace llvm::PatternMatch;
     104             : 
     105             : #define DEBUG_TYPE "codegenprepare"
     106             : 
     107             : STATISTIC(NumBlocksElim, "Number of blocks eliminated");
     108             : STATISTIC(NumPHIsElim,   "Number of trivial PHIs eliminated");
     109             : STATISTIC(NumGEPsElim,   "Number of GEPs converted to casts");
     110             : STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of "
     111             :                       "sunken Cmps");
     112             : STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses "
     113             :                        "of sunken Casts");
     114             : STATISTIC(NumMemoryInsts, "Number of memory instructions whose address "
     115             :                           "computations were sunk");
     116             : STATISTIC(NumExtsMoved,  "Number of [s|z]ext instructions combined with loads");
     117             : STATISTIC(NumExtUses,    "Number of uses of [s|z]ext instructions optimized");
     118             : STATISTIC(NumAndsAdded,
     119             :           "Number of and mask instructions added to form ext loads");
     120             : STATISTIC(NumAndUses, "Number of uses of and mask instructions optimized");
     121             : STATISTIC(NumRetsDup,    "Number of return instructions duplicated");
     122             : STATISTIC(NumDbgValueMoved, "Number of debug value instructions moved");
     123             : STATISTIC(NumSelectsExpanded, "Number of selects turned into branches");
     124             : STATISTIC(NumStoreExtractExposed, "Number of store(extractelement) exposed");
     125             : 
     126             : STATISTIC(NumMemCmpCalls, "Number of memcmp calls");
     127             : STATISTIC(NumMemCmpNotConstant, "Number of memcmp calls without constant size");
     128             : STATISTIC(NumMemCmpGreaterThanMax,
     129             :           "Number of memcmp calls with size greater than max size");
     130             : STATISTIC(NumMemCmpInlined, "Number of inlined memcmp calls");
     131             : 
     132       72306 : static cl::opt<bool> DisableBranchOpts(
     133      216918 :   "disable-cgp-branch-opts", cl::Hidden, cl::init(false),
     134      289224 :   cl::desc("Disable branch optimizations in CodeGenPrepare"));
     135             : 
     136             : static cl::opt<bool>
     137      289224 :     DisableGCOpts("disable-cgp-gc-opts", cl::Hidden, cl::init(false),
     138      289224 :                   cl::desc("Disable GC optimizations in CodeGenPrepare"));
     139             : 
     140       72306 : static cl::opt<bool> DisableSelectToBranch(
     141      216918 :   "disable-cgp-select2branch", cl::Hidden, cl::init(false),
     142      289224 :   cl::desc("Disable select to branch conversion."));
     143             : 
     144       72306 : static cl::opt<bool> AddrSinkUsingGEPs(
     145      216918 :   "addr-sink-using-gep", cl::Hidden, cl::init(true),
     146      289224 :   cl::desc("Address sinking in CGP using GEPs."));
     147             : 
     148       72306 : static cl::opt<bool> EnableAndCmpSinking(
     149      216918 :    "enable-andcmp-sinking", cl::Hidden, cl::init(true),
     150      289224 :    cl::desc("Enable sinkinig and/cmp into branches."));
     151             : 
     152       72306 : static cl::opt<bool> DisableStoreExtract(
     153      216918 :     "disable-cgp-store-extract", cl::Hidden, cl::init(false),
     154      289224 :     cl::desc("Disable store(extract) optimizations in CodeGenPrepare"));
     155             : 
     156       72306 : static cl::opt<bool> StressStoreExtract(
     157      216918 :     "stress-cgp-store-extract", cl::Hidden, cl::init(false),
     158      289224 :     cl::desc("Stress test store(extract) optimizations in CodeGenPrepare"));
     159             : 
     160       72306 : static cl::opt<bool> DisableExtLdPromotion(
     161      216918 :     "disable-cgp-ext-ld-promotion", cl::Hidden, cl::init(false),
     162      216918 :     cl::desc("Disable ext(promotable(ld)) -> promoted(ext(ld)) optimization in "
     163       72306 :              "CodeGenPrepare"));
     164             : 
     165       72306 : static cl::opt<bool> StressExtLdPromotion(
     166      216918 :     "stress-cgp-ext-ld-promotion", cl::Hidden, cl::init(false),
     167      216918 :     cl::desc("Stress test ext(promotable(ld)) -> promoted(ext(ld)) "
     168       72306 :              "optimization in CodeGenPrepare"));
     169             : 
     170       72306 : static cl::opt<bool> DisablePreheaderProtect(
     171      216918 :     "disable-preheader-prot", cl::Hidden, cl::init(false),
     172      289224 :     cl::desc("Disable protection against removing loop preheaders"));
     173             : 
     174       72306 : static cl::opt<bool> ProfileGuidedSectionPrefix(
     175      216918 :     "profile-guided-section-prefix", cl::Hidden, cl::init(true), cl::ZeroOrMore,
     176      289224 :     cl::desc("Use profile info to add section prefix for hot/cold functions"));
     177             : 
     178       72306 : static cl::opt<unsigned> FreqRatioToSkipMerge(
     179      216918 :     "cgp-freq-ratio-to-skip-merge", cl::Hidden, cl::init(2),
     180      216918 :     cl::desc("Skip merging empty blocks if (frequency of empty block) / "
     181       72306 :              "(frequency of destination block) is greater than this ratio"));
     182             : 
     183       72306 : static cl::opt<bool> ForceSplitStore(
     184      216918 :     "force-split-store", cl::Hidden, cl::init(false),
     185      289224 :     cl::desc("Force store splitting no matter what the target query says."));
     186             : 
     187             : static cl::opt<bool>
     188       72306 : EnableTypePromotionMerge("cgp-type-promotion-merge", cl::Hidden,
     189      216918 :     cl::desc("Enable merging of redundant sexts when one is dominating"
     190      289224 :     " the other."), cl::init(true));
     191             : 
     192       72306 : static cl::opt<unsigned> MemCmpNumLoadsPerBlock(
     193      216918 :     "memcmp-num-loads-per-block", cl::Hidden, cl::init(1),
     194      216918 :     cl::desc("The number of loads per basic block for inline expansion of "
     195       72306 :              "memcmp that is only being compared against zero."));
     196             : 
     197             : namespace {
     198             : 
     199             : using SetOfInstrs = SmallPtrSet<Instruction *, 16>;
     200             : using TypeIsSExt = PointerIntPair<Type *, 1, bool>;
     201             : using InstrToOrigTy = DenseMap<Instruction *, TypeIsSExt>;
     202             : using SExts = SmallVector<Instruction *, 16>;
     203             : using ValueToSExts = DenseMap<Value *, SExts>;
     204             : 
     205             : class TypePromotionTransaction;
     206             : 
     207      124686 :   class CodeGenPrepare : public FunctionPass {
     208             :     const TargetMachine *TM = nullptr;
     209             :     const TargetSubtargetInfo *SubtargetInfo;
     210             :     const TargetLowering *TLI = nullptr;
     211             :     const TargetRegisterInfo *TRI;
     212             :     const TargetTransformInfo *TTI = nullptr;
     213             :     const TargetLibraryInfo *TLInfo;
     214             :     const LoopInfo *LI;
     215             :     std::unique_ptr<BlockFrequencyInfo> BFI;
     216             :     std::unique_ptr<BranchProbabilityInfo> BPI;
     217             : 
     218             :     /// As we scan instructions optimizing them, this is the next instruction
     219             :     /// to optimize. Transforms that can invalidate this should update it.
     220             :     BasicBlock::iterator CurInstIterator;
     221             : 
     222             :     /// Keeps track of non-local addresses that have been sunk into a block.
     223             :     /// This allows us to avoid inserting duplicate code for blocks with
     224             :     /// multiple load/stores of the same address.
     225             :     ValueMap<Value*, Value*> SunkAddrs;
     226             : 
     227             :     /// Keeps track of all instructions inserted for the current function.
     228             :     SetOfInstrs InsertedInsts;
     229             : 
     230             :     /// Keeps track of the type of the related instruction before their
     231             :     /// promotion for the current function.
     232             :     InstrToOrigTy PromotedInsts;
     233             : 
     234             :     /// Keep track of instructions removed during promotion.
     235             :     SetOfInstrs RemovedInsts;
     236             : 
     237             :     /// Keep track of sext chains based on their initial value.
     238             :     DenseMap<Value *, Instruction *> SeenChainsForSExt;
     239             : 
     240             :     /// Keep track of SExt promoted.
     241             :     ValueToSExts ValToSExtendedUses;
     242             : 
     243             :     /// True if CFG is modified in any way.
     244             :     bool ModifiedDT;
     245             : 
     246             :     /// True if optimizing for size.
     247             :     bool OptSize;
     248             : 
     249             :     /// DataLayout for the Function being processed.
     250             :     const DataLayout *DL = nullptr;
     251             : 
     252             :   public:
     253             :     static char ID; // Pass identification, replacement for typeid
     254             : 
     255      172524 :     CodeGenPrepare() : FunctionPass(ID) {
     256       15684 :       initializeCodeGenPreparePass(*PassRegistry::getPassRegistry());
     257       15684 :     }
     258             : 
     259             :     bool runOnFunction(Function &F) override;
     260             : 
     261          27 :     StringRef getPassName() const override { return "CodeGen Prepare"; }
     262             : 
     263       15649 :     void getAnalysisUsage(AnalysisUsage &AU) const override {
     264             :       // FIXME: When we can selectively preserve passes, preserve the domtree.
     265       15649 :       AU.addRequired<ProfileSummaryInfoWrapperPass>();
     266       15649 :       AU.addRequired<TargetLibraryInfoWrapperPass>();
     267       15649 :       AU.addRequired<TargetTransformInfoWrapperPass>();
     268       15649 :       AU.addRequired<LoopInfoWrapperPass>();
     269       15649 :     }
     270             : 
     271             :   private:
     272             :     bool eliminateFallThrough(Function &F);
     273             :     bool eliminateMostlyEmptyBlocks(Function &F);
     274             :     BasicBlock *findDestBlockOfMergeableEmptyBlock(BasicBlock *BB);
     275             :     bool canMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const;
     276             :     void eliminateMostlyEmptyBlock(BasicBlock *BB);
     277             :     bool isMergingEmptyBlockProfitable(BasicBlock *BB, BasicBlock *DestBB,
     278             :                                        bool isPreheader);
     279             :     bool optimizeBlock(BasicBlock &BB, bool &ModifiedDT);
     280             :     bool optimizeInst(Instruction *I, bool &ModifiedDT);
     281             :     bool optimizeMemoryInst(Instruction *I, Value *Addr,
     282             :                             Type *AccessTy, unsigned AS);
     283             :     bool optimizeInlineAsmInst(CallInst *CS);
     284             :     bool optimizeCallInst(CallInst *CI, bool &ModifiedDT);
     285             :     bool optimizeExt(Instruction *&I);
     286             :     bool optimizeExtUses(Instruction *I);
     287             :     bool optimizeLoadExt(LoadInst *I);
     288             :     bool optimizeSelectInst(SelectInst *SI);
     289             :     bool optimizeShuffleVectorInst(ShuffleVectorInst *SI);
     290             :     bool optimizeSwitchInst(SwitchInst *CI);
     291             :     bool optimizeExtractElementInst(Instruction *Inst);
     292             :     bool dupRetToEnableTailCallOpts(BasicBlock *BB);
     293             :     bool placeDbgValues(Function &F);
     294             :     bool canFormExtLd(const SmallVectorImpl<Instruction *> &MovedExts,
     295             :                       LoadInst *&LI, Instruction *&Inst, bool HasPromoted);
     296             :     bool tryToPromoteExts(TypePromotionTransaction &TPT,
     297             :                           const SmallVectorImpl<Instruction *> &Exts,
     298             :                           SmallVectorImpl<Instruction *> &ProfitablyMovedExts,
     299             :                           unsigned CreatedInstsCost = 0);
     300             :     bool mergeSExts(Function &F);
     301             :     bool performAddressTypePromotion(
     302             :         Instruction *&Inst,
     303             :         bool AllowPromotionWithoutCommonHeader,
     304             :         bool HasPromoted, TypePromotionTransaction &TPT,
     305             :         SmallVectorImpl<Instruction *> &SpeculativelyMovedExts);
     306             :     bool splitBranchCondition(Function &F);
     307             :     bool simplifyOffsetableRelocate(Instruction &I);
     308             :     bool splitIndirectCriticalEdges(Function &F);
     309             :   };
     310             : 
     311             : } // end anonymous namespace
     312             : 
     313             : char CodeGenPrepare::ID = 0;
     314             : 
     315       26583 : INITIALIZE_PASS_BEGIN(CodeGenPrepare, DEBUG_TYPE,
     316             :                       "Optimize for code generation", false, false)
     317       26583 : INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
     318      238437 : INITIALIZE_PASS_END(CodeGenPrepare, DEBUG_TYPE,
     319             :                     "Optimize for code generation", false, false)
     320             : 
     321       15609 : FunctionPass *llvm::createCodeGenPreparePass() { return new CodeGenPrepare(); }
     322             : 
     323      136559 : bool CodeGenPrepare::runOnFunction(Function &F) {
     324      136559 :   if (skipFunction(F))
     325             :     return false;
     326             : 
     327      136502 :   DL = &F.getParent()->getDataLayout();
     328             : 
     329      136502 :   bool EverMadeChange = false;
     330             :   // Clear per function information.
     331      136502 :   InsertedInsts.clear();
     332      136502 :   PromotedInsts.clear();
     333      273004 :   BFI.reset();
     334      273004 :   BPI.reset();
     335             : 
     336      136502 :   ModifiedDT = false;
     337      136502 :   if (auto *TPC = getAnalysisIfAvailable<TargetPassConfig>()) {
     338      136493 :     TM = &TPC->getTM<TargetMachine>();
     339      136493 :     SubtargetInfo = TM->getSubtargetImpl(F);
     340      136493 :     TLI = SubtargetInfo->getTargetLowering();
     341      136493 :     TRI = SubtargetInfo->getRegisterInfo();
     342             :   }
     343      273004 :   TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
     344      136502 :   TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
     345      273004 :   LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
     346      136502 :   OptSize = F.optForSize();
     347             : 
     348      136502 :   if (ProfileGuidedSectionPrefix) {
     349             :     ProfileSummaryInfo *PSI =
     350      273004 :         getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
     351      136502 :     if (PSI->isFunctionHotInCallGraph(&F))
     352           7 :       F.setSectionPrefix(".hot");
     353      136495 :     else if (PSI->isFunctionColdInCallGraph(&F))
     354           8 :       F.setSectionPrefix(".unlikely");
     355             :   }
     356             : 
     357             :   /// This optimization identifies DIV instructions that can be
     358             :   /// profitably bypassed and carried out with a shorter, faster divide.
     359      271282 :   if (!OptSize && TLI && TLI->isSlowDivBypassed()) {
     360             :     const DenseMap<unsigned int, unsigned int> &BypassWidths =
     361       23546 :        TLI->getBypassSlowDivWidths();
     362       11773 :     BasicBlock* BB = &*F.begin();
     363       35943 :     while (BB != nullptr) {
     364             :       // bypassSlowDivision may create new BBs, but we don't want to reapply the
     365             :       // optimization to those blocks.
     366       24170 :       BasicBlock* Next = BB->getNextNode();
     367       12085 :       EverMadeChange |= bypassSlowDivision(BB, BypassWidths);
     368       12085 :       BB = Next;
     369             :     }
     370             :   }
     371             : 
     372             :   // Eliminate blocks that contain only PHI nodes and an
     373             :   // unconditional branch.
     374      136502 :   EverMadeChange |= eliminateMostlyEmptyBlocks(F);
     375             : 
     376             :   // llvm.dbg.value is far away from the value then iSel may not be able
     377             :   // handle it properly. iSel will drop llvm.dbg.value if it can not
     378             :   // find a node corresponding to the value.
     379      136502 :   EverMadeChange |= placeDbgValues(F);
     380             : 
     381      136502 :   if (!DisableBranchOpts)
     382      136439 :     EverMadeChange |= splitBranchCondition(F);
     383             : 
     384             :   // Split some critical edges where one of the sources is an indirect branch,
     385             :   // to help generate sane code for PHIs involving such edges.
     386      136502 :   EverMadeChange |= splitIndirectCriticalEdges(F);
     387             : 
     388      136502 :   bool MadeChange = true;
     389      424040 :   while (MadeChange) {
     390      143769 :     MadeChange = false;
     391      143769 :     SeenChainsForSExt.clear();
     392      143769 :     ValToSExtendedUses.clear();
     393      143769 :     RemovedInsts.clear();
     394     1091464 :     for (Function::iterator I = F.begin(); I != F.end(); ) {
     395     1206516 :       BasicBlock *BB = &*I++;
     396      402172 :       bool ModifiedDTOnIteration = false;
     397      402172 :       MadeChange |= optimizeBlock(*BB, ModifiedDTOnIteration);
     398             : 
     399             :       // Restart BB iteration if the dominator tree of the Function was changed
     400      402172 :       if (ModifiedDTOnIteration)
     401             :         break;
     402             :     }
     403      287538 :     if (EnableTypePromotionMerge && !ValToSExtendedUses.empty())
     404          35 :       MadeChange |= mergeSExts(F);
     405             : 
     406             :     // Really free removed instructions during promotion.
     407      143909 :     for (Instruction *I : RemovedInsts)
     408         140 :       I->deleteValue();
     409             : 
     410      143769 :     EverMadeChange |= MadeChange;
     411             :   }
     412             : 
     413      273004 :   SunkAddrs.clear();
     414             : 
     415      136502 :   if (!DisableBranchOpts) {
     416      136439 :     MadeChange = false;
     417      272878 :     SmallPtrSet<BasicBlock*, 8> WorkList;
     418      684019 :     for (BasicBlock &BB : F) {
     419      828609 :       SmallVector<BasicBlock *, 2> Successors(succ_begin(&BB), succ_end(&BB));
     420      274702 :       MadeChange |= ConstantFoldTerminator(&BB, true);
     421      544901 :       if (!MadeChange) continue;
     422             : 
     423        7195 :       for (SmallVectorImpl<BasicBlock*>::iterator
     424        9006 :              II = Successors.begin(), IE = Successors.end(); II != IE; ++II)
     425       28780 :         if (pred_begin(*II) == pred_end(*II))
     426        1071 :           WorkList.insert(*II);
     427             :     }
     428             : 
     429             :     // Delete the dead blocks and any of their dead successors.
     430      136439 :     MadeChange |= !WorkList.empty();
     431      139793 :     while (!WorkList.empty()) {
     432        3354 :       BasicBlock *BB = *WorkList.begin();
     433        1677 :       WorkList.erase(BB);
     434        6708 :       SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB));
     435             : 
     436        1677 :       DeleteDeadBlock(BB);
     437             : 
     438        1377 :       for (SmallVectorImpl<BasicBlock*>::iterator
     439        3354 :              II = Successors.begin(), IE = Successors.end(); II != IE; ++II)
     440        5508 :         if (pred_begin(*II) == pred_end(*II))
     441         777 :           WorkList.insert(*II);
     442             :     }
     443             : 
     444             :     // Merge pairs of basic blocks with unconditional branches, connected by
     445             :     // a single edge.
     446      136439 :     if (EverMadeChange || MadeChange)
     447        7983 :       MadeChange |= eliminateFallThrough(F);
     448             : 
     449      136439 :     EverMadeChange |= MadeChange;
     450             :   }
     451             : 
     452      136502 :   if (!DisableGCOpts) {
     453      273004 :     SmallVector<Instruction *, 2> Statepoints;
     454      681168 :     for (BasicBlock &BB : F)
     455     2746137 :       for (Instruction &I : BB)
     456     1931151 :         if (isStatepoint(I))
     457          75 :           Statepoints.push_back(&I);
     458      409581 :     for (auto &I : Statepoints)
     459          75 :       EverMadeChange |= simplifyOffsetableRelocate(*I);
     460             :   }
     461             : 
     462             :   return EverMadeChange;
     463             : }
     464             : 
     465             : /// Merge basic blocks which are connected by a single edge, where one of the
     466             : /// basic blocks has a single successor pointing to the other basic block,
     467             : /// which has a single predecessor.
     468        7983 : bool CodeGenPrepare::eliminateFallThrough(Function &F) {
     469        7983 :   bool Changed = false;
     470             :   // Scan all of the blocks in the function, except for the entry block.
     471       23949 :   for (Function::iterator I = std::next(F.begin()), E = F.end(); I != E;) {
     472      367341 :     BasicBlock *BB = &*I++;
     473             :     // If the destination block has a single pred, then this is a trivial
     474             :     // edge, just collapse it.
     475      122447 :     BasicBlock *SinglePred = BB->getSinglePredecessor();
     476             : 
     477             :     // Don't merge if BB's address is taken.
     478      206426 :     if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) continue;
     479             : 
     480      122878 :     BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator());
     481       38999 :     if (Term && !Term->isConditional()) {
     482        1478 :       Changed = true;
     483             :       DEBUG(dbgs() << "To merge:\n"<< *SinglePred << "\n\n\n");
     484             :       // Remember if SinglePred was the entry block of the function.
     485             :       // If so, we will need to move BB back to the entry position.
     486        2956 :       bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
     487        1478 :       MergeBasicBlockIntoOnlyPred(BB, nullptr);
     488             : 
     489        2048 :       if (isEntry && BB != &BB->getParent()->getEntryBlock())
     490           0 :         BB->moveBefore(&BB->getParent()->getEntryBlock());
     491             : 
     492             :       // We have erased a block. Update the iterator.
     493        2956 :       I = BB->getIterator();
     494             :     }
     495             :   }
     496        7983 :   return Changed;
     497             : }
     498             : 
     499             : /// Find a destination block from BB if BB is mergeable empty block.
     500      141106 : BasicBlock *CodeGenPrepare::findDestBlockOfMergeableEmptyBlock(BasicBlock *BB) {
     501             :   // If this block doesn't end with an uncond branch, ignore it.
     502      231420 :   BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
     503       90314 :   if (!BI || !BI->isUnconditional())
     504             :     return nullptr;
     505             : 
     506             :   // If the instruction before the branch (skipping debug info) isn't a phi
     507             :   // node, then other stuff is happening here.
     508      116408 :   BasicBlock::iterator BBI = BI->getIterator();
     509       58204 :   if (BBI != BB->begin()) {
     510             :     --BBI;
     511       53423 :     while (isa<DbgInfoIntrinsic>(BBI)) {
     512         680 :       if (BBI == BB->begin())
     513             :         break;
     514             :       --BBI;
     515             :     }
     516      106173 :     if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI))
     517             :       return nullptr;
     518             :   }
     519             : 
     520             :   // Do not break infinite loops.
     521        5379 :   BasicBlock *DestBB = BI->getSuccessor(0);
     522        5379 :   if (DestBB == BB)
     523             :     return nullptr;
     524             : 
     525        5298 :   if (!canMergeBlocks(BB, DestBB))
     526         417 :     DestBB = nullptr;
     527             : 
     528             :   return DestBB;
     529             : }
     530             : 
     531             : // Return the unique indirectbr predecessor of a block. This may return null
     532             : // even if such a predecessor exists, if it's not useful for splitting.
     533             : // If a predecessor is found, OtherPreds will contain all other (non-indirectbr)
     534             : // predecessors of BB.
     535             : static BasicBlock *
     536         197 : findIBRPredecessor(BasicBlock *BB, SmallVectorImpl<BasicBlock *> &OtherPreds) {
     537             :   // If the block doesn't have any PHIs, we don't care about it, since there's
     538             :   // no point in splitting it.
     539         253 :   PHINode *PN = dyn_cast<PHINode>(BB->begin());
     540             :   if (!PN)
     541             :     return nullptr;
     542             : 
     543             :   // Verify we have exactly one IBR predecessor.
     544             :   // Conservatively bail out if one of the other predecessors is not a "regular"
     545             :   // terminator (that is, not a switch or a br).
     546          56 :   BasicBlock *IBB = nullptr;
     547         223 :   for (unsigned Pred = 0, E = PN->getNumIncomingValues(); Pred != E; ++Pred) {
     548         113 :     BasicBlock *PredBB = PN->getIncomingBlock(Pred);
     549         226 :     TerminatorInst *PredTerm = PredBB->getTerminator();
     550         281 :     switch (PredTerm->getOpcode()) {
     551          58 :     case Instruction::IndirectBr:
     552          58 :       if (IBB)
     553           2 :         return nullptr;
     554          56 :       IBB = PredBB;
     555             :       break;
     556          55 :     case Instruction::Br:
     557             :     case Instruction::Switch:
     558          55 :       OtherPreds.push_back(PredBB);
     559          55 :       continue;
     560             :     default:
     561             :       return nullptr;
     562             :     }
     563             :   }
     564             : 
     565             :   return IBB;
     566             : }
     567             : 
     568             : // Split critical edges where the source of the edge is an indirectbr
     569             : // instruction. This isn't always possible, but we can handle some easy cases.
     570             : // This is useful because MI is unable to split such critical edges,
     571             : // which means it will not be able to sink instructions along those edges.
     572             : // This is especially painful for indirect branches with many successors, where
     573             : // we end up having to prepare all outgoing values in the origin block.
     574             : //
     575             : // Our normal algorithm for splitting critical edges requires us to update
     576             : // the outgoing edges of the edge origin block, but for an indirectbr this
     577             : // is hard, since it would require finding and updating the block addresses
     578             : // the indirect branch uses. But if a block only has a single indirectbr
     579             : // predecessor, with the others being regular branches, we can do it in a
     580             : // different way.
     581             : // Say we have A -> D, B -> D, I -> D where only I -> D is an indirectbr.
     582             : // We can split D into D0 and D1, where D0 contains only the PHIs from D,
     583             : // and D1 is the D block body. We can then duplicate D0 as D0A and D0B, and
     584             : // create the following structure:
     585             : // A -> D0A, B -> D0A, I -> D0B, D0A -> D1, D0B -> D1
     586      136502 : bool CodeGenPrepare::splitIndirectCriticalEdges(Function &F) {
     587             :   // Check whether the function has any indirectbrs, and collect which blocks
     588             :   // they may jump to. Since most functions don't have indirect branches,
     589             :   // this lowers the common case's overhead to O(Blocks) instead of O(Edges).
     590      273004 :   SmallSetVector<BasicBlock *, 16> Targets;
     591      683753 :   for (auto &BB : F) {
     592      274330 :     auto *IBI = dyn_cast<IndirectBrInst>(BB.getTerminator());
     593      274164 :     if (!IBI)
     594             :       continue;
     595             : 
     596         383 :     for (unsigned Succ = 0, E = IBI->getNumSuccessors(); Succ != E; ++Succ)
     597         217 :       Targets.insert(IBI->getSuccessor(Succ));
     598             :   }
     599             : 
     600      136502 :   if (Targets.empty())
     601             :     return false;
     602             : 
     603             :   bool Changed = false;
     604         463 :   for (BasicBlock *Target : Targets) {
     605         249 :     SmallVector<BasicBlock *, 16> OtherPreds;
     606         197 :     BasicBlock *IBRPred = findIBRPredecessor(Target, OtherPreds);
     607             :     // If we did not found an indirectbr, or the indirectbr is the only
     608             :     // incoming edge, this isn't the kind of edge we're looking for.
     609         342 :     if (!IBRPred || OtherPreds.empty())
     610         145 :       continue;
     611             : 
     612             :     // Don't even think about ehpads/landingpads.
     613          52 :     Instruction *FirstNonPHI = Target->getFirstNonPHI();
     614          52 :     if (FirstNonPHI->isEHPad() || Target->isLandingPad())
     615             :       continue;
     616             : 
     617         104 :     BasicBlock *BodyBlock = Target->splitBasicBlock(FirstNonPHI, ".split");
     618             :     // It's possible Target was its own successor through an indirectbr.
     619             :     // In this case, the indirectbr now comes from BodyBlock.
     620          52 :     if (IBRPred == Target)
     621           5 :       IBRPred = BodyBlock;
     622             : 
     623             :     // At this point Target only has PHIs, and BodyBlock has the rest of the
     624             :     // block's body. Create a copy of Target that will be used by the "direct"
     625             :     // preds.
     626         104 :     ValueToValueMapTy VMap;
     627          52 :     BasicBlock *DirectSucc = CloneBasicBlock(Target, VMap, ".clone", &F);
     628             : 
     629         209 :     for (BasicBlock *Pred : OtherPreds) {
     630             :       // If the target is a loop to itself, then the terminator of the split
     631             :       // block needs to be updated.
     632          53 :       if (Pred == Target)
     633           1 :         BodyBlock->getTerminator()->replaceUsesOfWith(Target, DirectSucc);
     634             :       else
     635          52 :         Pred->getTerminator()->replaceUsesOfWith(Target, DirectSucc);
     636             :     }
     637             : 
     638             :     // Ok, now fix up the PHIs. We know the two blocks only have PHIs, and that
     639             :     // they are clones, so the number of PHIs are the same.
     640             :     // (a) Remove the edge coming from IBRPred from the "Direct" PHI
     641             :     // (b) Leave that as the only edge in the "Indirect" PHI.
     642             :     // (c) Merge the two in the body block.
     643          52 :     BasicBlock::iterator Indirect = Target->begin(),
     644         104 :                          End = Target->getFirstNonPHI()->getIterator();
     645          52 :     BasicBlock::iterator Direct = DirectSucc->begin();
     646          52 :     BasicBlock::iterator MergeInsert = BodyBlock->getFirstInsertionPt();
     647             : 
     648             :     assert(&*End == Target->getTerminator() &&
     649             :            "Block was expected to only contain PHIs");
     650             : 
     651         106 :     while (Indirect != End) {
     652          54 :       PHINode *DirPHI = cast<PHINode>(Direct);
     653          54 :       PHINode *IndPHI = cast<PHINode>(Indirect);
     654             : 
     655             :       // Now, clean up - the direct block shouldn't get the indirect value,
     656             :       // and vice versa.
     657          54 :       DirPHI->removeIncomingValue(IBRPred);
     658         108 :       Direct++;
     659             : 
     660             :       // Advance the pointer here, to avoid invalidation issues when the old
     661             :       // PHI is erased.
     662         108 :       Indirect++;
     663             : 
     664         108 :       PHINode *NewIndPHI = PHINode::Create(IndPHI->getType(), 1, "ind", IndPHI);
     665          54 :       NewIndPHI->addIncoming(IndPHI->getIncomingValueForBlock(IBRPred),
     666             :                              IBRPred);
     667             : 
     668             :       // Create a PHI in the body block, to merge the direct and indirect
     669             :       // predecessors.
     670             :       PHINode *MergePHI =
     671         162 :           PHINode::Create(IndPHI->getType(), 2, "merge", &*MergeInsert);
     672          54 :       MergePHI->addIncoming(NewIndPHI, Target);
     673          54 :       MergePHI->addIncoming(DirPHI, DirectSucc);
     674             : 
     675          54 :       IndPHI->replaceAllUsesWith(MergePHI);
     676          54 :       IndPHI->eraseFromParent();
     677             :     }
     678             : 
     679          52 :     Changed = true;
     680             :   }
     681             : 
     682             :   return Changed;
     683             : }
     684             : 
     685             : /// Eliminate blocks that contain only PHI nodes, debug info directives, and an
     686             : /// unconditional branch. Passes before isel (e.g. LSR/loopsimplify) often split
     687             : /// edges in ways that are non-optimal for isel. Start by eliminating these
     688             : /// blocks so we can split them the way we want them.
     689      136502 : bool CodeGenPrepare::eliminateMostlyEmptyBlocks(Function &F) {
     690      273004 :   SmallPtrSet<BasicBlock *, 16> Preheaders;
     691      546008 :   SmallVector<Loop *, 16> LoopList(LI->begin(), LI->end());
     692      142249 :   while (!LoopList.empty()) {
     693        5747 :     Loop *L = LoopList.pop_back_val();
     694       17241 :     LoopList.insert(LoopList.end(), L->begin(), L->end());
     695        5747 :     if (BasicBlock *Preheader = L->getLoopPreheader())
     696        5711 :       Preheaders.insert(Preheader);
     697             :   }
     698             : 
     699      136502 :   bool MadeChange = false;
     700             :   // Note that this intentionally skips the entry block.
     701      409506 :   for (Function::iterator I = std::next(F.begin()), E = F.end(); I != E;) {
     702      423297 :     BasicBlock *BB = &*I++;
     703      141099 :     BasicBlock *DestBB = findDestBlockOfMergeableEmptyBlock(BB);
     704      145976 :     if (!DestBB ||
     705        4877 :         !isMergingEmptyBlockProfitable(BB, DestBB, Preheaders.count(BB)))
     706      137742 :       continue;
     707             : 
     708        3357 :     eliminateMostlyEmptyBlock(BB);
     709        3357 :     MadeChange = true;
     710             :   }
     711      273004 :   return MadeChange;
     712             : }
     713             : 
     714        4877 : bool CodeGenPrepare::isMergingEmptyBlockProfitable(BasicBlock *BB,
     715             :                                                    BasicBlock *DestBB,
     716             :                                                    bool isPreheader) {
     717             :   // Do not delete loop preheaders if doing so would create a critical edge.
     718             :   // Loop preheaders can be good locations to spill registers. If the
     719             :   // preheader is deleted and we create a critical edge, registers may be
     720             :   // spilled in the loop body instead.
     721        6344 :   if (!DisablePreheaderProtect && isPreheader &&
     722        2846 :       !(BB->getSinglePredecessor() &&
     723        2758 :         BB->getSinglePredecessor()->getSingleSuccessor()))
     724             :     return false;
     725             : 
     726             :   // Try to skip merging if the unique predecessor of BB is terminated by a
     727             :   // switch or indirect branch instruction, and BB is used as an incoming block
     728             :   // of PHIs in DestBB. In such case, merging BB and DestBB would cause ISel to
     729             :   // add COPY instructions in the predecessor of BB instead of BB (if it is not
     730             :   // merged). Note that the critical edge created by merging such blocks wont be
     731             :   // split in MachineSink because the jump table is not analyzable. By keeping
     732             :   // such empty block (BB), ISel will place COPY instructions in BB, not in the
     733             :   // predecessor of BB.
     734        3415 :   BasicBlock *Pred = BB->getUniquePredecessor();
     735        6335 :   if (!Pred ||
     736        5441 :       !(isa<SwitchInst>(Pred->getTerminator()) ||
     737        2521 :         isa<IndirectBrInst>(Pred->getTerminator())))
     738             :     return true;
     739             : 
     740         820 :   if (BB->getTerminator() != BB->getFirstNonPHI())
     741             :     return true;
     742             : 
     743             :   // We use a simple cost heuristic which determine skipping merging is
     744             :   // profitable if the cost of skipping merging is less than the cost of
     745             :   // merging : Cost(skipping merging) < Cost(merging BB), where the
     746             :   // Cost(skipping merging) is Freq(BB) * (Cost(Copy) + Cost(Branch)), and
     747             :   // the Cost(merging BB) is Freq(Pred) * Cost(Copy).
     748             :   // Assuming Cost(Copy) == Cost(Branch), we could simplify it to :
     749             :   //   Freq(Pred) / Freq(BB) > 2.
     750             :   // Note that if there are multiple empty blocks sharing the same incoming
     751             :   // value for the PHIs in the DestBB, we consider them together. In such
     752             :   // case, Cost(merging BB) will be the sum of their frequencies.
     753             : 
     754         820 :   if (!isa<PHINode>(DestBB->begin()))
     755             :     return true;
     756             : 
     757          71 :   SmallPtrSet<BasicBlock *, 16> SameIncomingValueBBs;
     758             : 
     759             :   // Find all other incoming blocks from which incoming values of all PHIs in
     760             :   // DestBB are the same as the ones from BB.
     761         450 :   for (pred_iterator PI = pred_begin(DestBB), E = pred_end(DestBB); PI != E;
     762             :        ++PI) {
     763         237 :     BasicBlock *DestBBPred = *PI;
     764         237 :     if (DestBBPred == BB)
     765          71 :       continue;
     766             : 
     767         166 :     bool HasAllSameValue = true;
     768         332 :     BasicBlock::const_iterator DestBBI = DestBB->begin();
     769         570 :     while (const PHINode *DestPN = dyn_cast<PHINode>(DestBBI++)) {
     770         332 :       if (DestPN->getIncomingValueForBlock(BB) !=
     771         166 :           DestPN->getIncomingValueForBlock(DestBBPred)) {
     772             :         HasAllSameValue = false;
     773             :         break;
     774             :       }
     775             :     }
     776         166 :     if (HasAllSameValue)
     777          36 :       SameIncomingValueBBs.insert(DestBBPred);
     778             :   }
     779             : 
     780             :   // See if all BB's incoming values are same as the value from Pred. In this
     781             :   // case, no reason to skip merging because COPYs are expected to be place in
     782             :   // Pred already.
     783          71 :   if (SameIncomingValueBBs.count(Pred))
     784             :     return true;
     785             : 
     786         130 :   if (!BFI) {
     787          46 :     Function &F = *BB->getParent();
     788         138 :     LoopInfo LI{DominatorTree(F)};
     789          92 :     BPI.reset(new BranchProbabilityInfo(F, LI));
     790         138 :     BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
     791             :   }
     792             : 
     793         130 :   BlockFrequency PredFreq = BFI->getBlockFreq(Pred);
     794         130 :   BlockFrequency BBFreq = BFI->getBlockFreq(BB);
     795             : 
     796          80 :   for (auto SameValueBB : SameIncomingValueBBs)
     797          22 :     if (SameValueBB->getUniquePredecessor() == Pred &&
     798           7 :         DestBB == findDestBlockOfMergeableEmptyBlock(SameValueBB))
     799           8 :       BBFreq += BFI->getBlockFreq(SameValueBB);
     800             : 
     801          65 :   return PredFreq.getFrequency() <=
     802         130 :          BBFreq.getFrequency() * FreqRatioToSkipMerge;
     803             : }
     804             : 
     805             : /// Return true if we can merge BB into DestBB if there is a single
     806             : /// unconditional branch between them, and BB contains no other non-phi
     807             : /// instructions.
     808        5298 : bool CodeGenPrepare::canMergeBlocks(const BasicBlock *BB,
     809             :                                     const BasicBlock *DestBB) const {
     810             :   // We only want to eliminate blocks whose phi nodes are used by phi nodes in
     811             :   // the successor.  If there are more complex condition (e.g. preheaders),
     812             :   // don't mess around with them.
     813        5298 :   BasicBlock::const_iterator BBI = BB->begin();
     814       11309 :   while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
     815        1392 :     for (const User *U : PN->users()) {
     816         298 :       const Instruction *UI = cast<Instruction>(U);
     817         536 :       if (UI->getParent() != DestBB || !isa<PHINode>(UI))
     818             :         return false;
     819             :       // If User is inside DestBB block and it is a PHINode then check
     820             :       // incoming value. If incoming value is not from BB then this is
     821             :       // a complex condition (e.g. preheaders) we want to avoid here.
     822         217 :       if (UI->getParent() == DestBB) {
     823         434 :         if (const PHINode *UPN = dyn_cast<PHINode>(UI))
     824         974 :           for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) {
     825         891 :             Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I));
     826         566 :             if (Insn && Insn->getParent() == BB &&
     827         434 :                 Insn->getParent() != UPN->getIncomingBlock(I))
     828             :               return false;
     829             :           }
     830             :       }
     831             :     }
     832             :   }
     833             : 
     834             :   // If BB and DestBB contain any common predecessors, then the phi nodes in BB
     835             :   // and DestBB may have conflicting incoming values for the block.  If so, we
     836             :   // can't merge the block.
     837        7423 :   const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin());
     838             :   if (!DestBBPN) return true;  // no conflict.
     839             : 
     840             :   // Collect the preds of BB.
     841        2208 :   SmallPtrSet<const BasicBlock*, 16> BBPreds;
     842        2381 :   if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
     843             :     // It is faster to get preds from a PHI than with pred_iterator.
     844         729 :     for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
     845         383 :       BBPreds.insert(BBPN->getIncomingBlock(i));
     846             :   } else {
     847        6105 :     BBPreds.insert(pred_begin(BB), pred_end(BB));
     848             :   }
     849             : 
     850             :   // Walk the preds of DestBB.
     851        9186 :   for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) {
     852        5104 :     BasicBlock *Pred = DestBBPN->getIncomingBlock(i);
     853        5104 :     if (BBPreds.count(Pred)) {   // Common predecessor?
     854         359 :       BBI = DestBB->begin();
     855        1157 :       while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
     856         369 :         const Value *V1 = PN->getIncomingValueForBlock(Pred);
     857         369 :         const Value *V2 = PN->getIncomingValueForBlock(BB);
     858             : 
     859             :         // If V2 is a phi node in BB, look up what the mapped value will be.
     860          40 :         if (const PHINode *V2PN = dyn_cast<PHINode>(V2))
     861          40 :           if (V2PN->getParent() == BB)
     862          26 :             V2 = V2PN->getIncomingValueForBlock(Pred);
     863             : 
     864             :         // If there is a conflict, bail out.
     865         369 :         if (V1 != V2) return false;
     866             :       }
     867             :     }
     868             :   }
     869             : 
     870             :   return true;
     871             : }
     872             : 
     873             : /// Eliminate a basic block that has only phi's and an unconditional branch in
     874             : /// it.
     875        3357 : void CodeGenPrepare::eliminateMostlyEmptyBlock(BasicBlock *BB) {
     876        6714 :   BranchInst *BI = cast<BranchInst>(BB->getTerminator());
     877        3357 :   BasicBlock *DestBB = BI->getSuccessor(0);
     878             : 
     879             :   DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB);
     880             : 
     881             :   // If the destination block has a single pred, then this is a trivial edge,
     882             :   // just collapse it.
     883        3357 :   if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) {
     884         110 :     if (SinglePred != DestBB) {
     885             :       // Remember if SinglePred was the entry block of the function.  If so, we
     886             :       // will need to move BB back to the entry position.
     887         220 :       bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
     888         110 :       MergeBasicBlockIntoOnlyPred(DestBB, nullptr);
     889             : 
     890         110 :       if (isEntry && BB != &BB->getParent()->getEntryBlock())
     891           0 :         BB->moveBefore(&BB->getParent()->getEntryBlock());
     892             : 
     893             :       DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
     894             :       return;
     895             :     }
     896             :   }
     897             : 
     898             :   // Otherwise, we have multiple predecessors of BB.  Update the PHIs in DestBB
     899             :   // to handle the new incoming edges it is about to have.
     900             :   PHINode *PN;
     901        3247 :   for (BasicBlock::iterator BBI = DestBB->begin();
     902        1326 :        (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
     903             :     // Remove the incoming value for BB, and remember it.
     904        1326 :     Value *InVal = PN->removeIncomingValue(BB, false);
     905             : 
     906             :     // Two options: either the InVal is a phi node defined in BB or it is some
     907             :     // value that dominates BB.
     908         417 :     PHINode *InValPhi = dyn_cast<PHINode>(InVal);
     909         417 :     if (InValPhi && InValPhi->getParent() == BB) {
     910             :       // Add all of the input values of the input PHI as inputs of this phi.
     911         585 :       for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i)
     912         630 :         PN->addIncoming(InValPhi->getIncomingValue(i),
     913             :                         InValPhi->getIncomingBlock(i));
     914             :     } else {
     915             :       // Otherwise, add one instance of the dominating value for each edge that
     916             :       // we will be adding.
     917        1205 :       if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
     918          59 :         for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
     919          31 :           PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
     920             :       } else {
     921        4746 :         for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
     922        1215 :           PN->addIncoming(InVal, *PI);
     923             :       }
     924             :     }
     925             :   }
     926             : 
     927             :   // The PHIs are now updated, change everything that refers to BB to use
     928             :   // DestBB and remove BB.
     929        3247 :   BB->replaceAllUsesWith(DestBB);
     930        3247 :   BB->eraseFromParent();
     931             :   ++NumBlocksElim;
     932             : 
     933             :   DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
     934             : }
     935             : 
     936             : // Computes a map of base pointer relocation instructions to corresponding
     937             : // derived pointer relocation instructions given a vector of all relocate calls
     938          25 : static void computeBaseDerivedRelocateMap(
     939             :     const SmallVectorImpl<GCRelocateInst *> &AllRelocateCalls,
     940             :     DenseMap<GCRelocateInst *, SmallVector<GCRelocateInst *, 2>>
     941             :         &RelocateInstMap) {
     942             :   // Collect information in two maps: one primarily for locating the base object
     943             :   // while filling the second map; the second map is the final structure holding
     944             :   // a mapping between Base and corresponding Derived relocate calls
     945          50 :   DenseMap<std::pair<unsigned, unsigned>, GCRelocateInst *> RelocateIdxMap;
     946         137 :   for (auto *ThisRelocate : AllRelocateCalls) {
     947         124 :     auto K = std::make_pair(ThisRelocate->getBasePtrIndex(),
     948         248 :                             ThisRelocate->getDerivedPtrIndex());
     949         186 :     RelocateIdxMap.insert(std::make_pair(K, ThisRelocate));
     950             :   }
     951         199 :   for (auto &Item : RelocateIdxMap) {
     952          62 :     std::pair<unsigned, unsigned> Key = Item.first;
     953          62 :     if (Key.first == Key.second)
     954             :       // Base relocation: nothing to insert
     955          70 :       continue;
     956             : 
     957          28 :     GCRelocateInst *I = Item.second;
     958          56 :     auto BaseKey = std::make_pair(Key.first, Key.first);
     959             : 
     960             :     // We're iterating over RelocateIdxMap so we cannot modify it.
     961          28 :     auto MaybeBase = RelocateIdxMap.find(BaseKey);
     962          56 :     if (MaybeBase == RelocateIdxMap.end())
     963             :       // TODO: We might want to insert a new base object relocate and gep off
     964             :       // that, if there are enough derived object relocates.
     965           2 :       continue;
     966             : 
     967          52 :     RelocateInstMap[MaybeBase->second].push_back(I);
     968             :   }
     969          25 : }
     970             : 
     971             : // Accepts a GEP and extracts the operands into a vector provided they're all
     972             : // small integer constants
     973          11 : static bool getGEPSmallConstantIntOffsetV(GetElementPtrInst *GEP,
     974             :                                           SmallVectorImpl<Value *> &OffsetV) {
     975          42 :   for (unsigned i = 1; i < GEP->getNumOperands(); i++) {
     976             :     // Only accept small constant integer operands
     977          24 :     auto Op = dyn_cast<ConstantInt>(GEP->getOperand(i));
     978          11 :     if (!Op || Op->getZExtValue() > 20)
     979             :       return false;
     980             :   }
     981             : 
     982          26 :   for (unsigned i = 1; i < GEP->getNumOperands(); i++)
     983          18 :     OffsetV.push_back(GEP->getOperand(i));
     984             :   return true;
     985             : }
     986             : 
     987             : // Takes a RelocatedBase (base pointer relocation instruction) and Targets to
     988             : // replace, computes a replacement, and affects it.
     989             : static bool
     990          18 : simplifyRelocatesOffABase(GCRelocateInst *RelocatedBase,
     991             :                           const SmallVectorImpl<GCRelocateInst *> &Targets) {
     992          18 :   bool MadeChange = false;
     993             :   // We must ensure the relocation of derived pointer is defined after
     994             :   // relocation of base pointer. If we find a relocation corresponding to base
     995             :   // defined earlier than relocation of base then we move relocation of base
     996             :   // right before found relocation. We consider only relocation in the same
     997             :   // basic block as relocation of base. Relocations from other basic block will
     998             :   // be skipped by optimization and we do not care about them.
     999          36 :   for (auto R = RelocatedBase->getParent()->getFirstInsertionPt();
    1000          64 :        &*R != RelocatedBase; ++R)
    1001          12 :     if (auto RI = dyn_cast<GCRelocateInst>(R))
    1002          12 :       if (RI->getStatepoint() == RelocatedBase->getStatepoint())
    1003          12 :         if (RI->getBasePtrIndex() == RelocatedBase->getBasePtrIndex()) {
    1004           6 :           RelocatedBase->moveBefore(RI);
    1005           6 :           break;
    1006             :         }
    1007             : 
    1008          80 :   for (GCRelocateInst *ToReplace : Targets) {
    1009             :     assert(ToReplace->getBasePtrIndex() == RelocatedBase->getBasePtrIndex() &&
    1010             :            "Not relocating a derived object of the original base object");
    1011          52 :     if (ToReplace->getBasePtrIndex() == ToReplace->getDerivedPtrIndex()) {
    1012             :       // A duplicate relocate call. TODO: coalesce duplicates.
    1013          18 :       continue;
    1014             :     }
    1015             : 
    1016          26 :     if (RelocatedBase->getParent() != ToReplace->getParent()) {
    1017             :       // Base and derived relocates are in different basic blocks.
    1018             :       // In this case transform is only valid when base dominates derived
    1019             :       // relocate. However it would be too expensive to check dominance
    1020             :       // for each such relocate, so we skip the whole transformation.
    1021           1 :       continue;
    1022             :     }
    1023             : 
    1024          25 :     Value *Base = ToReplace->getBasePtr();
    1025          36 :     auto Derived = dyn_cast<GetElementPtrInst>(ToReplace->getDerivedPtr());
    1026          25 :     if (!Derived || Derived->getPointerOperand() != Base)
    1027          14 :       continue;
    1028             : 
    1029          19 :     SmallVector<Value *, 2> OffsetV;
    1030          14 :     if (!getGEPSmallConstantIntOffsetV(Derived, OffsetV))
    1031             :       continue;
    1032             : 
    1033             :     // Create a Builder and replace the target callsite with a gep
    1034             :     assert(RelocatedBase->getNextNode() &&
    1035             :            "Should always have one since it's not a terminator");
    1036             : 
    1037             :     // Insert after RelocatedBase
    1038          24 :     IRBuilder<> Builder(RelocatedBase->getNextNode());
    1039          32 :     Builder.SetCurrentDebugLocation(ToReplace->getDebugLoc());
    1040             : 
    1041             :     // If gc_relocate does not match the actual type, cast it to the right type.
    1042             :     // In theory, there must be a bitcast after gc_relocate if the type does not
    1043             :     // match, and we should reuse it to get the derived pointer. But it could be
    1044             :     // cases like this:
    1045             :     // bb1:
    1046             :     //  ...
    1047             :     //  %g1 = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(...)
    1048             :     //  br label %merge
    1049             :     //
    1050             :     // bb2:
    1051             :     //  ...
    1052             :     //  %g2 = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(...)
    1053             :     //  br label %merge
    1054             :     //
    1055             :     // merge:
    1056             :     //  %p1 = phi i8 addrspace(1)* [ %g1, %bb1 ], [ %g2, %bb2 ]
    1057             :     //  %cast = bitcast i8 addrspace(1)* %p1 in to i32 addrspace(1)*
    1058             :     //
    1059             :     // In this case, we can not find the bitcast any more. So we insert a new bitcast
    1060             :     // no matter there is already one or not. In this way, we can handle all cases, and
    1061             :     // the extra bitcast should be optimized away in later passes.
    1062           8 :     Value *ActualRelocatedBase = RelocatedBase;
    1063           8 :     if (RelocatedBase->getType() != Base->getType()) {
    1064           0 :       ActualRelocatedBase =
    1065           0 :           Builder.CreateBitCast(RelocatedBase, Base->getType());
    1066             :     }
    1067          24 :     Value *Replacement = Builder.CreateGEP(
    1068           8 :         Derived->getSourceElementType(), ActualRelocatedBase, makeArrayRef(OffsetV));
    1069           8 :     Replacement->takeName(ToReplace);
    1070             :     // If the newly generated derived pointer's type does not match the original derived
    1071             :     // pointer's type, cast the new derived pointer to match it. Same reasoning as above.
    1072           8 :     Value *ActualReplacement = Replacement;
    1073           8 :     if (Replacement->getType() != ToReplace->getType()) {
    1074           0 :       ActualReplacement =
    1075           0 :           Builder.CreateBitCast(Replacement, ToReplace->getType());
    1076             :     }
    1077           8 :     ToReplace->replaceAllUsesWith(ActualReplacement);
    1078           8 :     ToReplace->eraseFromParent();
    1079             : 
    1080           8 :     MadeChange = true;
    1081             :   }
    1082          18 :   return MadeChange;
    1083             : }
    1084             : 
    1085             : // Turns this:
    1086             : //
    1087             : // %base = ...
    1088             : // %ptr = gep %base + 15
    1089             : // %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr)
    1090             : // %base' = relocate(%tok, i32 4, i32 4)
    1091             : // %ptr' = relocate(%tok, i32 4, i32 5)
    1092             : // %val = load %ptr'
    1093             : //
    1094             : // into this:
    1095             : //
    1096             : // %base = ...
    1097             : // %ptr = gep %base + 15
    1098             : // %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr)
    1099             : // %base' = gc.relocate(%tok, i32 4, i32 4)
    1100             : // %ptr' = gep %base' + 15
    1101             : // %val = load %ptr'
    1102          75 : bool CodeGenPrepare::simplifyOffsetableRelocate(Instruction &I) {
    1103          75 :   bool MadeChange = false;
    1104         150 :   SmallVector<GCRelocateInst *, 2> AllRelocateCalls;
    1105             : 
    1106         429 :   for (auto *U : I.users())
    1107         102 :     if (GCRelocateInst *Relocate = dyn_cast<GCRelocateInst>(U))
    1108             :       // Collect all the relocate calls associated with a statepoint
    1109          78 :       AllRelocateCalls.push_back(Relocate);
    1110             : 
    1111             :   // We need atleast one base pointer relocation + one derived pointer
    1112             :   // relocation to mangle
    1113          75 :   if (AllRelocateCalls.size() < 2)
    1114             :     return false;
    1115             : 
    1116             :   // RelocateInstMap is a mapping from the base relocate instruction to the
    1117             :   // corresponding derived relocate instructions
    1118          25 :   DenseMap<GCRelocateInst *, SmallVector<GCRelocateInst *, 2>> RelocateInstMap;
    1119          25 :   computeBaseDerivedRelocateMap(AllRelocateCalls, RelocateInstMap);
    1120          25 :   if (RelocateInstMap.empty())
    1121             :     return false;
    1122             : 
    1123          72 :   for (auto &Item : RelocateInstMap)
    1124             :     // Item.first is the RelocatedBase to offset against
    1125             :     // Item.second is the vector of Targets to replace
    1126          18 :     MadeChange = simplifyRelocatesOffABase(Item.first, Item.second);
    1127             :   return MadeChange;
    1128             : }
    1129             : 
    1130             : /// SinkCast - Sink the specified cast instruction into its user blocks
    1131      205482 : static bool SinkCast(CastInst *CI) {
    1132      205482 :   BasicBlock *DefBB = CI->getParent();
    1133             : 
    1134             :   /// InsertedCasts - Only insert a cast in each block once.
    1135      410964 :   DenseMap<BasicBlock*, CastInst*> InsertedCasts;
    1136             : 
    1137      205482 :   bool MadeChange = false;
    1138      616446 :   for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end();
    1139      463021 :        UI != E; ) {
    1140      257539 :     Use &TheUse = UI.getUse();
    1141      515078 :     Instruction *User = cast<Instruction>(*UI);
    1142             : 
    1143             :     // Figure out which BB this cast is used in.  For PHI's this is the
    1144             :     // appropriate predecessor block.
    1145      257539 :     BasicBlock *UserBB = User->getParent();
    1146        3958 :     if (PHINode *PN = dyn_cast<PHINode>(User)) {
    1147        3958 :       UserBB = PN->getIncomingBlock(TheUse);
    1148             :     }
    1149             : 
    1150             :     // Preincrement use iterator so we don't invalidate it.
    1151      257539 :     ++UI;
    1152             : 
    1153             :     // The first insertion point of a block containing an EH pad is after the
    1154             :     // pad.  If the pad is the user, we cannot sink the cast past the pad.
    1155      257539 :     if (User->isEHPad())
    1156      212547 :       continue;
    1157             : 
    1158             :     // If the block selected to receive the cast is an EH pad that does not
    1159             :     // allow non-PHI instructions before the terminator, we can't sink the
    1160             :     // cast.
    1161      772614 :     if (UserBB->getTerminator()->isEHPad())
    1162           0 :       continue;
    1163             : 
    1164             :     // If this user is in the same block as the cast, don't change the cast.
    1165      470083 :     if (UserBB == DefBB) continue;
    1166             : 
    1167             :     // If we have already inserted a cast into this block, use it.
    1168       44993 :     CastInst *&InsertedCast = InsertedCasts[UserBB];
    1169             : 
    1170       44993 :     if (!InsertedCast) {
    1171       86118 :       BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
    1172             :       assert(InsertPt != UserBB->end());
    1173      129177 :       InsertedCast = CastInst::Create(CI->getOpcode(), CI->getOperand(0),
    1174       43059 :                                       CI->getType(), "", &*InsertPt);
    1175             :     }
    1176             : 
    1177             :     // Replace a use of the cast with a use of the new cast.
    1178       89986 :     TheUse = InsertedCast;
    1179       44993 :     MadeChange = true;
    1180       44993 :     ++NumCastUses;
    1181             :   }
    1182             : 
    1183             :   // If we removed all uses, nuke the cast.
    1184      410964 :   if (CI->use_empty()) {
    1185        6758 :     CI->eraseFromParent();
    1186        6758 :     MadeChange = true;
    1187             :   }
    1188             : 
    1189      410964 :   return MadeChange;
    1190             : }
    1191             : 
    1192             : /// If the specified cast instruction is a noop copy (e.g. it's casting from
    1193             : /// one pointer type to another, i32->i8 on PPC), sink it into user blocks to
    1194             : /// reduce the number of virtual registers that must be created and coalesced.
    1195             : ///
    1196             : /// Return true if any changes are made.
    1197      264837 : static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI,
    1198             :                                        const DataLayout &DL) {
    1199             :   // Sink only "cheap" (or nop) address-space casts.  This is a weaker condition
    1200             :   // than sinking only nop casts, but is helpful on some platforms.
    1201         275 :   if (auto *ASC = dyn_cast<AddrSpaceCastInst>(CI)) {
    1202         825 :     if (!TLI.isCheapAddrSpaceCast(ASC->getSrcAddressSpace(),
    1203         275 :                                   ASC->getDestAddressSpace()))
    1204             :       return false;
    1205             :   }
    1206             : 
    1207             :   // If this is a noop copy,
    1208      529378 :   EVT SrcVT = TLI.getValueType(DL, CI->getOperand(0)->getType());
    1209      264689 :   EVT DstVT = TLI.getValueType(DL, CI->getType());
    1210             : 
    1211             :   // This is an fp<->int conversion?
    1212      264689 :   if (SrcVT.isInteger() != DstVT.isInteger())
    1213             :     return false;
    1214             : 
    1215             :   // If this is an extension, it will be a zero or sign extension, which
    1216             :   // isn't a noop.
    1217      252316 :   if (SrcVT.bitsLT(DstVT)) return false;
    1218             : 
    1219             :   // If these values will be promoted, find out what they will be promoted
    1220             :   // to.  This helps us consider truncates on PPC as noop copies when they
    1221             :   // are.
    1222      444886 :   if (TLI.getTypeAction(CI->getContext(), SrcVT) ==
    1223             :       TargetLowering::TypePromoteInteger)
    1224        2516 :     SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT);
    1225      444886 :   if (TLI.getTypeAction(CI->getContext(), DstVT) ==
    1226             :       TargetLowering::TypePromoteInteger)
    1227        7418 :     DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT);
    1228             : 
    1229             :   // If, after promotion, these are the same types, this is a noop copy.
    1230      222484 :   if (SrcVT != DstVT)
    1231             :     return false;
    1232             : 
    1233      202316 :   return SinkCast(CI);
    1234             : }
    1235             : 
    1236             : /// Try to combine CI into a call to the llvm.uadd.with.overflow intrinsic if
    1237             : /// possible.
    1238             : ///
    1239             : /// Return true if any changes were made.
    1240       76972 : static bool CombineUAddWithOverflow(CmpInst *CI) {
    1241             :   Value *A, *B;
    1242             :   Instruction *AddI;
    1243      153944 :   if (!match(CI,
    1244      384860 :              m_UAddWithOverflow(m_Value(A), m_Value(B), m_Instruction(AddI))))
    1245             :     return false;
    1246             : 
    1247          47 :   Type *Ty = AddI->getType();
    1248          94 :   if (!isa<IntegerType>(Ty))
    1249             :     return false;
    1250             : 
    1251             :   // We don't want to move around uses of condition values this late, so we we
    1252             :   // check if it is legal to create the call to the intrinsic in the basic
    1253             :   // block containing the icmp:
    1254             : 
    1255          54 :   if (AddI->getParent() != CI->getParent() && !AddI->hasOneUse())
    1256             :     return false;
    1257             : 
    1258             : #ifndef NDEBUG
    1259             :   // Someday m_UAddWithOverflow may get smarter, but this is a safe assumption
    1260             :   // for now:
    1261             :   if (AddI->hasOneUse())
    1262             :     assert(*AddI->user_begin() == CI && "expected!");
    1263             : #endif
    1264             : 
    1265          82 :   Module *M = CI->getModule();
    1266          41 :   Value *F = Intrinsic::getDeclaration(M, Intrinsic::uadd_with_overflow, Ty);
    1267             : 
    1268          82 :   auto *InsertPt = AddI->hasOneUse() ? CI : AddI;
    1269             : 
    1270             :   auto *UAddWithOverflow =
    1271         123 :       CallInst::Create(F, {A, B}, "uadd.overflow", InsertPt);
    1272          82 :   auto *UAdd = ExtractValueInst::Create(UAddWithOverflow, 0, "uadd", InsertPt);
    1273             :   auto *Overflow =
    1274          82 :       ExtractValueInst::Create(UAddWithOverflow, 1, "overflow", InsertPt);
    1275             : 
    1276          41 :   CI->replaceAllUsesWith(Overflow);
    1277          41 :   AddI->replaceAllUsesWith(UAdd);
    1278          41 :   CI->eraseFromParent();
    1279          41 :   AddI->eraseFromParent();
    1280          41 :   return true;
    1281             : }
    1282             : 
    1283             : /// Sink the given CmpInst into user blocks to reduce the number of virtual
    1284             : /// registers that must be created and coalesced. This is a clear win except on
    1285             : /// targets with multiple condition code registers (PowerPC), where it might
    1286             : /// lose; some adjustment may be wanted there.
    1287             : ///
    1288             : /// Return true if any changes are made.
    1289       77853 : static bool SinkCmpExpression(CmpInst *CI, const TargetLowering *TLI) {
    1290       77853 :   BasicBlock *DefBB = CI->getParent();
    1291             : 
    1292             :   // Avoid sinking soft-FP comparisons, since this can move them into a loop.
    1293       77942 :   if (TLI && TLI->useSoftFloat() && isa<FCmpInst>(CI))
    1294             :     return false;
    1295             : 
    1296             :   // Only insert a cmp in each block once.
    1297       77808 :   DenseMap<BasicBlock*, CmpInst*> InsertedCmps;
    1298             : 
    1299       77808 :   bool MadeChange = false;
    1300      233424 :   for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end();
    1301      193734 :        UI != E; ) {
    1302      115926 :     Use &TheUse = UI.getUse();
    1303      231852 :     Instruction *User = cast<Instruction>(*UI);
    1304             : 
    1305             :     // Preincrement use iterator so we don't invalidate it.
    1306      115926 :     ++UI;
    1307             : 
    1308             :     // Don't bother for PHI nodes.
    1309      231852 :     if (isa<PHINode>(User))
    1310      115461 :       continue;
    1311             : 
    1312             :     // Figure out which BB this cmp is used in.
    1313      115461 :     BasicBlock *UserBB = User->getParent();
    1314             : 
    1315             :     // If this user is in the same block as the cmp, don't change the cmp.
    1316      115461 :     if (UserBB == DefBB) continue;
    1317             : 
    1318             :     // If we have already inserted a cmp into this block, use it.
    1319         930 :     CmpInst *&InsertedCmp = InsertedCmps[UserBB];
    1320             : 
    1321         930 :     if (!InsertedCmp) {
    1322        1560 :       BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
    1323             :       assert(InsertPt != UserBB->end());
    1324         780 :       InsertedCmp =
    1325        4680 :           CmpInst::Create(CI->getOpcode(), CI->getPredicate(),
    1326         780 :                           CI->getOperand(0), CI->getOperand(1), "", &*InsertPt);
    1327             :       // Propagate the debug info.
    1328        3120 :       InsertedCmp->setDebugLoc(CI->getDebugLoc());
    1329             :     }
    1330             : 
    1331             :     // Replace a use of the cmp with a use of the new cmp.
    1332        1860 :     TheUse = InsertedCmp;
    1333         930 :     MadeChange = true;
    1334         930 :     ++NumCmpUses;
    1335             :   }
    1336             : 
    1337             :   // If we removed all uses, nuke the cmp.
    1338      155616 :   if (CI->use_empty()) {
    1339         584 :     CI->eraseFromParent();
    1340         584 :     MadeChange = true;
    1341             :   }
    1342             : 
    1343       77808 :   return MadeChange;
    1344             : }
    1345             : 
    1346       77853 : static bool OptimizeCmpExpression(CmpInst *CI, const TargetLowering *TLI) {
    1347       77853 :   if (SinkCmpExpression(CI, TLI))
    1348             :     return true;
    1349             : 
    1350       76972 :   if (CombineUAddWithOverflow(CI))
    1351             :     return true;
    1352             : 
    1353       76931 :   return false;
    1354             : }
    1355             : 
    1356             : /// Duplicate and sink the given 'and' instruction into user blocks where it is
    1357             : /// used in a compare to allow isel to generate better code for targets where
    1358             : /// this operation can be combined.
    1359             : ///
    1360             : /// Return true if any changes are made.
    1361       13320 : static bool sinkAndCmp0Expression(Instruction *AndI,
    1362             :                                   const TargetLowering &TLI,
    1363             :                                   SetOfInstrs &InsertedInsts) {
    1364             :   // Double-check that we're not trying to optimize an instruction that was
    1365             :   // already optimized by some other part of this pass.
    1366             :   assert(!InsertedInsts.count(AndI) &&
    1367             :          "Attempting to optimize already optimized and instruction");
    1368             :   (void) InsertedInsts;
    1369             : 
    1370             :   // Nothing to do for single use in same basic block.
    1371       38498 :   if (AndI->hasOneUse() &&
    1372       47652 :       AndI->getParent() == cast<Instruction>(*AndI->user_begin())->getParent())
    1373             :     return false;
    1374             : 
    1375             :   // Try to avoid cases where sinking/duplicating is likely to increase register
    1376             :   // pressure.
    1377        5037 :   if (!isa<ConstantInt>(AndI->getOperand(0)) &&
    1378        3898 :       !isa<ConstantInt>(AndI->getOperand(1)) &&
    1379        4375 :       AndI->getOperand(0)->hasOneUse() && AndI->getOperand(1)->hasOneUse())
    1380             :     return false;
    1381             : 
    1382        2941 :   for (auto *U : AndI->users()) {
    1383        1429 :     Instruction *User = cast<Instruction>(U);
    1384             : 
    1385             :     // Only sink for and mask feeding icmp with 0.
    1386        2858 :     if (!isa<ICmpInst>(User))
    1387             :       return false;
    1388             : 
    1389         619 :     auto *CmpC = dyn_cast<ConstantInt>(User->getOperand(1));
    1390         103 :     if (!CmpC || !CmpC->isZero())
    1391             :       return false;
    1392             :   }
    1393             : 
    1394          83 :   if (!TLI.isMaskAndCmp0FoldingBeneficial(*AndI))
    1395             :     return false;
    1396             : 
    1397             :   DEBUG(dbgs() << "found 'and' feeding only icmp 0;\n");
    1398             :   DEBUG(AndI->getParent()->dump());
    1399             : 
    1400             :   // Push the 'and' into the same block as the icmp 0.  There should only be
    1401             :   // one (icmp (and, 0)) in each block, since CSE/GVN should have removed any
    1402             :   // others, so we don't need to keep track of which BBs we insert into.
    1403         126 :   for (Value::user_iterator UI = AndI->user_begin(), E = AndI->user_end();
    1404          75 :        UI != E; ) {
    1405          33 :     Use &TheUse = UI.getUse();
    1406          66 :     Instruction *User = cast<Instruction>(*UI);
    1407             : 
    1408             :     // Preincrement use iterator so we don't invalidate it.
    1409          33 :     ++UI;
    1410             : 
    1411             :     DEBUG(dbgs() << "sinking 'and' use: " << *User << "\n");
    1412             : 
    1413             :     // Keep the 'and' in the same place if the use is already in the same block.
    1414             :     Instruction *InsertPt =
    1415          33 :         User->getParent() == AndI->getParent() ? AndI : User;
    1416             :     Instruction *InsertedAnd =
    1417         132 :         BinaryOperator::Create(Instruction::And, AndI->getOperand(0),
    1418          33 :                                AndI->getOperand(1), "", InsertPt);
    1419             :     // Propagate the debug info.
    1420         132 :     InsertedAnd->setDebugLoc(AndI->getDebugLoc());
    1421             : 
    1422             :     // Replace a use of the 'and' with a use of the new 'and'.
    1423          33 :     TheUse = InsertedAnd;
    1424             :     ++NumAndUses;
    1425             :     DEBUG(User->getParent()->dump());
    1426             :   }
    1427             : 
    1428             :   // We removed all uses, nuke the and.
    1429          42 :   AndI->eraseFromParent();
    1430             :   return true;
    1431             : }
    1432             : 
    1433             : /// Check if the candidates could be combined with a shift instruction, which
    1434             : /// includes:
    1435             : /// 1. Truncate instruction
    1436             : /// 2. And instruction and the imm is a mask of the low bits:
    1437             : /// imm & (imm+1) == 0
    1438        1135 : static bool isExtractBitsCandidateUse(Instruction *User) {
    1439        2270 :   if (!isa<TruncInst>(User)) {
    1440        2246 :     if (User->getOpcode() != Instruction::And ||
    1441         488 :         !isa<ConstantInt>(User->getOperand(1)))
    1442             :       return false;
    1443             : 
    1444         940 :     const APInt &Cimm = cast<ConstantInt>(User->getOperand(1))->getValue();
    1445             : 
    1446        1645 :     if ((Cimm & (Cimm + 1)).getBoolValue())
    1447             :       return false;
    1448             :   }
    1449             :   return true;
    1450             : }
    1451             : 
    1452             : /// Sink both shift and truncate instruction to the use of truncate's BB.
    1453             : static bool
    1454          39 : SinkShiftAndTruncate(BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI,
    1455             :                      DenseMap<BasicBlock *, BinaryOperator *> &InsertedShifts,
    1456             :                      const TargetLowering &TLI, const DataLayout &DL) {
    1457          39 :   BasicBlock *UserBB = User->getParent();
    1458          78 :   DenseMap<BasicBlock *, CastInst *> InsertedTruncs;
    1459          39 :   TruncInst *TruncI = dyn_cast<TruncInst>(User);
    1460          39 :   bool MadeChange = false;
    1461             : 
    1462          78 :   for (Value::user_iterator TruncUI = TruncI->user_begin(),
    1463          78 :                             TruncE = TruncI->user_end();
    1464          79 :        TruncUI != TruncE;) {
    1465             : 
    1466          40 :     Use &TruncTheUse = TruncUI.getUse();
    1467          80 :     Instruction *TruncUser = cast<Instruction>(*TruncUI);
    1468             :     // Preincrement use iterator so we don't invalidate it.
    1469             : 
    1470          40 :     ++TruncUI;
    1471             : 
    1472          80 :     int ISDOpcode = TLI.InstructionOpcodeToISD(TruncUser->getOpcode());
    1473          40 :     if (!ISDOpcode)
    1474          53 :       continue;
    1475             : 
    1476             :     // If the use is actually a legal node, there will not be an
    1477             :     // implicit truncate.
    1478             :     // FIXME: always querying the result type is just an
    1479             :     // approximation; some nodes' legality is determined by the
    1480             :     // operand or other means. There's no good way to find out though.
    1481          25 :     if (TLI.isOperationLegalOrCustom(
    1482             :             ISDOpcode, TLI.getValueType(DL, TruncUser->getType(), true)))
    1483           5 :       continue;
    1484             : 
    1485             :     // Don't bother for PHI nodes.
    1486          40 :     if (isa<PHINode>(TruncUser))
    1487           0 :       continue;
    1488             : 
    1489          20 :     BasicBlock *TruncUserBB = TruncUser->getParent();
    1490             : 
    1491          20 :     if (UserBB == TruncUserBB)
    1492          18 :       continue;
    1493             : 
    1494           2 :     BinaryOperator *&InsertedShift = InsertedShifts[TruncUserBB];
    1495           2 :     CastInst *&InsertedTrunc = InsertedTruncs[TruncUserBB];
    1496             : 
    1497           2 :     if (!InsertedShift && !InsertedTrunc) {
    1498           4 :       BasicBlock::iterator InsertPt = TruncUserBB->getFirstInsertionPt();
    1499             :       assert(InsertPt != TruncUserBB->end());
    1500             :       // Sink the shift
    1501           2 :       if (ShiftI->getOpcode() == Instruction::AShr)
    1502           0 :         InsertedShift = BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI,
    1503           0 :                                                    "", &*InsertPt);
    1504             :       else
    1505           6 :         InsertedShift = BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI,
    1506           2 :                                                    "", &*InsertPt);
    1507             : 
    1508             :       // Sink the trunc
    1509           4 :       BasicBlock::iterator TruncInsertPt = TruncUserBB->getFirstInsertionPt();
    1510           4 :       TruncInsertPt++;
    1511             :       assert(TruncInsertPt != TruncUserBB->end());
    1512             : 
    1513           4 :       InsertedTrunc = CastInst::Create(TruncI->getOpcode(), InsertedShift,
    1514           2 :                                        TruncI->getType(), "", &*TruncInsertPt);
    1515             : 
    1516           2 :       MadeChange = true;
    1517             : 
    1518           4 :       TruncTheUse = InsertedTrunc;
    1519             :     }
    1520             :   }
    1521          78 :   return MadeChange;
    1522             : }
    1523             : 
    1524             : /// Sink the shift *right* instruction into user blocks if the uses could
    1525             : /// potentially be combined with this shift instruction and generate BitExtract
    1526             : /// instruction. It will only be applied if the architecture supports BitExtract
    1527             : /// instruction. Here is an example:
    1528             : /// BB1:
    1529             : ///   %x.extract.shift = lshr i64 %arg1, 32
    1530             : /// BB2:
    1531             : ///   %x.extract.trunc = trunc i64 %x.extract.shift to i16
    1532             : /// ==>
    1533             : ///
    1534             : /// BB2:
    1535             : ///   %x.extract.shift.1 = lshr i64 %arg1, 32
    1536             : ///   %x.extract.trunc = trunc i64 %x.extract.shift.1 to i16
    1537             : ///
    1538             : /// CodeGen will recoginze the pattern in BB2 and generate BitExtract
    1539             : /// instruction.
    1540             : /// Return true if any changes are made.
    1541        1064 : static bool OptimizeExtractBits(BinaryOperator *ShiftI, ConstantInt *CI,
    1542             :                                 const TargetLowering &TLI,
    1543             :                                 const DataLayout &DL) {
    1544        1064 :   BasicBlock *DefBB = ShiftI->getParent();
    1545             : 
    1546             :   /// Only insert instructions in each block once.
    1547        2128 :   DenseMap<BasicBlock *, BinaryOperator *> InsertedShifts;
    1548             : 
    1549        2128 :   bool shiftIsLegal = TLI.isTypeLegal(TLI.getValueType(DL, ShiftI->getType()));
    1550             : 
    1551        1064 :   bool MadeChange = false;
    1552        3192 :   for (Value::user_iterator UI = ShiftI->user_begin(), E = ShiftI->user_end();
    1553        2204 :        UI != E;) {
    1554        1140 :     Use &TheUse = UI.getUse();
    1555        2280 :     Instruction *User = cast<Instruction>(*UI);
    1556             :     // Preincrement use iterator so we don't invalidate it.
    1557        1140 :     ++UI;
    1558             : 
    1559             :     // Don't bother for PHI nodes.
    1560        2280 :     if (isa<PHINode>(User))
    1561        1092 :       continue;
    1562             : 
    1563        1135 :     if (!isExtractBitsCandidateUse(User))
    1564         777 :       continue;
    1565             : 
    1566         358 :     BasicBlock *UserBB = User->getParent();
    1567             : 
    1568         663 :     if (UserBB == DefBB) {
    1569             :       // If the shift and truncate instruction are in the same BB. The use of
    1570             :       // the truncate(TruncUse) may still introduce another truncate if not
    1571             :       // legal. In this case, we would like to sink both shift and truncate
    1572             :       // instruction to the BB of TruncUse.
    1573             :       // for example:
    1574             :       // BB1:
    1575             :       // i64 shift.result = lshr i64 opnd, imm
    1576             :       // trunc.result = trunc shift.result to i16
    1577             :       //
    1578             :       // BB2:
    1579             :       //   ----> We will have an implicit truncate here if the architecture does
    1580             :       //   not have i16 compare.
    1581             :       // cmp i16 trunc.result, opnd2
    1582             :       //
    1583         436 :       if (isa<TruncInst>(User) && shiftIsLegal
    1584             :           // If the type of the truncate is legal, no trucate will be
    1585             :           // introduced in other basic blocks.
    1586         305 :           &&
    1587          95 :           (!TLI.isTypeLegal(TLI.getValueType(DL, User->getType()))))
    1588          39 :         MadeChange =
    1589             :             SinkShiftAndTruncate(ShiftI, User, CI, InsertedShifts, TLI, DL);
    1590             : 
    1591         305 :       continue;
    1592             :     }
    1593             :     // If we have already inserted a shift into this block, use it.
    1594          53 :     BinaryOperator *&InsertedShift = InsertedShifts[UserBB];
    1595             : 
    1596          53 :     if (!InsertedShift) {
    1597         106 :       BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
    1598             :       assert(InsertPt != UserBB->end());
    1599             : 
    1600          53 :       if (ShiftI->getOpcode() == Instruction::AShr)
    1601          24 :         InsertedShift = BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI,
    1602           8 :                                                    "", &*InsertPt);
    1603             :       else
    1604         135 :         InsertedShift = BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI,
    1605          45 :                                                    "", &*InsertPt);
    1606             : 
    1607             :       MadeChange = true;
    1608             :     }
    1609             : 
    1610             :     // Replace a use of the shift with a use of the new shift.
    1611         106 :     TheUse = InsertedShift;
    1612             :   }
    1613             : 
    1614             :   // If we removed all uses, nuke the shift.
    1615        2128 :   if (ShiftI->use_empty())
    1616          25 :     ShiftI->eraseFromParent();
    1617             : 
    1618        2128 :   return MadeChange;
    1619             : }
    1620             : 
    1621             : /// If counting leading or trailing zeros is an expensive operation and a zero
    1622             : /// input is defined, add a check for zero to avoid calling the intrinsic.
    1623             : ///
    1624             : /// We want to transform:
    1625             : ///     %z = call i64 @llvm.cttz.i64(i64 %A, i1 false)
    1626             : ///
    1627             : /// into:
    1628             : ///   entry:
    1629             : ///     %cmpz = icmp eq i64 %A, 0
    1630             : ///     br i1 %cmpz, label %cond.end, label %cond.false
    1631             : ///   cond.false:
    1632             : ///     %z = call i64 @llvm.cttz.i64(i64 %A, i1 true)
    1633             : ///     br label %cond.end
    1634             : ///   cond.end:
    1635             : ///     %ctz = phi i64 [ 64, %entry ], [ %z, %cond.false ]
    1636             : ///
    1637             : /// If the transform is performed, return true and set ModifiedDT to true.
    1638        1376 : static bool despeculateCountZeros(IntrinsicInst *CountZeros,
    1639             :                                   const TargetLowering *TLI,
    1640             :                                   const DataLayout *DL,
    1641             :                                   bool &ModifiedDT) {
    1642        1376 :   if (!TLI || !DL)
    1643             :     return false;
    1644             : 
    1645             :   // If a zero input is undefined, it doesn't make sense to despeculate that.
    1646        4128 :   if (match(CountZeros->getOperand(1), m_One()))
    1647             :     return false;
    1648             : 
    1649             :   // If it's cheap to speculate, there's nothing to do.
    1650         644 :   auto IntrinsicID = CountZeros->getIntrinsicID();
    1651         644 :   if ((IntrinsicID == Intrinsic::cttz && TLI->isCheapToSpeculateCttz()) ||
    1652         388 :       (IntrinsicID == Intrinsic::ctlz && TLI->isCheapToSpeculateCtlz()))
    1653             :     return false;
    1654             : 
    1655             :   // Only handle legal scalar cases. Anything else requires too much work.
    1656         381 :   Type *Ty = CountZeros->getType();
    1657         381 :   unsigned SizeInBits = Ty->getPrimitiveSizeInBits();
    1658         381 :   if (Ty->isVectorTy() || SizeInBits > DL->getLargestLegalIntTypeSizeInBits())
    1659             :     return false;
    1660             : 
    1661             :   // The intrinsic will be sunk behind a compare against zero and branch.
    1662          27 :   BasicBlock *StartBlock = CountZeros->getParent();
    1663          54 :   BasicBlock *CallBlock = StartBlock->splitBasicBlock(CountZeros, "cond.false");
    1664             : 
    1665             :   // Create another block after the count zero intrinsic. A PHI will be added
    1666             :   // in this block to select the result of the intrinsic or the bit-width
    1667             :   // constant if the input to the intrinsic is zero.
    1668          54 :   BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(CountZeros));
    1669          27 :   BasicBlock *EndBlock = CallBlock->splitBasicBlock(SplitPt, "cond.end");
    1670             : 
    1671             :   // Set up a builder to create a compare, conditional branch, and PHI.
    1672          54 :   IRBuilder<> Builder(CountZeros->getContext());
    1673          27 :   Builder.SetInsertPoint(StartBlock->getTerminator());
    1674         108 :   Builder.SetCurrentDebugLocation(CountZeros->getDebugLoc());
    1675             : 
    1676             :   // Replace the unconditional branch that was created by the first split with
    1677             :   // a compare against zero and a conditional branch.
    1678          27 :   Value *Zero = Constant::getNullValue(Ty);
    1679          81 :   Value *Cmp = Builder.CreateICmpEQ(CountZeros->getOperand(0), Zero, "cmpz");
    1680          27 :   Builder.CreateCondBr(Cmp, EndBlock, CallBlock);
    1681          27 :   StartBlock->getTerminator()->eraseFromParent();
    1682             : 
    1683             :   // Create a PHI in the end block to select either the output of the intrinsic
    1684             :   // or the bit width of the operand.
    1685          27 :   Builder.SetInsertPoint(&EndBlock->front());
    1686          27 :   PHINode *PN = Builder.CreatePHI(Ty, 2, "ctz");
    1687          27 :   CountZeros->replaceAllUsesWith(PN);
    1688         108 :   Value *BitWidth = Builder.getInt(APInt(SizeInBits, SizeInBits));
    1689          27 :   PN->addIncoming(BitWidth, StartBlock);
    1690          27 :   PN->addIncoming(CountZeros, CallBlock);
    1691             : 
    1692             :   // We are explicitly handling the zero case, so we can set the intrinsic's
    1693             :   // undefined zero argument to 'true'. This will also prevent reprocessing the
    1694             :   // intrinsic; we only despeculate when a zero input is defined.
    1695          81 :   CountZeros->setArgOperand(1, Builder.getTrue());
    1696          27 :   ModifiedDT = true;
    1697          27 :   return true;
    1698             : }
    1699             : 
    1700             : namespace {
    1701             : 
    1702             : // This class provides helper functions to expand a memcmp library call into an
    1703             : // inline expansion.
    1704         528 : class MemCmpExpansion {
    1705             :   struct ResultBlock {
    1706             :     BasicBlock *BB = nullptr;
    1707             :     PHINode *PhiSrc1 = nullptr;
    1708             :     PHINode *PhiSrc2 = nullptr;
    1709             : 
    1710         176 :     ResultBlock() = default;
    1711             :   };
    1712             : 
    1713             :   CallInst *CI;
    1714             :   ResultBlock ResBlock;
    1715             :   unsigned MaxLoadSize;
    1716             :   unsigned NumBlocks;
    1717             :   unsigned NumBlocksNonOneByte;
    1718             :   unsigned NumLoadsPerBlock;
    1719             :   std::vector<BasicBlock *> LoadCmpBlocks;
    1720             :   BasicBlock *EndBlock;
    1721             :   PHINode *PhiRes;
    1722             :   bool IsUsedForZeroCmp;
    1723             :   const DataLayout &DL;
    1724             :   IRBuilder<> Builder;
    1725             : 
    1726             :   unsigned calculateNumBlocks(unsigned Size);
    1727             :   void createLoadCmpBlocks();
    1728             :   void createResultBlock();
    1729             :   void setupResultBlockPHINodes();
    1730             :   void setupEndBlockPHINodes();
    1731             :   void emitLoadCompareBlock(unsigned Index, unsigned LoadSize,
    1732             :                             unsigned GEPIndex);
    1733             :   Value *getCompareLoadPairs(unsigned Index, unsigned Size,
    1734             :                              unsigned &NumBytesProcessed);
    1735             :   void emitLoadCompareBlockMultipleLoads(unsigned Index, unsigned Size,
    1736             :                                          unsigned &NumBytesProcessed);
    1737             :   void emitLoadCompareByteBlock(unsigned Index, unsigned GEPIndex);
    1738             :   void emitMemCmpResultBlock();
    1739             :   Value *getMemCmpExpansionZeroCase(unsigned Size);
    1740             :   Value *getMemCmpEqZeroOneBlock(unsigned Size);
    1741             :   Value *getMemCmpOneBlock(unsigned Size);
    1742             :   unsigned getLoadSize(unsigned Size);
    1743             :   unsigned getNumLoads(unsigned Size);
    1744             : 
    1745             : public:
    1746             :   MemCmpExpansion(CallInst *CI, uint64_t Size, unsigned MaxLoadSize,
    1747             :                   unsigned NumLoadsPerBlock, const DataLayout &DL);
    1748             : 
    1749             :   Value *getMemCmpExpansion(uint64_t Size);
    1750             : };
    1751             : 
    1752             : } // end anonymous namespace
    1753             : 
    1754             : // Initialize the basic block structure required for expansion of memcmp call
    1755             : // with given maximum load size and memcmp size parameter.
    1756             : // This structure includes:
    1757             : // 1. A list of load compare blocks - LoadCmpBlocks.
    1758             : // 2. An EndBlock, split from original instruction point, which is the block to
    1759             : // return from.
    1760             : // 3. ResultBlock, block to branch to for early exit when a
    1761             : // LoadCmpBlock finds a difference.
    1762         176 : MemCmpExpansion::MemCmpExpansion(CallInst *CI, uint64_t Size,
    1763             :                                  unsigned MaxLoadSize, unsigned LoadsPerBlock,
    1764         176 :                                  const DataLayout &TheDataLayout)
    1765             :     : CI(CI), MaxLoadSize(MaxLoadSize), NumLoadsPerBlock(LoadsPerBlock),
    1766         704 :       DL(TheDataLayout), Builder(CI) {
    1767             :   // A memcmp with zero-comparison with only one block of load and compare does
    1768             :   // not need to set up any extra blocks. This case could be handled in the DAG,
    1769             :   // but since we have all of the machinery to flexibly expand any memcpy here,
    1770             :   // we choose to handle this case too to avoid fragmented lowering.
    1771         176 :   IsUsedForZeroCmp = isOnlyUsedInZeroEqualityComparison(CI);
    1772         176 :   NumBlocks = calculateNumBlocks(Size);
    1773         176 :   if ((!IsUsedForZeroCmp && NumLoadsPerBlock != 1) || NumBlocks != 1) {
    1774          97 :     BasicBlock *StartBlock = CI->getParent();
    1775         194 :     EndBlock = StartBlock->splitBasicBlock(CI, "endblock");
    1776          97 :     setupEndBlockPHINodes();
    1777          97 :     createResultBlock();
    1778             : 
    1779             :     // If return value of memcmp is not used in a zero equality, we need to
    1780             :     // calculate which source was larger. The calculation requires the
    1781             :     // two loaded source values of each load compare block.
    1782             :     // These will be saved in the phi nodes created by setupResultBlockPHINodes.
    1783          97 :     if (!IsUsedForZeroCmp)
    1784          45 :       setupResultBlockPHINodes();
    1785             : 
    1786             :     // Create the number of required load compare basic blocks.
    1787          97 :     createLoadCmpBlocks();
    1788             : 
    1789             :     // Update the terminator added by splitBasicBlock to branch to the first
    1790             :     // LoadCmpBlock.
    1791          97 :     StartBlock->getTerminator()->setSuccessor(0, LoadCmpBlocks[0]);
    1792             :   }
    1793             : 
    1794         704 :   Builder.SetCurrentDebugLocation(CI->getDebugLoc());
    1795         176 : }
    1796             : 
    1797          97 : void MemCmpExpansion::createLoadCmpBlocks() {
    1798         296 :   for (unsigned i = 0; i < NumBlocks; i++) {
    1799         796 :     BasicBlock *BB = BasicBlock::Create(CI->getContext(), "loadbb",
    1800         199 :                                         EndBlock->getParent(), EndBlock);
    1801         199 :     LoadCmpBlocks.push_back(BB);
    1802             :   }
    1803          97 : }
    1804             : 
    1805          97 : void MemCmpExpansion::createResultBlock() {
    1806         291 :   ResBlock.BB = BasicBlock::Create(CI->getContext(), "res_block",
    1807             :                                    EndBlock->getParent(), EndBlock);
    1808          97 : }
    1809             : 
    1810             : // This function creates the IR instructions for loading and comparing 1 byte.
    1811             : // It loads 1 byte from each source of the memcmp parameters with the given
    1812             : // GEPIndex. It then subtracts the two loaded values and adds this result to the
    1813             : // final phi node for selecting the memcmp result.
    1814          23 : void MemCmpExpansion::emitLoadCompareByteBlock(unsigned Index,
    1815             :                                                unsigned GEPIndex) {
    1816          46 :   Value *Source1 = CI->getArgOperand(0);
    1817          46 :   Value *Source2 = CI->getArgOperand(1);
    1818             : 
    1819          69 :   Builder.SetInsertPoint(LoadCmpBlocks[Index]);
    1820          23 :   Type *LoadSizeType = Type::getInt8Ty(CI->getContext());
    1821             :   // Cast source to LoadSizeType*.
    1822          23 :   if (Source1->getType() != LoadSizeType)
    1823          69 :     Source1 = Builder.CreateBitCast(Source1, LoadSizeType->getPointerTo());
    1824          23 :   if (Source2->getType() != LoadSizeType)
    1825          69 :     Source2 = Builder.CreateBitCast(Source2, LoadSizeType->getPointerTo());
    1826             : 
    1827             :   // Get the base address using the GEPIndex.
    1828          23 :   if (GEPIndex != 0) {
    1829          69 :     Source1 = Builder.CreateGEP(LoadSizeType, Source1,
    1830          23 :                                 ConstantInt::get(LoadSizeType, GEPIndex));
    1831          69 :     Source2 = Builder.CreateGEP(LoadSizeType, Source2,
    1832          23 :                                 ConstantInt::get(LoadSizeType, GEPIndex));
    1833             :   }
    1834             : 
    1835          46 :   Value *LoadSrc1 = Builder.CreateLoad(LoadSizeType, Source1);
    1836          46 :   Value *LoadSrc2 = Builder.CreateLoad(LoadSizeType, Source2);
    1837             : 
    1838          69 :   LoadSrc1 = Builder.CreateZExt(LoadSrc1, Type::getInt32Ty(CI->getContext()));
    1839          69 :   LoadSrc2 = Builder.CreateZExt(LoadSrc2, Type::getInt32Ty(CI->getContext()));
    1840          46 :   Value *Diff = Builder.CreateSub(LoadSrc1, LoadSrc2);
    1841             : 
    1842          46 :   PhiRes->addIncoming(Diff, LoadCmpBlocks[Index]);
    1843             : 
    1844          46 :   if (Index < (LoadCmpBlocks.size() - 1)) {
    1845             :     // Early exit branch if difference found to EndBlock. Otherwise, continue to
    1846             :     // next LoadCmpBlock,
    1847           0 :     Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_NE, Diff,
    1848           0 :                                     ConstantInt::get(Diff->getType(), 0));
    1849             :     BranchInst *CmpBr =
    1850           0 :         BranchInst::Create(EndBlock, LoadCmpBlocks[Index + 1], Cmp);
    1851           0 :     Builder.Insert(CmpBr);
    1852             :   } else {
    1853             :     // The last block has an unconditional branch to EndBlock.
    1854          46 :     BranchInst *CmpBr = BranchInst::Create(EndBlock);
    1855          46 :     Builder.Insert(CmpBr);
    1856             :   }
    1857          23 : }
    1858             : 
    1859             : unsigned MemCmpExpansion::getNumLoads(unsigned Size) {
    1860         306 :   return (Size / MaxLoadSize) + countPopulation(Size % MaxLoadSize);
    1861             : }
    1862             : 
    1863             : unsigned MemCmpExpansion::getLoadSize(unsigned Size) {
    1864         459 :   return MinAlign(PowerOf2Floor(Size), MaxLoadSize);
    1865             : }
    1866             : 
    1867             : /// Generate an equality comparison for one or more pairs of loaded values.
    1868             : /// This is used in the case where the memcmp() call is compared equal or not
    1869             : /// equal to zero.
    1870         153 : Value *MemCmpExpansion::getCompareLoadPairs(unsigned Index, unsigned Size,
    1871             :                                             unsigned &NumBytesProcessed) {
    1872         612 :   std::vector<Value *> XorList, OrList;
    1873             :   Value *Diff;
    1874             : 
    1875         153 :   unsigned RemainingBytes = Size - NumBytesProcessed;
    1876         306 :   unsigned NumLoadsRemaining = getNumLoads(RemainingBytes);
    1877         306 :   unsigned NumLoads = std::min(NumLoadsRemaining, NumLoadsPerBlock);
    1878             : 
    1879             :   // For a single-block expansion, start inserting before the memcmp call.
    1880         306 :   if (LoadCmpBlocks.empty())
    1881          48 :     Builder.SetInsertPoint(CI);
    1882             :   else
    1883         210 :     Builder.SetInsertPoint(LoadCmpBlocks[Index]);
    1884             : 
    1885             :   Value *Cmp = nullptr;
    1886         459 :   for (unsigned i = 0; i < NumLoads; ++i) {
    1887         306 :     unsigned LoadSize = getLoadSize(RemainingBytes);
    1888         153 :     unsigned GEPIndex = NumBytesProcessed / LoadSize;
    1889         153 :     NumBytesProcessed += LoadSize;
    1890         153 :     RemainingBytes -= LoadSize;
    1891             : 
    1892         153 :     Type *LoadSizeType = IntegerType::get(CI->getContext(), LoadSize * 8);
    1893         153 :     Type *MaxLoadType = IntegerType::get(CI->getContext(), MaxLoadSize * 8);
    1894             :     assert(LoadSize <= MaxLoadSize && "Unexpected load type");
    1895             : 
    1896         306 :     Value *Source1 = CI->getArgOperand(0);
    1897         306 :     Value *Source2 = CI->getArgOperand(1);
    1898             : 
    1899             :     // Cast source to LoadSizeType*.
    1900         153 :     if (Source1->getType() != LoadSizeType)
    1901         459 :       Source1 = Builder.CreateBitCast(Source1, LoadSizeType->getPointerTo());
    1902         153 :     if (Source2->getType() != LoadSizeType)
    1903         459 :       Source2 = Builder.CreateBitCast(Source2, LoadSizeType->getPointerTo());
    1904             : 
    1905             :     // Get the base address using the GEPIndex.
    1906         153 :     if (GEPIndex != 0) {
    1907         159 :       Source1 = Builder.CreateGEP(LoadSizeType, Source1,
    1908          53 :                                   ConstantInt::get(LoadSizeType, GEPIndex));
    1909         159 :       Source2 = Builder.CreateGEP(LoadSizeType, Source2,
    1910          53 :                                   ConstantInt::get(LoadSizeType, GEPIndex));
    1911             :     }
    1912             : 
    1913             :     // Get a constant or load a value for each source address.
    1914         153 :     Value *LoadSrc1 = nullptr;
    1915           4 :     if (auto *Source1C = dyn_cast<Constant>(Source1))
    1916           4 :       LoadSrc1 = ConstantFoldLoadFromConstPtr(Source1C, LoadSizeType, DL);
    1917           4 :     if (!LoadSrc1)
    1918         298 :       LoadSrc1 = Builder.CreateLoad(LoadSizeType, Source1);
    1919             : 
    1920         153 :     Value *LoadSrc2 = nullptr;
    1921          42 :     if (auto *Source2C = dyn_cast<Constant>(Source2))
    1922          42 :       LoadSrc2 = ConstantFoldLoadFromConstPtr(Source2C, LoadSizeType, DL);
    1923          42 :     if (!LoadSrc2)
    1924         226 :       LoadSrc2 = Builder.CreateLoad(LoadSizeType, Source2);
    1925             : 
    1926         153 :     if (NumLoads != 1) {
    1927           0 :       if (LoadSizeType != MaxLoadType) {
    1928           0 :         LoadSrc1 = Builder.CreateZExt(LoadSrc1, MaxLoadType);
    1929           0 :         LoadSrc2 = Builder.CreateZExt(LoadSrc2, MaxLoadType);
    1930             :       }
    1931             :       // If we have multiple loads per block, we need to generate a composite
    1932             :       // comparison using xor+or.
    1933           0 :       Diff = Builder.CreateXor(LoadSrc1, LoadSrc2);
    1934           0 :       Diff = Builder.CreateZExt(Diff, MaxLoadType);
    1935           0 :       XorList.push_back(Diff);
    1936             :     } else {
    1937             :       // If there's only one load per block, we just compare the loaded values.
    1938         459 :       Cmp = Builder.CreateICmpNE(LoadSrc1, LoadSrc2);
    1939             :     }
    1940             :   }
    1941             : 
    1942           0 :   auto pairWiseOr = [&](std::vector<Value *> &InList) -> std::vector<Value *> {
    1943           0 :     std::vector<Value *> OutList;
    1944           0 :     for (unsigned i = 0; i < InList.size() - 1; i = i + 2) {
    1945           0 :       Value *Or = Builder.CreateOr(InList[i], InList[i + 1]);
    1946           0 :       OutList.push_back(Or);
    1947             :     }
    1948           0 :     if (InList.size() % 2 != 0)
    1949           0 :       OutList.push_back(InList.back());
    1950           0 :     return OutList;
    1951         153 :   };
    1952             : 
    1953         153 :   if (!Cmp) {
    1954             :     // Pairwise OR the XOR results.
    1955           0 :     OrList = pairWiseOr(XorList);
    1956             : 
    1957             :     // Pairwise OR the OR results until one result left.
    1958           0 :     while (OrList.size() != 1) {
    1959           0 :       OrList = pairWiseOr(OrList);
    1960             :     }
    1961           0 :     Cmp = Builder.CreateICmpNE(OrList[0], ConstantInt::get(Diff->getType(), 0));
    1962             :   }
    1963             : 
    1964         306 :   return Cmp;
    1965             : }
    1966             : 
    1967         105 : void MemCmpExpansion::emitLoadCompareBlockMultipleLoads(
    1968             :     unsigned Index, unsigned Size, unsigned &NumBytesProcessed) {
    1969         105 :   Value *Cmp = getCompareLoadPairs(Index, Size, NumBytesProcessed);
    1970             : 
    1971         210 :   BasicBlock *NextBB = (Index == (LoadCmpBlocks.size() - 1))
    1972         158 :                            ? EndBlock
    1973         211 :                            : LoadCmpBlocks[Index + 1];
    1974             :   // Early exit branch if difference found to ResultBlock. Otherwise,
    1975             :   // continue to next LoadCmpBlock or EndBlock.
    1976         210 :   BranchInst *CmpBr = BranchInst::Create(ResBlock.BB, NextBB, Cmp);
    1977         210 :   Builder.Insert(CmpBr);
    1978             : 
    1979             :   // Add a phi edge for the last LoadCmpBlock to Endblock with a value of 0
    1980             :   // since early exit to ResultBlock was not taken (no difference was found in
    1981             :   // any of the bytes).
    1982         210 :   if (Index == LoadCmpBlocks.size() - 1) {
    1983          52 :     Value *Zero = ConstantInt::get(Type::getInt32Ty(CI->getContext()), 0);
    1984         104 :     PhiRes->addIncoming(Zero, LoadCmpBlocks[Index]);
    1985             :   }
    1986         105 : }
    1987             : 
    1988             : // This function creates the IR intructions for loading and comparing using the
    1989             : // given LoadSize. It loads the number of bytes specified by LoadSize from each
    1990             : // source of the memcmp parameters. It then does a subtract to see if there was
    1991             : // a difference in the loaded values. If a difference is found, it branches
    1992             : // with an early exit to the ResultBlock for calculating which source was
    1993             : // larger. Otherwise, it falls through to the either the next LoadCmpBlock or
    1994             : // the EndBlock if this is the last LoadCmpBlock. Loading 1 byte is handled with
    1995             : // a special case through emitLoadCompareByteBlock. The special handling can
    1996             : // simply subtract the loaded values and add it to the result phi node.
    1997          94 : void MemCmpExpansion::emitLoadCompareBlock(unsigned Index, unsigned LoadSize,
    1998             :                                            unsigned GEPIndex) {
    1999          94 :   if (LoadSize == 1) {
    2000          23 :     MemCmpExpansion::emitLoadCompareByteBlock(Index, GEPIndex);
    2001          23 :     return;
    2002             :   }
    2003             : 
    2004          71 :   Type *LoadSizeType = IntegerType::get(CI->getContext(), LoadSize * 8);
    2005          71 :   Type *MaxLoadType = IntegerType::get(CI->getContext(), MaxLoadSize * 8);
    2006             :   assert(LoadSize <= MaxLoadSize && "Unexpected load type");
    2007             : 
    2008         142 :   Value *Source1 = CI->getArgOperand(0);
    2009         142 :   Value *Source2 = CI->getArgOperand(1);
    2010             : 
    2011         213 :   Builder.SetInsertPoint(LoadCmpBlocks[Index]);
    2012             :   // Cast source to LoadSizeType*.
    2013          71 :   if (Source1->getType() != LoadSizeType)
    2014         213 :     Source1 = Builder.CreateBitCast(Source1, LoadSizeType->getPointerTo());
    2015          71 :   if (Source2->getType() != LoadSizeType)
    2016         213 :     Source2 = Builder.CreateBitCast(Source2, LoadSizeType->getPointerTo());
    2017             : 
    2018             :   // Get the base address using the GEPIndex.
    2019          71 :   if (GEPIndex != 0) {
    2020         104 :     Source1 = Builder.CreateGEP(LoadSizeType, Source1,
    2021          52 :                                 ConstantInt::get(LoadSizeType, GEPIndex));
    2022          78 :     Source2 = Builder.CreateGEP(LoadSizeType, Source2,
    2023          26 :                                 ConstantInt::get(LoadSizeType, GEPIndex));
    2024             :   }
    2025             : 
    2026             :   // Load LoadSizeType from the base address.
    2027         142 :   Value *LoadSrc1 = Builder.CreateLoad(LoadSizeType, Source1);
    2028         142 :   Value *LoadSrc2 = Builder.CreateLoad(LoadSizeType, Source2);
    2029             : 
    2030          71 :   if (DL.isLittleEndian()) {
    2031         198 :     Function *Bswap = Intrinsic::getDeclaration(CI->getModule(),
    2032          66 :                                                 Intrinsic::bswap, LoadSizeType);
    2033         198 :     LoadSrc1 = Builder.CreateCall(Bswap, LoadSrc1);
    2034         198 :     LoadSrc2 = Builder.CreateCall(Bswap, LoadSrc2);
    2035             :   }
    2036             : 
    2037          71 :   if (LoadSizeType != MaxLoadType) {
    2038          36 :     LoadSrc1 = Builder.CreateZExt(LoadSrc1, MaxLoadType);
    2039          36 :     LoadSrc2 = Builder.CreateZExt(LoadSrc2, MaxLoadType);
    2040             :   }
    2041             : 
    2042             :   // Add the loaded values to the phi nodes for calculating memcmp result only
    2043             :   // if result is not used in a zero equality.
    2044          71 :   if (!IsUsedForZeroCmp) {
    2045         142 :     ResBlock.PhiSrc1->addIncoming(LoadSrc1, LoadCmpBlocks[Index]);
    2046         142 :     ResBlock.PhiSrc2->addIncoming(LoadSrc2, LoadCmpBlocks[Index]);
    2047             :   }
    2048             : 
    2049         142 :   Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_EQ, LoadSrc1, LoadSrc2);
    2050         142 :   BasicBlock *NextBB = (Index == (LoadCmpBlocks.size() - 1))
    2051         120 :                            ? EndBlock
    2052         169 :                            : LoadCmpBlocks[Index + 1];
    2053             :   // Early exit branch if difference found to ResultBlock. Otherwise, continue
    2054             :   // to next LoadCmpBlock or EndBlock.
    2055         142 :   BranchInst *CmpBr = BranchInst::Create(NextBB, ResBlock.BB, Cmp);
    2056         142 :   Builder.Insert(CmpBr);
    2057             : 
    2058             :   // Add a phi edge for the last LoadCmpBlock to Endblock with a value of 0
    2059             :   // since early exit to ResultBlock was not taken (no difference was found in
    2060             :   // any of the bytes).
    2061         142 :   if (Index == LoadCmpBlocks.size() - 1) {
    2062          22 :     Value *Zero = ConstantInt::get(Type::getInt32Ty(CI->getContext()), 0);
    2063          44 :     PhiRes->addIncoming(Zero, LoadCmpBlocks[Index]);
    2064             :   }
    2065             : }
    2066             : 
    2067             : // This function populates the ResultBlock with a sequence to calculate the
    2068             : // memcmp result. It compares the two loaded source values and returns -1 if
    2069             : // src1 < src2 and 1 if src1 > src2.
    2070          97 : void MemCmpExpansion::emitMemCmpResultBlock() {
    2071             :   // Special case: if memcmp result is used in a zero equality, result does not
    2072             :   // need to be calculated and can simply return 1.
    2073          97 :   if (IsUsedForZeroCmp) {
    2074         104 :     BasicBlock::iterator InsertPt = ResBlock.BB->getFirstInsertionPt();
    2075          52 :     Builder.SetInsertPoint(ResBlock.BB, InsertPt);
    2076          52 :     Value *Res = ConstantInt::get(Type::getInt32Ty(CI->getContext()), 1);
    2077          52 :     PhiRes->addIncoming(Res, ResBlock.BB);
    2078         104 :     BranchInst *NewBr = BranchInst::Create(EndBlock);
    2079         104 :     Builder.Insert(NewBr);
    2080             :     return;
    2081             :   }
    2082          90 :   BasicBlock::iterator InsertPt = ResBlock.BB->getFirstInsertionPt();
    2083          45 :   Builder.SetInsertPoint(ResBlock.BB, InsertPt);
    2084             : 
    2085         180 :   Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_ULT, ResBlock.PhiSrc1,
    2086          90 :                                   ResBlock.PhiSrc2);
    2087             : 
    2088             :   Value *Res =
    2089         225 :       Builder.CreateSelect(Cmp, ConstantInt::get(Builder.getInt32Ty(), -1),
    2090         135 :                            ConstantInt::get(Builder.getInt32Ty(), 1));
    2091             : 
    2092          90 :   BranchInst *NewBr = BranchInst::Create(EndBlock);
    2093          90 :   Builder.Insert(NewBr);
    2094          45 :   PhiRes->addIncoming(Res, ResBlock.BB);
    2095             : }
    2096             : 
    2097         176 : unsigned MemCmpExpansion::calculateNumBlocks(unsigned Size) {
    2098         176 :   unsigned NumBlocks = 0;
    2099         176 :   bool HaveOneByteLoad = false;
    2100         176 :   unsigned RemainingSize = Size;
    2101         176 :   unsigned LoadSize = MaxLoadSize;
    2102         714 :   while (RemainingSize) {
    2103         269 :     if (LoadSize == 1)
    2104          47 :       HaveOneByteLoad = true;
    2105         269 :     NumBlocks += RemainingSize / LoadSize;
    2106         269 :     RemainingSize = RemainingSize % LoadSize;
    2107         269 :     LoadSize = LoadSize / 2;
    2108             :   }
    2109         176 :   NumBlocksNonOneByte = HaveOneByteLoad ? (NumBlocks - 1) : NumBlocks;
    2110             : 
    2111         176 :   if (IsUsedForZeroCmp)
    2112         200 :     NumBlocks = NumBlocks / NumLoadsPerBlock +
    2113         100 :                 (NumBlocks % NumLoadsPerBlock != 0 ? 1 : 0);
    2114             : 
    2115         176 :   return NumBlocks;
    2116             : }
    2117             : 
    2118          45 : void MemCmpExpansion::setupResultBlockPHINodes() {
    2119          45 :   Type *MaxLoadType = IntegerType::get(CI->getContext(), MaxLoadSize * 8);
    2120          90 :   Builder.SetInsertPoint(ResBlock.BB);
    2121          45 :   ResBlock.PhiSrc1 =
    2122         135 :       Builder.CreatePHI(MaxLoadType, NumBlocksNonOneByte, "phi.src1");
    2123          45 :   ResBlock.PhiSrc2 =
    2124         135 :       Builder.CreatePHI(MaxLoadType, NumBlocksNonOneByte, "phi.src2");
    2125          45 : }
    2126             : 
    2127          97 : void MemCmpExpansion::setupEndBlockPHINodes() {
    2128         194 :   Builder.SetInsertPoint(&EndBlock->front());
    2129         194 :   PhiRes = Builder.CreatePHI(Type::getInt32Ty(CI->getContext()), 2, "phi.res");
    2130          97 : }
    2131             : 
    2132          52 : Value *MemCmpExpansion::getMemCmpExpansionZeroCase(unsigned Size) {
    2133          52 :   unsigned NumBytesProcessed = 0;
    2134             :   // This loop populates each of the LoadCmpBlocks with the IR sequence to
    2135             :   // handle multiple loads per block.
    2136         157 :   for (unsigned i = 0; i < NumBlocks; ++i)
    2137         105 :     emitLoadCompareBlockMultipleLoads(i, Size, NumBytesProcessed);
    2138             : 
    2139          52 :   emitMemCmpResultBlock();
    2140          52 :   return PhiRes;
    2141             : }
    2142             : 
    2143             : /// A memcmp expansion that compares equality with 0 and only has one block of
    2144             : /// load and compare can bypass the compare, branch, and phi IR that is required
    2145             : /// in the general case.
    2146          48 : Value *MemCmpExpansion::getMemCmpEqZeroOneBlock(unsigned Size) {
    2147          48 :   unsigned NumBytesProcessed = 0;
    2148          48 :   Value *Cmp = getCompareLoadPairs(0, Size, NumBytesProcessed);
    2149         144 :   return Builder.CreateZExt(Cmp, Type::getInt32Ty(CI->getContext()));
    2150             : }
    2151             : 
    2152             : /// A memcmp expansion that only has one block of load and compare can bypass
    2153             : /// the compare, branch, and phi IR that is required in the general case.
    2154          31 : Value *MemCmpExpansion::getMemCmpOneBlock(unsigned Size) {
    2155             :   assert(NumLoadsPerBlock == 1 && "Only handles one load pair per block");
    2156             : 
    2157          31 :   Type *LoadSizeType = IntegerType::get(CI->getContext(), Size * 8);
    2158          62 :   Value *Source1 = CI->getArgOperand(0);
    2159          62 :   Value *Source2 = CI->getArgOperand(1);
    2160             : 
    2161             :   // Cast source to LoadSizeType*.
    2162          31 :   if (Source1->getType() != LoadSizeType)
    2163          93 :     Source1 = Builder.CreateBitCast(Source1, LoadSizeType->getPointerTo());
    2164          31 :   if (Source2->getType() != LoadSizeType)
    2165          93 :     Source2 = Builder.CreateBitCast(Source2, LoadSizeType->getPointerTo());
    2166             : 
    2167             :   // Load LoadSizeType from the base address.
    2168          62 :   Value *LoadSrc1 = Builder.CreateLoad(LoadSizeType, Source1);
    2169          62 :   Value *LoadSrc2 = Builder.CreateLoad(LoadSizeType, Source2);
    2170             : 
    2171          31 :   if (DL.isLittleEndian() && Size != 1) {
    2172          87 :     Function *Bswap = Intrinsic::getDeclaration(CI->getModule(),
    2173          29 :                                                 Intrinsic::bswap, LoadSizeType);
    2174          87 :     LoadSrc1 = Builder.CreateCall(Bswap, LoadSrc1);
    2175          87 :     LoadSrc2 = Builder.CreateCall(Bswap, LoadSrc2);
    2176             :   }
    2177             : 
    2178          31 :   if (Size < 4) {
    2179             :     // The i8 and i16 cases don't need compares. We zext the loaded values and
    2180             :     // subtract them to get the suitable negative, zero, or positive i32 result.
    2181          48 :     LoadSrc1 = Builder.CreateZExt(LoadSrc1, Builder.getInt32Ty());
    2182          48 :     LoadSrc2 = Builder.CreateZExt(LoadSrc2, Builder.getInt32Ty());
    2183          24 :     return Builder.CreateSub(LoadSrc1, LoadSrc2);
    2184             :   }
    2185             : 
    2186             :   // The result of memcmp is negative, zero, or positive, so produce that by
    2187             :   // subtracting 2 extended compare bits: sub (ugt, ult).
    2188             :   // If a target prefers to use selects to get -1/0/1, they should be able
    2189             :   // to transform this later. The inverse transform (going from selects to math)
    2190             :   // may not be possible in the DAG because the selects got converted into
    2191             :   // branches before we got there.
    2192          57 :   Value *CmpUGT = Builder.CreateICmpUGT(LoadSrc1, LoadSrc2);
    2193          57 :   Value *CmpULT = Builder.CreateICmpULT(LoadSrc1, LoadSrc2);
    2194          76 :   Value *ZextUGT = Builder.CreateZExt(CmpUGT, Builder.getInt32Ty());
    2195          76 :   Value *ZextULT = Builder.CreateZExt(CmpULT, Builder.getInt32Ty());
    2196          38 :   return Builder.CreateSub(ZextUGT, ZextULT);
    2197             : }
    2198             : 
    2199             : // This function expands the memcmp call into an inline expansion and returns
    2200             : // the memcmp result.
    2201         176 : Value *MemCmpExpansion::getMemCmpExpansion(uint64_t Size) {
    2202         176 :   if (IsUsedForZeroCmp)
    2203         152 :     return NumBlocks == 1 ? getMemCmpEqZeroOneBlock(Size) :
    2204          52 :                             getMemCmpExpansionZeroCase(Size);
    2205             : 
    2206             :   // TODO: Handle more than one load pair per block in getMemCmpOneBlock().
    2207          76 :   if (NumBlocks == 1 && NumLoadsPerBlock == 1)
    2208          31 :     return getMemCmpOneBlock(Size);
    2209             : 
    2210             :   // This loop calls emitLoadCompareBlock for comparing Size bytes of the two
    2211             :   // memcmp sources. It starts with loading using the maximum load size set by
    2212             :   // the target. It processes any remaining bytes using a load size which is the
    2213             :   // next smallest power of 2.
    2214          45 :   unsigned LoadSize = MaxLoadSize;
    2215          45 :   unsigned NumBytesToBeProcessed = Size;
    2216          45 :   unsigned Index = 0;
    2217         231 :   while (NumBytesToBeProcessed) {
    2218             :     // Calculate how many blocks we can create with the current load size.
    2219          93 :     unsigned NumBlocks = NumBytesToBeProcessed / LoadSize;
    2220          93 :     unsigned GEPIndex = (Size - NumBytesToBeProcessed) / LoadSize;
    2221          93 :     NumBytesToBeProcessed = NumBytesToBeProcessed % LoadSize;
    2222             : 
    2223             :     // For each NumBlocks, populate the instruction sequence for loading and
    2224             :     // comparing LoadSize bytes.
    2225         281 :     while (NumBlocks--) {
    2226          94 :       emitLoadCompareBlock(Index, LoadSize, GEPIndex);
    2227          94 :       Index++;
    2228          94 :       GEPIndex++;
    2229             :     }
    2230             :     // Get the next LoadSize to use.
    2231          93 :     LoadSize = LoadSize / 2;
    2232             :   }
    2233             : 
    2234          45 :   emitMemCmpResultBlock();
    2235          45 :   return PhiRes;
    2236             : }
    2237             : 
    2238             : // This function checks to see if an expansion of memcmp can be generated.
    2239             : // It checks for constant compare size that is less than the max inline size.
    2240             : // If an expansion cannot occur, returns false to leave as a library call.
    2241             : // Otherwise, the library call is replaced with a new IR instruction sequence.
    2242             : /// We want to transform:
    2243             : /// %call = call signext i32 @memcmp(i8* %0, i8* %1, i64 15)
    2244             : /// To:
    2245             : /// loadbb:
    2246             : ///  %0 = bitcast i32* %buffer2 to i8*
    2247             : ///  %1 = bitcast i32* %buffer1 to i8*
    2248             : ///  %2 = bitcast i8* %1 to i64*
    2249             : ///  %3 = bitcast i8* %0 to i64*
    2250             : ///  %4 = load i64, i64* %2
    2251             : ///  %5 = load i64, i64* %3
    2252             : ///  %6 = call i64 @llvm.bswap.i64(i64 %4)
    2253             : ///  %7 = call i64 @llvm.bswap.i64(i64 %5)
    2254             : ///  %8 = sub i64 %6, %7
    2255             : ///  %9 = icmp ne i64 %8, 0
    2256             : ///  br i1 %9, label %res_block, label %loadbb1
    2257             : /// res_block:                                        ; preds = %loadbb2,
    2258             : /// %loadbb1, %loadbb
    2259             : ///  %phi.src1 = phi i64 [ %6, %loadbb ], [ %22, %loadbb1 ], [ %36, %loadbb2 ]
    2260             : ///  %phi.src2 = phi i64 [ %7, %loadbb ], [ %23, %loadbb1 ], [ %37, %loadbb2 ]
    2261             : ///  %10 = icmp ult i64 %phi.src1, %phi.src2
    2262             : ///  %11 = select i1 %10, i32 -1, i32 1
    2263             : ///  br label %endblock
    2264             : /// loadbb1:                                          ; preds = %loadbb
    2265             : ///  %12 = bitcast i32* %buffer2 to i8*
    2266             : ///  %13 = bitcast i32* %buffer1 to i8*
    2267             : ///  %14 = bitcast i8* %13 to i32*
    2268             : ///  %15 = bitcast i8* %12 to i32*
    2269             : ///  %16 = getelementptr i32, i32* %14, i32 2
    2270             : ///  %17 = getelementptr i32, i32* %15, i32 2
    2271             : ///  %18 = load i32, i32* %16
    2272             : ///  %19 = load i32, i32* %17
    2273             : ///  %20 = call i32 @llvm.bswap.i32(i32 %18)
    2274             : ///  %21 = call i32 @llvm.bswap.i32(i32 %19)
    2275             : ///  %22 = zext i32 %20 to i64
    2276             : ///  %23 = zext i32 %21 to i64
    2277             : ///  %24 = sub i64 %22, %23
    2278             : ///  %25 = icmp ne i64 %24, 0
    2279             : ///  br i1 %25, label %res_block, label %loadbb2
    2280             : /// loadbb2:                                          ; preds = %loadbb1
    2281             : ///  %26 = bitcast i32* %buffer2 to i8*
    2282             : ///  %27 = bitcast i32* %buffer1 to i8*
    2283             : ///  %28 = bitcast i8* %27 to i16*
    2284             : ///  %29 = bitcast i8* %26 to i16*
    2285             : ///  %30 = getelementptr i16, i16* %28, i16 6
    2286             : ///  %31 = getelementptr i16, i16* %29, i16 6
    2287             : ///  %32 = load i16, i16* %30
    2288             : ///  %33 = load i16, i16* %31
    2289             : ///  %34 = call i16 @llvm.bswap.i16(i16 %32)
    2290             : ///  %35 = call i16 @llvm.bswap.i16(i16 %33)
    2291             : ///  %36 = zext i16 %34 to i64
    2292             : ///  %37 = zext i16 %35 to i64
    2293             : ///  %38 = sub i64 %36, %37
    2294             : ///  %39 = icmp ne i64 %38, 0
    2295             : ///  br i1 %39, label %res_block, label %loadbb3
    2296             : /// loadbb3:                                          ; preds = %loadbb2
    2297             : ///  %40 = bitcast i32* %buffer2 to i8*
    2298             : ///  %41 = bitcast i32* %buffer1 to i8*
    2299             : ///  %42 = getelementptr i8, i8* %41, i8 14
    2300             : ///  %43 = getelementptr i8, i8* %40, i8 14
    2301             : ///  %44 = load i8, i8* %42
    2302             : ///  %45 = load i8, i8* %43
    2303             : ///  %46 = zext i8 %44 to i32
    2304             : ///  %47 = zext i8 %45 to i32
    2305             : ///  %48 = sub i32 %46, %47
    2306             : ///  br label %endblock
    2307             : /// endblock:                                         ; preds = %res_block,
    2308             : /// %loadbb3
    2309             : ///  %phi.res = phi i32 [ %48, %loadbb3 ], [ %11, %res_block ]
    2310             : ///  ret i32 %phi.res
    2311         470 : static bool expandMemCmp(CallInst *CI, const TargetTransformInfo *TTI,
    2312             :                          const TargetLowering *TLI, const DataLayout *DL) {
    2313         470 :   NumMemCmpCalls++;
    2314             : 
    2315             :   // TTI call to check if target would like to expand memcmp. Also, get the
    2316             :   // MaxLoadSize.
    2317             :   unsigned MaxLoadSize;
    2318         470 :   if (!TTI->expandMemCmp(CI, MaxLoadSize))
    2319             :     return false;
    2320             : 
    2321             :   // Early exit from expansion if -Oz.
    2322        1347 :   if (CI->getFunction()->optForMinSize())
    2323             :     return false;
    2324             : 
    2325             :   // Early exit from expansion if size is not a constant.
    2326         643 :   ConstantInt *SizeCast = dyn_cast<ConstantInt>(CI->getArgOperand(2));
    2327             :   if (!SizeCast) {
    2328             :     NumMemCmpNotConstant++;
    2329             :     return false;
    2330             :   }
    2331             : 
    2332             :   // Scale the max size down if the target can load more bytes than we need.
    2333         302 :   uint64_t SizeVal = SizeCast->getZExtValue();
    2334         302 :   if (MaxLoadSize > SizeVal)
    2335         172 :     MaxLoadSize = 1 << SizeCast->getValue().logBase2();
    2336             : 
    2337             :   // Calculate how many load pairs are needed for the constant size.
    2338         302 :   unsigned NumLoads = 0;
    2339         302 :   unsigned RemainingSize = SizeVal;
    2340         302 :   unsigned LoadSize = MaxLoadSize;
    2341        1204 :   while (RemainingSize) {
    2342         451 :     NumLoads += RemainingSize / LoadSize;
    2343         451 :     RemainingSize = RemainingSize % LoadSize;
    2344         451 :     LoadSize = LoadSize / 2;
    2345             :   }
    2346             : 
    2347             :   // Don't expand if this will require more loads than desired by the target.
    2348         906 :   if (NumLoads > TLI->getMaxExpandSizeMemcmp(CI->getFunction()->optForSize())) {
    2349             :     NumMemCmpGreaterThanMax++;
    2350             :     return false;
    2351             :   }
    2352             : 
    2353         176 :   NumMemCmpInlined++;
    2354             : 
    2355             :   // MemCmpHelper object creates and sets up basic blocks required for
    2356             :   // expanding memcmp with size SizeVal.
    2357         176 :   unsigned NumLoadsPerBlock = MemCmpNumLoadsPerBlock;
    2358         352 :   MemCmpExpansion MemCmpHelper(CI, SizeVal, MaxLoadSize, NumLoadsPerBlock, *DL);
    2359             : 
    2360         176 :   Value *Res = MemCmpHelper.getMemCmpExpansion(SizeVal);
    2361             : 
    2362             :   // Replace call with result of expansion and erase call.
    2363         176 :   CI->replaceAllUsesWith(Res);
    2364         176 :   CI->eraseFromParent();
    2365             : 
    2366             :   return true;
    2367             : }
    2368             : 
    2369      466690 : bool CodeGenPrepare::optimizeCallInst(CallInst *CI, bool &ModifiedDT) {
    2370      466690 :   BasicBlock *BB = CI->getParent();
    2371             : 
    2372             :   // Lower inline assembly if we can.
    2373             :   // If we found an inline asm expession, and if the target knows how to
    2374             :   // lower it to normal LLVM code, do so now.
    2375     1400060 :   if (TLI && isa<InlineAsm>(CI->getCalledValue())) {
    2376        6228 :     if (TLI->ExpandInlineAsm(CI)) {
    2377             :       // Avoid invalidating the iterator.
    2378          27 :       CurInstIterator = BB->begin();
    2379             :       // Avoid processing instructions out of order, which could cause
    2380             :       // reuse before a value is defined.
    2381          54 :       SunkAddrs.clear();
    2382          27 :       return true;
    2383             :     }
    2384             :     // Sink address computing for memory operands into the block.
    2385        6201 :     if (optimizeInlineAsmInst(CI))
    2386             :       return true;
    2387             :   }
    2388             : 
    2389             :   // Align the pointer arguments to this call if the target thinks it's a good
    2390             :   // idea
    2391             :   unsigned MinSize, PrefAlign;
    2392      466648 :   if (TLI && TLI->shouldAlignPointerArgs(CI, MinSize, PrefAlign)) {
    2393        3786 :     for (auto &Arg : CI->arg_operands()) {
    2394             :       // We want to align both objects whose address is used directly and
    2395             :       // objects whose address is used in casts and GEPs, though it only makes
    2396             :       // sense for GEPs if the offset is a multiple of the desired alignment and
    2397             :       // if size - offset meets the size threshold.
    2398        6310 :       if (!Arg->getType()->isPointerTy())
    2399        4162 :         continue;
    2400        1097 :       APInt Offset(DL->getPointerSizeInBits(
    2401             :                        cast<PointerType>(Arg->getType())->getAddressSpace()),
    2402        6536 :                    0);
    2403        2194 :       Value *Val = Arg->stripAndAccumulateInBoundsConstantOffsets(*DL, Offset);
    2404        1097 :       uint64_t Offset2 = Offset.getLimitedValue();
    2405        1097 :       if ((Offset2 & (PrefAlign-1)) != 0)
    2406             :         continue;
    2407             :       AllocaInst *AI;
    2408         436 :       if ((AI = dyn_cast<AllocaInst>(Val)) && AI->getAlignment() < PrefAlign &&
    2409         138 :           DL->getTypeAllocSize(AI->getAllocatedType()) >= MinSize + Offset2)
    2410          60 :         AI->setAlignment(PrefAlign);
    2411             :       // Global variables can only be aligned if they are defined in this
    2412             :       // object (i.e. they are uniquely initialized in this object), and
    2413             :       // over-aligning global variables that have an explicit section is
    2414             :       // forbidden.
    2415             :       GlobalVariable *GV;
    2416         271 :       if ((GV = dyn_cast<GlobalVariable>(Val)) && GV->canIncreaseAlignment() &&
    2417         338 :           GV->getPointerAlignment(*DL) < PrefAlign &&
    2418          67 :           DL->getTypeAllocSize(GV->getValueType()) >=
    2419          67 :               MinSize + Offset2)
    2420          39 :         GV->setAlignment(PrefAlign);
    2421             :     }
    2422             :     // If this is a memcpy (or similar) then we may be able to improve the
    2423             :     // alignment
    2424         631 :     if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(CI)) {
    2425        1893 :       unsigned Align = getKnownAlignment(MI->getDest(), *DL);
    2426         466 :       if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI))
    2427        1864 :         Align = std::min(Align, getKnownAlignment(MTI->getSource(), *DL));
    2428         631 :       if (Align > MI->getAlignment())
    2429         688 :         MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), Align));
    2430             :     }
    2431             :   }
    2432             : 
    2433             :   // If we have a cold call site, try to sink addressing computation into the
    2434             :   // cold block.  This interacts with our handling for loads and stores to
    2435             :   // ensure that we can fold all uses of a potential addressing computation
    2436             :   // into their uses.  TODO: generalize this to work over profiling data
    2437      931605 :   if (!OptSize && CI->hasFnAttr(Attribute::Cold))
    2438         140 :     for (auto &Arg : CI->arg_operands()) {
    2439         256 :       if (!Arg->getType()->isPointerTy())
    2440             :         continue;
    2441         236 :       unsigned AS = Arg->getType()->getPointerAddressSpace();
    2442         118 :       return optimizeMemoryInst(CI, Arg, Arg->getType(), AS);
    2443             :     }
    2444             : 
    2445      212445 :   IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
    2446             :   if (II) {
    2447      212445 :     switch (II->getIntrinsicID()) {
    2448             :     default: break;
    2449          29 :     case Intrinsic::objectsize: {
    2450             :       // Lower all uses of llvm.objectsize.*
    2451             :       ConstantInt *RetVal =
    2452          29 :           lowerObjectSizeCall(II, *DL, TLInfo, /*MustSucceed=*/true);
    2453             :       // Substituting this can cause recursive simplifications, which can
    2454             :       // invalidate our iterator.  Use a WeakTrackingVH to hold onto it in case
    2455             :       // this
    2456             :       // happens.
    2457          58 :       Value *CurValue = &*CurInstIterator;
    2458          58 :       WeakTrackingVH IterHandle(CurValue);
    2459             : 
    2460          29 :       replaceAndRecursivelySimplify(CI, RetVal, TLInfo, nullptr);
    2461             : 
    2462             :       // If the iterator instruction was recursively deleted, start over at the
    2463             :       // start of the block.
    2464          29 :       if (IterHandle != CurValue) {
    2465           4 :         CurInstIterator = BB->begin();
    2466           4 :         SunkAddrs.clear();
    2467             :       }
    2468          29 :       return true;
    2469             :     }
    2470         112 :     case Intrinsic::aarch64_stlxr:
    2471             :     case Intrinsic::aarch64_stxr: {
    2472         197 :       ZExtInst *ExtVal = dyn_cast<ZExtInst>(CI->getArgOperand(0));
    2473         255 :       if (!ExtVal || !ExtVal->hasOneUse() ||
    2474          85 :           ExtVal->getParent() == CI->getParent())
    2475             :         return false;
    2476             :       // Sink a zext feeding stlxr/stxr before it, so it can be folded into it.
    2477           1 :       ExtVal->moveBefore(CI);
    2478             :       // Mark this instruction as "inserted by CGP", so that other
    2479             :       // optimizations don't touch it.
    2480           1 :       InsertedInsts.insert(ExtVal);
    2481           1 :       return true;
    2482             :     }
    2483           2 :     case Intrinsic::invariant_group_barrier:
    2484           4 :       II->replaceAllUsesWith(II->getArgOperand(0));
    2485           2 :       II->eraseFromParent();
    2486           2 :       return true;
    2487             : 
    2488        1376 :     case Intrinsic::cttz:
    2489             :     case Intrinsic::ctlz:
    2490             :       // If counting zeros is expensive, try to avoid it.
    2491        1376 :       return despeculateCountZeros(II, TLI, DL, ModifiedDT);
    2492             :     }
    2493             : 
    2494      210926 :     if (TLI) {
    2495      421834 :       SmallVector<Value*, 2> PtrOps;
    2496             :       Type *AccessTy;
    2497      210925 :       if (TLI->getAddrModeArguments(II, PtrOps, AccessTy))
    2498         332 :         while (!PtrOps.empty()) {
    2499         174 :           Value *PtrVal = PtrOps.pop_back_val();
    2500         348 :           unsigned AS = PtrVal->getType()->getPointerAddressSpace();
    2501         174 :           if (optimizeMemoryInst(II, PtrVal, AccessTy, AS))
    2502          32 :             return true;
    2503             :         }
    2504             :     }
    2505             :   }
    2506             : 
    2507             :   // From here on out we're working with named functions.
    2508      456172 :   if (!CI->getCalledFunction()) return false;
    2509             : 
    2510             :   // Lower all default uses of _chk calls.  This is very similar
    2511             :   // to what InstCombineCalls does, but here we are only lowering calls
    2512             :   // to fortified library functions (e.g. __memcpy_chk) that have the default
    2513             :   // "don't know" as the objectsize.  Anything else should be left alone.
    2514      456172 :   FortifiedLibCallSimplifier Simplifier(TLInfo, true);
    2515      456172 :   if (Value *V = Simplifier.optimizeCall(CI)) {
    2516          38 :     CI->replaceAllUsesWith(V);
    2517          38 :     CI->eraseFromParent();
    2518          38 :     return true;
    2519             :   }
    2520             : 
    2521             :   LibFunc Func;
    2522     1380715 :   if (TLInfo->getLibFunc(ImmutableCallSite(CI), Func) &&
    2523      456604 :       Func == LibFunc_memcmp && expandMemCmp(CI, TTI, TLI, DL)) {
    2524         176 :     ModifiedDT = true;
    2525         176 :     return true;
    2526             :   }
    2527             :   return false;
    2528             : }
    2529             : 
    2530             : /// Look for opportunities to duplicate return instructions to the predecessor
    2531             : /// to enable tail call optimizations. The case it is currently looking for is:
    2532             : /// @code
    2533             : /// bb0:
    2534             : ///   %tmp0 = tail call i32 @f0()
    2535             : ///   br label %return
    2536             : /// bb1:
    2537             : ///   %tmp1 = tail call i32 @f1()
    2538             : ///   br label %return
    2539             : /// bb2:
    2540             : ///   %tmp2 = tail call i32 @f2()
    2541             : ///   br label %return
    2542             : /// return:
    2543             : ///   %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
    2544             : ///   ret i32 %retval
    2545             : /// @endcode
    2546             : ///
    2547             : /// =>
    2548             : ///
    2549             : /// @code
    2550             : /// bb0:
    2551             : ///   %tmp0 = tail call i32 @f0()
    2552             : ///   ret i32 %tmp0
    2553             : /// bb1:
    2554             : ///   %tmp1 = tail call i32 @f1()
    2555             : ///   ret i32 %tmp1
    2556             : /// bb2:
    2557             : ///   %tmp2 = tail call i32 @f2()
    2558             : ///   ret i32 %tmp2
    2559             : /// @endcode
    2560      401969 : bool CodeGenPrepare::dupRetToEnableTailCallOpts(BasicBlock *BB) {
    2561      401969 :   if (!TLI)
    2562             :     return false;
    2563             : 
    2564      547726 :   ReturnInst *RetI = dyn_cast<ReturnInst>(BB->getTerminator());
    2565             :   if (!RetI)
    2566             :     return false;
    2567             : 
    2568      145919 :   PHINode *PN = nullptr;
    2569      145919 :   BitCastInst *BCI = nullptr;
    2570       96087 :   Value *V = RetI->getReturnValue();
    2571             :   if (V) {
    2572        5312 :     BCI = dyn_cast<BitCastInst>(V);
    2573             :     if (BCI)
    2574        5312 :       V = BCI->getOperand(0);
    2575             : 
    2576        2020 :     PN = dyn_cast<PHINode>(V);
    2577             :     if (!PN)
    2578             :       return false;
    2579             :   }
    2580             : 
    2581        2020 :   if (PN && PN->getParent() != BB)
    2582             :     return false;
    2583             : 
    2584             :   // Make sure there are no instructions between the PHI and return, or that the
    2585             :   // return is the first instruction in the block.
    2586       51684 :   if (PN) {
    2587        1852 :     BasicBlock::iterator BI = BB->begin();
    2588        3706 :     do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
    2589        1852 :     if (&*BI == BCI)
    2590             :       // Also skip over the bitcast.
    2591             :       ++BI;
    2592        1852 :     if (&*BI != RetI)
    2593         954 :       return false;
    2594             :   } else {
    2595       49832 :     BasicBlock::iterator BI = BB->begin();
    2596       49993 :     while (isa<DbgInfoIntrinsic>(BI)) ++BI;
    2597       49832 :     if (&*BI != RetI)
    2598       44446 :       return false;
    2599             :   }
    2600             : 
    2601             :   /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
    2602             :   /// call.
    2603        6284 :   const Function *F = BB->getParent();
    2604        6284 :   SmallVector<CallInst*, 4> TailCalls;
    2605        6284 :   if (PN) {
    2606        4151 :     for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
    2607        4710 :       CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
    2608             :       // Make sure the phi value is indeed produced by the tail call.
    2609         714 :       if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
    2610        2584 :           TLI->mayBeEmittedAsTailCall(CI) &&
    2611          72 :           attributesPermitTailCall(F, CI, RetI, *TLI))
    2612          72 :         TailCalls.push_back(CI);
    2613             :     }
    2614             :   } else {
    2615       10772 :     SmallPtrSet<BasicBlock*, 4> VisitedBBs;
    2616       22621 :     for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
    2617        6463 :       if (!VisitedBBs.insert(*PI).second)
    2618        1221 :         continue;
    2619             : 
    2620        6191 :       BasicBlock::InstListType &InstList = (*PI)->getInstList();
    2621       12382 :       BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin();
    2622       12382 :       BasicBlock::InstListType::reverse_iterator RE = InstList.rend();
    2623       11591 :       do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI));
    2624        6191 :       if (RI == RE)
    2625             :         continue;
    2626             : 
    2627       10484 :       CallInst *CI = dyn_cast<CallInst>(&*RI);
    2628        6459 :       if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI) &&
    2629         310 :           attributesPermitTailCall(F, CI, RetI, *TLI))
    2630         310 :         TailCalls.push_back(CI);
    2631             :     }
    2632             :   }
    2633             : 
    2634        6284 :   bool Changed = false;
    2635       12950 :   for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) {
    2636         764 :     CallInst *CI = TailCalls[i];
    2637         382 :     CallSite CS(CI);
    2638             : 
    2639             :     // Conservatively require the attributes of the call to match those of the
    2640             :     // return. Ignore noalias because it doesn't affect the call sequence.
    2641         382 :     AttributeList CalleeAttrs = CS.getAttributes();
    2642        1146 :     if (AttrBuilder(CalleeAttrs, AttributeList::ReturnIndex)
    2643         764 :             .removeAttribute(Attribute::NoAlias) !=
    2644         764 :         AttrBuilder(CalleeAttrs, AttributeList::ReturnIndex)
    2645         382 :             .removeAttribute(Attribute::NoAlias))
    2646          12 :       continue;
    2647             : 
    2648             :     // Make sure the call instruction is followed by an unconditional branch to
    2649             :     // the return block.
    2650         382 :     BasicBlock *CallBB = CI->getParent();
    2651         761 :     BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator());
    2652         761 :     if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB)
    2653             :       continue;
    2654             : 
    2655             :     // Duplicate the return into CallBB.
    2656         370 :     (void)FoldReturnIntoUncondBranch(RetI, BB, CallBB);
    2657         370 :     ModifiedDT = Changed = true;
    2658         370 :     ++NumRetsDup;
    2659             :   }
    2660             : 
    2661             :   // If we eliminated all predecessors of the block, delete the block now.
    2662        7161 :   if (Changed && !BB->hasAddressTaken() && pred_begin(BB) == pred_end(BB))
    2663          24 :     BB->eraseFromParent();
    2664             : 
    2665        6284 :   return Changed;
    2666             : }
    2667             : 
    2668             : //===----------------------------------------------------------------------===//
    2669             : // Memory Optimization
    2670             : //===----------------------------------------------------------------------===//
    2671             : 
    2672             : namespace {
    2673             : 
    2674             : /// This is an extended version of TargetLowering::AddrMode
    2675             : /// which holds actual Value*'s for register values.
    2676             : struct ExtAddrMode : public TargetLowering::AddrMode {
    2677             :   Value *BaseReg = nullptr;
    2678             :   Value *ScaledReg = nullptr;
    2679             : 
    2680             :   ExtAddrMode() = default;
    2681             : 
    2682             :   void print(raw_ostream &OS) const;
    2683             :   void dump() const;
    2684             : 
    2685             :   bool operator==(const ExtAddrMode& O) const {
    2686        5692 :     return (BaseReg == O.BaseReg) && (ScaledReg == O.ScaledReg) &&
    2687        8876 :            (BaseGV == O.BaseGV) && (BaseOffs == O.BaseOffs) &&
    2688       15223 :            (HasBaseReg == O.HasBaseReg) && (Scale == O.Scale);
    2689             :   }
    2690             : };
    2691             : 
    2692             : } // end anonymous namespace
    2693             : 
    2694             : #ifndef NDEBUG
    2695             : static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) {
    2696             :   AM.print(OS);
    2697             :   return OS;
    2698             : }
    2699             : #endif
    2700             : 
    2701             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    2702             : void ExtAddrMode::print(raw_ostream &OS) const {
    2703             :   bool NeedPlus = false;
    2704             :   OS << "[";
    2705             :   if (BaseGV) {
    2706             :     OS << (NeedPlus ? " + " : "")
    2707             :        << "GV:";
    2708             :     BaseGV->printAsOperand(OS, /*PrintType=*/false);
    2709             :     NeedPlus = true;
    2710             :   }
    2711             : 
    2712             :   if (BaseOffs) {
    2713             :     OS << (NeedPlus ? " + " : "")
    2714             :        << BaseOffs;
    2715             :     NeedPlus = true;
    2716             :   }
    2717             : 
    2718             :   if (BaseReg) {
    2719             :     OS << (NeedPlus ? " + " : "")
    2720             :        << "Base:";
    2721             :     BaseReg->printAsOperand(OS, /*PrintType=*/false);
    2722             :     NeedPlus = true;
    2723             :   }
    2724             :   if (Scale) {
    2725             :     OS << (NeedPlus ? " + " : "")
    2726             :        << Scale << "*";
    2727             :     ScaledReg->printAsOperand(OS, /*PrintType=*/false);
    2728             :   }
    2729             : 
    2730             :   OS << ']';
    2731             : }
    2732             : 
    2733             : LLVM_DUMP_METHOD void ExtAddrMode::dump() const {
    2734             :   print(dbgs());
    2735             :   dbgs() << '\n';
    2736             : }
    2737             : #endif
    2738             : 
    2739             : namespace {
    2740             : 
    2741             : /// \brief This class provides transaction based operation on the IR.
    2742             : /// Every change made through this class is recorded in the internal state and
    2743             : /// can be undone (rollback) until commit is called.
    2744     2040480 : class TypePromotionTransaction {
    2745             :   /// \brief This represents the common interface of the individual transaction.
    2746             :   /// Each class implements the logic for doing one specific modification on
    2747             :   /// the IR via the TypePromotionTransaction.
    2748             :   class TypePromotionAction {
    2749             :   protected:
    2750             :     /// The Instruction modified.
    2751             :     Instruction *Inst;
    2752             : 
    2753             :   public:
    2754             :     /// \brief Constructor of the action.
    2755             :     /// The constructor performs the related action on the IR.
    2756        4259 :     TypePromotionAction(Instruction *Inst) : Inst(Inst) {}
    2757             : 
    2758             :     virtual ~TypePromotionAction() = default;
    2759             : 
    2760             :     /// \brief Undo the modification done by this action.
    2761             :     /// When this method is called, the IR must be in the same state as it was
    2762             :     /// before this action was applied.
    2763             :     /// \pre Undoing the action works if and only if the IR is in the exact same
    2764             :     /// state as it was directly after this action was applied.
    2765             :     virtual void undo() = 0;
    2766             : 
    2767             :     /// \brief Advocate every change made by this action.
    2768             :     /// When the results on the IR of the action are to be kept, it is important
    2769             :     /// to call this function, otherwise hidden information may be kept forever.
    2770           0 :     virtual void commit() {
    2771             :       // Nothing to be done, this action is not doing anything.
    2772           0 :     }
    2773             :   };
    2774             : 
    2775             :   /// \brief Utility to remember the position of an instruction.
    2776             :   class InsertionHandler {
    2777             :     /// Position of an instruction.
    2778             :     /// Either an instruction:
    2779             :     /// - Is the first in a basic block: BB is used.
    2780             :     /// - Has a previous instructon: PrevInst is used.
    2781             :     union {
    2782             :       Instruction *PrevInst;
    2783             :       BasicBlock *BB;
    2784             :     } Point;
    2785             : 
    2786             :     /// Remember whether or not the instruction had a previous instruction.
    2787             :     bool HasPrevInstruction;
    2788             : 
    2789             :   public:
    2790             :     /// \brief Record the position of \p Inst.
    2791         827 :     InsertionHandler(Instruction *Inst) {
    2792        1654 :       BasicBlock::iterator It = Inst->getIterator();
    2793        2481 :       HasPrevInstruction = (It != (Inst->getParent()->begin()));
    2794         827 :       if (HasPrevInstruction)
    2795        1482 :         Point.PrevInst = &*--It;
    2796             :       else
    2797          86 :         Point.BB = Inst->getParent();
    2798             :     }
    2799             : 
    2800             :     /// \brief Insert \p Inst at the recorded position.
    2801         465 :     void insert(Instruction *Inst) {
    2802         465 :       if (HasPrevInstruction) {
    2803         410 :         if (Inst->getParent())
    2804         326 :           Inst->removeFromParent();
    2805         410 :         Inst->insertAfter(Point.PrevInst);
    2806             :       } else {
    2807         165 :         Instruction *Position = &*Point.BB->getFirstInsertionPt();
    2808          55 :         if (Inst->getParent())
    2809          47 :           Inst->moveBefore(Position);
    2810             :         else
    2811           8 :           Inst->insertBefore(Position);
    2812             :       }
    2813         465 :     }
    2814             :   };
    2815             : 
    2816             :   /// \brief Move an instruction before another.
    2817         648 :   class InstructionMoveBefore : public TypePromotionAction {
    2818             :     /// Original position of the instruction.
    2819             :     InsertionHandler Position;
    2820             : 
    2821             :   public:
    2822             :     /// \brief Move \p Inst before \p Before.
    2823         648 :     InstructionMoveBefore(Instruction *Inst, Instruction *Before)
    2824        1944 :         : TypePromotionAction(Inst), Position(Inst) {
    2825             :       DEBUG(dbgs() << "Do: move: " << *Inst << "\nbefore: " << *Before << "\n");
    2826         648 :       Inst->moveBefore(Before);
    2827         648 :     }
    2828             : 
    2829             :     /// \brief Move the instruction back to its original position.
    2830         373 :     void undo() override {
    2831             :       DEBUG(dbgs() << "Undo: moveBefore: " << *Inst << "\n");
    2832         373 :       Position.insert(Inst);
    2833         373 :     }
    2834             :   };
    2835             : 
    2836             :   /// \brief Set the operand of an instruction with a new value.
    2837        1780 :   class OperandSetter : public TypePromotionAction {
    2838             :     /// Original operand of the instruction.
    2839             :     Value *Origin;
    2840             : 
    2841             :     /// Index of the modified instruction.
    2842             :     unsigned Idx;
    2843             : 
    2844             :   public:
    2845             :     /// \brief Set \p Idx operand of \p Inst with \p NewVal.
    2846        1780 :     OperandSetter(Instruction *Inst, unsigned Idx, Value *NewVal)
    2847        3560 :         : TypePromotionAction(Inst), Idx(Idx) {
    2848             :       DEBUG(dbgs() << "Do: setOperand: " << Idx << "\n"
    2849             :                    << "for:" << *Inst << "\n"
    2850             :                    << "with:" << *NewVal << "\n");
    2851        3560 :       Origin = Inst->getOperand(Idx);
    2852        1780 :       Inst->setOperand(Idx, NewVal);
    2853        1780 :     }
    2854             : 
    2855             :     /// \brief Restore the original value of the instruction.
    2856        1042 :     void undo() override {
    2857             :       DEBUG(dbgs() << "Undo: setOperand:" << Idx << "\n"
    2858             :                    << "for: " << *Inst << "\n"
    2859             :                    << "with: " << *Origin << "\n");
    2860        1042 :       Inst->setOperand(Idx, Origin);
    2861        1042 :     }
    2862             :   };
    2863             : 
    2864             :   /// \brief Hide the operands of an instruction.
    2865             :   /// Do as if this instruction was not using any of its operands.
    2866         358 :   class OperandsHider : public TypePromotionAction {
    2867             :     /// The list of original operands.
    2868             :     SmallVector<Value *, 4> OriginalValues;
    2869             : 
    2870             :   public:
    2871             :     /// \brief Remove \p Inst from the uses of the operands of \p Inst.
    2872         537 :     OperandsHider(Instruction *Inst) : TypePromotionAction(Inst) {
    2873             :       DEBUG(dbgs() << "Do: OperandsHider: " << *Inst << "\n");
    2874         358 :       unsigned NumOpnds = Inst->getNumOperands();
    2875         179 :       OriginalValues.reserve(NumOpnds);
    2876         358 :       for (unsigned It = 0; It < NumOpnds; ++It) {
    2877             :         // Save the current operand.
    2878         358 :         Value *Val = Inst->getOperand(It);
    2879         179 :         OriginalValues.push_back(Val);
    2880             :         // Set a dummy one.
    2881             :         // We could use OperandSetter here, but that would imply an overhead
    2882             :         // that we are not willing to pay.
    2883         179 :         Inst->setOperand(It, UndefValue::get(Val->getType()));
    2884             :       }
    2885         179 :     }
    2886             : 
    2887             :     /// \brief Restore the original list of uses.
    2888          92 :     void undo() override {
    2889             :       DEBUG(dbgs() << "Undo: OperandsHider: " << *Inst << "\n");
    2890         276 :       for (unsigned It = 0, EndIt = OriginalValues.size(); It != EndIt; ++It)
    2891         184 :         Inst->setOperand(It, OriginalValues[It]);
    2892          92 :     }
    2893             :   };
    2894             : 
    2895             :   /// \brief Build a truncate instruction.
    2896          48 :   class TruncBuilder : public TypePromotionAction {
    2897             :     Value *Val;
    2898             : 
    2899             :   public:
    2900             :     /// \brief Build a truncate instruction of \p Opnd producing a \p Ty
    2901             :     /// result.
    2902             :     /// trunc Opnd to Ty.
    2903          96 :     TruncBuilder(Instruction *Opnd, Type *Ty) : TypePromotionAction(Opnd) {
    2904          96 :       IRBuilder<> Builder(Opnd);
    2905          96 :       Val = Builder.CreateTrunc(Opnd, Ty, "promoted");
    2906             :       DEBUG(dbgs() << "Do: TruncBuilder: " << *Val << "\n");
    2907          48 :     }
    2908             : 
    2909             :     /// \brief Get the built value.
    2910             :     Value *getBuiltValue() { return Val; }
    2911             : 
    2912             :     /// \brief Remove the built instruction.
    2913          40 :     void undo() override {
    2914             :       DEBUG(dbgs() << "Undo: TruncBuilder: " << *Val << "\n");
    2915          80 :       if (Instruction *IVal = dyn_cast<Instruction>(Val))
    2916          40 :         IVal->eraseFromParent();
    2917          40 :     }
    2918             :   };
    2919             : 
    2920             :   /// \brief Build a sign extension instruction.
    2921          89 :   class SExtBuilder : public TypePromotionAction {
    2922             :     Value *Val;
    2923             : 
    2924             :   public:
    2925             :     /// \brief Build a sign extension instruction of \p Opnd producing a \p Ty
    2926             :     /// result.
    2927             :     /// sext Opnd to Ty.
    2928          89 :     SExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty)
    2929         178 :         : TypePromotionAction(InsertPt) {
    2930         178 :       IRBuilder<> Builder(InsertPt);
    2931         178 :       Val = Builder.CreateSExt(Opnd, Ty, "promoted");
    2932             :       DEBUG(dbgs() << "Do: SExtBuilder: " << *Val << "\n");
    2933          89 :     }
    2934             : 
    2935             :     /// \brief Get the built value.
    2936             :     Value *getBuiltValue() { return Val; }
    2937             : 
    2938             :     /// \brief Remove the built instruction.
    2939          47 :     void undo() override {
    2940             :       DEBUG(dbgs() << "Undo: SExtBuilder: " << *Val << "\n");
    2941          94 :       if (Instruction *IVal = dyn_cast<Instruction>(Val))
    2942          47 :         IVal->eraseFromParent();
    2943          47 :     }
    2944             :   };
    2945             : 
    2946             :   /// \brief Build a zero extension instruction.
    2947         124 :   class ZExtBuilder : public TypePromotionAction {
    2948             :     Value *Val;
    2949             : 
    2950             :   public:
    2951             :     /// \brief Build a zero extension instruction of \p Opnd producing a \p Ty
    2952             :     /// result.
    2953             :     /// zext Opnd to Ty.
    2954         124 :     ZExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty)
    2955         248 :         : TypePromotionAction(InsertPt) {
    2956         248 :       IRBuilder<> Builder(InsertPt);
    2957         248 :       Val = Builder.CreateZExt(Opnd, Ty, "promoted");
    2958             :       DEBUG(dbgs() << "Do: ZExtBuilder: " << *Val << "\n");
    2959         124 :     }
    2960             : 
    2961             :     /// \brief Get the built value.
    2962             :     Value *getBuiltValue() { return Val; }
    2963             : 
    2964             :     /// \brief Remove the built instruction.
    2965          59 :     void undo() override {
    2966             :       DEBUG(dbgs() << "Undo: ZExtBuilder: " << *Val << "\n");
    2967         117 :       if (Instruction *IVal = dyn_cast<Instruction>(Val))
    2968          58 :         IVal->eraseFromParent();
    2969          59 :     }
    2970             :   };
    2971             : 
    2972             :   /// \brief Mutate an instruction to another type.
    2973         535 :   class TypeMutator : public TypePromotionAction {
    2974             :     /// Record the original type.
    2975             :     Type *OrigTy;
    2976             : 
    2977             :   public:
    2978             :     /// \brief Mutate the type of \p Inst into \p NewTy.
    2979             :     TypeMutator(Instruction *Inst, Type *NewTy)
    2980        1070 :         : TypePromotionAction(Inst), OrigTy(Inst->getType()) {
    2981             :       DEBUG(dbgs() << "Do: MutateType: " << *Inst << " with " << *NewTy
    2982             :                    << "\n");
    2983        1070 :       Inst->mutateType(NewTy);
    2984             :     }
    2985             : 
    2986             :     /// \brief Mutate the instruction back to its original type.
    2987         309 :     void undo() override {
    2988             :       DEBUG(dbgs() << "Undo: MutateType: " << *Inst << " with " << *OrigTy
    2989             :                    << "\n");
    2990         618 :       Inst->mutateType(OrigTy);
    2991         309 :     }
    2992             :   };
    2993             : 
    2994             :   /// \brief Replace the uses of an instruction by another instruction.
    2995        1354 :   class UsesReplacer : public TypePromotionAction {
    2996             :     /// Helper structure to keep track of the replaced uses.
    2997             :     struct InstructionAndIdx {
    2998             :       /// The instruction using the instruction.
    2999             :       Instruction *Inst;
    3000             : 
    3001             :       /// The index where this instruction is used for Inst.
    3002             :       unsigned Idx;
    3003             : 
    3004             :       InstructionAndIdx(Instruction *Inst, unsigned Idx)
    3005         873 :           : Inst(Inst), Idx(Idx) {}
    3006             :     };
    3007             : 
    3008             :     /// Keep track of the original uses (pair Instruction, Index).
    3009             :     SmallVector<InstructionAndIdx, 4> OriginalUses;
    3010             : 
    3011             :     using use_iterator = SmallVectorImpl<InstructionAndIdx>::iterator;
    3012             : 
    3013             :   public:
    3014             :     /// \brief Replace all the use of \p Inst by \p New.
    3015        2031 :     UsesReplacer(Instruction *Inst, Value *New) : TypePromotionAction(Inst) {
    3016             :       DEBUG(dbgs() << "Do: UsersReplacer: " << *Inst << " with " << *New
    3017             :                    << "\n");
    3018             :       // Record the original uses.
    3019        2904 :       for (Use &U : Inst->uses()) {
    3020        1746 :         Instruction *UserI = cast<Instruction>(U.getUser());
    3021        1746 :         OriginalUses.push_back(InstructionAndIdx(UserI, U.getOperandNo()));
    3022             :       }
    3023             :       // Now, we can replace the uses.
    3024         677 :       Inst->replaceAllUsesWith(New);
    3025         677 :     }
    3026             : 
    3027             :     /// \brief Reassign the original uses of Inst to Inst.
    3028         391 :     void undo() override {
    3029             :       DEBUG(dbgs() << "Undo: UsersReplacer: " << *Inst << "\n");
    3030        1341 :       for (use_iterator UseIt = OriginalUses.begin(),
    3031         782 :                         EndIt = OriginalUses.end();
    3032         950 :            UseIt != EndIt; ++UseIt) {
    3033         559 :         UseIt->Inst->setOperand(UseIt->Idx, Inst);
    3034             :       }
    3035         391 :     }
    3036             :   };
    3037             : 
    3038             :   /// \brief Remove an instruction from the IR.
    3039             :   class InstructionRemover : public TypePromotionAction {
    3040             :     /// Original position of the instruction.
    3041             :     InsertionHandler Inserter;
    3042             : 
    3043             :     /// Helper structure to hide all the link to the instruction. In other
    3044             :     /// words, this helps to do as if the instruction was removed.
    3045             :     OperandsHider Hider;
    3046             : 
    3047             :     /// Keep track of the uses replaced, if any.
    3048             :     UsesReplacer *Replacer = nullptr;
    3049             : 
    3050             :     /// Keep track of instructions removed.
    3051             :     SetOfInstrs &RemovedInsts;
    3052             : 
    3053             :   public:
    3054             :     /// \brief Remove all reference of \p Inst and optinally replace all its
    3055             :     /// uses with New.
    3056             :     /// \p RemovedInsts Keep track of the instructions removed by this Action.
    3057             :     /// \pre If !Inst->use_empty(), then New != nullptr
    3058         179 :     InstructionRemover(Instruction *Inst, SetOfInstrs &RemovedInsts,
    3059             :                        Value *New = nullptr)
    3060         179 :         : TypePromotionAction(Inst), Inserter(Inst), Hider(Inst),
    3061         537 :           RemovedInsts(RemovedInsts) {
    3062         179 :       if (New)
    3063           0 :         Replacer = new UsesReplacer(Inst, New);
    3064             :       DEBUG(dbgs() << "Do: InstructionRemover: " << *Inst << "\n");
    3065         179 :       RemovedInsts.insert(Inst);
    3066             :       /// The instructions removed here will be freed after completing
    3067             :       /// optimizeBlock() for all blocks as we need to keep track of the
    3068             :       /// removed instructions during promotion.
    3069         179 :       Inst->removeFromParent();
    3070         179 :     }
    3071             : 
    3072         537 :     ~InstructionRemover() override { delete Replacer; }
    3073             : 
    3074             :     /// \brief Resurrect the instruction and reassign it to the proper uses if
    3075             :     /// new value was provided when build this action.
    3076          92 :     void undo() override {
    3077             :       DEBUG(dbgs() << "Undo: InstructionRemover: " << *Inst << "\n");
    3078          92 :       Inserter.insert(Inst);
    3079          92 :       if (Replacer)
    3080           0 :         Replacer->undo();
    3081          92 :       Hider.undo();
    3082         184 :       RemovedInsts.erase(Inst);
    3083          92 :     }
    3084             :   };
    3085             : 
    3086             : public:
    3087             :   /// Restoration point.
    3088             :   /// The restoration point is a pointer to an action instead of an iterator
    3089             :   /// because the iterator may be invalidated but not the pointer.
    3090             :   using ConstRestorationPt = const TypePromotionAction *;
    3091             : 
    3092             :   TypePromotionTransaction(SetOfInstrs &RemovedInsts)
    3093     2040480 :       : RemovedInsts(RemovedInsts) {}
    3094             : 
    3095             :   /// Advocate every changes made in that transaction.
    3096             :   void commit();
    3097             : 
    3098             :   /// Undo all the changes made after the given point.
    3099             :   void rollback(ConstRestorationPt Point);
    3100             : 
    3101             :   /// Get the current restoration point.
    3102             :   ConstRestorationPt getRestorationPoint() const;
    3103             : 
    3104             :   /// \name API for IR modification with state keeping to support rollback.
    3105             :   /// @{
    3106             :   /// Same as Instruction::setOperand.
    3107             :   void setOperand(Instruction *Inst, unsigned Idx, Value *NewVal);
    3108             : 
    3109             :   /// Same as Instruction::eraseFromParent.
    3110             :   void eraseInstruction(Instruction *Inst, Value *NewVal = nullptr);
    3111             : 
    3112             :   /// Same as Value::replaceAllUsesWith.
    3113             :   void replaceAllUsesWith(Instruction *Inst, Value *New);
    3114             : 
    3115             :   /// Same as Value::mutateType.
    3116             :   void mutateType(Instruction *Inst, Type *NewTy);
    3117             : 
    3118             :   /// Same as IRBuilder::createTrunc.
    3119             :   Value *createTrunc(Instruction *Opnd, Type *Ty);
    3120             : 
    3121             :   /// Same as IRBuilder::createSExt.
    3122             :   Value *createSExt(Instruction *Inst, Value *Opnd, Type *Ty);
    3123             : 
    3124             :   /// Same as IRBuilder::createZExt.
    3125             :   Value *createZExt(Instruction *Inst, Value *Opnd, Type *Ty);
    3126             : 
    3127             :   /// Same as Instruction::moveBefore.
    3128             :   void moveBefore(Instruction *Inst, Instruction *Before);
    3129             :   /// @}
    3130             : 
    3131             : private:
    3132             :   /// The ordered list of actions made so far.
    3133             :   SmallVector<std::unique_ptr<TypePromotionAction>, 16> Actions;
    3134             : 
    3135             :   using CommitPt = SmallVectorImpl<std::unique_ptr<TypePromotionAction>>::iterator;
    3136             : 
    3137             :   SetOfInstrs &RemovedInsts;
    3138             : };
    3139             : 
    3140             : } // end anonymous namespace
    3141             : 
    3142        1780 : void TypePromotionTransaction::setOperand(Instruction *Inst, unsigned Idx,
    3143             :                                           Value *NewVal) {
    3144        8900 :   Actions.push_back(llvm::make_unique<TypePromotionTransaction::OperandSetter>(
    3145             :       Inst, Idx, NewVal));
    3146        1780 : }
    3147             : 
    3148         179 : void TypePromotionTransaction::eraseInstruction(Instruction *Inst,
    3149             :                                                 Value *NewVal) {
    3150         537 :   Actions.push_back(
    3151         537 :       llvm::make_unique<TypePromotionTransaction::InstructionRemover>(
    3152         179 :           Inst, RemovedInsts, NewVal));
    3153         179 : }
    3154             : 
    3155         677 : void TypePromotionTransaction::replaceAllUsesWith(Instruction *Inst,
    3156             :                                                   Value *New) {
    3157        2031 :   Actions.push_back(
    3158        2031 :       llvm::make_unique<TypePromotionTransaction::UsesReplacer>(Inst, New));
    3159         677 : }
    3160             : 
    3161         535 : void TypePromotionTransaction::mutateType(Instruction *Inst, Type *NewTy) {
    3162        1605 :   Actions.push_back(
    3163        1605 :       llvm::make_unique<TypePromotionTransaction::TypeMutator>(Inst, NewTy));
    3164         535 : }
    3165             : 
    3166          48 : Value *TypePromotionTransaction::createTrunc(Instruction *Opnd,
    3167             :                                              Type *Ty) {
    3168         144 :   std::unique_ptr<TruncBuilder> Ptr(new TruncBuilder(Opnd, Ty));
    3169          48 :   Value *Val = Ptr->getBuiltValue();
    3170         144 :   Actions.push_back(std::move(Ptr));
    3171          96 :   return Val;
    3172             : }
    3173             : 
    3174          89 : Value *TypePromotionTransaction::createSExt(Instruction *Inst,
    3175             :                                             Value *Opnd, Type *Ty) {
    3176         267 :   std::unique_ptr<SExtBuilder> Ptr(new SExtBuilder(Inst, Opnd, Ty));
    3177          89 :   Value *Val = Ptr->getBuiltValue();
    3178         267 :   Actions.push_back(std::move(Ptr));
    3179         178 :   return Val;
    3180             : }
    3181             : 
    3182         124 : Value *TypePromotionTransaction::createZExt(Instruction *Inst,
    3183             :                                             Value *Opnd, Type *Ty) {
    3184         372 :   std::unique_ptr<ZExtBuilder> Ptr(new ZExtBuilder(Inst, Opnd, Ty));
    3185         124 :   Value *Val = Ptr->getBuiltValue();
    3186         372 :   Actions.push_back(std::move(Ptr));
    3187         248 :   return Val;
    3188             : }
    3189             : 
    3190         648 : void TypePromotionTransaction::moveBefore(Instruction *Inst,
    3191             :                                           Instruction *Before) {
    3192        1944 :   Actions.push_back(
    3193        1944 :       llvm::make_unique<TypePromotionTransaction::InstructionMoveBefore>(
    3194             :           Inst, Before));
    3195         648 : }
    3196             : 
    3197             : TypePromotionTransaction::ConstRestorationPt
    3198             : TypePromotionTransaction::getRestorationPoint() const {
    3199     2955462 :   return !Actions.empty() ? Actions.back().get() : nullptr;
    3200             : }
    3201             : 
    3202             : void TypePromotionTransaction::commit() {
    3203     2974710 :   for (CommitPt It = Actions.begin(), EndIt = Actions.end(); It != EndIt;
    3204             :        ++It)
    3205             :     (*It)->commit();
    3206      991570 :   Actions.clear();
    3207             : }
    3208             : 
    3209       38964 : void TypePromotionTransaction::rollback(
    3210             :     TypePromotionTransaction::ConstRestorationPt Point) {
    3211       49332 :   while (!Actions.empty() && Point != Actions.back().get()) {
    3212        7059 :     std::unique_ptr<TypePromotionAction> Curr = Actions.pop_back_val();
    3213        2353 :     Curr->undo();
    3214             :   }
    3215       38964 : }
    3216             : 
    3217             : namespace {
    3218             : 
    3219             : /// \brief A helper class for matching addressing modes.
    3220             : ///
    3221             : /// This encapsulates the logic for matching the target-legal addressing modes.
    3222             : class AddressingModeMatcher {
    3223             :   SmallVectorImpl<Instruction*> &AddrModeInsts;
    3224             :   const TargetLowering &TLI;
    3225             :   const TargetRegisterInfo &TRI;
    3226             :   const DataLayout &DL;
    3227             : 
    3228             :   /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and
    3229             :   /// the memory instruction that we're computing this address for.
    3230             :   Type *AccessTy;
    3231             :   unsigned AddrSpace;
    3232             :   Instruction *MemoryInst;
    3233             : 
    3234             :   /// This is the addressing mode that we're building up. This is
    3235             :   /// part of the return value of this addressing mode matching stuff.
    3236             :   ExtAddrMode &AddrMode;
    3237             : 
    3238             :   /// The instructions inserted by other CodeGenPrepare optimizations.
    3239             :   const SetOfInstrs &InsertedInsts;
    3240             : 
    3241             :   /// A map from the instructions to their type before promotion.
    3242             :   InstrToOrigTy &PromotedInsts;
    3243             : 
    3244             :   /// The ongoing transaction where every action should be registered.
    3245             :   TypePromotionTransaction &TPT;
    3246             : 
    3247             :   /// This is set to true when we should not do profitability checks.
    3248             :   /// When true, IsProfitableToFoldIntoAddressingMode always returns true.
    3249             :   bool IgnoreProfitability;
    3250             : 
    3251             :   AddressingModeMatcher(SmallVectorImpl<Instruction *> &AMI,
    3252             :                         const TargetLowering &TLI,
    3253             :                         const TargetRegisterInfo &TRI,
    3254             :                         Type *AT, unsigned AS,
    3255             :                         Instruction *MI, ExtAddrMode &AM,
    3256             :                         const SetOfInstrs &InsertedInsts,
    3257             :                         InstrToOrigTy &PromotedInsts,
    3258             :                         TypePromotionTransaction &TPT)
    3259     1006564 :       : AddrModeInsts(AMI), TLI(TLI), TRI(TRI),
    3260     1006564 :         DL(MI->getModule()->getDataLayout()), AccessTy(AT), AddrSpace(AS),
    3261             :         MemoryInst(MI), AddrMode(AM), InsertedInsts(InsertedInsts),
    3262     2013128 :         PromotedInsts(PromotedInsts), TPT(TPT) {
    3263     1002586 :     IgnoreProfitability = false;
    3264             :   }
    3265             : 
    3266             : public:
    3267             :   /// Find the maximal addressing mode that a load/store of V can fold,
    3268             :   /// give an access type of AccessTy.  This returns a list of involved
    3269             :   /// instructions in AddrModeInsts.
    3270             :   /// \p InsertedInsts The instructions inserted by other CodeGenPrepare
    3271             :   /// optimizations.
    3272             :   /// \p PromotedInsts maps the instructions to their type before promotion.
    3273             :   /// \p The ongoing transaction where every action should be registered.
    3274     1002586 :   static ExtAddrMode Match(Value *V, Type *AccessTy, unsigned AS,
    3275             :                            Instruction *MemoryInst,
    3276             :                            SmallVectorImpl<Instruction*> &AddrModeInsts,
    3277             :                            const TargetLowering &TLI,
    3278             :                            const TargetRegisterInfo &TRI,
    3279             :                            const SetOfInstrs &InsertedInsts,
    3280             :                            InstrToOrigTy &PromotedInsts,
    3281             :                            TypePromotionTransaction &TPT) {
    3282     1002586 :     ExtAddrMode Result;
    3283             : 
    3284     2005172 :     bool Success = AddressingModeMatcher(AddrModeInsts, TLI, TRI,
    3285             :                                          AccessTy, AS,
    3286             :                                          MemoryInst, Result, InsertedInsts,
    3287     1002586 :                                          PromotedInsts, TPT).matchAddr(V, 0);
    3288             :     (void)Success; assert(Success && "Couldn't select *anything*?");
    3289     1002586 :     return Result;
    3290             :   }
    3291             : 
    3292             : private:
    3293             :   bool matchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth);
    3294             :   bool matchAddr(Value *V, unsigned Depth);
    3295             :   bool matchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth,
    3296             :                           bool *MovedAway = nullptr);
    3297             :   bool isProfitableToFoldIntoAddressingMode(Instruction *I,
    3298             :                                             ExtAddrMode &AMBefore,
    3299             :                                             ExtAddrMode &AMAfter);
    3300             :   bool valueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2);
    3301             :   bool isPromotionProfitable(unsigned NewCost, unsigned OldCost,
    3302             :                              Value *PromotedOperand) const;
    3303             : };
    3304             : 
    3305             : } // end anonymous namespace
    3306             : 
    3307             : /// Try adding ScaleReg*Scale to the current addressing mode.
    3308             : /// Return true and update AddrMode if this addr mode is legal for the target,
    3309             : /// false if not.
    3310      169932 : bool AddressingModeMatcher::matchScaledValue(Value *ScaleReg, int64_t Scale,
    3311             :                                              unsigned Depth) {
    3312             :   // If Scale is 1, then this is the same as adding ScaleReg to the addressing
    3313             :   // mode.  Just process that directly.
    3314      169932 :   if (Scale == 1)
    3315        7676 :     return matchAddr(ScaleReg, Depth);
    3316             : 
    3317             :   // If the scale is 0, it takes nothing to add this.
    3318      162256 :   if (Scale == 0)
    3319             :     return true;
    3320             : 
    3321             :   // If we already have a scale of this value, we can add to it, otherwise, we
    3322             :   // need an available scale field.
    3323      162252 :   if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg)
    3324             :     return false;
    3325             : 
    3326      162010 :   ExtAddrMode TestAddrMode = AddrMode;
    3327             : 
    3328             :   // Add scale to turn X*4+X*3 -> X*7.  This could also do things like
    3329             :   // [A+B + A*7] -> [B+A*8].
    3330      162010 :   TestAddrMode.Scale += Scale;
    3331      162010 :   TestAddrMode.ScaledReg = ScaleReg;
    3332             : 
    3333             :   // If the new address isn't legal, bail out.
    3334      162010 :   if (!TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace))
    3335             :     return false;
    3336             : 
    3337             :   // It was legal, so commit it.
    3338       99883 :   AddrMode = TestAddrMode;
    3339             : 
    3340             :   // Okay, we decided that we can add ScaleReg+Scale to AddrMode.  Check now
    3341             :   // to see if ScaleReg is actually X+C.  If so, we can turn this into adding
    3342             :   // X*Scale + C*Scale to addr mode.
    3343       99883 :   ConstantInt *CI = nullptr; Value *AddLHS = nullptr;
    3344      299127 :   if (isa<Instruction>(ScaleReg) &&  // not a constant expr.
    3345      497327 :       match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) {
    3346         841 :     TestAddrMode.ScaledReg = AddLHS;
    3347        1682 :     TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale;
    3348             : 
    3349             :     // If this addressing mode is legal, commit it and remember that we folded
    3350             :     // this instruction.
    3351         841 :     if (TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace)) {
    3352        1500 :       AddrModeInsts.push_back(cast<Instruction>(ScaleReg));
    3353         750 :       AddrMode = TestAddrMode;
    3354         750 :       return true;
    3355             :     }
    3356             :   }
    3357             : 
    3358             :   // Otherwise, not (x+c)*scale, just return what we have.
    3359             :   return true;
    3360             : }
    3361             : 
    3362             : /// This is a little filter, which returns true if an addressing computation
    3363             : /// involving I might be folded into a load/store accessing it.
    3364             : /// This doesn't need to be perfect, but needs to accept at least
    3365             : /// the set of instructions that MatchOperationAddr can.
    3366       11084 : static bool MightBeFoldableInst(Instruction *I) {
    3367       11084 :   switch (I->getOpcode()) {
    3368        3468 :   case Instruction::BitCast:
    3369             :   case Instruction::AddrSpaceCast:
    3370             :     // Don't touch identity bitcasts.
    3371        6936 :     if (I->getType() == I->getOperand(0)->getType())
    3372             :       return false;
    3373        6936 :     return I->getType()->isPointerTy() || I->getType()->isIntegerTy();
    3374             :   case Instruction::PtrToInt:
    3375             :     // PtrToInt is always a noop, as we know that the int type is pointer sized.
    3376             :     return true;
    3377             :   case Instruction::IntToPtr:
    3378             :     // We know the input is intptr_t, so this is foldable.
    3379             :     return true;
    3380             :   case Instruction::Add:
    3381             :     return true;
    3382          30 :   case Instruction::Mul:
    3383             :   case Instruction::Shl:
    3384             :     // Can only handle X*C and X << C.
    3385          60 :     return isa<ConstantInt>(I->getOperand(1));
    3386             :   case Instruction::GetElementPtr:
    3387             :     return true;
    3388        1354 :   default:
    3389        1354 :     return false;
    3390             :   }
    3391             : }
    3392             : 
    3393             : /// \brief Check whether or not \p Val is a legal instruction for \p TLI.
    3394             : /// \note \p Val is assumed to be the product of some type promotion.
    3395             : /// Therefore if \p Val has an undefined state in \p TLI, this is assumed
    3396             : /// to be legal, as the non-promoted value would have had the same state.
    3397         417 : static bool isPromotedInstructionLegal(const TargetLowering &TLI,
    3398             :                                        const DataLayout &DL, Value *Val) {
    3399         416 :   Instruction *PromotedInst = dyn_cast<Instruction>(Val);
    3400             :   if (!PromotedInst)
    3401             :     return false;
    3402         832 :   int ISDOpcode = TLI.InstructionOpcodeToISD(PromotedInst->getOpcode());
    3403             :   // If the ISDOpcode is undefined, it was undefined before the promotion.
    3404         416 :   if (!ISDOpcode)
    3405             :     return true;
    3406             :   // Otherwise, check if the promoted instruction is legal or not.
    3407         416 :   return TLI.isOperationLegalOrCustom(
    3408         416 :       ISDOpcode, TLI.getValueType(DL, PromotedInst->getType()));
    3409             : }
    3410             : 
    3411             : namespace {
    3412             : 
    3413             : /// \brief Hepler class to perform type promotion.
    3414             : class TypePromotionHelper {
    3415             :   /// \brief Utility function to check whether or not a sign or zero extension
    3416             :   /// of \p Inst with \p ConsideredExtType can be moved through \p Inst by
    3417             :   /// either using the operands of \p Inst or promoting \p Inst.
    3418             :   /// The type of the extension is defined by \p IsSExt.
    3419             :   /// In other words, check if:
    3420             :   /// ext (Ty Inst opnd1 opnd2 ... opndN) to ConsideredExtType.
    3421             :   /// #1 Promotion applies:
    3422             :   /// ConsideredExtType Inst (ext opnd1 to ConsideredExtType, ...).
    3423             :   /// #2 Operand reuses:
    3424             :   /// ext opnd1 to ConsideredExtType.
    3425             :   /// \p PromotedInsts maps the instructions to their type before promotion.
    3426             :   static bool canGetThrough(const Instruction *Inst, Type *ConsideredExtType,
    3427             :                             const InstrToOrigTy &PromotedInsts, bool IsSExt);
    3428             : 
    3429             :   /// \brief Utility function to determine if \p OpIdx should be promoted when
    3430             :   /// promoting \p Inst.
    3431             :   static bool shouldExtOperand(const Instruction *Inst, int OpIdx) {
    3432        2140 :     return !(isa<SelectInst>(Inst) && OpIdx == 0);
    3433             :   }
    3434             : 
    3435             :   /// \brief Utility function to promote the operand of \p Ext when this
    3436             :   /// operand is a promotable trunc or sext or zext.
    3437             :   /// \p PromotedInsts maps the instructions to their type before promotion.
    3438             :   /// \p CreatedInstsCost[out] contains the cost of all instructions
    3439             :   /// created to promote the operand of Ext.
    3440             :   /// Newly added extensions are inserted in \p Exts.
    3441             :   /// Newly added truncates are inserted in \p Truncs.
    3442             :   /// Should never be called directly.
    3443             :   /// \return The promoted value which is used instead of Ext.
    3444             :   static Value *promoteOperandForTruncAndAnyExt(
    3445             :       Instruction *Ext, TypePromotionTransaction &TPT,
    3446             :       InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
    3447             :       SmallVectorImpl<Instruction *> *Exts,
    3448             :       SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI);
    3449             : 
    3450             :   /// \brief Utility function to promote the operand of \p Ext when this
    3451             :   /// operand is promotable and is not a supported trunc or sext.
    3452             :   /// \p PromotedInsts maps the instructions to their type before promotion.
    3453             :   /// \p CreatedInstsCost[out] contains the cost of all the instructions
    3454             :   /// created to promote the operand of Ext.
    3455             :   /// Newly added extensions are inserted in \p Exts.
    3456             :   /// Newly added truncates are inserted in \p Truncs.
    3457             :   /// Should never be called directly.
    3458             :   /// \return The promoted value which is used instead of Ext.
    3459             :   static Value *promoteOperandForOther(Instruction *Ext,
    3460             :                                        TypePromotionTransaction &TPT,
    3461             :                                        InstrToOrigTy &PromotedInsts,
    3462             :                                        unsigned &CreatedInstsCost,
    3463             :                                        SmallVectorImpl<Instruction *> *Exts,
    3464             :                                        SmallVectorImpl<Instruction *> *Truncs,
    3465             :                                        const TargetLowering &TLI, bool IsSExt);
    3466             : 
    3467             :   /// \see promoteOperandForOther.
    3468         273 :   static Value *signExtendOperandForOther(
    3469             :       Instruction *Ext, TypePromotionTransaction &TPT,
    3470             :       InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
    3471             :       SmallVectorImpl<Instruction *> *Exts,
    3472             :       SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
    3473             :     return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost,
    3474         273 :                                   Exts, Truncs, TLI, true);
    3475             :   }
    3476             : 
    3477             :   /// \see promoteOperandForOther.
    3478         262 :   static Value *zeroExtendOperandForOther(
    3479             :       Instruction *Ext, TypePromotionTransaction &TPT,
    3480             :       InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
    3481             :       SmallVectorImpl<Instruction *> *Exts,
    3482             :       SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
    3483             :     return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost,
    3484         262 :                                   Exts, Truncs, TLI, false);
    3485             :   }
    3486             : 
    3487             : public:
    3488             :   /// Type for the utility function that promotes the operand of Ext.
    3489             :   using Action = Value *(*)(Instruction *Ext, TypePromotionTransaction &TPT,
    3490             :                             InstrToOrigTy &PromotedInsts,
    3491             :                             unsigned &CreatedInstsCost,
    3492             :                             SmallVectorImpl<Instruction *> *Exts,
    3493             :                             SmallVectorImpl<Instruction *> *Truncs,
    3494             :                             const TargetLowering &TLI);
    3495             : 
    3496             :   /// \brief Given a sign/zero extend instruction \p Ext, return the approriate
    3497             :   /// action to promote the operand of \p Ext instead of using Ext.
    3498             :   /// \return NULL if no promotable action is possible with the current
    3499             :   /// sign extension.
    3500             :   /// \p InsertedInsts keeps track of all the instructions inserted by the
    3501             :   /// other CodeGenPrepare optimizations. This information is important
    3502             :   /// because we do not want to promote these instructions as CodeGenPrepare
    3503             :   /// will reinsert them later. Thus creating an infinite loop: create/remove.
    3504             :   /// \p PromotedInsts maps the instructions to their type before promotion.
    3505             :   static Action getAction(Instruction *Ext, const SetOfInstrs &InsertedInsts,
    3506             :                           const TargetLowering &TLI,
    3507             :                           const InstrToOrigTy &PromotedInsts);
    3508             : };
    3509             : 
    3510             : } // end anonymous namespace
    3511             : 
    3512        8825 : bool TypePromotionHelper::canGetThrough(const Instruction *Inst,
    3513             :                                         Type *ConsideredExtType,
    3514             :                                         const InstrToOrigTy &PromotedInsts,
    3515             :                                         bool IsSExt) {
    3516             :   // The promotion helper does not know how to deal with vector types yet.
    3517             :   // To be able to fix that, we would need to fix the places where we
    3518             :   // statically extend, e.g., constants and such.
    3519       17650 :   if (Inst->getType()->isVectorTy())
    3520             :     return false;
    3521             : 
    3522             :   // We can always get through zext.
    3523       11884 :   if (isa<ZExtInst>(Inst))
    3524             :     return true;
    3525             : 
    3526             :   // sext(sext) is ok too.
    3527        6779 :   if (IsSExt && isa<SExtInst>(Inst))
    3528             :     return true;
    3529             : 
    3530             :   // We can get through binary operator, if it is legal. In other words, the
    3531             :   // binary operator must have a nuw or nsw flag.
    3532        8184 :   const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Inst);
    3533        3209 :   if (BinOp && isa<OverflowingBinaryOperator>(BinOp) &&
    3534         476 :       ((!IsSExt && BinOp->hasNoUnsignedWrap()) ||
    3535         390 :        (IsSExt && BinOp->hasNoSignedWrap())))
    3536             :     return true;
    3537             : 
    3538             :   // Check if we can do the following simplification.
    3539             :   // ext(trunc(opnd)) --> ext(opnd)
    3540       10612 :   if (!isa<TruncInst>(Inst))
    3541             :     return false;
    3542             : 
    3543         966 :   Value *OpndVal = Inst->getOperand(0);
    3544             :   // Check if we can use this operand in the extension.
    3545             :   // If the type is larger than the result type of the extension, we cannot.
    3546        1449 :   if (!OpndVal->getType()->isIntegerTy() ||
    3547         966 :       OpndVal->getType()->getIntegerBitWidth() >
    3548         483 :           ConsideredExtType->getIntegerBitWidth())
    3549             :     return false;
    3550             : 
    3551             :   // If the operand of the truncate is not an instruction, we will not have
    3552             :   // any information on the dropped bits.
    3553             :   // (Actually we could for constant but it is not worth the extra logic).
    3554         394 :   Instruction *Opnd = dyn_cast<Instruction>(OpndVal);
    3555             :   if (!Opnd)
    3556             :     return false;
    3557             : 
    3558             :   // Check if the source of the type is narrow enough.
    3559             :   // I.e., check that trunc just drops extended bits of the same kind of
    3560             :   // the extension.
    3561             :   // #1 get the type of the operand and check the kind of the extended bits.
    3562             :   const Type *OpndType;
    3563         394 :   InstrToOrigTy::const_iterator It = PromotedInsts.find(Opnd);
    3564         394 :   if (It != PromotedInsts.end() && It->second.getInt() == IsSExt)
    3565           0 :     OpndType = It->second.getPointer();
    3566         852 :   else if ((IsSExt && isa<SExtInst>(Opnd)) || (!IsSExt && isa<ZExtInst>(Opnd)))
    3567          78 :     OpndType = Opnd->getOperand(0)->getType();
    3568             :   else
    3569             :     return false;
    3570             : 
    3571             :   // #2 check that the truncate just drops extended bits.
    3572          78 :   return Inst->getType()->getIntegerBitWidth() >=
    3573          39 :          OpndType->getIntegerBitWidth();
    3574             : }
    3575             : 
    3576       11270 : TypePromotionHelper::Action TypePromotionHelper::getAction(
    3577             :     Instruction *Ext, const SetOfInstrs &InsertedInsts,
    3578             :     const TargetLowering &TLI, const InstrToOrigTy &PromotedInsts) {
    3579             :   assert((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) &&
    3580             :          "Unexpected instruction type");
    3581       33810 :   Instruction *ExtOpnd = dyn_cast<Instruction>(Ext->getOperand(0));
    3582       11270 :   Type *ExtTy = Ext->getType();
    3583       22540 :   bool IsSExt = isa<SExtInst>(Ext);
    3584             :   // If the operand of the extension is not an instruction, we cannot
    3585             :   // get through.
    3586             :   // If it, check we can get through.
    3587       11270 :   if (!ExtOpnd || !canGetThrough(ExtOpnd, ExtTy, PromotedInsts, IsSExt))
    3588             :     return nullptr;
    3589             : 
    3590             :   // Do not promote if the operand has been added by codegenprepare.
    3591             :   // Otherwise, it means we are undoing an optimization that is likely to be
    3592             :   // redone, thus causing potential infinite loop.
    3593        1346 :   if (isa<TruncInst>(ExtOpnd) && InsertedInsts.count(ExtOpnd))
    3594             :     return nullptr;
    3595             : 
    3596             :   // SExt or Trunc instructions.
    3597             :   // Return the related handler.
    3598        2551 :   if (isa<SExtInst>(ExtOpnd) || isa<TruncInst>(ExtOpnd) ||
    3599         629 :       isa<ZExtInst>(ExtOpnd))
    3600             :     return promoteOperandForTruncAndAnyExt;
    3601             : 
    3602             :   // Regular instruction.
    3603             :   // Abort early if we will have to insert non-free instructions.
    3604        1070 :   if (!ExtOpnd->hasOneUse() && !TLI.isTruncateFree(ExtTy, ExtOpnd->getType()))
    3605             :     return nullptr;
    3606         535 :   return IsSExt ? signExtendOperandForOther : zeroExtendOperandForOther;
    3607             : }
    3608             : 
    3609         108 : Value *TypePromotionHelper::promoteOperandForTruncAndAnyExt(
    3610             :     Instruction *SExt, TypePromotionTransaction &TPT,
    3611             :     InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
    3612             :     SmallVectorImpl<Instruction *> *Exts,
    3613             :     SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
    3614             :   // By construction, the operand of SExt is an instruction. Otherwise we cannot
    3615             :   // get through it and this method should not be called.
    3616         324 :   Instruction *SExtOpnd = cast<Instruction>(SExt->getOperand(0));
    3617         108 :   Value *ExtVal = SExt;
    3618         108 :   bool HasMergedNonFreeExt = false;
    3619         216 :   if (isa<ZExtInst>(SExtOpnd)) {
    3620             :     // Replace s|zext(zext(opnd))
    3621             :     // => zext(opnd).
    3622          94 :     HasMergedNonFreeExt = !TLI.isExtFree(SExtOpnd);
    3623             :     Value *ZExt =
    3624         188 :         TPT.createZExt(SExt, SExtOpnd->getOperand(0), SExt->getType());
    3625          94 :     TPT.replaceAllUsesWith(SExt, ZExt);
    3626          94 :     TPT.eraseInstruction(SExt);
    3627          94 :     ExtVal = ZExt;
    3628             :   } else {
    3629             :     // Replace z|sext(trunc(opnd)) or sext(sext(opnd))
    3630             :     // => z|sext(opnd).
    3631          28 :     TPT.setOperand(SExt, 0, SExtOpnd->getOperand(0));
    3632             :   }
    3633         108 :   CreatedInstsCost = 0;
    3634             : 
    3635             :   // Remove dead code.
    3636         216 :   if (SExtOpnd->use_empty())
    3637          85 :     TPT.eraseInstruction(SExtOpnd);
    3638             : 
    3639             :   // Check if the extension is still needed.
    3640         108 :   Instruction *ExtInst = dyn_cast<Instruction>(ExtVal);
    3641         214 :   if (!ExtInst || ExtInst->getType() != ExtInst->getOperand(0)->getType()) {
    3642         108 :     if (ExtInst) {
    3643         106 :       if (Exts)
    3644          94 :         Exts->push_back(ExtInst);
    3645         106 :       CreatedInstsCost = !TLI.isExtFree(ExtInst) && !HasMergedNonFreeExt;
    3646             :     }
    3647             :     return ExtVal;
    3648             :   }
    3649             : 
    3650             :   // At this point we have: ext ty opnd to ty.
    3651             :   // Reassign the uses of ExtInst to the opnd and remove ExtInst.
    3652           0 :   Value *NextVal = ExtInst->getOperand(0);
    3653           0 :   TPT.eraseInstruction(ExtInst, NextVal);
    3654           0 :   return NextVal;
    3655             : }
    3656             : 
    3657         535 : Value *TypePromotionHelper::promoteOperandForOther(
    3658             :     Instruction *Ext, TypePromotionTransaction &TPT,
    3659             :     InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
    3660             :     SmallVectorImpl<Instruction *> *Exts,
    3661             :     SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI,
    3662             :     bool IsSExt) {
    3663             :   // By construction, the operand of Ext is an instruction. Otherwise we cannot
    3664             :   // get through it and this method should not be called.
    3665        1605 :   Instruction *ExtOpnd = cast<Instruction>(Ext->getOperand(0));
    3666         535 :   CreatedInstsCost = 0;
    3667        1070 :   if (!ExtOpnd->hasOneUse()) {
    3668             :     // ExtOpnd will be promoted.
    3669             :     // All its uses, but Ext, will need to use a truncated value of the
    3670             :     // promoted version.
    3671             :     // Create the truncate now.
    3672          48 :     Value *Trunc = TPT.createTrunc(Ext, ExtOpnd->getType());
    3673          48 :     if (Instruction *ITrunc = dyn_cast<Instruction>(Trunc)) {
    3674             :       // Insert it just after the definition.
    3675          48 :       ITrunc->moveAfter(ExtOpnd);
    3676          48 :       if (Truncs)
    3677           0 :         Truncs->push_back(ITrunc);
    3678             :     }
    3679             : 
    3680          48 :     TPT.replaceAllUsesWith(ExtOpnd, Trunc);
    3681             :     // Restore the operand of Ext (which has been replaced by the previous call
    3682             :     // to replaceAllUsesWith) to avoid creating a cycle trunc <-> sext.
    3683          48 :     TPT.setOperand(Ext, 0, ExtOpnd);
    3684             :   }
    3685             : 
    3686             :   // Get through the Instruction:
    3687             :   // 1. Update its type.
    3688             :   // 2. Replace the uses of Ext by Inst.
    3689             :   // 3. Extend each operand that needs to be extended.
    3690             : 
    3691             :   // Remember the original type of the instruction before promotion.
    3692             :   // This is useful to know that the high bits are sign extended bits.
    3693        1070 :   PromotedInsts.insert(std::pair<Instruction *, TypeIsSExt>(
    3694        1070 :       ExtOpnd, TypeIsSExt(ExtOpnd->getType(), IsSExt)));
    3695             :   // Step #1.
    3696         535 :   TPT.mutateType(ExtOpnd, Ext->getType());
    3697             :   // Step #2.
    3698         535 :   TPT.replaceAllUsesWith(Ext, ExtOpnd);
    3699             :   // Step #3.
    3700         535 :   Instruction *ExtForOpnd = Ext;
    3701             : 
    3702             :   DEBUG(dbgs() << "Propagate Ext to operands\n");
    3703        2140 :   for (int OpIdx = 0, EndOpIdx = ExtOpnd->getNumOperands(); OpIdx != EndOpIdx;
    3704             :        ++OpIdx) {
    3705             :     DEBUG(dbgs() << "Operand:\n" << *(ExtOpnd->getOperand(OpIdx)) << '\n');
    3706        2140 :     if (ExtOpnd->getOperand(OpIdx)->getType() == Ext->getType() ||
    3707        1070 :         !shouldExtOperand(ExtOpnd, OpIdx)) {
    3708             :       DEBUG(dbgs() << "No need to propagate\n");
    3709           0 :       continue;
    3710             :     }
    3711             :     // Check if we can statically extend the operand.
    3712        2140 :     Value *Opnd = ExtOpnd->getOperand(OpIdx);
    3713        1486 :     if (const ConstantInt *Cst = dyn_cast<ConstantInt>(Opnd)) {
    3714             :       DEBUG(dbgs() << "Statically extend\n");
    3715         832 :       unsigned BitWidth = Ext->getType()->getIntegerBitWidth();
    3716         184 :       APInt CstVal = IsSExt ? Cst->getValue().sext(BitWidth)
    3717        1248 :                             : Cst->getValue().zext(BitWidth);
    3718         416 :       TPT.setOperand(ExtOpnd, OpIdx, ConstantInt::get(Ext->getType(), CstVal));
    3719             :       continue;
    3720             :     }
    3721             :     // UndefValue are typed, so we have to statically sign extend them.
    3722        1308 :     if (isa<UndefValue>(Opnd)) {
    3723             :       DEBUG(dbgs() << "Statically extend\n");
    3724           0 :       TPT.setOperand(ExtOpnd, OpIdx, UndefValue::get(Ext->getType()));
    3725           0 :       continue;
    3726             :     }
    3727             : 
    3728             :     // Otherwise we have to explicity sign extend the operand.
    3729             :     // Check if Ext was reused to extend an operand.
    3730         654 :     if (!ExtForOpnd) {
    3731             :       // If yes, create a new one.
    3732             :       DEBUG(dbgs() << "More operands to ext\n");
    3733         149 :       Value *ValForExtOpnd = IsSExt ? TPT.createSExt(Ext, Opnd, Ext->getType())
    3734         149 :         : TPT.createZExt(Ext, Opnd, Ext->getType());
    3735         244 :       if (!isa<Instruction>(ValForExtOpnd)) {
    3736           6 :         TPT.setOperand(ExtOpnd, OpIdx, ValForExtOpnd);
    3737           6 :         continue;
    3738             :       }
    3739         226 :       ExtForOpnd = cast<Instruction>(ValForExtOpnd);
    3740             :     }
    3741         648 :     if (Exts)
    3742         429 :       Exts->push_back(ExtForOpnd);
    3743         648 :     TPT.setOperand(ExtForOpnd, 0, Opnd);
    3744             : 
    3745             :     // Move the sign extension before the insertion point.
    3746         648 :     TPT.moveBefore(ExtForOpnd, ExtOpnd);
    3747         648 :     TPT.setOperand(ExtOpnd, OpIdx, ExtForOpnd);
    3748         648 :     CreatedInstsCost += !TLI.isExtFree(ExtForOpnd);
    3749             :     // If more sext are required, new instructions will have to be created.
    3750         648 :     ExtForOpnd = nullptr;
    3751             :   }
    3752         535 :   if (ExtForOpnd == Ext) {
    3753             :     DEBUG(dbgs() << "Extension is useless now\n");
    3754           0 :     TPT.eraseInstruction(Ext);
    3755             :   }
    3756         535 :   return ExtOpnd;
    3757             : }
    3758             : 
    3759             : /// Check whether or not promoting an instruction to a wider type is profitable.
    3760             : /// \p NewCost gives the cost of extension instructions created by the
    3761             : /// promotion.
    3762             : /// \p OldCost gives the cost of extension instructions before the promotion
    3763             : /// plus the number of instructions that have been
    3764             : /// matched in the addressing mode the promotion.
    3765             : /// \p PromotedOperand is the value that has been promoted.
    3766             : /// \return True if the promotion is profitable, false otherwise.
    3767             : bool AddressingModeMatcher::isPromotionProfitable(
    3768             :     unsigned NewCost, unsigned OldCost, Value *PromotedOperand) const {
    3769             :   DEBUG(dbgs() << "OldCost: " << OldCost << "\tNewCost: " << NewCost << '\n');
    3770             :   // The cost of the new extensions is greater than the cost of the
    3771             :   // old extension plus what we folded.
    3772             :   // This is not profitable.
    3773         159 :   if (NewCost > OldCost)
    3774             :     return false;
    3775         148 :   if (NewCost < OldCost)
    3776             :     return true;
    3777             :   // The promotion is neutral but it may help folding the sign extension in
    3778             :   // loads for instance.
    3779             :   // Check that we did not create an illegal instruction.
    3780          58 :   return isPromotedInstructionLegal(TLI, DL, PromotedOperand);
    3781             : }
    3782             : 
    3783             : /// Given an instruction or constant expr, see if we can fold the operation
    3784             : /// into the addressing mode. If so, update the addressing mode and return
    3785             : /// true, otherwise return false without modifying AddrMode.
    3786             : /// If \p MovedAway is not NULL, it contains the information of whether or
    3787             : /// not AddrInst has to be folded into the addressing mode on success.
    3788             : /// If \p MovedAway == true, \p AddrInst will not be part of the addressing
    3789             : /// because it has been moved away.
    3790             : /// Thus AddrInst must not be added in the matched instructions.
    3791             : /// This state can happen when AddrInst is a sext, since it may be moved away.
    3792             : /// Therefore, AddrInst may not be valid when MovedAway is true and it must
    3793             : /// not be referenced anymore.
    3794     1021668 : bool AddressingModeMatcher::matchOperationAddr(User *AddrInst, unsigned Opcode,
    3795             :                                                unsigned Depth,
    3796             :                                                bool *MovedAway) {
    3797             :   // Avoid exponential behavior on extremely deep expression trees.
    3798     1021668 :   if (Depth >= 5) return false;
    3799             : 
    3800             :   // By default, all matched instructions stay in place.
    3801     1021632 :   if (MovedAway)
    3802      403318 :     *MovedAway = false;
    3803             : 
    3804     1021632 :   switch (Opcode) {
    3805         742 :   case Instruction::PtrToInt:
    3806             :     // PtrToInt is always a noop, as we know that the int type is pointer sized.
    3807         742 :     return matchAddr(AddrInst->getOperand(0), Depth);
    3808        3323 :   case Instruction::IntToPtr: {
    3809        6646 :     auto AS = AddrInst->getType()->getPointerAddressSpace();
    3810        6646 :     auto PtrTy = MVT::getIntegerVT(DL.getPointerSizeInBits(AS));
    3811             :     // This inttoptr is a no-op if the integer type is pointer sized.
    3812        9969 :     if (TLI.getValueType(DL, AddrInst->getOperand(0)->getType()) == PtrTy)
    3813        3283 :       return matchAddr(AddrInst->getOperand(0), Depth);
    3814             :     return false;
    3815             :   }
    3816      344230 :   case Instruction::BitCast:
    3817             :     // BitCast is always a noop, and we can handle it as long as it is
    3818             :     // int->int or pointer->pointer (we don't want int<->fp or something).
    3819      688695 :     if ((AddrInst->getOperand(0)->getType()->isPointerTy() ||
    3820      688930 :          AddrInst->getOperand(0)->getType()->isIntegerTy()) &&
    3821             :         // Don't touch identity bitcasts.  These were probably put here by LSR,
    3822             :         // and we don't want to mess around with them.  Assume it knows what it
    3823             :         // is doing.
    3824      344230 :         AddrInst->getOperand(0)->getType() != AddrInst->getType())
    3825      342780 :       return matchAddr(AddrInst->getOperand(0), Depth);
    3826             :     return false;
    3827         293 :   case Instruction::AddrSpaceCast: {
    3828             :     unsigned SrcAS
    3829         586 :       = AddrInst->getOperand(0)->getType()->getPointerAddressSpace();
    3830         586 :     unsigned DestAS = AddrInst->getType()->getPointerAddressSpace();
    3831         293 :     if (TLI.isNoopAddrSpaceCast(SrcAS, DestAS))
    3832          95 :       return matchAddr(AddrInst->getOperand(0), Depth);
    3833             :     return false;
    3834             :   }
    3835        2438 :   case Instruction::Add: {
    3836             :     // Check to see if we can merge in the RHS then the LHS.  If so, we win.
    3837        2438 :     ExtAddrMode BackupAddrMode = AddrMode;
    3838        4876 :     unsigned OldSize = AddrModeInsts.size();
    3839             :     // Start a transaction at this point.
    3840             :     // The LHS may match but not the RHS.
    3841             :     // Therefore, we need a higher level restoration point to undo partially
    3842             :     // matched operation.
    3843             :     TypePromotionTransaction::ConstRestorationPt LastKnownGood =
    3844        4876 :         TPT.getRestorationPoint();
    3845             : 
    3846        7279 :     if (matchAddr(AddrInst->getOperand(1), Depth+1) &&
    3847        4806 :         matchAddr(AddrInst->getOperand(0), Depth+1))
    3848             :       return true;
    3849             : 
    3850             :     // Restore the old addr mode info.
    3851         467 :     AddrMode = BackupAddrMode;
    3852         467 :     AddrModeInsts.resize(OldSize);
    3853         467 :     TPT.rollback(LastKnownGood);
    3854             : 
    3855             :     // Otherwise this was over-aggressive.  Try merging in the LHS then the RHS.
    3856        1287 :     if (matchAddr(AddrInst->getOperand(0), Depth+1) &&
    3857         706 :         matchAddr(AddrInst->getOperand(1), Depth+1))
    3858             :       return true;
    3859             : 
    3860             :     // Otherwise we definitely can't merge the ADD in.
    3861         465 :     AddrMode = BackupAddrMode;
    3862         465 :     AddrModeInsts.resize(OldSize);
    3863         465 :     TPT.rollback(LastKnownGood);
    3864         465 :     break;
    3865             :   }
    3866             :   //case Instruction::Or:
    3867             :   // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD.
    3868             :   //break;
    3869        2088 :   case Instruction::Mul:
    3870             :   case Instruction::Shl: {
    3871             :     // Can only handle X*C and X << C.
    3872        4078 :     ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1));
    3873             :     if (!RHS)
    3874             :       return false;
    3875        1990 :     int64_t Scale = RHS->getSExtValue();
    3876        1990 :     if (Opcode == Instruction::Shl)
    3877        1251 :       Scale = 1LL << Scale;
    3878             : 
    3879        1990 :     return matchScaledValue(AddrInst->getOperand(0), Scale, Depth);
    3880             :   }
    3881      563189 :   case Instruction::GetElementPtr: {
    3882             :     // Scan the GEP.  We check it if it contains constant offsets and at most
    3883             :     // one variable offset.
    3884      563189 :     int VariableOperand = -1;
    3885      563189 :     unsigned VariableScale = 0;
    3886             : 
    3887      563189 :     int64_t ConstantOffset = 0;
    3888      563189 :     gep_type_iterator GTI = gep_type_begin(AddrInst);
    3889     2242669 :     for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) {
    3890       82348 :       if (StructType *STy = GTI.getStructTypeOrNull()) {
    3891       82348 :         const StructLayout *SL = DL.getStructLayout(STy);
    3892             :         unsigned Idx =
    3893      247044 :           cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue();
    3894       82348 :         ConstantOffset += SL->getElementOffset(Idx);
    3895             :       } else {
    3896     1034921 :         uint64_t TypeSize = DL.getTypeAllocSize(GTI.getIndexedType());
    3897     1949090 :         if (ConstantInt *CI = dyn_cast<ConstantInt>(AddrInst->getOperand(i))) {
    3898      914169 :           ConstantOffset += CI->getSExtValue()*TypeSize;
    3899      120752 :         } else if (TypeSize) {  // Scales of zero don't do anything.
    3900             :           // We only allow one variable index at the moment.
    3901      120748 :           if (VariableOperand != -1)
    3902             :             return false;
    3903             : 
    3904             :           // Remember the variable index.
    3905      119770 :           VariableOperand = i;
    3906      119770 :           VariableScale = TypeSize;
    3907             :         }
    3908             :       }
    3909             :     }
    3910             : 
    3911             :     // A common case is for the GEP to only do a constant offset.  In this case,
    3912             :     // just add it to the disp field and check validity.
    3913      562211 :     if (VariableOperand == -1) {
    3914      443419 :       AddrMode.BaseOffs += ConstantOffset;
    3915      813891 :       if (ConstantOffset == 0 ||
    3916      370472 :           TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) {
    3917             :         // Check to see if we can fold the base pointer in too.
    3918      880666 :         if (matchAddr(AddrInst->getOperand(0), Depth+1))
    3919             :           return true;
    3920             :       }
    3921        3086 :       AddrMode.BaseOffs -= ConstantOffset;
    3922        3086 :       return false;
    3923             :     }
    3924             : 
    3925             :     // Save the valid addressing mode in case we can't match.
    3926      118792 :     ExtAddrMode BackupAddrMode = AddrMode;
    3927      237584 :     unsigned OldSize = AddrModeInsts.size();
    3928             : 
    3929             :     // See if the scale and offset amount is valid for this target.
    3930      118792 :     AddrMode.BaseOffs += ConstantOffset;
    3931             : 
    3932             :     // Match the base operand of the GEP.
    3933      237584 :     if (!matchAddr(AddrInst->getOperand(0), Depth+1)) {
    3934             :       // If it couldn't be matched, just stuff the value in a register.
    3935           5 :       if (AddrMode.HasBaseReg) {
    3936           0 :         AddrMode = BackupAddrMode;
    3937           0 :         AddrModeInsts.resize(OldSize);
    3938           0 :         return false;
    3939             :       }
    3940           5 :       AddrMode.HasBaseReg = true;
    3941          10 :       AddrMode.BaseReg = AddrInst->getOperand(0);
    3942             :     }
    3943             : 
    3944             :     // Match the remaining variable portion of the GEP.
    3945      237584 :     if (!matchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale,
    3946             :                           Depth)) {
    3947             :       // If it couldn't be matched, try stuffing the base into a register
    3948             :       // instead of matching it, and retrying the match of the scale.
    3949       49150 :       AddrMode = BackupAddrMode;
    3950       49150 :       AddrModeInsts.resize(OldSize);
    3951       49150 :       if (AddrMode.HasBaseReg)
    3952             :         return false;
    3953       49150 :       AddrMode.HasBaseReg = true;
    3954       98300 :       AddrMode.BaseReg = AddrInst->getOperand(0);
    3955       49150 :       AddrMode.BaseOffs += ConstantOffset;
    3956       98300 :       if (!matchScaledValue(AddrInst->getOperand(VariableOperand),
    3957             :                             VariableScale, Depth)) {
    3958             :         // If even that didn't work, bail.
    3959       13937 :         AddrMode = BackupAddrMode;
    3960       13937 :         AddrModeInsts.resize(OldSize);
    3961       13937 :         return false;
    3962             :       }
    3963             :     }
    3964             : 
    3965             :     return true;
    3966             :   }
    3967         977 :   case Instruction::SExt:
    3968             :   case Instruction::ZExt: {
    3969         976 :     Instruction *Ext = dyn_cast<Instruction>(AddrInst);
    3970             :     if (!Ext)
    3971             :       return false;
    3972             : 
    3973             :     // Try to move this ext out of the way of the addressing mode.
    3974             :     // Ask for a method for doing so.
    3975             :     TypePromotionHelper::Action TPH =
    3976         976 :         TypePromotionHelper::getAction(Ext, InsertedInsts, TLI, PromotedInsts);
    3977         976 :     if (!TPH)
    3978             :       return false;
    3979             : 
    3980             :     TypePromotionTransaction::ConstRestorationPt LastKnownGood =
    3981         430 :         TPT.getRestorationPoint();
    3982         215 :     unsigned CreatedInstsCost = 0;
    3983         215 :     unsigned ExtCost = !TLI.isExtFree(Ext);
    3984             :     Value *PromotedOperand =
    3985         215 :         TPH(Ext, TPT, PromotedInsts, CreatedInstsCost, nullptr, nullptr, TLI);
    3986             :     // SExt has been moved away.
    3987             :     // Thus either it will be rematched later in the recursive calls or it is
    3988             :     // gone. Anyway, we must not fold it into the addressing mode at this point.
    3989             :     // E.g.,
    3990             :     // op = add opnd, 1
    3991             :     // idx = ext op
    3992             :     // addr = gep base, idx
    3993             :     // is now:
    3994             :     // promotedOpnd = ext opnd            <- no match here
    3995             :     // op = promoted_add promotedOpnd, 1  <- match (later in recursive calls)
    3996             :     // addr = gep base, op                <- match
    3997         215 :     if (MovedAway)
    3998         215 :       *MovedAway = true;
    3999             : 
    4000             :     assert(PromotedOperand &&
    4001             :            "TypePromotionHelper should have filtered out those cases");
    4002             : 
    4003         215 :     ExtAddrMode BackupAddrMode = AddrMode;
    4004         430 :     unsigned OldSize = AddrModeInsts.size();
    4005             : 
    4006         273 :     if (!matchAddr(PromotedOperand, Depth) ||
    4007             :         // The total of the new cost is equal to the cost of the created
    4008             :         // instructions.
    4009             :         // The total of the old cost is equal to the cost of the extension plus
    4010             :         // what we have saved in the addressing mode.
    4011         217 :         !isPromotionProfitable(CreatedInstsCost,
    4012         318 :                                ExtCost + (AddrModeInsts.size() - OldSize),
    4013             :                                PromotedOperand)) {
    4014          97 :       AddrMode = BackupAddrMode;
    4015          97 :       AddrModeInsts.resize(OldSize);
    4016             :       DEBUG(dbgs() << "Sign extension does not pay off: rollback\n");
    4017          97 :       TPT.rollback(LastKnownGood);
    4018          97 :       return false;
    4019             :     }
    4020             :     return true;
    4021             :   }
    4022             :   }
    4023             :   return false;
    4024             : }
    4025             : 
    4026             : /// If we can, try to add the value of 'Addr' into the current addressing mode.
    4027             : /// If Addr can't be added to AddrMode this returns false and leaves AddrMode
    4028             : /// unmodified. This assumes that Addr is either a pointer type or intptr_t
    4029             : /// for the target.
    4030             : ///
    4031     1926141 : bool AddressingModeMatcher::matchAddr(Value *Addr, unsigned Depth) {
    4032             :   // Start a transaction at this point that we will rollback if the matching
    4033             :   // fails.
    4034             :   TypePromotionTransaction::ConstRestorationPt LastKnownGood =
    4035     3852282 :       TPT.getRestorationPoint();
    4036     1928021 :   if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) {
    4037             :     // Fold in immediates if legal for the target.
    4038        1880 :     AddrMode.BaseOffs += CI->getSExtValue();
    4039        1880 :     if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
    4040             :       return true;
    4041         519 :     AddrMode.BaseOffs -= CI->getSExtValue();
    4042     2716235 :   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
    4043             :     // If this is a global variable, try to fold it into the addressing mode.
    4044      791974 :     if (!AddrMode.BaseGV) {
    4045      791974 :       AddrMode.BaseGV = GV;
    4046      791974 :       if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
    4047             :         return true;
    4048      162584 :       AddrMode.BaseGV = nullptr;
    4049             :     }
    4050     2264574 :   } else if (Instruction *I = dyn_cast<Instruction>(Addr)) {
    4051      403354 :     ExtAddrMode BackupAddrMode = AddrMode;
    4052      806708 :     unsigned OldSize = AddrModeInsts.size();
    4053             : 
    4054             :     // Check to see if it is possible to fold this operation.
    4055      403354 :     bool MovedAway = false;
    4056      806708 :     if (matchOperationAddr(I, I->getOpcode(), Depth, &MovedAway)) {
    4057             :       // This instruction may have been moved away. If so, there is nothing
    4058             :       // to check here.
    4059      276852 :       if (MovedAway)
    4060      274693 :         return true;
    4061             :       // Okay, it's possible to fold this.  Check to see if it is actually
    4062             :       // *profitable* to do so.  We use a simple cost model to avoid increasing
    4063             :       // register pressure too much.
    4064      687922 :       if (I->hasOneUse() ||
    4065      134454 :           isProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) {
    4066      274575 :         AddrModeInsts.push_back(I);
    4067      274575 :         return true;
    4068             :       }
    4069             : 
    4070             :       // It isn't profitable to do this, roll back.
    4071             :       //cerr << "NOT FOLDING: " << *I;
    4072        2159 :       AddrMode = BackupAddrMode;
    4073        2159 :       AddrModeInsts.resize(OldSize);
    4074        2159 :       TPT.rollback(LastKnownGood);
    4075             :     }
    4076     1347247 :   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
    4077      618314 :     if (matchOperationAddr(CE, CE->getOpcode(), Depth))
    4078             :       return true;
    4079         192 :     TPT.rollback(LastKnownGood);
    4080      221238 :   } else if (isa<ConstantPointerNull>(Addr)) {
    4081             :     // Null pointer gets folded without affecting the addressing mode.
    4082             :     return true;
    4083             :   }
    4084             : 
    4085             :   // Worse case, the target should support [reg] addressing modes. :)
    4086      401561 :   if (!AddrMode.HasBaseReg) {
    4087      392638 :     AddrMode.HasBaseReg = true;
    4088      392638 :     AddrMode.BaseReg = Addr;
    4089             :     // Still check for legality in case the target supports [imm] but not [i+r].
    4090      392638 :     if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
    4091             :       return true;
    4092          10 :     AddrMode.HasBaseReg = false;
    4093          10 :     AddrMode.BaseReg = nullptr;
    4094             :   }
    4095             : 
    4096             :   // If the base register is already taken, see if we can do [r+r].
    4097        8933 :   if (AddrMode.Scale == 0) {
    4098        8414 :     AddrMode.Scale = 1;
    4099        8414 :     AddrMode.ScaledReg = Addr;
    4100        8414 :     if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
    4101             :       return true;
    4102        2387 :     AddrMode.Scale = 0;
    4103        2387 :     AddrMode.ScaledReg = nullptr;
    4104             :   }
    4105             :   // Couldn't match.
    4106        2906 :   TPT.rollback(LastKnownGood);
    4107        2906 :   return false;
    4108             : }
    4109             : 
    4110             : /// Check to see if all uses of OpVal by the specified inline asm call are due
    4111             : /// to memory operands. If so, return true, otherwise return false.
    4112          14 : static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal,
    4113             :                                     const TargetLowering &TLI,
    4114             :                                     const TargetRegisterInfo &TRI) {
    4115          28 :   const Function *F = CI->getFunction();
    4116             :   TargetLowering::AsmOperandInfoVector TargetConstraints =
    4117             :       TLI.ParseConstraints(F->getParent()->getDataLayout(), &TRI,
    4118          42 :                             ImmutableCallSite(CI));
    4119             : 
    4120          82 :   for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
    4121         132 :     TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
    4122             : 
    4123             :     // Compute the constraint code and ConstraintType to use.
    4124          66 :     TLI.ComputeConstraintToUse(OpInfo, SDValue());
    4125             : 
    4126             :     // If this asm operand is our Value*, and if it isn't an indirect memory
    4127             :     // operand, we can't fold it!
    4128          94 :     if (OpInfo.CallOperandVal == OpVal &&
    4129          44 :         (OpInfo.ConstraintType != TargetLowering::C_Memory ||
    4130          16 :          !OpInfo.isIndirect))
    4131             :       return false;
    4132             :   }
    4133             : 
    4134             :   return true;
    4135             : }
    4136             : 
    4137             : // Max number of memory uses to look at before aborting the search to conserve
    4138             : // compile time.
    4139             : static constexpr int MaxMemoryUsesToScan = 20;
    4140             : 
    4141             : /// Recursively walk all the uses of I until we find a memory use.
    4142             : /// If we find an obviously non-foldable instruction, return true.
    4143             : /// Add the ultimately found memory instructions to MemoryUses.
    4144       11084 : static bool FindAllMemoryUses(
    4145             :     Instruction *I,
    4146             :     SmallVectorImpl<std::pair<Instruction *, unsigned>> &MemoryUses,
    4147             :     SmallPtrSetImpl<Instruction *> &ConsideredInsts, const TargetLowering &TLI,
    4148             :     const TargetRegisterInfo &TRI, int SeenInsts = 0) {
    4149             :   // If we already considered this instruction, we're done.
    4150       11084 :   if (!ConsideredInsts.insert(I).second)
    4151             :     return false;
    4152             : 
    4153             :   // If this is an obviously unfoldable instruction, bail out.
    4154       11084 :   if (!MightBeFoldableInst(I))
    4155             :     return true;
    4156             : 
    4157        9730 :   const bool OptSize = I->getFunction()->optForSize();
    4158             : 
    4159             :   // Loop over all the uses, recursively processing them.
    4160       42331 :   for (Use &U : I->uses()) {
    4161             :     // Conservatively return true if we're seeing a large number or a deep chain
    4162             :     // of users. This avoids excessive compilation times in pathological cases.
    4163       16588 :     if (SeenInsts++ >= MaxMemoryUsesToScan)
    4164             :       return true;
    4165             : 
    4166       33016 :     Instruction *UserI = cast<Instruction>(U.getUser());
    4167       10430 :     if (LoadInst *LI = dyn_cast<LoadInst>(UserI)) {
    4168       15645 :       MemoryUses.push_back(std::make_pair(LI, U.getOperandNo()));
    4169        5215 :       continue;
    4170             :     }
    4171             : 
    4172        2999 :     if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) {
    4173        2999 :       unsigned opNo = U.getOperandNo();
    4174        2999 :       if (opNo != StoreInst::getPointerOperandIndex())
    4175             :         return true; // Storing addr, not into addr.
    4176        8958 :       MemoryUses.push_back(std::make_pair(SI, opNo));
    4177        2986 :       continue;
    4178             :     }
    4179             : 
    4180          68 :     if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(UserI)) {
    4181          68 :       unsigned opNo = U.getOperandNo();
    4182          68 :       if (opNo != AtomicRMWInst::getPointerOperandIndex())
    4183             :         return true; // Storing addr, not into addr.
    4184         204 :       MemoryUses.push_back(std::make_pair(RMW, opNo));
    4185          68 :       continue;
    4186             :     }
    4187             : 
    4188           2 :     if (AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(UserI)) {
    4189           2 :       unsigned opNo = U.getOperandNo();
    4190           2 :       if (opNo != AtomicCmpXchgInst::getPointerOperandIndex())
    4191             :         return true; // Storing addr, not into addr.
    4192           6 :       MemoryUses.push_back(std::make_pair(CmpX, opNo));
    4193           2 :       continue;
    4194             :     }
    4195             : 
    4196         553 :     if (CallInst *CI = dyn_cast<CallInst>(UserI)) {
    4197             :       // If this is a cold call, we can sink the addressing calculation into
    4198             :       // the cold path.  See optimizeCallInst
    4199        1111 :       if (!OptSize && CI->hasFnAttr(Attribute::Cold))
    4200           7 :         continue;
    4201             : 
    4202         560 :       InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue());
    4203             :       if (!IA) return true;
    4204             : 
    4205             :       // If this is a memory operand, we're cool, otherwise bail out.
    4206          14 :       if (!IsOperandAMemoryOperand(CI, IA, I, TLI, TRI))
    4207             :         return true;
    4208           2 :       continue;
    4209             :     }
    4210             : 
    4211        7671 :     if (FindAllMemoryUses(UserI, MemoryUses, ConsideredInsts, TLI, TRI,
    4212             :                           SeenInsts))
    4213             :       return true;
    4214             :   }
    4215             : 
    4216             :   return false;
    4217             : }
    4218             : 
    4219             : /// Return true if Val is already known to be live at the use site that we're
    4220             : /// folding it into. If so, there is no cost to include it in the addressing
    4221             : /// mode. KnownLive1 and KnownLive2 are two values that we know are live at the
    4222             : /// instruction already.
    4223      260228 : bool AddressingModeMatcher::valueAlreadyLiveAtInst(Value *Val,Value *KnownLive1,
    4224             :                                                    Value *KnownLive2) {
    4225             :   // If Val is either of the known-live values, we know it is live!
    4226      260228 :   if (Val == nullptr || Val == KnownLive1 || Val == KnownLive2)
    4227             :     return true;
    4228             : 
    4229             :   // All values other than instructions and arguments (e.g. constants) are live.
    4230      383355 :   if (!isa<Instruction>(Val) && !isa<Argument>(Val)) return true;
    4231             : 
    4232             :   // If Val is a constant sized alloca in the entry block, it is live, this is
    4233             :   // true because it is just a reference to the stack/frame pointer, which is
    4234             :   // live for the whole function.
    4235      150093 :   if (AllocaInst *AI = dyn_cast<AllocaInst>(Val))
    4236       17365 :     if (AI->isStaticAlloca())
    4237             :       return true;
    4238             : 
    4239             :   // Check to see if this value is already used in the memory instruction's
    4240             :   // block.  If so, it's already live into the block at the very least, so we
    4241             :   // can reasonably fold it.
    4242      115394 :   return Val->isUsedInBasicBlock(MemoryInst->getParent());
    4243             : }
    4244             : 
    4245             : /// It is possible for the addressing mode of the machine to fold the specified
    4246             : /// instruction into a load or store that ultimately uses it.
    4247             : /// However, the specified instruction has multiple uses.
    4248             : /// Given this, it may actually increase register pressure to fold it
    4249             : /// into the load. For example, consider this code:
    4250             : ///
    4251             : ///     X = ...
    4252             : ///     Y = X+1
    4253             : ///     use(Y)   -> nonload/store
    4254             : ///     Z = Y+1
    4255             : ///     load Z
    4256             : ///
    4257             : /// In this case, Y has multiple uses, and can be folded into the load of Z
    4258             : /// (yielding load [X+2]).  However, doing this will cause both "X" and "X+1" to
    4259             : /// be live at the use(Y) line.  If we don't fold Y into load Z, we use one
    4260             : /// fewer register.  Since Y can't be folded into "use(Y)" we don't increase the
    4261             : /// number of computations either.
    4262             : ///
    4263             : /// Note that this (like most of CodeGenPrepare) is just a rough heuristic.  If
    4264             : /// X was live across 'load Z' for other reasons, we actually *would* want to
    4265             : /// fold the addressing mode in the Z case.  This would make Y die earlier.
    4266      134454 : bool AddressingModeMatcher::
    4267             : isProfitableToFoldIntoAddressingMode(Instruction *I, ExtAddrMode &AMBefore,
    4268             :                                      ExtAddrMode &AMAfter) {
    4269      134454 :   if (IgnoreProfitability) return true;
    4270             : 
    4271             :   // AMBefore is the addressing mode before this instruction was folded into it,
    4272             :   // and AMAfter is the addressing mode after the instruction was folded.  Get
    4273             :   // the set of registers referenced by AMAfter and subtract out those
    4274             :   // referenced by AMBefore: this is the set of values which folding in this
    4275             :   // address extends the lifetime of.
    4276             :   //
    4277             :   // Note that there are only two potential values being referenced here,
    4278             :   // BaseReg and ScaleReg (global addresses are always available, as are any
    4279             :   // folded immediates).
    4280      130114 :   Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg;
    4281             : 
    4282             :   // If the BaseReg or ScaledReg was referenced by the previous addrmode, their
    4283             :   // lifetime wasn't extended by adding this instruction.
    4284      130114 :   if (valueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg))
    4285      127065 :     BaseReg = nullptr;
    4286      130114 :   if (valueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg))
    4287      129407 :     ScaledReg = nullptr;
    4288             : 
    4289             :   // If folding this instruction (and it's subexprs) didn't extend any live
    4290             :   // ranges, we're ok with it.
    4291      130114 :   if (!BaseReg && !ScaledReg)
    4292             :     return true;
    4293             : 
    4294             :   // If all uses of this instruction can have the address mode sunk into them,
    4295             :   // we can remove the addressing mode and effectively trade one live register
    4296             :   // for another (at worst.)  In this context, folding an addressing mode into
    4297             :   // the use is just a particularly nice way of sinking it.
    4298        3413 :   SmallVector<std::pair<Instruction*,unsigned>, 16> MemoryUses;
    4299        6826 :   SmallPtrSet<Instruction*, 16> ConsideredInsts;
    4300        3413 :   if (FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TLI, TRI))
    4301             :     return false;  // Has a non-memory, non-foldable use!
    4302             : 
    4303             :   // Now that we know that all uses of this instruction are part of a chain of
    4304             :   // computation involving only operations that could theoretically be folded
    4305             :   // into a memory use, loop over each of these memory operation uses and see
    4306             :   // if they could  *actually* fold the instruction.  The assumption is that
    4307             :   // addressing modes are cheap and that duplicating the computation involved
    4308             :   // many times is worthwhile, even on a fastpath. For sinking candidates
    4309             :   // (i.e. cold call sites), this serves as a way to prevent excessive code
    4310             :   // growth since most architectures have some reasonable small and fast way to
    4311             :   // compute an effective address.  (i.e LEA on x86)
    4312        1422 :   SmallVector<Instruction*, 32> MatchedAddrModeInsts;
    4313        6654 :   for (unsigned i = 0, e = MemoryUses.size(); i != e; ++i) {
    4314        7956 :     Instruction *User = MemoryUses[i].first;
    4315        7956 :     unsigned OpNo = MemoryUses[i].second;
    4316             : 
    4317             :     // Get the access type of this use.  If the use isn't a pointer, we don't
    4318             :     // know what it accesses.
    4319        7956 :     Value *Address = User->getOperand(OpNo);
    4320        7956 :     PointerType *AddrTy = dyn_cast<PointerType>(Address->getType());
    4321             :     if (!AddrTy)
    4322         168 :       return false;
    4323        3978 :     Type *AddressAccessTy = AddrTy->getElementType();
    4324        3978 :     unsigned AS = AddrTy->getAddressSpace();
    4325             : 
    4326             :     // Do a match against the root of this address, ignoring profitability. This
    4327             :     // will tell us if the addressing mode for the memory operation will
    4328             :     // *actually* cover the shared instruction.
    4329        3978 :     ExtAddrMode Result;
    4330             :     TypePromotionTransaction::ConstRestorationPt LastKnownGood =
    4331        7956 :         TPT.getRestorationPoint();
    4332             :     AddressingModeMatcher Matcher(MatchedAddrModeInsts, TLI, TRI,
    4333             :                                   AddressAccessTy, AS,
    4334             :                                   MemoryInst, Result, InsertedInsts,
    4335        7956 :                                   PromotedInsts, TPT);
    4336        3978 :     Matcher.IgnoreProfitability = true;
    4337        3978 :     bool Success = Matcher.matchAddr(Address, 0);
    4338             :     (void)Success; assert(Success && "Couldn't select *anything*?");
    4339             : 
    4340             :     // The match was to check the profitability, the changes made are not
    4341             :     // part of the original matcher. Therefore, they should be dropped
    4342             :     // otherwise the original matcher will not present the right state.
    4343        3978 :     TPT.rollback(LastKnownGood);
    4344             : 
    4345             :     // If the match didn't cover I, then it won't be shared by it.
    4346        3978 :     if (!is_contained(MatchedAddrModeInsts, I))
    4347             :       return false;
    4348             : 
    4349        3810 :     MatchedAddrModeInsts.clear();
    4350             :   }
    4351             : 
    4352             :   return true;
    4353             : }
    4354             : 
    4355             : /// Return true if the specified values are defined in a
    4356             : /// different basic block than BB.
    4357             : static bool IsNonLocalValue(Value *V, BasicBlock *BB) {
    4358      252843 :   if (Instruction *I = dyn_cast<Instruction>(V))
    4359      252843 :     return I->getParent() != BB;
    4360             :   return false;
    4361             : }
    4362             : 
    4363             : /// Sink addressing mode computation immediate before MemoryInst if doing so
    4364             : /// can be done without increasing register pressure.  The need for the
    4365             : /// register pressure constraint means this can end up being an all or nothing
    4366             : /// decision for all uses of the same addressing computation.
    4367             : ///
    4368             : /// Load and Store Instructions often have addressing modes that can do
    4369             : /// significant amounts of computation. As such, instruction selection will try
    4370             : /// to get the load or store to do as much computation as possible for the
    4371             : /// program. The problem is that isel can only see within a single block. As
    4372             : /// such, we sink as much legal addressing mode work into the block as possible.
    4373             : ///
    4374             : /// This method is used to optimize both load/store and inline asms with memory
    4375             : /// operands.  It's also used to sink addressing computations feeding into cold
    4376             : /// call sites into their (cold) basic block.
    4377             : ///
    4378             : /// The motivation for handling sinking into cold blocks is that doing so can
    4379             : /// both enable other address mode sinking (by satisfying the register pressure
    4380             : /// constraint above), and reduce register pressure globally (by removing the
    4381             : /// addressing mode computation from the fast path entirely.).
    4382      994853 : bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
    4383             :                                         Type *AccessTy, unsigned AddrSpace) {
    4384      994853 :   Value *Repl = Addr;
    4385             : 
    4386             :   // Try to collapse single-value PHI nodes.  This is necessary to undo
    4387             :   // unprofitable PRE transformations.
    4388     1989706 :   SmallVector<Value*, 8> worklist;
    4389     1989706 :   SmallPtrSet<Value*, 16> Visited;
    4390      994853 :   worklist.push_back(Addr);
    4391             : 
    4392             :   // Use a worklist to iteratively look through PHI nodes, and ensure that
    4393             :   // the addressing mode obtained from the non-PHI roots of the graph
    4394             :   // are equivalent.
    4395      994853 :   bool AddrModeFound = false;
    4396      994853 :   bool PhiSeen = false;
    4397     1989706 :   SmallVector<Instruction*, 16> AddrModeInsts;
    4398      994853 :   ExtAddrMode AddrMode;
    4399     2984559 :   TypePromotionTransaction TPT(RemovedInsts);
    4400             :   TypePromotionTransaction::ConstRestorationPt LastKnownGood =
    4401      994853 :       TPT.getRestorationPoint();
    4402     3009507 :   while (!worklist.empty()) {
    4403     2022630 :     Value *V = worklist.back();
    4404     1011315 :     worklist.pop_back();
    4405             : 
    4406             :     // We allow traversing cyclic Phi nodes.
    4407             :     // In case of success after this loop we ensure that traversing through
    4408             :     // Phi nodes ends up with all cases to compute address of the form
    4409             :     //    BaseGV + Base + Scale * Index + Offset
    4410             :     // where Scale and Offset are constans and BaseGV, Base and Index
    4411             :     // are exactly the same Values in all cases.
    4412             :     // It means that BaseGV, Scale and Offset dominate our memory instruction
    4413             :     // and have the same value as they had in address computation represented
    4414             :     // as Phi. So we can safely sink address computation to memory instruction.
    4415     1011315 :     if (!Visited.insert(V).second)
    4416     1007941 :       continue;
    4417             : 
    4418             :     // For a PHI node, push all of its incoming values.
    4419        8115 :     if (PHINode *P = dyn_cast<PHINode>(V)) {
    4420       25351 :       for (Value *IncValue : P->incoming_values())
    4421       17236 :         worklist.push_back(IncValue);
    4422        8115 :       PhiSeen = true;
    4423        8115 :       continue;
    4424             :     }
    4425             : 
    4426             :     // For non-PHIs, determine the addressing mode being computed.  Note that
    4427             :     // the result may differ depending on what other uses our candidate
    4428             :     // addressing instructions might have.
    4429     1002586 :     AddrModeInsts.clear();
    4430             :     ExtAddrMode NewAddrMode = AddressingModeMatcher::Match(
    4431     1002586 :         V, AccessTy, AddrSpace, MemoryInst, AddrModeInsts, *TLI, *TRI,
    4432     2005172 :         InsertedInsts, PromotedInsts, TPT);
    4433             : 
    4434     1997439 :     if (!AddrModeFound) {
    4435      994853 :       AddrModeFound = true;
    4436      994853 :       AddrMode = NewAddrMode;
    4437      994853 :       continue;
    4438             :     }
    4439        7733 :     if (NewAddrMode == AddrMode)
    4440        3745 :       continue;
    4441             : 
    4442        3988 :     AddrModeFound = false;
    4443        3988 :     break;
    4444             :   }
    4445             : 
    4446             :   // If the addressing mode couldn't be determined, or if multiple different
    4447             :   // ones were determined, bail out now.
    4448      990865 :   if (!AddrModeFound) {
    4449        3988 :     TPT.rollback(LastKnownGood);
    4450        3988 :     return false;
    4451             :   }
    4452      990865 :   TPT.commit();
    4453             : 
    4454             :   // If all the instructions matched are already in this BB, don't do anything.
    4455             :   // If we saw Phi node then it is not local definitely.
    4456     1978076 :   if (!PhiSeen && none_of(AddrModeInsts, [&](Value *V) {
    4457      252843 :         return IsNonLocalValue(V, MemoryInst->getParent());
    4458      252843 :                   })) {
    4459             :     DEBUG(dbgs() << "CGP: Found      local addrmode: " << AddrMode << "\n");
    4460             :     return false;
    4461             :   }
    4462             : 
    4463             :   // Insert this computation right after this user.  Since our caller is
    4464             :   // scanning from the top of the BB to the bottom, reuse of the expr are
    4465             :   // guaranteed to happen later.
    4466       14684 :   IRBuilder<> Builder(MemoryInst);
    4467             : 
    4468             :   // Now that we determined the addressing expression we want to use and know
    4469             :   // that we have to sink it into this block.  Check to see if we have already
    4470             :   // done this for some other load/store instr in this block.  If so, reuse the
    4471             :   // computation.
    4472       14684 :   Value *&SunkAddr = SunkAddrs[Addr];
    4473       14684 :   if (SunkAddr) {
    4474             :     DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for "
    4475             :                  << *MemoryInst << "\n");
    4476         832 :     if (SunkAddr->getType() != Addr->getType())
    4477           0 :       SunkAddr = Builder.CreatePointerCast(SunkAddr, Addr->getType());
    4478       13857 :   } else if (AddrSinkUsingGEPs ||
    4479           5 :              (!AddrSinkUsingGEPs.getNumOccurrences() && TM &&
    4480       13852 :               SubtargetInfo->useAA())) {
    4481             :     // By default, we use the GEP-based method when AA is used later. This
    4482             :     // prevents new inttoptr/ptrtoint pairs from degrading AA capabilities.
    4483             :     DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
    4484             :                  << *MemoryInst << "\n");
    4485       13847 :     Type *IntPtrTy = DL->getIntPtrType(Addr->getType());
    4486       13847 :     Value *ResultPtr = nullptr, *ResultIndex = nullptr;
    4487             : 
    4488             :     // First, find the pointer.
    4489       41461 :     if (AddrMode.BaseReg && AddrMode.BaseReg->getType()->isPointerTy()) {
    4490             :       ResultPtr = AddrMode.BaseReg;
    4491             :       AddrMode.BaseReg = nullptr;
    4492             :     }
    4493             : 
    4494       14639 :     if (AddrMode.Scale && AddrMode.ScaledReg->getType()->isPointerTy()) {
    4495             :       // We can't add more than one pointer together, nor can we scale a
    4496             :       // pointer (both of which seem meaningless).
    4497          15 :       if (ResultPtr || AddrMode.Scale != 1)
    4498             :         return false;
    4499             : 
    4500             :       ResultPtr = AddrMode.ScaledReg;
    4501             :       AddrMode.Scale = 0;
    4502             :     }
    4503             : 
    4504             :     // It is only safe to sign extend the BaseReg if we know that the math
    4505             :     // required to create it did not overflow before we extend it. Since
    4506             :     // the original IR value was tossed in favor of a constant back when
    4507             :     // the AddrMode was created we need to bail out gracefully if widths
    4508             :     // do not match instead of extending it.
    4509             :     //
    4510             :     // (See below for code to add the scale.)
    4511       13847 :     if (AddrMode.Scale) {
    4512         777 :       Type *ScaledRegTy = AddrMode.ScaledReg->getType();
    4513        2331 :       if (cast<IntegerType>(IntPtrTy)->getBitWidth() >
    4514        1554 :           cast<IntegerType>(ScaledRegTy)->getBitWidth())
    4515             :         return false;
    4516             :     }
    4517             : 
    4518       13837 :     if (AddrMode.BaseGV) {
    4519          34 :       if (ResultPtr)
    4520             :         return false;
    4521             : 
    4522             :       ResultPtr = AddrMode.BaseGV;
    4523             :     }
    4524             : 
    4525             :     // If the real base value actually came from an inttoptr, then the matcher
    4526             :     // will look through it and provide only the integer value. In that case,
    4527             :     // use it here.
    4528       13837 :     if (!DL->isNonIntegralPointerType(Addr->getType())) {
    4529       13834 :       if (!ResultPtr && AddrMode.BaseReg) {
    4530         106 :         ResultPtr = Builder.CreateIntToPtr(AddrMode.BaseReg, Addr->getType(),
    4531             :                                            "sunkaddr");
    4532          53 :         AddrMode.BaseReg = nullptr;
    4533       13781 :       } else if (!ResultPtr && AddrMode.Scale == 1) {
    4534           0 :         ResultPtr = Builder.CreateIntToPtr(AddrMode.ScaledReg, Addr->getType(),
    4535             :                                            "sunkaddr");
    4536           0 :         AddrMode.Scale = 0;
    4537             :       }
    4538             :     }
    4539             : 
    4540       13837 :     if (!ResultPtr &&
    4541           7 :         !AddrMode.BaseReg && !AddrMode.Scale && !AddrMode.BaseOffs) {
    4542           1 :       SunkAddr = Constant::getNullValue(Addr->getType());
    4543       13836 :     } else if (!ResultPtr) {
    4544             :       return false;
    4545             :     } else {
    4546             :       Type *I8PtrTy =
    4547       41484 :           Builder.getInt8PtrTy(Addr->getType()->getPointerAddressSpace());
    4548       27656 :       Type *I8Ty = Builder.getInt8Ty();
    4549             : 
    4550             :       // Start with the base register. Do this first so that subsequent address
    4551             :       // matching finds it last, which will prevent it from trying to match it
    4552             :       // as the scaled value in case it happens to be a mul. That would be
    4553             :       // problematic if we've sunk a different mul for the scale, because then
    4554             :       // we'd end up sinking both muls.
    4555       13828 :       if (AddrMode.BaseReg) {
    4556          17 :         Value *V = AddrMode.BaseReg;
    4557          17 :         if (V->getType() != IntPtrTy)
    4558           2 :           V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr");
    4559             : 
    4560             :         ResultIndex = V;
    4561             :       }
    4562             : 
    4563             :       // Add the scale value.
    4564       13828 :       if (AddrMode.Scale) {
    4565         767 :         Value *V = AddrMode.ScaledReg;
    4566         767 :         if (V->getType() == IntPtrTy) {
    4567             :           // done.
    4568             :         } else {
    4569             :           assert(cast<IntegerType>(IntPtrTy)->getBitWidth() <
    4570             :                  cast<IntegerType>(V->getType())->getBitWidth() &&
    4571             :                  "We can't transform if ScaledReg is too narrow");
    4572           2 :           V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr");
    4573             :         }
    4574             : 
    4575         767 :         if (AddrMode.Scale != 1)
    4576         444 :           V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale),
    4577             :                                 "sunkaddr");
    4578         767 :         if (ResultIndex)
    4579           0 :           ResultIndex = Builder.CreateAdd(ResultIndex, V, "sunkaddr");
    4580             :         else
    4581             :           ResultIndex = V;
    4582             :       }
    4583             : 
    4584             :       // Add in the Base Offset if present.
    4585       13828 :       if (AddrMode.BaseOffs) {
    4586       13145 :         Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
    4587       13145 :         if (ResultIndex) {
    4588             :           // We need to add this separately from the scale above to help with
    4589             :           // SDAG consecutive load/store merging.
    4590         394 :           if (ResultPtr->getType() != I8PtrTy)
    4591         330 :             ResultPtr = Builder.CreatePointerCast(ResultPtr, I8PtrTy);
    4592         394 :           ResultPtr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr");
    4593             :         }
    4594             : 
    4595             :         ResultIndex = V;
    4596             :       }
    4597             : 
    4598       13828 :       if (!ResultIndex) {
    4599         293 :         SunkAddr = ResultPtr;
    4600             :       } else {
    4601       13535 :         if (ResultPtr->getType() != I8PtrTy)
    4602       13017 :           ResultPtr = Builder.CreatePointerCast(ResultPtr, I8PtrTy);
    4603       13535 :         SunkAddr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr");
    4604             :       }
    4605             : 
    4606       13828 :       if (SunkAddr->getType() != Addr->getType())
    4607       13137 :         SunkAddr = Builder.CreatePointerCast(SunkAddr, Addr->getType());
    4608             :     }
    4609             :   } else {
    4610             :     // We'd require a ptrtoint/inttoptr down the line, which we can't do for
    4611             :     // non-integral pointers, so in that case bail out now.
    4612          10 :     Type *BaseTy = AddrMode.BaseReg ? AddrMode.BaseReg->getType() : nullptr;
    4613           5 :     Type *ScaleTy = AddrMode.Scale ? AddrMode.ScaledReg->getType() : nullptr;
    4614           5 :     PointerType *BasePtrTy = dyn_cast_or_null<PointerType>(BaseTy);
    4615           5 :     PointerType *ScalePtrTy = dyn_cast_or_null<PointerType>(ScaleTy);
    4616           6 :     if (DL->isNonIntegralPointerType(Addr->getType()) ||
    4617           1 :         (BasePtrTy && DL->isNonIntegralPointerType(BasePtrTy)) ||
    4618           0 :         (ScalePtrTy && DL->isNonIntegralPointerType(ScalePtrTy)) ||
    4619           0 :         (AddrMode.BaseGV &&
    4620           0 :          DL->isNonIntegralPointerType(AddrMode.BaseGV->getType())))
    4621             :       return false;
    4622             : 
    4623             :     DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
    4624             :                  << *MemoryInst << "\n");
    4625           0 :     Type *IntPtrTy = DL->getIntPtrType(Addr->getType());
    4626           0 :     Value *Result = nullptr;
    4627             : 
    4628             :     // Start with the base register. Do this first so that subsequent address
    4629             :     // matching finds it last, which will prevent it from trying to match it
    4630             :     // as the scaled value in case it happens to be a mul. That would be
    4631             :     // problematic if we've sunk a different mul for the scale, because then
    4632             :     // we'd end up sinking both muls.
    4633           0 :     if (AddrMode.BaseReg) {
    4634           0 :       Value *V = AddrMode.BaseReg;
    4635           0 :       if (V->getType()->isPointerTy())
    4636           0 :         V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
    4637           0 :       if (V->getType() != IntPtrTy)
    4638           0 :         V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr");
    4639             :       Result = V;
    4640             :     }
    4641             : 
    4642             :     // Add the scale value.
    4643           0 :     if (AddrMode.Scale) {
    4644           0 :       Value *V = AddrMode.ScaledReg;
    4645           0 :       if (V->getType() == IntPtrTy) {
    4646             :         // done.
    4647           0 :       } else if (V->getType()->isPointerTy()) {
    4648           0 :         V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
    4649           0 :       } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
    4650           0 :                  cast<IntegerType>(V->getType())->getBitWidth()) {
    4651           0 :         V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr");
    4652             :       } else {
    4653             :         // It is only safe to sign extend the BaseReg if we know that the math
    4654             :         // required to create it did not overflow before we extend it. Since
    4655             :         // the original IR value was tossed in favor of a constant back when
    4656             :         // the AddrMode was created we need to bail out gracefully if widths
    4657             :         // do not match instead of extending it.
    4658           0 :         Instruction *I = dyn_cast_or_null<Instruction>(Result);
    4659           0 :         if (I && (Result != AddrMode.BaseReg))
    4660           0 :           I->eraseFromParent();
    4661             :         return false;
    4662             :       }
    4663           0 :       if (AddrMode.Scale != 1)
    4664           0 :         V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale),
    4665             :                               "sunkaddr");
    4666           0 :       if (Result)
    4667           0 :         Result = Builder.CreateAdd(Result, V, "sunkaddr");
    4668             :       else
    4669             :         Result = V;
    4670             :     }
    4671             : 
    4672             :     // Add in the BaseGV if present.
    4673           0 :     if (AddrMode.BaseGV) {
    4674           0 :       Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr");
    4675           0 :       if (Result)
    4676           0 :         Result = Builder.CreateAdd(Result, V, "sunkaddr");
    4677             :       else
    4678             :         Result = V;
    4679             :     }
    4680             : 
    4681             :     // Add in the Base Offset if present.
    4682           0 :     if (AddrMode.BaseOffs) {
    4683           0 :       Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
    4684           0 :       if (Result)
    4685           0 :         Result = Builder.CreateAdd(Result, V, "sunkaddr");
    4686             :       else
    4687             :         Result = V;
    4688             :     }
    4689             : 
    4690           0 :     if (!Result)
    4691           0 :       SunkAddr = Constant::getNullValue(Addr->getType());
    4692             :     else
    4693           0 :       SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr");
    4694             :   }
    4695             : 
    4696       14661 :   MemoryInst->replaceUsesOfWith(Repl, SunkAddr);
    4697             : 
    4698             :   // If we have no uses, recursively delete the value and all dead instructions
    4699             :   // using it.
    4700       29322 :   if (Repl->use_empty()) {
    4701             :     // This can cause recursive deletion, which can invalidate our iterator.
    4702             :     // Use a WeakTrackingVH to hold onto it in case this happens.
    4703       10004 :     Value *CurValue = &*CurInstIterator;
    4704       10004 :     WeakTrackingVH IterHandle(CurValue);
    4705       10004 :     BasicBlock *BB = CurInstIterator->getParent();
    4706             : 
    4707        5002 :     RecursivelyDeleteTriviallyDeadInstructions(Repl, TLInfo);
    4708             : 
    4709        5002 :     if (IterHandle != CurValue) {
    4710             :       // If the iterator instruction was recursively deleted, start over at the
    4711             :       // start of the block.
    4712           1 :       CurInstIterator = BB->begin();
    4713           1 :       SunkAddrs.clear();
    4714             :     }
    4715             :   }
    4716             :   ++NumMemoryInsts;
    4717             :   return true;
    4718             : }
    4719             : 
    4720             : /// If there are any memory operands, use OptimizeMemoryInst to sink their
    4721             : /// address computing into the block when possible / profitable.
    4722        6201 : bool CodeGenPrepare::optimizeInlineAsmInst(CallInst *CS) {
    4723        6201 :   bool MadeChange = false;
    4724             : 
    4725             :   const TargetRegisterInfo *TRI =
    4726       12402 :       TM->getSubtargetImpl(*CS->getFunction())->getRegisterInfo();
    4727             :   TargetLowering::AsmOperandInfoVector TargetConstraints =
    4728       18603 :       TLI->ParseConstraints(*DL, TRI, CS);
    4729        6201 :   unsigned ArgNo = 0;
    4730       66176 :   for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
    4731      107548 :     TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
    4732             : 
    4733             :     // Compute the constraint code and ConstraintType to use.
    4734       53774 :     TLI->ComputeConstraintToUse(OpInfo, SDValue());
    4735             : 
    4736       55140 :     if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
    4737        1366 :         OpInfo.isIndirect) {
    4738         866 :       Value *OpVal = CS->getArgOperand(ArgNo++);
    4739         433 :       MadeChange |= optimizeMemoryInst(CS, OpVal, OpVal->getType(), ~0u);
    4740       53341 :     } else if (OpInfo.Type == InlineAsm::isInput)
    4741        2729 :       ArgNo++;
    4742             :   }
    4743             : 
    4744        6201 :   return MadeChange;
    4745             : }
    4746             : 
    4747             : /// \brief Check if all the uses of \p Val are equivalent (or free) zero or
    4748             : /// sign extensions.
    4749          31 : static bool hasSameExtUse(Value *Val, const TargetLowering &TLI) {
    4750             :   assert(!Val->use_empty() && "Input must have at least one use");
    4751          93 :   const Instruction *FirstUser = cast<Instruction>(*Val->user_begin());
    4752          62 :   bool IsSExt = isa<SExtInst>(FirstUser);
    4753          31 :   Type *ExtTy = FirstUser->getType();
    4754         186 :   for (const User *U : Val->users()) {
    4755          71 :     const Instruction *UI = cast<Instruction>(U);
    4756         142 :     if ((IsSExt && !isa<SExtInst>(UI)) || (!IsSExt && !isa<ZExtInst>(UI)))
    4757             :       return false;
    4758          54 :     Type *CurTy = UI->getType();
    4759             :     // Same input and output types: Same instruction after CSE.
    4760          54 :     if (CurTy == ExtTy)
    4761          44 :       continue;
    4762             : 
    4763             :     // If IsSExt is true, we are in this situation:
    4764             :     // a = Val
    4765             :     // b = sext ty1 a to ty2
    4766             :     // c = sext ty1 a to ty3
    4767             :     // Assuming ty2 is shorter than ty3, this could be turned into:
    4768             :     // a = Val
    4769             :     // b = sext ty1 a to ty2
    4770             :     // c = sext ty2 b to ty3
    4771             :     // However, the last sext is not free.
    4772          10 :     if (IsSExt)
    4773             :       return false;
    4774             : 
    4775             :     // This is a ZExt, maybe this is free to extend from one type to another.
    4776             :     // In that case, we would not account for a different use.
    4777             :     Type *NarrowTy;
    4778             :     Type *LargeTy;
    4779          27 :     if (ExtTy->getScalarType()->getIntegerBitWidth() >
    4780          18 :         CurTy->getScalarType()->getIntegerBitWidth()) {
    4781             :       NarrowTy = CurTy;
    4782             :       LargeTy = ExtTy;
    4783             :     } else {
    4784           0 :       NarrowTy = ExtTy;
    4785           0 :       LargeTy = CurTy;
    4786             :     }
    4787             : 
    4788           9 :     if (!TLI.isZExtFree(NarrowTy, LargeTy))
    4789             :       return false;
    4790             :   }
    4791             :   // All uses are the same or can be derived from one another for free.
    4792             :   return true;
    4793             : }
    4794             : 
    4795             : /// \brief Try to speculatively promote extensions in \p Exts and continue
    4796             : /// promoting through newly promoted operands recursively as far as doing so is
    4797             : /// profitable. Save extensions profitably moved up, in \p ProfitablyMovedExts.
    4798             : /// When some promotion happened, \p TPT contains the proper state to revert
    4799             : /// them.
    4800             : ///
    4801             : /// \return true if some promotion happened, false otherwise.
    4802       25802 : bool CodeGenPrepare::tryToPromoteExts(
    4803             :     TypePromotionTransaction &TPT, const SmallVectorImpl<Instruction *> &Exts,
    4804             :     SmallVectorImpl<Instruction *> &ProfitablyMovedExts,
    4805             :     unsigned CreatedInstsCost) {
    4806       25802 :   bool Promoted = false;
    4807             : 
    4808             :   // Iterate over all the extensions to try to promote them.
    4809       95758 :   for (auto I : Exts) {
    4810             :     // Early check if we directly have ext(load).
    4811       59830 :     if (isa<LoadInst>(I->getOperand(0))) {
    4812        8058 :       ProfitablyMovedExts.push_back(I);
    4813       25995 :       continue;
    4814             :     }
    4815             : 
    4816             :     // Check whether or not we want to do any promotion.  The reason we have
    4817             :     // this check inside the for loop is to catch the case where an extension
    4818             :     // is directly fed by a load because in such case the extension can be moved
    4819             :     // up without any promotion on its operands.
    4820       28160 :     if (!TLI || !TLI->enableExtLdPromotion() || DisableExtLdPromotion)
    4821        7534 :       return false;
    4822             : 
    4823             :     // Get the action to perform the promotion.
    4824             :     TypePromotionHelper::Action TPH =
    4825       10294 :         TypePromotionHelper::getAction(I, InsertedInsts, *TLI, PromotedInsts);
    4826             :     // Check if we can promote.
    4827       20160 :     if (!TPH) {
    4828             :       // Save the current extension as we cannot move up through its operand.
    4829        9866 :       ProfitablyMovedExts.push_back(I);
    4830        9866 :       continue;
    4831             :     }
    4832             : 
    4833             :     // Save the current state.
    4834             :     TypePromotionTransaction::ConstRestorationPt LastKnownGood =
    4835         428 :         TPT.getRestorationPoint();
    4836         843 :     SmallVector<Instruction *, 4> NewExts;
    4837         428 :     unsigned NewCreatedInstsCost = 0;
    4838         428 :     unsigned ExtCost = !TLI->isExtFree(I);
    4839             :     // Promote.
    4840         428 :     Value *PromotedVal = TPH(I, TPT, PromotedInsts, NewCreatedInstsCost,
    4841         856 :                              &NewExts, nullptr, *TLI);
    4842             :     assert(PromotedVal &&
    4843             :            "TypePromotionHelper should have filtered out those cases");
    4844             : 
    4845             :     // We would be able to merge only one extension in a load.
    4846             :     // Therefore, if we have more than 1 new extension we heuristically
    4847             :     // cut this search path, because it means we degrade the code quality.
    4848             :     // With exactly 2, the transformation is neutral, because we will merge
    4849             :     // one extension but leave one. However, we optimistically keep going,
    4850             :     // because the new extension may be removed too.
    4851         428 :     long long TotalCreatedInstsCost = CreatedInstsCost + NewCreatedInstsCost;
    4852             :     // FIXME: It would be possible to propagate a negative value instead of
    4853             :     // conservatively ceiling it to 0.
    4854         428 :     TotalCreatedInstsCost =
    4855        1284 :         std::max((long long)0, (TotalCreatedInstsCost - ExtCost));
    4856         441 :     if (!StressExtLdPromotion &&
    4857         359 :         (TotalCreatedInstsCost > 1 ||
    4858         359 :          !isPromotedInstructionLegal(*TLI, *DL, PromotedVal))) {
    4859             :       // This promotion is not profitable, rollback to the previous state, and
    4860             :       // save the current extension in ProfitablyMovedExts as the latest
    4861             :       // speculative promotion turned out to be unprofitable.
    4862          13 :       TPT.rollback(LastKnownGood);
    4863          13 :       ProfitablyMovedExts.push_back(I);
    4864          13 :       continue;
    4865             :     }
    4866             :     // Continue promoting NewExts as far as doing so is profitable.
    4867         830 :     SmallVector<Instruction *, 2> NewlyMovedExts;
    4868         415 :     (void)tryToPromoteExts(TPT, NewExts, NewlyMovedExts, TotalCreatedInstsCost);
    4869         415 :     bool NewPromoted = false;
    4870        1746 :     for (auto ExtInst : NewlyMovedExts) {
    4871         501 :       Instruction *MovedExt = cast<Instruction>(ExtInst);
    4872        1002 :       Value *ExtOperand = MovedExt->getOperand(0);
    4873             :       // If we have reached to a load, we need this extra profitability check
    4874             :       // as it could potentially be merged into an ext(load).
    4875         668 :       if (isa<LoadInst>(ExtOperand) &&
    4876         279 :           !(StressExtLdPromotion || NewCreatedInstsCost <= ExtCost ||
    4877         107 :             (ExtOperand->hasOneUse() || hasSameExtUse(ExtOperand, *TLI))))
    4878          18 :         continue;
    4879             : 
    4880         483 :       ProfitablyMovedExts.push_back(MovedExt);
    4881         483 :       NewPromoted = true;
    4882             :     }
    4883             : 
    4884             :     // If none of speculative promotions for NewExts is profitable, rollback
    4885             :     // and save the current extension (I) as the last profitable extension.
    4886         415 :     if (!NewPromoted) {
    4887           0 :       TPT.rollback(LastKnownGood);
    4888           0 :       ProfitablyMovedExts.push_back(I);
    4889             :       continue;
    4890             :     }
    4891             :     // The promotion is profitable.
    4892         415 :     Promoted = true;
    4893             :   }
    4894       18268 :   return Promoted;
    4895             : }
    4896             : 
    4897             : /// Merging redundant sexts when one is dominating the other.
    4898          35 : bool CodeGenPrepare::mergeSExts(Function &F) {
    4899          70 :   DominatorTree DT(F);
    4900          35 :   bool Changed = false;
    4901         154 :   for (auto &Entry : ValToSExtendedUses) {
    4902          49 :     SExts &Insts = Entry.second;
    4903          98 :     SExts CurPts;
    4904         249 :     for (Instruction *Inst : Insts) {
    4905         306 :       if (RemovedInsts.count(Inst) || !isa<SExtInst>(Inst) ||
    4906         204 :           Inst->getOperand(0) != Entry.first)
    4907           0 :         continue;
    4908         102 :       bool inserted = false;
    4909         306 :       for (auto &Pt : CurPts) {
    4910          53 :         if (DT.dominates(Inst, Pt)) {
    4911          30 :           Pt->replaceAllUsesWith(Inst);
    4912          30 :           RemovedInsts.insert(Pt);
    4913          30 :           Pt->removeFromParent();
    4914          30 :           Pt = Inst;
    4915          30 :           inserted = true;
    4916          30 :           Changed = true;
    4917          30 :           break;
    4918             :         }
    4919          23 :         if (!DT.dominates(Pt, Inst))
    4920             :           // Give up if we need to merge in a common dominator as the
    4921             :           // expermients show it is not profitable.
    4922             :           continue;
    4923          23 :         Inst->replaceAllUsesWith(Pt);
    4924          23 :         RemovedInsts.insert(Inst);
    4925          23 :         Inst->removeFromParent();
    4926          23 :         inserted = true;
    4927          23 :         Changed = true;
    4928          23 :         break;
    4929             :       }
    4930         102 :       if (!inserted)
    4931          49 :         CurPts.push_back(Inst);
    4932             :     }
    4933             :   }
    4934          70 :   return Changed;
    4935             : }
    4936             : 
    4937             : /// Return true, if an ext(load) can be formed from an extension in
    4938             : /// \p MovedExts.
    4939       25357 : bool CodeGenPrepare::canFormExtLd(
    4940             :     const SmallVectorImpl<Instruction *> &MovedExts, LoadInst *&LI,
    4941             :     Instruction *&Inst, bool HasPromoted) {
    4942       85893 :   for (auto *MovedExtInst : MovedExts) {
    4943       35712 :     if (isa<LoadInst>(MovedExtInst->getOperand(0))) {
    4944       24102 :       LI = cast<LoadInst>(MovedExtInst->getOperand(0));
    4945        8034 :       Inst = MovedExtInst;
    4946        8034 :       break;
    4947             :     }
    4948             :   }
    4949       25357 :   if (!LI)
    4950             :     return false;
    4951             : 
    4952             :   // If they're already in the same block, there's nothing to do.
    4953             :   // Make the cheap checks first if we did not promote.
    4954             :   // If we promoted, we need to check if it is indeed profitable.
    4955        8034 :   if (!HasPromoted && LI->getParent() == Inst->getParent())
    4956             :     return false;
    4957             : 
    4958         611 :   return TLI->isExtLoad(LI, Inst, *DL);
    4959             : }
    4960             : 
    4961             : /// Move a zext or sext fed by a load into the same basic block as the load,
    4962             : /// unless conditions are unfavorable. This allows SelectionDAG to fold the
    4963             : /// extend into the load.
    4964             : ///
    4965             : /// E.g.,
    4966             : /// \code
    4967             : /// %ld = load i32* %addr
    4968             : /// %add = add nuw i32 %ld, 4
    4969             : /// %zext = zext i32 %add to i64
    4970             : // \endcode
    4971             : /// =>
    4972             : /// \code
    4973             : /// %ld = load i32* %addr
    4974             : /// %zext = zext i32 %ld to i64
    4975             : /// %add = add nuw i64 %zext, 4
    4976             : /// \encode
    4977             : /// Note that the promotion in %add to i64 is done in tryToPromoteExts(), which
    4978             : /// allow us to match zext(load i32*) to i64.
    4979             : ///
    4980             : /// Also, try to promote the computations used to obtain a sign extended
    4981             : /// value used into memory accesses.
    4982             : /// E.g.,
    4983             : /// \code
    4984             : /// a = add nsw i32 b, 3
    4985             : /// d = sext i32 a to i64
    4986             : /// e = getelementptr ..., i64 d
    4987             : /// \endcode
    4988             : /// =>
    4989             : /// \code
    4990             : /// f = sext i32 b to i64
    4991             : /// a = add nsw i64 f, 3
    4992             : /// e = getelementptr ..., i64 a
    4993             : /// \endcode
    4994             : ///
    4995             : /// \p Inst[in/out] the extension may be modified during the process if some
    4996             : /// promotions apply.
    4997       25359 : bool CodeGenPrepare::optimizeExt(Instruction *&Inst) {
    4998             :   // ExtLoad formation and address type promotion infrastructure requires TLI to
    4999             :   // be effective.
    5000       25359 :   if (!TLI)
    5001             :     return false;
    5002             : 
    5003       25357 :   bool AllowPromotionWithoutCommonHeader = false;
    5004             :   /// See if it is an interesting sext operations for the address type
    5005             :   /// promotion before trying to promote it, e.g., the ones with the right
    5006             :   /// type and used in memory accesses.
    5007       25357 :   bool ATPConsiderable = TTI->shouldConsiderAddressTypePromotion(
    5008       25357 :       *Inst, AllowPromotionWithoutCommonHeader);
    5009       50714 :   TypePromotionTransaction TPT(RemovedInsts);
    5010             :   TypePromotionTransaction::ConstRestorationPt LastKnownGood =
    5011       25357 :       TPT.getRestorationPoint();
    5012       50714 :   SmallVector<Instruction *, 1> Exts;
    5013       50714 :   SmallVector<Instruction *, 2> SpeculativelyMovedExts;
    5014       25357 :   Exts.push_back(Inst);
    5015             : 
    5016       25357 :   bool HasPromoted = tryToPromoteExts(TPT, Exts, SpeculativelyMovedExts);
    5017             : 
    5018             :   // Look for a load being extended.
    5019       25357 :   LoadInst *LI = nullptr;
    5020             :   Instruction *ExtFedByLoad;
    5021             : 
    5022             :   // Try to promote a chain of computation if it allows to form an extended
    5023             :   // load.
    5024       25357 :   if (canFormExtLd(SpeculativelyMovedExts, LI, ExtFedByLoad, HasPromoted)) {
    5025             :     assert(LI && ExtFedByLoad && "Expect a valid load and extension");
    5026         606 :     TPT.commit();
    5027             :     // Move the extend into the same block as the load
    5028         606 :     ExtFedByLoad->moveAfter(LI);
    5029             :     // CGP does not check if the zext would be speculatively executed when moved
    5030             :     // to the same basic block as the load. Preserving its original location
    5031             :     // would pessimize the debugging experience, as well as negatively impact
    5032             :     // the quality of sample pgo. We don't want to use "line 0" as that has a
    5033             :     // size cost in the line-table section and logically the zext can be seen as
    5034             :     // part of the load. Therefore we conservatively reuse the same debug
    5035             :     // location for the load and the zext.
    5036        2424 :     ExtFedByLoad->setDebugLoc(LI->getDebugLoc());
    5037         606 :     ++NumExtsMoved;
    5038         606 :     Inst = ExtFedByLoad;
    5039         606 :     return true;
    5040             :   }
    5041             : 
    5042             :   // Continue promoting SExts if known as considerable depending on targets.
    5043       24890 :   if (ATPConsiderable &&
    5044         139 :       performAddressTypePromotion(Inst, AllowPromotionWithoutCommonHeader,
    5045             :                                   HasPromoted, TPT, SpeculativelyMovedExts))
    5046             :     return true;
    5047             : 
    5048       24699 :   TPT.rollback(LastKnownGood);
    5049       24699 :   return false;
    5050             : }
    5051             : 
    5052             : // Perform address type promotion if doing so is profitable.
    5053             : // If AllowPromotionWithoutCommonHeader == false, we should find other sext
    5054             : // instructions that sign extended the same initial value. However, if
    5055             : // AllowPromotionWithoutCommonHeader == true, we expect promoting the
    5056             : // extension is just profitable.
    5057         139 : bool CodeGenPrepare::performAddressTypePromotion(
    5058             :     Instruction *&Inst, bool AllowPromotionWithoutCommonHeader,
    5059             :     bool HasPromoted, TypePromotionTransaction &TPT,
    5060             :     SmallVectorImpl<Instruction *> &SpeculativelyMovedExts) {
    5061         139 :   bool Promoted = false;
    5062         278 :   SmallPtrSet<Instruction *, 1> UnhandledExts;
    5063         139 :   bool AllSeenFirst = true;
    5064         558 :   for (auto I : SpeculativelyMovedExts) {
    5065         282 :     Value *HeadOfChain = I->getOperand(0);
    5066             :     DenseMap<Value *, Instruction *>::iterator AlreadySeen =
    5067         141 :         SeenChainsForSExt.find(HeadOfChain);
    5068             :     // If there is an unhandled SExt which has the same header, try to promote
    5069             :     // it as well.
    5070         423 :     if (AlreadySeen != SeenChainsForSExt.end()) {
    5071          53 :       if (AlreadySeen->second != nullptr)
    5072          30 :         UnhandledExts.insert(AlreadySeen->second);
    5073             :       AllSeenFirst = false;
    5074             :     }
    5075             :   }
    5076             : 
    5077         155 :   if (!AllSeenFirst || (AllowPromotionWithoutCommonHeader &&
    5078          32 :                         SpeculativelyMovedExts.size() == 1)) {
    5079          69 :     TPT.commit();
    5080          69 :     if (HasPromoted)
    5081          51 :       Promoted = true;
    5082         277 :     for (auto I : SpeculativelyMovedExts) {
    5083         140 :       Value *HeadOfChain = I->getOperand(0);
    5084         140 :       SeenChainsForSExt[HeadOfChain] = nullptr;
    5085         140 :       ValToSExtendedUses[HeadOfChain].push_back(I);
    5086             :     }
    5087             :     // Update Inst as promotion happen.
    5088          69 :     Inst = SpeculativelyMovedExts.pop_back_val();
    5089             :   } else {
    5090             :     // This is the first chain visited from the header, keep the current chain
    5091             :     // as unhandled. Defer to promote this until we encounter another SExt
    5092             :     // chain derived from the same header.
    5093         422 :     for (auto I : SpeculativelyMovedExts) {
    5094         142 :       Value *HeadOfChain = I->getOperand(0);
    5095         142 :       SeenChainsForSExt[HeadOfChain] = Inst;
    5096             :     }
    5097             :     return false;
    5098             :   }
    5099             : 
    5100         122 :   if (!AllSeenFirst && !UnhandledExts.empty())
    5101          60 :     for (auto VisitedSExt : UnhandledExts) {
    5102          30 :       if (RemovedInsts.count(VisitedSExt))
    5103           0 :         continue;
    5104          90 :       TypePromotionTransaction TPT(RemovedInsts);
    5105          60 :       SmallVector<Instruction *, 1> Exts;
    5106          60 :       SmallVector<Instruction *, 2> Chains;
    5107          30 :       Exts.push_back(VisitedSExt);
    5108          30 :       bool HasPromoted = tryToPromoteExts(TPT, Exts, Chains);
    5109          30 :       TPT.commit();
    5110          30 :       if (HasPromoted)
    5111          12 :         Promoted = true;
    5112         122 :       for (auto I : Chains) {
    5113          64 :         Value *HeadOfChain = I->getOperand(0);
    5114             :         // Mark this as handled.
    5115          64 :         SeenChainsForSExt[HeadOfChain] = nullptr;
    5116          64 :         ValToSExtendedUses[HeadOfChain].push_back(I);
    5117             :       }
    5118             :     }
    5119             :   return Promoted;
    5120             : }
    5121             : 
    5122       25359 : bool CodeGenPrepare::optimizeExtUses(Instruction *I) {
    5123       25359 :   BasicBlock *DefBB = I->getParent();
    5124             : 
    5125             :   // If the result of a {s|z}ext and its source are both live out, rewrite all
    5126             :   // other uses of the source with result of extension.
    5127       50718 :   Value *Src = I->getOperand(0);
    5128       50718 :   if (Src->hasOneUse())
    5129             :     return false;
    5130             : 
    5131             :   // Only do this xform if truncating is free.
    5132        3301 :   if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType()))
    5133             :     return false;
    5134             : 
    5135             :   // Only safe to perform the optimization if the source is also defined in
    5136             :   // this block.
    5137        8901 :   if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
    5138             :     return false;
    5139             : 
    5140        2451 :   bool DefIsLiveOut = false;
    5141       11293 :   for (User *U : I->users()) {
    5142        2536 :     Instruction *UI = cast<Instruction>(U);
    5143             : 
    5144             :     // Figure out which BB this ext is used in.
    5145        2536 :     BasicBlock *UserBB = UI->getParent();
    5146        2536 :     if (UserBB == DefBB) continue;
    5147             :     DefIsLiveOut = true;
    5148             :     break;
    5149             :   }
    5150        2451 :   if (!DefIsLiveOut)
    5151             :     return false;
    5152             : 
    5153             :   // Make sure none of the uses are PHI nodes.
    5154        8944 :   for (User *U : Src->users()) {
    5155        2785 :     Instruction *UI = cast<Instruction>(U);
    5156        2785 :     BasicBlock *UserBB = UI->getParent();
    5157        2785 :     if (UserBB == DefBB) continue;
    5158             :     // Be conservative. We don't want this xform to end up introducing
    5159             :     // reloads just before load / store instructions.
    5160        2170 :     if (isa<PHINode>(UI) || isa<LoadInst>(UI) || isa<StoreInst>(UI))
    5161             :       return false;
    5162             :   }
    5163             : 
    5164             :   // InsertedTruncs - Only insert one trunc in each block once.
    5165        1110 :   DenseMap<BasicBlock*, Instruction*> InsertedTruncs;
    5166             : 
    5167        1110 :   bool MadeChange = false;
    5168        5141 :   for (Use &U : Src->uses()) {
    5169        3622 :     Instruction *User = cast<Instruction>(U.getUser());
    5170             : 
    5171             :     // Figure out which BB this ext is used in.
    5172        1811 :     BasicBlock *UserBB = User->getParent();
    5173        1811 :     if (UserBB == DefBB) continue;
    5174             : 
    5175             :     // Both src and def are live in this block. Rewrite the use.
    5176         482 :     Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
    5177             : 
    5178         482 :     if (!InsertedTrunc) {
    5179         964 :       BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
    5180             :       assert(InsertPt != UserBB->end());
    5181        1928 :       InsertedTrunc = new TruncInst(I, Src->getType(), "", &*InsertPt);
    5182         482 :       InsertedInsts.insert(InsertedTrunc);
    5183             :     }
    5184             : 
    5185             :     // Replace a use of the {s|z}ext source with a use of the result.
    5186         964 :     U = InsertedTrunc;
    5187         482 :     ++NumExtUses;
    5188         482 :     MadeChange = true;
    5189             :   }
    5190             : 
    5191        1110 :   return MadeChange;
    5192             : }
    5193             : 
    5194             : // Find loads whose uses only use some of the loaded value's bits.  Add an "and"
    5195             : // just after the load if the target can fold this into one extload instruction,
    5196             : // with the hope of eliminating some of the other later "and" instructions using
    5197             : // the loaded value.  "and"s that are made trivially redundant by the insertion
    5198             : // of the new "and" are removed by this function, while others (e.g. those whose
    5199             : // path from the load goes through a phi) are left for isel to potentially
    5200             : // remove.
    5201             : //
    5202             : // For example:
    5203             : //
    5204             : // b0:
    5205             : //   x = load i32
    5206             : //   ...
    5207             : // b1:
    5208             : //   y = and x, 0xff
    5209             : //   z = use y
    5210             : //
    5211             : // becomes:
    5212             : //
    5213             : // b0:
    5214             : //   x = load i32
    5215             : //   x' = and x, 0xff
    5216             : //   ...
    5217             : // b1:
    5218             : //   z = use x'
    5219             : //
    5220             : // whereas:
    5221             : //
    5222             : // b0:
    5223             : //   x1 = load i32
    5224             : //   ...
    5225             : // b1:
    5226             : //   x2 = load i32
    5227             : //   ...
    5228             : // b2:
    5229             : //   x = phi x1, x2
    5230             : //   y = and x, 0xff
    5231             : //
    5232             : // becomes (after a call to optimizeLoadExt for each load):
    5233             : //
    5234             : // b0:
    5235             : //   x1 = load i32
    5236             : //   x1' = and x1, 0xff
    5237             : //   ...
    5238             : // b1:
    5239             : //   x2 = load i32
    5240             : //   x2' = and x2, 0xff
    5241             : //   ...
    5242             : // b2:
    5243             : //   x = phi x1', x2'
    5244             : //   y = and x, 0xff
    5245      484364 : bool CodeGenPrepare::optimizeLoadExt(LoadInst *Load) {
    5246      953148 :   if (!Load->isSimple() ||
    5247     1350398 :       !(Load->getType()->isIntegerTy() || Load->getType()->isPointerTy()))
    5248             :     return false;
    5249             : 
    5250             :   // Skip loads we've already transformed.
    5251      926076 :   if (Load->hasOneUse() &&
    5252     1171044 :       InsertedInsts.count(cast<Instruction>(*Load->user_begin())))
    5253             :     return false;
    5254             : 
    5255             :   // Look at all uses of Load, looking through phis, to determine how many bits
    5256             :   // of the loaded value are needed.
    5257      316950 :   SmallVector<Instruction *, 8> WorkList;
    5258      633900 :   SmallPtrSet<Instruction *, 16> Visited;
    5259      633900 :   SmallVector<Instruction *, 8> AndsToMaybeRemove;
    5260     1663470 :   for (auto *U : Load->users())
    5261      356310 :     WorkList.push_back(cast<Instruction>(U));
    5262             : 
    5263      316950 :   EVT LoadResultVT = TLI->getValueType(*DL, Load->getType());
    5264      316950 :   unsigned BitWidth = LoadResultVT.getSizeInBits();
    5265      633900 :   APInt DemandBits(BitWidth, 0);
    5266      316950 :   APInt WidestAndBits(BitWidth, 0);
    5267             : 
    5268      325913 :   while (!WorkList.empty()) {
    5269      647048 :     Instruction *I = WorkList.back();
    5270      323524 :     WorkList.pop_back();
    5271             : 
    5272             :     // Break use-def graph loops.
    5273      323524 :     if (!Visited.insert(I).second)
    5274        6854 :       continue;
    5275             : 
    5276             :     // For a PHI node, push all of its users.
    5277      330216 :     if (auto *Phi = dyn_cast<PHINode>(I)) {
    5278       38496 :       for (auto *U : Phi->users())
    5279        9129 :         WorkList.push_back(cast<Instruction>(U));
    5280        6746 :       continue;
    5281             :     }
    5282             : 
    5283      633448 :     switch (I->getOpcode()) {
    5284        2044 :     case Instruction::And: {
    5285        5068 :       auto *AndC = dyn_cast<ConstantInt>(I->getOperand(1));
    5286             :       if (!AndC)
    5287        1064 :         return false;
    5288        2940 :       APInt AndBits = AndC->getValue();
    5289         980 :       DemandBits |= AndBits;
    5290             :       // Keep track of the widest and mask we see.
    5291         980 :       if (AndBits.ugt(WidestAndBits))
    5292         914 :         WidestAndBits = AndBits;
    5293        1894 :       if (AndBits == WidestAndBits && I->getOperand(0) == Load)
    5294         738 :         AndsToMaybeRemove.push_back(I);
    5295             :       break;
    5296             :     }
    5297             : 
    5298         959 :     case Instruction::Shl: {
    5299        2773 :       auto *ShlC = dyn_cast<ConstantInt>(I->getOperand(1));
    5300             :       if (!ShlC)
    5301             :         return false;
    5302        1710 :       uint64_t ShiftAmt = ShlC->getLimitedValue(BitWidth - 1);
    5303         855 :       DemandBits.setLowBits(BitWidth - ShiftAmt);
    5304             :       break;
    5305             :     }
    5306             : 
    5307         328 :     case Instruction::Trunc: {
    5308         328 :       EVT TruncVT = TLI->getValueType(*DL, I->getType());
    5309         328 :       unsigned TruncBitWidth = TruncVT.getSizeInBits();
    5310         328 :       DemandBits.setLowBits(TruncBitWidth);
    5311             :       break;
    5312             :     }
    5313             : 
    5314             :     default:
    5315             :       return false;
    5316             :     }
    5317             :   }
    5318             : 
    5319        2389 :   uint32_t ActiveBits = DemandBits.getActiveBits();
    5320             :   // Avoid hoisting (and (load x) 1) since it is unlikely to be folded by the
    5321             :   // target even if isLoadExtLegal says an i1 EXTLOAD is valid.  For example,
    5322             :   // for the AArch64 target isLoadExtLegal(ZEXTLOAD, i32, i1) returns true, but
    5323             :   // (and (load x) 1) is not matched as a single instruction, rather as a LDR
    5324             :   // followed by an AND.
    5325             :   // TODO: Look into removing this restriction by fixing backends to either
    5326             :   // return false for isLoadExtLegal for i1 or have them select this pattern to
    5327             :   // a single instruction.
    5328             :   //
    5329             :   // Also avoid hoisting if we didn't see any ands with the exact DemandBits
    5330             :   // mask, since these are the only ands that will be removed by isel.
    5331        3564 :   if (ActiveBits <= 1 || !DemandBits.isMask(ActiveBits) ||
    5332        1175 :       WidestAndBits != DemandBits)
    5333             :     return false;
    5334             : 
    5335          97 :   LLVMContext &Ctx = Load->getType()->getContext();
    5336          97 :   Type *TruncTy = Type::getIntNTy(Ctx, ActiveBits);
    5337          97 :   EVT TruncVT = TLI->getValueType(*DL, TruncTy);
    5338             : 
    5339             :   // Reject cases that won't be matched as extloads.
    5340         173 :   if (!LoadResultVT.bitsGT(TruncVT) || !TruncVT.isRound() ||
    5341          76 :       !TLI->isLoadExtLegal(ISD::ZEXTLOAD, LoadResultVT, TruncVT))
    5342             :     return false;
    5343             : 
    5344          76 :   IRBuilder<> Builder(Load->getNextNode());
    5345         114 :   auto *NewAnd = dyn_cast<Instruction>(
    5346          76 :       Builder.CreateAnd(Load, ConstantInt::get(Ctx, DemandBits)));
    5347             :   // Mark this instruction as "inserted by CGP", so that other
    5348             :   // optimizations don't touch it.
    5349          38 :   InsertedInsts.insert(NewAnd);
    5350             : 
    5351             :   // Replace all uses of load with new and (except for the use of load in the
    5352             :   // new and itself).
    5353          38 :   Load->replaceAllUsesWith(NewAnd);
    5354          38 :   NewAnd->setOperand(0, Load);
    5355             : 
    5356             :   // Remove any and instructions that are now redundant.
    5357         144 :   for (auto *And : AndsToMaybeRemove)
    5358             :     // Check that the and mask is the same as the one we decided to put on the
    5359             :     // new and.
    5360         150 :     if (cast<ConstantInt>(And->getOperand(1))->getValue() == DemandBits) {
    5361          30 :       And->replaceAllUsesWith(NewAnd);
    5362          60 :       if (&*CurInstIterator == And)
    5363          84 :         CurInstIterator = std::next(And->getIterator());
    5364          30 :       And->eraseFromParent();
    5365             :       ++NumAndUses;
    5366             :     }
    5367             : 
    5368          38 :   ++NumAndsAdded;
    5369          38 :   return true;
    5370             : }
    5371             : 
    5372             : /// Check if V (an operand of a select instruction) is an expensive instruction
    5373             : /// that is only used once.
    5374        7518 : static bool sinkSelectOperand(const TargetTransformInfo *TTI, Value *V) {
    5375        1074 :   auto *I = dyn_cast<Instruction>(V);
    5376             :   // If it's safe to speculatively execute, then it should not have side
    5377             :   // effects; therefore, it's safe to sink and possibly *not* execute.
    5378        2485 :   return I && I->hasOneUse() && isSafeToSpeculativelyExecute(I) &&
    5379        7855 :          TTI->getUserCost(I) >= TargetTransformInfo::TCC_Expensive;
    5380             : }
    5381             : 
    5382             : /// Returns true if a SelectInst should be turned into an explicit branch.
    5383       54858 : static bool isFormingBranchFromSelectProfitable(const TargetTransformInfo *TTI,
    5384             :                                                 const TargetLowering *TLI,
    5385             :                                                 SelectInst *SI) {
    5386             :   // If even a predictable select is cheap, then a branch can't be cheaper.
    5387       54858 :   if (!TLI->isPredictableSelectExpensive())
    5388             :     return false;
    5389             : 
    5390             :   // FIXME: This should use the same heuristics as IfConversion to determine
    5391             :   // whether a select is better represented as a branch.
    5392             : 
    5393             :   // If metadata tells us that the select condition is obviously predictable,
    5394             :   // then we want to replace the select with a branch.
    5395             :   uint64_t TrueWeight, FalseWeight;
    5396       48683 :   if (SI->extractProfMetadata(TrueWeight, FalseWeight)) {
    5397           9 :     uint64_t Max = std::max(TrueWeight, FalseWeight);
    5398           9 :     uint64_t Sum = TrueWeight + FalseWeight;
    5399           9 :     if (Sum != 0) {
    5400           8 :       auto Probability = BranchProbability::getBranchProbability(Max, Sum);
    5401          16 :       if (Probability > TLI->getPredictableBranchThreshold())
    5402           7 :         return true;
    5403             :     }
    5404             :   }
    5405             : 
    5406       89465 :   CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
    5407             : 
    5408             :   // If a branch is predictable, an out-of-order CPU can avoid blocking on its
    5409             :   // comparison condition. If the compare has more than one use, there's
    5410             :   // probably another cmov or setcc around, so it's not worth emitting a branch.
    5411       81578 :   if (!Cmp || !Cmp->hasOneUse())
    5412             :     return false;
    5413             : 
    5414             :   // If either operand of the select is expensive and only needed on one side
    5415             :   // of the select, we should form a branch.
    5416        7472 :   if (sinkSelectOperand(TTI, SI->getTrueValue()) ||
    5417        3735 :       sinkSelectOperand(TTI, SI->getFalseValue()))
    5418             :     return true;
    5419             : 
    5420             :   return false;
    5421             : }
    5422             : 
    5423             : /// If \p isTrue is true, return the true value of \p SI, otherwise return
    5424             : /// false value of \p SI. If the true/false value of \p SI is defined by any
    5425             : /// select instructions in \p Selects, look through the defining select
    5426             : /// instruction until the true/false value is not defined in \p Selects.
    5427          46 : static Value *getTrueOrFalseValue(
    5428             :     SelectInst *SI, bool isTrue,
    5429             :     const SmallPtrSet<const Instruction *, 2> &Selects) {
    5430             :   Value *V;
    5431             : 
    5432          46 :   for (SelectInst *DefSI = SI; DefSI != nullptr && Selects.count(DefSI);
    5433             :        DefSI = dyn_cast<SelectInst>(V)) {
    5434             :     assert(DefSI->getCondition() == SI->getCondition() &&
    5435             :            "The condition of DefSI does not match with SI");
    5436          98 :     V = (isTrue ? DefSI->getTrueValue() : DefSI->getFalseValue());
    5437             :   }
    5438          46 :   return V;
    5439             : }
    5440             : 
    5441             : /// If we have a SelectInst that will likely profit from branch prediction,
    5442             : /// turn it into a branch.
    5443       64105 : bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) {
    5444             :   // Find all consecutive select instructions that share the same condition.
    5445      128210 :   SmallVector<SelectInst *, 2> ASI;
    5446       64105 :   ASI.push_back(SI);
    5447      192315 :   for (BasicBlock::iterator It = ++BasicBlock::iterator(SI);
    5448      129088 :        It != SI->getParent()->end(); ++It) {
    5449      129088 :     SelectInst *I = dyn_cast<SelectInst>(&*It);
    5450       65904 :     if (I && SI->getCondition() == I->getCondition()) {
    5451         439 :       ASI.push_back(I);
    5452             :     } else {
    5453             :       break;
    5454             :     }
    5455             :   }
    5456             : 
    5457      128210 :   SelectInst *LastSI = ASI.back();
    5458             :   // Increment the current iterator to skip all the rest of select instructions
    5459             :   // because they will be either "not lowered" or "all lowered" to branch.
    5460      192315 :   CurInstIterator = std::next(LastSI->getIterator());
    5461             : 
    5462      128210 :   bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1);
    5463             : 
    5464             :   // Can we convert the 'select' to CF ?
    5465      110552 :   if (DisableSelectToBranch || OptSize || !TLI || VectorCond ||
    5466      101316 :       SI->getMetadata(LLVMContext::MD_unpredictable))
    5467             :     return false;
    5468             : 
    5469             :   TargetLowering::SelectSupportKind SelectKind;
    5470             :   if (VectorCond)
    5471             :     SelectKind = TargetLowering::VectorMaskSelect;
    5472      109736 :   else if (SI->getType()->isVectorTy())
    5473             :     SelectKind = TargetLowering::ScalarCondVectorVal;
    5474             :   else
    5475       54136 :     SelectKind = TargetLowering::ScalarValSelect;
    5476             : 
    5477      109726 :   if (TLI->isSelectSupported(SelectKind) &&
    5478       54858 :       !isFormingBranchFromSelectProfitable(TTI, TLI, SI))
    5479             :     return false;
    5480             : 
    5481          21 :   ModifiedDT = true;
    5482             : 
    5483             :   // Transform a sequence like this:
    5484             :   //    start:
    5485             :   //       %cmp = cmp uge i32 %a, %b
    5486             :   //       %sel = select i1 %cmp, i32 %c, i32 %d
    5487             :   //
    5488             :   // Into:
    5489             :   //    start:
    5490             :   //       %cmp = cmp uge i32 %a, %b
    5491             :   //       br i1 %cmp, label %select.true, label %select.false
    5492             :   //    select.true:
    5493             :   //       br label %select.end
    5494             :   //    select.false:
    5495             :   //       br label %select.end
    5496             :   //    select.end:
    5497             :   //       %sel = phi i32 [ %c, %select.true ], [ %d, %select.false ]
    5498             :   //
    5499             :   // In addition, we may sink instructions that produce %c or %d from
    5500             :   // the entry block into the destination(s) of the new branch.
    5501             :   // If the true or false blocks do not contain a sunken instruction, that
    5502             :   // block and its branch may be optimized away. In that case, one side of the
    5503             :   // first branch will point directly to select.end, and the corresponding PHI
    5504             :   // predecessor block will be the start block.
    5505             : 
    5506             :   // First, we split the block containing the select into 2 blocks.
    5507          21 :   BasicBlock *StartBlock = SI->getParent();
    5508          42 :   BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(LastSI));
    5509          21 :   BasicBlock *EndBlock = StartBlock->splitBasicBlock(SplitPt, "select.end");
    5510             : 
    5511             :   // Delete the unconditional branch that was just created by the split.
    5512          21 :   StartBlock->getTerminator()->eraseFromParent();
    5513             : 
    5514             :   // These are the new basic blocks for the conditional branch.
    5515             :   // At least one will become an actual new basic block.
    5516          21 :   BasicBlock *TrueBlock = nullptr;
    5517          21 :   BasicBlock *FalseBlock = nullptr;
    5518          21 :   BranchInst *TrueBranch = nullptr;
    5519          21 :   BranchInst *FalseBranch = nullptr;
    5520             : 
    5521             :   // Sink expensive instructions into the conditional blocks to avoid executing
    5522             :   // them speculatively.
    5523          86 :   for (SelectInst *SI : ASI) {
    5524          23 :     if (sinkSelectOperand(TTI, SI->getTrueValue())) {
    5525           2 :       if (TrueBlock == nullptr) {
    5526           6 :         TrueBlock = BasicBlock::Create(SI->getContext(), "select.true.sink",
    5527             :                                        EndBlock->getParent(), EndBlock);
    5528           2 :         TrueBranch = BranchInst::Create(EndBlock, TrueBlock);
    5529             :       }
    5530           4 :       auto *TrueInst = cast<Instruction>(SI->getTrueValue());
    5531           2 :       TrueInst->moveBefore(TrueBranch);
    5532             :     }
    5533          23 :     if (sinkSelectOperand(TTI, SI->getFalseValue())) {
    5534           3 :       if (FalseBlock == nullptr) {
    5535           9 :         FalseBlock = BasicBlock::Create(SI->getContext(), "select.false.sink",
    5536             :                                         EndBlock->getParent(), EndBlock);
    5537           3 :         FalseBranch = BranchInst::Create(EndBlock, FalseBlock);
    5538             :       }
    5539           6 :       auto *FalseInst = cast<Instruction>(SI->getFalseValue());
    5540           3 :       FalseInst->moveBefore(FalseBranch);
    5541             :     }
    5542             :   }
    5543             : 
    5544             :   // If there was nothing to sink, then arbitrarily choose the 'false' side
    5545             :   // for a new input value to the PHI.
    5546          21 :   if (TrueBlock == FalseBlock) {
    5547             :     assert(TrueBlock == nullptr &&
    5548             :            "Unexpected basic block transform while optimizing select");
    5549             : 
    5550          51 :     FalseBlock = BasicBlock::Create(SI->getContext(), "select.false",
    5551             :                                     EndBlock->getParent(), EndBlock);
    5552          17 :     BranchInst::Create(EndBlock, FalseBlock);
    5553             :   }
    5554             : 
    5555             :   // Insert the real conditional branch based on the original condition.
    5556             :   // If we did not create a new block for one of the 'true' or 'false' paths
    5557             :   // of the condition, it means that side of the branch goes to the end block
    5558             :   // directly and the path originates from the start block from the point of
    5559             :   // view of the new PHI.
    5560             :   BasicBlock *TT, *FT;
    5561          21 :   if (TrueBlock == nullptr) {
    5562             :     TT = EndBlock;
    5563             :     FT = FalseBlock;
    5564             :     TrueBlock = StartBlock;
    5565           2 :   } else if (FalseBlock == nullptr) {
    5566             :     TT = TrueBlock;
    5567             :     FT = EndBlock;
    5568             :     FalseBlock = StartBlock;
    5569             :   } else {
    5570           1 :     TT = TrueBlock;
    5571           1 :     FT = FalseBlock;
    5572             :   }
    5573          63 :   IRBuilder<>(SI).CreateCondBr(SI->getCondition(), TT, FT, SI);
    5574             : 
    5575          21 :   SmallPtrSet<const Instruction *, 2> INS;
    5576          63 :   INS.insert(ASI.begin(), ASI.end());
    5577             :   // Use reverse iterator because later select may use the value of the
    5578             :   // earlier select, and we need to propagate value through earlier select
    5579             :   // to get the PHI operand.
    5580         153 :   for (auto It = ASI.rbegin(); It != ASI.rend(); ++It) {
    5581          23 :     SelectInst *SI = *It;
    5582             :     // The select itself is replaced with a PHI Node.
    5583          69 :     PHINode *PN = PHINode::Create(SI->getType(), 2, "", &EndBlock->front());
    5584          23 :     PN->takeName(SI);
    5585          23 :     PN->addIncoming(getTrueOrFalseValue(SI, true, INS), TrueBlock);
    5586          23 :     PN->addIncoming(getTrueOrFalseValue(SI, false, INS), FalseBlock);
    5587             : 
    5588          23 :     SI->replaceAllUsesWith(PN);
    5589          23 :     SI->eraseFromParent();
    5590          23 :     INS.erase(SI);
    5591          23 :     ++NumSelectsExpanded;
    5592             :   }
    5593             : 
    5594             :   // Instruct OptimizeBlock to skip to the next block.
    5595          21 :   CurInstIterator = StartBlock->end();
    5596          21 :   return true;
    5597             : }
    5598             : 
    5599       10766 : static bool isBroadcastShuffle(ShuffleVectorInst *SVI) {
    5600       21532 :   SmallVector<int, 16> Mask(SVI->getShuffleMask());
    5601       10766 :   int SplatElem = -1;
    5602       63120 :   for (unsigned i = 0; i < Mask.size(); ++i) {
    5603       65739 :     if (SplatElem != -1 && Mask[i] != -1 && Mask[i] != SplatElem)
    5604             :       return false;
    5605       41588 :     SplatElem = Mask[i];
    5606             :   }
    5607             : 
    5608             :   return true;
    5609             : }
    5610             : 
    5611             : /// Some targets have expensive vector shifts if the lanes aren't all the same
    5612             : /// (e.g. x86 only introduced "vpsllvd" and friends with AVX2). In these cases
    5613             : /// it's often worth sinking a shufflevector splat down to its use so that
    5614             : /// codegen can spot all lanes are identical.
    5615       26490 : bool CodeGenPrepare::optimizeShuffleVectorInst(ShuffleVectorInst *SVI) {
    5616       26490 :   BasicBlock *DefBB = SVI->getParent();
    5617             : 
    5618             :   // Only do this xform if variable vector shifts are particularly expensive.
    5619       52980 :   if (!TLI || !TLI->isVectorShiftByScalarCheap(SVI->getType()))
    5620             :     return false;
    5621             : 
    5622             :   // We only expect better codegen by sinking a shuffle if we can recognise a
    5623             :   // constant splat.
    5624       10766 :   if (!isBroadcastShuffle(SVI))
    5625             :     return false;
    5626             : 
    5627             :   // InsertedShuffles - Only insert a shuffle in each block once.
    5628        1027 :   DenseMap<BasicBlock*, Instruction*> InsertedShuffles;
    5629             : 
    5630        1027 :   bool MadeChange = false;
    5631        5171 :   for (User *U : SVI->users()) {
    5632        1045 :     Instruction *UI = cast<Instruction>(U);
    5633             : 
    5634             :     // Figure out which BB this ext is used in.
    5635        1045 :     BasicBlock *UserBB = UI->getParent();
    5636        2086 :     if (UserBB == DefBB) continue;
    5637             : 
    5638             :     // For now only apply this when the splat is used by a shift instruction.
    5639          50 :     if (!UI->isShift()) continue;
    5640             : 
    5641             :     // Everything checks out, sink the shuffle if the user's block doesn't
    5642             :     // already have a copy.
    5643           4 :     Instruction *&InsertedShuffle = InsertedShuffles[UserBB];
    5644             : 
    5645           4 :     if (!InsertedShuffle) {
    5646           8 :       BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
    5647             :       assert(InsertPt != UserBB->end());
    5648           8 :       InsertedShuffle =
    5649           8 :           new ShuffleVectorInst(SVI->getOperand(0), SVI->getOperand(1),
    5650          12 :                                 SVI->getOperand(2), "", &*InsertPt);
    5651             :     }
    5652             : 
    5653           4 :     UI->replaceUsesOfWith(SVI, InsertedShuffle);
    5654           4 :     MadeChange = true;
    5655             :   }
    5656             : 
    5657             :   // If we removed all uses, nuke the shuffle.
    5658        2054 :   if (SVI->use_empty()) {
    5659           2 :     SVI->eraseFromParent();
    5660           2 :     MadeChange = true;
    5661             :   }
    5662             : 
    5663        1027 :   return MadeChange;
    5664             : }
    5665             : 
    5666        1325 : bool CodeGenPrepare::optimizeSwitchInst(SwitchInst *SI) {
    5667        1325 :   if (!TLI || !DL)
    5668             :     return false;
    5669             : 
    5670        1321 :   Value *Cond = SI->getCondition();
    5671        1321 :   Type *OldType = Cond->getType();
    5672        1321 :   LLVMContext &Context = Cond->getContext();
    5673        1321 :   MVT RegType = TLI->getRegisterType(Context, TLI->getValueType(*DL, OldType));
    5674        1321 :   unsigned RegWidth = RegType.getSizeInBits();
    5675             : 
    5676        2642 :   if (RegWidth <= cast<IntegerType>(OldType)->getBitWidth())
    5677             :     return false;
    5678             : 
    5679             :   // If the register width is greater than the type width, expand the condition
    5680             :   // of the switch instruction and each case constant to the width of the
    5681             :   // register. By widening the type of the switch condition, subsequent
    5682             :   // comparisons (for case comparisons) will not need to be extended to the
    5683             :   // preferred register width, so we will potentially eliminate N-1 extends,
    5684             :   // where N is the number of cases in the switch.
    5685          51 :   auto *NewType = Type::getIntNTy(Context, RegWidth);
    5686             : 
    5687             :   // Zero-extend the switch condition and case constants unless the switch
    5688             :   // condition is a function argument that is already being sign-extended.
    5689             :   // In that case, we can avoid an unnecessary mask/extension by sign-extending
    5690             :   // everything instead.
    5691          51 :   Instruction::CastOps ExtType = Instruction::ZExt;
    5692           3 :   if (auto *Arg = dyn_cast<Argument>(Cond))
    5693           3 :     if (Arg->hasSExtAttr())
    5694           2 :       ExtType = Instruction::SExt;
    5695             : 
    5696          51 :   auto *ExtInst = CastInst::Create(ExtType, Cond, NewType);
    5697          51 :   ExtInst->insertBefore(SI);
    5698          51 :   SI->setCondition(ExtInst);
    5699         230 :   for (auto Case : SI->cases()) {
    5700         512 :     APInt NarrowConst = Case.getCaseValue()->getValue();
    5701             :     APInt WideConst = (ExtType == Instruction::ZExt) ?
    5702         256 :                       NarrowConst.zext(RegWidth) : NarrowConst.sext(RegWidth);
    5703         256 :     Case.setValue(ConstantInt::get(Context, WideConst));
    5704             :   }
    5705             : 
    5706             :   return true;
    5707             : }
    5708             : 
    5709             : 
    5710             : namespace {
    5711             : 
    5712             : /// \brief Helper class to promote a scalar operation to a vector one.
    5713             : /// This class is used to move downward extractelement transition.
    5714             : /// E.g.,
    5715             : /// a = vector_op <2 x i32>
    5716             : /// b = extractelement <2 x i32> a, i32 0
    5717             : /// c = scalar_op b
    5718             : /// store c
    5719             : ///
    5720             : /// =>
    5721             : /// a = vector_op <2 x i32>
    5722             : /// c = vector_op a (equivalent to scalar_op on the related lane)
    5723             : /// * d = extractelement <2 x i32> c, i32 0
    5724             : /// * store d
    5725             : /// Assuming both extractelement and store can be combine, we get rid of the
    5726             : /// transition.
    5727         270 : class VectorPromoteHelper {
    5728             :   /// DataLayout associated with the current module.
    5729             :   const DataLayout &DL;
    5730             : 
    5731             :   /// Used to perform some checks on the legality of vector operations.
    5732             :   const TargetLowering &TLI;
    5733             : 
    5734             :   /// Used to estimated the cost of the promoted chain.
    5735             :   const TargetTransformInfo &TTI;
    5736             : 
    5737             :   /// The transition being moved downwards.
    5738             :   Instruction *Transition;
    5739             : 
    5740             :   /// The sequence of instructions to be promoted.
    5741             :   SmallVector<Instruction *, 4> InstsToBePromoted;
    5742             : 
    5743             :   /// Cost of combining a store and an extract.
    5744             :   unsigned StoreExtractCombineCost;
    5745             : 
    5746             :   /// Instruction that will be combined with the transition.
    5747             :   Instruction *CombineInst = nullptr;
    5748             : 
    5749             :   /// \brief The instruction that represents the current end of the transition.
    5750             :   /// Since we are faking the promotion until we reach the end of the chain
    5751             :   /// of computation, we need a way to get the current end of the transition.
    5752             :   Instruction *getEndOfTransition() const {
    5753         114 :     if (InstsToBePromoted.empty())
    5754          78 :       return Transition;
    5755          72 :     return InstsToBePromoted.back();
    5756             :   }
    5757             : 
    5758             :   /// \brief Return the index of the original value in the transition.
    5759             :   /// E.g., for "extractelement <2 x i32> c, i32 1" the original value,
    5760             :   /// c, is at index 0.
    5761             :   unsigned getTransitionOriginalValueIdx() const {
    5762             :     assert(isa<ExtractElementInst>(Transition) &&
    5763             :            "Other kind of transitions are not supported yet");
    5764             :     return 0;
    5765             :   }
    5766             : 
    5767             :   /// \brief Return the index of the index in the transition.
    5768             :   /// E.g., for "extractelement <2 x i32> c, i32 0" the index
    5769             :   /// is at index 1.
    5770             :   unsigned getTransitionIdx() const {
    5771             :     assert(isa<ExtractElementInst>(Transition) &&
    5772             :            "Other kind of transitions are not supported yet");
    5773             :     return 1;
    5774             :   }
    5775             : 
    5776             :   /// \brief Get the type of the transition.
    5777             :   /// This is the type of the original value.
    5778             :   /// E.g., for "extractelement <2 x i32> c, i32 1" the type of the
    5779             :   /// transition is <2 x i32>.
    5780             :   Type *getTransitionType() const {
    5781         224 :     return Transition->getOperand(getTransitionOriginalValueIdx())->getType();
    5782             :   }
    5783             : 
    5784             :   /// \brief Promote \p ToBePromoted by moving \p Def downward through.
    5785             :   /// I.e., we have the following sequence:
    5786             :   /// Def = Transition <ty1> a to <ty2>
    5787             :   /// b = ToBePromoted <ty2> Def, ...
    5788             :   /// =>
    5789             :   /// b = ToBePromoted <ty1> a, ...
    5790             :   /// Def = Transition <ty1> ToBePromoted to <ty2>
    5791             :   void promoteImpl(Instruction *ToBePromoted);
    5792             : 
    5793             :   /// \brief Check whether or not it is profitable to promote all the
    5794             :   /// instructions enqueued to be promoted.
    5795           6 :   bool isProfitableToPromote() {
    5796          12 :     Value *ValIdx = Transition->getOperand(getTransitionOriginalValueIdx());
    5797           6 :     unsigned Index = isa<ConstantInt>(ValIdx)
    5798           0 :                          ? cast<ConstantInt>(ValIdx)->getZExtValue()
    5799           6 :                          : -1;
    5800          12 :     Type *PromotedType = getTransitionType();
    5801             : 
    5802          12 :     StoreInst *ST = cast<StoreInst>(CombineInst);
    5803           6 :     unsigned AS = ST->getPointerAddressSpace();
    5804           6 :     unsigned Align = ST->getAlignment();
    5805             :     // Check if this store is supported.
    5806          18 :     if (!TLI.allowsMisalignedMemoryAccesses(
    5807             :             TLI.getValueType(DL, ST->getValueOperand()->getType()), AS,
    5808           6 :             Align)) {
    5809             :       // If this is not supported, there is no way we can combine
    5810             :       // the extract with the store.
    5811             :       return false;
    5812             :     }
    5813             : 
    5814             :     // The scalar chain of computation has to pay for the transition
    5815             :     // scalar to vector.
    5816             :     // The vector chain has to account for the combining cost.
    5817             :     uint64_t ScalarCost =
    5818          12 :         TTI.getVectorInstrCost(Transition->getOpcode(), PromotedType, Index);
    5819           6 :     uint64_t VectorCost = StoreExtractCombineCost;
    5820          36 :     for (const auto &Inst : InstsToBePromoted) {
    5821             :       // Compute the cost.
    5822             :       // By construction, all instructions being promoted are arithmetic ones.
    5823             :       // Moreover, one argument is a constant that can be viewed as a splat
    5824             :       // constant.
    5825          36 :       Value *Arg0 = Inst->getOperand(0);
    5826          72 :       bool IsArg0Constant = isa<UndefValue>(Arg0) || isa<ConstantInt>(Arg0) ||
    5827          18 :                             isa<ConstantFP>(Arg0);
    5828          18 :       TargetTransformInfo::OperandValueKind Arg0OVK =
    5829             :           IsArg0Constant ? TargetTransformInfo::OK_UniformConstantValue
    5830             :                          : TargetTransformInfo::OK_AnyValue;
    5831          18 :       TargetTransformInfo::OperandValueKind Arg1OVK =
    5832          18 :           !IsArg0Constant ? TargetTransformInfo::OK_UniformConstantValue
    5833             :                           : TargetTransformInfo::OK_AnyValue;
    5834          54 :       ScalarCost += TTI.getArithmeticInstrCost(
    5835          18 :           Inst->getOpcode(), Inst->getType(), Arg0OVK, Arg1OVK);
    5836          54 :       VectorCost += TTI.getArithmeticInstrCost(Inst->getOpcode(), PromotedType,
    5837          18 :                                                Arg0OVK, Arg1OVK);
    5838             :     }
    5839             :     DEBUG(dbgs() << "Estimated cost of computation to be promoted:\nScalar: "
    5840             :                  << ScalarCost << "\nVector: " << VectorCost << '\n');
    5841           6 :     return ScalarCost > VectorCost;
    5842             :   }
    5843             : 
    5844             :   /// \brief Generate a constant vector with \p Val with the same
    5845             :   /// number of elements as the transition.
    5846             :   /// \p UseSplat defines whether or not \p Val should be replicated
    5847             :   /// across the whole vector.
    5848             :   /// In other words, if UseSplat == true, we generate <Val, Val, ..., Val>,
    5849             :   /// otherwise we generate a vector with as many undef as possible:
    5850             :   /// <undef, ..., undef, Val, undef, ..., undef> where \p Val is only
    5851             :   /// used at the index of the extract.
    5852          38 :   Value *getConstantVector(Constant *Val, bool UseSplat) const {
    5853          38 :     unsigned ExtractIdx = std::numeric_limits<unsigned>::max();
    5854          38 :     if (!UseSplat) {
    5855             :       // If we cannot determine where the constant must be, we have to
    5856             :       // use a splat constant.
    5857          64 :       Value *ValExtractIdx = Transition->getOperand(getTransitionIdx());
    5858          31 :       if (ConstantInt *CstVal = dyn_cast<ConstantInt>(ValExtractIdx))
    5859          31 :         ExtractIdx = CstVal->getSExtValue();
    5860             :       else
    5861             :         UseSplat = true;
    5862             :     }
    5863             : 
    5864         114 :     unsigned End = getTransitionType()->getVectorNumElements();
    5865          38 :     if (UseSplat)
    5866           7 :       return ConstantVector::getSplat(End, Val);
    5867             : 
    5868          31 :     SmallVector<Constant *, 4> ConstVec;
    5869          31 :     UndefValue *UndefVal = UndefValue::get(Val->getType());
    5870         105 :     for (unsigned Idx = 0; Idx != End; ++Idx) {
    5871          74 :       if (Idx == ExtractIdx)
    5872          31 :         ConstVec.push_back(Val);
    5873             :       else
    5874          43 :         ConstVec.push_back(UndefVal);
    5875             :     }
    5876          31 :     return ConstantVector::get(ConstVec);
    5877             :   }
    5878             : 
    5879             :   /// \brief Check if promoting to a vector type an operand at \p OperandIdx
    5880             :   /// in \p Use can trigger undefined behavior.
    5881          95 :   static bool canCauseUndefinedBehavior(const Instruction *Use,
    5882             :                                         unsigned OperandIdx) {
    5883             :     // This is not safe to introduce undef when the operand is on
    5884             :     // the right hand side of a division-like instruction.
    5885          95 :     if (OperandIdx != 1)
    5886             :       return false;
    5887          44 :     switch (Use->getOpcode()) {
    5888             :     default:
    5889             :       return false;
    5890          10 :     case Instruction::SDiv:
    5891             :     case Instruction::UDiv:
    5892             :     case Instruction::SRem:
    5893             :     case Instruction::URem:
    5894          10 :       return true;
    5895           4 :     case Instruction::FDiv:
    5896             :     case Instruction::FRem:
    5897           4 :       return !Use->hasNoNaNs();
    5898             :     }
    5899             :     llvm_unreachable(nullptr);
    5900             :   }
    5901             : 
    5902             : public:
    5903             :   VectorPromoteHelper(const DataLayout &DL, const TargetLowering &TLI,
    5904             :                       const TargetTransformInfo &TTI, Instruction *Transition,
    5905             :                       unsigned CombineCost)
    5906         135 :       : DL(DL), TLI(TLI), TTI(TTI), Transition(Transition),
    5907         270 :         StoreExtractCombineCost(CombineCost) {
    5908             :     assert(Transition && "Do not know how to promote null");
    5909             :   }
    5910             : 
    5911             :   /// \brief Check if we can promote \p ToBePromoted to \p Type.
    5912             :   bool canPromote(const Instruction *ToBePromoted) const {
    5913             :     // We could support CastInst too.
    5914          87 :     return isa<BinaryOperator>(ToBePromoted);
    5915             :   }
    5916             : 
    5917             :   /// \brief Check if it is profitable to promote \p ToBePromoted
    5918             :   /// by moving downward the transition through.
    5919          57 :   bool shouldPromote(const Instruction *ToBePromoted) const {
    5920             :     // Promote only if all the operands can be statically expanded.
    5921             :     // Indeed, we do not want to introduce any new kind of transitions.
    5922         222 :     for (const Use &U : ToBePromoted->operands()) {
    5923         114 :       const Value *Val = U.get();
    5924         165 :       if (Val == getEndOfTransition()) {
    5925             :         // If the use is a division and the transition is on the rhs,
    5926             :         // we cannot promote the operation, otherwise we may create a
    5927             :         // division by zero.
    5928          57 :         if (canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo()))
    5929             :           return false;
    5930          51 :         continue;
    5931             :       }
    5932         124 :       if (!isa<ConstantInt>(Val) && !isa<UndefValue>(Val) &&
    5933           5 :           !isa<ConstantFP>(Val))
    5934             :         return false;
    5935             :     }
    5936             :     // Check that the resulting operation is legal.
    5937         102 :     int ISDOpcode = TLI.InstructionOpcodeToISD(ToBePromoted->getOpcode());
    5938          51 :     if (!ISDOpcode)
    5939             :       return false;
    5940          81 :     return StressStoreExtract ||
    5941          60 :            TLI.isOperationLegalOrCustom(
    5942          51 :                ISDOpcode, TLI.getValueType(DL, getTransitionType(), true));
    5943             :   }
    5944             : 
    5945             :   /// \brief Check whether or not \p Use can be combined
    5946             :   /// with the transition.
    5947             :   /// I.e., is it possible to do Use(Transition) => AnotherUse?
    5948         160 :   bool canCombine(const Instruction *Use) { return isa<StoreInst>(Use); }
    5949             : 
    5950             :   /// \brief Record \p ToBePromoted as part of the chain to be promoted.
    5951             :   void enqueueForPromotion(Instruction *ToBePromoted) {
    5952          41 :     InstsToBePromoted.push_back(ToBePromoted);
    5953             :   }
    5954             : 
    5955             :   /// \brief Set the instruction that will be combined with the transition.
    5956             :   void recordCombineInstruction(Instruction *ToBeCombined) {
    5957             :     assert(canCombine(ToBeCombined) && "Unsupported instruction to combine");
    5958          73 :     CombineInst = ToBeCombined;
    5959             :   }
    5960             : 
    5961             :   /// \brief Promote all the instructions enqueued for promotion if it is
    5962             :   /// is profitable.
    5963             :   /// \return True if the promotion happened, false otherwise.
    5964          73 :   bool promote() {
    5965             :     // Check if there is something to promote.
    5966             :     // Right now, if we do not have anything to combine with,
    5967             :     // we assume the promotion is not profitable.
    5968          73 :     if (InstsToBePromoted.empty() || !CombineInst)
    5969             :       return false;
    5970             : 
    5971             :     // Check cost.
    5972          20 :     if (!StressStoreExtract && !isProfitableToPromote())
    5973             :       return false;
    5974             : 
    5975             :     // Promote.
    5976          98 :     for (auto &ToBePromoted : InstsToBePromoted)
    5977          38 :       promoteImpl(ToBePromoted);
    5978          40 :     InstsToBePromoted.clear();
    5979          20 :     return true;
    5980             :   }
    5981             : };
    5982             : 
    5983             : } // end anonymous namespace
    5984             : 
    5985          38 : void VectorPromoteHelper::promoteImpl(Instruction *ToBePromoted) {
    5986             :   // At this point, we know that all the operands of ToBePromoted but Def
    5987             :   // can be statically promoted.
    5988             :   // For Def, we need to use its parameter in ToBePromoted:
    5989             :   // b = ToBePromoted ty1 a
    5990             :   // Def = Transition ty1 b to ty2
    5991             :   // Move the transition down.
    5992             :   // 1. Replace all uses of the promoted operation by the transition.
    5993             :   // = ... b => = ... Def.
    5994             :   assert(ToBePromoted->getType() == Transition->getType() &&
    5995             :          "The type of the result of the transition does not match "
    5996             :          "the final type");
    5997          38 :   ToBePromoted->replaceAllUsesWith(Transition);
    5998             :   // 2. Update the type of the uses.
    5999             :   // b = ToBePromoted ty2 Def => b = ToBePromoted ty1 Def.
    6000          76 :   Type *TransitionTy = getTransitionType();
    6001          76 :   ToBePromoted->mutateType(TransitionTy);
    6002             :   // 3. Update all the operands of the promoted operation with promoted
    6003             :   // operands.
    6004             :   // b = ToBePromoted ty1 Def => b = ToBePromoted ty1 a.
    6005         152 :   for (Use &U : ToBePromoted->operands()) {
    6006          76 :     Value *Val = U.get();
    6007          76 :     Value *NewVal = nullptr;
    6008          76 :     if (Val == Transition)
    6009          76 :       NewVal = Transition->getOperand(getTransitionOriginalValueIdx());
    6010         119 :     else if (isa<UndefValue>(Val) || isa<ConstantInt>(Val) ||
    6011           5 :              isa<ConstantFP>(Val)) {
    6012             :       // Use a splat constant if it is not safe to use undef.
    6013          76 :       NewVal = getConstantVector(
    6014             :           cast<Constant>(Val),
    6015         114 :           isa<UndefValue>(Val) ||
    6016          38 :               canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo()));
    6017             :     } else
    6018           0 :       llvm_unreachable("Did you modified shouldPromote and forgot to update "
    6019             :                        "this?");
    6020          76 :     ToBePromoted->setOperand(U.getOperandNo(), NewVal);
    6021             :   }
    6022          38 :   Transition->moveAfter(ToBePromoted);
    6023          38 :   Transition->setOperand(getTransitionOriginalValueIdx(), ToBePromoted);
    6024          38 : }
    6025             : 
    6026             : /// Some targets can do store(extractelement) with one instruction.
    6027             : /// Try to push the extractelement towards the stores when the target
    6028             : /// has this feature and this is profitable.
    6029       11708 : bool CodeGenPrepare::optimizeExtractElementInst(Instruction *Inst) {
    6030       11708 :   unsigned CombineCost = std::numeric_limits<unsigned>::max();
    6031       23416 :   if (DisableStoreExtract || !TLI ||
    6032       23369 :       (!StressStoreExtract &&
    6033       46644 :        !TLI->canCombineStoreAndExtract(Inst->getOperand(0)->getType(),
    6034       11661 :                                        Inst->getOperand(1), CombineCost)))
    6035             :     return false;
    6036             : 
    6037             :   // At this point we know that Inst is a vector to scalar transition.
    6038             :   // Try to move it down the def-use chain, until:
    6039             :   // - We can combine the transition with its single use
    6040             :   //   => we got rid of the transition.
    6041             :   // - We escape the current basic block
    6042             :   //   => we would need to check that we are moving it at a cheaper place and
    6043             :   //      we do not do that for now.
    6044         135 :   BasicBlock *Parent = Inst->getParent();
    6045             :   DEBUG(dbgs() << "Found an interesting transition: " << *Inst << '\n');
    6046         135 :   VectorPromoteHelper VPH(*DL, *TLI, *TTI, Inst, CombineCost);
    6047             :   // If the transition has more than one use, assume this is not going to be
    6048             :   // beneficial.
    6049         393 :   while (Inst->hasOneUse()) {
    6050         652 :     Instruction *ToBePromoted = cast<Instruction>(*Inst->user_begin());
    6051             :     DEBUG(dbgs() << "Use: " << *ToBePromoted << '\n');
    6052             : 
    6053         163 :     if (ToBePromoted->getParent() != Parent) {
    6054             :       DEBUG(dbgs() << "Instruction to promote is in a different block ("
    6055             :                    << ToBePromoted->getParent()->getName()
    6056             :                    << ") than the transition (" << Parent->getName() << ").\n");
    6057             :       return false;
    6058             :     }
    6059             : 
    6060         320 :     if (VPH.canCombine(ToBePromoted)) {
    6061             :       DEBUG(dbgs() << "Assume " << *Inst << '\n'
    6062             :                    << "will be combined with: " << *ToBePromoted << '\n');
    6063         146 :       VPH.recordCombineInstruction(ToBePromoted);
    6064          73 :       bool Changed = VPH.promote();
    6065          73 :       NumStoreExtractExposed += Changed;
    6066          73 :       return Changed;
    6067             :     }
    6068             : 
    6069             :     DEBUG(dbgs() << "Try promoting.\n");
    6070         174 :     if (!VPH.canPromote(ToBePromoted) || !VPH.shouldPromote(ToBePromoted))
    6071             :       return false;
    6072             : 
    6073             :     DEBUG(dbgs() << "Promoting is possible... Enqueue for promotion!\n");
    6074             : 
    6075          41 :     VPH.enqueueForPromotion(ToBePromoted);
    6076          41 :     Inst = ToBePromoted;
    6077             :   }
    6078             :   return false;
    6079             : }
    6080             : 
    6081             : /// For the instruction sequence of store below, F and I values
    6082             : /// are bundled together as an i64 value before being stored into memory.
    6083             : /// Sometimes it is more efficent to generate separate stores for F and I,
    6084             : /// which can remove the bitwise instructions or sink them to colder places.
    6085             : ///
    6086             : ///   (store (or (zext (bitcast F to i32) to i64),
    6087             : ///              (shl (zext I to i64), 32)), addr)  -->
    6088             : ///   (store F, addr) and (store I, addr+4)
    6089             : ///
    6090             : /// Similarly, splitting for other merged store can also be beneficial, like:
    6091             : /// For pair of {i32, i32}, i64 store --> two i32 stores.
    6092             : /// For pair of {i32, i16}, i64 store --> two i32 stores.
    6093             : /// For pair of {i16, i16}, i32 store --> two i16 stores.
    6094             : /// For pair of {i16, i8},  i32 store --> two i16 stores.
    6095             : /// For pair of {i8, i8},   i16 store --> two i8 stores.
    6096             : ///
    6097             : /// We allow each target to determine specifically which kind of splitting is
    6098             : /// supported.
    6099             : ///
    6100             : /// The store patterns are commonly seen from the simple code snippet below
    6101             : /// if only std::make_pair(...) is sroa transformed before inlined into hoo.
    6102             : ///   void goo(const std::pair<int, float> &);
    6103             : ///   hoo() {
    6104             : ///     ...
    6105             : ///     goo(std::make_pair(tmp, ftmp));
    6106             : ///     ...
    6107             : ///   }
    6108             : ///
    6109             : /// Although we already have similar splitting in DAG Combine, we duplicate
    6110             : /// it in CodeGenPrepare to catch the case in which pattern is across
    6111             : /// multiple BBs. The logic in DAG Combine is kept to catch case generated
    6112             : /// during code expansion.
    6113      504715 : static bool splitMergedValStore(StoreInst &SI, const DataLayout &DL,
    6114             :                                 const TargetLowering &TLI) {
    6115             :   // Handle simple but common cases only.
    6116      504715 :   Type *StoreType = SI.getValueOperand()->getType();
    6117     1008099 :   if (DL.getTypeStoreSizeInBits(StoreType) != DL.getTypeSizeInBits(StoreType) ||
    6118      503384 :       DL.getTypeSizeInBits(StoreType) == 0)
    6119             :     return false;
    6120             : 
    6121      503367 :   unsigned HalfValBitSize = DL.getTypeSizeInBits(StoreType) / 2;
    6122      503367 :   Type *SplitStoreType = Type::getIntNTy(SI.getContext(), HalfValBitSize);
    6123     1510101 :   if (DL.getTypeStoreSizeInBits(SplitStoreType) !=
    6124      503367 :       DL.getTypeSizeInBits(SplitStoreType))
    6125             :     return false;
    6126             : 
    6127             :   // Match the following patterns:
    6128             :   // (store (or (zext LValue to i64),
    6129             :   //            (shl (zext HValue to i64), 32)), HalfValBitSize)
    6130             :   //  or
    6131             :   // (store (or (shl (zext HValue to i64), 32)), HalfValBitSize)
    6132             :   //            (zext LValue to i64),
    6133             :   // Expect both operands of OR and the first operand of SHL have only
    6134             :   // one use.
    6135             :   Value *LValue, *HValue;
    6136     1481322 :   if (!match(SI.getValueOperand(),
    6137     2468870 :              m_c_Or(m_OneUse(m_ZExt(m_Value(LValue))),
    6138     2468870 :                     m_OneUse(m_Shl(m_OneUse(m_ZExt(m_Value(HValue))),
    6139      987548 :                                    m_SpecificInt(HalfValBitSize))))))
    6140             :     return false;
    6141             : 
    6142             :   // Check LValue and HValue are int with size less or equal than 32.
    6143         204 :   if (!LValue->getType()->isIntegerTy() ||
    6144         136 :       DL.getTypeSizeInBits(LValue->getType()) > HalfValBitSize ||
    6145         272 :       !HValue->getType()->isIntegerTy() ||
    6146          68 :       DL.getTypeSizeInBits(HValue->getType()) > HalfValBitSize)
    6147             :     return false;
    6148             : 
    6149             :   // If LValue/HValue is a bitcast instruction, use the EVT before bitcast
    6150             :   // as the input of target query.
    6151         136 :   auto *LBC = dyn_cast<BitCastInst>(LValue);
    6152         136 :   auto *HBC = dyn_cast<BitCastInst>(HValue);
    6153           2 :   EVT LowTy = LBC ? EVT::getEVT(LBC->getOperand(0)->getType())
    6154          69 :                   : EVT::getEVT(LValue->getType());
    6155          12 :   EVT HighTy = HBC ? EVT::getEVT(HBC->getOperand(0)->getType())
    6156          74 :                    : EVT::getEVT(HValue->getType());
    6157          68 :   if (!ForceSplitStore && !TLI.isMultiStoresCheaperThanBitsMerge(LowTy, HighTy))
    6158             :     return false;
    6159             : 
    6160             :   // Start to split store.
    6161          28 :   IRBuilder<> Builder(SI.getContext());
    6162          14 :   Builder.SetInsertPoint(&SI);
    6163             : 
    6164             :   // If LValue/HValue is a bitcast in another BB, create a new one in current
    6165             :   // BB so it may be merged with the splitted stores by dag combiner.
    6166          14 :   if (LBC && LBC->getParent() != SI.getParent())
    6167           0 :     LValue = Builder.CreateBitCast(LBC->getOperand(0), LBC->getType());
    6168          14 :   if (HBC && HBC->getParent() != SI.getParent())
    6169           9 :     HValue = Builder.CreateBitCast(HBC->getOperand(0), HBC->getType());
    6170             : 
    6171          28 :   auto CreateSplitStore = [&](Value *V, bool Upper) {
    6172         140 :     V = Builder.CreateZExtOrBitCast(V, SplitStoreType);
    6173          84 :     Value *Addr = Builder.CreateBitCast(
    6174          98 :         SI.getOperand(1),
    6175          84 :         SplitStoreType->getPointerTo(SI.getPointerAddressSpace()));
    6176          28 :     if (Upper)
    6177          28 :       Addr = Builder.CreateGEP(
    6178             :           SplitStoreType, Addr,
    6179          14 :           ConstantInt::get(Type::getInt32Ty(SI.getContext()), 1));
    6180          84 :     Builder.CreateAlignedStore(
    6181          14 :         V, Addr, Upper ? SI.getAlignment() / 2 : SI.getAlignment());
    6182          42 :   };
    6183             : 
    6184          14 :   CreateSplitStore(LValue, false);
    6185          14 :   CreateSplitStore(HValue, true);
    6186             : 
    6187             :   // Delete the old store.
    6188          14 :   SI.eraseFromParent();
    6189          14 :   return true;
    6190             : }
    6191             : 
    6192             : // Return true if the GEP has two operands, the first operand is of a sequential
    6193             : // type, and the second operand is a constant.
    6194          74 : static bool GEPSequentialConstIndexed(GetElementPtrInst *GEP) {
    6195          74 :   gep_type_iterator I = gep_type_begin(*GEP);
    6196          95 :   return GEP->getNumOperands() == 2 &&
    6197         116 :       I.isSequential() &&
    6198          95 :       isa<ConstantInt>(GEP->getOperand(1));
    6199             : }
    6200             : 
    6201             : // Try unmerging GEPs to reduce liveness interference (register pressure) across
    6202             : // IndirectBr edges. Since IndirectBr edges tend to touch on many blocks,
    6203             : // reducing liveness interference across those edges benefits global register
    6204             : // allocation. Currently handles only certain cases.
    6205             : //
    6206             : // For example, unmerge %GEPI and %UGEPI as below.
    6207             : //
    6208             : // ---------- BEFORE ----------
    6209             : // SrcBlock:
    6210             : //   ...
    6211             : //   %GEPIOp = ...
    6212             : //   ...
    6213             : //   %GEPI = gep %GEPIOp, Idx
    6214             : //   ...
    6215             : //   indirectbr ... [ label %DstB0, label %DstB1, ... label %DstBi ... ]
    6216             : //   (* %GEPI is alive on the indirectbr edges due to other uses ahead)
    6217             : //   (* %GEPIOp is alive on the indirectbr edges only because of it's used by
    6218             : //   %UGEPI)
    6219             : //
    6220             : // DstB0: ... (there may be a gep similar to %UGEPI to be unmerged)
    6221             : // DstB1: ... (there may be a gep similar to %UGEPI to be unmerged)
    6222             : // ...
    6223             : //
    6224             : // DstBi:
    6225             : //   ...
    6226             : //   %UGEPI = gep %GEPIOp, UIdx
    6227             : // ...
    6228             : // ---------------------------
    6229             : //
    6230             : // ---------- AFTER ----------
    6231             : // SrcBlock:
    6232             : //   ... (same as above)
    6233             : //    (* %GEPI is still alive on the indirectbr edges)
    6234             : //    (* %GEPIOp is no longer alive on the indirectbr edges as a result of the
    6235             : //    unmerging)
    6236             : // ...
    6237             : //
    6238             : // DstBi:
    6239             : //   ...
    6240             : //   %UGEPI = gep %GEPI, (UIdx-Idx)
    6241             : //   ...
    6242             : // ---------------------------
    6243             : //
    6244             : // The register pressure on the IndirectBr edges is reduced because %GEPIOp is
    6245             : // no longer alive on them.
    6246             : //
    6247             : // We try to unmerge GEPs here in CodGenPrepare, as opposed to limiting merging
    6248             : // of GEPs in the first place in InstCombiner::visitGetElementPtrInst() so as
    6249             : // not to disable further simplications and optimizations as a result of GEP
    6250             : // merging.
    6251             : //
    6252             : // Note this unmerging may increase the length of the data flow critical path
    6253             : // (the path from %GEPIOp to %UGEPI would go through %GEPI), which is a tradeoff
    6254             : // between the register pressure and the length of data-flow critical
    6255             : // path. Restricting this to the uncommon IndirectBr case would minimize the
    6256             : // impact of potentially longer critical path, if any, and the impact on compile
    6257             : // time.
    6258      141899 : static bool tryUnmergingGEPsAcrossIndirectBr(GetElementPtrInst *GEPI,
    6259             :                                              const TargetTransformInfo *TTI) {
    6260      141899 :   BasicBlock *SrcBlock = GEPI->getParent();
    6261             :   // Check that SrcBlock ends with an IndirectBr. If not, give up. The common
    6262             :   // (non-IndirectBr) cases exit early here.
    6263      425697 :   if (!isa<IndirectBrInst>(SrcBlock->getTerminator()))
    6264             :     return false;
    6265             :   // Check that GEPI is a simple gep with a single constant index.
    6266          71 :   if (!GEPSequentialConstIndexed(GEPI))
    6267             :     return false;
    6268          16 :   ConstantInt *GEPIIdx = cast<ConstantInt>(GEPI->getOperand(1));
    6269             :   // Check that GEPI is a cheap one.
    6270          16 :   if (TTI->getIntImmCost(GEPIIdx->getValue(), GEPIIdx->getType())
    6271             :       > TargetTransformInfo::TCC_Basic)
    6272             :     return false;
    6273           8 :   Value *GEPIOp = GEPI->getOperand(0);
    6274             :   // Check that GEPIOp is an instruction that's also defined in SrcBlock.
    6275          16 :   if (!isa<Instruction>(GEPIOp))
    6276             :     return false;
    6277          16 :   auto *GEPIOpI = cast<Instruction>(GEPIOp);
    6278           8 :   if (GEPIOpI->getParent() != SrcBlock)
    6279             :     return false;
    6280             :   // Check that GEP is used outside the block, meaning it's alive on the
    6281             :   // IndirectBr edge(s).
    6282          32 :   if (find_if(GEPI->users(), [&](User *Usr) {
    6283           9 :         if (auto *I = dyn_cast<Instruction>(Usr)) {
    6284           9 :           if (I->getParent() != SrcBlock) {
    6285             :             return true;
    6286             :           }
    6287             :         }
    6288             :         return false;
    6289          24 :       }) == GEPI->users().end())
    6290             :     return false;
    6291             :   // The second elements of the GEP chains to be unmerged.
    6292           6 :   std::vector<GetElementPtrInst *> UGEPIs;
    6293             :   // Check each user of GEPIOp to check if unmerging would make GEPIOp not alive
    6294             :   // on IndirectBr edges.
    6295          48 :   for (User *Usr : GEPIOp->users()) {
    6296          27 :     if (Usr == GEPI) continue;
    6297             :     // Check if Usr is an Instruction. If not, give up.
    6298          18 :     if (!isa<Instruction>(Usr))
    6299           0 :       return false;
    6300          18 :     auto *UI = cast<Instruction>(Usr);
    6301             :     // Check if Usr in the same block as GEPIOp, which is fine, skip.
    6302           9 :     if (UI->getParent() == SrcBlock)
    6303           6 :       continue;
    6304             :     // Check if Usr is a GEP. If not, give up.
    6305           3 :     if (!isa<GetElementPtrInst>(Usr))
    6306             :       return false;
    6307           6 :     auto *UGEPI = cast<GetElementPtrInst>(Usr);
    6308             :     // Check if UGEPI is a simple gep with a single constant index and GEPIOp is
    6309             :     // the pointer operand to it. If so, record it in the vector. If not, give
    6310             :     // up.
    6311           3 :     if (!GEPSequentialConstIndexed(UGEPI))
    6312             :       return false;
    6313           6 :     if (UGEPI->getOperand(0) != GEPIOp)
    6314             :       return false;
    6315           6 :     if (GEPIIdx->getType() !=
    6316          12 :         cast<ConstantInt>(UGEPI->getOperand(1))->getType())
    6317             :       return false;
    6318           9 :     ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1));
    6319           6 :     if (TTI->getIntImmCost(UGEPIIdx->getValue(), UGEPIIdx->getType())
    6320             :         > TargetTransformInfo::TCC_Basic)
    6321             :       return false;
    6322           3 :     UGEPIs.push_back(UGEPI);
    6323             :   }
    6324          12 :   if (UGEPIs.size() == 0)
    6325             :     return false;
    6326             :   // Check the materializing cost of (Uidx-Idx).
    6327           4 :   for (GetElementPtrInst *UGEPI : UGEPIs) {
    6328           6 :     ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1));
    6329          18 :     APInt NewIdx = UGEPIIdx->getValue() - GEPIIdx->getValue();
    6330           3 :     unsigned ImmCost = TTI->getIntImmCost(NewIdx, GEPIIdx->getType());
    6331           3 :     if (ImmCost > TargetTransformInfo::TCC_Basic)
    6332           0 :       return false;
    6333             :   }
    6334             :   // Now unmerge between GEPI and UGEPIs.
    6335           7 :   for (GetElementPtrInst *UGEPI : UGEPIs) {
    6336           3 :     UGEPI->setOperand(0, GEPI);
    6337           6 :     ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1));
    6338             :     Constant *NewUGEPIIdx =
    6339           3 :         ConstantInt::get(GEPIIdx->getType(),
    6340          21 :                          UGEPIIdx->getValue() - GEPIIdx->getValue());
    6341           3 :     UGEPI->setOperand(1, NewUGEPIIdx);
    6342             :     // If GEPI is not inbounds but UGEPI is inbounds, change UGEPI to not
    6343             :     // inbounds to avoid UB.
    6344           3 :     if (!GEPI->isInBounds()) {
    6345           3 :       UGEPI->setIsInBounds(false);
    6346             :     }
    6347             :   }
    6348             :   // After unmerging, verify that GEPIOp is actually only used in SrcBlock (not
    6349             :   // alive on IndirectBr edges).
    6350             :   assert(find_if(GEPIOp->users(), [&](User *Usr) {
    6351             :         return cast<Instruction>(Usr)->getParent() != SrcBlock;
    6352             :       }) == GEPIOp->users().end() && "GEPIOp is used outside SrcBlock");
    6353             :   return true;
    6354             : }
    6355             : 
    6356     3214026 : bool CodeGenPrepare::optimizeInst(Instruction *I, bool &ModifiedDT) {
    6357             :   // Bail out if we inserted the instruction to prevent optimizations from
    6358             :   // stepping on each other's toes.
    6359     3214026 :   if (InsertedInsts.count(I))
    6360             :     return false;
    6361             : 
    6362     3290210 :   if (PHINode *P = dyn_cast<PHINode>(I)) {
    6363             :     // It is possible for very late stage optimizations (such as SimplifyCFG)
    6364             :     // to introduce PHI nodes too late to be cleaned up.  If we detect such a
    6365             :     // trivial PHI, go ahead and zap it here.
    6366      153654 :     if (Value *V = SimplifyInstruction(P, {*DL, TLInfo})) {
    6367         383 :       P->replaceAllUsesWith(V);
    6368         383 :       P->eraseFromParent();
    6369         383 :       ++NumPHIsElim;
    6370         383 :       return true;
    6371             :     }
    6372             :     return false;
    6373             :   }
    6374             : 
    6375     3404447 :   if (CastInst *CI = dyn_cast<CastInst>(I)) {
    6376             :     // If the source of the cast is a constant, then this should have
    6377             :     // already been constant folded.  The only reason NOT to constant fold
    6378             :     // it is if something (e.g. LSR) was careful to place the constant
    6379             :     // evaluation in a block other than then one that uses it (e.g. to hoist
    6380             :     // the address of globals out of a loop).  If this is the case, we don't
    6381             :     // want to forward-subst the cast.
    6382      535782 :     if (isa<Constant>(CI->getOperand(0)))
    6383             :       return false;
    6384             : 
    6385      264845 :     if (TLI && OptimizeNoopCopyExpression(CI, *TLI, *DL))
    6386             :       return true;
    6387             : 
    6388      715598 :     if (isa<ZExtInst>(I) || isa<SExtInst>(I)) {
    6389             :       /// Sink a zext or sext into its user blocks if the target type doesn't
    6390             :       /// fit in one register
    6391       57048 :       if (TLI &&
    6392       85569 :           TLI->getTypeAction(CI->getContext(),
    6393       28523 :                              TLI->getValueType(*DL, CI->getType())) ==
    6394             :               TargetLowering::TypeExpandInteger) {
    6395        3166 :         return SinkCast(CI);
    6396             :       } else {
    6397       25359 :         bool MadeChange = optimizeExt(I);
    6398       25359 :         return MadeChange | optimizeExtUses(I);
    6399             :       }
    6400             :     }
    6401             :     return false;
    6402             :   }
    6403             : 
    6404     2952420 :   if (CmpInst *CI = dyn_cast<CmpInst>(I))
    6405       83755 :     if (!TLI || !TLI->hasMultipleConditionRegisters())
    6406       77853 :       return OptimizeCmpExpression(CI, TLI);
    6407             : 
    6408     3275201 :   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
    6409      484389 :     LI->setMetadata(LLVMContext::MD_invariant_group, nullptr);
    6410      484389 :     if (TLI) {
    6411      484364 :       bool Modified = optimizeLoadExt(LI);
    6412      484364 :       unsigned AS = LI->getPointerAddressSpace();
    6413      968728 :       Modified |= optimizeMemoryInst(I, I->getOperand(0), LI->getType(), AS);
    6414      484364 :       return Modified;
    6415             :     }
    6416             :     return false;
    6417             :   }
    6418             : 
    6419     2811155 :   if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
    6420      504732 :     if (TLI && splitMergedValStore(*SI, *DL, *TLI))
    6421             :       return true;
    6422      504718 :     SI->setMetadata(LLVMContext::MD_invariant_group, nullptr);
    6423      504718 :     if (TLI) {
    6424      504701 :       unsigned AS = SI->getPointerAddressSpace();
    6425     1009402 :       return optimizeMemoryInst(I, SI->getOperand(1),
    6426      504701 :                                 SI->getOperand(0)->getType(), AS);
    6427             :     }
    6428             :     return false;
    6429             :   }
    6430             : 
    6431     1805520 :   if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
    6432        3829 :       unsigned AS = RMW->getPointerAddressSpace();
    6433        7658 :       return optimizeMemoryInst(I, RMW->getPointerOperand(),
    6434        3829 :                                 RMW->getType(), AS);
    6435             :   }
    6436             : 
    6437     1799096 :   if (AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(I)) {
    6438        1234 :       unsigned AS = CmpX->getPointerAddressSpace();
    6439        2468 :       return optimizeMemoryInst(I, CmpX->getPointerOperand(),
    6440        1234 :                                 CmpX->getCompareOperand()->getType(), AS);
    6441             :   }
    6442             : 
    6443     2271246 :   BinaryOperator *BinOp = dyn_cast<BinaryOperator>(I);
    6444             : 
    6445      487938 :   if (BinOp && (BinOp->getOpcode() == Instruction::And) &&
    6446       26640 :       EnableAndCmpSinking && TLI)
    6447       13320 :     return sinkAndCmp0Expression(BinOp, *TLI, InsertedInsts);
    6448             : 
    6449     2702275 :   if (BinOp && (BinOp->getOpcode() == Instruction::AShr ||
    6450      457669 :                 BinOp->getOpcode() == Instruction::LShr)) {
    6451       21268 :     ConstantInt *CI = dyn_cast<ConstantInt>(BinOp->getOperand(1));
    6452       10634 :     if (TLI && CI && TLI->hasExtractBitsInsn())
    6453        1064 :       return OptimizeExtractBits(BinOp, CI, *TLI, *DL);
    6454             : 
    6455             :     return false;
    6456             :   }
    6457             : 
    6458     1929072 :   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
    6459      156398 :     if (GEPI->hasAllZeroIndices()) {
    6460             :       /// The GEP operand must be a pointer, so must its result -> BitCast
    6461       14499 :       Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(),
    6462       43497 :                                         GEPI->getName(), GEPI);
    6463       14499 :       GEPI->replaceAllUsesWith(NC);
    6464       14499 :       GEPI->eraseFromParent();
    6465       14499 :       ++NumGEPsElim;
    6466       14499 :       optimizeInst(NC, ModifiedDT);
    6467       14499 :       return true;
    6468             :     }
    6469      141899 :     if (tryUnmergingGEPsAcrossIndirectBr(GEPI, TTI)) {
    6470             :       return true;
    6471             :     }
    6472      141898 :     return false;
    6473             :   }
    6474             : 
    6475     2082966 :   if (CallInst *CI = dyn_cast<CallInst>(I))
    6476      466690 :     return optimizeCallInst(CI, ModifiedDT);
    6477             : 
    6478     1213691 :   if (SelectInst *SI = dyn_cast<SelectInst>(I))
    6479       64105 :     return optimizeSelectInst(SI);
    6480             : 
    6481     1111971 :   if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I))
    6482       26490 :     return optimizeShuffleVectorInst(SVI);
    6483             : 
    6484     1060316 :   if (auto *Switch = dyn_cast<SwitchInst>(I))
    6485        1325 :     return optimizeSwitchInst(Switch);
    6486             : 
    6487     2115332 :   if (isa<ExtractElementInst>(I))
    6488       11708 :     return optimizeExtractElementInst(I);
    6489             : 
    6490             :   return false;
    6491             : }
    6492             : 
    6493             : /// Given an OR instruction, check to see if this is a bitreverse
    6494             : /// idiom. If so, insert the new intrinsic and return true.
    6495     3228974 : static bool makeBitReverse(Instruction &I, const DataLayout &DL,
    6496             :                            const TargetLowering &TLI) {
    6497     6457948 :   if (!I.getType()->isIntegerTy() ||
    6498      856288 :       !TLI.isOperationLegalOrCustom(ISD::BITREVERSE,
    6499             :                                     TLI.getValueType(DL, I.getType(), true)))
    6500             :     return false;
    6501             : 
    6502       42084 :   SmallVector<Instruction*, 4> Insts;
    6503       42084 :   if (!recognizeBSwapOrBitReverseIdiom(&I, false, true, Insts))
    6504             :     return false;
    6505          12 :   Instruction *LastInst = Insts.back();
    6506           6 :   I.replaceAllUsesWith(LastInst);
    6507           6 :   RecursivelyDeleteTriviallyDeadInstructions(&I);
    6508           6 :   return true;
    6509             : }
    6510             : 
    6511             : // In this pass we look for GEP and cast instructions that are used
    6512             : // across basic blocks and rewrite them to improve basic-block-at-a-time
    6513             : // selection.
    6514      402172 : bool CodeGenPrepare::optimizeBlock(BasicBlock &BB, bool &ModifiedDT) {
    6515      804344 :   SunkAddrs.clear();
    6516      402172 :   bool MadeChange = false;
    6517             : 
    6518      402172 :   CurInstIterator = BB.begin();
    6519     7202992 :   while (CurInstIterator != BB.end()) {
    6520     9598581 :     MadeChange |= optimizeInst(&*CurInstIterator++, ModifiedDT);
    6521     3199527 :     if (ModifiedDT)
    6522             :       return true;
    6523             :   }
    6524             : 
    6525             :   bool MadeBitReverse = true;
    6526      803782 :   while (TLI && MadeBitReverse) {
    6527      401813 :     MadeBitReverse = false;
    6528     4032600 :     for (auto &I : reverse(BB)) {
    6529     3228974 :       if (makeBitReverse(I, *DL, *TLI)) {
    6530           6 :         MadeBitReverse = MadeChange = true;
    6531           6 :         ModifiedDT = true;
    6532           6 :         break;
    6533             :       }
    6534             :     }
    6535             :   }
    6536      401969 :   MadeChange |= dupRetToEnableTailCallOpts(&BB);
    6537             : 
    6538      401969 :   return MadeChange;
    6539             : }
    6540             : 
    6541             : // llvm.dbg.value is far away from the value then iSel may not be able
    6542             : // handle it properly. iSel will drop llvm.dbg.value if it can not
    6543             : // find a node corresponding to the value.
    6544      136502 : bool CodeGenPrepare::placeDbgValues(Function &F) {
    6545      136502 :   bool MadeChange = false;
    6546      683750 :   for (BasicBlock &BB : F) {
    6547      274244 :     Instruction *PrevNonDbgInst = nullptr;
    6548      548488 :     for (BasicBlock::iterator BI = BB.begin(), BE = BB.end(); BI != BE;) {
    6549     5591109 :       Instruction *Insn = &*BI++;
    6550       27663 :       DbgValueInst *DVI = dyn_cast<DbgValueInst>(Insn);
    6551             :       // Leave dbg.values that refer to an alloca alone. These
    6552             :       // instrinsics describe the address of a variable (= the alloca)
    6553             :       // being taken.  They should not be moved next to the alloca
    6554             :       // (and to the beginning of the scope), but rather stay close to
    6555             :       // where said address is used.
    6556     1894654 :       if (!DVI || (DVI->getValue() && isa<AllocaInst>(DVI->getValue()))) {
    6557     1840476 :         PrevNonDbgInst = Insn;
    6558             :         continue;
    6559             :       }
    6560             : 
    6561       46454 :       Instruction *VI = dyn_cast_or_null<Instruction>(DVI->getValue());
    6562       31573 :       if (VI && VI != PrevNonDbgInst && !VI->isTerminator()) {
    6563             :         // If VI is a phi in a block with an EHPad terminator, we can't insert
    6564             :         // after it.
    6565       17694 :         if (isa<PHINode>(VI) && VI->getParent()->getTerminator()->isEHPad())
    6566             :           continue;
    6567             :         DEBUG(dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI);
    6568        7802 :         DVI->removeFromParent();
    6569       15604 :         if (isa<PHINode>(VI))
    6570        6258 :           DVI->insertBefore(&*VI->getParent()->getFirstInsertionPt());
    6571             :         else
    6572        5716 :           DVI->insertAfter(VI);
    6573             :         MadeChange = true;
    6574             :         ++NumDbgValueMoved;
    6575             :       }
    6576             :     }
    6577             :   }
    6578      136502 :   return MadeChange;
    6579             : }
    6580             : 
    6581             : /// \brief Scale down both weights to fit into uint32_t.
    6582             : static void scaleWeights(uint64_t &NewTrue, uint64_t &NewFalse) {
    6583           4 :   uint64_t NewMax = (NewTrue > NewFalse) ? NewTrue : NewFalse;
    6584           4 :   uint32_t Scale = (NewMax / std::numeric_limits<uint32_t>::max()) + 1;
    6585           4 :   NewTrue = NewTrue / Scale;
    6586           4 :   NewFalse = NewFalse / Scale;
    6587             : }
    6588             : 
    6589             : /// \brief Some targets prefer to split a conditional branch like:
    6590             : /// \code
    6591             : ///   %0 = icmp ne i32 %a, 0
    6592             : ///   %1 = icmp ne i32 %b, 0
    6593             : ///   %or.cond = or i1 %0, %1
    6594             : ///   br i1 %or.cond, label %TrueBB, label %FalseBB
    6595             : /// \endcode
    6596             : /// into multiple branch instructions like:
    6597             : /// \code
    6598             : ///   bb1:
    6599             : ///     %0 = icmp ne i32 %a, 0
    6600             : ///     br i1 %0, label %TrueBB, label %bb2
    6601             : ///   bb2:
    6602             : ///     %1 = icmp ne i32 %b, 0
    6603             : ///     br i1 %1, label %TrueBB, label %FalseBB
    6604             : /// \endcode
    6605             : /// This usually allows instruction selection to do even further optimizations
    6606             : /// and combine the compare with the branch instruction. Currently this is
    6607             : /// applied for targets which have "cheap" jump instructions.
    6608             : ///
    6609             : /// FIXME: Remove the (equivalent?) implementation in SelectionDAG.
    6610             : ///
    6611      136439 : bool CodeGenPrepare::splitBranchCondition(Function &F) {
    6612      136439 :   if (!TM || !TM->Options.EnableFastISel || !TLI || TLI->isJumpExpensive())
    6613             :     return false;
    6614             : 
    6615        3610 :   bool MadeChange = false;
    6616       14809 :   for (auto &BB : F) {
    6617             :     // Does this BB end with the following?
    6618             :     //   %cond1 = icmp|fcmp|binary instruction ...
    6619             :     //   %cond2 = icmp|fcmp|binary instruction ...
    6620             :     //   %cond.or = or|and i1 %cond1, cond2
    6621             :     //   br i1 %cond.or label %dest1, label %dest2"
    6622             :     BinaryOperator *LogicOp;
    6623             :     BasicBlock *TBB, *FBB;
    6624       19895 :     if (!match(BB.getTerminator(), m_Br(m_OneUse(m_BinOp(LogicOp)), TBB, FBB)))
    6625        7950 :       continue;
    6626             : 
    6627          10 :     auto *Br1 = cast<BranchInst>(BB.getTerminator());
    6628           9 :     if (Br1->getMetadata(LLVMContext::MD_unpredictable))
    6629           2 :       continue;
    6630             : 
    6631             :     unsigned Opc;
    6632             :     Value *Cond1, *Cond2;
    6633          12 :     if (match(LogicOp, m_And(m_OneUse(m_Value(Cond1)),
    6634           6 :                              m_OneUse(m_Value(Cond2)))))
    6635             :       Opc = Instruction::And;
    6636           4 :     else if (match(LogicOp, m_Or(m_OneUse(m_Value(Cond1)),
    6637           2 :                                  m_OneUse(m_Value(Cond2)))))
    6638             :       Opc = Instruction::Or;
    6639             :     else
    6640           0 :       continue;
    6641             : 
    6642           6 :     if (!match(Cond1, m_CombineOr(m_Cmp(), m_BinOp())) ||
    6643           6 :         !match(Cond2, m_CombineOr(m_Cmp(), m_BinOp()))   )
    6644           0 :       continue;
    6645             : 
    6646             :     DEBUG(dbgs() << "Before branch condition splitting\n"; BB.dump());
    6647             : 
    6648             :     // Create a new BB.
    6649             :     auto TmpBB =
    6650          12 :         BasicBlock::Create(BB.getContext(), BB.getName() + ".cond.split",
    6651           3 :                            BB.getParent(), BB.getNextNode());
    6652             : 
    6653             :     // Update original basic block by using the first condition directly by the
    6654             :     // branch instruction and removing the no longer needed and/or instruction.
    6655           6 :     Br1->setCondition(Cond1);
    6656           3 :     LogicOp->eraseFromParent();
    6657             : 
    6658             :     // Depending on the conditon we have to either replace the true or the false
    6659             :     // successor of the original branch instruction.
    6660           3 :     if (Opc == Instruction::And)
    6661             :       Br1->setSuccessor(0, TmpBB);
    6662             :     else
    6663             :       Br1->setSuccessor(1, TmpBB);
    6664             : 
    6665             :     // Fill in the new basic block.
    6666           9 :     auto *Br2 = IRBuilder<>(TmpBB).CreateCondBr(Cond2, TBB, FBB);
    6667           6 :     if (auto *I = dyn_cast<Instruction>(Cond2)) {
    6668           3 :       I->removeFromParent();
    6669           3 :       I->insertBefore(Br2);
    6670             :     }
    6671             : 
    6672             :     // Update PHI nodes in both successors. The original BB needs to be
    6673             :     // replaced in one successor's PHI nodes, because the branch comes now from
    6674             :     // the newly generated BB (NewBB). In the other successor we need to add one
    6675             :     // incoming edge to the PHI nodes, because both branch instructions target
    6676             :     // now the same successor. Depending on the original branch condition
    6677             :     // (and/or) we have to swap the successors (TrueDest, FalseDest), so that
    6678             :     // we perform the correct update for the PHI nodes.
    6679             :     // This doesn't change the successor order of the just created branch
    6680             :     // instruction (or any other instruction).
    6681           3 :     if (Opc == Instruction::Or)
    6682             :       std::swap(TBB, FBB);
    6683             : 
    6684             :     // Replace the old BB with the new BB.
    6685          12 :     for (auto &I : *TBB) {
    6686             :       PHINode *PN = dyn_cast<PHINode>(&I);
    6687             :       if (!PN)
    6688             :         break;
    6689             :       int i;
    6690           0 :       while ((i = PN->getBasicBlockIndex(&BB)) >= 0)
    6691           0 :         PN->setIncomingBlock(i, TmpBB);
    6692             :     }
    6693             : 
    6694             :     // Add another incoming edge form the new BB.
    6695          12 :     for (auto &I : *FBB) {
    6696           0 :       PHINode *PN = dyn_cast<PHINode>(&I);
    6697             :       if (!PN)
    6698             :         break;
    6699           0 :       auto *Val = PN->getIncomingValueForBlock(&BB);
    6700           0 :       PN->addIncoming(Val, TmpBB);
    6701             :     }
    6702             : 
    6703             :     // Update the branch weights (from SelectionDAGBuilder::
    6704             :     // FindMergedConditions).
    6705           3 :     if (Opc == Instruction::Or) {
    6706             :       // Codegen X | Y as:
    6707             :       // BB1:
    6708             :       //   jmp_if_X TBB
    6709             :       //   jmp TmpBB
    6710             :       // TmpBB:
    6711             :       //   jmp_if_Y TBB
    6712             :       //   jmp FBB
    6713             :       //
    6714             : 
    6715             :       // We have flexibility in setting Prob for BB1 and Prob for NewBB.
    6716             :       // The requirement is that
    6717             :       //   TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
    6718             :       //     = TrueProb for orignal BB.
    6719             :       // Assuming the orignal weights are A and B, one choice is to set BB1's
    6720             :       // weights to A and A+2B, and set TmpBB's weights to A and 2B. This choice
    6721             :       // assumes that
    6722             :       //   TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
    6723             :       // Another choice is to assume TrueProb for BB1 equals to TrueProb for
    6724             :       // TmpBB, but the math is more complicated.
    6725             :       uint64_t TrueWeight, FalseWeight;
    6726           1 :       if (Br1->extractProfMetadata(TrueWeight, FalseWeight)) {
    6727           1 :         uint64_t NewTrueWeight = TrueWeight;
    6728           1 :         uint64_t NewFalseWeight = TrueWeight + 2 * FalseWeight;
    6729           1 :         scaleWeights(NewTrueWeight, NewFalseWeight);
    6730           2 :         Br1->setMetadata(LLVMContext::MD_prof, MDBuilder(Br1->getContext())
    6731             :                          .createBranchWeights(TrueWeight, FalseWeight));
    6732             : 
    6733           1 :         NewTrueWeight = TrueWeight;
    6734           1 :         NewFalseWeight = 2 * FalseWeight;
    6735           1 :         scaleWeights(NewTrueWeight, NewFalseWeight);
    6736           2 :         Br2->setMetadata(LLVMContext::MD_prof, MDBuilder(Br2->getContext())
    6737             :                          .createBranchWeights(TrueWeight, FalseWeight));
    6738             :       }
    6739             :     } else {
    6740             :       // Codegen X & Y as:
    6741             :       // BB1:
    6742             :       //   jmp_if_X TmpBB
    6743             :       //   jmp FBB
    6744             :       // TmpBB:
    6745             :       //   jmp_if_Y TBB
    6746             :       //   jmp FBB
    6747             :       //
    6748             :       //  This requires creation of TmpBB after CurBB.
    6749             : 
    6750             :       // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
    6751             :       // The requirement is that
    6752             :       //   FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
    6753             :       //     = FalseProb for orignal BB.
    6754             :       // Assuming the orignal weights are A and B, one choice is to set BB1's
    6755             :       // weights to 2A+B and B, and set TmpBB's weights to 2A and B. This choice
    6756             :       // assumes that
    6757             :       //   FalseProb for BB1 == TrueProb for BB1 * FalseProb for TmpBB.
    6758             :       uint64_t TrueWeight, FalseWeight;
    6759           2 :       if (Br1->extractProfMetadata(TrueWeight, FalseWeight)) {
    6760           1 :         uint64_t NewTrueWeight = 2 * TrueWeight + FalseWeight;
    6761           1 :         uint64_t NewFalseWeight = FalseWeight;
    6762           1 :         scaleWeights(NewTrueWeight, NewFalseWeight);
    6763           2 :         Br1->setMetadata(LLVMContext::MD_prof, MDBuilder(Br1->getContext())
    6764             :                          .createBranchWeights(TrueWeight, FalseWeight));
    6765             : 
    6766           1 :         NewTrueWeight = 2 * TrueWeight;
    6767           1 :         NewFalseWeight = FalseWeight;
    6768           1 :         scaleWeights(NewTrueWeight, NewFalseWeight);
    6769           2 :         Br2->setMetadata(LLVMContext::MD_prof, MDBuilder(Br2->getContext())
    6770             :                          .createBranchWeights(TrueWeight, FalseWeight));
    6771             :       }
    6772             :     }
    6773             : 
    6774             :     // Note: No point in getting fancy here, since the DT info is never
    6775             :     // available to CodeGenPrepare.
    6776           3 :     ModifiedDT = true;
    6777             : 
    6778           3 :     MadeChange = true;
    6779             : 
    6780             :     DEBUG(dbgs() << "After branch condition splitting\n"; BB.dump();
    6781             :           TmpBB->dump());
    6782             :   }
    6783             :   return MadeChange;
    6784      216918 : }

Generated by: LCOV version 1.13