Use the non access check entrypoint when possible.

Change-Id: I0b53d63141395e26816d5d2ce3fa6a297bb39b54
diff --git a/compiler/optimizing/bounds_check_elimination_test.cc b/compiler/optimizing/bounds_check_elimination_test.cc
index b6aaab5..3dcb08d 100644
--- a/compiler/optimizing/bounds_check_elimination_test.cc
+++ b/compiler/optimizing/bounds_check_elimination_test.cc
@@ -652,7 +652,7 @@
   graph->AddBlock(block);
   entry->AddSuccessor(block);
   HInstruction* new_array = new (allocator)
-      HNewArray(constant_10, 0, Primitive::kPrimInt);
+      HNewArray(constant_10, 0, Primitive::kPrimInt, kQuickAllocArray);
   block->AddInstruction(new_array);
   block->AddInstruction(new (allocator) HGoto());
 
diff --git a/compiler/optimizing/builder.cc b/compiler/optimizing/builder.cc
index 955deaa..4b97a62 100644
--- a/compiler/optimizing/builder.cc
+++ b/compiler/optimizing/builder.cc
@@ -847,7 +847,10 @@
                                         uint32_t* args,
                                         uint32_t register_index) {
   HInstruction* length = GetIntConstant(number_of_vreg_arguments);
-  HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index);
+  QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
+      ? kQuickAllocArrayWithAccessCheck
+      : kQuickAllocArray;
+  HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index, entrypoint);
   current_block_->AddInstruction(object);
 
   const char* descriptor = dex_file_->StringByTypeIdx(type_index);
@@ -987,6 +990,11 @@
   return true;
 }
 
+bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const {
+  return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks(
+      dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index);
+}
+
 void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) {
   SwitchTable table(instruction, dex_pc, false);
 
@@ -1772,16 +1780,24 @@
     }
 
     case Instruction::NEW_INSTANCE: {
-      current_block_->AddInstruction(
-          new (arena_) HNewInstance(dex_pc, instruction.VRegB_21c()));
+      uint16_t type_index = instruction.VRegB_21c();
+      QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
+          ? kQuickAllocObjectWithAccessCheck
+          : kQuickAllocObject;
+
+      current_block_->AddInstruction(new (arena_) HNewInstance(dex_pc, type_index, entrypoint));
       UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
       break;
     }
 
     case Instruction::NEW_ARRAY: {
+      uint16_t type_index = instruction.VRegC_22c();
       HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
+      QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index)
+          ? kQuickAllocArrayWithAccessCheck
+          : kQuickAllocArray;
       current_block_->AddInstruction(
-          new (arena_) HNewArray(length, dex_pc, instruction.VRegC_22c()));
+          new (arena_) HNewArray(length, dex_pc, type_index, entrypoint));
       UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
       break;
     }
diff --git a/compiler/optimizing/builder.h b/compiler/optimizing/builder.h
index cc5f6a0..592db23 100644
--- a/compiler/optimizing/builder.h
+++ b/compiler/optimizing/builder.h
@@ -108,6 +108,7 @@
   HInstruction* LoadLocal(int register_index, Primitive::Type type) const;
   void PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_pc);
   void InitializeParameters(uint16_t number_of_parameters);
+  bool NeedsAccessCheck(uint32_t type_index) const;
 
   template<typename T>
   void Unop_12x(const Instruction& instruction, Primitive::Type type);
diff --git a/compiler/optimizing/code_generator_arm.cc b/compiler/optimizing/code_generator_arm.cc
index 7731a10..b0cd7ba 100644
--- a/compiler/optimizing/code_generator_arm.cc
+++ b/compiler/optimizing/code_generator_arm.cc
@@ -2416,8 +2416,9 @@
   InvokeRuntimeCallingConvention calling_convention;
   codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
   __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
-  codegen_->InvokeRuntime(
-      QUICK_ENTRY_POINT(pAllocObjectWithAccessCheck), instruction, instruction->GetDexPc());
+  codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
+                          instruction,
+                          instruction->GetDexPc());
 }
 
 void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
@@ -2434,8 +2435,9 @@
   InvokeRuntimeCallingConvention calling_convention;
   codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
   __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
-  codegen_->InvokeRuntime(
-      QUICK_ENTRY_POINT(pAllocArrayWithAccessCheck), instruction, instruction->GetDexPc());
+  codegen_->InvokeRuntime(GetThreadOffset<kArmWordSize>(instruction->GetEntrypoint()).Int32Value(),
+                          instruction,
+                          instruction->GetDexPc());
 }
 
 void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
