Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/NVPTX/NVPTXPrologEpilogPass.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- NVPTXPrologEpilogPass.cpp - NVPTX prolog/epilog inserter ----------===//
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 is a copy of the generic LLVM PrologEpilogInserter pass, modified
10
// to remove unneeded functionality and to handle virtual registers. Most code
11
// here is a copy of PrologEpilogInserter.cpp.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "NVPTX.h"
16
#include "llvm/CodeGen/MachineFrameInfo.h"
17
#include "llvm/CodeGen/MachineFunction.h"
18
#include "llvm/CodeGen/MachineFunctionPass.h"
19
#include "llvm/CodeGen/TargetFrameLowering.h"
20
#include "llvm/CodeGen/TargetRegisterInfo.h"
21
#include "llvm/CodeGen/TargetSubtargetInfo.h"
22
#include "llvm/IR/DebugInfoMetadata.h"
23
#include "llvm/Pass.h"
24
#include "llvm/Support/Debug.h"
25
#include "llvm/Support/raw_ostream.h"
26
27
using namespace llvm;
28
29
#define DEBUG_TYPE "nvptx-prolog-epilog"
30
31
namespace {
32
class NVPTXPrologEpilogPass : public MachineFunctionPass {
33
public:
34
  static char ID;
35
263
  NVPTXPrologEpilogPass() : MachineFunctionPass(ID) {}
36
37
  bool runOnMachineFunction(MachineFunction &MF) override;
38
39
private:
40
  void calculateFrameObjectOffsets(MachineFunction &Fn);
41
};
42
}
43
44
263
MachineFunctionPass *llvm::createNVPTXPrologEpilogPass() {
45
263
  return new NVPTXPrologEpilogPass();
46
263
}
47
48
char NVPTXPrologEpilogPass::ID = 0;
49
50
1.68k
bool NVPTXPrologEpilogPass::runOnMachineFunction(MachineFunction &MF) {
51
1.68k
  const TargetSubtargetInfo &STI = MF.getSubtarget();
52
1.68k
  const TargetFrameLowering &TFI = *STI.getFrameLowering();
53
1.68k
  const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
54
1.68k
  bool Modified = false;
55
1.68k
56
1.68k
  calculateFrameObjectOffsets(MF);
57
1.68k
58
1.84k
  for (MachineBasicBlock &MBB : MF) {
59
13.4k
    for (MachineInstr &MI : MBB) {
60
59.1k
      for (unsigned i = 0, e = MI.getNumOperands(); i != e; 
++i45.7k
) {
61
45.7k
        if (!MI.getOperand(i).isFI())
62
45.6k
          continue;
63
99
64
99
        // Frame indices in debug values are encoded in a target independent
65
99
        // way with simply the frame index and offset rather than any
66
99
        // target-specific addressing mode.
67
99
        if (MI.isDebugValue()) {
68
1
          assert(i == 0 && "Frame indices can only appear as the first "
69
1
                           "operand of a DBG_VALUE machine instruction");
70
1
          unsigned Reg;
71
1
          int64_t Offset =
72
1
              TFI.getFrameIndexReference(MF, MI.getOperand(0).getIndex(), Reg);
73
1
          MI.getOperand(0).ChangeToRegister(Reg, /*isDef=*/false);
74
1
          MI.getOperand(0).setIsDebug();
75
1
          auto *DIExpr = DIExpression::prepend(
76
1
              MI.getDebugExpression(), DIExpression::ApplyOffset, Offset);
77
1
          MI.getOperand(3).setMetadata(DIExpr);
78
1
          continue;
79
1
        }
80
98
81
98
        TRI.eliminateFrameIndex(MI, 0, i, nullptr);
82
98
        Modified = true;
83
98
      }
84
13.4k
    }
85
1.84k
  }
86
1.68k
87
1.68k
  // Add function prolog/epilog
88
1.68k
  TFI.emitPrologue(MF, MF.front());
89
1.68k
90
3.52k
  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; 
++I1.84k
) {
91
1.84k
    // If last instruction is a return instruction, add an epilogue
92
1.84k
    if (I->isReturnBlock())
93
1.68k
      TFI.emitEpilogue(MF, *I);
94
1.84k
  }
