Home | History | Annotate | Download | only in src
      1 // Copyright 2013 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #include "hydrogen-deoptimizing-mark.h"
     29 
     30 namespace v8 {
     31 namespace internal {
     32 
     33 void HPropagateDeoptimizingMarkPhase::MarkAsDeoptimizing() {
     34   HBasicBlock* block = graph()->entry_block();
     35   ZoneList<HBasicBlock*> stack(graph()->blocks()->length(), zone());
     36   while (block != NULL) {
     37     const ZoneList<HBasicBlock*>* dominated_blocks(block->dominated_blocks());
     38     if (!dominated_blocks->is_empty()) {
     39       if (block->IsDeoptimizing()) {
     40         for (int i = 0; i < dominated_blocks->length(); ++i) {
     41           dominated_blocks->at(i)->MarkAsDeoptimizing();
     42         }
     43       }
     44       for (int i = 1; i < dominated_blocks->length(); ++i) {
     45         stack.Add(dominated_blocks->at(i), zone());
     46       }
     47       block = dominated_blocks->at(0);
     48     } else if (!stack.is_empty()) {
     49       // Pop next block from stack.
     50       block = stack.RemoveLast();
     51     } else {
     52       // All blocks processed.
     53       block = NULL;
     54     }
     55   }
     56 }
     57 
     58 
     59 void HPropagateDeoptimizingMarkPhase::NullifyUnreachableInstructions() {
     60   if (!FLAG_unreachable_code_elimination) return;
     61   for (int i = 0; i < graph()->blocks()->length(); ++i) {
     62     HBasicBlock* block = graph()->blocks()->at(i);
     63     bool nullify = false;
     64     const ZoneList<HBasicBlock*>* predecessors = block->predecessors();
     65     int predecessors_length = predecessors->length();
     66     bool all_predecessors_deoptimizing = (predecessors_length > 0);
     67     for (int j = 0; j < predecessors_length; ++j) {
     68       if (!predecessors->at(j)->IsDeoptimizing()) {
     69         all_predecessors_deoptimizing = false;
     70         break;
     71       }
     72     }
     73     if (all_predecessors_deoptimizing) nullify = true;
     74     for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
     75       HInstruction* instr = it.Current();
     76       // Leave the basic structure of the graph intact.
     77       if (instr->IsBlockEntry()) continue;
     78       if (instr->IsControlInstruction()) continue;
     79       if (instr->IsSimulate()) continue;
     80       if (instr->IsEnterInlined()) continue;
     81       if (instr->IsLeaveInlined()) continue;
     82       if (nullify) {
     83         HInstruction* last_dummy = NULL;
     84         for (int j = 0; j < instr->OperandCount(); ++j) {
     85           HValue* operand = instr->OperandAt(j);
     86           // Insert an HDummyUse for each operand, unless the operand
     87           // is an HDummyUse itself. If it's even from the same block,
     88           // remember it as a potential replacement for the instruction.
     89           if (operand->IsDummyUse()) {
     90             if (operand->block() == instr->block() &&
     91                 last_dummy == NULL) {
     92               last_dummy = HInstruction::cast(operand);
     93             }
     94             continue;
     95           }
     96           if (operand->IsControlInstruction()) {
     97             // Inserting a dummy use for a value that's not defined anywhere
     98             // will fail. Some instructions define fake inputs on such
     99             // values as control flow dependencies.
    100             continue;
    101           }
    102           HDummyUse* dummy = new(graph()->zone()) HDummyUse(operand);
    103           dummy->InsertBefore(instr);
    104           last_dummy = dummy;
    105         }
    106         if (last_dummy == NULL) last_dummy = graph()->GetConstant1();
    107         instr->DeleteAndReplaceWith(last_dummy);
    108         continue;
    109       }
    110       if (instr->IsDeoptimize()) {
    111         ASSERT(block->IsDeoptimizing());
    112         nullify = true;
    113       }
    114     }
    115   }
    116 }
    117 
    118 
    119 void HPropagateDeoptimizingMarkPhase::Run() {
    120   // Skip this phase if there is nothing to be done anyway.
    121   if (!graph()->has_soft_deoptimize()) return;
    122   MarkAsDeoptimizing();
    123   NullifyUnreachableInstructions();
    124 }
    125 
    126 } }  // namespace v8::internal
    127