[clang] ecc8479 - Look through calls to std::addressof to compute pointer alignment.

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 20 11:30:23 PDT 2022


Author: Eli Friedman
Date: 2022-04-20T11:30:11-07:00
New Revision: ecc8479a01d3bee1c145a0f7990271651db72ab0

URL: https://github.com/llvm/llvm-project/commit/ecc8479a01d3bee1c145a0f7990271651db72ab0
DIFF: https://github.com/llvm/llvm-project/commit/ecc8479a01d3bee1c145a0f7990271651db72ab0.diff

LOG: Look through calls to std::addressof to compute pointer alignment.

This is sort of a followup to D37310; that basically fixed the same
issue, but then the libstdc++ implementation of <atomic> changed. Re-fix
the the issue in essentially the same way: look through the addressof
operation to find the alignment of the underlying object.

Differential Revision: https://reviews.llvm.org/D123950

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExpr.cpp
    clang/test/CodeGenCXX/atomic-align.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 664ca605fc7ea..fbe1586001b19 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -1170,6 +1170,22 @@ Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E,
     }
   }
 
+  // std::addressof and variants.
+  if (auto *Call = dyn_cast<CallExpr>(E)) {
+    switch (Call->getBuiltinCallee()) {
+    default:
+      break;
+    case Builtin::BIaddressof:
+    case Builtin::BI__addressof:
+    case Builtin::BI__builtin_addressof: {
+      LValue LV = EmitLValue(Call->getArg(0));
+      if (BaseInfo) *BaseInfo = LV.getBaseInfo();
+      if (TBAAInfo) *TBAAInfo = LV.getTBAAInfo();
+      return LV.getAddress(*this);
+    }
+    }
+  }
+
   // TODO: conditional operators, comma.
 
   // Otherwise, use the alignment of the type.

diff  --git a/clang/test/CodeGenCXX/atomic-align.cpp b/clang/test/CodeGenCXX/atomic-align.cpp
index 624de9286f528..f6e478e526934 100644
--- a/clang/test/CodeGenCXX/atomic-align.cpp
+++ b/clang/test/CodeGenCXX/atomic-align.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -std=c++11 -emit-llvm -o - -triple=x86_64-linux-gnu | FileCheck %s
+// RUN: %clang_cc1 %s -std=c++14 -emit-llvm -o - -triple=x86_64-linux-gnu | FileCheck %s
 
 struct AM {
   int f1, f2;
@@ -28,3 +28,23 @@ AM load2() {
   __atomic_load(&bm.f2, &am, 0);
   return am;
 }
+
+namespace std {
+  template <class _Tp>
+  inline constexpr
+  __attribute__ ((__visibility__("hidden"), __internal_linkage__))
+  _Tp* __addressof(_Tp& __x) noexcept
+  {
+      return __builtin_addressof(__x);
+  }
+}
+
+AM load3() {
+  AM am;
+  // m is declared to align to 8bytes, so generate load atomic instead
+  // of libcall.
+  // CHECK-LABEL: @_Z5load3v
+  // CHECK: load atomic {{.*}} monotonic, align 8
+  __atomic_load(std::__addressof(m), &am, 0);
+  return am;
+}


        


More information about the cfe-commits mailing list