Skip to content

Commit b35f294

Browse files
committed
[InstSimplify] Avoid use of ConstantExpr::getCast()
Use the constant folding API instead. One of these uses actually improves results, because the bitcast expression gets folded away.
1 parent b4afade commit b35f294

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,7 +2019,8 @@ static Value *simplifyAndOrOfCmps(const SimplifyQuery &Q, Value *Op0,
20192019
// If we looked through casts, we can only handle a constant simplification
20202020
// because we are not allowed to create a cast instruction here.
20212021
if (auto *C = dyn_cast<Constant>(V))
2022-
return ConstantExpr::getCast(Cast0->getOpcode(), C, Cast0->getType());
2022+
return ConstantFoldCastOperand(Cast0->getOpcode(), C, Cast0->getType(),
2023+
Q.DL);
20232024

20242025
return nullptr;
20252026
}
@@ -3876,9 +3877,15 @@ static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
38763877

38773878
// Compute the constant that would happen if we truncated to SrcTy then
38783879
// reextended to DstTy.
3879-
Constant *Trunc = ConstantExpr::getTrunc(C, SrcTy);
3880-
Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy);
3881-
Constant *AnyEq = ConstantExpr::getICmp(ICmpInst::ICMP_EQ, RExt, C);
3880+
Constant *Trunc =
3881+
ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3882+
assert(Trunc && "Constant-fold of ImmConstant should not fail");
3883+
Constant *RExt =
3884+
ConstantFoldCastOperand(CastInst::ZExt, Trunc, DstTy, Q.DL);
3885+
assert(RExt && "Constant-fold of ImmConstant should not fail");
3886+
Constant *AnyEq =
3887+
ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL);
3888+
assert(AnyEq && "Constant-fold of ImmConstant should not fail");
38823889

38833890
// If the re-extended constant didn't change any of the elements then
38843891
// this is effectively also a case of comparing two zero-extended
@@ -3947,9 +3954,15 @@ static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
39473954

39483955
// Compute the constant that would happen if we truncated to SrcTy then
39493956
// reextended to DstTy.
3950-
Constant *Trunc = ConstantExpr::getTrunc(C, SrcTy);
3951-
Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy);
3952-
Constant *AnyEq = ConstantExpr::getICmp(ICmpInst::ICMP_EQ, RExt, C);
3957+
Constant *Trunc =
3958+
ConstantFoldCastOperand(Instruction::Trunc, C, SrcTy, Q.DL);
3959+
assert(Trunc && "Constant-fold of ImmConstant should not fail");
3960+
Constant *RExt =
3961+
ConstantFoldCastOperand(CastInst::SExt, Trunc, DstTy, Q.DL);
3962+
assert(RExt && "Constant-fold of ImmConstant should not fail");
3963+
Constant *AnyEq =
3964+
ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ, RExt, C, Q.DL);
3965+
assert(AnyEq && "Constant-fold of ImmConstant should not fail");
39533966

39543967
// If the re-extended constant didn't change then this is effectively
39553968
// also a case of comparing two sign-extended values.

llvm/test/Transforms/InstSimplify/AndOrXor.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ define i32 @or_of_zexted_icmps(i32 %i) {
523523

524524
define i3 @or_of_bitcast_icmps_vec(<3 x i65> %i) {
525525
; CHECK-LABEL: @or_of_bitcast_icmps_vec(
526-
; CHECK-NEXT: ret i3 bitcast (<3 x i1> <i1 true, i1 true, i1 true> to i3)
526+
; CHECK-NEXT: ret i3 -1
527527
;
528528
%cmp0 = icmp sge <3 x i65> %i, zeroinitializer
529529
%conv0 = bitcast <3 x i1> %cmp0 to i3

0 commit comments

Comments
 (0)