diff --git a/clang/lib/CodeGen/Address.h b/clang/lib/CodeGen/Address.h index 899aceac88672..cf48df8f5e736 100644 --- a/clang/lib/CodeGen/Address.h +++ b/clang/lib/CodeGen/Address.h @@ -95,6 +95,12 @@ class Address { isKnownNonNull()); } + /// Return address with different element type, but same pointer and + /// alignment. + Address withElementType(llvm::Type *ElemTy) const { + return Address(getPointer(), ElemTy, getAlignment(), isKnownNonNull()); + } + /// Whether the pointer is known not to be null. KnownNonNull_t isKnownNonNull() const { assert(isValid()); diff --git a/clang/lib/CodeGen/CGBlocks.cpp b/clang/lib/CodeGen/CGBlocks.cpp index 100144cabbf48..cfbe3272196e3 100644 --- a/clang/lib/CodeGen/CGBlocks.cpp +++ b/clang/lib/CodeGen/CGBlocks.cpp @@ -1259,9 +1259,8 @@ Address CodeGenFunction::GetAddrOfBlockDecl(const VarDecl *variable) { // to byref*. auto &byrefInfo = getBlockByrefInfo(variable); - addr = Address(Builder.CreateLoad(addr), Int8Ty, byrefInfo.ByrefAlignment); - - addr = Builder.CreateElementBitCast(addr, byrefInfo.Type, "byref.addr"); + addr = Address(Builder.CreateLoad(addr), byrefInfo.Type, + byrefInfo.ByrefAlignment); addr = emitBlockByrefAddress(addr, byrefInfo, /*follow*/ true, variable->getName()); @@ -1935,14 +1934,12 @@ CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) { auto AL = ApplyDebugLocation::CreateArtificial(*this); Address src = GetAddrOfLocalVar(&SrcDecl); - src = Address(Builder.CreateLoad(src), Int8Ty, blockInfo.BlockAlign); - src = Builder.CreateElementBitCast(src, blockInfo.StructureType, - "block.source"); + src = Address(Builder.CreateLoad(src), blockInfo.StructureType, + blockInfo.BlockAlign); Address dst = GetAddrOfLocalVar(&DstDecl); - dst = Address(Builder.CreateLoad(dst), Int8Ty, blockInfo.BlockAlign); - dst = - Builder.CreateElementBitCast(dst, blockInfo.StructureType, "block.dest"); + dst = Address(Builder.CreateLoad(dst), blockInfo.StructureType, + blockInfo.BlockAlign); for (auto &capture : blockInfo.SortedCaptures) { if (capture.isConstantOrTrivial()) @@ -2125,8 +2122,8 @@ CodeGenFunction::GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo) { auto AL = ApplyDebugLocation::CreateArtificial(*this); Address src = GetAddrOfLocalVar(&SrcDecl); - src = Address(Builder.CreateLoad(src), Int8Ty, blockInfo.BlockAlign); - src = Builder.CreateElementBitCast(src, blockInfo.StructureType, "block"); + src = Address(Builder.CreateLoad(src), blockInfo.StructureType, + blockInfo.BlockAlign); CodeGenFunction::RunCleanupsScope cleanups(*this); @@ -2163,9 +2160,9 @@ class ObjectByrefHelpers final : public BlockByrefHelpers { void emitCopy(CodeGenFunction &CGF, Address destField, Address srcField) override { - destField = CGF.Builder.CreateElementBitCast(destField, CGF.Int8Ty); + destField = destField.withElementType(CGF.Int8Ty); - srcField = CGF.Builder.CreateElementBitCast(srcField, CGF.Int8PtrTy); + srcField = srcField.withElementType(CGF.Int8PtrTy); llvm::Value *srcValue = CGF.Builder.CreateLoad(srcField); unsigned flags = (Flags | BLOCK_BYREF_CALLER).getBitMask(); @@ -2178,7 +2175,7 @@ class ObjectByrefHelpers final : public BlockByrefHelpers { } void emitDispose(CodeGenFunction &CGF, Address field) override { - field = CGF.Builder.CreateElementBitCast(field, CGF.Int8PtrTy); + field = field.withElementType(CGF.Int8PtrTy); llvm::Value *value = CGF.Builder.CreateLoad(field); CGF.BuildBlockRelease(value, Flags | BLOCK_BYREF_CALLER, false); @@ -2370,17 +2367,15 @@ generateByrefCopyHelper(CodeGenFunction &CGF, const BlockByrefInfo &byrefInfo, if (generator.needsCopy()) { // dst->x Address destField = CGF.GetAddrOfLocalVar(&Dst); - destField = Address(CGF.Builder.CreateLoad(destField), CGF.Int8Ty, + destField = Address(CGF.Builder.CreateLoad(destField), byrefInfo.Type, byrefInfo.ByrefAlignment); - destField = CGF.Builder.CreateElementBitCast(destField, byrefInfo.Type); destField = CGF.emitBlockByrefAddress(destField, byrefInfo, false, "dest-object"); // src->x Address srcField = CGF.GetAddrOfLocalVar(&Src); - srcField = Address(CGF.Builder.CreateLoad(srcField), CGF.Int8Ty, + srcField = Address(CGF.Builder.CreateLoad(srcField), byrefInfo.Type, byrefInfo.ByrefAlignment); - srcField = CGF.Builder.CreateElementBitCast(srcField, byrefInfo.Type); srcField = CGF.emitBlockByrefAddress(srcField, byrefInfo, false, "src-object"); @@ -2436,9 +2431,8 @@ generateByrefDisposeHelper(CodeGenFunction &CGF, if (generator.needsDispose()) { Address addr = CGF.GetAddrOfLocalVar(&Src); - addr = Address(CGF.Builder.CreateLoad(addr), CGF.Int8Ty, + addr = Address(CGF.Builder.CreateLoad(addr), byrefInfo.Type, byrefInfo.ByrefAlignment); - addr = CGF.Builder.CreateElementBitCast(addr, byrefInfo.Type); addr = CGF.emitBlockByrefAddress(addr, byrefInfo, false, "object"); generator.emitDispose(CGF, addr); diff --git a/clang/lib/CodeGen/CGBuilder.h b/clang/lib/CodeGen/CGBuilder.h index 13049beec932b..92dd38868ace8 100644 --- a/clang/lib/CodeGen/CGBuilder.h +++ b/clang/lib/CodeGen/CGBuilder.h @@ -155,10 +155,8 @@ class CGBuilderTy : public CGBuilderBaseTy { Addr.isKnownNonNull()); } - /// Cast the element type of the given address to a different type, - /// preserving information like the alignment and address space. - Address CreateElementBitCast(Address Addr, llvm::Type *Ty, - const llvm::Twine &Name = "") { + /// This method is to be deprecated. Use `Address::withElementType` instead. + Address CreateElementBitCast(Address Addr, llvm::Type *Ty) { return Address(Addr.getPointer(), Ty, Addr.getAlignment(), Addr.isKnownNonNull()); } diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index b87ca22cb13f2..15671b48033e5 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -1858,8 +1858,7 @@ llvm::Function *CodeGenFunction::generateBuiltinOSLogHelperFunction( Address Arg = GetAddrOfLocalVar(Args[I]); Address Addr = Builder.CreateConstByteGEP(BufAddr, Offset, "argData"); - Addr = - Builder.CreateElementBitCast(Addr, Arg.getElementType(), "argDataCast"); + Addr = Addr.withElementType(Arg.getElementType()); Builder.CreateStore(Builder.CreateLoad(Arg), Addr); Offset += Size; ++I; diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp index 48110dd56830f..fc16fb370dbaa 100644 --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -727,8 +727,8 @@ static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF, // Handle a formal type change to avoid asserting. auto srcAddr = srcLV.getAddress(CGF); if (needsCast) { - srcAddr = CGF.Builder.CreateElementBitCast( - srcAddr, destLV.getAddress(CGF).getElementType()); + srcAddr = + srcAddr.withElementType(destLV.getAddress(CGF).getElementType()); } // If it was an l-value, use objc_copyWeak. @@ -1173,7 +1173,7 @@ static Address createUnnamedGlobalForMemcpyFrom(CodeGenModule &CGM, llvm::Constant *Constant, CharUnits Align) { Address SrcPtr = CGM.createUnnamedGlobalFrom(D, Constant, Align); - return Builder.CreateElementBitCast(SrcPtr, CGM.Int8Ty); + return SrcPtr.withElementType(CGM.Int8Ty); } static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D, @@ -1207,7 +1207,7 @@ static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D, bool valueAlreadyCorrect = constant->isNullValue() || isa(constant); if (!valueAlreadyCorrect) { - Loc = Builder.CreateElementBitCast(Loc, Ty); + Loc = Loc.withElementType(Ty); emitStoresForInitAfterBZero(CGM, constant, Loc, isVolatile, Builder, IsAutoInit); } @@ -1790,7 +1790,7 @@ void CodeGenFunction::emitZeroOrPatternForAutoVarInit(QualType type, SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize)); llvm::Value *BaseSizeInChars = llvm::ConstantInt::get(IntPtrTy, EltSize.getQuantity()); - Address Begin = Builder.CreateElementBitCast(Loc, Int8Ty, "vla.begin"); + Address Begin = Loc.withElementType(Int8Ty); llvm::Value *End = Builder.CreateInBoundsGEP( Begin.getElementType(), Begin.getPointer(), SizeVal, "vla.end"); llvm::BasicBlock *OriginBB = Builder.GetInsertBlock(); @@ -1921,7 +1921,7 @@ void CodeGenFunction::EmitAutoVarInit(const AutoVarEmission &emission) { return EmitStoreThroughLValue(RValue::get(constant), lv, true); } - emitStoresForConstant(CGM, D, Builder.CreateElementBitCast(Loc, CGM.Int8Ty), + emitStoresForConstant(CGM, D, Loc.withElementType(CGM.Int8Ty), type.isVolatileQualified(), Builder, constant, /*IsAutoInit=*/false); } @@ -2482,10 +2482,8 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg, // If we already have a pointer to the argument, reuse the input pointer. if (Arg.isIndirect()) { - // If we have a prettier pointer type at this point, bitcast to that. DeclPtr = Arg.getIndirectAddress(); - DeclPtr = Builder.CreateElementBitCast(DeclPtr, ConvertTypeForMem(Ty), - D.getName()); + DeclPtr = DeclPtr.withElementType(ConvertTypeForMem(Ty)); // Indirect argument is in alloca address space, which may be different // from the default address space. auto AllocaAS = CGM.getASTAllocaAddressSpace(); diff --git a/clang/lib/CodeGen/CGException.cpp b/clang/lib/CodeGen/CGException.cpp index 603bd47be1694..9cb7d4c7731de 100644 --- a/clang/lib/CodeGen/CGException.cpp +++ b/clang/lib/CodeGen/CGException.cpp @@ -401,7 +401,7 @@ void CodeGenFunction::EmitAnyExprToExn(const Expr *e, Address addr) { // __cxa_allocate_exception returns a void*; we need to cast this // to the appropriate type for the object. llvm::Type *ty = ConvertTypeForMem(e->getType()); - Address typedAddr = Builder.CreateElementBitCast(addr, ty); + Address typedAddr = addr.withElementType(ty); // FIXME: this isn't quite right! If there's a final unelided call // to a copy constructor, then according to [except.terminate]p1 we diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 2afdc6abb104a..93f1e7f5712c3 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -1755,10 +1755,9 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile, if (!CGM.getCodeGenOpts().PreserveVec3Type && VTy->getNumElements() == 3) { - // Bitcast to vec4 type. llvm::VectorType *vec4Ty = llvm::FixedVectorType::get(VTy->getElementType(), 4); - Address Cast = Builder.CreateElementBitCast(Addr, vec4Ty, "castToVec4"); + Address Cast = Addr.withElementType(vec4Ty); // Now load value. llvm::Value *V = Builder.CreateLoad(Cast, Volatile, "loadVec4"); @@ -1898,7 +1897,7 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr, SrcTy = llvm::FixedVectorType::get(VecTy->getElementType(), 4); } if (Addr.getElementType() != SrcTy) { - Addr = Builder.CreateElementBitCast(Addr, SrcTy, "storetmp"); + Addr = Addr.withElementType(SrcTy); } } } @@ -2080,9 +2079,7 @@ Address CodeGenFunction::EmitExtVectorElementLValue(LValue LV) { QualType EQT = LV.getType()->castAs()->getElementType(); llvm::Type *VectorElementTy = CGM.getTypes().ConvertType(EQT); - Address CastToPointerElement = - Builder.CreateElementBitCast(VectorAddress, VectorElementTy, - "conv.ptr.element"); + Address CastToPointerElement = VectorAddress.withElementType(VectorElementTy); const llvm::Constant *Elts = LV.getExtVectorElts(); unsigned ix = getAccessedFieldNo(0, Elts); @@ -4501,8 +4498,7 @@ LValue CodeGenFunction::EmitLValueForField(LValue base, } if (FieldType->isReferenceType()) - addr = Builder.CreateElementBitCast( - addr, CGM.getTypes().ConvertTypeForMem(FieldType), field->getName()); + addr = addr.withElementType(CGM.getTypes().ConvertTypeForMem(FieldType)); } else { if (!IsInPreservedAIRegion && (!getDebugInfo() || !rec->hasAttr())) @@ -4527,11 +4523,8 @@ LValue CodeGenFunction::EmitLValueForField(LValue base, } // Make sure that the address is pointing to the right type. This is critical - // for both unions and structs. A union needs a bitcast, a struct element - // will need a bitcast if the LLVM type laid out doesn't match the desired - // type. - addr = Builder.CreateElementBitCast( - addr, CGM.getTypes().ConvertTypeForMem(FieldType), field->getName()); + // for both unions and structs. + addr = addr.withElementType(CGM.getTypes().ConvertTypeForMem(FieldType)); if (field->hasAttr()) addr = EmitFieldAnnotations(field, addr); @@ -4558,7 +4551,7 @@ CodeGenFunction::EmitLValueForFieldInitialization(LValue Base, // Make sure that the address is pointing to the right type. llvm::Type *llvmType = ConvertTypeForMem(FieldType); - V = Builder.CreateElementBitCast(V, llvmType, Field->getName()); + V = V.withElementType(llvmType); // TODO: Generate TBAA information that describes this access as a structure // member access and not just an access to an object of the field's type. This diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 48643106416bb..02b80f3aba21c 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -2046,15 +2046,15 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { case CK_LValueBitCast: case CK_ObjCObjectLValueCast: { Address Addr = EmitLValue(E).getAddress(CGF); - Addr = Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(DestTy)); + Addr = Addr.withElementType(CGF.ConvertTypeForMem(DestTy)); LValue LV = CGF.MakeAddrLValue(Addr, DestTy); return EmitLoadOfLValue(LV, CE->getExprLoc()); } case CK_LValueToRValueBitCast: { LValue SourceLVal = CGF.EmitLValue(E); - 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, CE->getExprLoc()); @@ -2177,8 +2177,7 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { Address Addr = CGF.CreateDefaultAlignTempAlloca(SrcTy, "saved-value"); LValue LV = CGF.MakeAddrLValue(Addr, E->getType()); CGF.EmitStoreOfScalar(Src, LV); - Addr = Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(DestTy), - "castFixedSve"); + Addr = Addr.withElementType(CGF.ConvertTypeForMem(DestTy)); LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy); DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo()); return EmitLoadOfLValue(DestLV, CE->getExprLoc()); @@ -5144,7 +5143,7 @@ LValue CodeGenFunction::EmitObjCIsaExpr(const ObjCIsaExpr *E) { } // Cast the address to Class*. - Addr = Builder.CreateElementBitCast(Addr, ConvertType(E->getType())); + Addr = Addr.withElementType(ConvertType(E->getType())); return MakeAddrLValue(Addr, E->getType()); } diff --git a/clang/lib/CodeGen/CGObjC.cpp b/clang/lib/CodeGen/CGObjC.cpp index c8f0070192dd6..46c37eaea82b1 100644 --- a/clang/lib/CodeGen/CGObjC.cpp +++ b/clang/lib/CodeGen/CGObjC.cpp @@ -1190,7 +1190,7 @@ CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, // Perform an atomic load. This does not impose ordering constraints. Address ivarAddr = LV.getAddress(*this); - ivarAddr = Builder.CreateElementBitCast(ivarAddr, bitcastType); + ivarAddr = ivarAddr.withElementType(bitcastType); llvm::LoadInst *load = Builder.CreateLoad(ivarAddr, "load"); load->setAtomic(llvm::AtomicOrdering::Unordered); @@ -1204,8 +1204,7 @@ CodeGenFunction::generateObjCGetterBody(const ObjCImplementationDecl *classImpl, bitcastType = llvm::Type::getIntNTy(getLLVMContext(), retTySize); ivarVal = Builder.CreateTrunc(load, bitcastType); } - Builder.CreateStore(ivarVal, - Builder.CreateElementBitCast(ReturnValue, bitcastType)); + Builder.CreateStore(ivarVal, ReturnValue.withElementType(bitcastType)); // Make sure we don't do an autorelease. AutoreleaseResult = false; @@ -1485,15 +1484,13 @@ CodeGenFunction::generateObjCSetterBody(const ObjCImplementationDecl *classImpl, // Currently, all atomic accesses have to be through integer // types, so there's no point in trying to pick a prettier type. - llvm::Type *bitcastType = - llvm::Type::getIntNTy(getLLVMContext(), - getContext().toBits(strategy.getIvarSize())); + llvm::Type *castType = llvm::Type::getIntNTy( + getLLVMContext(), getContext().toBits(strategy.getIvarSize())); // Cast both arguments to the chosen operation type. - argAddr = Builder.CreateElementBitCast(argAddr, bitcastType); - ivarAddr = Builder.CreateElementBitCast(ivarAddr, bitcastType); + argAddr = argAddr.withElementType(castType); + ivarAddr = ivarAddr.withElementType(castType); - // This bitcast load is likely to cause some nasty IR. llvm::Value *load = Builder.CreateLoad(argAddr); // Perform an atomic store. There are no memory ordering requirements. @@ -2205,18 +2202,7 @@ static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, Address addr, if (!fn) fn = getARCIntrinsic(IntID, CGF.CGM); - // Cast the argument to 'id*'. - llvm::Type *origType = addr.getElementType(); - addr = CGF.Builder.CreateElementBitCast(addr, CGF.Int8PtrTy); - - // Call the function. - llvm::Value *result = CGF.EmitNounwindRuntimeCall(fn, addr.getPointer()); - - // Cast the result back to a dereference of the original type. - if (origType != CGF.Int8PtrTy) - result = CGF.Builder.CreateBitCast(result, origType); - - return result; + return CGF.EmitNounwindRuntimeCall(fn, addr.getPointer()); } /// Perform an operation having the following signature: @@ -2661,9 +2647,6 @@ void CodeGenFunction::EmitARCDestroyWeak(Address addr) { if (!fn) fn = getARCIntrinsic(llvm::Intrinsic::objc_destroyWeak, CGM); - // Cast the argument to 'id*'. - addr = Builder.CreateElementBitCast(addr, Int8PtrTy); - EmitNounwindRuntimeCall(fn, addr.getPointer()); } diff --git a/clang/lib/CodeGen/CGObjCMac.cpp b/clang/lib/CodeGen/CGObjCMac.cpp index 7fcfff062fda8..32f4f411347a8 100644 --- a/clang/lib/CodeGen/CGObjCMac.cpp +++ b/clang/lib/CodeGen/CGObjCMac.cpp @@ -5017,11 +5017,8 @@ void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, } void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, - Address DestPtr, - Address SrcPtr, + Address DestPtr, Address SrcPtr, llvm::Value *size) { - SrcPtr = CGF.Builder.CreateElementBitCast(SrcPtr, CGF.Int8Ty); - DestPtr = CGF.Builder.CreateElementBitCast(DestPtr, CGF.Int8Ty); llvm::Value *args[] = { DestPtr.getPointer(), SrcPtr.getPointer(), size }; CGF.EmitNounwindRuntimeCall(ObjCTypes.GcMemmoveCollectableFn(), args); } @@ -7690,12 +7687,8 @@ void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( } void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( - CodeGen::CodeGenFunction &CGF, - Address DestPtr, - Address SrcPtr, - llvm::Value *Size) { - SrcPtr = CGF.Builder.CreateElementBitCast(SrcPtr, CGF.Int8Ty); - DestPtr = CGF.Builder.CreateElementBitCast(DestPtr, CGF.Int8Ty); + CodeGen::CodeGenFunction &CGF, Address DestPtr, Address SrcPtr, + llvm::Value *Size) { llvm::Value *args[] = { DestPtr.getPointer(), SrcPtr.getPointer(), Size }; CGF.EmitNounwindRuntimeCall(ObjCTypes.GcMemmoveCollectableFn(), args); } diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp index dcbcba6aed7f4..bdcf5dc162055 100644 --- a/clang/lib/CodeGen/CGStmt.cpp +++ b/clang/lib/CodeGen/CGStmt.cpp @@ -2193,9 +2193,9 @@ std::pair CodeGenFunction::EmitAsmInputLValue( getTargetHooks().isScalarizableAsmOperand(*this, Ty)) { Ty = llvm::IntegerType::get(getLLVMContext(), Size); - return {Builder.CreateLoad(Builder.CreateElementBitCast( - InputValue.getAddress(*this), Ty)), - nullptr}; + return { + Builder.CreateLoad(InputValue.getAddress(*this).withElementType(Ty)), + nullptr}; } } @@ -2395,8 +2395,7 @@ EmitAsmStores(CodeGenFunction &CGF, const AsmStmt &S, // ResultTypeRequiresCast.size() elements of RegResults. if ((i < ResultTypeRequiresCast.size()) && ResultTypeRequiresCast[i]) { unsigned Size = CGF.getContext().getTypeSize(ResultRegQualTys[i]); - Address A = - Builder.CreateElementBitCast(Dest.getAddress(CGF), ResultRegTypes[i]); + Address A = Dest.getAddress(CGF).withElementType(ResultRegTypes[i]); if (CGF.getTargetHooks().isScalarizableAsmOperand(CGF, TruncTy)) { Builder.CreateStore(Tmp, A); continue; @@ -2576,8 +2575,7 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { // Otherwise there will be a mis-match if the matrix is also an // input-argument which is represented as vector. if (isa(OutExpr->getType().getCanonicalType())) - DestAddr = Builder.CreateElementBitCast( - DestAddr, ConvertType(OutExpr->getType())); + DestAddr = DestAddr.withElementType(ConvertType(OutExpr->getType())); ArgTypes.push_back(DestAddr.getType()); ArgElemTypes.push_back(DestAddr.getElementType()); diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp index cc7ea221ed414..a9e9b986bc60a 100644 --- a/clang/lib/CodeGen/CGStmtOpenMP.cpp +++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp @@ -1266,10 +1266,9 @@ void CodeGenFunction::EmitOMPReductionClauseInit( // implicit variable. PrivateScope.addPrivate(LHSVD, RedCG.getSharedLValue(Count).getAddress(*this)); - PrivateScope.addPrivate(RHSVD, Builder.CreateElementBitCast( - GetAddrOfLocalVar(PrivateVD), - ConvertTypeForMem(RHSVD->getType()), - "rhs.begin")); + PrivateScope.addPrivate(RHSVD, + GetAddrOfLocalVar(PrivateVD).withElementType( + ConvertTypeForMem(RHSVD->getType()))); } else { QualType Type = PrivateVD->getType(); bool IsArray = getContext().getAsArrayType(Type) != nullptr; @@ -1277,14 +1276,13 @@ void CodeGenFunction::EmitOMPReductionClauseInit( // Store the address of the original variable associated with the LHS // implicit variable. if (IsArray) { - OriginalAddr = Builder.CreateElementBitCast( - OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); + OriginalAddr = + OriginalAddr.withElementType(ConvertTypeForMem(LHSVD->getType())); } PrivateScope.addPrivate(LHSVD, OriginalAddr); PrivateScope.addPrivate( - RHSVD, IsArray ? Builder.CreateElementBitCast( - GetAddrOfLocalVar(PrivateVD), - ConvertTypeForMem(RHSVD->getType()), "rhs.begin") + RHSVD, IsArray ? GetAddrOfLocalVar(PrivateVD).withElementType( + ConvertTypeForMem(RHSVD->getType())) : GetAddrOfLocalVar(PrivateVD)); } ++ILHS; diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index f6601638e62a1..b8d39371a9330 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -1935,8 +1935,7 @@ static void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType, llvm::Value *baseSizeInChars = llvm::ConstantInt::get(CGF.IntPtrTy, baseSize.getQuantity()); - Address begin = - Builder.CreateElementBitCast(dest, CGF.Int8Ty, "vla.begin"); + Address begin = dest.withElementType(CGF.Int8Ty); llvm::Value *end = Builder.CreateInBoundsGEP( begin.getElementType(), begin.getPointer(), sizeInChars, "vla.end"); @@ -1980,9 +1979,8 @@ CodeGenFunction::EmitNullInitialization(Address DestPtr, QualType Ty) { } } - // Cast the dest ptr to the appropriate i8 pointer type. if (DestPtr.getElementType() != Int8Ty) - DestPtr = Builder.CreateElementBitCast(DestPtr, Int8Ty); + DestPtr = DestPtr.withElementType(Int8Ty); // Get size and alignment info for this aggregate. CharUnits size = getContext().getTypeSizeInChars(Ty); @@ -2142,7 +2140,7 @@ llvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType, } llvm::Type *baseType = ConvertType(eltType); - addr = Builder.CreateElementBitCast(addr, baseType, "array.begin"); + addr = addr.withElementType(baseType); } else { // Create the actual GEP. addr = Address(Builder.CreateInBoundsGEP( diff --git a/clang/lib/CodeGen/MicrosoftCXXABI.cpp b/clang/lib/CodeGen/MicrosoftCXXABI.cpp index e5a2b35c0b396..19b95e61e2283 100644 --- a/clang/lib/CodeGen/MicrosoftCXXABI.cpp +++ b/clang/lib/CodeGen/MicrosoftCXXABI.cpp @@ -1279,7 +1279,7 @@ void MicrosoftCXXABI::EmitCXXConstructors(const CXXConstructorDecl *D) { void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD) { Address This = getThisAddress(CGF); - This = CGF.Builder.CreateElementBitCast(This, CGM.Int8Ty, "this.int8"); + This = This.withElementType(CGM.Int8Ty); const ASTContext &Context = getContext(); const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); @@ -1296,8 +1296,7 @@ void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF, Address VBPtr = CGF.Builder.CreateConstInBoundsByteGEP(This, Offs); llvm::Value *GVPtr = CGF.Builder.CreateConstInBoundsGEP2_32(GV->getValueType(), GV, 0, 0); - VBPtr = CGF.Builder.CreateElementBitCast(VBPtr, GVPtr->getType(), - "vbptr." + VBT->ObjectWithVPtr->getName()); + VBPtr = VBPtr.withElementType(GVPtr->getType()); CGF.Builder.CreateStore(GVPtr, VBPtr); } } diff --git a/clang/lib/CodeGen/Targets/Hexagon.cpp b/clang/lib/CodeGen/Targets/Hexagon.cpp index 8f098c1ca9177..09a32cca78d68 100644 --- a/clang/lib/CodeGen/Targets/Hexagon.cpp +++ b/clang/lib/CodeGen/Targets/Hexagon.cpp @@ -236,7 +236,7 @@ Address HexagonABIInfo::EmitVAArgForHexagon(CodeGenFunction &CGF, // FIXME: Need to handle alignment llvm::Type *BP = CGF.Int8PtrTy; CGBuilderTy &Builder = CGF.Builder; - Address VAListAddrAsBPP = Builder.CreateElementBitCast(VAListAddr, BP, "ap"); + Address VAListAddrAsBPP = VAListAddr.withElementType(BP); llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); // Handle address alignment for type alignment > 32 bits uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; diff --git a/clang/lib/CodeGen/Targets/Sparc.cpp b/clang/lib/CodeGen/Targets/Sparc.cpp index d0de5f2f3d184..f5cafaa973150 100644 --- a/clang/lib/CodeGen/Targets/Sparc.cpp +++ b/clang/lib/CodeGen/Targets/Sparc.cpp @@ -315,7 +315,7 @@ Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, case ABIArgInfo::Indirect: case ABIArgInfo::IndirectAliased: Stride = SlotSize; - ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); + ArgAddr = Addr.withElementType(ArgPtrTy); ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), ArgTy, TypeInfo.Align); break; @@ -328,7 +328,7 @@ Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next"); Builder.CreateStore(NextPtr.getPointer(), VAListAddr); - return Builder.CreateElementBitCast(ArgAddr, ArgTy, "arg.addr"); + return ArgAddr.withElementType(ArgTy); } void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { diff --git a/clang/lib/CodeGen/Targets/SystemZ.cpp b/clang/lib/CodeGen/Targets/SystemZ.cpp index a4f2ec9cb46c1..6eb0c6ef2f7d6 100644 --- a/clang/lib/CodeGen/Targets/SystemZ.cpp +++ b/clang/lib/CodeGen/Targets/SystemZ.cpp @@ -302,8 +302,7 @@ Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, Address OverflowArgArea = Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), CGF.Int8Ty, TyInfo.Align); - Address MemAddr = - CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); + Address MemAddr = OverflowArgArea.withElementType(DirectTy); // Update overflow_arg_area_ptr pointer llvm::Value *NewOverflowArgArea = CGF.Builder.CreateGEP( @@ -360,8 +359,7 @@ Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, Address RawRegAddr( CGF.Builder.CreateGEP(CGF.Int8Ty, RegSaveArea, RegOffset, "raw_reg_addr"), CGF.Int8Ty, PaddedSize); - Address RegAddr = - CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); + Address RegAddr = RawRegAddr.withElementType(DirectTy); // Update the register count llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); @@ -381,8 +379,7 @@ Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, CGF.Int8Ty, PaddedSize); Address RawMemAddr = CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); - Address MemAddr = - CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); + Address MemAddr = RawMemAddr.withElementType(DirectTy); // Update overflow_arg_area_ptr pointer llvm::Value *NewOverflowArgArea =