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.
23 #include "codegen/nv50_ir.h"
24 #include "codegen/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] =
194 static const char *atomSubOpStr
[] =
196 "add", "min", "max", "inc", "dec", "and", "or", "xor", "cas", "exch"
199 static const char *DataTypeStr
[] =
210 static const char *RoundModeStr
[] =
212 "", "rm", "rz", "rp", "rni", "rmi", "rzi", "rpi"
215 static const char *CondCodeStr
[] =
243 static const char *SemanticStr
[SV_LAST
+ 1] =
280 static const char *interpStr
[16] =
300 #define PRINT(args...) \
302 pos += snprintf(&buf[pos], size - pos, args); \
305 #define SPACE_PRINT(cond, args...) \
309 pos += snprintf(&buf[pos], size - pos, args); \
318 int Modifier::print(char *buf
, size_t size
) const
323 PRINT("%s", colour
[TXT_INSN
]);
327 if (bits
& NV50_IR_MOD_NOT
)
329 if (bits
& NV50_IR_MOD_SAT
)
330 SPACE_PRINT(pos
> base
&& pos
< size
, "sat");
331 if (bits
& NV50_IR_MOD_NEG
)
332 SPACE_PRINT(pos
> base
&& pos
< size
, "neg");
333 if (bits
& NV50_IR_MOD_ABS
)
334 SPACE_PRINT(pos
> base
&& pos
< size
, "abs");
339 int LValue::print(char *buf
, size_t size
, DataType ty
) const
341 const char *postFix
= "";
343 int idx
= join
->reg
.data
.id
>= 0 ? join
->reg
.data
.id
: id
;
344 char p
= join
->reg
.data
.id
>= 0 ? '$' : '%';
346 int col
= TXT_DEFAULT
;
350 r
= 'r'; col
= TXT_GPR
;
353 postFix
= (idx
& 1) ? "h" : "l";
362 if (reg
.size
== 16) {
365 if (reg
.size
== 12) {
370 r
= 'p'; col
= TXT_REGISTER
;
378 r
= 'c'; col
= TXT_FLAGS
;
381 r
= 'a'; col
= TXT_REGISTER
;
384 assert(!"invalid file for lvalue");
389 PRINT("%s%c%c%i%s", colour
[col
], p
, r
, idx
, postFix
);
394 int ImmediateValue::print(char *buf
, size_t size
, DataType ty
) const
398 PRINT("%s", colour
[TXT_IMMD
]);
401 case TYPE_F32
: PRINT("%f", reg
.data
.f32
); break;
402 case TYPE_F64
: PRINT("%f", reg
.data
.f64
); break;
403 case TYPE_U8
: PRINT("0x%02x", reg
.data
.u8
); break;
404 case TYPE_S8
: PRINT("%i", reg
.data
.s8
); break;
405 case TYPE_U16
: PRINT("0x%04x", reg
.data
.u16
); break;
406 case TYPE_S16
: PRINT("%i", reg
.data
.s16
); break;
407 case TYPE_U32
: PRINT("0x%08x", reg
.data
.u32
); break;
408 case TYPE_S32
: PRINT("%i", reg
.data
.s32
); break;
412 PRINT("0x%016"PRIx64
, reg
.data
.u64
);
418 int Symbol::print(char *buf
, size_t size
, DataType ty
) const
420 return print(buf
, size
, NULL
, NULL
, ty
);
423 int Symbol::print(char *buf
, size_t size
,
424 Value
*rel
, Value
*dimRel
, DataType ty
) const
430 ty
= typeOfSize(reg
.size
);
432 if (reg
.file
== FILE_SYSTEM_VALUE
) {
433 PRINT("%ssv[%s%s:%i%s", colour
[TXT_MEM
],
434 colour
[TXT_REGISTER
],
435 SemanticStr
[reg
.data
.sv
.sv
], reg
.data
.sv
.index
, colour
[TXT_MEM
]);
437 PRINT("%s+", colour
[TXT_DEFAULT
]);
438 pos
+= rel
->print(&buf
[pos
], size
- pos
);
440 PRINT("%s]", colour
[TXT_MEM
]);
445 case FILE_MEMORY_CONST
: c
= 'c'; break;
446 case FILE_SHADER_INPUT
: c
= 'a'; break;
447 case FILE_SHADER_OUTPUT
: c
= 'o'; break;
448 case FILE_MEMORY_GLOBAL
: c
= 'g'; break;
449 case FILE_MEMORY_SHARED
: c
= 's'; break;
450 case FILE_MEMORY_LOCAL
: c
= 'l'; break;
452 assert(!"invalid file");
458 PRINT("%s%c%i[", colour
[TXT_MEM
], c
, reg
.fileIndex
);
460 PRINT("%s%c[", colour
[TXT_MEM
], c
);
463 pos
+= dimRel
->print(&buf
[pos
], size
- pos
, TYPE_S32
);
464 PRINT("%s][", colour
[TXT_MEM
]);
468 pos
+= rel
->print(&buf
[pos
], size
- pos
);
469 PRINT("%s%c", colour
[TXT_DEFAULT
], (reg
.data
.offset
< 0) ? '-' : '+');
471 assert(reg
.data
.offset
>= 0);
473 PRINT("%s0x%x%s]", colour
[TXT_IMMD
], abs(reg
.data
.offset
), colour
[TXT_MEM
]);
478 void Instruction::print() const
482 const size_t size
= BUFSZ
;
488 PRINT("%s", colour
[TXT_INSN
]);
494 const size_t pre
= pos
;
495 if (getSrc(predSrc
)->reg
.file
== FILE_PREDICATE
) {
499 PRINT("%s", CondCodeStr
[cc
]);
503 pos
+= getSrc(predSrc
)->print(&buf
[pos
], BUFSZ
- pos
);
504 PRINT(" %s", colour
[TXT_INSN
]);
511 PRINT("%s", operationStr
[op
]);
512 if (asFlow()->indirect
)
514 if (asFlow()->absolute
)
516 if (op
== OP_CALL
&& asFlow()->builtin
) {
517 PRINT(" %sBUILTIN:%i", colour
[TXT_BRA
], asFlow()->target
.builtin
);
519 if (op
== OP_CALL
&& asFlow()->target
.fn
) {
520 PRINT(" %s%s:%i", colour
[TXT_BRA
],
521 asFlow()->target
.fn
->getName(),
522 asFlow()->target
.fn
->getLabel());
524 if (asFlow()->target
.bb
)
525 PRINT(" %sBB:%i", colour
[TXT_BRA
], asFlow()->target
.bb
->getId());
527 PRINT("%s ", operationStr
[op
]);
528 if (op
== OP_LINTERP
|| op
== OP_PINTERP
)
529 PRINT("%s ", interpStr
[ipa
]);
533 if (subOp
< Elements(atomSubOpStr
))
534 PRINT("%s ", atomSubOpStr
[subOp
]);
538 PRINT("(SUBOP:%u) ", subOp
);
544 PRINT("%s %s$r%u $s%u %s", asTex()->tex
.target
.getName(),
545 colour
[TXT_MEM
], asTex()->tex
.r
, asTex()->tex
.s
,
548 PRINT("x2^%i ", postFactor
);
549 PRINT("%s%s", dnz
? "dnz " : (ftz
? "ftz " : ""), DataTypeStr
[dType
]);
553 PRINT(" %s", RoundModeStr
[rnd
]);
557 for (d
= 0; defExists(d
); ++d
) {
559 pos
+= getDef(d
)->print(&buf
[pos
], size
- pos
);
562 PRINT(" %s}", colour
[TXT_INSN
]);
565 PRINT(" %s#", colour
[TXT_INSN
]);
568 PRINT(" %s%s", colour
[TXT_INSN
], CondCodeStr
[asCmp()->setCond
]);
571 PRINT(" %s%s", colour
[TXT_INSN
], DataTypeStr
[sType
]);
573 for (s
= 0; srcExists(s
); ++s
) {
574 if (s
== predSrc
|| src(s
).usedAsPtr
)
576 const size_t pre
= pos
;
578 pos
+= src(s
).mod
.print(&buf
[pos
], BUFSZ
- pos
);
581 if (src(s
).isIndirect(0) || src(s
).isIndirect(1))
582 pos
+= getSrc(s
)->asSym()->print(&buf
[pos
], BUFSZ
- pos
,
586 pos
+= getSrc(s
)->print(&buf
[pos
], BUFSZ
- pos
, sType
);
589 PRINT("%s exit", colour
[TXT_INSN
]);
591 PRINT("%s", colour
[TXT_DEFAULT
]);
593 buf
[MIN2(pos
, BUFSZ
- 1)] = 0;
595 INFO("%s (%u)\n", buf
, encSize
);
598 class PrintPass
: public Pass
601 PrintPass() : serial(0) { }
603 virtual bool visit(Function
*);
604 virtual bool visit(BasicBlock
*);
605 virtual bool visit(Instruction
*);
612 PrintPass::visit(Function
*fn
)
616 INFO("\n%s:%i (", fn
->getName(), fn
->getLabel());
618 if (!fn
->outs
.empty())
620 for (std::deque
<ValueRef
>::iterator it
= fn
->outs
.begin();
621 it
!= fn
->outs
.end();
623 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
627 if (!fn
->ins
.empty())
628 INFO("%s%sin", colour
[TXT_DEFAULT
], fn
->outs
.empty() ? "" : ", ");
629 for (std::deque
<ValueDef
>::iterator it
= fn
->ins
.begin();
632 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
635 INFO("%s)\n", colour
[TXT_DEFAULT
]);
641 PrintPass::visit(BasicBlock
*bb
)
645 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next())
646 INFO(" <- BB:%i (%s)\n",
647 BasicBlock::get(ei
.getNode())->getId(),
648 ei
.getEdge()->typeStr());
650 INFO("BB:%i (%u instructions) - ", bb
->getId(), bb
->getInsnCount());
653 INFO("idom = BB:%i, ", bb
->idom()->getId());
656 for (DLList::Iterator df
= bb
->getDF().iterator(); !df
.end(); df
.next())
657 INFO("BB:%i ", BasicBlock::get(df
)->getId());
661 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next())
662 INFO(" -> BB:%i (%s)\n",
663 BasicBlock::get(ei
.getNode())->getId(),
664 ei
.getEdge()->typeStr());
670 PrintPass::visit(Instruction
*insn
)
672 INFO("%3i: ", serial
++);
681 pass
.run(this, true, false);
689 pass
.run(this, true, false);
693 Function::printLiveIntervals() const
695 INFO("printing live intervals ...\n");
697 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next()) {
698 const Value
*lval
= Value::get(it
)->asLValue();
699 if (lval
&& !lval
->livei
.isEmpty()) {
700 INFO("livei(%%%i): ", lval
->id
);
706 } // namespace nv50_ir