diff options
Diffstat (limited to 'clang/lib/CodeGen/CGExpr.cpp')
-rw-r--r-- | clang/lib/CodeGen/CGExpr.cpp | 322 |
1 files changed, 239 insertions, 83 deletions
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 8e0604181fb1..9e8770573d70 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -27,6 +27,7 @@ #include "clang/AST/NSAPI.h" #include "clang/Basic/Builtins.h" #include "clang/Basic/CodeGenOptions.h" +#include "clang/Basic/SourceManager.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/StringExtras.h" #include "llvm/IR/DataLayout.h" @@ -125,8 +126,8 @@ Address CodeGenFunction::CreateDefaultAlignTempAlloca(llvm::Type *Ty, void CodeGenFunction::InitTempAlloca(Address Var, llvm::Value *Init) { assert(isa<llvm::AllocaInst>(Var.getPointer())); - auto *Store = new llvm::StoreInst(Init, Var.getPointer()); - Store->setAlignment(Var.getAlignment().getAsAlign()); + auto *Store = new llvm::StoreInst(Init, Var.getPointer(), /*volatile*/ false, + Var.getAlignment().getAsAlign()); llvm::BasicBlock *Block = AllocaInsertPt->getParent(); Block->getInstList().insertAfter(AllocaInsertPt->getIterator(), Store); } @@ -144,8 +145,19 @@ Address CodeGenFunction::CreateMemTemp(QualType Ty, const Twine &Name, Address CodeGenFunction::CreateMemTemp(QualType Ty, CharUnits Align, const Twine &Name, Address *Alloca) { - return CreateTempAlloca(ConvertTypeForMem(Ty), Align, Name, - /*ArraySize=*/nullptr, Alloca); + Address Result = CreateTempAlloca(ConvertTypeForMem(Ty), Align, Name, + /*ArraySize=*/nullptr, Alloca); + + if (Ty->isConstantMatrixType()) { + auto *ArrayTy = cast<llvm::ArrayType>(Result.getType()->getElementType()); + auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(), + ArrayTy->getNumElements()); + + Result = Address( + Builder.CreateBitCast(Result.getPointer(), VectorTy->getPointerTo()), + Result.getAlignment()); + } + return Result; } Address CodeGenFunction::CreateMemTempWithoutCast(QualType Ty, CharUnits Align, @@ -415,6 +427,11 @@ static Address createReferenceTemporary(CodeGenFunction &CGF, llvm_unreachable("unknown storage duration"); } +/// Helper method to check if the underlying ABI is AAPCS +static bool isAAPCS(const TargetInfo &TargetInfo) { + return TargetInfo.getABI().startswith("aapcs"); +} + LValue CodeGenFunction:: EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) { const Expr *E = M->getSubExpr(); @@ -711,7 +728,7 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, if (SanOpts.has(SanitizerKind::ObjectSize) && !SkippedChecks.has(SanitizerKind::ObjectSize) && !Ty->isIncompleteType()) { - uint64_t TySize = getContext().getTypeSizeInChars(Ty).getQuantity(); + uint64_t TySize = CGM.getMinimumObjectSize(Ty).getQuantity(); llvm::Value *Size = llvm::ConstantInt::get(IntPtrTy, TySize); if (ArraySize) Size = Builder.CreateMul(Size, ArraySize); @@ -742,7 +759,9 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, !SkippedChecks.has(SanitizerKind::Alignment)) { AlignVal = Alignment.getQuantity(); if (!Ty->isIncompleteType() && !AlignVal) - AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity(); + AlignVal = CGM.getNaturalTypeAlignment(Ty, nullptr, nullptr, + /*ForPointeeType=*/true) + .getQuantity(); // The glvalue must be suitably aligned. if (AlignVal > 1 && @@ -858,8 +877,12 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, static bool isFlexibleArrayMemberExpr(const Expr *E) { // For compatibility with existing code, we treat arrays of length 0 or // 1 as flexible array members. + // FIXME: This is inconsistent with the warning code in SemaChecking. Unify + // the two mechanisms. const ArrayType *AT = E->getType()->castAsArrayTypeUnsafe(); if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) { + // FIXME: Sema doesn't treat [1] as a flexible array member if the bound + // was produced by macro expansion. if (CAT->getSize().ugt(1)) return false; } else if (!isa<IncompleteArrayType>(AT)) @@ -872,6 +895,10 @@ static bool isFlexibleArrayMemberExpr(const Expr *E) { // FIXME: If the base type of the member expr is not FD->getParent(), // this should not be treated as a flexible array member access. if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { + // FIXME: Sema doesn't treat a T[1] union member as a flexible array + // member, only a T[0] or T[] member gets that treatment. + if (FD->getParent()->isUnion()) + return true; RecordDecl::field_iterator FI( DeclContext::decl_iterator(const_cast<FieldDecl *>(FD))); return ++FI == FD->getParent()->field_end(); @@ -1069,9 +1096,8 @@ Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E, if (isa<ExplicitCastExpr>(CE)) { LValueBaseInfo TargetTypeBaseInfo; TBAAAccessInfo TargetTypeTBAAInfo; - CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(), - &TargetTypeBaseInfo, - &TargetTypeTBAAInfo); + CharUnits Align = CGM.getNaturalPointeeTypeAlignment( + E->getType(), &TargetTypeBaseInfo, &TargetTypeTBAAInfo); if (TBAAInfo) *TBAAInfo = CGM.mergeTBAAInfoForCast(*TBAAInfo, TargetTypeTBAAInfo); @@ -1139,8 +1165,8 @@ Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E, // TODO: conditional operators, comma. // Otherwise, use the alignment of the type. - CharUnits Align = getNaturalPointeeTypeAlignment(E->getType(), BaseInfo, - TBAAInfo); + CharUnits Align = + CGM.getNaturalPointeeTypeAlignment(E->getType(), BaseInfo, TBAAInfo); return Address(EmitScalarExpr(E), Align); } @@ -1276,8 +1302,15 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { return EmitVAArgExprLValue(cast<VAArgExpr>(E)); case Expr::DeclRefExprClass: return EmitDeclRefLValue(cast<DeclRefExpr>(E)); - case Expr::ConstantExprClass: + case Expr::ConstantExprClass: { + const ConstantExpr *CE = cast<ConstantExpr>(E); + if (llvm::Value *Result = ConstantEmitter(*this).tryEmitConstantExpr(CE)) { + QualType RetType = cast<CallExpr>(CE->getSubExpr()->IgnoreImplicit()) + ->getCallReturnType(getContext()); + return MakeNaturalAlignAddrLValue(Result, RetType); + } return EmitLValue(cast<ConstantExpr>(E)->getSubExpr()); + } case Expr::ParenExprClass: return EmitLValue(cast<ParenExpr>(E)->getSubExpr()); case Expr::GenericSelectionExprClass: @@ -1304,7 +1337,6 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { case Expr::ExprWithCleanupsClass: { const auto *cleanups = cast<ExprWithCleanups>(E); - enterFullExpression(cleanups); RunCleanupsScope Scope(*this); LValue LV = EmitLValue(cleanups->getSubExpr()); if (LV.isSimple()) { @@ -1343,6 +1375,8 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { return EmitUnaryOpLValue(cast<UnaryOperator>(E)); case Expr::ArraySubscriptExprClass: return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E)); + case Expr::MatrixSubscriptExprClass: + return EmitMatrixSubscriptExpr(cast<MatrixSubscriptExpr>(E)); case Expr::OMPArraySectionExprClass: return EmitOMPArraySectionExpr(cast<OMPArraySectionExpr>(E)); case Expr::ExtVectorElementExprClass: @@ -1368,6 +1402,7 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { case Expr::CXXDynamicCastExprClass: case Expr::CXXReinterpretCastExprClass: case Expr::CXXConstCastExprClass: + case Expr::CXXAddrspaceCastExprClass: case Expr::ObjCBridgedCastExprClass: return EmitCastLValue(cast<CastExpr>(E)); @@ -1651,15 +1686,14 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile, if (VTy->getNumElements() == 3) { // Bitcast to vec4 type. - llvm::VectorType *vec4Ty = - llvm::VectorType::get(VTy->getElementType(), 4); + auto *vec4Ty = llvm::FixedVectorType::get(VTy->getElementType(), 4); Address Cast = Builder.CreateElementBitCast(Addr, vec4Ty, "castToVec4"); // Now load value. llvm::Value *V = Builder.CreateLoad(Cast, Volatile, "loadVec4"); // Shuffle vector to get vec3. V = Builder.CreateShuffleVector(V, llvm::UndefValue::get(vec4Ty), - {0, 1, 2}, "extractVec"); + ArrayRef<int>{0, 1, 2}, "extractVec"); return EmitFromMemory(V, Ty); } } @@ -1716,6 +1750,42 @@ llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) { return Value; } +// Convert the pointer of \p Addr to a pointer to a vector (the value type of +// MatrixType), if it points to a array (the memory type of MatrixType). +static Address MaybeConvertMatrixAddress(Address Addr, CodeGenFunction &CGF, + bool IsVector = true) { + auto *ArrayTy = dyn_cast<llvm::ArrayType>( + cast<llvm::PointerType>(Addr.getPointer()->getType())->getElementType()); + if (ArrayTy && IsVector) { + auto *VectorTy = llvm::FixedVectorType::get(ArrayTy->getElementType(), + ArrayTy->getNumElements()); + + return Address(CGF.Builder.CreateElementBitCast(Addr, VectorTy)); + } + auto *VectorTy = dyn_cast<llvm::VectorType>( + cast<llvm::PointerType>(Addr.getPointer()->getType())->getElementType()); + if (VectorTy && !IsVector) { + auto *ArrayTy = llvm::ArrayType::get(VectorTy->getElementType(), + VectorTy->getNumElements()); + + return Address(CGF.Builder.CreateElementBitCast(Addr, ArrayTy)); + } + + return Addr; +} + +// Emit a store of a matrix LValue. This may require casting the original +// pointer to memory address (ArrayType) to a pointer to the value type +// (VectorType). +static void EmitStoreOfMatrixScalar(llvm::Value *value, LValue lvalue, + bool isInit, CodeGenFunction &CGF) { + Address Addr = MaybeConvertMatrixAddress(lvalue.getAddress(CGF), CGF, + value->getType()->isVectorTy()); + CGF.EmitStoreOfScalar(value, Addr, lvalue.isVolatile(), lvalue.getType(), + lvalue.getBaseInfo(), lvalue.getTBAAInfo(), isInit, + lvalue.isNontemporal()); +} + void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, LValueBaseInfo BaseInfo, @@ -1729,13 +1799,10 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr, // Handle vec3 special. if (VecTy && VecTy->getNumElements() == 3) { // Our source is a vec3, do a shuffle vector to make it a vec4. - llvm::Constant *Mask[] = {Builder.getInt32(0), Builder.getInt32(1), - Builder.getInt32(2), - llvm::UndefValue::get(Builder.getInt32Ty())}; - llvm::Value *MaskV = llvm::ConstantVector::get(Mask); Value = Builder.CreateShuffleVector(Value, llvm::UndefValue::get(VecTy), - MaskV, "extractVec"); - SrcTy = llvm::VectorType::get(VecTy->getElementType(), 4); + ArrayRef<int>{0, 1, 2, -1}, + "extractVec"); + SrcTy = llvm::FixedVectorType::get(VecTy->getElementType(), 4); } if (Addr.getElementType() != SrcTy) { Addr = Builder.CreateElementBitCast(Addr, SrcTy, "storetmp"); @@ -1766,11 +1833,26 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr, void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit) { + if (lvalue.getType()->isConstantMatrixType()) { + EmitStoreOfMatrixScalar(value, lvalue, isInit, *this); + return; + } + EmitStoreOfScalar(value, lvalue.getAddress(*this), lvalue.isVolatile(), lvalue.getType(), lvalue.getBaseInfo(), lvalue.getTBAAInfo(), isInit, lvalue.isNontemporal()); } +// Emit a load of a LValue of matrix type. This may require casting the pointer +// to memory address (ArrayType) to a pointer to the value type (VectorType). +static RValue EmitLoadOfMatrixLValue(LValue LV, SourceLocation Loc, + CodeGenFunction &CGF) { + assert(LV.getType()->isConstantMatrixType()); + Address Addr = MaybeConvertMatrixAddress(LV.getAddress(CGF), CGF); + LV.setAddress(Addr); + return RValue::get(CGF.EmitLoadOfScalar(LV, Loc)); +} + /// EmitLoadOfLValue - Given an expression that represents a value lvalue, this /// method emits the address of the lvalue, then loads the result as an rvalue, /// returning the rvalue. @@ -1796,6 +1878,9 @@ RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) { if (LV.isSimple()) { assert(!LV.getType()->isFunctionType()); + if (LV.getType()->isConstantMatrixType()) + return EmitLoadOfMatrixLValue(LV, Loc, *this); + // Everything needs a load. return RValue::get(EmitLoadOfScalar(LV, Loc)); } @@ -1809,13 +1894,21 @@ RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) { // If this is a reference to a subset of the elements of a vector, either // shuffle the input or extract/insert them as appropriate. - if (LV.isExtVectorElt()) + if (LV.isExtVectorElt()) { return EmitLoadOfExtVectorElementLValue(LV); + } // Global Register variables always invoke intrinsics if (LV.isGlobalReg()) return EmitLoadOfGlobalRegLValue(LV); + if (LV.isMatrixElt()) { + llvm::LoadInst *Load = + Builder.CreateLoad(LV.getMatrixAddress(), LV.isVolatileQualified()); + return RValue::get( + Builder.CreateExtractElement(Load, LV.getMatrixIdx(), "matrixext")); + } + assert(LV.isBitField() && "Unknown LValue type!"); return EmitLoadOfBitfieldLValue(LV, Loc); } @@ -1870,13 +1963,12 @@ RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) { // Always use shuffle vector to try to retain the original program structure unsigned NumResultElts = ExprVT->getNumElements(); - SmallVector<llvm::Constant*, 4> Mask; + SmallVector<int, 4> Mask; for (unsigned i = 0; i != NumResultElts; ++i) - Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts))); + Mask.push_back(getAccessedFieldNo(i, Elts)); - llvm::Value *MaskV = llvm::ConstantVector::get(Mask); Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()), - MaskV); + Mask); return RValue::get(Vec); } @@ -1922,7 +2014,6 @@ RValue CodeGenFunction::EmitLoadOfGlobalRegLValue(LValue LV) { return RValue::get(Call); } - /// EmitStoreThroughLValue - Store the specified rvalue into the specified /// lvalue, where both are guaranteed to the have the same type, and that type /// is 'Ty'. @@ -1948,6 +2039,15 @@ void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst, if (Dst.isGlobalReg()) return EmitStoreThroughGlobalRegLValue(Src, Dst); + if (Dst.isMatrixElt()) { + llvm::Value *Vec = Builder.CreateLoad(Dst.getMatrixAddress()); + Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(), + Dst.getMatrixIdx(), "matins"); + Builder.CreateStore(Vec, Dst.getMatrixAddress(), + Dst.isVolatileQualified()); + return; + } + assert(Dst.isBitField() && "Unknown LValue type"); return EmitStoreThroughBitfieldLValue(Src, Dst); } @@ -2066,6 +2166,14 @@ void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set"); } else { assert(Info.Offset == 0); + // According to the AACPS: + // When a volatile bit-field is written, and its container does not overlap + // with any non-bit-field member, its container must be read exactly once and + // written exactly once using the access width appropriate to the type of the + // container. The two accesses are not atomic. + if (Dst.isVolatileQualified() && isAAPCS(CGM.getTarget()) && + CGM.getCodeGenOpts().ForceAAPCSBitfieldLoad) + Builder.CreateLoad(Ptr, true, "bf.load"); } // Write the new value back out. @@ -2103,37 +2211,33 @@ void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) { unsigned NumSrcElts = VTy->getNumElements(); - unsigned NumDstElts = Vec->getType()->getVectorNumElements(); + unsigned NumDstElts = + cast<llvm::VectorType>(Vec->getType())->getNumElements(); if (NumDstElts == NumSrcElts) { // Use shuffle vector is the src and destination are the same number of // elements and restore the vector mask since it is on the side it will be // stored. - SmallVector<llvm::Constant*, 4> Mask(NumDstElts); + SmallVector<int, 4> Mask(NumDstElts); for (unsigned i = 0; i != NumSrcElts; ++i) - Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i); + Mask[getAccessedFieldNo(i, Elts)] = i; - llvm::Value *MaskV = llvm::ConstantVector::get(Mask); - Vec = Builder.CreateShuffleVector(SrcVal, - llvm::UndefValue::get(Vec->getType()), - MaskV); + Vec = Builder.CreateShuffleVector( + SrcVal, llvm::UndefValue::get(Vec->getType()), Mask); } else if (NumDstElts > NumSrcElts) { // Extended the source vector to the same length and then shuffle it // into the destination. // FIXME: since we're shuffling with undef, can we just use the indices // into that? This could be simpler. - SmallVector<llvm::Constant*, 4> ExtMask; + SmallVector<int, 4> ExtMask; for (unsigned i = 0; i != NumSrcElts; ++i) - ExtMask.push_back(Builder.getInt32(i)); - ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty)); - llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask); - llvm::Value *ExtSrcVal = - Builder.CreateShuffleVector(SrcVal, - llvm::UndefValue::get(SrcVal->getType()), - ExtMaskV); + ExtMask.push_back(i); + ExtMask.resize(NumDstElts, -1); + llvm::Value *ExtSrcVal = Builder.CreateShuffleVector( + SrcVal, llvm::UndefValue::get(SrcVal->getType()), ExtMask); // build identity - SmallVector<llvm::Constant*, 4> Mask; + SmallVector<int, 4> Mask; for (unsigned i = 0; i != NumDstElts; ++i) - Mask.push_back(Builder.getInt32(i)); + Mask.push_back(i); // When the vector size is odd and .odd or .hi is used, the last element // of the Elts constant array will be one past the size of the vector. @@ -2143,9 +2247,8 @@ void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src, // modify when what gets shuffled in for (unsigned i = 0; i != NumSrcElts; ++i) - Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts); - llvm::Value *MaskV = llvm::ConstantVector::get(Mask); - Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV); + Mask[getAccessedFieldNo(i, Elts)] = i + NumDstElts; + Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, Mask); } else { // We should never shorten the vector llvm_unreachable("unexpected shorten vector length"); @@ -2295,7 +2398,13 @@ EmitBitCastOfLValueToProperType(CodeGenFunction &CGF, static LValue EmitThreadPrivateVarDeclLValue( CodeGenFunction &CGF, const VarDecl *VD, QualType T, Address Addr, llvm::Type *RealVarTy, SourceLocation Loc) { - Addr = CGF.CGM.getOpenMPRuntime().getAddrOfThreadPrivate(CGF, VD, Addr, Loc); + if (CGF.CGM.getLangOpts().OpenMPIRBuilder) + Addr = CodeGenFunction::OMPBuilderCBHelpers::getAddrOfThreadPrivate( + CGF, VD, Addr, Loc); + else + Addr = + CGF.CGM.getOpenMPRuntime().getAddrOfThreadPrivate(CGF, VD, Addr, Loc); + Addr = CGF.Builder.CreateElementBitCast(Addr, RealVarTy); return CGF.MakeAddrLValue(Addr, T, AlignmentSource::Decl); } @@ -2327,9 +2436,9 @@ CodeGenFunction::EmitLoadOfReference(LValue RefLVal, Builder.CreateLoad(RefLVal.getAddress(*this), RefLVal.isVolatile()); CGM.DecorateInstructionWithTBAA(Load, RefLVal.getTBAAInfo()); - CharUnits Align = getNaturalTypeAlignment(RefLVal.getType()->getPointeeType(), - PointeeBaseInfo, PointeeTBAAInfo, - /* forPointeeType= */ true); + CharUnits Align = CGM.getNaturalTypeAlignment( + RefLVal.getType()->getPointeeType(), PointeeBaseInfo, PointeeTBAAInfo, + /* forPointeeType= */ true); return Address(Load, Align); } @@ -2347,9 +2456,9 @@ Address CodeGenFunction::EmitLoadOfPointer(Address Ptr, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) { llvm::Value *Addr = Builder.CreateLoad(Ptr); - return Address(Addr, getNaturalTypeAlignment(PtrTy->getPointeeType(), - BaseInfo, TBAAInfo, - /*forPointeeType=*/true)); + return Address(Addr, CGM.getNaturalTypeAlignment(PtrTy->getPointeeType(), + BaseInfo, TBAAInfo, + /*forPointeeType=*/true)); } LValue CodeGenFunction::EmitLoadOfPointerLValue(Address PtrAddr, @@ -2397,13 +2506,14 @@ static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF, } static llvm::Constant *EmitFunctionDeclPointer(CodeGenModule &CGM, - const FunctionDecl *FD) { + GlobalDecl GD) { + const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); if (FD->hasAttr<WeakRefAttr>()) { ConstantAddress aliasee = CGM.GetWeakRefReference(FD); return aliasee.getPointer(); } - llvm::Constant *V = CGM.GetAddrOfFunction(FD); + llvm::Constant *V = CGM.GetAddrOfFunction(GD); if (!FD->hasPrototype()) { if (const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>()) { @@ -2420,9 +2530,10 @@ static llvm::Constant *EmitFunctionDeclPointer(CodeGenModule &CGM, return V; } -static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, - const Expr *E, const FunctionDecl *FD) { - llvm::Value *V = EmitFunctionDeclPointer(CGF.CGM, FD); +static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF, const Expr *E, + GlobalDecl GD) { + const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); + llvm::Value *V = EmitFunctionDeclPointer(CGF.CGM, GD); CharUnits Alignment = CGF.getContext().getDeclAlign(FD); return CGF.MakeAddrLValue(V, E->getType(), Alignment, AlignmentSource::Decl); @@ -2552,10 +2663,10 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { } else { // Should we be using the alignment of the constant pointer we emitted? CharUnits Alignment = - getNaturalTypeAlignment(E->getType(), - /* BaseInfo= */ nullptr, - /* TBAAInfo= */ nullptr, - /* forPointeeType= */ true); + CGM.getNaturalTypeAlignment(E->getType(), + /* BaseInfo= */ nullptr, + /* TBAAInfo= */ nullptr, + /* forPointeeType= */ true); Addr = Address(Val, Alignment); } return MakeAddrLValue(Addr, T, AlignmentSource::Decl); @@ -2689,6 +2800,12 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { if (const auto *BD = dyn_cast<BindingDecl>(ND)) return EmitLValue(BD->getBinding()); + // We can form DeclRefExprs naming GUID declarations when reconstituting + // non-type template parameters into expressions. + if (const auto *GD = dyn_cast<MSGuidDecl>(ND)) + return MakeAddrLValue(CGM.GetAddrOfMSGuidDecl(GD), T, + AlignmentSource::Decl); + llvm_unreachable("Unhandled DeclRefExpr"); } @@ -2779,7 +2896,7 @@ LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { PredefinedExpr::getIdentKindName(E->getIdentKind()), FnName}; std::string GVName = llvm::join(NameItems, NameItems + 2, "."); if (auto *BD = dyn_cast_or_null<BlockDecl>(CurCodeDecl)) { - std::string Name = SL->getString(); + std::string Name = std::string(SL->getString()); if (!Name.empty()) { unsigned Discriminator = CGM.getCXXABI().getMangleContext().getBlockId(BD, true); @@ -2788,7 +2905,8 @@ LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) { auto C = CGM.GetAddrOfConstantCString(Name, GVName.c_str()); return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl); } else { - auto C = CGM.GetAddrOfConstantCString(FnName, GVName.c_str()); + auto C = + CGM.GetAddrOfConstantCString(std::string(FnName), GVName.c_str()); return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl); } } @@ -2918,7 +3036,8 @@ llvm::Constant *CodeGenFunction::EmitCheckSourceLocation(SourceLocation Loc) { FilenameString = llvm::sys::path::filename(FilenameString); } - auto FilenameGV = CGM.GetAddrOfConstantCString(FilenameString, ".src"); + auto FilenameGV = + CGM.GetAddrOfConstantCString(std::string(FilenameString), ".src"); CGM.getSanitizerMetadata()->disableSanitizerForGlobal( cast<llvm::GlobalVariable>(FilenameGV.getPointer())); Filename = FilenameGV.getPointer(); @@ -3665,6 +3784,23 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E, return LV; } +LValue CodeGenFunction::EmitMatrixSubscriptExpr(const MatrixSubscriptExpr *E) { + assert( + !E->isIncomplete() && + "incomplete matrix subscript expressions should be rejected during Sema"); + LValue Base = EmitLValue(E->getBase()); + llvm::Value *RowIdx = EmitScalarExpr(E->getRowIdx()); + llvm::Value *ColIdx = EmitScalarExpr(E->getColumnIdx()); + llvm::Value *NumRows = Builder.getIntN( + RowIdx->getType()->getScalarSizeInBits(), + E->getBase()->getType()->getAs<ConstantMatrixType>()->getNumRows()); + llvm::Value *FinalIdx = + Builder.CreateAdd(Builder.CreateMul(ColIdx, NumRows), RowIdx); + return LValue::MakeMatrixElt( + MaybeConvertMatrixAddress(Base.getAddress(*this), *this), FinalIdx, + E->getBase()->getType(), Base.getBaseInfo(), TBAAAccessInfo()); +} + static Address emitOMPArraySectionBase(CodeGenFunction &CGF, const Expr *Base, LValueBaseInfo &BaseInfo, TBAAAccessInfo &TBAAInfo, @@ -3695,8 +3831,8 @@ static Address emitOMPArraySectionBase(CodeGenFunction &CGF, const Expr *Base, } LValueBaseInfo TypeBaseInfo; TBAAAccessInfo TypeTBAAInfo; - CharUnits Align = CGF.getNaturalTypeAlignment(ElTy, &TypeBaseInfo, - &TypeTBAAInfo); + CharUnits Align = + CGF.CGM.getNaturalTypeAlignment(ElTy, &TypeBaseInfo, &TypeTBAAInfo); BaseInfo.mergeForCast(TypeBaseInfo); TBAAInfo = CGF.CGM.mergeTBAAInfoForCast(TBAAInfo, TypeTBAAInfo); return Address(CGF.Builder.CreateLoad(BaseLVal.getAddress(CGF)), Align); @@ -3713,7 +3849,7 @@ LValue CodeGenFunction::EmitOMPArraySectionExpr(const OMPArraySectionExpr *E, else ResultExprTy = BaseTy->getPointeeType(); llvm::Value *Idx = nullptr; - if (IsLowerBound || E->getColonLoc().isInvalid()) { + if (IsLowerBound || E->getColonLocFirst().isInvalid()) { // Requesting lower bound or upper bound, but without provided length and // without ':' symbol for the default length -> length = 1. // Idx = LowerBound ?: 0; @@ -4020,17 +4156,17 @@ static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base, return CGF.Builder.CreateStructGEP(base, idx, field->getName()); } -static Address emitPreserveStructAccess(CodeGenFunction &CGF, Address base, - const FieldDecl *field) { +static Address emitPreserveStructAccess(CodeGenFunction &CGF, LValue base, + Address addr, const FieldDecl *field) { const RecordDecl *rec = field->getParent(); - llvm::DIType *DbgInfo = CGF.getDebugInfo()->getOrCreateRecordType( - CGF.getContext().getRecordType(rec), rec->getLocation()); + llvm::DIType *DbgInfo = CGF.getDebugInfo()->getOrCreateStandaloneType( + base.getType(), rec->getLocation()); unsigned idx = CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field); return CGF.Builder.CreatePreserveStructAccessIndex( - base, idx, CGF.getDebugInfoFIndex(rec, field->getFieldIndex()), DbgInfo); + addr, idx, CGF.getDebugInfoFIndex(rec, field->getFieldIndex()), DbgInfo); } static bool hasAnyVptr(const QualType Type, const ASTContext &Context) { @@ -4154,8 +4290,8 @@ LValue CodeGenFunction::EmitLValueForField(LValue base, if (IsInPreservedAIRegion || (getDebugInfo() && rec->hasAttr<BPFPreserveAccessIndexAttr>())) { // Remember the original union field index - llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateRecordType( - getContext().getRecordType(rec), rec->getLocation()); + llvm::DIType *DbgInfo = getDebugInfo()->getOrCreateStandaloneType(base.getType(), + rec->getLocation()); addr = Address( Builder.CreatePreserveUnionAccessIndex( addr.getPointer(), getDebugInfoFIndex(rec, field->getFieldIndex()), DbgInfo), @@ -4172,7 +4308,7 @@ LValue CodeGenFunction::EmitLValueForField(LValue base, addr = emitAddrOfFieldStorage(*this, addr, field); else // Remember the original struct field index - addr = emitPreserveStructAccess(*this, addr, field); + addr = emitPreserveStructAccess(*this, base, addr, field); } // If this is a reference field, load the reference right now. @@ -4248,6 +4384,14 @@ LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){ EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(), /*Init*/ true); + // Block-scope compound literals are destroyed at the end of the enclosing + // scope in C. + if (!getLangOpts().CPlusPlus) + if (QualType::DestructionKind DtorKind = E->getType().isDestructedType()) + pushLifetimeExtendedDestroy(getCleanupKind(DtorKind), DeclPtr, + E->getType(), getDestroyer(DtorKind), + DtorKind & EHCleanup); + return Result; } @@ -4295,6 +4439,16 @@ EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) { // If the true case is live, we need to track its region. if (CondExprBool) incrementProfileCounter(expr); + // If a throw expression we emit it and return an undefined lvalue + // because it can't be used. + if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(live->IgnoreParens())) { + EmitCXXThrowExpr(ThrowExpr); + llvm::Type *Ty = + llvm::PointerType::getUnqual(ConvertType(dead->getType())); + return MakeAddrLValue( + Address(llvm::UndefValue::get(Ty), CharUnits::One()), + dead->getType()); + } return EmitLValue(live); } } @@ -4620,7 +4774,8 @@ RValue CodeGenFunction::EmitSimpleCallExpr(const CallExpr *E, return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue); } -static CGCallee EmitDirectCallee(CodeGenFunction &CGF, const FunctionDecl *FD) { +static CGCallee EmitDirectCallee(CodeGenFunction &CGF, GlobalDecl GD) { + const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); if (auto builtinID = FD->getBuiltinID()) { // Replaceable builtin provide their own implementation of a builtin. Unless @@ -4632,8 +4787,8 @@ static CGCallee EmitDirectCallee(CodeGenFunction &CGF, const FunctionDecl *FD) { return CGCallee::forBuiltin(builtinID, FD); } - llvm::Constant *calleePtr = EmitFunctionDeclPointer(CGF.CGM, FD); - return CGCallee::forDirect(calleePtr, GlobalDecl(FD)); + llvm::Constant *calleePtr = EmitFunctionDeclPointer(CGF.CGM, GD); + return CGCallee::forDirect(calleePtr, GD); } CGCallee CodeGenFunction::EmitCallee(const Expr *E) { @@ -4774,7 +4929,7 @@ CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) { } Address CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) { - return Builder.CreateElementBitCast(CGM.GetAddrOfUuidDescriptor(E), + return Builder.CreateElementBitCast(CGM.GetAddrOfMSGuidDecl(E->getGuidDecl()), ConvertType(E->getType())); } @@ -5019,7 +5174,8 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, const CGCallee &OrigCallee // to the function type. if (isa<FunctionNoProtoType>(FnType) || Chain) { llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo); - CalleeTy = CalleeTy->getPointerTo(); + int AS = Callee.getFunctionPointer()->getType()->getPointerAddressSpace(); + CalleeTy = CalleeTy->getPointerTo(AS); llvm::Value *CalleePtr = Callee.getFunctionPointer(); CalleePtr = Builder.CreateBitCast(CalleePtr, CalleeTy, "callee.knr.cast"); |