diff options
author | Sanjay Patel <spatel@rotateright.com> | 2016-11-07 15:52:45 +0000 |
---|---|---|
committer | Sanjay Patel <spatel@rotateright.com> | 2016-11-07 15:52:45 +0000 |
commit | 86408a80486971d41d94a9959baf5080356cbae3 (patch) | |
tree | e2a813974143801bee8ad05b7e5d5cf9a308a8f7 /llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | |
parent | 4f0509fab3c934a2f31ad5b464a75b3029cb2c12 (diff) | |
download | bcm5719-llvm-86408a80486971d41d94a9959baf5080356cbae3.tar.gz bcm5719-llvm-86408a80486971d41d94a9959baf5080356cbae3.zip |
[InstCombine] allow splat vector folds in adjustMinMax() (retry r285732)
This was reverted at r285866 because there was a crash handling a scalar
select of vectors. I added a check for that pattern and a test case based
on the example provided in the post-commit thread for r285732.
llvm-svn: 286113
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index cfae47fbd7e..6121b80f23b 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -424,24 +424,22 @@ static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) { Value *FalseVal = Sel.getFalseValue(); // We may move or edit the compare, so make sure the select is the only user. - if (!Cmp.hasOneUse()) + const APInt *CmpC; + if (!Cmp.hasOneUse() || !match(CmpRHS, m_APInt(CmpC))) return false; - // FIXME: Use m_APInt to allow vector folds. - auto *CI = dyn_cast<ConstantInt>(CmpRHS); - if (!CI) - return false; - - // These transformations only work for selects over integers. - IntegerType *SelectTy = dyn_cast<IntegerType>(Sel.getType()); - if (!SelectTy) + // These transforms only work for selects of integers or vector selects of + // integer vectors. + Type *SelTy = Sel.getType(); + auto *SelEltTy = dyn_cast<IntegerType>(SelTy->getScalarType()); + if (!SelEltTy || SelTy->isVectorTy() != Cmp.getType()->isVectorTy()) return false; Constant *AdjustedRHS; if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT) - AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() + 1); + AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC + 1); else if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) - AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() - 1); + AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC - 1); else return false; @@ -454,8 +452,8 @@ static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) { // Types do not match. Instead of calculating this with mixed types, promote // all to the larger type. This enables scalar evolution to analyze this // expression. - else if (CmpRHS->getType()->getScalarSizeInBits() < SelectTy->getBitWidth()) { - Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, SelectTy); + else if (CmpRHS->getType()->getScalarSizeInBits() < SelEltTy->getBitWidth()) { + Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, SelTy); // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X @@ -469,7 +467,7 @@ static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) { CmpLHS = FalseVal; AdjustedRHS = SextRHS; } else if (Cmp.isUnsigned()) { - Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, SelectTy); + Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, SelTy); // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X // zext + signed compare cannot be changed: |