Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
[Alignment][NFC] Add a helper function to DataLayout
Browse files Browse the repository at this point in the history
Summary:
This is patch is part of a series 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: courbet

Subscribers: llvm-commits

Tags: #llvm

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@375413 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
gchatelet committed Oct 21, 2019
1 parent a588936 commit d770871
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/llvm/IR/DataLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ class DataLayout {
/// Returns the minimum ABI-required alignment for the specified type.
unsigned getABITypeAlignment(Type *Ty) const;

/// Helper function to return `Alignment` if it's set or the result of
/// `getABITypeAlignment(Ty)`, in any case the result is a valid alignment.
inline Align getValueOrABITypeAlignment(MaybeAlign Alignment,
Type *Ty) const {
return Alignment ? *Alignment : Align(getABITypeAlignment(Ty));
}

/// Returns the minimum ABI-required alignment for an integer type of
/// the specified bitwidth.
Align getABIIntegerTypeAlignment(unsigned BitWidth) const;
Expand Down
12 changes: 12 additions & 0 deletions unittests/IR/DataLayoutTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//===----------------------------------------------------------------------===//

#include "llvm/IR/DataLayout.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Type.h"
#include "gtest/gtest.h"

using namespace llvm;
Expand Down Expand Up @@ -44,4 +46,14 @@ TEST(DataLayoutTest, FunctionPtrAlign) {
EXPECT_EQ(a, c);
}

TEST(DataLayoutTest, ValueOrABITypeAlignment) {
const DataLayout DL("Fi8");
LLVMContext Context;
Type *const FourByteAlignType = Type::getInt32Ty(Context);
EXPECT_EQ(Align(16),
DL.getValueOrABITypeAlignment(MaybeAlign(16), FourByteAlignType));
EXPECT_EQ(Align(4),
DL.getValueOrABITypeAlignment(MaybeAlign(), FourByteAlignType));
}

} // anonymous namespace

0 comments on commit d770871

Please sign in to comment.