Coverage Report

Created: 2021-01-23 06:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/CGVTT.cpp
Line
Count
Source
1
//===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===//
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 contains code dealing with C++ code generation of VTTs (vtable tables).
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CodeGenModule.h"
14
#include "CGCXXABI.h"
15
#include "clang/AST/RecordLayout.h"
16
#include "clang/AST/VTTBuilder.h"
17
using namespace clang;
18
using namespace CodeGen;
19
20
static llvm::GlobalVariable *
21
GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM,
22
                   const CXXRecordDecl *MostDerivedClass,
23
                   const VTTVTable &VTable,
24
                   llvm::GlobalVariable::LinkageTypes Linkage,
25
671
                   VTableLayout::AddressPointsMapTy &AddressPoints) {
26
671
  if (VTable.getBase() == MostDerivedClass) {
27
366
    assert(VTable.getBaseOffset().isZero() &&
28
366
           "Most derived class vtable must have a zero offset!");
29
    // This is a regular vtable.
30
366
    return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
31
366
  }
32
33
305
  return CGVT.GenerateConstructionVTable(MostDerivedClass,
34
305
                                         VTable.getBaseSubobject(),
35
305
                                         VTable.isVirtual(),
36
305
                                         Linkage,
37
305
                                         AddressPoints);
38
305
}
39
40
void
41
CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
42
                                  llvm::GlobalVariable::LinkageTypes Linkage,
43
366
                                  const CXXRecordDecl *RD) {
44
366
  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
45
366
  llvm::ArrayType *ArrayType =
46
366
      llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
47
48
366
  SmallVector<llvm::GlobalVariable *, 8> VTables;
49
366
  SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
50
366
  for (const VTTVTable *i = Builder.getVTTVTables().begin(),
51
1.03k
                       *e = Builder.getVTTVTables().end(); i != e; 
++i671
) {
52
671
    VTableAddressPoints.push_back(VTableAddressPointsMapTy());
53
671
    VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
54
671
                                         VTableAddressPoints.back()));
55
671
  }
56
57
366
  SmallVector<llvm::Constant *, 8> VTTComponents;
58
366
  for (const VTTComponent *i = Builder.getVTTComponents().begin(),
59
1.83k
                          *e = Builder.getVTTComponents().end(); i != e; 
++i1.47k
) {
60
1.47k
    const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
61
1.47k
    llvm::GlobalVariable *VTable = VTables[i->VTableIndex];
62
1.47k
    VTableLayout::AddressPointLocation AddressPoint;
63
1.47k
    if (VTTVT.getBase() == RD) {
64
      // Just get the address point for the regular vtable.
65
785
      AddressPoint =
66
785
          getItaniumVTableContext().getVTableLayout(RD).getAddressPoint(
67
785
              i->VTableBase);
68
687
    } else {
69
687
      AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
70
687
      assert(AddressPoint.AddressPointIndex != 0 &&
71
687
             "Did not find ctor vtable address point!");
72
687
    }
73
74
1.47k
     llvm::Value *Idxs[] = {
75
1.47k
       llvm::ConstantInt::get(CGM.Int32Ty, 0),
76
1.47k
       llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex),
77
1.47k
       llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex),
78
1.47k
     };
79
80
1.47k
     llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
81
1.47k
         VTable->getValueType(), VTable, Idxs, /*InBounds=*/true,
82
1.47k
         /*InRangeIndex=*/1);
83
84
1.47k
     Init = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(Init,
85
1.47k
                                                                 CGM.Int8PtrTy);
86
87
1.47k
     VTTComponents.push_back(Init);
88
1.47k
  }
89
90
366
  llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
91
92
366
  VTT->setInitializer(Init);
93
94
  // Set the correct linkage.
95
366
  VTT->setLinkage(Linkage);
96
97
366
  if (CGM.supportsCOMDAT() && 
VTT->isWeakForLinker()148
)
98
64
    VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName()));
99
100
  // Set the right visibility.
101
366
  CGM.setGVProperties(VTT, RD);
102
366
}
103
104
594
llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
105
594
  assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
106
107
594
  SmallString<256> OutName;
108
594
  llvm::raw_svector_ostream Out(OutName);
109
594
  cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
110
594
      .mangleCXXVTT(RD, Out);
111
594
  StringRef Name = OutName.str();
112
113
  // This will also defer the definition of the VTT.
114
594
  (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
115
116
594
  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
117
118
594
  llvm::ArrayType *ArrayType =
119
594
    llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
120
594
  unsigned Align = CGM.getDataLayout().getABITypeAlignment(CGM.Int8PtrTy);
121
122
594
  llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
123
594
      Name, ArrayType, llvm::GlobalValue::ExternalLinkage, Align);
124
594
  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
125
594
  return GV;
126
594
}
127
128
uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
129
174
                                        BaseSubobject Base) {
130
174
  BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
131
132
174
  SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
133
174
  if (I != SubVTTIndicies.end())
134
88
    return I->second;
135
136
86
  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
137
138
86
  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
139
86
       Builder.getSubVTTIndicies().begin(),
140
207
       E = Builder.getSubVTTIndicies().end(); I != E; 
++I121
) {
141
    // Insert all indices.
142
121
    BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
143
144
121
    SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
145
121
  }
146
147
86
  I = SubVTTIndicies.find(ClassSubobjectPair);
148
86
  assert(I != SubVTTIndicies.end() && "Did not find index!");
149
150
86
  return I->second;
151
86
}
152
153
uint64_t
154
CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
155
281
                                                BaseSubobject Base) {
156
281
  SecondaryVirtualPointerIndicesMapTy::iterator I =
157
281
    SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
158
159
281
  if (I != SecondaryVirtualPointerIndices.end())
160
114
    return I->second;
161
162
167
  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
163
164
  // Insert all secondary vpointer indices.
165
167
  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
166
167
       Builder.getSecondaryVirtualPointerIndices().begin(),
167
428
       E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; 
++I261
) {
168
261
    std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
169
261
      std::make_pair(RD, I->first);
170
171
261
    SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
172
261
  }
173
174
167
  I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
175
167
  assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
176
177
167
  return I->second;
178
167
}