Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/MachineFrameInfo.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- MachineFrameInfo.cpp ---------------------------------------------===//
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
/// \file Implements MachineFrameInfo that manages the stack frame.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/CodeGen/MachineFrameInfo.h"
14
15
#include "llvm/ADT/BitVector.h"
16
#include "llvm/CodeGen/MachineFunction.h"
17
#include "llvm/CodeGen/MachineRegisterInfo.h"
18
#include "llvm/CodeGen/TargetFrameLowering.h"
19
#include "llvm/CodeGen/TargetInstrInfo.h"
20
#include "llvm/CodeGen/TargetRegisterInfo.h"
21
#include "llvm/CodeGen/TargetSubtargetInfo.h"
22
#include "llvm/Config/llvm-config.h"
23
#include "llvm/Support/Debug.h"
24
#include "llvm/Support/raw_ostream.h"
25
#include <cassert>
26
27
#define DEBUG_TYPE "codegen"
28
29
using namespace llvm;
30
31
2.06M
void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
32
2.06M
  if (!StackRealignable)
33
2.06M
    assert(Align <= StackAlignment &&
34
2.06M
           "For targets without stack realignment, Align is out of limit!");
35
2.06M
  if (MaxAlignment < Align) 
MaxAlignment = Align291k
;
36
2.06M
}
37
38
/// Clamp the alignment if requested and emit a warning.
39
static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align,
40
1.84M
                                           unsigned StackAlign) {
41
1.84M
  if (!ShouldClamp || 
Align <= StackAlign6.18k
)
42
1.84M
    return Align;
43
153
  LLVM_DEBUG(dbgs() << "Warning: requested alignment " << Align
44
153
                    << " exceeds the stack alignment " << StackAlign
45
153
                    << " when stack realignment is off" << '\n');
46
153
  return StackAlign;
47
153
}
48
49
int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
50
                                        bool IsSpillSlot,
51
                                        const AllocaInst *Alloca,
52
1.54M
                                        uint8_t StackID) {
53
1.54M
  assert(Size != 0 && "Cannot allocate zero size stack objects!");
54
1.54M
  Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
55
1.54M
  Objects.push_back(StackObject(Size, Alignment, 0, false, IsSpillSlot, Alloca,
56
1.54M
                                !IsSpillSlot, StackID));
57
1.54M
  int Index = (int)Objects.size() - NumFixedObjects - 1;
58
1.54M
  assert(Index >= 0 && "Bad frame index!");
59
1.54M
  if (StackID == 0)
60
1.54M
    ensureMaxAlignment(Alignment);
61
1.54M
  return Index;
62
1.54M
}
63
64
int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
65
126k
                                             unsigned Alignment) {
66
126k
  Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
67
126k
  CreateStackObject(Size, Alignment, true);
68
126k
  int Index = (int)Objects.size() - NumFixedObjects - 1;
69
126k
  ensureMaxAlignment(Alignment);
70
126k
  return Index;
71
126k
}
72
73
int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
74
913
                                                const AllocaInst *Alloca) {
75
913
  HasVarSizedObjects = true;
76
913
  Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
77
913
  Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
78
913
  ensureMaxAlignment(Alignment);
79
913
  return (int)Objects.size()-NumFixedObjects-1;
80
913
}
81
82
int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
83
82.0k
                                        bool IsImmutable, bool IsAliased) {
84
82.0k
  assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
85
82.0k
  // The alignment of the frame index can be determined from its offset from
86
82.0k
  // the incoming frame position.  If the frame object is at offset 32 and
87
82.0k
  // the stack is guaranteed to be 16-byte aligned, then we know that the
88
82.0k
  // object is 16-byte aligned. Note that unlike the non-fixed case, if the
89
82.0k
  // stack needs realignment, we can't assume that the stack will in fact be
90
82.0k
  // aligned.
91
82.0k
  unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 
127
:
StackAlignment82.0k
);
92
82.0k
  Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
93
82.0k
  Objects.insert(Objects.begin(),
94
82.0k
                 StackObject(Size, Alignment, SPOffset, IsImmutable,
95
82.0k
                             /*IsSpillSlot=*/false, /*Alloca=*/nullptr,
96
82.0k
                             IsAliased));
