Add inferred register category map to Method object.

Change-Id: I69456c79eb3ce3df1924bffe4db5314b0552f1de
diff --git a/build/Android.common.mk b/build/Android.common.mk
index 525ca83..ba421cb 100644
--- a/build/Android.common.mk
+++ b/build/Android.common.mk
@@ -218,6 +218,7 @@
 	src/compiler_llvm/compiler_llvm.cc \
 	src/compiler_llvm/frontend.cc \
 	src/compiler_llvm/ir_builder.cc \
+	src/compiler_llvm/inferred_reg_category_map.cc \
 	src/compiler_llvm/method_compiler.cc
 else
 LIBART_COMMON_SRC_FILES += \
diff --git a/src/compiler_llvm/inferred_reg_category_map.cc b/src/compiler_llvm/inferred_reg_category_map.cc
new file mode 100644
index 0000000..528247b
--- /dev/null
+++ b/src/compiler_llvm/inferred_reg_category_map.cc
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+#include "inferred_reg_category_map.h"
+
+#include "backend_types.h"
+#include "stl_util.h"
+
+#include <stdint.h>
+#include <vector>
+
+namespace art {
+namespace compiler_llvm {
+
+
+InferredRegCategoryMap::InferredRegCategoryMap(uint32_t insns_size,
+                                               uint8_t regs_size)
+: registers_size_(regs_size), lines_(insns_size, NULL) {
+}
+
+InferredRegCategoryMap::~InferredRegCategoryMap() {
+  STLDeleteElements(&lines_);
+}
+
+RegCategory InferredRegCategoryMap::GetRegCategory(uint32_t dex_pc,
+                                                   uint16_t reg_idx) const {
+  CHECK_NE(lines_[dex_pc], static_cast<RegCategoryLine*>(NULL));
+  return static_cast<RegCategory>((*lines_[dex_pc])[reg_idx]);
+}
+
+void InferredRegCategoryMap::SetRegCategory(uint32_t dex_pc,
+                                            uint16_t reg_idx,
+                                            RegCategory cat) {
+  if (lines_[dex_pc] == NULL) {
+    lines_[dex_pc] = new RegCategoryLine(registers_size_, kRegUnknown);
+  }
+
+  (*lines_[dex_pc])[reg_idx] = cat;
+}
+
+bool InferredRegCategoryMap::
+operator==(InferredRegCategoryMap const& rhs) const {
+
+  if (registers_size_ != rhs.registers_size_) {
+    return false;
+  }
+
+  if (lines_.size() != rhs.lines_.size()) {
+    return false;
+  }
+
+  for (size_t i = 0; i < lines_.size(); ++i) {
+    if (lines_[i] == NULL && rhs.lines_[i] == NULL) {
+      continue;
+    }
+
+    if ((lines_[i] == NULL && rhs.lines_[i] != NULL) ||
+        (lines_[i] != NULL && rhs.lines_[i] == NULL)) {
+      return false;
+    }
+
+    if (*lines_[i] != *rhs.lines_[i]) {
+      return false;
+    }
+  }
+
+  return true;
+}
+
+bool InferredRegCategoryMap::
+operator!=(InferredRegCategoryMap const& rhs) const {
+
+  return !(*this == rhs);
+}
+
+
+} // namespace compiler_llvm
+} // namespace art
diff --git a/src/compiler_llvm/inferred_reg_category_map.h b/src/compiler_llvm/inferred_reg_category_map.h
new file mode 100644
index 0000000..55d64fe
--- /dev/null
+++ b/src/compiler_llvm/inferred_reg_category_map.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2012 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.
+ */
+
+#ifndef ART_SRC_COMPILER_LLVM_INFERRED_REG_CATEGORY_MAP_H_
+#define ART_SRC_COMPILER_LLVM_INFERRED_REG_CATEGORY_MAP_H_
+
+#include "backend_types.h"
+
+#include <stdint.h>
+#include <vector>
+
+namespace art {
+namespace compiler_llvm {
+
+
+class InferredRegCategoryMap {
+ private:
+  typedef std::vector<uint8_t> RegCategoryLine;
+
+ public:
+  InferredRegCategoryMap(uint32_t insns_size_in_code_units, uint8_t regs_size);
+
+  ~InferredRegCategoryMap();
+
+  RegCategory GetRegCategory(uint32_t dex_pc, uint16_t reg_idx) const;
+  void SetRegCategory(uint32_t dex_pc, uint16_t reg_idx, RegCategory cat);
+
+  bool operator==(InferredRegCategoryMap const& rhs) const;
+  bool operator!=(InferredRegCategoryMap const& rhs) const;
+
+ private:
+  uint16_t registers_size_;
+
+  std::vector<RegCategoryLine*> lines_;
+
+  DISALLOW_COPY_AND_ASSIGN(InferredRegCategoryMap);
+};
+
+
+} // namespace compiler_llvm
+} // namespace art
+
+#endif // ART_SRC_COMPILER_LLVM_INFERRED_REG_CATEGORY_MAP_H_
diff --git a/src/compiler_llvm/method_compiler.cc b/src/compiler_llvm/method_compiler.cc
index cf0ea3e..767897c 100644
--- a/src/compiler_llvm/method_compiler.cc
+++ b/src/compiler_llvm/method_compiler.cc
@@ -16,6 +16,7 @@
 
 #include "method_compiler.h"
 
