diff options
author | Lang Hames <lhames@gmail.com> | 2024-02-17 13:21:38 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-17 13:21:38 -0800 |
commit | 80c25daca8f8282ceabd2a5bb567203e0dcf8374 (patch) | |
tree | 36352016b4be295570aafb5ab880f5412e253b99 | |
parent | f81f7a57664bd7dccd5e179ed01155efbc8ff481 (diff) | |
download | llvm-80c25daca8f8282ceabd2a5bb567203e0dcf8374.zip llvm-80c25daca8f8282ceabd2a5bb567203e0dcf8374.tar.gz llvm-80c25daca8f8282ceabd2a5bb567203e0dcf8374.tar.bz2 |
[TargetParser] Expose Triple::getArchPointerBitWidth. (#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.
-rw-r--r-- | llvm/include/llvm/TargetParser/Triple.h | 8 | ||||
-rw-r--r-- | llvm/lib/TargetParser/Triple.cpp | 2 | ||||
-rw-r--r-- | 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 e732070..26c3d2f 100644 --- a/llvm/include/llvm/TargetParser/Triple.h +++ b/llvm/include/llvm/TargetParser/Triple.h @@ -451,6 +451,14 @@ public: /// @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 96dbd5c..3cd897a 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 4db54a0..e1e1bbd 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()); |