X86RegisterInfo.td revision ca561ffcf320e9dbfafcac5efcee81471f3259c3
1//===- X86RegisterInfo.td - Describe the X86 Register File --*- tablegen -*-==//
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 describes the X86 Register file, defining the registers themselves,
11// aliases between the registers, and the register classes built out of the
12// registers.
13//
14//===----------------------------------------------------------------------===//
15
16//===----------------------------------------------------------------------===//
17//  Register definitions...
18//
19let Namespace = "X86" in {
20
21  // Subregister indices.
22  def sub_8bit    : SubRegIndex;
23  def sub_8bit_hi : SubRegIndex;
24  def sub_16bit   : SubRegIndex;
25  def sub_32bit   : SubRegIndex;
26
27  def sub_ss  : SubRegIndex;
28  def sub_sd  : SubRegIndex;
29  def sub_xmm : SubRegIndex;
30
31
32  // In the register alias definitions below, we define which registers alias
33  // which others.  We only specify which registers the small registers alias,
34  // because the register file generator is smart enough to figure out that
35  // AL aliases AX if we tell it that AX aliased AL (for example).
36
37  // Dwarf numbering is different for 32-bit and 64-bit, and there are 
38  // variations by target as well. Currently the first entry is for X86-64, 
39  // second - for EH on X86-32/Darwin and third is 'generic' one (X86-32/Linux
40  // and debug information on X86-32/Darwin)
41
42  // 8-bit registers
43  // Low registers
44  def AL : Register<"al">, DwarfRegNum<[0, 0, 0]>;
45  def DL : Register<"dl">, DwarfRegNum<[1, 2, 2]>;
46  def CL : Register<"cl">, DwarfRegNum<[2, 1, 1]>;
47  def BL : Register<"bl">, DwarfRegNum<[3, 3, 3]>;
48
49  // X86-64 only
50  def SIL : Register<"sil">, DwarfRegNum<[4, 6, 6]>;
51  def DIL : Register<"dil">, DwarfRegNum<[5, 7, 7]>;
52  def BPL : Register<"bpl">, DwarfRegNum<[6, 4, 5]>;
53  def SPL : Register<"spl">, DwarfRegNum<[7, 5, 4]>;
54  def R8B  : Register<"r8b">,  DwarfRegNum<[8, -2, -2]>;
55  def R9B  : Register<"r9b">,  DwarfRegNum<[9, -2, -2]>;
56  def R10B : Register<"r10b">, DwarfRegNum<[10, -2, -2]>;
57  def R11B : Register<"r11b">, DwarfRegNum<[11, -2, -2]>;
58  def R12B : Register<"r12b">, DwarfRegNum<[12, -2, -2]>;
59  def R13B : Register<"r13b">, DwarfRegNum<[13, -2, -2]>;
60  def R14B : Register<"r14b">, DwarfRegNum<[14, -2, -2]>;
61  def R15B : Register<"r15b">, DwarfRegNum<[15, -2, -2]>;
62
63  // High registers. On x86-64, these cannot be used in any instruction
64  // with a REX prefix.
65  def AH : Register<"ah">, DwarfRegNum<[0, 0, 0]>;
66  def DH : Register<"dh">, DwarfRegNum<[1, 2, 2]>;
67  def CH : Register<"ch">, DwarfRegNum<[2, 1, 1]>;
68  def BH : Register<"bh">, DwarfRegNum<[3, 3, 3]>;
69
70  // 16-bit registers
71  let SubRegIndices = [sub_8bit, sub_8bit_hi] in {
72  def AX : RegisterWithSubRegs<"ax", [AL,AH]>, DwarfRegNum<[0, 0, 0]>;
73  def DX : RegisterWithSubRegs<"dx", [DL,DH]>, DwarfRegNum<[1, 2, 2]>;
74  def CX : RegisterWithSubRegs<"cx", [CL,CH]>, DwarfRegNum<[2, 1, 1]>;
75  def BX : RegisterWithSubRegs<"bx", [BL,BH]>, DwarfRegNum<[3, 3, 3]>;
76  }
77  let SubRegIndices = [sub_8bit] in {
78  def SI : RegisterWithSubRegs<"si", [SIL]>, DwarfRegNum<[4, 6, 6]>;
79  def DI : RegisterWithSubRegs<"di", [DIL]>, DwarfRegNum<[5, 7, 7]>;
80  def BP : RegisterWithSubRegs<"bp", [BPL]>, DwarfRegNum<[6, 4, 5]>;
81  def SP : RegisterWithSubRegs<"sp", [SPL]>, DwarfRegNum<[7, 5, 4]>;
82  }
83  def IP : Register<"ip">, DwarfRegNum<[16]>;
84  
85  // X86-64 only
86  let SubRegIndices = [sub_8bit] in {
87  def R8W  : RegisterWithSubRegs<"r8w", [R8B]>, DwarfRegNum<[8, -2, -2]>;
88  def R9W  : RegisterWithSubRegs<"r9w", [R9B]>, DwarfRegNum<[9, -2, -2]>;
89  def R10W : RegisterWithSubRegs<"r10w", [R10B]>, DwarfRegNum<[10, -2, -2]>;
90  def R11W : RegisterWithSubRegs<"r11w", [R11B]>, DwarfRegNum<[11, -2, -2]>;
91  def R12W : RegisterWithSubRegs<"r12w", [R12B]>, DwarfRegNum<[12, -2, -2]>;
92  def R13W : RegisterWithSubRegs<"r13w", [R13B]>, DwarfRegNum<[13, -2, -2]>;
93  def R14W : RegisterWithSubRegs<"r14w", [R14B]>, DwarfRegNum<[14, -2, -2]>;
94  def R15W : RegisterWithSubRegs<"r15w", [R15B]>, DwarfRegNum<[15, -2, -2]>;
95  }
96  // 32-bit registers
97  let SubRegIndices = [sub_16bit] in {
98  def EAX : RegisterWithSubRegs<"eax", [AX]>, DwarfRegNum<[0, 0, 0]>;
99  def EDX : RegisterWithSubRegs<"edx", [DX]>, DwarfRegNum<[1, 2, 2]>;
100  def ECX : RegisterWithSubRegs<"ecx", [CX]>, DwarfRegNum<[2, 1, 1]>;
101  def EBX : RegisterWithSubRegs<"ebx", [BX]>, DwarfRegNum<[3, 3, 3]>;
102  def ESI : RegisterWithSubRegs<"esi", [SI]>, DwarfRegNum<[4, 6, 6]>;
103  def EDI : RegisterWithSubRegs<"edi", [DI]>, DwarfRegNum<[5, 7, 7]>;
104  def EBP : RegisterWithSubRegs<"ebp", [BP]>, DwarfRegNum<[6, 4, 5]>;
105  def ESP : RegisterWithSubRegs<"esp", [SP]>, DwarfRegNum<[7, 5, 4]>;
106  def EIP : RegisterWithSubRegs<"eip", [IP]>, DwarfRegNum<[16, 8, 8]>;  
107  
108  // X86-64 only
109  def R8D  : RegisterWithSubRegs<"r8d", [R8W]>, DwarfRegNum<[8, -2, -2]>;
110  def R9D  : RegisterWithSubRegs<"r9d", [R9W]>, DwarfRegNum<[9, -2, -2]>;
111  def R10D : RegisterWithSubRegs<"r10d", [R10W]>, DwarfRegNum<[10, -2, -2]>;
112  def R11D : RegisterWithSubRegs<"r11d", [R11W]>, DwarfRegNum<[11, -2, -2]>;
113  def R12D : RegisterWithSubRegs<"r12d", [R12W]>, DwarfRegNum<[12, -2, -2]>;
114  def R13D : RegisterWithSubRegs<"r13d", [R13W]>, DwarfRegNum<[13, -2, -2]>;
115  def R14D : RegisterWithSubRegs<"r14d", [R14W]>, DwarfRegNum<[14, -2, -2]>;
116  def R15D : RegisterWithSubRegs<"r15d", [R15W]>, DwarfRegNum<[15, -2, -2]>;
117  }
118
119  // 64-bit registers, X86-64 only
120  let SubRegIndices = [sub_32bit] in {
121  def RAX : RegisterWithSubRegs<"rax", [EAX]>, DwarfRegNum<[0, -2, -2]>;
122  def RDX : RegisterWithSubRegs<"rdx", [EDX]>, DwarfRegNum<[1, -2, -2]>;
123  def RCX : RegisterWithSubRegs<"rcx", [ECX]>, DwarfRegNum<[2, -2, -2]>;
124  def RBX : RegisterWithSubRegs<"rbx", [EBX]>, DwarfRegNum<[3, -2, -2]>;
125  def RSI : RegisterWithSubRegs<"rsi", [ESI]>, DwarfRegNum<[4, -2, -2]>;
126  def RDI : RegisterWithSubRegs<"rdi", [EDI]>, DwarfRegNum<[5, -2, -2]>;
127  def RBP : RegisterWithSubRegs<"rbp", [EBP]>, DwarfRegNum<[6, -2, -2]>;
128  def RSP : RegisterWithSubRegs<"rsp", [ESP]>, DwarfRegNum<[7, -2, -2]>;
129
130  def R8  : RegisterWithSubRegs<"r8", [R8D]>, DwarfRegNum<[8, -2, -2]>;
131  def R9  : RegisterWithSubRegs<"r9", [R9D]>, DwarfRegNum<[9, -2, -2]>;
132  def R10 : RegisterWithSubRegs<"r10", [R10D]>, DwarfRegNum<[10, -2, -2]>;
133  def R11 : RegisterWithSubRegs<"r11", [R11D]>, DwarfRegNum<[11, -2, -2]>;
134  def R12 : RegisterWithSubRegs<"r12", [R12D]>, DwarfRegNum<[12, -2, -2]>;
135  def R13 : RegisterWithSubRegs<"r13", [R13D]>, DwarfRegNum<[13, -2, -2]>;
136  def R14 : RegisterWithSubRegs<"r14", [R14D]>, DwarfRegNum<[14, -2, -2]>;
137  def R15 : RegisterWithSubRegs<"r15", [R15D]>, DwarfRegNum<[15, -2, -2]>;
138  def RIP : RegisterWithSubRegs<"rip", [EIP]>,  DwarfRegNum<[16, -2, -2]>;
139  }
140
141  // MMX Registers. These are actually aliased to ST0 .. ST7
142  def MM0 : Register<"mm0">, DwarfRegNum<[41, 29, 29]>;
143  def MM1 : Register<"mm1">, DwarfRegNum<[42, 30, 30]>;
144  def MM2 : Register<"mm2">, DwarfRegNum<[43, 31, 31]>;
145  def MM3 : Register<"mm3">, DwarfRegNum<[44, 32, 32]>;
146  def MM4 : Register<"mm4">, DwarfRegNum<[45, 33, 33]>;
147  def MM5 : Register<"mm5">, DwarfRegNum<[46, 34, 34]>;
148  def MM6 : Register<"mm6">, DwarfRegNum<[47, 35, 35]>;
149  def MM7 : Register<"mm7">, DwarfRegNum<[48, 36, 36]>;
150  
151  // Pseudo Floating Point registers
152  def FP0 : Register<"fp0">;
153  def FP1 : Register<"fp1">;
154  def FP2 : Register<"fp2">;
155  def FP3 : Register<"fp3">;
156  def FP4 : Register<"fp4">;
157  def FP5 : Register<"fp5">;
158  def FP6 : Register<"fp6">; 
159
160  // XMM Registers, used by the various SSE instruction set extensions.
161  // The sub_ss and sub_sd subregs are the same registers with another regclass.
162  let CompositeIndices = [(sub_ss), (sub_sd)] in {
163  def XMM0: Register<"xmm0">, DwarfRegNum<[17, 21, 21]>;
164  def XMM1: Register<"xmm1">, DwarfRegNum<[18, 22, 22]>;
165  def XMM2: Register<"xmm2">, DwarfRegNum<[19, 23, 23]>;
166  def XMM3: Register<"xmm3">, DwarfRegNum<[20, 24, 24]>;
167  def XMM4: Register<"xmm4">, DwarfRegNum<[21, 25, 25]>;
168  def XMM5: Register<"xmm5">, DwarfRegNum<[22, 26, 26]>;
169  def XMM6: Register<"xmm6">, DwarfRegNum<[23, 27, 27]>;
170  def XMM7: Register<"xmm7">, DwarfRegNum<[24, 28, 28]>;
171
172  // X86-64 only
173  def XMM8:  Register<"xmm8">,  DwarfRegNum<[25, -2, -2]>;
174  def XMM9:  Register<"xmm9">,  DwarfRegNum<[26, -2, -2]>;
175  def XMM10: Register<"xmm10">, DwarfRegNum<[27, -2, -2]>;
176  def XMM11: Register<"xmm11">, DwarfRegNum<[28, -2, -2]>;
177  def XMM12: Register<"xmm12">, DwarfRegNum<[29, -2, -2]>;
178  def XMM13: Register<"xmm13">, DwarfRegNum<[30, -2, -2]>;
179  def XMM14: Register<"xmm14">, DwarfRegNum<[31, -2, -2]>;
180  def XMM15: Register<"xmm15">, DwarfRegNum<[32, -2, -2]>;
181  }
182
183  // YMM Registers, used by AVX instructions
184  let SubRegIndices = [sub_xmm] in {
185  def YMM0: RegisterWithSubRegs<"ymm0", [XMM0]>, DwarfRegNum<[17, 21, 21]>;
186  def YMM1: RegisterWithSubRegs<"ymm1", [XMM1]>, DwarfRegNum<[18, 22, 22]>;
187  def YMM2: RegisterWithSubRegs<"ymm2", [XMM2]>, DwarfRegNum<[19, 23, 23]>;
188  def YMM3: RegisterWithSubRegs<"ymm3", [XMM3]>, DwarfRegNum<[20, 24, 24]>;
189  def YMM4: RegisterWithSubRegs<"ymm4", [XMM4]>, DwarfRegNum<[21, 25, 25]>;
190  def YMM5: RegisterWithSubRegs<"ymm5", [XMM5]>, DwarfRegNum<[22, 26, 26]>;
191  def YMM6: RegisterWithSubRegs<"ymm6", [XMM6]>, DwarfRegNum<[23, 27, 27]>;
192  def YMM7: RegisterWithSubRegs<"ymm7", [XMM7]>, DwarfRegNum<[24, 28, 28]>;
193  def YMM8:  RegisterWithSubRegs<"ymm8", [XMM8]>,  DwarfRegNum<[25, -2, -2]>;
194  def YMM9:  RegisterWithSubRegs<"ymm9", [XMM9]>,  DwarfRegNum<[26, -2, -2]>;
195  def YMM10: RegisterWithSubRegs<"ymm10", [XMM10]>, DwarfRegNum<[27, -2, -2]>;
196  def YMM11: RegisterWithSubRegs<"ymm11", [XMM11]>, DwarfRegNum<[28, -2, -2]>;
197  def YMM12: RegisterWithSubRegs<"ymm12", [XMM12]>, DwarfRegNum<[29, -2, -2]>;
198  def YMM13: RegisterWithSubRegs<"ymm13", [XMM13]>, DwarfRegNum<[30, -2, -2]>;
199  def YMM14: RegisterWithSubRegs<"ymm14", [XMM14]>, DwarfRegNum<[31, -2, -2]>;
200  def YMM15: RegisterWithSubRegs<"ymm15", [XMM15]>, DwarfRegNum<[32, -2, -2]>;
201  }
202
203  // Floating point stack registers
204  def ST0 : Register<"st(0)">, DwarfRegNum<[33, 12, 11]>;
205  def ST1 : Register<"st(1)">, DwarfRegNum<[34, 13, 12]>;
206  def ST2 : Register<"st(2)">, DwarfRegNum<[35, 14, 13]>;
207  def ST3 : Register<"st(3)">, DwarfRegNum<[36, 15, 14]>;
208  def ST4 : Register<"st(4)">, DwarfRegNum<[37, 16, 15]>;
209  def ST5 : Register<"st(5)">, DwarfRegNum<[38, 17, 16]>;
210  def ST6 : Register<"st(6)">, DwarfRegNum<[39, 18, 17]>;
211  def ST7 : Register<"st(7)">, DwarfRegNum<[40, 19, 18]>; 
212
213  // Status flags register
214  def EFLAGS : Register<"flags">;
215
216  // Segment registers
217  def CS : Register<"cs">;
218  def DS : Register<"ds">;
219  def SS : Register<"ss">;
220  def ES : Register<"es">;
221  def FS : Register<"fs">;
222  def GS : Register<"gs">;
223  
224  // Debug registers
225  def DR0 : Register<"dr0">;
226  def DR1 : Register<"dr1">;
227  def DR2 : Register<"dr2">;
228  def DR3 : Register<"dr3">;
229  def DR4 : Register<"dr4">;
230  def DR5 : Register<"dr5">;
231  def DR6 : Register<"dr6">;
232  def DR7 : Register<"dr7">;
233  
234  // Condition registers
235  def CR0 : Register<"cr0">;
236  def CR1 : Register<"cr1">;
237  def CR2 : Register<"cr2">;
238  def CR3 : Register<"cr3">;
239  def CR4 : Register<"cr4">;
240  def CR5 : Register<"cr5">;
241  def CR6 : Register<"cr6">;
242  def CR7 : Register<"cr7">;
243  def CR8 : Register<"cr8">;
244}
245
246
247//===----------------------------------------------------------------------===//
248// Register Class Definitions... now that we have all of the pieces, define the
249// top-level register classes.  The order specified in the register list is
250// implicitly defined to be the register allocation order.
251//
252
253// List call-clobbered registers before callee-save registers. RBX, RBP, (and 
254// R12, R13, R14, and R15 for X86-64) are callee-save registers.
255// In 64-mode, there are 12 additional i8 registers, SIL, DIL, BPL, SPL, and
256// R8B, ... R15B. 
257// Allocate R12 and R13 last, as these require an extra byte when
258// encoded in x86_64 instructions.
259// FIXME: Allow AH, CH, DH, BH to be used as general-purpose registers in
260// 64-bit mode. The main complication is that they cannot be encoded in an
261// instruction requiring a REX prefix, while SIL, DIL, BPL, R8D, etc.
262// require a REX prefix. For example, "addb %ah, %dil" and "movzbl %ah, %r8d"
263// cannot be encoded.
264def GR8 : RegisterClass<"X86", [i8],  8,
265                        [AL, CL, DL, AH, CH, DH, BL, BH, SIL, DIL, BPL, SPL,
266                         R8B, R9B, R10B, R11B, R14B, R15B, R12B, R13B]> {
267  let MethodProtos = [{
268    iterator allocation_order_begin(const MachineFunction &MF) const;
269    iterator allocation_order_end(const MachineFunction &MF) const;
270  }];
271  let MethodBodies = [{
272    static const unsigned X86_GR8_AO_64[] = {
273      X86::AL,   X86::CL,   X86::DL,   X86::SIL, X86::DIL,
274      X86::R8B,  X86::R9B,  X86::R10B, X86::R11B,
275      X86::BL,   X86::R14B, X86::R15B, X86::R12B, X86::R13B, X86::BPL
276    };
277
278    GR8Class::iterator
279    GR8Class::allocation_order_begin(const MachineFunction &MF) const {
280      const TargetMachine &TM = MF.getTarget();
281      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
282      if (Subtarget.is64Bit())
283        return X86_GR8_AO_64;
284      else
285        return begin();
286    }
287
288    GR8Class::iterator
289    GR8Class::allocation_order_end(const MachineFunction &MF) const {
290      const TargetMachine &TM = MF.getTarget();
291      const TargetRegisterInfo *RI = TM.getRegisterInfo();
292      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
293      const X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
294      // Does the function dedicate RBP / EBP to being a frame ptr?
295      if (!Subtarget.is64Bit())
296        // In 32-mode, none of the 8-bit registers aliases EBP or ESP.
297        return begin() + 8;
298      else if (RI->hasFP(MF) || MFI->getReserveFP())
299        // If so, don't allocate SPL or BPL.
300        return array_endof(X86_GR8_AO_64) - 1;
301      else
302        // If not, just don't allocate SPL.
303        return array_endof(X86_GR8_AO_64);
304    }
305  }];
306}
307
308def GR16 : RegisterClass<"X86", [i16], 16,
309                         [AX, CX, DX, SI, DI, BX, BP, SP,
310                          R8W, R9W, R10W, R11W, R14W, R15W, R12W, R13W]> {
311  let SubRegClasses = [(GR8 sub_8bit, sub_8bit_hi)];
312  let MethodProtos = [{
313    iterator allocation_order_begin(const MachineFunction &MF) const;
314    iterator allocation_order_end(const MachineFunction &MF) const;
315  }];
316  let MethodBodies = [{
317    static const unsigned X86_GR16_AO_64[] = {
318      X86::AX,  X86::CX,   X86::DX,   X86::SI,   X86::DI,
319      X86::R8W, X86::R9W,  X86::R10W, X86::R11W,
320      X86::BX, X86::R14W, X86::R15W,  X86::R12W, X86::R13W, X86::BP
321    };
322
323    GR16Class::iterator
324    GR16Class::allocation_order_begin(const MachineFunction &MF) const {
325      const TargetMachine &TM = MF.getTarget();
326      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
327      if (Subtarget.is64Bit())
328        return X86_GR16_AO_64;
329      else
330        return begin();
331    }
332
333    GR16Class::iterator
334    GR16Class::allocation_order_end(const MachineFunction &MF) const {
335      const TargetMachine &TM = MF.getTarget();
336      const TargetRegisterInfo *RI = TM.getRegisterInfo();
337      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
338      const X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
339      if (Subtarget.is64Bit()) {
340        // Does the function dedicate RBP to being a frame ptr?
341        if (RI->hasFP(MF) || MFI->getReserveFP())
342          // If so, don't allocate SP or BP.
343          return array_endof(X86_GR16_AO_64) - 1;
344        else
345          // If not, just don't allocate SP.
346          return array_endof(X86_GR16_AO_64);
347      } else {
348        // Does the function dedicate EBP to being a frame ptr?
349        if (RI->hasFP(MF) || MFI->getReserveFP())
350          // If so, don't allocate SP or BP.
351          return begin() + 6;
352        else
353          // If not, just don't allocate SP.
354          return begin() + 7;
355      }
356    }
357  }];
358}
359
360def GR32 : RegisterClass<"X86", [i32], 32, 
361                         [EAX, ECX, EDX, ESI, EDI, EBX, EBP, ESP,
362                          R8D, R9D, R10D, R11D, R14D, R15D, R12D, R13D]> {
363  let SubRegClasses = [(GR8 sub_8bit, sub_8bit_hi), (GR16 sub_16bit)];
364  let MethodProtos = [{
365    iterator allocation_order_begin(const MachineFunction &MF) const;
366    iterator allocation_order_end(const MachineFunction &MF) const;
367  }];
368  let MethodBodies = [{
369    static const unsigned X86_GR32_AO_64[] = {
370      X86::EAX, X86::ECX,  X86::EDX,  X86::ESI,  X86::EDI,
371      X86::R8D, X86::R9D,  X86::R10D, X86::R11D,
372      X86::EBX, X86::R14D, X86::R15D, X86::R12D, X86::R13D, X86::EBP
373    };
374
375    GR32Class::iterator
376    GR32Class::allocation_order_begin(const MachineFunction &MF) const {
377      const TargetMachine &TM = MF.getTarget();
378      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
379      if (Subtarget.is64Bit())
380        return X86_GR32_AO_64;
381      else
382        return begin();
383    }
384
385    GR32Class::iterator
386    GR32Class::allocation_order_end(const MachineFunction &MF) const {
387      const TargetMachine &TM = MF.getTarget();
388      const TargetRegisterInfo *RI = TM.getRegisterInfo();
389      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
390      const X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
391      if (Subtarget.is64Bit()) {
392        // Does the function dedicate RBP to being a frame ptr?
393        if (RI->hasFP(MF) || MFI->getReserveFP())
394          // If so, don't allocate ESP or EBP.
395          return array_endof(X86_GR32_AO_64) - 1;
396        else
397          // If not, just don't allocate ESP.
398          return array_endof(X86_GR32_AO_64);
399      } else {
400        // Does the function dedicate EBP to being a frame ptr?
401        if (RI->hasFP(MF) || MFI->getReserveFP())
402          // If so, don't allocate ESP or EBP.
403          return begin() + 6;
404        else
405          // If not, just don't allocate ESP.
406          return begin() + 7;
407      }
408    }
409  }];
410}
411
412// GR64 - 64-bit GPRs. This oddly includes RIP, which isn't accurate, since
413// RIP isn't really a register and it can't be used anywhere except in an
414// address, but it doesn't cause trouble.
415def GR64 : RegisterClass<"X86", [i64], 64, 
416                         [RAX, RCX, RDX, RSI, RDI, R8, R9, R10, R11,
417                          RBX, R14, R15, R12, R13, RBP, RSP, RIP]> {
418  let SubRegClasses = [(GR8 sub_8bit, sub_8bit_hi),
419                       (GR16 sub_16bit),
420                       (GR32 sub_32bit)];
421  let MethodProtos = [{
422    iterator allocation_order_end(const MachineFunction &MF) const;
423  }];
424  let MethodBodies = [{
425    GR64Class::iterator
426    GR64Class::allocation_order_end(const MachineFunction &MF) const {
427      const TargetMachine &TM = MF.getTarget();
428      const TargetRegisterInfo *RI = TM.getRegisterInfo();
429      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
430      const X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
431      if (!Subtarget.is64Bit())
432        return begin();  // None of these are allocatable in 32-bit.
433      // Does the function dedicate RBP to being a frame ptr?
434      if (RI->hasFP(MF) || MFI->getReserveFP())
435        return end()-3;  // If so, don't allocate RIP, RSP or RBP
436      else
437        return end()-2;  // If not, just don't allocate RIP or RSP
438    }
439  }];
440}
441
442// Segment registers for use by MOV instructions (and others) that have a
443//   segment register as one operand.  Always contain a 16-bit segment
444//   descriptor.
445def SEGMENT_REG : RegisterClass<"X86", [i16], 16, [CS, DS, SS, ES, FS, GS]> {
446}
447
448// Debug registers.
449def DEBUG_REG : RegisterClass<"X86", [i32], 32, 
450                              [DR0, DR1, DR2, DR3, DR4, DR5, DR6, DR7]> {
451}
452
453// Control registers.
454def CONTROL_REG : RegisterClass<"X86", [i64], 64,
455                                [CR0, CR1, CR2, CR3, CR4, CR5, CR6, CR7, CR8]> {
456}
457
458// GR8_ABCD_L, GR8_ABCD_H, GR16_ABCD, GR32_ABCD, GR64_ABCD - Subclasses of
459// GR8, GR16, GR32, and GR64 which contain just the "a" "b", "c", and "d"
460// registers. On x86-32, GR16_ABCD and GR32_ABCD are classes for registers
461// that support 8-bit subreg operations. On x86-64, GR16_ABCD, GR32_ABCD,
462// and GR64_ABCD are classes for registers that support 8-bit h-register
463// operations.
464def GR8_ABCD_L : RegisterClass<"X86", [i8], 8, [AL, CL, DL, BL]> {
465}
466def GR8_ABCD_H : RegisterClass<"X86", [i8], 8, [AH, CH, DH, BH]> {
467}
468def GR16_ABCD : RegisterClass<"X86", [i16], 16, [AX, CX, DX, BX]> {
469  let SubRegClasses = [(GR8_ABCD_L sub_8bit), (GR8_ABCD_H sub_8bit_hi)];
470}
471def GR32_ABCD : RegisterClass<"X86", [i32], 32, [EAX, ECX, EDX, EBX]> {
472  let SubRegClasses = [(GR8_ABCD_L sub_8bit),
473                       (GR8_ABCD_H sub_8bit_hi),
474                       (GR16_ABCD sub_16bit)];
475}
476def GR64_ABCD : RegisterClass<"X86", [i64], 64, [RAX, RCX, RDX, RBX]> {
477  let SubRegClasses = [(GR8_ABCD_L sub_8bit),
478                       (GR8_ABCD_H sub_8bit_hi),
479                       (GR16_ABCD sub_16bit),
480                       (GR32_ABCD sub_32bit)];
481}
482def GR32_TC   : RegisterClass<"X86", [i32], 32, [EAX, ECX, EDX]> {
483  let SubRegClasses = [(GR8 sub_8bit, sub_8bit_hi), (GR16 sub_16bit)];
484}
485def GR64_TC   : RegisterClass<"X86", [i64], 64, [RAX, RCX, RDX, RSI, RDI,
486                                                 R8, R9, R11]> {
487  let SubRegClasses = [(GR8 sub_8bit, sub_8bit_hi),
488                       (GR16 sub_16bit),
489                       (GR32_TC sub_32bit)];
490}
491
492// GR8_NOREX - GR8 registers which do not require a REX prefix.
493def GR8_NOREX : RegisterClass<"X86", [i8], 8,
494                              [AL, CL, DL, AH, CH, DH, BL, BH]> {
495  let MethodProtos = [{
496    iterator allocation_order_begin(const MachineFunction &MF) const;
497    iterator allocation_order_end(const MachineFunction &MF) const;
498  }];
499  let MethodBodies = [{
500    // In 64-bit mode, it's not safe to blindly allocate H registers.
501    static const unsigned X86_GR8_NOREX_AO_64[] = {
502      X86::AL, X86::CL, X86::DL, X86::BL
503    };
504
505    GR8_NOREXClass::iterator
506    GR8_NOREXClass::allocation_order_begin(const MachineFunction &MF) const {
507      const TargetMachine &TM = MF.getTarget();
508      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
509      if (Subtarget.is64Bit())
510        return X86_GR8_NOREX_AO_64;
511      else
512        return begin();
513    }
514
515    GR8_NOREXClass::iterator
516    GR8_NOREXClass::allocation_order_end(const MachineFunction &MF) const {
517      const TargetMachine &TM = MF.getTarget();
518      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
519      if (Subtarget.is64Bit())
520        return array_endof(X86_GR8_NOREX_AO_64);
521      else
522        return end();
523    }
524  }];
525}
526// GR16_NOREX - GR16 registers which do not require a REX prefix.
527def GR16_NOREX : RegisterClass<"X86", [i16], 16,
528                               [AX, CX, DX, SI, DI, BX, BP, SP]> {
529  let SubRegClasses = [(GR8_NOREX sub_8bit, sub_8bit_hi)];
530  let MethodProtos = [{
531    iterator allocation_order_end(const MachineFunction &MF) const;
532  }];
533  let MethodBodies = [{
534    GR16_NOREXClass::iterator
535    GR16_NOREXClass::allocation_order_end(const MachineFunction &MF) const {
536      const TargetMachine &TM = MF.getTarget();
537      const TargetRegisterInfo *RI = TM.getRegisterInfo();
538      const X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
539      // Does the function dedicate RBP / EBP to being a frame ptr?
540      if (RI->hasFP(MF) || MFI->getReserveFP())
541        // If so, don't allocate SP or BP.
542        return end() - 2;
543      else
544        // If not, just don't allocate SP.
545        return end() - 1;
546    }
547  }];
548}
549// GR32_NOREX - GR32 registers which do not require a REX prefix.
550def GR32_NOREX : RegisterClass<"X86", [i32], 32,
551                               [EAX, ECX, EDX, ESI, EDI, EBX, EBP, ESP]> {
552  let SubRegClasses = [(GR8_NOREX sub_8bit, sub_8bit_hi),
553                       (GR16_NOREX sub_16bit)];
554  let MethodProtos = [{
555    iterator allocation_order_end(const MachineFunction &MF) const;
556  }];
557  let MethodBodies = [{
558    GR32_NOREXClass::iterator
559    GR32_NOREXClass::allocation_order_end(const MachineFunction &MF) const {
560      const TargetMachine &TM = MF.getTarget();
561      const TargetRegisterInfo *RI = TM.getRegisterInfo();
562      const X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
563      // Does the function dedicate RBP / EBP to being a frame ptr?
564      if (RI->hasFP(MF) || MFI->getReserveFP())
565        // If so, don't allocate ESP or EBP.
566        return end() - 2;
567      else
568        // If not, just don't allocate ESP.
569        return end() - 1;
570    }
571  }];
572}
573// GR64_NOREX - GR64 registers which do not require a REX prefix.
574def GR64_NOREX : RegisterClass<"X86", [i64], 64,
575                               [RAX, RCX, RDX, RSI, RDI, RBX, RBP, RSP, RIP]> {
576  let SubRegClasses = [(GR8_NOREX sub_8bit, sub_8bit_hi),
577                       (GR16_NOREX sub_16bit),
578                       (GR32_NOREX sub_32bit)];
579  let MethodProtos = [{
580    iterator allocation_order_end(const MachineFunction &MF) const;
581  }];
582  let MethodBodies = [{
583    GR64_NOREXClass::iterator
584    GR64_NOREXClass::allocation_order_end(const MachineFunction &MF) const {
585      const TargetMachine &TM = MF.getTarget();
586      const TargetRegisterInfo *RI = TM.getRegisterInfo();
587      const X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
588      // Does the function dedicate RBP to being a frame ptr?
589      if (RI->hasFP(MF) || MFI->getReserveFP())
590        // If so, don't allocate RIP, RSP or RBP.
591        return end() - 3;
592      else
593        // If not, just don't allocate RIP or RSP.
594        return end() - 2;
595    }
596  }];
597}
598
599// GR32_NOSP - GR32 registers except ESP.
600def GR32_NOSP : RegisterClass<"X86", [i32], 32,
601                              [EAX, ECX, EDX, ESI, EDI, EBX, EBP,
602                               R8D, R9D, R10D, R11D, R14D, R15D, R12D, R13D]> {
603  let SubRegClasses = [(GR8 sub_8bit, sub_8bit_hi), (GR16 sub_16bit)];
604  let MethodProtos = [{
605    iterator allocation_order_begin(const MachineFunction &MF) const;
606    iterator allocation_order_end(const MachineFunction &MF) const;
607  }];
608  let MethodBodies = [{
609    static const unsigned X86_GR32_NOSP_AO_64[] = {
610      X86::EAX, X86::ECX,  X86::EDX,  X86::ESI,  X86::EDI,
611      X86::R8D, X86::R9D,  X86::R10D, X86::R11D,
612      X86::EBX, X86::R14D, X86::R15D, X86::R12D, X86::R13D, X86::EBP
613    };
614
615    GR32_NOSPClass::iterator
616    GR32_NOSPClass::allocation_order_begin(const MachineFunction &MF) const {
617      const TargetMachine &TM = MF.getTarget();
618      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
619      if (Subtarget.is64Bit())
620        return X86_GR32_NOSP_AO_64;
621      else
622        return begin();
623    }
624
625    GR32_NOSPClass::iterator
626    GR32_NOSPClass::allocation_order_end(const MachineFunction &MF) const {
627      const TargetMachine &TM = MF.getTarget();
628      const TargetRegisterInfo *RI = TM.getRegisterInfo();
629      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
630      const X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
631      if (Subtarget.is64Bit()) {
632        // Does the function dedicate RBP to being a frame ptr?
633        if (RI->hasFP(MF) || MFI->getReserveFP())
634          // If so, don't allocate EBP.
635          return array_endof(X86_GR32_NOSP_AO_64) - 1;
636        else
637          // If not, any reg in this class is ok.
638          return array_endof(X86_GR32_NOSP_AO_64);
639      } else {
640        // Does the function dedicate EBP to being a frame ptr?
641        if (RI->hasFP(MF) || MFI->getReserveFP())
642          // If so, don't allocate EBP.
643          return begin() + 6;
644        else
645          // If not, any reg in this class is ok.
646          return begin() + 7;
647      }
648    }
649  }];
650}
651
652// GR64_NOSP - GR64 registers except RSP (and RIP).
653def GR64_NOSP : RegisterClass<"X86", [i64], 64,
654                              [RAX, RCX, RDX, RSI, RDI, R8, R9, R10, R11,
655                               RBX, R14, R15, R12, R13, RBP]> {
656  let SubRegClasses = [(GR8 sub_8bit, sub_8bit_hi),
657                       (GR16 sub_16bit),
658                       (GR32_NOSP sub_32bit)];
659  let MethodProtos = [{
660    iterator allocation_order_end(const MachineFunction &MF) const;
661  }];
662  let MethodBodies = [{
663    GR64_NOSPClass::iterator
664    GR64_NOSPClass::allocation_order_end(const MachineFunction &MF) const {
665      const TargetMachine &TM = MF.getTarget();
666      const TargetRegisterInfo *RI = TM.getRegisterInfo();
667      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
668      const X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
669      if (!Subtarget.is64Bit())
670        return begin();  // None of these are allocatable in 32-bit.
671      // Does the function dedicate RBP to being a frame ptr?
672      if (RI->hasFP(MF) || MFI->getReserveFP())
673        return end()-1;  // If so, don't allocate RBP
674      else
675        return end();  // If not, any reg in this class is ok.
676    }
677  }];
678}
679
680// GR64_NOREX_NOSP - GR64_NOREX registers except RSP.
681def GR64_NOREX_NOSP : RegisterClass<"X86", [i64], 64,
682                                    [RAX, RCX, RDX, RSI, RDI, RBX, RBP]> {
683  let SubRegClasses = [(GR8_NOREX sub_8bit, sub_8bit_hi),
684                       (GR16_NOREX sub_16bit),
685                       (GR32_NOREX sub_32bit)];
686  let MethodProtos = [{
687    iterator allocation_order_end(const MachineFunction &MF) const;
688  }];
689  let MethodBodies = [{
690    GR64_NOREX_NOSPClass::iterator
691    GR64_NOREX_NOSPClass::allocation_order_end(const MachineFunction &MF) const
692  {
693      const TargetMachine &TM = MF.getTarget();
694      const TargetRegisterInfo *RI = TM.getRegisterInfo();
695      const X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
696      // Does the function dedicate RBP to being a frame ptr?
697      if (RI->hasFP(MF) || MFI->getReserveFP())
698        // If so, don't allocate RBP.
699        return end() - 1;
700      else
701        // If not, any reg in this class is ok.
702        return end();
703    }
704  }];
705}
706
707// A class to support the 'A' assembler constraint: EAX then EDX.
708def GR32_AD : RegisterClass<"X86", [i32], 32, [EAX, EDX]> {
709  let SubRegClasses = [(GR8_ABCD_L sub_8bit),
710                       (GR8_ABCD_H sub_8bit_hi),
711                       (GR16_ABCD sub_16bit)];
712}
713
714// Scalar SSE2 floating point registers.
715def FR32 : RegisterClass<"X86", [f32], 32,
716                         [XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
717                          XMM8, XMM9, XMM10, XMM11,
718                          XMM12, XMM13, XMM14, XMM15]> {
719  let MethodProtos = [{
720    iterator allocation_order_end(const MachineFunction &MF) const;
721  }];
722  let MethodBodies = [{
723    FR32Class::iterator
724    FR32Class::allocation_order_end(const MachineFunction &MF) const {
725      const TargetMachine &TM = MF.getTarget();
726      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
727      if (!Subtarget.is64Bit())
728        return end()-8; // Only XMM0 to XMM7 are available in 32-bit mode.
729      else
730        return end();
731    }
732  }];
733}
734
735def FR64 : RegisterClass<"X86", [f64], 64,
736                         [XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
737                          XMM8, XMM9, XMM10, XMM11,
738                          XMM12, XMM13, XMM14, XMM15]> {
739  let MethodProtos = [{
740    iterator allocation_order_end(const MachineFunction &MF) const;
741  }];
742  let MethodBodies = [{
743    FR64Class::iterator
744    FR64Class::allocation_order_end(const MachineFunction &MF) const {
745      const TargetMachine &TM = MF.getTarget();
746      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
747      if (!Subtarget.is64Bit())
748        return end()-8; // Only XMM0 to XMM7 are available in 32-bit mode.
749      else
750        return end();
751    }
752  }];
753}
754
755
756// FIXME: This sets up the floating point register files as though they are f64
757// values, though they really are f80 values.  This will cause us to spill
758// values as 64-bit quantities instead of 80-bit quantities, which is much much
759// faster on common hardware.  In reality, this should be controlled by a
760// command line option or something.
761
762def RFP32 : RegisterClass<"X86",[f32], 32, [FP0, FP1, FP2, FP3, FP4, FP5, FP6]>;
763def RFP64 : RegisterClass<"X86",[f64], 32, [FP0, FP1, FP2, FP3, FP4, FP5, FP6]>;
764def RFP80 : RegisterClass<"X86",[f80], 32, [FP0, FP1, FP2, FP3, FP4, FP5, FP6]>;
765
766// Floating point stack registers (these are not allocatable by the
767// register allocator - the floating point stackifier is responsible
768// for transforming FPn allocations to STn registers)
769def RST : RegisterClass<"X86", [f80, f64, f32], 32,
770                        [ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7]> {
771    let MethodProtos = [{
772    iterator allocation_order_end(const MachineFunction &MF) const;
773  }];
774  let MethodBodies = [{
775    RSTClass::iterator
776    RSTClass::allocation_order_end(const MachineFunction &MF) const {
777      return begin();
778    }
779  }];
780}
781
782// Generic vector registers: VR64 and VR128.
783def VR64  : RegisterClass<"X86", [v8i8, v4i16, v2i32, v1i64, v2f32], 64,
784                          [MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7]>;
785def VR128 : RegisterClass<"X86", [v16i8, v8i16, v4i32, v2i64, v4f32, v2f64],128,
786                          [XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
787                           XMM8, XMM9, XMM10, XMM11,
788                           XMM12, XMM13, XMM14, XMM15]> {
789  let SubRegClasses = [(FR32 sub_ss), (FR64 sub_sd)];
790  
791  let MethodProtos = [{
792    iterator allocation_order_end(const MachineFunction &MF) const;
793  }];
794  let MethodBodies = [{
795    VR128Class::iterator
796    VR128Class::allocation_order_end(const MachineFunction &MF) const {
797      const TargetMachine &TM = MF.getTarget();
798      const X86Subtarget &Subtarget = TM.getSubtarget<X86Subtarget>();
799      if (!Subtarget.is64Bit())
800        return end()-8; // Only XMM0 to XMM7 are available in 32-bit mode.
801      else
802        return end();
803    }
804  }];
805}
806def VR256 : RegisterClass<"X86", [ v8i32, v4i64, v8f32, v4f64],256,
807                          [YMM0, YMM1, YMM2, YMM3, YMM4, YMM5, YMM6, YMM7,
808                           YMM8, YMM9, YMM10, YMM11,
809                           YMM12, YMM13, YMM14, YMM15]> {
810  let SubRegClasses = [(FR32 sub_ss), (FR64 sub_sd), (VR128 sub_xmm)];
811}
812
813// Status flags registers.
814def CCR : RegisterClass<"X86", [i32], 32, [EFLAGS]> {
815  let CopyCost = -1;  // Don't allow copying of status registers.
816}
817