# HG changeset patch
# User Stefan Mahr <dac922@gmx.de>
# Date 1399768113 -7200
#      Sun May 11 02:28:33 2014 +0200
# Node ID cddaf7f8123d97c315c137812a0c529311651ec7
# Parent  6ca9387934b28b9b0070db3012d781e6dfb0e63a
fix LLVM 3.4 build (bug #41061)

* configure.ac: Call new functions OCTAVE_LLVM_RAW_FD_OSTREAM_API and
  OCTAVE_LLVM_LEGACY_PASSMANAGER_API, check for Verifier.h header file
* m4/acinclude.m4 (OCTAVE_LLVM_RAW_FD_OSTREAM_API): New function to
  detect correct raw_fd_ostream API
* m4/acinclude.m4 (OCTAVE_LLVM_LEGACY_PASSMANAGER_API): New function
  to detect legacy passmanager API
* libinterp/corefcn/jit-util.h: Use legacy passmanager namespace if
  necessary
* libinterp/corefcn/pt-jit.h (class tree_jit): Use legacy passmanager
  class if necessary
* libinterp/corefcn/pt-jit.cc: Include appropriate header files
* libinterp/corefcn/pt-jit.cc (tree_jit::initialize): Use legacy
  passmanager if necessary
* libinterp/corefcn/pt-jit.cc (tree_jit::optimize): Use correct API
* libinterp/corefcn/jit-typeinfo.cc: Include appropriate header file

diff -r 6ca9387934b2 -r cddaf7f8123d configure.ac
--- a/configure.ac	Sun Mar 16 13:25:57 2014 +0100
+++ b/configure.ac	Sun May 11 02:28:33 2014 +0200
@@ -857,9 +857,13 @@
       warn_llvm="Missing LLVM file TargetData.h.  JIT compiler is disabled."
     fi
 
+    AC_CHECK_HEADERS([llvm/IR/Verifier.h])
+
     OCTAVE_LLVM_FUNCTION_ADDATTRIBUTE_API
     OCTAVE_LLVM_FUNCTION_ADDFNATTR_API
     OCTAVE_LLVM_CALLINST_ADDATTRIBUTE_API
+    OCTAVE_LLVM_RAW_FD_OSTREAM_API
+    OCTAVE_LLVM_LEGACY_PASSMANAGER_API
     AC_LANG_POP(C++)
     CPPFLAGS="$save_CPPFLAGS"
     CXXFLAGS="$save_CXXFLAGS"
diff -r 6ca9387934b2 -r cddaf7f8123d libinterp/corefcn/jit-typeinfo.cc
--- a/libinterp/corefcn/jit-typeinfo.cc	Sun Mar 16 13:25:57 2014 +0100
+++ b/libinterp/corefcn/jit-typeinfo.cc	Sun May 11 02:28:33 2014 +0200
@@ -34,7 +34,12 @@
 
 #include "jit-typeinfo.h"
 
+#ifdef HAVE_LLVM_IR_VERIFIER_H
+#include <llvm/IR/Verifier.h>
+#else
 #include <llvm/Analysis/Verifier.h>
+#endif
+
 #include <llvm/ExecutionEngine/ExecutionEngine.h>
 
 #ifdef HAVE_LLVM_IR_FUNCTION_H
diff -r 6ca9387934b2 -r cddaf7f8123d libinterp/corefcn/jit-util.h
--- a/libinterp/corefcn/jit-util.h	Sun Mar 16 13:25:57 2014 +0100
+++ b/libinterp/corefcn/jit-util.h	Sun May 11 02:28:33 2014 +0200
@@ -42,8 +42,15 @@
 {
   class Value;
   class Module;
+#ifdef LEGACY_PASSMANAGER
+  namespace legacy {
+    class FunctionPassManager;
+    class PassManager;
+  }
+#else
   class FunctionPassManager;
   class PassManager;
+#endif
   class ExecutionEngine;
   class Function;
   class BasicBlock;
diff -r 6ca9387934b2 -r cddaf7f8123d libinterp/corefcn/pt-jit.cc
--- a/libinterp/corefcn/pt-jit.cc	Sun Mar 16 13:25:57 2014 +0100
+++ b/libinterp/corefcn/pt-jit.cc	Sun May 11 02:28:33 2014 +0200
@@ -50,11 +50,22 @@
 
 #include <llvm/Analysis/CallGraph.h>
 #include <llvm/Analysis/Passes.h>
+
+#ifdef HAVE_LLVM_IR_VERIFIER_H
+#include <llvm/IR/Verifier.h>
+#else
 #include <llvm/Analysis/Verifier.h>
+#endif
+
 #include <llvm/Bitcode/ReaderWriter.h>
 #include <llvm/ExecutionEngine/ExecutionEngine.h>
 #include <llvm/ExecutionEngine/JIT.h>
+
+#ifdef LEGACY_PASSMANAGER
+#include <llvm/IR/LegacyPassManager.h>
+#else
 #include <llvm/PassManager.h>
+#endif
 
 #ifdef HAVE_LLVM_IR_FUNCTION_H
 #include <llvm/IR/LLVMContext.h>
@@ -2061,10 +2072,15 @@
   if (! engine)
     return false;
 
+#ifdef LEGACY_PASSMANAGER
+  module_pass_manager = new llvm::legacy::PassManager ();
+  pass_manager = new llvm::legacy::FunctionPassManager (module); 
+#else
   module_pass_manager = new llvm::PassManager ();
+  pass_manager = new llvm::FunctionPassManager (module);
+#endif
   module_pass_manager->add (llvm::createAlwaysInlinerPass ());
 
-  pass_manager = new llvm::FunctionPassManager (module);
 #ifdef HAVE_LLVM_DATALAYOUT
   pass_manager->add (new llvm::DataLayout (*engine->getDataLayout ()));
 #else
@@ -2179,8 +2195,13 @@
   if (Vdebug_jit)
     {
       std::string error;
+#ifdef RAW_FD_OSTREAM_ARG_IS_LLVM_SYS_FS
+      llvm::raw_fd_ostream fout ("test.bc", error,
+                                 llvm::sys::fs::F_Binary);
+#else
       llvm::raw_fd_ostream fout ("test.bc", error,
                                  llvm::raw_fd_ostream::F_Binary);
+#endif
       llvm::WriteBitcodeToFile (module, fout);
     }
 }
diff -r 6ca9387934b2 -r cddaf7f8123d libinterp/corefcn/pt-jit.h
--- a/libinterp/corefcn/pt-jit.h	Sun Mar 16 13:25:57 2014 +0100
+++ b/libinterp/corefcn/pt-jit.h	Sun May 11 02:28:33 2014 +0200
@@ -386,8 +386,13 @@
   size_t trip_count (const octave_value& bounds) const;
 
   llvm::Module *module;
+#ifdef LEGACY_PASSMANAGER
+  llvm::legacy::PassManager *module_pass_manager;
+  llvm::legacy::FunctionPassManager *pass_manager;
+#else
   llvm::PassManager *module_pass_manager;
   llvm::FunctionPassManager *pass_manager;
+#endif
   llvm::ExecutionEngine *engine;
 };
 
