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"
25 #include "codegen/nv50_ir_build_util.h"
28 #include "util/u_math.h"
34 Instruction::isNop() const
36 if (op
== OP_PHI
|| op
== OP_SPLIT
|| op
== OP_MERGE
|| op
== OP_CONSTRAINT
)
38 if (terminator
|| join
) // XXX: should terminator imply flow ?
42 if (!fixed
&& op
== OP_NOP
)
45 if (defExists(0) && def(0).rep()->reg
.data
.id
< 0) {
46 for (int d
= 1; defExists(d
); ++d
)
47 if (def(d
).rep()->reg
.data
.id
>= 0)
48 WARN("part of vector result is unused !\n");
52 if (op
== OP_MOV
|| op
== OP_UNION
) {
53 if (!getDef(0)->equals(getSrc(0)))
56 if (!def(0).rep()->equals(getSrc(1)))
64 bool Instruction::isDead() const
69 op
== OP_SUSTB
|| op
== OP_SUSTP
|| op
== OP_SUREDP
|| op
== OP_SUREDB
||
73 for (int d
= 0; defExists(d
); ++d
)
74 if (getDef(d
)->refCount() || getDef(d
)->reg
.data
.id
>= 0)
77 if (terminator
|| asFlow())
85 // =============================================================================
87 class CopyPropagation
: public Pass
90 virtual bool visit(BasicBlock
*);
93 // Propagate all MOVs forward to make subsequent optimization easier, except if
94 // the sources stem from a phi, in which case we don't want to mess up potential
95 // swaps $rX <-> $rY, i.e. do not create live range overlaps of phi src and def.
97 CopyPropagation::visit(BasicBlock
*bb
)
99 Instruction
*mov
, *si
, *next
;
101 for (mov
= bb
->getEntry(); mov
; mov
= next
) {
103 if (mov
->op
!= OP_MOV
|| mov
->fixed
|| !mov
->getSrc(0)->asLValue())
105 if (mov
->getPredicate())
107 if (mov
->def(0).getFile() != mov
->src(0).getFile())
109 si
= mov
->getSrc(0)->getInsn();
110 if (mov
->getDef(0)->reg
.data
.id
< 0 && si
&& si
->op
!= OP_PHI
) {
112 mov
->def(0).replace(mov
->getSrc(0), false);
113 delete_Instruction(prog
, mov
);
119 // =============================================================================
121 class LoadPropagation
: public Pass
124 virtual bool visit(BasicBlock
*);
126 void checkSwapSrc01(Instruction
*);
128 bool isCSpaceLoad(Instruction
*);
129 bool isImmd32Load(Instruction
*);
130 bool isAttribOrSharedLoad(Instruction
*);
134 LoadPropagation::isCSpaceLoad(Instruction
*ld
)
136 return ld
&& ld
->op
== OP_LOAD
&& ld
->src(0).getFile() == FILE_MEMORY_CONST
;
140 LoadPropagation::isImmd32Load(Instruction
*ld
)
142 if (!ld
|| (ld
->op
!= OP_MOV
) || (typeSizeof(ld
->dType
) != 4))
144 return ld
->src(0).getFile() == FILE_IMMEDIATE
;
148 LoadPropagation::isAttribOrSharedLoad(Instruction
*ld
)
151 (ld
->op
== OP_VFETCH
||
152 (ld
->op
== OP_LOAD
&&
153 (ld
->src(0).getFile() == FILE_SHADER_INPUT
||
154 ld
->src(0).getFile() == FILE_MEMORY_SHARED
)));
158 LoadPropagation::checkSwapSrc01(Instruction
*insn
)
160 if (!prog
->getTarget()->getOpInfo(insn
).commutative
)
161 if (insn
->op
!= OP_SET
&& insn
->op
!= OP_SLCT
)
163 if (insn
->src(1).getFile() != FILE_GPR
)
166 Instruction
*i0
= insn
->getSrc(0)->getInsn();
167 Instruction
*i1
= insn
->getSrc(1)->getInsn();
169 if (isCSpaceLoad(i0
)) {
170 if (!isCSpaceLoad(i1
))
171 insn
->swapSources(0, 1);
175 if (isImmd32Load(i0
)) {
176 if (!isCSpaceLoad(i1
) && !isImmd32Load(i1
))
177 insn
->swapSources(0, 1);
181 if (isAttribOrSharedLoad(i1
)) {
182 if (!isAttribOrSharedLoad(i0
))
183 insn
->swapSources(0, 1);
190 if (insn
->op
== OP_SET
|| insn
->op
== OP_SET_AND
||
191 insn
->op
== OP_SET_OR
|| insn
->op
== OP_SET_XOR
)
192 insn
->asCmp()->setCond
= reverseCondCode(insn
->asCmp()->setCond
);
194 if (insn
->op
== OP_SLCT
)
195 insn
->asCmp()->setCond
= inverseCondCode(insn
->asCmp()->setCond
);
199 LoadPropagation::visit(BasicBlock
*bb
)
201 const Target
*targ
= prog
->getTarget();
204 for (Instruction
*i
= bb
->getEntry(); i
; i
= next
) {
207 if (i
->op
== OP_CALL
) // calls have args as sources, they must be in regs
213 for (int s
= 0; i
->srcExists(s
); ++s
) {
214 Instruction
*ld
= i
->getSrc(s
)->getInsn();
216 if (!ld
|| ld
->fixed
|| (ld
->op
!= OP_LOAD
&& ld
->op
!= OP_MOV
))
218 if (!targ
->insnCanLoad(i
, s
, ld
))
222 i
->setSrc(s
, ld
->getSrc(0));
223 if (ld
->src(0).isIndirect(0))
224 i
->setIndirect(s
, 0, ld
->getIndirect(0, 0));
226 if (ld
->getDef(0)->refCount() == 0)
227 delete_Instruction(prog
, ld
);
233 // =============================================================================
235 // Evaluate constant expressions.
236 class ConstantFolding
: public Pass
239 bool foldAll(Program
*);
242 virtual bool visit(BasicBlock
*);
244 void expr(Instruction
*, ImmediateValue
&, ImmediateValue
&);
245 void expr(Instruction
*, ImmediateValue
&, ImmediateValue
&, ImmediateValue
&);
246 void opnd(Instruction
*, ImmediateValue
&, int s
);
248 void unary(Instruction
*, const ImmediateValue
&);
250 void tryCollapseChainedMULs(Instruction
*, const int s
, ImmediateValue
&);
252 // TGSI 'true' is converted to -1 by F2I(NEG(SET)), track back to SET
253 CmpInstruction
*findOriginForTestWithZero(Value
*);
255 unsigned int foldCount
;
260 // TODO: remember generated immediates and only revisit these
262 ConstantFolding::foldAll(Program
*prog
)
264 unsigned int iterCount
= 0;
269 } while (foldCount
&& ++iterCount
< 2);
274 ConstantFolding::visit(BasicBlock
*bb
)
276 Instruction
*i
, *next
;
278 for (i
= bb
->getEntry(); i
; i
= next
) {
280 if (i
->op
== OP_MOV
|| i
->op
== OP_CALL
)
283 ImmediateValue src0
, src1
, src2
;
285 if (i
->srcExists(2) &&
286 i
->src(0).getImmediate(src0
) &&
287 i
->src(1).getImmediate(src1
) &&
288 i
->src(2).getImmediate(src2
))
289 expr(i
, src0
, src1
, src2
);
291 if (i
->srcExists(1) &&
292 i
->src(0).getImmediate(src0
) && i
->src(1).getImmediate(src1
))
295 if (i
->srcExists(0) && i
->src(0).getImmediate(src0
))
298 if (i
->srcExists(1) && i
->src(1).getImmediate(src1
))
305 ConstantFolding::findOriginForTestWithZero(Value
*value
)
309 Instruction
*insn
= value
->getInsn();
311 while (insn
&& insn
->op
!= OP_SET
) {
312 Instruction
*next
= NULL
;
317 next
= insn
->getSrc(0)->getInsn();
318 if (insn
->sType
!= next
->dType
)
322 next
= insn
->getSrc(0)->getInsn();
329 return insn
? insn
->asCmp() : NULL
;
333 Modifier::applyTo(ImmediateValue
& imm
) const
335 if (!bits
) // avoid failure if imm.reg.type is unhandled (e.g. b128)
337 switch (imm
.reg
.type
) {
339 if (bits
& NV50_IR_MOD_ABS
)
340 imm
.reg
.data
.f32
= fabsf(imm
.reg
.data
.f32
);
341 if (bits
& NV50_IR_MOD_NEG
)
342 imm
.reg
.data
.f32
= -imm
.reg
.data
.f32
;
343 if (bits
& NV50_IR_MOD_SAT
) {
344 if (imm
.reg
.data
.f32
< 0.0f
)
345 imm
.reg
.data
.f32
= 0.0f
;
347 if (imm
.reg
.data
.f32
> 1.0f
)
348 imm
.reg
.data
.f32
= 1.0f
;
350 assert(!(bits
& NV50_IR_MOD_NOT
));
353 case TYPE_S8
: // NOTE: will be extended
356 case TYPE_U8
: // NOTE: treated as signed
359 if (bits
& NV50_IR_MOD_ABS
)
360 imm
.reg
.data
.s32
= (imm
.reg
.data
.s32
>= 0) ?
361 imm
.reg
.data
.s32
: -imm
.reg
.data
.s32
;
362 if (bits
& NV50_IR_MOD_NEG
)
363 imm
.reg
.data
.s32
= -imm
.reg
.data
.s32
;
364 if (bits
& NV50_IR_MOD_NOT
)
365 imm
.reg
.data
.s32
= ~imm
.reg
.data
.s32
;
369 if (bits
& NV50_IR_MOD_ABS
)
370 imm
.reg
.data
.f64
= fabs(imm
.reg
.data
.f64
);
371 if (bits
& NV50_IR_MOD_NEG
)
372 imm
.reg
.data
.f64
= -imm
.reg
.data
.f64
;
373 if (bits
& NV50_IR_MOD_SAT
) {
374 if (imm
.reg
.data
.f64
< 0.0)
375 imm
.reg
.data
.f64
= 0.0;
377 if (imm
.reg
.data
.f64
> 1.0)
378 imm
.reg
.data
.f64
= 1.0;
380 assert(!(bits
& NV50_IR_MOD_NOT
));
384 assert(!"invalid/unhandled type");
385 imm
.reg
.data
.u64
= 0;
391 Modifier::getOp() const
394 case NV50_IR_MOD_ABS
: return OP_ABS
;
395 case NV50_IR_MOD_NEG
: return OP_NEG
;
396 case NV50_IR_MOD_SAT
: return OP_SAT
;
397 case NV50_IR_MOD_NOT
: return OP_NOT
;
406 ConstantFolding::expr(Instruction
*i
,
407 ImmediateValue
&imm0
, ImmediateValue
&imm1
)
409 struct Storage
*const a
= &imm0
.reg
, *const b
= &imm1
.reg
;
412 memset(&res
.data
, 0, sizeof(res
.data
));
418 if (i
->dnz
&& i
->dType
== TYPE_F32
) {
419 if (!isfinite(a
->data
.f32
))
421 if (!isfinite(b
->data
.f32
))
425 case TYPE_F32
: res
.data
.f32
= a
->data
.f32
* b
->data
.f32
; break;
426 case TYPE_F64
: res
.data
.f64
= a
->data
.f64
* b
->data
.f64
; break;
428 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
) {
429 res
.data
.s32
= ((int64_t)a
->data
.s32
* b
->data
.s32
) >> 32;
434 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
) {
435 res
.data
.u32
= ((uint64_t)a
->data
.u32
* b
->data
.u32
) >> 32;
438 res
.data
.u32
= a
->data
.u32
* b
->data
.u32
; break;
444 if (b
->data
.u32
== 0)
447 case TYPE_F32
: res
.data
.f32
= a
->data
.f32
/ b
->data
.f32
; break;
448 case TYPE_F64
: res
.data
.f64
= a
->data
.f64
/ b
->data
.f64
; break;
449 case TYPE_S32
: res
.data
.s32
= a
->data
.s32
/ b
->data
.s32
; break;
450 case TYPE_U32
: res
.data
.u32
= a
->data
.u32
/ b
->data
.u32
; break;
457 case TYPE_F32
: res
.data
.f32
= a
->data
.f32
+ b
->data
.f32
; break;
458 case TYPE_F64
: res
.data
.f64
= a
->data
.f64
+ b
->data
.f64
; break;
460 case TYPE_U32
: res
.data
.u32
= a
->data
.u32
+ b
->data
.u32
; break;
467 case TYPE_F32
: res
.data
.f32
= pow(a
->data
.f32
, b
->data
.f32
); break;
468 case TYPE_F64
: res
.data
.f64
= pow(a
->data
.f64
, b
->data
.f64
); break;
475 case TYPE_F32
: res
.data
.f32
= MAX2(a
->data
.f32
, b
->data
.f32
); break;
476 case TYPE_F64
: res
.data
.f64
= MAX2(a
->data
.f64
, b
->data
.f64
); break;
477 case TYPE_S32
: res
.data
.s32
= MAX2(a
->data
.s32
, b
->data
.s32
); break;
478 case TYPE_U32
: res
.data
.u32
= MAX2(a
->data
.u32
, b
->data
.u32
); break;
485 case TYPE_F32
: res
.data
.f32
= MIN2(a
->data
.f32
, b
->data
.f32
); break;
486 case TYPE_F64
: res
.data
.f64
= MIN2(a
->data
.f64
, b
->data
.f64
); break;
487 case TYPE_S32
: res
.data
.s32
= MIN2(a
->data
.s32
, b
->data
.s32
); break;
488 case TYPE_U32
: res
.data
.u32
= MIN2(a
->data
.u32
, b
->data
.u32
); break;
494 res
.data
.u64
= a
->data
.u64
& b
->data
.u64
;
497 res
.data
.u64
= a
->data
.u64
| b
->data
.u64
;
500 res
.data
.u64
= a
->data
.u64
^ b
->data
.u64
;
503 res
.data
.u32
= a
->data
.u32
<< b
->data
.u32
;
507 case TYPE_S32
: res
.data
.s32
= a
->data
.s32
>> b
->data
.u32
; break;
508 case TYPE_U32
: res
.data
.u32
= a
->data
.u32
>> b
->data
.u32
; break;
514 if (a
->data
.u32
!= b
->data
.u32
)
516 res
.data
.u32
= a
->data
.u32
;
519 int offset
= b
->data
.u32
& 0xff;
520 int width
= (b
->data
.u32
>> 8) & 0xff;
527 if (width
+ offset
< 32) {
529 lshift
= 32 - width
- offset
;
531 if (i
->subOp
== NV50_IR_SUBOP_EXTBF_REV
)
532 res
.data
.u32
= util_bitreverse(a
->data
.u32
);
534 res
.data
.u32
= a
->data
.u32
;
536 case TYPE_S32
: res
.data
.s32
= (res
.data
.s32
<< lshift
) >> rshift
; break;
537 case TYPE_U32
: res
.data
.u32
= (res
.data
.u32
<< lshift
) >> rshift
; break;
544 res
.data
.u32
= util_bitcount(a
->data
.u32
& b
->data
.u32
);
551 i
->src(0).mod
= Modifier(0);
552 i
->src(1).mod
= Modifier(0);
554 i
->setSrc(0, new_ImmediateValue(i
->bb
->getProgram(), res
.data
.u32
));
557 i
->getSrc(0)->reg
.data
= res
.data
;
559 if (i
->op
== OP_MAD
|| i
->op
== OP_FMA
) {
562 i
->setSrc(1, i
->getSrc(0));
563 i
->src(1).mod
= i
->src(2).mod
;
564 i
->setSrc(0, i
->getSrc(2));
568 if (i
->src(0).getImmediate(src0
))
569 expr(i
, src0
, *i
->getSrc(1)->asImm());
570 if (i
->saturate
&& !prog
->getTarget()->isSatSupported(i
)) {
571 bld
.setPosition(i
, false);
572 i
->setSrc(1, bld
.loadImm(NULL
, res
.data
.u32
));
575 i
->op
= i
->saturate
? OP_SAT
: OP_MOV
; /* SAT handled by unary() */
581 ConstantFolding::expr(Instruction
*i
,
582 ImmediateValue
&imm0
,
583 ImmediateValue
&imm1
,
584 ImmediateValue
&imm2
)
586 struct Storage
*const a
= &imm0
.reg
, *const b
= &imm1
.reg
, *const c
= &imm2
.reg
;
589 memset(&res
.data
, 0, sizeof(res
.data
));
593 int offset
= b
->data
.u32
& 0xff;
594 int width
= (b
->data
.u32
>> 8) & 0xff;
595 unsigned bitmask
= ((1 << width
) - 1) << offset
;
596 res
.data
.u32
= ((a
->data
.u32
<< offset
) & bitmask
) | (c
->data
.u32
& ~bitmask
);
604 i
->src(0).mod
= Modifier(0);
605 i
->src(1).mod
= Modifier(0);
606 i
->src(2).mod
= Modifier(0);
608 i
->setSrc(0, new_ImmediateValue(i
->bb
->getProgram(), res
.data
.u32
));
612 i
->getSrc(0)->reg
.data
= res
.data
;
618 ConstantFolding::unary(Instruction
*i
, const ImmediateValue
&imm
)
622 if (i
->dType
!= TYPE_F32
)
625 case OP_NEG
: res
.data
.f32
= -imm
.reg
.data
.f32
; break;
626 case OP_ABS
: res
.data
.f32
= fabsf(imm
.reg
.data
.f32
); break;
627 case OP_SAT
: res
.data
.f32
= CLAMP(imm
.reg
.data
.f32
, 0.0f
, 1.0f
); break;
628 case OP_RCP
: res
.data
.f32
= 1.0f
/ imm
.reg
.data
.f32
; break;
629 case OP_RSQ
: res
.data
.f32
= 1.0f
/ sqrtf(imm
.reg
.data
.f32
); break;
630 case OP_LG2
: res
.data
.f32
= log2f(imm
.reg
.data
.f32
); break;
631 case OP_EX2
: res
.data
.f32
= exp2f(imm
.reg
.data
.f32
); break;
632 case OP_SIN
: res
.data
.f32
= sinf(imm
.reg
.data
.f32
); break;
633 case OP_COS
: res
.data
.f32
= cosf(imm
.reg
.data
.f32
); break;
634 case OP_SQRT
: res
.data
.f32
= sqrtf(imm
.reg
.data
.f32
); break;
637 // these should be handled in subsequent OP_SIN/COS/EX2
638 res
.data
.f32
= imm
.reg
.data
.f32
;
644 i
->setSrc(0, new_ImmediateValue(i
->bb
->getProgram(), res
.data
.f32
));
645 i
->src(0).mod
= Modifier(0);
649 ConstantFolding::tryCollapseChainedMULs(Instruction
*mul2
,
650 const int s
, ImmediateValue
& imm2
)
652 const int t
= s
? 0 : 1;
654 Instruction
*mul1
= NULL
; // mul1 before mul2
656 float f
= imm2
.reg
.data
.f32
;
659 assert(mul2
->op
== OP_MUL
&& mul2
->dType
== TYPE_F32
);
661 if (mul2
->getSrc(t
)->refCount() == 1) {
662 insn
= mul2
->getSrc(t
)->getInsn();
663 if (!mul2
->src(t
).mod
&& insn
->op
== OP_MUL
&& insn
->dType
== TYPE_F32
)
665 if (mul1
&& !mul1
->saturate
) {
668 if (mul1
->src(s1
= 0).getImmediate(imm1
) ||
669 mul1
->src(s1
= 1).getImmediate(imm1
)) {
670 bld
.setPosition(mul1
, false);
672 // d = mul a, imm2 -> d = mul r, (imm1 * imm2)
673 mul1
->setSrc(s1
, bld
.loadImm(NULL
, f
* imm1
.reg
.data
.f32
));
674 mul1
->src(s1
).mod
= Modifier(0);
675 mul2
->def(0).replace(mul1
->getDef(0), false);
677 if (prog
->getTarget()->isPostMultiplySupported(OP_MUL
, f
, e
)) {
679 // d = mul c, imm -> d = mul_x_imm a, b
680 mul1
->postFactor
= e
;
681 mul2
->def(0).replace(mul1
->getDef(0), false);
683 mul1
->src(0).mod
*= Modifier(NV50_IR_MOD_NEG
);
685 mul1
->saturate
= mul2
->saturate
;
689 if (mul2
->getDef(0)->refCount() == 1 && !mul2
->saturate
) {
691 // d = mul b, c -> d = mul_x_imm a, c
693 insn
= (*mul2
->getDef(0)->uses
.begin())->getInsn();
698 s2
= insn
->getSrc(0) == mul1
->getDef(0) ? 0 : 1;
700 if (insn
->op
== OP_MUL
&& insn
->dType
== TYPE_F32
)
701 if (!insn
->src(s2
).mod
&& !insn
->src(t2
).getImmediate(imm1
))
703 if (mul2
&& prog
->getTarget()->isPostMultiplySupported(OP_MUL
, f
, e
)) {
704 mul2
->postFactor
= e
;
705 mul2
->setSrc(s2
, mul1
->src(t
));
707 mul2
->src(s2
).mod
*= Modifier(NV50_IR_MOD_NEG
);
713 ConstantFolding::opnd(Instruction
*i
, ImmediateValue
&imm0
, int s
)
716 const operation op
= i
->op
;
717 Instruction
*newi
= i
;
721 if (i
->dType
== TYPE_F32
)
722 tryCollapseChainedMULs(i
, s
, imm0
);
724 if (i
->subOp
== NV50_IR_SUBOP_MUL_HIGH
) {
725 assert(!isFloatType(i
->sType
));
726 if (imm0
.isInteger(1) && i
->dType
== TYPE_S32
) {
727 bld
.setPosition(i
, false);
728 // Need to set to the sign value, which is a compare.
729 newi
= bld
.mkCmp(OP_SET
, CC_LT
, TYPE_S32
, i
->getDef(0),
730 TYPE_S32
, i
->getSrc(t
), bld
.mkImm(0));
731 delete_Instruction(prog
, i
);
732 } else if (imm0
.isInteger(0) || imm0
.isInteger(1)) {
733 // The high bits can't be set in this case (either mul by 0 or
737 i
->setSrc(0, new_ImmediateValue(prog
, 0u));
738 i
->src(0).mod
= Modifier(0);
740 } else if (!imm0
.isNegative() && imm0
.isPow2()) {
741 // Translate into a shift
745 imm0
.reg
.data
.u32
= 32 - imm0
.reg
.data
.u32
;
746 i
->setSrc(0, i
->getSrc(t
));
747 i
->src(0).mod
= i
->src(t
).mod
;
748 i
->setSrc(1, new_ImmediateValue(prog
, imm0
.reg
.data
.u32
));
752 if (imm0
.isInteger(0)) {
754 i
->setSrc(0, new_ImmediateValue(prog
, 0u));
755 i
->src(0).mod
= Modifier(0);
758 if (imm0
.isInteger(1) || imm0
.isInteger(-1)) {
759 if (imm0
.isNegative())
760 i
->src(t
).mod
= i
->src(t
).mod
^ Modifier(NV50_IR_MOD_NEG
);
761 i
->op
= i
->src(t
).mod
.getOp();
763 i
->setSrc(0, i
->getSrc(1));
764 i
->src(0).mod
= i
->src(1).mod
;
771 if (imm0
.isInteger(2) || imm0
.isInteger(-2)) {
772 if (imm0
.isNegative())
773 i
->src(t
).mod
= i
->src(t
).mod
^ Modifier(NV50_IR_MOD_NEG
);
775 i
->setSrc(s
, i
->getSrc(t
));
776 i
->src(s
).mod
= i
->src(t
).mod
;
778 if (!isFloatType(i
->sType
) && !imm0
.isNegative() && imm0
.isPow2()) {
781 i
->setSrc(0, i
->getSrc(t
));
782 i
->src(0).mod
= i
->src(t
).mod
;
783 i
->setSrc(1, new_ImmediateValue(prog
, imm0
.reg
.data
.u32
));
788 if (imm0
.isInteger(0)) {
789 i
->setSrc(0, i
->getSrc(2));
790 i
->src(0).mod
= i
->src(2).mod
;
793 i
->op
= i
->src(0).mod
.getOp();
797 if (imm0
.isInteger(1) || imm0
.isInteger(-1)) {
798 if (imm0
.isNegative())
799 i
->src(t
).mod
= i
->src(t
).mod
^ Modifier(NV50_IR_MOD_NEG
);
801 i
->setSrc(0, i
->getSrc(1));
802 i
->src(0).mod
= i
->src(1).mod
;
804 i
->setSrc(1, i
->getSrc(2));
805 i
->src(1).mod
= i
->src(2).mod
;
813 if (imm0
.isInteger(0)) {
815 i
->setSrc(0, i
->getSrc(1));
816 i
->src(0).mod
= i
->src(1).mod
;
819 i
->op
= i
->src(0).mod
.getOp();
821 i
->src(0).mod
= Modifier(0);
826 if (s
!= 1 || (i
->dType
!= TYPE_S32
&& i
->dType
!= TYPE_U32
))
828 bld
.setPosition(i
, false);
829 if (imm0
.reg
.data
.u32
== 0) {
832 if (imm0
.reg
.data
.u32
== 1) {
836 if (i
->dType
== TYPE_U32
&& imm0
.isPow2()) {
838 i
->setSrc(1, bld
.mkImm(util_logbase2(imm0
.reg
.data
.u32
)));
840 if (i
->dType
== TYPE_U32
) {
843 const uint32_t d
= imm0
.reg
.data
.u32
;
846 uint32_t l
= util_logbase2(d
);
847 if (((uint32_t)1 << l
) < d
)
849 m
= (((uint64_t)1 << 32) * (((uint64_t)1 << l
) - d
)) / d
+ 1;
855 mul
= bld
.mkOp2(OP_MUL
, TYPE_U32
, tA
, i
->getSrc(0),
856 bld
.loadImm(NULL
, m
));
857 mul
->subOp
= NV50_IR_SUBOP_MUL_HIGH
;
858 bld
.mkOp2(OP_SUB
, TYPE_U32
, tB
, i
->getSrc(0), tA
);
861 bld
.mkOp2(OP_SHR
, TYPE_U32
, tA
, tB
, bld
.mkImm(r
));
864 tB
= s
? bld
.getSSA() : i
->getDef(0);
865 newi
= bld
.mkOp2(OP_ADD
, TYPE_U32
, tB
, mul
->getDef(0), tA
);
867 bld
.mkOp2(OP_SHR
, TYPE_U32
, i
->getDef(0), tB
, bld
.mkImm(s
));
869 delete_Instruction(prog
, i
);
871 if (imm0
.reg
.data
.s32
== -1) {
877 const int32_t d
= imm0
.reg
.data
.s32
;
879 int32_t l
= util_logbase2(static_cast<unsigned>(abs(d
)));
880 if ((1 << l
) < abs(d
))
884 m
= ((uint64_t)1 << (32 + l
- 1)) / abs(d
) + 1 - ((uint64_t)1 << 32);
888 bld
.mkOp3(OP_MAD
, TYPE_S32
, tA
, i
->getSrc(0), bld
.loadImm(NULL
, m
),
889 i
->getSrc(0))->subOp
= NV50_IR_SUBOP_MUL_HIGH
;
891 bld
.mkOp2(OP_SHR
, TYPE_S32
, tB
, tA
, bld
.mkImm(l
- 1));
895 bld
.mkCmp(OP_SET
, CC_LT
, TYPE_S32
, tA
, TYPE_S32
, i
->getSrc(0), bld
.mkImm(0));
896 tD
= (d
< 0) ? bld
.getSSA() : i
->getDef(0)->asLValue();
897 newi
= bld
.mkOp2(OP_SUB
, TYPE_U32
, tD
, tB
, tA
);
899 bld
.mkOp1(OP_NEG
, TYPE_S32
, i
->getDef(0), tB
);
901 delete_Instruction(prog
, i
);
906 if (i
->sType
== TYPE_U32
&& imm0
.isPow2()) {
907 bld
.setPosition(i
, false);
909 i
->setSrc(1, bld
.loadImm(NULL
, imm0
.reg
.data
.u32
- 1));
913 case OP_SET
: // TODO: SET_AND,OR,XOR
915 CmpInstruction
*si
= findOriginForTestWithZero(i
->getSrc(t
));
917 if (i
->src(t
).mod
!= Modifier(0))
919 if (imm0
.reg
.data
.u32
!= 0 || !si
|| si
->op
!= OP_SET
)
922 ccZ
= (CondCode
)((unsigned int)i
->asCmp()->setCond
& ~CC_U
);
924 ccZ
= reverseCondCode(ccZ
);
926 case CC_LT
: cc
= CC_FL
; break;
927 case CC_GE
: cc
= CC_TR
; break;
928 case CC_EQ
: cc
= inverseCondCode(cc
); break;
929 case CC_LE
: cc
= inverseCondCode(cc
); break;
935 i
->asCmp()->setCond
= cc
;
936 i
->setSrc(0, si
->src(0));
937 i
->setSrc(1, si
->src(1));
938 i
->sType
= si
->sType
;
944 if (s
!= 1 || i
->src(0).mod
!= Modifier(0))
946 // try to concatenate shifts
947 Instruction
*si
= i
->getSrc(0)->getInsn();
948 if (!si
|| si
->op
!= OP_SHL
)
951 if (si
->src(1).getImmediate(imm1
)) {
952 bld
.setPosition(i
, false);
953 i
->setSrc(0, si
->getSrc(0));
954 i
->setSrc(1, bld
.loadImm(NULL
, imm0
.reg
.data
.u32
+ imm1
.reg
.data
.u32
));
976 case TYPE_S32
: res
= util_last_bit_signed(imm0
.reg
.data
.s32
) - 1; break;
977 case TYPE_U32
: res
= util_last_bit(imm0
.reg
.data
.u32
) - 1; break;
981 if (i
->subOp
== NV50_IR_SUBOP_BFIND_SAMT
&& res
>= 0)
983 bld
.setPosition(i
, false); /* make sure bld is init'ed */
984 i
->setSrc(0, bld
.mkImm(res
));
991 // Only deal with 1-arg POPCNT here
994 uint32_t res
= util_bitcount(imm0
.reg
.data
.u32
);
995 i
->setSrc(0, new_ImmediateValue(i
->bb
->getProgram(), res
));
1007 // =============================================================================
1009 // Merge modifier operations (ABS, NEG, NOT) into ValueRefs where allowed.
1010 class ModifierFolding
: public Pass
1013 virtual bool visit(BasicBlock
*);
1017 ModifierFolding::visit(BasicBlock
*bb
)
1019 const Target
*target
= prog
->getTarget();
1021 Instruction
*i
, *next
, *mi
;
1024 for (i
= bb
->getEntry(); i
; i
= next
) {
1027 if (0 && i
->op
== OP_SUB
) {
1028 // turn "sub" into "add neg" (do we really want this ?)
1030 i
->src(0).mod
= i
->src(0).mod
^ Modifier(NV50_IR_MOD_NEG
);
1033 for (int s
= 0; s
< 3 && i
->srcExists(s
); ++s
) {
1034 mi
= i
->getSrc(s
)->getInsn();
1036 mi
->predSrc
>= 0 || mi
->getDef(0)->refCount() > 8)
1038 if (i
->sType
== TYPE_U32
&& mi
->dType
== TYPE_S32
) {
1039 if ((i
->op
!= OP_ADD
&&
1041 (mi
->op
!= OP_ABS
&&
1045 if (i
->sType
!= mi
->dType
) {
1048 if ((mod
= Modifier(mi
->op
)) == Modifier(0))
1050 mod
*= mi
->src(0).mod
;
1052 if ((i
->op
== OP_ABS
) || i
->src(s
).mod
.abs()) {
1053 // abs neg [abs] = abs
1054 mod
= mod
& Modifier(~(NV50_IR_MOD_NEG
| NV50_IR_MOD_ABS
));
1056 if ((i
->op
== OP_NEG
) && mod
.neg()) {
1058 // neg as both opcode and modifier on same insn is prohibited
1059 // neg neg abs = abs, neg neg = identity
1060 mod
= mod
& Modifier(~NV50_IR_MOD_NEG
);
1061 i
->op
= mod
.getOp();
1062 mod
= mod
& Modifier(~NV50_IR_MOD_ABS
);
1063 if (mod
== Modifier(0))
1067 if (target
->isModSupported(i
, s
, mod
)) {
1068 i
->setSrc(s
, mi
->getSrc(0));
1069 i
->src(s
).mod
*= mod
;
1073 if (i
->op
== OP_SAT
) {
1074 mi
= i
->getSrc(0)->getInsn();
1076 mi
->getDef(0)->refCount() <= 1 && target
->isSatSupported(mi
)) {
1078 mi
->setDef(0, i
->getDef(0));
1079 delete_Instruction(prog
, i
);
1087 // =============================================================================
1089 // MUL + ADD -> MAD/FMA
1090 // MIN/MAX(a, a) -> a, etc.
1091 // SLCT(a, b, const) -> cc(const) ? a : b
1093 // MUL(MUL(a, b), const) -> MUL_Xconst(a, b)
1094 class AlgebraicOpt
: public Pass
1097 virtual bool visit(BasicBlock
*);
1099 void handleABS(Instruction
*);
1100 bool handleADD(Instruction
*);
1101 bool tryADDToMADOrSAD(Instruction
*, operation toOp
);
1102 void handleMINMAX(Instruction
*);
1103 void handleRCP(Instruction
*);
1104 void handleSLCT(Instruction
*);
1105 void handleLOGOP(Instruction
*);
1106 void handleCVT(Instruction
*);
1107 void handleSUCLAMP(Instruction
*);
1113 AlgebraicOpt::handleABS(Instruction
*abs
)
1115 Instruction
*sub
= abs
->getSrc(0)->getInsn();
1118 !prog
->getTarget()->isOpSupported(OP_SAD
, abs
->dType
))
1120 // expect not to have mods yet, if we do, bail
1121 if (sub
->src(0).mod
|| sub
->src(1).mod
)
1123 // hidden conversion ?
1124 ty
= intTypeToSigned(sub
->dType
);
1125 if (abs
->dType
!= abs
->sType
|| ty
!= abs
->sType
)
1128 if ((sub
->op
!= OP_ADD
&& sub
->op
!= OP_SUB
) ||
1129 sub
->src(0).getFile() != FILE_GPR
|| sub
->src(0).mod
||
1130 sub
->src(1).getFile() != FILE_GPR
|| sub
->src(1).mod
)
1133 Value
*src0
= sub
->getSrc(0);
1134 Value
*src1
= sub
->getSrc(1);
1136 if (sub
->op
== OP_ADD
) {
1137 Instruction
*neg
= sub
->getSrc(1)->getInsn();
1138 if (neg
&& neg
->op
!= OP_NEG
) {
1139 neg
= sub
->getSrc(0)->getInsn();
1140 src0
= sub
->getSrc(1);
1142 if (!neg
|| neg
->op
!= OP_NEG
||
1143 neg
->dType
!= neg
->sType
|| neg
->sType
!= ty
)
1145 src1
= neg
->getSrc(0);
1149 abs
->moveSources(1, 2); // move sources >=1 up by 2
1151 abs
->setType(sub
->dType
);
1152 abs
->setSrc(0, src0
);
1153 abs
->setSrc(1, src1
);
1154 bld
.setPosition(abs
, false);
1155 abs
->setSrc(2, bld
.loadImm(bld
.getSSA(typeSizeof(ty
)), 0));
1159 AlgebraicOpt::handleADD(Instruction
*add
)
1161 Value
*src0
= add
->getSrc(0);
1162 Value
*src1
= add
->getSrc(1);
1164 if (src0
->reg
.file
!= FILE_GPR
|| src1
->reg
.file
!= FILE_GPR
)
1167 bool changed
= false;
1168 if (!changed
&& prog
->getTarget()->isOpSupported(OP_MAD
, add
->dType
))
1169 changed
= tryADDToMADOrSAD(add
, OP_MAD
);
1170 if (!changed
&& prog
->getTarget()->isOpSupported(OP_SAD
, add
->dType
))
1171 changed
= tryADDToMADOrSAD(add
, OP_SAD
);
1175 // ADD(SAD(a,b,0), c) -> SAD(a,b,c)
1176 // ADD(MUL(a,b), c) -> MAD(a,b,c)
1178 AlgebraicOpt::tryADDToMADOrSAD(Instruction
*add
, operation toOp
)
1180 Value
*src0
= add
->getSrc(0);
1181 Value
*src1
= add
->getSrc(1);
1184 const operation srcOp
= toOp
== OP_SAD
? OP_SAD
: OP_MUL
;
1185 const Modifier modBad
= Modifier(~((toOp
== OP_MAD
) ? NV50_IR_MOD_NEG
: 0));
1188 if (src0
->refCount() == 1 &&
1189 src0
->getUniqueInsn() && src0
->getUniqueInsn()->op
== srcOp
)
1192 if (src1
->refCount() == 1 &&
1193 src1
->getUniqueInsn() && src1
->getUniqueInsn()->op
== srcOp
)
1198 if ((src0
->getUniqueInsn() && src0
->getUniqueInsn()->bb
!= add
->bb
) ||
1199 (src1
->getUniqueInsn() && src1
->getUniqueInsn()->bb
!= add
->bb
))
1202 src
= add
->getSrc(s
);
1204 if (src
->getInsn()->postFactor
)
1206 if (toOp
== OP_SAD
) {
1208 if (!src
->getInsn()->src(2).getImmediate(imm
))
1210 if (!imm
.isInteger(0))
1214 mod
[0] = add
->src(0).mod
;
1215 mod
[1] = add
->src(1).mod
;
1216 mod
[2] = src
->getUniqueInsn()->src(0).mod
;
1217 mod
[3] = src
->getUniqueInsn()->src(1).mod
;
1219 if (((mod
[0] | mod
[1]) | (mod
[2] | mod
[3])) & modBad
)
1223 add
->subOp
= src
->getInsn()->subOp
; // potentially mul-high
1225 add
->setSrc(2, add
->src(s
? 0 : 1));
1227 add
->setSrc(0, src
->getInsn()->getSrc(0));
1228 add
->src(0).mod
= mod
[2] ^ mod
[s
];
1229 add
->setSrc(1, src
->getInsn()->getSrc(1));
1230 add
->src(1).mod
= mod
[3];
1236 AlgebraicOpt::handleMINMAX(Instruction
*minmax
)
1238 Value
*src0
= minmax
->getSrc(0);
1239 Value
*src1
= minmax
->getSrc(1);
1241 if (src0
!= src1
|| src0
->reg
.file
!= FILE_GPR
)
1243 if (minmax
->src(0).mod
== minmax
->src(1).mod
) {
1244 if (minmax
->def(0).mayReplace(minmax
->src(0))) {
1245 minmax
->def(0).replace(minmax
->src(0), false);
1246 minmax
->bb
->remove(minmax
);
1248 minmax
->op
= OP_CVT
;
1249 minmax
->setSrc(1, NULL
);
1253 // min(x, -x) = -abs(x)
1254 // min(x, -abs(x)) = -abs(x)
1255 // min(x, abs(x)) = x
1256 // max(x, -abs(x)) = x
1257 // max(x, abs(x)) = abs(x)
1258 // max(x, -x) = abs(x)
1263 AlgebraicOpt::handleRCP(Instruction
*rcp
)
1265 Instruction
*si
= rcp
->getSrc(0)->getUniqueInsn();
1267 if (si
&& si
->op
== OP_RCP
) {
1268 Modifier mod
= rcp
->src(0).mod
* si
->src(0).mod
;
1269 rcp
->op
= mod
.getOp();
1270 rcp
->setSrc(0, si
->getSrc(0));
1275 AlgebraicOpt::handleSLCT(Instruction
*slct
)
1277 if (slct
->getSrc(2)->reg
.file
== FILE_IMMEDIATE
) {
1278 if (slct
->getSrc(2)->asImm()->compare(slct
->asCmp()->setCond
, 0.0f
))
1279 slct
->setSrc(0, slct
->getSrc(1));
1281 if (slct
->getSrc(0) != slct
->getSrc(1)) {
1285 slct
->setSrc(1, NULL
);
1286 slct
->setSrc(2, NULL
);
1290 AlgebraicOpt::handleLOGOP(Instruction
*logop
)
1292 Value
*src0
= logop
->getSrc(0);
1293 Value
*src1
= logop
->getSrc(1);
1295 if (src0
->reg
.file
!= FILE_GPR
|| src1
->reg
.file
!= FILE_GPR
)
1299 if ((logop
->op
== OP_AND
|| logop
->op
== OP_OR
) &&
1300 logop
->def(0).mayReplace(logop
->src(0))) {
1301 logop
->def(0).replace(logop
->src(0), false);
1302 delete_Instruction(prog
, logop
);
1305 // try AND(SET, SET) -> SET_AND(SET)
1306 Instruction
*set0
= src0
->getInsn();
1307 Instruction
*set1
= src1
->getInsn();
1309 if (!set0
|| set0
->fixed
|| !set1
|| set1
->fixed
)
1311 if (set1
->op
!= OP_SET
) {
1312 Instruction
*xchg
= set0
;
1315 if (set1
->op
!= OP_SET
)
1318 operation redOp
= (logop
->op
== OP_AND
? OP_SET_AND
:
1319 logop
->op
== OP_XOR
? OP_SET_XOR
: OP_SET_OR
);
1320 if (!prog
->getTarget()->isOpSupported(redOp
, set1
->sType
))
1322 if (set0
->op
!= OP_SET
&&
1323 set0
->op
!= OP_SET_AND
&&
1324 set0
->op
!= OP_SET_OR
&&
1325 set0
->op
!= OP_SET_XOR
)
1327 if (set0
->getDef(0)->refCount() > 1 &&
1328 set1
->getDef(0)->refCount() > 1)
1330 if (set0
->getPredicate() || set1
->getPredicate())
1332 // check that they don't source each other
1333 for (int s
= 0; s
< 2; ++s
)
1334 if (set0
->getSrc(s
) == set1
->getDef(0) ||
1335 set1
->getSrc(s
) == set0
->getDef(0))
1338 set0
= cloneForward(func
, set0
);
1339 set1
= cloneShallow(func
, set1
);
1340 logop
->bb
->insertAfter(logop
, set1
);
1341 logop
->bb
->insertAfter(logop
, set0
);
1343 set0
->dType
= TYPE_U8
;
1344 set0
->getDef(0)->reg
.file
= FILE_PREDICATE
;
1345 set0
->getDef(0)->reg
.size
= 1;
1346 set1
->setSrc(2, set0
->getDef(0));
1348 set1
->setDef(0, logop
->getDef(0));
1349 delete_Instruction(prog
, logop
);
1353 // F2I(NEG(SET with result 1.0f/0.0f)) -> SET with result -1/0
1355 // F2I(NEG(I2F(ABS(SET))))
1357 AlgebraicOpt::handleCVT(Instruction
*cvt
)
1359 if (cvt
->sType
!= TYPE_F32
||
1360 cvt
->dType
!= TYPE_S32
|| cvt
->src(0).mod
!= Modifier(0))
1362 Instruction
*insn
= cvt
->getSrc(0)->getInsn();
1363 if (!insn
|| insn
->op
!= OP_NEG
|| insn
->dType
!= TYPE_F32
)
1365 if (insn
->src(0).mod
!= Modifier(0))
1367 insn
= insn
->getSrc(0)->getInsn();
1369 // check for nv50 SET(-1,0) -> SET(1.0f/0.0f) chain and nvc0's f32 SET
1370 if (insn
&& insn
->op
== OP_CVT
&&
1371 insn
->dType
== TYPE_F32
&&
1372 insn
->sType
== TYPE_S32
) {
1373 insn
= insn
->getSrc(0)->getInsn();
1374 if (!insn
|| insn
->op
!= OP_ABS
|| insn
->sType
!= TYPE_S32
||
1377 insn
= insn
->getSrc(0)->getInsn();
1378 if (!insn
|| insn
->op
!= OP_SET
|| insn
->dType
!= TYPE_U32
)
1381 if (!insn
|| insn
->op
!= OP_SET
|| insn
->dType
!= TYPE_F32
) {
1385 Instruction
*bset
= cloneShallow(func
, insn
);
1386 bset
->dType
= TYPE_U32
;
1387 bset
->setDef(0, cvt
->getDef(0));
1388 cvt
->bb
->insertAfter(cvt
, bset
);
1389 delete_Instruction(prog
, cvt
);
1392 // SUCLAMP dst, (ADD b imm), k, 0 -> SUCLAMP dst, b, k, imm (if imm fits s6)
1394 AlgebraicOpt::handleSUCLAMP(Instruction
*insn
)
1397 int32_t val
= insn
->getSrc(2)->asImm()->reg
.data
.s32
;
1401 assert(insn
->srcExists(0) && insn
->src(0).getFile() == FILE_GPR
);
1403 // look for ADD (TODO: only count references by non-SUCLAMP)
1404 if (insn
->getSrc(0)->refCount() > 1)
1406 add
= insn
->getSrc(0)->getInsn();
1407 if (!add
|| add
->op
!= OP_ADD
||
1408 (add
->dType
!= TYPE_U32
&&
1409 add
->dType
!= TYPE_S32
))
1412 // look for immediate
1413 for (s
= 0; s
< 2; ++s
)
1414 if (add
->src(s
).getImmediate(imm
))
1419 // determine if immediate fits
1420 val
+= imm
.reg
.data
.s32
;
1421 if (val
> 31 || val
< -32)
1423 // determine if other addend fits
1424 if (add
->src(s
).getFile() != FILE_GPR
|| add
->src(s
).mod
!= Modifier(0))
1427 bld
.setPosition(insn
, false); // make sure bld is init'ed
1429 insn
->setSrc(2, bld
.mkImm(val
));
1430 insn
->setSrc(0, add
->getSrc(s
));
1434 AlgebraicOpt::visit(BasicBlock
*bb
)
1437 for (Instruction
*i
= bb
->getEntry(); i
; i
= next
) {
1475 // =============================================================================
1478 updateLdStOffset(Instruction
*ldst
, int32_t offset
, Function
*fn
)
1480 if (offset
!= ldst
->getSrc(0)->reg
.data
.offset
) {
1481 if (ldst
->getSrc(0)->refCount() > 1)
1482 ldst
->setSrc(0, cloneShallow(fn
, ldst
->getSrc(0)));
1483 ldst
->getSrc(0)->reg
.data
.offset
= offset
;
1487 // Combine loads and stores, forward stores to loads where possible.
1488 class MemoryOpt
: public Pass
1496 const Value
*rel
[2];
1504 bool overlaps(const Instruction
*ldst
) const;
1506 inline void link(Record
**);
1507 inline void unlink(Record
**);
1508 inline void set(const Instruction
*ldst
);
1514 Record
*loads
[DATA_FILE_COUNT
];
1515 Record
*stores
[DATA_FILE_COUNT
];
1517 MemoryPool recordPool
;
1520 virtual bool visit(BasicBlock
*);
1521 bool runOpt(BasicBlock
*);
1523 Record
**getList(const Instruction
*);
1525 Record
*findRecord(const Instruction
*, bool load
, bool& isAdjacent
) const;
1527 // merge @insn into load/store instruction from @rec
1528 bool combineLd(Record
*rec
, Instruction
*ld
);
1529 bool combineSt(Record
*rec
, Instruction
*st
);
1531 bool replaceLdFromLd(Instruction
*ld
, Record
*ldRec
);
1532 bool replaceLdFromSt(Instruction
*ld
, Record
*stRec
);
1533 bool replaceStFromSt(Instruction
*restrict st
, Record
*stRec
);
1535 void addRecord(Instruction
*ldst
);
1536 void purgeRecords(Instruction
*const st
, DataFile
);
1537 void lockStores(Instruction
*const ld
);
1544 MemoryOpt::MemoryOpt() : recordPool(sizeof(MemoryOpt::Record
), 6)
1546 for (int i
= 0; i
< DATA_FILE_COUNT
; ++i
) {
1556 for (unsigned int i
= 0; i
< DATA_FILE_COUNT
; ++i
) {
1558 for (it
= loads
[i
]; it
; it
= next
) {
1560 recordPool
.release(it
);
1563 for (it
= stores
[i
]; it
; it
= next
) {
1565 recordPool
.release(it
);
1572 MemoryOpt::combineLd(Record
*rec
, Instruction
*ld
)
1574 int32_t offRc
= rec
->offset
;
1575 int32_t offLd
= ld
->getSrc(0)->reg
.data
.offset
;
1576 int sizeRc
= rec
->size
;
1577 int sizeLd
= typeSizeof(ld
->dType
);
1578 int size
= sizeRc
+ sizeLd
;
1581 if (!prog
->getTarget()->
1582 isAccessSupported(ld
->getSrc(0)->reg
.file
, typeOfSize(size
)))
1584 // no unaligned loads
1585 if (((size
== 0x8) && (MIN2(offLd
, offRc
) & 0x7)) ||
1586 ((size
== 0xc) && (MIN2(offLd
, offRc
) & 0xf)))
1589 assert(sizeRc
+ sizeLd
<= 16 && offRc
!= offLd
);
1591 for (j
= 0; sizeRc
; sizeRc
-= rec
->insn
->getDef(j
)->reg
.size
, ++j
);
1593 if (offLd
< offRc
) {
1595 for (sz
= 0, d
= 0; sz
< sizeLd
; sz
+= ld
->getDef(d
)->reg
.size
, ++d
);
1596 // d: nr of definitions in ld
1597 // j: nr of definitions in rec->insn, move:
1598 for (d
= d
+ j
- 1; j
> 0; --j
, --d
)
1599 rec
->insn
->setDef(d
, rec
->insn
->getDef(j
- 1));
1601 if (rec
->insn
->getSrc(0)->refCount() > 1)
1602 rec
->insn
->setSrc(0, cloneShallow(func
, rec
->insn
->getSrc(0)));
1603 rec
->offset
= rec
->insn
->getSrc(0)->reg
.data
.offset
= offLd
;
1609 // move definitions of @ld to @rec->insn
1610 for (j
= 0; sizeLd
; ++j
, ++d
) {
1611 sizeLd
-= ld
->getDef(j
)->reg
.size
;
1612 rec
->insn
->setDef(d
, ld
->getDef(j
));
1616 rec
->insn
->getSrc(0)->reg
.size
= size
;
1617 rec
->insn
->setType(typeOfSize(size
));
1619 delete_Instruction(prog
, ld
);
1625 MemoryOpt::combineSt(Record
*rec
, Instruction
*st
)
1627 int32_t offRc
= rec
->offset
;
1628 int32_t offSt
= st
->getSrc(0)->reg
.data
.offset
;
1629 int sizeRc
= rec
->size
;
1630 int sizeSt
= typeSizeof(st
->dType
);
1632 int size
= sizeRc
+ sizeSt
;
1634 Value
*src
[4]; // no modifiers in ValueRef allowed for st
1637 if (!prog
->getTarget()->
1638 isAccessSupported(st
->getSrc(0)->reg
.file
, typeOfSize(size
)))
1640 if (size
== 8 && MIN2(offRc
, offSt
) & 0x7)
1643 st
->takeExtraSources(0, extra
); // save predicate and indirect address
1645 if (offRc
< offSt
) {
1646 // save values from @st
1647 for (s
= 0; sizeSt
; ++s
) {
1648 sizeSt
-= st
->getSrc(s
+ 1)->reg
.size
;
1649 src
[s
] = st
->getSrc(s
+ 1);
1651 // set record's values as low sources of @st
1652 for (j
= 1; sizeRc
; ++j
) {
1653 sizeRc
-= rec
->insn
->getSrc(j
)->reg
.size
;
1654 st
->setSrc(j
, rec
->insn
->getSrc(j
));
1656 // set saved values as high sources of @st
1657 for (k
= j
, j
= 0; j
< s
; ++j
)
1658 st
->setSrc(k
++, src
[j
]);
1660 updateLdStOffset(st
, offRc
, func
);
1662 for (j
= 1; sizeSt
; ++j
)
1663 sizeSt
-= st
->getSrc(j
)->reg
.size
;
1664 for (s
= 1; sizeRc
; ++j
, ++s
) {
1665 sizeRc
-= rec
->insn
->getSrc(s
)->reg
.size
;
1666 st
->setSrc(j
, rec
->insn
->getSrc(s
));
1668 rec
->offset
= offSt
;
1670 st
->putExtraSources(0, extra
); // restore pointer and predicate
1672 delete_Instruction(prog
, rec
->insn
);
1675 rec
->insn
->getSrc(0)->reg
.size
= size
;
1676 rec
->insn
->setType(typeOfSize(size
));
1681 MemoryOpt::Record::set(const Instruction
*ldst
)
1683 const Symbol
*mem
= ldst
->getSrc(0)->asSym();
1684 fileIndex
= mem
->reg
.fileIndex
;
1685 rel
[0] = ldst
->getIndirect(0, 0);
1686 rel
[1] = ldst
->getIndirect(0, 1);
1687 offset
= mem
->reg
.data
.offset
;
1688 base
= mem
->getBase();
1689 size
= typeSizeof(ldst
->sType
);
1693 MemoryOpt::Record::link(Record
**list
)
1703 MemoryOpt::Record::unlink(Record
**list
)
1713 MemoryOpt::Record
**
1714 MemoryOpt::getList(const Instruction
*insn
)
1716 if (insn
->op
== OP_LOAD
|| insn
->op
== OP_VFETCH
)
1717 return &loads
[insn
->src(0).getFile()];
1718 return &stores
[insn
->src(0).getFile()];
1722 MemoryOpt::addRecord(Instruction
*i
)
1724 Record
**list
= getList(i
);
1725 Record
*it
= reinterpret_cast<Record
*>(recordPool
.allocate());
1734 MemoryOpt::findRecord(const Instruction
*insn
, bool load
, bool& isAdj
) const
1736 const Symbol
*sym
= insn
->getSrc(0)->asSym();
1737 const int size
= typeSizeof(insn
->sType
);
1739 Record
*it
= load
? loads
[sym
->reg
.file
] : stores
[sym
->reg
.file
];
1741 for (; it
; it
= it
->next
) {
1742 if (it
->locked
&& insn
->op
!= OP_LOAD
)
1744 if ((it
->offset
>> 4) != (sym
->reg
.data
.offset
>> 4) ||
1745 it
->rel
[0] != insn
->getIndirect(0, 0) ||
1746 it
->fileIndex
!= sym
->reg
.fileIndex
||
1747 it
->rel
[1] != insn
->getIndirect(0, 1))
1750 if (it
->offset
< sym
->reg
.data
.offset
) {
1751 if (it
->offset
+ it
->size
>= sym
->reg
.data
.offset
) {
1752 isAdj
= (it
->offset
+ it
->size
== sym
->reg
.data
.offset
);
1755 if (!(it
->offset
& 0x7))
1759 isAdj
= it
->offset
!= sym
->reg
.data
.offset
;
1760 if (size
<= it
->size
&& !isAdj
)
1763 if (!(sym
->reg
.data
.offset
& 0x7))
1764 if (it
->offset
- size
<= sym
->reg
.data
.offset
)
1772 MemoryOpt::replaceLdFromSt(Instruction
*ld
, Record
*rec
)
1774 Instruction
*st
= rec
->insn
;
1775 int32_t offSt
= rec
->offset
;
1776 int32_t offLd
= ld
->getSrc(0)->reg
.data
.offset
;
1779 for (s
= 1; offSt
!= offLd
&& st
->srcExists(s
); ++s
)
1780 offSt
+= st
->getSrc(s
)->reg
.size
;
1784 for (d
= 0; ld
->defExists(d
) && st
->srcExists(s
); ++d
, ++s
) {
1785 if (ld
->getDef(d
)->reg
.size
!= st
->getSrc(s
)->reg
.size
)
1787 if (st
->getSrc(s
)->reg
.file
!= FILE_GPR
)
1789 ld
->def(d
).replace(st
->src(s
), false);
1796 MemoryOpt::replaceLdFromLd(Instruction
*ldE
, Record
*rec
)
1798 Instruction
*ldR
= rec
->insn
;
1799 int32_t offR
= rec
->offset
;
1800 int32_t offE
= ldE
->getSrc(0)->reg
.data
.offset
;
1803 assert(offR
<= offE
);
1804 for (dR
= 0; offR
< offE
&& ldR
->defExists(dR
); ++dR
)
1805 offR
+= ldR
->getDef(dR
)->reg
.size
;
1809 for (dE
= 0; ldE
->defExists(dE
) && ldR
->defExists(dR
); ++dE
, ++dR
) {
1810 if (ldE
->getDef(dE
)->reg
.size
!= ldR
->getDef(dR
)->reg
.size
)
1812 ldE
->def(dE
).replace(ldR
->getDef(dR
), false);
1815 delete_Instruction(prog
, ldE
);
1820 MemoryOpt::replaceStFromSt(Instruction
*restrict st
, Record
*rec
)
1822 const Instruction
*const ri
= rec
->insn
;
1825 int32_t offS
= st
->getSrc(0)->reg
.data
.offset
;
1826 int32_t offR
= rec
->offset
;
1827 int32_t endS
= offS
+ typeSizeof(st
->dType
);
1828 int32_t endR
= offR
+ typeSizeof(ri
->dType
);
1830 rec
->size
= MAX2(endS
, endR
) - MIN2(offS
, offR
);
1832 st
->takeExtraSources(0, extra
);
1838 // get non-replaced sources of ri
1839 for (s
= 1; offR
< offS
; offR
+= ri
->getSrc(s
)->reg
.size
, ++s
)
1840 vals
[k
++] = ri
->getSrc(s
);
1842 // get replaced sources of st
1843 for (s
= 1; st
->srcExists(s
); offS
+= st
->getSrc(s
)->reg
.size
, ++s
)
1844 vals
[k
++] = st
->getSrc(s
);
1845 // skip replaced sources of ri
1846 for (s
= n
; offR
< endS
; offR
+= ri
->getSrc(s
)->reg
.size
, ++s
);
1847 // get non-replaced sources after values covered by st
1848 for (; offR
< endR
; offR
+= ri
->getSrc(s
)->reg
.size
, ++s
)
1849 vals
[k
++] = ri
->getSrc(s
);
1850 assert((unsigned int)k
<= Elements(vals
));
1851 for (s
= 0; s
< k
; ++s
)
1852 st
->setSrc(s
+ 1, vals
[s
]);
1853 st
->setSrc(0, ri
->getSrc(0));
1857 for (j
= 1; offR
< endS
; offR
+= ri
->getSrc(j
++)->reg
.size
);
1858 for (s
= 1; offS
< endS
; offS
+= st
->getSrc(s
++)->reg
.size
);
1859 for (; offR
< endR
; offR
+= ri
->getSrc(j
++)->reg
.size
)
1860 st
->setSrc(s
++, ri
->getSrc(j
));
1862 st
->putExtraSources(0, extra
);
1864 delete_Instruction(prog
, rec
->insn
);
1867 rec
->offset
= st
->getSrc(0)->reg
.data
.offset
;
1869 st
->setType(typeOfSize(rec
->size
));
1875 MemoryOpt::Record::overlaps(const Instruction
*ldst
) const
1880 if (this->fileIndex
!= that
.fileIndex
)
1883 if (this->rel
[0] || that
.rel
[0])
1884 return this->base
== that
.base
;
1886 (this->offset
< that
.offset
+ that
.size
) &&
1887 (this->offset
+ this->size
> that
.offset
);
1890 // We must not eliminate stores that affect the result of @ld if
1891 // we find later stores to the same location, and we may no longer
1892 // merge them with later stores.
1893 // The stored value can, however, still be used to determine the value
1894 // returned by future loads.
1896 MemoryOpt::lockStores(Instruction
*const ld
)
1898 for (Record
*r
= stores
[ld
->src(0).getFile()]; r
; r
= r
->next
)
1899 if (!r
->locked
&& r
->overlaps(ld
))
1903 // Prior loads from the location of @st are no longer valid.
1904 // Stores to the location of @st may no longer be used to derive
1905 // the value at it nor be coalesced into later stores.
1907 MemoryOpt::purgeRecords(Instruction
*const st
, DataFile f
)
1910 f
= st
->src(0).getFile();
1912 for (Record
*r
= loads
[f
]; r
; r
= r
->next
)
1913 if (!st
|| r
->overlaps(st
))
1914 r
->unlink(&loads
[f
]);
1916 for (Record
*r
= stores
[f
]; r
; r
= r
->next
)
1917 if (!st
|| r
->overlaps(st
))
1918 r
->unlink(&stores
[f
]);
1922 MemoryOpt::visit(BasicBlock
*bb
)
1924 bool ret
= runOpt(bb
);
1925 // Run again, one pass won't combine 4 32 bit ld/st to a single 128 bit ld/st
1926 // where 96 bit memory operations are forbidden.
1933 MemoryOpt::runOpt(BasicBlock
*bb
)
1935 Instruction
*ldst
, *next
;
1937 bool isAdjacent
= true;
1939 for (ldst
= bb
->getEntry(); ldst
; ldst
= next
) {
1944 if (ldst
->op
== OP_LOAD
|| ldst
->op
== OP_VFETCH
) {
1945 if (ldst
->isDead()) {
1946 // might have been produced by earlier optimization
1947 delete_Instruction(prog
, ldst
);
1951 if (ldst
->op
== OP_STORE
|| ldst
->op
== OP_EXPORT
) {
1954 // TODO: maybe have all fixed ops act as barrier ?
1955 if (ldst
->op
== OP_CALL
||
1956 ldst
->op
== OP_BAR
||
1957 ldst
->op
== OP_MEMBAR
) {
1958 purgeRecords(NULL
, FILE_MEMORY_LOCAL
);
1959 purgeRecords(NULL
, FILE_MEMORY_GLOBAL
);
1960 purgeRecords(NULL
, FILE_MEMORY_SHARED
);
1961 purgeRecords(NULL
, FILE_SHADER_OUTPUT
);
1963 if (ldst
->op
== OP_ATOM
|| ldst
->op
== OP_CCTL
) {
1964 if (ldst
->src(0).getFile() == FILE_MEMORY_GLOBAL
) {
1965 purgeRecords(NULL
, FILE_MEMORY_LOCAL
);
1966 purgeRecords(NULL
, FILE_MEMORY_GLOBAL
);
1967 purgeRecords(NULL
, FILE_MEMORY_SHARED
);
1969 purgeRecords(NULL
, ldst
->src(0).getFile());
1972 if (ldst
->op
== OP_EMIT
|| ldst
->op
== OP_RESTART
) {
1973 purgeRecords(NULL
, FILE_SHADER_OUTPUT
);
1977 if (ldst
->getPredicate()) // TODO: handle predicated ld/st
1981 DataFile file
= ldst
->src(0).getFile();
1983 // if ld l[]/g[] look for previous store to eliminate the reload
1984 if (file
== FILE_MEMORY_GLOBAL
|| file
== FILE_MEMORY_LOCAL
) {
1985 // TODO: shared memory ?
1986 rec
= findRecord(ldst
, false, isAdjacent
);
1987 if (rec
&& !isAdjacent
)
1988 keep
= !replaceLdFromSt(ldst
, rec
);
1991 // or look for ld from the same location and replace this one
1992 rec
= keep
? findRecord(ldst
, true, isAdjacent
) : NULL
;
1995 keep
= !replaceLdFromLd(ldst
, rec
);
1997 // or combine a previous load with this one
1998 keep
= !combineLd(rec
, ldst
);
2003 rec
= findRecord(ldst
, false, isAdjacent
);
2006 keep
= !replaceStFromSt(ldst
, rec
);
2008 keep
= !combineSt(rec
, ldst
);
2011 purgeRecords(ldst
, DATA_FILE_COUNT
);
2021 // =============================================================================
2023 // Turn control flow into predicated instructions (after register allocation !).
2025 // Could move this to before register allocation on NVC0 and also handle nested
2027 class FlatteningPass
: public Pass
2030 virtual bool visit(BasicBlock
*);
2032 bool tryPredicateConditional(BasicBlock
*);
2033 void predicateInstructions(BasicBlock
*, Value
*pred
, CondCode cc
);
2034 void tryPropagateBranch(BasicBlock
*);
2035 inline bool isConstantCondition(Value
*pred
);
2036 inline bool mayPredicate(const Instruction
*, const Value
*pred
) const;
2037 inline void removeFlow(Instruction
*);
2041 FlatteningPass::isConstantCondition(Value
*pred
)
2043 Instruction
*insn
= pred
->getUniqueInsn();
2045 if (insn
->op
!= OP_SET
|| insn
->srcExists(2))
2048 for (int s
= 0; s
< 2 && insn
->srcExists(s
); ++s
) {
2049 Instruction
*ld
= insn
->getSrc(s
)->getUniqueInsn();
2052 if (ld
->op
!= OP_MOV
&& ld
->op
!= OP_LOAD
)
2054 if (ld
->src(0).isIndirect(0))
2056 file
= ld
->src(0).getFile();
2058 file
= insn
->src(s
).getFile();
2059 // catch $r63 on NVC0
2060 if (file
== FILE_GPR
&& insn
->getSrc(s
)->reg
.data
.id
> prog
->maxGPR
)
2061 file
= FILE_IMMEDIATE
;
2063 if (file
!= FILE_IMMEDIATE
&& file
!= FILE_MEMORY_CONST
)
2070 FlatteningPass::removeFlow(Instruction
*insn
)
2072 FlowInstruction
*term
= insn
? insn
->asFlow() : NULL
;
2075 Graph::Edge::Type ty
= term
->bb
->cfg
.outgoing().getType();
2077 if (term
->op
== OP_BRA
) {
2078 // TODO: this might get more difficult when we get arbitrary BRAs
2079 if (ty
== Graph::Edge::CROSS
|| ty
== Graph::Edge::BACK
)
2082 if (term
->op
!= OP_JOIN
)
2085 Value
*pred
= term
->getPredicate();
2087 delete_Instruction(prog
, term
);
2089 if (pred
&& pred
->refCount() == 0) {
2090 Instruction
*pSet
= pred
->getUniqueInsn();
2091 pred
->join
->reg
.data
.id
= -1; // deallocate
2093 delete_Instruction(prog
, pSet
);
2098 FlatteningPass::predicateInstructions(BasicBlock
*bb
, Value
*pred
, CondCode cc
)
2100 for (Instruction
*i
= bb
->getEntry(); i
; i
= i
->next
) {
2103 assert(!i
->getPredicate());
2104 i
->setPredicate(cc
, pred
);
2106 removeFlow(bb
->getExit());
2110 FlatteningPass::mayPredicate(const Instruction
*insn
, const Value
*pred
) const
2112 if (insn
->isPseudo())
2114 // TODO: calls where we don't know which registers are modified
2116 if (!prog
->getTarget()->mayPredicate(insn
, pred
))
2118 for (int d
= 0; insn
->defExists(d
); ++d
)
2119 if (insn
->getDef(d
)->equals(pred
))
2124 // If we jump to BRA/RET/EXIT, replace the jump with it.
2125 // NOTE: We do not update the CFG anymore here !
2127 // TODO: Handle cases where we skip over a branch (maybe do that elsewhere ?):
2129 // @p0 bra BB:2 -> @!p0 bra BB:3 iff (!) BB:2 immediately adjoins BB:1
2137 FlatteningPass::tryPropagateBranch(BasicBlock
*bb
)
2139 for (Instruction
*i
= bb
->getExit(); i
&& i
->op
== OP_BRA
; i
= i
->prev
) {
2140 BasicBlock
*bf
= i
->asFlow()->target
.bb
;
2142 if (bf
->getInsnCount() != 1)
2145 FlowInstruction
*bra
= i
->asFlow();
2146 FlowInstruction
*rep
= bf
->getExit()->asFlow();
2148 if (!rep
|| rep
->getPredicate())
2150 if (rep
->op
!= OP_BRA
&&
2151 rep
->op
!= OP_JOIN
&&
2155 // TODO: If there are multiple branches to @rep, only the first would
2156 // be replaced, so only remove them after this pass is done ?
2157 // Also, need to check all incident blocks for fall-through exits and
2158 // add the branch there.
2160 bra
->target
.bb
= rep
->target
.bb
;
2161 if (bf
->cfg
.incidentCount() == 1)
2167 FlatteningPass::visit(BasicBlock
*bb
)
2169 if (tryPredicateConditional(bb
))
2172 // try to attach join to previous instruction
2173 if (prog
->getTarget()->hasJoin
) {
2174 Instruction
*insn
= bb
->getExit();
2175 if (insn
&& insn
->op
== OP_JOIN
&& !insn
->getPredicate()) {
2177 if (insn
&& !insn
->getPredicate() &&
2179 insn
->op
!= OP_TEXBAR
&&
2180 !isTextureOp(insn
->op
) && // probably just nve4
2181 !isSurfaceOp(insn
->op
) && // not confirmed
2182 insn
->op
!= OP_LINTERP
&& // probably just nve4
2183 insn
->op
!= OP_PINTERP
&& // probably just nve4
2184 ((insn
->op
!= OP_LOAD
&& insn
->op
!= OP_STORE
) ||
2185 typeSizeof(insn
->dType
) <= 4) &&
2188 bb
->remove(bb
->getExit());
2194 tryPropagateBranch(bb
);
2200 FlatteningPass::tryPredicateConditional(BasicBlock
*bb
)
2202 BasicBlock
*bL
= NULL
, *bR
= NULL
;
2203 unsigned int nL
= 0, nR
= 0, limit
= 12;
2207 mask
= bb
->initiatesSimpleConditional();
2211 assert(bb
->getExit());
2212 Value
*pred
= bb
->getExit()->getPredicate();
2215 if (isConstantCondition(pred
))
2218 Graph::EdgeIterator ei
= bb
->cfg
.outgoing();
2221 bL
= BasicBlock::get(ei
.getNode());
2222 for (insn
= bL
->getEntry(); insn
; insn
= insn
->next
, ++nL
)
2223 if (!mayPredicate(insn
, pred
))
2226 return false; // too long, do a real branch
2231 bR
= BasicBlock::get(ei
.getNode());
2232 for (insn
= bR
->getEntry(); insn
; insn
= insn
->next
, ++nR
)
2233 if (!mayPredicate(insn
, pred
))
2236 return false; // too long, do a real branch
2240 predicateInstructions(bL
, pred
, bb
->getExit()->cc
);
2242 predicateInstructions(bR
, pred
, inverseCondCode(bb
->getExit()->cc
));
2245 bb
->remove(bb
->joinAt
);
2248 removeFlow(bb
->getExit()); // delete the branch/join at the fork point
2250 // remove potential join operations at the end of the conditional
2251 if (prog
->getTarget()->joinAnterior
) {
2252 bb
= BasicBlock::get((bL
? bL
: bR
)->cfg
.outgoing().getNode());
2253 if (bb
->getEntry() && bb
->getEntry()->op
== OP_JOIN
)
2254 removeFlow(bb
->getEntry());
2260 // =============================================================================
2262 // Fold Immediate into MAD; must be done after register allocation due to
2263 // constraint SDST == SSRC2
2265 // Does NVC0+ have other situations where this pass makes sense?
2266 class NV50PostRaConstantFolding
: public Pass
2269 virtual bool visit(BasicBlock
*);
2273 NV50PostRaConstantFolding::visit(BasicBlock
*bb
)
2278 for (Instruction
*i
= bb
->getFirst(); i
; i
= i
->next
) {
2281 if (i
->def(0).getFile() != FILE_GPR
||
2282 i
->src(0).getFile() != FILE_GPR
||
2283 i
->src(1).getFile() != FILE_GPR
||
2284 i
->src(2).getFile() != FILE_GPR
||
2285 i
->getDef(0)->reg
.data
.id
!= i
->getSrc(2)->reg
.data
.id
)
2288 def
= i
->getSrc(1)->getInsn();
2289 if (def
->op
== OP_MOV
&& def
->src(0).getFile() == FILE_IMMEDIATE
) {
2290 vtmp
= i
->getSrc(1);
2291 i
->setSrc(1, def
->getSrc(0));
2293 /* There's no post-RA dead code elimination, so do it here
2294 * XXX: if we add more code-removing post-RA passes, we might
2295 * want to create a post-RA dead-code elim pass */
2296 if (vtmp
->refCount() == 0)
2297 delete_Instruction(bb
->getProgram(), def
);
2310 // =============================================================================
2312 // Common subexpression elimination. Stupid O^2 implementation.
2313 class LocalCSE
: public Pass
2316 virtual bool visit(BasicBlock
*);
2318 inline bool tryReplace(Instruction
**, Instruction
*);
2320 DLList ops
[OP_LAST
+ 1];
2323 class GlobalCSE
: public Pass
2326 virtual bool visit(BasicBlock
*);
2330 Instruction::isActionEqual(const Instruction
*that
) const
2332 if (this->op
!= that
->op
||
2333 this->dType
!= that
->dType
||
2334 this->sType
!= that
->sType
)
2336 if (this->cc
!= that
->cc
)
2339 if (this->asTex()) {
2340 if (memcmp(&this->asTex()->tex
,
2341 &that
->asTex()->tex
,
2342 sizeof(this->asTex()->tex
)))
2345 if (this->asCmp()) {
2346 if (this->asCmp()->setCond
!= that
->asCmp()->setCond
)
2349 if (this->asFlow()) {
2352 if (this->ipa
!= that
->ipa
||
2353 this->lanes
!= that
->lanes
||
2354 this->perPatch
!= that
->perPatch
)
2356 if (this->postFactor
!= that
->postFactor
)
2360 if (this->subOp
!= that
->subOp
||
2361 this->saturate
!= that
->saturate
||
2362 this->rnd
!= that
->rnd
||
2363 this->ftz
!= that
->ftz
||
2364 this->dnz
!= that
->dnz
||
2365 this->cache
!= that
->cache
||
2366 this->mask
!= that
->mask
)
2373 Instruction::isResultEqual(const Instruction
*that
) const
2377 // NOTE: location of discard only affects tex with liveOnly and quadops
2378 if (!this->defExists(0) && this->op
!= OP_DISCARD
)
2381 if (!isActionEqual(that
))
2384 if (this->predSrc
!= that
->predSrc
)
2387 for (d
= 0; this->defExists(d
); ++d
) {
2388 if (!that
->defExists(d
) ||
2389 !this->getDef(d
)->equals(that
->getDef(d
), false))
2392 if (that
->defExists(d
))
2395 for (s
= 0; this->srcExists(s
); ++s
) {
2396 if (!that
->srcExists(s
))
2398 if (this->src(s
).mod
!= that
->src(s
).mod
)
2400 if (!this->getSrc(s
)->equals(that
->getSrc(s
), true))
2403 if (that
->srcExists(s
))
2406 if (op
== OP_LOAD
|| op
== OP_VFETCH
) {
2407 switch (src(0).getFile()) {
2408 case FILE_MEMORY_CONST
:
2409 case FILE_SHADER_INPUT
:
2419 // pull through common expressions from different in-blocks
2421 GlobalCSE::visit(BasicBlock
*bb
)
2423 Instruction
*phi
, *next
, *ik
;
2426 // TODO: maybe do this with OP_UNION, too
2428 for (phi
= bb
->getPhi(); phi
&& phi
->op
== OP_PHI
; phi
= next
) {
2430 if (phi
->getSrc(0)->refCount() > 1)
2432 ik
= phi
->getSrc(0)->getInsn();
2434 continue; // probably a function input
2435 for (s
= 1; phi
->srcExists(s
); ++s
) {
2436 if (phi
->getSrc(s
)->refCount() > 1)
2438 if (!phi
->getSrc(s
)->getInsn() ||
2439 !phi
->getSrc(s
)->getInsn()->isResultEqual(ik
))
2442 if (!phi
->srcExists(s
)) {
2443 Instruction
*entry
= bb
->getEntry();
2445 if (!entry
|| entry
->op
!= OP_JOIN
)
2448 bb
->insertAfter(entry
, ik
);
2449 ik
->setDef(0, phi
->getDef(0));
2450 delete_Instruction(prog
, phi
);
2458 LocalCSE::tryReplace(Instruction
**ptr
, Instruction
*i
)
2460 Instruction
*old
= *ptr
;
2462 // TODO: maybe relax this later (causes trouble with OP_UNION)
2463 if (i
->isPredicated())
2466 if (!old
->isResultEqual(i
))
2469 for (int d
= 0; old
->defExists(d
); ++d
)
2470 old
->def(d
).replace(i
->getDef(d
), false);
2471 delete_Instruction(prog
, old
);
2477 LocalCSE::visit(BasicBlock
*bb
)
2479 unsigned int replaced
;
2482 Instruction
*ir
, *next
;
2486 // will need to know the order of instructions
2488 for (ir
= bb
->getFirst(); ir
; ir
= ir
->next
)
2489 ir
->serial
= serial
++;
2491 for (ir
= bb
->getEntry(); ir
; ir
= next
) {
2498 ops
[ir
->op
].insert(ir
);
2502 for (s
= 0; ir
->srcExists(s
); ++s
)
2503 if (ir
->getSrc(s
)->asLValue())
2504 if (!src
|| ir
->getSrc(s
)->refCount() < src
->refCount())
2505 src
= ir
->getSrc(s
);
2508 for (Value::UseIterator it
= src
->uses
.begin();
2509 it
!= src
->uses
.end(); ++it
) {
2510 Instruction
*ik
= (*it
)->getInsn();
2511 if (ik
&& ik
->bb
== ir
->bb
&& ik
->serial
< ir
->serial
)
2512 if (tryReplace(&ir
, ik
))
2516 DLLIST_FOR_EACH(&ops
[ir
->op
], iter
)
2518 Instruction
*ik
= reinterpret_cast<Instruction
*>(iter
.get());
2519 if (tryReplace(&ir
, ik
))
2525 ops
[ir
->op
].insert(ir
);
2529 for (unsigned int i
= 0; i
<= OP_LAST
; ++i
)
2537 // =============================================================================
2539 // Remove computations of unused values.
2540 class DeadCodeElim
: public Pass
2543 bool buryAll(Program
*);
2546 virtual bool visit(BasicBlock
*);
2548 void checkSplitLoad(Instruction
*ld
); // for partially dead loads
2550 unsigned int deadCount
;
2554 DeadCodeElim::buryAll(Program
*prog
)
2558 if (!this->run(prog
, false, false))
2560 } while (deadCount
);
2566 DeadCodeElim::visit(BasicBlock
*bb
)
2570 for (Instruction
*i
= bb
->getFirst(); i
; i
= next
) {
2574 delete_Instruction(prog
, i
);
2576 if (i
->defExists(1) && (i
->op
== OP_VFETCH
|| i
->op
== OP_LOAD
)) {
2579 if (i
->defExists(0) && !i
->getDef(0)->refCount()) {
2580 if (i
->op
== OP_ATOM
||
2581 i
->op
== OP_SUREDP
||
2590 DeadCodeElim::checkSplitLoad(Instruction
*ld1
)
2592 Instruction
*ld2
= NULL
; // can get at most 2 loads
2595 int32_t addr1
, addr2
;
2596 int32_t size1
, size2
;
2598 uint32_t mask
= 0xffffffff;
2600 for (d
= 0; ld1
->defExists(d
); ++d
)
2601 if (!ld1
->getDef(d
)->refCount() && ld1
->getDef(d
)->reg
.data
.id
< 0)
2603 if (mask
== 0xffffffff)
2606 addr1
= ld1
->getSrc(0)->reg
.data
.offset
;
2609 for (d
= 0; ld1
->defExists(d
); ++d
) {
2610 if (mask
& (1 << d
)) {
2611 if (size1
&& (addr1
& 0x7))
2613 def1
[n1
] = ld1
->getDef(d
);
2614 size1
+= def1
[n1
++]->reg
.size
;
2617 addr1
+= ld1
->getDef(d
)->reg
.size
;
2622 for (addr2
= addr1
+ size1
; ld1
->defExists(d
); ++d
) {
2623 if (mask
& (1 << d
)) {
2624 def2
[n2
] = ld1
->getDef(d
);
2625 size2
+= def2
[n2
++]->reg
.size
;
2628 addr2
+= ld1
->getDef(d
)->reg
.size
;
2632 updateLdStOffset(ld1
, addr1
, func
);
2633 ld1
->setType(typeOfSize(size1
));
2634 for (d
= 0; d
< 4; ++d
)
2635 ld1
->setDef(d
, (d
< n1
) ? def1
[d
] : NULL
);
2640 ld2
= cloneShallow(func
, ld1
);
2641 updateLdStOffset(ld2
, addr2
, func
);
2642 ld2
->setType(typeOfSize(size2
));
2643 for (d
= 0; d
< 4; ++d
)
2644 ld2
->setDef(d
, (d
< n2
) ? def2
[d
] : NULL
);
2646 ld1
->bb
->insertAfter(ld1
, ld2
);
2649 // =============================================================================
2651 #define RUN_PASS(l, n, f) \
2652 if (level >= (l)) { \
2653 if (dbgFlags & NV50_IR_DEBUG_VERBOSE) \
2654 INFO("PEEPHOLE: %s\n", #n); \
2656 if (!pass.f(this)) \
2661 Program::optimizeSSA(int level
)
2663 RUN_PASS(1, DeadCodeElim
, buryAll
);
2664 RUN_PASS(1, CopyPropagation
, run
);
2665 RUN_PASS(2, GlobalCSE
, run
);
2666 RUN_PASS(1, LocalCSE
, run
);
2667 RUN_PASS(2, AlgebraicOpt
, run
);
2668 RUN_PASS(2, ModifierFolding
, run
); // before load propagation -> less checks
2669 RUN_PASS(1, ConstantFolding
, foldAll
);
2670 RUN_PASS(1, LoadPropagation
, run
);
2671 RUN_PASS(2, MemoryOpt
, run
);
2672 RUN_PASS(2, LocalCSE
, run
);
2673 RUN_PASS(0, DeadCodeElim
, buryAll
);
2679 Program::optimizePostRA(int level
)
2681 RUN_PASS(2, FlatteningPass
, run
);
2682 if (getTarget()->getChipset() < 0xc0)
2683 RUN_PASS(2, NV50PostRaConstantFolding
, run
);