Fix llvm runtime support bug.

Change-Id: I7c023e861cb8738ade67ef66155a318c8bd5735a
diff --git a/src/compiler_llvm/art_module.ll b/src/compiler_llvm/art_module.ll
index 87be92a..6d4b56b 100644
--- a/src/compiler_llvm/art_module.ll
+++ b/src/compiler_llvm/art_module.ll
@@ -41,7 +41,7 @@
 declare void @art_lock_object_from_code(%JavaObject*, %JavaObject*)
 declare void @art_unlock_object_from_code(%JavaObject*, %JavaObject*)
 
-declare void @art_test_suspend_from_code()
+declare void @art_test_suspend_from_code(%JavaObject*)
 
 declare void @art_push_shadow_frame_from_code(%ShadowFrame*)
 declare void @art_pop_shadow_frame_from_code()
diff --git a/src/compiler_llvm/generated/art_module.cc b/src/compiler_llvm/generated/art_module.cc
index c727d40..1237ec0 100644
--- a/src/compiler_llvm/generated/art_module.cc
+++ b/src/compiler_llvm/generated/art_module.cc
@@ -83,13 +83,13 @@
  /*isVarArg=*/false);
 
 std::vector<Type*>FuncTy_6_args;
+FuncTy_6_args.push_back(PointerTy_2);
 FunctionType* FuncTy_6 = FunctionType::get(
  /*Result=*/Type::getVoidTy(mod->getContext()),
  /*Params=*/FuncTy_6_args,
  /*isVarArg=*/false);
 
 std::vector<Type*>FuncTy_7_args;
