Eliminatable CheckMaps replaced with if(true) or if(false).
authorishell@chromium.org <ishell@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 14 Jan 2014 16:06:40 +0000 (16:06 +0000)
committerishell@chromium.org <ishell@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Tue, 14 Jan 2014 16:06:40 +0000 (16:06 +0000)
R=titzer@chromium.org

Review URL: https://codereview.chromium.org/130613003

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18592 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/hydrogen-check-elimination.cc
src/hydrogen-flow-engine.h
src/hydrogen-instructions.cc
src/hydrogen.cc
src/hydrogen.h

index ae11042..90e8450 100644 (file)
@@ -132,8 +132,10 @@ class HCheckTable : public ZoneObject {
 
     // Branch-sensitive analysis for certain comparisons may add more facts
     // to the state for the successor on the true branch.
-    HControlInstruction* end = succ->predecessors()->at(0)->end();
-    if (succ->predecessors()->length() == 1 && end->SuccessorAt(0) == succ) {
+    HBasicBlock* pred_block = succ->predecessors()->at(0);
+    HControlInstruction* end = pred_block->end();
+    if (succ->predecessors()->length() == 1 && end->SuccessorAt(0) == succ &&
+        pred_block->IsReachable()) {
       if (end->IsCompareMap()) {
         // Learn on the true branch of if(CompareMap(x)).
         HCompareMap* cmp = HCompareMap::cast(end);
@@ -311,14 +313,27 @@ class HCheckTable : public ZoneObject {
   void ReduceCompareMap(HCompareMap* instr) {
     MapSet maps = FindMaps(instr->value()->ActualValue());
     if (maps == NULL) return;
+
+    TRACE(("CompareMap for #%d at B%d ",
+        instr->value()->ActualValue()->id(), instr->block()->block_id()));
+
     if (maps->Contains(instr->map())) {
       if (maps->size() == 1) {
-        // TODO(titzer): replace with goto true branch
         INC_STAT(compares_true_);
+
+        TRACE(("replaced with goto B%d (true target)\n",
+               instr->SuccessorAt(0)->block_id()));
+
+        instr->block()->ReplaceControlWithGotoSuccessor(0);
+      } else {
+        TRACE(("can't be replaced with goto: ambiguous set of maps\n"));
       }
     } else {
-      // TODO(titzer): replace with goto false branch
       INC_STAT(compares_false_);
+      TRACE(("replaced with goto B%d (false target)\n",
+             instr->SuccessorAt(1)->block_id()));
+
+      instr->block()->ReplaceControlWithGotoSuccessor(1);
     }
   }
 
index 4e12755..477fdb1 100644 (file)
@@ -124,17 +124,19 @@ class HFlowEngine {
       if (SkipNonDominatedBlock(root, block)) continue;
       State* state = StateAt(block);
 
-      if (block->IsLoopHeader()) {
-        // Apply loop effects before analyzing loop body.
-        ComputeLoopEffects(block)->Apply(state);
-      } else {
-        // Must have visited all predecessors before this block.
-        CheckPredecessorCount(block);
-      }
+      if (block->IsReachable()) {
+        if (block->IsLoopHeader()) {
+          // Apply loop effects before analyzing loop body.
+          ComputeLoopEffects(block)->Apply(state);
+        } else {
+          // Must have visited all predecessors before this block.
+          CheckPredecessorCount(block);
+        }
 
-      // Go through all instructions of the current block, updating the state.
-      for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
-        state = state->Process(it.Current(), zone_);
+        // Go through all instructions of the current block, updating the state.
+        for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
+          state = state->Process(it.Current(), zone_);
+        }
       }
 
       // Propagate the block state forward to all successor blocks.
@@ -185,6 +187,7 @@ class HFlowEngine {
         i = member->loop_information()->GetLastBackEdge()->block_id();
       } else {
         // Process all the effects of the block.
+        if (member->IsUnreachable()) continue;
         ASSERT(member->current_loop() == loop);
         for (HInstructionIterator it(member); !it.Done(); it.Advance()) {
           effects->Process(it.Current(), zone_);
index 5256169..c6feeb7 100644 (file)
@@ -707,7 +707,6 @@ void HInstruction::PrintMnemonicTo(StringStream* stream) {
 
 void HInstruction::Unlink() {
   ASSERT(IsLinked());
-  ASSERT(!IsControlInstruction());  // Must never move control instructions.
   ASSERT(!IsBlockEntry());  // Doesn't make sense to delete these.
   ASSERT(previous_ != NULL);
   previous_->next_ = next_;
index 7d6df4f..7841e1f 100644 (file)
@@ -313,6 +313,30 @@ int HBasicBlock::LoopNestingDepth() const {
 }
 
 
+void HBasicBlock::ReplaceControlWithGotoSuccessor(int succ) {
+  ASSERT(IsFinished());
+  ASSERT(end()->SuccessorCount() == 2);  // Only this case is supported yet.
+  ASSERT(succ < end()->SuccessorCount());
+
+  int unreachable_succ = 1 - succ;
+
+  // Replace control instruction with if (true) {succ} else {unreachable_succ}.
+  HBranch* new_branch = HBranch::New(
+      zone(),
+      NULL,
+      graph()->GetConstantTrue(),
+      ToBooleanStub::Types(ToBooleanStub::BOOLEAN),
+      end()->SuccessorAt(succ),
+      end()->SuccessorAt(unreachable_succ));
+
+  MarkSuccEdgeUnreachable(unreachable_succ);
+
+  end()->DeleteAndReplaceWith(end()->ActualValue());
+  new_branch->InsertAfter(last());
+  end_ = new_branch;
+}
+
+
 void HBasicBlock::PostProcessLoopHeader(IterationStatement* stmt) {
   ASSERT(IsLoopHeader());
 
@@ -331,6 +355,15 @@ void HBasicBlock::PostProcessLoopHeader(IterationStatement* stmt) {
 }
 
 
+void HBasicBlock::MarkSuccEdgeUnreachable(int succ) {
+  ASSERT(IsFinished());
+  HBasicBlock* succ_block = end()->SuccessorAt(succ);
+
+  ASSERT(succ_block->predecessors()->length() == 1);
+  succ_block->MarkUnreachable();
+}
+
+
 void HBasicBlock::RegisterPredecessor(HBasicBlock* pred) {
   if (HasPredecessor()) {
     // Only loop header blocks can have a predecessor added after
index 22394dc..cb99b55 100644 (file)
@@ -114,6 +114,8 @@ class HBasicBlock V8_FINAL : public ZoneObject {
   bool Dominates(HBasicBlock* other) const;
   int LoopNestingDepth() const;
 
+  void ReplaceControlWithGotoSuccessor(int succ);
+
   void SetInitialEnvironment(HEnvironment* env);
   void ClearEnvironment() {
     ASSERT(IsFinished());
@@ -200,6 +202,7 @@ class HBasicBlock V8_FINAL : public ZoneObject {
                        int position);
 
  private:
+  void MarkSuccEdgeUnreachable(int succ);
   void RegisterPredecessor(HBasicBlock* pred);
   void AddDominatedBlock(HBasicBlock* block);