Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/CallingConvLower.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- CallingConvLower.cpp - Calling Conventions ------------------------===//
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 implements the CCState class, used for lowering and implementing
10
// calling conventions.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/CodeGen/CallingConvLower.h"
15
#include "llvm/CodeGen/MachineFrameInfo.h"
16
#include "llvm/CodeGen/MachineRegisterInfo.h"
17
#include "llvm/CodeGen/TargetLowering.h"
18
#include "llvm/CodeGen/TargetRegisterInfo.h"
19
#include "llvm/CodeGen/TargetSubtargetInfo.h"
20
#include "llvm/IR/DataLayout.h"
21
#include "llvm/Support/Debug.h"
22
#include "llvm/Support/ErrorHandling.h"
23
#include "llvm/Support/SaveAndRestore.h"
24
#include "llvm/Support/raw_ostream.h"
25
#include <algorithm>
26
27
using namespace llvm;
28
29
CCState::CCState(CallingConv::ID CC, bool isVarArg, MachineFunction &mf,
30
                 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C)
31
    : CallingConv(CC), IsVarArg(isVarArg), MF(mf),
32
4.72M
      TRI(*MF.getSubtarget().getRegisterInfo()), Locs(locs), Context(C) {
33
4.72M
  // No stack is used.
34
4.72M
  StackOffset = 0;
35
4.72M
  MaxStackArgAlign = 1;
36
4.72M
37
4.72M
  clearByValRegsInfo();
38
4.72M
  UsedRegs.resize((TRI.getNumRegs()+31)/32);
39
4.72M
}
40
41
/// Allocate space on the stack large enough to pass an argument by value.
42
/// The size and alignment information of the argument is encoded in
43
/// its parameter attribute.
44
void CCState::HandleByVal(unsigned ValNo, MVT ValVT,
45
                          MVT LocVT, CCValAssign::LocInfo LocInfo,
46
                          int MinSize, int MinAlign,
47
1.62k
                          ISD::ArgFlagsTy ArgFlags) {
48
1.62k
  unsigned Align = ArgFlags.getByValAlign();
49
1.62k
  unsigned Size  = ArgFlags.getByValSize();
50
1.62k
  if (MinSize > (int)Size)
51
79
    Size = MinSize;
52
1.62k
  if (MinAlign > (int)Align)
53
254
    Align = MinAlign;
54
1.62k
  ensureMaxAlignment(Align);
55
1.62k
  MF.getSubtarget().getTargetLowering()->HandleByVal(this, Size, Align);
56
1.62k
  Size = unsigned(alignTo(Size, MinAlign));
57
1.62k
  unsigned Offset = AllocateStack(Size, Align);
58
1.62k
  addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
59
1.62k
}
60
61
/// Mark a register and all of its aliases as allocated.
62
12.6M
void CCState::MarkAllocated(unsigned Reg) {
63
72.5M
  for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); 
++AI59.8M
)
64
59.8M
    UsedRegs[*AI/32] |= 1 << (*AI&31);
65
12.6M
}
66
67
70
bool CCState::IsShadowAllocatedReg(unsigned Reg) const {
68
70
  if (!isAllocated(Reg))
69
0
    return false;
70
70
71
197
  
for (auto const &ValAssign : Locs)70
{
72
197
    if (ValAssign.isRegLoc()) {
73
197
      for (MCRegAliasIterator AI(ValAssign.getLocReg(), &TRI, true);
74
1.11k
           AI.isValid(); 
++AI919
) {
75
975
        if (*AI == Reg)
76
56
          return false;
77
975
      }
78
197
    }
79
197
  }
80
70
  
return true14
;
81
70
}
82
83
/// Analyze an array of argument values,
84
/// incorporating info about the formals into this state.
85
void
86
CCState::AnalyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Ins,
87
195k
                                CCAssignFn Fn) {
88
195k
  unsigned NumArgs = Ins.size();
89
195k
90
595k
  for (unsigned i = 0; i != NumArgs; 
++i400k
) {
91
400k
    MVT ArgVT = Ins[i].VT;
92
400k
    ISD::ArgFlagsTy ArgFlags = Ins[i].Flags;
93
400k
    if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
94
#ifndef NDEBUG
95
      dbgs() << "Formal argument #" << i << " has unhandled type "
96
             << EVT(ArgVT).getEVTString() << '\n';
97
#endif
98
0
      llvm_unreachable(nullptr);
99
0
    }
100
400k
  }
101
195k
}
102
103
/// Analyze the return values of a function, returning true if the return can
104
/// be performed without sret-demotion and false otherwise.
105
bool CCState::CheckReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
106
701k
                          CCAssignFn Fn) {
107
701k
  // Determine which register each value should be copied into.
108
1.16M
  for (unsigned i = 0, e = Outs.size(); i != e; 
++i467k
) {
109
468k
    MVT VT = Outs[i].VT;
110
468k
    ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
111
468k
    if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this))
