Simplify demanded bits of select sources where the condition is a constant vector

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@160835 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Pete Cooper
2012-07-26 23:10:24 +00:00
parent 3ba90d9c0e
commit 7971de4178
3 changed files with 53 additions and 0 deletions

View File

@@ -899,5 +899,16 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
return &SI;
}
if (VectorType* VecTy = dyn_cast<VectorType>(SI.getType())) {
unsigned VWidth = VecTy->getNumElements();
APInt UndefElts(VWidth, 0);
APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) {
if (V != &SI)
return ReplaceInstUsesWith(SI, V);
return &SI;
}
}
return 0;
}

View File

@@ -989,6 +989,29 @@ Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
}
break;
}
case Instruction::Select: {
APInt LeftDemanded(DemandedElts), RightDemanded(DemandedElts);
if (ConstantVector* CV = dyn_cast<ConstantVector>(I->getOperand(0))) {
for (unsigned i = 0; i < VWidth; i++) {
if (CV->getAggregateElement(i)->isNullValue())
LeftDemanded.clearBit(i);
else
RightDemanded.clearBit(i);
}
}
TmpV = SimplifyDemandedVectorElts(I->getOperand(1), LeftDemanded,
UndefElts, Depth+1);
if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; }
TmpV = SimplifyDemandedVectorElts(I->getOperand(2), RightDemanded,
UndefElts2, Depth+1);
if (TmpV) { I->setOperand(2, TmpV); MadeChange = true; }
// Output elements are undefined if both are undefined.
UndefElts &= UndefElts2;
break;
}
case Instruction::BitCast: {
// Vector->vector casts only.
VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType());