Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/Sparc/SparcRegisterInfo.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- SparcRegisterInfo.cpp - SPARC Register Information ----------------===//
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
// This file contains the SPARC implementation of the TargetRegisterInfo class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "SparcRegisterInfo.h"
14
#include "Sparc.h"
15
#include "SparcMachineFunctionInfo.h"
16
#include "SparcSubtarget.h"
17
#include "llvm/ADT/BitVector.h"
18
#include "llvm/ADT/STLExtras.h"
19
#include "llvm/CodeGen/MachineFrameInfo.h"
20
#include "llvm/CodeGen/MachineFunction.h"
21
#include "llvm/CodeGen/MachineInstrBuilder.h"
22
#include "llvm/CodeGen/TargetInstrInfo.h"
23
#include "llvm/IR/Type.h"
24
#include "llvm/Support/CommandLine.h"
25
#include "llvm/Support/ErrorHandling.h"
26
27
using namespace llvm;
28
29
#define GET_REGINFO_TARGET_DESC
30
#include "SparcGenRegisterInfo.inc"
31
32
static cl::opt<bool>
33
ReserveAppRegisters("sparc-reserve-app-registers", cl::Hidden, cl::init(false),
34
                    cl::desc("Reserve application registers (%g2-%g4)"));
35
36
425
SparcRegisterInfo::SparcRegisterInfo() : SparcGenRegisterInfo(SP::O7) {}
37
38
const MCPhysReg*
39
5.14k
SparcRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
40
5.14k
  return CSR_SaveList;
41
5.14k
}
42
43
const uint32_t *
44
SparcRegisterInfo::getCallPreservedMask(const MachineFunction &MF,
45
365
                                        CallingConv::ID CC) const {
46
365
  return CSR_RegMask;
47
365
}
48
49
const uint32_t*
50
2
SparcRegisterInfo::getRTCallPreservedMask(CallingConv::ID CC) const {
51
2
  return RTCSR_RegMask;
52
2
}
53
54
2.07k
BitVector SparcRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
55
2.07k
  BitVector Reserved(getNumRegs());
56
2.07k
  const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
57
2.07k
  // FIXME: G1 reserved for now for large imm generation by frame code.
58
2.07k
  Reserved.set(SP::G1);
59
2.07k
60
2.07k
  // G1-G4 can be used in applications.
61
2.07k
  if (ReserveAppRegisters) {
62
0
    Reserved.set(SP::G2);
63
0
    Reserved.set(SP::G3);
64
0
    Reserved.set(SP::G4);
65
0
  }
66
2.07k
  // G5 is not reserved in 64 bit mode.
67
2.07k
  if (!Subtarget.is64Bit())
68
1.32k
    Reserved.set(SP::G5);
69
2.07k
70
2.07k
  Reserved.set(SP::O6);
71
2.07k
  Reserved.set(SP::I6);
72
2.07k
  Reserved.set(SP::I7);
73
2.07k
  Reserved.set(SP::G0);
74
2.07k
  Reserved.set(SP::G6);
75
2.07k
  Reserved.set(SP::G7);
76
2.07k
77
2.07k
  // Also reserve the register pair aliases covering the above
78
2.07k
  // registers, with the same conditions.
79
2.07k
  Reserved.set(SP::G0_G1);
80
2.07k
  if (ReserveAppRegisters)
81
0
    Reserved.set(SP::G2_G3);
82
2.07k
  if (ReserveAppRegisters || !Subtarget.is64Bit())
83
1.32k
    Reserved.set(SP::G4_G5);
84
2.07k
85
2.07k
  Reserved.set(SP::O6_O7);
86
2.07k
  Reserved.set(SP::I6_I7);
87
2.07k
  Reserved.set(SP::G6_G7);
88
2.07k
89
2.07k
  // Unaliased double registers are not available in non-V9 targets.
90
2.07k
  if (!Subtarget.isV9()) {
91
20.6k
    for (unsigned n = 0; n != 16; 
++n19.4k
) {
92
58.3k
      for (MCRegAliasIterator AI(SP::D16 + n, this, true); AI.isValid(); 
++AI38.8k
)
93
38.8k
        Reserved.set(*AI);
94
19.4k
    }
95
1.21k
  }
