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 | |
| 17 | #ifndef LLVM_CODEGEN_ALLOCATIONORDER_H |
| 18 | #define LLVM_CODEGEN_ALLOCATIONORDER_H |
| 19 | |
Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/ArrayRef.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCRegisterInfo.h" |
Jakob Stoklund Olesen | bdb55e0 | 2012-11-29 03:34:17 +0000 | [diff] [blame] | 22 | |
Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 23 | namespace llvm { |
| 24 | |
Jakob Stoklund Olesen | b8bf3c0 | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 25 | class RegisterClassInfo; |
Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 26 | class VirtRegMap; |
| 27 | |
Benjamin Kramer | 079b96e | 2013-09-11 18:05:11 +0000 | [diff] [blame] | 28 | class AllocationOrder { |
Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 29 | SmallVector<MCPhysReg, 16> Hints; |
| 30 | ArrayRef<MCPhysReg> Order; |
Jakob Stoklund Olesen | 3cb2cb8 | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 31 | int Pos; |
Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 32 | |
Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 33 | public: |
| 34 | /// Create a new AllocationOrder for VirtReg. |
Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 35 | /// @param VirtReg Virtual register to allocate for. |
| 36 | /// @param VRM Virtual register map for function. |
Jakob Stoklund Olesen | 5b9deab | 2012-01-24 18:09:18 +0000 | [diff] [blame] | 37 | /// @param RegClassInfo Information about reserved and allocatable registers. |
Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 38 | AllocationOrder(unsigned VirtReg, |
| 39 | const VirtRegMap &VRM, |
Jakob Stoklund Olesen | b8bf3c0 | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 40 | const RegisterClassInfo &RegClassInfo); |
Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 41 | |
Jakob Stoklund Olesen | 3dd236c | 2013-01-12 00:57:44 +0000 | [diff] [blame] | 42 | /// Get the allocation order without reordered hints. |
| 43 | ArrayRef<MCPhysReg> getOrder() const { return Order; } |
| 44 | |
Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 45 | /// Return the next physical register in the allocation order, or 0. |
| 46 | /// It is safe to call next() again after it returned 0, it will keep |
| 47 | /// returning 0 until rewind() is called. |
Aditya Nandakumar | 73f3d33 | 2013-12-05 21:18:40 +0000 | [diff] [blame] | 48 | unsigned next(unsigned Limit = 0) { |
Jakob Stoklund Olesen | 3cb2cb8 | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 49 | if (Pos < 0) |
| 50 | return Hints.end()[Pos++]; |
Aditya Nandakumar | 73f3d33 | 2013-12-05 21:18:40 +0000 | [diff] [blame] | 51 | if (!Limit) |
| 52 | Limit = Order.size(); |
| 53 | while (Pos < int(Limit)) { |
Jakob Stoklund Olesen | 3cb2cb8 | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 54 | unsigned Reg = Order[Pos++]; |
| 55 | if (!isHint(Reg)) |
| 56 | return Reg; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
Jakob Stoklund Olesen | 0cde8eb | 2011-06-06 21:02:04 +0000 | [diff] [blame] | 60 | |
Jakob Stoklund Olesen | 3dd236c | 2013-01-12 00:57:44 +0000 | [diff] [blame] | 61 | /// As next(), but allow duplicates to be returned, and stop before the |
| 62 | /// Limit'th register in the RegisterClassInfo allocation order. |
| 63 | /// |
| 64 | /// This can produce more than Limit registers if there are hints. |
| 65 | unsigned nextWithDups(unsigned Limit) { |
| 66 | if (Pos < 0) |
| 67 | return Hints.end()[Pos++]; |
| 68 | if (Pos < int(Limit)) |
| 69 | return Order[Pos++]; |
| 70 | return 0; |
| 71 | } |
| 72 | |
Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 73 | /// Start over from the beginning. |
Jakob Stoklund Olesen | 3cb2cb8 | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 74 | void rewind() { Pos = -int(Hints.size()); } |
Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 75 | |
Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 76 | /// 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] | 77 | bool isHint() const { return Pos <= 0; } |
Jakob Stoklund Olesen | c784a1f | 2012-12-03 22:51:04 +0000 | [diff] [blame] | 78 | |
| 79 | /// Return true if PhysReg is a preferred register. |
Jakob Stoklund Olesen | 3cb2cb8 | 2012-12-04 22:25:16 +0000 | [diff] [blame] | 80 | bool isHint(unsigned PhysReg) const { |
| 81 | return std::find(Hints.begin(), Hints.end(), PhysReg) != Hints.end(); |
| 82 | } |
Jakob Stoklund Olesen | 0c67e01 | 2010-12-10 18:36:02 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // end namespace llvm |
| 86 | |
| 87 | #endif |