[clang] d9b8d13 - [NFC][Alignment] Use MaybeAlign in CGCleanup/CGExpr

Guillaume Chatelet via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 14 03:57:11 PDT 2022


Author: Guillaume Chatelet
Date: 2022-06-14T10:56:36Z
New Revision: d9b8d13f8b58086e2c8d5c59be6f79222a07931e

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

LOG: [NFC][Alignment] Use MaybeAlign in CGCleanup/CGExpr

Added: 
    

Modified: 
    clang/include/clang/AST/CharUnits.h
    clang/lib/CodeGen/CGCleanup.cpp
    clang/lib/CodeGen/CGExpr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/CharUnits.h b/clang/include/clang/AST/CharUnits.h
index f14d3abf71e5e..2705f24789e4c 100644
--- a/clang/include/clang/AST/CharUnits.h
+++ b/clang/include/clang/AST/CharUnits.h
@@ -182,6 +182,12 @@ namespace clang {
       /// Beware llvm::Align assumes power of two 8-bit bytes.
       llvm::Align getAsAlign() const { return llvm::Align(Quantity); }
 
+      /// getAsMaybeAlign - Returns Quantity as a valid llvm::Align or
+      /// llvm::None, Beware llvm::MaybeAlign assumes power of two 8-bit bytes.
+      llvm::MaybeAlign getAsMaybeAlign() const {
+        return llvm::MaybeAlign(Quantity);
+      }
+
       /// alignTo - Returns the next integer (mod 2**64) that is
       /// greater than or equal to this quantity and is a multiple of \p Align.
       /// Align must be non-zero.

diff  --git a/clang/lib/CodeGen/CGCleanup.cpp b/clang/lib/CodeGen/CGCleanup.cpp
index a10851edfb82c..5035ed34358d2 100644
--- a/clang/lib/CodeGen/CGCleanup.cpp
+++ b/clang/lib/CodeGen/CGCleanup.cpp
@@ -77,7 +77,7 @@ RValue DominatingValue<RValue>::saved_type::restore(CodeGenFunction &CGF) {
   auto getSavingAddress = [&](llvm::Value *value) {
     auto *AI = cast<llvm::AllocaInst>(value);
     return Address(value, AI->getAllocatedType(),
-                   CharUnits::fromQuantity(AI->getAlignment()));
+                   CharUnits::fromQuantity(AI->getAlign().value()));
   };
   switch (K) {
   case ScalarLiteral:

diff  --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 3d7f13aed0aba..cbeb6c938bee7 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -757,23 +757,23 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
     }
   }
 
-  uint64_t AlignVal = 0;
+  llvm::MaybeAlign AlignVal;
   llvm::Value *PtrAsInt = nullptr;
 
   if (SanOpts.has(SanitizerKind::Alignment) &&
       !SkippedChecks.has(SanitizerKind::Alignment)) {
-    AlignVal = Alignment.getQuantity();
+    AlignVal = Alignment.getAsMaybeAlign();
     if (!Ty->isIncompleteType() && !AlignVal)
       AlignVal = CGM.getNaturalTypeAlignment(Ty, nullptr, nullptr,
                                              /*ForPointeeType=*/true)
-                     .getQuantity();
+                     .getAsMaybeAlign();
 
     // The glvalue must be suitably aligned.
-    if (AlignVal > 1 &&
-        (!PtrToAlloca || PtrToAlloca->getAlignment() < AlignVal)) {
+    if (AlignVal && *AlignVal > llvm::Align(1) &&
+        (!PtrToAlloca || PtrToAlloca->getAlign() < *AlignVal)) {
       PtrAsInt = Builder.CreatePtrToInt(Ptr, IntPtrTy);
       llvm::Value *Align = Builder.CreateAnd(
-          PtrAsInt, llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));
+          PtrAsInt, llvm::ConstantInt::get(IntPtrTy, AlignVal->value() - 1));
       llvm::Value *Aligned =
           Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
       if (Aligned != True)
@@ -782,12 +782,9 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
   }
 
   if (Checks.size() > 0) {
-    // Make sure we're not losing information. Alignment needs to be a power of
-    // 2
-    assert(!AlignVal || (uint64_t)1 << llvm::Log2_64(AlignVal) == AlignVal);
     llvm::Constant *StaticData[] = {
         EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(Ty),
-        llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2_64(AlignVal) : 1),
+        llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2(*AlignVal) : 1),
         llvm::ConstantInt::get(Int8Ty, TCK)};
     EmitCheck(Checks, SanitizerHandler::TypeMismatch, StaticData,
               PtrAsInt ? PtrAsInt : Ptr);


        


More information about the cfe-commits mailing list