clang  6.0.0svn
CGVTT.cpp
Go to the documentation of this file.
1 //===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code dealing with C++ code generation of VTTs (vtable tables).
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenModule.h"
15 #include "CGCXXABI.h"
16 #include "clang/AST/RecordLayout.h"
17 #include "clang/AST/VTTBuilder.h"
18 using namespace clang;
19 using namespace CodeGen;
20 
21 static llvm::GlobalVariable *
23  const CXXRecordDecl *MostDerivedClass,
24  const VTTVTable &VTable,
25  llvm::GlobalVariable::LinkageTypes Linkage,
26  VTableLayout::AddressPointsMapTy &AddressPoints) {
27  if (VTable.getBase() == MostDerivedClass) {
28  assert(VTable.getBaseOffset().isZero() &&
29  "Most derived class vtable must have a zero offset!");
30  // This is a regular vtable.
31  return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
32  }
33 
34  return CGVT.GenerateConstructionVTable(MostDerivedClass,
35  VTable.getBaseSubobject(),
36  VTable.isVirtual(),
37  Linkage,
38  AddressPoints);
39 }
40 
41 void
42 CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
43  llvm::GlobalVariable::LinkageTypes Linkage,
44  const CXXRecordDecl *RD) {
45  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
46 
47  llvm::Type *Int8PtrTy = CGM.Int8PtrTy, *Int32Ty = CGM.Int32Ty;
48  llvm::ArrayType *ArrayType =
49  llvm::ArrayType::get(Int8PtrTy, Builder.getVTTComponents().size());
50 
52  SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
53  for (const VTTVTable *i = Builder.getVTTVTables().begin(),
54  *e = Builder.getVTTVTables().end(); i != e; ++i) {
55  VTableAddressPoints.push_back(VTableAddressPointsMapTy());
56  VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
57  VTableAddressPoints.back()));
58  }
59 
61  for (const VTTComponent *i = Builder.getVTTComponents().begin(),
62  *e = Builder.getVTTComponents().end(); i != e; ++i) {
63  const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
64  llvm::GlobalVariable *VTable = VTables[i->VTableIndex];
66  if (VTTVT.getBase() == RD) {
67  // Just get the address point for the regular vtable.
68  AddressPoint =
70  i->VTableBase);
71  } else {
72  AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
73  assert(AddressPoint.AddressPointIndex != 0 &&
74  "Did not find ctor vtable address point!");
75  }
76 
77  llvm::Value *Idxs[] = {
78  llvm::ConstantInt::get(Int32Ty, 0),
79  llvm::ConstantInt::get(Int32Ty, AddressPoint.VTableIndex),
80  llvm::ConstantInt::get(Int32Ty, AddressPoint.AddressPointIndex),
81  };
82 
83  llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
84  VTable->getValueType(), VTable, Idxs, /*InBounds=*/true,
85  /*InRangeIndex=*/1);
86 
87  Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
88 
89  VTTComponents.push_back(Init);
90  }
91 
92  llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
93 
94  VTT->setInitializer(Init);
95 
96  // Set the correct linkage.
97  VTT->setLinkage(Linkage);
98 
99  if (CGM.supportsCOMDAT() && VTT->isWeakForLinker())
100  VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName()));
101 
102  // Set the right visibility.
103  CGM.setGlobalVisibility(VTT, RD, ForDefinition);
104 }
105 
106 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
107  assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
108 
109  SmallString<256> OutName;
110  llvm::raw_svector_ostream Out(OutName);
111  cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
112  .mangleCXXVTT(RD, Out);
113  StringRef Name = OutName.str();
114 
115  // This will also defer the definition of the VTT.
116  (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
117 
118  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
119 
120  llvm::ArrayType *ArrayType =
121  llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
122 
123  llvm::GlobalVariable *GV =
124  CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
126  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
127  return GV;
128 }
129 
132  BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
133 
134  SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
135  if (I != SubVTTIndicies.end())
136  return I->second;
137 
138  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
139 
140  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
141  Builder.getSubVTTIndicies().begin(),
142  E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
143  // Insert all indices.
144  BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
145 
146  SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
147  }
148 
149  I = SubVTTIndicies.find(ClassSubobjectPair);
150  assert(I != SubVTTIndicies.end() && "Did not find index!");
151 
152  return I->second;
153 }
154 
155 uint64_t
158  SecondaryVirtualPointerIndicesMapTy::iterator I =
159  SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
160 
161  if (I != SecondaryVirtualPointerIndices.end())
162  return I->second;
163 
164  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
165 
166  // Insert all secondary vpointer indices.
167  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
168  Builder.getSecondaryVirtualPointerIndices().begin(),
169  E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
170  std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
171  std::make_pair(RD, I->first);
172 
173  SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
174  }
175 
176  I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
177  assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
178 
179  return I->second;
180 }
static llvm::GlobalVariable * GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM, const CXXRecordDecl *MostDerivedClass, const VTTVTable &VTable, llvm::GlobalVariable::LinkageTypes Linkage, VTableLayout::AddressPointsMapTy &AddressPoints)
Definition: CGVTT.cpp:22
void EmitVTTDefinition(llvm::GlobalVariable *VTT, llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD)
EmitVTTDefinition - Emit the definition of the given vtable.
Definition: CGVTT.cpp:42
External linkage, which indicates that the entity can be referred to from other translation units...
Definition: Linkage.h:61
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:788
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2560
AddressPointLocation getAddressPoint(BaseSubobject Base) const
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:25
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:130
bool isVirtual() const
Definition: VTTBuilder.h:53
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
const CXXRecordDecl * getBase() const
Definition: VTTBuilder.h:45
void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D, ForDefinition_t IsForDefinition) const
Set the visibility for the given LLVM GlobalValue.
ItaniumVTableContext & getItaniumVTableContext()
Definition: CGVTables.h:81
ASTContext & getContext() const
llvm::GlobalVariable * CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage)
Will return a global variable of the given type.
The l-value was considered opaque, so the alignment was determined from a type.
llvm::GlobalVariable * GetAddrOfVTT(const CXXRecordDecl *RD)
GetAddrOfVTT - Get the address of the VTT for the given record decl.
Definition: CGVTT.cpp:106
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:97
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:697
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:156
Class for building VTT layout information.
Definition: VTTBuilder.h:72
BaseSubobject getBaseSubobject() const
Definition: VTTBuilder.h:57
const VTableLayout & getVTableLayout(const CXXRecordDecl *RD)
llvm::DenseMap< BaseSubobject, AddressPointLocation > AddressPointsMapTy
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
CGCXXABI & getCXXABI() const
CharUnits getBaseOffset() const
Definition: VTTBuilder.h:49