# HG changeset patch # User Frits van Bommel # Date 1237065331 -3600 # Node ID 4c20fcc4252bb2502f2a3d1fcc2beb822bac320c # Parent d584cda84b00b231ff2acc94edc048190cf675df Fun with parameter attributes: For several of the "synthetic" parameters added to D functions, we can apply noalias and nocapture. They are sret parameters, 'nest' pointers passed to nested functions, and _argptr: Nocapture: - Sret and nest are nocapture because they don't represent D-level variables, and thus the callee can't (validly) obtain a pointer to them, let alone keep it around after it returns. - _argptr is nocapture because although the callee has access to it as a pointer, that pointer is invalidated when it returns. All three are noalias because they're function-local variables - Sret and _argptr are noalias because they're freshly alloca'd memory only used for a single function call that's not allowed to keep an aliasing pointer to it around (since the parameter is nocapture). - 'Nest' is noalias because the callee only ever has access to one such pointer per parent function, and every parent function has a different one. This commit also ensures attributes set on sret, _arguments and _argptr are propagated to calls to such functions. It also adds one exception to the general rule that attributes on function types should propagate to calls: the type of a delegate's function pointer has a 'nest' parameter, but this can either be a true 'nest' (for delegates to nested functions) or a 'this' (for delegates to member functions). Since 'this' is neither noalias nor nocapture, and there's generally no way to tell which one it is, we remove these attributes at the call site if the callee is a delegate. diff -r d584cda84b00 -r 4c20fcc4252b gen/functions.cpp --- a/gen/functions.cpp Sat Mar 14 01:32:10 2009 +0100 +++ b/gen/functions.cpp Sat Mar 14 22:15:31 2009 +0100 @@ -22,6 +22,8 @@ #include "gen/dvalue.h" #include "gen/abi.h" +using namespace llvm::Attribute; + const llvm::FunctionType* DtoFunctionType(Type* type, Type* thistype, Type* nesttype, bool ismain) { if (Logger::enabled()) @@ -62,7 +64,7 @@ if (f->linkage != LINKintrinsic) if (gABI->returnInArg(f)) { - f->fty.arg_sret = new IrFuncTyArg(rt, true, llvm::Attribute::StructRet); + f->fty.arg_sret = new IrFuncTyArg(rt, true, StructRet | NoAlias | NoCapture); rt = Type::tvoid; lidx++; } @@ -86,7 +88,7 @@ // and nested functions else if (nesttype) { - f->fty.arg_nest = new IrFuncTyArg(nesttype, false); + f->fty.arg_nest = new IrFuncTyArg(nesttype, false, NoAlias | NoCapture); lidx++; } @@ -103,7 +105,7 @@ f->fty.arg_arguments = new IrFuncTyArg(Type::typeinfo->type->arrayOf(), false); lidx++; // _argptr - f->fty.arg_argptr = new IrFuncTyArg(Type::tvoid->pointerTo(), false); + f->fty.arg_argptr = new IrFuncTyArg(Type::tvoid->pointerTo(), false, NoAlias | NoCapture); lidx++; } } diff -r d584cda84b00 -r 4c20fcc4252b gen/tocall.cpp --- a/gen/tocall.cpp Sat Mar 14 01:32:10 2009 +0100 +++ b/gen/tocall.cpp Sat Mar 14 22:15:31 2009 +0100 @@ -188,10 +188,22 @@ LLValue* typeinfoarrayparam = new llvm::GlobalVariable(tiarrty, true, llvm::GlobalValue::InternalLinkage, tiinits, "._arguments.array", gIR->module); + llvm::AttributeWithIndex Attr; // specify arguments args.push_back(DtoLoad(typeinfoarrayparam)); + if (unsigned atts = tf->fty.arg_arguments->attrs) { + Attr.Index = argidx; + Attr.Attrs = atts; + attrs.push_back(Attr); + } ++argidx; + args.push_back(gIR->ir->CreateBitCast(mem, getPtrToType(LLType::Int8Ty), "tmp")); + if (unsigned atts = tf->fty.arg_argptr->attrs) { + Attr.Index = argidx; + Attr.Attrs = atts; + attrs.push_back(Attr); + } ++argidx; // pass non variadic args @@ -287,7 +299,8 @@ // add attrs for hidden ptr Attr.Index = 1; - Attr.Attrs = llvm::Attribute::StructRet; + Attr.Attrs = tf->fty.arg_sret->attrs; + assert((Attr.Attrs & llvm::Attribute::StructRet) && "Sret arg not sret?"); attrs.push_back(Attr); } @@ -342,7 +355,16 @@ { Attr.Index = retinptr ? 2 : 1; Attr.Attrs = tf->fty.arg_nest->attrs; - attrs.push_back(Attr); + // For delegates, we can't assume 'nest' is noalias and nocapture + // (like we can with nested functions) since it might actually be + // a 'this', and thus neither attribute generally applies to it. + // TODO: don't remove nocapture if it's a "pure" delegate? + if (delegatecall) { + Attr.Attrs &= ~(llvm::Attribute::NoAlias | llvm::Attribute::NoCapture); + } + // LLVM doesn't like it when no bits are set... + if (Attr.Attrs) + attrs.push_back(Attr); } }