Remove CodeAllocator and the extra copy of generated code.

The code used to copy the final generated code twice: from assembler to
CodeAllocator, and then to CodeAllocator to SwapAllocator/JitMemory.

The assemblers never depended on the exact location of the generated
code, so just drop that feature.

Test: test.py
Change-Id: I8dc82e4926097092b9aac336a5a5d40f79dc62ca
diff --git a/compiler/utils/arm/assembler_arm_vixl.cc b/compiler/utils/arm/assembler_arm_vixl.cc
index c7ca003..d64de09 100644
--- a/compiler/utils/arm/assembler_arm_vixl.cc
+++ b/compiler/utils/arm/assembler_arm_vixl.cc
@@ -52,7 +52,7 @@
   return vixl_masm_.GetBuffer().GetStartAddress<const uint8_t*>();
 }
 
-void ArmVIXLAssembler::FinalizeInstructions(const MemoryRegion& region) {
+void ArmVIXLAssembler::CopyInstructions(const MemoryRegion& region) {
   // Copy the instructions from the buffer.
   MemoryRegion from(vixl_masm_.GetBuffer()->GetStartAddress<void*>(), CodeSize());
   region.CopyFrom(0, from);
diff --git a/compiler/utils/arm/assembler_arm_vixl.h b/compiler/utils/arm/assembler_arm_vixl.h
index 025bba0..50dc06f 100644
--- a/compiler/utils/arm/assembler_arm_vixl.h
+++ b/compiler/utils/arm/assembler_arm_vixl.h
@@ -218,7 +218,7 @@
   const uint8_t* CodeBufferBaseAddress() const override;
 
   // Copy instructions out of assembly buffer into the given region of memory.
-  void FinalizeInstructions(const MemoryRegion& region) override;
+  void CopyInstructions(const MemoryRegion& region) override;
 
   void Bind([[maybe_unused]] Label* label) override {
     UNIMPLEMENTED(FATAL) << "Do not use Bind(Label*) for ARM";
diff --git a/compiler/utils/arm64/assembler_arm64.cc b/compiler/utils/arm64/assembler_arm64.cc
index 26dce7c..13acc7c 100644
--- a/compiler/utils/arm64/assembler_arm64.cc
+++ b/compiler/utils/arm64/assembler_arm64.cc
@@ -79,7 +79,7 @@
   return vixl_masm_.GetBuffer().GetStartAddress<const uint8_t*>();
 }
 
-void Arm64Assembler::FinalizeInstructions(const MemoryRegion& region) {
+void Arm64Assembler::CopyInstructions(const MemoryRegion& region) {
   // Copy the instructions from the buffer.
   MemoryRegion from(vixl_masm_.GetBuffer()->GetStartAddress<void*>(), CodeSize());
   region.CopyFrom(0, from);
diff --git a/compiler/utils/arm64/assembler_arm64.h b/compiler/utils/arm64/assembler_arm64.h
index 5eff8ca..ad6a8ed 100644
--- a/compiler/utils/arm64/assembler_arm64.h
+++ b/compiler/utils/arm64/assembler_arm64.h
@@ -91,7 +91,7 @@
   const uint8_t* CodeBufferBaseAddress() const override;
 
   // Copy instructions out of assembly buffer into the given region of memory.
-  void FinalizeInstructions(const MemoryRegion& region) override;
+  void CopyInstructions(const MemoryRegion& region) override;
 
   void LoadRawPtr(ManagedRegister dest, ManagedRegister base, Offset offs);
 
diff --git a/compiler/utils/assembler.cc b/compiler/utils/assembler.cc
index b82f0dc..1c04a3d 100644
--- a/compiler/utils/assembler.cc
+++ b/compiler/utils/assembler.cc
@@ -57,21 +57,24 @@
     fixup->Process(region, fixup->position());
     fixup = fixup->previous();
   }
-}
-
-
-void AssemblerBuffer::FinalizeInstructions(const MemoryRegion& instructions) {
-  // Copy the instructions from the buffer.
-  MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
-  instructions.CopyFrom(0, from);
-  // Process fixups in the instructions.
-  ProcessFixups(instructions);
 #ifndef NDEBUG
   fixups_processed_ = true;
 #endif
 }
 
 
+void AssemblerBuffer::ProcessFixups() {
+  MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
+  ProcessFixups(from);
+}
+
+
+void AssemblerBuffer::CopyInstructions(const MemoryRegion& instructions) {
+  MemoryRegion from(reinterpret_cast<void*>(contents()), Size());
+  instructions.CopyFrom(0, from);
+}
+
+
 void AssemblerBuffer::ExtendCapacity(size_t min_capacity) {
   size_t old_size = Size();
   size_t old_capacity = Capacity();
diff --git a/compiler/utils/assembler.h b/compiler/utils/assembler.h
index 63747be..f3fa711 100644
--- a/compiler/utils/assembler.h
+++ b/compiler/utils/assembler.h
@@ -163,9 +163,8 @@
 
   uint8_t* contents() const { return contents_; }
 
-  // Copy the assembled instructions into the specified memory block
-  // and apply all fixups.
-  void FinalizeInstructions(const MemoryRegion& region);
+  // Copy the assembled instructions into the specified memory block.
+  void CopyInstructions(const MemoryRegion& region);
 
   // To emit an instruction to the assembler buffer, the EnsureCapacity helper
   // must be used to guarantee that the underlying data area is big enough to
@@ -246,6 +245,8 @@
   // The provided `min_capacity` must be higher than current `Capacity()`.
   void ExtendCapacity(size_t min_capacity);
 
+  void ProcessFixups();
+
  private:
   // The limit is set to kMinimumGap bytes before the end of the data area.
   // This leaves enough space for the longest possible instruction and allows
@@ -357,7 +358,10 @@
 class Assembler : public DeletableArenaObject<kArenaAllocAssembler> {
  public:
   // Finalize the code; emit slow paths, fixup branches, add literal pool, etc.
-  virtual void FinalizeCode() { buffer_.EmitSlowPaths(this); }
+  virtual void FinalizeCode() {
+    buffer_.EmitSlowPaths(this);
+    buffer_.ProcessFixups();
+  }
 
   // Size of generated code
   virtual size_t CodeSize() const { return buffer_.Size(); }
@@ -375,8 +379,8 @@
   virtual size_t CodePosition() { return CodeSize(); }
 
   // Copy instructions out of assembly buffer into the given region of memory
-  virtual void FinalizeInstructions(const MemoryRegion& region) {
-    buffer_.FinalizeInstructions(region);
+  virtual void CopyInstructions(const MemoryRegion& region) {
+    buffer_.CopyInstructions(region);
   }
 
   // TODO: Implement with disassembler.
diff --git a/compiler/utils/assembler_test.h b/compiler/utils/assembler_test.h
index 810c843..c2e5064 100644
--- a/compiler/utils/assembler_test.h
+++ b/compiler/utils/assembler_test.h
@@ -1583,7 +1583,7 @@
     size_t cs = assembler_->CodeSize();
     std::unique_ptr<std::vector<uint8_t>> data(new std::vector<uint8_t>(cs));
     MemoryRegion code(&(*data)[0], data->size());
-    assembler_->FinalizeInstructions(code);
+    assembler_->CopyInstructions(code);
     Pad(*data);
     Driver(*data, assembly_text, test_name);
   }
diff --git a/compiler/utils/assembler_thumb_test.cc b/compiler/utils/assembler_thumb_test.cc
index 672cd3d..53cb3d6 100644
--- a/compiler/utils/assembler_thumb_test.cc
+++ b/compiler/utils/assembler_thumb_test.cc
@@ -79,7 +79,7 @@
     size_t cs = __ CodeSize();
     std::vector<uint8_t> managed_code(cs);
     MemoryRegion code(&managed_code[0], managed_code.size());
-    __ FinalizeInstructions(code);
+    __ CopyInstructions(code);
 
     DumpAndCheck(managed_code, testname, expected);
   }
diff --git a/compiler/utils/jni_macro_assembler.h b/compiler/utils/jni_macro_assembler.h
index 0c72970..286c378 100644
--- a/compiler/utils/jni_macro_assembler.h
+++ b/compiler/utils/jni_macro_assembler.h
@@ -92,7 +92,7 @@
   virtual size_t CodeSize() const = 0;
 
   // Copy instructions out of assembly buffer into the given region of memory
-  virtual void FinalizeInstructions(const MemoryRegion& region) = 0;
+  virtual void CopyInstructions(const MemoryRegion& region) = 0;
 
   // Emit code that will create an activation on the stack
   virtual void BuildFrame(size_t frame_size,
@@ -266,8 +266,8 @@
     return asm_.CodeSize();
   }
 
-  void FinalizeInstructions(const MemoryRegion& region) override {
-    asm_.FinalizeInstructions(region);
+  void CopyInstructions(const MemoryRegion& region) override {
+    asm_.CopyInstructions(region);
   }
 
   DebugFrameOpCodeWriterForAssembler& cfi() override {
diff --git a/compiler/utils/jni_macro_assembler_test.h b/compiler/utils/jni_macro_assembler_test.h
index 0d0a992..ff182e6 100644
--- a/compiler/utils/jni_macro_assembler_test.h
+++ b/compiler/utils/jni_macro_assembler_test.h
@@ -84,7 +84,7 @@
     size_t cs = assembler_->CodeSize();
     std::unique_ptr<std::vector<uint8_t>> data(new std::vector<uint8_t>(cs));
     MemoryRegion code(&(*data)[0], data->size());
-    assembler_->FinalizeInstructions(code);
+    assembler_->CopyInstructions(code);
     Pad(*data);
     Driver(*data, assembly_text, test_name);
   }
diff --git a/compiler/utils/riscv64/assembler_riscv64.cc b/compiler/utils/riscv64/assembler_riscv64.cc
index 4df3a7e..a09a23e 100644
--- a/compiler/utils/riscv64/assembler_riscv64.cc
+++ b/compiler/utils/riscv64/assembler_riscv64.cc
@@ -50,15 +50,12 @@
 }
 
 void Riscv64Assembler::FinalizeCode() {
+  Assembler::FinalizeCode();
   ReserveJumpTableSpace();
   EmitLiterals();
   PromoteBranches();
-}
-
-void Riscv64Assembler::FinalizeInstructions(const MemoryRegion& region) {
   EmitBranches();
   EmitJumpTables();
-  Assembler::FinalizeInstructions(region);
   PatchCFI();
 }
 
diff --git a/compiler/utils/riscv64/assembler_riscv64.h b/compiler/utils/riscv64/assembler_riscv64.h
index d94433f..fc0b442 100644
--- a/compiler/utils/riscv64/assembler_riscv64.h
+++ b/compiler/utils/riscv64/assembler_riscv64.h
@@ -593,12 +593,10 @@
   JumpTable* CreateJumpTable(ArenaVector<Riscv64Label*>&& labels);
 
  public:
-  // Emit slow paths queued during assembly and promote short branches to long if needed.
+  // Emit slow paths queued during assembly, promote short branches to long if needed,
+  // and emit branches.
   void FinalizeCode() override;
 
-  // Emit branches and finalize all instructions.
-  void FinalizeInstructions(const MemoryRegion& region) override;
-
   // Returns the current location of a label.
   //
   // This function must be used instead of `Riscv64Label::GetPosition()`
diff --git a/compiler/utils/riscv64/jni_macro_assembler_riscv64_test.cc b/compiler/utils/riscv64/jni_macro_assembler_riscv64_test.cc
index 3dc9f62..6a27885 100644
--- a/compiler/utils/riscv64/jni_macro_assembler_riscv64_test.cc
+++ b/compiler/utils/riscv64/jni_macro_assembler_riscv64_test.cc
@@ -47,7 +47,7 @@
     size_t cs = assembler_.CodeSize();
     std::vector<uint8_t> data(cs);
     MemoryRegion code(&data[0], data.size());
-    assembler_.FinalizeInstructions(code);
+    assembler_.CopyInstructions(code);
     Driver(data, assembly_text, test_name);
   }