Revert "Cleanup runtime support. Inline via IR builder."

This reverts commit afa97e2b4ede9c5fb590399b106a42728ce3b999.
diff --git a/src/compiler_llvm/compilation_unit.cc b/src/compiler_llvm/compilation_unit.cc
index cdfd422..38faf61 100644
--- a/src/compiler_llvm/compilation_unit.cc
+++ b/src/compiler_llvm/compilation_unit.cc
@@ -128,10 +128,9 @@
   virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const {
     AU.addRequiredID(llvm::LoopSimplifyID);
 
-    // TODO: Preserve more.
-    //AU.addPreserved<llvm::DominatorTree>();
+    AU.addPreserved<llvm::DominatorTree>();
     AU.addPreserved<llvm::LoopInfo>();
-    //AU.addPreservedID(llvm::LoopSimplifyID);
+    AU.addPreservedID(llvm::LoopSimplifyID);
     AU.addPreserved<llvm::ScalarEvolution>();
     AU.addPreservedID(llvm::BreakCriticalEdgesID);
   }
@@ -141,13 +140,11 @@
     llvm::BasicBlock* bb = loop->getLoopLatch();
     CHECK_NE(bb, static_cast<void*>(NULL)) << "A single loop latch must exist.";
 
-    llvm::BasicBlock* tb = bb->splitBasicBlock(bb->getTerminator(), "suspend_exit");
-    // Remove unconditional branch which is added by splitBasicBlock.
-    bb->getTerminator()->eraseFromParent();
+    irb_->SetInsertPoint(bb->getTerminator());
 
-    irb_->SetInsertPoint(bb);
-    irb_->Runtime().EmitTestSuspend();
-    irb_->CreateBr(tb);
+    using art::compiler_llvm::runtime_support::TestSuspend;
+    llvm::Value* runtime_func = irb_->GetRuntime(TestSuspend);
+    irb_->CreateCall(runtime_func, irb_->getJNull());
 
     return true;
   }
@@ -198,6 +195,8 @@
     break;
   }
 
+  runtime_support_->OptimizeRuntimeSupport();
+
   irb_->SetRuntimeSupport(runtime_support_.get());
 }
 
@@ -353,10 +352,8 @@
 
   // Add optimization pass
   llvm::PassManagerBuilder pm_builder;
-  // TODO: Use inliner after we can do IPO.
-  pm_builder.Inliner = NULL;
   //pm_builder.Inliner = llvm::createFunctionInliningPass();
-  //pm_builder.Inliner = llvm::createAlwaysInlinerPass();
+  pm_builder.Inliner = llvm::createAlwaysInlinerPass();
   //pm_builder.Inliner = llvm::createPartialInliningPass();
   pm_builder.OptLevel = 3;
   pm_builder.DisableSimplifyLibCalls = 1;
diff --git a/src/compiler_llvm/method_compiler.cc b/src/compiler_llvm/method_compiler.cc
index ce2b99f..eef71db 100644
--- a/src/compiler_llvm/method_compiler.cc
+++ b/src/compiler_llvm/method_compiler.cc
@@ -2217,7 +2217,9 @@
 
 void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
   // Using runtime support, let the target can override by InlineAssembly.
-  irb_.Runtime().EmitMarkGCCard(value, target_addr);
+  llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
+
+  irb_.CreateCall2(runtime_func, value, target_addr);
 }
 
 void
diff --git a/src/compiler_llvm/runtime_support_builder.cc b/src/compiler_llvm/runtime_support_builder.cc
index 6be113d..8bbac94 100644
--- a/src/compiler_llvm/runtime_support_builder.cc
+++ b/src/compiler_llvm/runtime_support_builder.cc
@@ -21,6 +21,7 @@
 #include "monitor.h"
 #include "object.h"
 #include "thread.h"
+#include "utils_llvm.h"
 
 #include <llvm/DerivedTypes.h>
 #include <llvm/Function.h>
@@ -54,12 +55,27 @@
 #undef GET_RUNTIME_SUPPORT_FUNC_DECL
 }
 
