Skip to content

Commit a3d2d34

Browse files
committed
[Clang] Use poison as base for vector literals
When constructing vectors from elements, use poison instead of undef as the base value. These literals always initialize all elements (padding the remainder with zero), so that the choice of base value does not affect semantics.
1 parent 18e1179 commit a3d2d34

35 files changed

+508
-508
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,8 +1894,8 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
18941894
// initializer, since LLVM optimizers generally do not want to touch
18951895
// shuffles.
18961896
unsigned CurIdx = 0;
1897-
bool VIsUndefShuffle = false;
1898-
llvm::Value *V = llvm::UndefValue::get(VType);
1897+
bool VIsPoisonShuffle = false;
1898+
llvm::Value *V = llvm::PoisonValue::get(VType);
18991899
for (unsigned i = 0; i != NumInitElements; ++i) {
19001900
Expr *IE = E->getInit(i);
19011901
Value *Init = Visit(IE);
@@ -1915,16 +1915,16 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
19151915
llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand());
19161916
Value *LHS = nullptr, *RHS = nullptr;
19171917
if (CurIdx == 0) {
1918-
// insert into undef -> shuffle (src, undef)
1918+
// insert into poison -> shuffle (src, poison)
19191919
// shufflemask must use an i32
19201920
Args.push_back(getAsInt32(C, CGF.Int32Ty));
19211921
Args.resize(ResElts, -1);
19221922

19231923
LHS = EI->getVectorOperand();
19241924
RHS = V;
1925-
VIsUndefShuffle = true;
1926-
} else if (VIsUndefShuffle) {
1927-
// insert into undefshuffle && size match -> shuffle (v, src)
1925+
VIsPoisonShuffle = true;
1926+
} else if (VIsPoisonShuffle) {
1927+
// insert into poison shuffle && size match -> shuffle (v, src)
19281928
llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V);
19291929
for (unsigned j = 0; j != CurIdx; ++j)
19301930
Args.push_back(getMaskElt(SVV, j, 0));
@@ -1933,7 +1933,7 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
19331933

19341934
LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
19351935
RHS = EI->getVectorOperand();
1936-
VIsUndefShuffle = false;
1936+
VIsPoisonShuffle = false;
19371937
}
19381938
if (!Args.empty()) {
19391939
V = Builder.CreateShuffleVector(LHS, RHS, Args);
@@ -1944,7 +1944,7 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
19441944
}
19451945
V = Builder.CreateInsertElement(V, Init, Builder.getInt32(CurIdx),
19461946
"vecinit");
1947-
VIsUndefShuffle = false;
1947+
VIsPoisonShuffle = false;
19481948
++CurIdx;
19491949
continue;
19501950
}
@@ -1962,9 +1962,9 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
19621962

19631963
if (OpTy->getNumElements() == ResElts) {
19641964
for (unsigned j = 0; j != CurIdx; ++j) {
1965-
// If the current vector initializer is a shuffle with undef, merge
1965+
// If the current vector initializer is a shuffle with poison, merge
19661966
// this shuffle directly into it.
1967-
if (VIsUndefShuffle) {
1967+
if (VIsPoisonShuffle) {
19681968
Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0));
19691969
} else {
19701970
Args.push_back(j);
@@ -1974,7 +1974,7 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
19741974
Args.push_back(getMaskElt(SVI, j, Offset));
19751975
Args.resize(ResElts, -1);
19761976

1977-
if (VIsUndefShuffle)
1977+
if (VIsPoisonShuffle)
19781978
V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
19791979

19801980
Init = SVOp;
@@ -1997,12 +1997,12 @@ Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
19971997
Args.resize(ResElts, -1);
19981998
}
19991999

2000-
// If V is undef, make sure it ends up on the RHS of the shuffle to aid
2000+
// If V is poison, make sure it ends up on the RHS of the shuffle to aid
20012001
// merging subsequent shuffles into this one.
20022002
if (CurIdx == 0)
20032003
std::swap(V, Init);
20042004
V = Builder.CreateShuffleVector(V, Init, Args, "vecinit");
2005-
VIsUndefShuffle = isa<llvm::UndefValue>(Init);
2005+
VIsPoisonShuffle = isa<llvm::PoisonValue>(Init);
20062006
CurIdx += InitElts;
20072007
}
20082008