-FuncTy_7_args.push_back(PointerTy_2);
 FunctionType* FuncTy_7 = FunctionType::get(
  /*Result=*/Type::getVoidTy(mod->getContext()),
  /*Params=*/FuncTy_7_args,
@@ -387,7 +387,7 @@
 Function* func_art_test_suspend_from_code = mod->getFunction("art_test_suspend_from_code");
 if (!func_art_test_suspend_from_code) {
 func_art_test_suspend_from_code = Function::Create(
- /*Type=*/FuncTy_6,
+ /*Type=*/FuncTy_4,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_test_suspend_from_code", mod); // (external, no body)
 func_art_test_suspend_from_code->setCallingConv(CallingConv::C);
@@ -398,7 +398,7 @@
 Function* func_art_push_shadow_frame_from_code = mod->getFunction("art_push_shadow_frame_from_code");
 if (!func_art_push_shadow_frame_from_code) {
 func_art_push_shadow_frame_from_code = Function::Create(
- /*Type=*/FuncTy_7,
+ /*Type=*/FuncTy_6,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_push_shadow_frame_from_code", mod); // (external, no body)
 func_art_push_shadow_frame_from_code->setCallingConv(CallingConv::C);
@@ -409,7 +409,7 @@
 Function* func_art_pop_shadow_frame_from_code = mod->getFunction("art_pop_shadow_frame_from_code");
 if (!func_art_pop_shadow_frame_from_code) {
 func_art_pop_shadow_frame_from_code = Function::Create(
- /*Type=*/FuncTy_6,
+ /*Type=*/FuncTy_7,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_pop_shadow_frame_from_code", mod); // (external, no body)
 func_art_pop_shadow_frame_from_code->setCallingConv(CallingConv::C);
@@ -431,7 +431,7 @@
 Function* func_art_throw_div_zero_from_code = mod->getFunction("art_throw_div_zero_from_code");
 if (!func_art_throw_div_zero_from_code) {
 func_art_throw_div_zero_from_code = Function::Create(
- /*Type=*/FuncTy_6,
+ /*Type=*/FuncTy_7,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_throw_div_zero_from_code", mod); // (external, no body)
 func_art_throw_div_zero_from_code->setCallingConv(CallingConv::C);
@@ -475,7 +475,7 @@
 Function* func_art_throw_stack_overflow_from_code = mod->getFunction("art_throw_stack_overflow_from_code");
 if (!func_art_throw_stack_overflow_from_code) {
 func_art_throw_stack_overflow_from_code = Function::Create(
- /*Type=*/FuncTy_6,
+ /*Type=*/FuncTy_7,
  /*Linkage=*/GlobalValue::ExternalLinkage,
  /*Name=*/"art_throw_stack_overflow_from_code", mod); // (external, no body)
 func_art_throw_stack_overflow_from_code->setCallingConv(CallingConv::C);
diff --git a/src/compiler_llvm/jni_compiler.cc b/src/compiler_llvm/jni_compiler.cc
index c1152ab..02985f7 100644
--- a/src/compiler_llvm/jni_compiler.cc
+++ b/src/compiler_llvm/jni_compiler.cc
@@ -30,7 +30,6 @@
 #include "shadow_frame.h"
 #include "utils_llvm.h"
 
-#include <llvm/Analysis/Verifier.h>
 #include <llvm/BasicBlock.h>
 #include <llvm/DerivedTypes.h>
 #include <llvm/Function.h>
@@ -264,7 +263,7 @@
                            irb_.getInt32(kRunnable));
 
   // Do a suspend check
-  irb_.CreateCall(irb_.GetRuntime(TestSuspend));
+  irb_.CreateCall(irb_.GetRuntime(TestSuspend), thread_object_addr);
 
   if (return_shorty == 'L') {
     // If the return value is reference, it may point to SIRT, we should decode it.
@@ -298,7 +297,7 @@
   }
 
   // Verify the generated bitcode
-  llvm::verifyFunction(*func_, llvm::PrintMessageAction);
+  VERIFY_LLVM_FUNCTION(*func_);
 
   // Add the memory usage approximation of the compilation unit
   cunit_->AddMemUsageApproximation((sirt_size * 4 + 50) * 50);
diff --git a/src/compiler_llvm/method_compiler.cc b/src/compiler_llvm/method_compiler.cc
index 5bd1695..8e592fe 100644
--- a/src/compiler_llvm/method_compiler.cc
+++ b/src/compiler_llvm/method_compiler.cc
@@ -35,7 +35,6 @@
 
 #include <iomanip>
 
-#include <llvm/Analysis/Verifier.h>
 #include <llvm/BasicBlock.h>
 #include <llvm/Function.h>
 #include <llvm/GlobalVariable.h>
@@ -3687,7 +3686,7 @@
   EmitPrologueLastBranch();
 
   // Verify the generated bitcode
-  llvm::verifyFunction(*func_, llvm::PrintMessageAction);
+  VERIFY_LLVM_FUNCTION(*func_);
 
   // Add the memory usage approximation of the compilation unit
   cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
@@ -3738,8 +3737,12 @@
 
 void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
   llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
+
+  llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
+
   EmitUpdateDexPC(dex_pc);
-  irb_.CreateCall(runtime_func);
+
+  irb_.CreateCall(runtime_func, thread_object_addr);
 
   EmitGuard_ExceptionLandingPad(dex_pc);
 }
diff --git a/src/compiler_llvm/runtime_support_builder.cc b/src/compiler_llvm/runtime_support_builder.cc
index a13867c..c5ff6d7 100644
--- a/src/compiler_llvm/runtime_support_builder.cc
+++ b/src/compiler_llvm/runtime_support_builder.cc
@@ -20,6 +20,7 @@
 #include "ir_builder.h"
 #include "shadow_frame.h"
 #include "thread.h"
+#include "utils_llvm.h"
 
 #include <llvm/DerivedTypes.h>
 #include <llvm/Function.h>
@@ -98,6 +99,8 @@
                              Thread::TopShadowFrameOffset().Int32Value(),
                              new_shadow_frame);
     irb_.CreateRetVoid();
+
+    VERIFY_LLVM_FUNCTION(*func);
   }
 
   if (!target_runtime_support_func_[PopShadowFrame]) {
@@ -118,6 +121,8 @@
                              Thread::TopShadowFrameOffset().Int32Value(),
                              old_shadow_frame);
     irb_.CreateRetVoid();
+
+    VERIFY_LLVM_FUNCTION(*func);
   }
 
   if (!target_runtime_support_func_[IsExceptionPending]) {
@@ -133,6 +138,8 @@
                                                  irb_.getJObjectTy());
     Value* is_exception_not_null = irb_.CreateICmpNE(exception, irb_.getJNull());
     irb_.CreateRet(is_exception_not_null);
+
+    VERIFY_LLVM_FUNCTION(*func);
   }
 
   if (!target_runtime_support_func_[TestSuspend]) {
@@ -146,8 +153,7 @@
     BasicBlock* basic_block = BasicBlock::Create(context_, "entry", func);
     irb_.SetInsertPoint(basic_block);
 
-    Function* get_thread = GetRuntimeSupportFunction(GetCurrentThread);
-    Value* thread = irb_.CreateCall(get_thread);
+    Value* thread = func->arg_begin();
     Value* suspend_count = irb_.LoadFromObjectOffset(thread,
                                                      Thread::SuspendCountOffset().Int32Value(),
                                                      irb_.getJIntTy());
@@ -165,6 +171,8 @@
     irb_.CreateRetVoid();
 
     OverrideRuntimeSupportFunction(TestSuspend, func);
+
+    VERIFY_LLVM_FUNCTION(*func);
   }
 
   if (!target_runtime_support_func_[MarkGCCard]) {
@@ -197,6 +205,8 @@
     Value* card_table_entry = irb_.CreateGEP(card_table, card_no);
     irb_.CreateStore(irb_.getInt8(GC_CARD_DIRTY), card_table_entry);
     irb_.CreateRetVoid();
+
+    VERIFY_LLVM_FUNCTION(*func);
   }
 }
 
diff --git a/src/compiler_llvm/runtime_support_builder_arm.cc b/src/compiler_llvm/runtime_support_builder_arm.cc
index ab33967..6029d3e 100644
--- a/src/compiler_llvm/runtime_support_builder_arm.cc
+++ b/src/compiler_llvm/runtime_support_builder_arm.cc
@@ -18,6 +18,7 @@
 
 #include "ir_builder.h"
 #include "thread.h"
+#include "utils_llvm.h"
 
 #include <llvm/DerivedTypes.h>
 #include <llvm/Function.h>
@@ -45,6 +46,8 @@
     InlineAsm* get_r9 = InlineAsm::get(func->getFunctionType(), "mov $0, r9", "=r", false);
     Value* r9 = irb_.CreateCall(get_r9);
     irb_.CreateRet(r9);
+
+    VERIFY_LLVM_FUNCTION(*func);
   }
 
   {
@@ -57,6 +60,8 @@
     Value* thread = func->arg_begin();
     irb_.CreateCall(set_r9, thread);
     irb_.CreateRetVoid();
+
+    VERIFY_LLVM_FUNCTION(*func);
   }
 }
 
diff --git a/src/compiler_llvm/runtime_support_builder_x86.cc b/src/compiler_llvm/runtime_support_builder_x86.cc
index c381874..ee5f6d8 100644
--- a/src/compiler_llvm/runtime_support_builder_x86.cc
+++ b/src/compiler_llvm/runtime_support_builder_x86.cc
@@ -18,6 +18,7 @@
 
 #include "ir_builder.h"
 #include "thread.h"
+#include "utils_llvm.h"
 
 #include <llvm/DerivedTypes.h>
 #include <llvm/Function.h>
@@ -50,6 +51,8 @@
     InlineAsm* get_fp = InlineAsm::get(func_ty, "movl %fs:($1), $0", "=r,r", false);
     Value* fp = irb_.CreateCall(get_fp, irb_.getPtrEquivInt(Thread::SelfOffset().Int32Value()));
     irb_.CreateRet(fp);
+
+    VERIFY_LLVM_FUNCTION(*func);
   }
 
   {
@@ -58,6 +61,8 @@
     BasicBlock* basic_block = BasicBlock::Create(context_, "entry", func);
     irb_.SetInsertPoint(basic_block);
     irb_.CreateRetVoid();
+
+    VERIFY_LLVM_FUNCTION(*func);
   }
 }
 
