Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions llvm/lib/IR/ConstantFold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,36 +198,35 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
bool ignored;
APFloat Val = FPC->getValueAPF();
Val.convert(DestTy->getFltSemantics(), APFloat::rmNearestTiesToEven,
&ignored);
return ConstantFP::get(V->getContext(), Val);
Val.convert(DestTy->getScalarType()->getFltSemantics(),
APFloat::rmNearestTiesToEven, &ignored);
return ConstantFP::get(DestTy, Val);
}
return nullptr; // Can't fold.
case Instruction::FPToUI:
case Instruction::FPToSI:
if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
const APFloat &V = FPC->getValueAPF();
bool ignored;
uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
APSInt IntVal(DestBitWidth, opc == Instruction::FPToUI);
APSInt IntVal(DestTy->getScalarSizeInBits(), opc == Instruction::FPToUI);
if (APFloat::opInvalidOp ==
V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) {
// Undefined behavior invoked - the destination type can't represent
// the input constant.
return PoisonValue::get(DestTy);
}
return ConstantInt::get(FPC->getContext(), IntVal);
return ConstantInt::get(DestTy, IntVal);
}
return nullptr; // Can't fold.
case Instruction::UIToFP:
case Instruction::SIToFP:
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
const APInt &api = CI->getValue();
APFloat apf(DestTy->getFltSemantics(),
APInt::getZero(DestTy->getPrimitiveSizeInBits()));
APFloat apf(DestTy->getScalarType()->getFltSemantics(),
APInt::getZero(DestTy->getScalarSizeInBits()));
apf.convertFromAPInt(api, opc==Instruction::SIToFP,
APFloat::rmNearestTiesToEven);
return ConstantFP::get(V->getContext(), apf);
return ConstantFP::get(DestTy, apf);
}
return nullptr;
case Instruction::ZExt:
Expand Down Expand Up @@ -573,7 +572,7 @@ Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) {
default:
break;
case Instruction::FNeg:
return ConstantFP::get(C->getContext(), neg(CV));
return ConstantFP::get(C->getType(), neg(CV));
}
} else if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
// Fast path for splatted constants.
Expand Down Expand Up @@ -857,19 +856,19 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
break;
case Instruction::FAdd:
(void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
return ConstantFP::get(C1->getContext(), C3V);
return ConstantFP::get(C1->getType(), C3V);
case Instruction::FSub:
(void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
return ConstantFP::get(C1->getContext(), C3V);
return ConstantFP::get(C1->getType(), C3V);
case Instruction::FMul:
(void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
return ConstantFP::get(C1->getContext(), C3V);
return ConstantFP::get(C1->getType(), C3V);
case Instruction::FDiv:
(void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
return ConstantFP::get(C1->getContext(), C3V);
return ConstantFP::get(C1->getType(), C3V);
case Instruction::FRem:
(void)C3V.mod(C2V);
return ConstantFP::get(C1->getContext(), C3V);
return ConstantFP::get(C1->getType(), C3V);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/IR/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ bool Constant::containsUndefElement() const {
}

bool Constant::containsConstantExpression() const {
if (isa<ConstantInt>(this) || isa<ConstantFP>(this))
return false;

if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
if (isa<ConstantExpr>(getAggregateElement(i)))
Expand Down
1 change: 1 addition & 0 deletions llvm/test/Transforms/InstCombine/clamp-to-minmax.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
; RUN: opt < %s -passes=instcombine -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat -S | FileCheck %s

; (X < C1) ? C1 : MIN(X, C2)
define float @clamp_float_fast_ordered_strict_maxmin(float %x) {
Expand Down
1 change: 1 addition & 0 deletions llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=instcombine %s | FileCheck %s
; RUN: opt -S -passes=instcombine -use-constant-fp-for-fixed-length-splat %s | FileCheck %s

define float @fabs_fneg_basic(float %x) {
; CHECK-LABEL: define float @fabs_fneg_basic(
Expand Down
1 change: 1 addition & 0 deletions llvm/test/Transforms/InstCombine/fadd.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
; RUN: opt < %s -passes=instcombine -use-constant-fp-for-fixed-length-splat -S | FileCheck %s

declare void @use(float)
declare void @use_vec(<2 x float>)
Expand Down
1 change: 1 addition & 0 deletions llvm/test/Transforms/InstCombine/fdiv.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -passes=instcombine < %s | FileCheck %s
; RUN: opt -S -passes=instcombine -use-constant-fp-for-fixed-length-splat < %s | FileCheck %s

declare float @llvm.fabs.f32(float) nounwind readnone
declare float @llvm.pow.f32(float, float) nounwind readnone
Expand Down
1 change: 1 addition & 0 deletions llvm/test/Transforms/InstCombine/fmul.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -passes=instcombine < %s | FileCheck %s
; RUN: opt -S -passes=instcombine -use-constant-fp-for-fixed-length-splat < %s | FileCheck %s

; (-0.0 - X) * C => X * -C
define float @neg_constant(float %x) {
Expand Down
1 change: 1 addition & 0 deletions llvm/test/Transforms/InstCombine/fneg.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
; RUN: opt < %s -passes=instcombine -use-constant-fp-for-fixed-length-splat -S | FileCheck %s

declare float @llvm.ldexp.f32.i32(float, i32)
declare <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float>, <2 x i32>)
Expand Down
Loading