jmolloy created this revision. jmolloy added a reviewer: aaron.ballman. jmolloy added a subscriber: cfe-commits. jmolloy set the repository for this revision to rL LLVM.
The C++ spec (3.6.1.3) says "The function `main` shall not be used within a program". This implies that it cannot recurse, so add the norecurse attribute to help the midend out a bit. Repository: rL LLVM http://reviews.llvm.org/D14615 Files: lib/CodeGen/CodeGenFunction.cpp test/CodeGenCXX/main-norecurse.cpp test/CodeGenObjC/objc-literal-tests.m Index: test/CodeGenObjC/objc-literal-tests.m =================================================================== --- test/CodeGenObjC/objc-literal-tests.m +++ test/CodeGenObjC/objc-literal-tests.m @@ -94,4 +94,4 @@ bar(^(void) { return YES; }); } -// CHECK: attributes [[NUW]] = { nounwind{{.*}} } +// CHECK: attributes [[NUW]] = { {{(norecurse )?}}nounwind{{.*}} } Index: test/CodeGenCXX/main-norecurse.cpp =================================================================== --- /dev/null +++ test/CodeGenCXX/main-norecurse.cpp @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -emit-llvm %s -o - + +// CHECK: define i{{.*}} @main({{.*}}) [[A:#.*]] +int main(int argc, char **argv) { + return 1; +} + +// CHECK: attribute [[A]] = attributes { norecurse{{.*}} } Index: lib/CodeGen/CodeGenFunction.cpp =================================================================== --- lib/CodeGen/CodeGenFunction.cpp +++ lib/CodeGen/CodeGenFunction.cpp @@ -716,6 +716,14 @@ } } + // If we're in C++ mode and the function name is "main", it is guaranteed + // to be norecurse by the standard (3.6.1.3 "The function main shall not be + // used within a program") + if (getLangOpts().CPlusPlus) + if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) + if (FD->isMain()) + Fn->addFnAttr(llvm::Attribute::NoRecurse); + llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); // Create a marker to make it easy to insert allocas into the entryblock
Index: test/CodeGenObjC/objc-literal-tests.m =================================================================== --- test/CodeGenObjC/objc-literal-tests.m +++ test/CodeGenObjC/objc-literal-tests.m @@ -94,4 +94,4 @@ bar(^(void) { return YES; }); } -// CHECK: attributes [[NUW]] = { nounwind{{.*}} } +// CHECK: attributes [[NUW]] = { {{(norecurse )?}}nounwind{{.*}} } Index: test/CodeGenCXX/main-norecurse.cpp =================================================================== --- /dev/null +++ test/CodeGenCXX/main-norecurse.cpp @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -emit-llvm %s -o - + +// CHECK: define i{{.*}} @main({{.*}}) [[A:#.*]] +int main(int argc, char **argv) { + return 1; +} + +// CHECK: attribute [[A]] = attributes { norecurse{{.*}} } Index: lib/CodeGen/CodeGenFunction.cpp =================================================================== --- lib/CodeGen/CodeGenFunction.cpp +++ lib/CodeGen/CodeGenFunction.cpp @@ -716,6 +716,14 @@ } } + // If we're in C++ mode and the function name is "main", it is guaranteed + // to be norecurse by the standard (3.6.1.3 "The function main shall not be + // used within a program") + if (getLangOpts().CPlusPlus) + if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) + if (FD->isMain()) + Fn->addFnAttr(llvm::Attribute::NoRecurse); + llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); // Create a marker to make it easy to insert allocas into the entryblock
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits