blob: 8223a52e333bc00d6fd132e95642c0b358109b4f [file] [log] [blame]
Jakob Stoklund Olesen0c67e012010-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 Kramera7c40ef2014-08-13 16:26:38 +000017#ifndef LLVM_LIB_CODEGEN_ALLOCATIONORDER_H
18#define LLVM_LIB_CODEGEN_ALLOCATIONORDER_H
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000019
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000020#include "llvm/ADT/ArrayRef.h"
David Majnemer0d955d02016-08-11 22:21:41 +000021#include "llvm/ADT/STLExtras.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000022#include "llvm/MC/MCRegisterInfo.h"
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +000023
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000024namespace llvm {
25
Jakob Stoklund Olesenb8bf3c02011-06-03 20:34:53 +000026class RegisterClassInfo;
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000027class VirtRegMap;
Matthias Braun5d1f12d2015-07-15 22:16:00 +000028class LiveRegMatrix;
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000029
Benjamin Kramerf4c20252015-07-01 14:47:39 +000030class LLVM_LIBRARY_VISIBILITY AllocationOrder {
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000031 SmallVector<MCPhysReg, 16> Hints;
32 ArrayRef<MCPhysReg> Order;
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000033 int Pos;
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000034
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000035public:
36 /// Create a new AllocationOrder for VirtReg.
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000037 /// @param VirtReg Virtual register to allocate for.
38 /// @param VRM Virtual register map for function.
Jakob Stoklund Olesen5b9deab2012-01-24 18:09:18 +000039 /// @param RegClassInfo Information about reserved and allocatable registers.
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000040 AllocationOrder(unsigned VirtReg,
41 const VirtRegMap &VRM,
Matthias Braun5d1f12d2015-07-15 22:16:00 +000042 const RegisterClassInfo &RegClassInfo,
43 const LiveRegMatrix *Matrix);
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000044
Jakob Stoklund Olesen3dd236c2013-01-12 00:57:44 +000045 /// Get the allocation order without reordered hints.
46 ArrayRef<MCPhysReg> getOrder() const { return Order; }
47
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000048 /// Return the next physical register in the allocation order, or 0.
49 /// It is safe to call next() again after it returned 0, it will keep
50 /// returning 0 until rewind() is called.
Aditya Nandakumar73f3d332013-12-05 21:18:40 +000051 unsigned next(unsigned Limit = 0) {
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000052 if (Pos < 0)
53 return Hints.end()[Pos++];
Aditya Nandakumar73f3d332013-12-05 21:18:40 +000054 if (!Limit)
55 Limit = Order.size();
56 while (Pos < int(Limit)) {
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000057 unsigned Reg = Order[Pos++];
58 if (!isHint(Reg))
59 return Reg;
60 }
61 return 0;
62 }
Jakob Stoklund Olesen0cde8eb2011-06-06 21:02:04 +000063
Jakob Stoklund Olesen3dd236c2013-01-12 00:57:44 +000064 /// As next(), but allow duplicates to be returned, and stop before the
65 /// Limit'th register in the RegisterClassInfo allocation order.
66 ///
67 /// This can produce more than Limit registers if there are hints.
68 unsigned nextWithDups(unsigned Limit) {
69 if (Pos < 0)
70 return Hints.end()[Pos++];
71 if (Pos < int(Limit))
72 return Order[Pos++];
73 return 0;
74 }
75
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000076 /// Start over from the beginning.
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000077 void rewind() { Pos = -int(Hints.size()); }
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000078
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000079 /// Return true if the last register returned from next() was a preferred register.
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000080 bool isHint() const { return Pos <= 0; }
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000081
82 /// Return true if PhysReg is a preferred register.
David Majnemer0d955d02016-08-11 22:21:41 +000083 bool isHint(unsigned PhysReg) const { return is_contained(Hints, PhysReg); }
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000084};
85
86} // end namespace llvm
87
88#endif