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

Side by Side Diff: vm/intermediate_language.h

Issue 10539108: First step to SSA construction: Phi insertion. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: addressed comments, fixed bug in BitVector::Contains Created 8 years, 6 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
« no previous file with comments | « vm/il_printer.cc ('k') | vm/intermediate_language.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 #ifndef VM_INTERMEDIATE_LANGUAGE_H_ 5 #ifndef VM_INTERMEDIATE_LANGUAGE_H_
6 #define VM_INTERMEDIATE_LANGUAGE_H_ 6 #define VM_INTERMEDIATE_LANGUAGE_H_
7 7
8 #include "vm/allocation.h" 8 #include "vm/allocation.h"
9 #include "vm/ast.h" 9 #include "vm/ast.h"
10 #include "vm/growable_array.h" 10 #include "vm/growable_array.h"
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 virtual RawAbstractType* StaticType() const; \ 275 virtual RawAbstractType* StaticType() const; \
276 virtual LocationSummary* MakeLocationSummary() const; \ 276 virtual LocationSummary* MakeLocationSummary() const; \
277 virtual void EmitNativeCode(FlowGraphCompiler* compiler); 277 virtual void EmitNativeCode(FlowGraphCompiler* compiler);
278 278
279 // Functions defined in all concrete value classes. 279 // Functions defined in all concrete value classes.
280 #define DECLARE_VALUE(ShortName) \ 280 #define DECLARE_VALUE(ShortName) \
281 DECLARE_COMPUTATION(ShortName) \ 281 DECLARE_COMPUTATION(ShortName) \
282 virtual void PrintTo(BufferFormatter* f) const; 282 virtual void PrintTo(BufferFormatter* f) const;
283 283
284 284
285 class Definition;
285 class BindInstr; 286 class BindInstr;
287 class PhiInstr;
286 288
287 class UseVal : public Value { 289 class UseVal : public Value {
288 public: 290 public:
289 explicit UseVal(BindInstr* definition) : definition_(definition) {} 291 explicit UseVal(Definition* definition) : definition_(definition) {}
290 292
291 DECLARE_VALUE(Use) 293 DECLARE_VALUE(Use)
292 294
293 BindInstr* definition() const { return definition_; } 295 Definition* definition() const { return definition_; }
296 void set_definition(Definition* definition) {
297 definition_ = definition;
298 }
294 299
295 private: 300 private:
296 BindInstr* definition_; 301 Definition* definition_;
297 302
298 DISALLOW_COPY_AND_ASSIGN(UseVal); 303 DISALLOW_COPY_AND_ASSIGN(UseVal);
299 }; 304 };
300 305
301 306
302 class ConstantVal: public Value { 307 class ConstantVal: public Value {
303 public: 308 public:
304 explicit ConstantVal(const Object& value) 309 explicit ConstantVal(const Object& value)
305 : value_(value) { 310 : value_(value) {
306 ASSERT(value.IsZoneHandle()); 311 ASSERT(value.IsZoneHandle());
(...skipping 1228 matching lines...) Expand 10 before | Expand all | Expand 10 after
1535 1540
1536 // M is a single argument macro. It is applied to each concrete instruction 1541 // M is a single argument macro. It is applied to each concrete instruction
1537 // type name. The concrete instruction classes are the name with Instr 1542 // type name. The concrete instruction classes are the name with Instr
1538 // concatenated. 1543 // concatenated.
1539 #define FOR_EACH_INSTRUCTION(M) \ 1544 #define FOR_EACH_INSTRUCTION(M) \
1540 M(GraphEntry) \ 1545 M(GraphEntry) \
1541 M(JoinEntry) \ 1546 M(JoinEntry) \
1542 M(TargetEntry) \ 1547 M(TargetEntry) \
1543 M(Do) \ 1548 M(Do) \
1544 M(Bind) \ 1549 M(Bind) \
1550 M(Phi) \
1545 M(Return) \ 1551 M(Return) \
1546 M(Throw) \ 1552 M(Throw) \
1547 M(ReThrow) \ 1553 M(ReThrow) \
1548 M(Branch) \ 1554 M(Branch) \
1549 1555
1550 1556
1551 // Forward declarations for Instruction classes. 1557 // Forward declarations for Instruction classes.
1552 class BlockEntryInstr; 1558 class BlockEntryInstr;
1553 class FlowGraphBuilder; 1559 class FlowGraphBuilder;
1554 1560
1555 #define FORWARD_DECLARATION(type) class type##Instr; 1561 #define FORWARD_DECLARATION(type) class type##Instr;
1556 FOR_EACH_INSTRUCTION(FORWARD_DECLARATION) 1562 FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1557 #undef FORWARD_DECLARATION 1563 #undef FORWARD_DECLARATION
1558 1564
1559 1565
1560 // Functions required in all concrete instruction classes. 1566 // Functions required in all concrete instruction classes.
1561 #define DECLARE_INSTRUCTION(type) \ 1567 #define DECLARE_INSTRUCTION(type) \
1562 virtual Instruction* Accept(FlowGraphVisitor* visitor); \ 1568 virtual Instruction* Accept(FlowGraphVisitor* visitor); \
1563 virtual bool Is##type() const { return true; } \ 1569 virtual bool Is##type() const { return true; } \
1564 virtual type##Instr* As##type() { return this; } \ 1570 virtual type##Instr* As##type() { return this; } \
1565 virtual intptr_t InputCount() const; \ 1571 virtual intptr_t InputCount() const; \
1572 virtual Value* InputAt(intptr_t i) const; \
1566 virtual const char* DebugName() const { return #type; } \ 1573 virtual const char* DebugName() const { return #type; } \
1567 virtual void PrintTo(BufferFormatter* f) const; \ 1574 virtual void PrintTo(BufferFormatter* f) const; \
1568 virtual void PrintToVisualizer(BufferFormatter* f) const; 1575 virtual void PrintToVisualizer(BufferFormatter* f) const;
1569 1576
1570 1577
1571 class Instruction : public ZoneAllocated { 1578 class Instruction : public ZoneAllocated {
1572 public: 1579 public:
1573 Instruction() : cid_(-1), ic_data_(NULL) { 1580 Instruction() : cid_(-1), ic_data_(NULL) {
1574 Isolate* isolate = Isolate::Current(); 1581 Isolate* isolate = Isolate::Current();
1575 cid_ = Computation::GetNextCid(isolate); 1582 cid_ = Computation::GetNextCid(isolate);
1576 ic_data_ = Computation::GetICDataForCid(cid_, isolate); 1583 ic_data_ = Computation::GetICDataForCid(cid_, isolate);
1577 } 1584 }
1578 1585
1579 // Unique computation/instruction id, used for deoptimization, e.g. for 1586 // Unique computation/instruction id, used for deoptimization, e.g. for
1580 // ReturnInstr, ThrowInstr and ReThrowInstr. 1587 // ReturnInstr, ThrowInstr and ReThrowInstr.
1581 intptr_t cid() const { return cid_; } 1588 intptr_t cid() const { return cid_; }
1582 1589
1583 const ICData* ic_data() const { return ic_data_; } 1590 const ICData* ic_data() const { return ic_data_; }
1584 1591
1585 virtual bool IsBlockEntry() const { return false; } 1592 virtual bool IsBlockEntry() const { return false; }
1586 BlockEntryInstr* AsBlockEntry() { 1593 BlockEntryInstr* AsBlockEntry() {
1587 return IsBlockEntry() ? reinterpret_cast<BlockEntryInstr*>(this) : NULL; 1594 return IsBlockEntry() ? reinterpret_cast<BlockEntryInstr*>(this) : NULL;
1588 } 1595 }
1589 virtual bool IsBindInstr() const { return false; } 1596 virtual bool IsDefinition() const { return false; }
1590 virtual BindInstr* AsBindInstr() { 1597 virtual Definition* AsDefinition() { return NULL; }
1591 return NULL;
1592 }
1593 1598
1594 virtual intptr_t InputCount() const = 0; 1599 virtual intptr_t InputCount() const = 0;
1600 virtual Value* InputAt(intptr_t i) const = 0;
1595 1601
1596 // Visiting support. 1602 // Visiting support.
1597 virtual Instruction* Accept(FlowGraphVisitor* visitor) = 0; 1603 virtual Instruction* Accept(FlowGraphVisitor* visitor) = 0;
1598 1604
1599 virtual Instruction* StraightLineSuccessor() const = 0; 1605 virtual Instruction* StraightLineSuccessor() const = 0;
1600 virtual void SetSuccessor(Instruction* instr) = 0; 1606 virtual void SetSuccessor(Instruction* instr) = 0;
1601 1607
1602 // Normal instructions can have 0 (inside a block) or 1 (last instruction in 1608 // Normal instructions can have 0 (inside a block) or 1 (last instruction in
1603 // a block) successors. Branch instruction with >1 successors override this 1609 // a block) successors. Branch instruction with >1 successors override this
1604 // function. 1610 // function.
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1711 1717
1712 intptr_t postorder_number() const { return postorder_number_; } 1718 intptr_t postorder_number() const { return postorder_number_; }
1713 void set_postorder_number(intptr_t number) { postorder_number_ = number; } 1719 void set_postorder_number(intptr_t number) { postorder_number_ = number; }
1714 1720
1715 intptr_t block_id() const { return block_id_; } 1721 intptr_t block_id() const { return block_id_; }
1716 void set_block_id(intptr_t value) { block_id_ = value; } 1722 void set_block_id(intptr_t value) { block_id_ = value; }
1717 1723
1718 BlockEntryInstr* dominator() const { return dominator_; } 1724 BlockEntryInstr* dominator() const { return dominator_; }
1719 void set_dominator(BlockEntryInstr* instr) { dominator_ = instr; } 1725 void set_dominator(BlockEntryInstr* instr) { dominator_ = instr; }
1720 1726
1727 const GrowableArray<BlockEntryInstr*>& dominated_blocks() {
1728 return dominated_blocks_;
1729 }
1730
1731 void AddDominatedBlock(BlockEntryInstr* block) {
1732 dominated_blocks_.Add(block);
1733 }
1734
1721 Instruction* last_instruction() const { return last_instruction_; } 1735 Instruction* last_instruction() const { return last_instruction_; }
1722 void set_last_instruction(Instruction* instr) { last_instruction_ = instr; } 1736 void set_last_instruction(Instruction* instr) { last_instruction_ = instr; }
1723 1737
1724 virtual void DiscoverBlocks( 1738 virtual void DiscoverBlocks(
1725 BlockEntryInstr* current_block, 1739 BlockEntryInstr* current_block,
1726 GrowableArray<BlockEntryInstr*>* preorder, 1740 GrowableArray<BlockEntryInstr*>* preorder,
1727 GrowableArray<BlockEntryInstr*>* postorder, 1741 GrowableArray<BlockEntryInstr*>* postorder,
1728 GrowableArray<intptr_t>* parent, 1742 GrowableArray<intptr_t>* parent,
1729 GrowableArray<BitVector*>* assigned_vars, 1743 GrowableArray<BitVector*>* assigned_vars,
1730 intptr_t variable_count); 1744 intptr_t variable_count);
1731 1745
1732 protected: 1746 protected:
1733 BlockEntryInstr() 1747 BlockEntryInstr()
1734 : preorder_number_(-1), 1748 : preorder_number_(-1),
1735 postorder_number_(-1), 1749 postorder_number_(-1),
1736 block_id_(-1), 1750 block_id_(-1),
1737 dominator_(NULL), 1751 dominator_(NULL),
1752 dominated_blocks_(1),
1738 last_instruction_(NULL) { } 1753 last_instruction_(NULL) { }
1739 1754
1740 private: 1755 private:
1741 intptr_t preorder_number_; 1756 intptr_t preorder_number_;
1742 intptr_t postorder_number_; 1757 intptr_t postorder_number_;
1743 intptr_t block_id_; 1758 intptr_t block_id_;
1744 BlockEntryInstr* dominator_; // Immediate dominator, NULL for graph entry. 1759 BlockEntryInstr* dominator_; // Immediate dominator, NULL for graph entry.
1760 // TODO(fschneider): Optimize the case of one child to save space.
1761 GrowableArray<BlockEntryInstr*> dominated_blocks_;
1745 Instruction* last_instruction_; 1762 Instruction* last_instruction_;
1746 1763
1747 DISALLOW_COPY_AND_ASSIGN(BlockEntryInstr); 1764 DISALLOW_COPY_AND_ASSIGN(BlockEntryInstr);
1748 }; 1765 };
1749 1766
1750 1767
1751 class GraphEntryInstr : public BlockEntryInstr { 1768 class GraphEntryInstr : public BlockEntryInstr {
1752 public: 1769 public:
1753 explicit GraphEntryInstr(TargetEntryInstr* normal_entry) 1770 explicit GraphEntryInstr(TargetEntryInstr* normal_entry)
1754 : BlockEntryInstr(), normal_entry_(normal_entry), catch_entries_() { } 1771 : BlockEntryInstr(), normal_entry_(normal_entry), catch_entries_() { }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1786 1803
1787 DISALLOW_COPY_AND_ASSIGN(GraphEntryInstr); 1804 DISALLOW_COPY_AND_ASSIGN(GraphEntryInstr);
1788 }; 1805 };
1789 1806
1790 1807
1791 class JoinEntryInstr : public BlockEntryInstr { 1808 class JoinEntryInstr : public BlockEntryInstr {
1792 public: 1809 public:
1793 JoinEntryInstr() 1810 JoinEntryInstr()
1794 : BlockEntryInstr(), 1811 : BlockEntryInstr(),
1795 predecessors_(2), // Two is the assumed to be the common case. 1812 predecessors_(2), // Two is the assumed to be the common case.
1796 successor_(NULL) { } 1813 successor_(NULL),
1814 phis_(NULL),
1815 phi_count_(0) { }
1797 1816
1798 DECLARE_INSTRUCTION(JoinEntry) 1817 DECLARE_INSTRUCTION(JoinEntry)
1799 1818
1800 virtual intptr_t PredecessorCount() const { return predecessors_.length(); } 1819 virtual intptr_t PredecessorCount() const { return predecessors_.length(); }
1801 virtual BlockEntryInstr* PredecessorAt(intptr_t index) const { 1820 virtual BlockEntryInstr* PredecessorAt(intptr_t index) const {
1802 return predecessors_[index]; 1821 return predecessors_[index];
1803 } 1822 }
1804 virtual void AddPredecessor(BlockEntryInstr* predecessor) { 1823 virtual void AddPredecessor(BlockEntryInstr* predecessor) {
1805 predecessors_.Add(predecessor); 1824 predecessors_.Add(predecessor);
1806 } 1825 }
1807 1826
1808 virtual Instruction* StraightLineSuccessor() const { 1827 virtual Instruction* StraightLineSuccessor() const {
1809 return successor_; 1828 return successor_;
1810 } 1829 }
1811 virtual void SetSuccessor(Instruction* instr) { 1830 virtual void SetSuccessor(Instruction* instr) {
1812 ASSERT(successor_ == NULL); 1831 ASSERT(successor_ == NULL);
1813 successor_ = instr; 1832 successor_ = instr;
1814 } 1833 }
1815 1834
1835 ZoneGrowableArray<PhiInstr*>* phis() const { return phis_; }
1836
1816 virtual void PrepareEntry(FlowGraphCompiler* compiler); 1837 virtual void PrepareEntry(FlowGraphCompiler* compiler);
1817 1838
1839 void InsertPhi(intptr_t var_index, intptr_t var_count);
1840
1841 intptr_t phi_count() const { return phi_count_; }
1842
1818 private: 1843 private:
1819 ZoneGrowableArray<BlockEntryInstr*> predecessors_; 1844 ZoneGrowableArray<BlockEntryInstr*> predecessors_;
1820 Instruction* successor_; 1845 Instruction* successor_;
1846 ZoneGrowableArray<PhiInstr*>* phis_;
1847 intptr_t phi_count_;
1821 1848
1822 DISALLOW_COPY_AND_ASSIGN(JoinEntryInstr); 1849 DISALLOW_COPY_AND_ASSIGN(JoinEntryInstr);
1823 }; 1850 };
1824 1851
1825 1852
1826 class TargetEntryInstr : public BlockEntryInstr { 1853 class TargetEntryInstr : public BlockEntryInstr {
1827 public: 1854 public:
1828 TargetEntryInstr() 1855 TargetEntryInstr()
1829 : BlockEntryInstr(), 1856 : BlockEntryInstr(),
1830 predecessor_(NULL), 1857 predecessor_(NULL),
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1913 } 1940 }
1914 1941
1915 private: 1942 private:
1916 Computation* computation_; 1943 Computation* computation_;
1917 Instruction* successor_; 1944 Instruction* successor_;
1918 1945
1919 DISALLOW_COPY_AND_ASSIGN(DoInstr); 1946 DISALLOW_COPY_AND_ASSIGN(DoInstr);
1920 }; 1947 };
1921 1948
1922 1949
1923 class BindInstr : public Instruction { 1950 // Abstract super-class of all instructions that define a value (Bind, Phi).
1951 class Definition : public Instruction {
1952 public:
1953 Definition() : temp_index_(-1) { }
1954
1955 virtual bool IsDefinition() const { return true; }
1956 virtual Definition* AsDefinition() { return this; }
1957
1958 intptr_t temp_index() const { return temp_index_; }
1959 void set_temp_index(intptr_t index) { temp_index_ = index; }
1960
1961 private:
1962 intptr_t temp_index_;
1963
1964 DISALLOW_COPY_AND_ASSIGN(Definition);
1965 };
1966
1967
1968 class BindInstr : public Definition {
1924 public: 1969 public:
1925 explicit BindInstr(Computation* computation) 1970 explicit BindInstr(Computation* computation)
1926 : temp_index_(-1), computation_(computation), successor_(NULL) { 1971 : computation_(computation), successor_(NULL) {
1927 ASSERT(computation != NULL); 1972 ASSERT(computation != NULL);
1928 computation->set_instr(this); 1973 computation->set_instr(this);
1929 } 1974 }
1930 1975
1931 DECLARE_INSTRUCTION(Bind) 1976 DECLARE_INSTRUCTION(Bind)
1932 1977
1933 virtual bool IsBindInstr() const { return true; }
1934 virtual BindInstr* AsBindInstr() { return this; }
1935
1936 intptr_t temp_index() const { return temp_index_; }
1937 void set_temp_index(intptr_t index) { temp_index_ = index; }
1938
1939 Computation* computation() const { return computation_; } 1978 Computation* computation() const { return computation_; }
1940 virtual void replace_computation(Computation* value) { computation_ = value; } 1979 virtual void replace_computation(Computation* value) { computation_ = value; }
1941 1980
1942 virtual Instruction* StraightLineSuccessor() const { 1981 virtual Instruction* StraightLineSuccessor() const {
1943 return successor_; 1982 return successor_;
1944 } 1983 }
1945 1984
1946 virtual void SetSuccessor(Instruction* instr) { 1985 virtual void SetSuccessor(Instruction* instr) {
1947 ASSERT(successor_ == NULL); 1986 ASSERT(successor_ == NULL);
1948 successor_ = instr; 1987 successor_ = instr;
1949 } 1988 }
1950 1989
1951 // Static type of the underlying computation. 1990 // Static type of the underlying computation.
1952 virtual RawAbstractType* StaticType() const { 1991 virtual RawAbstractType* StaticType() const {
1953 return computation()->StaticType(); 1992 return computation()->StaticType();
1954 } 1993 }
1955 1994
1956 virtual void RecordAssignedVars(BitVector* assigned_vars); 1995 virtual void RecordAssignedVars(BitVector* assigned_vars);
1957 1996
1958 virtual LocationSummary* locs() { 1997 virtual LocationSummary* locs() {
1959 return computation()->locs(); 1998 return computation()->locs();
1960 } 1999 }
1961 2000
1962 virtual void EmitNativeCode(FlowGraphCompiler* compiler); 2001 virtual void EmitNativeCode(FlowGraphCompiler* compiler);
1963 2002
1964 private: 2003 private:
1965 intptr_t temp_index_;
1966 Computation* computation_; 2004 Computation* computation_;
1967 Instruction* successor_; 2005 Instruction* successor_;
1968 2006
1969 DISALLOW_COPY_AND_ASSIGN(BindInstr); 2007 DISALLOW_COPY_AND_ASSIGN(BindInstr);
1970 }; 2008 };
1971 2009
1972 2010
2011 class PhiInstr: public Definition {
2012 public:
2013 explicit PhiInstr(intptr_t num_inputs) : inputs_(num_inputs) {
2014 for (intptr_t i = 0; i < num_inputs; ++i) {
2015 inputs_.Add(NULL);
2016 }
2017 }
2018
2019 DECLARE_INSTRUCTION(Phi)
2020
2021 void SetInputAt(intptr_t i, Value* value) {
2022 inputs_[i] = value;
2023 }
2024
2025 virtual Instruction* StraightLineSuccessor() const { return NULL; }
2026 virtual void SetSuccessor(Instruction* instr) { UNREACHABLE(); }
2027
2028 private:
2029 GrowableArray<Value*> inputs_;
2030
2031 DISALLOW_COPY_AND_ASSIGN(PhiInstr);
2032 };
2033
2034
2035
2036
1973 class ReturnInstr : public InstructionWithInputs { 2037 class ReturnInstr : public InstructionWithInputs {
1974 public: 2038 public:
1975 ReturnInstr(intptr_t token_index, Value* value) 2039 ReturnInstr(intptr_t token_index, Value* value)
1976 : InstructionWithInputs(), token_index_(token_index), value_(value) { 2040 : InstructionWithInputs(), token_index_(token_index), value_(value) {
1977 ASSERT(value_ != NULL); 2041 ASSERT(value_ != NULL);
1978 } 2042 }
1979 2043
1980 DECLARE_INSTRUCTION(Return) 2044 DECLARE_INSTRUCTION(Return)
1981 2045
1982 Value* value() const { return value_; } 2046 Value* value() const { return value_; }
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
2160 const GrowableArray<BlockEntryInstr*>& block_order_; 2224 const GrowableArray<BlockEntryInstr*>& block_order_;
2161 2225
2162 private: 2226 private:
2163 DISALLOW_COPY_AND_ASSIGN(FlowGraphVisitor); 2227 DISALLOW_COPY_AND_ASSIGN(FlowGraphVisitor);
2164 }; 2228 };
2165 2229
2166 2230
2167 } // namespace dart 2231 } // namespace dart
2168 2232
2169 #endif // VM_INTERMEDIATE_LANGUAGE_H_ 2233 #endif // VM_INTERMEDIATE_LANGUAGE_H_
OLDNEW
« no previous file with comments | « vm/il_printer.cc ('k') | vm/intermediate_language.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698