Skip to content

Commit e0858b1

Browse files
[LLVM][IR] Add support for vector ConstantInt/FP to ConstandFolding:FoldBitCast.
1 parent d800ea7 commit e0858b1

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 7 additions & 1 deletion
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.

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>)

0 commit comments

Comments
 (0)