diff --git a/src/compiler_llvm/upcall_compiler.cc b/src/compiler_llvm/upcall_compiler.cc
index 4b99cf7..11d50ff 100644
--- a/src/compiler_llvm/upcall_compiler.cc
+++ b/src/compiler_llvm/upcall_compiler.cc
@@ -26,7 +26,6 @@
 #include "runtime_support_func.h"
 #include "utils_llvm.h"
 
-#include <llvm/Analysis/Verifier.h>
 #include <llvm/BasicBlock.h>
 #include <llvm/Function.h>
 #include <llvm/GlobalVariable.h>
@@ -181,7 +180,7 @@
   irb_.CreateRetVoid();
 
   // Verify the generated function
-  llvm::verifyFunction(*func, llvm::PrintMessageAction);
+  VERIFY_LLVM_FUNCTION(*func);
 
   // Add the memory usage approximation of the compilation unit
   cunit_->AddMemUsageApproximation((shorty_size * 3 + 8) * 50);
diff --git a/src/compiler_llvm/utils_llvm.h b/src/compiler_llvm/utils_llvm.h
index fb9f0cb..89a1946 100644
--- a/src/compiler_llvm/utils_llvm.h
+++ b/src/compiler_llvm/utils_llvm.h
@@ -19,11 +19,19 @@
 
 #include "stringprintf.h"
 
+#include <llvm/Analysis/Verifier.h>
+
 #include <stdint.h>
 #include <string>
 
 namespace art {
 
+#ifndef NDEBUG
+#define VERIFY_LLVM_FUNCTION(func) llvm::verifyFunction(func, llvm::PrintMessageAction)
+#else
+#define VERIFY_LLVM_FUNCTION(func)
+#endif
+
 inline static std::string ElfFuncName(uint16_t elf_func_idx) {
   return StringPrintf("Art%u", static_cast<unsigned int>(elf_func_idx));
 }