Skip to content

Commit

Permalink
[clang][CodeGen] Drop some typed pointer bitcasts
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D156911
  • Loading branch information
bjope committed Aug 3, 2023
1 parent deec9e7 commit 2bdc864
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 205 deletions.
13 changes: 4 additions & 9 deletions clang/lib/CodeGen/CGBlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
if (CI.isNested())
byrefPointer = Builder.CreateLoad(src, "byref.capture");
else
byrefPointer = Builder.CreateBitCast(src.getPointer(), VoidPtrTy);
byrefPointer = src.getPointer();

// Write that void* into the capture field.
Builder.CreateStore(byrefPointer, blockField);
Expand Down Expand Up @@ -1667,7 +1667,6 @@ struct CallBlockRelease final : EHScopeStack::Cleanup {
llvm::Value *BlockVarAddr;
if (LoadBlockVarAddr) {
BlockVarAddr = CGF.Builder.CreateLoad(Addr);
BlockVarAddr = CGF.Builder.CreateBitCast(BlockVarAddr, CGF.VoidPtrTy);
} else {
BlockVarAddr = Addr.getPointer();
}
Expand Down Expand Up @@ -1975,9 +1974,7 @@ CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) {
}
case BlockCaptureEntityKind::BlockObject: {
llvm::Value *srcValue = Builder.CreateLoad(srcField, "blockcopy.src");
srcValue = Builder.CreateBitCast(srcValue, VoidPtrTy);
llvm::Value *dstAddr =
Builder.CreateBitCast(dstField.getPointer(), VoidPtrTy);
llvm::Value *dstAddr = dstField.getPointer();
llvm::Value *args[] = {
dstAddr, srcValue, llvm::ConstantInt::get(Int32Ty, flags.getBitMask())
};
Expand Down Expand Up @@ -2774,10 +2771,8 @@ void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
void CodeGenFunction::BuildBlockRelease(llvm::Value *V, BlockFieldFlags flags,
bool CanThrow) {
llvm::FunctionCallee F = CGM.getBlockObjectDispose();
llvm::Value *args[] = {
Builder.CreateBitCast(V, Int8PtrTy),
llvm::ConstantInt::get(Int32Ty, flags.getBitMask())
};
llvm::Value *args[] = {V,
llvm::ConstantInt::get(Int32Ty, flags.getBitMask())};

if (CanThrow)
EmitRuntimeCallOrInvoke(F, args);
Expand Down
157 changes: 34 additions & 123 deletions clang/lib/CodeGen/CGBuiltin.cpp

Large diffs are not rendered by default.

35 changes: 11 additions & 24 deletions clang/lib/CodeGen/CGClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,8 @@ CodeGenFunction::GetAddressOfDerivedClass(Address BaseAddr,
assert(PathBegin != PathEnd && "Base path should not be empty!");

QualType DerivedTy =
getContext().getCanonicalType(getContext().getTagDeclType(Derived));
unsigned AddrSpace = BaseAddr.getAddressSpace();
getContext().getCanonicalType(getContext().getTagDeclType(Derived));
llvm::Type *DerivedValueTy = ConvertType(DerivedTy);
llvm::Type *DerivedPtrTy =
llvm::PointerType::get(getLLVMContext(), AddrSpace);

llvm::Value *NonVirtualOffset =
CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd);
Expand All @@ -432,13 +429,10 @@ CodeGenFunction::GetAddressOfDerivedClass(Address BaseAddr,
}

// Apply the offset.
llvm::Value *Value = Builder.CreateBitCast(BaseAddr.getPointer(), Int8PtrTy);
llvm::Value *Value = BaseAddr.getPointer();
Value = Builder.CreateInBoundsGEP(
Int8Ty, Value, Builder.CreateNeg(NonVirtualOffset), "sub.ptr");

// Just cast.
Value = Builder.CreateBitCast(Value, DerivedPtrTy);

// Produce a PHI if we had a null-check.
if (NullCheckValue) {
Builder.CreateBr(CastEnd);
Expand Down Expand Up @@ -1676,8 +1670,7 @@ namespace {
CodeGenFunction::SanitizerScope SanScope(&CGF);
// Pass in void pointer and size of region as arguments to runtime
// function
SmallVector<llvm::Value *, 2> Args = {
CGF.Builder.CreateBitCast(Ptr, CGF.VoidPtrTy)};
SmallVector<llvm::Value *, 2> Args = {Ptr};
SmallVector<llvm::Type *, 2> ArgTypes = {CGF.VoidPtrTy};

if (PoisonSize.has_value()) {
Expand Down Expand Up @@ -1756,10 +1749,8 @@ namespace {
llvm::ConstantInt *OffsetSizePtr =
llvm::ConstantInt::get(CGF.SizeTy, PoisonStart.getQuantity());

llvm::Value *OffsetPtr = CGF.Builder.CreateGEP(
CGF.Int8Ty,
CGF.Builder.CreateBitCast(CGF.LoadCXXThis(), CGF.Int8PtrTy),
OffsetSizePtr);
llvm::Value *OffsetPtr =
CGF.Builder.CreateGEP(CGF.Int8Ty, CGF.LoadCXXThis(), OffsetSizePtr);

CharUnits PoisonEnd;
if (EndIndex >= Layout.getFieldCount()) {
Expand Down Expand Up @@ -2736,7 +2727,6 @@ void CodeGenFunction::EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD,
llvm::Value *TypeId =
llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD);

llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy);
// If we already know that the call has hidden LTO visibility, emit
// @llvm.type.test(). Otherwise emit @llvm.public.type.test(), which WPD
// will convert to @llvm.type.test() if we assert at link time that we have
Expand All @@ -2745,7 +2735,7 @@ void CodeGenFunction::EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD,
? llvm::Intrinsic::type_test
: llvm::Intrinsic::public_type_test;
llvm::Value *TypeTest =
Builder.CreateCall(CGM.getIntrinsic(IID), {CastedVTable, TypeId});
Builder.CreateCall(CGM.getIntrinsic(IID), {VTable, TypeId});
Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::assume), TypeTest);
}
}
Expand Down Expand Up @@ -2849,9 +2839,8 @@ void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD,
CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);

llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy);
llvm::Value *TypeTest = Builder.CreateCall(
CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, TypeId});
CGM.getIntrinsic(llvm::Intrinsic::type_test), {VTable, TypeId});

llvm::Constant *StaticData[] = {
llvm::ConstantInt::get(Int8Ty, TCK),
Expand All @@ -2861,7 +2850,7 @@ void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD,

auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD);
if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) {
EmitCfiSlowPathCheck(M, TypeTest, CrossDsoTypeId, CastedVTable, StaticData);
EmitCfiSlowPathCheck(M, TypeTest, CrossDsoTypeId, VTable, StaticData);
return;
}

Expand All @@ -2874,9 +2863,9 @@ void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD,
CGM.getLLVMContext(),
llvm::MDString::get(CGM.getLLVMContext(), "all-vtables"));
llvm::Value *ValidVtable = Builder.CreateCall(
CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, AllVtables});
CGM.getIntrinsic(llvm::Intrinsic::type_test), {VTable, AllVtables});
EmitCheck(std::make_pair(TypeTest, M), SanitizerHandler::CFICheckFail,
StaticData, {CastedVTable, ValidVtable});
StaticData, {VTable, ValidVtable});
}

bool CodeGenFunction::ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD) {
Expand Down Expand Up @@ -2907,11 +2896,9 @@ llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad(
CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
llvm::Value *TypeId = llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD);

llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy);
llvm::Value *CheckedLoad = Builder.CreateCall(
CGM.getIntrinsic(llvm::Intrinsic::type_checked_load),
{CastedVTable, llvm::ConstantInt::get(Int32Ty, VTableByteOffset),
TypeId});
{VTable, llvm::ConstantInt::get(Int32Ty, VTableByteOffset), TypeId});
llvm::Value *CheckResult = Builder.CreateExtractValue(CheckedLoad, 1);

std::string TypeName = RD->getQualifiedNameAsString();
Expand Down
41 changes: 11 additions & 30 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ Address CodeGenFunction::CreateMemTemp(QualType Ty, CharUnits Align,
auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(),
ArrayTy->getNumElements());

Result = Address(
Builder.CreateBitCast(Result.getPointer(), VectorTy->getPointerTo()),
VectorTy, Result.getAlignment(), KnownNonNull);
Result = Address(Result.getPointer(), VectorTy, Result.getAlignment(),
KnownNonNull);
}
return Result;
}
Expand Down Expand Up @@ -746,9 +745,8 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
llvm::Value *Min = Builder.getFalse();
llvm::Value *NullIsUnknown = Builder.getFalse();
llvm::Value *Dynamic = Builder.getFalse();
llvm::Value *CastAddr = Builder.CreateBitCast(Ptr, Int8PtrTy);
llvm::Value *LargeEnough = Builder.CreateICmpUGE(
Builder.CreateCall(F, {CastAddr, Min, NullIsUnknown, Dynamic}), Size);
Builder.CreateCall(F, {Ptr, Min, NullIsUnknown, Dynamic}), Size);
Checks.push_back(std::make_pair(LargeEnough, SanitizerKind::ObjectSize));
}
}
Expand Down Expand Up @@ -825,9 +823,7 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,

