util: Move gallium's PIPE_FORMAT utils to /util/format/
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_misc.cpp
index 9c2de2ccbe6fdc27eac89cc8ddefb2b54ab7749e..e80676d5062fde756c074e2d0fed9b43c1a0fa74 100644 (file)
  */
 
 
-#ifndef __STDC_LIMIT_MACROS
-#define __STDC_LIMIT_MACROS
-#endif
-
-#ifndef __STDC_CONSTANT_MACROS
-#define __STDC_CONSTANT_MACROS
-#endif
-
 // Undef these vars just to silence warnings
 #undef PACKAGE_BUGREPORT
 #undef PACKAGE_NAME
 
 #include <stddef.h>
 
+#include <llvm/Config/llvm-config.h>
+
+#if LLVM_VERSION_MAJOR < 7
+// Workaround http://llvm.org/PR23628
+#pragma push_macro("DEBUG")
+#undef DEBUG
+#endif
+
+#include <llvm/Config/llvm-config.h>
 #include <llvm-c/Core.h>
+#include <llvm-c/Support.h>
 #include <llvm-c/ExecutionEngine.h>
 #include <llvm/Target/TargetOptions.h>
 #include <llvm/ExecutionEngine/ExecutionEngine.h>
 #include <llvm/ADT/Triple.h>
-#if HAVE_LLVM < 0x0306
-#include <llvm/ExecutionEngine/JITMemoryManager.h>
-#endif
+#include <llvm/Analysis/TargetLibraryInfo.h>
+#include <llvm/ExecutionEngine/SectionMemoryManager.h>
 #include <llvm/Support/CommandLine.h>
 #include <llvm/Support/Host.h>
 #include <llvm/Support/PrettyStackTrace.h>
 
 #include <llvm/Support/TargetSelect.h>
 
-#if HAVE_LLVM >= 0x0303
+#include <llvm/IR/CallSite.h>
 #include <llvm/IR/IRBuilder.h>
 #include <llvm/IR/Module.h>
 #include <llvm/Support/CBindingWrapping.h>
+
+#include <llvm/Config/llvm-config.h>
+#if LLVM_USE_INTEL_JITEVENTS
+#include <llvm/ExecutionEngine/JITEventListener.h>
 #endif
 
+#if LLVM_VERSION_MAJOR < 7
+// Workaround http://llvm.org/PR23628
+#pragma pop_macro("DEBUG")
+#endif
+
+#include "c11/threads.h"
+#include "os/os_thread.h"
 #include "pipe/p_config.h"
 #include "util/u_debug.h"
 #include "util/u_cpu_detect.h"
 
 #include "lp_bld_misc.h"
+#include "lp_bld_debug.h"
 
 namespace {
 
@@ -82,15 +95,9 @@ class LLVMEnsureMultithreaded {
 public:
    LLVMEnsureMultithreaded()
    {
-#if HAVE_LLVM < 0x0303
-      if (!llvm::llvm_is_multithreaded()) {
-         llvm::llvm_start_multithreaded();
-      }
-#else
       if (!LLVMIsMultithreaded()) {
          LLVMStartMultithreaded();
       }
-#endif
    }
 };
 
@@ -98,18 +105,10 @@ static LLVMEnsureMultithreaded lLVMEnsureMultithreaded;
 
 }
 
