Coverage Report

Created: 2021-09-21 08:58

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