// Load the vptr, and compute hash_16_bytes(TypeHash, vptr).
llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash);
llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0);
Address VPtrAddr(Builder.CreateBitCast(Ptr, VPtrTy), IntPtrTy,
getPointerAlign());
Address VPtrAddr(Ptr, IntPtrTy, getPointerAlign());
llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr);
llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty);

Expand Down Expand Up @@ -2492,14 +2488,6 @@ static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
}
}

static llvm::Value *
EmitBitCastOfLValueToProperType(CodeGenFunction &CGF,
llvm::Value *V, llvm::Type *IRType,
StringRef Name = StringRef()) {
unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
}

static LValue EmitThreadPrivateVarDeclLValue(
CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr,
llvm::Type *RealVarTy, SourceLocation Loc) {
Expand Down Expand Up @@ -2600,7 +2588,6 @@ static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
V = CGF.Builder.CreateThreadLocalAddress(V);

llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy);
CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
Address Addr(V, RealVarTy, Alignment);
// Emit reference to the private copy of the variable if it is an OpenMP
Expand Down Expand Up @@ -3421,8 +3408,7 @@ void CodeGenFunction::EmitCfiSlowPathCheck(
"__cfi_slowpath_diag",
llvm::FunctionType::get(VoidTy, {Int64Ty, Int8PtrTy, Int8PtrTy},
false));
CheckCall = Builder.CreateCall(
SlowPathFn, {TypeId, Ptr, Builder.CreateBitCast(InfoPtr, Int8PtrTy)});
CheckCall = Builder.CreateCall(SlowPathFn, {TypeId, Ptr, InfoPtr});
} else {
SlowPathFn = CGM.getModule().getOrInsertFunction(
"__cfi_slowpath",
Expand Down Expand Up @@ -5365,8 +5351,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee
AlignedCalleePtr = CalleePtr;
}

llvm::Value *CalleePrefixStruct = Builder.CreateBitCast(
AlignedCalleePtr, llvm::PointerType::getUnqual(PrefixStructTy));
llvm::Value *CalleePrefixStruct = AlignedCalleePtr;
llvm::Value *CalleeSigPtr =
Builder.CreateConstGEP2_32(PrefixStructTy, CalleePrefixStruct, -1, 0);
llvm::Value *CalleeSig =
Expand Down Expand Up @@ -5413,9 +5398,8 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee
llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);

llvm::Value *CalleePtr = Callee.getFunctionPointer();
llvm::Value *CastedCallee = Builder.CreateBitCast(CalleePtr, Int8PtrTy);
llvm::Value *TypeTest = Builder.CreateCall(
CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedCallee, TypeId});
CGM.getIntrinsic(llvm::Intrinsic::type_test), {CalleePtr, TypeId});

auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD);
llvm::Constant *StaticData[] = {
Expand All @@ -5425,18 +5409,17 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee
};
if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) {
EmitCfiSlowPathCheck(SanitizerKind::CFIICall, TypeTest, CrossDsoTypeId,
CastedCallee, StaticData);
CalleePtr, StaticData);
} else {
EmitCheck(std::make_pair(TypeTest, SanitizerKind::CFIICall),
SanitizerHandler::CFICheckFail, StaticData,
{CastedCallee, llvm::UndefValue::get(IntPtrTy)});
{CalleePtr, llvm::UndefValue::get(IntPtrTy)});
}
}

CallArgList Args;
if (Chain)
Args.add(RValue::get(Builder.CreateBitCast(Chain, CGM.VoidPtrTy)),
CGM.getContext().VoidPtrTy);
Args.add(RValue::get(Chain), CGM.getContext().VoidPtrTy);

// C++17 requires that we evaluate arguments to a call using assignment syntax
// right-to-left, and that we evaluate arguments to certain other operators
Expand Down Expand Up @@ -5507,10 +5490,8 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee
isa<CUDAKernelCallExpr>(E) &&
(!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
llvm::Value *Handle = Callee.getFunctionPointer();
auto *Cast =
Builder.CreateBitCast(Handle, Handle->getType()->getPointerTo());
auto *Stub = Builder.CreateLoad(
Address(Cast, Handle->getType(), CGM.getPointerAlign()));
Address(Handle, Handle->getType(), CGM.getPointerAlign()));
Callee.setFunctionPointer(Stub);
}
llvm::CallBase *CallOrInvoke = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3690,8 +3690,8 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF,

