summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2016-10-07 17:53:07 +0000
committerSanjay Patel <spatel@rotateright.com>2016-10-07 17:53:07 +0000
commit4326c4ac8f72658ccd378d5abd569cc7004d3a9a (patch)
tree6ecd11a69cd8c7da1ccdb2334a99718e43a9e54e /llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
parent9913583e06240ef1e6dc2576770a34f2abc5d97e (diff)
downloadbcm5719-llvm-4326c4ac8f72658ccd378d5abd569cc7004d3a9a.tar.gz
bcm5719-llvm-4326c4ac8f72658ccd378d5abd569cc7004d3a9a.zip
[InstCombine] fold select X, (ext X), C
If we're going to canonicalize IR towards select of constants, try harder to create those. Also, don't lose the metadata. This is actually 4 related transforms in one patch: // select X, (sext X), C --> select X, -1, C // select X, (zext X), C --> select X, 1, C // select X, C, (sext X) --> select X, C, 0 // select X, C, (zext X) --> select X, C, 0 Differential Revision: https://reviews.llvm.org/D25126 llvm-svn: 283575
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp22
1 files changed, 21 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index b8213426a63..042b3731a90 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -939,11 +939,11 @@ Instruction *InstCombiner::foldSelectExtConst(SelectInst &Sel) {
// If the constant is the same after truncation to the smaller type and
// extension to the original type, we can narrow the select.
+ Value *Cond = Sel.getCondition();
Type *SelType = Sel.getType();
Constant *TruncC = ConstantExpr::getTrunc(C, SmallType);
Constant *ExtC = ConstantExpr::getCast(ExtOpcode, TruncC, SelType);
if (ExtC == C) {
- Value *Cond = Sel.getCondition();
Value *TruncCVal = cast<Value>(TruncC);
if (ExtInst == Sel.getFalseValue())
std::swap(X, TruncCVal);
@@ -954,6 +954,26 @@ Instruction *InstCombiner::foldSelectExtConst(SelectInst &Sel) {
return CastInst::Create(Instruction::CastOps(ExtOpcode), NewSel, SelType);
}
+ // If one arm of the select is the extend of the condition, replace that arm
+ // with the extension of the appropriate known bool value.
+ if (Cond == X) {
+ SelectInst *NewSel;
+ if (ExtInst == Sel.getTrueValue()) {
+ // select X, (sext X), C --> select X, -1, C
+ // select X, (zext X), C --> select X, 1, C
+ Constant *One = ConstantInt::getTrue(SmallType);
+ Constant *AllOnesOrOne = ConstantExpr::getCast(ExtOpcode, One, SelType);
+ NewSel = SelectInst::Create(Cond, AllOnesOrOne, C);
+ } else {
+ // select X, C, (sext X) --> select X, C, 0
+ // select X, C, (zext X) --> select X, C, 0
+ Constant *Zero = ConstantInt::getNullValue(SelType);
+ NewSel = SelectInst::Create(Cond, C, Zero);
+ }
+ NewSel->copyMetadata(Sel);
+ return NewSel;
+ }
+
return nullptr;
}
OpenPOWER on IntegriCloud