Skip to content

Commit

Permalink
[clang][CodeGen] Remove no-op EmitCastToVoidPtr (NFC)
Browse files Browse the repository at this point in the history
Reviewed By: JOE1994

Differential Revision: https://reviews.llvm.org/D153694
  • Loading branch information
s-barannikov committed Jun 29, 2023
1 parent 0e84eec commit 2348902
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 154 deletions.
56 changes: 23 additions & 33 deletions clang/lib/CodeGen/CGAtomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@ namespace {
AtomicSizeInBits = C.toBits(
C.toCharUnitsFromBits(Offset + OrigBFI.Size + C.getCharWidth() - 1)
.alignTo(lvalue.getAlignment()));
auto VoidPtrAddr = CGF.EmitCastToVoidPtr(lvalue.getBitFieldPointer());
llvm::Value *BitFieldPtr = lvalue.getBitFieldPointer();
auto OffsetInChars =
(C.toCharUnitsFromBits(OrigBFI.Offset) / lvalue.getAlignment()) *
lvalue.getAlignment();
VoidPtrAddr = CGF.Builder.CreateConstGEP1_64(
CGF.Int8Ty, VoidPtrAddr, OffsetInChars.getQuantity());
llvm::Type *IntTy = CGF.Builder.getIntNTy(AtomicSizeInBits);
auto Addr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
VoidPtrAddr, llvm::PointerType::getUnqual(CGF.getLLVMContext()),
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");
BFI = OrigBFI;
BFI.Offset = Offset;
BFI.StorageSize = AtomicSizeInBits;
BFI.StorageOffset += OffsetInChars;
LVal = LValue::MakeBitfield(Address(Addr, IntTy, lvalue.getAlignment()),
BFI, lvalue.getType(), lvalue.getBaseInfo(),
lvalue.getTBAAInfo());
llvm::Type *StorageTy = CGF.Builder.getIntNTy(AtomicSizeInBits);
LVal = LValue::MakeBitfield(
Address(StoragePtr, StorageTy, lvalue.getAlignment()), BFI,
lvalue.getType(), lvalue.getBaseInfo(), lvalue.getTBAAInfo());
AtomicTy = C.getIntTypeForBitwidth(AtomicSizeInBits, OrigBFI.IsSigned);
if (AtomicTy.isNull()) {
llvm::APInt Size(
Expand Down Expand Up @@ -805,8 +805,7 @@ AddDirectArgument(CodeGenFunction &CGF, CallArgList &Args,
Args.add(RValue::get(Val), ValTy);
} else {
// Non-optimized functions always take a reference.
Args.add(RValue::get(CGF.EmitCastToVoidPtr(Val)),
CGF.getContext().VoidPtrTy);
Args.add(RValue::get(Val), CGF.getContext().VoidPtrTy);
}
}

Expand Down Expand Up @@ -1103,8 +1102,8 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) {
*this, V, AS, LangAS::opencl_generic, DestType, false);
};

