Skip to content

Commit 3654f1b

Browse files
[LLVM][IR] Add support for vector ConstantInt/FP to ConstandFolding:FoldBitCast. (#117163)
1 parent 7ea1fe7 commit 3654f1b

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,14 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
151151
return FoldBitCast(ConstantVector::get(Ops), DestTy, DL);
152152
}
153153

154+
// Some of what follows may extend to cover scalable vectors but the current
155+
// implementation is fixed length specific.
156+
if (!isa<FixedVectorType>(C->getType()))
157+
return ConstantExpr::getBitCast(C, DestTy);
158+
154159
// If this is a bitcast from constant vector -> vector, fold it.
155-
if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
160+
if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C) &&
161+
!isa<ConstantInt>(C) && !isa<ConstantFP>(C))
156162
return ConstantExpr::getBitCast(C, DestTy);
157163

158164
// If the element types match, IR can fold it.
@@ -194,10 +200,9 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
194200
IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
195201
// Ask IR to do the conversion now that #elts line up.
196202
C = ConstantExpr::getBitCast(C, SrcIVTy);
197-
// If IR wasn't able to fold it, bail out.
198-
if (!isa<ConstantVector>(C) && // FIXME: Remove ConstantVector.
199-
!isa<ConstantDataVector>(C))
200-
return C;
203+
assert((isa<ConstantVector>(C) || // FIXME: Remove ConstantVector.
204+
isa<ConstantDataVector>(C) || isa<ConstantInt>(C)) &&
205+
"Constant folding cannot fail for plain fp->int bitcast!");
201206
}
202207

203208
// Now we know that the input and output vectors are both integer vectors

llvm/test/Transforms/InstCombine/cast.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
; Tests to make sure elimination of casts is working correctly
33
; RUN: opt < %s -passes=instcombine -S -data-layout="E-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64" | FileCheck %s --check-prefixes=ALL,BE
44
; RUN: opt < %s -passes=instcombine -S -data-layout="e-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64" | FileCheck %s --check-prefixes=ALL,LE
5+
; RUN: opt < %s -passes=instcombine -S -data-layout="E-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64" -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat | FileCheck %s --check-prefixes=ALL,BE
6+
; RUN: opt < %s -passes=instcombine -S -data-layout="e-p:64:64:64-p1:32:32:32-p2:64:64:64-p3:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64" -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat | FileCheck %s --check-prefixes=ALL,LE
57

68
declare void @use_i32(i32)
79
declare void @use_v2i32(<2 x i32>)

llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,11 @@ define <1 x i32> @bitcast_constexpr_scalar_fp_to_vector_int() {
284284
%res = bitcast float 1.0 to <1 x i32>
285285
ret <1 x i32> %res
286286
}
287+
288+
define <2 x i64> @bitcast_constexpr_4f32_2i64_1111() {
289+
; CHECK-LABEL: @bitcast_constexpr_4f32_2i64_1111(
290+
; CHECK-NEXT: ret <2 x i64> splat (i64 4575657222473777152)
291+
;
292+
%res = bitcast <4 x float> splat (float 1.0) to <2 x i64>
293+
ret <2 x i64> %res
294+
}

0 commit comments

Comments
 (0)