Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 1 | //===- TargetRegisterInfo.cpp - Target Register Information Implementation ===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9208bbf | 2002-12-17 04:03:08 +0000 | [diff] [blame] | 9 | // |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 10 | // This file implements the TargetRegisterInfo interface. |
Chris Lattner | 9208bbf | 2002-12-17 04:03:08 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jim Laskey | 6b59a36 | 2006-08-03 17:27:09 +0000 | [diff] [blame] | 14 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetRegisterInfo.h" |
Jim Laskey | 6b59a36 | 2006-08-03 17:27:09 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetFrameInfo.h" |
Jim Laskey | a997918 | 2006-03-28 13:48:33 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Evan Cheng | 61de82d | 2007-02-15 05:59:24 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/BitVector.h" |
Jim Laskey | a997918 | 2006-03-28 13:48:33 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 20ea062 | 2006-02-01 18:10:56 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 23 | TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterDesc *D, unsigned NR, |
Chris Lattner | 3ad7642 | 2002-12-28 20:34:18 +0000 | [diff] [blame] | 24 | regclass_iterator RCB, regclass_iterator RCE, |
Jakob Stoklund Olesen | 1fc8e75 | 2010-05-25 19:49:38 +0000 | [diff] [blame] | 25 | const char *const *subregindexnames, |
Owen Anderson | 605041e | 2008-07-01 17:34:38 +0000 | [diff] [blame] | 26 | int CFSO, int CFDO, |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 27 | const unsigned* subregs, const unsigned subregsize, |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 28 | const unsigned* aliases, const unsigned aliasessize) |
Owen Anderson | 7d770be | 2009-04-09 03:50:16 +0000 | [diff] [blame] | 29 | : SubregHash(subregs), SubregHashSize(subregsize), |
Owen Anderson | 3ca15c9 | 2009-04-09 22:19:30 +0000 | [diff] [blame] | 30 | AliasesHash(aliases), AliasesHashSize(aliasessize), |
Jakob Stoklund Olesen | 1fc8e75 | 2010-05-25 19:49:38 +0000 | [diff] [blame] | 31 | Desc(D), SubRegIndexNames(subregindexnames), NumRegs(NR), |
| 32 | RegClassBegin(RCB), RegClassEnd(RCE) { |
Chris Lattner | 9208bbf | 2002-12-17 04:03:08 +0000 | [diff] [blame] | 33 | assert(NumRegs < FirstVirtualRegister && |
| 34 | "Target has too many physical registers!"); |
| 35 | |
Chris Lattner | 3ad7642 | 2002-12-28 20:34:18 +0000 | [diff] [blame] | 36 | CallFrameSetupOpcode = CFSO; |
| 37 | CallFrameDestroyOpcode = CFDO; |
Chris Lattner | 9208bbf | 2002-12-17 04:03:08 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 40 | TargetRegisterInfo::~TargetRegisterInfo() {} |
Nate Begeman | 0aafc32 | 2004-10-27 06:00:53 +0000 | [diff] [blame] | 41 | |
Evan Cheng | 42d6027 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 42 | /// getPhysicalRegisterRegClass - Returns the Register Class of a physical |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 43 | /// register of the given type. If type is EVT::Other, then just return any |
Evan Cheng | 676dd7c | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 44 | /// register class the register belongs to. |
Evan Cheng | 42d6027 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 45 | const TargetRegisterClass * |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 46 | TargetRegisterInfo::getPhysicalRegisterRegClass(unsigned reg, EVT VT) const { |
Evan Cheng | 42d6027 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 47 | assert(isPhysicalRegister(reg) && "reg must be a physical register"); |
Evan Cheng | fa2f786 | 2008-03-11 07:54:14 +0000 | [diff] [blame] | 48 | |
Dan Gohman | 4893c06 | 2008-09-21 21:01:49 +0000 | [diff] [blame] | 49 | // Pick the most super register class of the right type that contains |
| 50 | // this physreg. |
| 51 | const TargetRegisterClass* BestRC = 0; |
Evan Cheng | 682f810 | 2008-04-25 17:21:40 +0000 | [diff] [blame] | 52 | for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){ |
Dan Gohman | 4893c06 | 2008-09-21 21:01:49 +0000 | [diff] [blame] | 53 | const TargetRegisterClass* RC = *I; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 54 | if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) && |
Dan Gohman | 4893c06 | 2008-09-21 21:01:49 +0000 | [diff] [blame] | 55 | (!BestRC || BestRC->hasSuperClass(RC))) |
| 56 | BestRC = RC; |
Evan Cheng | fa2f786 | 2008-03-11 07:54:14 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Dan Gohman | 4893c06 | 2008-09-21 21:01:49 +0000 | [diff] [blame] | 59 | assert(BestRC && "Couldn't find the register class"); |
| 60 | return BestRC; |
Evan Cheng | 42d6027 | 2007-09-26 21:36:17 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Rafael Espindola | ce48c1d | 2010-06-02 12:39:06 +0000 | [diff] [blame] | 63 | /// getMinimalPhysRegClass - Returns the Register Class of a physical |
Dan Gohman | c2af869 | 2010-07-06 15:31:55 +0000 | [diff] [blame] | 64 | /// register of the given type, picking the most sub register class of |
| 65 | /// the right type that contains this physreg. |
Rafael Espindola | ce48c1d | 2010-06-02 12:39:06 +0000 | [diff] [blame] | 66 | const TargetRegisterClass * |
Rafael Espindola | d31f972 | 2010-06-29 14:02:34 +0000 | [diff] [blame] | 67 | TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, EVT VT) const { |
Rafael Espindola | ce48c1d | 2010-06-02 12:39:06 +0000 | [diff] [blame] | 68 | assert(isPhysicalRegister(reg) && "reg must be a physical register"); |
| 69 | |
| 70 | // Pick the most sub register class of the right type that contains |
| 71 | // this physreg. |
| 72 | const TargetRegisterClass* BestRC = 0; |
| 73 | for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){ |
| 74 | const TargetRegisterClass* RC = *I; |
Rafael Espindola | d31f972 | 2010-06-29 14:02:34 +0000 | [diff] [blame] | 75 | if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) && |
| 76 | (!BestRC || BestRC->hasSubClass(RC))) |
Rafael Espindola | ce48c1d | 2010-06-02 12:39:06 +0000 | [diff] [blame] | 77 | BestRC = RC; |
| 78 | } |
| 79 | |
| 80 | assert(BestRC && "Couldn't find the register class"); |
| 81 | return BestRC; |
| 82 | } |
| 83 | |
Evan Cheng | 7be6368 | 2007-04-17 23:33:39 +0000 | [diff] [blame] | 84 | /// getAllocatableSetForRC - Toggle the bits that represent allocatable |
| 85 | /// registers for the specific register class. |
Dan Gohman | 769b7f8 | 2009-10-09 22:09:05 +0000 | [diff] [blame] | 86 | static void getAllocatableSetForRC(const MachineFunction &MF, |
Evan Cheng | 7be6368 | 2007-04-17 23:33:39 +0000 | [diff] [blame] | 87 | const TargetRegisterClass *RC, BitVector &R){ |
| 88 | for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF), |
| 89 | E = RC->allocation_order_end(MF); I != E; ++I) |
| 90 | R.set(*I); |
| 91 | } |
| 92 | |
Dan Gohman | 769b7f8 | 2009-10-09 22:09:05 +0000 | [diff] [blame] | 93 | BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 94 | const TargetRegisterClass *RC) const { |
Evan Cheng | 61de82d | 2007-02-15 05:59:24 +0000 | [diff] [blame] | 95 | BitVector Allocatable(NumRegs); |
Evan Cheng | 7be6368 | 2007-04-17 23:33:39 +0000 | [diff] [blame] | 96 | if (RC) { |
| 97 | getAllocatableSetForRC(MF, RC, Allocatable); |
| 98 | return Allocatable; |
Alkis Evlogimenos | bb4bdf4 | 2004-08-26 22:21:04 +0000 | [diff] [blame] | 99 | } |
Evan Cheng | 7be6368 | 2007-04-17 23:33:39 +0000 | [diff] [blame] | 100 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 101 | for (TargetRegisterInfo::regclass_iterator I = regclass_begin(), |
Evan Cheng | 7be6368 | 2007-04-17 23:33:39 +0000 | [diff] [blame] | 102 | E = regclass_end(); I != E; ++I) |
| 103 | getAllocatableSetForRC(MF, *I, Allocatable); |
Alkis Evlogimenos | bb4bdf4 | 2004-08-26 22:21:04 +0000 | [diff] [blame] | 104 | return Allocatable; |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 105 | } |
Jim Laskey | a997918 | 2006-03-28 13:48:33 +0000 | [diff] [blame] | 106 | |
Evan Cheng | 72bebb9 | 2008-01-31 03:37:28 +0000 | [diff] [blame] | 107 | /// getFrameIndexOffset - Returns the displacement from the frame register to |
| 108 | /// the stack frame of the specified index. This is the default implementation |
Bob Wilson | 959b002 | 2009-09-23 20:57:02 +0000 | [diff] [blame] | 109 | /// which is overridden for some targets. |
Chris Lattner | 30c6b75 | 2010-01-26 23:15:09 +0000 | [diff] [blame] | 110 | int TargetRegisterInfo::getFrameIndexOffset(const MachineFunction &MF, |
| 111 | int FI) const { |
Jim Laskey | 6b59a36 | 2006-08-03 17:27:09 +0000 | [diff] [blame] | 112 | const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo(); |
Chris Lattner | 30c6b75 | 2010-01-26 23:15:09 +0000 | [diff] [blame] | 113 | const MachineFrameInfo *MFI = MF.getFrameInfo(); |
Evan Cheng | 72bebb9 | 2008-01-31 03:37:28 +0000 | [diff] [blame] | 114 | return MFI->getObjectOffset(FI) + MFI->getStackSize() - |
| 115 | TFI.getOffsetOfLocalArea() + MFI->getOffsetAdjustment(); |
Jim Laskey | a997918 | 2006-03-28 13:48:33 +0000 | [diff] [blame] | 116 | } |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 117 | |
| 118 | /// getInitialFrameState - Returns a list of machine moves that are assumed |
| 119 | /// on entry to a function. |
| 120 | void |
Chris Lattner | 30c6b75 | 2010-01-26 23:15:09 +0000 | [diff] [blame] | 121 | TargetRegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves) const{ |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 122 | // Default is to do nothing. |
| 123 | } |
| 124 | |
Jakob Stoklund Olesen | ba67d87 | 2009-04-30 21:23:32 +0000 | [diff] [blame] | 125 | const TargetRegisterClass * |
| 126 | llvm::getCommonSubClass(const TargetRegisterClass *A, |
| 127 | const TargetRegisterClass *B) { |
| 128 | // First take care of the trivial cases |
| 129 | if (A == B) |
| 130 | return A; |
| 131 | if (!A || !B) |
| 132 | return 0; |
| 133 | |
| 134 | // If B is a subclass of A, it will be handled in the loop below |
| 135 | if (B->hasSubClass(A)) |
| 136 | return A; |
| 137 | |
| 138 | const TargetRegisterClass *Best = 0; |
| 139 | for (TargetRegisterClass::sc_iterator I = A->subclasses_begin(); |
| 140 | const TargetRegisterClass *X = *I; ++I) { |
| 141 | if (X == B) |
| 142 | return B; // B is a subclass of A |
| 143 | |
| 144 | // X must be a common subclass of A and B |
| 145 | if (!B->hasSubClass(X)) |
| 146 | continue; |
| 147 | |
| 148 | // A superclass is definitely better. |
| 149 | if (!Best || Best->hasSuperClass(X)) { |
| 150 | Best = X; |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | // A subclass is definitely worse |
| 155 | if (Best->hasSubClass(X)) |
| 156 | continue; |
| 157 | |
| 158 | // Best and *I have no super/sub class relation - pick the larger class, or |
| 159 | // the smaller spill size. |
| 160 | int nb = std::distance(Best->begin(), Best->end()); |
| 161 | int ni = std::distance(X->begin(), X->end()); |
| 162 | if (ni>nb || (ni==nb && X->getSize() < Best->getSize())) |
| 163 | Best = X; |
| 164 | } |
| 165 | return Best; |
| 166 | } |