From: Eli Friedman Date: Thu, 5 Apr 2012 21:48:40 +0000 (+0000) Subject: Make the variant of __builtin_shufflevector that takes the shuffle indexes as a vecto... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=87b9c0311f96c35109204a49d4bda909aa72b62a;p=clang Make the variant of __builtin_shufflevector that takes the shuffle indexes as a vector actually usable. Patch by David Neto. PR12465. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154128 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index e9de43dd66..5723541c62 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -754,8 +754,8 @@ Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { MTy->getNumElements()); Value* NewV = llvm::UndefValue::get(RTy); for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) { - Value *Indx = Builder.getInt32(i); - Indx = Builder.CreateExtractElement(Mask, Indx, "shuf_idx"); + Value *IIndx = Builder.getInt32(i); + Value *Indx = Builder.CreateExtractElement(Mask, IIndx, "shuf_idx"); Indx = Builder.CreateZExt(Indx, CGF.Int32Ty, "idx_zext"); // Handle vec3 special since the index will be off by one for the RHS. @@ -767,7 +767,7 @@ Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { Indx = Builder.CreateSelect(cmpIndx, newIndx, Indx, "sel_shuf_idx"); } Value *VExt = Builder.CreateExtractElement(LHS, Indx, "shuf_elt"); - NewV = Builder.CreateInsertElement(NewV, VExt, Indx, "shuf_ins"); + NewV = Builder.CreateInsertElement(NewV, VExt, IIndx, "shuf_ins"); } return NewV; } diff --git a/test/CodeGen/builtinshufflevector2.c b/test/CodeGen/builtinshufflevector2.c new file mode 100644 index 0000000000..faf7a3ec1a --- /dev/null +++ b/test/CodeGen/builtinshufflevector2.c @@ -0,0 +1,35 @@ +// RUN: %clang -emit-llvm -S -o - %s | FileCheck %s + +typedef float float4 __attribute__((ext_vector_type(4))); +typedef unsigned int uint4 __attribute__((ext_vector_type(4))); + +// CHECK: define void @clang_shufflevector_v_v( +void clang_shufflevector_v_v( float4* A, float4 x, uint4 mask ) { +// CHECK: [[MASK:%.*]] = and <4 x i32> {{%.*}}, +// CHECK: [[I:%.*]] = extractelement <4 x i32> [[MASK]], i32 0 +// CHECK: [[E:%.*]] = extractelement <4 x float> [[X:%.*]], i32 [[I]] +// +// Here is where ToT Clang code generation makes a mistake. +// It uses [[I]] as the insertion index instead of 0. +// Similarly on the remaining insertelement. +// CHECK: [[V:%[a-zA-Z0-9._]+]] = insertelement <4 x float> undef, float [[E]], i32 0 + +// CHECK: [[I:%.*]] = extractelement <4 x i32> [[MASK]], i32 1 +// CHECK: [[E:%.*]] = extractelement <4 x float> [[X]], i32 [[I]] +// CHECK: [[V:%.*]] = insertelement <4 x float> [[V]], float [[E]], i32 1 +// CHECK: [[I:%.*]] = extractelement <4 x i32> [[MASK]], i32 2 +// CHECK: [[E:%.*]] = extractelement <4 x float> [[X]], i32 [[I]] +// CHECK: [[V:%.*]] = insertelement <4 x float> [[V]], float [[E]], i32 2 +// CHECK: [[I:%.*]] = extractelement <4 x i32> [[MASK]], i32 3 +// CHECK: [[E:%.*]] = extractelement <4 x float> [[X]], i32 [[I]] +// CHECK: [[V:%.*]] = insertelement <4 x float> [[V]], float [[E]], i32 3 +// CHECK: store <4 x float> [[V]], <4 x float>* {{%.*}}, + *A = __builtin_shufflevector( x, mask ); +} + +// CHECK: define void @clang_shufflevector_v_v_c( +void clang_shufflevector_v_v_c( float4* A, float4 x, float4 y, uint4 mask ) { +// CHECK: [[V:%.*]] = shufflevector <4 x float> {{%.*}}, <4 x float> {{%.*}}, <4 x i32> +// CHECK: store <4 x float> [[V]], <4 x float>* {{%.*}} + *A = __builtin_shufflevector( x, y, 0, 4, 1, 5 ); +}