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