+#include "backend_types.h"
 #include "compiler.h"
 #include "ir_builder.h"
 #include "logging.h"
diff --git a/src/compiler_llvm/method_compiler.h b/src/compiler_llvm/method_compiler.h
index 8d9820a..7b1154d 100644
--- a/src/compiler_llvm/method_compiler.h
+++ b/src/compiler_llvm/method_compiler.h
@@ -17,6 +17,7 @@
 #ifndef ART_SRC_COMPILER_LLVM_METHOD_COMPILER_H_
 #define ART_SRC_COMPILER_LLVM_METHOD_COMPILER_H_
 
+#include "backend_types.h"
 #include "constants.h"
 #include "dex_file.h"
 #include "dex_instruction.h"
diff --git a/src/dex_verifier.cc b/src/dex_verifier.cc
index 077bb32..5c31090 100644
--- a/src/dex_verifier.cc
+++ b/src/dex_verifier.cc
@@ -32,6 +32,12 @@
 #include "runtime.h"
 #include "stringpiece.h"
 
+#if defined(ART_USE_LLVM_COMPILER)
+#include "compiler_llvm/backend_types.h"
+#include "compiler_llvm/inferred_reg_category_map.h"
+using namespace art::compiler_llvm;
+#endif
+
 namespace art {
 namespace verifier {
 
@@ -1608,7 +1614,15 @@
   const std::vector<uint8_t>* gc_map = CreateLengthPrefixedGcMap(*(map.get()));
   Compiler::MethodReference ref(dex_file_, method_->GetDexMethodIndex());
   verifier::DexVerifier::SetGcMap(ref, *gc_map);
+
+#if !defined(ART_USE_LLVM_COMPILER)
   method_->SetGcMap(&gc_map->at(0));
+#else
+  /* Generate Inferred Register Category for LLVM-based Code Generator */
+  const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
+  method_->SetInferredRegCategoryMap(table);
+#endif
+
   return true;
 }
 
@@ -3933,5 +3947,37 @@
   STLDeleteValues(&gc_maps_);
 }
 
+#if defined(ART_USE_LLVM_COMPILER)
+InferredRegCategoryMap const* DexVerifier::GenerateInferredRegCategoryMap() {
+  uint32_t insns_size = code_item_->insns_size_in_code_units_;
+  uint16_t regs_size = code_item_->registers_size_;
+
+  UniquePtr<InferredRegCategoryMap> table(
+    new InferredRegCategoryMap(insns_size, regs_size));
+
+  for (size_t i = 0; i < insns_size; ++i) {
+    if (RegisterLine* line = reg_table_.GetLine(i)) {
+      for (size_t r = 0; r < regs_size; ++r) {
+        RegType const &rt = line->GetRegisterType(r);
+
+        if (rt.IsZero()) {
+          table->SetRegCategory(i, r, kRegZero);
+        } else if (rt.IsCategory1Types()) {
+          table->SetRegCategory(i, r, kRegCat1nr);
+        } else if (rt.IsCategory2Types()) {
+          table->SetRegCategory(i, r, kRegCat2);
+        } else if (rt.IsReferenceTypes()) {
+          table->SetRegCategory(i, r, kRegObject);
+        } else {
+          table->SetRegCategory(i, r, kRegUnknown);
+        }
+      }
+    }
+  }
+
+  return table.release();
+}
+#endif
+
 }  // namespace verifier
 }  // namespace art
