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] =
192 static const char *atomSubOpStr
[] =
194 "add", "min", "max", "inc", "dec", "and", "or", "xor", "cas", "exch"
197 static const char *DataTypeStr
[] =
208 static const char *RoundModeStr
[] =
210 "", "rm", "rz", "rp", "rni", "rmi", "rzi", "rpi"
213 static const char *CondCodeStr
[] =
241 static const char *SemanticStr
[SV_LAST
+ 1] =
276 static const char *interpStr
[16] =
296 #define PRINT(args...) \
298 pos += snprintf(&buf[pos], size - pos, args); \
301 #define SPACE_PRINT(cond, args...) \
305 pos += snprintf(&buf[pos], size - pos, args); \
314 int Modifier::print(char *buf
, size_t size
) const
319 PRINT("%s", colour
[TXT_INSN
]);
323 if (bits
& NV50_IR_MOD_NOT
)
325 if (bits
& NV50_IR_MOD_SAT
)
326 SPACE_PRINT(pos
> base
&& pos
< size
, "sat");
327 if (bits
& NV50_IR_MOD_NEG
)
328 SPACE_PRINT(pos
> base
&& pos
< size
, "neg");
329 if (bits
& NV50_IR_MOD_ABS
)
330 SPACE_PRINT(pos
> base
&& pos
< size
, "abs");
335 int LValue::print(char *buf
, size_t size
, DataType ty
) const
337 const char *postFix
= "";
339 int idx
= join
->reg
.data
.id
>= 0 ? join
->reg
.data
.id
: id
;
340 char p
= join
->reg
.data
.id
>= 0 ? '$' : '%';
342 int col
= TXT_DEFAULT
;
346 r
= 'r'; col
= TXT_GPR
;
349 postFix
= (idx
& 1) ? "h" : "l";
358 if (reg
.size
== 16) {
361 if (reg
.size
== 12) {
366 r
= 'p'; col
= TXT_REGISTER
;
374 r
= 'c'; col
= TXT_FLAGS
;
377 r
= 'a'; col
= TXT_REGISTER
;
380 assert(!"invalid file for lvalue");
385 PRINT("%s%c%c%i%s", colour
[col
], p
, r
, idx
, postFix
);
390 int ImmediateValue::print(char *buf
, size_t size
, DataType ty
) const
394 PRINT("%s", colour
[TXT_IMMD
]);
397 case TYPE_F32
: PRINT("%f", reg
.data
.f32
); break;
398 case TYPE_F64
: PRINT("%f", reg
.data
.f64
); break;
399 case TYPE_U8
: PRINT("0x%02x", reg
.data
.u8
); break;
400 case TYPE_S8
: PRINT("%i", reg
.data
.s8
); break;
401 case TYPE_U16
: PRINT("0x%04x", reg
.data
.u16
); break;
402 case TYPE_S16
: PRINT("%i", reg
.data
.s16
); break;
403 case TYPE_U32
: PRINT("0x%08x", reg
.data
.u32
); break;
404 case TYPE_S32
: PRINT("%i", reg
.data
.s32
); break;
408 PRINT("0x%016"PRIx64
, reg
.data
.u64
);
414 int Symbol::print(char *buf
, size_t size
, DataType ty
) const
416 return print(buf
, size
, NULL
, NULL
, ty
);
419 int Symbol::print(char *buf
, size_t size
,
420 Value
*rel
, Value
*dimRel
, DataType ty
) const
426 ty
= typeOfSize(reg
.size
);
428 if (reg
.file
== FILE_SYSTEM_VALUE
) {
429 PRINT("%ssv[%s%s:%i%s", colour
[TXT_MEM
],
430 colour
[TXT_REGISTER
],
431 SemanticStr
[reg
.data
.sv
.sv
], reg
.data
.sv
.index
, colour
[TXT_MEM
]);
433 PRINT("%s+", colour
[TXT_DEFAULT
]);
434 pos
+= rel
->print(&buf
[pos
], size
- pos
);
436 PRINT("%s]", colour
[TXT_MEM
]);
441 case FILE_MEMORY_CONST
: c
= 'c'; break;
442 case FILE_SHADER_INPUT
: c
= 'a'; break;
443 case FILE_SHADER_OUTPUT
: c
= 'o'; break;
444 case FILE_MEMORY_GLOBAL
: c
= 'g'; break;
445 case FILE_MEMORY_SHARED
: c
= 's'; break;
446 case FILE_MEMORY_LOCAL
: c
= 'l'; break;
448 assert(!"invalid file");
454 PRINT("%s%c%i[", colour
[TXT_MEM
], c
, reg
.fileIndex
);
456 PRINT("%s%c[", colour
[TXT_MEM
], c
);
459 pos
+= dimRel
->print(&buf
[pos
], size
- pos
, TYPE_S32
);
460 PRINT("%s][", colour
[TXT_MEM
]);
464 pos
+= rel
->print(&buf
[pos
], size
- pos
);
465 PRINT("%s%c", colour
[TXT_DEFAULT
], (reg
.data
.offset
< 0) ? '-' : '+');
467 assert(reg
.data
.offset
>= 0);
469 PRINT("%s0x%x%s]", colour
[TXT_IMMD
], abs(reg
.data
.offset
), colour
[TXT_MEM
]);
474 void Instruction::print() const
478 const size_t size
= BUFSZ
;
484 PRINT("%s", colour
[TXT_INSN
]);
490 const size_t pre
= pos
;
491 if (getSrc(predSrc
)->reg
.file
== FILE_PREDICATE
) {
495 PRINT("%s", CondCodeStr
[cc
]);
499 pos
+= getSrc(predSrc
)->print(&buf
[pos
], BUFSZ
- pos
);
500 PRINT(" %s", colour
[TXT_INSN
]);
507 PRINT("%s", operationStr
[op
]);
508 if (asFlow()->indirect
)
510 if (asFlow()->absolute
)
512 if (op
== OP_CALL
&& asFlow()->builtin
) {
513 PRINT(" %sBUILTIN:%i", colour
[TXT_BRA
], asFlow()->target
.builtin
);
515 if (op
== OP_CALL
&& asFlow()->target
.fn
) {
516 PRINT(" %s%s:%i", colour
[TXT_BRA
],
517 asFlow()->target
.fn
->getName(),
518 asFlow()->target
.fn
->getLabel());
520 if (asFlow()->target
.bb
)
521 PRINT(" %sBB:%i", colour
[TXT_BRA
], asFlow()->target
.bb
->getId());
523 PRINT("%s ", operationStr
[op
]);
524 if (op
== OP_LINTERP
|| op
== OP_PINTERP
)
525 PRINT("%s ", interpStr
[ipa
]);
529 if (subOp
< Elements(atomSubOpStr
))
530 PRINT("%s ", atomSubOpStr
[subOp
]);
534 PRINT("(SUBOP:%u) ", subOp
);
540 PRINT("%s %s$r%u $s%u %s", asTex()->tex
.target
.getName(),
541 colour
[TXT_MEM
], asTex()->tex
.r
, asTex()->tex
.s
,
544 PRINT("x2^%i ", postFactor
);
545 PRINT("%s%s", dnz
? "dnz " : (ftz
? "ftz " : ""), DataTypeStr
[dType
]);
549 PRINT(" %s", RoundModeStr
[rnd
]);
553 for (d
= 0; defExists(d
); ++d
) {
555 pos
+= getDef(d
)->print(&buf
[pos
], size
- pos
);
558 PRINT(" %s}", colour
[TXT_INSN
]);
561 PRINT(" %s#", colour
[TXT_INSN
]);
564 PRINT(" %s%s", colour
[TXT_INSN
], CondCodeStr
[asCmp()->setCond
]);
567 PRINT(" %s%s", colour
[TXT_INSN
], DataTypeStr
[sType
]);
569 for (s
= 0; srcExists(s
); ++s
) {
570 if (s
== predSrc
|| src(s
).usedAsPtr
)
572 const size_t pre
= pos
;
574 pos
+= src(s
).mod
.print(&buf
[pos
], BUFSZ
- pos
);
577 if (src(s
).isIndirect(0) || src(s
).isIndirect(1))
578 pos
+= getSrc(s
)->asSym()->print(&buf
[pos
], BUFSZ
- pos
,
582 pos
+= getSrc(s
)->print(&buf
[pos
], BUFSZ
- pos
, sType
);
585 PRINT("%s exit", colour
[TXT_INSN
]);
587 PRINT("%s", colour
[TXT_DEFAULT
]);
589 buf
[MIN2(pos
, BUFSZ
- 1)] = 0;
591 INFO("%s (%u)\n", buf
, encSize
);
594 class PrintPass
: public Pass
597 PrintPass() : serial(0) { }
599 virtual bool visit(Function
*);
600 virtual bool visit(BasicBlock
*);
601 virtual bool visit(Instruction
*);
608 PrintPass::visit(Function
*fn
)
612 INFO("\n%s:%i (", fn
->getName(), fn
->getLabel());
614 if (!fn
->outs
.empty())
616 for (std::deque
<ValueRef
>::iterator it
= fn
->outs
.begin();
617 it
!= fn
->outs
.end();
619 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
623 if (!fn
->ins
.empty())
624 INFO("%s%sin", colour
[TXT_DEFAULT
], fn
->outs
.empty() ? "" : ", ");
625 for (std::deque
<ValueDef
>::iterator it
= fn
->ins
.begin();
628 it
->get()->print(str
, sizeof(str
), typeOfSize(it
->get()->reg
.size
));
631 INFO("%s)\n", colour
[TXT_DEFAULT
]);
637 PrintPass::visit(BasicBlock
*bb
)
641 for (Graph::EdgeIterator ei
= bb
->cfg
.incident(); !ei
.end(); ei
.next())
642 INFO(" <- BB:%i (%s)\n",
643 BasicBlock::get(ei
.getNode())->getId(),
644 ei
.getEdge()->typeStr());
646 INFO("BB:%i (%u instructions) - ", bb
->getId(), bb
->getInsnCount());
649 INFO("idom = BB:%i, ", bb
->idom()->getId());
652 for (DLList::Iterator df
= bb
->getDF().iterator(); !df
.end(); df
.next())
653 INFO("BB:%i ", BasicBlock::get(df
)->getId());
657 for (Graph::EdgeIterator ei
= bb
->cfg
.outgoing(); !ei
.end(); ei
.next())
658 INFO(" -> BB:%i (%s)\n",
659 BasicBlock::get(ei
.getNode())->getId(),
660 ei
.getEdge()->typeStr());
666 PrintPass::visit(Instruction
*insn
)
668 INFO("%3i: ", serial
++);
677 pass
.run(this, true, false);
685 pass
.run(this, true, false);
689 Function::printLiveIntervals() const
691 INFO("printing live intervals ...\n");
693 for (ArrayList::Iterator it
= allLValues
.iterator(); !it
.end(); it
.next()) {
694 const Value
*lval
= Value::get(it
)->asLValue();
695 if (lval
&& !lval
->livei
.isEmpty()) {
696 INFO("livei(%%%i): ", lval
->id
);
702 } // namespace nv50_ir