Skip to content

Commit

Permalink
[clang][CodeGen] Simplify code based on opaque pointers (#65624)
Browse files Browse the repository at this point in the history
- Update CodeGenTypeCache to use a single union for all pointers in
  address space zero.
- Introduce a UnqualPtrTy in CodeGenTypeCache, and use that (for
  example instead of llvm::PointerType::getUnqual) in some places.
- Drop some redundant bit/pointers casts from ptr to ptr.
  • Loading branch information
bjope committed Sep 25, 2023
1 parent 85175ed commit b4858c6
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 147 deletions.
3 changes: 1 addition & 2 deletions clang/lib/CodeGen/CGAtomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ namespace {
llvm::Value *StoragePtr = CGF.Builder.CreateConstGEP1_64(
CGF.Int8Ty, BitFieldPtr, OffsetInChars.getQuantity());
StoragePtr = CGF.Builder.CreateAddrSpaceCast(
StoragePtr, llvm::PointerType::getUnqual(CGF.getLLVMContext()),
"atomic_bitfield_base");
StoragePtr, CGF.UnqualPtrTy, "atomic_bitfield_base");
BFI = OrigBFI;
BFI.Offset = Offset;
BFI.StorageSize = AtomicSizeInBits;
Expand Down
20 changes: 7 additions & 13 deletions clang/lib/CodeGen/CGBlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1189,8 +1189,8 @@ RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E,
}
} else {
// Bitcast the block literal to a generic block literal.
BlockPtr = Builder.CreatePointerCast(
BlockPtr, llvm::PointerType::get(GenBlockTy, 0), "block.literal");
BlockPtr =
Builder.CreatePointerCast(BlockPtr, UnqualPtrTy, "block.literal");
// Get pointer to the block invoke function
llvm::Value *FuncPtr = Builder.CreateStructGEP(GenBlockTy, BlockPtr, 3);

Expand All @@ -1208,12 +1208,6 @@ RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E,
const CGFunctionInfo &FnInfo =
CGM.getTypes().arrangeBlockFunctionCall(Args, FuncTy);

// Cast the function pointer to the right type.
llvm::Type *BlockFTy = CGM.getTypes().GetFunctionType(FnInfo);

llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Func = Builder.CreatePointerCast(Func, BlockFTyPtr);

// Prepare the callee.
CGCallee Callee(CGCalleeInfo(), Func);

Expand Down Expand Up @@ -2589,11 +2583,11 @@ const BlockByrefInfo &CodeGenFunction::getBlockByrefInfo(const VarDecl *D) {
SmallVector<llvm::Type *, 8> types;

// void *__isa;
types.push_back(Int8PtrTy);
types.push_back(VoidPtrTy);
size += getPointerSize();

// void *__forwarding;
types.push_back(llvm::PointerType::getUnqual(byrefType));
types.push_back(VoidPtrTy);
size += getPointerSize();

// int32_t __flags;
Expand All @@ -2608,11 +2602,11 @@ const BlockByrefInfo &CodeGenFunction::getBlockByrefInfo(const VarDecl *D) {
bool hasCopyAndDispose = getContext().BlockRequiresCopying(Ty, D);
if (hasCopyAndDispose) {
/// void *__copy_helper;
types.push_back(Int8PtrTy);
types.push_back(VoidPtrTy);
size += getPointerSize();

/// void *__destroy_helper;
types.push_back(Int8PtrTy);
types.push_back(VoidPtrTy);
size += getPointerSize();
}

Expand All @@ -2621,7 +2615,7 @@ const BlockByrefInfo &CodeGenFunction::getBlockByrefInfo(const VarDecl *D) {
if (getContext().getByrefLifetime(Ty, Lifetime, HasByrefExtendedLayout) &&
HasByrefExtendedLayout) {
/// void *__byref_variable_layout;
types.push_back(Int8PtrTy);
types.push_back(VoidPtrTy);
size += CharUnits::fromQuantity(PointerSizeInBytes);
}

Expand Down
99 changes: 30 additions & 69 deletions clang/lib/CodeGen/CGBuiltin.cpp

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions clang/lib/CodeGen/CGCUDANV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,15 @@ CGNVCUDARuntime::CGNVCUDARuntime(CodeGenModule &CGM)
TheModule(CGM.getModule()),
RelocatableDeviceCode(CGM.getLangOpts().GPURelocatableDeviceCode),
DeviceMC(InitDeviceMC(CGM)) {
CodeGen::CodeGenTypes &Types = CGM.getTypes();
ASTContext &Ctx = CGM.getContext();

IntTy = CGM.IntTy;
SizeTy = CGM.SizeTy;
VoidTy = CGM.VoidTy;
Zeros[0] = llvm::ConstantInt::get(SizeTy, 0);
Zeros[1] = Zeros[0];

CharPtrTy = llvm::PointerType::getUnqual(Types.ConvertType(Ctx.CharTy));
VoidPtrTy = cast<llvm::PointerType>(Types.ConvertType(Ctx.VoidPtrTy));
VoidPtrPtrTy = llvm::PointerType::getUnqual(CGM.getLLVMContext());
CharPtrTy = CGM.UnqualPtrTy;
VoidPtrTy = CGM.UnqualPtrTy;
VoidPtrPtrTy = CGM.UnqualPtrTy;
}

llvm::FunctionCallee CGNVCUDARuntime::getSetupArgumentFn() const {
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
const char *Name) {
ErrorUnsupported(E, Name);
llvm::Type *ElTy = ConvertType(E->getType());
llvm::Type *Ty = llvm::PointerType::getUnqual(ElTy);
llvm::Type *Ty = UnqualPtrTy;
return MakeAddrLValue(
Address(llvm::UndefValue::get(Ty), ElTy, CharUnits::One()), E->getType());
}
Expand Down Expand Up @@ -4599,7 +4599,7 @@ std::optional<LValue> HandleConditionalOperatorLValueSimpleCase(
if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Live->IgnoreParens())) {
CGF.EmitCXXThrowExpr(ThrowExpr);
llvm::Type *ElemTy = CGF.ConvertType(Dead->getType());
llvm::Type *Ty = llvm::PointerType::getUnqual(ElemTy);
llvm::Type *Ty = CGF.UnqualPtrTy;
return CGF.MakeAddrLValue(
Address(llvm::UndefValue::get(Ty), ElemTy, CharUnits::One()),
Dead->getType());
Expand Down
4 changes: 1 addition & 3 deletions clang/lib/CodeGen/CGObjCRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
CGF.CGM.getContext().getObjCObjectPointerType(InterfaceTy);
QualType IvarTy =
Ivar->getUsageType(ObjectPtrTy).withCVRQualifiers(CVRQualifiers);
llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, CGF.Int8PtrTy);
llvm::Value *V = BaseValue;
V = CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, V, Offset, "add.ptr");

if (!Ivar->isBitField()) {
V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
LValue LV = CGF.MakeNaturalAlignAddrLValue(V, IvarTy);
return LV;
}
Expand Down
16 changes: 7 additions & 9 deletions clang/lib/CodeGen/CGOpenCLRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ llvm::PointerType *CGOpenCLRuntime::getPointerType(const Type *T,
llvm::LLVMContext &Ctx = CGM.getLLVMContext();
uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T));
auto *PTy =
llvm::PointerType::get(llvm::StructType::create(Ctx, Name), AddrSpc);
auto *PTy = llvm::PointerType::get(Ctx, AddrSpc);
CachedTys[Name] = PTy;
return PTy;
}
Expand All @@ -90,10 +89,9 @@ llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) {
llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T, StringRef Name,
llvm::Type *&PipeTy) {
if (!PipeTy)
PipeTy = llvm::PointerType::get(llvm::StructType::create(
CGM.getLLVMContext(), Name),
CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T)));
PipeTy = llvm::PointerType::get(
CGM.getLLVMContext(), CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T)));
return PipeTy;
}