95
1.68k
96
1.68k
  return Modified;
97
1.68k
}
98
99
/// AdjustStackOffset - Helper function used to adjust the stack frame offset.
100
static inline void
101
AdjustStackOffset(MachineFrameInfo &MFI, int FrameIdx,
102
                  bool StackGrowsDown, int64_t &Offset,
103
71
                  unsigned &MaxAlign) {
104
71
  // If the stack grows down, add the object size to find the lowest address.
105
71
  if (StackGrowsDown)
106
0
    Offset += MFI.getObjectSize(FrameIdx);
107
71
108
71
  unsigned Align = MFI.getObjectAlignment(FrameIdx);
109
71
110
71
  // If the alignment of this object is greater than that of the stack, then
111
71
  // increase the stack alignment to match.
112
71
  MaxAlign = std::max(MaxAlign, Align);
113
71
114
71
  // Adjust to alignment boundary.
115
71
  Offset = (Offset + Align - 1) / Align * Align;
116
71
117
71
  if (StackGrowsDown) {
118
0
    LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset
119
0
                      << "]\n");
120
0
    MFI.setObjectOffset(FrameIdx, -Offset); // Set the computed offset
121
71
  } else {
122
71
    LLVM_DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset
123
71
                      << "]\n");
124
71
    MFI.setObjectOffset(FrameIdx, Offset);
125
71
    Offset += MFI.getObjectSize(FrameIdx);
126
71
  }
127
71
}
128
129
void
130
1.68k
NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
131
1.68k
  const TargetFrameLowering &TFI = *Fn.getSubtarget().getFrameLowering();
132
1.68k
  const TargetRegisterInfo *RegInfo = Fn.getSubtarget().getRegisterInfo();
133
1.68k
134
1.68k
  bool StackGrowsDown =
135
1.68k
    TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
136
1.68k
137
1.68k
  // Loop over all of the stack objects, assigning sequential addresses...
138
1.68k
  MachineFrameInfo &MFI = Fn.getFrameInfo();
139
1.68k
140
1.68k
  // Start at the beginning of the local area.
141
1.68k
  // The Offset is the distance from the stack top in the direction
142
1.68k
  // of stack growth -- so it's always nonnegative.
143
1.68k
  int LocalAreaOffset = TFI.getOffsetOfLocalArea();
144
1.68k
  if (StackGrowsDown)
145
0
    LocalAreaOffset = -LocalAreaOffset;
146
1.68k
  assert(LocalAreaOffset >= 0
147
1.68k
         && "Local area offset should be in direction of stack growth");
148
1.68k
  int64_t Offset = LocalAreaOffset;
149
1.68k
150
1.68k
  // If there are fixed sized objects that are preallocated in the local area,
151
1.68k
  // non-fixed objects can't be allocated right at the start of local area.
152
1.68k
  // We currently don't support filling in holes in between fixed sized
153
1.68k
  // objects, so we adjust 'Offset' to point to the end of last fixed sized
154
1.68k
  // preallocated object.
155
1.68k
  for (int i = MFI.getObjectIndexBegin(); i != 0; 
++i0
) {
156
0
    int64_t FixedOff;
157
0
    if (StackGrowsDown) {
158
0
      // The maximum distance from the stack pointer is at lower address of
159
0
      // the object -- which is given by offset. For down growing stack
160
0
      // the offset is negative, so we negate the offset to get the distance.
161
0
      FixedOff = -MFI.getObjectOffset(i);
162
0
    } else {
163
0
      // The maximum distance from the start pointer is at the upper
164
0
      // address of the object.
165
0
      FixedOff = MFI.getObjectOffset(i) + MFI.getObjectSize(i);
166
0
    }
167
0
    if (FixedOff > Offset) Offset = FixedOff;
168
0
  }
