blob: a5293f60a07729147446d0eed1691dcf7d98d7f5 [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
17#ifndef LLVM_CODEGEN_ALLOCATIONORDER_H
18#define LLVM_CODEGEN_ALLOCATIONORDER_H
19
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000020#include "llvm/ADT/ArrayRef.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000021#include "llvm/MC/MCRegisterInfo.h"
Jakob Stoklund Olesen39b5c0c2012-11-29 03:34:17 +000022
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000023namespace llvm {
24
Jakob Stoklund Olesen5f2316a2011-06-03 20:34:53 +000025class RegisterClassInfo;
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000026class VirtRegMap;
27
28class AllocationOrder {
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000029 SmallVector<MCPhysReg, 16> Hints;
30 ArrayRef<MCPhysReg> Order;
Jakob Stoklund Olesenf7999fe2012-12-04 22:25:16 +000031 int Pos;
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000032
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000033public:
34 /// Create a new AllocationOrder for VirtReg.
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000035 /// @param VirtReg Virtual register to allocate for.
36 /// @param VRM Virtual register map for function.
Jakob Stoklund Olesen10c6fdc2012-01-24 18:09:18 +000037 /// @param RegClassInfo Information about reserved and allocatable registers.
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000038 AllocationOrder(unsigned VirtReg,
39 const VirtRegMap &VRM,
Jakob Stoklund Olesen5f2316a2011-06-03 20:34:53 +000040 const RegisterClassInfo &RegClassInfo);
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000041
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000042 /// Return the next physical register in the allocation order, or 0.
43 /// It is safe to call next() again after it returned 0, it will keep
44 /// returning 0 until rewind() is called.
Jakob Stoklund Olesenf7999fe2012-12-04 22:25:16 +000045 unsigned next() {
46 if (Pos < 0)
47 return Hints.end()[Pos++];
48 while (Pos < int(Order.size())) {
49 unsigned Reg = Order[Pos++];
50 if (!isHint(Reg))
51 return Reg;
52 }
53 return 0;
54 }
Jakob Stoklund Olesena46a1002011-06-06 21:02:04 +000055
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000056 /// Start over from the beginning.
Jakob Stoklund Olesenf7999fe2012-12-04 22:25:16 +000057 void rewind() { Pos = -int(Hints.size()); }
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000058
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000059 /// Return true if the last register returned from next() was a preferred register.
Jakob Stoklund Olesenf7999fe2012-12-04 22:25:16 +000060 bool isHint() const { return Pos <= 0; }
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000061
62 /// Return true if PhysReg is a preferred register.
Jakob Stoklund Olesenf7999fe2012-12-04 22:25:16 +000063 bool isHint(unsigned PhysReg) const {
64 return std::find(Hints.begin(), Hints.end(), PhysReg) != Hints.end();
65 }
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000066};
67
68} // end namespace llvm
69
70#endif