96
2.07k
97
2.07k
  // Reserve ASR1-ASR31
98
66.5k
  for (unsigned n = 0; n < 31; 
n++64.4k
)
99
64.4k
    Reserved.set(SP::ASR1 + n);
100
2.07k
101
2.07k
  return Reserved;
102
2.07k
}
103
104
const TargetRegisterClass*
105
SparcRegisterInfo::getPointerRegClass(const MachineFunction &MF,
106
10.0k
                                      unsigned Kind) const {
107
10.0k
  const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
108
10.0k
  return Subtarget.is64Bit() ? 
&SP::I64RegsRegClass2.00k
:
&SP::IntRegsRegClass8.09k
;
109
10.0k
}
110
111
static void replaceFI(MachineFunction &MF, MachineBasicBlock::iterator II,
112
                      MachineInstr &MI, const DebugLoc &dl,
113
5.44k
                      unsigned FIOperandNum, int Offset, unsigned FramePtr) {
114
5.44k
  // Replace frame index with a frame pointer reference.
115
5.44k
  if (Offset >= -4096 && 
Offset <= 40955.43k
) {
116
5.42k
    // If the offset is small enough to fit in the immediate field, directly
117
5.42k
    // encode it.
118
5.42k
    MI.getOperand(FIOperandNum).ChangeToRegister(FramePtr, false);
119
5.42k
    MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
120
5.42k
    return;
121
5.42k
  }
122
18
123
18
  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
124
18
125
18
  // FIXME: it would be better to scavenge a register here instead of
126
18
  // reserving G1 all of the time.
127
18
  if (Offset >= 0) {
128
16
    // Emit nonnegaive immediates with sethi + or.
129
16
    // sethi %hi(Offset), %g1
130
16
    // add %g1, %fp, %g1
131
16
    // Insert G1+%lo(offset) into the user.
132
16
    BuildMI(*MI.getParent(), II, dl, TII.get(SP::SETHIi), SP::G1)
133
16
      .addImm(HI22(Offset));
134
16
135
16
136
16
    // Emit G1 = G1 + I6
137
16
    BuildMI(*MI.getParent(), II, dl, TII.get(SP::ADDrr), SP::G1).addReg(SP::G1)
138
16
      .addReg(FramePtr);
139
16
    // Insert: G1+%lo(offset) into the user.
140
16
    MI.getOperand(FIOperandNum).ChangeToRegister(SP::G1, false);
141
16
    MI.getOperand(FIOperandNum + 1).ChangeToImmediate(LO10(Offset));
142
16
    return;
143
16
  }
144
2
145
2
  // Emit Negative numbers with sethi + xor
146
2
  // sethi %hix(Offset), %g1
147
2
  // xor  %g1, %lox(offset), %g1
148
2
  // add %g1, %fp, %g1
149
2
  // Insert: G1 + 0 into the user.
150
2
  BuildMI(*MI.getParent(), II, dl, TII.get(SP::SETHIi), SP::G1)
151
2
    .addImm(HIX22(Offset));
152
2
  BuildMI(*MI.getParent(), II, dl, TII.get(SP::XORri), SP::G1)
153
2
    .addReg(SP::G1).addImm(LOX10(Offset));
154
2
155
2
  BuildMI(*MI.getParent(), II, dl, TII.get(SP::ADDrr), SP::G1).addReg(SP::G1)
156
2
    .addReg(FramePtr);
157
2
  // Insert: G1+%lo(offset) into the user.
158
2
  MI.getOperand(FIOperandNum).ChangeToRegister(SP::G1, false);
159
2
  MI.getOperand(FIOperandNum + 1).ChangeToImmediate(0);
160
2
}
161
162
163
void
164
SparcRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
165
                                       int SPAdj, unsigned FIOperandNum,
