blob: edcf2662367f2f549e5a7f32a9afad3b15309760 [file] [log] [blame]
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001//===- subzero/src/IceRegAlloc.h - Linear-scan reg. allocation --*- C++ -*-===//
2//
3// The Subzero Code Generator
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 declares the data structures used during linear-scan
11// register allocation. This includes LiveRangeWrapper which
12// encapsulates a variable and its live range, and LinearScan which
13// holds the various work queues for the linear-scan algorithm.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef SUBZERO_SRC_ICEREGALLOC_H
18#define SUBZERO_SRC_ICEREGALLOC_H
19
20#include "IceDefs.h"
21#include "IceTypes.h"
22
23namespace Ice {
24
25// Currently this just wraps a Variable pointer, so in principle we
26// could use containers of Variable* instead of LiveRangeWrapper. But
27// in the future, we may want to do more complex things such as live
28// range splitting, and keeping a wrapper should make that simpler.
29class LiveRangeWrapper {
30public:
31 LiveRangeWrapper(Variable *Var) : Var(Var) {}
32 const LiveRange &range() const { return Var->getLiveRange(); }
33 bool endsBefore(const LiveRangeWrapper &Other) const {
34 return range().endsBefore(Other.range());
35 }
36 bool overlaps(const LiveRangeWrapper &Other) const {
37 return range().overlaps(Other.range());
38 }
39 bool overlapsStart(const LiveRangeWrapper &Other) const {
40 return range().overlaps(Other.range().getStart());
41 }
42 Variable *const Var;
43 void dump(const Cfg *Func) const;
44
45private:
46 // LiveRangeWrapper(const LiveRangeWrapper &) LLVM_DELETED_FUNCTION;
47 LiveRangeWrapper &operator=(const LiveRangeWrapper &) LLVM_DELETED_FUNCTION;
48};
49
50class LinearScan {
51public:
52 LinearScan(Cfg *Func) : Func(Func) {}
53 void scan(const llvm::SmallBitVector &RegMask);
54 void dump(Cfg *Func) const;
55
56private:
57 Cfg *const Func;
58 // RangeCompare is the comparator for sorting an LiveRangeWrapper
59 // by starting point in a std::set<>. Ties are broken by variable
60 // number so that sorting is stable.
61 struct RangeCompare {
62 bool operator()(const LiveRangeWrapper &L,
63 const LiveRangeWrapper &R) const {
64 InstNumberT Lstart = L.Var->getLiveRange().getStart();
65 InstNumberT Rstart = R.Var->getLiveRange().getStart();
66 if (Lstart == Rstart)
67 return L.Var->getIndex() < R.Var->getIndex();
68 return Lstart < Rstart;
69 }
70 };
71 typedef std::set<LiveRangeWrapper, RangeCompare> OrderedRanges;
72 typedef std::list<LiveRangeWrapper> UnorderedRanges;
73 OrderedRanges Unhandled;
74 UnorderedRanges Active, Inactive, Handled;
75 LinearScan(const LinearScan &) LLVM_DELETED_FUNCTION;
76 LinearScan &operator=(const LinearScan &) LLVM_DELETED_FUNCTION;
77};
78
79} // end of namespace Ice
80
81#endif // SUBZERO_SRC_ICEREGALLOC_H