48ade15842ddb7766ba42f1fd8787ed69c0f1041
2 * Copyright 2011 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #include "nv50_ir_target.h"
26 #define __STDC_FORMAT_MACROS
43 static const char *_colour
[8] =
55 static const char *_nocolour
[8] =
57 "", "", "", "", "", "", "", ""
60 static const char **colour
;
62 static void init_colours()
64 if (getenv("NV50_PROG_DEBUG_NO_COLORS") != NULL
)
70 const char *operationStr
[OP_LAST
+ 1] =
189 static const char *atomSubOpStr
[] =
191 "add", "min", "max", "inc", "dec", "and", "or", "xor", "cas", "exch"
194 static const char *DataTypeStr
[] =
205 static const char *RoundModeStr
[] =
207 "", "rm", "rz", "rp", "rni", "rmi", "rzi", "rpi"
210 static const char *CondCodeStr
[] =
238 static const char *SemanticStr
[SV_LAST
+ 1] =
271 static const char *interpStr
[16] =
291 #define PRINT(args...) \
293 pos += snprintf(&buf[pos], size - pos, args); \
296 #define SPACE_PRINT(cond, args...) \
300 pos += snprintf(&buf[pos], size - pos, args); \
309 int Modifier::print(char *buf
, size_t size
) const
314 PRINT("%s", colour
[TXT_INSN
]);
318 if (bits
& NV50_IR_MOD_NOT
)
320 if (bits
& NV50_IR_MOD_SAT
)
321 SPACE_PRINT(pos
> base
&& pos
< size
, "sat");
322 if (bits
& NV50_IR_MOD_NEG
)
323 SPACE_PRINT(pos
> base
&& pos
< size
, "neg");
324 if (bits
& NV50_IR_MOD_ABS
)
325 SPACE_PRINT(pos
> base
&& pos
< size
, "abs");
330 int LValue::print(char *buf
, size_t size
, DataType ty
) const
332 const char *postFix
= "";
334 int idx
= join
->reg
.data
.id
>= 0 ? join
->reg
.data
.id
: id
;
335 char p
= join
->reg
.data
.id
>= 0 ? '$' : '%';
337 int col
= TXT_DEFAULT
;
341 r
= 'r'; col
= TXT_GPR
;
344 postFix
= (idx
& 1) ? "h" : "l";
353 if (reg
.size
== 16) {
356 if (reg
.size
== 12) {
361 r
= 'p'; col
= TXT_REGISTER
;
369 r
= 'c'; col
= TXT_FLAGS
;
372 r
= 'a'; col
= TXT_REGISTER
;
375 assert(!"invalid file for lvalue");
380 PRINT("%s%c%c%i%s", colour
[col
], p
, r
, idx
, postFix
);
385 int ImmediateValue::print(char *buf
, size_t size
, DataType ty
) const
389 PRINT("%s", colour
[TXT_IMMD
]);
392 case TYPE_F32
: PRINT("%f", reg
.data
.f32
); break;
393 case TYPE_F64
: PRINT("%f", reg
.data
.f64
); break;
394 case TYPE_U8
: PRINT("0x%02x", reg
.data
.u8
); break;
395 case TYPE_S8
: PRINT("%i", reg
.data
.s8
); break;
396 case TYPE_U16
: PRINT("0x%04x", reg
.data
.u16
); break;
397 case TYPE_S16
: PRINT("%i", reg
.data
.s16
); break;
398 case TYPE_U32
: PRINT("0x%08x", reg
.data
.u32
); break;
399 case TYPE_S32
: PRINT("%i", reg
.data
.s32
); break;
403 PRINT("0x%016"PRIx64
, reg
.data
.u64
);
409 int Symbol::print(char *buf
, size_t size
, DataType ty
) const
411 return print(buf
, size
, NULL
, NULL
, ty
);
414 int Symbol::print(char *buf
, size_t size
,
415 Value
*rel
, Value
*dimRel
, DataType ty
) const
421 ty
= typeOfSize(reg
.size
);
423 if (reg
.file
== FILE_SYSTEM_VALUE
) {
424 PRINT("%ssv[%s%s:%i%s", colour
[TXT_MEM
],
425 colour
[TXT_REGISTER
],
426 SemanticStr
[reg
.data
.sv
.sv
], reg
.data
.sv
.index
, colour
[TXT_MEM
]);
428 PRINT("%s+", colour
[TXT_DEFAULT
]);
429 pos
+= rel
->print(&buf
[pos
], size
- pos
);
431 PRINT("%s]", colour
[TXT_MEM
]);
436 case FILE_MEMORY_CONST
: c
= 'c'; break;
437 case FILE_SHADER_INPUT
: c
= 'a'; break;
438 case FILE_SHADER_OUTPUT
: c
= 'o'; break;
439 case FILE_MEMORY_GLOBAL
: c
= 'g'; break;
440 case FILE_MEMORY_SHARED
: c
= 's'; break;
441 case FILE_MEMORY_LOCAL
: c
= 'l'; break;
443 assert(!"invalid file");
449 PRINT("%s%c%i[", colour
[TXT_MEM
], c
, reg
.fileIndex
);
451 PRINT("%s%c[", colour
[TXT_MEM
], c
);
454 pos
+= dimRel
->print(&buf
[pos
], size
- pos
, TYPE_S32
);
455 PRINT("%s][", colour
[TXT_MEM
]);
459 pos
+= rel
->print(&buf
[pos
], size
- pos
);
460 PRINT("%s%c", colour
[TXT_DEFAULT
], (reg
.data
.offset
< 0) ? '-' : '+');
462 assert(reg
.data
.offset
>= 0);
464 PRINT("%s0x%x%s]", colour
[TXT_IMMD
], abs(reg
.data
.offset
), colour
[TXT_MEM
]);
469 void Instruction::print() const
473 const size_t size
= BUFSZ
;
479 PRINT("%s", colour
[TXT_INSN
]);
485 const size_t pre
= pos
;
486 if (getSrc(predSrc
)->reg
.file
== FILE_PREDICATE
) {
490 PRINT("%s", CondCodeStr
[cc
]);
494 pos
+= getSrc(predSrc
)->print(&buf
[pos
], BUFSZ
- pos
);
495 PRINT(" %s", colour
[TXT_INSN
]);
502 PRINT("%s", operationStr
[op
]);
503 if (op
== OP_CALL
&& asFlow()->builtin
) {
504 PRINT(" %sBUILTIN:%i", colour
[TXT_BRA
], asFlow()->target
.builtin
);
506 if (op
== OP_CALL
&& asFlow()->target
.fn
) {
507 PRINT(" %s%s:%i", colour
[TXT_BRA
],
508 asFlow()->target
.fn
->getName(),
509 asFlow()->target
.fn
->getLabel());
511 if (asFlow()->target
.bb
)
512 PRINT(" %sBB:%i", colour
[TXT_BRA
], asFlow()->target
.bb
->getId());
514 PRINT("%s ", operationStr
[op
]);
515 if (op
== OP_LINTERP
|| op
== OP_PINTERP
)
516 PRINT("%s ", interpStr
[ipa
]);
520 if (subOp
< Elements(atomSubOpStr
))
521 PRINT("%s ", atomSubOpStr
[subOp
]);
525 PRINT("(SUBOP:%u) ", subOp
);
531 PRINT("%s ", asTex()->tex
.target
.getName());
533 PRINT("x2^%i ", postFactor
);
534 PRINT("%s%s", dnz
? "dnz " : (ftz
? "ftz " : ""), DataTypeStr
[dType
]);
538 PRINT(" %s", RoundModeStr
[rnd
]);
542 for (d
= 0; defExists(d
); ++d
) {
544 pos
+= getDef(d
)->print(&buf
[pos
], size
- pos
);
547 PRINT(" %s}", colour
[TXT_INSN
]);
550 PRINT(" %s#", colour
[TXT_INSN
]);
553 PRINT(" %s%s", colour
[TXT_INSN
], CondCodeStr
[asCmp()->setCond
]);
556 PRINT(" %s%s", colour
[TXT_INSN
], DataTypeStr
[sType
]);
558 for (s
= 0; srcExists(s
); ++s
) {
559 if (s
== predSrc
|| src(s
).usedAsPtr
)
561 const size_t pre
= pos
;
563 pos
+= src(s
).mod
.print(&buf
[pos
], BUFSZ
- pos
);
566 if (src(s
).isIndirect(0) || src(s
).isIndirect(1))
567 pos
+= getSrc(s
)->asSym()->print(&buf
[pos
], BUFSZ
- pos
,
571 pos
+= getSrc(s
)->print(&buf
[pos
], BUFSZ
- pos
, sType
);
574 PRINT("%s exit", colour
[TXT_INSN
]);
576 PRINT("%s", colour
[TXT_DEFAULT
]);
578 buf
[MIN2(pos
, BUFSZ
- 1)] = 0;
580 INFO("%s (%u)\n", buf
, encSize
);
583 class PrintPass
: public Pass
586 PrintPass() : serial(0) { }
588 virtual bool visit(Function
*);
589 virtual bool visit(BasicBlock
*);
590 virtual bool visit(Instruction
*);
597 PrintPass::visit(Function
*fn
)
599 INFO("\n%s:%i\n", fn
->getName(), fn
->getLabel());
605 PrintPass::visit(BasicBlock
*bb
)
609 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next())
610 INFO(" <- BB:%i (%s)\n",
611 BasicBlock::get(ei
.getNode())->getId(),
612 ei
.getEdge()->typeStr());
614 INFO("BB:%i (%u instructions) - ", bb
->getId(), bb
->getInsnCount());
617 INFO("idom = BB:%i, ", bb
->idom()->getId());
620 for (DLList::Iterator df
= bb
->getDF().iterator(); !df
.end(); df
.next())
621 INFO("BB:%i ", BasicBlock::get(df
)->getId());
625 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next())
626 INFO(" -> BB:%i (%s)\n",
627 BasicBlock::get(ei
.getNode())->getId(),
628 ei
.getEdge()->typeStr());
634 PrintPass::visit(Instruction
*insn
)
636 INFO("%3i: ", serial
++);
645 pass
.run(this, true, false);
653 pass
.run(this, true, false);
657 Function::printLiveIntervals() const
659 INFO("printing live intervals ...\n");
661 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next()) {
662 const Value
*lval
= Value::get(it
)->asLValue();
663 if (lval
&& !lval
->livei
.isEmpty()) {
664 INFO("livei(%%%i): ", lval
->id
);
670 } // namespace nv50_ir