Index: include/clang/AST/Type.h =================================================================== --- include/clang/AST/Type.h +++ include/clang/AST/Type.h @@ -709,6 +709,8 @@ return QualType::isConstant(*this, Ctx); } + bool isWriteOnce(ASTContext &Ctx); + /// \brief Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10). bool isPODType(ASTContext &Context) const; Index: lib/AST/Type.cpp =================================================================== --- lib/AST/Type.cpp +++ lib/AST/Type.cpp @@ -1913,6 +1913,19 @@ } } +bool QualType::isWriteOnce(ASTContext &Context) { + + if (!Context.getLangOpts().CPlusPlus || + !isConstant(Context) || getTypePtr()->isReferenceType()) + return false; + + if (const CXXRecordDecl *Record + = Context.getBaseElementType(*this)->getAsCXXRecordDecl()) + return !Record->hasMutableFields(); + + return true; +} + bool QualType::isPODType(ASTContext &Context) const { // C++11 has a more relaxed definition of POD. if (Context.getLangOpts().CPlusPlus11) Index: lib/CodeGen/CGDecl.cpp =================================================================== --- lib/CodeGen/CGDecl.cpp +++ lib/CodeGen/CGDecl.cpp @@ -524,6 +524,20 @@ CGF.EmitLifetimeEnd(Size, Addr); } }; + + /// A cleanup to call @llvm.invariant.end. + class CallInvariantEnd final : public EHScopeStack::Cleanup { + llvm::CallInst *StartInst; + llvm::Value *Addr; + llvm::Value *Size; // TODO: Is this even necessary? + public: + CallInvariantEnd(llvm::CallInst *C, llvm::Value *addr, llvm::Value *size) + : StartInst(C), Addr(addr), Size(size) {} + + void Emit(CodeGenFunction &CGF, Flags flags) override { + CGF.EmitInvariantEnd(StartInst, Size, Addr); + } + }; } /// EmitAutoVarWithLifetime - Does the setup required for an automatic @@ -842,12 +856,40 @@ canEmitInitWithFewStoresAfterMemset(Init, StoreBudget); } +/// Emit the code necessary to initialize the given global variable. +MarkWriteOnceWritten::MarkWriteOnceWritten(CodeGenFunction &CGF, + const VarDecl& D, llvm::Value *Addr) +: GV(dyn_cast(Addr)), + AI(dyn_cast(Addr)), + Args{nullptr, 0, nullptr}, + CGF(CGF) { + + // Only GlobalVariable's and AllocaInst's can be writeonce. + // Exit if the given address is none of these. + if (!GV && !AI) return; + + // If the address is writeonce, then emit @llvm.invariant.start() intrinsic. + // Then, for non-global variables, push the emission of the + // @llvm.invariant.end() intrinsic onto the cleanup stack. + if ((AI || !GV->isConstant()) && D.getType().isWriteOnce(CGF.getContext())) + Args = CGF.EmitInvariantStart(D, Addr); +} + +MarkWriteOnceWritten::~MarkWriteOnceWritten() { + if (AI && Args.StartInst) { + assert(!GV && "Can't have it both ways!"); + CGF.EHStack.pushCleanup(NormalCleanup, Args.StartInst, + Args.Addr, Args.Size); + } +} + /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a /// variable declaration with auto, register, or no storage class specifier. /// These turn into simple stack objects, or GlobalValues depending on target. void CodeGenFunction::EmitAutoVarDecl(const VarDecl &D) { AutoVarEmission emission = EmitAutoVarAlloca(D); EmitAutoVarInit(emission); + MarkWriteOnceWritten MWO(*this, D, emission.Address); EmitAutoVarCleanups(emission); } @@ -880,6 +922,49 @@ C->setDoesNotThrow(); } +/// Emit code to cause the variable at the given address to be considered as +/// constant from this point onwards. +CodeGenModule::InvariantArgs +CodeGenFunction::EmitInvariantStart(const VarDecl &D, llvm::Value *Addr) { + // Don't emit the intrinsic if we're not optimizing. + if (!CGM.getCodeGenOpts().OptimizationLevel) + return CodeGenModule::InvariantArgs{nullptr, 0, nullptr}; + + // Grab the llvm.invariant.start intrinsic. + llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start; + llvm::Constant *InvariantStart = CGM.getIntrinsic(InvStartID); + + // Emit a call with the size in bytes of the object. + CharUnits WidthChars = getContext().getTypeSizeInChars(D.getType()); + uint64_t Width = WidthChars.getQuantity(); + + llvm::Value *Size; + if (llvm::Constant* CAddr = dyn_cast(Addr)) { + Size = llvm::ConstantInt::getSigned(Int64Ty, Width); + Addr = llvm::ConstantExpr::getBitCast(CAddr, Int8PtrTy); + } + else { + //Width = CGM.getDataLayout().getTypeAllocSize(LTy); + Size = llvm::ConstantInt::get(Int64Ty, Width); + Addr = Builder.CreateBitCast(Addr, Int8PtrTy); + } + llvm::CallInst *C = Builder.CreateCall(InvariantStart, {Size, Addr}); + C->setDoesNotThrow(); + + return CodeGenModule::InvariantArgs{C, Size, Addr}; +} + +void CodeGenFunction::EmitInvariantEnd(llvm::CallInst *Start, + llvm::Value *Size, llvm::Value *Addr) { + // Grab the llvm.invariant.end intrinsic. + llvm::Intrinsic::ID InvEndID = llvm::Intrinsic::invariant_end; + llvm::Constant *InvariantEnd = CGM.getIntrinsic(InvEndID); + + // Emit a call with the size in bytes of the object. + llvm::CallInst *C = Builder.CreateCall(InvariantEnd, {Start, Size, Addr}); + C->setDoesNotThrow(); +} + /// EmitAutoVarAlloca - Emit the alloca and debug information for a /// local variable. Does not emit initialization or destruction. CodeGenFunction::AutoVarEmission Index: lib/CodeGen/CGDeclCXX.cpp =================================================================== --- lib/CodeGen/CGDeclCXX.cpp +++ lib/CodeGen/CGDeclCXX.cpp @@ -112,26 +112,6 @@ CGM.getCXXABI().registerGlobalDtor(CGF, D, function, argument); } -/// Emit code to cause the variable at the given address to be considered as -/// constant from this point onwards. -static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D, - llvm::Constant *Addr) { - // Don't emit the intrinsic if we're not optimizing. - if (!CGF.CGM.getCodeGenOpts().OptimizationLevel) - return; - - // Grab the llvm.invariant.start intrinsic. - llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start; - llvm::Constant *InvariantStart = CGF.CGM.getIntrinsic(InvStartID); - - // Emit a call with the size in bytes of the object. - CharUnits WidthChars = CGF.getContext().getTypeSizeInChars(D.getType()); - uint64_t Width = WidthChars.getQuantity(); - llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(CGF.Int64Ty, Width), - llvm::ConstantExpr::getBitCast(Addr, CGF.Int8PtrTy)}; - CGF.Builder.CreateCall(InvariantStart, Args); -} - void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr, bool PerformInit) { @@ -169,9 +149,13 @@ PerformInit, this); if (PerformInit) EmitDeclInit(*this, D, DeclPtr); - if (CGM.isTypeConstant(D.getType(), true)) - EmitDeclInvariant(*this, D, DeclPtr); - else + if (CGM.isTypeConstant(D.getType(), true)) { + // Generate the @llvm.invariant.start intrinsic call if DeclPtr is + // not 'writeonce'. We'll do the same for 'writeonce' addresses later. + llvm::GlobalVariable* GV = dyn_cast(DeclPtr); + if (GV && (GV->isConstant() || !D.getType().isWriteOnce(getContext()))) + (void) EmitInvariantStart(D, DeclPtr); + } else EmitDeclDestroy(*this, D, DeclPtr); return; } @@ -491,6 +475,7 @@ } else { EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit); } + MarkWriteOnceWritten MWO(*this, *D, Addr); FinishFunction(); } Index: lib/CodeGen/CGExprAgg.cpp =================================================================== --- lib/CodeGen/CGExprAgg.cpp +++ lib/CodeGen/CGExprAgg.cpp @@ -1408,7 +1408,7 @@ // Optimize the slot if possible. CheckAggExprForMemSetUse(Slot, E, *this); - + AggExprEmitter(*this, Slot, Slot.isIgnored()).Visit(const_cast(E)); } Index: lib/CodeGen/CodeGenFunction.h =================================================================== --- lib/CodeGen/CodeGenFunction.h +++ lib/CodeGen/CodeGenFunction.h @@ -1797,6 +1797,10 @@ llvm::Value *EmitLifetimeStart(uint64_t Size, llvm::Value *Addr); void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr); + CodeGenModule::InvariantArgs EmitInvariantStart(const VarDecl &D, + llvm::Value *Addr); + void EmitInvariantEnd(llvm::CallInst *C, + llvm::Value *Size, llvm::Value *Addr); llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E); void EmitCXXDeleteExpr(const CXXDeleteExpr *E); @@ -3050,6 +3054,17 @@ llvm::Value *GetValueForARMHint(unsigned BuiltinID); }; +class MarkWriteOnceWritten { + llvm::GlobalVariable *GV; + llvm::AllocaInst *AI; + CodeGenModule::InvariantArgs Args; + CodeGenFunction &CGF; +public: + MarkWriteOnceWritten(CodeGenFunction &CGF, + const VarDecl& D, llvm::Value *Addr); + ~MarkWriteOnceWritten(); +}; + /// Helper class with most of the code for saving a value for a /// conditional expression cleanup. struct DominatingLLVMValue { Index: lib/CodeGen/CodeGenModule.h =================================================================== --- lib/CodeGen/CodeGenModule.h +++ lib/CodeGen/CodeGenModule.h @@ -277,6 +277,14 @@ typedef std::vector CtorList; + struct InvariantArgs { + llvm::CallInst *StartInst; + llvm::Value *Size; + llvm::Value *Addr; + InvariantArgs(llvm::CallInst *C, llvm::Value *size, llvm::Value *addr) + : StartInst(C), Size(size), Addr(addr) {} + }; + private: ASTContext &Context; const LangOptions &LangOpts;