Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

394 lines
15 KiB

  1. //===-- LiveIntervalAnalysis.h - Live Interval Analysis ---------*- C++ -*-===//
  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 file implements the LiveInterval analysis pass. Given some numbering of
  11. // each the machine instructions (in this implemention depth-first order) an
  12. // interval [i, j) is said to be a live interval for register v if there is no
  13. // instruction with number j' > j such that v is live at j' and there is no
  14. // instruction with number i' < i such that v is live at i'. In this
  15. // implementation intervals can have holes, i.e. an interval might look like
  16. // [1,20), [50,65), [1000,1001).
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
  20. #define LLVM_CODEGEN_LIVEINTERVAL_ANALYSIS_H
  21. #include "llvm/ADT/IndexedMap.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/CodeGen/LiveInterval.h"
  24. #include "llvm/CodeGen/MachineBasicBlock.h"
  25. #include "llvm/CodeGen/MachineFunctionPass.h"
  26. #include "llvm/CodeGen/SlotIndexes.h"
  27. #include "llvm/Support/Allocator.h"
  28. #include "llvm/Target/TargetRegisterInfo.h"
  29. #include <cmath>
  30. #include <iterator>
  31. namespace llvm {
  32. class AliasAnalysis;
  33. class BitVector;
  34. class LiveRangeCalc;
  35. class LiveVariables;
  36. class MachineDominatorTree;
  37. class MachineLoopInfo;
  38. class TargetRegisterInfo;
  39. class MachineRegisterInfo;
  40. class TargetInstrInfo;
  41. class TargetRegisterClass;
  42. class VirtRegMap;
  43. class LiveIntervals : public MachineFunctionPass {
  44. MachineFunction* MF;
  45. MachineRegisterInfo* MRI;
  46. const TargetMachine* TM;
  47. const TargetRegisterInfo* TRI;
  48. const TargetInstrInfo* TII;
  49. AliasAnalysis *AA;
  50. SlotIndexes* Indexes;
  51. MachineDominatorTree *DomTree;
  52. LiveRangeCalc *LRCalc;
  53. /// Special pool allocator for VNInfo's (LiveInterval val#).
  54. ///
  55. VNInfo::Allocator VNInfoAllocator;
  56. /// Live interval pointers for all the virtual registers.
  57. IndexedMap<LiveInterval*, VirtReg2IndexFunctor> VirtRegIntervals;
  58. /// RegMaskSlots - Sorted list of instructions with register mask operands.
  59. /// Always use the 'r' slot, RegMasks are normal clobbers, not early
  60. /// clobbers.
  61. SmallVector<SlotIndex, 8> RegMaskSlots;
  62. /// RegMaskBits - This vector is parallel to RegMaskSlots, it holds a
  63. /// pointer to the corresponding register mask. This pointer can be
  64. /// recomputed as:
  65. ///
  66. /// MI = Indexes->getInstructionFromIndex(RegMaskSlot[N]);
  67. /// unsigned OpNum = findRegMaskOperand(MI);
  68. /// RegMaskBits[N] = MI->getOperand(OpNum).getRegMask();
  69. ///
  70. /// This is kept in a separate vector partly because some standard
  71. /// libraries don't support lower_bound() with mixed objects, partly to
  72. /// improve locality when searching in RegMaskSlots.
  73. /// Also see the comment in LiveInterval::find().
  74. SmallVector<const uint32_t*, 8> RegMaskBits;
  75. /// For each basic block number, keep (begin, size) pairs indexing into the
  76. /// RegMaskSlots and RegMaskBits arrays.
  77. /// Note that basic block numbers may not be layout contiguous, that's why
  78. /// we can't just keep track of the first register mask in each basic
  79. /// block.
  80. SmallVector<std::pair<unsigned, unsigned>, 8> RegMaskBlocks;
  81. /// RegUnitIntervals - Keep a live interval for each register unit as a way
  82. /// of tracking fixed physreg interference.
  83. SmallVector<LiveInterval*, 0> RegUnitIntervals;
  84. public:
  85. static char ID; // Pass identification, replacement for typeid
  86. LiveIntervals();
  87. virtual ~LiveIntervals();
  88. // Calculate the spill weight to assign to a single instruction.
  89. static float getSpillWeight(bool isDef, bool isUse, unsigned loopDepth);
  90. LiveInterval &getInterval(unsigned Reg) {
  91. LiveInterval *LI = VirtRegIntervals[Reg];
  92. assert(LI && "Interval does not exist for virtual register");
  93. return *LI;
  94. }
  95. const LiveInterval &getInterval(unsigned Reg) const {
  96. return const_cast<LiveIntervals*>(this)->getInterval(Reg);
  97. }
  98. bool hasInterval(unsigned Reg) const {
  99. return VirtRegIntervals.inBounds(Reg) && VirtRegIntervals[Reg];
  100. }
  101. // Interval creation.
  102. LiveInterval &getOrCreateInterval(unsigned Reg) {
  103. if (!hasInterval(Reg)) {
  104. VirtRegIntervals.grow(Reg);
  105. VirtRegIntervals[Reg] = createInterval(Reg);
  106. }
  107. return getInterval(Reg);
  108. }
  109. // Interval removal.
  110. void removeInterval(unsigned Reg) {
  111. delete VirtRegIntervals[Reg];
  112. VirtRegIntervals[Reg] = 0;
  113. }
  114. /// addLiveRangeToEndOfBlock - Given a register and an instruction,
  115. /// adds a live range from that instruction to the end of its MBB.
  116. LiveRange addLiveRangeToEndOfBlock(unsigned reg,
  117. MachineInstr* startInst);
  118. /// shrinkToUses - After removing some uses of a register, shrink its live
  119. /// range to just the remaining uses. This method does not compute reaching
  120. /// defs for new uses, and it doesn't remove dead defs.
  121. /// Dead PHIDef values are marked as unused.
  122. /// New dead machine instructions are added to the dead vector.
  123. /// Return true if the interval may have been separated into multiple
  124. /// connected components.
  125. bool shrinkToUses(LiveInterval *li,
  126. SmallVectorImpl<MachineInstr*> *dead = 0);
  127. /// extendToIndices - Extend the live range of LI to reach all points in
  128. /// Indices. The points in the Indices array must be jointly dominated by
  129. /// existing defs in LI. PHI-defs are added as needed to maintain SSA form.
  130. ///
  131. /// If a SlotIndex in Indices is the end index of a basic block, LI will be
  132. /// extended to be live out of the basic block.
  133. ///
  134. /// See also LiveRangeCalc::extend().
  135. void extendToIndices(LiveInterval *LI, ArrayRef<SlotIndex> Indices);
  136. /// pruneValue - If an LI value is live at Kill, prune its live range by
  137. /// removing any liveness reachable from Kill. Add live range end points to
  138. /// EndPoints such that extendToIndices(LI, EndPoints) will reconstruct the
  139. /// value's live range.
  140. ///
  141. /// Calling pruneValue() and extendToIndices() can be used to reconstruct
  142. /// SSA form after adding defs to a virtual register.
  143. void pruneValue(LiveInterval *LI, SlotIndex Kill,
  144. SmallVectorImpl<SlotIndex> *EndPoints);
  145. SlotIndexes *getSlotIndexes() const {
  146. return Indexes;
  147. }
  148. AliasAnalysis *getAliasAnalysis() const {
  149. return AA;
  150. }
  151. /// isNotInMIMap - returns true if the specified machine instr has been
  152. /// removed or was never entered in the map.
  153. bool isNotInMIMap(const MachineInstr* Instr) const {
  154. return !Indexes->hasIndex(Instr);
  155. }
  156. /// Returns the base index of the given instruction.
  157. SlotIndex getInstructionIndex(const MachineInstr *instr) const {
  158. return Indexes->getInstructionIndex(instr);
  159. }
  160. /// Returns the instruction associated with the given index.
  161. MachineInstr* getInstructionFromIndex(SlotIndex index) const {
  162. return Indexes->getInstructionFromIndex(index);
  163. }
  164. /// Return the first index in the given basic block.
  165. SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
  166. return Indexes->getMBBStartIdx(mbb);
  167. }
  168. /// Return the last index in the given basic block.
  169. SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
  170. return Indexes->getMBBEndIdx(mbb);
  171. }
  172. bool isLiveInToMBB(const LiveInterval &li,
  173. const MachineBasicBlock *mbb) const {
  174. return li.liveAt(getMBBStartIdx(mbb));
  175. }
  176. bool isLiveOutOfMBB(const LiveInterval &li,
  177. const MachineBasicBlock *mbb) const {
  178. return li.liveAt(getMBBEndIdx(mbb).getPrevSlot());
  179. }
  180. MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
  181. return Indexes->getMBBFromIndex(index);
  182. }
  183. void insertMBBInMaps(MachineBasicBlock *MBB) {
  184. Indexes->insertMBBInMaps(MBB);
  185. assert(unsigned(MBB->getNumber()) == RegMaskBlocks.size() &&
  186. "Blocks must be added in order.");
  187. RegMaskBlocks.push_back(std::make_pair(RegMaskSlots.size(), 0));
  188. }
  189. SlotIndex InsertMachineInstrInMaps(MachineInstr *MI) {
  190. return Indexes->insertMachineInstrInMaps(MI);
  191. }
  192. void RemoveMachineInstrFromMaps(MachineInstr *MI) {
  193. Indexes->removeMachineInstrFromMaps(MI);
  194. }
  195. void ReplaceMachineInstrInMaps(MachineInstr *MI, MachineInstr *NewMI) {
  196. Indexes->replaceMachineInstrInMaps(MI, NewMI);
  197. }
  198. bool findLiveInMBBs(SlotIndex Start, SlotIndex End,
  199. SmallVectorImpl<MachineBasicBlock*> &MBBs) const {
  200. return Indexes->findLiveInMBBs(Start, End, MBBs);
  201. }
  202. VNInfo::Allocator& getVNInfoAllocator() { return VNInfoAllocator; }
  203. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  204. virtual void releaseMemory();
  205. /// runOnMachineFunction - pass entry point
  206. virtual bool runOnMachineFunction(MachineFunction&);
  207. /// print - Implement the dump method.
  208. virtual void print(raw_ostream &O, const Module* = 0) const;
  209. /// intervalIsInOneMBB - If LI is confined to a single basic block, return
  210. /// a pointer to that block. If LI is live in to or out of any block,
  211. /// return NULL.
  212. MachineBasicBlock *intervalIsInOneMBB(const LiveInterval &LI) const;
  213. /// Returns true if VNI is killed by any PHI-def values in LI.
  214. /// This may conservatively return true to avoid expensive computations.
  215. bool hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const;
  216. /// addKillFlags - Add kill flags to any instruction that kills a virtual
  217. /// register.
  218. void addKillFlags(const VirtRegMap*);
  219. /// handleMove - call this method to notify LiveIntervals that
  220. /// instruction 'mi' has been moved within a basic block. This will update
  221. /// the live intervals for all operands of mi. Moves between basic blocks
  222. /// are not supported.
  223. ///
  224. /// \param UpdateFlags Update live intervals for nonallocatable physregs.
  225. void handleMove(MachineInstr* MI, bool UpdateFlags = false);
  226. /// moveIntoBundle - Update intervals for operands of MI so that they
  227. /// begin/end on the SlotIndex for BundleStart.
  228. ///
  229. /// \param UpdateFlags Update live intervals for nonallocatable physregs.
  230. ///
  231. /// Requires MI and BundleStart to have SlotIndexes, and assumes
  232. /// existing liveness is accurate. BundleStart should be the first
  233. /// instruction in the Bundle.
  234. void handleMoveIntoBundle(MachineInstr* MI, MachineInstr* BundleStart,
  235. bool UpdateFlags = false);
  236. /// repairIntervalsInRange - Update live intervals for instructions in a
  237. /// range of iterators. It is intended for use after target hooks that may
  238. /// insert or remove instructions, and is only efficient for a small number
  239. /// of instructions.
  240. ///
  241. /// OrigRegs is a vector of registers that were originally used by the
  242. /// instructions in the range between the two iterators.
  243. ///
  244. /// Currently, the only only changes that are supported are simple removal
  245. /// and addition of uses.
  246. void repairIntervalsInRange(MachineBasicBlock *MBB,
  247. MachineBasicBlock::iterator Begin,
  248. MachineBasicBlock::iterator End,
  249. ArrayRef<unsigned> OrigRegs);
  250. // Register mask functions.
  251. //
  252. // Machine instructions may use a register mask operand to indicate that a
  253. // large number of registers are clobbered by the instruction. This is
  254. // typically used for calls.
  255. //
  256. // For compile time performance reasons, these clobbers are not recorded in
  257. // the live intervals for individual physical registers. Instead,
  258. // LiveIntervalAnalysis maintains a sorted list of instructions with
  259. // register mask operands.
  260. /// getRegMaskSlots - Returns a sorted array of slot indices of all
  261. /// instructions with register mask operands.
  262. ArrayRef<SlotIndex> getRegMaskSlots() const { return RegMaskSlots; }
  263. /// getRegMaskSlotsInBlock - Returns a sorted array of slot indices of all
  264. /// instructions with register mask operands in the basic block numbered
  265. /// MBBNum.
  266. ArrayRef<SlotIndex> getRegMaskSlotsInBlock(unsigned MBBNum) const {
  267. std::pair<unsigned, unsigned> P = RegMaskBlocks[MBBNum];
  268. return getRegMaskSlots().slice(P.first, P.second);
  269. }
  270. /// getRegMaskBits() - Returns an array of register mask pointers
  271. /// corresponding to getRegMaskSlots().
  272. ArrayRef<const uint32_t*> getRegMaskBits() const { return RegMaskBits; }
  273. /// getRegMaskBitsInBlock - Returns an array of mask pointers corresponding
  274. /// to getRegMaskSlotsInBlock(MBBNum).
  275. ArrayRef<const uint32_t*> getRegMaskBitsInBlock(unsigned MBBNum) const {
  276. std::pair<unsigned, unsigned> P = RegMaskBlocks[MBBNum];
  277. return getRegMaskBits().slice(P.first, P.second);
  278. }
  279. /// checkRegMaskInterference - Test if LI is live across any register mask
  280. /// instructions, and compute a bit mask of physical registers that are not
  281. /// clobbered by any of them.
  282. ///
  283. /// Returns false if LI doesn't cross any register mask instructions. In
  284. /// that case, the bit vector is not filled in.
  285. bool checkRegMaskInterference(LiveInterval &LI,
  286. BitVector &UsableRegs);
  287. // Register unit functions.
  288. //
  289. // Fixed interference occurs when MachineInstrs use physregs directly
  290. // instead of virtual registers. This typically happens when passing
  291. // arguments to a function call, or when instructions require operands in
  292. // fixed registers.
  293. //
  294. // Each physreg has one or more register units, see MCRegisterInfo. We
  295. // track liveness per register unit to handle aliasing registers more
  296. // efficiently.
  297. /// getRegUnit - Return the live range for Unit.
  298. /// It will be computed if it doesn't exist.
  299. LiveInterval &getRegUnit(unsigned Unit) {
  300. LiveInterval *LI = RegUnitIntervals[Unit];
  301. if (!LI) {
  302. // Compute missing ranges on demand.
  303. RegUnitIntervals[Unit] = LI = new LiveInterval(Unit, HUGE_VALF);
  304. computeRegUnitInterval(LI);
  305. }
  306. return *LI;
  307. }
  308. /// getCachedRegUnit - Return the live range for Unit if it has already
  309. /// been computed, or NULL if it hasn't been computed yet.
  310. LiveInterval *getCachedRegUnit(unsigned Unit) {
  311. return RegUnitIntervals[Unit];
  312. }
  313. const LiveInterval *getCachedRegUnit(unsigned Unit) const {
  314. return RegUnitIntervals[Unit];
  315. }
  316. private:
  317. /// Compute live intervals for all virtual registers.
  318. void computeVirtRegs();
  319. /// Compute RegMaskSlots and RegMaskBits.
  320. void computeRegMasks();
  321. static LiveInterval* createInterval(unsigned Reg);
  322. void printInstrs(raw_ostream &O) const;
  323. void dumpInstrs() const;
  324. void computeLiveInRegUnits();
  325. void computeRegUnitInterval(LiveInterval*);
  326. void computeVirtRegInterval(LiveInterval*);
  327. class HMEditor;
  328. };
  329. } // End llvm namespace
  330. #endif