Skip to content

Commit

Permalink
[clang] Replace uses of CreateElementBitCast (NFC)
Browse files Browse the repository at this point in the history
Partial progress towards replacing uses of CreateElementBitCast, as it
no longer does what its name suggests.

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D154229
  • Loading branch information
JOE1994 committed Jun 30, 2023
1 parent a63d6a0 commit 5f32baf
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 79 deletions.
5 changes: 2 additions & 3 deletions clang/lib/CodeGen/ABIInfoImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ CodeGen::emitVoidPtrDirectVAArg(CodeGenFunction &CGF, Address VAListAddr,
// Cast the element type to i8* if necessary. Some platforms define
// va_list as a struct containing an i8* instead of just an i8*.
if (VAListAddr.getElementType() != CGF.Int8PtrTy)
VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
VAListAddr = VAListAddr.withElementType(CGF.Int8PtrTy);

llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");

Expand All @@ -196,8 +196,7 @@ CodeGen::emitVoidPtrDirectVAArg(CodeGenFunction &CGF, Address VAListAddr,
Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
}

Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
return Addr;
return Addr.withElementType(DirectTy);
}

Address CodeGen::emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
Expand Down
20 changes: 9 additions & 11 deletions clang/lib/CodeGen/CGClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,10 @@ CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(Address This,
// TODO: for complete types, this should be possible with a GEP.
Address V = This;
if (!Offset.isZero()) {
V = Builder.CreateElementBitCast(V, Int8Ty);
V = V.withElementType(Int8Ty);
V = Builder.CreateConstInBoundsByteGEP(V, Offset);
}
V = Builder.CreateElementBitCast(V, ConvertType(Base));

return V;
return V.withElementType(ConvertType(Base));
}

static Address
Expand Down Expand Up @@ -342,7 +340,7 @@ Address CodeGenFunction::GetAddressOfBaseClass(
EmitTypeCheck(TCK_Upcast, Loc, Value.getPointer(),
DerivedTy, DerivedAlign, SkippedChecks);
}
return Builder.CreateElementBitCast(Value, BaseValueTy);
return Value.withElementType(BaseValueTy);
}

llvm::BasicBlock *origBB = nullptr;
Expand Down Expand Up @@ -379,7 +377,7 @@ Address CodeGenFunction::GetAddressOfBaseClass(
VirtualOffset, Derived, VBase);

// Cast to the destination type.
Value = Builder.CreateElementBitCast(Value, BaseValueTy);
Value = Value.withElementType(BaseValueTy);

