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

Side by Side Diff: runtime/vm/flow_graph_optimizer.cc

Issue 345563007: Add Uint32 representation (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 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 (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/flow_graph_optimizer.h" 5 #include "vm/flow_graph_optimizer.h"
6 6
7 #include "vm/bit_vector.h" 7 #include "vm/bit_vector.h"
8 #include "vm/cha.h" 8 #include "vm/cha.h"
9 #include "vm/cpu.h" 9 #include "vm/cpu.h"
10 #include "vm/dart_entry.h" 10 #include "vm/dart_entry.h"
(...skipping 26 matching lines...) Expand all
37 DEFINE_FLAG(bool, remove_redundant_phis, true, "Remove redundant phis."); 37 DEFINE_FLAG(bool, remove_redundant_phis, true, "Remove redundant phis.");
38 DEFINE_FLAG(bool, trace_constant_propagation, false, 38 DEFINE_FLAG(bool, trace_constant_propagation, false,
39 "Print constant propagation and useless code elimination."); 39 "Print constant propagation and useless code elimination.");
40 DEFINE_FLAG(bool, trace_load_optimization, false, 40 DEFINE_FLAG(bool, trace_load_optimization, false,
41 "Print live sets for load optimization pass."); 41 "Print live sets for load optimization pass.");
42 DEFINE_FLAG(bool, trace_optimization, false, "Print optimization details."); 42 DEFINE_FLAG(bool, trace_optimization, false, "Print optimization details.");
43 DEFINE_FLAG(bool, trace_range_analysis, false, "Trace range analysis progress"); 43 DEFINE_FLAG(bool, trace_range_analysis, false, "Trace range analysis progress");
44 DEFINE_FLAG(bool, truncating_left_shift, true, 44 DEFINE_FLAG(bool, truncating_left_shift, true,
45 "Optimize left shift to truncate if possible"); 45 "Optimize left shift to truncate if possible");
46 DEFINE_FLAG(bool, use_cha, true, "Use class hierarchy analysis."); 46 DEFINE_FLAG(bool, use_cha, true, "Use class hierarchy analysis.");
47 DEFINE_FLAG(bool, trace_integer_ir_selection, false,
48 "Print integer IR selection optimization pass.");
47 DECLARE_FLAG(bool, enable_type_checks); 49 DECLARE_FLAG(bool, enable_type_checks);
48 DECLARE_FLAG(bool, source_lines); 50 DECLARE_FLAG(bool, source_lines);
49 DECLARE_FLAG(bool, trace_type_check_elimination); 51 DECLARE_FLAG(bool, trace_type_check_elimination);
50 DECLARE_FLAG(bool, warn_on_javascript_compatibility); 52 DECLARE_FLAG(bool, warn_on_javascript_compatibility);
51 53
52 // Quick access to the locally defined isolate() method. 54 // Quick access to the locally defined isolate() method.
53 #define I (isolate()) 55 #define I (isolate())
54 56
55 static bool ShouldInlineSimd() { 57 static bool ShouldInlineSimd() {
56 return FlowGraphCompiler::SupportsUnboxedSimd128(); 58 return FlowGraphCompiler::SupportsUnboxedSimd128();
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 deopt_target = insert_before = use->instruction(); 603 deopt_target = insert_before = use->instruction();
602 } 604 }
603 605
604 Definition* converted = NULL; 606 Definition* converted = NULL;
605 if ((from == kTagged) && (to == kUnboxedMint)) { 607 if ((from == kTagged) && (to == kUnboxedMint)) {
606 ASSERT((deopt_target != NULL) || 608 ASSERT((deopt_target != NULL) ||
607 (use->Type()->ToCid() == kUnboxedMint)); 609 (use->Type()->ToCid() == kUnboxedMint));
608 const intptr_t deopt_id = (deopt_target != NULL) ? 610 const intptr_t deopt_id = (deopt_target != NULL) ?
609 deopt_target->DeoptimizationTarget() : Isolate::kNoDeoptId; 611 deopt_target->DeoptimizationTarget() : Isolate::kNoDeoptId;
610 converted = new(I) UnboxIntegerInstr(use->CopyWithType(), deopt_id); 612 converted = new(I) UnboxIntegerInstr(use->CopyWithType(), deopt_id);
611
612 } else if ((from == kUnboxedMint) && (to == kTagged)) { 613 } else if ((from == kUnboxedMint) && (to == kTagged)) {
613 converted = new(I) BoxIntegerInstr(use->CopyWithType()); 614 converted = new(I) BoxIntegerInstr(use->CopyWithType());
614 615 } else if ((from == kUnboxedMint) && (to == kUnboxedUint32)) {
616 converted = new(I) UnboxedIntConverterInstr(from, to, use->CopyWithType());
617 } else if ((from == kUnboxedUint32) && (to == kUnboxedMint)) {
618 converted = new(I) UnboxedIntConverterInstr(from, to, use->CopyWithType());
615 } else if (from == kUnboxedMint && to == kUnboxedDouble) { 619 } else if (from == kUnboxedMint && to == kUnboxedDouble) {
616 ASSERT(CanUnboxDouble()); 620 ASSERT(CanUnboxDouble());
617 // Convert by boxing/unboxing. 621 // Convert by boxing/unboxing.
618 // TODO(fschneider): Implement direct unboxed mint-to-double conversion. 622 // TODO(fschneider): Implement direct unboxed mint-to-double conversion.
619 BoxIntegerInstr* boxed = 623 BoxIntegerInstr* boxed =
620 new(I) BoxIntegerInstr(use->CopyWithType()); 624 new(I) BoxIntegerInstr(use->CopyWithType());
621 use->BindTo(boxed); 625 use->BindTo(boxed);
622 InsertBefore(insert_before, boxed, NULL, FlowGraph::kValue); 626 InsertBefore(insert_before, boxed, NULL, FlowGraph::kValue);
623 627
624 const intptr_t deopt_id = (deopt_target != NULL) ? 628 const intptr_t deopt_id = (deopt_target != NULL) ?
(...skipping 4515 matching lines...) Expand 10 before | Expand all | Expand 10 after
5140 } 5144 }
5141 } 5145 }
5142 5146
5143 5147
5144 void FlowGraphOptimizer::InferIntRanges() { 5148 void FlowGraphOptimizer::InferIntRanges() {
5145 RangeAnalysis range_analysis(flow_graph_); 5149 RangeAnalysis range_analysis(flow_graph_);
5146 range_analysis.Analyze(); 5150 range_analysis.Analyze();
5147 } 5151 }
5148 5152
5149 5153
5154 // Replaces Mint IL instructions with Uint32 IL instructions
5155 // when possible. Uses output of RangeAnalysis.
5156 class IntegerInstructionSelector : public ValueObject {
5157 public:
5158 explicit IntegerInstructionSelector(FlowGraph* flow_graph)
5159 : flow_graph_(flow_graph),
5160 isolate_(NULL),
5161 changed_(true) {
5162 ASSERT(flow_graph_ != NULL);
5163 isolate_ = flow_graph_->isolate();
5164 ASSERT(isolate_ != NULL);
5165 selected_uint32_defs_ =
5166 new(I) BitVector(flow_graph_->current_ssa_temp_index());
5167 }
5168
5169 void Select();
5170
5171 private:
5172 bool IsPotentialUint32Definition(Definition* def);
5173 void FindPotentialUint32Definitions();
5174 bool IsUint32NarrowingDefinition(Definition* def);
5175 void FindUint32NarrowingDefinitions();
5176 bool CanBecomeUint32(Definition* def);
5177 void Propagate();
5178 Definition* ConstructReplacementFor(Definition* def);
5179 void ReplaceInstructions();
5180
5181 Isolate* isolate() const { return isolate_; }
5182
5183 GrowableArray<Definition*> potential_uint32_defs_;
5184 BitVector* selected_uint32_defs_;
5185
5186 FlowGraph* flow_graph_;
5187 Isolate* isolate_;
5188 bool changed_;
5189 };
5190
5191
5192 void IntegerInstructionSelector::Select() {
5193 if (FLAG_trace_integer_ir_selection) {
5194 OS::Print("---- starting integer ir selection -------\n");
5195 }
5196 FindPotentialUint32Definitions();
5197 FindUint32NarrowingDefinitions();
5198 Propagate();
5199 ReplaceInstructions();
5200 if (FLAG_trace_integer_ir_selection) {
5201 OS::Print("---- after integer ir selection -------\n");
5202 FlowGraphPrinter printer(*flow_graph_);
5203 printer.PrintBlocks();
5204 }
5205 }
5206
5207
5208 bool IntegerInstructionSelector::IsPotentialUint32Definition(Definition* def) {
5209 // TODO(johnmccutchan): Consider Smi operations, to avoid unnecessary tagging
5210 // & untagged of intermediate results.
5211 return def->IsBoxInteger() || // BoxMint.
5212 def->IsUnboxInteger() || // UnboxMint.
5213 def->IsBinaryMintOp() ||
5214 def->IsShiftMintOp() ||
5215 def->IsUnaryMintOp();
5216 }
5217
5218
5219 void IntegerInstructionSelector::FindPotentialUint32Definitions() {
5220 if (FLAG_trace_integer_ir_selection) {
5221 OS::Print("++++ Finding potential Uint32 definitions:\n");
5222 }
5223
5224 for (BlockIterator block_it = flow_graph_->reverse_postorder_iterator();
5225 !block_it.Done();
5226 block_it.Advance()) {
5227 BlockEntryInstr* block = block_it.Current();
5228
5229 for (ForwardInstructionIterator instr_it(block);
5230 !instr_it.Done();
5231 instr_it.Advance()) {
5232 Instruction* current = instr_it.Current();
5233 Definition* defn = current->AsDefinition();
5234 if ((defn != NULL) && (defn->ssa_temp_index() != -1)) {
5235 if (IsPotentialUint32Definition(defn)) {
5236 if (FLAG_trace_integer_ir_selection) {
5237 OS::Print("Adding %s\n", current->ToCString());
5238 }
5239 potential_uint32_defs_.Add(defn);
5240 }
5241 }
5242 }
5243 }
5244 }
5245
5246
5247 // BinaryMintOp masks and stores into unsigned typed arrays that truncate the
5248 // value into a Uint32 range.
5249 bool IntegerInstructionSelector::IsUint32NarrowingDefinition(Definition* def) {
5250 if (def->IsBinaryMintOp()) {
5251 BinaryMintOpInstr* op = def->AsBinaryMintOp();
5252 // Must be a mask operation.
5253 if (op->op_kind() != Token::kBIT_AND) {
5254 return false;
5255 }
5256 Range* range = op->range();
5257 if ((range == NULL) ||
5258 !range->IsWithin(0, static_cast<int64_t>(kMaxUint32))) {
5259 return false;
5260 }
5261 return true;
5262 }
5263 // TODO(johnmccutchan): Add typed array stores.
5264 return false;
5265 }
5266
5267
5268 void IntegerInstructionSelector::FindUint32NarrowingDefinitions() {
5269 ASSERT(selected_uint32_defs_ != NULL);
5270 if (FLAG_trace_integer_ir_selection) {
5271 OS::Print("++++ Selecting Uint32 definitions:\n");
5272 OS::Print("++++ Initial set:\n");
5273 }
5274 for (intptr_t i = 0; i < potential_uint32_defs_.length(); i++) {
5275 Definition* defn = potential_uint32_defs_[i];
5276 if (IsUint32NarrowingDefinition(defn)) {
5277 if (FLAG_trace_integer_ir_selection) {
5278 OS::Print("Adding %s\n", defn->ToCString());
5279 }
5280 selected_uint32_defs_->Add(defn->ssa_temp_index());
5281 }
5282 }
5283 }
5284
5285
5286 bool IntegerInstructionSelector::CanBecomeUint32(Definition* def) {
5287 ASSERT(IsPotentialUint32Definition(def));
5288 if (def->IsBoxInteger()) {
5289 // If a BoxInteger's input is a candidate, the box is a candidate.
5290 BoxIntegerInstr* box = def->AsBoxInteger();
5291 Definition* box_input = box->value()->definition();
5292 return selected_uint32_defs_->Contains(box_input->ssa_temp_index());
5293 }
5294 // A right shift with an input outside of Uint32 range cannot be converted
5295 // because we need the high bits.
5296 if (def->IsShiftMintOp()) {
5297 ShiftMintOpInstr* op = def->AsShiftMintOp();
5298 if (op->op_kind() == Token::kSHR) {
5299 Definition* shift_input = op->left()->definition();
5300 ASSERT(shift_input != NULL);
5301 Range* range = shift_input->range();
5302 if ((range == NULL) ||
5303 !range->IsWithin(0, static_cast<int64_t>(kMaxUint32))) {
5304 return false;
5305 }
5306 }
5307 }
5308 if (!def->HasUses()) {
5309 // No uses, skip.
5310 return false;
5311 }
5312 // All input uses are candidates.
5313 for (Value* use = def->input_use_list(); use != NULL; use = use->next_use()) {
Florian Schneider 2014/07/10 13:05:05 Use Value::Iterator for iterating the use list.
Cutch 2014/07/10 17:12:53 Done.
5314 Definition* defn = use->instruction()->AsDefinition();
5315 if ((defn == NULL) ||
5316 (defn->ssa_temp_index() == -1) ||
5317 !selected_uint32_defs_->Contains(defn->ssa_temp_index())) {
5318 return false;
5319 }
5320 }
5321
5322 // All environment uses are candidates.
5323 for (Value* use = def->env_use_list(); use != NULL; use = use->next_use()) {
Florian Schneider 2014/07/10 13:05:05 Use Value::Iterator for iterating the use list.
Cutch 2014/07/10 17:12:53 Done.
5324 Definition* defn = use->instruction()->AsDefinition();
5325 if ((defn == NULL) ||
5326 (defn->ssa_temp_index() == -1) ||
5327 !selected_uint32_defs_->Contains(defn->ssa_temp_index())) {
5328 return false;
5329 }
5330 }
5331
5332 return true;
5333 }
5334
5335
5336 void IntegerInstructionSelector::Propagate() {
5337 ASSERT(selected_uint32_defs_ != NULL);
5338 ASSERT(changed_);
5339 intptr_t iteration = 0;
5340 while (changed_) {
5341 if (FLAG_trace_integer_ir_selection) {
5342 OS::Print("+++ Iteration: %" Pd "\n", iteration++);
5343 }
5344 changed_ = false;
5345 for (intptr_t i = 0; i < potential_uint32_defs_.length(); i++) {
5346 Definition* defn = potential_uint32_defs_[i];
5347 if (selected_uint32_defs_->Contains(defn->ssa_temp_index())) {
5348 // Already marked as a candidate, skip.
5349 continue;
5350 }
5351 if (defn->IsConstant()) {
5352 // Skip constants.
5353 continue;
5354 }
5355 if (CanBecomeUint32(defn)) {
5356 if (FLAG_trace_integer_ir_selection) {
5357 OS::Print("Adding %s\n", defn->ToCString());
5358 }
5359 // Found a new candidate.
5360 selected_uint32_defs_->Add(defn->ssa_temp_index());
5361 // Haven't reached fixed point yet.
5362 changed_ = true;
5363 }
5364 }
5365 }
5366 if (FLAG_trace_integer_ir_selection) {
5367 OS::Print("Reached fixed point\n");
5368 }
5369 }
5370
5371
5372 Definition* IntegerInstructionSelector::ConstructReplacementFor(
5373 Definition* def) {
5374 // Should only see mint definitions.
5375 ASSERT(IsPotentialUint32Definition(def));
5376 // Should not see constant instructions.
5377 ASSERT(!def->IsConstant());
5378 if (def->IsBinaryMintOp()) {
5379 BinaryMintOpInstr* op = def->AsBinaryMintOp();
5380 Token::Kind op_kind = op->op_kind();
5381 Value* left = op->left()->CopyWithType();
5382 Value* right = op->right()->CopyWithType();
5383 intptr_t deopt_id = op->DeoptimizationTarget();
5384 return new(I) BinaryUint32OpInstr(op_kind, left, right, deopt_id);
5385 } else if (def->IsBoxInteger()) {
5386 BoxIntegerInstr* box = def->AsBoxInteger();
5387 Value* value = box->value()->CopyWithType();
5388 return new(I) BoxUint32Instr(value);
5389 } else if (def->IsUnboxInteger()) {
5390 UnboxIntegerInstr* unbox = def->AsUnboxInteger();
5391 Value* value = unbox->value()->CopyWithType();
5392 intptr_t deopt_id = unbox->deopt_id();
5393 return new(I) UnboxUint32Instr(value, deopt_id);
5394 } else if (def->IsUnaryMintOp()) {
5395 UnaryMintOpInstr* op = def->AsUnaryMintOp();
5396 Token::Kind op_kind = op->op_kind();
5397 Value* value = op->value()->CopyWithType();
5398 intptr_t deopt_id = op->DeoptimizationTarget();
5399 return new(I) UnaryUint32OpInstr(op_kind, value, deopt_id);
5400 } else if (def->IsShiftMintOp()) {
5401 ShiftMintOpInstr* op = def->AsShiftMintOp();
5402 Token::Kind op_kind = op->op_kind();
5403 Value* left = op->left()->CopyWithType();
5404 Value* right = op->right()->CopyWithType();
5405 intptr_t deopt_id = op->DeoptimizationTarget();
5406 return new(I) ShiftUint32OpInstr(op_kind, left, right, deopt_id);
5407 }
5408 UNREACHABLE();
5409 return NULL;
5410 }
5411
5412
5413 void IntegerInstructionSelector::ReplaceInstructions() {
5414 if (FLAG_trace_integer_ir_selection) {
5415 OS::Print("++++ Replacing instructions:\n");
5416 }
5417 for (intptr_t i = 0; i < potential_uint32_defs_.length(); i++) {
5418 Definition* defn = potential_uint32_defs_[i];
5419 if (!selected_uint32_defs_->Contains(defn->ssa_temp_index())) {
5420 // Not a candidate.
5421 continue;
5422 }
5423 Definition* replacement = ConstructReplacementFor(defn);
5424 ASSERT(replacement != NULL);
5425 if (FLAG_trace_integer_ir_selection) {
5426 OS::Print("Replacing %s with %s\n", defn->ToCString(),
5427 replacement->ToCString());
5428 }
5429 defn->ReplaceWith(replacement, NULL);
5430 ASSERT(flow_graph_->VerifyUseLists());
5431 }
5432 }
5433
5434 void FlowGraphOptimizer::SelectIntegerInstructions() {
5435 IntegerInstructionSelector iis(flow_graph_);
5436 iis.Select();
5437 }
5438
5439
5150 void TryCatchAnalyzer::Optimize(FlowGraph* flow_graph) { 5440 void TryCatchAnalyzer::Optimize(FlowGraph* flow_graph) {
5151 // For every catch-block: Iterate over all call instructions inside the 5441 // For every catch-block: Iterate over all call instructions inside the
5152 // corresponding try-block and figure out for each environment value if it 5442 // corresponding try-block and figure out for each environment value if it
5153 // is the same constant at all calls. If yes, replace the initial definition 5443 // is the same constant at all calls. If yes, replace the initial definition
5154 // at the catch-entry with this constant. 5444 // at the catch-entry with this constant.
5155 const GrowableArray<CatchBlockEntryInstr*>& catch_entries = 5445 const GrowableArray<CatchBlockEntryInstr*>& catch_entries =
5156 flow_graph->graph_entry()->catch_entries(); 5446 flow_graph->graph_entry()->catch_entries();
5157 intptr_t base = kFirstLocalSlotFromFp + flow_graph->num_non_copied_params(); 5447 intptr_t base = kFirstLocalSlotFromFp + flow_graph->num_non_copied_params();
5158 for (intptr_t catch_idx = 0; 5448 for (intptr_t catch_idx = 0;
5159 catch_idx < catch_entries.length(); 5449 catch_idx < catch_entries.length();
(...skipping 3750 matching lines...) Expand 10 before | Expand all | Expand 10 after
8910 const Object& value = instr->value()->definition()->constant_value(); 9200 const Object& value = instr->value()->definition()->constant_value();
8911 if (IsNonConstant(value)) { 9201 if (IsNonConstant(value)) {
8912 SetValue(instr, non_constant_); 9202 SetValue(instr, non_constant_);
8913 } else if (IsConstant(value)) { 9203 } else if (IsConstant(value)) {
8914 // TODO(kmillikin): Handle conversion. 9204 // TODO(kmillikin): Handle conversion.
8915 SetValue(instr, non_constant_); 9205 SetValue(instr, non_constant_);
8916 } 9206 }
8917 } 9207 }
8918 9208
8919 9209
9210 void ConstantPropagator::VisitBoxUint32(BoxUint32Instr* instr) {
9211 // TODO(kmillikin): Handle box operation.
9212 SetValue(instr, non_constant_);
9213 }
9214
9215
9216 void ConstantPropagator::VisitUnboxUint32(UnboxUint32Instr* instr) {
9217 // TODO(kmillikin): Handle unbox operation.
9218 SetValue(instr, non_constant_);
9219 }
9220
9221
9222 void ConstantPropagator::VisitUnboxedIntConverter(
9223 UnboxedIntConverterInstr* instr) {
9224 SetValue(instr, non_constant_);
9225 }
9226
9227
9228 void ConstantPropagator::VisitBinaryUint32Op(BinaryUint32OpInstr* instr) {
9229 HandleBinaryOp(instr, instr->op_kind(), *instr->left(), *instr->right());
9230 }
9231
9232
9233 void ConstantPropagator::VisitShiftUint32Op(ShiftUint32OpInstr* instr) {
9234 HandleBinaryOp(instr, instr->op_kind(), *instr->left(), *instr->right());
9235 }
9236
9237
9238 void ConstantPropagator::VisitUnaryUint32Op(UnaryUint32OpInstr* instr) {
9239 // TODO(kmillikin): Handle unary operations.
9240 SetValue(instr, non_constant_);
9241 }
9242
9243
8920 void ConstantPropagator::Analyze() { 9244 void ConstantPropagator::Analyze() {
8921 GraphEntryInstr* entry = graph_->graph_entry(); 9245 GraphEntryInstr* entry = graph_->graph_entry();
8922 reachable_->Add(entry->preorder_number()); 9246 reachable_->Add(entry->preorder_number());
8923 block_worklist_.Add(entry); 9247 block_worklist_.Add(entry);
8924 9248
8925 while (true) { 9249 while (true) {
8926 if (block_worklist_.is_empty()) { 9250 if (block_worklist_.is_empty()) {
8927 if (definition_worklist_.is_empty()) break; 9251 if (definition_worklist_.is_empty()) break;
8928 Definition* definition = definition_worklist_.RemoveLast(); 9252 Definition* definition = definition_worklist_.RemoveLast();
8929 definition_marks_->Remove(definition->ssa_temp_index()); 9253 definition_marks_->Remove(definition->ssa_temp_index());
(...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after
9783 } 10107 }
9784 10108
9785 // Insert materializations at environment uses. 10109 // Insert materializations at environment uses.
9786 for (intptr_t i = 0; i < exits.length(); i++) { 10110 for (intptr_t i = 0; i < exits.length(); i++) {
9787 CreateMaterializationAt(exits[i], alloc, alloc->cls(), *slots); 10111 CreateMaterializationAt(exits[i], alloc, alloc->cls(), *slots);
9788 } 10112 }
9789 } 10113 }
9790 10114
9791 10115
9792 } // namespace dart 10116 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698