Bryce-MW created this revision.
Bryce-MW edited the summary of this revision.
Bryce-MW retitled this revision from "[CGCall] Annotate op new with 
inaccessiblememonly if AssumeSaneOperatorNew is on" to "[CGCall] Annotate 
operator new with inaccessiblememonly if AssumeSaneOperatorNew is on".
Bryce-MW edited the summary of this revision.
Bryce-MW updated this revision to Diff 401006.
Bryce-MW added a comment.
Herald added a subscriber: dang.
Bryce-MW published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

- Add change to docs


Basic Alias Analysis in LLVM currently has a special case for allocation 
functions (including operator new) that essentially assumes that they are 
inaccessiblememonly. Operator new is not currently annotated with 
inaccessiblememonly because that is not technically correct (especially 
considering global replacement functions). This patch allows that case to be 
removed (D117180 <https://reviews.llvm.org/D117180>) allowing operator new to 
be correct when needed without causing the significant regression that we found 
when removing the special case).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117600

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGCall.cpp


Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -2062,11 +2062,14 @@
       AddAttributesFromFunctionProtoType(
           getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>());
       if (AttrOnCallSite && Fn->isReplaceableGlobalAllocationFunction()) {
-        // A sane operator new returns a non-aliasing pointer.
+        // A sane operator new returns a non-aliasing pointer and is
+        // inaccessiblememonly
         auto Kind = Fn->getDeclName().getCXXOverloadedOperator();
         if (getCodeGenOpts().AssumeSaneOperatorNew &&
-            (Kind == OO_New || Kind == OO_Array_New))
+            (Kind == OO_New || Kind == OO_Array_New)) {
           RetAttrs.addAttribute(llvm::Attribute::NoAlias);
+          FuncAttrs.addAttribute("inaccessiblememonly");
+        }
       }
       const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
       const bool IsVirtualCall = MD && MD->isVirtual();
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -2256,7 +2256,7 @@
 def fno_asm : Flag<["-"], "fno-asm">, Group<f_Group>;
 def fno_asynchronous_unwind_tables : Flag<["-"], 
"fno-asynchronous-unwind-tables">, Group<f_Group>;
 def fno_assume_sane_operator_new : Flag<["-"], 
"fno-assume-sane-operator-new">, Group<f_Group>,
-  HelpText<"Don't assume that C++'s global operator new can't alias any 
pointer">,
+  HelpText<"Don't assume that C++'s global operator new can't alias any 
pointer or access accessible memory">,
   Flags<[CC1Option]>, 
MarshallingInfoNegativeFlag<CodeGenOpts<"AssumeSaneOperatorNew">>;
 def fno_builtin : Flag<["-"], "fno-builtin">, Group<f_Group>, 
Flags<[CC1Option, CoreOption]>,
   HelpText<"Disable implicit builtin knowledge of functions">;


Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -2062,11 +2062,14 @@
       AddAttributesFromFunctionProtoType(
           getContext(), FuncAttrs, Fn->getType()->getAs<FunctionProtoType>());
       if (AttrOnCallSite && Fn->isReplaceableGlobalAllocationFunction()) {
-        // A sane operator new returns a non-aliasing pointer.
+        // A sane operator new returns a non-aliasing pointer and is
+        // inaccessiblememonly
         auto Kind = Fn->getDeclName().getCXXOverloadedOperator();
         if (getCodeGenOpts().AssumeSaneOperatorNew &&
-            (Kind == OO_New || Kind == OO_Array_New))
+            (Kind == OO_New || Kind == OO_Array_New)) {
           RetAttrs.addAttribute(llvm::Attribute::NoAlias);
+          FuncAttrs.addAttribute("inaccessiblememonly");
+        }
       }
       const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
       const bool IsVirtualCall = MD && MD->isVirtual();
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -2256,7 +2256,7 @@
 def fno_asm : Flag<["-"], "fno-asm">, Group<f_Group>;
 def fno_asynchronous_unwind_tables : Flag<["-"], "fno-asynchronous-unwind-tables">, Group<f_Group>;
 def fno_assume_sane_operator_new : Flag<["-"], "fno-assume-sane-operator-new">, Group<f_Group>,
-  HelpText<"Don't assume that C++'s global operator new can't alias any pointer">,
+  HelpText<"Don't assume that C++'s global operator new can't alias any pointer or access accessible memory">,
   Flags<[CC1Option]>, MarshallingInfoNegativeFlag<CodeGenOpts<"AssumeSaneOperatorNew">>;
 def fno_builtin : Flag<["-"], "fno-builtin">, Group<f_Group>, Flags<[CC1Option, CoreOption]>,
   HelpText<"Disable implicit builtin knowledge of functions">;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to