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-representation-changes.h"
     29 
     30 namespace v8 {
     31 namespace internal {
     32 
     33 void HRepresentationChangesPhase::InsertRepresentationChangeForUse(
     34     HValue* value, HValue* use_value, int use_index, Representation to) {
     35   // Insert the representation change right before its use. For phi-uses we
     36   // insert at the end of the corresponding predecessor.
     37   HInstruction* next = NULL;
     38   if (use_value->IsPhi()) {
     39     next = use_value->block()->predecessors()->at(use_index)->end();
     40   } else {
     41     next = HInstruction::cast(use_value);
     42   }
     43   // For constants we try to make the representation change at compile
     44   // time. When a representation change is not possible without loss of
     45   // information we treat constants like normal instructions and insert the
     46   // change instructions for them.
     47   HInstruction* new_value = NULL;
     48   bool is_truncating_to_smi = use_value->CheckFlag(HValue::kTruncatingToSmi);
     49   bool is_truncating_to_int = use_value->CheckFlag(HValue::kTruncatingToInt32);
     50   if (value->IsConstant()) {
     51     HConstant* constant = HConstant::cast(value);
     52     // Try to create a new copy of the constant with the new representation.
     53     if (is_truncating_to_int && to.IsInteger32()) {
     54       Maybe<HConstant*> res = constant->CopyToTruncatedInt32(graph()->zone());
     55       if (res.has_value) new_value = res.value;
     56     } else {
     57       new_value = constant->CopyToRepresentation(to, graph()->zone());
     58     }
     59   }
     60 
     61   if (new_value == NULL) {
     62     new_value = new(graph()->zone()) HChange(
     63         value, to, is_truncating_to_smi, is_truncating_to_int);
     64   }
     65 
     66   new_value->InsertBefore(next);
     67   use_value->SetOperandAt(use_index, new_value);
     68 }
     69 
     70 
     71 void HRepresentationChangesPhase::InsertRepresentationChangesForValue(
     72     HValue* value) {
     73   Representation r = value->representation();
     74   if (r.IsNone()) return;
     75   if (value->HasNoUses()) return;
     76 
     77   for (HUseIterator it(value->uses()); !it.Done(); it.Advance()) {
     78     HValue* use_value = it.value();
     79     int use_index = it.index();
     80     Representation req = use_value->RequiredInputRepresentation(use_index);
     81     if (req.IsNone() || req.Equals(r)) continue;
     82     InsertRepresentationChangeForUse(value, use_value, use_index, req);
     83   }
     84   if (value->HasNoUses()) {
     85     ASSERT(value->IsConstant());
     86     value->DeleteAndReplaceWith(NULL);
     87   }
     88 
     89   // The only purpose of a HForceRepresentation is to represent the value
     90   // after the (possible) HChange instruction.  We make it disappear.
     91   if (value->IsForceRepresentation()) {
     92     value->DeleteAndReplaceWith(HForceRepresentation::cast(value)->value());
     93   }
     94 }
     95 
     96 
     97 void HRepresentationChangesPhase::Run() {
     98   // Compute truncation flag for phis: Initially assume that all
     99   // int32-phis allow truncation and iteratively remove the ones that
    100   // are used in an operation that does not allow a truncating
    101   // conversion.
    102   ZoneList<HPhi*> worklist(8, zone());
    103 
    104   const ZoneList<HPhi*>* phi_list(graph()->phi_list());
    105   for (int i = 0; i < phi_list->length(); i++) {
    106     HPhi* phi = phi_list->at(i);
    107     if (phi->representation().IsInteger32()) {
    108       phi->SetFlag(HValue::kTruncatingToInt32);
    109     } else if (phi->representation().IsSmi()) {
    110       phi->SetFlag(HValue::kTruncatingToSmi);
    111     }
    112   }
    113 
    114   for (int i = 0; i < phi_list->length(); i++) {
    115     HPhi* phi = phi_list->at(i);
    116     for (HUseIterator it(phi->uses()); !it.Done(); it.Advance()) {
    117       // If a Phi is used as a non-truncating int32 or as a double,
    118       // clear its "truncating" flag.
    119       HValue* use = it.value();
    120       Representation input_representation =
    121           use->RequiredInputRepresentation(it.index());
    122       if ((phi->representation().IsInteger32() &&
    123            !(input_representation.IsInteger32() &&
    124              use->CheckFlag(HValue::kTruncatingToInt32))) ||
    125           (phi->representation().IsSmi() &&
    126            !(input_representation.IsSmi() &&
    127              use->CheckFlag(HValue::kTruncatingToSmi)))) {
    128         if (FLAG_trace_representation) {
    129           PrintF("#%d Phi is not truncating because of #%d %s\n",
    130                  phi->id(), it.value()->id(), it.value()->Mnemonic());
    131         }
    132         phi->ClearFlag(HValue::kTruncatingToInt32);
    133         phi->ClearFlag(HValue::kTruncatingToSmi);
    134         worklist.Add(phi, zone());
    135         break;
    136       }
    137     }
    138   }
    139 
    140   while (!worklist.is_empty()) {
    141     HPhi* current = worklist.RemoveLast();
    142     for (int i = 0; i < current->OperandCount(); ++i) {
    143       HValue* input = current->OperandAt(i);
    144       if (input->IsPhi() &&
    145           ((input->representation().IsInteger32() &&
    146             input->CheckFlag(HValue::kTruncatingToInt32)) ||
    147            (input->representation().IsSmi() &&
    148             input->CheckFlag(HValue::kTruncatingToSmi)))) {
    149         if (FLAG_trace_representation) {
    150           PrintF("#%d Phi is not truncating because of #%d %s\n",
    151                  input->id(), current->id(), current->Mnemonic());
    152         }
    153         input->ClearFlag(HValue::kTruncatingToInt32);
    154         input->ClearFlag(HValue::kTruncatingToSmi);
    155         worklist.Add(HPhi::cast(input), zone());
    156       }
    157     }
    158   }
    159 
    160   const ZoneList<HBasicBlock*>* blocks(graph()->blocks());
    161   for (int i = 0; i < blocks->length(); ++i) {
    162     // Process phi instructions first.
    163     const HBasicBlock* block(blocks->at(i));
    164     const ZoneList<HPhi*>* phis = block->phis();
    165     for (int j = 0; j < phis->length(); j++) {
    166       InsertRepresentationChangesForValue(phis->at(j));
    167     }
    168 
    169     // Process normal instructions.
    170     for (HInstruction* current = block->first(); current != NULL; ) {
    171       HInstruction* next = current->next();
    172       InsertRepresentationChangesForValue(current);
    173       current = next;
    174     }
    175   }
    176 }
    177 
    178 } }  // namespace v8::internal
    179