diff --git a/src/dex_verifier.h b/src/dex_verifier.h
index 41adff0..eb0508b 100644
--- a/src/dex_verifier.h
+++ b/src/dex_verifier.h
@@ -33,6 +33,12 @@
 
 namespace art {
 
+#if defined(ART_USE_LLVM_COMPILER)
+namespace compiler_llvm {
+  class InferredRegCategoryMap;
+}
+#endif
+
 namespace verifier {
 
 class DexVerifier;
@@ -1222,6 +1228,14 @@
   */
   bool UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line);
 
+#if defined(ART_USE_LLVM_COMPILER)
+  /*
+   * Generate the inferred register category for LLVM-based code generator.
+   * Returns a pointer to a two-dimension Class array, or NULL on failure.
+   */
+  const compiler_llvm::InferredRegCategoryMap* GenerateInferredRegCategoryMap();
+#endif
+
   /*
    * Generate the GC map for a method that has just been verified (i.e. we're doing this as part of
    * verification). For type-precise determination we have all the data we need, so we just need to
diff --git a/src/object.cc b/src/object.cc
index 22ee742..698a985 100644
--- a/src/object.cc
+++ b/src/object.cc
@@ -37,6 +37,11 @@
 #include "stack.h"
 #include "utils.h"
 
+#if defined(ART_USE_LLVM_COMPILER)
+#include "compiler_llvm/inferred_reg_category_map.h"
+using art::compiler_llvm::InferredRegCategoryMap;
+#endif
+
 namespace art {
 
 String* Object::AsString() {
@@ -378,6 +383,34 @@
       new_value, false);
 }
 
+#if defined(ART_USE_LLVM_COMPILER)
+
+const InferredRegCategoryMap* Method::GetInferredRegCategoryMap() const {
+  const InferredRegCategoryMap* map = GetFieldPtr<const InferredRegCategoryMap*>(
+      OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
+  DCHECK(map != NULL) << PrettyMethod(this);
+  return map;
+}
+
+void Method::SetInferredRegCategoryMap(const InferredRegCategoryMap* map) {
+  const InferredRegCategoryMap* existing_map = GetInferredRegCategoryMap();
+
+  DCHECK(existing_map == NULL) << PrettyMethod(this);
+  DCHECK(map != NULL) << PrettyMethod(this);
+
+  // TODO: Remove if we won't find any use of InferredRegCategoryMap at runtime.
+  SetFieldPtr<const InferredRegCategoryMap*>(
+      OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), map, false);
+}
+
+void Method::ResetInferredRegCategoryMap() {
+  delete GetInferredRegCategoryMap();
+  SetFieldPtr<const InferredRegCategoryMap*>(
+    OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), NULL, false);
+}
+
+#endif
+
 size_t Method::NumArgRegisters(const StringPiece& shorty) {
   CHECK_LE(1, shorty.length());
   uint32_t num_registers = 0;
diff --git a/src/object.h b/src/object.h
index 6092ce4..5a664ff 100644
--- a/src/object.h
+++ b/src/object.h
@@ -72,6 +72,12 @@
   Object* l;
 };
 
+#if defined(ART_USE_LLVM_COMPILER)
+namespace compiler_llvm {
+  class InferredRegCategoryMap;
+}
+#endif
+
 static const uint32_t kAccPublic = 0x0001;  // class, field, method, ic
 static const uint32_t kAccPrivate = 0x0002;  // field, method, ic
 static const uint32_t kAccProtected = 0x0004;  // field, method, ic
@@ -726,6 +732,16 @@
     SetGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
   }
 
+#if defined(ART_USE_LLVM_COMPILER)
+  // NOTE: In order not to change the Oat file format, we are reusing the
+  // gc_map_ field, so be careful while altering the GC map related code.
+
+  const compiler_llvm::InferredRegCategoryMap* GetInferredRegCategoryMap() const;
+
+  void SetInferredRegCategoryMap(const compiler_llvm::InferredRegCategoryMap* map);
+  void ResetInferredRegCategoryMap();
+#endif
+
   size_t GetFrameSizeInBytes() const {
     DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
     size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);