From bb8c799e1a9699ef3ef8ee1b6ea7da5ca42102fb Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Sat, 8 Jul 2017 05:16:13 +0000 Subject: [PATCH] [X86] Cleanup some CPUID usage in getAvailableFeatures. We should make sure leaf 1 is available before accessing it. Same with leaf 0x80000001. llvm-svn: 307462 --- llvm/lib/Support/Host.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Support/Host.cpp b/llvm/lib/Support/Host.cpp index 31d43fe..0b5a5c1 100644 --- a/llvm/lib/Support/Host.cpp +++ b/llvm/lib/Support/Host.cpp @@ -987,8 +987,14 @@ static unsigned getAvailableFeatures(unsigned int ECX, unsigned int EDX, Features |= (HasAVX512Save << FEATURE_AVX512SAVE); Features |= (HasADX << FEATURE_ADX); - getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX); - Features |= (((EDX >> 29) & 0x1) << FEATURE_EM64T); + unsigned MaxExtLevel; + getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX); + + bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 && + !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX); + if (HasExtLeaf1) + Features |= (((EDX >> 29) & 0x1) << FEATURE_EM64T); + return Features; } @@ -1004,10 +1010,9 @@ StringRef sys::getHostCPUName() { if(!isCpuIdSupported()) return "generic"; #endif - if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX)) - return "generic"; - if (getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX)) + if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1) return "generic"; + getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX); unsigned Brand_id = EBX & 0xff; unsigned Family = 0, Model = 0; -- 2.7.4