clang/test/CodeGen/PowerPC/ppc-emmintrin.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -521,14 +521,14 @@ test_converts() {
521521
// CHECK: sitofp i64 %{{[0-9a-zA-Z_.]+}} to double
522522

523523
// CHECK-LABEL: define available_externally <2 x i64> @_mm_cvtsi64_si128
524-
// CHECK: %[[INS:[0-9a-zA-Z_.]+]] = insertelement <2 x i64> undef, i64 %{{[0-9a-zA-Z_.]+}}, i32 0
524+
// CHECK: %[[INS:[0-9a-zA-Z_.]+]] = insertelement <2 x i64> poison, i64 %{{[0-9a-zA-Z_.]+}}, i32 0
525525
// CHECK: insertelement <2 x i64> %[[INS]], i64 0, i32 1
526526

527527
// CHECK-LABEL: define available_externally <2 x double> @_mm_cvtsi64x_sd
528528
// CHECK: call <2 x double> @_mm_cvtsi64_sd(<2 x double> noundef %{{[0-9a-zA-Z_.]+}}, i64 noundef %{{[0-9a-zA-Z_.]+}})
529529

530530
// CHECK-LABEL: define available_externally <2 x i64> @_mm_cvtsi64x_si128
531-
// CHECK: %[[INS:[0-9a-zA-Z_.]+]] = insertelement <2 x i64> undef, i64 %{{[0-9a-zA-Z_.]+}}, i32 0
531+
// CHECK: %[[INS:[0-9a-zA-Z_.]+]] = insertelement <2 x i64> poison, i64 %{{[0-9a-zA-Z_.]+}}, i32 0
532532
// CHECK: insertelement <2 x i64> %[[INS]], i64 0, i32 1
533533

534534
// CHECK-LABEL: define available_externally <2 x double> @_mm_cvtss_sd
@@ -906,35 +906,35 @@ test_set() {
906906

907907
// CHECK-LABEL: define available_externally <2 x i64> @_mm_set_epi16
908908
// CHECK-COUNT-8: store i16 {{[0-9a-zA-Z_%.]+}}, ptr {{[0-9a-zA-Z_%.]+}}, align 2
909-
// CHECK: insertelement <8 x i16> undef, i16 {{[0-9a-zA-Z_%.]+}}, i32 0
909+
// CHECK: insertelement <8 x i16> poison, i16 {{[0-9a-zA-Z_%.]+}}, i32 0
910910
// CHECK-COUNT-7: insertelement <8 x i16> {{[0-9a-zA-Z_%.]+}}, i16 {{[0-9a-zA-Z_%.]+}}, i32 {{[1-7]}}
911911

912912
// CHECK-LABEL: define available_externally <2 x i64> @_mm_set_epi32
913913
// CHECK-COUNT-4: store i32 {{[0-9a-zA-Z_%.]+}}, ptr {{[0-9a-zA-Z_%.]+}}, align 4
914-
// CHECK: insertelement <4 x i32> undef, i32 {{[0-9a-zA-Z_%.]+}}, i32 0
914+
// CHECK: insertelement <4 x i32> poison, i32 {{[0-9a-zA-Z_%.]+}}, i32 0
915915
// CHECK-COUNT-3: insertelement <4 x i32> {{[0-9a-zA-Z_%.]+}}, i32 {{[0-9a-zA-Z_%.]+}}, i32 {{[1-3]}}
916916

917917
// CHECK-LABEL: define available_externally <2 x i64> @_mm_set_epi64
918918
// CHECK: call <2 x i64> @_mm_set_epi64x(i64 noundef %{{[0-9a-zA-Z_.]+}}, i64 noundef %{{[0-9a-zA-Z_.]+}})
919919

920920
// CHECK-LABEL: define available_externally <2 x i64> @_mm_set_epi64x
921-
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <2 x i64> undef, i64 %{{[0-9a-zA-Z_.]+}}, i32 0
921+
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <2 x i64> poison, i64 %{{[0-9a-zA-Z_.]+}}, i32 0
922922
// CHECK: insertelement <2 x i64> %[[VEC]], i64 %{{[0-9a-zA-Z_.]+}}, i32 1
923923

924924
// CHECK-LABEL: define available_externally <2 x i64> @_mm_set_epi8
925925
// CHECK-COUNT-16: store i8 {{[0-9a-zA-Z_%.]+}}, ptr {{[0-9a-zA-Z_%.]+}}, align 1
926-
// CHECK: insertelement <16 x i8> undef, i8 {{[0-9a-zA-Z_%.]+}}, i32 {{[0-9]+}}
926+
// CHECK: insertelement <16 x i8> poison, i8 {{[0-9a-zA-Z_%.]+}}, i32 {{[0-9]+}}
927927
// CHECK-COUNT-15: {{[0-9a-zA-Z_%.]+}} = insertelement <16 x i8> {{[0-9a-zA-Z_%.]+}}, i8 {{[0-9a-zA-Z_%.]+}}, i32 {{[0-9]+}}
928928

929929
// CHECK-LABEL: define available_externally <2 x double> @_mm_set_pd
930-
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <2 x double> undef, double %{{[0-9a-zA-Z_.]+}}, i32 0
930+
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <2 x double> poison, double %{{[0-9a-zA-Z_.]+}}, i32 0
931931
// CHECK: insertelement <2 x double> %[[VEC]], double %{{[0-9a-zA-Z_.]+}}, i32 1
932932

933933
// CHECK-LABEL: define available_externally <2 x double> @_mm_set_pd1
934934
// CHECK: call <2 x double> @_mm_set1_pd(double noundef %{{[0-9a-zA-Z_.]+}})
935935

936936
// CHECK-LABEL: define available_externally <2 x double> @_mm_set_sd
937-
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <2 x double> undef, double %{{[0-9a-zA-Z_.]+}}, i32 0
937+
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <2 x double> poison, double %{{[0-9a-zA-Z_.]+}}, i32 0
938938
// CHECK: insertelement <2 x double> %[[VEC]], double 0.000000e+00, i32 1
939939

940940
// CHECK-LABEL: define available_externally <2 x i64> @_mm_set1_epi16
@@ -960,7 +960,7 @@ test_set() {
960960
// CHECK: call <2 x i64> @_mm_set_epi8
961961

962962
// CHECK-LABEL: define available_externally <2 x double> @_mm_set1_pd
963-
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <2 x double> undef, double %{{[0-9a-zA-Z_.]+}}, i32 0
963+
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <2 x double> poison, double %{{[0-9a-zA-Z_.]+}}, i32 0
964964
// CHECK: insertelement <2 x double> %[[VEC]], double %{{[0-9a-zA-Z_.]+}}, i32 1
965965

966966
// CHECK-LABEL: define available_externally <2 x i64> @_mm_setr_epi16
@@ -981,7 +981,7 @@ test_set() {
981981
// CHECK: call <2 x i64> @_mm_set_epi8
982982

983983
// CHECK-LABEL: define available_externally <2 x double> @_mm_setr_pd
984-
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <2 x double> undef, double %{{[0-9a-zA-Z_.]+}}, i32 0
984+
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <2 x double> poison, double %{{[0-9a-zA-Z_.]+}}, i32 0
985985
// CHECK: insertelement <2 x double> %[[VEC]], double %{{[0-9a-zA-Z_.]+}}, i32 1
986986

987987
// CHECK-LABEL: define available_externally <2 x double> @_mm_setzero_pd()

clang/test/CodeGen/PowerPC/ppc-xmmintrin.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,8 @@ test_sad() {
796796

797797
// CHECK-LABEL: define available_externally i64 @_mm_sad_pu8
798798
// CHECK: call void @llvm.memset.p0.i64(ptr align 8 %{{[0-9a-zA-Z_.]+}}, i8 0, i64 8, i1 false)
799-
// CHECK: insertelement <2 x i64> <i64 0, i64 undef>, i64 %{{[0-9a-zA-Z_.]+}}, i32 1
800-
// CHECK: insertelement <2 x i64> <i64 0, i64 undef>, i64 %{{[0-9a-zA-Z_.]+}}, i32 1
799+
// CHECK: insertelement <2 x i64> <i64 0, i64 poison>, i64 %{{[0-9a-zA-Z_.]+}}, i32 1
800+
// CHECK: insertelement <2 x i64> <i64 0, i64 poison>, i64 %{{[0-9a-zA-Z_.]+}}, i32 1
801801
// CHECK: call <16 x i8> @vec_min(unsigned char vector[16], unsigned char vector[16])
802802
// CHECK: call <16 x i8> @vec_max(unsigned char vector[16], unsigned char vector[16])
803803
// CHECK: call <16 x i8> @vec_sub(unsigned char vector[16], unsigned char vector[16])
@@ -823,7 +823,7 @@ test_set() {
823823
// CHECK-LABEL: @test_set
824824

825825
// CHECK-LABEL: define available_externally <4 x float> @_mm_set_ps
826-
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <4 x float> undef, float %{{[0-9a-zA-Z_.]+}}, i32 0
826+
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <4 x float> poison, float %{{[0-9a-zA-Z_.]+}}, i32 0
827827
// CHECK: %[[VEC2:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC]], float %{{[0-9a-zA-Z_.]+}}, i32 1
828828
// CHECK: %[[VEC3:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC2]], float %{{[0-9a-zA-Z_.]+}}, i32 2
829829
// CHECK: %[[VEC4:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC3]], float %{{[0-9a-zA-Z_.]+}}, i32 3
@@ -833,21 +833,21 @@ test_set() {
833833
// CHECK: call <4 x float> @_mm_set1_ps
834834

835835
// CHECK-LABEL: define available_externally <4 x float> @_mm_set_ss
836-
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <4 x float> undef, float %{{[0-9a-zA-Z_.]+}}, i32 0
836+
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <4 x float> poison, float %{{[0-9a-zA-Z_.]+}}, i32 0
837837
// CHECK: %[[VEC2:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC]], float 0.000000e+00, i32 1
838838
// CHECK: %[[VEC3:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC2]], float 0.000000e+00, i32 2
839839
// CHECK: %[[VEC4:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC3]], float 0.000000e+00, i32 3
840840
// CHECK: store <4 x float> %[[VEC4]], ptr %{{[0-9a-zA-Z_.]+}}, align 16
841841

842842
// CHECK-LABEL: define available_externally <4 x float> @_mm_set1_ps
843-
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <4 x float> undef, float %{{[0-9a-zA-Z_.]+}}, i32 0
843+
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <4 x float> poison, float %{{[0-9a-zA-Z_.]+}}, i32 0
844844
// CHECK: %[[VEC2:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC]], float %{{[0-9a-zA-Z_.]+}}, i32 1
845845
// CHECK: %[[VEC3:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC2]], float %{{[0-9a-zA-Z_.]+}}, i32 2
846846
// CHECK: %[[VEC4:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC3]], float %{{[0-9a-zA-Z_.]+}}, i32 3
847847
// CHECK: store <4 x float> %[[VEC4]], ptr %{{[0-9a-zA-Z_.]+}}, align 16
848848

849849
// CHECK-LABEL: define available_externally <4 x float> @_mm_setr_ps
850-
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <4 x float> undef, float %{{[0-9a-zA-Z_.]+}}, i32 0
850+
// CHECK: %[[VEC:[0-9a-zA-Z_.]+]] = insertelement <4 x float> poison, float %{{[0-9a-zA-Z_.]+}}, i32 0
851851
// CHECK: %[[VEC2:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC]], float %{{[0-9a-zA-Z_.]+}}, i32 1
852852
// CHECK: %[[VEC3:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC2]], float %{{[0-9a-zA-Z_.]+}}, i32 2
853853
// CHECK: %[[VEC4:[0-9a-zA-Z_.]+]] = insertelement <4 x float> %[[VEC3]], float %{{[0-9a-zA-Z_.]+}}, i32 3

0 commit comments

Comments
 (0)