10b57cec5SDimitry Andric //===- RegAllocBase.h - basic regalloc interface and driver -----*- 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 defines the RegAllocBase class, which is the skeleton of a basic 100b57cec5SDimitry Andric // register allocation algorithm and interface for extending it. It provides the 110b57cec5SDimitry Andric // building blocks on which to construct other experimental allocators and test 120b57cec5SDimitry Andric // the validity of two principles: 130b57cec5SDimitry Andric // 140b57cec5SDimitry Andric // - If virtual and physical register liveness is modeled using intervals, then 150b57cec5SDimitry Andric // on-the-fly interference checking is cheap. Furthermore, interferences can be 160b57cec5SDimitry Andric // lazily cached and reused. 170b57cec5SDimitry Andric // 180b57cec5SDimitry Andric // - Register allocation complexity, and generated code performance is 190b57cec5SDimitry Andric // determined by the effectiveness of live range splitting rather than optimal 200b57cec5SDimitry Andric // coloring. 210b57cec5SDimitry Andric // 220b57cec5SDimitry Andric // Following the first principle, interfering checking revolves around the 230b57cec5SDimitry Andric // LiveIntervalUnion data structure. 240b57cec5SDimitry Andric // 250b57cec5SDimitry Andric // To fulfill the second principle, the basic allocator provides a driver for 260b57cec5SDimitry Andric // incremental splitting. It essentially punts on the problem of register 270b57cec5SDimitry Andric // coloring, instead driving the assignment of virtual to physical registers by 280b57cec5SDimitry Andric // the cost of splitting. The basic allocator allows for heuristic reassignment 290b57cec5SDimitry Andric // of registers, if a more sophisticated allocator chooses to do that. 300b57cec5SDimitry Andric // 310b57cec5SDimitry Andric // This framework provides a way to engineer the compile time vs. code 320b57cec5SDimitry Andric // quality trade-off without relying on a particular theoretical solver. 330b57cec5SDimitry Andric // 340b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric #ifndef LLVM_LIB_CODEGEN_REGALLOCBASE_H 370b57cec5SDimitry Andric #define LLVM_LIB_CODEGEN_REGALLOCBASE_H 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 40*0fca6ea1SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 41fe6060f1SDimitry Andric #include "llvm/CodeGen/RegAllocCommon.h" 420b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h" 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric namespace llvm { 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric class LiveInterval; 470b57cec5SDimitry Andric class LiveIntervals; 480b57cec5SDimitry Andric class LiveRegMatrix; 490b57cec5SDimitry Andric class MachineInstr; 500b57cec5SDimitry Andric class MachineRegisterInfo; 510b57cec5SDimitry Andric template<typename T> class SmallVectorImpl; 520b57cec5SDimitry Andric class Spiller; 530b57cec5SDimitry Andric class TargetRegisterInfo; 540b57cec5SDimitry Andric class VirtRegMap; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric /// RegAllocBase provides the register allocation driver and interface that can 570b57cec5SDimitry Andric /// be extended to add interesting heuristics. 580b57cec5SDimitry Andric /// 590b57cec5SDimitry Andric /// Register allocators must override the selectOrSplit() method to implement 600b57cec5SDimitry Andric /// live range splitting. They must also override enqueue/dequeue to provide an 610b57cec5SDimitry Andric /// assignment order. 620b57cec5SDimitry Andric class RegAllocBase { 630b57cec5SDimitry Andric virtual void anchor(); 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric protected: 660b57cec5SDimitry Andric const TargetRegisterInfo *TRI = nullptr; 670b57cec5SDimitry Andric MachineRegisterInfo *MRI = nullptr; 680b57cec5SDimitry Andric VirtRegMap *VRM = nullptr; 690b57cec5SDimitry Andric LiveIntervals *LIS = nullptr; 700b57cec5SDimitry Andric LiveRegMatrix *Matrix = nullptr; 710b57cec5SDimitry Andric RegisterClassInfo RegClassInfo; 720b57cec5SDimitry Andric 73*0fca6ea1SDimitry Andric private: 74*0fca6ea1SDimitry Andric /// Private, callees should go through shouldAllocateRegister 75*0fca6ea1SDimitry Andric const RegAllocFilterFunc shouldAllocateRegisterImpl; 76*0fca6ea1SDimitry Andric 77*0fca6ea1SDimitry Andric protected: 780b57cec5SDimitry Andric /// Inst which is a def of an original reg and whose defs are already all 790b57cec5SDimitry Andric /// dead after remat is saved in DeadRemats. The deletion of such inst is 800b57cec5SDimitry Andric /// postponed till all the allocations are done, so its remat expr is 810b57cec5SDimitry Andric /// always available for the remat of all the siblings of the original reg. 820b57cec5SDimitry Andric SmallPtrSet<MachineInstr *, 32> DeadRemats; 830b57cec5SDimitry Andric 84*0fca6ea1SDimitry Andric RegAllocBase(const RegAllocFilterFunc F = nullptr) 85*0fca6ea1SDimitry Andric : shouldAllocateRegisterImpl(F) {} 86fe6060f1SDimitry Andric 870b57cec5SDimitry Andric virtual ~RegAllocBase() = default; 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric // A RegAlloc pass should call this before allocatePhysRegs. 900b57cec5SDimitry Andric void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat); 910b57cec5SDimitry Andric 92*0fca6ea1SDimitry Andric /// Get whether a given register should be allocated 93*0fca6ea1SDimitry Andric bool shouldAllocateRegister(Register Reg) { 94*0fca6ea1SDimitry Andric if (!shouldAllocateRegisterImpl) 95*0fca6ea1SDimitry Andric return true; 96*0fca6ea1SDimitry Andric return shouldAllocateRegisterImpl(*TRI, *MRI, Reg); 97*0fca6ea1SDimitry Andric } 98*0fca6ea1SDimitry Andric 990b57cec5SDimitry Andric // The top-level driver. The output is a VirtRegMap that us updated with 1000b57cec5SDimitry Andric // physical register assignments. 1010b57cec5SDimitry Andric void allocatePhysRegs(); 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric // Include spiller post optimization and removing dead defs left because of 1040b57cec5SDimitry Andric // rematerialization. 1050b57cec5SDimitry Andric virtual void postOptimization(); 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric // Get a temporary reference to a Spiller instance. 1080b57cec5SDimitry Andric virtual Spiller &spiller() = 0; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric /// enqueue - Add VirtReg to the priority queue of unassigned registers. 11181ad6265SDimitry Andric virtual void enqueueImpl(const LiveInterval *LI) = 0; 112fe6060f1SDimitry Andric 113fe6060f1SDimitry Andric /// enqueue - Add VirtReg to the priority queue of unassigned registers. 11481ad6265SDimitry Andric void enqueue(const LiveInterval *LI); 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric /// dequeue - Return the next unassigned register, or NULL. 11781ad6265SDimitry Andric virtual const LiveInterval *dequeue() = 0; 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric // A RegAlloc pass should override this to provide the allocation heuristics. 1200b57cec5SDimitry Andric // Each call must guarantee forward progess by returning an available PhysReg 1210b57cec5SDimitry Andric // or new set of split live virtual registers. It is up to the splitter to 1220b57cec5SDimitry Andric // converge quickly toward fully spilled live ranges. 12381ad6265SDimitry Andric virtual MCRegister selectOrSplit(const LiveInterval &VirtReg, 1245ffd83dbSDimitry Andric SmallVectorImpl<Register> &splitLVRs) = 0; 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric // Use this group name for NamedRegionTimer. 1270b57cec5SDimitry Andric static const char TimerGroupName[]; 1280b57cec5SDimitry Andric static const char TimerGroupDescription[]; 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric /// Method called when the allocator is about to remove a LiveInterval. 13181ad6265SDimitry Andric virtual void aboutToRemoveInterval(const LiveInterval &LI) {} 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric public: 1340b57cec5SDimitry Andric /// VerifyEnabled - True when -verify-regalloc is given. 1350b57cec5SDimitry Andric static bool VerifyEnabled; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric private: 1380b57cec5SDimitry Andric void seedLiveRegs(); 1390b57cec5SDimitry Andric }; 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric } // end namespace llvm 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric #endif // LLVM_LIB_CODEGEN_REGALLOCBASE_H 144