Chromium Code Reviews| Index: src/hydrogen-bounds-check-removal.cc | 
| diff --git a/src/hydrogen-bounds-check-removal.cc b/src/hydrogen-bounds-check-removal.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..2c7359b649150e0c47a83a34e9784c869d13160e | 
| --- /dev/null | 
| +++ b/src/hydrogen-bounds-check-removal.cc | 
| @@ -0,0 +1,397 @@ | 
| +// Copyright 2013 the V8 project authors. All rights reserved. | 
| +// Redistribution and use in source and binary forms, with or without | 
| +// modification, are permitted provided that the following conditions are | 
| +// met: | 
| +// | 
| +// * Redistributions of source code must retain the above copyright | 
| +// notice, this list of conditions and the following disclaimer. | 
| +// * Redistributions in binary form must reproduce the above | 
| +// copyright notice, this list of conditions and the following | 
| +// disclaimer in the documentation and/or other materials provided | 
| +// with the distribution. | 
| +// * Neither the name of Google Inc. nor the names of its | 
| +// contributors may be used to endorse or promote products derived | 
| +// from this software without specific prior written permission. | 
| +// | 
| +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
| +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
| +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
| +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
| +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
| +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
| +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| + | 
| +#include "hydrogen.h" | 
| + | 
| +namespace v8 { | 
| +namespace internal { | 
| + | 
| +/* | 
| + * This class is a table with one element for eack basic block. | 
| + * | 
| + * It is used to check if, inside one loop, all execution paths contain | 
| + * a bounds check for a particular [index, length] combination. | 
| + * The reason is that if there is a path that stays in the loop without | 
| + * executing a check then the check cannot be hoisted out of the loop (it | 
| + * would likely fail and cause a deopt for no good reason). | 
| + * We also check is there are paths that exit the loop early, and if yes we | 
| + * perform the hoisting only if graph()->use_optimistic_licm() is true. | 
| + * The reason is that such paths are realtively common and harmless (like in | 
| + * a "search" method that scans an array until an element is found), but in | 
| + * some cases they could cause a deopt if we hoist the check so this is a | 
| + * situation we need to detect. | 
| + * | 
| + * InitializeLoop() sets up the table for a given loop. | 
| + * ClearIterationData() prepares the table for a new check. | 
| + * LoopPathsAreChecked() explores the loop graph searching for paths that do | 
| + * not contain a check ahains a given induction variable. | 
| + * ProcessRelatedChecks() is the "main" method that processes all the checks | 
| + * related to a given induction variable inside its induction loop. | 
| + */ | 
| +class InductionVariableBlocksTable BASE_EMBEDDED { | 
| + public: | 
| + class Element { | 
| + public: | 
| + static const int kNoBlock = -1; | 
| + | 
| + HBasicBlock* block() { return block_; } | 
| + void set_block(HBasicBlock* block) { block_ = block; } | 
| + bool has_check() { return has_check_; } | 
| + void set_has_check() { has_check_ = true; } | 
| + InductionVariableLimitUpdate* additional_limit() { | 
| + return &additional_limit_; | 
| + } | 
| + | 
| + void InitializeLoop(InductionVariableData* data) { | 
| + ASSERT(data->limit() != NULL); | 
| + HLoopInformation* loop = data->phi()->block()->current_loop(); | 
| + current_successor_ = kNoBlock; | 
| + backtrack_to_ = kNoBlock; | 
| + is_start_ = (block() == loop->loop_header()); | 
| + is_proper_exit_ = (block() == data->induction_exit_target()); | 
| + is_in_loop_ = loop->IsNestedInThisLoop(block()->current_loop()); | 
| + has_check_ = false; | 
| + } | 
| + | 
| + void ClearIterationData() { | 
| + current_successor_ = kNoBlock; | 
| + backtrack_to_ = kNoBlock; | 
| + } | 
| + | 
| + int CurrentSuccessorBlock() { | 
| + if (current_successor_ < block()->end()->SuccessorCount()) { | 
| + return block()->end()->SuccessorAt(current_successor_)->block_id(); | 
| + } else { | 
| + return kNoBlock; | 
| + } | 
| + } | 
| + | 
| + /* | 
| + * Perform one step of the loop graph traversal. | 
| + * from_block: The index of the block we are coming from. | 
| + * failure: Set this if we found a path that comes back to the loop header | 
| + * without passing through a check first. | 
| + * unsafe: Set this if we found an exit from the loop without passing | 
| + * through a check first. | 
| + * returns: The index of the next block to be processed. | 
| + */ | 
| 
 
titzer
2013/07/10 16:34:18
It is still not clear to me what order the code vi
 
Massi
2013/07/11 11:15:22
Done.
 
 | 
| + int PerformStep(int from_block, bool* failure, bool* unsafe) { | 
| + if (!is_in_loop_) { | 
| + if (!is_proper_exit_) { | 
| + *unsafe = true; | 
| + } | 
| + return from_block; | 
| + } | 
| + | 
| + if (is_start_ && | 
| + from_block != kNoBlock && | 
| + from_block != CurrentSuccessorBlock()) { | 
| + *failure = true; | 
| + return from_block; | 
| + } | 
| + | 
| + if (has_check()) { | 
| + return from_block; | 
| + } | 
| + | 
| + if (current_successor_ == kNoBlock) { | 
| + backtrack_to_ = from_block; | 
| + } | 
| + current_successor_++; | 
| + | 
| + if (CurrentSuccessorBlock() != kNoBlock) { | 
| + return CurrentSuccessorBlock(); | 
| + } else { | 
| + return backtrack_to_; | 
| + } | 
| + } | 
| + | 
| + Element() | 
| + : block_(NULL), current_successor_(kNoBlock), backtrack_to_(kNoBlock), | 
| + is_start_(false), is_proper_exit_(false), has_check_(false), | 
| + additional_limit_() {} | 
| + | 
| + private: | 
| + HBasicBlock* block_; | 
| + int current_successor_; | 
| + int backtrack_to_; | 
| + bool is_start_; | 
| + bool is_proper_exit_; | 
| + bool is_in_loop_; | 
| + bool has_check_; | 
| + InductionVariableLimitUpdate additional_limit_; | 
| + }; | 
| + | 
| + HGraph* graph() { return graph_; } | 
| + HBasicBlock* loop_header() { return loop_header_; } | 
| + Element* at(int index) { return &(elements_.at(index)); } | 
| + Element* at(HBasicBlock* block) { return at(block->block_id()); } | 
| + | 
| + void AddCheckAt(HBasicBlock* block) { | 
| + at(block->block_id())->set_has_check(); | 
| + } | 
| + | 
| + void InitializeLoop(InductionVariableData* data) { | 
| + for (int i = 0; i < graph()->blocks()->length(); i++) { | 
| + at(i)->InitializeLoop(data); | 
| + } | 
| + loop_header_ = data->phi()->block()->current_loop()->loop_header(); | 
| + } | 
| + | 
| + void ClearIterationData() { | 
| + ASSERT(loop_header() != NULL); | 
| + HLoopInformation* loop = loop_header()->loop_information(); | 
| + for (int i = 0; i < loop->blocks()->length(); i++) { | 
| + at(loop->blocks()->at(i)->block_id())->ClearIterationData(); | 
| + } | 
| + } | 
| + | 
| + bool LoopPathsAreChecked(bool* unsafe) { | 
| + bool failure = false; | 
| + *unsafe = false; | 
| + int previous_block = Element::kNoBlock; | 
| + int current_block = loop_header()->block_id(); | 
| + while (current_block != Element::kNoBlock) { | 
| + int next_block = at(current_block)->PerformStep(previous_block, | 
| + &failure, unsafe); | 
| 
 
titzer
2013/07/10 16:34:18
It might actually be clearer to inline this Perfor
 
Massi
2013/07/11 11:15:22
Done.
 
 | 
| + previous_block = current_block; | 
| + current_block = next_block; | 
| + if (failure) return false; | 
| + } | 
| + return true; | 
| + } | 
| + | 
| + explicit InductionVariableBlocksTable(HGraph* graph) | 
| + : graph_(graph), loop_header_(NULL), | 
| + elements_(graph->blocks()->length(), graph->zone()) { | 
| + for (int i = 0; i < graph->blocks()->length(); i++) { | 
| + Element element; | 
| + element.set_block(graph->blocks()->at(i)); | 
| + elements_.Add(element, graph->zone()); | 
| + ASSERT(at(i)->block()->block_id() == i); | 
| + } | 
| + } | 
| + | 
| + // Tries to hoist a check out of its induction loop. | 
| + void ProcessRelatedChecks( | 
| + InductionVariableData::InductionVariableCheck* check, | 
| + InductionVariableData* data) { | 
| + HValue* length = check->check()->length(); | 
| + ClearIterationData(); | 
| + check->set_processed(); | 
| + HBasicBlock* header = | 
| + data->phi()->block()->current_loop()->loop_header(); | 
| + HBasicBlock* pre_header = header->predecessors()->at(0); | 
| + // Check that the limit is defined in the loop preheader. | 
| + if (!data->limit()->IsInteger32Constant()) { | 
| + HBasicBlock* limit_block = data->limit()->block(); | 
| + if (limit_block != pre_header && | 
| + !limit_block->Dominates(pre_header)) { | 
| + return; | 
| + } | 
| + } | 
| + // Check that the length and limit have compatible representations. | 
| + if (!(data->limit()->representation().Equals( | 
| + length->representation()) || | 
| + data->limit()->IsInteger32Constant())) { | 
| + return; | 
| + } | 
| + // Check that the length is defined in the loop preheader. | 
| + if (check->check()->length()->block() != pre_header && | 
| + !check->check()->length()->block()->Dominates(pre_header)) { | 
| + return; | 
| + } | 
| + | 
| + // Add checks to the table. | 
| + for (InductionVariableData::InductionVariableCheck* current_check = check; | 
| + current_check != NULL; | 
| + current_check = current_check->next()) { | 
| + if (current_check->check()->length() != length) continue; | 
| + | 
| + AddCheckAt(current_check->check()->block()); | 
| + current_check->set_processed(); | 
| + } | 
| + | 
| + // Check that we will not cause unwanted deoptimizations. | 
| + bool unsafe; | 
| + bool failure = !LoopPathsAreChecked(&unsafe); | 
| + if (failure || (unsafe && !graph()->use_optimistic_licm())) { | 
| + return; | 
| + } | 
| + | 
| + // We will do the hoisting, but we must see if the limit is "limit" or if | 
| + // all checks are done on constants: if all check are done against the same | 
| + // constant limit we will use that instead of the induction limit. | 
| + bool has_upper_constant_limit = true; | 
| + InductionVariableData::InductionVariableCheck* current_check = check; | 
| + int32_t upper_constant_limit = | 
| + current_check != NULL && current_check->HasUpperLimit() ? | 
| + current_check->upper_limit() : 0; | 
| + while (current_check != NULL) { | 
| + if (check->HasUpperLimit()) { | 
| + if (check->upper_limit() != upper_constant_limit) { | 
| + has_upper_constant_limit = false; | 
| + } | 
| + } else { | 
| + has_upper_constant_limit = false; | 
| + } | 
| + | 
| + current_check->check()->set_skip_check(); | 
| + current_check = current_check->next(); | 
| + } | 
| + | 
| + // Choose the appropriate limit. | 
| + HValue* limit = data->limit(); | 
| + if (has_upper_constant_limit) { | 
| + HConstant* new_limit = new(pre_header->graph()->zone()) HConstant( | 
| + upper_constant_limit, length->representation()); | 
| + new_limit->InsertBefore(pre_header->end()); | 
| + limit = new_limit; | 
| + } | 
| + | 
| + // If necessary, redefine the limit in the preheader. | 
| + if (limit->IsInteger32Constant() && | 
| + limit->block() != pre_header && | 
| + !limit->block()->Dominates(pre_header)) { | 
| + HConstant* new_limit = new(pre_header->graph()->zone()) HConstant( | 
| + limit->GetInteger32Constant(), length->representation()); | 
| + new_limit->InsertBefore(pre_header->end()); | 
| + limit = new_limit; | 
| + } | 
| + | 
| + // Do the hoisting. | 
| + HBoundsCheck* hoisted_check = new(pre_header->zone()) HBoundsCheck( | 
| + limit, check->check()->length()); | 
| + hoisted_check->InsertBefore(pre_header->end()); | 
| + hoisted_check->set_allow_equality(true); | 
| + } | 
| + | 
| + void CollectInductionVariableData(HBasicBlock* bb); | 
| 
 
titzer
2013/07/10 16:34:18
It's OK IMO to go ahead and pull up these other me
 
Massi
2013/07/11 11:15:22
Done.
 
 | 
| + void EliminateRedundantBoundsChecks(HBasicBlock* bb); | 
| + | 
| + private: | 
| + HGraph* graph_; | 
| + HBasicBlock* loop_header_; | 
| + ZoneList<Element> elements_; | 
| +}; | 
| + | 
| + | 
| +void InductionVariableBlocksTable::CollectInductionVariableData( | 
| + HBasicBlock* bb) { | 
| + bool additional_limit = false; | 
| + | 
| + for (int i = 0; i < bb->phis()->length(); i++) { | 
| + HPhi* phi = bb->phis()->at(i); | 
| + phi->DetectInductionVariable(); | 
| + } | 
| + | 
| + additional_limit = InductionVariableData::ComputeInductionVariableLimit( | 
| + bb, at(bb)->additional_limit()); | 
| + | 
| + if (additional_limit) { | 
| + at(bb)->additional_limit()->updated_variable-> | 
| + UpdateAdditionalLimit(at(bb)->additional_limit()); | 
| + } | 
| + | 
| + for (HInstruction* i = bb->first(); i != NULL; i = i->next()) { | 
| + if (!i->IsBoundsCheck()) continue; | 
| + HBoundsCheck* check = HBoundsCheck::cast(i); | 
| + InductionVariableData::BitwiseDecompositionResult decomposition; | 
| + InductionVariableData::DecomposeBitwise(check->index(), &decomposition); | 
| + if (!decomposition.base->IsPhi()) continue; | 
| + HPhi* phi = HPhi::cast(decomposition.base); | 
| + | 
| + if (!phi->IsInductionVariable()) continue; | 
| + InductionVariableData* data = phi->induction_variable_data(); | 
| + | 
| + // For now ignore loops decrementing the index. | 
| + if (data->increment() <= 0) continue; | 
| + if (!data->lower_limit_is_non_negative_constant()) continue; | 
| + | 
| + // TODO(mmassi): skip OSR values for check->length(). | 
| + if (check->length() == data->limit() || | 
| + check->length() == data->additional_upper_limit()) { | 
| + check->set_skip_check(); | 
| + continue; | 
| + } | 
| + | 
| + if (!phi->IsLimitedInductionVariable()) continue; | 
| + | 
| + int32_t limit = data->ComputeUpperLimit(decomposition.and_mask, | 
| + decomposition.or_mask); | 
| + phi->induction_variable_data()->AddCheck(check, limit); | 
| + } | 
| + | 
| + for (int i = 0; i < bb->dominated_blocks()->length(); i++) { | 
| + CollectInductionVariableData(bb->dominated_blocks()->at(i)); | 
| + } | 
| + | 
| + if (additional_limit) { | 
| + at(bb->block_id())->additional_limit()->updated_variable-> | 
| + UpdateAdditionalLimit(at(bb->block_id())->additional_limit()); | 
| + } | 
| +} | 
| + | 
| + | 
| +void InductionVariableBlocksTable::EliminateRedundantBoundsChecks( | 
| + HBasicBlock* bb) { | 
| + for (int i = 0; i < bb->phis()->length(); i++) { | 
| + HPhi* phi = bb->phis()->at(i); | 
| + if (!phi->IsLimitedInductionVariable()) continue; | 
| + | 
| + InductionVariableData* induction_data = phi->induction_variable_data(); | 
| + InductionVariableData::ChecksRelatedToLength* current_length_group = | 
| + induction_data->checks(); | 
| + while (current_length_group != NULL) { | 
| + current_length_group->CloseCurrentBlock(); | 
| + InductionVariableData::InductionVariableCheck* current_base_check = | 
| + current_length_group->checks(); | 
| + InitializeLoop(induction_data); | 
| + | 
| + while (current_base_check != NULL) { | 
| + ProcessRelatedChecks(current_base_check, induction_data); | 
| + while (current_base_check != NULL && current_base_check->processed()) { | 
| + current_base_check = current_base_check->next(); | 
| + } | 
| + } | 
| + | 
| + current_length_group = current_length_group->next(); | 
| + } | 
| + } | 
| +} | 
| + | 
| + | 
| +void HGraph::EliminateRedundantBoundsChecksUsingInductionVariables() { | 
| + InductionVariableBlocksTable table(this); | 
| + table.CollectInductionVariableData(entry_block()); | 
| + for (int i = 0; i < blocks()->length(); i++) { | 
| + table.EliminateRedundantBoundsChecks(blocks()->at(i)); | 
| + } | 
| +} | 
| + | 
| +} } // namespace v8::internal | 
| + |