-extern "C" void
-lp_set_target_options(void)
-{
-#if HAVE_LLVM < 0x0304
-   /*
-    * By default LLVM adds a signal handler to output a pretty stack trace.
-    * This signal handler is never removed, causing problems when unloading the
-    * shared object where the gallium driver resides.
-    */
-   llvm::DisablePrettyStackTrace = true;
-#endif
+static once_flag init_native_targets_once_flag = ONCE_FLAG_INIT;
 
+static void init_native_targets()
+{
    // If we have a native target, initialize it to ensure it is linked in and
    // usable by the JIT.
    llvm::InitializeNativeTarget();
@@ -117,135 +116,77 @@ lp_set_target_options(void)
    llvm::InitializeNativeTargetAsmPrinter();
 
    llvm::InitializeNativeTargetDisassembler();
+#if DEBUG
+   {
+      char *env_llc_options = getenv("GALLIVM_LLC_OPTIONS");
+      if (env_llc_options) {
+         char *option;
+         char *options[64] = {(char *) "llc"};      // Warning without cast
+         int   n;
+         for (n = 0, option = strtok(env_llc_options, " "); option; n++, option = strtok(NULL, " ")) {
+            options[n + 1] = option;
+         }
+         if (gallivm_debug & (GALLIVM_DEBUG_IR | GALLIVM_DEBUG_ASM | GALLIVM_DEBUG_DUMP_BC)) {
+            debug_printf("llc additional options (%d):\n", n);
+            for (int i = 1; i <= n; i++)
+               debug_printf("\t%s\n", options[i]);
+            debug_printf("\n");
+         }
+         LLVMParseCommandLineOptions(n + 1, options, NULL);
+      }
+   }
+#endif
 }
 
-
-extern "C"
-LLVMValueRef
-lp_build_load_volatile(LLVMBuilderRef B, LLVMValueRef PointerVal,
-                       const char *Name)
+extern "C" void
+lp_set_target_options(void)
 {
-   return llvm::wrap(llvm::unwrap(B)->CreateLoad(llvm::unwrap(PointerVal), true, Name));
+   /* The llvm target registry is not thread-safe, so drivers and state-trackers
+    * that want to initialize targets should use the lp_set_target_options()
+    * function to safely initialize targets.
+    *
+    * LLVM targets should be initialized before the driver or state-tracker tries
+    * to access the registry.
+    */
+   call_once(&init_native_targets_once_flag, init_native_targets);
 }
 
-
 extern "C"
-void
-lp_set_load_alignment(LLVMValueRef Inst,
-                       unsigned Align)
+LLVMTargetLibraryInfoRef
+gallivm_create_target_library_info(const char *triple)
 {
-   llvm::unwrap<llvm::LoadInst>(Inst)->setAlignment(Align);
+   return reinterpret_cast<LLVMTargetLibraryInfoRef>(
+   new llvm::TargetLibraryInfoImpl(
+   llvm::Triple(triple)));
 }
 
 extern "C"
 void
-lp_set_store_alignment(LLVMValueRef Inst,
-                      unsigned Align)
+gallivm_dispose_target_library_info(LLVMTargetLibraryInfoRef library_info)
 {
-   llvm::unwrap<llvm::StoreInst>(Inst)->setAlignment(Align);
+   delete reinterpret_cast<
+   llvm::TargetLibraryInfoImpl
+   *>(library_info);
 }
 
-#if HAVE_LLVM < 0x0306
+
+typedef llvm::RTDyldMemoryManager BaseMemoryManager;
+
 
 /*
  * Delegating is tedious but the default manager class is hidden in an
  * anonymous namespace in LLVM, so we cannot just derive from it to change
  * its behavior.
  */
-class DelegatingJITMemoryManager : public llvm::JITMemoryManager {
+class DelegatingJITMemoryManager : public BaseMemoryManager {
 
    protected:
-      virtual llvm::JITMemoryManager *mgr() const = 0;
+      virtual BaseMemoryManager *mgr() const = 0;
 
    public:
-      /*
-       * From JITMemoryManager
-       */
-      virtual void setMemoryWritable() {
-         mgr()->setMemoryWritable();
-      }
-      virtual void setMemoryExecutable() {
-         mgr()->setMemoryExecutable();
-      }
-      virtual void setPoisonMemory(bool poison) {
-         mgr()->setPoisonMemory(poison);
-      }
-      virtual void AllocateGOT() {
-         mgr()->AllocateGOT();
-         /*
-          * isManagingGOT() is not virtual in base class so we can't delegate.
-          * Instead we mirror the value of HasGOT in our instance.
-          */
-         HasGOT = mgr()->isManagingGOT();
-      }
-      virtual uint8_t *getGOTBase() const {
-         return mgr()->getGOTBase();
-      }
-      virtual uint8_t *startFunctionBody(const llvm::Function *F,
-                                         uintptr_t &ActualSize) {
-         return mgr()->startFunctionBody(F, ActualSize);
-      }
-      virtual uint8_t *allocateStub(const llvm::GlobalValue *F,
-                                    unsigned StubSize,
-                                    unsigned Alignment) {
-         return mgr()->allocateStub(F, StubSize, Alignment);
-      }
-      virtual void endFunctionBody(const llvm::Function *F,
-                                   uint8_t *FunctionStart,
-                                   uint8_t *FunctionEnd) {
-         mgr()->endFunctionBody(F, FunctionStart, FunctionEnd);
-      }
-      virtual uint8_t *allocateSpace(intptr_t Size, unsigned Alignment) {
-         return mgr()->allocateSpace(Size, Alignment);
-      }
-      virtual uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment) {
-         return mgr()->allocateGlobal(Size, Alignment);
-      }
-      virtual void deallocateFunctionBody(void *Body) {
-         mgr()->deallocateFunctionBody(Body);
-      }
-#if HAVE_LLVM < 0x0304
-      virtual uint8_t *startExceptionTable(const llvm::Function *F,
-                                           uintptr_t &ActualSize) {
-         return mgr()->startExceptionTable(F, ActualSize);
-      }
-      virtual void endExceptionTable(const llvm::Function *F,
-                                     uint8_t *TableStart,
-                                     uint8_t *TableEnd,
-                                     uint8_t *FrameRegister) {
-         mgr()->endExceptionTable(F, TableStart, TableEnd,
-                                  FrameRegister);
-      }
-      virtual void deallocateExceptionTable(void *ET) {
-         mgr()->deallocateExceptionTable(ET);
-      }
-#endif
-      virtual bool CheckInvariants(std::string &s) {
-         return mgr()->CheckInvariants(s);
-      }
-      virtual size_t GetDefaultCodeSlabSize() {
-         return mgr()->GetDefaultCodeSlabSize();
-      }
-      virtual size_t GetDefaultDataSlabSize() {
-         return mgr()->GetDefaultDataSlabSize();
-      }
-      virtual size_t GetDefaultStubSlabSize() {
-         return mgr()->GetDefaultStubSlabSize();
-      }
-      virtual unsigned GetNumCodeSlabs() {
-         return mgr()->GetNumCodeSlabs();
-      }
-      virtual unsigned GetNumDataSlabs() {
-         return mgr()->GetNumDataSlabs();
-      }
-      virtual unsigned GetNumStubSlabs() {
-         return mgr()->GetNumStubSlabs();
-      }
-
       /*
        * From RTDyldMemoryManager
        */
-#if HAVE_LLVM >= 0x0304
       virtual uint8_t *allocateCodeSection(uintptr_t Size,
                                            unsigned Alignment,
                                            unsigned SectionID,
@@ -253,59 +194,34 @@ class DelegatingJITMemoryManager : public llvm::JITMemoryManager {
          return mgr()->allocateCodeSection(Size, Alignment, SectionID,
                                            SectionName);
       }
-#else
-      virtual uint8_t *allocateCodeSection(uintptr_t Size,
-                                           unsigned Alignment,
-                                           unsigned SectionID) {
-         return mgr()->allocateCodeSection(Size, Alignment, SectionID);
-      }
-#endif
-#if HAVE_LLVM >= 0x0303
       virtual uint8_t *allocateDataSection(uintptr_t Size,
                                            unsigned Alignment,
                                            unsigned SectionID,
-#if HAVE_LLVM >= 0x0304
                                            llvm::StringRef SectionName,
-#endif
                                            bool IsReadOnly) {
          return mgr()->allocateDataSection(Size, Alignment, SectionID,
-#if HAVE_LLVM >= 0x0304
                                            SectionName,
-#endif
                                            IsReadOnly);
       }
-#if HAVE_LLVM >= 0x0304
       virtual void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
          mgr()->registerEHFrames(Addr, LoadAddr, Size);
       }
-      virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
-         mgr()->deregisterEHFrames(Addr, LoadAddr, Size);
+#if LLVM_VERSION_MAJOR >= 5
+      virtual void deregisterEHFrames() {
+         mgr()->deregisterEHFrames();
       }
 #else
-      virtual void registerEHFrames(llvm::StringRef SectionData) {
-         mgr()->registerEHFrames(SectionData);
-      }
-#endif
-#else
-      virtual uint8_t *allocateDataSection(uintptr_t Size,
-                                           unsigned Alignment,
-                                           unsigned SectionID) {
-         return mgr()->allocateDataSection(Size, Alignment, SectionID);
+      virtual void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {
+         mgr()->deregisterEHFrames(Addr, LoadAddr, Size);
       }
 #endif
       virtual void *getPointerToNamedFunction(const std::string &Name,
                                               bool AbortOnFailure=true) {
          return mgr()->getPointerToNamedFunction(Name, AbortOnFailure);
       }
-#if HAVE_LLVM == 0x0303
-      virtual bool applyPermissions(std::string *ErrMsg = 0) {
-         return mgr()->applyPermissions(ErrMsg);
-      }
-#elif HAVE_LLVM > 0x0303
       virtual bool finalizeMemory(std::string *ErrMsg = 0) {
          return mgr()->finalizeMemory(ErrMsg);
       }
-#endif
 };
 
 
@@ -322,43 +238,30 @@ class DelegatingJITMemoryManager : public llvm::JITMemoryManager {
  */
 class ShaderMemoryManager : public DelegatingJITMemoryManager {
 
-   llvm::JITMemoryManager *TheMM;
+   BaseMemoryManager *TheMM;
 
    struct GeneratedCode {
       typedef std::vector<void *> Vec;
       Vec FunctionBody, ExceptionTable;
-      llvm::JITMemoryManager *TheMM;
+      BaseMemoryManager *TheMM;
 
-      GeneratedCode(llvm::JITMemoryManager *MM) {
+      GeneratedCode(BaseMemoryManager *MM) {
          TheMM = MM;
       }
 
       ~GeneratedCode() {
-         /*
-          * Deallocate things as previously requested and
-          * free shared manager when no longer used.
-          */
-        Vec::iterator i;
-
-        assert(TheMM);
-        for ( i = FunctionBody.begin(); i != FunctionBody.end(); ++i )
-           TheMM->deallocateFunctionBody(*i);
-#if HAVE_LLVM < 0x0304
-        for ( i = ExceptionTable.begin(); i != ExceptionTable.end(); ++i )
-           TheMM->deallocateExceptionTable(*i);
-#endif
       }
    };
 
    GeneratedCode *code;
 
-   llvm::JITMemoryManager *mgr() const {
+   BaseMemoryManager *mgr() const {
       return TheMM;
    }
 
    public:
 
-      ShaderMemoryManager(llvm::JITMemoryManager* MM) {
+      ShaderMemoryManager(BaseMemoryManager* MM) {
          TheMM = MM;
          code = new GeneratedCode(MM);
       }
@@ -378,20 +281,12 @@ class ShaderMemoryManager : public DelegatingJITMemoryManager {
          delete (GeneratedCode *) code;
       }
 
-#if HAVE_LLVM < 0x0304
-      virtual void deallocateExceptionTable(void *ET) {
-         // remember for later deallocation
-         code->ExceptionTable.push_back(ET);
-      }
-#endif
-
       virtual void deallocateFunctionBody(void *Body) {
          // remember for later deallocation
          code->FunctionBody.push_back(Body);
       }
 };
 
-#endif
 
 /**
  * Same as LLVMCreateJITCompilerForModule, but:
@@ -410,22 +305,12 @@ lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
                                         LLVMModuleRef M,
                                         LLVMMCJITMemoryManagerRef CMM,
                                         unsigned OptLevel,
-                                        int useMCJIT,
                                         char **OutError)
 {
    using namespace llvm;
 
-#if HAVE_LLVM >= 0x0306
-   *OutError = strdup("MCJIT not supported");
-   return 1;
-#else
-
    std::string Error;
-#if HAVE_LLVM >= 0x0306
    EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
-#else
-   EngineBuilder builder(unwrap(M));
-#endif
 
    /**
     * LLVM 3.1+ haven't more "extern unsigned llvm::StackAlignmentOverride" and
@@ -434,20 +319,6 @@ lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
    TargetOptions options;
 #if defined(PIPE_ARCH_X86)
    options.StackAlignmentOverride = 4;
-#if HAVE_LLVM < 0x0304
-   options.RealignStack = true;
-#endif
-#endif
-
-#if defined(DEBUG)
-   options.JITEmitDebugInfo = true;
-#endif
-
-#if defined(DEBUG) || defined(PROFILE)
-#if HAVE_LLVM < 0x0304
-   options.NoFramePointerElimNonLeaf = true;
-#endif
-   options.NoFramePointerElim = true;
 #endif
 
    builder.setEngineKind(EngineKind::JIT)
@@ -455,36 +326,123 @@ lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
           .setTargetOptions(options)
           .setOptLevel((CodeGenOpt::Level)OptLevel);
 
-   if (useMCJIT) {
-#if HAVE_LLVM < 0x0306
-       builder.setUseMCJIT(true);
-#endif
 #ifdef _WIN32
-       /*
-        * MCJIT works on Windows, but currently only through ELF object format.
-        */
-       std::string targetTriple = llvm::sys::getProcessTriple();
-       targetTriple.append("-elf");
-       unwrap(M)->setTargetTriple(targetTriple);
+    /*
+     * MCJIT works on Windows, but currently only through ELF object format.
+     *
+     * XXX: We could use `LLVM_HOST_TRIPLE "-elf"` but LLVM_HOST_TRIPLE has
+     * different strings for MinGW/MSVC, so better play it safe and be
+     * explicit.
+     */
+#  ifdef _WIN64
+    LLVMSetTarget(M, "x86_64-pc-win32-elf");
+#  else
+    LLVMSetTarget(M, "i686-pc-win32-elf");
+#  endif
 #endif
+
+   llvm::SmallVector<std::string, 16> MAttrs;
+
+#if LLVM_VERSION_MAJOR >= 4 && (defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64) || defined(PIPE_ARCH_ARM))
+   /* llvm-3.3+ implements sys::getHostCPUFeatures for Arm
+    * and llvm-3.7+ for x86, which allows us to enable/disable
+    * code generation based on the results of cpuid on these
+    * architectures.
+    */
+   llvm::StringMap<bool> features;
+   llvm::sys::getHostCPUFeatures(features);
+
+   for (StringMapIterator<bool> f = features.begin();
+        f != features.end();
+        ++f) {
+      MAttrs.push_back(((*f).second ? "+" : "-") + (*f).first().str());
    }
+#elif defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
+   /*
+    * We need to unset attributes because sometimes LLVM mistakenly assumes
+    * certain features are present given the processor name.
+    *
+    * https://bugs.freedesktop.org/show_bug.cgi?id=92214
+    * http://llvm.org/PR25021
+    * http://llvm.org/PR19429
+    * http://llvm.org/PR16721
+    */
+   MAttrs.push_back(util_cpu_caps.has_sse    ? "+sse"    : "-sse"   );
+   MAttrs.push_back(util_cpu_caps.has_sse2   ? "+sse2"   : "-sse2"  );
+   MAttrs.push_back(util_cpu_caps.has_sse3   ? "+sse3"   : "-sse3"  );
+   MAttrs.push_back(util_cpu_caps.has_ssse3  ? "+ssse3"  : "-ssse3" );
+   MAttrs.push_back(util_cpu_caps.has_sse4_1 ? "+sse4.1" : "-sse4.1");
+   MAttrs.push_back(util_cpu_caps.has_sse4_2 ? "+sse4.2" : "-sse4.2");
+   /*
+    * AVX feature is not automatically detected from CPUID by the X86 target
+    * yet, because the old (yet default) JIT engine is not capable of
+    * emitting the opcodes. On newer llvm versions it is and at least some
+    * versions (tested with 3.3) will emit avx opcodes without this anyway.
+    */
+   MAttrs.push_back(util_cpu_caps.has_avx  ? "+avx"  : "-avx");
+   MAttrs.push_back(util_cpu_caps.has_f16c ? "+f16c" : "-f16c");
+   MAttrs.push_back(util_cpu_caps.has_fma  ? "+fma"  : "-fma");
+   MAttrs.push_back(util_cpu_caps.has_avx2 ? "+avx2" : "-avx2");
+   /* disable avx512 and all subvariants */
+   MAttrs.push_back("-avx512cd");
+   MAttrs.push_back("-avx512er");
+   MAttrs.push_back("-avx512f");
+   MAttrs.push_back("-avx512pf");
+   MAttrs.push_back("-avx512bw");
+   MAttrs.push_back("-avx512dq");
+   MAttrs.push_back("-avx512vl");
+#endif
+#if defined(PIPE_ARCH_ARM)
+   if (!util_cpu_caps.has_neon) {
+      MAttrs.push_back("-neon");
+      MAttrs.push_back("-crypto");
+      MAttrs.push_back("-vfp2");
+   }
+#endif
 
-   llvm::SmallVector<std::string, 1> MAttrs;
-   if (util_cpu_caps.has_avx) {
-      /*
-       * AVX feature is not automatically detected from CPUID by the X86 target
-       * yet, because the old (yet default) JIT engine is not capable of
-       * emitting the opcodes. On newer llvm versions it is and at least some
-       * versions (tested with 3.3) will emit avx opcodes without this anyway.
-       */
-      MAttrs.push_back("+avx");
-      if (util_cpu_caps.has_f16c) {
-         MAttrs.push_back("+f16c");
+#if defined(PIPE_ARCH_PPC)
+   MAttrs.push_back(util_cpu_caps.has_altivec ? "+altivec" : "-altivec");
+#if (LLVM_VERSION_MAJOR < 4)
+   /*
+    * Make sure VSX instructions are disabled
+    * See LLVM bugs:
+    * https://llvm.org/bugs/show_bug.cgi?id=25503#c7 (fixed in 3.8.1)
+    * https://llvm.org/bugs/show_bug.cgi?id=26775 (fixed in 3.8.1)
+    * https://llvm.org/bugs/show_bug.cgi?id=33531 (fixed in 4.0)
+    * https://llvm.org/bugs/show_bug.cgi?id=34647 (llc performance on certain unusual shader IR; intro'd in 4.0, pending as of 5.0)
+    */
+   if (util_cpu_caps.has_altivec) {
+      MAttrs.push_back("-vsx");
+   }
+#else
+   /*
+    * Bug 25503 is fixed, by the same fix that fixed
+    * bug 26775, in versions of LLVM later than 3.8 (starting with 3.8.1).
+    * BZ 33531 actually comprises more than one bug, all of
+    * which are fixed in LLVM 4.0.
+    *
+    * With LLVM 4.0 or higher:
+    * Make sure VSX instructions are ENABLED (if supported), unless
+    * VSX instructions are explicitly enabled/disabled via GALLIVM_VSX=1 or 0.
+    */
+   if (util_cpu_caps.has_altivec) {
+      MAttrs.push_back(util_cpu_caps.has_vsx ? "+vsx" : "-vsx");
+   }
+#endif
+#endif
+
+   builder.setMAttrs(MAttrs);
+
+   if (gallivm_debug & (GALLIVM_DEBUG_IR | GALLIVM_DEBUG_ASM | GALLIVM_DEBUG_DUMP_BC)) {
+      int n = MAttrs.size();
+      if (n > 0) {
+         debug_printf("llc -mattr option(s): ");
+         for (int i = 0; i < n; i++)
+            debug_printf("%s%s", MAttrs[i].c_str(), (i < n - 1) ? "," : "");
+         debug_printf("\n");
       }
-      builder.setMAttrs(MAttrs);
    }
 
-#if HAVE_LLVM >= 0x0305
    StringRef MCPU = llvm::sys::getHostCPUName();
    /*
     * The cpu bits are no longer set automatically, so need to set mcpu manually.
@@ -498,27 +456,38 @@ lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
     * when not using MCJIT so no instructions are generated which the old JIT
     * can't handle. Not entirely sure if we really need to do anything yet.
     */
-   builder.setMCPU(MCPU);
+#if UTIL_ARCH_LITTLE_ENDIAN  && defined(PIPE_ARCH_PPC_64)
+   /*
+    * Versions of LLVM prior to 4.0 lacked a table entry for "POWER8NVL",
+    * resulting in (big-endian) "generic" being returned on
+    * little-endian Power8NVL systems.  The result was that code that
+    * attempted to load the least significant 32 bits of a 64-bit quantity
+    * from memory loaded the wrong half.  This resulted in failures in some
+    * Piglit tests, e.g.
+    * .../arb_gpu_shader_fp64/execution/conversion/frag-conversion-explicit-double-uint
+    */
+   if (MCPU == "generic")
+      MCPU = "pwr8";
 #endif
+   builder.setMCPU(MCPU);
+   if (gallivm_debug & (GALLIVM_DEBUG_IR | GALLIVM_DEBUG_ASM | GALLIVM_DEBUG_DUMP_BC)) {
+      debug_printf("llc -mcpu option: %s\n", MCPU.str().c_str());
+   }
 
-   llvm::JITMemoryManager* JMM = reinterpret_cast<llvm::JITMemoryManager*>(CMM);
-   ShaderMemoryManager *MM = new ShaderMemoryManager(JMM);
+   ShaderMemoryManager *MM = NULL;
+   BaseMemoryManager* JMM = reinterpret_cast<BaseMemoryManager*>(CMM);
+   MM = new ShaderMemoryManager(JMM);
    *OutCode = MM->getGeneratedCode();
 
-   builder.setJITMemoryManager(MM);
+   builder.setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager>(MM));
+   MM = NULL; // ownership taken by std::unique_ptr
 
    ExecutionEngine *JIT;
 
-#if HAVE_LLVM >= 0x0302
    JIT = builder.create();
-#else
-   /*
-    * Workaround http://llvm.org/PR12833
-    */
-   StringRef MArch = "";
-   StringRef MCPU = "";
-   Triple TT(unwrap(M)->getTargetTriple());
-   JIT = builder.create(builder.selectTarget(TT, MArch, MCPU, MAttrs));
+#if LLVM_USE_INTEL_JITEVENTS
+   JITEventListener *JEL = JITEventListener::createIntelJITEventListener();
+   JIT->RegisterJITEventListener(JEL);
 #endif
    if (JIT) {
       *OutJIT = wrap(JIT);
@@ -529,7 +498,6 @@ lp_build_create_jit_compiler_for_module(LLVMExecutionEngineRef *OutJIT,
    delete MM;
    *OutError = strdup(Error.c_str());
    return 1;
-#endif
 }
 
 
@@ -537,29 +505,33 @@ extern "C"
 void
 lp_free_generated_code(struct lp_generated_code *code)
 {
-#if HAVE_LLVM < 0x0306
    ShaderMemoryManager::freeGeneratedCode(code);
-#endif
 }
 
 extern "C"
 LLVMMCJITMemoryManagerRef
 lp_get_default_memory_manager()
 {
-#if HAVE_LLVM < 0x0306
-   llvm::JITMemoryManager *mm;
-   mm = llvm::JITMemoryManager::CreateDefaultMemManager();
+   BaseMemoryManager *mm;
+   mm = new llvm::SectionMemoryManager();
    return reinterpret_cast<LLVMMCJITMemoryManagerRef>(mm);
-#else
-   return 0;
-#endif
 }
 
 extern "C"
 void
 lp_free_memory_manager(LLVMMCJITMemoryManagerRef memorymgr)
 {
-#if HAVE_LLVM < 0x0306
-   delete reinterpret_cast<llvm::JITMemoryManager*>(memorymgr);
-#endif
+   delete reinterpret_cast<BaseMemoryManager*>(memorymgr);
+}
+
+extern "C" LLVMValueRef
+lp_get_called_value(LLVMValueRef call)
+{
+       return LLVMGetCalledValue(call);
+}
+
+extern "C" bool
+lp_is_function(LLVMValueRef v)
+{
+       return LLVMGetValueKind(v) == LLVMFunctionValueKind;
 }