Coverage Report

Created: 2022-07-16 07:03

/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
684
                   VTableLayout::AddressPointsMapTy &AddressPoints) {
26
684
  if (VTable.getBase() == MostDerivedClass) {
27
373
    assert(VTable.getBaseOffset().isZero() &&
28
373
           "Most derived class vtable must have a zero offset!");
29
    // This is a regular vtable.
30
0
    return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
31
373
  }
32
33
311
  return CGVT.GenerateConstructionVTable(MostDerivedClass,
34
311
                                         VTable.getBaseSubobject(),
35
311
                                         VTable.isVirtual(),
36
311
                                         Linkage,
37
311
                                         AddressPoints);
38
684
}
39
40
void
41
CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
42
                                  llvm::GlobalVariable::LinkageTypes Linkage,
43
373
                                  const CXXRecordDecl *RD) {
44
373
  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
45
373
  llvm::ArrayType *ArrayType =
46
373
      llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
47
48
373
  SmallVector<llvm::GlobalVariable *, 8> VTables;
49
373
  SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
50
373
  for (const VTTVTable *i = Builder.getVTTVTables().begin(),
51
1.05k
                       *e = Builder.getVTTVTables().end(); i != e; 
++i684
) {
52
684
    VTableAddressPoints.push_back(VTableAddressPointsMapTy());
53
684
    VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
54
684
                                         VTableAddressPoints.back()));
55
684
  }
56
57
373
  SmallVector<llvm::Constant *, 8> VTTComponents;
58
373
  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
697
      AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
70
697
      assert(AddressPoint.AddressPointIndex != 0 &&
71
697
             "Did not find ctor vtable address point!");
72
697
    }
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
373
  llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
91
92
373
  VTT->setInitializer(Init);
93
94
  // Set the correct linkage.
95
373
  VTT->setLinkage(Linkage);
96
97
373
  if (CGM.supportsCOMDAT() && 
VTT->isWeakForLinker()154
)
98
63
    VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName()));
99
373
}
100
101
613
llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
102
613
  assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
103
104
0
  SmallString<256> OutName;
105
613
  llvm::raw_svector_ostream Out(OutName);
106
613
  cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
107
613
      .mangleCXXVTT(RD, Out);
108
613
  StringRef Name = OutName.str();
109
110
  // This will also defer the definition of the VTT.
111
613
  (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
112
113
613
  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
114
115
613
  llvm::ArrayType *ArrayType =
116
613
    llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
117
613
  unsigned Align = CGM.getDataLayout().getABITypeAlignment(CGM.Int8PtrTy);
118
119
613
  llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
120
613
      Name, ArrayType, llvm::GlobalValue::ExternalLinkage, Align);
121
613
  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
122
613
  CGM.setGVProperties(GV, RD);
123
613
  return GV;
124
613
}
125
126
uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
127
186
                                        BaseSubobject Base) {
128
186
  BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
129
130
186
  SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
131
186
  if (I != SubVTTIndicies.end())
132
95
    return I->second;
133
134
91
  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
135
136
91
  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
137
91
       Builder.getSubVTTIndicies().begin(),
138
218
       E = Builder.getSubVTTIndicies().end(); I != E; 
++I127
) {
139
    // Insert all indices.
140
127
    BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
141
142
127
    SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
143
127
  }
144
145
91
  I = SubVTTIndicies.find(ClassSubobjectPair);
146
91
  assert(I != SubVTTIndicies.end() && "Did not find index!");
147
148
0
  return I->second;
149
186
}
150
151
uint64_t
152
CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
153
307
                                                BaseSubobject Base) {
154
307
  SecondaryVirtualPointerIndicesMapTy::iterator I =
155
307
    SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
156
157
307
  if (I != SecondaryVirtualPointerIndices.end())
158
127
    return I->second;
159
160
180
  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
161
162
  // Insert all secondary vpointer indices.
163
180
  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
164
180
       Builder.getSecondaryVirtualPointerIndices().begin(),
165
465
       E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; 
++I285
) {
166
285
    std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
167
285
      std::make_pair(RD, I->first);
168
169
285
    SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
170
285
  }
171
172
180
  I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
173
180
  assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
174
175
0
  return I->second;
176
307
}