Args.add(RValue::get(CastToGenericAddrSpace(
EmitCastToVoidPtr(Ptr.getPointer()), E->getPtr()->getType())),
Args.add(RValue::get(CastToGenericAddrSpace(Ptr.getPointer(),
E->getPtr()->getType())),
getContext().VoidPtrTy);

std::string LibCallName;
Expand Down Expand Up @@ -1137,10 +1136,9 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) {
LibCallName = "__atomic_compare_exchange";
RetTy = getContext().BoolTy;
HaveRetTy = true;
Args.add(
RValue::get(CastToGenericAddrSpace(
EmitCastToVoidPtr(Val1.getPointer()), E->getVal1()->getType())),
getContext().VoidPtrTy);
Args.add(RValue::get(CastToGenericAddrSpace(Val1.getPointer(),
E->getVal1()->getType())),
getContext().VoidPtrTy);
AddDirectArgument(*this, Args, UseOptimizedLibcall, Val2.getPointer(),
MemTy, E->getExprLoc(), TInfo.Width);
Args.add(RValue::get(Order), getContext().IntTy);
Expand Down Expand Up @@ -1302,8 +1300,7 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) {
} else {
// Value is returned through parameter before the order.
RetTy = getContext().VoidTy;
Args.add(RValue::get(EmitCastToVoidPtr(Dest.getPointer())),
getContext().VoidPtrTy);
Args.add(RValue::get(Dest.getPointer()), getContext().VoidPtrTy);
}
}
// order is always the last parameter
Expand Down Expand Up @@ -1571,10 +1568,8 @@ void AtomicInfo::EmitAtomicLoadLibcall(llvm::Value *AddForLoaded,
// void __atomic_load(size_t size, void *mem, void *return, int order);
CallArgList Args;
Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicPointer())),
CGF.getContext().VoidPtrTy);
Args.add(RValue::get(CGF.EmitCastToVoidPtr(AddForLoaded)),
CGF.getContext().VoidPtrTy);
Args.add(RValue::get(getAtomicPointer()), CGF.getContext().VoidPtrTy);
Args.add(RValue::get(AddForLoaded), CGF.getContext().VoidPtrTy);
Args.add(
RValue::get(llvm::ConstantInt::get(CGF.IntTy, (int)llvm::toCABI(AO))),
CGF.getContext().IntTy);
Expand Down Expand Up @@ -1768,12 +1763,9 @@ AtomicInfo::EmitAtomicCompareExchangeLibcall(llvm::Value *ExpectedAddr,
// void *desired, int success, int failure);
CallArgList Args;
Args.add(RValue::get(getAtomicSizeValue()), CGF.getContext().getSizeType());
Args.add(RValue::get(CGF.EmitCastToVoidPtr(getAtomicPointer())),
CGF.getContext().VoidPtrTy);
Args.add(RValue::get(CGF.EmitCastToVoidPtr(ExpectedAddr)),
CGF.getContext().VoidPtrTy);
Args.add(RValue::get(CGF.EmitCastToVoidPtr(DesiredAddr)),
CGF.getContext().VoidPtrTy);
Args.add(RValue::get(getAtomicPointer()), CGF.getContext().VoidPtrTy);
Args.add(RValue::get(ExpectedAddr), CGF.getContext().VoidPtrTy);
Args.add(RValue::get(DesiredAddr), CGF.getContext().VoidPtrTy);
Args.add(RValue::get(
llvm::ConstantInt::get(CGF.IntTy, (int)llvm::toCABI(Success))),
CGF.getContext().IntTy);
Expand Down Expand Up @@ -2076,10 +2068,8 @@ void CodeGenFunction::EmitAtomicStore(RValue rvalue, LValue dest,
CallArgList args;
args.add(RValue::get(atomics.getAtomicSizeValue()),
getContext().getSizeType());
args.add(RValue::get(EmitCastToVoidPtr(atomics.getAtomicPointer())),
getContext().VoidPtrTy);
args.add(RValue::get(EmitCastToVoidPtr(srcAddr.getPointer())),
getContext().VoidPtrTy);
args.add(RValue::get(atomics.getAtomicPointer()), getContext().VoidPtrTy);
args.add(RValue::get(srcAddr.getPointer()), getContext().VoidPtrTy);
args.add(
RValue::get(llvm::ConstantInt::get(IntTy, (int)llvm::toCABI(AO))),
getContext().IntTy);
Expand Down
28 changes: 10 additions & 18 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19198,15 +19198,14 @@ RValue CodeGenFunction::EmitBuiltinAlignTo(const CallExpr *E, bool AlignUp) {
llvm::Value *Difference = Builder.CreateSub(Result, SrcAddr, "diff");
// The result must point to the same underlying allocation. This means we
// can use an inbounds GEP to enable better optimization.
Value *Base = EmitCastToVoidPtr(Args.Src);
if (getLangOpts().isSignedOverflowDefined())
Result = Builder.CreateGEP(Int8Ty, Base, Difference, "aligned_result");
Result =
Builder.CreateGEP(Int8Ty, Args.Src, Difference, "aligned_result");
else
Result = EmitCheckedInBoundsGEP(Int8Ty, Base, Difference,
Result = EmitCheckedInBoundsGEP(Int8Ty, Args.Src, Difference,
/*SignedIndices=*/true,
/*isSubtraction=*/!AlignUp,
E->getExprLoc(), "aligned_result");
Result = Builder.CreatePointerCast(Result, Args.SrcType);
// Emit an alignment assumption to ensure that the new alignment is
// propagated to loads/stores, etc.
emitAlignmentAssumption(Result, E, E->getExprLoc(), Args.Alignment);
Expand Down Expand Up @@ -19763,8 +19762,7 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
}
case WebAssembly::BI__builtin_wasm_table_get: {
assert(E->getArg(0)->getType()->isArrayType());
Value *Table =
EmitCastToVoidPtr(EmitArrayToPointerDecay(E->getArg(0)).getPointer());
Value *Table = EmitArrayToPointerDecay(E->getArg(0)).getPointer();
Value *Index = EmitScalarExpr(E->getArg(1));
Function *Callee;
if (E->getType().isWebAssemblyExternrefType())
Expand All @@ -19778,8 +19776,7 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
}
case WebAssembly::BI__builtin_wasm_table_set: {
assert(E->getArg(0)->getType()->isArrayType());
Value *Table =
EmitCastToVoidPtr(EmitArrayToPointerDecay(E->getArg(0)).getPointer());
Value *Table = EmitArrayToPointerDecay(E->getArg(0)).getPointer();
Value *Index = EmitScalarExpr(E->getArg(1));
Value *Val = EmitScalarExpr(E->getArg(2));
Function *Callee;
Expand All @@ -19794,15 +19791,13 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
}
case WebAssembly::BI__builtin_wasm_table_size: {
assert(E->getArg(0)->getType()->isArrayType());
Value *Value =
EmitCastToVoidPtr(EmitArrayToPointerDecay(E->getArg(0)).getPointer());
Value *Value = EmitArrayToPointerDecay(E->getArg(0)).getPointer();
Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_table_size);
return Builder.CreateCall(Callee, Value);
}
case WebAssembly::BI__builtin_wasm_table_grow: {
assert(E->getArg(0)->getType()->isArrayType());
Value *Table =
EmitCastToVoidPtr(EmitArrayToPointerDecay(E->getArg(0)).getPointer());
Value *Table = EmitArrayToPointerDecay(E->getArg(0)).getPointer();
Value *Val = EmitScalarExpr(E->getArg(1));
Value *NElems = EmitScalarExpr(E->getArg(2));

Expand All @@ -19819,8 +19814,7 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
}
case WebAssembly::BI__builtin_wasm_table_fill: {
assert(E->getArg(0)->getType()->isArrayType());
Value *Table =
EmitCastToVoidPtr(EmitArrayToPointerDecay(E->getArg(0)).getPointer());
Value *Table = EmitArrayToPointerDecay(E->getArg(0)).getPointer();
Value *Index = EmitScalarExpr(E->getArg(1));
Value *Val = EmitScalarExpr(E->getArg(2));
Value *NElems = EmitScalarExpr(E->getArg(3));
Expand All @@ -19838,10 +19832,8 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
}
case WebAssembly::BI__builtin_wasm_table_copy: {
assert(E->getArg(0)->getType()->isArrayType());
Value *TableX =
EmitCastToVoidPtr(EmitArrayToPointerDecay(E->getArg(0)).getPointer());
Value *TableY =
EmitCastToVoidPtr(EmitArrayToPointerDecay(E->getArg(1)).getPointer());
Value *TableX = EmitArrayToPointerDecay(E->getArg(0)).getPointer();
Value *TableY = EmitArrayToPointerDecay(E->getArg(1)).getPointer();
Value *DstIdx = EmitScalarExpr(E->getArg(2));
Value *SrcIdx = EmitScalarExpr(E->getArg(3));
Value *NElems = EmitScalarExpr(E->getArg(4));
Expand Down
14 changes: 7 additions & 7 deletions clang/lib/CodeGen/CGCXXABI.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@ class CGCXXABI {
virtual bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr,
QualType SrcRecordTy) = 0;

virtual llvm::Value *
EmitDynamicCastCall(CodeGenFunction &CGF, Address Value,
QualType SrcRecordTy, QualType DestTy,
QualType DestRecordTy, llvm::BasicBlock *CastEnd) = 0;
virtual llvm::Value *emitDynamicCastCall(CodeGenFunction &CGF, Address Value,
QualType SrcRecordTy,
QualType DestTy,
QualType DestRecordTy,
llvm::BasicBlock *CastEnd) = 0;

virtual llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF,
virtual llvm::Value *emitDynamicCastToVoid(CodeGenFunction &CGF,
Address Value,
QualType SrcRecordTy,
QualType DestTy) = 0;
QualType SrcRecordTy) = 0;

virtual bool EmitBadCastCall(CodeGenFunction &CGF) = 0;

Expand Down
14 changes: 1 addition & 13 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@ using namespace CodeGen;
// Miscellaneous Helper Methods
//===--------------------------------------------------------------------===//

llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) {
unsigned addressSpace =
cast<llvm::PointerType>(value->getType())->getAddressSpace();

llvm::PointerType *destType = Int8PtrTy;
if (addressSpace)
destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace);

if (value->getType() == destType) return value;
return Builder.CreateBitCast(value, destType);
}