index = CGF.Builder.CreateMul(index, objectSize);

Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy);
result = CGF.Builder.CreateGEP(CGF.Int8Ty, result, index, "add.ptr");
Value *result =
CGF.Builder.CreateGEP(CGF.Int8Ty, pointer, index, "add.ptr");
return CGF.Builder.CreateBitCast(result, pointer->getType());
}

Expand Down
3 changes: 1 addition & 2 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2049,8 +2049,7 @@ CodeGenFunction::EmitNullInitialization(Address DestPtr, QualType Ty) {
NullConstant, Twine());
CharUnits NullAlign = DestPtr.getAlignment();
NullVariable->setAlignment(NullAlign.getAsAlign());
Address SrcPtr(Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy()),
Builder.getInt8Ty(), NullAlign);
Address SrcPtr(NullVariable, Builder.getInt8Ty(), NullAlign);

if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal);

Expand Down
22 changes: 7 additions & 15 deletions clang/lib/CodeGen/ItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,7 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
// Apply the adjustment and cast back to the original struct type
// for consistency.
llvm::Value *This = ThisAddr.getPointer();
llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
Ptr = Builder.CreateInBoundsGEP(Builder.getInt8Ty(), Ptr, Adj);
This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
This = Builder.CreateInBoundsGEP(Builder.getInt8Ty(), This, Adj);
ThisPtrForCall = This;

// Load the function pointer.
Expand Down Expand Up @@ -740,9 +738,8 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
? llvm::Intrinsic::type_test
: llvm::Intrinsic::public_type_test;

CheckResult = Builder.CreateCall(
CGM.getIntrinsic(IID),
{Builder.CreateBitCast(VFPAddr, CGF.Int8PtrTy), TypeId});
CheckResult =
Builder.CreateCall(CGM.getIntrinsic(IID), {VFPAddr, TypeId});
}

if (CGM.getItaniumVTableContext().isRelativeLayout()) {
Expand Down Expand Up @@ -812,8 +809,6 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
};

llvm::Value *Bit = Builder.getFalse();
llvm::Value *CastedNonVirtualFn =
Builder.CreateBitCast(NonVirtualFn, CGF.Int8PtrTy);
for (const CXXRecordDecl *Base : CGM.getMostBaseClasses(RD)) {
llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(
getContext().getMemberPointerType(
Expand All @@ -824,13 +819,13 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(

llvm::Value *TypeTest =
Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test),
{CastedNonVirtualFn, TypeId});
{NonVirtualFn, TypeId});
Bit = Builder.CreateOr(Bit, TypeTest);
}

CGF.EmitCheck(std::make_pair(Bit, SanitizerKind::CFIMFCall),
SanitizerHandler::CFICheckFail, StaticData,
{CastedNonVirtualFn, llvm::UndefValue::get(CGF.IntPtrTy)});
{NonVirtualFn, llvm::UndefValue::get(CGF.IntPtrTy)});

FnNonVirtual = Builder.GetInsertBlock();
}
Expand Down Expand Up @@ -1253,8 +1248,7 @@ void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
CGF.getPointerAlign());

// Apply the offset.
llvm::Value *CompletePtr =
CGF.Builder.CreateBitCast(Ptr.getPointer(), CGF.Int8PtrTy);
llvm::Value *CompletePtr = Ptr.getPointer();
CompletePtr =
CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, CompletePtr, Offset);

Expand Down Expand Up @@ -1454,7 +1448,6 @@ llvm::Value *ItaniumCXXABI::EmitTypeid(CodeGenFunction &CGF,

if (CGM.getItaniumVTableContext().isRelativeLayout()) {
// Load the type info.
Value = CGF.Builder.CreateBitCast(Value, CGM.Int8PtrTy);
Value = CGF.Builder.CreateCall(
CGM.getIntrinsic(llvm::Intrinsic::load_relative, {CGM.Int32Ty}),
{Value, llvm::ConstantInt::get(CGM.Int32Ty, -4)});
Expand Down Expand Up @@ -2211,8 +2204,7 @@ static llvm::Value *performTypeAdjustment(CodeGenFunction &CGF,
NonVirtualAdjustment);
}

// Cast back to the original type.
return CGF.Builder.CreateBitCast(ResultPtr, InitialPtr.getType());
return ResultPtr;
}

llvm::Value *ItaniumCXXABI::performThisAdjustment(CodeGenFunction &CGF,
Expand Down

0 comments on commit 2bdc864

Please sign in to comment.