1/*
2 * Copyright (C) 2015-2018 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#include "config.h"
27#include "B3Generate.h"
28
29#if ENABLE(B3_JIT)
30
31#include "AirCode.h"
32#include "AirGenerate.h"
33#include "AirInstInlines.h"
34#include "B3Common.h"
35#include "B3DuplicateTails.h"
36#include "B3EliminateCommonSubexpressions.h"
37#include "B3EliminateDeadCode.h"
38#include "B3FixSSA.h"
39#include "B3FoldPathConstants.h"
40#include "B3HoistLoopInvariantValues.h"
41#include "B3InferSwitches.h"
42#include "B3LegalizeMemoryOffsets.h"
43#include "B3LowerMacros.h"
44#include "B3LowerMacrosAfterOptimizations.h"
45#include "B3LowerToAir.h"
46#include "B3MoveConstants.h"
47#include "B3OptimizeAssociativeExpressionTrees.h"
48#include "B3Procedure.h"
49#include "B3PureCSE.h"
50#include "B3ReduceDoubleToFloat.h"
51#include "B3ReduceStrength.h"
52#include "B3TimingScope.h"
53#include "B3Validate.h"
54#include "PCToCodeOriginMap.h"
55
56namespace JSC { namespace B3 {
57
58void prepareForGeneration(Procedure& procedure)
59{
60 TimingScope timingScope("prepareForGeneration");
61
62 generateToAir(procedure);
63 Air::prepareForGeneration(procedure.code());
64}
65
66void generate(Procedure& procedure, CCallHelpers& jit)
67{
68 Air::generate(procedure.code(), jit);
69}
70
71void generateToAir(Procedure& procedure)
72{
73 TimingScope timingScope("generateToAir");
74
75 if (shouldDumpIR(B3Mode) && !shouldDumpIRAtEachPhase(B3Mode)) {
76 dataLog("Initial B3:\n");
77 dataLog(procedure);
78 }
79
80 // We don't require the incoming IR to have predecessors computed.
81 procedure.resetReachability();
82
83 if (shouldValidateIR())
84 validate(procedure);
85
86 if (procedure.optLevel() >= 2) {
87 reduceDoubleToFloat(procedure);
88 reduceStrength(procedure);
89 hoistLoopInvariantValues(procedure);
90 if (eliminateCommonSubexpressions(procedure))
91 eliminateCommonSubexpressions(procedure);
92 eliminateDeadCode(procedure);
93 inferSwitches(procedure);
94 if (Options::useB3TailDup())
95 duplicateTails(procedure);
96 fixSSA(procedure);
97 foldPathConstants(procedure);
98 // FIXME: Add more optimizations here.
99 // https://bugs.webkit.org/show_bug.cgi?id=150507
100 } else if (procedure.optLevel() >= 1) {
101 // FIXME: Explore better "quick mode" optimizations.
102 reduceStrength(procedure);
103 }
104
105 // This puts the IR in quirks mode.
106 lowerMacros(procedure);
107
108 if (procedure.optLevel() >= 2) {
109 optimizeAssociativeExpressionTrees(procedure);
110 reduceStrength(procedure);
111
112 // FIXME: Add more optimizations here.
113 // https://bugs.webkit.org/show_bug.cgi?id=150507
114 }
115
116 lowerMacrosAfterOptimizations(procedure);
117 legalizeMemoryOffsets(procedure);
118 moveConstants(procedure);
119 eliminateDeadCode(procedure);
120
121 // FIXME: We should run pureCSE here to clean up some platform specific changes from the previous phases.
122 // https://bugs.webkit.org/show_bug.cgi?id=164873
123
124 if (shouldValidateIR())
125 validate(procedure);
126
127 // If we're doing super verbose dumping, the phase scope of any phase will already do a dump.
128 // Note that lowerToAir() acts like a phase in this regard.
129 if (shouldDumpIR(B3Mode) && !shouldDumpIRAtEachPhase(B3Mode)) {
130 dataLog("B3 after ", procedure.lastPhaseName(), ", before generation:\n");
131 dataLog(procedure);
132 }
133
134 lowerToAir(procedure);
135}
136
137} } // namespace JSC::B3
138
139#endif // ENABLE(B3_JIT)
140
141