Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/MSP430/MSP430FrameLowering.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- MSP430FrameLowering.cpp - MSP430 Frame 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 MSP430 implementation of TargetFrameLowering class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "MSP430FrameLowering.h"
14
#include "MSP430InstrInfo.h"
15
#include "MSP430MachineFunctionInfo.h"
16
#include "MSP430Subtarget.h"
17
#include "llvm/CodeGen/MachineFrameInfo.h"
18
#include "llvm/CodeGen/MachineFunction.h"
19
#include "llvm/CodeGen/MachineInstrBuilder.h"
20
#include "llvm/CodeGen/MachineModuleInfo.h"
21
#include "llvm/CodeGen/MachineRegisterInfo.h"
22
#include "llvm/IR/DataLayout.h"
23
#include "llvm/IR/Function.h"
24
#include "llvm/Target/TargetOptions.h"
25
26
using namespace llvm;
27
28
4.65k
bool MSP430FrameLowering::hasFP(const MachineFunction &MF) const {
29
4.65k
  const MachineFrameInfo &MFI = MF.getFrameInfo();
30
4.65k
31
4.65k
  return (MF.getTarget().Options.DisableFramePointerElim(MF) ||
32
4.65k
          
MF.getFrameInfo().hasVarSizedObjects()4.39k
||
33
4.65k
          
MFI.isFrameAddressTaken()4.38k
);
34
4.65k
}
35
36
497
bool MSP430FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
37
497
  return !MF.getFrameInfo().hasVarSizedObjects();
38
497
}
39
40
void MSP430FrameLowering::emitPrologue(MachineFunction &MF,
41
318
                                       MachineBasicBlock &MBB) const {
42
318
  assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
43
318
  MachineFrameInfo &MFI = MF.getFrameInfo();
44
318
  MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
45
318
  const MSP430InstrInfo &TII =
46
318
      *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
47
318
48
318
  MachineBasicBlock::iterator MBBI = MBB.begin();
49
318
  DebugLoc DL = MBBI != MBB.end() ? 
MBBI->getDebugLoc()315
:
DebugLoc()3
;
50
318
51
318
  // Get the number of bytes to allocate from the FrameInfo.
52
318
  uint64_t StackSize = MFI.getStackSize();
53
318
54
318
  uint64_t NumBytes = 0;
55
318
  if (hasFP(MF)) {
56
20
    // Calculate required stack adjustment
57
20
    uint64_t FrameSize = StackSize - 2;
58
20
    NumBytes = FrameSize - MSP430FI->getCalleeSavedFrameSize();
59
20
60
20
    // Get the offset of the stack slot for the EBP register... which is
61
20
    // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
62
20
    // Update the frame offset adjustment.
63
20
    MFI.setOffsetAdjustment(-NumBytes);
64
20
65
20
    // Save FP into the appropriate stack slot...
66
20
    BuildMI(MBB, MBBI, DL, TII.get(MSP430::PUSH16r))
67
20
      .addReg(MSP430::FP, RegState::Kill);
68
20
69
20
    // Update FP with the new base value...
70
20
    BuildMI(MBB, MBBI, DL, TII.get(MSP430::MOV16rr), MSP430::FP)
71
20
      .addReg(MSP430::SP);
72
20
73
20
    // Mark the FramePtr as live-in in every block except the entry.
74
20
    for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
75
22
         I != E; 
++I2
)
76
2
      I->addLiveIn(MSP430::FP);
77
20
78
20
  } else
79
298
    NumBytes = StackSize - MSP430FI->getCalleeSavedFrameSize();
80
318
81
318
  // Skip the callee-saved push instructions.
82
400
  while (MBBI != MBB.end() && 
(MBBI->getOpcode() == MSP430::PUSH16r)397
)
83
82
    ++MBBI;
84
318
85
318
  if (MBBI != MBB.end())
86
315
    DL = MBBI->getDebugLoc();
87
318
88
318
  if (NumBytes) { // adjust stack pointer: SP -= numbytes
89
85
    // If there is an SUB16ri of SP immediately before this instruction, merge
90
85
    // the two.
91
85
    //NumBytes -= mergeSPUpdates(MBB, MBBI, true);
92
85
    // If there is an ADD16ri or SUB16ri of SP immediately after this
93
85
    // instruction, merge the two instructions.
94
85
    // mergeSPUpdatesDown(MBB, MBBI, &NumBytes);
95
85
96
85
    if (NumBytes) {
97
85
      MachineInstr *MI =
98
85
        BuildMI(MBB, MBBI, DL, TII.get(MSP430::SUB16ri), MSP430::SP)
99
85
        .addReg(MSP430::SP).addImm(NumBytes);
100
85
      // The SRW implicit def is dead.
101
85
      MI->getOperand(3).setIsDead();
102
85
    }
103
85
  }
