clang  10.0.0svn
CGVTT.cpp
Go to the documentation of this file.
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 *
22  const CXXRecordDecl *MostDerivedClass,
23  const VTTVTable &VTable,
24  llvm::GlobalVariable::LinkageTypes Linkage,
25  VTableLayout::AddressPointsMapTy &AddressPoints) {
26  if (VTable.getBase() == MostDerivedClass) {
27  assert(VTable.getBaseOffset().isZero() &&
28  "Most derived class vtable must have a zero offset!");
29  // This is a regular vtable.
30  return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
31  }
32 
33  return CGVT.GenerateConstructionVTable(MostDerivedClass,
34  VTable.getBaseSubobject(),
35  VTable.isVirtual(),
36  Linkage,
37  AddressPoints);
38 }
39 
40 void
41 CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
42  llvm::GlobalVariable::LinkageTypes Linkage,
43  const CXXRecordDecl *RD) {
44  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
45 
46  llvm::Type *Int8PtrTy = CGM.Int8PtrTy, *Int32Ty = CGM.Int32Ty;
47  llvm::ArrayType *ArrayType =
48  llvm::ArrayType::get(Int8PtrTy, Builder.getVTTComponents().size());
49 
51  SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
52  for (const VTTVTable *i = Builder.getVTTVTables().begin(),
53  *e = Builder.getVTTVTables().end(); i != e; ++i) {
54  VTableAddressPoints.push_back(VTableAddressPointsMapTy());
55  VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
56  VTableAddressPoints.back()));
57  }
58 
60  for (const VTTComponent *i = Builder.getVTTComponents().begin(),
61  *e = Builder.getVTTComponents().end(); i != e; ++i) {
62  const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
63  llvm::GlobalVariable *VTable = VTables[i->VTableIndex];
65  if (VTTVT.getBase() == RD) {
66  // Just get the address point for the regular vtable.
67  AddressPoint =
69  i->VTableBase);
70  } else {
71  AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
72  assert(AddressPoint.AddressPointIndex != 0 &&
73  "Did not find ctor vtable address point!");
74  }
75 
76  llvm::Value *Idxs[] = {
77  llvm::ConstantInt::get(Int32Ty, 0),
78  llvm::ConstantInt::get(Int32Ty, AddressPoint.VTableIndex),
79  llvm::ConstantInt::get(Int32Ty, AddressPoint.AddressPointIndex),
80  };
81 
82  llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
83  VTable->getValueType(), VTable, Idxs, /*InBounds=*/true,
84  /*InRangeIndex=*/1);
85 
86  Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
87 
88  VTTComponents.push_back(Init);
89  }
90 
91  llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
92 
93  VTT->setInitializer(Init);
94 
95  // Set the correct linkage.
96  VTT->setLinkage(Linkage);
97 
98  if (CGM.supportsCOMDAT() && VTT->isWeakForLinker())
99  VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName()));
100 
101  // Set the right visibility.
102  CGM.setGVProperties(VTT, RD);
103 }
104 
105 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
106  assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
107 
108  SmallString<256> OutName;
109  llvm::raw_svector_ostream Out(OutName);
110  cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
111  .mangleCXXVTT(RD, Out);
112  StringRef Name = OutName.str();
113 
114  // This will also defer the definition of the VTT.
115  (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
116 
117  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
118 
119  llvm::ArrayType *ArrayType =
120  llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
121  unsigned Align = CGM.getDataLayout().getABITypeAlignment(CGM.Int8PtrTy);
122 
123  llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
124  Name, ArrayType, llvm::GlobalValue::ExternalLinkage, Align);
125  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
126  return GV;
127 }
128 
131  BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
132 
133  SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
134  if (I != SubVTTIndicies.end())
135  return I->second;
136 
137  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
138 
139  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
140  Builder.getSubVTTIndicies().begin(),
141  E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
142  // Insert all indices.
143  BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
144 
145  SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
146  }
147 
148  I = SubVTTIndicies.find(ClassSubobjectPair);
149  assert(I != SubVTTIndicies.end() && "Did not find index!");
150 
151  return I->second;
152 }
153 
154 uint64_t
157  SecondaryVirtualPointerIndicesMapTy::iterator I =
158  SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
159 
160  if (I != SecondaryVirtualPointerIndices.end())
161  return I->second;
162 
163  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
164 
165  // Insert all secondary vpointer indices.
166  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
167  Builder.getSecondaryVirtualPointerIndices().begin(),
168  E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
169  std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
170  std::make_pair(RD, I->first);
171 
172  SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
173  }
174 
175  I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
176  assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
177 
178  return I->second;
179 }
static llvm::GlobalVariable * GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM, const CXXRecordDecl *MostDerivedClass, const VTTVTable &VTable, llvm::GlobalVariable::LinkageTypes Linkage, VTableLayout::AddressPointsMapTy &AddressPoints)
Definition: CGVTT.cpp:21
const llvm::DataLayout & getDataLayout() const
void EmitVTTDefinition(llvm::GlobalVariable *VTT, llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD)
EmitVTTDefinition - Emit the definition of the given vtable.
Definition: CGVTT.cpp:41
External linkage, which indicates that the entity can be referred to from other translation units...
Definition: Linkage.h:59
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:794
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2850
AddressPointLocation getAddressPoint(BaseSubobject Base) const
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:115
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:23
uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base)
getSubVTTIndex - Return the index of the sub-VTT for the base class of the given record decl...
Definition: CGVTT.cpp:129
bool isVirtual() const
Definition: VTTBuilder.h:52
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:37
const CXXRecordDecl * getBase() const
Definition: VTTBuilder.h:44
ItaniumVTableContext & getItaniumVTableContext()
Definition: CGVTables.h:78
ASTContext & getContext() const
The l-value was considered opaque, so the alignment was determined from a type.
void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const
Set visibility, dllimport/dllexport and dso_local.
llvm::GlobalVariable * GetAddrOfVTT(const CXXRecordDecl *RD)
GetAddrOfVTT - Get the address of the VTT for the given record decl.
Definition: CGVTT.cpp:105
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:96
llvm::GlobalVariable * GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, bool BaseIsVirtual, llvm::GlobalVariable::LinkageTypes Linkage, VTableAddressPointsMapTy &AddressPoints)
GenerateConstructionVTable - Generate a construction vtable for the given base subobject.
Definition: CGVTables.cpp:756
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
virtual llvm::GlobalVariable * getAddrOfVTable(const CXXRecordDecl *RD, CharUnits VPtrOffset)=0
Get the address of the vtable for the given record decl which should be used for the vptr at the give...
llvm::Module & getModule() const
uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, BaseSubobject Base)
getSecondaryVirtualPointerIndex - Return the index in the VTT where the virtual pointer for the given...
Definition: CGVTT.cpp:155
Class for building VTT layout information.
Definition: VTTBuilder.h:71
BaseSubobject getBaseSubobject() const
Definition: VTTBuilder.h:56
const VTableLayout & getVTableLayout(const CXXRecordDecl *RD)
llvm::DenseMap< BaseSubobject, AddressPointLocation > AddressPointsMapTy
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
CGCXXABI & getCXXABI() const
llvm::GlobalVariable * CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage, unsigned Alignment)
Will return a global variable of the given type.
CharUnits getBaseOffset() const
Definition: VTTBuilder.h:48