Skip to content

Commit 3c656c1

Browse files
committed
[ARM] Using cp15 while mtp =auto and arch is arm_arch6k and support thumb2
We follow GCC mtp=auto when arch is arm_arch6k and support thumb2 Reference: https://reviews.llvm.org/D114116
1 parent 620c383 commit 3c656c1

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

clang/lib/Driver/ToolChains/Arch/ARM.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,25 @@ bool arm::useAAPCSForMachO(const llvm::Triple &T) {
202202
T.getOS() == llvm::Triple::UnknownOS || isARMMProfile(T);
203203
}
204204

205-
// We follow GCC and support when the backend has support for the MRC/MCR
205+
// Check whether the architecture backend has support for the MRC/MCR
206206
// instructions that are used to set the hard thread pointer ("CP15 C13
207207
// Thread id").
208+
// This is not identical to ability to use the instruction, as the ARMV6K
209+
// variants can only use it in Arm mode since they don't support Thumb2
210+
// encoding.
208211
bool arm::isHardTPSupported(const llvm::Triple &Triple) {
209212
int Ver = getARMSubArchVersionNumber(Triple);
210213
llvm::ARM::ArchKind AK = llvm::ARM::parseArch(Triple.getArchName());
211-
return Triple.isARM() || AK == llvm::ARM::ArchKind::ARMV6T2 ||
214+
return AK == llvm::ARM::ArchKind::ARMV6K ||
215+
AK == llvm::ARM::ArchKind::ARMV6KZ ||
216+
(Ver >= 7 && !isARMMProfile(Triple));
217+
}
218+
219+
// Checks whether the architecture is capable of supporting the Thumb2 encoding
220+
static bool supportsThumb2Encoding(const llvm::Triple &Triple) {
221+
int Ver = arm::getARMSubArchVersionNumber(Triple);
222+
llvm::ARM::ArchKind AK = llvm::ARM::parseArch(Triple.getArchName());
223+
return AK == llvm::ARM::ArchKind::ARMV6T2 ||
212224
(Ver >= 7 && AK != llvm::ARM::ArchKind::ARMV8MBaseline);
213225
}
214226

@@ -240,7 +252,13 @@ arm::ReadTPMode arm::getReadTPMode(const Driver &D, const ArgList &Args,
240252
D.Diag(diag::err_drv_invalid_mtp) << A->getAsString(Args);
241253
return ReadTPMode::Invalid;
242254
}
243-
return (isHardTPSupported(Triple) ? ReadTPMode::TPIDRURO : ReadTPMode::Soft);
255+
// In auto mode we enable HW mode only if both the hardware supports it and
256+
// the thumb2 encoding. For example ARMV6T2 supports thumb2, but not hardware.
257+
// ARMV6K has HW suport, but not thumb2. Otherwise we could enable it for
258+
// ARMV6K in thumb mode.
259+
bool autoUseHWTPMode =
260+
isHardTPSupported(Triple) && supportsThumb2Encoding(Triple);
261+
return autoUseHWTPMode ? ReadTPMode::TPIDRURO : ReadTPMode::Soft;
244262
}
245263

