| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifdef JS_JITSPEW |
| 8 |
|
| 9 |
# include "jit/JSONSpewer.h" |
| 10 |
|
| 11 |
# include "jit/BacktrackingAllocator.h" |
| 12 |
# include "jit/LIR.h" |
| 13 |
# include "jit/MIR.h" |
| 14 |
# include "jit/MIRGraph.h" |
| 15 |
# include "jit/RangeAnalysis.h" |
| 16 |
# include "wasm/WasmMetadata.h" |
| 17 |
|
| 18 |
using namespace js; |
| 19 |
using namespace js::jit; |
| 20 |
|
| 21 |
void JSONSpewer::beginFunction(JSScript* script) { |
| 22 |
beginObject(); |
| 23 |
formatProperty("name", "%s:%u", script->filename(), script->lineno()); |
| 24 |
beginListProperty("passes"); |
| 25 |
} |
| 26 |
|
| 27 |
void JSONSpewer::beginWasmFunction(unsigned funcIndex) { |
| 28 |
beginObject(); |
| 29 |
formatProperty("name", "wasm-func%u", funcIndex); |
| 30 |
beginListProperty("passes"); |
| 31 |
} |
| 32 |
|
| 33 |
void JSONSpewer::beginPass(const char* pass) { |
| 34 |
beginObject(); |
| 35 |
property("name", pass); |
| 36 |
} |
| 37 |
|
| 38 |
void JSONSpewer::spewMResumePoint(MResumePoint* rp) { |
| 39 |
if (!rp) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
beginObjectProperty("resumePoint"); |
| 44 |
|
| 45 |
if (rp->caller()) { |
| 46 |
property("caller", rp->caller()->block()->id()); |
| 47 |
} |
| 48 |
|
| 49 |
property("mode", ResumeModeToString(rp->mode())); |
| 50 |
|
| 51 |
beginListProperty("operands"); |
| 52 |
for (MResumePoint* iter = rp; iter; iter = iter->caller()) { |
| 53 |
for (int i = iter->numOperands() - 1; i >= 0; i--) { |
| 54 |
value(iter->getOperand(i)->id()); |
| 55 |
} |
| 56 |
if (iter->caller()) { |
| 57 |
value("|"); |
| 58 |
} |
| 59 |
} |
| 60 |
endList(); |
| 61 |
|
| 62 |
endObject(); |
| 63 |
} |
| 64 |
|
| 65 |
void JSONSpewer::spewMDef(MDefinition* def) { |
| 66 |
beginObject(); |
| 67 |
|
| 68 |
property("id", def->id()); |
| 69 |
|
| 70 |
propertyName("opcode"); |
| 71 |
out_.printf("\""); |
| 72 |
def->printOpcode(out_); |
| 73 |
out_.printf("\""); |
| 74 |
|
| 75 |
beginListProperty("attributes"); |
| 76 |
# define OUTPUT_ATTRIBUTE(X) \ |
| 77 |
do { \ |
| 78 |
if (def->is##X()) value(#X); \ |
| 79 |
} while (0); |
| 80 |
MIR_FLAG_LIST(OUTPUT_ATTRIBUTE); |
| 81 |
# undef OUTPUT_ATTRIBUTE |
| 82 |
endList(); |
| 83 |
|
| 84 |
beginListProperty("inputs"); |
| 85 |
for (size_t i = 0, e = def->numOperands(); i < e; i++) { |
| 86 |
value(def->getOperand(i)->id()); |
| 87 |
} |
| 88 |
endList(); |
| 89 |
|
| 90 |
beginListProperty("uses"); |
| 91 |
for (MUseDefIterator use(def); use; use++) { |
| 92 |
value(use.def()->id()); |
| 93 |
} |
| 94 |
endList(); |
| 95 |
|
| 96 |
if (!def->isLowered()) { |
| 97 |
beginListProperty("memInputs"); |
| 98 |
if (def->dependency()) { |
| 99 |
value(def->dependency()->id()); |
| 100 |
} |
| 101 |
endList(); |
| 102 |
} |
| 103 |
|
| 104 |
bool isTruncated = false; |
| 105 |
if (def->isAdd() || def->isSub() || def->isMod() || def->isMul() || |
| 106 |
def->isDiv()) { |
| 107 |
isTruncated = static_cast<MBinaryArithInstruction*>(def)->isTruncated(); |
| 108 |
} |
| 109 |
|
| 110 |
beginStringProperty("type"); |
| 111 |
if (def->type() != MIRType::None && def->range()) { |
| 112 |
def->range()->dump(out_); |
| 113 |
out_.printf(": "); |
| 114 |
} |
| 115 |
if (def->wasmRefType().isSome()) { |
| 116 |
out_.printf("%s: ", |
| 117 |
wasm::ToString(def->wasmRefType(), wasmCodeMeta_->types).get()); |
| 118 |
} |
| 119 |
out_.printf("%s", StringFromMIRType(def->type())); |
| 120 |
if (isTruncated) { |
| 121 |
out_.printf(" (t)"); |
| 122 |
} |
| 123 |
endStringProperty(); |
| 124 |
|
| 125 |
if (def->isInstruction()) { |
| 126 |
if (MResumePoint* rp = def->toInstruction()->resumePoint()) { |
| 127 |
spewMResumePoint(rp); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
endObject(); |
| 132 |
} |
| 133 |
|
| 134 |
void JSONSpewer::spewMIR(MIRGraph* mir) { |
| 135 |
beginObjectProperty("mir"); |
| 136 |
beginListProperty("blocks"); |
| 137 |
|
| 138 |
for (MBasicBlockIterator block(mir->begin()); block != mir->end(); block++) { |
| 139 |
beginObject(); |
| 140 |
|
| 141 |
property("number", block->id()); |
| 142 |
|
| 143 |
beginListProperty("attributes"); |
| 144 |
if (block->hasLastIns()) { |
| 145 |
if (block->isLoopBackedge()) { |
| 146 |
value("backedge"); |
| 147 |
} |
| 148 |
if (block->isLoopHeader()) { |
| 149 |
value("loopheader"); |
| 150 |
} |
| 151 |
if (block->isSplitEdge()) { |
| 152 |
value("splitedge"); |
| 153 |
} |
| 154 |
} |
| 155 |
endList(); |
| 156 |
|
| 157 |
beginListProperty("predecessors"); |
| 158 |
for (size_t i = 0; i < block->numPredecessors(); i++) { |
| 159 |
value(block->getPredecessor(i)->id()); |
| 160 |
} |
| 161 |
endList(); |
| 162 |
|
| 163 |
beginListProperty("successors"); |
| 164 |
if (block->hasLastIns()) { |
| 165 |
for (size_t i = 0; i < block->numSuccessors(); i++) { |
| 166 |
value(block->getSuccessor(i)->id()); |
| 167 |
} |
| 168 |
} |
| 169 |
endList(); |
| 170 |
|
| 171 |
beginListProperty("instructions"); |
| 172 |
for (MPhiIterator phi(block->phisBegin()); phi != block->phisEnd(); phi++) { |
| 173 |
spewMDef(*phi); |
| 174 |
} |
| 175 |
for (MInstructionIterator i(block->begin()); i != block->end(); i++) { |
| 176 |
spewMDef(*i); |
| 177 |
} |
| 178 |
endList(); |
| 179 |
|
| 180 |
spewMResumePoint(block->entryResumePoint()); |
| 181 |
|
| 182 |
endObject(); |
| 183 |
} |
| 184 |
|
| 185 |
endList(); |
| 186 |
endObject(); |
| 187 |
} |
| 188 |
|
| 189 |
void JSONSpewer::spewLIns(LNode* ins) { |
| 190 |
beginObject(); |
| 191 |
|
| 192 |
property("id", ins->id()); |
| 193 |
|
| 194 |
propertyName("opcode"); |
| 195 |
out_.printf("\""); |
| 196 |
ins->dump(out_); |
| 197 |
out_.printf("\""); |
| 198 |
|
| 199 |
beginListProperty("defs"); |
| 200 |
for (size_t i = 0; i < ins->numDefs(); i++) { |
| 201 |
if (ins->isPhi()) { |
| 202 |
value(ins->toPhi()->getDef(i)->virtualRegister()); |
| 203 |
} else { |
| 204 |
value(ins->toInstruction()->getDef(i)->virtualRegister()); |
| 205 |
} |
| 206 |
} |
| 207 |
endList(); |
| 208 |
|
| 209 |
endObject(); |
| 210 |
} |
| 211 |
|
| 212 |
void JSONSpewer::spewLIR(MIRGraph* mir) { |
| 213 |
beginObjectProperty("lir"); |
| 214 |
beginListProperty("blocks"); |
| 215 |
|
| 216 |
for (MBasicBlockIterator i(mir->begin()); i != mir->end(); i++) { |
| 217 |
LBlock* block = i->lir(); |
| 218 |
if (!block) { |
| 219 |
continue; |
| 220 |
} |
| 221 |
|
| 222 |
beginObject(); |
| 223 |
property("number", i->id()); |
| 224 |
|
| 225 |
beginListProperty("instructions"); |
| 226 |
for (size_t p = 0; p < block->numPhis(); p++) { |
| 227 |
spewLIns(block->getPhi(p)); |
| 228 |
} |
| 229 |
for (LInstructionIterator ins(block->begin()); ins != block->end(); ins++) { |
| 230 |
spewLIns(*ins); |
| 231 |
} |
| 232 |
endList(); |
| 233 |
|
| 234 |
endObject(); |
| 235 |
} |
| 236 |
|
| 237 |
endList(); |
| 238 |
endObject(); |
| 239 |
} |
| 240 |
|
| 241 |
void JSONSpewer::spewRanges(BacktrackingAllocator* regalloc) { |
| 242 |
beginObjectProperty("ranges"); |
| 243 |
beginListProperty("blocks"); |
| 244 |
|
| 245 |
for (size_t bno = 0; bno < regalloc->graph.numBlocks(); bno++) { |
| 246 |
beginObject(); |
| 247 |
property("number", bno); |
| 248 |
beginListProperty("vregs"); |
| 249 |
|
| 250 |
LBlock* lir = regalloc->graph.getBlock(bno); |
| 251 |
for (LInstructionIterator ins = lir->begin(); ins != lir->end(); ins++) { |
| 252 |
for (size_t k = 0; k < ins->numDefs(); k++) { |
| 253 |
uint32_t id = ins->getDef(k)->virtualRegister(); |
| 254 |
VirtualRegister* vreg = ®alloc->vregs[id]; |
| 255 |
|
| 256 |
beginObject(); |
| 257 |
property("vreg", id); |
| 258 |
beginListProperty("ranges"); |
| 259 |
|
| 260 |
for (VirtualRegister::RangeIterator iter(*vreg); iter; iter++) { |
| 261 |
LiveRange* range = *iter; |
| 262 |
|
| 263 |
beginObject(); |
| 264 |
property("allocation", |
| 265 |
range->bundle()->allocation().toString().get()); |
| 266 |
property("start", range->from().bits()); |
| 267 |
property("end", range->to().bits()); |
| 268 |
endObject(); |
| 269 |
} |
| 270 |
|
| 271 |
endList(); |
| 272 |
endObject(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
endList(); |
| 277 |
endObject(); |
| 278 |
} |
| 279 |
|
| 280 |
endList(); |
| 281 |
endObject(); |
| 282 |
} |
| 283 |
|
| 284 |
void JSONSpewer::endPass() { endObject(); } |
| 285 |
|
| 286 |
void JSONSpewer::endFunction() { |
| 287 |
endList(); |
| 288 |
endObject(); |
| 289 |
} |
| 290 |
|
| 291 |
#endif /* JS_JITSPEW */ |