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#include "config.h"
27#include "AirReportUsedRegisters.h"
28
29#if ENABLE(B3_JIT)
30
31#include "AirArgInlines.h"
32#include "AirCode.h"
33#include "AirInstInlines.h"
34#include "AirPadInterference.h"
35#include "AirRegLiveness.h"
36#include "AirPhaseScope.h"
37
38namespace JSC { namespace B3 { namespace Air {
39
40void reportUsedRegisters(Code& code)
41{
42 PhaseScope phaseScope(code, "reportUsedRegisters");
43
44 static constexpr bool verbose = false;
45
46 padInterference(code);
47
48 if (verbose)
49 dataLog("Doing reportUsedRegisters on:\n", code);
50
51 RegLiveness liveness(code);
52
53 for (BasicBlock* block : code) {
54 if (verbose)
55 dataLog("Looking at: ", *block, "\n");
56
57 RegLiveness::LocalCalc localCalc(liveness, block);
58
59 for (unsigned instIndex = block->size(); instIndex--;) {
60 Inst& inst = block->at(instIndex);
61
62 if (verbose)
63 dataLog(" Looking at: ", inst, "\n");
64
65 // Kill dead assignments to registers. For simplicity we say that a store is killable if
66 // it has only late defs and those late defs are to registers that are dead right now.
67 if (!inst.hasNonArgEffects()) {
68 bool canDelete = true;
69 inst.forEachArg(
70 [&] (Arg& arg, Arg::Role role, Bank, Width) {
71 if (Arg::isEarlyDef(role)) {
72 if (verbose)
73 dataLog(" Cannot delete because of ", arg, "\n");
74 canDelete = false;
75 return;
76 }
77 if (!Arg::isLateDef(role))
78 return;
79 if (!arg.isReg()) {
80 if (verbose)
81 dataLog(" Cannot delete because arg is not reg: ", arg, "\n");
82 canDelete = false;
83 return;
84 }
85 if (localCalc.isLive(arg.reg())) {
86 if (verbose)
87 dataLog(" Cannot delete because arg is live: ", arg, "\n");
88 canDelete = false;
89 return;
90 }
91 });
92 if (canDelete)
93 inst = Inst();
94 }
95
96 if (inst.kind.opcode == Patch) {
97 RegisterSet registerSet;
98 for (Reg reg : localCalc.live())
99 registerSet.set(reg);
100 inst.reportUsedRegisters(registerSet);
101 }
102 localCalc.execute(instIndex);
103 }
104
105 block->insts().removeAllMatching(
106 [&] (const Inst& inst) -> bool {
107 return !inst;
108 });
109 }
110
111 if (verbose)
112 dataLog("After reportUsedRegisters:\n", code);
113}
114
115} } } // namespace JSC::B3::Air
116
117#endif // ENABLE(B3_JIT)
118
119
120