104
318
}
105
106
void MSP430FrameLowering::emitEpilogue(MachineFunction &MF,
107
320
                                       MachineBasicBlock &MBB) const {
108
320
  const MachineFrameInfo &MFI = MF.getFrameInfo();
109
320
  MSP430MachineFunctionInfo *MSP430FI = MF.getInfo<MSP430MachineFunctionInfo>();
110
320
  const MSP430InstrInfo &TII =
111
320
      *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
112
320
113
320
  MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
114
320
  unsigned RetOpcode = MBBI->getOpcode();
115
320
  DebugLoc DL = MBBI->getDebugLoc();
116
320
117
320
  switch (RetOpcode) {
118
320
  case MSP430::RET:
119
320
  case MSP430::RETI: break;  // These are ok
120
320
  default:
121
0
    llvm_unreachable("Can only insert epilog into returning blocks");
122
320
  }
123
320
124
320
  // Get the number of bytes to allocate from the FrameInfo
125
320
  uint64_t StackSize = MFI.getStackSize();
126
320
  unsigned CSSize = MSP430FI->getCalleeSavedFrameSize();
127
320
  uint64_t NumBytes = 0;
128
320
129
320
  if (hasFP(MF)) {
130
20
    // Calculate required stack adjustment
131
20
    uint64_t FrameSize = StackSize - 2;
132
20
    NumBytes = FrameSize - CSSize;
133
20
134
20
    // pop FP.
135
20
    BuildMI(MBB, MBBI, DL, TII.get(MSP430::POP16r), MSP430::FP);
136
20
  } else
137
300
    NumBytes = StackSize - CSSize;
138
320
139
320
  // Skip the callee-saved pop instructions.
140
422
  while (MBBI != MBB.begin()) {
141
394
    MachineBasicBlock::iterator PI = std::prev(MBBI);
142
394
    unsigned Opc = PI->getOpcode();
143
394
    if (Opc != MSP430::POP16r && 
!PI->isTerminator()292
)
144
292
      break;
145
102
    --MBBI;
146
102
  }
147
320
148
320
  DL = MBBI->getDebugLoc();
149
320
150
320
  // If there is an ADD16ri or SUB16ri of SP immediately before this
151
320
  // instruction, merge the two instructions.
152
320
  //if (NumBytes || MFI.hasVarSizedObjects())
153
320
  //  mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
154
320
155
320
  if (MFI.hasVarSizedObjects()) {
156
1
    BuildMI(MBB, MBBI, DL,
157
1
            TII.get(MSP430::MOV16rr), MSP430::SP).addReg(MSP430::FP);
158
1
    if (CSSize) {
159
0
      MachineInstr *MI =
160
0
        BuildMI(MBB, MBBI, DL,
161
0
                TII.get(MSP430::SUB16ri), MSP430::SP)
162
0
        .addReg(MSP430::SP).addImm(CSSize);
163
0
      // The SRW implicit def is dead.
164
0
      MI->getOperand(3).setIsDead();
165
0
    }
166
319
  } else {
167
319
    // adjust stack pointer back: SP += numbytes
168
319
    if (NumBytes) {
169
85
      MachineInstr *MI =
170
85
        BuildMI(MBB, MBBI, DL, TII.get(MSP430::ADD16ri), MSP430::SP)
171
85
        .addReg(MSP430::SP).addImm(NumBytes);
172
85
      // The SRW implicit def is dead.
173
85
      MI->getOperand(3).setIsDead();
174
85
    }
175
319
  }
176
320
}
177
178
// FIXME: Can we eleminate these in favour of generic code?
179
bool
180
MSP430FrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
181
                                           MachineBasicBlock::iterator MI,
182
                                        const std::vector<CalleeSavedInfo> &CSI,
183
23
                                        const TargetRegisterInfo *TRI) const {
184
23
  if (CSI.empty())
185
0
    return false;
186
23
187
23
  DebugLoc DL;
188
23
  if (MI != MBB.end()) DL = MI->getDebugLoc();
189
23
190
23
  MachineFunction &MF = *MBB.getParent();
191
23
  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
192
23
  MSP430MachineFunctionInfo *MFI = MF.getInfo<MSP430MachineFunctionInfo>();
193
23
  MFI->setCalleeSavedFrameSize(CSI.size() * 2);
194
23
195
105
  for (unsigned i = CSI.size(); i != 0; 
--i82
) {
196
82
    unsigned Reg = CSI[i-1].getReg();
197
82
    // Add the callee-saved register as live-in. It's killed at the spill.
198
82
    MBB.addLiveIn(Reg);
199
82
    BuildMI(MBB, MI, DL, TII.get(MSP430::PUSH16r))
200
82
      .addReg(Reg, RegState::Kill);
201
82
  }
202
23
  return true;
203
23
}
204
205
bool
206
MSP430FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
207
                                                 MachineBasicBlock::iterator MI,
