| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 1 | //===-- 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 Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 17 | #ifndef LLVM_LIB_CODEGEN_ALLOCATIONORDER_H | 
|  | 18 | #define LLVM_LIB_CODEGEN_ALLOCATIONORDER_H | 
| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 19 |  | 
| Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/ArrayRef.h" | 
| David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" | 
| Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCRegisterInfo.h" | 
| Jakob Stoklund Olesen | bdb55e0 | 2012-11-29 03:34:17 +0000 | [diff] [blame] | 23 |  | 
| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 24 | namespace llvm { | 
|  | 25 |  | 
| Jakob Stoklund Olesen | b8bf3c0 | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 26 | class RegisterClassInfo; | 
| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 27 | class VirtRegMap; | 
| Matthias Braun | 5d1f12d | 2015-07-15 22:16:00 +0000 | [diff] [blame] | 28 | class LiveRegMatrix; | 
| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 29 |  | 
| Benjamin Kramer | f4c2025 | 2015-07-01 14:47:39 +0000 | [diff] [blame] | 30 | class LLVM_LIBRARY_VISIBILITY AllocationOrder { | 
| Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 31 | SmallVector<MCPhysReg, 16> Hints; | 
|  | 32 | ArrayRef<MCPhysReg> Order; | 
| Jakob Stoklund Olesen | 3cb2cb8 | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 33 | int Pos; | 
| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 34 |  | 
| Jonas Paulsson | 4b017e6 | 2017-11-10 08:46:26 +0000 | [diff] [blame] | 35 | // If HardHints is true, *only* Hints will be returned. | 
|  | 36 | bool HardHints; | 
|  | 37 |  | 
| Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 38 | public: | 
| Jonas Paulsson | 4b017e6 | 2017-11-10 08:46:26 +0000 | [diff] [blame] | 39 |  | 
| Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 40 | /// Create a new AllocationOrder for VirtReg. | 
| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 41 | /// @param VirtReg      Virtual register to allocate for. | 
|  | 42 | /// @param VRM          Virtual register map for function. | 
| Jakob Stoklund Olesen | 5b9deab | 2012-01-24 18:09:18 +0000 | [diff] [blame] | 43 | /// @param RegClassInfo Information about reserved and allocatable registers. | 
| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 44 | AllocationOrder(unsigned VirtReg, | 
|  | 45 | const VirtRegMap &VRM, | 
| Matthias Braun | 5d1f12d | 2015-07-15 22:16:00 +0000 | [diff] [blame] | 46 | const RegisterClassInfo &RegClassInfo, | 
|  | 47 | const LiveRegMatrix *Matrix); | 
| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 48 |  | 
| Jakob Stoklund Olesen | 3dd236c | 2013-01-12 00:57:44 +0000 | [diff] [blame] | 49 | /// Get the allocation order without reordered hints. | 
|  | 50 | ArrayRef<MCPhysReg> getOrder() const { return Order; } | 
|  | 51 |  | 
| Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 52 | /// 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 Nandakumar | 73f3d33 | 2013-12-05 21:18:40 +0000 | [diff] [blame] | 55 | unsigned next(unsigned Limit = 0) { | 
| Jakob Stoklund Olesen | 3cb2cb8 | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 56 | if (Pos < 0) | 
|  | 57 | return Hints.end()[Pos++]; | 
| Jonas Paulsson | 4b017e6 | 2017-11-10 08:46:26 +0000 | [diff] [blame] | 58 | if (HardHints) | 
|  | 59 | return 0; | 
| Aditya Nandakumar | 73f3d33 | 2013-12-05 21:18:40 +0000 | [diff] [blame] | 60 | if (!Limit) | 
|  | 61 | Limit = Order.size(); | 
|  | 62 | while (Pos < int(Limit)) { | 
| Jakob Stoklund Olesen | 3cb2cb8 | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 63 | unsigned Reg = Order[Pos++]; | 
|  | 64 | if (!isHint(Reg)) | 
|  | 65 | return Reg; | 
|  | 66 | } | 
|  | 67 | return 0; | 
|  | 68 | } | 
| Jakob Stoklund Olesen | 0cde8eb | 2011-06-06 21:02:04 +0000 | [diff] [blame] | 69 |  | 
| Jakob Stoklund Olesen | 3dd236c | 2013-01-12 00:57:44 +0000 | [diff] [blame] | 70 | /// 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 Paulsson | 4b017e6 | 2017-11-10 08:46:26 +0000 | [diff] [blame] | 77 | if (HardHints) | 
|  | 78 | return 0; | 
| Jakob Stoklund Olesen | 3dd236c | 2013-01-12 00:57:44 +0000 | [diff] [blame] | 79 | if (Pos < int(Limit)) | 
|  | 80 | return Order[Pos++]; | 
|  | 81 | return 0; | 
|  | 82 | } | 
|  | 83 |  | 
| Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 84 | /// Start over from the beginning. | 
| Jakob Stoklund Olesen | 3cb2cb8 | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 85 | void rewind() { Pos = -int(Hints.size()); } | 
| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 86 |  | 
| Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 87 | /// Return true if the last register returned from next() was a preferred register. | 
| Jakob Stoklund Olesen | 3cb2cb8 | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 88 | bool isHint() const { return Pos <= 0; } | 
| Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 89 |  | 
|  | 90 | /// Return true if PhysReg is a preferred register. | 
| David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 91 | bool isHint(unsigned PhysReg) const { return is_contained(Hints, PhysReg); } | 
| Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 92 | }; | 
|  | 93 |  | 
|  | 94 | } // end namespace llvm | 
|  | 95 |  | 
|  | 96 | #endif |