diff --git a/clang/lib/CodeGen/CGAtomic.cpp b/clang/lib/CodeGen/CGAtomic.cpp --- a/clang/lib/CodeGen/CGAtomic.cpp +++ b/clang/lib/CodeGen/CGAtomic.cpp @@ -80,23 +80,23 @@ 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( @@ -805,8 +805,7 @@ 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); } } @@ -1103,8 +1102,8 @@ *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; @@ -1137,10 +1136,9 @@ 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); @@ -1302,8 +1300,7 @@ } 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 @@ -1575,10 +1572,8 @@ // 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); @@ -1772,12 +1767,9 @@ // 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); @@ -2080,10 +2072,8 @@ 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); diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -19174,15 +19174,14 @@ 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); @@ -19739,8 +19738,7 @@ } 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()) @@ -19754,8 +19752,7 @@ } 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; @@ -19770,15 +19767,13 @@ } 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)); @@ -19795,8 +19790,7 @@ } 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)); @@ -19814,10 +19808,8 @@ } 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)); diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -55,18 +55,6 @@ // Miscellaneous Helper Methods //===--------------------------------------------------------------------===// -llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) { - unsigned addressSpace = - cast(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, @@ -3367,7 +3355,7 @@ 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()); } diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp --- a/clang/lib/CodeGen/CGExprCXX.cpp +++ b/clang/lib/CodeGen/CGExprCXX.cpp @@ -555,8 +555,7 @@ 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 Store : Stores) { diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -2689,15 +2689,13 @@ } 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 { @@ -2808,7 +2806,6 @@ // Objective-C pointer types. } else { const ObjCObjectPointerType *OPT = type->castAs(); - value = CGF.EmitCastToVoidPtr(value); CharUnits size = CGF.getContext().getTypeSizeInChars(OPT->getObjectType()); if (!isInc) size = -size; @@ -3719,11 +3716,8 @@ // 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()) diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -5680,14 +5680,12 @@ // 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; @@ -5704,21 +5702,19 @@ 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); @@ -7583,8 +7579,7 @@ .getAddress(CGF); } Size = CGF.Builder.CreatePtrDiff( - CGF.Int8Ty, CGF.EmitCastToVoidPtr(ComponentLB.getPointer()), - CGF.EmitCastToVoidPtr(LB.getPointer())); + CGF.Int8Ty, ComponentLB.getPointer(), LB.getPointer()); break; } } @@ -7607,7 +7602,7 @@ 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); diff --git a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp --- a/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp @@ -2372,8 +2372,7 @@ Address GlobAddr = GlobLVal.getAddress(CGF); llvm::Value *BufferPtr = Bld.CreateInBoundsGEP( GlobAddr.getElementType(), GlobAddr.getPointer(), Idxs); - llvm::Value *Ptr = CGF.EmitCastToVoidPtr(BufferPtr); - CGF.EmitStoreOfScalar(Ptr, Elem, /*Volatile=*/false, C.VoidPtrTy); + CGF.EmitStoreOfScalar(BufferPtr, Elem, /*Volatile=*/false, C.VoidPtrTy); if ((*IPriv)->getType()->isVariablyModifiedType()) { // Store array size. ++Idx; @@ -2389,8 +2388,7 @@ } // Call reduce_function(GlobalReduceList, ReduceList) - llvm::Value *GlobalReduceList = - CGF.EmitCastToVoidPtr(ReductionList.getPointer()); + llvm::Value *GlobalReduceList = ReductionList.getPointer(); Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg); llvm::Value *ReducedPtr = CGF.EmitLoadOfScalar( AddrReduceListArg, /*Volatile=*/false, C.VoidPtrTy, Loc); @@ -2582,8 +2580,7 @@ Address GlobAddr = GlobLVal.getAddress(CGF); llvm::Value *BufferPtr = Bld.CreateInBoundsGEP( GlobAddr.getElementType(), GlobAddr.getPointer(), Idxs); - llvm::Value *Ptr = CGF.EmitCastToVoidPtr(BufferPtr); - CGF.EmitStoreOfScalar(Ptr, Elem, /*Volatile=*/false, C.VoidPtrTy); + CGF.EmitStoreOfScalar(BufferPtr, Elem, /*Volatile=*/false, C.VoidPtrTy); if ((*IPriv)->getType()->isVariablyModifiedType()) { // Store array size. ++Idx; @@ -2599,8 +2596,7 @@ } // Call reduce_function(ReduceList, GlobalReduceList) - llvm::Value *GlobalReduceList = - CGF.EmitCastToVoidPtr(ReductionList.getPointer()); + llvm::Value *GlobalReduceList = ReductionList.getPointer(); Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg); llvm::Value *ReducedPtr = CGF.EmitLoadOfScalar( AddrReduceListArg, /*Volatile=*/false, C.VoidPtrTy, Loc); diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -2625,9 +2625,6 @@ AggValueSlot::DoesNotOverlap); } - /// Emit a cast to void* in the appropriate address space. - llvm::Value *EmitCastToVoidPtr(llvm::Value *value); - /// EvaluateExprAsBool - Perform the usual unary conversions on the specified /// expression and compare the result against zero, returning an Int1Ty value. llvm::Value *EvaluateExprAsBool(const Expr *E); diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp --- a/clang/lib/CodeGen/ItaniumCXXABI.cpp +++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp @@ -1433,7 +1433,6 @@ QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { llvm::Type *PtrDiffLTy = CGF.ConvertType(CGF.getContext().getPointerDiffType()); - llvm::Type *DestLTy = CGF.ConvertType(DestTy); llvm::Value *SrcRTTI = CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); @@ -1448,12 +1447,9 @@ computeOffsetHint(CGF.getContext(), SrcDecl, DestDecl).getQuantity()); // Emit the call to __dynamic_cast. - llvm::Value *Value = ThisAddr.getPointer(); - Value = CGF.EmitCastToVoidPtr(Value); - - llvm::Value *args[] = {Value, SrcRTTI, DestRTTI, OffsetHint}; - Value = CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), args); - Value = CGF.Builder.CreateBitCast(Value, DestLTy); + llvm::Value *Args[] = {ThisAddr.getPointer(), SrcRTTI, DestRTTI, OffsetHint}; + llvm::Value *Value = + CGF.EmitNounwindRuntimeCall(getItaniumDynamicCastFn(CGF), Args); /// C++ [expr.dynamic.cast]p9: /// A failed cast to reference type throws std::bad_cast @@ -1475,7 +1471,6 @@ Address ThisAddr, QualType SrcRecordTy, QualType DestTy) { - llvm::Type *DestLTy = CGF.ConvertType(DestTy); auto *ClassDecl = cast(SrcRecordTy->castAs()->getDecl()); llvm::Value *OffsetToTop; @@ -1506,10 +1501,8 @@ PtrDiffLTy, OffsetToTop, CGF.getPointerAlign(), "offset.to.top"); } // Finally, add the offset to the pointer. - llvm::Value *Value = ThisAddr.getPointer(); - Value = CGF.EmitCastToVoidPtr(Value); - Value = CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, Value, OffsetToTop); - return CGF.Builder.CreateBitCast(Value, DestLTy); + return CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, ThisAddr.getPointer(), + OffsetToTop); } bool ItaniumCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { diff --git a/clang/lib/CodeGen/MicrosoftCXXABI.cpp b/clang/lib/CodeGen/MicrosoftCXXABI.cpp --- a/clang/lib/CodeGen/MicrosoftCXXABI.cpp +++ b/clang/lib/CodeGen/MicrosoftCXXABI.cpp @@ -1011,10 +1011,8 @@ } llvm::Value *MicrosoftCXXABI::EmitDynamicCastCall( - CodeGenFunction &CGF, Address This, QualType SrcRecordTy, - QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { - llvm::Type *DestLTy = CGF.ConvertType(DestTy); - + CodeGenFunction &CGF, Address This, QualType SrcRecordTy, QualType DestTy, + QualType DestRecordTy, llvm::BasicBlock *CastEnd) { llvm::Value *SrcRTTI = CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); llvm::Value *DestRTTI = @@ -1040,8 +1038,7 @@ llvm::Value *Args[] = { ThisPtr, Offset, SrcRTTI, DestRTTI, llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())}; - ThisPtr = CGF.EmitRuntimeCallOrInvoke(Function, Args); - return CGF.Builder.CreateBitCast(ThisPtr, DestLTy); + return CGF.EmitRuntimeCallOrInvoke(Function, Args); } llvm::Value * @@ -1568,11 +1565,8 @@ // 1) getThisValue is currently protected // 2) in theory, an ABI could implement 'this' returns some other way; // HasThisReturn only specifies a contract, not the implementation - if (HasThisReturn(CGF.CurGD)) + if (HasThisReturn(CGF.CurGD) || hasMostDerivedReturn(CGF.CurGD)) CGF.Builder.CreateStore(getThisValue(CGF), CGF.ReturnValue); - else if (hasMostDerivedReturn(CGF.CurGD)) - CGF.Builder.CreateStore(CGF.EmitCastToVoidPtr(getThisValue(CGF)), - CGF.ReturnValue); if (isa(MD) && MD->getParent()->getNumVBases()) { assert(getStructorImplicitParamDecl(CGF) &&