Skip to content

Commit 6a1fb8f

Browse files
committed
Fix a bug in r123034 (trying to sext/zext non-integers) and clean up a little.
llvm-svn: 123061
1 parent 8c5defd commit 6a1fb8f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
299299
case ICmpInst::ICMP_SLT:
300300
case ICmpInst::ICMP_UGT:
301301
case ICmpInst::ICMP_SGT: {
302+
// These transformations only work for selects over integers.
303+
const IntegerType *SelectTy = dyn_cast<IntegerType>(SI.getType());
304+
if (!SelectTy)
305+
break;
306+
302307
Constant *AdjustedRHS;
303308
if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT)
304309
AdjustedRHS = ConstantInt::get(CI->getContext(), CI->getValue() + 1);
@@ -315,9 +320,8 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
315320
// promote all to the larger type. This enables scalar evolution to
316321
// analyze this expression.
317322
else if (CmpRHS->getType()->getScalarSizeInBits()
318-
< SI.getType()->getScalarSizeInBits()) {
319-
Constant *sextRHS = ConstantExpr::getSExt(AdjustedRHS,
320-
SI.getType());
323+
< SelectTy->getBitWidth()) {
324+
Constant *sextRHS = ConstantExpr::getSExt(AdjustedRHS, SelectTy);
321325

322326
// X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X
323327
// X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X
@@ -332,8 +336,7 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI,
332336
CmpLHS = FalseVal;
333337
AdjustedRHS = sextRHS;
334338
} else if (ICI->isUnsigned()) {
335-
Constant *zextRHS = ConstantExpr::getZExt(AdjustedRHS,
336-
SI.getType());
339+
Constant *zextRHS = ConstantExpr::getZExt(AdjustedRHS, SelectTy);
337340
// X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X
338341
// X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X
339342
// zext + signed compare cannot be changed:

llvm/test/Transforms/InstCombine/crash.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,9 @@ ret void
335335

336336
declare i32 @func_14()
337337

338+
339+
define double @test16(i32 %a) nounwind {
340+
%cmp = icmp slt i32 %a, 2
341+
%select = select i1 %cmp, double 2.000000e+00, double 3.141592e+00
342+
ret double %select
343+
}

0 commit comments

Comments
 (0)