blob: c37850112c0dbe0084ab4a08b64fa1c63c916485 [file] [log] [blame]
ethannicholas22f939e2016-10-13 13:25:34 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SKSL_CFGGENERATOR
9#define SKSL_CFGGENERATOR
10
11#include "ir/SkSLExpression.h"
12#include "ir/SkSLFunctionDefinition.h"
13
14#include <set>
15#include <stack>
16
17namespace SkSL {
18
19// index of a block within CFG.fBlocks
20typedef size_t BlockId;
21
22struct BasicBlock {
23 struct Node {
24 enum Kind {
25 kStatement_Kind,
26 kExpression_Kind
27 };
28
29 Kind fKind;
30 const IRNode* fNode;
31 };
32
33 std::vector<Node> fNodes;
34 std::set<BlockId> fEntrances;
35 std::set<BlockId> fExits;
36 // variable definitions upon entering this basic block (null expression = undefined)
37 std::unordered_map<const Variable*, const Expression*> fBefore;
38};
39
40struct CFG {
41 BlockId fStart;
42 BlockId fExit;
43 std::vector<BasicBlock> fBlocks;
44
45 void dump();
46
47private:
48 BlockId fCurrent;
49
50 // Adds a new block, adds an exit* from the current block to the new block, then marks the new
51 // block as the current block
52 // *see note in addExit()
53 BlockId newBlock();
54
55 // Adds a new block, but does not mark it current or add an exit from the current block
56 BlockId newIsolatedBlock();
57
58 // Adds an exit from the 'from' block to the 'to' block
59 // Note that we skip adding the exit if the 'from' block is itself unreachable; this means that
60 // we don't actually have to trace the tree to see if a particular block is unreachable, we can
61 // just check to see if it has any entrances. This does require a bit of care in the order in
62 // which we set the CFG up.
63 void addExit(BlockId from, BlockId to);
64
65 friend class CFGGenerator;
66};
67
68/**
69 * Converts functions into control flow graphs.
70 */
71class CFGGenerator {
72public:
73 CFGGenerator() {}
74
75 CFG getCFG(const FunctionDefinition& f);
76
77private:
78 void addStatement(CFG& cfg, const Statement* s);
79
80 void addExpression(CFG& cfg, const Expression* e);
81
82 void addLValue(CFG& cfg, const Expression* e);
83
84 std::stack<BlockId> fLoopContinues;
85 std::stack<BlockId> fLoopExits;
86};
87
88}
89
90#endif