diff -r 6ca9387934b2 -r cddaf7f8123d m4/acinclude.m4
--- a/m4/acinclude.m4	Sun Mar 16 13:25:57 2014 +0100
+++ b/m4/acinclude.m4	Sun May 11 02:28:33 2014 +0200
@@ -1744,6 +1744,58 @@
   fi
 ])
 dnl
+dnl Check for raw_fd_ostream API
+dnl
+AC_DEFUN([OCTAVE_LLVM_RAW_FD_OSTREAM_API], [
+  AC_CACHE_CHECK([check LLVM::raw_fd_ostream arg type is llvm::sys:fs],
+    [octave_cv_raw_fd_ostream_arg_is_llvm_sys_fs],
+    [AC_LANG_PUSH(C++)
+      AC_COMPILE_IFELSE(
+        [AC_LANG_PROGRAM([[
+          #include <llvm/Support/raw_os_ostream.h>
+          ]], [[
+          std::string str;
+          llvm::raw_fd_ostream fout ("", str, llvm::sys::fs::F_Binary);
+        ]])],
+        octave_cv_raw_fd_ostream_arg_is_llvm_sys_fs=yes,
+        octave_cv_raw_fd_ostream_arg_is_llvm_sys_fs=no)
+    AC_LANG_POP(C++)
+  ])
+  if test $octave_cv_raw_fd_ostream_arg_is_llvm_sys_fs = yes; then
+    AC_DEFINE(RAW_FD_OSTREAM_ARG_IS_LLVM_SYS_FS, 1,
+      [Define to 1 if LLVM::raw_fd_ostream arg type is llvm::sys:fs.])
+  fi
+])
+dnl
+dnl Check for legacy::PassManager API
+dnl
+AC_DEFUN([OCTAVE_LLVM_LEGACY_PASSMANAGER_API], [
+  AC_CACHE_CHECK([check for LLVM::legacy::PassManager],
+    [octave_cv_legacy_passmanager],
+    [AC_LANG_PUSH(C++)
+      save_LIBS="$LIBS"
+      LIBS="$LLVM_LIBS $LIBS"
+      AC_LINK_IFELSE(
+        [AC_LANG_PROGRAM([[
+          #include <llvm/IR/LegacyPassManager.h>
+          ]], [[
+          llvm::Module *module;
+          llvm::legacy::PassManager *module_pass_manager;
+          llvm::legacy::FunctionPassManager *pass_manager;      
+          module_pass_manager = new llvm::legacy::PassManager ();
+          pass_manager = new llvm::legacy::FunctionPassManager (module);
+        ]])],
+        octave_cv_legacy_passmanager=yes,
+        octave_cv_legacy_passmanager=no)
+      LIBS="$save_LIBS"
+    AC_LANG_POP(C++)
+  ])
+  if test $octave_cv_legacy_passmanager = yes; then
+    AC_DEFINE(LEGACY_PASSMANAGER, 1,
+      [Define to 1 if LLVM::legacy::PassManager exists.])
+  fi
+])
+dnl
 dnl Check for ar.
 dnl
 AC_DEFUN([OCTAVE_PROG_AR], [