97
82.0k
  return -++NumFixedObjects;
98
82.0k
}
99
100
int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
101
                                                  int64_t SPOffset,
102
86.7k
                                                  bool IsImmutable) {
103
86.7k
  unsigned Alignment = MinAlign(SPOffset, ForcedRealign ? 
126
:
StackAlignment86.6k
);
104
86.7k
  Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
105
86.7k
  Objects.insert(Objects.begin(),
106
86.7k
                 StackObject(Size, Alignment, SPOffset, IsImmutable,
107
86.7k
                             /*IsSpillSlot=*/true, /*Alloca=*/nullptr,
108
86.7k
                             /*IsAliased=*/false));
109
86.7k
  return -++NumFixedObjects;
110
86.7k
}
111
112
2.89M
BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
113
2.89M
  const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
114
2.89M
  BitVector BV(TRI->getNumRegs());
115
2.89M
116
2.89M
  // Before CSI is calculated, no registers are considered pristine. They can be
117
2.89M
  // freely used and PEI will make sure they are saved.
118
2.89M
  if (!isCalleeSavedInfoValid())
119
1.86M
    return BV;
120
1.02M
121
1.02M
  const MachineRegisterInfo &MRI = MF.getRegInfo();
122
25.8M
  for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR;
123
24.8M
       ++CSR)
124
24.8M
    BV.set(*CSR);
125
1.02M
126
1.02M
  // Saved CSRs are not pristine.
127
1.02M
  for (auto &I : getCalleeSavedInfo())
128
1.42M
    
for (MCSubRegIterator S(I.getReg(), TRI, true); 477k
S.isValid();
++S946k
)
129
946k
      BV.reset(*S);
130
1.02M
131
1.02M
  return BV;
132
1.02M
}
133
134
362k
unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
135
362k
  const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
136
362k
  const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
137
362k
  unsigned MaxAlign = getMaxAlignment();
138
362k
  int Offset = 0;
139
362k
140
362k
  // This code is very, very similar to PEI::calculateFrameObjectOffsets().
141
362k
  // It really should be refactored to share code. Until then, changes
142
362k
  // should keep in mind that there's tight coupling between the two.
143
362k
144
442k
  for (int i = getObjectIndexBegin(); i != 0; 
++i79.5k
) {
145
79.5k
    // Only estimate stack size of default stack.
146
79.5k
    if (getStackID(i) != TargetStackID::Default)
147
1
      continue;
148
79.5k
    int FixedOff = -getObjectOffset(i);
149
79.5k
    if (FixedOff > Offset) 
Offset = FixedOff3.40k
;
150
79.5k
  }
151
665k
  for (unsigned i = 0, e = getObjectIndexEnd(); i != e; 
++i302k
) {
152
302k
    // Only estimate stack size of live objects on default stack.
153
302k
    if (isDeadObjectIndex(i) || 
getStackID(i) != TargetStackID::Default260k
)
154
42.3k
      continue;
155
260k
    Offset += getObjectSize(i);
156
260k
    unsigned Align = getObjectAlignment(i);
157
260k
    // Adjust to alignment boundary
158
260k
    Offset = (Offset+Align-1)/Align*Align;
159
260k
160
260k
    MaxAlign = std::max(Align, MaxAlign);
161
260k
  }
162
362k
163
362k
  if (adjustsStack() && 
TFI->hasReservedCallFrame(MF)217k
)
164
217k
    Offset += getMaxCallFrameSize();
165
362k
166
362k
  // Round up the size to a multiple of the alignment.  If the function has
167
362k
  // any calls or alloca's, align to the target's StackAlignment value to
168
362k
  // ensure that the callee's frame or the alloca data is suitably aligned;
169
362k
  // otherwise, for leaf functions, align to the TransientStackAlignment
170
362k
  // value.
171
362k
  unsigned StackAlign;
172
362k
  if (adjustsStack() || 
hasVarSizedObjects()144k
||
173
362k
      
(144k
RegInfo->needsStackRealignment(MF)144k
&&
getObjectIndexEnd() != 0599
))
174
218k
    StackAlign = TFI->getStackAlignment();
