1 //===-- RegisterCoalescer.h - Register Coalescing Interface ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the abstract interface for register coalescers,
11 // allowing them to interact with and query register allocators.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/System/IncludeFile.h"
16 #include "llvm/CodeGen/LiveInterval.h"
17 #include "llvm/ADT/SmallPtrSet.h"
19 #ifndef LLVM_CODEGEN_REGISTER_COALESCER_H
20 #define LLVM_CODEGEN_REGISTER_COALESCER_H
24 class MachineFunction;
28 class TargetRegisterInfo;
29 class TargetRegisterClass;
30 class TargetInstrInfo;
32 /// An abstract interface for register coalescers. Coalescers must
33 /// implement this interface to be part of the coalescer analysis
35 class RegisterCoalescer {
37 static char ID; // Class identification, replacement for typeinfo
38 RegisterCoalescer() {}
39 virtual ~RegisterCoalescer(); // We want to be subclassed
41 /// Run the coalescer on this function, providing interference
42 /// data to query. Return whether we removed any copies.
43 virtual bool coalesceFunction(MachineFunction &mf,
44 RegallocQuery &ifd) = 0;
46 /// Reset state. Can be used to allow a coalescer run by
47 /// PassManager to be run again by the register allocator.
48 virtual void reset(MachineFunction &mf) {}
50 /// Register allocators must call this from their own
51 /// getAnalysisUsage to cover the case where the coalescer is not
52 /// a Pass in the proper sense and isn't managed by PassManager.
53 /// PassManager needs to know which analyses to make available and
54 /// which to invalidate when running the register allocator or any
55 /// pass that might call coalescing. The long-term solution is to
56 /// allow hierarchies of PassManagers.
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {}
60 /// An abstract interface for register allocators to interact with
65 /// This is simply an example of how to use the RegallocQuery
66 /// interface. It is not meant to be used in production.
68 /// class LinearScanRegallocQuery : public RegallocQuery {
70 /// const LiveIntervals \&li;
73 /// LinearScanRegallocQuery(LiveIntervals &intervals)
74 /// : li(intervals) {}
76 /// /// This is pretty slow and conservative, but since linear scan
77 /// /// allocation doesn't pre-compute interference information it's
78 /// /// the best we can do. Coalescers are always free to ignore this
79 /// /// and implement their own discovery strategy. See
80 /// /// SimpleRegisterCoalescing for an example.
81 /// void getInterferences(IntervalSet &interferences,
82 /// const LiveInterval &a) const {
83 /// for(LiveIntervals::const_iterator iv = li.begin(),
87 /// if (interfere(a, iv->second)) {
88 /// interferences.insert(&iv->second);
93 /// /// This is *really* slow and stupid. See above.
94 /// int getNumberOfInterferences(const LiveInterval &a) const {
95 /// IntervalSet intervals;
96 /// getInterferences(intervals, a);
97 /// return intervals.size();
101 /// In the allocator:
103 /// RegisterCoalescer &coalescer = getAnalysis<RegisterCoalescer>();
105 /// // We don't reset the coalescer so if it's already been run this
106 /// // takes almost no time.
107 /// LinearScanRegallocQuery ifd(*li_);
108 /// coalescer.coalesceFunction(fn, ifd);
110 class RegallocQuery {
112 typedef SmallPtrSet<const LiveInterval *, 8> IntervalSet;
114 virtual ~RegallocQuery() {}
116 /// Return whether two live ranges interfere.
117 virtual bool interfere(const LiveInterval &a,
118 const LiveInterval &b) const {
120 return a.overlaps(b);
123 /// Return the set of intervals that interfere with this one.
124 virtual void getInterferences(IntervalSet &interferences,
125 const LiveInterval &a) const = 0;
127 /// This can often be cheaper than actually returning the
129 virtual int getNumberOfInterferences(const LiveInterval &a) const = 0;
131 /// Make any data structure updates necessary to reflect
132 /// coalescing or other modifications.
133 virtual void updateDataForMerge(const LiveInterval &a,
134 const LiveInterval &b,
135 const MachineInstr ©) {}
137 /// Allow the register allocator to communicate when it doesn't
138 /// want a copy coalesced. This may be due to assumptions made by
139 /// the allocator about various invariants and so this question is
140 /// a matter of legality, not performance. Performance decisions
141 /// about which copies to coalesce should be made by the
143 virtual bool isLegalToCoalesce(const MachineInstr &inst) const {
149 /// CoalescerPair - A helper class for register coalescers. When deciding if
150 /// two registers can be coalesced, CoalescerPair can determine if a copy
151 /// instruction would become an identity copy after coalescing.
152 class CoalescerPair {
153 const TargetInstrInfo &tii_;
154 const TargetRegisterInfo &tri_;
156 /// dstReg_ - The register that will be left after coalescing. It can be a
157 /// virtual or physical register.
160 /// srcReg_ - the virtual register that will be coalesced into dstReg.
163 /// subReg_ - The subregister index of srcReg in dstReg_. It is possible the
164 /// coalesce srcReg_ into a subreg of the larger dstReg_ when dstReg_ is a
165 /// virtual register.
168 /// partial_ - True when the original copy was a partial subregister copy.
171 /// crossClass_ - True when both regs are virtual, and newRC is constrained.
174 /// flipped_ - True when DstReg and SrcReg are reversed from the oriignal copy
178 /// newRC_ - The register class of the coalesced register, or NULL if dstReg_
180 const TargetRegisterClass *newRC_;
182 /// compose - Compose subreg indices a and b, either may be 0.
183 unsigned compose(unsigned, unsigned) const;
185 /// isMoveInstr - Return true if MI is a move or subreg instruction.
186 bool isMoveInstr(const MachineInstr *MI, unsigned &Src, unsigned &Dst,
187 unsigned &SrcSub, unsigned &DstSub) const;
190 CoalescerPair(const TargetInstrInfo &tii, const TargetRegisterInfo &tri)
191 : tii_(tii), tri_(tri), dstReg_(0), srcReg_(0), subIdx_(0),
192 partial_(false), crossClass_(false), flipped_(false), newRC_(0) {}
194 /// setRegisters - set registers to match the copy instruction MI. Return
195 /// false if MI is not a coalescable copy instruction.
196 bool setRegisters(const MachineInstr*);
198 /// flip - Swap srcReg_ and dstReg_. Return false if swapping is impossible
199 /// because dstReg_ is a physical register, or subIdx_ is set.
202 /// isCoalescable - Return true if MI is a copy instruction that will become
203 /// an identity copy after coalescing.
204 bool isCoalescable(const MachineInstr*) const;
206 /// isPhys - Return true if DstReg is a physical register.
207 bool isPhys() const { return !newRC_; }
209 /// isPartial - Return true if the original copy instruction did not copy the
210 /// full register, but was a subreg operation.
211 bool isPartial() const { return partial_; }
213 /// isCrossClass - Return true if DstReg is virtual and NewRC is a smaller register class than DstReg's.
214 bool isCrossClass() const { return crossClass_; }
216 /// isFlipped - Return true when getSrcReg is the register being defined by
217 /// the original copy instruction.
218 bool isFlipped() const { return flipped_; }
220 /// getDstReg - Return the register (virtual or physical) that will remain
221 /// after coalescing.
222 unsigned getDstReg() const { return dstReg_; }
224 /// getSrcReg - Return the virtual register that will be coalesced away.
225 unsigned getSrcReg() const { return srcReg_; }
227 /// getSubIdx - Return the subregister index in DstReg that SrcReg will be
228 /// coalesced into, or 0.
229 unsigned getSubIdx() const { return subIdx_; }
231 /// getNewRC - Return the register class of the coalesced register.
232 const TargetRegisterClass *getNewRC() const { return newRC_; }
236 // Because of the way .a files work, we must force the SimpleRC
237 // implementation to be pulled in if the RegisterCoalescing header is
238 // included. Otherwise we run the risk of RegisterCoalescing being
239 // used, but the default implementation not being linked into the tool
241 FORCE_DEFINING_FILE_TO_BE_LINKED(RegisterCoalescer)
242 FORCE_DEFINING_FILE_TO_BE_LINKED(SimpleRegisterCoalescing)