blob: 9247dd8449366128a2be2fd2301e1fda4d1407cf [file] [log] [blame]
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +00001//===-- llvm/CodeGen/AllocationOrder.h - Allocation Order -*- C++ -*-------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements an allocation order for virtual registers.
10//
11// The preferred allocation order for a virtual register depends on allocation
12// hints and target hooks. The AllocationOrder class encapsulates all of that.
13//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000016#ifndef LLVM_LIB_CODEGEN_ALLOCATIONORDER_H
17#define LLVM_LIB_CODEGEN_ALLOCATIONORDER_H
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000018
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000019#include "llvm/ADT/ArrayRef.h"
David Majnemer0d955d02016-08-11 22:21:41 +000020#include "llvm/ADT/STLExtras.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000021#include "llvm/MC/MCRegisterInfo.h"
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +000022
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000023namespace llvm {
24
Jakob Stoklund Olesenb8bf3c02011-06-03 20:34:53 +000025class RegisterClassInfo;
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000026class VirtRegMap;
Matthias Braun5d1f12d2015-07-15 22:16:00 +000027class LiveRegMatrix;
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000028
Benjamin Kramerf4c20252015-07-01 14:47:39 +000029class LLVM_LIBRARY_VISIBILITY AllocationOrder {
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000030 SmallVector<MCPhysReg, 16> Hints;
31 ArrayRef<MCPhysReg> Order;
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000032 int Pos;
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000033
Jonas Paulsson4b017e62017-11-10 08:46:26 +000034 // If HardHints is true, *only* Hints will be returned.
35 bool HardHints;
36
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000037public:
Jonas Paulsson4b017e62017-11-10 08:46:26 +000038
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000039 /// Create a new AllocationOrder for VirtReg.
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000040 /// @param VirtReg Virtual register to allocate for.
41 /// @param VRM Virtual register map for function.
Jakob Stoklund Olesen5b9deab2012-01-24 18:09:18 +000042 /// @param RegClassInfo Information about reserved and allocatable registers.
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000043 AllocationOrder(unsigned VirtReg,
44 const VirtRegMap &VRM,
Matthias Braun5d1f12d2015-07-15 22:16:00 +000045 const RegisterClassInfo &RegClassInfo,
46 const LiveRegMatrix *Matrix);
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000047
Jakob Stoklund Olesen3dd236c2013-01-12 00:57:44 +000048 /// Get the allocation order without reordered hints.
49 ArrayRef<MCPhysReg> getOrder() const { return Order; }
50
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000051 /// Return the next physical register in the allocation order, or 0.
52 /// It is safe to call next() again after it returned 0, it will keep
53 /// returning 0 until rewind() is called.
Aditya Nandakumar73f3d332013-12-05 21:18:40 +000054 unsigned next(unsigned Limit = 0) {
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000055 if (Pos < 0)
56 return Hints.end()[Pos++];
Jonas Paulsson4b017e62017-11-10 08:46:26 +000057 if (HardHints)
58 return 0;
Aditya Nandakumar73f3d332013-12-05 21:18:40 +000059 if (!Limit)
60 Limit = Order.size();
61 while (Pos < int(Limit)) {
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000062 unsigned Reg = Order[Pos++];
63 if (!isHint(Reg))
64 return Reg;
65 }
66 return 0;
67 }
Jakob Stoklund Olesen0cde8eb2011-06-06 21:02:04 +000068
Jakob Stoklund Olesen3dd236c2013-01-12 00:57:44 +000069 /// As next(), but allow duplicates to be returned, and stop before the
70 /// Limit'th register in the RegisterClassInfo allocation order.
71 ///
72 /// This can produce more than Limit registers if there are hints.
73 unsigned nextWithDups(unsigned Limit) {
74 if (Pos < 0)
75 return Hints.end()[Pos++];
Jonas Paulsson4b017e62017-11-10 08:46:26 +000076 if (HardHints)
77 return 0;
Jakob Stoklund Olesen3dd236c2013-01-12 00:57:44 +000078 if (Pos < int(Limit))
79 return Order[Pos++];
80 return 0;
81 }
82
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000083 /// Start over from the beginning.
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000084 void rewind() { Pos = -int(Hints.size()); }
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000085
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000086 /// Return true if the last register returned from next() was a preferred register.
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000087 bool isHint() const { return Pos <= 0; }
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000088
89 /// Return true if PhysReg is a preferred register.
David Majnemer0d955d02016-08-11 22:21:41 +000090 bool isHint(unsigned PhysReg) const { return is_contained(Hints, PhysReg); }
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000091};
92
93} // end namespace llvm
94
95#endif