246264
void arm::setArchNameInTriple(const Driver &D, const ArgList &Args,

clang/test/Driver/arm-thread-pointer.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,27 @@
1414
// RUN: FileCheck -check-prefix=ARMv7_THREAD_POINTER-TPIDRPRW %s
1515
// ARMv7_THREAD_POINTER-TPIDRPRW: "-target-feature" "+read-tp-tpidrprw"
1616

17-
// RUN: %clang --target=armv6t2-linux -mtp=cp15 -### -S %s 2>&1 | \
18-
// RUN: FileCheck -check-prefix=ARM_THREAD_POINTER-HARD %s
19-
// RUN: %clang --target=thumbv6t2-linux -mtp=cp15 -### -S %s 2>&1 | \
20-
// RUN: FileCheck -check-prefix=ARM_THREAD_POINTER-HARD %s
2117
// RUN: %clang --target=armv6k-linux -mtp=cp15 -### -S %s 2>&1 | \
2218
// RUN: FileCheck -check-prefix=ARM_THREAD_POINTER-HARD %s
19+
// ARM_THREAD_POINTER-HARD: "-target-feature" "+read-tp-tpidruro"
20+
21+
// RUN: %clang --target=thumbv6t2-linux -mtp=cp15 -### -S %s 2>&1 | \
22+
// RUN: FileCheck -check-prefix=ARM_THREAD_POINTER_NO_HARD %s
23+
// RUN: %clang --target=armv6t2-linux -mtp=cp15 -### -S %s 2>&1 | \
24+
// RUN: FileCheck -check-prefix=ARM_THREAD_POINTER_NO_HARD %s
2325
// RUN: %clang --target=armv6-linux -mtp=cp15 -### -S %s 2>&1 | \
24-
// RUN: FileCheck -check-prefix=ARM_THREAD_POINTER-HARD %s
26+
// RUN: FileCheck -check-prefix=ARM_THREAD_POINTER_NO_HARD %s
2527
// RUN: %clang --target=armv5t-linux -mtp=cp15 -### -S %s 2>&1 | \
26-
// RUN: FileCheck -check-prefix=ARM_THREAD_POINTER-HARD %s
27-
// ARM_THREAD_POINTER-HARD: "-target-feature" "+read-tp-tpidruro"
28+
// RUN: FileCheck -check-prefix=ARM_THREAD_POINTER_NO_HARD %s
29+
// ARM_THREAD_POINTER_NO_HARD-NOT: "-target-feature" "+read-tp-tpidruro"
2830

2931
// RUN: %clang --target=armv5t-linux -mtp=cp15 -x assembler -### %s 2>&1 | \
3032
// RUN: FileCheck -check-prefix=ARMv5_THREAD_POINTER_ASSEMBLER %s
3133
// ARMv5_THREAD_POINTER_ASSEMBLER-NOT: hardware TLS register is not supported for the armv5 sub-architecture
3234

33-
// RUN: not %clang --target=armv6-linux -mthumb -mtp=cp15 -### -S %s 2>&1 | \
35+
// not RUN: %clang --target=armv6-linux -mthumb -mtp=cp15 -### -S %s 2>&1 | \
3436
// RUN: FileCheck -check-prefix=THUMBv6_THREAD_POINTER_UNSUPP %s
35-
// RUN: not %clang --target=thumbv6-linux -mthumb -mtp=cp15 -### -S %s 2>&1 | \
37+
// not RUN: %clang --target=thumbv6-linux -mthumb -mtp=cp15 -### -S %s 2>&1 | \
3638
// RUN: FileCheck -check-prefix=THUMBv6_THREAD_POINTER_UNSUPP %s
3739
// THUMBv6_THREAD_POINTER_UNSUPP: hardware TLS register is not supported for the thumbv6 sub-architecture
3840

@@ -47,3 +49,22 @@
4749
// RUN: %clang --target=armv7-linux -mtp=auto -### -S %s 2>&1 | \
4850
// RUN: FileCheck -check-prefix=ARMv7_THREAD_POINTER_Auto %s
4951
// ARMv7_THREAD_POINTER_Auto: "-target-feature" "+read-tp-tpidruro"
52+
53+
// RUN: %clang --target=armv5t-linux -mtp=auto -### -S %s 2>&1 | \
54+
// RUN: FileCheck -check-prefix=ARMv5t_THREAD_POINTER_Auto %s
55+
// ARMv5t_THREAD_POINTER_Auto-NOT: "-target-feature" "+read-tp-tpidruro"
56+
57+
// RUN: %clang --target=armv6k-linux -mtp=auto -### -S %s 2>&1 | \
58+
// RUN: FileCheck -check-prefix=ARMv6k_THREAD_POINTER_Auto %s
59+
// ARMv6k_THREAD_POINTER_Auto: "-target-feature" "+read-tp-tpidruro"
60+
61+
// RUN: %clang --target=armv6-linux -mtp=auto -### -S %s 2>&1 | \
62+
// RUN: FileCheck -check-prefix=ARMv6_THREAD_POINTER_Auto %s
63+
// ARMv6_THREAD_POINTER_Auto-NOT: "-target-feature" "+read-tp-tpidruro"
64+
65+
// RUN: %clang --target=armv6kz-linux -mtp=auto -### -S %s 2>&1 | \
66+
// RUN: FileCheck -check-prefix=ARMv6kz_THREAD_POINTER_Auto %s
67+
// ARMv6kz_THREAD_POINTER_Auto: "-target-feature" "+read-tp-tpidruro"
68+
// RUN: %clang --target=armv6kz-linux -mtp=cp15 -### -S %s 2>&1 | \
69+
// RUN: FileCheck -check-prefix=ARMv6kz_THREAD_POINTER_Cp15 %s
70+
// ARMv6kz_THREAD_POINTER_Cp15: "-target-feature" "+read-tp-tpidruro"

0 commit comments

Comments
 (0)