169
1.68k
170
1.68k
  // NOTE: We do not have a call stack
171
1.68k
172
1.68k
  unsigned MaxAlign = MFI.getMaxAlignment();
173
1.68k
174
1.68k
  // No scavenger
175
1.68k
176
1.68k
  // FIXME: Once this is working, then enable flag will change to a target
177
1.68k
  // check for whether the frame is large enough to want to use virtual
178
1.68k
  // frame index registers. Functions which don't want/need this optimization
179
1.68k
  // will continue to use the existing code path.
180
1.68k
  if (MFI.getUseLocalStackAllocationBlock()) {
181
0
    unsigned Align = MFI.getLocalFrameMaxAlign();
182
0
183
0
    // Adjust to alignment boundary.
184
0
    Offset = (Offset + Align - 1) / Align * Align;
185
0
186
0
    LLVM_DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
187
0
188
0
    // Resolve offsets for objects in the local block.
189
0
    for (unsigned i = 0, e = MFI.getLocalFrameObjectCount(); i != e; ++i) {
190
0
      std::pair<int, int64_t> Entry = MFI.getLocalFrameObjectMap(i);
191
0
      int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
192
0
      LLVM_DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" << FIOffset
193
0
                        << "]\n");
194
0
      MFI.setObjectOffset(Entry.first, FIOffset);
195
0
    }
196
0
    // Allocate the local block
197
0
    Offset += MFI.getLocalFrameSize();
198
0
199
0
    MaxAlign = std::max(Align, MaxAlign);
200
0
  }
201
1.68k
202
1.68k
  // No stack protector
203
1.68k
204
1.68k
  // Then assign frame offsets to stack objects that are not used to spill
205
1.68k
  // callee saved registers.
206
1.75k
  for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; 
++i71
) {
207
71
    if (MFI.isObjectPreAllocated(i) &&
208
71
        
MFI.getUseLocalStackAllocationBlock()0
)
209
0
      continue;
210
71
    if (MFI.isDeadObjectIndex(i))
211
0
      continue;
212
71
213
71
    AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
214
71
  }
215
1.68k
216
1.68k
  // No scavenger
217
1.68k
218
1.68k
  if (!TFI.targetHandlesStackFrameRounding()) {
219
1.68k
    // If we have reserved argument space for call sites in the function
220
1.68k
    // immediately on entry to the current function, count it as part of the
221
1.68k
    // overall stack size.
222
1.68k
    if (MFI.adjustsStack() && 
TFI.hasReservedCallFrame(Fn)0
)
223
0
      Offset += MFI.getMaxCallFrameSize();
224
1.68k
225
1.68k
    // Round up the size to a multiple of the alignment.  If the function has
226
1.68k
    // any calls or alloca's, align to the target's StackAlignment value to
227
1.68k
    // ensure that the callee's frame or the alloca data is suitably aligned;
228
1.68k
    // otherwise, for leaf functions, align to the TransientStackAlignment
229
1.68k
    // value.
230
1.68k
    unsigned StackAlign;
231
1.68k
    if (MFI.adjustsStack() || MFI.hasVarSizedObjects() ||
232
1.68k
        (RegInfo->needsStackRealignment(Fn) && 
MFI.getObjectIndexEnd() != 00
))
233
0
      StackAlign = TFI.getStackAlignment();
234
1.68k
    else
235
1.68k
      StackAlign = TFI.getTransientStackAlignment();
236
1.68k
237
1.68k
    // If the frame pointer is eliminated, all frame offsets will be relative to
238
1.68k
    // SP not FP. Align to MaxAlign so this works.
239
1.68k
    StackAlign = std::max(StackAlign, MaxAlign);
240
1.68k
    unsigned AlignMask = StackAlign - 1;
241
1.68k
    Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
242
1.68k
  }
243
1.68k
244
1.68k
  // Update frame info to pretend that this is part of the stack...
245
1.68k
  int64_t StackSize = Offset - LocalAreaOffset;
246
1.68k
  MFI.setStackSize(StackSize);
247
1.68k
}