Implement monitorenter/monitorexit.

Pretty simple as they just invoke the runtime.

Change-Id: I5fcb2c783deac27e55e28d8b3da3e68ea4b77363
diff --git a/compiler/optimizing/builder.cc b/compiler/optimizing/builder.cc
index 11dd0f3..e338178 100644
--- a/compiler/optimizing/builder.cc
+++ b/compiler/optimizing/builder.cc
@@ -1354,6 +1354,22 @@
       break;
     }
 
+    case Instruction::MONITOR_ENTER: {
+      current_block_->AddInstruction(new (arena_) HMonitorOperation(
+          LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
+          HMonitorOperation::kEnter,
+          dex_offset));
+      break;
+    }
+
+    case Instruction::MONITOR_EXIT: {
+      current_block_->AddInstruction(new (arena_) HMonitorOperation(
+          LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot),
+          HMonitorOperation::kExit,
+          dex_offset));
+      break;
+    }
+
     default:
       return false;
   }
diff --git a/compiler/optimizing/code_generator_arm.cc b/compiler/optimizing/code_generator_arm.cc
index 0958cdf..c2e9a2e 100644
--- a/compiler/optimizing/code_generator_arm.cc
+++ b/compiler/optimizing/code_generator_arm.cc
@@ -2795,5 +2795,19 @@
   __ Bind(slow_path->GetExitLabel());
 }
 
+void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
+  LocationSummary* locations =
+      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
+  InvokeRuntimeCallingConvention calling_convention;
+  locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
+}
+
+void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
+  codegen_->InvokeRuntime(instruction->IsEnter()
+        ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
+      instruction,
+      instruction->GetDexPc());
+}
+
 }  // namespace arm
 }  // namespace art
diff --git a/compiler/optimizing/code_generator_arm64.cc b/compiler/optimizing/code_generator_arm64.cc
index ac65c1d..95c2fe7 100644
--- a/compiler/optimizing/code_generator_arm64.cc
+++ b/compiler/optimizing/code_generator_arm64.cc
@@ -639,6 +639,7 @@
   M(LoadClass)                                             \
   M(LoadException)                                         \
   M(LoadString)                                            \
+  M(MonitorOperation)                                      \
   M(ParallelMove)                                          \
   M(StaticFieldGet)                                        \
   M(StaticFieldSet)                                        \
diff --git a/compiler/optimizing/code_generator_x86.cc b/compiler/optimizing/code_generator_x86.cc
index 14c7fe4..bed44b2 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -2905,5 +2905,19 @@
   __ Bind(slow_path->GetExitLabel());
 }
 
+void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
+  LocationSummary* locations =
+      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
+  InvokeRuntimeCallingConvention calling_convention;
+  locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
+}
+
+void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
+  __ fs()->call(Address::Absolute(instruction->IsEnter()
+        ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
+        : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
+  codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
+}
+
 }  // namespace x86
 }  // namespace art
diff --git a/compiler/optimizing/code_generator_x86_64.cc b/compiler/optimizing/code_generator_x86_64.cc
index 5667fa7..112c179 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -2895,5 +2895,20 @@
   __ Bind(slow_path->GetExitLabel());
 }
 
+void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
+  LocationSummary* locations =
+      new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
+  InvokeRuntimeCallingConvention calling_convention;
+  locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
+}
+
+void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
+  __ gs()->call(Address::Absolute(instruction->IsEnter()
+        ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
+        : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
+      true));
+  codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
+}
+
 }  // namespace x86_64
 }  // namespace art
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h
index 0c75e41..1545b0a 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -508,6 +508,7 @@
   M(LoadString, Instruction)                                            \
   M(Local, Instruction)                                                 \
   M(LongConstant, Constant)                                             \
+  M(MonitorOperation, Instruction)                                      \
   M(Mul, BinaryOperation)                                               \
   M(Neg, UnaryOperation)                                                \
   M(NewArray, Instruction)                                              \
@@ -2431,6 +2432,36 @@
   DISALLOW_COPY_AND_ASSIGN(HCheckCast);
 };
 
+class HMonitorOperation : public HTemplateInstruction<1> {
+ public:
+  enum OperationKind {
+    kEnter,
+    kExit,
+  };
+
+  HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
+    : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
+    SetRawInputAt(0, object);
+  }
+
+  // Instruction may throw a Java exception, so we need an environment.
+  bool NeedsEnvironment() const OVERRIDE { return true; }
+  bool CanThrow() const OVERRIDE { return true; }
+
+  uint32_t GetDexPc() const { return dex_pc_; }
+
+  bool IsEnter() const { return kind_ == kEnter; }
+
+  DECLARE_INSTRUCTION(MonitorOperation);
+
+ protected:
+  const OperationKind kind_;
+  const uint32_t dex_pc_;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
+};
+
 
 class MoveOperands : public ArenaObject<kArenaAllocMisc> {
  public:
diff --git a/test/426-monitor/expected.txt b/test/426-monitor/expected.txt
new file mode 100644
index 0000000..2ffeff4
--- /dev/null
+++ b/test/426-monitor/expected.txt
@@ -0,0 +1,5 @@
+In static method
+In instance method
+In synchronized block
+In second instance method
+In second static method
diff --git a/test/426-monitor/info.txt b/test/426-monitor/info.txt
new file mode 100644
index 0000000..1b093ea
--- /dev/null
+++ b/test/426-monitor/info.txt
@@ -0,0 +1 @@
+Simple tests for monitorenter/monitorexit.
diff --git a/test/426-monitor/src/Main.java b/test/426-monitor/src/Main.java
new file mode 100644
index 0000000..a073a95
--- /dev/null
+++ b/test/426-monitor/src/Main.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class Main {
+  public static void main(String[] args) {
+    $opt$StaticSynchronizedMethod();
+    new Main().$opt$InstanceSynchronizedMethod();
+    $opt$SynchronizedBlock();
+    new Main().$opt$DoubleInstanceSynchronized();
+    $opt$DoubleStaticSynchronized();
+  }
+
+  public static synchronized void $opt$StaticSynchronizedMethod() {
+    System.out.println("In static method");
+  }
+
+  public synchronized void $opt$InstanceSynchronizedMethod() {
+    System.out.println("In instance method");
+  }
+
+  public static void $opt$SynchronizedBlock() {
+    Object o = new Object();
+    synchronized(o) {
+      System.out.println("In synchronized block");
+    }
+  }
+
+  public synchronized void $opt$DoubleInstanceSynchronized() {
+    synchronized (this) {
+      System.out.println("In second instance method");
+    }
+  }
+
+  public synchronized static void $opt$DoubleStaticSynchronized() {
+    synchronized (Main.class) {
+      System.out.println("In second static method");
+    }
+  }
+}