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] =
196 static const char *atomSubOpStr
[] =
198 "add", "min", "max", "inc", "dec", "and", "or", "xor", "cas", "exch"
201 static const char *ldstSubOpStr
[] =
206 static const char *DataTypeStr
[] =
217 static const char *RoundModeStr
[] =
219 "", "rm", "rz", "rp", "rni", "rmi", "rzi", "rpi"
222 static const char *CondCodeStr
[] =
250 static const char *SemanticStr
[SV_LAST
+ 1] =
289 static const char *interpStr
[16] =
309 #define PRINT(args...) \
311 pos += snprintf(&buf[pos], size - pos, args); \
314 #define SPACE_PRINT(cond, args...) \
318 pos += snprintf(&buf[pos], size - pos, args); \
327 int Modifier::print(char *buf
, size_t size
) const
332 PRINT("%s", colour
[TXT_INSN
]);
336 if (bits
& NV50_IR_MOD_NOT
)
338 if (bits
& NV50_IR_MOD_SAT
)
339 SPACE_PRINT(pos
> base
&& pos
< size
, "sat");
340 if (bits
& NV50_IR_MOD_NEG
)
341 SPACE_PRINT(pos
> base
&& pos
< size
, "neg");
342 if (bits
& NV50_IR_MOD_ABS
)
343 SPACE_PRINT(pos
> base
&& pos
< size
, "abs");
348 int LValue::print(char *buf
, size_t size
, DataType ty
) const
350 const char *postFix
= "";
352 int idx
= join
->reg
.data
.id
>= 0 ? join
->reg
.data
.id
: id
;
353 char p
= join
->reg
.data
.id
>= 0 ? '$' : '%';
355 int col
= TXT_DEFAULT
;
359 r
= 'r'; col
= TXT_GPR
;
362 postFix
= (idx
& 1) ? "h" : "l";
371 if (reg
.size
== 16) {
374 if (reg
.size
== 12) {
379 r
= 'p'; col
= TXT_REGISTER
;
387 r
= 'c'; col
= TXT_FLAGS
;
390 r
= 'a'; col
= TXT_REGISTER
;
393 assert(!"invalid file for lvalue");
398 PRINT("%s%c%c%i%s", colour
[col
], p
, r
, idx
, postFix
);
403 int ImmediateValue::print(char *buf
, size_t size
, DataType ty
) const
407 PRINT("%s", colour
[TXT_IMMD
]);
410 case TYPE_F32
: PRINT("%f", reg
.data
.f32
); break;
411 case TYPE_F64
: PRINT("%f", reg
.data
.f64
); break;
412 case TYPE_U8
: PRINT("0x%02x", reg
.data
.u8
); break;
413 case TYPE_S8
: PRINT("%i", reg
.data
.s8
); break;
414 case TYPE_U16
: PRINT("0x%04x", reg
.data
.u16
); break;
415 case TYPE_S16
: PRINT("%i", reg
.data
.s16
); break;
416 case TYPE_U32
: PRINT("0x%08x", reg
.data
.u32
); break;
417 case TYPE_S32
: PRINT("%i", reg
.data
.s32
); break;
421 PRINT("0x%016" PRIx64
, reg
.data
.u64
);
427 int Symbol::print(char *buf
, size_t size
, DataType ty
) const
429 return print(buf
, size
, NULL
, NULL
, ty
);
432 int Symbol::print(char *buf
, size_t size
,
433 Value
*rel
, Value
*dimRel
, DataType ty
) const
439 ty
= typeOfSize(reg
.size
);
441 if (reg
.file
== FILE_SYSTEM_VALUE
) {
442 PRINT("%ssv[%s%s:%i%s", colour
[TXT_MEM
],
443 colour
[TXT_REGISTER
],
444 SemanticStr
[reg
.data
.sv
.sv
], reg
.data
.sv
.index
, colour
[TXT_MEM
]);
446 PRINT("%s+", colour
[TXT_DEFAULT
]);
447 pos
+= rel
->print(&buf
[pos
], size
- pos
);
449 PRINT("%s]", colour
[TXT_MEM
]);
454 case FILE_MEMORY_CONST
: c
= 'c'; break;
455 case FILE_SHADER_INPUT
: c
= 'a'; break;
456 case FILE_SHADER_OUTPUT
: c
= 'o'; break;
457 case FILE_MEMORY_GLOBAL
: c
= 'g'; break;
458 case FILE_MEMORY_SHARED
: c
= 's'; break;
459 case FILE_MEMORY_LOCAL
: c
= 'l'; break;
461 assert(!"invalid file");
467 PRINT("%s%c%i[", colour
[TXT_MEM
], c
, reg
.fileIndex
);
469 PRINT("%s%c[", colour
[TXT_MEM
], c
);
472 pos
+= dimRel
->print(&buf
[pos
], size
- pos
, TYPE_S32
);
473 PRINT("%s][", colour
[TXT_MEM
]);
477 pos
+= rel
->print(&buf
[pos
], size
- pos
);
478 PRINT("%s%c", colour
[TXT_DEFAULT
], (reg
.data
.offset
< 0) ? '-' : '+');
480 assert(reg
.data
.offset
>= 0);
482 PRINT("%s0x%x%s]", colour
[TXT_IMMD
], abs(reg
.data
.offset
), colour
[TXT_MEM
]);
487 void Instruction::print() const
491 const size_t size
= BUFSZ
;
497 PRINT("%s", colour
[TXT_INSN
]);
503 const size_t pre
= pos
;
504 if (getSrc(predSrc
)->reg
.file
== FILE_PREDICATE
) {
508 PRINT("%s", CondCodeStr
[cc
]);
512 pos
+= getSrc(predSrc
)->print(&buf
[pos
], BUFSZ
- pos
);
513 PRINT(" %s", colour
[TXT_INSN
]);
520 PRINT("%s", operationStr
[op
]);
521 if (asFlow()->indirect
)
523 if (asFlow()->absolute
)
525 if (op
== OP_CALL
&& asFlow()->builtin
) {
526 PRINT(" %sBUILTIN:%i", colour
[TXT_BRA
], asFlow()->target
.builtin
);
528 if (op
== OP_CALL
&& asFlow()->target
.fn
) {
529 PRINT(" %s%s:%i", colour
[TXT_BRA
],
530 asFlow()->target
.fn
->getName(),
531 asFlow()->target
.fn
->getLabel());
533 if (asFlow()->target
.bb
)
534 PRINT(" %sBB:%i", colour
[TXT_BRA
], asFlow()->target
.bb
->getId());
536 PRINT("%s ", operationStr
[op
]);
537 if (op
== OP_LINTERP
|| op
== OP_PINTERP
)
538 PRINT("%s ", interpStr
[ipa
]);
542 if (subOp
< Elements(atomSubOpStr
))
543 PRINT("%s ", atomSubOpStr
[subOp
]);
547 if (subOp
< Elements(ldstSubOpStr
))
548 PRINT("%s ", ldstSubOpStr
[subOp
]);
552 PRINT("(SUBOP:%u) ", subOp
);
558 PRINT("%s %s$r%u $s%u %s", asTex()->tex
.target
.getName(),
559 colour
[TXT_MEM
], asTex()->tex
.r
, asTex()->tex
.s
,
562 PRINT("x2^%i ", postFactor
);
563 PRINT("%s%s", dnz
? "dnz " : (ftz
? "ftz " : ""), DataTypeStr
[dType
]);
567 PRINT(" %s", RoundModeStr
[rnd
]);
571 for (d
= 0; defExists(d
); ++d
) {
573 pos
+= getDef(d
)->print(&buf
[pos
], size
- pos
);
576 PRINT(" %s}", colour
[TXT_INSN
]);
579 PRINT(" %s#", colour
[TXT_INSN
]);
582 PRINT(" %s%s", colour
[TXT_INSN
], CondCodeStr
[asCmp()->setCond
]);
585 PRINT(" %s%s", colour
[TXT_INSN
], DataTypeStr
[sType
]);
587 for (s
= 0; srcExists(s
); ++s
) {
588 if (s
== predSrc
|| src(s
).usedAsPtr
)
590 const size_t pre
= pos
;
592 pos
+= src(s
).mod
.print(&buf
[pos
], BUFSZ
- pos
);
595 if (src(s
).isIndirect(0) || src(s
).isIndirect(1))
596 pos
+= getSrc(s
)->asSym()->print(&buf
[pos
], BUFSZ
- pos
,
600 pos
+= getSrc(s
)->print(&buf
[pos
], BUFSZ
- pos
, sType
);
603 PRINT("%s exit", colour
[TXT_INSN
]);
605 PRINT("%s", colour
[TXT_DEFAULT
]);
607 buf
[MIN2(pos
, BUFSZ
- 1)] = 0;
609 INFO("%s (%u)\n", buf
, encSize
);
612 class PrintPass
: public Pass
615 PrintPass() : serial(0) { }
617 virtual bool visit(Function
*);
618 virtual bool visit(BasicBlock
*);
619 virtual bool visit(Instruction
*);
626 PrintPass::visit(Function
*fn
)
630 INFO("\n%s:%i (", fn
->getName(), fn
->getLabel());
632 if (!fn
->outs
.empty())
634 for (std::deque
<ValueRef
>::iterator it
= fn
->outs
.begin();
635 it
!= fn
->outs
.end();
637 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
641 if (!fn
->ins
.empty())
642 INFO("%s%sin", colour
[TXT_DEFAULT
], fn
->outs
.empty() ? "" : ", ");
643 for (std::deque
<ValueDef
>::iterator it
= fn
->ins
.begin();
646 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
649 INFO("%s)\n", colour
[TXT_DEFAULT
]);
655 PrintPass::visit(BasicBlock
*bb
)
659 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next())
660 INFO(" <- BB:%i (%s)\n",
661 BasicBlock::get(ei
.getNode())->getId(),
662 ei
.getEdge()->typeStr());
664 INFO("BB:%i (%u instructions) - ", bb
->getId(), bb
->getInsnCount());
667 INFO("idom = BB:%i, ", bb
->idom()->getId());
670 for (DLList::Iterator df
= bb
->getDF().iterator(); !df
.end(); df
.next())
671 INFO("BB:%i ", BasicBlock::get(df
)->getId());
675 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next())
676 INFO(" -> BB:%i (%s)\n",
677 BasicBlock::get(ei
.getNode())->getId(),
678 ei
.getEdge()->typeStr());
684 PrintPass::visit(Instruction
*insn
)
686 INFO("%3i: ", serial
++);
695 pass
.run(this, true, false);
703 pass
.run(this, true, false);
707 Function::printLiveIntervals() const
709 INFO("printing live intervals ...\n");
711 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next()) {
712 const Value
*lval
= Value::get(it
)->asLValue();
713 if (lval
&& !lval
->livei
.isEmpty()) {
714 INFO("livei(%%%i): ", lval
->id
);
720 } // namespace nv50_ir