Skip to content

Commit

Permalink
[LLVM][IR] Add support for vector ConstantInt/FP to ConstandFolding:F…
Browse files Browse the repository at this point in the history
…oldBitCast. (llvm#117163)
  • Loading branch information
paulwalker-arm authored Dec 10, 2024
1 parent 7ea1fe7 commit 3654f1b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
15 changes: 10 additions & 5 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,14 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
return FoldBitCast(ConstantVector::get(Ops), DestTy, DL);
}

// Some of what follows may extend to cover scalable vectors but the current
// implementation is fixed length specific.
if (!isa<FixedVectorType>(C->getType()))
return ConstantExpr::getBitCast(C, DestTy);

// If this is a bitcast from constant vector -> vector, fold it.
if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C))
if (!isa<ConstantDataVector>(C) && !isa<ConstantVector>(C) &&
!isa<ConstantInt>(C) && !isa<ConstantFP>(C))
return ConstantExpr::getBitCast(C, DestTy);

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

// Now we know that the input and output vectors are both integer vectors
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/Transforms/InstCombine/cast.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
; Tests to make sure elimination of casts is working correctly
; 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
; 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
; 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
; 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

declare void @use_i32(i32)
declare void @use_v2i32(<2 x i32>)
Expand Down
8 changes: 8 additions & 0 deletions llvm/test/Transforms/InstSimplify/bitcast-vector-fold.ll
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,11 @@ define <1 x i32> @bitcast_constexpr_scalar_fp_to_vector_int() {
%res = bitcast float 1.0 to <1 x i32>
ret <1 x i32> %res
}

define <2 x i64> @bitcast_constexpr_4f32_2i64_1111() {
; CHECK-LABEL: @bitcast_constexpr_4f32_2i64_1111(
; CHECK-NEXT: ret <2 x i64> splat (i64 4575657222473777152)
;
%res = bitcast <4 x float> splat (float 1.0) to <2 x i64>
ret <2 x i64> %res
}

0 comments on commit 3654f1b

Please sign in to comment.