[clang] [llvm] [AIX] support builtin_cpu_is() for aix (PR #80069)

zhijian lin via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 12 10:24:38 PST 2024


https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/80069

>From a82a60e3af041f50b833d241a284e09a883e849a Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Tue, 30 Jan 2024 15:13:39 -0500
Subject: [PATCH 1/2] support builtin_cpu_is() for aix

---
 .../clang/Basic/DiagnosticSemaKinds.td        |  2 +
 clang/lib/Basic/Targets/PPC.cpp               | 13 ++-
 clang/lib/Basic/Targets/PPC.h                 | 12 ++-
 clang/lib/CodeGen/CGBuiltin.cpp               | 57 ++++++++++++-
 clang/lib/Sema/SemaChecking.cpp               |  5 +-
 clang/test/CodeGen/aix-builtin-cpu-is.c       | 83 +++++++++++++++++++
 clang/test/Sema/aix-builtin-cpu-unsupports.c  |  6 ++
 .../llvm/TargetParser/PPCTargetParser.def     | 53 ++++++++++++
 8 files changed, 225 insertions(+), 6 deletions(-)
 create mode 100644 clang/test/CodeGen/aix-builtin-cpu-is.c
 create mode 100644 clang/test/Sema/aix-builtin-cpu-unsupports.c

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1c0ebbe4e68343..139a90a3020684 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10347,6 +10347,8 @@ def err_x86_builtin_tile_arg_duplicate : Error<
 
 def err_builtin_target_unsupported : Error<
   "builtin is not supported on this target">;
+def err_builtin_aix_os_unsupported : Error<
+  "this builtin is available only in AIX 7.2 and later operating systems">;
 def err_builtin_longjmp_unsupported : Error<
   "__builtin_longjmp is not supported for the current target">;
 def err_builtin_setjmp_unsupported : Error<
diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp
index 8c891ccdeb59d4..c9cf7435334402 100644
--- a/clang/lib/Basic/Targets/PPC.cpp
+++ b/clang/lib/Basic/Targets/PPC.cpp
@@ -904,8 +904,17 @@ bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const {
 }
 
 bool PPCTargetInfo::validateCpuIs(StringRef CPUName) const {
+  llvm::Triple Triple = getTriple();
+  if (Triple.isOSLinux()) {
 #define PPC_LNX_CPU(NAME, NUM) .Case(NAME, true)
-  return llvm::StringSwitch<bool>(CPUName)
+    return llvm::StringSwitch<bool>(CPUName)
 #include "llvm/TargetParser/PPCTargetParser.def"
-      .Default(false);
+        .Default(false);
+  } else if (Triple.isOSAIX()) {
+#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, MASK, OP, VALUE) .Case(NAME, true)
+    return llvm::StringSwitch<bool>(CPUName)
+#include "llvm/TargetParser/PPCTargetParser.def"
+        .Default(false);
+  }
+  return false;
 }
diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h
index a91bdede53e40d..5f92c80d15c0fa 100644
--- a/clang/lib/Basic/Targets/PPC.h
+++ b/clang/lib/Basic/Targets/PPC.h
@@ -362,8 +362,18 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo {
 
   // We support __builtin_cpu_supports/__builtin_cpu_is on targets that
   // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv.
+#define MINIMUM_AIX_OS_MAJOR 7
+#define MINIMUM_AIX_OS_MINOR 2
   bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); }
-  bool supportsCpuIs() const override { return getTriple().isOSGlibc(); }
+  bool supportsCpuIs() const override {
+    llvm::Triple Tri = getTriple();
+    // The AIX7.2 is mininum requirement to support __builtin_cpu_is().
+    return Tri.isOSGlibc() ||
+           (Tri.isOSAIX() &&
+            !Tri.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR));
+  }
+#undef MINIMUM_AIX_OS_MAJOR
+#undef MINIMUM_AIX_OS_MINOR
   bool validateCpuSupports(StringRef Feature) const override;
   bool validateCpuIs(StringRef Name) const override;
 };
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index f3ab5ad7b08ec8..bab5b144275fd8 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -16542,22 +16542,75 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
 
   Intrinsic::ID ID = Intrinsic::not_intrinsic;
 