+void RuntimeSupportBuilder::MakeFunctionInline(llvm::Function* func) {
+  func->setLinkage(GlobalValue::LinkOnceODRLinkage);
+  func->addFnAttr(Attribute::AlwaysInline);
+}
+
+void RuntimeSupportBuilder::OverrideRuntimeSupportFunction(RuntimeId id, llvm::Function* function) {
+  // TODO: Check function prototype.
+  if (id >= 0 && id < MAX_ID) {
+    runtime_support_func_decls_[id] = function;
+    target_runtime_support_func_[id] = true;
+  } else {
+    LOG(ERROR) << "Unknown runtime function id: " << id;
+  }
+}
+
 
 /* Thread */
 
 llvm::Value* RuntimeSupportBuilder::EmitGetCurrentThread() {
   Function* func = GetRuntimeSupportFunction(runtime_support::GetCurrentThread);
-  CallInst* call_inst = irb_.CreateCall(func);
+  llvm::CallInst* call_inst = irb_.CreateCall(func);
   call_inst->setOnlyReadsMemory();
   irb_.SetTBAA(call_inst, kTBAAConstJObject);
   return call_inst;
@@ -67,13 +83,13 @@
 
 llvm::Value* RuntimeSupportBuilder::EmitLoadFromThreadOffset(int64_t offset, llvm::Type* type,
                                                              TBAASpecialType s_ty) {
-  Value* thread = EmitGetCurrentThread();
+  llvm::Value* thread = EmitGetCurrentThread();
   return irb_.LoadFromObjectOffset(thread, offset, type, s_ty);
 }
 
 void RuntimeSupportBuilder::EmitStoreToThreadOffset(int64_t offset, llvm::Value* value,
                                                     TBAASpecialType s_ty) {
-  Value* thread = EmitGetCurrentThread();
+  llvm::Value* thread = EmitGetCurrentThread();
   irb_.StoreToObjectOffset(thread, offset, value, s_ty);
 }
 
@@ -140,7 +156,7 @@
                                               irb_.getJObjectTy(),
                                               kTBAAJRuntime);
   // If exception not null
-  return irb_.CreateIsNotNull(exception);
+  return irb_.CreateICmpNE(exception, irb_.getJNull());
 }
 
 void RuntimeSupportBuilder::EmitTestSuspend() {
@@ -150,7 +166,7 @@
                                                   kTBAARuntimeInfo);
   Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getJInt(0));
 
-  Function* parent_func = irb_.GetInsertBlock()->getParent();
+  llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
   BasicBlock* basic_block_suspend = BasicBlock::Create(context_, "suspend", parent_func);
   BasicBlock* basic_block_cont = BasicBlock::Create(context_, "suspend_cont", parent_func);
   irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
