llvm.org GIT mirror llvm / 2c9c3e7
Implement function notes as function attributes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@56716 91177308-0d34-0410-b5e6-96231b3b80d8 Devang Patel 12 years ago
17 changed file(s) with 101 addition(s) and 187 deletion(s). Raw diff Collapse all Expand all
2525
  • Functions
  • 2626
  • Aliases
  • 2727
  • Parameter Attributes
  • 28
  • Function Attributes
  • 2829
  • Garbage Collector Names
  • 29
  • Function Notes
  • 3030
  • Module-Level Inline Assembly
  • 3131
  • Data Layout
  • 3232
    825825
    826826
    827827
    
                      
                    
    828 declare i32 @printf(i8* noalias , ...) nounwind
    829 declare i32 @atoi(i8*) nounwind readonly
    828 declare i32 @printf(i8* noalias , ...)
    829 declare i32 @atoi(i8 zeroext*)
    830830
    831831
    832832
    869869 parameter. The caller is responsible for ensuring that this is the case,
    870870 usually by placing the value in a stack allocation.
    871871
    872
    noreturn
    873
    This function attribute indicates that the function never returns. This
    874 indicates to LLVM that every call to this function should be treated as if
    875 an unreachable instruction immediately followed the call.
    876
    877
    nounwind
    878
    This function attribute indicates that no exceptions unwind out of the
    879 function. Usually this is because the function makes no use of exceptions,
    880 but it may also be that the function catches any exceptions thrown when
    881 executing it.
    882
    883872
    nest
    884873
    This indicates that the pointer parameter can be excised using the
    885874 trampoline intrinsics.
    886
    readonly
    887
    This function attribute indicates that the function has no side-effects
    888 except for producing a return value or throwing an exception. The value
    889 returned must only depend on the function arguments and/or global variables.
    890 It may use values obtained by dereferencing pointers.
    891
    readnone
    892
    A readnone function has the same restrictions as a readonly
    893 function, but in addition it is not allowed to dereference any pointer arguments
    894 or global variables.
    895875
    896876
    897877
    915895
    916896
    917897
    918 Function Notes
    919
    920
    921
    922

    The function definition may list function notes which are used by

    923 various passes.>
    898 Function Attributes>
    899
    900
    901
    902
    903

    Function attributes are set to communicate additional information about

    904 a function. Function attributes are considered to be part of the function,
    905 not of the function type, so functions with different parameter attributes
    906 can have the same function type.

    907
    908

    Function attributes are simple keywords that follow the type specified. If

    909 multiple attributes are needed, they are space separated. For
    910 example:

    924911
    925912
    926913
    
                      
                    
    927 define void @f() notes(inline=Always) { ... }
    928 define void @f() notes(inline=Always,opt-size) { ... }
    929 define void @f() notes(inline=Never,opt-size) { ... }
    930 define void @f() notes(opt-size) { ... }
    914 define void @f() noinline { ... }
    915 define void @f() alwaysinline { ... }
    916 define void @f() alwaysinline optsize { ... }
    917 define void @f() optsize
    931918
    932919
    933920
    934921
    935
    inline=Always
    936
    This note requests inliner to inline this function irrespective of inlining
    937 size threshold for this function.
    938
    939
    inline=Never
    940
    This note requests inliner to never inline this function in any situation.
    941 This note may not be used together with inline=Always note.
    942
    943
    opt-size
    944
    This note suggests optimization passes and code generator passes to make
    945 choices that help reduce code size.
    946
    922
    alwaysinline
    923
    This attribute requests inliner to inline this function irrespective of
    924 inlining size threshold for this function.
    925
    926
    noinline
    927
    This attributes requests inliner to never inline this function in any
    928 situation. This attribute may not be used together with alwaysinline
    929 attribute.
    930
    931
    optsize
    932
    This attribute suggests optimization passes and code generator passes to
    933 make choices that help reduce code size.
    934
    935
    noreturn
    936
    This function attribute indicates that the function never returns. This
    937 indicates to LLVM that every call to this function should be treated as if
    938 an unreachable instruction immediately followed the call.
    939
    940
    nounwind
    941
    This function attribute indicates that no exceptions unwind out of the
    942 function. Usually this is because the function makes no use of exceptions,
    943 but it may also be that the function catches any exceptions thrown when
    944 executing it.
    945
    946
    readonly
    947
    This function attribute indicates that the function has no side-effects
    948 except for producing a return value or throwing an exception. The value
    949 returned must only depend on the function arguments and/or global variables.
    950 It may use values obtained by dereferencing pointers.
    951
    readnone
    952
    A readnone function has the same restrictions as a readonly
    953 function, but in addition it is not allowed to dereference any pointer arguments
    954 or global variables.
    947955
    948956
    949

    Any notes that are not documented here are considered invalid notes.

    950957
    951958
    952959
    148148 void setAttributes(const AttrListPtr &attrs) { AttributeList = attrs; }
    149149
    150150
    151 /// hasNote - Return true if this function has given note.
    152 bool hasNote(Attributes N) const {
    153 // Notes are stored at ~0 index in parameter attribute list
    154 return (paramHasAttr(~0, N));
    155 }
    156
    157 /// setNotes - Set notes for this function
    158 ///
    159 void setNotes(const Attributes N) {
    160 // Notes are stored at ~0 index in parameter attribute list
    151 /// hasFnAttr - Return true if this function has given attribute.
    152 bool hasFnAttr(Attributes N) const {
    153 // Function Attributes are stored at ~0 index
    154 return AttributeList.paramHasAttr(~0U, N);
    155 }
    156
    157 /// addFnAttr - Add function attributes
    158 ///
    159 void addFnAttr(const Attributes N) {
    160 // Function Attributes are stored at ~0 index
    161161 addAttribute(~0, N);
    162162 }
    163163
    495495 KEYWORD("readnone", READNONE);
    496496 KEYWORD("readonly", READONLY);
    497497
    498 KEYWORD("notes", FNNOTE);
    499 KEYWORD("inline", INLINE);
    500 KEYWORD("always", ALWAYS);
    501 KEYWORD("never", NEVER);
    502 KEYWORD("opt_size", OPTIMIZEFORSIZE);
    498 KEYWORD("noinline", NOINLINE);
    499 KEYWORD("alwaysinline", ALWAYSINLINE);
    500 KEYWORD("optsize", OPTSIZE);
    503501
    504502 KEYWORD("type", TYPE);
    505503 KEYWORD("opaque", OPAQUE);
    188188 READNONE = 405,
    189189 READONLY = 406,
    190190 GC = 407,
    191 FNNOTE = 408,
    192 INLINE = 409,
    193 ALWAYS = 410,
    194 NEVER = 411,
    195 OPTIMIZEFORSIZE = 412,
    196 DEFAULT = 413,
    197 HIDDEN = 414,
    198 PROTECTED = 415
    191 OPTSIZE = 408,
    192 NOINLINE = 409,
    193 ALWAYSINLINE = 410,
    194 DEFAULT = 411,
    195 HIDDEN = 412,
    196 PROTECTED = 413
    199197 };
    200198 #endif
    201199 /* Tokens. */
    349347 #define READNONE 405
    350348 #define READONLY 406
    351349 #define GC 407
    352 #define FNNOTE 408
    353 #define INLINE 409
    354 #define ALWAYS 410
    355 #define NEVER 411
    356 #define OPTIMIZEFORSIZE 412
    357 #define DEFAULT 413
    358 #define HIDDEN 414
    359 #define PROTECTED 415
    350 #define OPTSIZE 408
    351 #define NOINLINE 409
    352 #define ALWAYSINLINE 410
    353 #define DEFAULT 411
    354 #define HIDDEN 412
    355 #define PROTECTED 413
    360356
    361357
    362358
    363359
    364360 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
    365361 typedef union YYSTYPE
    366 #line 970 "/Volumes/MacOS9/gcc/llvm/lib/AsmParser/llvmAsmParser.y"
    362 #line 970 "/Volumes/Nanpura/mainline/llvm/lib/AsmParser/llvmAsmParser.y"
    367363 {
    368364 llvm::Module *ModuleVal;
    369365 llvm::Function *FunctionVal;
    412408 llvm::FCmpInst::Predicate FPredicate;
    413409 }
    414410 /* Line 1529 of yacc.c. */
    415 #line 417 "llvmAsmParser.tab.h"
    411 #line 413 "llvmAsmParser.tab.h"
    416412 YYSTYPE;
    417413 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
    418414 # define YYSTYPE_IS_DECLARED 1
    10881088 %type OptCallingConv LocalNumber
    10891089 %type OptAttributes Attribute
    10901090 %type OptFuncAttrs FuncAttr
    1091 %type OptFuncNotes FuncNote
    1092 %type FuncNoteList
    10931091
    10941092 // Basic Block Terminating Operators
    10951093 %token RET BR SWITCH INVOKE UNWIND UNREACHABLE
    11211119
    11221120 // Function Attributes
    11231121 %token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
    1124 %token READNONE READONLY GC
    1125
    1126 // Function Notes
    1127 %token FNNOTE INLINE ALWAYS NEVER OPTIMIZEFORSIZE
    1122 %token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE
    11281123
    11291124 // Visibility Styles
    11301125 %token DEFAULT HIDDEN PROTECTED
    12831278 | SIGNEXT { $$ = Attribute::SExt; }
    12841279 | READNONE { $$ = Attribute::ReadNone; }
    12851280 | READONLY { $$ = Attribute::ReadOnly; }
    1281 | NOINLINE { $$ = Attribute::NoInline }
    1282 | ALWAYSINLINE { $$ = Attribute::AlwaysInline }
    1283 | OPTSIZE { $$ = Attribute::OptimizeForSize }
    12861284 ;
    12871285
    12881286 OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
    12891287 | OptFuncAttrs FuncAttr {
    12901288 $$ = $1 | $2;
    1291 }
    1292 ;
    1293
    1294 FuncNoteList : FuncNote { $$ = $1; }
    1295 | FuncNoteList ',' FuncNote {
    1296 unsigned tmp = $1 | $3;
    1297 if ($3 == Attribute::NoInline
    1298 && ($1 & Attribute::AlwaysInline))
    1299 GEN_ERROR("Function Notes may include only one inline notes!")
    1300 if ($3 == Attribute::AlwaysInline
    1301 && ($1 & Attribute::NoInline))
    1302 GEN_ERROR("Function Notes may include only one inline notes!")
    1303 $$ = tmp;
    1304 CHECK_FOR_ERROR
    1305 }
    1306 ;
    1307
    1308 FuncNote : INLINE '=' NEVER { $$ = Attribute::NoInline; }
    1309 | INLINE '=' ALWAYS { $$ = Attribute::AlwaysInline; }
    1310 | OPTIMIZEFORSIZE { $$ = Attribute::OptimizeForSize; }
    1311 ;
    1312
    1313 OptFuncNotes : /* empty */ { $$ = Attribute::None; }
    1314 | FNNOTE '(' FuncNoteList ')' {
    1315 $$ = $3;
    13161289 }
    13171290 ;
    13181291
    23312304 };
    23322305
    23332306 FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
    2334 OptFuncAttrs OptSection OptAlign OptGC OptFuncNotes {
    2307 OptFuncAttrs OptSection OptAlign OptGC {
    23352308 std::string FunctionName(*$3);
    23362309 delete $3; // Free strdup'd memory!
    23372310
    24522425 Fn->setGC($10->c_str());
    24532426 delete $10;
    24542427 }
    2455 if ($11) {
    2456 Fn->setNotes($11);
    2457 }
    24582428
    24592429 // Add all of the arguments we parsed to the function...
    24602430 if ($5) { // Is null if empty...
    10881088 %type OptCallingConv LocalNumber
    10891089 %type OptAttributes Attribute
    10901090 %type OptFuncAttrs FuncAttr
    1091 %type OptFuncNotes FuncNote
    1092 %type FuncNoteList
    10931091
    10941092 // Basic Block Terminating Operators
    10951093 %token RET BR SWITCH INVOKE UNWIND UNREACHABLE
    11211119
    11221120 // Function Attributes
    11231121 %token SIGNEXT ZEROEXT NORETURN INREG SRET NOUNWIND NOALIAS BYVAL NEST
    1124 %token READNONE READONLY GC
    1125
    1126 // Function Notes
    1127 %token FNNOTE INLINE ALWAYS NEVER OPTIMIZEFORSIZE
    1122 %token READNONE READONLY GC OPTSIZE NOINLINE ALWAYSINLINE
    11281123
    11291124 // Visibility Styles
    11301125 %token DEFAULT HIDDEN PROTECTED
    12831278 | SIGNEXT { $$ = Attribute::SExt; }
    12841279 | READNONE { $$ = Attribute::ReadNone; }
    12851280 | READONLY { $$ = Attribute::ReadOnly; }
    1281 | NOINLINE { $$ = Attribute::NoInline }
    1282 | ALWAYSINLINE { $$ = Attribute::AlwaysInline }
    1283 | OPTSIZE { $$ = Attribute::OptimizeForSize }
    12861284 ;
    12871285
    12881286 OptFuncAttrs : /* empty */ { $$ = Attribute::None; }
    12891287 | OptFuncAttrs FuncAttr {
    12901288 $$ = $1 | $2;
    1291 }
    1292 ;
    1293
    1294 FuncNoteList : FuncNote { $$ = $1; }
    1295 | FuncNoteList ',' FuncNote {
    1296 unsigned tmp = $1 | $3;
    1297 if ($3 == Attribute::NoInline
    1298 && ($1 & Attribute::AlwaysInline))
    1299 GEN_ERROR("Function Notes may include only one inline notes!")
    1300 if ($3 == Attribute::AlwaysInline
    1301 && ($1 & Attribute::NoInline))
    1302 GEN_ERROR("Function Notes may include only one inline notes!")
    1303 $$ = tmp;
    1304 CHECK_FOR_ERROR
    1305 }
    1306 ;
    1307
    1308 FuncNote : INLINE '=' NEVER { $$ = Attribute::NoInline; }
    1309 | INLINE '=' ALWAYS { $$ = Attribute::AlwaysInline; }
    1310 | OPTIMIZEFORSIZE { $$ = Attribute::OptimizeForSize; }
    1311 ;
    1312
    1313 OptFuncNotes : /* empty */ { $$ = Attribute::None; }
    1314 | FNNOTE '(' FuncNoteList ')' {
    1315 $$ = $3;
    13161289 }
    13171290 ;
    13181291
    23312304 };
    23322305
    23332306 FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
    2334 OptFuncAttrs OptSection OptAlign OptGC OptFuncNotes {
    2307 OptFuncAttrs OptSection OptAlign OptGC {
    23352308 std::string FunctionName(*$3);
    23362309 delete $3; // Free strdup'd memory!
    23372310
    24522425 Fn->setGC($10->c_str());
    24532426 delete $10;
    24542427 }
    2455 if ($11) {
    2456 Fn->setNotes($11);
    2457 }
    24582428
    24592429 // Add all of the arguments we parsed to the function...
    24602430 if ($5) { // Is null if empty...
    153153 SwitchToSection(TAI->SectionForGlobal(F));
    154154
    155155 unsigned FnAlign = OptimizeForSize ? 1 : 4;
    156 if (!F->isDeclaration() && F->hasNote(Attribute::OptimizeForSize))
    156 if (!F->isDeclaration() && F->hasFnAttr(Attribute::OptimizeForSize))
    157157 FnAlign = 1;
    158158 switch (F->getLinkage()) {
    159159 default: assert(0 && "Unknown linkage type!");
    140140 SwitchToTextSection("_text", F);
    141141
    142142 unsigned FnAlign = OptimizeForSize ? 1 : 4;
    143 if (!F->isDeclaration() && F->hasNote(Attribute::OptimizeForSize))
    143 if (!F->isDeclaration() && F->hasFnAttr(Attribute::OptimizeForSize))
    144144 FnAlign = 1;
    145145 switch (F->getLinkage()) {
    146146 default: assert(0 && "Unsupported linkage type!");
    6262
    6363 for (Module::iterator I = M.begin(), E = M.end();
    6464 I != E; ++I)
    65 if (!I->isDeclaration() && !I->hasNote(Attribute::AlwaysInline))
    65 if (!I->isDeclaration() && !I->hasFnAttr(Attribute::AlwaysInline))
    6666 NeverInline.insert(I);
    6767
    6868 return false;
    6464
    6565 for (Module::iterator I = M.begin(), E = M.end();
    6666 I != E; ++I)
    67 if (!I->isDeclaration() && I->hasNote(Attribute::NoInline))
    67 if (!I->isDeclaration() && I->hasFnAttr(Attribute::NoInline))
    6868 NeverInline.insert(I);
    6969
    7070 // Get llvm.noinline
    140140
    141141 int CurrentThreshold = InlineThreshold;
    142142 Function *Fn = CS.getCaller();
    143 if (Fn && !Fn->isDeclaration() && Fn->hasNote(Attribute::OptimizeForSize)
    143 if (Fn && !Fn->isDeclaration()
    144 && Fn->hasFnAttr(Attribute::OptimizeForSize)
    144145 && InlineThreshold != 50) {
    145146 CurrentThreshold = 50;
    146147 }
    429429 Function *F = loopHeader->getParent();
    430430
    431431 // Do not unswitch if the function is optimized for size.
    432 if (!F->isDeclaration() && F->hasNote(Attribute::OptimizeForSize))
    432 if (!F->isDeclaration() && F->hasFnAttr(Attribute::OptimizeForSize))
    433433 return false;
    434434
    435435 // Check to see if it would be profitable to unswitch current loop.
    221221 if (CalleeFI.NeverInline)
    222222 return 2000000000;
    223223
    224 if (!Callee->isDeclaration() && Callee->hasNote(Attribute::AlwaysInline))
    224 if (!Callee->isDeclaration() && Callee->hasFnAttr(Attribute::AlwaysInline))
    225225 return -2000000000;
    226226
    227227 // Add to the inline quality for properties that make the call valuable to
    14121412 if (F->isDeclaration()) {
    14131413 Out << "\n";
    14141414 } else {
    1415
    1416 bool insideNotes = false;
    1417 if (F->hasNote(Attribute::AlwaysInline)) {
    1418 Out << " notes(";
    1419 insideNotes = true;
    1420 Out << "inline=always";
    1421 }
    1422 if (F->hasNote(Attribute::NoInline)) {
    1423 if (insideNotes)
    1424 Out << ",";
    1425 else {
    1426 Out << " notes(";
    1427 insideNotes = true;
    1428 }
    1429 Out << "inline=never";
    1430 }
    1431 if (F->hasNote(Attribute::OptimizeForSize)) {
    1432 if (insideNotes)
    1433 Out << ",";
    1434 else {
    1435 Out << " notes(";
    1436 insideNotes = true;
    1437 }
    1438 Out << "opt_size";
    1439 }
    1440 if (insideNotes)
    1441 Out << ")";
    1442
    14431415 Out << " {";
    14441416
    14451417 // Output all of its basic blocks... for the function
    0 ; RUN: llvm-as < %s | llc -march=arm
    11 ; RUN: llvm-as < %s | llc -march=thumb
    22
    3 define double @t(double %x, double %y) nounwind notes(opt_size) {
    3 define double @t(double %x, double %y) nounwind optsize {
    44 entry:
    55 %0 = tail call double @llvm.pow.f64( double %x, double %y ) ; [#uses=1]
    66 ret double %0
    0 ; RUN: llvm-as < %s | opt -inline-threshold=0 -inline | llvm-dis | not grep call
    11
    2 define i32 @fn2() notes(inline=always) {
    2 define i32 @fn2() alwaysinline {
    33 ret i32 1
    44 }
    55
    0 ; RUN: llvm-as < %s | opt -inline | llvm-dis | grep call | count 1
    11
    2 define i32 @fn2() notes(inline=never) {
    2 define i32 @fn2() noinline {
    33 ret i32 1
    44 }
    55