Ensure environment is ready when populating loop.

Rationale:
OSR requires the suspend check to already have an environment,
albeit just for testing irreducible loops. This CL fixes the
omission. Note, the error is spurious on OSR and writing a
unit or regression test for this is hard.

Test: test-art-host
Bug: 36950873
Change-Id: Ica89e18e10deb438dead79e2cc40dd00a60b529f
diff --git a/compiler/optimizing/loop_optimization.cc b/compiler/optimizing/loop_optimization.cc
index 42ed04d..0d84c4d 100644
--- a/compiler/optimizing/loop_optimization.cc
+++ b/compiler/optimizing/loop_optimization.cc
@@ -533,29 +533,25 @@
                                              kNoRegNumber,
                                              0,
                                              HPhi::ToPhiType(induc_type));
-  // Generate header.
+  // Generate header and prepare body.
   // for (i = lo; i < hi; i += step)
   //    <loop-body>
   HInstruction* cond = new (global_allocator_) HAboveOrEqual(vector_phi_, hi);
   vector_header_->AddPhi(vector_phi_);
   vector_header_->AddInstruction(cond);
   vector_header_->AddInstruction(new (global_allocator_) HIf(cond));
-  // Suspend check and environment.
-  HInstruction* suspend = vector_header_->GetFirstInstruction();
-  suspend->CopyEnvironmentFromWithLoopPhiAdjustment(
-      node->loop_info->GetSuspendCheck()->GetEnvironment(), vector_header_);
-  // Generate body.
   for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
     bool vectorized_def = VectorizeDef(node, it.Current(), /*generate_code*/ true);
     DCHECK(vectorized_def);
   }
+  // Generate body.
+  HEnvironment* env = vector_header_->GetFirstInstruction()->GetEnvironment();
   for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
     auto i = vector_map_->find(it.Current());
     if (i != vector_map_->end() && !i->second->IsInBlock()) {
       Insert(vector_body_, i->second);  // lays out in original order
       if (i->second->NeedsEnvironment()) {
-        i->second->CopyEnvironmentFromWithLoopPhiAdjustment(
-            suspend->GetEnvironment(), vector_header_);
+        i->second->CopyEnvironmentFromWithLoopPhiAdjustment(env, vector_header_);
       }
     }
   }
diff --git a/compiler/optimizing/nodes.cc b/compiler/optimizing/nodes.cc
index 5617e4b..e71fea9 100644
--- a/compiler/optimizing/nodes.cc
+++ b/compiler/optimizing/nodes.cc
@@ -2380,11 +2380,13 @@
   MakeRoomFor(&reverse_post_order_, 1, index_of_body - 1);
   reverse_post_order_[index_of_body] = new_body;
 
-  // Add gotos and suspend check (client must add conditional in header and copy environment).
+  // Add gotos and suspend check (client must add conditional in header).
   new_pre_header->AddInstruction(new (arena_) HGoto());
   HSuspendCheck* suspend_check = new (arena_) HSuspendCheck(header->GetDexPc());
   new_header->AddInstruction(suspend_check);
   new_body->AddInstruction(new (arena_) HGoto());
+  suspend_check->CopyEnvironmentFromWithLoopPhiAdjustment(
+      loop->GetSuspendCheck()->GetEnvironment(), header);
 
   // Update loop information.
   new_header->AddBackEdge(new_body);