208
                                        std::vector<CalleeSavedInfo> &CSI,
209
23
                                        const TargetRegisterInfo *TRI) const {
210
23
  if (CSI.empty())
211
0
    return false;
212
23
213
23
  DebugLoc DL;
214
23
  if (MI != MBB.end()) DL = MI->getDebugLoc();
215
23
216
23
  MachineFunction &MF = *MBB.getParent();
217
23
  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
218
23
219
105
  for (unsigned i = 0, e = CSI.size(); i != e; 
++i82
)
220
82
    BuildMI(MBB, MI, DL, TII.get(MSP430::POP16r), CSI[i].getReg());
221
23
222
23
  return true;
223
23
}
224
225
MachineBasicBlock::iterator MSP430FrameLowering::eliminateCallFramePseudoInstr(
226
    MachineFunction &MF, MachineBasicBlock &MBB,
227
204
    MachineBasicBlock::iterator I) const {
228
204
  const MSP430InstrInfo &TII =
229
204
      *static_cast<const MSP430InstrInfo *>(MF.getSubtarget().getInstrInfo());
230
204
  unsigned StackAlign = getStackAlignment();
231
204
232
204
  if (!hasReservedCallFrame(MF)) {
233
4
    // If the stack pointer can be changed after prologue, turn the
234
4
    // adjcallstackup instruction into a 'sub SP, <amt>' and the
235
4
    // adjcallstackdown instruction into 'add SP, <amt>'
236
4
    // TODO: consider using push / pop instead of sub + store / add
237
4
    MachineInstr &Old = *I;
238
4
    uint64_t Amount = TII.getFrameSize(Old);
239
4
    if (Amount != 0) {
240
0
      // We need to keep the stack aligned properly.  To do this, we round the
241
0
      // amount of space needed for the outgoing arguments up to the next
242
0
      // alignment boundary.
243
0
      Amount = (Amount+StackAlign-1)/StackAlign*StackAlign;
244
0
245
0
      MachineInstr *New = nullptr;
246
0
      if (Old.getOpcode() == TII.getCallFrameSetupOpcode()) {
247
0
        New =
248
0
            BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::SUB16ri), MSP430::SP)
249
0
                .addReg(MSP430::SP)
250
0
                .addImm(Amount);
251
0
      } else {
252
0
        assert(Old.getOpcode() == TII.getCallFrameDestroyOpcode());
253
0
        // factor out the amount the callee already popped.
254
0
        Amount -= TII.getFramePoppedByCallee(Old);
255
0
        if (Amount)
256
0
          New = BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::ADD16ri),
257
0
                        MSP430::SP)
258
0
                    .addReg(MSP430::SP)
259
0
                    .addImm(Amount);
260
0
      }
261
0
262
0
      if (New) {
263
0
        // The SRW implicit def is dead.
264
0
        New->getOperand(3).setIsDead();
265
0
266
0
        // Replace the pseudo instruction with a new instruction...
267
0
        MBB.insert(I, New);
268
0
      }
269
0
    }
270
200
  } else if (I->getOpcode() == TII.getCallFrameDestroyOpcode()) {
271
100
    // If we are performing frame pointer elimination and if the callee pops
272
100
    // something off the stack pointer, add it back.
273
100
    if (uint64_t CalleeAmt = TII.getFramePoppedByCallee(*I)) {
274
0
      MachineInstr &Old = *I;
275
0
      MachineInstr *New =
276
0
          BuildMI(MF, Old.getDebugLoc(), TII.get(MSP430::SUB16ri), MSP430::SP)
277
0
              .addReg(MSP430::SP)
278
0
              .addImm(CalleeAmt);
279
0
      // The SRW implicit def is dead.
280
0
      New->getOperand(3).setIsDead();
281
0
282
0
      MBB.insert(I, New);
283
0
    }
284
100
  }
285
204
286
204
  return MBB.erase(I);
287
204
}
288
289
void
290
MSP430FrameLowering::processFunctionBeforeFrameFinalized(MachineFunction &MF,
291
318
                                                         RegScavenger *) const {
292
318
  // Create a frame entry for the FP register that must be saved.
293
318
  if (hasFP(MF)) {
294
20
    int FrameIdx = MF.getFrameInfo().CreateFixedObject(2, -4, true);
295
20
    (void)FrameIdx;
296
20
    assert(FrameIdx == MF.getFrameInfo().getObjectIndexBegin() &&
297
20
           "Slot for FP register must be last in order to be found!");
298
20
  }
299
318
}