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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
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] =
190 static const char *atomSubOpStr
[] =
192 "add", "min", "max", "inc", "dec", "and", "or", "xor", "cas", "exch"
195 static const char *DataTypeStr
[] =
206 static const char *RoundModeStr
[] =
208 "", "rm", "rz", "rp", "rni", "rmi", "rzi", "rpi"
211 static const char *CondCodeStr
[] =
239 static const char *SemanticStr
[SV_LAST
+ 1] =
272 static const char *interpStr
[16] =
292 #define PRINT(args...) \
294 pos += snprintf(&buf[pos], size - pos, args); \
297 #define SPACE_PRINT(cond, args...) \
301 pos += snprintf(&buf[pos], size - pos, args); \
310 int Modifier::print(char *buf
, size_t size
) const
315 PRINT("%s", colour
[TXT_INSN
]);
319 if (bits
& NV50_IR_MOD_NOT
)
321 if (bits
& NV50_IR_MOD_SAT
)
322 SPACE_PRINT(pos
> base
&& pos
< size
, "sat");
323 if (bits
& NV50_IR_MOD_NEG
)
324 SPACE_PRINT(pos
> base
&& pos
< size
, "neg");
325 if (bits
& NV50_IR_MOD_ABS
)
326 SPACE_PRINT(pos
> base
&& pos
< size
, "abs");
331 int LValue::print(char *buf
, size_t size
, DataType ty
) const
333 const char *postFix
= "";
335 int idx
= join
->reg
.data
.id
>= 0 ? join
->reg
.data
.id
: id
;
336 char p
= join
->reg
.data
.id
>= 0 ? '$' : '%';
338 int col
= TXT_DEFAULT
;
342 r
= 'r'; col
= TXT_GPR
;
345 postFix
= (idx
& 1) ? "h" : "l";
354 if (reg
.size
== 16) {
357 if (reg
.size
== 12) {
362 r
= 'p'; col
= TXT_REGISTER
;
370 r
= 'c'; col
= TXT_FLAGS
;
373 r
= 'a'; col
= TXT_REGISTER
;
376 assert(!"invalid file for lvalue");
381 PRINT("%s%c%c%i%s", colour
[col
], p
, r
, idx
, postFix
);
386 int ImmediateValue::print(char *buf
, size_t size
, DataType ty
) const
390 PRINT("%s", colour
[TXT_IMMD
]);
393 case TYPE_F32
: PRINT("%f", reg
.data
.f32
); break;
394 case TYPE_F64
: PRINT("%f", reg
.data
.f64
); break;
395 case TYPE_U8
: PRINT("0x%02x", reg
.data
.u8
); break;
396 case TYPE_S8
: PRINT("%i", reg
.data
.s8
); break;
397 case TYPE_U16
: PRINT("0x%04x", reg
.data
.u16
); break;
398 case TYPE_S16
: PRINT("%i", reg
.data
.s16
); break;
399 case TYPE_U32
: PRINT("0x%08x", reg
.data
.u32
); break;
400 case TYPE_S32
: PRINT("%i", reg
.data
.s32
); break;
404 PRINT("0x%016"PRIx64
, reg
.data
.u64
);
410 int Symbol::print(char *buf
, size_t size
, DataType ty
) const
412 return print(buf
, size
, NULL
, NULL
, ty
);
415 int Symbol::print(char *buf
, size_t size
,
416 Value
*rel
, Value
*dimRel
, DataType ty
) const
422 ty
= typeOfSize(reg
.size
);
424 if (reg
.file
== FILE_SYSTEM_VALUE
) {
425 PRINT("%ssv[%s%s:%i%s", colour
[TXT_MEM
],
426 colour
[TXT_REGISTER
],
427 SemanticStr
[reg
.data
.sv
.sv
], reg
.data
.sv
.index
, colour
[TXT_MEM
]);
429 PRINT("%s+", colour
[TXT_DEFAULT
]);
430 pos
+= rel
->print(&buf
[pos
], size
- pos
);
432 PRINT("%s]", colour
[TXT_MEM
]);
437 case FILE_MEMORY_CONST
: c
= 'c'; break;
438 case FILE_SHADER_INPUT
: c
= 'a'; break;
439 case FILE_SHADER_OUTPUT
: c
= 'o'; break;
440 case FILE_MEMORY_GLOBAL
: c
= 'g'; break;
441 case FILE_MEMORY_SHARED
: c
= 's'; break;
442 case FILE_MEMORY_LOCAL
: c
= 'l'; break;
444 assert(!"invalid file");
450 PRINT("%s%c%i[", colour
[TXT_MEM
], c
, reg
.fileIndex
);
452 PRINT("%s%c[", colour
[TXT_MEM
], c
);
455 pos
+= dimRel
->print(&buf
[pos
], size
- pos
, TYPE_S32
);
456 PRINT("%s][", colour
[TXT_MEM
]);
460 pos
+= rel
->print(&buf
[pos
], size
- pos
);
461 PRINT("%s%c", colour
[TXT_DEFAULT
], (reg
.data
.offset
< 0) ? '-' : '+');
463 assert(reg
.data
.offset
>= 0);
465 PRINT("%s0x%x%s]", colour
[TXT_IMMD
], abs(reg
.data
.offset
), colour
[TXT_MEM
]);
470 void Instruction::print() const
474 const size_t size
= BUFSZ
;
480 PRINT("%s", colour
[TXT_INSN
]);
486 const size_t pre
= pos
;
487 if (getSrc(predSrc
)->reg
.file
== FILE_PREDICATE
) {
491 PRINT("%s", CondCodeStr
[cc
]);
495 pos
+= getSrc(predSrc
)->print(&buf
[pos
], BUFSZ
- pos
);
496 PRINT(" %s", colour
[TXT_INSN
]);
503 PRINT("%s", operationStr
[op
]);
504 if (asFlow()->indirect
)
506 if (asFlow()->absolute
)
508 if (op
== OP_CALL
&& asFlow()->builtin
) {
509 PRINT(" %sBUILTIN:%i", colour
[TXT_BRA
], asFlow()->target
.builtin
);
511 if (op
== OP_CALL
&& asFlow()->target
.fn
) {
512 PRINT(" %s%s:%i", colour
[TXT_BRA
],
513 asFlow()->target
.fn
->getName(),
514 asFlow()->target
.fn
->getLabel());
516 if (asFlow()->target
.bb
)
517 PRINT(" %sBB:%i", colour
[TXT_BRA
], asFlow()->target
.bb
->getId());
519 PRINT("%s ", operationStr
[op
]);
520 if (op
== OP_LINTERP
|| op
== OP_PINTERP
)
521 PRINT("%s ", interpStr
[ipa
]);
525 if (subOp
< Elements(atomSubOpStr
))
526 PRINT("%s ", atomSubOpStr
[subOp
]);
530 PRINT("(SUBOP:%u) ", subOp
);
536 PRINT("%s %s$r%u $s%u %s", asTex()->tex
.target
.getName(),
537 colour
[TXT_MEM
], asTex()->tex
.r
, asTex()->tex
.s
,
540 PRINT("x2^%i ", postFactor
);
541 PRINT("%s%s", dnz
? "dnz " : (ftz
? "ftz " : ""), DataTypeStr
[dType
]);
545 PRINT(" %s", RoundModeStr
[rnd
]);
549 for (d
= 0; defExists(d
); ++d
) {
551 pos
+= getDef(d
)->print(&buf
[pos
], size
- pos
);
554 PRINT(" %s}", colour
[TXT_INSN
]);
557 PRINT(" %s#", colour
[TXT_INSN
]);
560 PRINT(" %s%s", colour
[TXT_INSN
], CondCodeStr
[asCmp()->setCond
]);
563 PRINT(" %s%s", colour
[TXT_INSN
], DataTypeStr
[sType
]);
565 for (s
= 0; srcExists(s
); ++s
) {
566 if (s
== predSrc
|| src(s
).usedAsPtr
)
568 const size_t pre
= pos
;
570 pos
+= src(s
).mod
.print(&buf
[pos
], BUFSZ
- pos
);
573 if (src(s
).isIndirect(0) || src(s
).isIndirect(1))
574 pos
+= getSrc(s
)->asSym()->print(&buf
[pos
], BUFSZ
- pos
,
578 pos
+= getSrc(s
)->print(&buf
[pos
], BUFSZ
- pos
, sType
);
581 PRINT("%s exit", colour
[TXT_INSN
]);
583 PRINT("%s", colour
[TXT_DEFAULT
]);
585 buf
[MIN2(pos
, BUFSZ
- 1)] = 0;
587 INFO("%s (%u)\n", buf
, encSize
);
590 class PrintPass
: public Pass
593 PrintPass() : serial(0) { }
595 virtual bool visit(Function
*);
596 virtual bool visit(BasicBlock
*);
597 virtual bool visit(Instruction
*);
604 PrintPass::visit(Function
*fn
)
608 INFO("\n%s:%i (", fn
->getName(), fn
->getLabel());
610 if (!fn
->outs
.empty())
612 for (std::deque
<ValueRef
>::iterator it
= fn
->outs
.begin();
613 it
!= fn
->outs
.end();
615 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
619 if (!fn
->ins
.empty())
620 INFO("%s%sin", colour
[TXT_DEFAULT
], fn
->outs
.empty() ? "" : ", ");
621 for (std::deque
<ValueDef
>::iterator it
= fn
->ins
.begin();
624 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
627 INFO("%s)\n", colour
[TXT_DEFAULT
]);
633 PrintPass::visit(BasicBlock
*bb
)
637 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next())
638 INFO(" <- BB:%i (%s)\n",
639 BasicBlock::get(ei
.getNode())->getId(),
640 ei
.getEdge()->typeStr());
642 INFO("BB:%i (%u instructions) - ", bb
->getId(), bb
->getInsnCount());
645 INFO("idom = BB:%i, ", bb
->idom()->getId());
648 for (DLList::Iterator df
= bb
->getDF().iterator(); !df
.end(); df
.next())
649 INFO("BB:%i ", BasicBlock::get(df
)->getId());
653 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next())
654 INFO(" -> BB:%i (%s)\n",
655 BasicBlock::get(ei
.getNode())->getId(),
656 ei
.getEdge()->typeStr());
662 PrintPass::visit(Instruction
*insn
)
664 INFO("%3i: ", serial
++);
673 pass
.run(this, true, false);
681 pass
.run(this, true, false);
685 Function::printLiveIntervals() const
687 INFO("printing live intervals ...\n");
689 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next()) {
690 const Value
*lval
= Value::get(it
)->asLValue();
691 if (lval
&& !lval
->livei
.isEmpty()) {
692 INFO("livei(%%%i): ", lval
->id
);
698 } // namespace nv50_ir