[llvm] [TargetParser] Expose Triple::getArchPointerBitWidth. (PR #82086)
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 16 17:18:07 PST 2024
https://github.com/lhames created https://github.com/llvm/llvm-project/pull/82086
The getArchPointerBitWidth method provides direct access to the pointer size for arithmetic or switch statements, instead of requiring clients to use the isArch16Bit / isArch32Bit / isArch64Bit predicates.
>From 6547722ded0a8798bdd0a8667a92a0ecabb36734 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Fri, 16 Feb 2024 14:58:57 -0800
Subject: [PATCH] [TargetParser] Expose Triple::getArchPointerBitWidth.
The getArchPointerBitWidth method provides direct access to the pointer size
for arithmetic or switch statements, instead of requiring clients to use the
isArch16Bit / isArch32Bit / isArch64Bit predicates.
---
llvm/include/llvm/TargetParser/Triple.h | 8 ++++++++
llvm/lib/TargetParser/Triple.cpp | 2 +-
llvm/unittests/TargetParser/TripleTest.cpp | 18 +++++++++++++++++-
3 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index e73207021392ec..26c3d2f67913a5 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -451,6 +451,14 @@ class Triple {
/// @name Convenience Predicates
/// @{
+ /// Returns the pointer width of this architecture.
+ static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch);
+
+ /// Returns the pointer width of this architecture.
+ unsigned getArchPointerBitWidth() const {
+ return getArchPointerBitWidth(getArch());
+ }
+
/// Test whether the architecture is 64-bit
///
/// Note that this tests for 64-bit pointer width, and nothing else. Note
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 96dbd5ca673b76..3cd897aeb5970a 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -1458,7 +1458,7 @@ void Triple::setOSAndEnvironmentName(StringRef Str) {
setTriple(getArchName() + "-" + getVendorName() + "-" + Str);
}
-static unsigned getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
+unsigned Triple::getArchPointerBitWidth(llvm::Triple::ArchType Arch) {
switch (Arch) {
case llvm::Triple::UnknownArch:
return 0;
diff --git a/llvm/unittests/TargetParser/TripleTest.cpp b/llvm/unittests/TargetParser/TripleTest.cpp
index 4db54a08c0f639..e1e1bbd76ecda6 100644
--- a/llvm/unittests/TargetParser/TripleTest.cpp
+++ b/llvm/unittests/TargetParser/TripleTest.cpp
@@ -1205,86 +1205,102 @@ TEST(TripleTest, MutateName) {
EXPECT_EQ("i386-pc-darwin", T.getTriple());
}
-TEST(TripleTest, BitWidthPredicates) {
+TEST(TripleTest, BitWidthChecks) {
Triple T;
EXPECT_FALSE(T.isArch16Bit());
EXPECT_FALSE(T.isArch32Bit());
EXPECT_FALSE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 0U);
T.setArch(Triple::arm);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_TRUE(T.isArch32Bit());
EXPECT_FALSE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 32U);
T.setArch(Triple::hexagon);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_TRUE(T.isArch32Bit());
EXPECT_FALSE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 32U);
T.setArch(Triple::mips);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_TRUE(T.isArch32Bit());
EXPECT_FALSE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 32U);
T.setArch(Triple::mips64);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_FALSE(T.isArch32Bit());
EXPECT_TRUE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 64U);
T.setArch(Triple::msp430);
EXPECT_TRUE(T.isArch16Bit());
EXPECT_FALSE(T.isArch32Bit());
EXPECT_FALSE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 16U);
T.setArch(Triple::ppc);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_TRUE(T.isArch32Bit());
EXPECT_FALSE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 32U);
T.setArch(Triple::ppc64);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_FALSE(T.isArch32Bit());
EXPECT_TRUE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 64U);
T.setArch(Triple::x86);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_TRUE(T.isArch32Bit());
EXPECT_FALSE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 32U);
T.setArch(Triple::x86_64);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_FALSE(T.isArch32Bit());
EXPECT_TRUE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 64U);
T.setArch(Triple::amdil);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_TRUE(T.isArch32Bit());
EXPECT_FALSE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 32U);
T.setArch(Triple::amdil64);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_FALSE(T.isArch32Bit());
EXPECT_TRUE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 64U);
T.setArch(Triple::hsail);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_TRUE(T.isArch32Bit());
EXPECT_FALSE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 32U);
T.setArch(Triple::hsail64);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_FALSE(T.isArch32Bit());
EXPECT_TRUE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 64U);
T.setArch(Triple::spir);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_TRUE(T.isArch32Bit());
EXPECT_FALSE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 32U);
T.setArch(Triple::spir64);
EXPECT_FALSE(T.isArch16Bit());
EXPECT_FALSE(T.isArch32Bit());
EXPECT_TRUE(T.isArch64Bit());
+ EXPECT_EQ(T.getArchPointerBitWidth(), 64U);
T.setArch(Triple::spirv);
EXPECT_FALSE(T.isArch16Bit());
More information about the llvm-commits
mailing list