1//===-- PPCRegisterInfo.td - The PowerPC Register File -----*- tablegen -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//
10//===----------------------------------------------------------------------===//
11
12let Namespace = "PPC" in {
13def sub_lt : SubRegIndex<1>;
14def sub_gt : SubRegIndex<1, 1>;
15def sub_eq : SubRegIndex<1, 2>;
16def sub_un : SubRegIndex<1, 3>;
17def sub_32 : SubRegIndex<32>;
18def sub_64 : SubRegIndex<64>;
19}
20
21
22class PPCReg<string n> : Register<n> {
23  let Namespace = "PPC";
24}
25
26// We identify all our registers with a 5-bit ID, for consistency's sake.
27
28// GPR - One of the 32 32-bit general-purpose registers
29class GPR<bits<5> num, string n> : PPCReg<n> {
30  let HWEncoding{4-0} = num;
31}
32
33// GP8 - One of the 32 64-bit general-purpose registers
34class GP8<GPR SubReg, string n> : PPCReg<n> {
35  let HWEncoding = SubReg.HWEncoding;
36  let SubRegs = [SubReg];
37  let SubRegIndices = [sub_32];
38}
39
40// SPE - One of the 32 64-bit general-purpose registers (SPE)
41class SPE<GPR SubReg, string n> : PPCReg<n> {
42  let HWEncoding = SubReg.HWEncoding;
43  let SubRegs = [SubReg];
44  let SubRegIndices = [sub_32];
45}
46
47// SPR - One of the 32-bit special-purpose registers
48class SPR<bits<10> num, string n> : PPCReg<n> {
49  let HWEncoding{9-0} = num;
50}
51
52// FPR - One of the 32 64-bit floating-point registers
53class FPR<bits<5> num, string n> : PPCReg<n> {
54  let HWEncoding{4-0} = num;
55}
56
57// VF - One of the 32 64-bit floating-point subregisters of the vector
58// registers (used by VSX).
59class VF<bits<5> num, string n> : PPCReg<n> {
60  let HWEncoding{4-0} = num;
61  let HWEncoding{5} = 1;
62}
63
64// VR - One of the 32 128-bit vector registers
65class VR<VF SubReg, string n> : PPCReg<n> {
66  let HWEncoding{4-0} = SubReg.HWEncoding{4-0};
67  let HWEncoding{5} = 0;
68  let SubRegs = [SubReg];
69  let SubRegIndices = [sub_64];
70}
71
72// VSRL - One of the 32 128-bit VSX registers that overlap with the scalar
73// floating-point registers.
74class VSRL<FPR SubReg, string n> : PPCReg<n> {
75  let HWEncoding = SubReg.HWEncoding;
76  let SubRegs = [SubReg];
77  let SubRegIndices = [sub_64];
78}
79
80// VSXReg - One of the VSX registers in the range vs32-vs63 with numbering
81// and encoding to match.
82class VSXReg<bits<6> num, string n> : PPCReg<n> {
83  let HWEncoding{5-0} = num;
84}
85
86// CR - One of the 8 4-bit condition registers
87class CR<bits<3> num, string n, list<Register> subregs> : PPCReg<n> {
88  let HWEncoding{2-0} = num;
89  let SubRegs = subregs;
90}
91
92// CRBIT - One of the 32 1-bit condition register fields
93class CRBIT<bits<5> num, string n> : PPCReg<n> {
94  let HWEncoding{4-0} = num;
95}
96
97// General-purpose registers
98foreach Index = 0-31 in {
99  def R#Index : GPR<Index, "r"#Index>, DwarfRegNum<[-2, Index]>;
100}
101
102// 64-bit General-purpose registers
103foreach Index = 0-31 in {
104  def X#Index : GP8<!cast<GPR>("R"#Index), "r"#Index>,
105                    DwarfRegNum<[Index, -2]>;
106}
107
108// SPE registers
109foreach Index = 0-31 in {
110  def S#Index : SPE<!cast<GPR>("R"#Index), "r"#Index>,
111                    DwarfRegNum<[!add(Index, 1200), !add(Index, 1200)]>;
112}
113
114// Floating-point registers
115foreach Index = 0-31 in {
116  def F#Index : FPR<Index, "f"#Index>,
117                DwarfRegNum<[!add(Index, 32), !add(Index, 32)]>;
118}
119
120// 64-bit Floating-point subregisters of Altivec registers
121// Note: the register names are v0-v31 or vs32-vs63 depending on the use.
122//       Custom C++ code is used to produce the correct name and encoding.
123foreach Index = 0-31 in {
124  def VF#Index : VF<Index, "v" #Index>,
125                 DwarfRegNum<[!add(Index, 77), !add(Index, 77)]>;
126}
127
128// Vector registers
129foreach Index = 0-31 in {
130  def V#Index : VR<!cast<VF>("VF"#Index), "v"#Index>,
131                DwarfRegNum<[!add(Index, 77), !add(Index, 77)]>;
132}
133
134// VSX registers
135foreach Index = 0-31 in {
136  def VSL#Index : VSRL<!cast<FPR>("F"#Index), "vs"#Index>,
137                  DwarfRegAlias<!cast<FPR>("F"#Index)>;
138}
139
140// Dummy VSX registers, this defines string: "vs32"-"vs63", and is only used for
141// asm printing.
142foreach Index = 32-63 in {
143  def VSX#Index : VSXReg<Index, "vs"#Index>;
144}
145
146// The representation of r0 when treated as the constant 0.
147def ZERO  : GPR<0, "0">,    DwarfRegAlias<R0>;
148def ZERO8 : GP8<ZERO, "0">, DwarfRegAlias<X0>;
149
150// Representations of the frame pointer used by ISD::FRAMEADDR.
151def FP   : GPR<0 /* arbitrary */, "**FRAME POINTER**">;
152def FP8  : GP8<FP, "**FRAME POINTER**">;
153
154// Representations of the base pointer used by setjmp.
155def BP   : GPR<0 /* arbitrary */, "**BASE POINTER**">;
156def BP8  : GP8<BP, "**BASE POINTER**">;
157
158// Condition register bits
159def CR0LT : CRBIT< 0, "0">;
160def CR0GT : CRBIT< 1, "1">;
161def CR0EQ : CRBIT< 2, "2">;
162def CR0UN : CRBIT< 3, "3">;
163def CR1LT : CRBIT< 4, "4">;
164def CR1GT : CRBIT< 5, "5">;
165def CR1EQ : CRBIT< 6, "6">;
166def CR1UN : CRBIT< 7, "7">;
167def CR2LT : CRBIT< 8, "8">;
168def CR2GT : CRBIT< 9, "9">;
169def CR2EQ : CRBIT<10, "10">;
170def CR2UN : CRBIT<11, "11">;
171def CR3LT : CRBIT<12, "12">;
172def CR3GT : CRBIT<13, "13">;
173def CR3EQ : CRBIT<14, "14">;
174def CR3UN : CRBIT<15, "15">;
175def CR4LT : CRBIT<16, "16">;
176def CR4GT : CRBIT<17, "17">;
177def CR4EQ : CRBIT<18, "18">;
178def CR4UN : CRBIT<19, "19">;
179def CR5LT : CRBIT<20, "20">;
180def CR5GT : CRBIT<21, "21">;
181def CR5EQ : CRBIT<22, "22">;
182def CR5UN : CRBIT<23, "23">;
183def CR6LT : CRBIT<24, "24">;
184def CR6GT : CRBIT<25, "25">;
185def CR6EQ : CRBIT<26, "26">;
186def CR6UN : CRBIT<27, "27">;
187def CR7LT : CRBIT<28, "28">;
188def CR7GT : CRBIT<29, "29">;
189def CR7EQ : CRBIT<30, "30">;
190def CR7UN : CRBIT<31, "31">;
191
192// Condition registers
193let SubRegIndices = [sub_lt, sub_gt, sub_eq, sub_un] in {
194def CR0 : CR<0, "cr0", [CR0LT, CR0GT, CR0EQ, CR0UN]>, DwarfRegNum<[68, 68]>;
195def CR1 : CR<1, "cr1", [CR1LT, CR1GT, CR1EQ, CR1UN]>, DwarfRegNum<[69, 69]>;
196def CR2 : CR<2, "cr2", [CR2LT, CR2GT, CR2EQ, CR2UN]>, DwarfRegNum<[70, 70]>;
197def CR3 : CR<3, "cr3", [CR3LT, CR3GT, CR3EQ, CR3UN]>, DwarfRegNum<[71, 71]>;
198def CR4 : CR<4, "cr4", [CR4LT, CR4GT, CR4EQ, CR4UN]>, DwarfRegNum<[72, 72]>;
199def CR5 : CR<5, "cr5", [CR5LT, CR5GT, CR5EQ, CR5UN]>, DwarfRegNum<[73, 73]>;
200def CR6 : CR<6, "cr6", [CR6LT, CR6GT, CR6EQ, CR6UN]>, DwarfRegNum<[74, 74]>;
201def CR7 : CR<7, "cr7", [CR7LT, CR7GT, CR7EQ, CR7UN]>, DwarfRegNum<[75, 75]>;
202}
203
204// Link register
205def LR  : SPR<8, "lr">, DwarfRegNum<[-2, 65]>;
206//let Aliases = [LR] in
207def LR8 : SPR<8, "lr">, DwarfRegNum<[65, -2]>;
208
209// Count register
210def CTR  : SPR<9, "ctr">, DwarfRegNum<[-2, 66]>;
211def CTR8 : SPR<9, "ctr">, DwarfRegNum<[66, -2]>;
212
213// VRsave register
214def VRSAVE: SPR<256, "vrsave">, DwarfRegNum<[109]>;
215
216// SPE extra registers
217// SPE Accumulator for multiply-accumulate SPE operations.  Never directly
218// accessed, so there's no real encoding for it.
219def SPEACC: DwarfRegNum<[99, 111]>;
220def SPEFSCR: SPR<512, "spefscr">, DwarfRegNum<[612, 112]>;
221
222def XER: SPR<1, "xer">, DwarfRegNum<[76]>;
223
224// Carry bit.  In the architecture this is really bit 0 of the XER register
225// (which really is SPR register 1);  this is the only bit interesting to a
226// compiler.
227def CARRY: SPR<1, "xer">, DwarfRegNum<[76]> {
228  let Aliases = [XER];
229}
230
231// FP rounding mode:  bits 30 and 31 of the FP status and control register
232// This is not allocated as a normal register; it appears only in
233// Uses and Defs.  The ABI says it needs to be preserved by a function,
234// but this is not achieved by saving and restoring it as with
235// most registers, it has to be done in code; to make this work all the
236// return and call instructions are described as Uses of RM, so instructions
237// that do nothing but change RM will not get deleted.
238def RM: PPCReg<"**ROUNDING MODE**">;
239
240/// Register classes
241// Allocate volatiles first
242// then nonvolatiles in reverse order since stmw/lmw save from rN to r31
243def GPRC : RegisterClass<"PPC", [i32,f32], 32, (add (sequence "R%u", 2, 12),
244                                                    (sequence "R%u", 30, 13),
245                                                    R31, R0, R1, FP, BP)> {
246  // On non-Darwin PPC64 systems, R2 can be allocated, but must be restored, so
247  // put it at the end of the list.
248  let AltOrders = [(add (sub GPRC, R2), R2)];
249  let AltOrderSelect = [{
250    return MF.getSubtarget<PPCSubtarget>().is64BitELFABI();
251  }];
252}
253
254def G8RC : RegisterClass<"PPC", [i64], 64, (add (sequence "X%u", 2, 12),
255                                                (sequence "X%u", 30, 14),
256                                                X31, X13, X0, X1, FP8, BP8)> {
257  // On non-Darwin PPC64 systems, R2 can be allocated, but must be restored, so
258  // put it at the end of the list.
259  let AltOrders = [(add (sub G8RC, X2), X2)];
260  let AltOrderSelect = [{
261    return MF.getSubtarget<PPCSubtarget>().is64BitELFABI();
262  }];
263}
264
265// For some instructions r0 is special (representing the value 0 instead of
266// the value in the r0 register), and we use these register subclasses to
267// prevent r0 from being allocated for use by those instructions.
268def GPRC_NOR0 : RegisterClass<"PPC", [i32,f32], 32, (add (sub GPRC, R0), ZERO)> {
269  // On non-Darwin PPC64 systems, R2 can be allocated, but must be restored, so
270  // put it at the end of the list.
271  let AltOrders = [(add (sub GPRC_NOR0, R2), R2)];
272  let AltOrderSelect = [{
273    return MF.getSubtarget<PPCSubtarget>().is64BitELFABI();
274  }];
275}
276
277def G8RC_NOX0 : RegisterClass<"PPC", [i64], 64, (add (sub G8RC, X0), ZERO8)> {
278  // On non-Darwin PPC64 systems, R2 can be allocated, but must be restored, so
279  // put it at the end of the list.
280  let AltOrders = [(add (sub G8RC_NOX0, X2), X2)];
281  let AltOrderSelect = [{
282    return MF.getSubtarget<PPCSubtarget>().is64BitELFABI();
283  }];
284}
285
286def SPERC : RegisterClass<"PPC", [f64], 64, (add (sequence "S%u", 2, 12),
287                                                (sequence "S%u", 30, 13),
288                                                S31, S0, S1)>;
289
290// Allocate volatiles first, then non-volatiles in reverse order. With the SVR4
291// ABI the size of the Floating-point register save area is determined by the
292// allocated non-volatile register with the lowest register number, as FP
293// register N is spilled to offset 8 * (32 - N) below the back chain word of the
294// previous stack frame. By allocating non-volatiles in reverse order we make
295// sure that the Floating-point register save area is always as small as
296// possible because there aren't any unused spill slots.
297def F8RC : RegisterClass<"PPC", [f64], 64, (add (sequence "F%u", 0, 13),
298                                                (sequence "F%u", 31, 14))>;
299def F4RC : RegisterClass<"PPC", [f32], 32, (add F8RC)>;
300
301def VRRC : RegisterClass<"PPC",
302                         [v16i8,v8i16,v4i32,v2i64,v1i128,v4f32,v2f64, f128],
303                         128,
304                         (add V2, V3, V4, V5, V0, V1, V6, V7, V8, V9, V10, V11,
305                             V12, V13, V14, V15, V16, V17, V18, V19, V31, V30,
306                             V29, V28, V27, V26, V25, V24, V23, V22, V21, V20)>;
307
308// VSX register classes (the allocation order mirrors that of the corresponding
309// subregister classes).
310def VSLRC : RegisterClass<"PPC", [v4i32,v4f32,v2f64,v2i64], 128,
311                          (add (sequence "VSL%u", 0, 13),
312                               (sequence "VSL%u", 31, 14))>;
313def VSRC  : RegisterClass<"PPC", [v4i32,v4f32,v2f64,v2i64], 128,
314                          (add VSLRC, VRRC)>;
315
316// Register classes for the 64-bit "scalar" VSX subregisters.
317def VFRC :  RegisterClass<"PPC", [f64], 64,
318                          (add VF2, VF3, VF4, VF5, VF0, VF1, VF6, VF7,
319                               VF8, VF9, VF10, VF11, VF12, VF13, VF14,
320                               VF15, VF16, VF17, VF18, VF19, VF31, VF30,
321                               VF29, VF28, VF27, VF26, VF25, VF24, VF23,
322                               VF22, VF21, VF20)>;
323def VSFRC : RegisterClass<"PPC", [f64], 64, (add F8RC, VFRC)>;
324
325// Allow spilling GPR's into caller-saved VSR's.
326def SPILLTOVSRRC : RegisterClass<"PPC", [i64, f64], 64, (add G8RC, (sub VSFRC,
327				(sequence "VF%u", 31, 20),
328				(sequence "F%u", 31, 14)))>;
329
330// Register class for single precision scalars in VSX registers
331def VSSRC : RegisterClass<"PPC", [f32], 32, (add VSFRC)>;
332
333def CRBITRC : RegisterClass<"PPC", [i1], 32,
334  (add CR2LT, CR2GT, CR2EQ, CR2UN,
335       CR3LT, CR3GT, CR3EQ, CR3UN,
336       CR4LT, CR4GT, CR4EQ, CR4UN,
337       CR5LT, CR5GT, CR5EQ, CR5UN,
338       CR6LT, CR6GT, CR6EQ, CR6UN,
339       CR7LT, CR7GT, CR7EQ, CR7UN,
340       CR1LT, CR1GT, CR1EQ, CR1UN,
341       CR0LT, CR0GT, CR0EQ, CR0UN)> {
342  let Size = 32;
343  let AltOrders = [(sub CRBITRC, CR2LT, CR2GT, CR2EQ, CR2UN, CR3LT, CR3GT,
344                        CR3EQ, CR3UN, CR4LT, CR4GT, CR4EQ, CR4UN)];
345  let AltOrderSelect = [{
346    return MF.getSubtarget<PPCSubtarget>().isELFv2ABI() &&
347           MF.getInfo<PPCFunctionInfo>()->isNonVolatileCRDisabled();
348  }];
349}
350
351def CRRC : RegisterClass<"PPC", [i32], 32,
352  (add CR0, CR1, CR5, CR6,
353       CR7, CR2, CR3, CR4)> {
354  let AltOrders = [(sub CRRC, CR2, CR3, CR4)];
355  let AltOrderSelect = [{
356    return MF.getSubtarget<PPCSubtarget>().isELFv2ABI() &&
357           MF.getInfo<PPCFunctionInfo>()->isNonVolatileCRDisabled();
358  }];
359}
360// The CTR registers are not allocatable because they're used by the
361// decrement-and-branch instructions, and thus need to stay live across
362// multiple basic blocks.
363def CTRRC : RegisterClass<"PPC", [i32], 32, (add CTR)> {
364  let isAllocatable = 0;
365}
366def CTRRC8 : RegisterClass<"PPC", [i64], 64, (add CTR8)> {
367  let isAllocatable = 0;
368}
369
370def VRSAVERC : RegisterClass<"PPC", [i32], 32, (add VRSAVE)>;
371def CARRYRC : RegisterClass<"PPC", [i32], 32, (add CARRY, XER)> {
372  let CopyCost = -1;
373}
374
375