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] =
198 static const char *atomSubOpStr
[] =
200 "add", "min", "max", "inc", "dec", "and", "or", "xor", "cas", "exch"
203 static const char *ldstSubOpStr
[] =
208 static const char *subfmOpStr
[] =
213 static const char *shflOpStr
[] =
215 "idx", "up", "down", "bfly"
218 static const char *pixldOpStr
[] =
220 "count", "covmask", "offset", "cent_offset", "sampleid"
223 static const char *rcprsqOpStr
[] =
228 static const char *emitOpStr
[] =
233 static const char *DataTypeStr
[] =
244 static const char *RoundModeStr
[] =
246 "", "rm", "rz", "rp", "rni", "rmi", "rzi", "rpi"
249 static const char *CondCodeStr
[] =
277 static const char *SemanticStr
[SV_LAST
+ 1] =
320 static const char *interpStr
[16] =
340 #define PRINT(args...) \
342 pos += snprintf(&buf[pos], size - pos, args); \
345 #define SPACE_PRINT(cond, args...) \
349 pos += snprintf(&buf[pos], size - pos, args); \
358 int Modifier::print(char *buf
, size_t size
) const
363 PRINT("%s", colour
[TXT_INSN
]);
367 if (bits
& NV50_IR_MOD_NOT
)
369 if (bits
& NV50_IR_MOD_SAT
)
370 SPACE_PRINT(pos
> base
&& pos
< size
, "sat");
371 if (bits
& NV50_IR_MOD_NEG
)
372 SPACE_PRINT(pos
> base
&& pos
< size
, "neg");
373 if (bits
& NV50_IR_MOD_ABS
)
374 SPACE_PRINT(pos
> base
&& pos
< size
, "abs");
379 int LValue::print(char *buf
, size_t size
, DataType ty
) const
381 const char *postFix
= "";
383 int idx
= join
->reg
.data
.id
>= 0 ? join
->reg
.data
.id
: id
;
384 char p
= join
->reg
.data
.id
>= 0 ? '$' : '%';
386 int col
= TXT_DEFAULT
;
390 r
= 'r'; col
= TXT_GPR
;
393 postFix
= (idx
& 1) ? "h" : "l";
402 if (reg
.size
== 16) {
405 if (reg
.size
== 12) {
410 r
= 'p'; col
= TXT_REGISTER
;
418 r
= 'c'; col
= TXT_FLAGS
;
421 r
= 'a'; col
= TXT_REGISTER
;
424 assert(!"invalid file for lvalue");
429 PRINT("%s%c%c%i%s", colour
[col
], p
, r
, idx
, postFix
);
434 int ImmediateValue::print(char *buf
, size_t size
, DataType ty
) const
438 PRINT("%s", colour
[TXT_IMMD
]);
441 case TYPE_F32
: PRINT("%f", reg
.data
.f32
); break;
442 case TYPE_F64
: PRINT("%f", reg
.data
.f64
); break;
443 case TYPE_U8
: PRINT("0x%02x", reg
.data
.u8
); break;
444 case TYPE_S8
: PRINT("%i", reg
.data
.s8
); break;
445 case TYPE_U16
: PRINT("0x%04x", reg
.data
.u16
); break;
446 case TYPE_S16
: PRINT("%i", reg
.data
.s16
); break;
447 case TYPE_U32
: PRINT("0x%08x", reg
.data
.u32
); break;
448 case TYPE_S32
: PRINT("%i", reg
.data
.s32
); break;
452 PRINT("0x%016" PRIx64
, reg
.data
.u64
);
458 int Symbol::print(char *buf
, size_t size
, DataType ty
) const
460 return print(buf
, size
, NULL
, NULL
, ty
);
463 int Symbol::print(char *buf
, size_t size
,
464 Value
*rel
, Value
*dimRel
, DataType ty
) const
470 ty
= typeOfSize(reg
.size
);
472 if (reg
.file
== FILE_SYSTEM_VALUE
) {
473 PRINT("%ssv[%s%s:%i%s", colour
[TXT_MEM
],
474 colour
[TXT_REGISTER
],
475 SemanticStr
[reg
.data
.sv
.sv
], reg
.data
.sv
.index
, colour
[TXT_MEM
]);
477 PRINT("%s+", colour
[TXT_DEFAULT
]);
478 pos
+= rel
->print(&buf
[pos
], size
- pos
);
480 PRINT("%s]", colour
[TXT_MEM
]);
485 case FILE_MEMORY_CONST
: c
= 'c'; break;
486 case FILE_SHADER_INPUT
: c
= 'a'; break;
487 case FILE_SHADER_OUTPUT
: c
= 'o'; break;
488 case FILE_MEMORY_BUFFER
: c
= 'b'; break; // Only used before lowering
489 case FILE_MEMORY_GLOBAL
: c
= 'g'; break;
490 case FILE_MEMORY_SHARED
: c
= 's'; break;
491 case FILE_MEMORY_LOCAL
: c
= 'l'; break;
493 assert(!"invalid file");
499 PRINT("%s%c%i[", colour
[TXT_MEM
], c
, reg
.fileIndex
);
501 PRINT("%s%c[", colour
[TXT_MEM
], c
);
504 pos
+= dimRel
->print(&buf
[pos
], size
- pos
, TYPE_S32
);
505 PRINT("%s][", colour
[TXT_MEM
]);
509 pos
+= rel
->print(&buf
[pos
], size
- pos
);
510 PRINT("%s%c", colour
[TXT_DEFAULT
], (reg
.data
.offset
< 0) ? '-' : '+');
512 assert(reg
.data
.offset
>= 0);
514 PRINT("%s0x%x%s]", colour
[TXT_IMMD
], abs(reg
.data
.offset
), colour
[TXT_MEM
]);
519 void Instruction::print() const
523 const size_t size
= BUFSZ
;
529 PRINT("%s", colour
[TXT_INSN
]);
535 const size_t pre
= pos
;
536 if (getSrc(predSrc
)->reg
.file
== FILE_PREDICATE
) {
540 PRINT("%s", CondCodeStr
[cc
]);
544 pos
+= getSrc(predSrc
)->print(&buf
[pos
], BUFSZ
- pos
);
545 PRINT(" %s", colour
[TXT_INSN
]);
552 PRINT("%s", operationStr
[op
]);
553 if (asFlow()->indirect
)
555 if (asFlow()->absolute
)
557 if (op
== OP_CALL
&& asFlow()->builtin
) {
558 PRINT(" %sBUILTIN:%i", colour
[TXT_BRA
], asFlow()->target
.builtin
);
560 if (op
== OP_CALL
&& asFlow()->target
.fn
) {
561 PRINT(" %s%s:%i", colour
[TXT_BRA
],
562 asFlow()->target
.fn
->getName(),
563 asFlow()->target
.fn
->getLabel());
565 if (asFlow()->target
.bb
)
566 PRINT(" %sBB:%i", colour
[TXT_BRA
], asFlow()->target
.bb
->getId());
568 PRINT("%s ", operationStr
[op
]);
569 if (op
== OP_LINTERP
|| op
== OP_PINTERP
)
570 PRINT("%s ", interpStr
[ipa
]);
575 if (subOp
< ARRAY_SIZE(atomSubOpStr
))
576 PRINT("%s ", atomSubOpStr
[subOp
]);
580 if (subOp
< ARRAY_SIZE(ldstSubOpStr
))
581 PRINT("%s ", ldstSubOpStr
[subOp
]);
584 if (subOp
< ARRAY_SIZE(subfmOpStr
))
585 PRINT("%s ", subfmOpStr
[subOp
]);
588 if (subOp
< ARRAY_SIZE(shflOpStr
))
589 PRINT("%s ", shflOpStr
[subOp
]);
592 if (subOp
< ARRAY_SIZE(pixldOpStr
))
593 PRINT("%s ", pixldOpStr
[subOp
]);
597 if (subOp
< ARRAY_SIZE(rcprsqOpStr
))
598 PRINT("%s ", rcprsqOpStr
[subOp
]);
601 if (subOp
< ARRAY_SIZE(emitOpStr
))
602 PRINT("%s ", emitOpStr
[subOp
]);
606 PRINT("(SUBOP:%u) ", subOp
);
612 PRINT("%s %s$r%u $s%u %s", asTex()->tex
.target
.getName(),
613 colour
[TXT_MEM
], asTex()->tex
.r
, asTex()->tex
.s
,
616 PRINT("x2^%i ", postFactor
);
617 PRINT("%s%s", dnz
? "dnz " : (ftz
? "ftz " : ""), DataTypeStr
[dType
]);
621 PRINT(" %s", RoundModeStr
[rnd
]);
625 for (d
= 0; defExists(d
); ++d
) {
627 pos
+= getDef(d
)->print(&buf
[pos
], size
- pos
);
630 PRINT(" %s}", colour
[TXT_INSN
]);
633 PRINT(" %s#", colour
[TXT_INSN
]);
636 PRINT(" %s%s", colour
[TXT_INSN
], CondCodeStr
[asCmp()->setCond
]);
639 PRINT(" %s%s", colour
[TXT_INSN
], DataTypeStr
[sType
]);
641 for (s
= 0; srcExists(s
); ++s
) {
642 if (s
== predSrc
|| src(s
).usedAsPtr
)
644 const size_t pre
= pos
;
646 pos
+= src(s
).mod
.print(&buf
[pos
], BUFSZ
- pos
);
649 if (src(s
).isIndirect(0) || src(s
).isIndirect(1))
650 pos
+= getSrc(s
)->asSym()->print(&buf
[pos
], BUFSZ
- pos
,
654 pos
+= getSrc(s
)->print(&buf
[pos
], BUFSZ
- pos
, sType
);
657 PRINT("%s exit", colour
[TXT_INSN
]);
659 PRINT("%s", colour
[TXT_DEFAULT
]);
661 buf
[MIN2(pos
, BUFSZ
- 1)] = 0;
663 INFO("%s (%u)\n", buf
, encSize
);
666 class PrintPass
: public Pass
669 PrintPass() : serial(0) { }
671 virtual bool visit(Function
*);
672 virtual bool visit(BasicBlock
*);
673 virtual bool visit(Instruction
*);
680 PrintPass::visit(Function
*fn
)
684 INFO("\n%s:%i (", fn
->getName(), fn
->getLabel());
686 if (!fn
->outs
.empty())
688 for (std::deque
<ValueRef
>::iterator it
= fn
->outs
.begin();
689 it
!= fn
->outs
.end();
691 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
695 if (!fn
->ins
.empty())
696 INFO("%s%sin", colour
[TXT_DEFAULT
], fn
->outs
.empty() ? "" : ", ");
697 for (std::deque
<ValueDef
>::iterator it
= fn
->ins
.begin();
700 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
703 INFO("%s)\n", colour
[TXT_DEFAULT
]);
709 PrintPass::visit(BasicBlock
*bb
)
713 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next())
714 INFO(" <- BB:%i (%s)\n",
715 BasicBlock::get(ei
.getNode())->getId(),
716 ei
.getEdge()->typeStr());
718 INFO("BB:%i (%u instructions) - ", bb
->getId(), bb
->getInsnCount());
721 INFO("idom = BB:%i, ", bb
->idom()->getId());
724 for (DLList::Iterator df
= bb
->getDF().iterator(); !df
.end(); df
.next())
725 INFO("BB:%i ", BasicBlock::get(df
)->getId());
729 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next())
730 INFO(" -> BB:%i (%s)\n",
731 BasicBlock::get(ei
.getNode())->getId(),
732 ei
.getEdge()->typeStr());
738 PrintPass::visit(Instruction
*insn
)
740 INFO("%3i: ", serial
++);
749 pass
.run(this, true, false);
757 pass
.run(this, true, false);
761 Function::printLiveIntervals() const
763 INFO("printing live intervals ...\n");
765 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next()) {
766 const Value
*lval
= Value::get(it
)->asLValue();
767 if (lval
&& !lval
->livei
.isEmpty()) {
768 INFO("livei(%%%i): ", lval
->id
);
774 } // namespace nv50_ir