| 1 | //===-- HexagonGlobalRegion.h - VLIW global scheduling infrastructure -----===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Basic infrastructure for global scheduling. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef HEXAGON_GLOBAL_REGION_H |
| 14 | #define HEXAGON_GLOBAL_REGION_H |
| 15 | |
| 16 | #include "llvm/ADT/BitVector.h" |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/CodeGen/TargetInstrInfo.h" |
| 19 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Target/TargetMachine.h" |
| 22 | |
| 23 | #include <map> |
| 24 | #include <memory> |
| 25 | #include <vector> |
| 26 | |
| 27 | namespace llvm { |
| 28 | /// Class to track incremental liveness update. |
| 29 | class LivenessInfo { |
| 30 | const TargetInstrInfo *TII; |
| 31 | const TargetRegisterInfo *TRI; |
| 32 | BitVector LiveIns; |
| 33 | BitVector LiveOuts; |
| 34 | |
| 35 | public: |
| 36 | LivenessInfo(const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, |
| 37 | MachineBasicBlock *MBB); |
| 38 | ~LivenessInfo() {} |
| 39 | void parseOperands(MachineInstr *MI, BitVector &Gen, BitVector &Kill, |
| 40 | BitVector &Use); |
| 41 | void parseOperandsWithReset(MachineInstr *MI, BitVector &Gen, BitVector &Kill, |
| 42 | BitVector &Use); |
| 43 | void setUsed(BitVector &Set, unsigned Reg); |
| 44 | // Update Liveness for BB. |
| 45 | void UpdateLiveness(MachineBasicBlock *MBB); |
| 46 | void dump(); |
| 47 | }; |
| 48 | |
| 49 | /// Generic sequence of BBs. A trace or SB. |
| 50 | /// Maintains its own liveness info. |
| 51 | class BasicBlockRegion { |
| 52 | const TargetInstrInfo *TII; |
| 53 | const TargetRegisterInfo *TRI; |
| 54 | // Sequence of BBs in a larger block. |
| 55 | std::vector<MachineBasicBlock *> Elements; |
| 56 | std::map<MachineBasicBlock *, std::unique_ptr<LivenessInfo>> LiveInfo; |
| 57 | llvm::DenseMap<MachineBasicBlock *, unsigned> ElementIndex; |
| 58 | |
| 59 | public: |
| 60 | BasicBlockRegion(const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, |
| 61 | MachineBasicBlock *MBB); |
| 62 | ~BasicBlockRegion(); |
| 63 | |
| 64 | void addBBtoRegion(MachineBasicBlock *MBB); |
| 65 | |
| 66 | MachineBasicBlock *getEntryBB() { return Elements.front(); } |
| 67 | |
| 68 | MachineBasicBlock *findMBB(MachineBasicBlock *MBB) { |
| 69 | return ElementIndex.find(Val: MBB) != ElementIndex.end() ? MBB : nullptr; |
| 70 | } |
| 71 | |
| 72 | void RemoveBBFromRegion(MachineBasicBlock *MBB) { |
| 73 | auto It = ElementIndex.find(Val: MBB); |
| 74 | if (It == ElementIndex.end()) |
| 75 | return; |
| 76 | unsigned Index = It->second; |
| 77 | Elements.erase(position: Elements.begin() + Index); |
| 78 | ElementIndex.erase(I: It); |
| 79 | LiveInfo.erase(x: MBB); |
| 80 | for (unsigned I = Index, E = static_cast<unsigned>(Elements.size()); I != E; |
| 81 | ++I) |
| 82 | ElementIndex[Elements[I]] = I; |
| 83 | } |
| 84 | |
| 85 | MachineBasicBlock *findNextMBB(MachineBasicBlock *MBB) { |
| 86 | auto It = ElementIndex.find(Val: MBB); |
| 87 | if (It == ElementIndex.end()) |
| 88 | return nullptr; |
| 89 | unsigned Next = It->second + 1; |
| 90 | if (Next >= Elements.size()) |
| 91 | return nullptr; |
| 92 | return Elements[Next]; |
| 93 | } |
| 94 | |
| 95 | unsigned size() { return static_cast<unsigned>(Elements.size()); } |
| 96 | |
| 97 | std::vector<MachineBasicBlock *>::iterator getRootMBB() { |
| 98 | return Elements.begin(); |
| 99 | } |
| 100 | |
| 101 | std::vector<MachineBasicBlock *>::iterator getLastMBB() { |
| 102 | return Elements.end(); |
| 103 | } |
| 104 | |
| 105 | LivenessInfo *getLivenessInfoForBB(MachineBasicBlock *MBB); |
| 106 | }; |
| 107 | } // namespace llvm |
| 108 | |
| 109 | #endif |
| 110 | |