Coverage Report

Created: 2020-02-15 09:57

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