Coverage Report

Created: 2019-07-24 05:18

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