166
3.40k
                                       RegScavenger *RS) const {
167
3.40k
  assert(SPAdj == 0 && "Unexpected");
168
3.40k
169
3.40k
  MachineInstr &MI = *II;
170
3.40k
  DebugLoc dl = MI.getDebugLoc();
171
3.40k
  int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
172
3.40k
  MachineFunction &MF = *MI.getParent()->getParent();
173
3.40k
  const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
174
3.40k
  const SparcFrameLowering *TFI = getFrameLowering(MF);
175
3.40k
176
3.40k
  unsigned FrameReg;
177
3.40k
  int Offset;
178
3.40k
  Offset = TFI->getFrameIndexReference(MF, FrameIndex, FrameReg);
179
3.40k
180
3.40k
  Offset += MI.getOperand(FIOperandNum + 1).getImm();
181
3.40k
182
3.40k
  if (!Subtarget.isV9() || 
!Subtarget.hasHardQuad()236
) {
183
3.40k
    if (MI.getOpcode() == SP::STQFri) {
184
1.01k
      const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
185
1.01k
      unsigned SrcReg = MI.getOperand(2).getReg();
186
1.01k
      unsigned SrcEvenReg = getSubReg(SrcReg, SP::sub_even64);
187
1.01k
      unsigned SrcOddReg  = getSubReg(SrcReg, SP::sub_odd64);
188
1.01k
      MachineInstr *StMI =
189
1.01k
        BuildMI(*MI.getParent(), II, dl, TII.get(SP::STDFri))
190
1.01k
        .addReg(FrameReg).addImm(0).addReg(SrcEvenReg);
191
1.01k
      replaceFI(MF, *StMI, *StMI, dl, 0, Offset, FrameReg);
192
1.01k
      MI.setDesc(TII.get(SP::STDFri));
193
1.01k
      MI.getOperand(2).setReg(SrcOddReg);
194
1.01k
      Offset += 8;
195
2.39k
    } else if (MI.getOpcode() == SP::LDQFri) {
196
1.01k
      const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
197
1.01k
      unsigned DestReg     = MI.getOperand(0).getReg();
198
1.01k
      unsigned DestEvenReg = getSubReg(DestReg, SP::sub_even64);
199
1.01k
      unsigned DestOddReg  = getSubReg(DestReg, SP::sub_odd64);
200
1.01k
      MachineInstr *LdMI =
201
1.01k
        BuildMI(*MI.getParent(), II, dl, TII.get(SP::LDDFri), DestEvenReg)
202
1.01k
        .addReg(FrameReg).addImm(0);
203
1.01k
      replaceFI(MF, *LdMI, *LdMI, dl, 1, Offset, FrameReg);
204
1.01k
205
1.01k
      MI.setDesc(TII.get(SP::LDDFri));
206
1.01k
      MI.getOperand(0).setReg(DestOddReg);
207
1.01k
      Offset += 8;
208
1.01k
    }
209
3.40k
  }
210
3.40k
211
3.40k
  replaceFI(MF, II, MI, dl, FIOperandNum, Offset, FrameReg);
212
3.40k
213
3.40k
}
214
215
871
Register SparcRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
216
871
  return SP::I6;
217
871
}
218
219
// Sparc has no architectural need for stack realignment support,
220
// except that LLVM unfortunately currently implements overaligned
221
// stack objects by depending upon stack realignment support.
222
// If that ever changes, this can probably be deleted.
223
2.17k
bool SparcRegisterInfo::canRealignStack(const MachineFunction &MF) const {
224
2.17k
  if (!TargetRegisterInfo::canRealignStack(MF))
225
0
    return false;
226
2.17k
227
2.17k
  // Sparc always has a fixed frame pointer register, so don't need to
228
2.17k
  // worry about needing to reserve it. [even if we don't have a frame
229
2.17k
  // pointer for our frame, it still cannot be used for other things,
230
2.17k
  // or register window traps will be SADNESS.]
231
2.17k
232
2.17k
  // If there's a reserved call frame, we can use SP to access locals.
233
2.17k
  if (getFrameLowering(MF)->hasReservedCallFrame(MF))
234
2.16k
    return true;
235
10
236
10
  // Otherwise, we'd need a base pointer, but those aren't implemented
237
10
  // for SPARC at the moment.
238
10
239
10
  return false;
240
10
}