Jakob Stoklund Olesen | c9672cb | 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 | 00e08fc | 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 | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 19 | |
Jakob Stoklund Olesen | fc29db1 | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/ArrayRef.h" |
David Majnemer | 975248e | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCRegisterInfo.h" |
Jakob Stoklund Olesen | 39b5c0c | 2012-11-29 03:34:17 +0000 | [diff] [blame] | 23 | |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 24 | namespace llvm { |
| 25 | |
Jakob Stoklund Olesen | 5f2316a | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 26 | class RegisterClassInfo; |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 27 | class VirtRegMap; |
Matthias Braun | 2aa5727 | 2015-07-15 22:16:00 +0000 | [diff] [blame] | 28 | class LiveRegMatrix; |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 29 | |
Benjamin Kramer | b453775 | 2015-07-01 14:47:39 +0000 | [diff] [blame] | 30 | class LLVM_LIBRARY_VISIBILITY AllocationOrder { |
Jakob Stoklund Olesen | fc29db1 | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 31 | SmallVector<MCPhysReg, 16> Hints; |
| 32 | ArrayRef<MCPhysReg> Order; |
Jakob Stoklund Olesen | f7999fe | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 33 | int Pos; |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 34 | |
Jonas Paulsson | a9fba71 | 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 | fc29db1 | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 38 | public: |
Jonas Paulsson | a9fba71 | 2017-11-10 08:46:26 +0000 | [diff] [blame] | 39 | |
Jakob Stoklund Olesen | fc29db1 | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 40 | /// Create a new AllocationOrder for VirtReg. |
Jakob Stoklund Olesen | c9672cb | 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 | 10c6fdc | 2012-01-24 18:09:18 +0000 | [diff] [blame] | 43 | /// @param RegClassInfo Information about reserved and allocatable registers. |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 44 | AllocationOrder(unsigned VirtReg, |
| 45 | const VirtRegMap &VRM, |
Matthias Braun | 2aa5727 | 2015-07-15 22:16:00 +0000 | [diff] [blame] | 46 | const RegisterClassInfo &RegClassInfo, |
| 47 | const LiveRegMatrix *Matrix); |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 48 | |
Jakob Stoklund Olesen | 6d61329 | 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 | fc29db1 | 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 | 226e3ea | 2013-12-05 21:18:40 +0000 | [diff] [blame] | 55 | unsigned next(unsigned Limit = 0) { |
Jakob Stoklund Olesen | f7999fe | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 56 | if (Pos < 0) |
| 57 | return Hints.end()[Pos++]; |
Jonas Paulsson | a9fba71 | 2017-11-10 08:46:26 +0000 | [diff] [blame] | 58 | if (HardHints) |
| 59 | return 0; |
Aditya Nandakumar | 226e3ea | 2013-12-05 21:18:40 +0000 | [diff] [blame] | 60 | if (!Limit) |
| 61 | Limit = Order.size(); |
| 62 | while (Pos < int(Limit)) { |
Jakob Stoklund Olesen | f7999fe | 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 | a46a100 | 2011-06-06 21:02:04 +0000 | [diff] [blame] | 69 | |
Jakob Stoklund Olesen | 6d61329 | 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 | a9fba71 | 2017-11-10 08:46:26 +0000 | [diff] [blame] | 77 | if (HardHints) |
| 78 | return 0; |
Jakob Stoklund Olesen | 6d61329 | 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 | fc29db1 | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 84 | /// Start over from the beginning. |
Jakob Stoklund Olesen | f7999fe | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 85 | void rewind() { Pos = -int(Hints.size()); } |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 86 | |
Jakob Stoklund Olesen | fc29db1 | 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 | f7999fe | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 88 | bool isHint() const { return Pos <= 0; } |
Jakob Stoklund Olesen | fc29db1 | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 89 | |
| 90 | /// Return true if PhysReg is a preferred register. |
David Majnemer | 975248e | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 91 | bool isHint(unsigned PhysReg) const { return is_contained(Hints, PhysReg); } |
Jakob Stoklund Olesen | c9672cb | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // end namespace llvm |
| 95 | |
| 96 | #endif |