LLVM 23.0.0git
RegAllocBasic.h
Go to the documentation of this file.
1//===-- RegAllocBasic.h - Basic Register Allocator Header -----------------===//
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/// \file
10/// This file declares the RABasic class, which provides a minimal
11/// implementation of the basic register allocator.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CODEGEN_REGALLOCBASIC_H
16#define LLVM_CODEGEN_REGALLOCBASIC_H
17
18#include "RegAllocBase.h"
22#include <queue>
23#include <tuple>
24
25namespace llvm {
26
28 bool operator()(const LiveInterval *A, const LiveInterval *B) const {
29 // Compare by weight first, then use register number as a stable tie-breaker
30 // to ensure deterministic ordering when the weights are equal.
31 return std::tuple(A->weight(), A->reg()) <
32 std::tuple(B->weight(), B->reg());
33 }
34};
35
36/// RABasic provides a minimal implementation of the basic register allocation
37/// algorithm. It prioritizes live virtual registers by spill weight and spills
38/// whenever a register is unavailable. This is not practical in production but
39/// provides a useful baseline both for measuring other allocators and comparing
40/// the speed of the basic algorithm against other styles of allocators.
42 public RegAllocBase,
44 // context
45 MachineFunction *MF = nullptr;
46
47 // state
48 std::unique_ptr<Spiller> SpillerInstance;
49 std::priority_queue<const LiveInterval *, std::vector<const LiveInterval *>,
51 Queue;
52
53 // Scratch space. Allocated here to avoid repeated malloc calls in
54 // selectOrSplit().
55 BitVector UsableRegs;
56
57 bool LRE_CanEraseVirtReg(Register) override;
58 void LRE_WillShrinkVirtReg(Register) override;
59
60public:
61 RABasic(const RegAllocFilterFunc F = nullptr);
62
63 /// Return the pass name.
64 StringRef getPassName() const override { return "Basic Register Allocator"; }
65
66 /// RABasic analysis usage.
67 void getAnalysisUsage(AnalysisUsage &AU) const override;
68
69 void releaseMemory() override;
70
71 Spiller &spiller() override { return *SpillerInstance; }
72
73 void enqueueImpl(const LiveInterval *LI) override { Queue.push(LI); }
74
75 const LiveInterval *dequeue() override {
76 if (Queue.empty())
77 return nullptr;
78 const LiveInterval *LI = Queue.top();
79 Queue.pop();
80 return LI;
81 }
82
83 MCRegister selectOrSplit(const LiveInterval &VirtReg,
84 SmallVectorImpl<Register> &SplitVRegs) override;
85
86 /// Perform register allocation.
87 bool runOnMachineFunction(MachineFunction &mf) override;
88
93
98
99 // Helper for spilling all live virtual registers currently unified under preg
100 // that interfere with the most recently queried lvr. Return true if spilling
101 // was successful, and append any new spilled/split intervals to splitLVRs.
102 bool spillInterferences(const LiveInterval &VirtReg, MCRegister PhysReg,
103 SmallVectorImpl<Register> &SplitVRegs);
104
105 static char ID;
106};
107} // namespace llvm
108#endif
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_LIBRARY_VISIBILITY
Definition Compiler.h:137
#define F(x, y, z)
Definition MD5.cpp:54
Represent the analysis usage information of a pass.
LiveInterval - This class represents the liveness of a register, or stack slot.
Callback methods for LiveRangeEdit owners.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
const LiveInterval * dequeue() override
dequeue - Return the next unassigned register, or NULL.
Spiller & spiller() override
RABasic(const RegAllocFilterFunc F=nullptr)
void enqueueImpl(const LiveInterval *LI) override
enqueue - Add VirtReg to the priority queue of unassigned registers.
MachineFunctionProperties getClearedProperties() const override
static char ID
MachineFunctionProperties getRequiredProperties() const override
StringRef getPassName() const override
Return the pass name.
RegAllocBase(const RegAllocFilterFunc F=nullptr)
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Spiller interface.
Definition Spiller.h:33
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
std::function< bool(const TargetRegisterInfo &TRI, const MachineRegisterInfo &MRI, const Register Reg)> RegAllocFilterFunc
Filter function for register classes during regalloc.
bool operator()(const LiveInterval *A, const LiveInterval *B) const