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 *DataTypeStr
[] =
234 static const char *RoundModeStr
[] =
236 "", "rm", "rz", "rp", "rni", "rmi", "rzi", "rpi"
239 static const char *CondCodeStr
[] =
267 static const char *SemanticStr
[SV_LAST
+ 1] =
309 static const char *interpStr
[16] =
329 #define PRINT(args...) \
331 pos += snprintf(&buf[pos], size - pos, args); \
334 #define SPACE_PRINT(cond, args...) \
338 pos += snprintf(&buf[pos], size - pos, args); \
347 int Modifier::print(char *buf
, size_t size
) const
352 PRINT("%s", colour
[TXT_INSN
]);
356 if (bits
& NV50_IR_MOD_NOT
)
358 if (bits
& NV50_IR_MOD_SAT
)
359 SPACE_PRINT(pos
> base
&& pos
< size
, "sat");
360 if (bits
& NV50_IR_MOD_NEG
)
361 SPACE_PRINT(pos
> base
&& pos
< size
, "neg");
362 if (bits
& NV50_IR_MOD_ABS
)
363 SPACE_PRINT(pos
> base
&& pos
< size
, "abs");
368 int LValue::print(char *buf
, size_t size
, DataType ty
) const
370 const char *postFix
= "";
372 int idx
= join
->reg
.data
.id
>= 0 ? join
->reg
.data
.id
: id
;
373 char p
= join
->reg
.data
.id
>= 0 ? '$' : '%';
375 int col
= TXT_DEFAULT
;
379 r
= 'r'; col
= TXT_GPR
;
382 postFix
= (idx
& 1) ? "h" : "l";
391 if (reg
.size
== 16) {
394 if (reg
.size
== 12) {
399 r
= 'p'; col
= TXT_REGISTER
;
407 r
= 'c'; col
= TXT_FLAGS
;
410 r
= 'a'; col
= TXT_REGISTER
;
413 assert(!"invalid file for lvalue");
418 PRINT("%s%c%c%i%s", colour
[col
], p
, r
, idx
, postFix
);
423 int ImmediateValue::print(char *buf
, size_t size
, DataType ty
) const
427 PRINT("%s", colour
[TXT_IMMD
]);
430 case TYPE_F32
: PRINT("%f", reg
.data
.f32
); break;
431 case TYPE_F64
: PRINT("%f", reg
.data
.f64
); break;
432 case TYPE_U8
: PRINT("0x%02x", reg
.data
.u8
); break;
433 case TYPE_S8
: PRINT("%i", reg
.data
.s8
); break;
434 case TYPE_U16
: PRINT("0x%04x", reg
.data
.u16
); break;
435 case TYPE_S16
: PRINT("%i", reg
.data
.s16
); break;
436 case TYPE_U32
: PRINT("0x%08x", reg
.data
.u32
); break;
437 case TYPE_S32
: PRINT("%i", reg
.data
.s32
); break;
441 PRINT("0x%016" PRIx64
, reg
.data
.u64
);
447 int Symbol::print(char *buf
, size_t size
, DataType ty
) const
449 return print(buf
, size
, NULL
, NULL
, ty
);
452 int Symbol::print(char *buf
, size_t size
,
453 Value
*rel
, Value
*dimRel
, DataType ty
) const
459 ty
= typeOfSize(reg
.size
);
461 if (reg
.file
== FILE_SYSTEM_VALUE
) {
462 PRINT("%ssv[%s%s:%i%s", colour
[TXT_MEM
],
463 colour
[TXT_REGISTER
],
464 SemanticStr
[reg
.data
.sv
.sv
], reg
.data
.sv
.index
, colour
[TXT_MEM
]);
466 PRINT("%s+", colour
[TXT_DEFAULT
]);
467 pos
+= rel
->print(&buf
[pos
], size
- pos
);
469 PRINT("%s]", colour
[TXT_MEM
]);
474 case FILE_MEMORY_CONST
: c
= 'c'; break;
475 case FILE_SHADER_INPUT
: c
= 'a'; break;
476 case FILE_SHADER_OUTPUT
: c
= 'o'; break;
477 case FILE_MEMORY_BUFFER
: c
= 'b'; break; // Only used before lowering
478 case FILE_MEMORY_GLOBAL
: c
= 'g'; break;
479 case FILE_MEMORY_SHARED
: c
= 's'; break;
480 case FILE_MEMORY_LOCAL
: c
= 'l'; break;
482 assert(!"invalid file");
488 PRINT("%s%c%i[", colour
[TXT_MEM
], c
, reg
.fileIndex
);
490 PRINT("%s%c[", colour
[TXT_MEM
], c
);
493 pos
+= dimRel
->print(&buf
[pos
], size
- pos
, TYPE_S32
);
494 PRINT("%s][", colour
[TXT_MEM
]);
498 pos
+= rel
->print(&buf
[pos
], size
- pos
);
499 PRINT("%s%c", colour
[TXT_DEFAULT
], (reg
.data
.offset
< 0) ? '-' : '+');
501 assert(reg
.data
.offset
>= 0);
503 PRINT("%s0x%x%s]", colour
[TXT_IMMD
], abs(reg
.data
.offset
), colour
[TXT_MEM
]);
508 void Instruction::print() const
512 const size_t size
= BUFSZ
;
518 PRINT("%s", colour
[TXT_INSN
]);
524 const size_t pre
= pos
;
525 if (getSrc(predSrc
)->reg
.file
== FILE_PREDICATE
) {
529 PRINT("%s", CondCodeStr
[cc
]);
533 pos
+= getSrc(predSrc
)->print(&buf
[pos
], BUFSZ
- pos
);
534 PRINT(" %s", colour
[TXT_INSN
]);
541 PRINT("%s", operationStr
[op
]);
542 if (asFlow()->indirect
)
544 if (asFlow()->absolute
)
546 if (op
== OP_CALL
&& asFlow()->builtin
) {
547 PRINT(" %sBUILTIN:%i", colour
[TXT_BRA
], asFlow()->target
.builtin
);
549 if (op
== OP_CALL
&& asFlow()->target
.fn
) {
550 PRINT(" %s%s:%i", colour
[TXT_BRA
],
551 asFlow()->target
.fn
->getName(),
552 asFlow()->target
.fn
->getLabel());
554 if (asFlow()->target
.bb
)
555 PRINT(" %sBB:%i", colour
[TXT_BRA
], asFlow()->target
.bb
->getId());
557 PRINT("%s ", operationStr
[op
]);
558 if (op
== OP_LINTERP
|| op
== OP_PINTERP
)
559 PRINT("%s ", interpStr
[ipa
]);
563 if (subOp
< ARRAY_SIZE(atomSubOpStr
))
564 PRINT("%s ", atomSubOpStr
[subOp
]);
568 if (subOp
< ARRAY_SIZE(ldstSubOpStr
))
569 PRINT("%s ", ldstSubOpStr
[subOp
]);
572 if (subOp
< ARRAY_SIZE(subfmOpStr
))
573 PRINT("%s ", subfmOpStr
[subOp
]);
576 if (subOp
< ARRAY_SIZE(shflOpStr
))
577 PRINT("%s ", shflOpStr
[subOp
]);
580 if (subOp
< ARRAY_SIZE(pixldOpStr
))
581 PRINT("%s ", pixldOpStr
[subOp
]);
585 PRINT("(SUBOP:%u) ", subOp
);
591 PRINT("%s %s$r%u $s%u %s", asTex()->tex
.target
.getName(),
592 colour
[TXT_MEM
], asTex()->tex
.r
, asTex()->tex
.s
,
595 PRINT("x2^%i ", postFactor
);
596 PRINT("%s%s", dnz
? "dnz " : (ftz
? "ftz " : ""), DataTypeStr
[dType
]);
600 PRINT(" %s", RoundModeStr
[rnd
]);
604 for (d
= 0; defExists(d
); ++d
) {
606 pos
+= getDef(d
)->print(&buf
[pos
], size
- pos
);
609 PRINT(" %s}", colour
[TXT_INSN
]);
612 PRINT(" %s#", colour
[TXT_INSN
]);
615 PRINT(" %s%s", colour
[TXT_INSN
], CondCodeStr
[asCmp()->setCond
]);
618 PRINT(" %s%s", colour
[TXT_INSN
], DataTypeStr
[sType
]);
620 for (s
= 0; srcExists(s
); ++s
) {
621 if (s
== predSrc
|| src(s
).usedAsPtr
)
623 const size_t pre
= pos
;
625 pos
+= src(s
).mod
.print(&buf
[pos
], BUFSZ
- pos
);
628 if (src(s
).isIndirect(0) || src(s
).isIndirect(1))
629 pos
+= getSrc(s
)->asSym()->print(&buf
[pos
], BUFSZ
- pos
,
633 pos
+= getSrc(s
)->print(&buf
[pos
], BUFSZ
- pos
, sType
);
636 PRINT("%s exit", colour
[TXT_INSN
]);
638 PRINT("%s", colour
[TXT_DEFAULT
]);
640 buf
[MIN2(pos
, BUFSZ
- 1)] = 0;
642 INFO("%s (%u)\n", buf
, encSize
);
645 class PrintPass
: public Pass
648 PrintPass() : serial(0) { }
650 virtual bool visit(Function
*);
651 virtual bool visit(BasicBlock
*);
652 virtual bool visit(Instruction
*);
659 PrintPass::visit(Function
*fn
)
663 INFO("\n%s:%i (", fn
->getName(), fn
->getLabel());
665 if (!fn
->outs
.empty())
667 for (std::deque
<ValueRef
>::iterator it
= fn
->outs
.begin();
668 it
!= fn
->outs
.end();
670 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
674 if (!fn
->ins
.empty())
675 INFO("%s%sin", colour
[TXT_DEFAULT
], fn
->outs
.empty() ? "" : ", ");
676 for (std::deque
<ValueDef
>::iterator it
= fn
->ins
.begin();
679 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
682 INFO("%s)\n", colour
[TXT_DEFAULT
]);
688 PrintPass::visit(BasicBlock
*bb
)
692 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next())
693 INFO(" <- BB:%i (%s)\n",
694 BasicBlock::get(ei
.getNode())->getId(),
695 ei
.getEdge()->typeStr());
697 INFO("BB:%i (%u instructions) - ", bb
->getId(), bb
->getInsnCount());
700 INFO("idom = BB:%i, ", bb
->idom()->getId());
703 for (DLList::Iterator df
= bb
->getDF().iterator(); !df
.end(); df
.next())
704 INFO("BB:%i ", BasicBlock::get(df
)->getId());
708 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next())
709 INFO(" -> BB:%i (%s)\n",
710 BasicBlock::get(ei
.getNode())->getId(),
711 ei
.getEdge()->typeStr());
717 PrintPass::visit(Instruction
*insn
)
719 INFO("%3i: ", serial
++);
728 pass
.run(this, true, false);
736 pass
.run(this, true, false);
740 Function::printLiveIntervals() const
742 INFO("printing live intervals ...\n");
744 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next()) {
745 const Value
*lval
= Value::get(it
)->asLValue();
746 if (lval
&& !lval
->livei
.isEmpty()) {
747 INFO("livei(%%%i): ", lval
->id
);
753 } // namespace nv50_ir