Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: src/hydrogen.cc

Issue 10032029: Eliminate redundant array bound checks (checks already performed earlier in the DT). (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: More small style fixes. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 2543 matching lines...) Expand 10 before | Expand all | Expand 10 after
2554 if (FLAG_use_range) { 2554 if (FLAG_use_range) {
2555 HRangeAnalysis rangeAnalysis(graph()); 2555 HRangeAnalysis rangeAnalysis(graph());
2556 rangeAnalysis.Analyze(); 2556 rangeAnalysis.Analyze();
2557 } 2557 }
2558 graph()->ComputeMinusZeroChecks(); 2558 graph()->ComputeMinusZeroChecks();
2559 2559
2560 // Eliminate redundant stack checks on backwards branches. 2560 // Eliminate redundant stack checks on backwards branches.
2561 HStackCheckEliminator sce(graph()); 2561 HStackCheckEliminator sce(graph());
2562 sce.Process(); 2562 sce.Process();
2563 2563
2564 // Replace the results of check instructions with the original value, if the 2564 graph()->EliminateRedundantBoundsChecks();
2565 // result is used. This is safe now, since we don't do code motion after this
2566 // point. It enables better register allocation since the value produced by
2567 // check instructions is really a copy of the original value.
2568 graph()->ReplaceCheckedValues();
2569 2565
2570 return graph(); 2566 return graph();
2571 } 2567 }
2572 2568
2573 2569
2574 void HGraph::ReplaceCheckedValues() { 2570 // We try to "factor up" HBoundsCheck instructions towards the root of the
2575 HPhase phase("H_Replace checked values", this); 2571 // dominator tree.
2576 for (int i = 0; i < blocks()->length(); ++i) { 2572 // For now we handle checks where the index is like "exp + int32value".
2577 HInstruction* instr = blocks()->at(i)->first(); 2573 // If in the dominator tree we check "exp + v1" and later (dominated)
2578 while (instr != NULL) { 2574 // "exp + v2", if v2 <= v1 we can safely remove the second check, and if
2579 if (instr->IsBoundsCheck()) { 2575 // v2 > v1 we can use v2 in the 1st check and again remove the second.
2580 // Replace all uses of the checked value with the original input. 2576 // To do so we keep a dictionary of all checks where the key if the pair
2581 ASSERT(instr->UseCount() > 0); 2577 // "exp, length".
2582 instr->ReplaceAllUsesWith(HBoundsCheck::cast(instr)->index()); 2578 // The class BoundsCheckKey represents this key.
2579 class BoundsCheckKey : public ZoneObject {
2580 public:
2581 HValue* IndexBase() const { return index_base_; }
2582 HValue* Length() const { return length_; }
2583
2584 uint32_t Hash() {
2585 return static_cast<uint32_t>(index_base_->Hashcode() ^ length_->Hashcode());
2586 }
2587
2588 static BoundsCheckKey* Create(Zone* zone,
2589 HBoundsCheck* check,
2590 int32_t* offset) {
2591 HValue* index_base = NULL;
2592 HConstant* constant = NULL;
2593 bool is_sub = false;
2594
2595 if (check->index()->IsAdd()) {
2596 HAdd* index = HAdd::cast(check->index());
2597 if (index->left()->IsConstant()) {
2598 constant = HConstant::cast(index->left());
2599 index_base = index->right();
2600 } else if (index->right()->IsConstant()) {
2601 constant = HConstant::cast(index->right());
2602 index_base = index->left();
2583 } 2603 }
2584 instr = instr->next(); 2604 } else if (check->index()->IsSub()) {
2585 } 2605 HSub* index = HSub::cast(check->index());
2586 } 2606 is_sub = true;
2607 if (index->left()->IsConstant()) {
2608 constant = HConstant::cast(index->left());
2609 index_base = index->right();
2610 } else if (index->right()->IsConstant()) {
2611 constant = HConstant::cast(index->right());
2612 index_base = index->left();
2613 }
2614 }
2615
2616 if (constant != NULL && constant->HasInteger32Value()) {
2617 *offset = is_sub ? - constant->Integer32Value()
2618 : constant->Integer32Value();
2619 } else {
2620 *offset = 0;
2621 index_base = check->index();
2622 }
2623
2624 return new(zone) BoundsCheckKey(index_base, check->length());
2625 }
2626
2627 private:
2628 BoundsCheckKey(HValue* index_base, HValue* length)
2629 : index_base_(index_base),
2630 length_(length) { }
2631
2632 HValue* index_base_;
2633 HValue* length_;
2634 };
2635
2636
2637 // Data about each HBoundsCheck that can be eliminated or moved.
2638 // It is the "value" in the dictionary indexed by "base-index, length"
2639 // (the key is BoundsCheckKey).
2640 // We scan the code with a dominator tree traversal.
2641 // Traversing the dominator tree we keep a stack (implemented as a singly
2642 // linked list) of "data" for each basic block that contains a relevant check
2643 // with the same key (the dictionary holds the head of the list).
2644 // We also keep all the "data" created for a given basic block in a list, and
2645 // use it to "clean up" the dictionary when backtracking in the dominator tree
2646 // traversal.
2647 // Doing this each dictionary entry always directly points to the check that
2648 // is dominating the code being examined now.
2649 // We also track the current "offset" of the index expression and use it to
2650 // decide if any check is already "covered" (so it can be removed) or not.
2651 class BoundsCheckBbData: public ZoneObject {
2652 public:
2653 BoundsCheckKey* Key() const { return key_; }
2654 int32_t LowerOffset() const { return lower_offset_; }
2655 int32_t UpperOffset() const { return upper_offset_; }
2656 HBasicBlock* BasicBlock() const { return basic_block_; }
2657 HBoundsCheck* Check() const { return check_; }
2658 BoundsCheckBbData* NextInBasicBlock() const { return next_in_bb_; }
2659 BoundsCheckBbData* FatherInDominatorTree() const { return father_in_dt_; }
2660
2661 bool OffsetIsCovered(int32_t offset) const {
2662 return offset >= LowerOffset() && offset <= UpperOffset();
2663 }
2664
2665 // This method removes new_check and modifies the current check so that it
2666 // also "covers" what new_check covered.
2667 // The obvious precondition is that new_check follows Check() in the
2668 // same basic block, and that new_offset is not covered (otherwise we
2669 // could simply remove new_check).
2670 // As a consequence LowerOffset() or UpperOffset() change (the covered
2671 // range grows).
2672 //
2673 // In the general case the check covering the current range should be like
2674 // these two checks:
2675 // 0 <= Key()->IndexBase() + LowerOffset()
2676 // Key()->IndexBase() + UpperOffset() < Key()->Length()
2677 //
2678 // We can transform the second check like this:
2679 // Key()->IndexBase() + LowerOffset() <
2680 // Key()->Length() + (LowerOffset() - UpperOffset())
2681 // so we can handle both checks with a single unsigned comparison.
2682 //
2683 // The bulk of this method changes Check()->index() and Check()->length()
2684 // replacing them with new HAdd instructions to perform the transformation
2685 // described above.
2686 void CoverCheck(HBoundsCheck* new_check,
2687 int32_t new_offset) {
fschneider 2012/04/26 09:35:50 Fits in previous line?
2688 ASSERT(new_check->index()->representation().IsInteger32());
2689
2690 if (new_offset > upper_offset_) {
2691 upper_offset_ = new_offset;
2692 } else if (new_offset < lower_offset_) {
2693 lower_offset_ = new_offset;
2694 } else {
2695 ASSERT(false);
2696 }
2697
2698 BuildOffsetAdd(&added_index_,
2699 &added_index_offset_,
2700 Key()->IndexBase(),
2701 new_check->index()->representation(),
2702 lower_offset_);
2703 Check()->SetOperandAt(0, added_index_);
2704 BuildOffsetAdd(&added_length_,
2705 &added_length_offset_,
2706 Key()->Length(),
2707 new_check->length()->representation(),
2708 lower_offset_ - upper_offset_);
2709 Check()->SetOperandAt(1, added_length_);
2710
2711 new_check->DeleteAndReplaceWith(NULL);
2712 }
2713
2714 void RemoveZeroOperations() {
2715 RemoveZeroAdd(&added_index_, &added_index_offset_);
2716 RemoveZeroAdd(&added_length_, &added_length_offset_);
2717 }
2718
2719 BoundsCheckBbData(BoundsCheckKey* key,
2720 int32_t lower_offset,
2721 int32_t upper_offset,
2722 HBasicBlock* bb,
2723 HBoundsCheck* check,
2724 BoundsCheckBbData* next_in_bb,
2725 BoundsCheckBbData* father_in_dt)
2726 : key_(key),
2727 lower_offset_(lower_offset),
2728 upper_offset_(upper_offset),
2729 basic_block_(bb),
2730 check_(check),
2731 added_index_offset_(NULL),
2732 added_index_(NULL),
2733 added_length_offset_(NULL),
2734 added_length_(NULL),
2735 next_in_bb_(next_in_bb),
2736 father_in_dt_(father_in_dt) { }
2737
2738 private:
2739 BoundsCheckKey* key_;
2740 int32_t lower_offset_;
2741 int32_t upper_offset_;
2742 HBasicBlock* basic_block_;
2743 HBoundsCheck* check_;
2744 HConstant* added_index_offset_;
2745 HAdd* added_index_;
2746 HConstant* added_length_offset_;
2747 HAdd* added_length_;
2748 BoundsCheckBbData* next_in_bb_;
2749 BoundsCheckBbData* father_in_dt_;
2750
2751 void BuildOffsetAdd(HAdd** add,
2752 HConstant** constant,
2753 HValue* original_value,
2754 Representation representation,
2755 int32_t new_offset) {
2756 HConstant* new_constant = new(BasicBlock()->zone())
2757 HConstant(Handle<Object>(Smi::FromInt(new_offset)),
2758 Representation::Integer32());
2759 if (*add == NULL) {
2760 new_constant->InsertBefore(Check());
2761 *add = new(BasicBlock()->zone()) HAdd(NULL,
2762 original_value,
2763 new_constant);
2764 (*add)->AssumeRepresentation(representation);
2765 (*add)->InsertBefore(Check());
2766 } else {
2767 new_constant->InsertBefore(*add);
2768 (*constant)->DeleteAndReplaceWith(new_constant);
2769 }
2770 *constant = new_constant;
2771 }
2772
2773 void RemoveZeroAdd(HAdd** add, HConstant** constant) {
2774 if (*add != NULL && (*constant)->Integer32Value() == 0) {
2775 (*add)->DeleteAndReplaceWith((*add)->left());
fschneider 2012/04/26 09:35:50 Could you add a test case that exercises this code
2776 (*constant)->DeleteAndReplaceWith(NULL);
2777 }
2778 }
2779 };
2780
2781
2782 static bool BoundsCheckKeyMatch(void* key1, void* key2) {
2783 BoundsCheckKey* k1 = static_cast<BoundsCheckKey*>(key1);
2784 BoundsCheckKey* k2 = static_cast<BoundsCheckKey*>(key2);
2785 return k1->IndexBase() == k2->IndexBase() && k1->Length() == k2->Length();
2587 } 2786 }
2588 2787
2589 2788
2789 class BoundsCheckTable : private ZoneHashMap {
2790 public:
2791 BoundsCheckBbData** LookupOrInsert(BoundsCheckKey* key) {
2792 return reinterpret_cast<BoundsCheckBbData**>(
2793 &(Lookup(key, key->Hash(), true)->value));
2794 }
2795
2796 void Insert(BoundsCheckKey* key, BoundsCheckBbData* data) {
2797 Lookup(key, key->Hash(), true)->value = data;
2798 }
2799
2800 void Delete(BoundsCheckKey* key) {
2801 Remove(key, key->Hash());
2802 }
2803
2804 BoundsCheckTable() : ZoneHashMap(BoundsCheckKeyMatch) { }
2805 };
2806
2807
2808 // Eliminates checks in bb and recursively in the dominated blocks.
2809 // Also replace the results of check instructions with the original value, if
2810 // the result is used. This is safe now, since we don't do code motion after
2811 // this point. It enables better register allocation since the value produced
2812 // by check instructions is really a copy of the original value.
2813 void HGraph::EliminateRedundantBoundsChecks(HBasicBlock* bb,
2814 BoundsCheckTable* table) {
2815 BoundsCheckBbData* bb_data_list = NULL;
2816
2817 for (HInstruction* i = bb->first(); i != NULL; i = i->next()) {
2818 if (!i->IsBoundsCheck()) continue;
2819
2820 HBoundsCheck* check = HBoundsCheck::cast(i);
2821 check->ReplaceAllUsesWith(check->index());
2822
2823 isolate()->counters()->array_bounds_checks_seen()->Increment();
2824 if (!FLAG_array_bounds_checks_elimination) continue;
2825
2826 int32_t offset;
2827 BoundsCheckKey* key =
2828 BoundsCheckKey::Create(bb->zone(), check, &offset);
2829 BoundsCheckBbData** data_p = table->LookupOrInsert(key);
2830 BoundsCheckBbData* data = *data_p;
2831 if (data == NULL) {
2832 bb_data_list = new(zone()) BoundsCheckBbData(key,
2833 offset,
2834 offset,
2835 bb,
2836 check,
2837 bb_data_list,
2838 NULL);
2839 *data_p = bb_data_list;
2840 } else if (data->OffsetIsCovered(offset)) {
2841 check->DeleteAndReplaceWith(NULL);
2842 isolate()->counters()->array_bounds_checks_removed()->Increment();
2843 } else if (data->BasicBlock() == bb) {
2844 data->CoverCheck(check, offset);
2845 isolate()->counters()->array_bounds_checks_removed()->Increment();
2846 } else {
2847 int32_t new_lower_offset = offset < data->LowerOffset()
2848 ? offset
2849 : data->LowerOffset();
2850 int32_t new_upper_offset = offset > data->UpperOffset()
2851 ? offset
2852 : data->UpperOffset();
2853 bb_data_list = new(bb->zone()) BoundsCheckBbData(key,
2854 new_lower_offset,
2855 new_upper_offset,
2856 bb,
2857 check,
2858 bb_data_list,
2859 data);
2860 table->Insert(key, bb_data_list);
2861 }
2862 }
2863
2864 for (int i = 0; i < bb->dominated_blocks()->length(); ++i) {
2865 EliminateRedundantBoundsChecks(bb->dominated_blocks()->at(i), table);
2866 }
2867
2868 for (BoundsCheckBbData* data = bb_data_list;
2869 data != NULL;
2870 data = data->NextInBasicBlock()) {
2871 data->RemoveZeroOperations();
2872 if (data->FatherInDominatorTree()) {
2873 table->Insert(data->Key(), data->FatherInDominatorTree());
2874 } else {
2875 table->Delete(data->Key());
2876 }
2877 }
2878 }
2879
2880
2881 void HGraph::EliminateRedundantBoundsChecks() {
2882 HPhase phase("H_Eliminate bounds checks", this);
2883 AssertNoAllocation no_gc;
2884 BoundsCheckTable checks_table;
2885 EliminateRedundantBoundsChecks(entry_block(), &checks_table);
2886 }
2887
2888
2590 HInstruction* HGraphBuilder::AddInstruction(HInstruction* instr) { 2889 HInstruction* HGraphBuilder::AddInstruction(HInstruction* instr) {
2591 ASSERT(current_block() != NULL); 2890 ASSERT(current_block() != NULL);
2592 current_block()->AddInstruction(instr); 2891 current_block()->AddInstruction(instr);
2593 return instr; 2892 return instr;
2594 } 2893 }
2595 2894
2596 2895
2597 void HGraphBuilder::AddSimulate(int ast_id) { 2896 void HGraphBuilder::AddSimulate(int ast_id) {
2598 ASSERT(current_block() != NULL); 2897 ASSERT(current_block() != NULL);
2599 current_block()->AddSimulate(ast_id); 2898 current_block()->AddSimulate(ast_id);
(...skipping 5631 matching lines...) Expand 10 before | Expand all | Expand 10 after
8231 } 8530 }
8232 } 8531 }
8233 8532
8234 #ifdef DEBUG 8533 #ifdef DEBUG
8235 if (graph_ != NULL) graph_->Verify(false); // No full verify. 8534 if (graph_ != NULL) graph_->Verify(false); // No full verify.
8236 if (allocator_ != NULL) allocator_->Verify(); 8535 if (allocator_ != NULL) allocator_->Verify();
8237 #endif 8536 #endif
8238 } 8537 }
8239 8538
8240 } } // namespace v8::internal 8539 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698