From 79426d1fdcf4576f72d759af2f4fe65cdeee0e08 Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Tue, 6 Aug 2019 09:03:21 +0000 Subject: [PATCH] [LLVM][Alignment] Introduce Alignment In GlobalObject Summary: This is patch is part of a serie to introduce an Alignment type. See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html See this patch for the introduction of the type: https://reviews.llvm.org/D64790 Reviewers: jfb Subscribers: hiraditya, dexonsmith, llvm-commits, courbet Tags: #llvm Differential Revision: https://reviews.llvm.org/D65748 Address comments git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368000 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/GlobalObject.h | 7 ++++++- include/llvm/Support/Alignment.h | 2 +- lib/IR/Globals.cpp | 14 +++++++++----- unittests/IR/ValueTest.cpp | 3 ++- unittests/Support/AlignmentTest.cpp | 3 ++- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/include/llvm/IR/GlobalObject.h b/include/llvm/IR/GlobalObject.h index b8ab6140ebe..a47faac5d41 100644 --- a/include/llvm/IR/GlobalObject.h +++ b/include/llvm/IR/GlobalObject.h @@ -17,6 +17,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/Value.h" +#include "llvm/Support/Alignment.h" #include #include @@ -58,9 +59,13 @@ public: unsigned getAlignment() const { unsigned Data = getGlobalValueSubClassData(); unsigned AlignmentData = Data & AlignmentMask; - return (1u << AlignmentData) >> 1; + MaybeAlign Align = decodeMaybeAlign(AlignmentData); + return Align ? Align->value() : 0; } + + /// FIXME: Remove this setter once the migration to MaybeAlign is over. void setAlignment(unsigned Align); + void setAlignment(MaybeAlign Align); unsigned getGlobalObjectSubClassData() const { unsigned ValueData = getGlobalValueSubClassData(); diff --git a/include/llvm/Support/Alignment.h b/include/llvm/Support/Alignment.h index 2babcda3856..23dec64f80e 100644 --- a/include/llvm/Support/Alignment.h +++ b/include/llvm/Support/Alignment.h @@ -101,7 +101,7 @@ public: explicit MaybeAlign(uint64_t Value) { assert((Value == 0 || llvm::isPowerOf2_64(Value)) && - "Alignment is not 0 or a power of 2"); + "Alignment is neither 0 nor a power of 2"); if (Value) emplace(Value); } diff --git a/lib/IR/Globals.cpp b/lib/IR/Globals.cpp index 8e87b969a1b..cf65d574516 100644 --- a/lib/IR/Globals.cpp +++ b/lib/IR/Globals.cpp @@ -114,13 +114,17 @@ unsigned GlobalValue::getAddressSpace() const { } void GlobalObject::setAlignment(unsigned Align) { - assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); - assert(Align <= MaximumAlignment && - "Alignment is greater than MaximumAlignment!"); - unsigned AlignmentData = Log2_32(Align) + 1; + setAlignment(MaybeAlign(Align)); +} + +void GlobalObject::setAlignment(MaybeAlign Align) { + assert(!Align || Align <= MaximumAlignment && + "Alignment is greater than MaximumAlignment!"); + unsigned AlignmentData = encode(Align); unsigned OldData = getGlobalValueSubClassData(); setGlobalValueSubClassData((OldData & ~AlignmentMask) | AlignmentData); - assert(getAlignment() == Align && "Alignment representation error!"); + assert(MaybeAlign(getAlignment()) == Align && + "Alignment representation error!"); } void GlobalObject::copyAttributesFrom(const GlobalObject *Src) { diff --git a/unittests/IR/ValueTest.cpp b/unittests/IR/ValueTest.cpp index 03523a74e1a..f26046304f5 100644 --- a/unittests/IR/ValueTest.cpp +++ b/unittests/IR/ValueTest.cpp @@ -99,7 +99,8 @@ TEST(GlobalTest, AlignDeath) { Constant::getAllOnesValue(Int32Ty), "var", nullptr, GlobalVariable::NotThreadLocal, 1); - EXPECT_DEATH(Var->setAlignment(536870913U), "Alignment is not a power of 2"); + EXPECT_DEATH(Var->setAlignment(536870913U), + "Alignment is neither 0 nor a power of 2"); EXPECT_DEATH(Var->setAlignment(1073741824U), "Alignment is greater than MaximumAlignment"); } diff --git a/unittests/Support/AlignmentTest.cpp b/unittests/Support/AlignmentTest.cpp index 0e251eb4921..83d6a727721 100644 --- a/unittests/Support/AlignmentTest.cpp +++ b/unittests/Support/AlignmentTest.cpp @@ -256,7 +256,8 @@ TEST(AlignmentDeathTest, InvalidCTors) { EXPECT_DEATH((Align(0)), "Value must not be 0"); for (uint64_t Value : getNonPowerOfTwo()) { EXPECT_DEATH((Align(Value)), "Alignment is not a power of 2"); - EXPECT_DEATH((MaybeAlign(Value)), "Alignment is not 0 or a power of 2"); + EXPECT_DEATH((MaybeAlign(Value)), + "Alignment is neither 0 nor a power of 2"); } } -- 2.11.4.GIT