Expand All @@ -105,10 +103,10 @@ llvm::Type *CGOpenCLRuntime::getSamplerType(const Type *T) {
CGM, CGM.getContext().OCLSamplerTy.getTypePtr()))
SamplerTy = TransTy;
else
// struct opencl.sampler_t*
SamplerTy = llvm::PointerType::get(
llvm::StructType::create(CGM.getLLVMContext(), "opencl.sampler_t"),
CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T)));
CGM.getLLVMContext(), CGM.getContext().getTargetAddressSpace(
CGM.getContext().getOpenCLTypeAddrSpace(T)));
return SamplerTy;
}

Expand Down
13 changes: 7 additions & 6 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,14 @@ CodeGenModule::CodeGenModule(ASTContext &C,
IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
IntPtrTy = llvm::IntegerType::get(LLVMContext,
C.getTargetInfo().getMaxPointerWidth());
Int8PtrTy = Int8Ty->getPointerTo(0);
Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
Int8PtrTy = llvm::PointerType::get(LLVMContext, 0);
const llvm::DataLayout &DL = M.getDataLayout();
AllocaInt8PtrTy = Int8Ty->getPointerTo(DL.getAllocaAddrSpace());
GlobalsInt8PtrTy = Int8Ty->getPointerTo(DL.getDefaultGlobalsAddressSpace());
ConstGlobalsPtrTy = Int8Ty->getPointerTo(
C.getTargetAddressSpace(GetGlobalConstantAddressSpace()));
AllocaInt8PtrTy =
llvm::PointerType::get(LLVMContext, DL.getAllocaAddrSpace());
GlobalsInt8PtrTy =
llvm::PointerType::get(LLVMContext, DL.getDefaultGlobalsAddressSpace());
ConstGlobalsPtrTy = llvm::PointerType::get(
LLVMContext, C.getTargetAddressSpace(GetGlobalConstantAddressSpace()));
ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace();

// Build C++20 Module initializers.
Expand Down
7 changes: 2 additions & 5 deletions clang/lib/CodeGen/CodeGenTypeCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,11 @@ struct CodeGenTypeCache {
llvm::IntegerType *PtrDiffTy;
};

/// void* in address space 0
/// void*, void** in address space 0
union {
llvm::PointerType *UnqualPtrTy;
llvm::PointerType *VoidPtrTy;
llvm::PointerType *Int8PtrTy;
};

/// void** in address space 0
union {
llvm::PointerType *VoidPtrPtrTy;
llvm::PointerType *Int8PtrPtrTy;
};
Expand Down
39 changes: 16 additions & 23 deletions clang/lib/CodeGen/ItaniumCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,9 +750,9 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
} else {
llvm::Value *VFPAddr =
CGF.Builder.CreateGEP(CGF.Int8Ty, VTable, VTableOffset);
VirtualFn = CGF.Builder.CreateAlignedLoad(
llvm::PointerType::getUnqual(CGF.getLLVMContext()), VFPAddr,
CGF.getPointerAlign(), "memptr.virtualfn");
VirtualFn = CGF.Builder.CreateAlignedLoad(CGF.UnqualPtrTy, VFPAddr,
CGF.getPointerAlign(),
"memptr.virtualfn");
}
}
assert(VirtualFn && "Virtual fuction pointer not created!");
Expand Down Expand Up @@ -792,9 +792,8 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(
// In the non-virtual path, the function pointer is actually a
// function pointer.
CGF.EmitBlock(FnNonVirtual);
llvm::Value *NonVirtualFn = Builder.CreateIntToPtr(
FnAsInt, llvm::PointerType::getUnqual(CGF.getLLVMContext()),
"memptr.nonvirtualfn");
llvm::Value *NonVirtualFn =
Builder.CreateIntToPtr(FnAsInt, CGF.UnqualPtrTy, "memptr.nonvirtualfn");

// Check the function pointer if CFI on member function pointers is enabled.
if (ShouldEmitCFICheck) {
Expand Down Expand Up @@ -833,8 +832,7 @@ CGCallee ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(

// We're done.
CGF.EmitBlock(FnEnd);
llvm::PHINode *CalleePtr =
Builder.CreatePHI(llvm::PointerType::getUnqual(CGF.getLLVMContext()), 2);
llvm::PHINode *CalleePtr = Builder.CreatePHI(CGF.UnqualPtrTy, 2);
CalleePtr->addIncoming(VirtualFn, FnVirtual);
CalleePtr->addIncoming(NonVirtualFn, FnNonVirtual);

Expand Down Expand Up @@ -1238,8 +1236,7 @@ void ItaniumCXXABI::emitVirtualObjectDelete(CodeGenFunction &CGF,
// Grab the vtable pointer as an intptr_t*.
auto *ClassDecl =
cast<CXXRecordDecl>(ElementType->castAs<RecordType>()->getDecl());
llvm::Value *VTable = CGF.GetVTablePtr(
Ptr, llvm::PointerType::getUnqual(CGF.getLLVMContext()), ClassDecl);
llvm::Value *VTable = CGF.GetVTablePtr(Ptr, CGF.UnqualPtrTy, ClassDecl);

// Track back to entry -2 and pull out the offset there.
llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
Expand Down Expand Up @@ -1591,9 +1588,8 @@ llvm::Value *ItaniumCXXABI::emitDynamicCastToVoid(CodeGenFunction &CGF,
llvm::Value *OffsetToTop;
if (CGM.getItaniumVTableContext().isRelativeLayout()) {
// Get the vtable pointer.
llvm::Value *VTable = CGF.GetVTablePtr(
ThisAddr, llvm::PointerType::getUnqual(CGF.getLLVMContext()),
ClassDecl);
llvm::Value *VTable =
CGF.GetVTablePtr(ThisAddr, CGF.UnqualPtrTy, ClassDecl);

// Get the offset-to-top from the vtable.
OffsetToTop =
Expand All @@ -1605,9 +1601,8 @@ llvm::Value *ItaniumCXXABI::emitDynamicCastToVoid(CodeGenFunction &CGF,
CGF.ConvertType(CGF.getContext().getPointerDiffType());

// Get the vtable pointer.
llvm::Value *VTable = CGF.GetVTablePtr(
ThisAddr, llvm::PointerType::getUnqual(CGF.getLLVMContext()),
ClassDecl);
llvm::Value *VTable =
CGF.GetVTablePtr(ThisAddr, CGF.UnqualPtrTy, ClassDecl);

// Get the offset-to-top from the vtable.
OffsetToTop =
Expand Down Expand Up @@ -2308,8 +2303,8 @@ llvm::Value *ItaniumCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
// cookie, otherwise return 0 to avoid an infinite loop calling DTORs.
// We can't simply ignore this load using nosanitize metadata because
// the metadata may be lost.
llvm::FunctionType *FTy = llvm::FunctionType::get(
CGF.SizeTy, llvm::PointerType::getUnqual(CGF.getLLVMContext()), false);
llvm::FunctionType *FTy =
llvm::FunctionType::get(CGF.SizeTy, CGF.UnqualPtrTy, false);
llvm::FunctionCallee F =
CGM.CreateRuntimeFunction(FTy, "__asan_load_cxx_array_cookie");
return CGF.Builder.CreateCall(F, numElementsPtr.getPointer());
Expand Down Expand Up @@ -2652,7 +2647,7 @@ static void emitGlobalDtorWithCXAAtExit(CodeGenFunction &CGF,

// We're assuming that the destructor function is something we can
// reasonably call with the default CC.
llvm::Type *dtorTy = llvm::PointerType::getUnqual(CGF.getLLVMContext());
llvm::Type *dtorTy = CGF.UnqualPtrTy;

// Preserve address space of addr.
auto AddrAS = addr ? addr->getType()->getPointerAddressSpace() : 0;
Expand Down Expand Up @@ -4654,8 +4649,7 @@ static void InitCatchParam(CodeGenFunction &CGF,
auto catchRD = CatchType->getAsCXXRecordDecl();
CharUnits caughtExnAlignment = CGF.CGM.getClassPointerAlignment(catchRD);

llvm::Type *PtrTy =
llvm::PointerType::getUnqual(CGF.getLLVMContext()); // addrspace 0 ok
llvm::Type *PtrTy = CGF.UnqualPtrTy; // addrspace 0 ok

// Check for a copy expression. If we don't have a copy expression,
// that means a trivial copy is okay.
Expand Down Expand Up @@ -4843,8 +4837,7 @@ void XLCXXABI::registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D,
llvm::FunctionCallee Dtor,
llvm::Constant *Addr) {
if (D.getTLSKind() != VarDecl::TLS_None) {
llvm::PointerType *PtrTy =
llvm::PointerType::getUnqual(CGF.getLLVMContext());
llvm::PointerType *PtrTy = CGF.UnqualPtrTy;

// extern "C" int __pt_atexit_np(int flags, int(*)(int,...), ...);
llvm::FunctionType *AtExitTy =
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/Targets/PPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,

llvm::Type *DirectTy = CGF.ConvertType(Ty), *ElementTy = DirectTy;
if (isIndirect)
DirectTy = llvm::PointerType::getUnqual(CGF.getLLVMContext());
DirectTy = CGF.UnqualPtrTy;

// Case 1: consume registers.
Address RegAddr = Address::invalid();
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/Targets/Sparc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
CGBuilderTy &Builder = CGF.Builder;
Address Addr = Address(Builder.CreateLoad(VAListAddr, "ap.cur"),
getVAListElementType(CGF), SlotSize);
llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
llvm::Type *ArgPtrTy = CGF.UnqualPtrTy;

auto TypeInfo = getContext().getTypeInfoInChars(Ty);

Expand Down
10 changes: 3 additions & 7 deletions clang/lib/CodeGen/Targets/X86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2978,9 +2978,7 @@ static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,

// AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
llvm::Value *Res =
CGF.Builder.CreateBitCast(overflow_arg_area,
llvm::PointerType::getUnqual(LTy));
llvm::Value *Res = overflow_arg_area;

// AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
// l->overflow_arg_area + sizeof(type).
Expand Down Expand Up @@ -3083,8 +3081,6 @@ Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
llvm::Type *TyHi = ST->getElementType(1);
assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
"Unexpected ABI info for mixed regs");
llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
llvm::Value *GPAddr =
CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, gp_offset);
llvm::Value *FPAddr =
Expand All @@ -3095,13 +3091,13 @@ Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
// Copy the first element.
// FIXME: Our choice of alignment here and below is probably pessimistic.
llvm::Value *V = CGF.Builder.CreateAlignedLoad(
TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
TyLo, RegLoAddr,
CharUnits::fromQuantity(getDataLayout().getABITypeAlign(TyLo)));
CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));

// Copy the second element.
V = CGF.Builder.CreateAlignedLoad(
TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
TyHi, RegHiAddr,
CharUnits::fromQuantity(getDataLayout().getABITypeAlign(TyHi)));
CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));

Expand Down

0 comments on commit b4858c6

Please sign in to comment.