1 //===- llvm/Support/Host.h - Host machine characteristics --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Methods for querying the nature of the host machine.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_HOST_H
14 #define LLVM_SUPPORT_HOST_H
15 
16 #include "llvm/ADT/StringMap.h"
17 
18 #if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__)
19 #include <endian.h>
20 #elif defined(_AIX)
21 #include <sys/machine.h>
22 #elif defined(__sun)
23 /* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
24 #include <sys/types.h>
25 #define BIG_ENDIAN 4321
26 #define LITTLE_ENDIAN 1234
27 #if defined(_BIG_ENDIAN)
28 #define BYTE_ORDER BIG_ENDIAN
29 #else
30 #define BYTE_ORDER LITTLE_ENDIAN
31 #endif
32 #else
33 #if !defined(BYTE_ORDER) && !defined(_WIN32)
34 #include <machine/endian.h>
35 #endif
36 #endif
37 
38 #include <string>
39 
40 namespace llvm {
41 namespace sys {
42 
43 #if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
44 constexpr bool IsBigEndianHost = true;
45 #else
46 constexpr bool IsBigEndianHost = false;
47 #endif
48 
49   static const bool IsLittleEndianHost = !IsBigEndianHost;
50 
51   /// getDefaultTargetTriple() - Return the default target triple the compiler
52   /// has been configured to produce code for.
53   ///
54   /// The target triple is a string in the format of:
55   ///   CPU_TYPE-VENDOR-OPERATING_SYSTEM
56   /// or
57   ///   CPU_TYPE-VENDOR-KERNEL-OPERATING_SYSTEM
58   std::string getDefaultTargetTriple();
59 
60   /// getProcessTriple() - Return an appropriate target triple for generating
61   /// code to be loaded into the current process, e.g. when using the JIT.
62   std::string getProcessTriple();
63 
64   /// getHostCPUName - Get the LLVM name for the host CPU. The particular format
65   /// of the name is target dependent, and suitable for passing as -mcpu to the
66   /// target which matches the host.
67   ///
68   /// \return - The host CPU name, or empty if the CPU could not be determined.
69   StringRef getHostCPUName();
70 
71   /// getHostCPUFeatures - Get the LLVM names for the host CPU features.
72   /// The particular format of the names are target dependent, and suitable for
73   /// passing as -mattr to the target which matches the host.
74   ///
75   /// \param Features - A string mapping feature names to either
76   /// true (if enabled) or false (if disabled). This routine makes no guarantees
77   /// about exactly which features may appear in this map, except that they are
78   /// all valid LLVM feature names.
79   ///
80   /// \return - True on success.
81   bool getHostCPUFeatures(StringMap<bool> &Features);
82 
83   /// Get the number of physical cores (as opposed to logical cores returned
84   /// from thread::hardware_concurrency(), which includes hyperthreads).
85   /// Returns -1 if unknown for the current host system.
86   int getHostNumPhysicalCores();
87 
88   namespace detail {
89   /// Helper functions to extract HostCPUName from /proc/cpuinfo on linux.
90   StringRef getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent);
91   StringRef getHostCPUNameForARM(StringRef ProcCpuinfoContent);
92   StringRef getHostCPUNameForS390x(StringRef ProcCpuinfoContent);
93   StringRef getHostCPUNameForBPF();
94   }
95 }
96 }
97 
98 #endif
99