Coverage Report

Created: 2017-10-03 07:32

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