175
144k
  else
176
144k
    StackAlign = TFI->getTransientStackAlignment();
177
362k
178
362k
  // If the frame pointer is eliminated, all frame offsets will be relative to
179
362k
  // SP not FP. Align to MaxAlign so this works.
180
362k
  StackAlign = std::max(StackAlign, MaxAlign);
181
362k
  unsigned AlignMask = StackAlign - 1;
182
362k
  Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
183
362k
184
362k
  return (unsigned)Offset;
185
362k
}
186
187
571k
void MachineFrameInfo::computeMaxCallFrameSize(const MachineFunction &MF) {
188
571k
  const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
189
571k
  unsigned FrameSetupOpcode = TII.getCallFrameSetupOpcode();
190
571k
  unsigned FrameDestroyOpcode = TII.getCallFrameDestroyOpcode();
191
571k
  assert(FrameSetupOpcode != ~0u && FrameDestroyOpcode != ~0u &&
192
571k
         "Can only compute MaxCallFrameSize if Setup/Destroy opcode are known");
193
571k
194
571k
  MaxCallFrameSize = 0;
195
4.30M
  for (const MachineBasicBlock &MBB : MF) {
196
41.1M
    for (const MachineInstr &MI : MBB) {
197
41.1M
      unsigned Opcode = MI.getOpcode();
198
41.1M
      if (Opcode == FrameSetupOpcode || 
Opcode == FrameDestroyOpcode38.5M
) {
199
5.21M
        unsigned Size = TII.getFrameSize(MI);
200
5.21M
        MaxCallFrameSize = std::max(MaxCallFrameSize, Size);
201
5.21M
        AdjustsStack = true;
202
35.9M
      } else if (MI.isInlineAsm()) {
203
12.8k
        // Some inline asm's need a stack frame, as indicated by operand 1.
204
12.8k
        unsigned ExtraInfo = MI.getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
205
12.8k
        if (ExtraInfo & InlineAsm::Extra_IsAlignStack)
206
2
          AdjustsStack = true;
207
12.8k
      }
208
41.1M
    }
209
4.30M
  }
210
571k
}
211
212
2.95k
void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
213
2.95k
  if (Objects.empty()) 
return2.44k
;
214
506
215
506
  const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
216
506
  int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 
00
);
217
506
218
506
  OS << "Frame Objects:\n";
219
506
220
1.55k
  for (unsigned i = 0, e = Objects.size(); i != e; 
++i1.04k
) {
221
1.04k
    const StackObject &SO = Objects[i];
222
1.04k
    OS << "  fi#" << (int)(i-NumFixedObjects) << ": ";
223
1.04k
224
1.04k
    if (SO.StackID != 0)
225
0
      OS << "id=" << static_cast<unsigned>(SO.StackID) << ' ';
226
1.04k
227
1.04k
    if (SO.Size == ~0ULL) {
228
1
      OS << "dead\n";
229
1
      continue;
230
1
    }
231
1.04k
    if (SO.Size == 0)
232
1
      OS << "variable sized";
233
1.04k
    else
234
1.04k
      OS << "size=" << SO.Size;
235
1.04k
    OS << ", align=" << SO.Alignment;
236
1.04k
237
1.04k
    if (i < NumFixedObjects)
238
22
      OS << ", fixed";
239
1.04k
    if (i < NumFixedObjects || 
SO.SPOffset != -11.02k
) {
240
1.04k
      int64_t Off = SO.SPOffset - ValOffset;
241
1.04k
      OS << ", at location [SP";
242
1.04k
      if (Off > 0)
243
27
        OS << "+" << Off;
244
1.01k
      else if (Off < 0)
245
911
        OS << Off;
246
1.04k
      OS << "]";
247
1.04k
    }
248
1.04k
    OS << "\n";
249
1.04k
  }
250
506
}
251
252
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
253
LLVM_DUMP_METHOD void MachineFrameInfo::dump(const MachineFunction &MF) const {
254
  print(MF, dbgs());
255
}
256
#endif