Coverage Report

Created: 2020-09-22 08:39

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