// Build a phi if we needed a null check.
if (NullCheckValue) {
Expand Down Expand Up @@ -416,7 +414,7 @@ CodeGenFunction::GetAddressOfDerivedClass(Address BaseAddr,

if (!NonVirtualOffset) {
// No offset, we can just cast back.
return Builder.CreateElementBitCast(BaseAddr, DerivedValueTy);
return BaseAddr.withElementType(DerivedValueTy);
}

llvm::BasicBlock *CastNull = nullptr;
Expand Down Expand Up @@ -997,8 +995,8 @@ namespace {

private:
void emitMemcpyIR(Address DestPtr, Address SrcPtr, CharUnits Size) {
DestPtr = CGF.Builder.CreateElementBitCast(DestPtr, CGF.Int8Ty);
SrcPtr = CGF.Builder.CreateElementBitCast(SrcPtr, CGF.Int8Ty);
DestPtr = DestPtr.withElementType(CGF.Int8Ty);
SrcPtr = SrcPtr.withElementType(CGF.Int8Ty);
CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity());
}

Expand Down Expand Up @@ -2581,7 +2579,7 @@ void CodeGenFunction::InitializeVTablePointer(const VPtr &Vptr) {
llvm::Type *PtrTy = llvm::PointerType::get(CGM.getLLVMContext(), GlobalsAS);
// vtable field is derived from `this` pointer, therefore they should be in
// the same addr space. Note that this might not be LLVM address space 0.
VTableField = Builder.CreateElementBitCast(VTableField, PtrTy);
VTableField = VTableField.withElementType(PtrTy);

llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField);
TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(PtrTy);
Expand Down Expand Up @@ -2677,7 +2675,7 @@ void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) {
llvm::Value *CodeGenFunction::GetVTablePtr(Address This,
llvm::Type *VTableTy,
const CXXRecordDecl *RD) {
Address VTablePtrSrc = Builder.CreateElementBitCast(This, VTableTy);
Address VTablePtrSrc = This.withElementType(VTableTy);
llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable");
TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTableTy);
CGM.DecorateInstructionWithTBAA(VTable, TBAAInfo);
Expand Down
19 changes: 8 additions & 11 deletions clang/lib/CodeGen/CGExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
// destination but can have a different type. Just do a bitcast in this
// case to avoid incorrect GEPs.
if (Result->getType() != StoreDest.getType())
StoreDest =
CGF.Builder.CreateElementBitCast(StoreDest, Result->getType());
StoreDest = StoreDest.withElementType(Result->getType());

CGF.EmitAggregateStore(Result, StoreDest,
E->getType().isVolatileQualified());
return;
Expand Down Expand Up @@ -751,8 +751,7 @@ void AggExprEmitter::VisitCastExpr(CastExpr *E) {

// GCC union extension
QualType Ty = E->getSubExpr()->getType();
Address CastPtr =
Builder.CreateElementBitCast(Dest.getAddress(), CGF.ConvertType(Ty));
Address CastPtr = Dest.getAddress().withElementType(CGF.ConvertType(Ty));
EmitInitializationToLValue(E->getSubExpr(),
CGF.MakeAddrLValue(CastPtr, Ty));
break;
Expand All @@ -767,9 +766,8 @@ void AggExprEmitter::VisitCastExpr(CastExpr *E) {

LValue SourceLV = CGF.EmitLValue(E->getSubExpr());
Address SourceAddress =
Builder.CreateElementBitCast(SourceLV.getAddress(CGF), CGF.Int8Ty);
Address DestAddress =
Builder.CreateElementBitCast(Dest.getAddress(), CGF.Int8Ty);
SourceLV.getAddress(CGF).withElementType(CGF.Int8Ty);
Address DestAddress = Dest.getAddress().withElementType(CGF.Int8Ty);
llvm::Value *SizeVal = llvm::ConstantInt::get(
CGF.SizeTy,
CGF.getContext().getTypeSizeInChars(E->getType()).getQuantity());
Expand Down Expand Up @@ -2024,8 +2022,7 @@ static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E,
// Okay, it seems like a good idea to use an initial memset, emit the call.
llvm::Constant *SizeVal = CGF.Builder.getInt64(Size.getQuantity());

Address Loc = Slot.getAddress();
Loc = CGF.Builder.CreateElementBitCast(Loc, CGF.Int8Ty);
Address Loc = Slot.getAddress().withElementType(CGF.Int8Ty);
CGF.Builder.CreateMemSet(Loc, CGF.Builder.getInt8(0), SizeVal, false);

// Tell the AggExprEmitter that the slot is known zero.
Expand Down Expand Up @@ -2189,8 +2186,8 @@ void CodeGenFunction::EmitAggregateCopy(LValue Dest, LValue Src, QualType Ty,
// we need to use a different call here. We use isVolatile to indicate when
// either the source or the destination is volatile.

DestPtr = Builder.CreateElementBitCast(DestPtr, Int8Ty);
SrcPtr = Builder.CreateElementBitCast(SrcPtr, Int8Ty);
DestPtr = DestPtr.withElementType(Int8Ty);
SrcPtr = SrcPtr.withElementType(Int8Ty);

// Don't do any of the memmove_collectable tests if GC isn't set.
if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Expand Down
7 changes: 3 additions & 4 deletions clang/lib/CodeGen/CGExprComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,14 @@ ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,

case CK_LValueBitCast: {
LValue origLV = CGF.EmitLValue(Op);
Address V = origLV.getAddress(CGF);
V = Builder.CreateElementBitCast(V, CGF.ConvertType(DestTy));
Address V = origLV.getAddress(CGF).withElementType(CGF.ConvertType(DestTy));
return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), Op->getExprLoc());
}

case CK_LValueToRValueBitCast: {
LValue SourceLVal = CGF.EmitLValue(Op);
Address Addr = Builder.CreateElementBitCast(SourceLVal.getAddress(CGF),
CGF.ConvertTypeForMem(DestTy));
Address Addr = SourceLVal.getAddress(CGF).withElementType(
CGF.ConvertTypeForMem(DestTy));
LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy);
DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo());
return EmitLoadOfLValue(DestLV, Op->getExprLoc());
Expand Down
32 changes: 13 additions & 19 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,7 @@ static void EmitOMPAggregateInit(CodeGenFunction &CGF, Address DestAddr,
const ArrayType *ArrayTy = Type->getAsArrayTypeUnsafe();
llvm::Value *NumElements = CGF.emitArrayLength(ArrayTy, ElementTy, DestAddr);
if (DRD)
SrcAddr =
CGF.Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
SrcAddr = SrcAddr.withElementType(DestAddr.getElementType());

llvm::Value *SrcBegin = nullptr;
if (DRD)
Expand Down Expand Up @@ -906,8 +905,8 @@ void ReductionCodeGen::emitCleanups(CodeGenFunction &CGF, unsigned N,
QualType PrivateType = getPrivateType(N);
QualType::DestructionKind DTorKind = PrivateType.isDestructedType();
if (needCleanups(N)) {
PrivateAddr = CGF.Builder.CreateElementBitCast(
PrivateAddr, CGF.ConvertTypeForMem(PrivateType));
PrivateAddr =
PrivateAddr.withElementType(CGF.ConvertTypeForMem(PrivateType));
CGF.pushDestroy(DTorKind, PrivateAddr, PrivateType);
}
}
Expand All @@ -926,8 +925,7 @@ static LValue loadToBegin(CodeGenFunction &CGF, QualType BaseTy, QualType ElTy,
BaseTy = BaseTy->getPointeeType();
}
return CGF.MakeAddrLValue(
CGF.Builder.CreateElementBitCast(BaseLV.getAddress(CGF),
CGF.ConvertTypeForMem(ElTy)),
BaseLV.getAddress(CGF).withElementType(CGF.ConvertTypeForMem(ElTy)),
BaseLV.getType(), BaseLV.getBaseInfo(),
CGF.CGM.getTBAAInfoForSubobject(BaseLV, BaseLV.getType()));
}
Expand Down Expand Up @@ -1777,9 +1775,8 @@ llvm::Function *CGOpenMPRuntime::emitThreadPrivateVarDefinition(
llvm::Value *ArgVal = CtorCGF.EmitLoadOfScalar(
CtorCGF.GetAddrOfLocalVar(&Dst), /*Volatile=*/false,
CGM.getContext().VoidPtrTy, Dst.getLocation());
Address Arg(ArgVal, CtorCGF.Int8Ty, VDAddr.getAlignment());
Arg = CtorCGF.Builder.CreateElementBitCast(
Arg, CtorCGF.ConvertTypeForMem(ASTTy));
Address Arg(ArgVal, CtorCGF.ConvertTypeForMem(ASTTy),
VDAddr.getAlignment());
CtorCGF.EmitAnyExprToMem(Init, Arg, Init->getType().getQualifiers(),
/*IsInitializer=*/true);
ArgVal = CtorCGF.EmitLoadOfScalar(
Expand Down Expand Up @@ -4244,8 +4241,7 @@ CGOpenMPRuntime::getDepobjElements(CodeGenFunction &CGF, LValue DepobjLVal,
cast<RecordDecl>(KmpDependInfoTy->getAsTagDecl());
QualType KmpDependInfoPtrTy = C.getPointerType(KmpDependInfoTy);
LValue Base = CGF.EmitLoadOfPointerLValue(
CGF.Builder.CreateElementBitCast(
DepobjLVal.getAddress(CGF),
DepobjLVal.getAddress(CGF).withElementType(
CGF.ConvertTypeForMem(KmpDependInfoPtrTy)),
KmpDependInfoPtrTy->castAs<PointerType>());
Address DepObjAddr = CGF.Builder.CreateGEP(
Expand Down Expand Up @@ -5471,8 +5467,7 @@ static llvm::Value *emitReduceInitFunction(CodeGenModule &CGM,
CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, FnInfo, Args, Loc, Loc);
QualType PrivateType = RCG.getPrivateType(N);
Address PrivateAddr = CGF.EmitLoadOfPointer(
CGF.Builder.CreateElementBitCast(
CGF.GetAddrOfLocalVar(&Param),
CGF.GetAddrOfLocalVar(&Param).withElementType(
CGF.ConvertTypeForMem(PrivateType)->getPointerTo()),
C.getPointerType(PrivateType)->castAs<PointerType>());
llvm::Value *Size = nullptr;
Expand Down Expand Up @@ -5560,17 +5555,16 @@ static llvm::Value *emitReduceCombFunction(CodeGenModule &CGM,
LHSVD,
// Pull out the pointer to the variable.
CGF.EmitLoadOfPointer(
CGF.Builder.CreateElementBitCast(
CGF.GetAddrOfLocalVar(&ParamInOut),
CGF.ConvertTypeForMem(LHSVD->getType())->getPointerTo()),
CGF.GetAddrOfLocalVar(&ParamInOut)
.withElementType(
CGF.ConvertTypeForMem(LHSVD->getType())->getPointerTo()),
C.getPointerType(LHSVD->getType())->castAs<PointerType>()));
PrivateScope.addPrivate(
RHSVD,
// Pull out the pointer to the variable.
CGF.EmitLoadOfPointer(
CGF.Builder.CreateElementBitCast(
CGF.GetAddrOfLocalVar(&ParamIn),
CGF.ConvertTypeForMem(RHSVD->getType())->getPointerTo()),
CGF.GetAddrOfLocalVar(&ParamIn).withElementType(
CGF.ConvertTypeForMem(RHSVD->getType())->getPointerTo()),
C.getPointerType(RHSVD->getType())->castAs<PointerType>()));
PrivateScope.Privatize();
// Emit the combiner body:
Expand Down
42 changes: 18 additions & 24 deletions clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1554,10 +1554,9 @@ static void emitReductionListCopy(
case RemoteLaneToThread: {
// Step 1.1: Get the address for the src element in the Reduce list.
Address SrcElementPtrAddr = Bld.CreateConstArrayGEP(SrcBase, Idx);
SrcElementAddr =
CGF.EmitLoadOfPointer(CGF.Builder.CreateElementBitCast(
SrcElementPtrAddr, PrivateLlvmPtrType),
PrivatePtrType->castAs<PointerType>());
SrcElementAddr = CGF.EmitLoadOfPointer(
SrcElementPtrAddr.withElementType(PrivateLlvmPtrType),
PrivatePtrType->castAs<PointerType>());

// Step 1.2: Create a temporary to store the element in the destination
// Reduce list.
Expand All @@ -1571,27 +1570,24 @@ static void emitReductionListCopy(
case ThreadCopy: {
// Step 1.1: Get the address for the src element in the Reduce list.
Address SrcElementPtrAddr = Bld.CreateConstArrayGEP(SrcBase, Idx);
SrcElementAddr =
CGF.EmitLoadOfPointer(CGF.Builder.CreateElementBitCast(
SrcElementPtrAddr, PrivateLlvmPtrType),
PrivatePtrType->castAs<PointerType>());
SrcElementAddr = CGF.EmitLoadOfPointer(
SrcElementPtrAddr.withElementType(PrivateLlvmPtrType),
PrivatePtrType->castAs<PointerType>());

// Step 1.2: Get the address for dest element. The destination
// element has already been created on the thread's stack.
DestElementPtrAddr = Bld.CreateConstArrayGEP(DestBase, Idx);
DestElementAddr =
CGF.EmitLoadOfPointer(CGF.Builder.CreateElementBitCast(
DestElementPtrAddr, PrivateLlvmPtrType),
PrivatePtrType->castAs<PointerType>());
DestElementAddr = CGF.EmitLoadOfPointer(
DestElementPtrAddr.withElementType(PrivateLlvmPtrType),
PrivatePtrType->castAs<PointerType>());
break;
}
case ThreadToScratchpad: {
// Step 1.1: Get the address for the src element in the Reduce list.
Address SrcElementPtrAddr = Bld.CreateConstArrayGEP(SrcBase, Idx);
SrcElementAddr =
CGF.EmitLoadOfPointer(CGF.Builder.CreateElementBitCast(
SrcElementPtrAddr, PrivateLlvmPtrType),
PrivatePtrType->castAs<PointerType>());
SrcElementAddr = CGF.EmitLoadOfPointer(
SrcElementPtrAddr.withElementType(PrivateLlvmPtrType),
PrivatePtrType->castAs<PointerType>());

// Step 1.2: Get the address for dest element:
// address = base + index * ElementSizeInChars.
Expand Down Expand Up @@ -1633,10 +1629,10 @@ static void emitReductionListCopy(

// Regardless of src and dest of copy, we emit the load of src
// element as this is required in all directions
SrcElementAddr = Bld.CreateElementBitCast(
SrcElementAddr, CGF.ConvertTypeForMem(Private->getType()));
DestElementAddr = Bld.CreateElementBitCast(DestElementAddr,
SrcElementAddr.getElementType());
SrcElementAddr = SrcElementAddr.withElementType(
CGF.ConvertTypeForMem(Private->getType()));
DestElementAddr =
DestElementAddr.withElementType(SrcElementAddr.getElementType());

// Now that all active lanes have read the element in the
// Reduce list, shuffle over the value from the remote lane.
Expand Down Expand Up @@ -1865,8 +1861,7 @@ static llvm::Value *emitInterWarpCopyFunction(CodeGenModule &CGM,
llvm::Value *ElemPtrPtr = CGF.EmitLoadOfScalar(
ElemPtrPtrAddr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
// elemptr = ((CopyType*)(elemptrptr)) + I
Address ElemPtr(ElemPtrPtr, CGF.Int8Ty, Align);
ElemPtr = Bld.CreateElementBitCast(ElemPtr, CopyType);
Address ElemPtr(ElemPtrPtr, CopyType, Align);
if (NumIters > 1)
ElemPtr = Bld.CreateGEP(ElemPtr, Cnt);

Expand Down Expand Up @@ -1940,8 +1935,7 @@ static llvm::Value *emitInterWarpCopyFunction(CodeGenModule &CGM,
Address TargetElemPtrPtr = Bld.CreateConstArrayGEP(LocalReduceList, Idx);
llvm::Value *TargetElemPtrVal = CGF.EmitLoadOfScalar(
TargetElemPtrPtr, /*Volatile=*/false, C.VoidPtrTy, Loc);
Address TargetElemPtr(TargetElemPtrVal, CGF.Int8Ty, Align);
TargetElemPtr = Bld.CreateElementBitCast(TargetElemPtr, CopyType);
Address TargetElemPtr(TargetElemPtrVal, CopyType, Align);
if (NumIters > 1)
TargetElemPtr = Bld.CreateGEP(TargetElemPtr, Cnt);

Expand Down
10 changes: 5 additions & 5 deletions clang/lib/CodeGen/CGStmtOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ void CodeGenFunction::EmitOMPAggregateAssign(
// Drill down to the base element type on both arrays.
const ArrayType *ArrayTy = OriginalType->getAsArrayTypeUnsafe();
llvm::Value *NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr);
SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType());
SrcAddr = SrcAddr.withElementType(DestAddr.getElementType());

llvm::Value *SrcBegin = SrcAddr.getPointer();
llvm::Value *DestBegin = DestAddr.getPointer();
Expand Down Expand Up @@ -7192,7 +7192,7 @@ void CodeGenFunction::EmitOMPUseDevicePtrClause(
// privatization scope, so the initialization here disregards the fact
// the original variable is a reference.
llvm::Type *Ty = ConvertTypeForMem(OrigVD->getType().getNonReferenceType());
Address InitAddr = Builder.CreateElementBitCast(InitAddrIt->second, Ty);
Address InitAddr = InitAddrIt->second.withElementType(Ty);
setAddrOfLocalVar(InitVD, InitAddr);

// Emit private declaration, it will be initialized by the value we
Expand Down Expand Up @@ -7258,9 +7258,9 @@ void CodeGenFunction::EmitOMPUseDeviceAddrClause(
MatchingVD->getType()->isArrayType()) {
QualType PtrTy = getContext().getPointerType(
OrigVD->getType().getNonReferenceType());
PrivAddr = EmitLoadOfPointer(
Builder.CreateElementBitCast(PrivAddr, ConvertTypeForMem(PtrTy)),
PtrTy->castAs<PointerType>());
PrivAddr =
EmitLoadOfPointer(PrivAddr.withElementType(ConvertTypeForMem(PtrTy)),
PtrTy->castAs<PointerType>());
}

(void)PrivateScope.addPrivate(OrigVD, PrivAddr);
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/Targets/XCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
break;
case ABIArgInfo::Extend:
case ABIArgInfo::Direct:
Val = Builder.CreateElementBitCast(AP, ArgTy);
Val = AP.withElementType(ArgTy);
ArgSize = CharUnits::fromQuantity(
getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
ArgSize = ArgSize.alignTo(SlotSize);
break;
case ABIArgInfo::Indirect:
case ABIArgInfo::IndirectAliased:
Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
Val = AP.withElementType(ArgPtrTy);
Val = Address(Builder.CreateLoad(Val), ArgTy, TypeAlign);
ArgSize = SlotSize;
break;
Expand Down

0 comments on commit 5f32baf

Please sign in to comment.