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-infer-representation.h"
     29 
     30 namespace v8 {
     31 namespace internal {
     32 
     33 void HInferRepresentationPhase::AddToWorklist(HValue* current) {
     34   if (current->representation().IsTagged()) return;
     35   if (!current->CheckFlag(HValue::kFlexibleRepresentation)) return;
     36   if (in_worklist_.Contains(current->id())) return;
     37   worklist_.Add(current, zone());
     38   in_worklist_.Add(current->id());
     39 }
     40 
     41 
     42 void HInferRepresentationPhase::Run() {
     43   // (1) Initialize bit vectors and count real uses. Each phi gets a
     44   // bit-vector of length <number of phis>.
     45   const ZoneList<HPhi*>* phi_list = graph()->phi_list();
     46   int phi_count = phi_list->length();
     47   ZoneList<BitVector*> connected_phis(phi_count, zone());
     48   for (int i = 0; i < phi_count; ++i) {
     49     phi_list->at(i)->InitRealUses(i);
     50     BitVector* connected_set = new(zone()) BitVector(phi_count, zone());
     51     connected_set->Add(i);
     52     connected_phis.Add(connected_set, zone());
     53   }
     54 
     55   // (2) Do a fixed point iteration to find the set of connected phis.  A
     56   // phi is connected to another phi if its value is used either directly or
     57   // indirectly through a transitive closure of the def-use relation.
     58   bool change = true;
     59   while (change) {
     60     change = false;
     61     // We normally have far more "forward edges" than "backward edges",
     62     // so we terminate faster when we walk backwards.
     63     for (int i = phi_count - 1; i >= 0; --i) {
     64       HPhi* phi = phi_list->at(i);
     65       for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) {
     66         HValue* use = it.value();
     67         if (use->IsPhi()) {
     68           int id = HPhi::cast(use)->phi_id();
     69           if (connected_phis[i]->UnionIsChanged(*connected_phis[id]))
     70             change = true;
     71         }
     72       }
     73     }
     74   }
     75 
     76   // Set truncation flags for groups of connected phis. This is a conservative
     77   // approximation; the flag will be properly re-computed after representations
     78   // have been determined.
     79   if (phi_count > 0) {
     80     BitVector done(phi_count, zone());
     81     for (int i = 0; i < phi_count; ++i) {
     82       if (done.Contains(i)) continue;
     83 
     84       // Check if all uses of all connected phis in this group are truncating.
     85       bool all_uses_everywhere_truncating = true;
     86       for (BitVector::Iterator it(connected_phis[i]);
     87            !it.Done();
     88            it.Advance()) {
     89         int index = it.Current();
     90         all_uses_everywhere_truncating &=
     91             phi_list->at(index)->CheckFlag(HInstruction::kTruncatingToInt32);
     92         done.Add(index);
     93       }
     94       if (all_uses_everywhere_truncating) {
     95         continue;  // Great, nothing to do.
     96       }
     97       // Clear truncation flag of this group of connected phis.
     98       for (BitVector::Iterator it(connected_phis[i]);
     99            !it.Done();
    100            it.Advance()) {
    101         int index = it.Current();
    102         phi_list->at(index)->ClearFlag(HInstruction::kTruncatingToInt32);
    103       }
    104     }
    105   }
    106 
    107   // Simplify constant phi inputs where possible.
    108   // This step uses kTruncatingToInt32 flags of phis.
    109   for (int i = 0; i < phi_count; ++i) {
    110     phi_list->at(i)->SimplifyConstantInputs();
    111   }
    112 
    113   // Use the phi reachability information from step 2 to
    114   // sum up the non-phi use counts of all connected phis.
    115   for (int i = 0; i < phi_count; ++i) {
    116     HPhi* phi = phi_list->at(i);
    117     for (BitVector::Iterator it(connected_phis[i]);
    118          !it.Done();
    119          it.Advance()) {
    120       int index = it.Current();
    121       HPhi* it_use = phi_list->at(index);
    122       if (index != i) phi->AddNonPhiUsesFrom(it_use);  // Don't count twice.
    123     }
    124   }
    125 
    126   // Initialize work list
    127   for (int i = 0; i < graph()->blocks()->length(); ++i) {
    128     HBasicBlock* block = graph()->blocks()->at(i);
    129     const ZoneList<HPhi*>* phis = block->phis();
    130     for (int j = 0; j < phis->length(); ++j) {
    131       AddToWorklist(phis->at(j));
    132     }
    133 
    134     for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
    135       HInstruction* current = it.Current();
    136       AddToWorklist(current);
    137     }
    138   }
    139 
    140   // Do a fixed point iteration, trying to improve representations
    141   while (!worklist_.is_empty()) {
    142     HValue* current = worklist_.RemoveLast();
    143     in_worklist_.Remove(current->id());
    144     current->InferRepresentation(this);
    145   }
    146 
    147   // Lastly: any instruction that we don't have representation information
    148   // for defaults to Tagged.
    149   for (int i = 0; i < graph()->blocks()->length(); ++i) {
    150     HBasicBlock* block = graph()->blocks()->at(i);
    151     const ZoneList<HPhi*>* phis = block->phis();
    152     for (int j = 0; j < phis->length(); ++j) {
    153       HPhi* phi = phis->at(j);
    154       if (phi->representation().IsNone()) {
    155         phi->ChangeRepresentation(Representation::Tagged());
    156       }
    157     }
    158     for (HInstructionIterator it(block); !it.Done(); it.Advance()) {
    159       HInstruction* current = it.Current();
    160       if (current->representation().IsNone() &&
    161           current->CheckFlag(HInstruction::kFlexibleRepresentation)) {
    162         if (current->CheckFlag(HInstruction::kCannotBeTagged)) {
    163           current->ChangeRepresentation(Representation::Double());
    164         } else {
    165           current->ChangeRepresentation(Representation::Tagged());
    166         }
    167       }
    168     }
    169   }
    170 }
    171 
    172 } }  // namespace v8::internal
    173