comparison gen/functions.cpp @ 1117:4c20fcc4252b

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.
author Frits van Bommel <fvbommel wxs.nl>
date Sat, 14 Mar 2009 22:15:31 +0100
parents 802d508f66f1
children 3d1b16dabd25
comparison
equal deleted inserted replaced
1116:d584cda84b00 1117:4c20fcc4252b
20 #include "gen/todebug.h" 20 #include "gen/todebug.h"
21 #include "gen/classes.h" 21 #include "gen/classes.h"
22 #include "gen/dvalue.h" 22 #include "gen/dvalue.h"
23 #include "gen/abi.h" 23 #include "gen/abi.h"
24 24
25 using namespace llvm::Attribute;
26
25 const llvm::FunctionType* DtoFunctionType(Type* type, Type* thistype, Type* nesttype, bool ismain) 27 const llvm::FunctionType* DtoFunctionType(Type* type, Type* thistype, Type* nesttype, bool ismain)
26 { 28 {
27 if (Logger::enabled()) 29 if (Logger::enabled())
28 Logger::println("DtoFunctionType(%s)", type->toChars()); 30 Logger::println("DtoFunctionType(%s)", type->toChars());
29 LOG_SCOPE 31 LOG_SCOPE
60 unsigned a = 0; 62 unsigned a = 0;
61 // sret return 63 // sret return
62 if (f->linkage != LINKintrinsic) 64 if (f->linkage != LINKintrinsic)
63 if (gABI->returnInArg(f)) 65 if (gABI->returnInArg(f))
64 { 66 {
65 f->fty.arg_sret = new IrFuncTyArg(rt, true, llvm::Attribute::StructRet); 67 f->fty.arg_sret = new IrFuncTyArg(rt, true, StructRet | NoAlias | NoCapture);
66 rt = Type::tvoid; 68 rt = Type::tvoid;
67 lidx++; 69 lidx++;
68 } 70 }
69 // sext/zext return 71 // sext/zext return
70 else if (unsigned se = DtoShouldExtend(rt)) 72 else if (unsigned se = DtoShouldExtend(rt))
84 } 86 }
85 87
86 // and nested functions 88 // and nested functions
87 else if (nesttype) 89 else if (nesttype)
88 { 90 {
89 f->fty.arg_nest = new IrFuncTyArg(nesttype, false); 91 f->fty.arg_nest = new IrFuncTyArg(nesttype, false, NoAlias | NoCapture);
90 lidx++; 92 lidx++;
91 } 93 }
92 94
93 // vararg functions are special too 95 // vararg functions are special too
94 if (f->varargs) 96 if (f->varargs)
101 { 103 {
102 // _arguments 104 // _arguments
103 f->fty.arg_arguments = new IrFuncTyArg(Type::typeinfo->type->arrayOf(), false); 105 f->fty.arg_arguments = new IrFuncTyArg(Type::typeinfo->type->arrayOf(), false);
104 lidx++; 106 lidx++;
105 // _argptr 107 // _argptr
106 f->fty.arg_argptr = new IrFuncTyArg(Type::tvoid->pointerTo(), false); 108 f->fty.arg_argptr = new IrFuncTyArg(Type::tvoid->pointerTo(), false, NoAlias | NoCapture);
107 lidx++; 109 lidx++;
108 } 110 }
109 } 111 }
110 else if (f->linkage == LINKc) 112 else if (f->linkage == LINKc)
111 { 113 {