10b57cec5SDimitry Andric //===- SplitKit.h - Toolkit for splitting live ranges -----------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file contains the SplitAnalysis class as well as mutator functions for 100b57cec5SDimitry Andric // live range splitting. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #ifndef LLVM_LIB_CODEGEN_SPLITKIT_H 150b57cec5SDimitry Andric #define LLVM_LIB_CODEGEN_SPLITKIT_H 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 180b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 190b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 200b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 210b57cec5SDimitry Andric #include "llvm/ADT/IntervalMap.h" 220b57cec5SDimitry Andric #include "llvm/ADT/PointerIntPair.h" 230b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 240b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 255ffd83dbSDimitry Andric #include "llvm/CodeGen/LiveIntervalCalc.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 300b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 310b57cec5SDimitry Andric #include <utility> 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric namespace llvm { 340b57cec5SDimitry Andric 3581ad6265SDimitry Andric class LiveInterval; 3681ad6265SDimitry Andric class LiveRange; 370b57cec5SDimitry Andric class LiveIntervals; 380b57cec5SDimitry Andric class LiveRangeEdit; 390b57cec5SDimitry Andric class MachineBlockFrequencyInfo; 400b57cec5SDimitry Andric class MachineDominatorTree; 410b57cec5SDimitry Andric class MachineLoopInfo; 420b57cec5SDimitry Andric class MachineRegisterInfo; 430b57cec5SDimitry Andric class TargetInstrInfo; 440b57cec5SDimitry Andric class TargetRegisterInfo; 450b57cec5SDimitry Andric class VirtRegMap; 46fe6060f1SDimitry Andric class VirtRegAuxInfo; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric /// Determines the latest safe point in a block in which we can insert a split, 490b57cec5SDimitry Andric /// spill or other instruction related with CurLI. 500b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY InsertPointAnalysis { 510b57cec5SDimitry Andric private: 520b57cec5SDimitry Andric const LiveIntervals &LIS; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric /// Last legal insert point in each basic block in the current function. 550b57cec5SDimitry Andric /// The first entry is the first terminator, the second entry is the 560b57cec5SDimitry Andric /// last valid point to insert a split or spill for a variable that is 575ffd83dbSDimitry Andric /// live into a landing pad or inlineasm_br successor. 580b57cec5SDimitry Andric SmallVector<std::pair<SlotIndex, SlotIndex>, 8> LastInsertPoint; 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric SlotIndex computeLastInsertPoint(const LiveInterval &CurLI, 610b57cec5SDimitry Andric const MachineBasicBlock &MBB); 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric public: 640b57cec5SDimitry Andric InsertPointAnalysis(const LiveIntervals &lis, unsigned BBNum); 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric /// Return the base index of the last valid insert point for \pCurLI in \pMBB. getLastInsertPoint(const LiveInterval & CurLI,const MachineBasicBlock & MBB)670b57cec5SDimitry Andric SlotIndex getLastInsertPoint(const LiveInterval &CurLI, 680b57cec5SDimitry Andric const MachineBasicBlock &MBB) { 690b57cec5SDimitry Andric unsigned Num = MBB.getNumber(); 700b57cec5SDimitry Andric // Inline the common simple case. 710b57cec5SDimitry Andric if (LastInsertPoint[Num].first.isValid() && 720b57cec5SDimitry Andric !LastInsertPoint[Num].second.isValid()) 730b57cec5SDimitry Andric return LastInsertPoint[Num].first; 740b57cec5SDimitry Andric return computeLastInsertPoint(CurLI, MBB); 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric /// Returns the last insert point as an iterator for \pCurLI in \pMBB. 780b57cec5SDimitry Andric MachineBasicBlock::iterator getLastInsertPointIter(const LiveInterval &CurLI, 790b57cec5SDimitry Andric MachineBasicBlock &MBB); 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric /// Return the base index of the first insert point in \pMBB. getFirstInsertPoint(MachineBasicBlock & MBB)820b57cec5SDimitry Andric SlotIndex getFirstInsertPoint(MachineBasicBlock &MBB) { 830b57cec5SDimitry Andric SlotIndex Res = LIS.getMBBStartIdx(&MBB); 840b57cec5SDimitry Andric if (!MBB.empty()) { 850b57cec5SDimitry Andric MachineBasicBlock::iterator MII = MBB.SkipPHIsLabelsAndDebug(MBB.begin()); 860b57cec5SDimitry Andric if (MII != MBB.end()) 870b57cec5SDimitry Andric Res = LIS.getInstructionIndex(*MII); 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric return Res; 900b57cec5SDimitry Andric } 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric }; 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting 950b57cec5SDimitry Andric /// opportunities. 960b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY SplitAnalysis { 970b57cec5SDimitry Andric public: 980b57cec5SDimitry Andric const MachineFunction &MF; 990b57cec5SDimitry Andric const VirtRegMap &VRM; 1000b57cec5SDimitry Andric const LiveIntervals &LIS; 1010b57cec5SDimitry Andric const MachineLoopInfo &Loops; 1020b57cec5SDimitry Andric const TargetInstrInfo &TII; 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric /// Additional information about basic blocks where the current variable is 1050b57cec5SDimitry Andric /// live. Such a block will look like one of these templates: 1060b57cec5SDimitry Andric /// 1070b57cec5SDimitry Andric /// 1. | o---x | Internal to block. Variable is only live in this block. 1080b57cec5SDimitry Andric /// 2. |---x | Live-in, kill. 1090b57cec5SDimitry Andric /// 3. | o---| Def, live-out. 1100b57cec5SDimitry Andric /// 4. |---x o---| Live-in, kill, def, live-out. Counted by NumGapBlocks. 1110b57cec5SDimitry Andric /// 5. |---o---o---| Live-through with uses or defs. 1120b57cec5SDimitry Andric /// 6. |-----------| Live-through without uses. Counted by NumThroughBlocks. 1130b57cec5SDimitry Andric /// 1140b57cec5SDimitry Andric /// Two BlockInfo entries are created for template 4. One for the live-in 1150b57cec5SDimitry Andric /// segment, and one for the live-out segment. These entries look as if the 1160b57cec5SDimitry Andric /// block were split in the middle where the live range isn't live. 1170b57cec5SDimitry Andric /// 1180b57cec5SDimitry Andric /// Live-through blocks without any uses don't get BlockInfo entries. They 1190b57cec5SDimitry Andric /// are simply listed in ThroughBlocks instead. 1200b57cec5SDimitry Andric /// 1210b57cec5SDimitry Andric struct BlockInfo { 1220b57cec5SDimitry Andric MachineBasicBlock *MBB; 1230b57cec5SDimitry Andric SlotIndex FirstInstr; ///< First instr accessing current reg. 1240b57cec5SDimitry Andric SlotIndex LastInstr; ///< Last instr accessing current reg. 1250b57cec5SDimitry Andric SlotIndex FirstDef; ///< First non-phi valno->def, or SlotIndex(). 1260b57cec5SDimitry Andric bool LiveIn; ///< Current reg is live in. 1270b57cec5SDimitry Andric bool LiveOut; ///< Current reg is live out. 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric /// isOneInstr - Returns true when this BlockInfo describes a single 1300b57cec5SDimitry Andric /// instruction. isOneInstrBlockInfo1310b57cec5SDimitry Andric bool isOneInstr() const { 1320b57cec5SDimitry Andric return SlotIndex::isSameInstr(FirstInstr, LastInstr); 1330b57cec5SDimitry Andric } 134fe6060f1SDimitry Andric 135fe6060f1SDimitry Andric void print(raw_ostream &OS) const; 136fe6060f1SDimitry Andric void dump() const; 1370b57cec5SDimitry Andric }; 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric private: 1400b57cec5SDimitry Andric // Current live interval. 1410b57cec5SDimitry Andric const LiveInterval *CurLI = nullptr; 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric /// Insert Point Analysis. 1440b57cec5SDimitry Andric InsertPointAnalysis IPA; 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric // Sorted slot indexes of using instructions. 1470b57cec5SDimitry Andric SmallVector<SlotIndex, 8> UseSlots; 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric /// UseBlocks - Blocks where CurLI has uses. 1500b57cec5SDimitry Andric SmallVector<BlockInfo, 8> UseBlocks; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric /// NumGapBlocks - Number of duplicate entries in UseBlocks for blocks where 1530b57cec5SDimitry Andric /// the live range has a gap. 15406c3fb27SDimitry Andric unsigned NumGapBlocks = 0u; 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric /// ThroughBlocks - Block numbers where CurLI is live through without uses. 1570b57cec5SDimitry Andric BitVector ThroughBlocks; 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric /// NumThroughBlocks - Number of live-through blocks. 16006c3fb27SDimitry Andric unsigned NumThroughBlocks = 0u; 1610b57cec5SDimitry Andric 162*5f757f3fSDimitry Andric /// LooksLikeLoopIV - The variable defines what looks like it could be a loop 163*5f757f3fSDimitry Andric /// IV, where it defs a variable in the latch. 164*5f757f3fSDimitry Andric bool LooksLikeLoopIV = false; 165*5f757f3fSDimitry Andric 1660b57cec5SDimitry Andric // Sumarize statistics by counting instructions using CurLI. 1670b57cec5SDimitry Andric void analyzeUses(); 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric /// calcLiveBlockInfo - Compute per-block information about CurLI. 170349cc55cSDimitry Andric void calcLiveBlockInfo(); 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric public: 1730b57cec5SDimitry Andric SplitAnalysis(const VirtRegMap &vrm, const LiveIntervals &lis, 1740b57cec5SDimitry Andric const MachineLoopInfo &mli); 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric /// analyze - set CurLI to the specified interval, and analyze how it may be 1770b57cec5SDimitry Andric /// split. 1780b57cec5SDimitry Andric void analyze(const LiveInterval *li); 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric /// clear - clear all data structures so SplitAnalysis is ready to analyze a 1810b57cec5SDimitry Andric /// new interval. 1820b57cec5SDimitry Andric void clear(); 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric /// getParent - Return the last analyzed interval. getParent()1850b57cec5SDimitry Andric const LiveInterval &getParent() const { return *CurLI; } 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric /// isOriginalEndpoint - Return true if the original live range was killed or 1880b57cec5SDimitry Andric /// (re-)defined at Idx. Idx should be the 'def' slot for a normal kill/def, 1890b57cec5SDimitry Andric /// and 'use' for an early-clobber def. 1900b57cec5SDimitry Andric /// This can be used to recognize code inserted by earlier live range 1910b57cec5SDimitry Andric /// splitting. 1920b57cec5SDimitry Andric bool isOriginalEndpoint(SlotIndex Idx) const; 1930b57cec5SDimitry Andric 1940b57cec5SDimitry Andric /// getUseSlots - Return an array of SlotIndexes of instructions using CurLI. 1950b57cec5SDimitry Andric /// This include both use and def operands, at most one entry per instruction. getUseSlots()1960b57cec5SDimitry Andric ArrayRef<SlotIndex> getUseSlots() const { return UseSlots; } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric /// getUseBlocks - Return an array of BlockInfo objects for the basic blocks 1990b57cec5SDimitry Andric /// where CurLI has uses. getUseBlocks()2000b57cec5SDimitry Andric ArrayRef<BlockInfo> getUseBlocks() const { return UseBlocks; } 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric /// getNumThroughBlocks - Return the number of through blocks. getNumThroughBlocks()2030b57cec5SDimitry Andric unsigned getNumThroughBlocks() const { return NumThroughBlocks; } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric /// isThroughBlock - Return true if CurLI is live through MBB without uses. isThroughBlock(unsigned MBB)2060b57cec5SDimitry Andric bool isThroughBlock(unsigned MBB) const { return ThroughBlocks.test(MBB); } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric /// getThroughBlocks - Return the set of through blocks. getThroughBlocks()2090b57cec5SDimitry Andric const BitVector &getThroughBlocks() const { return ThroughBlocks; } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric /// getNumLiveBlocks - Return the number of blocks where CurLI is live. getNumLiveBlocks()2120b57cec5SDimitry Andric unsigned getNumLiveBlocks() const { 2130b57cec5SDimitry Andric return getUseBlocks().size() - NumGapBlocks + getNumThroughBlocks(); 2140b57cec5SDimitry Andric } 2150b57cec5SDimitry Andric looksLikeLoopIV()216*5f757f3fSDimitry Andric bool looksLikeLoopIV() const { return LooksLikeLoopIV; } 217*5f757f3fSDimitry Andric 2180b57cec5SDimitry Andric /// countLiveBlocks - Return the number of blocks where li is live. This is 2190b57cec5SDimitry Andric /// guaranteed to return the same number as getNumLiveBlocks() after calling 2200b57cec5SDimitry Andric /// analyze(li). 2210b57cec5SDimitry Andric unsigned countLiveBlocks(const LiveInterval *li) const; 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric using BlockPtrSet = SmallPtrSet<const MachineBasicBlock *, 16>; 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric /// shouldSplitSingleBlock - Returns true if it would help to create a local 2260b57cec5SDimitry Andric /// live range for the instructions in BI. There is normally no benefit to 2270b57cec5SDimitry Andric /// creating a live range for a single instruction, but it does enable 2280b57cec5SDimitry Andric /// register class inflation if the instruction has a restricted register 2290b57cec5SDimitry Andric /// class. 2300b57cec5SDimitry Andric /// 2310b57cec5SDimitry Andric /// @param BI The block to be isolated. 2320b57cec5SDimitry Andric /// @param SingleInstrs True when single instructions should be isolated. 2330b57cec5SDimitry Andric bool shouldSplitSingleBlock(const BlockInfo &BI, bool SingleInstrs) const; 2340b57cec5SDimitry Andric getLastSplitPoint(unsigned Num)2350b57cec5SDimitry Andric SlotIndex getLastSplitPoint(unsigned Num) { 2360b57cec5SDimitry Andric return IPA.getLastInsertPoint(*CurLI, *MF.getBlockNumbered(Num)); 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric getLastSplitPoint(MachineBasicBlock * BB)239fe6060f1SDimitry Andric SlotIndex getLastSplitPoint(MachineBasicBlock *BB) { 240fe6060f1SDimitry Andric return IPA.getLastInsertPoint(*CurLI, *BB); 241fe6060f1SDimitry Andric } 242fe6060f1SDimitry Andric getLastSplitPointIter(MachineBasicBlock * BB)2430b57cec5SDimitry Andric MachineBasicBlock::iterator getLastSplitPointIter(MachineBasicBlock *BB) { 2440b57cec5SDimitry Andric return IPA.getLastInsertPointIter(*CurLI, *BB); 2450b57cec5SDimitry Andric } 2460b57cec5SDimitry Andric getFirstSplitPoint(unsigned Num)2470b57cec5SDimitry Andric SlotIndex getFirstSplitPoint(unsigned Num) { 2480b57cec5SDimitry Andric return IPA.getFirstInsertPoint(*MF.getBlockNumbered(Num)); 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric }; 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric /// SplitEditor - Edit machine code and LiveIntervals for live range 2530b57cec5SDimitry Andric /// splitting. 2540b57cec5SDimitry Andric /// 2550b57cec5SDimitry Andric /// - Create a SplitEditor from a SplitAnalysis. 2560b57cec5SDimitry Andric /// - Start a new live interval with openIntv. 2570b57cec5SDimitry Andric /// - Mark the places where the new interval is entered using enterIntv* 2580b57cec5SDimitry Andric /// - Mark the ranges where the new interval is used with useIntv* 2590b57cec5SDimitry Andric /// - Mark the places where the interval is exited with exitIntv*. 2600b57cec5SDimitry Andric /// - Finish the current interval with closeIntv and repeat from 2. 2610b57cec5SDimitry Andric /// - Rewrite instructions with finish(). 2620b57cec5SDimitry Andric /// 2630b57cec5SDimitry Andric class LLVM_LIBRARY_VISIBILITY SplitEditor { 2640b57cec5SDimitry Andric SplitAnalysis &SA; 2650b57cec5SDimitry Andric LiveIntervals &LIS; 2660b57cec5SDimitry Andric VirtRegMap &VRM; 2670b57cec5SDimitry Andric MachineRegisterInfo &MRI; 2680b57cec5SDimitry Andric MachineDominatorTree &MDT; 2690b57cec5SDimitry Andric const TargetInstrInfo &TII; 2700b57cec5SDimitry Andric const TargetRegisterInfo &TRI; 2710b57cec5SDimitry Andric const MachineBlockFrequencyInfo &MBFI; 272fe6060f1SDimitry Andric VirtRegAuxInfo &VRAI; 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric public: 2750b57cec5SDimitry Andric /// ComplementSpillMode - Select how the complement live range should be 2760b57cec5SDimitry Andric /// created. SplitEditor automatically creates interval 0 to contain 2770b57cec5SDimitry Andric /// anything that isn't added to another interval. This complement interval 2780b57cec5SDimitry Andric /// can get quite complicated, and it can sometimes be an advantage to allow 2790b57cec5SDimitry Andric /// it to overlap the other intervals. If it is going to spill anyway, no 2800b57cec5SDimitry Andric /// registers are wasted by keeping a value in two places at the same time. 2810b57cec5SDimitry Andric enum ComplementSpillMode { 2820b57cec5SDimitry Andric /// SM_Partition(Default) - Try to create the complement interval so it 2830b57cec5SDimitry Andric /// doesn't overlap any other intervals, and the original interval is 2840b57cec5SDimitry Andric /// partitioned. This may require a large number of back copies and extra 2850b57cec5SDimitry Andric /// PHI-defs. Only segments marked with overlapIntv will be overlapping. 2860b57cec5SDimitry Andric SM_Partition, 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric /// SM_Size - Overlap intervals to minimize the number of inserted COPY 2890b57cec5SDimitry Andric /// instructions. Copies to the complement interval are hoisted to their 2900b57cec5SDimitry Andric /// common dominator, so only one COPY is required per value in the 2910b57cec5SDimitry Andric /// complement interval. This also means that no extra PHI-defs need to be 2920b57cec5SDimitry Andric /// inserted in the complement interval. 2930b57cec5SDimitry Andric SM_Size, 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric /// SM_Speed - Overlap intervals to minimize the expected execution 2960b57cec5SDimitry Andric /// frequency of the inserted copies. This is very similar to SM_Size, but 2970b57cec5SDimitry Andric /// the complement interval may get some extra PHI-defs. 2980b57cec5SDimitry Andric SM_Speed 2990b57cec5SDimitry Andric }; 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric private: 3020b57cec5SDimitry Andric /// Edit - The current parent register and new intervals created. 3030b57cec5SDimitry Andric LiveRangeEdit *Edit = nullptr; 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric /// Index into Edit of the currently open interval. 3060b57cec5SDimitry Andric /// The index 0 is used for the complement, so the first interval started by 3070b57cec5SDimitry Andric /// openIntv will be 1. 3080b57cec5SDimitry Andric unsigned OpenIdx = 0; 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric /// The current spill mode, selected by reset(). 3110b57cec5SDimitry Andric ComplementSpillMode SpillMode = SM_Partition; 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric using RegAssignMap = IntervalMap<SlotIndex, unsigned>; 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric /// Allocator for the interval map. This will eventually be shared with 3160b57cec5SDimitry Andric /// SlotIndexes and LiveIntervals. 3170b57cec5SDimitry Andric RegAssignMap::Allocator Allocator; 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric /// RegAssign - Map of the assigned register indexes. 3200b57cec5SDimitry Andric /// Edit.get(RegAssign.lookup(Idx)) is the register that should be live at 3210b57cec5SDimitry Andric /// Idx. 3220b57cec5SDimitry Andric RegAssignMap RegAssign; 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric using ValueForcePair = PointerIntPair<VNInfo *, 1>; 3250b57cec5SDimitry Andric using ValueMap = DenseMap<std::pair<unsigned, unsigned>, ValueForcePair>; 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric /// Values - keep track of the mapping from parent values to values in the new 3280b57cec5SDimitry Andric /// intervals. Given a pair (RegIdx, ParentVNI->id), Values contains: 3290b57cec5SDimitry Andric /// 3300b57cec5SDimitry Andric /// 1. No entry - the value is not mapped to Edit.get(RegIdx). 3310b57cec5SDimitry Andric /// 2. (Null, false) - the value is mapped to multiple values in 3320b57cec5SDimitry Andric /// Edit.get(RegIdx). Each value is represented by a minimal live range at 3330b57cec5SDimitry Andric /// its def. The full live range can be inferred exactly from the range 3340b57cec5SDimitry Andric /// of RegIdx in RegAssign. 3350b57cec5SDimitry Andric /// 3. (Null, true). As above, but the ranges in RegAssign are too large, and 3365ffd83dbSDimitry Andric /// the live range must be recomputed using ::extend(). 3370b57cec5SDimitry Andric /// 4. (VNI, false) The value is mapped to a single new value. 3380b57cec5SDimitry Andric /// The new value has no live ranges anywhere. 3390b57cec5SDimitry Andric ValueMap Values; 3400b57cec5SDimitry Andric 3415ffd83dbSDimitry Andric /// LICalc - Cache for computing live ranges and SSA update. Each instance 3420b57cec5SDimitry Andric /// can only handle non-overlapping live ranges, so use a separate 3435ffd83dbSDimitry Andric /// LiveIntervalCalc instance for the complement interval when in spill mode. 3445ffd83dbSDimitry Andric LiveIntervalCalc LICalc[2]; 3450b57cec5SDimitry Andric 3465ffd83dbSDimitry Andric /// getLICalc - Return the LICalc to use for RegIdx. In spill mode, the 3470b57cec5SDimitry Andric /// complement interval can overlap the other intervals, so it gets its own 3485ffd83dbSDimitry Andric /// LICalc instance. When not in spill mode, all intervals can share one. getLICalc(unsigned RegIdx)3495ffd83dbSDimitry Andric LiveIntervalCalc &getLICalc(unsigned RegIdx) { 3505ffd83dbSDimitry Andric return LICalc[SpillMode != SM_Partition && RegIdx != 0]; 3510b57cec5SDimitry Andric } 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric /// Add a segment to the interval LI for the value number VNI. If LI has 3540b57cec5SDimitry Andric /// subranges, corresponding segments will be added to them as well, but 3550b57cec5SDimitry Andric /// with newly created value numbers. If Original is true, dead def will 3560b57cec5SDimitry Andric /// only be added a subrange of LI if the corresponding subrange of the 3570b57cec5SDimitry Andric /// original interval has a def at this index. Otherwise, all subranges 3580b57cec5SDimitry Andric /// of LI will be updated. 3590b57cec5SDimitry Andric void addDeadDef(LiveInterval &LI, VNInfo *VNI, bool Original); 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric /// defValue - define a value in RegIdx from ParentVNI at Idx. 3620b57cec5SDimitry Andric /// Idx does not have to be ParentVNI->def, but it must be contained within 3630b57cec5SDimitry Andric /// ParentVNI's live range in ParentLI. The new value is added to the value 3640b57cec5SDimitry Andric /// map. The value being defined may either come from rematerialization 3650b57cec5SDimitry Andric /// (or an inserted copy), or it may be coming from the original interval. 3660b57cec5SDimitry Andric /// The parameter Original should be true in the latter case, otherwise 3670b57cec5SDimitry Andric /// it should be false. 3680b57cec5SDimitry Andric /// Return the new LI value. 3690b57cec5SDimitry Andric VNInfo *defValue(unsigned RegIdx, const VNInfo *ParentVNI, SlotIndex Idx, 3700b57cec5SDimitry Andric bool Original); 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric /// forceRecompute - Force the live range of ParentVNI in RegIdx to be 3730b57cec5SDimitry Andric /// recomputed by LiveRangeCalc::extend regardless of the number of defs. 3740b57cec5SDimitry Andric /// This is used for values whose live range doesn't match RegAssign exactly. 3750b57cec5SDimitry Andric /// They could have rematerialized, or back-copies may have been moved. 3760b57cec5SDimitry Andric void forceRecompute(unsigned RegIdx, const VNInfo &ParentVNI); 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric /// Calls forceRecompute() on any affected regidx and on ParentVNI 3790b57cec5SDimitry Andric /// predecessors in case of a phi definition. 3800b57cec5SDimitry Andric void forceRecomputeVNI(const VNInfo &ParentVNI); 3810b57cec5SDimitry Andric 3820b57cec5SDimitry Andric /// defFromParent - Define Reg from ParentVNI at UseIdx using either 3830b57cec5SDimitry Andric /// rematerialization or a COPY from parent. Return the new value. 38481ad6265SDimitry Andric VNInfo *defFromParent(unsigned RegIdx, const VNInfo *ParentVNI, 38581ad6265SDimitry Andric SlotIndex UseIdx, MachineBasicBlock &MBB, 3860b57cec5SDimitry Andric MachineBasicBlock::iterator I); 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric /// removeBackCopies - Remove the copy instructions that defines the values 3890b57cec5SDimitry Andric /// in the vector in the complement interval. 3900b57cec5SDimitry Andric void removeBackCopies(SmallVectorImpl<VNInfo*> &Copies); 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric /// getShallowDominator - Returns the least busy dominator of MBB that is 3930b57cec5SDimitry Andric /// also dominated by DefMBB. Busy is measured by loop depth. 3940b57cec5SDimitry Andric MachineBasicBlock *findShallowDominator(MachineBasicBlock *MBB, 3950b57cec5SDimitry Andric MachineBasicBlock *DefMBB); 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric /// Find out all the backCopies dominated by others. 3980b57cec5SDimitry Andric void computeRedundantBackCopies(DenseSet<unsigned> &NotToHoistSet, 3990b57cec5SDimitry Andric SmallVectorImpl<VNInfo *> &BackCopies); 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric /// Hoist back-copies to the complement interval. It tries to hoist all 4020b57cec5SDimitry Andric /// the back-copies to one BB if it is beneficial, or else simply remove 4030b57cec5SDimitry Andric /// redundant backcopies dominated by others. 4040b57cec5SDimitry Andric void hoistCopies(); 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric /// transferValues - Transfer values to the new ranges. 4070b57cec5SDimitry Andric /// Return true if any ranges were skipped. 4080b57cec5SDimitry Andric bool transferValues(); 4090b57cec5SDimitry Andric 4100b57cec5SDimitry Andric /// Live range @p LR corresponding to the lane Mask @p LM has a live 4110b57cec5SDimitry Andric /// PHI def at the beginning of block @p B. Extend the range @p LR of 4120b57cec5SDimitry Andric /// all predecessor values that reach this def. If @p LR is a subrange, 4130b57cec5SDimitry Andric /// the array @p Undefs is the set of all locations where it is undefined 4140b57cec5SDimitry Andric /// via <def,read-undef> in other subranges for the same register. 4155ffd83dbSDimitry Andric void extendPHIRange(MachineBasicBlock &B, LiveIntervalCalc &LIC, 4160b57cec5SDimitry Andric LiveRange &LR, LaneBitmask LM, 4170b57cec5SDimitry Andric ArrayRef<SlotIndex> Undefs); 4180b57cec5SDimitry Andric 4190b57cec5SDimitry Andric /// extendPHIKillRanges - Extend the ranges of all values killed by original 4200b57cec5SDimitry Andric /// parent PHIDefs. 4210b57cec5SDimitry Andric void extendPHIKillRanges(); 4220b57cec5SDimitry Andric 4230b57cec5SDimitry Andric /// rewriteAssigned - Rewrite all uses of Edit.getReg() to assigned registers. 4240b57cec5SDimitry Andric void rewriteAssigned(bool ExtendRanges); 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric /// deleteRematVictims - Delete defs that are dead after rematerializing. 4270b57cec5SDimitry Andric void deleteRematVictims(); 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric /// Add a copy instruction copying \p FromReg to \p ToReg before 4300b57cec5SDimitry Andric /// \p InsertBefore. This can be invoked with a \p LaneMask which may make it 4310b57cec5SDimitry Andric /// necessary to construct a sequence of copies to cover it exactly. 432e8d8bef9SDimitry Andric SlotIndex buildCopy(Register FromReg, Register ToReg, LaneBitmask LaneMask, 4330b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore, 4340b57cec5SDimitry Andric bool Late, unsigned RegIdx); 4350b57cec5SDimitry Andric 436e8d8bef9SDimitry Andric SlotIndex buildSingleSubRegCopy(Register FromReg, Register ToReg, 437*5f757f3fSDimitry Andric MachineBasicBlock &MB, 438*5f757f3fSDimitry Andric MachineBasicBlock::iterator InsertBefore, 439*5f757f3fSDimitry Andric unsigned SubIdx, LiveInterval &DestLI, 440*5f757f3fSDimitry Andric bool Late, SlotIndex Def, 441*5f757f3fSDimitry Andric const MCInstrDesc &Desc); 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric public: 4440b57cec5SDimitry Andric /// Create a new SplitEditor for editing the LiveInterval analyzed by SA. 4450b57cec5SDimitry Andric /// Newly created intervals will be appended to newIntervals. 446fcaf7f86SDimitry Andric SplitEditor(SplitAnalysis &SA, LiveIntervals &LIS, VirtRegMap &VRM, 447fcaf7f86SDimitry Andric MachineDominatorTree &MDT, MachineBlockFrequencyInfo &MBFI, 448fcaf7f86SDimitry Andric VirtRegAuxInfo &VRAI); 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric /// reset - Prepare for a new split. 4510b57cec5SDimitry Andric void reset(LiveRangeEdit&, ComplementSpillMode = SM_Partition); 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric /// Create a new virtual register and live interval. 4540b57cec5SDimitry Andric /// Return the interval index, starting from 1. Interval index 0 is the 4550b57cec5SDimitry Andric /// implicit complement interval. 4560b57cec5SDimitry Andric unsigned openIntv(); 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric /// currentIntv - Return the current interval index. currentIntv()4590b57cec5SDimitry Andric unsigned currentIntv() const { return OpenIdx; } 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric /// selectIntv - Select a previously opened interval index. 4620b57cec5SDimitry Andric void selectIntv(unsigned Idx); 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric /// enterIntvBefore - Enter the open interval before the instruction at Idx. 4650b57cec5SDimitry Andric /// If the parent interval is not live before Idx, a COPY is not inserted. 4660b57cec5SDimitry Andric /// Return the beginning of the new live range. 4670b57cec5SDimitry Andric SlotIndex enterIntvBefore(SlotIndex Idx); 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric /// enterIntvAfter - Enter the open interval after the instruction at Idx. 4700b57cec5SDimitry Andric /// Return the beginning of the new live range. 4710b57cec5SDimitry Andric SlotIndex enterIntvAfter(SlotIndex Idx); 4720b57cec5SDimitry Andric 4730b57cec5SDimitry Andric /// enterIntvAtEnd - Enter the open interval at the end of MBB. 4740b57cec5SDimitry Andric /// Use the open interval from the inserted copy to the MBB end. 4750b57cec5SDimitry Andric /// Return the beginning of the new live range. 4760b57cec5SDimitry Andric SlotIndex enterIntvAtEnd(MachineBasicBlock &MBB); 4770b57cec5SDimitry Andric 4780b57cec5SDimitry Andric /// useIntv - indicate that all instructions in MBB should use OpenLI. 4790b57cec5SDimitry Andric void useIntv(const MachineBasicBlock &MBB); 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric /// useIntv - indicate that all instructions in range should use OpenLI. 4820b57cec5SDimitry Andric void useIntv(SlotIndex Start, SlotIndex End); 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric /// leaveIntvAfter - Leave the open interval after the instruction at Idx. 4850b57cec5SDimitry Andric /// Return the end of the live range. 4860b57cec5SDimitry Andric SlotIndex leaveIntvAfter(SlotIndex Idx); 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric /// leaveIntvBefore - Leave the open interval before the instruction at Idx. 4890b57cec5SDimitry Andric /// Return the end of the live range. 4900b57cec5SDimitry Andric SlotIndex leaveIntvBefore(SlotIndex Idx); 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric /// leaveIntvAtTop - Leave the interval at the top of MBB. 4930b57cec5SDimitry Andric /// Add liveness from the MBB top to the copy. 4940b57cec5SDimitry Andric /// Return the end of the live range. 4950b57cec5SDimitry Andric SlotIndex leaveIntvAtTop(MachineBasicBlock &MBB); 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric /// overlapIntv - Indicate that all instructions in range should use the open 498fe6060f1SDimitry Andric /// interval if End does not have tied-def usage of the register and in this 499bdd1243dSDimitry Andric /// case complement interval is used. Let the complement interval be live. 5000b57cec5SDimitry Andric /// 5010b57cec5SDimitry Andric /// This doubles the register pressure, but is sometimes required to deal with 5020b57cec5SDimitry Andric /// register uses after the last valid split point. 5030b57cec5SDimitry Andric /// 5040b57cec5SDimitry Andric /// The Start index should be a return value from a leaveIntv* call, and End 5050b57cec5SDimitry Andric /// should be in the same basic block. The parent interval must have the same 5060b57cec5SDimitry Andric /// value across the range. 5070b57cec5SDimitry Andric /// 5080b57cec5SDimitry Andric void overlapIntv(SlotIndex Start, SlotIndex End); 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric /// finish - after all the new live ranges have been created, compute the 5110b57cec5SDimitry Andric /// remaining live range, and rewrite instructions to use the new registers. 5120b57cec5SDimitry Andric /// @param LRMap When not null, this vector will map each live range in Edit 5130b57cec5SDimitry Andric /// back to the indices returned by openIntv. 5140b57cec5SDimitry Andric /// There may be extra indices created by dead code elimination. 5150b57cec5SDimitry Andric void finish(SmallVectorImpl<unsigned> *LRMap = nullptr); 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric /// dump - print the current interval mapping to dbgs(). 5180b57cec5SDimitry Andric void dump() const; 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric // ===--- High level methods ---=== 5210b57cec5SDimitry Andric 5220b57cec5SDimitry Andric /// splitSingleBlock - Split CurLI into a separate live interval around the 5230b57cec5SDimitry Andric /// uses in a single block. This is intended to be used as part of a larger 5240b57cec5SDimitry Andric /// split, and doesn't call finish(). 5250b57cec5SDimitry Andric void splitSingleBlock(const SplitAnalysis::BlockInfo &BI); 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric /// splitLiveThroughBlock - Split CurLI in the given block such that it 5280b57cec5SDimitry Andric /// enters the block in IntvIn and leaves it in IntvOut. There may be uses in 5290b57cec5SDimitry Andric /// the block, but they will be ignored when placing split points. 5300b57cec5SDimitry Andric /// 5310b57cec5SDimitry Andric /// @param MBBNum Block number. 5320b57cec5SDimitry Andric /// @param IntvIn Interval index entering the block. 5330b57cec5SDimitry Andric /// @param LeaveBefore When set, leave IntvIn before this point. 5340b57cec5SDimitry Andric /// @param IntvOut Interval index leaving the block. 5350b57cec5SDimitry Andric /// @param EnterAfter When set, enter IntvOut after this point. 5360b57cec5SDimitry Andric void splitLiveThroughBlock(unsigned MBBNum, 5370b57cec5SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore, 5380b57cec5SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter); 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric /// splitRegInBlock - Split CurLI in the given block such that it enters the 5410b57cec5SDimitry Andric /// block in IntvIn and leaves it on the stack (or not at all). Split points 5420b57cec5SDimitry Andric /// are placed in a way that avoids putting uses in the stack interval. This 5430b57cec5SDimitry Andric /// may require creating a local interval when there is interference. 5440b57cec5SDimitry Andric /// 5450b57cec5SDimitry Andric /// @param BI Block descriptor. 5460b57cec5SDimitry Andric /// @param IntvIn Interval index entering the block. Not 0. 5470b57cec5SDimitry Andric /// @param LeaveBefore When set, leave IntvIn before this point. 5480b57cec5SDimitry Andric void splitRegInBlock(const SplitAnalysis::BlockInfo &BI, 5490b57cec5SDimitry Andric unsigned IntvIn, SlotIndex LeaveBefore); 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric /// splitRegOutBlock - Split CurLI in the given block such that it enters the 5520b57cec5SDimitry Andric /// block on the stack (or isn't live-in at all) and leaves it in IntvOut. 5530b57cec5SDimitry Andric /// Split points are placed to avoid interference and such that the uses are 5540b57cec5SDimitry Andric /// not in the stack interval. This may require creating a local interval 5550b57cec5SDimitry Andric /// when there is interference. 5560b57cec5SDimitry Andric /// 5570b57cec5SDimitry Andric /// @param BI Block descriptor. 5580b57cec5SDimitry Andric /// @param IntvOut Interval index leaving the block. 5590b57cec5SDimitry Andric /// @param EnterAfter When set, enter IntvOut after this point. 5600b57cec5SDimitry Andric void splitRegOutBlock(const SplitAnalysis::BlockInfo &BI, 5610b57cec5SDimitry Andric unsigned IntvOut, SlotIndex EnterAfter); 5620b57cec5SDimitry Andric }; 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric } // end namespace llvm 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric #endif // LLVM_LIB_CODEGEN_SPLITKIT_H 567