1/*
2 * Copyright (C) 2015-2017 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(B3_JIT)
29
30#include "B3HeapRange.h"
31#include <wtf/PrintStream.h>
32
33namespace JSC { namespace B3 {
34
35struct Effects {
36 // True if this cannot continue execution in the current block.
37 bool terminal { false };
38
39 // True if this value can cause execution to terminate abruptly, and that this abrupt termination is
40 // observable. An example of how this gets used is to limit the hoisting of controlDependent values.
41 // Note that if exitsSideways is set to true but reads is bottom, then B3 is free to assume that
42 // after abrupt termination of this procedure, none of the heap will be read. That's usually false,
43 // so make sure that reads corresponds to the set of things that are readable after this function
44 // terminates abruptly.
45 bool exitsSideways { false };
46
47 // True if the instruction may change semantics if hoisted above some control flow. For example,
48 // loads are usually control-dependent because we must assume that any control construct (either
49 // a terminal like Branch or anything that exits sideways, like Check) validates whether the
50 // pointer is valid. Hoisting the load above control may cause the load to trap even though it
51 // would not have otherwise trapped.
52 bool controlDependent { false };
53
54 // True if this writes to the local state. Operations that write local state don't write to anything
55 // in "memory" but they have a side-effect anyway. This is for modeling Upsilons, Sets, and Fences.
56 // This is a way of saying: even though this operation is not a terminal, does not exit sideways,
57 // and does not write to the heap, you still cannot kill this operation.
58 bool writesLocalState { false };
59
60 // True if this reads from the local state. This is only used for Phi and Get.
61 bool readsLocalState { false };
62
63 // B3 understands things about pinned registers. Therefore, it needs to know who reads them and
64 // who writes them. We don't track this on a per-register basis because that would be harder and
65 // we don't need it. Note that if you want to construct an immutable pinned register while also
66 // having other pinned registers that are mutable, then you can use ArgumentReg. Also note that
67 // nobody will stop you from making this get out-of-sync with your clobbered register sets in
68 // Patchpoint. It's recommended that you err on the side of being conservative.
69 // FIXME: Explore making these be RegisterSets. That's mainly hard because it would be awkward to
70 // reconcile with StackmapValue's support for clobbered regs.
71 // https://bugs.webkit.org/show_bug.cgi?id=163173
72 bool readsPinned { false };
73 bool writesPinned { false };
74
75 // Memory fences cannot be reordered around each other regardless of their effects. This is flagged
76 // if the operation is a memory fence.
77 bool fence { false };
78
79 // WARNING: The B3::hoistLoopInvariantValues() phase thinks that it understands this exhaustively. If you
80 // add any new kinds of things that can be read or written, you should check that phase.
81
82 HeapRange writes;
83 HeapRange reads;
84
85 static Effects none()
86 {
87 return Effects();
88 }
89
90 static Effects forCall()
91 {
92 Effects result;
93 result.exitsSideways = true;
94 result.controlDependent = true;
95 result.writes = HeapRange::top();
96 result.reads = HeapRange::top();
97 result.readsPinned = true;
98 result.writesPinned = true;
99 result.fence = true;
100 return result;
101 }
102
103 static Effects forCheck()
104 {
105 Effects result;
106 result.exitsSideways = true;
107 // The program could read anything after exiting, and it's on us to declare this.
108 result.reads = HeapRange::top();
109 return result;
110 }
111
112 bool mustExecute() const
113 {
114 return terminal || exitsSideways || writesLocalState || writes || writesPinned || fence;
115 }
116
117 // Returns true if reordering instructions with these respective effects would change program
118 // behavior in an observable way.
119 bool interferes(const Effects&) const;
120
121 JS_EXPORT_PRIVATE bool operator==(const Effects&) const;
122 JS_EXPORT_PRIVATE bool operator!=(const Effects&) const;
123
124 JS_EXPORT_PRIVATE void dump(PrintStream& out) const;
125};
126
127} } // namespace JSC::B3
128
129#endif // ENABLE(B3_JIT)
130