1/*
2 * Copyright (C) 2015 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(DFG_JIT)
29
30#include "DFGBasicBlock.h"
31#include "DFGBlockMapInlines.h"
32#include "DFGBlockSet.h"
33#include "DFGGraph.h"
34#include <wtf/SingleRootGraph.h>
35
36namespace JSC { namespace DFG {
37
38class CFG {
39 WTF_MAKE_NONCOPYABLE(CFG);
40 WTF_MAKE_FAST_ALLOCATED;
41public:
42 typedef BasicBlock* Node;
43 typedef BlockSet Set;
44 template<typename T> using Map = BlockMap<T>;
45 typedef BlockList List;
46
47 CFG(Graph& graph)
48 : m_graph(graph)
49 {
50 }
51
52 Node root()
53 {
54 ASSERT(m_graph.m_form == SSA || m_graph.m_isInSSAConversion);
55 return m_graph.block(0);
56 }
57
58 List roots()
59 {
60 List result;
61 for (BasicBlock* root : m_graph.m_roots)
62 result.append(root);
63 return result;
64 }
65
66 template<typename T>
67 Map<T> newMap() { return BlockMap<T>(m_graph); }
68
69 DFG::Node::SuccessorsIterable successors(Node node) { return node->successors(); }
70 PredecessorList& predecessors(Node node) { return node->predecessors; }
71
72 unsigned index(Node node) const { return node->index; }
73 Node node(unsigned index) const { return m_graph.block(index); }
74 unsigned numNodes() const { return m_graph.numBlocks(); }
75
76 PointerDump<BasicBlock> dump(Node node) const { return pointerDump(node); }
77
78 void dump(PrintStream& out) const
79 {
80 m_graph.dump(out);
81 }
82
83private:
84 Graph& m_graph;
85};
86
87class CPSCFG : public SingleRootGraph<CFG> {
88public:
89 CPSCFG(Graph& graph)
90 : SingleRootGraph<CFG>(*graph.m_ssaCFG)
91 {
92 ASSERT(graph.m_roots.size());
93 }
94};
95
96using SSACFG = CFG;
97
98template <typename T, typename = typename std::enable_if<std::is_same<T, CPSCFG>::value>::type>
99CPSCFG& selectCFG(Graph& graph)
100{
101 return graph.ensureCPSCFG();
102}
103
104template <typename T, typename = typename std::enable_if<std::is_same<T, SSACFG>::value>::type>
105SSACFG& selectCFG(Graph& graph)
106{
107 RELEASE_ASSERT(graph.m_ssaCFG);
108 return *graph.m_ssaCFG;
109}
110
111} } // namespace JSC::DFG
112
113#endif // ENABLE(DFG_JIT)
114