@@ -173,24 +189,24 @@
 }
 
 void RuntimeSupportBuilder::EmitUnlockObject(llvm::Value* object) {
-  Value* lock_id =
+  llvm::Value* lock_id =
       EmitLoadFromThreadOffset(Thread::ThinLockIdOffset().Int32Value(),
                                irb_.getJIntTy(),
                                kTBAARuntimeInfo);
-  Value* monitor =
+  llvm::Value* monitor =
       irb_.LoadFromObjectOffset(object,
                                 Object::MonitorOffset().Int32Value(),
                                 irb_.getJIntTy(),
                                 kTBAARuntimeInfo);
 
-  Value* my_monitor = irb_.CreateShl(lock_id, LW_LOCK_OWNER_SHIFT);
-  Value* hash_state = irb_.CreateAnd(monitor, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
-  Value* real_monitor = irb_.CreateAnd(monitor, ~(LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
+  llvm::Value* my_monitor = irb_.CreateShl(lock_id, LW_LOCK_OWNER_SHIFT);
+  llvm::Value* hash_state = irb_.CreateAnd(monitor, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
+  llvm::Value* real_monitor = irb_.CreateAnd(monitor, ~(LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
 
   // Is thin lock, held by us and not recursively acquired
-  Value* is_fast_path = irb_.CreateICmpEQ(real_monitor, my_monitor);
+  llvm::Value* is_fast_path = irb_.CreateICmpEQ(real_monitor, my_monitor);
 
-  Function* parent_func = irb_.GetInsertBlock()->getParent();
+  llvm::Function* parent_func = irb_.GetInsertBlock()->getParent();
   BasicBlock* bb_fast = BasicBlock::Create(context_, "unlock_fast", parent_func);
   BasicBlock* bb_slow = BasicBlock::Create(context_, "unlock_slow", parent_func);
   BasicBlock* bb_cont = BasicBlock::Create(context_, "unlock_cont", parent_func);
@@ -213,27 +229,59 @@
 }
 
 
-void RuntimeSupportBuilder::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
-  Function* parent_func = irb_.GetInsertBlock()->getParent();
-  BasicBlock* bb_mark_gc_card = BasicBlock::Create(context_, "mark_gc_card", parent_func);
-  BasicBlock* bb_cont = BasicBlock::Create(context_, "mark_gc_card_cont", parent_func);
+void RuntimeSupportBuilder::OptimizeRuntimeSupport() {
+  // TODO: Remove this after we remove suspend loop pass.
+  if (!target_runtime_support_func_[runtime_support::TestSuspend]) {
+    Function* slow_func = GetRuntimeSupportFunction(runtime_support::TestSuspend);
+    Function* func = Function::Create(slow_func->getFunctionType(),
+                                      GlobalValue::LinkOnceODRLinkage,
+                                      "test_suspend_fast",
+                                      &module_);
+    MakeFunctionInline(func);
+    BasicBlock* basic_block = BasicBlock::Create(context_, "entry", func);
+    irb_.SetInsertPoint(basic_block);
 
-  llvm::Value* not_null = irb_.CreateIsNotNull(value);
-  irb_.CreateCondBr(not_null, bb_mark_gc_card, bb_cont);
+    EmitTestSuspend();
 
-  irb_.SetInsertPoint(bb_mark_gc_card);
-  Value* card_table = EmitLoadFromThreadOffset(Thread::CardTableOffset().Int32Value(),
-                                               irb_.getInt8Ty()->getPointerTo(),
-                                               kTBAAConstJObject);
-  Value* target_addr_int = irb_.CreatePtrToInt(target_addr, irb_.getPtrEquivIntTy());
-  Value* card_no = irb_.CreateLShr(target_addr_int, irb_.getPtrEquivInt(GC_CARD_SHIFT));
-  Value* card_table_entry = irb_.CreateGEP(card_table, card_no);
-  irb_.CreateStore(irb_.getInt8(GC_CARD_DIRTY), card_table_entry, kTBAARuntimeInfo);
-  irb_.CreateBr(bb_cont);
+    irb_.CreateRetVoid();
 
-  irb_.SetInsertPoint(bb_cont);
+    OverrideRuntimeSupportFunction(runtime_support::TestSuspend, func);
+
+    VERIFY_LLVM_FUNCTION(*func);
+  }
+
+  if (!target_runtime_support_func_[MarkGCCard]) {
+    Function* func = GetRuntimeSupportFunction(MarkGCCard);
+    MakeFunctionInline(func);
+    BasicBlock* basic_block = BasicBlock::Create(context_, "entry", func);
+    irb_.SetInsertPoint(basic_block);
+    Function::arg_iterator arg_iter = func->arg_begin();
+    Value* value = arg_iter++;
+    Value* target_addr = arg_iter++;
+
+    llvm::Value* is_value_null = irb_.CreateICmpEQ(value, irb_.getJNull());
+
+    llvm::BasicBlock* block_value_is_null = BasicBlock::Create(context_, "value_is_null", func);
+    llvm::BasicBlock* block_mark_gc_card = BasicBlock::Create(context_, "mark_gc_card", func);
+
+    irb_.CreateCondBr(is_value_null, block_value_is_null, block_mark_gc_card);
+
+    irb_.SetInsertPoint(block_value_is_null);
+    irb_.CreateRetVoid();
+
+    irb_.SetInsertPoint(block_mark_gc_card);
+    Value* card_table = EmitLoadFromThreadOffset(Thread::CardTableOffset().Int32Value(),
+                                                 irb_.getInt8Ty()->getPointerTo(),
+                                                 kTBAAConstJObject);
+    Value* target_addr_int = irb_.CreatePtrToInt(target_addr, irb_.getPtrEquivIntTy());
+    Value* card_no = irb_.CreateLShr(target_addr_int, irb_.getPtrEquivInt(GC_CARD_SHIFT));
+    Value* card_table_entry = irb_.CreateGEP(card_table, card_no);
+    irb_.CreateStore(irb_.getInt8(GC_CARD_DIRTY), card_table_entry, kTBAARuntimeInfo);
+    irb_.CreateRetVoid();
+
+    VERIFY_LLVM_FUNCTION(*func);
+  }
 }
 
-
 } // namespace compiler_llvm
 } // namespace art
diff --git a/src/compiler_llvm/runtime_support_builder.h b/src/compiler_llvm/runtime_support_builder.h
index 05b9e53..8fd0d17 100644
--- a/src/compiler_llvm/runtime_support_builder.h
+++ b/src/compiler_llvm/runtime_support_builder.h
@@ -64,9 +64,6 @@
   virtual void EmitLockObject(llvm::Value* object);
   virtual void EmitUnlockObject(llvm::Value* object);
 
-  /* MarkGCCard */
-  virtual void EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr);
-
   llvm::Function* GetRuntimeSupportFunction(runtime_support::RuntimeId id) {
     if (id >= 0 && id < runtime_support::MAX_ID) {
       return runtime_support_func_decls_[id];
@@ -76,9 +73,19 @@
     }
   }
 
+  void OptimizeRuntimeSupport();
+
   virtual ~RuntimeSupportBuilder() {}
 
  protected:
+  // Mark a function as inline function.
+  // You should implement the function, if mark as inline.
+  void MakeFunctionInline(llvm::Function* function);
+
+  void OverrideRuntimeSupportFunction(runtime_support::RuntimeId id, llvm::Function* function);
+
+
+ protected:
   llvm::LLVMContext& context_;
   llvm::Module& module_;
   IRBuilder& irb_;