blob: 467bcc2edc6f69379a5cd39543688bbe390386da [file] [log] [blame]
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +00001//===-- llvm/CodeGen/AllocationOrder.h - Allocation Order -*- C++ -*-------===//
2//
3// The LLVM Compiler Infrastructure
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 implements an allocation order for virtual registers.
11//
12// The preferred allocation order for a virtual register depends on allocation
13// hints and target hooks. The AllocationOrder class encapsulates all of that.
14//
15//===----------------------------------------------------------------------===//
16
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000017#ifndef LLVM_LIB_CODEGEN_ALLOCATIONORDER_H
18#define LLVM_LIB_CODEGEN_ALLOCATIONORDER_H
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000019
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000020#include "llvm/ADT/ArrayRef.h"
David Majnemer975248e2016-08-11 22:21:41 +000021#include "llvm/ADT/STLExtras.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000022#include "llvm/MC/MCRegisterInfo.h"
Jakob Stoklund Olesen39b5c0c2012-11-29 03:34:17 +000023
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000024namespace llvm {
25
Jakob Stoklund Olesen5f2316a2011-06-03 20:34:53 +000026class RegisterClassInfo;
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000027class VirtRegMap;
Matthias Braun2aa57272015-07-15 22:16:00 +000028class LiveRegMatrix;
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000029
Benjamin Kramerb4537752015-07-01 14:47:39 +000030class LLVM_LIBRARY_VISIBILITY AllocationOrder {
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000031 SmallVector<MCPhysReg, 16> Hints;
32 ArrayRef<MCPhysReg> Order;
Jakob Stoklund Olesenf7999fe2012-12-04 22:25:16 +000033 int Pos;
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000034
Jonas Paulssona9fba712017-11-10 08:46:26 +000035 // If HardHints is true, *only* Hints will be returned.
36 bool HardHints;
37
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000038public:
Jonas Paulssona9fba712017-11-10 08:46:26 +000039
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000040 /// Create a new AllocationOrder for VirtReg.
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000041 /// @param VirtReg Virtual register to allocate for.
42 /// @param VRM Virtual register map for function.
Jakob Stoklund Olesen10c6fdc2012-01-24 18:09:18 +000043 /// @param RegClassInfo Information about reserved and allocatable registers.
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000044 AllocationOrder(unsigned VirtReg,
45 const VirtRegMap &VRM,
Matthias Braun2aa57272015-07-15 22:16:00 +000046 const RegisterClassInfo &RegClassInfo,
47 const LiveRegMatrix *Matrix);
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000048
Jakob Stoklund Olesen6d613292013-01-12 00:57:44 +000049 /// Get the allocation order without reordered hints.
50 ArrayRef<MCPhysReg> getOrder() const { return Order; }
51
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000052 /// Return the next physical register in the allocation order, or 0.
53 /// It is safe to call next() again after it returned 0, it will keep
54 /// returning 0 until rewind() is called.
Aditya Nandakumar226e3ea2013-12-05 21:18:40 +000055 unsigned next(unsigned Limit = 0) {
Jakob Stoklund Olesenf7999fe2012-12-04 22:25:16 +000056 if (Pos < 0)
57 return Hints.end()[Pos++];
Jonas Paulssona9fba712017-11-10 08:46:26 +000058 if (HardHints)
59 return 0;
Aditya Nandakumar226e3ea2013-12-05 21:18:40 +000060 if (!Limit)
61 Limit = Order.size();
62 while (Pos < int(Limit)) {
Jakob Stoklund Olesenf7999fe2012-12-04 22:25:16 +000063 unsigned Reg = Order[Pos++];
64 if (!isHint(Reg))
65 return Reg;
66 }
67 return 0;
68 }
Jakob Stoklund Olesena46a1002011-06-06 21:02:04 +000069
Jakob Stoklund Olesen6d613292013-01-12 00:57:44 +000070 /// As next(), but allow duplicates to be returned, and stop before the
71 /// Limit'th register in the RegisterClassInfo allocation order.
72 ///
73 /// This can produce more than Limit registers if there are hints.
74 unsigned nextWithDups(unsigned Limit) {
75 if (Pos < 0)
76 return Hints.end()[Pos++];
Jonas Paulssona9fba712017-11-10 08:46:26 +000077 if (HardHints)
78 return 0;
Jakob Stoklund Olesen6d613292013-01-12 00:57:44 +000079 if (Pos < int(Limit))
80 return Order[Pos++];
81 return 0;
82 }
83
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000084 /// Start over from the beginning.
Jakob Stoklund Olesenf7999fe2012-12-04 22:25:16 +000085 void rewind() { Pos = -int(Hints.size()); }
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000086
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000087 /// Return true if the last register returned from next() was a preferred register.
Jakob Stoklund Olesenf7999fe2012-12-04 22:25:16 +000088 bool isHint() const { return Pos <= 0; }
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000089
90 /// Return true if PhysReg is a preferred register.
David Majnemer975248e2016-08-11 22:21:41 +000091 bool isHint(unsigned PhysReg) const { return is_contained(Hints, PhysReg); }
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000092};
93
94} // end namespace llvm
95
96#endif