blob: 2aee3a63a2b1e5498d983318f3401e5b91e59871 [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"
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
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000034public:
35 /// Create a new AllocationOrder for VirtReg.
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000036 /// @param VirtReg Virtual register to allocate for.
37 /// @param VRM Virtual register map for function.
Jakob Stoklund Olesen5b9deab2012-01-24 18:09:18 +000038 /// @param RegClassInfo Information about reserved and allocatable registers.
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000039 AllocationOrder(unsigned VirtReg,
40 const VirtRegMap &VRM,
Matthias Braun5d1f12d2015-07-15 22:16:00 +000041 const RegisterClassInfo &RegClassInfo,
42 const LiveRegMatrix *Matrix);
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000043
Jakob Stoklund Olesen3dd236c2013-01-12 00:57:44 +000044 /// Get the allocation order without reordered hints.
45 ArrayRef<MCPhysReg> getOrder() const { return Order; }
46
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000047 /// Return the next physical register in the allocation order, or 0.
48 /// It is safe to call next() again after it returned 0, it will keep
49 /// returning 0 until rewind() is called.
Aditya Nandakumar73f3d332013-12-05 21:18:40 +000050 unsigned next(unsigned Limit = 0) {
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000051 if (Pos < 0)
52 return Hints.end()[Pos++];
Aditya Nandakumar73f3d332013-12-05 21:18:40 +000053 if (!Limit)
54 Limit = Order.size();
55 while (Pos < int(Limit)) {
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000056 unsigned Reg = Order[Pos++];
57 if (!isHint(Reg))
58 return Reg;
59 }
60 return 0;
61 }
Jakob Stoklund Olesen0cde8eb2011-06-06 21:02:04 +000062
Jakob Stoklund Olesen3dd236c2013-01-12 00:57:44 +000063 /// As next(), but allow duplicates to be returned, and stop before the
64 /// Limit'th register in the RegisterClassInfo allocation order.
65 ///
66 /// This can produce more than Limit registers if there are hints.
67 unsigned nextWithDups(unsigned Limit) {
68 if (Pos < 0)
69 return Hints.end()[Pos++];
70 if (Pos < int(Limit))
71 return Order[Pos++];
72 return 0;
73 }
74
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000075 /// Start over from the beginning.
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000076 void rewind() { Pos = -int(Hints.size()); }
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000077
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000078 /// Return true if the last register returned from next() was a preferred register.
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000079 bool isHint() const { return Pos <= 0; }
Jakob Stoklund Olesenc784a1f2012-12-03 22:51:04 +000080
81 /// Return true if PhysReg is a preferred register.
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +000082 bool isHint(unsigned PhysReg) const {
83 return std::find(Hints.begin(), Hints.end(), PhysReg) != Hints.end();
84 }
Jakob Stoklund Olesen0c67e012010-12-10 18:36:02 +000085};
86
87} // end namespace llvm
88
89#endif