diff --git a/compiler/optimizing/code_generator_arm64.cc b/compiler/optimizing/code_generator_arm64.cc
index 78ae55e..3bc23fe 100644
--- a/compiler/optimizing/code_generator_arm64.cc
+++ b/compiler/optimizing/code_generator_arm64.cc
@@ -2112,7 +2112,9 @@
   codegen_->LoadCurrentMethod(current_method);
   __ Mov(type_index, instruction->GetTypeIndex());
   codegen_->InvokeRuntime(
-      QUICK_ENTRY_POINT(pAllocArrayWithAccessCheck), instruction, instruction->GetDexPc());
+      GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
+      instruction,
+      instruction->GetDexPc());
   CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
                        void*, uint32_t, int32_t, mirror::ArtMethod*>();
 }
@@ -2136,7 +2138,9 @@
   codegen_->LoadCurrentMethod(current_method);
   __ Mov(type_index, instruction->GetTypeIndex());
   codegen_->InvokeRuntime(
-      QUICK_ENTRY_POINT(pAllocObjectWithAccessCheck), instruction, instruction->GetDexPc());
+      GetThreadOffset<kArm64WordSize>(instruction->GetEntrypoint()).Int32Value(),
+      instruction,
+      instruction->GetDexPc());
   CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, mirror::ArtMethod*>();
 }
 
diff --git a/compiler/optimizing/code_generator_x86.cc b/compiler/optimizing/code_generator_x86.cc
index 063550b..c840793 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -17,6 +17,7 @@
 #include "code_generator_x86.h"
 
 #include "entrypoints/quick/quick_entrypoints.h"
+#include "entrypoints/quick/quick_entrypoints_enum.h"
 #include "gc/accounting/card_table.h"
 #include "mirror/array-inl.h"
 #include "mirror/art_method.h"
@@ -2555,8 +2556,7 @@
   codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
   __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
 
-  __ fs()->call(
-      Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
+  __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
 
   codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
   DCHECK(!codegen_->IsLeafMethod());
@@ -2577,8 +2577,7 @@
   codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
   __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
 
-  __ fs()->call(
-      Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
+  __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
 
   codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
   DCHECK(!codegen_->IsLeafMethod());
diff --git a/compiler/optimizing/code_generator_x86_64.cc b/compiler/optimizing/code_generator_x86_64.cc
index 90b7bda..2ff53a0 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -2424,8 +2424,8 @@
   codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
   __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
 
-  __ gs()->call(Address::Absolute(
-      QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
+  __ gs()->call(
+      Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
 
   DCHECK(!codegen_->IsLeafMethod());
   codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
@@ -2446,8 +2446,8 @@
   codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(2)));
   __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
 
-  __ gs()->call(Address::Absolute(
-      QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
+  __ gs()->call(
+      Address::Absolute(GetThreadOffset<kX86_64WordSize>(instruction->GetEntrypoint()), true));
 
   DCHECK(!codegen_->IsLeafMethod());
   codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h
index dfb03c3..6f7bc0c 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -17,6 +17,7 @@
 #ifndef ART_COMPILER_OPTIMIZING_NODES_H_
 #define ART_COMPILER_OPTIMIZING_NODES_H_
 
+#include "entrypoints/quick/quick_entrypoints_enum.h"
 #include "invoke_type.h"
 #include "locations.h"
 #include "offsets.h"
@@ -1794,10 +1795,11 @@
 
 class HNewInstance : public HExpression<0> {
  public:
-  HNewInstance(uint32_t dex_pc, uint16_t type_index)
+  HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
       : HExpression(Primitive::kPrimNot, SideEffects::None()),
         dex_pc_(dex_pc),
-        type_index_(type_index) {}
+        type_index_(type_index),
+        entrypoint_(entrypoint) {}
 
   uint32_t GetDexPc() const { return dex_pc_; }
   uint16_t GetTypeIndex() const { return type_index_; }
@@ -1812,11 +1814,14 @@
 
   bool CanBeNull() const OVERRIDE { return false; }
 
+  QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
+
   DECLARE_INSTRUCTION(NewInstance);
 
  private:
   const uint32_t dex_pc_;
   const uint16_t type_index_;
+  const QuickEntrypointEnum entrypoint_;
 
   DISALLOW_COPY_AND_ASSIGN(HNewInstance);
 };
@@ -1837,10 +1842,14 @@
 
 class HNewArray : public HExpression<1> {
  public:
-  HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
+  HNewArray(HInstruction* length,
+            uint32_t dex_pc,
+            uint16_t type_index,
+            QuickEntrypointEnum entrypoint)
       : HExpression(Primitive::kPrimNot, SideEffects::None()),
         dex_pc_(dex_pc),
-        type_index_(type_index) {
+        type_index_(type_index),
+        entrypoint_(entrypoint) {
     SetRawInputAt(0, length);
   }
 
@@ -1852,11 +1861,14 @@
 
   bool CanBeNull() const OVERRIDE { return false; }
 
+  QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
+
   DECLARE_INSTRUCTION(NewArray);
 
  private:
   const uint32_t dex_pc_;
   const uint16_t type_index_;
+  const QuickEntrypointEnum entrypoint_;
 
   DISALLOW_COPY_AND_ASSIGN(HNewArray);
 };