Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 for (HInstructionIterator it(block); !it.Done(); it.Advance()) { | 56 for (HInstructionIterator it(block); !it.Done(); it.Advance()) { |
| 57 HInstruction* instr = it.Current(); | 57 HInstruction* instr = it.Current(); |
| 58 if (instr->IsAllocate()) { | 58 if (instr->IsAllocate()) { |
| 59 CollectIfNoEscapingUses(instr); | 59 CollectIfNoEscapingUses(instr); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 | 65 |
| 66 // Performs a forward data-flow analysis of all loads and stores on the | |
| 67 // given captured allocation. This uses a reverse post-order iteration | |
| 68 // over affected basic blocks. All non-escaping instructions are handled | |
| 69 // and replaced during the analysis. | |
| 70 void HEscapeAnalysisPhase::AnalyzeDataFlow(HInstruction* allocate) { | |
| 71 HBasicBlock* allocate_block = allocate->block(); | |
| 72 block_states_.AddBlock(NULL, graph()->blocks()->length(), zone()); | |
| 73 | |
| 74 for (int i = 0; i < graph()->blocks()->length(); i++) { | |
|
titzer
2013/08/01 17:11:50
You can start i at allocate_block->id(), since the
Michael Starzinger
2013/08/05 15:13:00
Done. So obvious, I feel embarrassed now.
| |
| 75 HBasicBlock* block = graph()->blocks()->at(i); | |
| 76 Zone* graph_zone = graph()->zone(); | |
| 77 HCapturedObject* state = NULL; | |
| 78 | |
| 79 // Skip blocks that are not dominated by the captured allocation. | |
| 80 if (!allocate_block->Dominates(block) && allocate_block != block) continue; | |
| 81 if (FLAG_trace_escape_analysis) { | |
| 82 PrintF("Analyzing data-flow in B%d\n", block->block_id()); | |
| 83 } | |
| 84 | |
| 85 // Update block state using all predecessor blocks. | |
| 86 if (block->predecessors()->length() == 1) { | |
| 87 state = StateAt(block->predecessors()->first()); | |
| 88 } else if (allocate_block != block) { | |
| 89 state = NewStateWithPhis(block->first(), block); | |
| 90 for (int i = 0; i < block->predecessors()->length(); i++) { | |
| 91 HBasicBlock* pred = block->predecessors()->at(i); | |
| 92 HCapturedObject* pred_state = StateAt(pred); | |
| 93 // Backwards branches will be post-processed later. | |
| 94 if (pred->block_id() >= block->block_id()) { | |
| 95 ASSERT(block->IsLoopHeader() && pred_state == NULL); | |
| 96 PushBackwardsBranch(pred, state); | |
| 97 continue; | |
| 98 } | |
| 99 for (int index = 0; index < number_of_values_; index++) { | |
|
titzer
2013/08/01 17:11:50
You're creating a lot of phis here. You only need
Michael Starzinger
2013/08/05 15:13:00
Done. Switched to lazy creation of phis only once
| |
| 100 HPhi* phi = HPhi::cast(state->OperandAt(index)); | |
| 101 phi->AddInput(pred_state->OperandAt(index)); | |
| 102 } | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 // Go through all instructions of the current block. | |
| 107 for (HInstructionIterator it(block); !it.Done(); it.Advance()) { | |
| 108 HInstruction* instr = it.Current(); | |
| 109 switch (instr->opcode()) { | |
| 110 case HValue::kAllocate: { | |
| 111 if (instr != allocate) continue; | |
| 112 state = NewStateWithUndefined(allocate); | |
| 113 break; | |
| 114 } | |
| 115 case HValue::kLoadNamedField: { | |
| 116 HLoadNamedField* load = HLoadNamedField::cast(instr); | |
| 117 int index = load->access().offset() / kPointerSize; | |
|
titzer
2013/08/01 17:11:50
Are you only tracking in-object properties? Otherw
Michael Starzinger
2013/08/05 15:13:00
Done. Good point, currently we don't HAllocate obj
| |
| 118 if (load->object() != allocate) continue; | |
| 119 HValue* replacement = state->OperandAt(index); | |
| 120 load->DeleteAndReplaceWith(replacement); | |
| 121 if (FLAG_trace_escape_analysis) { | |
| 122 PrintF("Replacing load #%d with #%d (%s)\n", instr->id(), | |
| 123 replacement->id(), replacement->Mnemonic()); | |
| 124 } | |
| 125 break; | |
| 126 } | |
| 127 case HValue::kStoreNamedField: { | |
| 128 HStoreNamedField* store = HStoreNamedField::cast(instr); | |
| 129 int index = store->access().offset() / kPointerSize; | |
| 130 if (store->object() != allocate) continue; | |
| 131 state = NewStateWithCopy(store, state); | |
| 132 state->SetOperandAt(index, store->value()); | |
| 133 if (!store->transition().is_null()) { | |
| 134 // TODO(mstarzinger): Handle dereference is actually not allowed | |
| 135 // here. Fix this by allocating the HConstant at graph building. | |
| 136 AllowHandleDereference allow_deref; | |
| 137 HConstant* map = new(graph_zone) HConstant(store->transition()); | |
| 138 map->FinalizeUniqueValueId(); | |
| 139 map->InsertBefore(store); | |
| 140 state->SetOperandAt(0, map); | |
| 141 } | |
| 142 store->DeleteAndReplaceWith(NULL); | |
| 143 if (FLAG_trace_escape_analysis) { | |
| 144 PrintF("Replacing store #%d%s\n", instr->id(), | |
| 145 store->transition().is_null() ? "" : " (with transition)"); | |
| 146 } | |
| 147 break; | |
| 148 } | |
| 149 case HValue::kSimulate: { | |
| 150 HSimulate* simulate = HSimulate::cast(instr); | |
| 151 // TODO(mstarzinger): This doesn't track deltas for values on the | |
| 152 // operand stack yet. Find a repro test case and fix this. | |
| 153 for (int i = 0; i < simulate->OperandCount(); i++) { | |
| 154 if (simulate->OperandAt(i) != allocate) continue; | |
| 155 simulate->SetOperandAt(i, state); | |
| 156 } | |
| 157 break; | |
| 158 } | |
| 159 case HValue::kArgumentsObject: | |
| 160 case HValue::kCapturedObject: { | |
| 161 for (int i = 0; i < instr->OperandCount(); i++) { | |
| 162 if (instr->OperandAt(i) != allocate) continue; | |
| 163 instr->SetOperandAt(i, state); | |
| 164 } | |
| 165 break; | |
| 166 } | |
| 167 case HValue::kCheckHeapObject: { | |
| 168 HCheckHeapObject* check = HCheckHeapObject::cast(instr); | |
| 169 if (check->value() != allocate) continue; | |
| 170 check->DeleteAndReplaceWith(NULL); | |
| 171 break; | |
| 172 } | |
| 173 case HValue::kCheckMaps: { | |
| 174 HCheckMaps* mapcheck = HCheckMaps::cast(instr); | |
| 175 if (mapcheck->value() != allocate) continue; | |
| 176 // TODO(mstarzinger): This approach breaks if the tracked map value | |
| 177 // is not a HConstant. Find a repro test case and fix this. | |
| 178 ASSERT(mapcheck->HasNoUses()); | |
| 179 mapcheck->DeleteAndReplaceWith(NULL); | |
| 180 break; | |
| 181 } | |
| 182 default: | |
| 183 // Nothing to see here, move along ... | |
| 184 break; | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 // Store block state at block exit into map. | |
| 189 SetStateAt(block, state); | |
|
titzer
2013/08/01 17:11:50
You should forward-propagate the block state to al
Michael Starzinger
2013/08/05 15:13:00
Done. Switched to forward propagation of the block
| |
| 190 } | |
| 191 | |
| 192 // All uses have been handled. | |
| 193 ASSERT(allocate->HasNoUses()); | |
| 194 allocate->DeleteAndReplaceWith(NULL); | |
| 195 } | |
| 196 | |
| 197 | |
| 198 // Post-process all loop headers and fill in values from backwards | |
| 199 // branches into the phis added to analyzed loop headers. | |
| 200 void HEscapeAnalysisPhase::AnalyzeLoopHeaders(HInstruction* allocate) { | |
| 201 BackwardsBranch* branch; | |
| 202 while (PopBackwardsBranch(&branch)) { | |
| 203 HBasicBlock* pred = branch->block_; | |
| 204 HCapturedObject* state = branch->state_; | |
| 205 HCapturedObject* pred_state = StateAt(pred); | |
| 206 for (int index = 0; index < number_of_values_; index++) { | |
|
titzer
2013/08/01 17:11:50
This is somewhat brittle because the phi input ind
Michael Starzinger
2013/08/05 15:13:00
Done. Switched to use HBasicBlock::PredecessorInde
| |
| 207 HPhi* phi = HPhi::cast(state->OperandAt(index)); | |
| 208 phi->AddInput(pred_state->OperandAt(index)); | |
| 209 } | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 | |
| 214 void HEscapeAnalysisPhase::PerformScalarReplacement() { | |
| 215 for (int i = 0; i < captured_.length(); i++) { | |
| 216 HAllocate* allocate = HAllocate::cast(captured_.at(i)); | |
| 217 | |
| 218 // Compute number of scalar values and start with clean slate. | |
| 219 if (!allocate->size()->IsInteger32Constant()) continue; | |
| 220 int size_in_bytes = allocate->size()->GetInteger32Constant(); | |
| 221 number_of_values_ = size_in_bytes / kPointerSize; | |
| 222 loops_affected_by_stores_.Clear(); | |
| 223 block_states_.Clear(); | |
| 224 | |
| 225 // Perform actual analysis steps. | |
| 226 AnalyzeDataFlow(allocate); | |
| 227 AnalyzeLoopHeaders(allocate); | |
| 228 | |
| 229 cumulative_values_ += number_of_values_; | |
| 230 ASSERT(backwards_branch_stack_ == NULL); | |
| 231 ASSERT(allocate->HasNoUses()); | |
| 232 ASSERT(!allocate->IsLinked()); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 | |
| 66 } } // namespace v8::internal | 237 } } // namespace v8::internal |
| OLD | NEW |