Skip to content

Commit 02328e0

Browse files
[LLVM][ConstantFold] Remove remaining uses of ConstantInt/FP::get(LLVMContext... (#119912)
This extends the constant folds to support vector ConstantInt/FP.
1 parent 3ad2399 commit 02328e0

File tree

8 files changed

+23
-15
lines changed

8 files changed

+23
-15
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,36 +198,35 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
198198
if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
199199
bool ignored;
200200
APFloat Val = FPC->getValueAPF();
201-
Val.convert(DestTy->getFltSemantics(), APFloat::rmNearestTiesToEven,
202-
&ignored);
203-
return ConstantFP::get(V->getContext(), Val);
201+
Val.convert(DestTy->getScalarType()->getFltSemantics(),
202+
APFloat::rmNearestTiesToEven, &ignored);
203+
return ConstantFP::get(DestTy, Val);
204204
}
205205
return nullptr; // Can't fold.
206206
case Instruction::FPToUI:
207207
case Instruction::FPToSI:
208208
if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
209209
const APFloat &V = FPC->getValueAPF();
210210
bool ignored;
211-
uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
212-
APSInt IntVal(DestBitWidth, opc == Instruction::FPToUI);
211+
APSInt IntVal(DestTy->getScalarSizeInBits(), opc == Instruction::FPToUI);
213212
if (APFloat::opInvalidOp ==
214213
V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) {
215214
// Undefined behavior invoked - the destination type can't represent
216215
// the input constant.
217216
return PoisonValue::get(DestTy);
218217
}
219-
return ConstantInt::get(FPC->getContext(), IntVal);
218+
return ConstantInt::get(DestTy, IntVal);
220219
}
221220
return nullptr; // Can't fold.
222221
case Instruction::UIToFP:
223222
case Instruction::SIToFP:
224223
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
225224
const APInt &api = CI->getValue();
226-
APFloat apf(DestTy->getFltSemantics(),
227-
APInt::getZero(DestTy->getPrimitiveSizeInBits()));
225+
APFloat apf(DestTy->getScalarType()->getFltSemantics(),
226+
APInt::getZero(DestTy->getScalarSizeInBits()));
228227
apf.convertFromAPInt(api, opc==Instruction::SIToFP,
229228
APFloat::rmNearestTiesToEven);
230-
return ConstantFP::get(V->getContext(), apf);
229+
return ConstantFP::get(DestTy, apf);
231230
}
232231
return nullptr;
233232
case Instruction::ZExt:
@@ -573,7 +572,7 @@ Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) {
573572
default:
574573
break;
575574
case Instruction::FNeg:
576-
return ConstantFP::get(C->getContext(), neg(CV));
575+
return ConstantFP::get(C->getType(), neg(CV));
577576
}
578577
} else if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
579578
// Fast path for splatted constants.
@@ -857,19 +856,19 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
857856
break;
858857
case Instruction::FAdd:
859858
(void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
860-
return ConstantFP::get(C1->getContext(), C3V);
859+
return ConstantFP::get(C1->getType(), C3V);
861860
case Instruction::FSub:
862861
(void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
863-
return ConstantFP::get(C1->getContext(), C3V);
862+
return ConstantFP::get(C1->getType(), C3V);
864863
case Instruction::FMul:
865864
(void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
866-
return ConstantFP::get(C1->getContext(), C3V);
865+
return ConstantFP::get(C1->getType(), C3V);
867866
case Instruction::FDiv:
868867
(void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
869-
return ConstantFP::get(C1->getContext(), C3V);
868+
return ConstantFP::get(C1->getType(), C3V);
870869
case Instruction::FRem:
871870
(void)C3V.mod(C2V);
872-
return ConstantFP::get(C1->getContext(), C3V);
871+
return ConstantFP::get(C1->getType(), C3V);
873872
}
874873
}
875874
}

llvm/lib/IR/Constants.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ bool Constant::containsUndefElement() const {
358358
}
359359

360360
bool Constant::containsConstantExpression() const {
361+
if (isa<ConstantInt>(this) || isa<ConstantFP>(this))
362+
return false;
363+
361364
if (auto *VTy = dyn_cast<FixedVectorType>(getType())) {
362365
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
363366
if (isa<ConstantExpr>(getAggregateElement(i)))

llvm/test/Transforms/InstCombine/clamp-to-minmax.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
; RUN: opt < %s -passes=instcombine -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat -S | FileCheck %s
34

45
; (X < C1) ? C1 : MIN(X, C2)
56
define float @clamp_float_fast_ordered_strict_maxmin(float %x) {

llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt -S -passes=instcombine %s | FileCheck %s
3+
; RUN: opt -S -passes=instcombine -use-constant-fp-for-fixed-length-splat %s | FileCheck %s
34

45
define float @fabs_fneg_basic(float %x) {
56
; CHECK-LABEL: define float @fabs_fneg_basic(

llvm/test/Transforms/InstCombine/fadd.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
; RUN: opt < %s -passes=instcombine -use-constant-fp-for-fixed-length-splat -S | FileCheck %s
34

45
declare void @use(float)
56
declare void @use_vec(<2 x float>)

llvm/test/Transforms/InstCombine/fdiv.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt -S -passes=instcombine < %s | FileCheck %s
3+
; RUN: opt -S -passes=instcombine -use-constant-fp-for-fixed-length-splat < %s | FileCheck %s
34

45
declare float @llvm.fabs.f32(float) nounwind readnone
56
declare float @llvm.pow.f32(float, float) nounwind readnone

llvm/test/Transforms/InstCombine/fmul.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt -S -passes=instcombine < %s | FileCheck %s
3+
; RUN: opt -S -passes=instcombine -use-constant-fp-for-fixed-length-splat < %s | FileCheck %s
34

45
; (-0.0 - X) * C => X * -C
56
define float @neg_constant(float %x) {

llvm/test/Transforms/InstCombine/fneg.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
; RUN: opt < %s -passes=instcombine -use-constant-fp-for-fixed-length-splat -S | FileCheck %s
34

45
declare float @llvm.ldexp.f32.i32(float, i32)
56
declare <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float>, <2 x i32>)

0 commit comments

Comments
 (0)