112
1.31k
      return false;
113
468k
  }
114
701k
  
return true700k
;
115
701k
}
116
117
/// Analyze the returned values of a return,
118
/// incorporating info about the result values into this state.
119
void CCState::AnalyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs,
120
238k
                            CCAssignFn Fn) {
121
238k
  // Determine which register each value should be copied into.
122
441k
  for (unsigned i = 0, e = Outs.size(); i != e; 
++i202k
) {
123
202k
    MVT VT = Outs[i].VT;
124
202k
    ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
125
202k
    if (Fn(i, VT, VT, CCValAssign::Full, ArgFlags, *this)) {
126
#ifndef NDEBUG
127
      dbgs() << "Return operand #" << i << " has unhandled type "
128
             << EVT(VT).getEVTString() << '\n';
129
#endif
130
0
      llvm_unreachable(nullptr);
131
0
    }
132
202k
  }
133
238k
}
134
135
/// Analyze the outgoing arguments to a call,
136
/// incorporating info about the passed values into this state.
137
void CCState::AnalyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Outs,
138
271k
                                  CCAssignFn Fn) {
139
271k
  unsigned NumOps = Outs.size();
140
952k
  for (unsigned i = 0; i != NumOps; 
++i681k
) {
141
681k
    MVT ArgVT = Outs[i].VT;
142
681k
    ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
143
681k
    if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
144
#ifndef NDEBUG
145
      dbgs() << "Call operand #" << i << " has unhandled type "
146
             << EVT(ArgVT).getEVTString() << '\n';
147
#endif
148
0
      llvm_unreachable(nullptr);
149
0
    }
150
681k
  }
151
271k
}
152
153
/// Same as above except it takes vectors of types and argument flags.
154
void CCState::AnalyzeCallOperands(SmallVectorImpl<MVT> &ArgVTs,
155
                                  SmallVectorImpl<ISD::ArgFlagsTy> &Flags,
156
1.76k
                                  CCAssignFn Fn) {
157
1.76k
  unsigned NumOps = ArgVTs.size();
158
5.57k
  for (unsigned i = 0; i != NumOps; 
++i3.80k
) {
159
3.80k
    MVT ArgVT = ArgVTs[i];
160
3.80k
    ISD::ArgFlagsTy ArgFlags = Flags[i];
161
3.80k
    if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, *this)) {
162
#ifndef NDEBUG
163
      dbgs() << "Call operand #" << i << " has unhandled type "
164
             << EVT(ArgVT).getEVTString() << '\n';
165
#endif
166
0
      llvm_unreachable(nullptr);
167
0
    }
168
3.80k
  }
