blob: 2dd9dac99303ba27987005e8d4991242e0f67cc3 [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 {
Jim Stichnoth037fa1d2014-10-07 11:09:33 -070037 const bool UseTrimmed = true;
38 return range().overlaps(Other.range(), UseTrimmed);
Jim Stichnothd97c7df2014-06-04 11:57:08 -070039 }
40 bool overlapsStart(const LiveRangeWrapper &Other) const {
Jim Stichnoth037fa1d2014-10-07 11:09:33 -070041 const bool UseTrimmed = true;
42 return range().overlapsInst(Other.range().getStart(), UseTrimmed);
Jim Stichnothd97c7df2014-06-04 11:57:08 -070043 }
44 Variable *const Var;
45 void dump(const Cfg *Func) const;
46
47private:
Jim Stichnoth0795ba02014-10-01 14:23:01 -070048 // LiveRangeWrapper(const LiveRangeWrapper &) = delete;
49 LiveRangeWrapper &operator=(const LiveRangeWrapper &) = delete;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070050};
51
52class LinearScan {
53public:
54 LinearScan(Cfg *Func) : Func(Func) {}
55 void scan(const llvm::SmallBitVector &RegMask);
56 void dump(Cfg *Func) const;
57
58private:
59 Cfg *const Func;
60 // RangeCompare is the comparator for sorting an LiveRangeWrapper
61 // by starting point in a std::set<>. Ties are broken by variable
62 // number so that sorting is stable.
63 struct RangeCompare {
64 bool operator()(const LiveRangeWrapper &L,
65 const LiveRangeWrapper &R) const {
66 InstNumberT Lstart = L.Var->getLiveRange().getStart();
67 InstNumberT Rstart = R.Var->getLiveRange().getStart();
68 if (Lstart == Rstart)
69 return L.Var->getIndex() < R.Var->getIndex();
70 return Lstart < Rstart;
71 }
72 };
73 typedef std::set<LiveRangeWrapper, RangeCompare> OrderedRanges;
74 typedef std::list<LiveRangeWrapper> UnorderedRanges;
75 OrderedRanges Unhandled;
Jim Stichnoth541ba662014-10-02 12:58:21 -070076 // UnhandledPrecolored is a subset of Unhandled, specially collected
77 // for faster processing.
78 OrderedRanges UnhandledPrecolored;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070079 UnorderedRanges Active, Inactive, Handled;
Jim Stichnoth0795ba02014-10-01 14:23:01 -070080 LinearScan(const LinearScan &) = delete;
81 LinearScan &operator=(const LinearScan &) = delete;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070082};
83
84} // end of namespace Ice
85
86#endif // SUBZERO_SRC_ICEREGALLOC_H