+#include "llvm/TargetParser/PPCTargetParser.def"
+  auto GetOpRes = [&](Value *FieldValue, unsigned Mask, unsigned Op,
+                      unsigned Op_Value) -> Value * {
+    Value *Value1 = FieldValue;
+    if (Mask)
+      Value1 = Builder.CreateAnd(Value1, Mask);
+    assert((Op == OP_EQ) && "Only support equal comparision");
+    return Builder.CreateICmp(ICmpInst::ICMP_EQ, FieldValue,
+                              ConstantInt::get(Int32Ty, Op_Value));
+  };
+
+  auto ConvBuiltinCpu = [&](unsigned SupportOP, unsigned FieldIdx,
+                            unsigned Op_Mask, unsigned Op,
+                            unsigned Op_Value) -> Value * {
+    if (SupportOP == AIX_BUILTIN_PPC_FALSE)
+      return llvm::ConstantInt::getFalse(ConvertType(E->getType()));
+
+    if (SupportOP == AIX_BUILTIN_PPC_TRUE)
+      return llvm::ConstantInt::getTrue(ConvertType(E->getType()));
+
+    assert(SupportOP <= COMP_OP && "Invalid value for SupportOP.");
+    llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE);
+
+    llvm::Constant *SysConf =
+        CGM.CreateRuntimeVariable(STy, "_system_configuration");
+
+    // Grab the appropriate field from _system_configuration.
+    llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0),
+                           ConstantInt::get(Int32Ty, FieldIdx)};
+
+    llvm::Value *FieldValue = Builder.CreateGEP(STy, SysConf, Idxs);
+    FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue,
+                                           CharUnits::fromQuantity(4));
+
+    return GetOpRes(FieldValue, Op_Mask, Op, Op_Value);
+  };
+
   switch (BuiltinID) {
   default: return nullptr;
 
   case Builtin::BI__builtin_cpu_is: {
     const Expr *CPUExpr = E->getArg(0)->IgnoreParenCasts();
     StringRef CPUStr = cast<clang::StringLiteral>(CPUExpr)->getString();
-    unsigned NumCPUID = StringSwitch<unsigned>(CPUStr)
+    llvm::Triple Triple = getTarget().getTriple();
+    if (Triple.isOSLinux()) {
+      unsigned NumCPUID = StringSwitch<unsigned>(CPUStr)
 #define PPC_LNX_CPU(Name, NumericID) .Case(Name, NumericID)
 #include "llvm/TargetParser/PPCTargetParser.def"
-                            .Default(-1U);
+                              .Default(-1U);
     assert(NumCPUID < -1U && "Invalid CPU name. Missed by SemaChecking?");
     Value *Op0 = llvm::ConstantInt::get(Int32Ty, PPC_FAWORD_CPUID);
     llvm::Function *F = CGM.getIntrinsic(Intrinsic::ppc_fixed_addr_ld);
     Value *TheCall = Builder.CreateCall(F, {Op0}, "cpu_is");
     return Builder.CreateICmpEQ(TheCall,
                                 llvm::ConstantInt::get(Int32Ty, NumCPUID));
+    } else if (Triple.isOSAIX()) {
+      unsigned IsCpuSupport, FieldIdx, CpuIdMask, CpuIdOp, CpuIdValue;
+      typedef std::tuple<unsigned, unsigned, unsigned, unsigned, unsigned>
+          CPUType;
+      std::tie(IsCpuSupport, FieldIdx, CpuIdMask, CpuIdOp, CpuIdValue) =
+          static_cast<CPUType>(StringSwitch<CPUType>(CPUStr)
+#define PPC_AIX_CPU(NAME, SUPPORT, MASK, INDEX, OP, VALUE)                     \
+  .Case(NAME, {SUPPORT, MASK, INDEX, OP, VALUE})
+#include "llvm/TargetParser/PPCTargetParser.def"
+          );
+      return ConvBuiltinCpu(IsCpuSupport, FieldIdx, CpuIdMask, CpuIdOp,
+                            CpuIdValue);
+    }
+    LLVM_FALLTHROUGH;
   }
   case Builtin::BI__builtin_cpu_supports: {
     unsigned FeatureWord;
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 502b24bcdf8b42..a2d3e8133a449e 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2165,7 +2165,10 @@ static bool SemaBuiltinCpu(Sema &S, const TargetInfo &TI, CallExpr *TheCall,
     return S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported)
            << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
   if (!IsCPUSupports && !TheTI->supportsCpuIs())
-    return S.Diag(TheCall->getBeginLoc(), diag::err_builtin_target_unsupported)
+    return S.Diag(TheCall->getBeginLoc(),
+                  TI.getTriple().isOSAIX()
+                      ? diag::err_builtin_aix_os_unsupported
+                      : diag::err_builtin_target_unsupported)
            << SourceRange(TheCall->getBeginLoc(), TheCall->getEndLoc());
 
   Expr *Arg = TheCall->getArg(0)->IgnoreParenImpCasts();
diff --git a/clang/test/CodeGen/aix-builtin-cpu-is.c b/clang/test/CodeGen/aix-builtin-cpu-is.c
new file mode 100644
index 00000000000000..87864ba5e2ef01
--- /dev/null
+++ b/clang/test/CodeGen/aix-builtin-cpu-is.c
@@ -0,0 +1,83 @@
+// RUN: echo "int main() { return __builtin_cpu_is(\"ppc970\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefix=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"ppc-cell-be\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefix=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"ppca2\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefix=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"ppc405\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefix=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"ppc440\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefix=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"ppc464\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefix=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"ppc476\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefix=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"power4\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefixes=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"power5\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefixes=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"power5+\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefixes=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"power6\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefixes=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"power6x\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
+// RUN:   --check-prefixes=CHECKBOOL
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"power7\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s -DVALUE=32768 \
+// RUN:   --check-prefixes=CHECKOP
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"power8\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s -DVALUE=65536 \
+// RUN:   --check-prefixes=CHECKOP
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"power9\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s -DVALUE=131072\
+// RUN:   --check-prefixes=CHECKOP
+
+// RUN: echo "int main() { return __builtin_cpu_is(\"power10\");}" > %t.c 
+// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s -DVALUE=262144 \
+// RUN:   --check-prefixes=CHECKOP
+
+// CHECKBOOL:     define i32 @main() #0 {
+// CHECKBOOL-NEXT: entry:
+// CHECKBOOL-NEXT:   %retval = alloca i32, align 4
+// CHECKBOOL-NEXT:   store i32 0, ptr %retval, align 4
+// CHECKBOOL-NEXT:   ret i32 0 
+// CHECKBOOL-NEXT: }
+
+// CHECKOP: @_system_configuration = external global { i32, i32, i32 }
+// CHECKOP:   define i32 @main() #0 {
+// CHECKOP-NEXT: entry:
+// CHECKOP-NEXT:   %retval = alloca i32, align 4
+// CHECKOP-NEXT:   store i32 0, ptr %retval, align 4
+// CHECKOP-NEXT:   %0 = load i32, ptr getelementptr inbounds ({ i32, i32, i32 }, ptr @_system_configuration, i32 0, i32 1), align 4
+// CHECKOP-NEXT:   %1 = icmp eq i32 %0, [[VALUE]] 
+// CHECKOP-NEXT:  %conv = zext i1 %1 to i32
+// CHECKOP-NEXT:   ret i32 %conv
+// CHECKOP-NEXT: }
+
+
diff --git a/clang/test/Sema/aix-builtin-cpu-unsupports.c b/clang/test/Sema/aix-builtin-cpu-unsupports.c
new file mode 100644
index 00000000000000..2f07ac0f02e2ff
--- /dev/null
+++ b/clang/test/Sema/aix-builtin-cpu-unsupports.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fsyntax-only -triple  powerpc-ibm-aix7.1.0.0 -verify %s
+
+int main(void) {
+  if (__builtin_cpu_is("power8")) // expected-error {{this builtin is available only in AIX 7.2 and later operating systems}}
+    return 1;
+}
diff --git a/llvm/include/llvm/TargetParser/PPCTargetParser.def b/llvm/include/llvm/TargetParser/PPCTargetParser.def
index f2c44b46fa6730..d4f10b4d1fe9e1 100644
--- a/llvm/include/llvm/TargetParser/PPCTargetParser.def
+++ b/llvm/include/llvm/TargetParser/PPCTargetParser.def
@@ -126,4 +126,57 @@ PPC_LNX_CPU("power10",47)
 #undef PPC_LNX_DEFINE_OFFSETS
 #undef PPC_LNX_FEATURE
 #undef PPC_LNX_CPU
+
+// Definition of following value are found in the AIX header file <systemcfg.h>
+#ifndef AIX_POWERPC_SYS_CONF
+#define AIX_POWERPC_SYS_CONF
+#define AIX_SYSCON_IMPL_IDX 1
+#define AIX_PPC7_VALUE 0x00008000
+#define AIX_PPC8_VALUE 0x00010000
+#define AIX_PPC9_VALUE 0x00020000
+#define AIX_PPC10_VALUE 0x00040000
+
+#define AIX_BUILTIN_PPC_TRUE 1
+#define AIX_BUILTIN_PPC_FALSE 0
+#define COMP_OP 2
+
+#define OP_EQ  0
+
+#endif
+
+//The value of SUPPORT is COMP_OP, it means the feature depend on the V(INDEX)&MASK OP VALUE
+//If the value of MASK is zero, it means we do not need to do mask, just check V(INDEX) OP VALUE.
+
+#ifndef PPC_AIX_CPU
+#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, MASK, OP, VALUE)
+#endif
+
+//Since the __builtin_cpu_is() is supported only on AIX7.2 or AIX7.2 later OS.
+//And AIX 7.2 is supported only on IBM POWER 7 and later processors.
+//The __builtin_cpu_is() function returns false for other CPUs which are not Power7 and Power7 up.
+
+PPC_AIX_CPU("power4",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("ppc970",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("power5",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("power5+",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("power6",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("ppc-cell-be",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("power6x",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("power7",COMP_OP,AIX_SYSCON_IMPL_IDX,0,OP_EQ,AIX_PPC7_VALUE)
+PPC_AIX_CPU("ppca2",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("ppc405",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("ppc440",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("ppc464",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("ppc476",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
+PPC_AIX_CPU("power8",COMP_OP,AIX_SYSCON_IMPL_IDX,0,OP_EQ,AIX_PPC8_VALUE)
+PPC_AIX_CPU("power9",COMP_OP,AIX_SYSCON_IMPL_IDX,0,OP_EQ,AIX_PPC9_VALUE)
+PPC_AIX_CPU("power10",COMP_OP,AIX_SYSCON_IMPL_IDX,0,OP_EQ,AIX_PPC10_VALUE)
+#undef PPC_AIX_CPU
+
+#ifndef PPC_SYSTEMCONFIG_TYPE
+#define PPC_SYSTEMCONFIG_TYPE \
+Int32Ty, Int32Ty, Int32Ty
+#endif
+
+
 #endif // !PPC_TGT_PARSER_UNDEF_MACROS

>From 6dfd52865878e9dc46060e3557e3e4125bc1c202 Mon Sep 17 00:00:00 2001
From: zhijian <zhijian at ca.ibm.com>
Date: Mon, 12 Feb 2024 13:24:27 -0500
Subject: [PATCH 2/2] address comment

---
 .../clang/Basic/DiagnosticSemaKinds.td        |  2 +-
 clang/lib/Basic/Targets/PPC.cpp               | 10 +--
 clang/lib/Basic/Targets/PPC.h                 | 16 ++---
 clang/lib/CodeGen/CGBuiltin.cpp               | 67 +++++++++----------
 clang/test/CodeGen/aix-builtin-cpu-is.c       | 36 +++++-----
 clang/test/Sema/aix-builtin-cpu-unsupports.c  |  2 +-
 .../llvm/TargetParser/PPCTargetParser.def     | 64 +++++++++---------
 7 files changed, 96 insertions(+), 101 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 139a90a3020684..2176854568042a 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10348,7 +10348,7 @@ def err_x86_builtin_tile_arg_duplicate : Error<
 def err_builtin_target_unsupported : Error<
   "builtin is not supported on this target">;
 def err_builtin_aix_os_unsupported : Error<
-  "this builtin is available only in AIX 7.2 and later operating systems">;
+  "this builtin is available only on AIX 7.2 and later operating systems">;
 def err_builtin_longjmp_unsupported : Error<
   "__builtin_longjmp is not supported for the current target">;
 def err_builtin_setjmp_unsupported : Error<
diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp
index c9cf7435334402..1aff5aa5274575 100644
--- a/clang/lib/Basic/Targets/PPC.cpp
+++ b/clang/lib/Basic/Targets/PPC.cpp
@@ -905,13 +905,15 @@ bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const {
 
 bool PPCTargetInfo::validateCpuIs(StringRef CPUName) const {
   llvm::Triple Triple = getTriple();
-  if (Triple.isOSLinux()) {
-#define PPC_LNX_CPU(NAME, NUM) .Case(NAME, true)
+  if (Triple.isOSAIX()) {
+#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, OP, VALUE) .Case(NAME, true)
     return llvm::StringSwitch<bool>(CPUName)
 #include "llvm/TargetParser/PPCTargetParser.def"
         .Default(false);
-  } else if (Triple.isOSAIX()) {
-#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, MASK, OP, VALUE) .Case(NAME, true)
+  }
+
+  if (Triple.isOSLinux()) {
+#define PPC_LNX_CPU(NAME, NUM) .Case(NAME, true)
     return llvm::StringSwitch<bool>(CPUName)
 #include "llvm/TargetParser/PPCTargetParser.def"
         .Default(false);
diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h
index 5f92c80d15c0fa..70683916a8b04f 100644
--- a/clang/lib/Basic/Targets/PPC.h
+++ b/clang/lib/Basic/Targets/PPC.h
@@ -362,18 +362,16 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo {
 
   // We support __builtin_cpu_supports/__builtin_cpu_is on targets that
   // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv.
-#define MINIMUM_AIX_OS_MAJOR 7
-#define MINIMUM_AIX_OS_MINOR 2
+  static constexpr int MINIMUM_AIX_OS_MAJOR = 7;
+  static constexpr int MINIMUM_AIX_OS_MINOR = 2;
   bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); }
   bool supportsCpuIs() const override {
-    llvm::Triple Tri = getTriple();
-    // The AIX7.2 is mininum requirement to support __builtin_cpu_is().
-    return Tri.isOSGlibc() ||
-           (Tri.isOSAIX() &&
-            !Tri.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR));
+    llvm::Triple Triple = getTriple();
+    // AIX 7.2 is the minimum requirement to support __builtin_cpu_is().
+    return Triple.isOSGlibc() ||
+           (Triple.isOSAIX() &&
+            !Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR));
   }
-#undef MINIMUM_AIX_OS_MAJOR
-#undef MINIMUM_AIX_OS_MINOR
   bool validateCpuSupports(StringRef Feature) const override;
   bool validateCpuIs(StringRef Name) const override;
 };
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index bab5b144275fd8..a7afbdce58b167 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -16542,27 +16542,20 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
 
   Intrinsic::ID ID = Intrinsic::not_intrinsic;
 
-#include "llvm/TargetParser/PPCTargetParser.def"
-  auto GetOpRes = [&](Value *FieldValue, unsigned Mask, unsigned Op,
-                      unsigned Op_Value) -> Value * {
-    Value *Value1 = FieldValue;
-    if (Mask)
-      Value1 = Builder.CreateAnd(Value1, Mask);
-    assert((Op == OP_EQ) && "Only support equal comparision");
-    return Builder.CreateICmp(ICmpInst::ICMP_EQ, FieldValue,
-                              ConstantInt::get(Int32Ty, Op_Value));
-  };
+  // The lambda function converts builtin_cpu_is function into directly
+  // returning false or true, or it gets and checks the information from the
+  // kernel variable _system_configuration for AIX OS.
 
-  auto ConvBuiltinCpu = [&](unsigned SupportOP, unsigned FieldIdx,
-                            unsigned Op_Mask, unsigned Op,
-                            unsigned Op_Value) -> Value * {
-    if (SupportOP == AIX_BUILTIN_PPC_FALSE)
+#include "llvm/TargetParser/PPCTargetParser.def"
+  auto ConvBuiltinCpu = [&](unsigned SupportMagic, unsigned FieldIdx,
+                            unsigned CompOp, unsigned OpValue) -> Value * {
+    if (SupportMagic == AIX_BUILTIN_PPC_FALSE)
       return llvm::ConstantInt::getFalse(ConvertType(E->getType()));
 
-    if (SupportOP == AIX_BUILTIN_PPC_TRUE)
+    if (SupportMagic == AIX_BUILTIN_PPC_TRUE)
       return llvm::ConstantInt::getTrue(ConvertType(E->getType()));
 
-    assert(SupportOP <= COMP_OP && "Invalid value for SupportOP.");
+    assert(SupportMagic <= SYS_CONF && "Invalid value for SupportMagic.");
     llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE);
 
     llvm::Constant *SysConf =
@@ -16576,7 +16569,13 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
     FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue,
                                            CharUnits::fromQuantity(4));
 
-    return GetOpRes(FieldValue, Op_Mask, Op, Op_Value);
+    assert((CompOp == COMP_EQ) && "Only equal comparisons are supported!");
+
+    assert(FieldValue->getType()->isIntegerTy(32) &&
+           "Only supports 32-bit integer in the GetOpRes.");
+
+    return Builder.CreateICmp(ICmpInst::ICMP_EQ, FieldValue,
+                              ConstantInt::get(Int32Ty, OpValue));
   };
 
   switch (BuiltinID) {
@@ -16586,31 +16585,31 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
     const Expr *CPUExpr = E->getArg(0)->IgnoreParenCasts();
     StringRef CPUStr = cast<clang::StringLiteral>(CPUExpr)->getString();
     llvm::Triple Triple = getTarget().getTriple();
-    if (Triple.isOSLinux()) {
-      unsigned NumCPUID = StringSwitch<unsigned>(CPUStr)
+
+    if (Triple.isOSAIX()) {
+      unsigned IsCpuSupport, FieldIdx, CompareOp, CpuIdValue;
+      typedef std::tuple<unsigned, unsigned, unsigned, unsigned> CPUType;
+      std::tie(IsCpuSupport, FieldIdx, CompareOp, CpuIdValue) =
+          static_cast<CPUType>(StringSwitch<CPUType>(CPUStr)
+#define PPC_AIX_CPU(NAME, SUPPORT_MAGIC, INDEX, COMPARE_OP, VALUE)             \
+  .Case(NAME, {SUPPORT_MAGIC, INDEX, COMPARE_OP, VALUE})
+#include "llvm/TargetParser/PPCTargetParser.def"
+          );
+      return ConvBuiltinCpu(IsCpuSupport, FieldIdx, CompareOp, CpuIdValue);
+    }
+
+    assert(Triple.isOSLinux() && "Triple for AIX OS has already been checked; "
+                                 "it must be Linux OS here.");
+    unsigned NumCPUID = StringSwitch<unsigned>(CPUStr)
 #define PPC_LNX_CPU(Name, NumericID) .Case(Name, NumericID)
 #include "llvm/TargetParser/PPCTargetParser.def"
-                              .Default(-1U);
+                            .Default(-1U);
     assert(NumCPUID < -1U && "Invalid CPU name. Missed by SemaChecking?");
     Value *Op0 = llvm::ConstantInt::get(Int32Ty, PPC_FAWORD_CPUID);
     llvm::Function *F = CGM.getIntrinsic(Intrinsic::ppc_fixed_addr_ld);
     Value *TheCall = Builder.CreateCall(F, {Op0}, "cpu_is");
     return Builder.CreateICmpEQ(TheCall,
                                 llvm::ConstantInt::get(Int32Ty, NumCPUID));
-    } else if (Triple.isOSAIX()) {
-      unsigned IsCpuSupport, FieldIdx, CpuIdMask, CpuIdOp, CpuIdValue;
-      typedef std::tuple<unsigned, unsigned, unsigned, unsigned, unsigned>
-          CPUType;
-      std::tie(IsCpuSupport, FieldIdx, CpuIdMask, CpuIdOp, CpuIdValue) =
-          static_cast<CPUType>(StringSwitch<CPUType>(CPUStr)
-#define PPC_AIX_CPU(NAME, SUPPORT, MASK, INDEX, OP, VALUE)                     \
-  .Case(NAME, {SUPPORT, MASK, INDEX, OP, VALUE})
-#include "llvm/TargetParser/PPCTargetParser.def"
-          );
-      return ConvBuiltinCpu(IsCpuSupport, FieldIdx, CpuIdMask, CpuIdOp,
-                            CpuIdValue);
-    }
-    LLVM_FALLTHROUGH;
   }
   case Builtin::BI__builtin_cpu_supports: {
     unsigned FeatureWord;
diff --git a/clang/test/CodeGen/aix-builtin-cpu-is.c b/clang/test/CodeGen/aix-builtin-cpu-is.c
index 87864ba5e2ef01..97ce5d958e67f6 100644
--- a/clang/test/CodeGen/aix-builtin-cpu-is.c
+++ b/clang/test/CodeGen/aix-builtin-cpu-is.c
@@ -1,50 +1,50 @@
 // RUN: echo "int main() { return __builtin_cpu_is(\"ppc970\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefix=CHECKBOOL
+// RUN:   --check-prefix=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"ppc-cell-be\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefix=CHECKBOOL
+// RUN:   --check-prefix=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"ppca2\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefix=CHECKBOOL
+// RUN:   --check-prefix=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"ppc405\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefix=CHECKBOOL
+// RUN:   --check-prefix=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"ppc440\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefix=CHECKBOOL
+// RUN:   --check-prefix=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"ppc464\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefix=CHECKBOOL
+// RUN:   --check-prefix=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"ppc476\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefix=CHECKBOOL
+// RUN:   --check-prefix=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"power4\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefixes=CHECKBOOL
+// RUN:   --check-prefixes=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"power5\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefixes=CHECKBOOL
+// RUN:   --check-prefixes=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"power5+\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefixes=CHECKBOOL
+// RUN:   --check-prefixes=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"power6\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefixes=CHECKBOOL
+// RUN:   --check-prefixes=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"power6x\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s \
-// RUN:   --check-prefixes=CHECKBOOL
+// RUN:   --check-prefixes=DIRECT-RET
 
 // RUN: echo "int main() { return __builtin_cpu_is(\"power7\");}" > %t.c 
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s -DVALUE=32768 \
@@ -62,12 +62,12 @@
 // RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %t.c | FileCheck %s -DVALUE=262144 \
 // RUN:   --check-prefixes=CHECKOP
 
-// CHECKBOOL:     define i32 @main() #0 {
-// CHECKBOOL-NEXT: entry:
-// CHECKBOOL-NEXT:   %retval = alloca i32, align 4
-// CHECKBOOL-NEXT:   store i32 0, ptr %retval, align 4
-// CHECKBOOL-NEXT:   ret i32 0 
-// CHECKBOOL-NEXT: }
+// DIRECT-RET:     define i32 @main() #0 {
+// DIRECT-RET-NEXT: entry:
+// DIRECT-RET-NEXT:   %retval = alloca i32, align 4
+// DIRECT-RET-NEXT:   store i32 0, ptr %retval, align 4
+// DIRECT-RET-NEXT:   ret i32 0 
+// DIRECT-RET-NEXT: }
 
 // CHECKOP: @_system_configuration = external global { i32, i32, i32 }
 // CHECKOP:   define i32 @main() #0 {
diff --git a/clang/test/Sema/aix-builtin-cpu-unsupports.c b/clang/test/Sema/aix-builtin-cpu-unsupports.c
index 2f07ac0f02e2ff..10e21867c39373 100644
--- a/clang/test/Sema/aix-builtin-cpu-unsupports.c
+++ b/clang/test/Sema/aix-builtin-cpu-unsupports.c
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -fsyntax-only -triple  powerpc-ibm-aix7.1.0.0 -verify %s
 
 int main(void) {
-  if (__builtin_cpu_is("power8")) // expected-error {{this builtin is available only in AIX 7.2 and later operating systems}}
+  if (__builtin_cpu_is("power8")) // expected-error {{this builtin is available only on AIX 7.2 and later operating systems}}
     return 1;
 }
diff --git a/llvm/include/llvm/TargetParser/PPCTargetParser.def b/llvm/include/llvm/TargetParser/PPCTargetParser.def
index d4f10b4d1fe9e1..fc6f2956a79b66 100644
--- a/llvm/include/llvm/TargetParser/PPCTargetParser.def
+++ b/llvm/include/llvm/TargetParser/PPCTargetParser.def
@@ -127,50 +127,46 @@ PPC_LNX_CPU("power10",47)
 #undef PPC_LNX_FEATURE
 #undef PPC_LNX_CPU
 
-// Definition of following value are found in the AIX header file <systemcfg.h>
+// Definition of the following values are found in the AIX header file: <systemcfg.h>.
 #ifndef AIX_POWERPC_SYS_CONF
 #define AIX_POWERPC_SYS_CONF
-#define AIX_SYSCON_IMPL_IDX 1
-#define AIX_PPC7_VALUE 0x00008000
-#define AIX_PPC8_VALUE 0x00010000
-#define AIX_PPC9_VALUE 0x00020000
-#define AIX_PPC10_VALUE 0x00040000
+  #define AIX_SYSCON_IMPL_IDX 1
+  #define AIX_PPC7_VALUE 0x00008000
+  #define AIX_PPC8_VALUE 0x00010000
+  #define AIX_PPC9_VALUE 0x00020000
+  #define AIX_PPC10_VALUE 0x00040000
 
-#define AIX_BUILTIN_PPC_TRUE 1
-#define AIX_BUILTIN_PPC_FALSE 0
-#define COMP_OP 2
+  #define AIX_BUILTIN_PPC_TRUE 1
+  #define AIX_BUILTIN_PPC_FALSE 0
+  #define SYS_CONF 2
 
-#define OP_EQ  0
+  #define COMP_EQ  0
 
 #endif
 
-//The value of SUPPORT is COMP_OP, it means the feature depend on the V(INDEX)&MASK OP VALUE
-//If the value of MASK is zero, it means we do not need to do mask, just check V(INDEX) OP VALUE.
-
+// When the value of SUPPORT_MAGIC is SYS_CONF, the return value depends on the 
+// _system_configuration[INDEX] COMPARE_OP VALUE.
 #ifndef PPC_AIX_CPU
-#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, MASK, OP, VALUE)
+  #define PPC_AIX_CPU(NAME, SUPPORT_MAGIC, INDEX, COMPARE_OP, VALUE)
 #endif
 
-//Since the __builtin_cpu_is() is supported only on AIX7.2 or AIX7.2 later OS.
-//And AIX 7.2 is supported only on IBM POWER 7 and later processors.
-//The __builtin_cpu_is() function returns false for other CPUs which are not Power7 and Power7 up.
-
-PPC_AIX_CPU("power4",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("ppc970",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("power5",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("power5+",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("power6",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("ppc-cell-be",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("power6x",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("power7",COMP_OP,AIX_SYSCON_IMPL_IDX,0,OP_EQ,AIX_PPC7_VALUE)
-PPC_AIX_CPU("ppca2",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("ppc405",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("ppc440",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("ppc464",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("ppc476",AIX_BUILTIN_PPC_FALSE,0,0,0,0)
-PPC_AIX_CPU("power8",COMP_OP,AIX_SYSCON_IMPL_IDX,0,OP_EQ,AIX_PPC8_VALUE)
-PPC_AIX_CPU("power9",COMP_OP,AIX_SYSCON_IMPL_IDX,0,OP_EQ,AIX_PPC9_VALUE)
-PPC_AIX_CPU("power10",COMP_OP,AIX_SYSCON_IMPL_IDX,0,OP_EQ,AIX_PPC10_VALUE)
+// __builtin_cpu_is() is supported only on Power7 and up.
+PPC_AIX_CPU("power4",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("ppc970",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("power5",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("power5+",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("power6",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("ppc-cell-be",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("power6x",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("power7",SYS_CONF,AIX_SYSCON_IMPL_IDX,COMP_EQ,AIX_PPC7_VALUE)
+PPC_AIX_CPU("ppca2",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("ppc405",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("ppc440",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("ppc464",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("ppc476",AIX_BUILTIN_PPC_FALSE,0,0,0)
+PPC_AIX_CPU("power8",SYS_CONF,AIX_SYSCON_IMPL_IDX,COMP_EQ,AIX_PPC8_VALUE)
+PPC_AIX_CPU("power9",SYS_CONF,AIX_SYSCON_IMPL_IDX,COMP_EQ,AIX_PPC9_VALUE)
+PPC_AIX_CPU("power10",SYS_CONF,AIX_SYSCON_IMPL_IDX,COMP_EQ,AIX_PPC10_VALUE)
 #undef PPC_AIX_CPU
 
 #ifndef PPC_SYSTEMCONFIG_TYPE



More information about the llvm-commits mailing list