#include "llvm/Transforms/IPO/LowerTypeTests.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/EquivalenceClasses.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Analysis/TypeMetadataUtils.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalObject.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ModuleSummaryIndex.h"
#include "llvm/IR/ModuleSummaryIndexYAML.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/TrailingObjects.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <memory>
#include <set>
#include <string>
#include <system_error>
#include <utility>
#include <vector>
using namespace llvm;
using namespace lowertypetests;
#define DEBUG_TYPE "lowertypetests"
STATISTIC(ByteArraySizeBits, "Byte array size in bits");
STATISTIC(ByteArraySizeBytes, "Byte array size in bytes");
STATISTIC(NumByteArraysCreated, "Number of byte arrays created");
STATISTIC(NumTypeTestCallsLowered, "Number of type test calls lowered");
STATISTIC(NumTypeIdDisjointSets, "Number of disjoint sets of type identifiers");
static cl::opt<bool> AvoidReuse(
"lowertypetests-avoid-reuse",
cl::desc("Try to avoid reuse of byte array addresses using aliases"),
cl::Hidden, cl::init(true));
static cl::opt<PassSummaryAction> ClSummaryAction(
"lowertypetests-summary-action",
cl::desc("What to do with the summary when running this pass"),
cl::values(clEnumValN(PassSummaryAction::None, "none", "Do nothing"),
clEnumValN(PassSummaryAction::Import, "import",
"Import typeid resolutions from summary and globals"),
clEnumValN(PassSummaryAction::Export, "export",
"Export typeid resolutions to summary and globals")),
cl::Hidden);
static cl::opt<std::string> ClReadSummary(
"lowertypetests-read-summary",
cl::desc("Read summary from given YAML file before running pass"),
cl::Hidden);
static cl::opt<std::string> ClWriteSummary(
"lowertypetests-write-summary",
cl::desc("Write summary to given YAML file after running pass"),
cl::Hidden);
static cl::opt<bool>
ClDropTypeTests("lowertypetests-drop-type-tests",
cl::desc("Simply drop type test assume sequences"),
cl::Hidden, cl::init(false));
bool BitSetInfo::containsGlobalOffset(uint64_t Offset) const {
if (Offset < ByteOffset)
return false;
if ((Offset - ByteOffset) % (uint64_t(1) << AlignLog2) != 0)
return false;
uint64_t BitOffset = (Offset - ByteOffset) >> AlignLog2;
if (BitOffset >= BitSize)
return false;
return Bits.count(BitOffset);
}
void BitSetInfo::print(raw_ostream &OS) const {
OS << "offset " << ByteOffset << " size " << BitSize << " align "
<< (1 << AlignLog2);
if (isAllOnes()) {
OS << " all-ones\n";
return;
}
OS << " { ";
for (uint64_t B : Bits)
OS << B << ' ';
OS << "}\n";
}
BitSetInfo BitSetBuilder::build() {
if (Min > Max)
Min = 0;
uint64_t Mask = 0;
for (uint64_t &Offset : Offsets) {
Offset -= Min;
Mask |= Offset;
}
BitSetInfo BSI;
BSI.ByteOffset = Min;
BSI.AlignLog2 = 0;
if (Mask != 0)
BSI.AlignLog2 = countTrailingZeros(Mask, ZB_Undefined);
BSI.BitSize = ((Max - Min) >> BSI.AlignLog2) + 1;
for (uint64_t Offset : Offsets) {
Offset >>= BSI.AlignLog2;
BSI.Bits.insert(Offset);
}
return BSI;
}
void GlobalLayoutBuilder::addFragment(const std::set<uint64_t> &F) {
Fragments.emplace_back();
std::vector<uint64_t> &Fragment = Fragments.back();
uint64_t FragmentIndex = Fragments.size() - 1;
for (auto ObjIndex : F) {
uint64_t OldFragmentIndex = FragmentMap[ObjIndex];
if (OldFragmentIndex == 0) {
Fragment.push_back(ObjIndex);
} else {
std::vector<uint64_t> &OldFragment = Fragments[OldFragmentIndex];
llvm::append_range(Fragment, OldFragment);
OldFragment.clear();
}
}
for (uint64_t ObjIndex : Fragment)
FragmentMap[ObjIndex] = FragmentIndex;
}
void ByteArrayBuilder::allocate(const std::set<uint64_t> &Bits,
uint64_t BitSize, uint64_t &AllocByteOffset,
uint8_t &AllocMask) {
unsigned Bit = 0;
for (unsigned I = 1; I != BitsPerByte; ++I)
if (BitAllocs[I] < BitAllocs[Bit])
Bit = I;
AllocByteOffset = BitAllocs[Bit];
unsigned ReqSize = AllocByteOffset + BitSize;
BitAllocs[Bit] = ReqSize;
if (Bytes.size() < ReqSize)
Bytes.resize(ReqSize);
AllocMask = 1 << Bit;
for (uint64_t B : Bits)
Bytes[AllocByteOffset + B] |= AllocMask;
}
bool lowertypetests::isJumpTableCanonical(Function *F) {
if (F->isDeclarationForLinker())
return false;
auto *CI = mdconst::extract_or_null<ConstantInt>(
F->getParent()->getModuleFlag("CFI Canonical Jump Tables"));
if (!CI || CI->getZExtValue() != 0)
return true;
return F->hasFnAttribute("cfi-canonical-jump-table");
}
namespace {
struct ByteArrayInfo {
std::set<uint64_t> Bits;
uint64_t BitSize;
GlobalVariable *ByteArray;
GlobalVariable *MaskGlobal;
uint8_t *MaskPtr = nullptr;
};
class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
friend TrailingObjects;
GlobalObject *GO;
size_t NTypes;
bool IsJumpTableCanonical;
bool IsExported;
size_t numTrailingObjects(OverloadToken<MDNode *>) const { return NTypes; }
public:
static GlobalTypeMember *create(BumpPtrAllocator &Alloc, GlobalObject *GO,
bool IsJumpTableCanonical, bool IsExported,
ArrayRef<MDNode *> Types) {
auto *GTM = static_cast<GlobalTypeMember *>(Alloc.Allocate(
totalSizeToAlloc<MDNode *>(Types.size()), alignof(GlobalTypeMember)));
GTM->GO = GO;
GTM->NTypes = Types.size();
GTM->IsJumpTableCanonical = IsJumpTableCanonical;
GTM->IsExported = IsExported;
std::uninitialized_copy(Types.begin(), Types.end(),
GTM->getTrailingObjects<MDNode *>());
return GTM;
}
GlobalObject *getGlobal() const {
return GO;
}
bool isJumpTableCanonical() const {
return IsJumpTableCanonical;
}
bool isExported() const {
return IsExported;
}
ArrayRef<MDNode *> types() const {
return makeArrayRef(getTrailingObjects<MDNode *>(), NTypes);
}
};
struct ICallBranchFunnel final
: TrailingObjects<ICallBranchFunnel, GlobalTypeMember *> {
static ICallBranchFunnel *create(BumpPtrAllocator &Alloc, CallInst *CI,
ArrayRef<GlobalTypeMember *> Targets,
unsigned UniqueId) {
auto *Call = static_cast<ICallBranchFunnel *>(
Alloc.Allocate(totalSizeToAlloc<GlobalTypeMember *>(Targets.size()),
alignof(ICallBranchFunnel)));
Call->CI = CI;
Call->UniqueId = UniqueId;
Call->NTargets = Targets.size();
std::uninitialized_copy(Targets.begin(), Targets.end(),
Call->getTrailingObjects<GlobalTypeMember *>());
return Call;
}
CallInst *CI;
ArrayRef<GlobalTypeMember *> targets() const {
return makeArrayRef(getTrailingObjects<GlobalTypeMember *>(), NTargets);
}
unsigned UniqueId;
private:
size_t NTargets;
};
struct ScopedSaveAliaseesAndUsed {
Module &M;
SmallVector<GlobalValue *, 4> Used, CompilerUsed;
std::vector<std::pair<GlobalAlias *, Function *>> FunctionAliases;
std::vector<std::pair<GlobalIFunc *, Function *>> ResolverIFuncs;
ScopedSaveAliaseesAndUsed(Module &M) : M(M) {
if (GlobalVariable *GV = collectUsedGlobalVariables(M, Used, false))
GV->eraseFromParent();
if (GlobalVariable *GV = collectUsedGlobalVariables(M, CompilerUsed, true))
GV->eraseFromParent();
for (auto &GA : M.aliases()) {
if (auto *F = dyn_cast<Function>(GA.getAliasee()->stripPointerCasts()))
FunctionAliases.push_back({&GA, F});
}
for (auto &GI : M.ifuncs())
if (auto *F = dyn_cast<Function>(GI.getResolver()->stripPointerCasts()))
ResolverIFuncs.push_back({&GI, F});
}
~ScopedSaveAliaseesAndUsed() {
appendToUsed(M, Used);
appendToCompilerUsed(M, CompilerUsed);
for (auto P : FunctionAliases)
P.first->setAliasee(
ConstantExpr::getBitCast(P.second, P.first->getType()));
for (auto P : ResolverIFuncs) {
P.first->setResolver(P.second);
}
}
};
class LowerTypeTestsModule {
Module &M;
ModuleSummaryIndex *ExportSummary;
const ModuleSummaryIndex *ImportSummary;
bool DropTypeTests;
Triple::ArchType Arch;
Triple::OSType OS;
Triple::ObjectFormatType ObjectFormat;
IntegerType *Int1Ty = Type::getInt1Ty(M.getContext());
IntegerType *Int8Ty = Type::getInt8Ty(M.getContext());
PointerType *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
ArrayType *Int8Arr0Ty = ArrayType::get(Type::getInt8Ty(M.getContext()), 0);
IntegerType *Int32Ty = Type::getInt32Ty(M.getContext());
PointerType *Int32PtrTy = PointerType::getUnqual(Int32Ty);
IntegerType *Int64Ty = Type::getInt64Ty(M.getContext());
IntegerType *IntPtrTy = M.getDataLayout().getIntPtrType(M.getContext(), 0);
uint64_t IndirectIndex = 1;
struct TypeIdUserInfo {
std::vector<CallInst *> CallSites;
bool IsExported = false;
};
DenseMap<Metadata *, TypeIdUserInfo> TypeIdUsers;
struct TypeIdLowering {
TypeTestResolution::Kind TheKind = TypeTestResolution::Unsat;
Constant *OffsetedGlobal;
Constant *AlignLog2;
Constant *SizeM1;
Constant *TheByteArray;
Constant *BitMask;
Constant *InlineBits;
};
std::vector<ByteArrayInfo> ByteArrayInfos;
Function *WeakInitializerFn = nullptr;
bool shouldExportConstantsAsAbsoluteSymbols();
uint8_t *exportTypeId(StringRef TypeId, const TypeIdLowering &TIL);
TypeIdLowering importTypeId(StringRef TypeId);
void importTypeTest(CallInst *CI);
void importFunction(Function *F, bool isJumpTableCanonical,
std::vector<GlobalAlias *> &AliasesToErase);
BitSetInfo
buildBitSet(Metadata *TypeId,
const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout);
ByteArrayInfo *createByteArray(BitSetInfo &BSI);
void allocateByteArrays();
Value *createBitSetTest(IRBuilder<> &B, const TypeIdLowering &TIL,
Value *BitOffset);
void lowerTypeTestCalls(
ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout);
Value *lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
const TypeIdLowering &TIL);
void buildBitSetsFromGlobalVariables(ArrayRef<Metadata *> TypeIds,
ArrayRef<GlobalTypeMember *> Globals);
unsigned getJumpTableEntrySize();
Type *getJumpTableEntryType();
void createJumpTableEntry(raw_ostream &AsmOS, raw_ostream &ConstraintOS,
Triple::ArchType JumpTableArch,
SmallVectorImpl<Value *> &AsmArgs, Function *Dest);
void verifyTypeMDNode(GlobalObject *GO, MDNode *Type);
void buildBitSetsFromFunctions(ArrayRef<Metadata *> TypeIds,
ArrayRef<GlobalTypeMember *> Functions);
void buildBitSetsFromFunctionsNative(ArrayRef<Metadata *> TypeIds,
ArrayRef<GlobalTypeMember *> Functions);
void buildBitSetsFromFunctionsWASM(ArrayRef<Metadata *> TypeIds,
ArrayRef<GlobalTypeMember *> Functions);
void
buildBitSetsFromDisjointSet(ArrayRef<Metadata *> TypeIds,
ArrayRef<GlobalTypeMember *> Globals,
ArrayRef<ICallBranchFunnel *> ICallBranchFunnels);
void replaceWeakDeclarationWithJumpTablePtr(Function *F, Constant *JT,
bool IsJumpTableCanonical);
void moveInitializerToModuleConstructor(GlobalVariable *GV);
void findGlobalVariableUsersOf(Constant *C,
SmallSetVector<GlobalVariable *, 8> &Out);
void createJumpTable(Function *F, ArrayRef<GlobalTypeMember *> Functions);
void replaceCfiUses(Function *Old, Value *New, bool IsJumpTableCanonical);
void replaceDirectCalls(Value *Old, Value *New);
public:
LowerTypeTestsModule(Module &M, ModuleSummaryIndex *ExportSummary,
const ModuleSummaryIndex *ImportSummary,
bool DropTypeTests);
bool lower();
static bool runForTesting(Module &M);
};
}
BitSetInfo LowerTypeTestsModule::buildBitSet(
Metadata *TypeId,
const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
BitSetBuilder BSB;
for (auto &GlobalAndOffset : GlobalLayout) {
for (MDNode *Type : GlobalAndOffset.first->types()) {
if (Type->getOperand(1) != TypeId)
continue;
uint64_t Offset =
cast<ConstantInt>(
cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
->getZExtValue();
BSB.addOffset(GlobalAndOffset.second + Offset);
}
}
return BSB.build();
}
static Value *createMaskedBitTest(IRBuilder<> &B, Value *Bits,
Value *BitOffset) {
auto BitsType = cast<IntegerType>(Bits->getType());
unsigned BitWidth = BitsType->getBitWidth();
BitOffset = B.CreateZExtOrTrunc(BitOffset, BitsType);
Value *BitIndex =
B.CreateAnd(BitOffset, ConstantInt::get(BitsType, BitWidth - 1));
Value *BitMask = B.CreateShl(ConstantInt::get(BitsType, 1), BitIndex);
Value *MaskedBits = B.CreateAnd(Bits, BitMask);
return B.CreateICmpNE(MaskedBits, ConstantInt::get(BitsType, 0));
}
ByteArrayInfo *LowerTypeTestsModule::createByteArray(BitSetInfo &BSI) {
auto ByteArrayGlobal = new GlobalVariable(
M, Int8Ty, true, GlobalValue::PrivateLinkage, nullptr);
auto MaskGlobal = new GlobalVariable(M, Int8Ty, true,
GlobalValue::PrivateLinkage, nullptr);
ByteArrayInfos.emplace_back();
ByteArrayInfo *BAI = &ByteArrayInfos.back();
BAI->Bits = BSI.Bits;
BAI->BitSize = BSI.BitSize;
BAI->ByteArray = ByteArrayGlobal;
BAI->MaskGlobal = MaskGlobal;
return BAI;
}
void LowerTypeTestsModule::allocateByteArrays() {
llvm::stable_sort(ByteArrayInfos,
[](const ByteArrayInfo &BAI1, const ByteArrayInfo &BAI2) {
return BAI1.BitSize > BAI2.BitSize;
});
std::vector<uint64_t> ByteArrayOffsets(ByteArrayInfos.size());
ByteArrayBuilder BAB;
for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
ByteArrayInfo *BAI = &ByteArrayInfos[I];
uint8_t Mask;
BAB.allocate(BAI->Bits, BAI->BitSize, ByteArrayOffsets[I], Mask);
BAI->MaskGlobal->replaceAllUsesWith(
ConstantExpr::getIntToPtr(ConstantInt::get(Int8Ty, Mask), Int8PtrTy));
BAI->MaskGlobal->eraseFromParent();
if (BAI->MaskPtr)
*BAI->MaskPtr = Mask;
}
Constant *ByteArrayConst = ConstantDataArray::get(M.getContext(), BAB.Bytes);
auto ByteArray =
new GlobalVariable(M, ByteArrayConst->getType(), true,
GlobalValue::PrivateLinkage, ByteArrayConst);
for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
ByteArrayInfo *BAI = &ByteArrayInfos[I];
Constant *Idxs[] = {ConstantInt::get(IntPtrTy, 0),
ConstantInt::get(IntPtrTy, ByteArrayOffsets[I])};
Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(
ByteArrayConst->getType(), ByteArray, Idxs);
GlobalAlias *Alias = GlobalAlias::create(
Int8Ty, 0, GlobalValue::PrivateLinkage, "bits", GEP, &M);
BAI->ByteArray->replaceAllUsesWith(Alias);
BAI->ByteArray->eraseFromParent();
}
ByteArraySizeBits = BAB.BitAllocs[0] + BAB.BitAllocs[1] + BAB.BitAllocs[2] +
BAB.BitAllocs[3] + BAB.BitAllocs[4] + BAB.BitAllocs[5] +
BAB.BitAllocs[6] + BAB.BitAllocs[7];
ByteArraySizeBytes = BAB.Bytes.size();
}
Value *LowerTypeTestsModule::createBitSetTest(IRBuilder<> &B,
const TypeIdLowering &TIL,
Value *BitOffset) {
if (TIL.TheKind == TypeTestResolution::Inline) {
return createMaskedBitTest(B, TIL.InlineBits, BitOffset);
} else {
Constant *ByteArray = TIL.TheByteArray;
if (AvoidReuse && !ImportSummary) {
ByteArray = GlobalAlias::create(Int8Ty, 0, GlobalValue::PrivateLinkage,
"bits_use", ByteArray, &M);
}
Value *ByteAddr = B.CreateGEP(Int8Ty, ByteArray, BitOffset);
Value *Byte = B.CreateLoad(Int8Ty, ByteAddr);
Value *ByteAndMask =
B.CreateAnd(Byte, ConstantExpr::getPtrToInt(TIL.BitMask, Int8Ty));
return B.CreateICmpNE(ByteAndMask, ConstantInt::get(Int8Ty, 0));
}
}
static bool isKnownTypeIdMember(Metadata *TypeId, const DataLayout &DL,
Value *V, uint64_t COffset) {
if (auto GV = dyn_cast<GlobalObject>(V)) {
SmallVector<MDNode *, 2> Types;
GV->getMetadata(LLVMContext::MD_type, Types);
for (MDNode *Type : Types) {
if (Type->getOperand(1) != TypeId)
continue;
uint64_t Offset =
cast<ConstantInt>(
cast<ConstantAsMetadata>(Type->getOperand(0))->getValue())
->getZExtValue();
if (COffset == Offset)
return true;
}
return false;
}
if (auto GEP = dyn_cast<GEPOperator>(V)) {
APInt APOffset(DL.getPointerSizeInBits(0), 0);
bool Result = GEP->accumulateConstantOffset(DL, APOffset);
if (!Result)
return false;
COffset += APOffset.getZExtValue();
return isKnownTypeIdMember(TypeId, DL, GEP->getPointerOperand(), COffset);
}
if (auto Op = dyn_cast<Operator>(V)) {
if (Op->getOpcode() == Instruction::BitCast)
return isKnownTypeIdMember(TypeId, DL, Op->getOperand(0), COffset);
if (Op->getOpcode() == Instruction::Select)
return isKnownTypeIdMember(TypeId, DL, Op->getOperand(1), COffset) &&
isKnownTypeIdMember(TypeId, DL, Op->getOperand(2), COffset);
}
return false;
}
Value *LowerTypeTestsModule::lowerTypeTestCall(Metadata *TypeId, CallInst *CI,
const TypeIdLowering &TIL) {
if (TIL.TheKind == TypeTestResolution::Unknown)
return nullptr;
if (TIL.TheKind == TypeTestResolution::Unsat)
return ConstantInt::getFalse(M.getContext());
Value *Ptr = CI->getArgOperand(0);
const DataLayout &DL = M.getDataLayout();
if (isKnownTypeIdMember(TypeId, DL, Ptr, 0))
return ConstantInt::getTrue(M.getContext());
BasicBlock *InitialBB = CI->getParent();
IRBuilder<> B(CI);
Value *PtrAsInt = B.CreatePtrToInt(Ptr, IntPtrTy);
Constant *OffsetedGlobalAsInt =
ConstantExpr::getPtrToInt(TIL.OffsetedGlobal, IntPtrTy);
if (TIL.TheKind == TypeTestResolution::Single)
return B.CreateICmpEQ(PtrAsInt, OffsetedGlobalAsInt);
Value *PtrOffset = B.CreateSub(PtrAsInt, OffsetedGlobalAsInt);
Value *OffsetSHR =
B.CreateLShr(PtrOffset, ConstantExpr::getZExt(TIL.AlignLog2, IntPtrTy));
Value *OffsetSHL = B.CreateShl(
PtrOffset, ConstantExpr::getZExt(
ConstantExpr::getSub(
ConstantInt::get(Int8Ty, DL.getPointerSizeInBits(0)),
TIL.AlignLog2),
IntPtrTy));
Value *BitOffset = B.CreateOr(OffsetSHR, OffsetSHL);
Value *OffsetInRange = B.CreateICmpULE(BitOffset, TIL.SizeM1);
if (TIL.TheKind == TypeTestResolution::AllOnes)
return OffsetInRange;
if (CI->hasOneUse())
if (auto *Br = dyn_cast<BranchInst>(*CI->user_begin()))
if (CI->getNextNode() == Br) {
BasicBlock *Then = InitialBB->splitBasicBlock(CI->getIterator());
BasicBlock *Else = Br->getSuccessor(1);
BranchInst *NewBr = BranchInst::Create(Then, Else, OffsetInRange);
NewBr->setMetadata(LLVMContext::MD_prof,
Br->getMetadata(LLVMContext::MD_prof));
ReplaceInstWithInst(InitialBB->getTerminator(), NewBr);
for (auto &Phi : Else->phis())
Phi.addIncoming(Phi.getIncomingValueForBlock(Then), InitialBB);
IRBuilder<> ThenB(CI);
return createBitSetTest(ThenB, TIL, BitOffset);
}
IRBuilder<> ThenB(SplitBlockAndInsertIfThen(OffsetInRange, CI, false));
Value *Bit = createBitSetTest(ThenB, TIL, BitOffset);
B.SetInsertPoint(CI);
PHINode *P = B.CreatePHI(Int1Ty, 2);
P->addIncoming(ConstantInt::get(Int1Ty, 0), InitialBB);
P->addIncoming(Bit, ThenB.GetInsertBlock());
return P;
}
void LowerTypeTestsModule::buildBitSetsFromGlobalVariables(
ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Globals) {
std::vector<Constant *> GlobalInits;
const DataLayout &DL = M.getDataLayout();
DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
Align MaxAlign;
uint64_t CurOffset = 0;
uint64_t DesiredPadding = 0;
for (GlobalTypeMember *G : Globals) {
auto *GV = cast<GlobalVariable>(G->getGlobal());
Align Alignment =
DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());
MaxAlign = std::max(MaxAlign, Alignment);
uint64_t GVOffset = alignTo(CurOffset + DesiredPadding, Alignment);
GlobalLayout[G] = GVOffset;
if (GVOffset != 0) {
uint64_t Padding = GVOffset - CurOffset;
GlobalInits.push_back(
ConstantAggregateZero::get(ArrayType::get(Int8Ty, Padding)));
}
GlobalInits.push_back(GV->getInitializer());
uint64_t InitSize = DL.getTypeAllocSize(GV->getValueType());
CurOffset = GVOffset + InitSize;
DesiredPadding = NextPowerOf2(InitSize - 1) - InitSize;
if (DesiredPadding > 32)
DesiredPadding = alignTo(InitSize, 32) - InitSize;
}
Constant *NewInit = ConstantStruct::getAnon(M.getContext(), GlobalInits);
auto *CombinedGlobal =
new GlobalVariable(M, NewInit->getType(), true,
GlobalValue::PrivateLinkage, NewInit);
CombinedGlobal->setAlignment(MaxAlign);
StructType *NewTy = cast<StructType>(NewInit->getType());
lowerTypeTestCalls(TypeIds, CombinedGlobal, GlobalLayout);
for (unsigned I = 0; I != Globals.size(); ++I) {
GlobalVariable *GV = cast<GlobalVariable>(Globals[I]->getGlobal());
Constant *CombinedGlobalIdxs[] = {ConstantInt::get(Int32Ty, 0),
ConstantInt::get(Int32Ty, I * 2)};
Constant *CombinedGlobalElemPtr = ConstantExpr::getGetElementPtr(
NewInit->getType(), CombinedGlobal, CombinedGlobalIdxs);
assert(GV->getType()->getAddressSpace() == 0);
GlobalAlias *GAlias =
GlobalAlias::create(NewTy->getElementType(I * 2), 0, GV->getLinkage(),
"", CombinedGlobalElemPtr, &M);
GAlias->setVisibility(GV->getVisibility());
GAlias->takeName(GV);
GV->replaceAllUsesWith(GAlias);
GV->eraseFromParent();
}
}
bool LowerTypeTestsModule::shouldExportConstantsAsAbsoluteSymbols() {
return (Arch == Triple::x86 || Arch == Triple::x86_64) &&
ObjectFormat == Triple::ELF;
}
uint8_t *LowerTypeTestsModule::exportTypeId(StringRef TypeId,
const TypeIdLowering &TIL) {
TypeTestResolution &TTRes =
ExportSummary->getOrInsertTypeIdSummary(TypeId).TTRes;
TTRes.TheKind = TIL.TheKind;
auto ExportGlobal = [&](StringRef Name, Constant *C) {
GlobalAlias *GA =
GlobalAlias::create(Int8Ty, 0, GlobalValue::ExternalLinkage,
"__typeid_" + TypeId + "_" + Name, C, &M);
GA->setVisibility(GlobalValue::HiddenVisibility);
};
auto ExportConstant = [&](StringRef Name, uint64_t &Storage, Constant *C) {
if (shouldExportConstantsAsAbsoluteSymbols())
ExportGlobal(Name, ConstantExpr::getIntToPtr(C, Int8PtrTy));
else
Storage = cast<ConstantInt>(C)->getZExtValue();
};
if (TIL.TheKind != TypeTestResolution::Unsat)
ExportGlobal("global_addr", TIL.OffsetedGlobal);
if (TIL.TheKind == TypeTestResolution::ByteArray ||
TIL.TheKind == TypeTestResolution::Inline ||
TIL.TheKind == TypeTestResolution::AllOnes) {
ExportConstant("align", TTRes.AlignLog2, TIL.AlignLog2);
ExportConstant("size_m1", TTRes.SizeM1, TIL.SizeM1);
uint64_t BitSize = cast<ConstantInt>(TIL.SizeM1)->getZExtValue() + 1;
if (TIL.TheKind == TypeTestResolution::Inline)
TTRes.SizeM1BitWidth = (BitSize <= 32) ? 5 : 6;
else
TTRes.SizeM1BitWidth = (BitSize <= 128) ? 7 : 32;
}
if (TIL.TheKind == TypeTestResolution::ByteArray) {
ExportGlobal("byte_array", TIL.TheByteArray);
if (shouldExportConstantsAsAbsoluteSymbols())
ExportGlobal("bit_mask", TIL.BitMask);
else
return &TTRes.BitMask;
}
if (TIL.TheKind == TypeTestResolution::Inline)
ExportConstant("inline_bits", TTRes.InlineBits, TIL.InlineBits);
return nullptr;
}
LowerTypeTestsModule::TypeIdLowering
LowerTypeTestsModule::importTypeId(StringRef TypeId) {
const TypeIdSummary *TidSummary = ImportSummary->getTypeIdSummary(TypeId);
if (!TidSummary)
return {}; const TypeTestResolution &TTRes = TidSummary->TTRes;
TypeIdLowering TIL;
TIL.TheKind = TTRes.TheKind;
auto ImportGlobal = [&](StringRef Name) {
Constant *C = M.getOrInsertGlobal(("__typeid_" + TypeId + "_" + Name).str(),
Int8Arr0Ty);
if (auto *GV = dyn_cast<GlobalVariable>(C))
GV->setVisibility(GlobalValue::HiddenVisibility);
C = ConstantExpr::getBitCast(C, Int8PtrTy);
return C;
};
auto ImportConstant = [&](StringRef Name, uint64_t Const, unsigned AbsWidth,
Type *Ty) {
if (!shouldExportConstantsAsAbsoluteSymbols()) {
Constant *C =
ConstantInt::get(isa<IntegerType>(Ty) ? Ty : Int64Ty, Const);
if (!isa<IntegerType>(Ty))
C = ConstantExpr::getIntToPtr(C, Ty);
return C;
}
Constant *C = ImportGlobal(Name);
auto *GV = cast<GlobalVariable>(C->stripPointerCasts());
if (isa<IntegerType>(Ty))
C = ConstantExpr::getPtrToInt(C, Ty);
if (GV->getMetadata(LLVMContext::MD_absolute_symbol))
return C;
auto SetAbsRange = [&](uint64_t Min, uint64_t Max) {
auto *MinC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Min));
auto *MaxC = ConstantAsMetadata::get(ConstantInt::get(IntPtrTy, Max));
GV->setMetadata(LLVMContext::MD_absolute_symbol,
MDNode::get(M.getContext(), {MinC, MaxC}));
};
if (AbsWidth == IntPtrTy->getBitWidth())
SetAbsRange(~0ull, ~0ull); else
SetAbsRange(0, 1ull << AbsWidth);
return C;
};
if (TIL.TheKind != TypeTestResolution::Unsat)
TIL.OffsetedGlobal = ImportGlobal("global_addr");
if (TIL.TheKind == TypeTestResolution::ByteArray ||
TIL.TheKind == TypeTestResolution::Inline ||
TIL.TheKind == TypeTestResolution::AllOnes) {
TIL.AlignLog2 = ImportConstant("align", TTRes.AlignLog2, 8, Int8Ty);
TIL.SizeM1 =
ImportConstant("size_m1", TTRes.SizeM1, TTRes.SizeM1BitWidth, IntPtrTy);
}
if (TIL.TheKind == TypeTestResolution::ByteArray) {
TIL.TheByteArray = ImportGlobal("byte_array");
TIL.BitMask = ImportConstant("bit_mask", TTRes.BitMask, 8, Int8PtrTy);
}
if (TIL.TheKind == TypeTestResolution::Inline)
TIL.InlineBits = ImportConstant(
"inline_bits", TTRes.InlineBits, 1 << TTRes.SizeM1BitWidth,
TTRes.SizeM1BitWidth <= 5 ? Int32Ty : Int64Ty);
return TIL;
}
void LowerTypeTestsModule::importTypeTest(CallInst *CI) {
auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
if (!TypeIdMDVal)
report_fatal_error("Second argument of llvm.type.test must be metadata");
auto TypeIdStr = dyn_cast<MDString>(TypeIdMDVal->getMetadata());
if (!TypeIdStr)
return;
TypeIdLowering TIL = importTypeId(TypeIdStr->getString());
Value *Lowered = lowerTypeTestCall(TypeIdStr, CI, TIL);
if (Lowered) {
CI->replaceAllUsesWith(Lowered);
CI->eraseFromParent();
}
}
void LowerTypeTestsModule::importFunction(
Function *F, bool isJumpTableCanonical,
std::vector<GlobalAlias *> &AliasesToErase) {
assert(F->getType()->getAddressSpace() == 0);
GlobalValue::VisibilityTypes Visibility = F->getVisibility();
std::string Name = std::string(F->getName());
if (F->isDeclarationForLinker() && isJumpTableCanonical) {
if (F->isDSOLocal()) {
Function *RealF = Function::Create(F->getFunctionType(),
GlobalValue::ExternalLinkage,
F->getAddressSpace(),
Name + ".cfi", &M);
RealF->setVisibility(GlobalVariable::HiddenVisibility);
replaceDirectCalls(F, RealF);
}
return;
}
Function *FDecl;
if (!isJumpTableCanonical) {
FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
F->getAddressSpace(), Name + ".cfi_jt", &M);
FDecl->setVisibility(GlobalValue::HiddenVisibility);
} else {
F->setName(Name + ".cfi");
F->setLinkage(GlobalValue::ExternalLinkage);
FDecl = Function::Create(F->getFunctionType(), GlobalValue::ExternalLinkage,
F->getAddressSpace(), Name, &M);
FDecl->setVisibility(Visibility);
Visibility = GlobalValue::HiddenVisibility;
for (auto &U : F->uses()) {
if (auto *A = dyn_cast<GlobalAlias>(U.getUser())) {
Function *AliasDecl = Function::Create(
F->getFunctionType(), GlobalValue::ExternalLinkage,
F->getAddressSpace(), "", &M);
AliasDecl->takeName(A);
A->replaceAllUsesWith(AliasDecl);
AliasesToErase.push_back(A);
}
}
}
if (F->hasExternalWeakLinkage())
replaceWeakDeclarationWithJumpTablePtr(F, FDecl, isJumpTableCanonical);
else
replaceCfiUses(F, FDecl, isJumpTableCanonical);
F->setVisibility(Visibility);
}
void LowerTypeTestsModule::lowerTypeTestCalls(
ArrayRef<Metadata *> TypeIds, Constant *CombinedGlobalAddr,
const DenseMap<GlobalTypeMember *, uint64_t> &GlobalLayout) {
CombinedGlobalAddr = ConstantExpr::getBitCast(CombinedGlobalAddr, Int8PtrTy);
for (Metadata *TypeId : TypeIds) {
BitSetInfo BSI = buildBitSet(TypeId, GlobalLayout);
LLVM_DEBUG({
if (auto MDS = dyn_cast<MDString>(TypeId))
dbgs() << MDS->getString() << ": ";
else
dbgs() << "<unnamed>: ";
BSI.print(dbgs());
});
ByteArrayInfo *BAI = nullptr;
TypeIdLowering TIL;
TIL.OffsetedGlobal = ConstantExpr::getGetElementPtr(
Int8Ty, CombinedGlobalAddr, ConstantInt::get(IntPtrTy, BSI.ByteOffset)),
TIL.AlignLog2 = ConstantInt::get(Int8Ty, BSI.AlignLog2);
TIL.SizeM1 = ConstantInt::get(IntPtrTy, BSI.BitSize - 1);
if (BSI.isAllOnes()) {
TIL.TheKind = (BSI.BitSize == 1) ? TypeTestResolution::Single
: TypeTestResolution::AllOnes;
} else if (BSI.BitSize <= 64) {
TIL.TheKind = TypeTestResolution::Inline;
uint64_t InlineBits = 0;
for (auto Bit : BSI.Bits)
InlineBits |= uint64_t(1) << Bit;
if (InlineBits == 0)
TIL.TheKind = TypeTestResolution::Unsat;
else
TIL.InlineBits = ConstantInt::get(
(BSI.BitSize <= 32) ? Int32Ty : Int64Ty, InlineBits);
} else {
TIL.TheKind = TypeTestResolution::ByteArray;
++NumByteArraysCreated;
BAI = createByteArray(BSI);
TIL.TheByteArray = BAI->ByteArray;
TIL.BitMask = BAI->MaskGlobal;
}
TypeIdUserInfo &TIUI = TypeIdUsers[TypeId];
if (TIUI.IsExported) {
uint8_t *MaskPtr = exportTypeId(cast<MDString>(TypeId)->getString(), TIL);
if (BAI)
BAI->MaskPtr = MaskPtr;
}
for (CallInst *CI : TIUI.CallSites) {
++NumTypeTestCallsLowered;
Value *Lowered = lowerTypeTestCall(TypeId, CI, TIL);
if (Lowered) {
CI->replaceAllUsesWith(Lowered);
CI->eraseFromParent();
}
}
}
}
void LowerTypeTestsModule::verifyTypeMDNode(GlobalObject *GO, MDNode *Type) {
if (Type->getNumOperands() != 2)
report_fatal_error("All operands of type metadata must have 2 elements");
if (GO->isThreadLocal())
report_fatal_error("Bit set element may not be thread-local");
if (isa<GlobalVariable>(GO) && GO->hasSection())
report_fatal_error(
"A member of a type identifier may not have an explicit section");
auto OffsetConstMD = dyn_cast<ConstantAsMetadata>(Type->getOperand(0));
if (!OffsetConstMD)
report_fatal_error("Type offset must be a constant");
auto OffsetInt = dyn_cast<ConstantInt>(OffsetConstMD->getValue());
if (!OffsetInt)
report_fatal_error("Type offset must be an integer constant");
}
static const unsigned kX86JumpTableEntrySize = 8;
static const unsigned kARMJumpTableEntrySize = 4;
static const unsigned kARMBTIJumpTableEntrySize = 8;
static const unsigned kRISCVJumpTableEntrySize = 8;
unsigned LowerTypeTestsModule::getJumpTableEntrySize() {
switch (Arch) {
case Triple::x86:
case Triple::x86_64:
return kX86JumpTableEntrySize;
case Triple::arm:
case Triple::thumb:
return kARMJumpTableEntrySize;
case Triple::aarch64:
if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("branch-target-enforcement")))
if (BTE->getZExtValue())
return kARMBTIJumpTableEntrySize;
return kARMJumpTableEntrySize;
case Triple::riscv32:
case Triple::riscv64:
return kRISCVJumpTableEntrySize;
default:
report_fatal_error("Unsupported architecture for jump tables");
}
}
void LowerTypeTestsModule::createJumpTableEntry(
raw_ostream &AsmOS, raw_ostream &ConstraintOS,
Triple::ArchType JumpTableArch, SmallVectorImpl<Value *> &AsmArgs,
Function *Dest) {
unsigned ArgIndex = AsmArgs.size();
if (JumpTableArch == Triple::x86 || JumpTableArch == Triple::x86_64) {
AsmOS << "jmp ${" << ArgIndex << ":c}@plt\n";
AsmOS << "int3\nint3\nint3\n";
} else if (JumpTableArch == Triple::arm) {
AsmOS << "b $" << ArgIndex << "\n";
} else if (JumpTableArch == Triple::aarch64) {
if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
Dest->getParent()->getModuleFlag("branch-target-enforcement")))
if (BTE->getZExtValue())
AsmOS << "bti c\n";
AsmOS << "b $" << ArgIndex << "\n";
} else if (JumpTableArch == Triple::thumb) {
AsmOS << "b.w $" << ArgIndex << "\n";
} else if (JumpTableArch == Triple::riscv32 ||
JumpTableArch == Triple::riscv64) {
AsmOS << "tail $" << ArgIndex << "@plt\n";
} else {
report_fatal_error("Unsupported architecture for jump tables");
}
ConstraintOS << (ArgIndex > 0 ? ",s" : "s");
AsmArgs.push_back(Dest);
}
Type *LowerTypeTestsModule::getJumpTableEntryType() {
return ArrayType::get(Int8Ty, getJumpTableEntrySize());
}
void LowerTypeTestsModule::buildBitSetsFromFunctions(
ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Functions) {
if (Arch == Triple::x86 || Arch == Triple::x86_64 || Arch == Triple::arm ||
Arch == Triple::thumb || Arch == Triple::aarch64 ||
Arch == Triple::riscv32 || Arch == Triple::riscv64)
buildBitSetsFromFunctionsNative(TypeIds, Functions);
else if (Arch == Triple::wasm32 || Arch == Triple::wasm64)
buildBitSetsFromFunctionsWASM(TypeIds, Functions);
else
report_fatal_error("Unsupported architecture for jump tables");
}
void LowerTypeTestsModule::moveInitializerToModuleConstructor(
GlobalVariable *GV) {
if (WeakInitializerFn == nullptr) {
WeakInitializerFn = Function::Create(
FunctionType::get(Type::getVoidTy(M.getContext()),
false),
GlobalValue::InternalLinkage,
M.getDataLayout().getProgramAddressSpace(),
"__cfi_global_var_init", &M);
BasicBlock *BB =
BasicBlock::Create(M.getContext(), "entry", WeakInitializerFn);
ReturnInst::Create(M.getContext(), BB);
WeakInitializerFn->setSection(
ObjectFormat == Triple::MachO
? "__TEXT,__StaticInit,regular,pure_instructions"
: ".text.startup");
appendToGlobalCtors(M, WeakInitializerFn, 0);
}
IRBuilder<> IRB(WeakInitializerFn->getEntryBlock().getTerminator());
GV->setConstant(false);
IRB.CreateAlignedStore(GV->getInitializer(), GV, GV->getAlign());
GV->setInitializer(Constant::getNullValue(GV->getValueType()));
}
void LowerTypeTestsModule::findGlobalVariableUsersOf(
Constant *C, SmallSetVector<GlobalVariable *, 8> &Out) {
for (auto *U : C->users()){
if (auto *GV = dyn_cast<GlobalVariable>(U))
Out.insert(GV);
else if (auto *C2 = dyn_cast<Constant>(U))
findGlobalVariableUsersOf(C2, Out);
}
}
void LowerTypeTestsModule::replaceWeakDeclarationWithJumpTablePtr(
Function *F, Constant *JT, bool IsJumpTableCanonical) {
SmallSetVector<GlobalVariable *, 8> GlobalVarUsers;
findGlobalVariableUsersOf(F, GlobalVarUsers);
for (auto GV : GlobalVarUsers)
moveInitializerToModuleConstructor(GV);
Function *PlaceholderFn =
Function::Create(cast<FunctionType>(F->getValueType()),
GlobalValue::ExternalWeakLinkage,
F->getAddressSpace(), "", &M);
replaceCfiUses(F, PlaceholderFn, IsJumpTableCanonical);
Constant *Target = ConstantExpr::getSelect(
ConstantExpr::getICmp(CmpInst::ICMP_NE, F,
Constant::getNullValue(F->getType())),
JT, Constant::getNullValue(F->getType()));
PlaceholderFn->replaceAllUsesWith(Target);
PlaceholderFn->eraseFromParent();
}
static bool isThumbFunction(Function *F, Triple::ArchType ModuleArch) {
Attribute TFAttr = F->getFnAttribute("target-features");
if (TFAttr.isValid()) {
SmallVector<StringRef, 6> Features;
TFAttr.getValueAsString().split(Features, ',');
for (StringRef Feature : Features) {
if (Feature == "-thumb-mode")
return false;
else if (Feature == "+thumb-mode")
return true;
}
}
return ModuleArch == Triple::thumb;
}
static Triple::ArchType
selectJumpTableArmEncoding(ArrayRef<GlobalTypeMember *> Functions,
Triple::ArchType ModuleArch) {
if (ModuleArch != Triple::arm && ModuleArch != Triple::thumb)
return ModuleArch;
unsigned ArmCount = 0, ThumbCount = 0;
for (const auto GTM : Functions) {
if (!GTM->isJumpTableCanonical()) {
++ArmCount;
continue;
}
Function *F = cast<Function>(GTM->getGlobal());
++(isThumbFunction(F, ModuleArch) ? ThumbCount : ArmCount);
}
return ArmCount > ThumbCount ? Triple::arm : Triple::thumb;
}
void LowerTypeTestsModule::createJumpTable(
Function *F, ArrayRef<GlobalTypeMember *> Functions) {
std::string AsmStr, ConstraintStr;
raw_string_ostream AsmOS(AsmStr), ConstraintOS(ConstraintStr);
SmallVector<Value *, 16> AsmArgs;
AsmArgs.reserve(Functions.size() * 2);
Triple::ArchType JumpTableArch = selectJumpTableArmEncoding(Functions, Arch);
for (unsigned I = 0; I != Functions.size(); ++I)
createJumpTableEntry(AsmOS, ConstraintOS, JumpTableArch, AsmArgs,
cast<Function>(Functions[I]->getGlobal()));
F->setAlignment(Align(getJumpTableEntrySize()));
if (OS != Triple::Win32)
F->addFnAttr(Attribute::Naked);
if (JumpTableArch == Triple::arm)
F->addFnAttr("target-features", "-thumb-mode");
if (JumpTableArch == Triple::thumb) {
F->addFnAttr("target-features", "+thumb-mode");
F->addFnAttr("target-cpu", "cortex-a8");
}
if (JumpTableArch == Triple::aarch64) {
F->addFnAttr("branch-target-enforcement", "false");
F->addFnAttr("sign-return-address", "none");
}
if (JumpTableArch == Triple::riscv32 || JumpTableArch == Triple::riscv64) {
F->addFnAttr("target-features", "-c,-relax");
}
F->addFnAttr(Attribute::NoUnwind);
BasicBlock *BB = BasicBlock::Create(M.getContext(), "entry", F);
IRBuilder<> IRB(BB);
SmallVector<Type *, 16> ArgTypes;
ArgTypes.reserve(AsmArgs.size());
for (const auto &Arg : AsmArgs)
ArgTypes.push_back(Arg->getType());
InlineAsm *JumpTableAsm =
InlineAsm::get(FunctionType::get(IRB.getVoidTy(), ArgTypes, false),
AsmOS.str(), ConstraintOS.str(),
true);
IRB.CreateCall(JumpTableAsm, AsmArgs);
IRB.CreateUnreachable();
}
void LowerTypeTestsModule::buildBitSetsFromFunctionsNative(
ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Functions) {
assert(!Functions.empty());
DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
unsigned EntrySize = getJumpTableEntrySize();
for (unsigned I = 0; I != Functions.size(); ++I)
GlobalLayout[Functions[I]] = I * EntrySize;
Function *JumpTableFn =
Function::Create(FunctionType::get(Type::getVoidTy(M.getContext()),
false),
GlobalValue::PrivateLinkage,
M.getDataLayout().getProgramAddressSpace(),
".cfi.jumptable", &M);
ArrayType *JumpTableType =
ArrayType::get(getJumpTableEntryType(), Functions.size());
auto JumpTable =
ConstantExpr::getPointerCast(JumpTableFn, JumpTableType->getPointerTo(0));
lowerTypeTestCalls(TypeIds, JumpTable, GlobalLayout);
{
ScopedSaveAliaseesAndUsed S(M);
for (unsigned I = 0; I != Functions.size(); ++I) {
Function *F = cast<Function>(Functions[I]->getGlobal());
bool IsJumpTableCanonical = Functions[I]->isJumpTableCanonical();
Constant *CombinedGlobalElemPtr = ConstantExpr::getBitCast(
ConstantExpr::getInBoundsGetElementPtr(
JumpTableType, JumpTable,
ArrayRef<Constant *>{ConstantInt::get(IntPtrTy, 0),
ConstantInt::get(IntPtrTy, I)}),
F->getType());
const bool IsExported = Functions[I]->isExported();
if (!IsJumpTableCanonical) {
GlobalValue::LinkageTypes LT = IsExported
? GlobalValue::ExternalLinkage
: GlobalValue::InternalLinkage;
GlobalAlias *JtAlias = GlobalAlias::create(F->getValueType(), 0, LT,
F->getName() + ".cfi_jt",
CombinedGlobalElemPtr, &M);
if (IsExported)
JtAlias->setVisibility(GlobalValue::HiddenVisibility);
else
appendToUsed(M, {JtAlias});
}
if (IsExported) {
if (IsJumpTableCanonical)
ExportSummary->cfiFunctionDefs().insert(std::string(F->getName()));
else
ExportSummary->cfiFunctionDecls().insert(std::string(F->getName()));
}
if (!IsJumpTableCanonical) {
if (F->hasExternalWeakLinkage())
replaceWeakDeclarationWithJumpTablePtr(F, CombinedGlobalElemPtr,
IsJumpTableCanonical);
else
replaceCfiUses(F, CombinedGlobalElemPtr, IsJumpTableCanonical);
} else {
assert(F->getType()->getAddressSpace() == 0);
GlobalAlias *FAlias =
GlobalAlias::create(F->getValueType(), 0, F->getLinkage(), "",
CombinedGlobalElemPtr, &M);
FAlias->setVisibility(F->getVisibility());
FAlias->takeName(F);
if (FAlias->hasName())
F->setName(FAlias->getName() + ".cfi");
replaceCfiUses(F, FAlias, IsJumpTableCanonical);
if (!F->hasLocalLinkage())
F->setVisibility(GlobalVariable::HiddenVisibility);
}
}
}
createJumpTable(JumpTableFn, Functions);
}
void LowerTypeTestsModule::buildBitSetsFromFunctionsWASM(
ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Functions) {
assert(!Functions.empty());
DenseMap<GlobalTypeMember *, uint64_t> GlobalLayout;
for (GlobalTypeMember *GTM : Functions) {
Function *F = cast<Function>(GTM->getGlobal());
if (!F->hasAddressTaken())
continue;
MDNode *MD = MDNode::get(F->getContext(),
ArrayRef<Metadata *>(ConstantAsMetadata::get(
ConstantInt::get(Int64Ty, IndirectIndex))));
F->setMetadata("wasm.index", MD);
GlobalLayout[GTM] = IndirectIndex++;
}
lowerTypeTestCalls(TypeIds, ConstantPointerNull::get(Int32PtrTy),
GlobalLayout);
}
void LowerTypeTestsModule::buildBitSetsFromDisjointSet(
ArrayRef<Metadata *> TypeIds, ArrayRef<GlobalTypeMember *> Globals,
ArrayRef<ICallBranchFunnel *> ICallBranchFunnels) {
DenseMap<Metadata *, uint64_t> TypeIdIndices;
for (unsigned I = 0; I != TypeIds.size(); ++I)
TypeIdIndices[TypeIds[I]] = I;
std::vector<std::set<uint64_t>> TypeMembers(TypeIds.size());
unsigned GlobalIndex = 0;
DenseMap<GlobalTypeMember *, uint64_t> GlobalIndices;
for (GlobalTypeMember *GTM : Globals) {
for (MDNode *Type : GTM->types()) {
auto I = TypeIdIndices.find(Type->getOperand(1));
if (I != TypeIdIndices.end())
TypeMembers[I->second].insert(GlobalIndex);
}
GlobalIndices[GTM] = GlobalIndex;
GlobalIndex++;
}
for (ICallBranchFunnel *JT : ICallBranchFunnels) {
TypeMembers.emplace_back();
std::set<uint64_t> &TMSet = TypeMembers.back();
for (GlobalTypeMember *T : JT->targets())
TMSet.insert(GlobalIndices[T]);
}
llvm::stable_sort(TypeMembers, [](const std::set<uint64_t> &O1,
const std::set<uint64_t> &O2) {
return O1.size() < O2.size();
});
GlobalLayoutBuilder GLB(Globals.size());
for (auto &&MemSet : TypeMembers)
GLB.addFragment(MemSet);
bool IsGlobalSet =
Globals.empty() || isa<GlobalVariable>(Globals[0]->getGlobal());
std::vector<GlobalTypeMember *> OrderedGTMs(Globals.size());
auto OGTMI = OrderedGTMs.begin();
for (auto &&F : GLB.Fragments) {
for (auto &&Offset : F) {
if (IsGlobalSet != isa<GlobalVariable>(Globals[Offset]->getGlobal()))
report_fatal_error("Type identifier may not contain both global "
"variables and functions");
*OGTMI++ = Globals[Offset];
}
}
if (IsGlobalSet)
buildBitSetsFromGlobalVariables(TypeIds, OrderedGTMs);
else
buildBitSetsFromFunctions(TypeIds, OrderedGTMs);
}
LowerTypeTestsModule::LowerTypeTestsModule(
Module &M, ModuleSummaryIndex *ExportSummary,
const ModuleSummaryIndex *ImportSummary, bool DropTypeTests)
: M(M), ExportSummary(ExportSummary), ImportSummary(ImportSummary),
DropTypeTests(DropTypeTests || ClDropTypeTests) {
assert(!(ExportSummary && ImportSummary));
Triple TargetTriple(M.getTargetTriple());
Arch = TargetTriple.getArch();
OS = TargetTriple.getOS();
ObjectFormat = TargetTriple.getObjectFormat();
}
bool LowerTypeTestsModule::runForTesting(Module &M) {
ModuleSummaryIndex Summary(false);
if (!ClReadSummary.empty()) {
ExitOnError ExitOnErr("-lowertypetests-read-summary: " + ClReadSummary +
": ");
auto ReadSummaryFile =
ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ClReadSummary)));
yaml::Input In(ReadSummaryFile->getBuffer());
In >> Summary;
ExitOnErr(errorCodeToError(In.error()));
}
bool Changed =
LowerTypeTestsModule(
M, ClSummaryAction == PassSummaryAction::Export ? &Summary : nullptr,
ClSummaryAction == PassSummaryAction::Import ? &Summary : nullptr,
false)
.lower();
if (!ClWriteSummary.empty()) {
ExitOnError ExitOnErr("-lowertypetests-write-summary: " + ClWriteSummary +
": ");
std::error_code EC;
raw_fd_ostream OS(ClWriteSummary, EC, sys::fs::OF_TextWithCRLF);
ExitOnErr(errorCodeToError(EC));
yaml::Output Out(OS);
Out << Summary;
}
return Changed;
}
static bool isDirectCall(Use& U) {
auto *Usr = dyn_cast<CallInst>(U.getUser());
if (Usr) {
auto *CB = dyn_cast<CallBase>(Usr);
if (CB && CB->isCallee(&U))
return true;
}
return false;
}
void LowerTypeTestsModule::replaceCfiUses(Function *Old, Value *New,
bool IsJumpTableCanonical) {
SmallSetVector<Constant *, 4> Constants;
for (Use &U : llvm::make_early_inc_range(Old->uses())) {
if (isa<BlockAddress, NoCFIValue>(U.getUser()))
continue;
if (isDirectCall(U) && (Old->isDSOLocal() || !IsJumpTableCanonical))
continue;
if (auto *C = dyn_cast<Constant>(U.getUser())) {
if (!isa<GlobalValue>(C)) {
Constants.insert(C);
continue;
}
}
U.set(New);
}
for (auto *C : Constants)
C->handleOperandChange(Old, New);
}
void LowerTypeTestsModule::replaceDirectCalls(Value *Old, Value *New) {
Old->replaceUsesWithIf(New, isDirectCall);
}
static void dropTypeTests(Module &M, Function &TypeTestFunc) {
for (Use &U : llvm::make_early_inc_range(TypeTestFunc.uses())) {
auto *CI = cast<CallInst>(U.getUser());
for (Use &CIU : llvm::make_early_inc_range(CI->uses()))
if (auto *Assume = dyn_cast<AssumeInst>(CIU.getUser()))
Assume->eraseFromParent();
if (!CI->use_empty()) {
assert(
all_of(CI->users(), [](User *U) -> bool { return isa<PHINode>(U); }));
CI->replaceAllUsesWith(ConstantInt::getTrue(M.getContext()));
}
CI->eraseFromParent();
}
}
bool LowerTypeTestsModule::lower() {
Function *TypeTestFunc =
M.getFunction(Intrinsic::getName(Intrinsic::type_test));
if (DropTypeTests) {
if (TypeTestFunc)
dropTypeTests(M, *TypeTestFunc);
Function *PublicTypeTestFunc =
M.getFunction(Intrinsic::getName(Intrinsic::public_type_test));
if (PublicTypeTestFunc)
dropTypeTests(M, *PublicTypeTestFunc);
if (TypeTestFunc || PublicTypeTestFunc) {
for (GlobalVariable &GV : M.globals())
GV.eraseMetadata(LLVMContext::MD_vcall_visibility);
return true;
}
return false;
}
if ((ExportSummary && ExportSummary->partiallySplitLTOUnits()) ||
(ImportSummary && ImportSummary->partiallySplitLTOUnits()))
return false;
Function *ICallBranchFunnelFunc =
M.getFunction(Intrinsic::getName(Intrinsic::icall_branch_funnel));
if ((!TypeTestFunc || TypeTestFunc->use_empty()) &&
(!ICallBranchFunnelFunc || ICallBranchFunnelFunc->use_empty()) &&
!ExportSummary && !ImportSummary)
return false;
if (ImportSummary) {
if (TypeTestFunc)
for (Use &U : llvm::make_early_inc_range(TypeTestFunc->uses()))
importTypeTest(cast<CallInst>(U.getUser()));
if (ICallBranchFunnelFunc && !ICallBranchFunnelFunc->use_empty())
report_fatal_error(
"unexpected call to llvm.icall.branch.funnel during import phase");
SmallVector<Function *, 8> Defs;
SmallVector<Function *, 8> Decls;
for (auto &F : M) {
if (F.hasLocalLinkage())
continue;
if (ImportSummary->cfiFunctionDefs().count(std::string(F.getName())))
Defs.push_back(&F);
else if (ImportSummary->cfiFunctionDecls().count(
std::string(F.getName())))
Decls.push_back(&F);
}
std::vector<GlobalAlias *> AliasesToErase;
{
ScopedSaveAliaseesAndUsed S(M);
for (auto F : Defs)
importFunction(F, true, AliasesToErase);
for (auto F : Decls)
importFunction(F, false, AliasesToErase);
}
for (GlobalAlias *GA : AliasesToErase)
GA->eraseFromParent();
return true;
}
using GlobalClassesTy = EquivalenceClasses<
PointerUnion<GlobalTypeMember *, Metadata *, ICallBranchFunnel *>>;
GlobalClassesTy GlobalClasses;
BumpPtrAllocator Alloc;
struct TIInfo {
unsigned UniqueId;
std::vector<GlobalTypeMember *> RefGlobals;
};
DenseMap<Metadata *, TIInfo> TypeIdInfo;
unsigned CurUniqueId = 0;
SmallVector<MDNode *, 2> Types;
const bool CrossDsoCfi = M.getModuleFlag("Cross-DSO CFI") != nullptr;
struct ExportedFunctionInfo {
CfiFunctionLinkage Linkage;
MDNode *FuncMD; };
DenseMap<StringRef, ExportedFunctionInfo> ExportedFunctions;
if (ExportSummary) {
DenseSet<GlobalValue::GUID> AddressTaken;
for (auto &I : *ExportSummary)
for (auto &GVS : I.second.SummaryList)
if (GVS->isLive())
for (auto &Ref : GVS->refs())
AddressTaken.insert(Ref.getGUID());
NamedMDNode *CfiFunctionsMD = M.getNamedMetadata("cfi.functions");
if (CfiFunctionsMD) {
for (auto FuncMD : CfiFunctionsMD->operands()) {
assert(FuncMD->getNumOperands() >= 2);
StringRef FunctionName =
cast<MDString>(FuncMD->getOperand(0))->getString();
CfiFunctionLinkage Linkage = static_cast<CfiFunctionLinkage>(
cast<ConstantAsMetadata>(FuncMD->getOperand(1))
->getValue()
->getUniqueInteger()
.getZExtValue());
const GlobalValue::GUID GUID = GlobalValue::getGUID(
GlobalValue::dropLLVMManglingEscape(FunctionName));
if (!ExportSummary->isGUIDLive(GUID))
continue;
if (!AddressTaken.count(GUID)) {
if (!CrossDsoCfi || Linkage != CFL_Definition)
continue;
bool Exported = false;
if (auto VI = ExportSummary->getValueInfo(GUID))
for (auto &GVS : VI.getSummaryList())
if (GVS->isLive() && !GlobalValue::isLocalLinkage(GVS->linkage()))
Exported = true;
if (!Exported)
continue;
}
auto P = ExportedFunctions.insert({FunctionName, {Linkage, FuncMD}});
if (!P.second && P.first->second.Linkage != CFL_Definition)
P.first->second = {Linkage, FuncMD};
}
for (const auto &P : ExportedFunctions) {
StringRef FunctionName = P.first;
CfiFunctionLinkage Linkage = P.second.Linkage;
MDNode *FuncMD = P.second.FuncMD;
Function *F = M.getFunction(FunctionName);
if (F && F->hasLocalLinkage()) {
F->setName(F->getName() + ".1");
F = nullptr;
}
if (!F)
F = Function::Create(
FunctionType::get(Type::getVoidTy(M.getContext()), false),
GlobalVariable::ExternalLinkage,
M.getDataLayout().getProgramAddressSpace(), FunctionName, &M);
if (F->hasAvailableExternallyLinkage()) {
F->setLinkage(GlobalValue::ExternalLinkage);
F->deleteBody();
F->setComdat(nullptr);
F->clearMetadata();
}
if (Linkage == CFL_Definition && F->hasExternalWeakLinkage())
F->setLinkage(GlobalValue::ExternalLinkage);
if (F->isDeclaration()) {
if (Linkage == CFL_WeakDeclaration)
F->setLinkage(GlobalValue::ExternalWeakLinkage);
F->eraseMetadata(LLVMContext::MD_type);
for (unsigned I = 2; I < FuncMD->getNumOperands(); ++I)
F->addMetadata(LLVMContext::MD_type,
*cast<MDNode>(FuncMD->getOperand(I).get()));
}
}
}
}
DenseMap<GlobalObject *, GlobalTypeMember *> GlobalTypeMembers;
for (GlobalObject &GO : M.global_objects()) {
if (isa<GlobalVariable>(GO) && GO.isDeclarationForLinker())
continue;
Types.clear();
GO.getMetadata(LLVMContext::MD_type, Types);
bool IsJumpTableCanonical = false;
bool IsExported = false;
if (Function *F = dyn_cast<Function>(&GO)) {
IsJumpTableCanonical = isJumpTableCanonical(F);
if (ExportedFunctions.count(F->getName())) {
IsJumpTableCanonical |=
ExportedFunctions[F->getName()].Linkage == CFL_Definition;
IsExported = true;
} else if (!F->hasAddressTaken()) {
if (!CrossDsoCfi || !IsJumpTableCanonical || F->hasLocalLinkage())
continue;
}
}
auto *GTM = GlobalTypeMember::create(Alloc, &GO, IsJumpTableCanonical,
IsExported, Types);
GlobalTypeMembers[&GO] = GTM;
for (MDNode *Type : Types) {
verifyTypeMDNode(&GO, Type);
auto &Info = TypeIdInfo[Type->getOperand(1)];
Info.UniqueId = ++CurUniqueId;
Info.RefGlobals.push_back(GTM);
}
}
auto AddTypeIdUse = [&](Metadata *TypeId) -> TypeIdUserInfo & {
auto Ins = TypeIdUsers.insert({TypeId, {}});
if (Ins.second) {
GlobalClassesTy::iterator GCI = GlobalClasses.insert(TypeId);
GlobalClassesTy::member_iterator CurSet = GlobalClasses.findLeader(GCI);
for (GlobalTypeMember *GTM : TypeIdInfo[TypeId].RefGlobals)
CurSet = GlobalClasses.unionSets(
CurSet, GlobalClasses.findLeader(GlobalClasses.insert(GTM)));
}
return Ins.first->second;
};
if (TypeTestFunc) {
for (const Use &U : TypeTestFunc->uses()) {
auto CI = cast<CallInst>(U.getUser());
bool OnlyAssumeUses = !CI->use_empty();
for (const Use &CIU : CI->uses()) {
if (isa<AssumeInst>(CIU.getUser()))
continue;
OnlyAssumeUses = false;
break;
}
if (OnlyAssumeUses)
continue;
auto TypeIdMDVal = dyn_cast<MetadataAsValue>(CI->getArgOperand(1));
if (!TypeIdMDVal)
report_fatal_error("Second argument of llvm.type.test must be metadata");
auto TypeId = TypeIdMDVal->getMetadata();
AddTypeIdUse(TypeId).CallSites.push_back(CI);
}
}
if (ICallBranchFunnelFunc) {
for (const Use &U : ICallBranchFunnelFunc->uses()) {
if (Arch != Triple::x86_64)
report_fatal_error(
"llvm.icall.branch.funnel not supported on this target");
auto CI = cast<CallInst>(U.getUser());
std::vector<GlobalTypeMember *> Targets;
if (CI->arg_size() % 2 != 1)
report_fatal_error("number of arguments should be odd");
GlobalClassesTy::member_iterator CurSet;
for (unsigned I = 1; I != CI->arg_size(); I += 2) {
int64_t Offset;
auto *Base = dyn_cast<GlobalObject>(GetPointerBaseWithConstantOffset(
CI->getOperand(I), Offset, M.getDataLayout()));
if (!Base)
report_fatal_error(
"Expected branch funnel operand to be global value");
GlobalTypeMember *GTM = GlobalTypeMembers[Base];
Targets.push_back(GTM);
GlobalClassesTy::member_iterator NewSet =
GlobalClasses.findLeader(GlobalClasses.insert(GTM));
if (I == 1)
CurSet = NewSet;
else
CurSet = GlobalClasses.unionSets(CurSet, NewSet);
}
GlobalClasses.unionSets(
CurSet, GlobalClasses.findLeader(
GlobalClasses.insert(ICallBranchFunnel::create(
Alloc, CI, Targets, ++CurUniqueId))));
}
}
if (ExportSummary) {
DenseMap<GlobalValue::GUID, TinyPtrVector<Metadata *>> MetadataByGUID;
for (auto &P : TypeIdInfo) {
if (auto *TypeId = dyn_cast<MDString>(P.first))
MetadataByGUID[GlobalValue::getGUID(TypeId->getString())].push_back(
TypeId);
}
for (auto &P : *ExportSummary) {
for (auto &S : P.second.SummaryList) {
if (!ExportSummary->isGlobalValueLive(S.get()))
continue;
if (auto *FS = dyn_cast<FunctionSummary>(S->getBaseObject()))
for (GlobalValue::GUID G : FS->type_tests())
for (Metadata *MD : MetadataByGUID[G])
AddTypeIdUse(MD).IsExported = true;
}
}
}
if (GlobalClasses.empty())
return false;
std::vector<std::pair<GlobalClassesTy::iterator, unsigned>> Sets;
for (GlobalClassesTy::iterator I = GlobalClasses.begin(),
E = GlobalClasses.end();
I != E; ++I) {
if (!I->isLeader())
continue;
++NumTypeIdDisjointSets;
unsigned MaxUniqueId = 0;
for (GlobalClassesTy::member_iterator MI = GlobalClasses.member_begin(I);
MI != GlobalClasses.member_end(); ++MI) {
if (auto *MD = MI->dyn_cast<Metadata *>())
MaxUniqueId = std::max(MaxUniqueId, TypeIdInfo[MD].UniqueId);
else if (auto *BF = MI->dyn_cast<ICallBranchFunnel *>())
MaxUniqueId = std::max(MaxUniqueId, BF->UniqueId);
}
Sets.emplace_back(I, MaxUniqueId);
}
llvm::sort(Sets, llvm::less_second());
for (const auto &S : Sets) {
std::vector<Metadata *> TypeIds;
std::vector<GlobalTypeMember *> Globals;
std::vector<ICallBranchFunnel *> ICallBranchFunnels;
for (GlobalClassesTy::member_iterator MI =
GlobalClasses.member_begin(S.first);
MI != GlobalClasses.member_end(); ++MI) {
if (MI->is<Metadata *>())
TypeIds.push_back(MI->get<Metadata *>());
else if (MI->is<GlobalTypeMember *>())
Globals.push_back(MI->get<GlobalTypeMember *>());
else
ICallBranchFunnels.push_back(MI->get<ICallBranchFunnel *>());
}
llvm::sort(TypeIds, [&](Metadata *M1, Metadata *M2) {
return TypeIdInfo[M1].UniqueId < TypeIdInfo[M2].UniqueId;
});
llvm::sort(ICallBranchFunnels,
[&](ICallBranchFunnel *F1, ICallBranchFunnel *F2) {
return F1->UniqueId < F2->UniqueId;
});
buildBitSetsFromDisjointSet(TypeIds, Globals, ICallBranchFunnels);
}
allocateByteArrays();
if (ExportSummary) {
if (NamedMDNode *AliasesMD = M.getNamedMetadata("aliases")) {
for (auto AliasMD : AliasesMD->operands()) {
assert(AliasMD->getNumOperands() >= 4);
StringRef AliasName =
cast<MDString>(AliasMD->getOperand(0))->getString();
StringRef Aliasee = cast<MDString>(AliasMD->getOperand(1))->getString();
if (!ExportedFunctions.count(Aliasee) ||
ExportedFunctions[Aliasee].Linkage != CFL_Definition ||
!M.getNamedAlias(Aliasee))
continue;
GlobalValue::VisibilityTypes Visibility =
static_cast<GlobalValue::VisibilityTypes>(
cast<ConstantAsMetadata>(AliasMD->getOperand(2))
->getValue()
->getUniqueInteger()
.getZExtValue());
bool Weak =
static_cast<bool>(cast<ConstantAsMetadata>(AliasMD->getOperand(3))
->getValue()
->getUniqueInteger()
.getZExtValue());
auto *Alias = GlobalAlias::create("", M.getNamedAlias(Aliasee));
Alias->setVisibility(Visibility);
if (Weak)
Alias->setLinkage(GlobalValue::WeakAnyLinkage);
if (auto *F = M.getFunction(AliasName)) {
Alias->takeName(F);
F->replaceAllUsesWith(Alias);
F->eraseFromParent();
} else {
Alias->setName(AliasName);
}
}
}
}
if (ExportSummary) {
if (NamedMDNode *SymversMD = M.getNamedMetadata("symvers")) {
for (auto Symver : SymversMD->operands()) {
assert(Symver->getNumOperands() >= 2);
StringRef SymbolName =
cast<MDString>(Symver->getOperand(0))->getString();
StringRef Alias = cast<MDString>(Symver->getOperand(1))->getString();
if (!ExportedFunctions.count(SymbolName))
continue;
M.appendModuleInlineAsm(
(llvm::Twine(".symver ") + SymbolName + ", " + Alias).str());
}
}
}
return true;
}
PreservedAnalyses LowerTypeTestsPass::run(Module &M,
ModuleAnalysisManager &AM) {
bool Changed;
if (UseCommandLine)
Changed = LowerTypeTestsModule::runForTesting(M);
else
Changed =
LowerTypeTestsModule(M, ExportSummary, ImportSummary, DropTypeTests)
.lower();
if (!Changed)
return PreservedAnalyses::all();
return PreservedAnalyses::none();
}