/// CreateTempAlloca - This creates a alloca and inserts it into the entry
/// block.
Address CodeGenFunction::CreateTempAllocaWithoutCast(llvm::Type *Ty,
Expand Down Expand Up @@ -3367,7 +3355,7 @@ void CodeGenFunction::EmitCheck(
CGM.getDataLayout().getDefaultGlobalsAddressSpace());
InfoPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr);
Args.push_back(EmitCastToVoidPtr(InfoPtr));
Args.push_back(InfoPtr);
ArgTypes.push_back(Args.back()->getType());
}

Expand Down
8 changes: 3 additions & 5 deletions clang/lib/CodeGen/CGExprCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,7 @@ static void EmitNullBaseClassInitialization(CodeGenFunction &CGF,
std::max(Layout.getNonVirtualAlignment(), DestPtr.getAlignment());
NullVariable->setAlignment(Align.getAsAlign());

Address SrcPtr =
Address(CGF.EmitCastToVoidPtr(NullVariable), CGF.Int8Ty, Align);
Address SrcPtr(NullVariable, CGF.Int8Ty, Align);

// Get and call the appropriate llvm.memcpy overload.
for (std::pair<CharUnits, CharUnits> Store : Stores) {
Expand Down Expand Up @@ -2292,12 +2291,11 @@ llvm::Value *CodeGenFunction::EmitDynamicCast(Address ThisAddr,

llvm::Value *Value;
if (isDynamicCastToVoid) {
Value = CGM.getCXXABI().EmitDynamicCastToVoid(*this, ThisAddr, SrcRecordTy,
DestTy);
Value = CGM.getCXXABI().emitDynamicCastToVoid(*this, ThisAddr, SrcRecordTy);
} else {
assert(DestRecordTy->isRecordType() &&
"destination type must be a record type!");
Value = CGM.getCXXABI().EmitDynamicCastCall(*this, ThisAddr, SrcRecordTy,
Value = CGM.getCXXABI().emitDynamicCastCall(*this, ThisAddr, SrcRecordTy,
DestTy, DestRecordTy, CastEnd);
CastNotNull = Builder.GetInsertBlock();
}
Expand Down
18 changes: 6 additions & 12 deletions clang/lib/CodeGen/CGExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2706,15 +2706,13 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
} else if (type->isFunctionType()) {
llvm::Value *amt = Builder.getInt32(amount);

value = CGF.EmitCastToVoidPtr(value);
if (CGF.getLangOpts().isSignedOverflowDefined())
value = Builder.CreateGEP(CGF.Int8Ty, value, amt, "incdec.funcptr");
else
value = CGF.EmitCheckedInBoundsGEP(CGF.Int8Ty, value, amt,
/*SignedIndices=*/false,
isSubtraction, E->getExprLoc(),
"incdec.funcptr");
value = Builder.CreateBitCast(value, input->getType());
value =
CGF.EmitCheckedInBoundsGEP(CGF.Int8Ty, value, amt,
/*SignedIndices=*/false, isSubtraction,
E->getExprLoc(), "incdec.funcptr");

// For everything else, we can just do a simple increment.
} else {
Expand Down Expand Up @@ -2825,7 +2823,6 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
// Objective-C pointer types.
} else {
const ObjCObjectPointerType *OPT = type->castAs<ObjCObjectPointerType>();
value = CGF.EmitCastToVoidPtr(value);

CharUnits size = CGF.getContext().getTypeSizeInChars(OPT->getObjectType());
if (!isInc) size = -size;
Expand Down Expand Up @@ -3736,11 +3733,8 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF,
// Explicitly handle GNU void* and function pointer arithmetic extensions. The
// GNU void* casts amount to no-ops since our void* type is i8*, but this is
// future proof.
if (elementType->isVoidType() || elementType->isFunctionType()) {
Value *result = CGF.EmitCastToVoidPtr(pointer);
result = CGF.Builder.CreateGEP(CGF.Int8Ty, result, index, "add.ptr");
return CGF.Builder.CreateBitCast(result, pointer->getType());
}
if (elementType->isVoidType() || elementType->isFunctionType())
return CGF.Builder.CreateGEP(CGF.Int8Ty, pointer, index, "add.ptr");

llvm::Type *elemTy = CGF.ConvertTypeForMem(elementType);
if (CGF.getLangOpts().isSignedOverflowDefined())
Expand Down
27 changes: 11 additions & 16 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5680,14 +5680,12 @@ llvm::Value *CGOpenMPRuntime::emitTaskReductionInit(
// ElemLVal.reduce_shar = &Shareds[Cnt];
LValue SharedLVal = CGF.EmitLValueForField(ElemLVal, SharedFD);
RCG.emitSharedOrigLValue(CGF, Cnt);
llvm::Value *CastedShared =
CGF.EmitCastToVoidPtr(RCG.getSharedLValue(Cnt).getPointer(CGF));
CGF.EmitStoreOfScalar(CastedShared, SharedLVal);
llvm::Value *Shared = RCG.getSharedLValue(Cnt).getPointer(CGF);
CGF.EmitStoreOfScalar(Shared, SharedLVal);
// ElemLVal.reduce_orig = &Origs[Cnt];
LValue OrigLVal = CGF.EmitLValueForField(ElemLVal, OrigFD);
llvm::Value *CastedOrig =
CGF.EmitCastToVoidPtr(RCG.getOrigLValue(Cnt).getPointer(CGF));
CGF.EmitStoreOfScalar(CastedOrig, OrigLVal);
llvm::Value *Orig = RCG.getOrigLValue(Cnt).getPointer(CGF);
CGF.EmitStoreOfScalar(Orig, OrigLVal);
RCG.emitAggregateType(CGF, Cnt);
llvm::Value *SizeValInChars;
llvm::Value *SizeVal;
Expand All @@ -5704,21 +5702,19 @@ llvm::Value *CGOpenMPRuntime::emitTaskReductionInit(
CGF.EmitStoreOfScalar(SizeValInChars, SizeLVal);
// ElemLVal.reduce_init = init;
LValue InitLVal = CGF.EmitLValueForField(ElemLVal, InitFD);
llvm::Value *InitAddr =
CGF.EmitCastToVoidPtr(emitReduceInitFunction(CGM, Loc, RCG, Cnt));
llvm::Value *InitAddr = emitReduceInitFunction(CGM, Loc, RCG, Cnt);
CGF.EmitStoreOfScalar(InitAddr, InitLVal);
// ElemLVal.reduce_fini = fini;
LValue FiniLVal = CGF.EmitLValueForField(ElemLVal, FiniFD);
llvm::Value *Fini = emitReduceFiniFunction(CGM, Loc, RCG, Cnt);
llvm::Value *FiniAddr = Fini
? CGF.EmitCastToVoidPtr(Fini)
: llvm::ConstantPointerNull::get(CGM.VoidPtrTy);
llvm::Value *FiniAddr =
Fini ? Fini : llvm::ConstantPointerNull::get(CGM.VoidPtrTy);
CGF.EmitStoreOfScalar(FiniAddr, FiniLVal);
// ElemLVal.reduce_comb = comb;
LValue CombLVal = CGF.EmitLValueForField(ElemLVal, CombFD);
llvm::Value *CombAddr = CGF.EmitCastToVoidPtr(emitReduceCombFunction(
llvm::Value *CombAddr = emitReduceCombFunction(
CGM, Loc, RCG, Cnt, Data.ReductionOps[Cnt], LHSExprs[Cnt],
RHSExprs[Cnt], Data.ReductionCopies[Cnt]));
RHSExprs[Cnt], Data.ReductionCopies[Cnt]);
CGF.EmitStoreOfScalar(CombAddr, CombLVal);
// ElemLVal.flags = 0;
LValue FlagsLVal = CGF.EmitLValueForField(ElemLVal, FlagsFD);
Expand Down Expand Up @@ -7583,8 +7579,7 @@ class MappableExprsHandler {
.getAddress(CGF);
}
Size = CGF.Builder.CreatePtrDiff(
CGF.Int8Ty, CGF.EmitCastToVoidPtr(ComponentLB.getPointer()),
CGF.EmitCastToVoidPtr(LB.getPointer()));
CGF.Int8Ty, ComponentLB.getPointer(), LB.getPointer());
break;
}
}
Expand All @@ -7607,7 +7602,7 @@ class MappableExprsHandler {
CombinedInfo.Pointers.push_back(LB.getPointer());
Size = CGF.Builder.CreatePtrDiff(
CGF.Int8Ty, CGF.Builder.CreateConstGEP(HB, 1).getPointer(),
CGF.EmitCastToVoidPtr(LB.getPointer()));
LB.getPointer());
CombinedInfo.Sizes.push_back(
CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
CombinedInfo.Types.push_back(Flags);
Expand Down

0 comments on commit 2348902

Please sign in to comment.