@@ -448,6 +448,21 @@ void GenXDeadVectorRemoval::processRdRegion(Instruction *Inst, LiveBits LB)
448448 addToWorkList (InInst);
449449}
450450
451+ static Constant *undefDeadConstElements (Constant *C, LiveBits LB) {
452+ if (isa<UndefValue>(C) || isa<ConstantAggregateZero>(C))
453+ return C;
454+ if (!C->getType ()->isVectorTy ()) {
455+ IGC_ASSERT (LB.getNumElements () == 1 );
456+ return LB.get (0 ) ? C : UndefValue::get (C->getType ());
457+ }
458+ SmallVector<Constant *, 8 > NewElems;
459+ for (unsigned i = 0 ; i < LB.getNumElements (); ++i)
460+ NewElems.push_back (LB.get (i)
461+ ? C->getAggregateElement (i)
462+ : UndefValue::get (C->getType ()->getScalarType ()));
463+ return ConstantVector::get (NewElems);
464+ }
465+
451466/* **********************************************************************
452467 * processWrRegion : process a wrregion instruction for element liveness
453468 */
@@ -533,23 +548,9 @@ void GenXDeadVectorRemoval::processWrRegion(Instruction *Inst, LiveBits LB)
533548 addToWorkList (OldInInst);
534549 // If some constant values are not in use, set it to undef so ConstantLoader
535550 // can benefit from it.
536- else if (auto OldInConst = dyn_cast<Constant>(OldInVal)) {
537- Constant *NewInConst = nullptr ;
538- if (isa<UndefValue>(OldInConst))
539- NewInConst = UndefValue::get (OldInConst->getType ());
540- else if (isa<ConstantAggregateZero>(OldInConst))
541- NewInConst = ConstantAggregateZero::get (OldInConst->getType ());
542- else {
543- SmallVector<Constant *, 8 > NewElems;
544- for (unsigned i = 0 ; i < OldInLB.getNumElements (); ++i)
545- NewElems.push_back (OldInLB.get (i) ?
546- OldInConst->getAggregateElement (i) :
547- UndefValue::get (OldInConst->getType ()->getScalarType ()));
548- NewInConst = ConstantVector::get (NewElems);
549- }
550- IGC_ASSERT (NewInConst);
551- Inst->setOperand (GenXIntrinsic::GenXRegion::OldValueOperandNum, NewInConst);
552- }
551+ else if (auto OldInConst = dyn_cast<Constant>(OldInVal))
552+ Inst->setOperand (GenXIntrinsic::GenXRegion::OldValueOperandNum,
553+ undefDeadConstElements (OldInConst, OldInLB));
553554 }
554555 if (UsedOldInput) {
555556 // We know that at least one element of the "old value" input is used,
@@ -563,10 +564,20 @@ void GenXDeadVectorRemoval::processWrRegion(Instruction *Inst, LiveBits LB)
563564 */
564565void GenXDeadVectorRemoval::processBitCast (Instruction *Inst, LiveBits LB)
565566{
566- auto InInst = dyn_cast<Instruction>(Inst->getOperand (0 ));
567- if (!InInst)
567+ LiveBits InLB;
568+ LiveBitsStorage ConstVecLBS;
569+ auto InVal = Inst->getOperand (0 );
570+ if (auto InInst = dyn_cast<Instruction>(InVal))
571+ InLB = createLiveBits (InInst);
572+ else if (isa<Constant>(InVal)) {
573+ unsigned NumElems =
574+ isa<VectorType>(InVal->getType ())
575+ ? cast<VectorType>(InVal->getType ())->getNumElements ()
576+ : 1 ;
577+ ConstVecLBS.setNumElements (NumElems);
578+ InLB = LiveBits (&ConstVecLBS, NumElems);
579+ } else
568580 return ;
569- LiveBits InLB = createLiveBits (InInst);
570581 bool Modified = false ;
571582 if (InLB.getNumElements () == LB.getNumElements ())
572583 Modified = InLB.orBits (LB);
@@ -589,8 +600,12 @@ void GenXDeadVectorRemoval::processBitCast(Instruction *Inst, LiveBits LB)
589600 Modified |= InLB.set (Idx);
590601 }
591602 }
592- if (Modified)
593- addToWorkList (InInst);
603+ if (Modified) {
604+ if (auto InInst = dyn_cast<Instruction>(InVal))
605+ addToWorkList (InInst);
606+ else if (auto InConst = dyn_cast<Constant>(InVal))
607+ Inst->setOperand (0 , undefDeadConstElements (InConst, InLB));
608+ }
594609}
595610
596611/* **********************************************************************
0 commit comments