From 1c9fe0361b3f9f5c814ce761b97ecd11637a1d7d Mon Sep 17 00:00:00 2001 From: Nadav Rotem Date: Sat, 20 Aug 2011 14:02:29 +0000 Subject: [PATCH] Add constant folding support for bitcasts of splat vectors to integers. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@138206 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ConstantFolding.cpp | 12 ++++++++++-- test/Transforms/InstCombine/bitcast.ll | 10 ++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index d74d7e806e6..7a8c703040c 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -46,7 +46,16 @@ using namespace llvm; static Constant *FoldBitCast(Constant *C, Type *DestTy, const TargetData &TD) { - // This only handles casts to vectors currently. + ConstantVector *CV = dyn_cast(C); + IntegerType *IntVTy = dyn_cast(DestTy); + // When casting vectors to scalar integers, catch the + // obvious splat cases. + if (IntVTy && CV) { + if (CV->isNullValue()) return ConstantInt::getNullValue(IntVTy); + if (CV->isAllOnesValue()) return ConstantInt::getAllOnesValue(IntVTy); + } + + // The code below only handles casts to vectors currently. VectorType *DestVTy = dyn_cast(DestTy); if (DestVTy == 0) return ConstantExpr::getBitCast(C, DestTy); @@ -59,7 +68,6 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy, } // If this is a bitcast from constant vector -> vector, fold it. - ConstantVector *CV = dyn_cast(C); if (CV == 0) return ConstantExpr::getBitCast(C, DestTy); diff --git a/test/Transforms/InstCombine/bitcast.ll b/test/Transforms/InstCombine/bitcast.ll index 0718b8a3aee..b0630d27254 100644 --- a/test/Transforms/InstCombine/bitcast.ll +++ b/test/Transforms/InstCombine/bitcast.ll @@ -1,5 +1,8 @@ ; RUN: opt < %s -instcombine -S | FileCheck %s +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" +target triple = "x86_64-apple-darwin10.0.0" + ; Bitcasts between vectors and scalars are valid. ; PR4487 define i32 @test1(i64 %a) { @@ -103,3 +106,10 @@ define <2 x float> @test6(float %A){ ; CHECK-NEXT: insertelement <2 x float> , float %A, i32 1 ; CHECK: ret } + +define i64 @ISPC0(i64 %in) { + %out = and i64 %in, xor (i64 bitcast (<4 x i16> to i64), i64 -1) + ret i64 %out +; CHECK: @ISPC0 +; CHECK: ret i64 0 +}