Reviewers: Jakob, titzer,

Message:
Ben: PTAL.
Jakob: FYI, comments are always welcome.

Description:
First simplistic implementation of escape analysis.

[email protected],[email protected]

Please review this at https://codereview.chromium.org/17914002/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/flag-definitions.h
  M src/hydrogen-instructions.h
  M src/hydrogen.cc


Index: src/flag-definitions.h
diff --git a/src/flag-definitions.h b/src/flag-definitions.h
index 91186defe4737774362661fba2f130888d3311ad..b07354a5e59f8d2061354e5c54aa2292d5ad7aec 100644
--- a/src/flag-definitions.h
+++ b/src/flag-definitions.h
@@ -214,6 +214,7 @@ DEFINE_bool(use_range, true, "use hydrogen range analysis")
 DEFINE_bool(use_gvn, true, "use hydrogen global value numbering")
DEFINE_bool(use_canonicalizing, true, "use hydrogen instruction canonicalizing")
 DEFINE_bool(use_inlining, true, "use function inlining")
+DEFINE_bool(use_escape_analysis, false, "use hydrogen escape analysis")
 DEFINE_int(max_inlined_source_size, 600,
            "maximum source size in bytes considered for a single inlining")
 DEFINE_int(max_inlined_nodes, 196,
@@ -234,6 +235,7 @@ DEFINE_bool(trace_all_uses, false, "trace all use positions")
 DEFINE_bool(trace_range, false, "trace range analysis")
 DEFINE_bool(trace_gvn, false, "trace global value numbering")
 DEFINE_bool(trace_representation, false, "trace representation types")
+DEFINE_bool(trace_escape_analysis, false, "trace hydrogen escape analysis")
 DEFINE_bool(trace_track_allocation_sites, false,
             "trace the tracking of allocation sites")
 DEFINE_bool(trace_migration, false, "trace object migration")
Index: src/hydrogen-instructions.h
diff --git a/src/hydrogen-instructions.h b/src/hydrogen-instructions.h
index 6de90deb5b3309ff9069abff3f85af5951deaf8f..cdb24e42401342094c0706d29f5555d7a31b940c 100644
--- a/src/hydrogen-instructions.h
+++ b/src/hydrogen-instructions.h
@@ -1058,6 +1058,9 @@ class HValue: public ZoneObject {
   void RemoveLastAddedRange();
   void ComputeInitialRange(Zone* zone);

+  // Escape analysis helpers.
+  virtual bool HasEscapingOperandAt(int index) { return true; }
+
   // Representation helpers.
   virtual Representation observed_input_representation(int index) {
     return Representation::None();
@@ -1429,6 +1432,7 @@ class HDummyUse: public HTemplateInstruction<1> {

   HValue* value() { return OperandAt(0); }

+  virtual bool HasEscapingOperandAt(int index) { return false; }
   virtual Representation RequiredInputRepresentation(int index) {
     return Representation::None();
   }
@@ -1888,6 +1892,7 @@ class HSimulate: public HInstruction {
   virtual int OperandCount() { return values_.length(); }
   virtual HValue* OperandAt(int index) const { return values_[index]; }

+  virtual bool HasEscapingOperandAt(int index) { return false; }
   virtual Representation RequiredInputRepresentation(int index) {
     return Representation::None();
   }
@@ -2797,6 +2802,7 @@ class HCheckMaps: public HTemplateInstruction<2> {
     return check_map;
   }

+  virtual bool HasEscapingOperandAt(int index) { return false; }
   virtual Representation RequiredInputRepresentation(int index) {
     return Representation::Tagged();
   }
@@ -3228,6 +3234,7 @@ class HArgumentsObject: public HTemplateInstruction<0> {
   virtual int OperandCount() { return values_.length(); }
   virtual HValue* OperandAt(int index) const { return values_[index]; }

+  virtual bool HasEscapingOperandAt(int index) { return false; }
   virtual Representation RequiredInputRepresentation(int index) {
     return Representation::None();
   }
@@ -5431,6 +5438,7 @@ class HLoadNamedField: public HTemplateInstruction<2> {
   HObjectAccess access() const { return access_; }
   Representation field_representation() const { return representation_; }

+  virtual bool HasEscapingOperandAt(int index) { return false; }
   virtual Representation RequiredInputRepresentation(int index) {
     return Representation::Tagged();
   }
@@ -5763,6 +5771,7 @@ class HStoreNamedField: public HTemplateInstruction<2> {

   DECLARE_CONCRETE_INSTRUCTION(StoreNamedField)

+  virtual bool HasEscapingOperandAt(int index) { return index == 1; }
   virtual Representation RequiredInputRepresentation(int index) {
     if (FLAG_track_double_fields &&
         index == 1 && field_representation_.IsDouble()) {
Index: src/hydrogen.cc
diff --git a/src/hydrogen.cc b/src/hydrogen.cc
index b03bf699389be7864587415c6143280159f27979..94ff56d373ffe73b4fbecf862ce6e1c04f64e4b4 100644
--- a/src/hydrogen.cc
+++ b/src/hydrogen.cc
@@ -2613,6 +2613,62 @@ void HGraph::InferTypes(ZoneList<HValue*>* worklist) {
 }


+class HEscapeAnalysis BASE_EMBEDDED {
+ public:
+  explicit HEscapeAnalysis(HGraph* graph)
+      : graph_(graph), zone_(graph->zone()), captured_(0, zone_) { }
+
+  void Analyze();
+
+ private:
+  void CollectCapturedValues();
+  void CollectIfNoEscapingUses(HInstruction* instr);
+
+  HGraph* graph_;
+  Zone* zone_;
+  ZoneList<HValue*> captured_;
+};
+
+
+void HEscapeAnalysis::CollectIfNoEscapingUses(HInstruction* instr) {
+  for (HUseIterator it(instr->uses()); !it.Done(); it.Advance()) {
+    HValue* use = it.value();
+    if (use->HasEscapingOperandAt(it.index())) {
+      if (FLAG_trace_escape_analysis) {
+        PrintF("#%d (%s) escapes through #%d (%s) @%d\n", instr->id(),
+               instr->Mnemonic(), use->id(), use->Mnemonic(), it.index());
+      }
+      return;
+    }
+  }
+  if (FLAG_trace_escape_analysis) {
+    PrintF("#%d (%s) is being captured\n", instr->id(), instr->Mnemonic());
+  }
+  captured_.Add(instr, zone_);
+}
+
+
+void HEscapeAnalysis::CollectCapturedValues() {
+  int block_count = graph_->blocks()->length();
+  for (int i = 0; i < block_count; ++i) {
+    HBasicBlock* block = graph_->blocks()->at(i);
+    HInstruction* instr = block->first();
+    while (instr != block->end()) {
+      if (instr->IsAllocate() || instr->IsAllocateObject()) {
+        CollectIfNoEscapingUses(instr);
+      }
+      instr = instr->next();
+    }
+  }
+}
+
+
+void HEscapeAnalysis::Analyze() {
+  HPhase phase("H_Escape analysis", graph_);
+  CollectCapturedValues();
+}
+
+
 class HRangeAnalysis BASE_EMBEDDED {
  public:
   explicit HRangeAnalysis(HGraph* graph) :
@@ -3983,6 +4039,7 @@ bool HGraph::Optimize(SmartArrayPointer<char>* bailout_reason) {
   }

   PropagateDeoptimizingMark();
+
   if (!CheckConstPhiUses()) {
     *bailout_reason = SmartArrayPointer<char>(StrDup(
         "Unsupported phi use of const variable"));
@@ -4029,6 +4086,11 @@ bool HGraph::Optimize(SmartArrayPointer<char>* bailout_reason) {

   if (FLAG_use_canonicalizing) Canonicalize();

+  if (FLAG_use_escape_analysis) {
+    HEscapeAnalysis escapeAnalysis(this);
+    escapeAnalysis.Analyze();
+  }
+
   if (FLAG_use_gvn) GlobalValueNumbering();

   if (FLAG_use_range) {


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to