169
1.76k
}
170
171
/// Analyze the return values of a call, incorporating info about the passed
172
/// values into this state.
173
void CCState::AnalyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins,
174
396k
                                CCAssignFn Fn) {
175
612k
  for (unsigned i = 0, e = Ins.size(); i != e; 
++i215k
) {
176
215k
    MVT VT = Ins[i].VT;
177
215k
    ISD::ArgFlagsTy Flags = Ins[i].Flags;
178
215k
    if (Fn(i, VT, VT, CCValAssign::Full, Flags, *this)) {
179
#ifndef NDEBUG
180
      dbgs() << "Call result #" << i << " has unhandled type "
181
             << EVT(VT).getEVTString() << '\n';
182
#endif
183
0
      llvm_unreachable(nullptr);
184
0
    }
185
215k
  }
186
396k
}
187
188
/// Same as above except it's specialized for calls that produce a single value.
189
331
void CCState::AnalyzeCallResult(MVT VT, CCAssignFn Fn) {
190
331
  if (Fn(0, VT, VT, CCValAssign::Full, ISD::ArgFlagsTy(), *this)) {
191
#ifndef NDEBUG
192
    dbgs() << "Call result has unhandled type "
193
           << EVT(VT).getEVTString() << '\n';
194
#endif
195
0
    llvm_unreachable(nullptr);
196
0
  }
197
331
}
198
199
73
static bool isValueTypeInRegForCC(CallingConv::ID CC, MVT VT) {
200
73
  if (VT.isVector())
201
33
    return true; // Assume -msse-regparm might be in effect.
202
40
  if (!VT.isInteger())
203
2
    return false;
204
38
  if (CC == CallingConv::X86_VectorCall || 
CC == CallingConv::X86_FastCall35
)
205
6
    return true;
206
32
  return false;
207
32
}
208
209
void CCState::getRemainingRegParmsForType(SmallVectorImpl<MCPhysReg> &Regs,
210
73
                                          MVT VT, CCAssignFn Fn) {
211
73
  unsigned SavedStackOffset = StackOffset;
212
73
  unsigned SavedMaxStackArgAlign = MaxStackArgAlign;
213
73
  unsigned NumLocs = Locs.size();
214
73
215
73
  // Set the 'inreg' flag if it is used for this calling convention.
216
73
  ISD::ArgFlagsTy Flags;
217
73
  if (isValueTypeInRegForCC(CallingConv, VT))
218
39
    Flags.setInReg();
219
73
220
73
  // Allocate something of this value type repeatedly until we get assigned a
221
73
  // location in memory.
222
73
  bool HaveRegParm = true;
223
499
  while (HaveRegParm) {
224
426
    if (Fn(0, VT, VT, CCValAssign::Full, Flags, *this)) {
225
#ifndef NDEBUG
226
      dbgs() << "Call has unhandled type " << EVT(VT).getEVTString()
227
             << " while computing remaining regparms\n";
228
#endif
229
0
      llvm_unreachable(nullptr);
230
0
    }
231
426
    HaveRegParm = Locs.back().isRegLoc();
232
426
  }
233
73
234
73
  // Copy all the registers from the value locations we added.
235
73
  assert(NumLocs < Locs.size() && "CC assignment failed to add location");
236
499
  for (unsigned I = NumLocs, E = Locs.size(); I != E; 
++I426
)
237
426
    if (Locs[I].isRegLoc())
238
353
      Regs.push_back(MCPhysReg(Locs[I].getLocReg()));
239
73
240
73
  // Clear the assigned values and stack memory. We leave the registers marked
241
73
  // as allocated so that future queries don't return the same registers, i.e.
242
73
  // when i64 and f64 are both passed in GPRs.
243
73
  StackOffset = SavedStackOffset;
244
73
  MaxStackArgAlign = SavedMaxStackArgAlign;
245
73
  Locs.resize(NumLocs);
246
73
}
247
248
void CCState::analyzeMustTailForwardedRegisters(
249
    SmallVectorImpl<ForwardedRegister> &Forwards, ArrayRef<MVT> RegParmTypes,
250
38
    CCAssignFn Fn) {
251
38
  // Oftentimes calling conventions will not user register parameters for
252
38
  // variadic functions, so we need to assume we're not variadic so that we get
253
38
  // all the registers that might be used in a non-variadic call.
254
38
  SaveAndRestore<bool> SavedVarArg(IsVarArg, false);
255
38
  SaveAndRestore<bool> SavedMustTail(AnalyzingMustTailForwardedRegs, true);
256
38
257
73
  for (MVT RegVT : RegParmTypes) {
258
73
    SmallVector<MCPhysReg, 8> RemainingRegs;
259
73
    getRemainingRegParmsForType(RemainingRegs, RegVT, Fn);
260
73
    const TargetLowering *TL = MF.getSubtarget().getTargetLowering();
261
73
    const TargetRegisterClass *RC = TL->getRegClassFor(RegVT);
262
353
    for (MCPhysReg PReg : RemainingRegs) {
263
353
      unsigned VReg = MF.addLiveIn(PReg, RC);
264
353
      Forwards.push_back(ForwardedRegister(VReg, PReg, RegVT));
265
353
    }
266
73
  }
267
38
}
268
269
bool CCState::resultsCompatible(CallingConv::ID CalleeCC,
270
                                CallingConv::ID CallerCC, MachineFunction &MF,
271
                                LLVMContext &C,
272
                                const SmallVectorImpl<ISD::InputArg> &Ins,
273
64.8k
                                CCAssignFn CalleeFn, CCAssignFn CallerFn) {
274
64.8k
  if (CalleeCC == CallerCC)
275
63.9k
    return true;
276
912
  SmallVector<CCValAssign, 4> RVLocs1;
277
912
  CCState CCInfo1(CalleeCC, false, MF, RVLocs1, C);
278
912
  CCInfo1.AnalyzeCallResult(Ins, CalleeFn);
279
912
280
912
  SmallVector<CCValAssign, 4> RVLocs2;
281
912
  CCState CCInfo2(CallerCC, false, MF, RVLocs2, C);
282
912
  CCInfo2.AnalyzeCallResult(Ins, CallerFn);
283
912
284
912
  if (RVLocs1.size() != RVLocs2.size())
285
18
    return false;
286
1.32k
  
for (unsigned I = 0, E = RVLocs1.size(); 894
I != E;
++I432
) {
287
439
    const CCValAssign &Loc1 = RVLocs1[I];
288
439
    const CCValAssign &Loc2 = RVLocs2[I];
289
439
    if (Loc1.getLocInfo() != Loc2.getLocInfo())
290
6
      return false;
291
433
    bool RegLoc1 = Loc1.isRegLoc();
292
433
    if (RegLoc1 != Loc2.isRegLoc())
293
0
      return false;
294
433
    if (RegLoc1) {
295
433
      if (Loc1.getLocReg() != Loc2.getLocReg())
296
1
        return false;
297
0
    } else {
298
0
      if (Loc1.getLocMemOffset() != Loc2.getLocMemOffset())
299
0
        return false;
300
0
    }
301
433
  }
302
894
  
return true887
;
303
894
}