blob: 6133cc5c8ba959bacbd01749640626ba716689ca [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_COMPILER_DIAMOND_H_
6#define V8_COMPILER_DIAMOND_H_
7
8#include "src/v8.h"
9
10#include "src/compiler/common-operator.h"
11#include "src/compiler/graph-inl.h"
12#include "src/compiler/node.h"
13
14namespace v8 {
15namespace internal {
16namespace compiler {
17
18// A helper to make it easier to build diamond-shaped control patterns.
19struct Diamond {
20 Graph* graph;
21 CommonOperatorBuilder* common;
22 Node* branch;
23 Node* if_true;
24 Node* if_false;
25 Node* merge;
26
27 Diamond(Graph* g, CommonOperatorBuilder* b, Node* cond,
28 BranchHint hint = BranchHint::kNone) {
29 graph = g;
30 common = b;
31 branch = graph->NewNode(common->Branch(hint), cond, graph->start());
32 if_true = graph->NewNode(common->IfTrue(), branch);
33 if_false = graph->NewNode(common->IfFalse(), branch);
34 merge = graph->NewNode(common->Merge(2), if_true, if_false);
35 }
36
37 // Place {this} after {that} in control flow order.
38 void Chain(Diamond& that) { branch->ReplaceInput(1, that.merge); }
39
40 // Place {this} after {that} in control flow order.
41 void Chain(Node* that) { branch->ReplaceInput(1, that); }
42
43 // Nest {this} into either the if_true or if_false branch of {that}.
44 void Nest(Diamond& that, bool if_true) {
45 if (if_true) {
46 branch->ReplaceInput(1, that.if_true);
47 that.merge->ReplaceInput(0, merge);
48 } else {
49 branch->ReplaceInput(1, that.if_false);
50 that.merge->ReplaceInput(1, merge);
51 }
52 }
53
54 Node* Phi(MachineType machine_type, Node* tv, Node* fv) {
55 return graph->NewNode(common->Phi(machine_type, 2), tv, fv, merge);
56 }
57
58 Node* EffectPhi(Node* tv, Node* fv) {
59 return graph->NewNode(common->EffectPhi(2), tv, fv, merge);
60 }
61
62 void OverwriteWithPhi(Node* node, MachineType machine_type, Node* tv,
63 Node* fv) {
64 DCHECK(node->InputCount() >= 3);
65 node->set_op(common->Phi(machine_type, 2));
66 node->ReplaceInput(0, tv);
67 node->ReplaceInput(1, fv);
68 node->ReplaceInput(2, merge);
69 node->TrimInputCount(3);
70 }
71
72 void OverwriteWithEffectPhi(Node* node, Node* te, Node* fe) {
73 DCHECK(node->InputCount() >= 3);
74 node->set_op(common->EffectPhi(2));
75 node->ReplaceInput(0, te);
76 node->ReplaceInput(1, fe);
77 node->ReplaceInput(2, merge);
78 node->TrimInputCount(3);
79 }
80};
81}
82}
83} // namespace v8::internal::compiler
84
85#endif // V8_COMPILER_DIAMOND_H_