Index: lib/IR/ConstantFold.cpp =================================================================== --- lib/IR/ConstantFold.cpp +++ lib/IR/ConstantFold.cpp @@ -534,7 +534,8 @@ // If the cast operand is a constant expression, there's a few things we can // do to try to simplify it. - if (ConstantExpr *CE = dyn_cast(V)) { + ConstantExpr *CE = dyn_cast(V); + if (CE && !DestTy->isVectorTy()) { if (CE->isCast()) { // Try hard to fold cast of cast because they are often eliminable. if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) Index: test/Transforms/LoopVectorize/X86/constant-fold.ll =================================================================== --- /dev/null +++ test/Transforms/LoopVectorize/X86/constant-fold.ll @@ -0,0 +1,40 @@ +; RUN: opt -loop-vectorize -S -mtriple=x86_64-- -o - %s | FileCheck %s + +%rec8 = type { i16 } + +@a = global [1 x %rec8] zeroinitializer +@b = global [2 x i16*] zeroinitializer + + +define void @f1() { + %_tmp112 = icmp slt i16 0, 2 + br i1 %_tmp112, label %bb2.lr.ph, label %bb3 + +bb2.lr.ph: + br label %bb2 + +bb2: + %c.1.0 = phi i16 [ 0, %bb2.lr.ph ], [ %_tmp9, %bb2 ] + %_tmp1 = zext i16 0 to i64 + %_tmp2 = getelementptr [1 x %rec8], [1 x %rec8]* @a, i16 0, i64 %_tmp1 + %_tmp4 = bitcast %rec8* %_tmp2 to i16* + %_tmp6 = sext i16 %c.1.0 to i64 + %_tmp7 = getelementptr [2 x i16*], [2 x i16*]* @b, i16 0, i64 %_tmp6 + store i16* %_tmp4, i16** %_tmp7 + %_tmp9 = add nsw i16 %c.1.0, 1 + %_tmp11 = icmp slt i16 %_tmp9, 2 + br i1 %_tmp11, label %bb2, label %bb1.bb3_crit_edge + +bb1.bb3_crit_edge: + br label %bb3 + +bb3: + ret void +} +; CHECK: vector.body: +; CHECK: [[BITC:%.*]] = bitcast i16** [[IN:%.*]] to <2 x i16*>* + +; Check that we don't generate a faulty bitcast that cast between different sizes +; CHECK-NOT: store <2 x i16*> bitcast ([1 x %rec8]* @a to <2 x i16*>), <2 x i16*>* [[BITC]] + +; CHECK: store <2 x i16*> getelementptr ([1 x %rec8], [1 x %rec8]* @a, <2 x i16> zeroinitializer, <2 x i64> zeroinitializer), i32 0), i32 0, i32 0), i16* getelementptr inbounds (%rec8, %rec8* extractelement (<2 x %rec8*> getelementptr ([1 x %rec8], [1 x %rec8]* @a, <2 x i16> zeroinitializer, <2 x i64> zeroinitializer), i32 1), i32 0, i32 0)>, <2 x i16*>* [[BITC]]