mirror of
https://github.com/llvm/llvm-project.git
synced 2025-05-05 19:46:06 +00:00

As part of the migration to ptradd (https://discourse.llvm.org/t/rfc-replacing-getelementptr-with-ptradd/68699), we need to change the representation of the `inrange` attribute, which is used for vtable splitting. Currently, inrange is specified as follows: ``` getelementptr inbounds ({ [4 x ptr], [4 x ptr] }, ptr @vt, i64 0, inrange i32 1, i64 2) ``` The `inrange` is placed on a GEP index, and all accesses must be "in range" of that index. The new representation is as follows: ``` getelementptr inbounds inrange(-16, 16) ({ [4 x ptr], [4 x ptr] }, ptr @vt, i64 0, i32 1, i64 2) ``` This specifies which offsets are "in range" of the GEP result. The new representation will continue working when canonicalizing to ptradd representation: ``` getelementptr inbounds inrange(-16, 16) (i8, ptr @vt, i64 48) ``` The inrange offsets are relative to the return value of the GEP. An alternative design could make them relative to the source pointer instead. The result-relative format was chosen on the off-chance that we want to extend support to non-constant GEPs in the future, in which case this variant is more expressive. This implementation "upgrades" the old inrange representation in bitcode by simply dropping it. This is a very niche feature, and I don't think trying to upgrade it is worthwhile. Let me know if you disagree.
188 lines
7.5 KiB
C++
188 lines
7.5 KiB
C++
//===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This contains code dealing with C++ code generation of VTTs (vtable tables).
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "CodeGenModule.h"
|
|
#include "CGCXXABI.h"
|
|
#include "clang/AST/RecordLayout.h"
|
|
#include "clang/AST/VTTBuilder.h"
|
|
using namespace clang;
|
|
using namespace CodeGen;
|
|
|
|
static llvm::GlobalVariable *
|
|
GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM,
|
|
const CXXRecordDecl *MostDerivedClass,
|
|
const VTTVTable &VTable,
|
|
llvm::GlobalVariable::LinkageTypes Linkage,
|
|
VTableLayout::AddressPointsMapTy &AddressPoints) {
|
|
if (VTable.getBase() == MostDerivedClass) {
|
|
assert(VTable.getBaseOffset().isZero() &&
|
|
"Most derived class vtable must have a zero offset!");
|
|
// This is a regular vtable.
|
|
return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
|
|
}
|
|
|
|
return CGVT.GenerateConstructionVTable(MostDerivedClass,
|
|
VTable.getBaseSubobject(),
|
|
VTable.isVirtual(),
|
|
Linkage,
|
|
AddressPoints);
|
|
}
|
|
|
|
void
|
|
CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
|
|
llvm::GlobalVariable::LinkageTypes Linkage,
|
|
const CXXRecordDecl *RD) {
|
|
VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
|
|
llvm::ArrayType *ArrayType = llvm::ArrayType::get(
|
|
CGM.GlobalsInt8PtrTy, Builder.getVTTComponents().size());
|
|
|
|
SmallVector<llvm::GlobalVariable *, 8> VTables;
|
|
SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
|
|
for (const VTTVTable *i = Builder.getVTTVTables().begin(),
|
|
*e = Builder.getVTTVTables().end(); i != e; ++i) {
|
|
VTableAddressPoints.push_back(VTableAddressPointsMapTy());
|
|
VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
|
|
VTableAddressPoints.back()));
|
|
}
|
|
|
|
SmallVector<llvm::Constant *, 8> VTTComponents;
|
|
for (const VTTComponent *i = Builder.getVTTComponents().begin(),
|
|
*e = Builder.getVTTComponents().end(); i != e; ++i) {
|
|
const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
|
|
llvm::GlobalVariable *VTable = VTables[i->VTableIndex];
|
|
VTableLayout::AddressPointLocation AddressPoint;
|
|
if (VTTVT.getBase() == RD) {
|
|
// Just get the address point for the regular vtable.
|
|
AddressPoint =
|
|
getItaniumVTableContext().getVTableLayout(RD).getAddressPoint(
|
|
i->VTableBase);
|
|
} else {
|
|
AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
|
|
assert(AddressPoint.AddressPointIndex != 0 &&
|
|
"Did not find ctor vtable address point!");
|
|
}
|
|
|
|
llvm::Value *Idxs[] = {
|
|
llvm::ConstantInt::get(CGM.Int32Ty, 0),
|
|
llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.VTableIndex),
|
|
llvm::ConstantInt::get(CGM.Int32Ty, AddressPoint.AddressPointIndex),
|
|
};
|
|
|
|
// Add inrange attribute to indicate that only the VTableIndex can be
|
|
// accessed.
|
|
unsigned ComponentSize =
|
|
CGM.getDataLayout().getTypeAllocSize(getVTableComponentType());
|
|
unsigned VTableSize = CGM.getDataLayout().getTypeAllocSize(
|
|
cast<llvm::StructType>(VTable->getValueType())
|
|
->getElementType(AddressPoint.VTableIndex));
|
|
unsigned Offset = ComponentSize * AddressPoint.AddressPointIndex;
|
|
llvm::ConstantRange InRange(llvm::APInt(32, -Offset, true),
|
|
llvm::APInt(32, VTableSize - Offset, true));
|
|
llvm::Constant *Init = llvm::ConstantExpr::getGetElementPtr(
|
|
VTable->getValueType(), VTable, Idxs, /*InBounds=*/true, InRange);
|
|
|
|
VTTComponents.push_back(Init);
|
|
}
|
|
|
|
llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
|
|
|
|
VTT->setInitializer(Init);
|
|
|
|
// Set the correct linkage.
|
|
VTT->setLinkage(Linkage);
|
|
|
|
if (CGM.supportsCOMDAT() && VTT->isWeakForLinker())
|
|
VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName()));
|
|
|
|
// Set the visibility. This will already have been set on the VTT declaration.
|
|
// Set it again, now that we have a definition, as the implicit visibility can
|
|
// apply differently to definitions.
|
|
CGM.setGVProperties(VTT, RD);
|
|
}
|
|
|
|
llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
|
|
assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
|
|
|
|
SmallString<256> OutName;
|
|
llvm::raw_svector_ostream Out(OutName);
|
|
cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
|
|
.mangleCXXVTT(RD, Out);
|
|
StringRef Name = OutName.str();
|
|
|
|
// This will also defer the definition of the VTT.
|
|
(void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
|
|
|
|
VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
|
|
|
|
llvm::ArrayType *ArrayType = llvm::ArrayType::get(
|
|
CGM.GlobalsInt8PtrTy, Builder.getVTTComponents().size());
|
|
llvm::Align Align = CGM.getDataLayout().getABITypeAlign(CGM.GlobalsInt8PtrTy);
|
|
|
|
llvm::GlobalVariable *GV = CGM.CreateOrReplaceCXXRuntimeVariable(
|
|
Name, ArrayType, llvm::GlobalValue::ExternalLinkage, Align);
|
|
GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
|
|
CGM.setGVProperties(GV, RD);
|
|
return GV;
|
|
}
|
|
|
|
uint64_t CodeGenVTables::getSubVTTIndex(const CXXRecordDecl *RD,
|
|
BaseSubobject Base) {
|
|
BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
|
|
|
|
SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
|
|
if (I != SubVTTIndicies.end())
|
|
return I->second;
|
|
|
|
VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
|
|
|
|
for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
|
|
Builder.getSubVTTIndicies().begin(),
|
|
E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
|
|
// Insert all indices.
|
|
BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
|
|
|
|
SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
|
|
}
|
|
|
|
I = SubVTTIndicies.find(ClassSubobjectPair);
|
|
assert(I != SubVTTIndicies.end() && "Did not find index!");
|
|
|
|
return I->second;
|
|
}
|
|
|
|
uint64_t
|
|
CodeGenVTables::getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
|
|
BaseSubobject Base) {
|
|
SecondaryVirtualPointerIndicesMapTy::iterator I =
|
|
SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
|
|
|
|
if (I != SecondaryVirtualPointerIndices.end())
|
|
return I->second;
|
|
|
|
VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
|
|
|
|
// Insert all secondary vpointer indices.
|
|
for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
|
|
Builder.getSecondaryVirtualPointerIndices().begin(),
|
|
E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
|
|
std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
|
|
std::make_pair(RD, I->first);
|
|
|
|
SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
|
|
}
|
|
|
|
I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
|
|
assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
|
|
|
|
return I->second;
|
|
}
|