Skip to content

Commit ff68f23

Browse files
Address suggestions from RKSimon
1 parent c14783d commit ff68f23

File tree

3 files changed

+110
-78
lines changed

3 files changed

+110
-78
lines changed

clang/lib/AST/ExprConstant.cpp

+11-17
Original file line numberDiff line numberDiff line change
@@ -10975,15 +10975,11 @@ static bool EvaluateVectorOrLValue(APValue &Result, EvalInfo &Info,
1097510975
// it was an rvalue to get the current APValue.
1097610976
LValue LValueFound;
1097710977
LValueFound.setFrom(Info.Ctx, Result);
10978-
if (!handleLValueToRValueConversion(Info, E, Type, LValueFound, Result)) {
10978+
if (!handleLValueToRValueConversion(Info, E, Type, LValueFound, Result))
1097910979
return false;
10980-
}
1098110980
}
1098210981

10983-
if (!Result.isVector()) {
10984-
return false;
10985-
}
10986-
return true;
10982+
return Result.isVector();
1098710983
}
1098810984

1098910985
static bool handleVectorConversion(EvalInfo &Info, const FPOptions FPO,
@@ -11027,9 +11023,10 @@ bool VectorExprEvaluator::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
1102711023

1102811024
const FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts());
1102911025

11026+
auto SourceLen = Source.getVectorLength();
1103011027
SmallVector<APValue, 4> ResultElements;
11031-
ResultElements.reserve(Source.getVectorLength());
11032-
for (unsigned EltNum = 0; EltNum < Source.getVectorLength(); ++EltNum) {
11028+
ResultElements.reserve(SourceLen);
11029+
for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
1103311030
APValue Elt;
1103411031
if (!handleVectorConversion(Info, FPO, E, SourceTy, DestTy,
1103511032
Source.getVectorElt(EltNum), Elt))
@@ -11048,22 +11045,19 @@ static bool handleVectorShuffle(EvalInfo &Info, const ShuffleVectorExpr *E,
1104811045

1104911046
Expr const *IndexExpr = E->getExpr(2 + EltNum);
1105011047
APSInt IndexVal;
11051-
if (!EvaluateInteger(IndexExpr, IndexVal, Info)) {
11048+
if (!EvaluateInteger(IndexExpr, IndexVal, Info))
1105211049
return false;
11053-
}
1105411050

1105511051
uint32_t index = IndexVal.getZExtValue();
1105611052
// The spec says that -1 should be treated as undef for optimizations,
1105711053
// but in constexpr we need to choose a value. We'll choose 0.
11058-
if (index >= TotalElementsInAVector * 2) {
11054+
if (index >= TotalElementsInAVector * 2)
1105911055
index = 0;
11060-
}
1106111056

11062-
if (index >= TotalElementsInAVector) {
11057+
if (index >= TotalElementsInAVector)
1106311058
Result = VecVal2.getVectorElt(index - TotalElementsInAVector);
11064-
} else {
11059+
else
1106511060
Result = VecVal1.getVectorElt(index);
11066-
}
1106711061
return true;
1106811062
}
1106911063

@@ -11078,9 +11072,9 @@ bool VectorExprEvaluator::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
1107811072
return false;
1107911073

1108011074
VectorType const *DestVecTy = E->getType()->castAs<VectorType>();
11081-
if (!DestVecTy) {
11075+
if (!DestVecTy)
1108211076
return false;
11083-
}
11077+
1108411078
QualType DestElTy = DestVecTy->getElementType();
1108511079

1108611080
auto TotalElementsInOutputVector = DestVecTy->getNumElements();

clang/test/Sema/constant-builtins-2.c

-61
Original file line numberDiff line numberDiff line change
@@ -427,64 +427,3 @@ extern __typeof__(__builtin_expect(0, 0)) bi0;
427427
// Strings
428428
int array1[__builtin_strlen("ab\0cd")];
429429
int array2[(sizeof(array1)/sizeof(int)) == 2? 1 : -1];
430-
431-
typedef double vector4double __attribute__((__vector_size__(32)));
432-
typedef float vector4float __attribute__((__vector_size__(16)));
433-
typedef long long vector4long __attribute__((__vector_size__(32)));
434-
typedef int vector4int __attribute__((__vector_size__(16)));
435-
typedef short vector4short __attribute__((__vector_size__(8)));
436-
typedef char vector4char __attribute__((__vector_size__(4)));
437-
typedef double vector8double __attribute__((__vector_size__(64)));
438-
typedef float vector8float __attribute__((__vector_size__(32)));
439-
typedef long long vector8long __attribute__((__vector_size__(64)));
440-
typedef int vector8int __attribute__((__vector_size__(32)));
441-
typedef short vector8short __attribute__((__vector_size__(16)));
442-
typedef char vector8char __attribute__((__vector_size__(8)));
443-
444-
// Convert vector
445-
#define CHECK_NUM(__size, __typeFrom, __typeTo, ...) \
446-
vector##__size##__typeTo \
447-
from_##vector##__size##__typeFrom##_to_##vector##__size##__typeTo##_var = \
448-
__builtin_convertvector((vector##__size##__typeFrom){__VA_ARGS__}, \
449-
vector##__size##__typeTo);
450-
#define CHECK_TO_ALL_TYPES(__size, __typeFrom, ...) \
451-
CHECK_NUM(__size, __typeFrom, double, __VA_ARGS__) \
452-
CHECK_NUM(__size, __typeFrom, float, __VA_ARGS__) \
453-
CHECK_NUM(__size, __typeFrom, long, __VA_ARGS__) \
454-
CHECK_NUM(__size, __typeFrom, int, __VA_ARGS__) \
455-
CHECK_NUM(__size, __typeFrom, short, __VA_ARGS__) \
456-
CHECK_NUM(__size, __typeFrom, char, __VA_ARGS__)
457-
458-
#define CHECK_ALL_COMBINATIONS(__size, ...) \
459-
CHECK_TO_ALL_TYPES(__size, double, __VA_ARGS__) \
460-
CHECK_TO_ALL_TYPES(__size, float, __VA_ARGS__) \
461-
CHECK_TO_ALL_TYPES(__size, long, __VA_ARGS__) \
462-
CHECK_TO_ALL_TYPES(__size, int, __VA_ARGS__) \
463-
CHECK_TO_ALL_TYPES(__size, short, __VA_ARGS__) \
464-
CHECK_TO_ALL_TYPES(__size, char, __VA_ARGS__)
465-
466-
CHECK_ALL_COMBINATIONS(4, 0, 1, 2, 3);
467-
CHECK_ALL_COMBINATIONS(8, 0, 1, 2, 3, 4, 5, 6, 7);
468-
#undef CHECK_ALL_COMBINATIONS
469-
#undef CHECK_TO_ALL_TYPES
470-
#undef CHECK_NUM
471-
472-
// Shuffle vector
473-
vector4int const vector4intConst1 = {0, 1, 2, 3};
474-
vector4int const vector4intConst2 = {4, 5, 6, 7};
475-
vector8int const vector8intConst = {};
476-
477-
vector4int vectorShuffle1 =
478-
__builtin_shufflevector(vector4intConst1, vector4intConst2, 0, 1, 2, 3);
479-
vector4int vectorShuffle2 =
480-
__builtin_shufflevector(vector4intConst1, vector4intConst2, 4, 5, 6, 7);
481-
vector4int vectorShuffle3 =
482-
__builtin_shufflevector(vector4intConst1, vector4intConst2, -1, -1, -1, -1);
483-
vector4int vectorShuffle4 =
484-
__builtin_shufflevector(vector4intConst1, vector4intConst2, 0, 2, 4, 6);
485-
vector8int vectorShuffle5 = __builtin_shufflevector(
486-
vector8intConst, vector8intConst, 0, 2, 4, 6, 8, 10, 12, 14);
487-
vector4int vectorShuffle6 = __builtin_shufflevector(
488-
vector8intConst, vector8intConst, 0, 2, 4, 6);
489-
vector8int vectorShuffle7 =
490-
__builtin_shufflevector(vector4intConst1, vector4intConst2, 0, 2, 4, 6, 1, 3, 5, 7);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only %s
2+
// expected-no-diagnostics
3+
4+
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
5+
#define LITTLE_END 1
6+
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
7+
#define LITTLE_END 0
8+
#else
9+
#error "huh?"
10+
#endif
11+
12+
typedef double vector4double __attribute__((__vector_size__(32)));
13+
typedef float vector4float __attribute__((__vector_size__(16)));
14+
typedef long long vector4long __attribute__((__vector_size__(32)));
15+
typedef int vector4int __attribute__((__vector_size__(16)));
16+
typedef short vector4short __attribute__((__vector_size__(8)));
17+
typedef char vector4char __attribute__((__vector_size__(4)));
18+
typedef double vector8double __attribute__((__vector_size__(64)));
19+
typedef float vector8float __attribute__((__vector_size__(32)));
20+
typedef long long vector8long __attribute__((__vector_size__(64)));
21+
typedef int vector8int __attribute__((__vector_size__(32)));
22+
typedef short vector8short __attribute__((__vector_size__(16)));
23+
typedef char vector8char __attribute__((__vector_size__(8)));
24+
25+
#define CHECK_NUM(__size, __typeFrom, __typeTo, ...) \
26+
constexpr vector##__size##__typeTo \
27+
from_##vector##__size##__typeFrom##_to_##vector##__size##__typeTo##_var = \
28+
__builtin_convertvector((vector##__size##__typeFrom){__VA_ARGS__}, \
29+
vector##__size##__typeTo);
30+
#define CHECK_TO_ALL_TYPES(__size, __typeFrom, ...) \
31+
CHECK_NUM(__size, __typeFrom, double, __VA_ARGS__) \
32+
CHECK_NUM(__size, __typeFrom, float, __VA_ARGS__) \
33+
CHECK_NUM(__size, __typeFrom, long, __VA_ARGS__) \
34+
CHECK_NUM(__size, __typeFrom, int, __VA_ARGS__) \
35+
CHECK_NUM(__size, __typeFrom, short, __VA_ARGS__) \
36+
CHECK_NUM(__size, __typeFrom, char, __VA_ARGS__) \
37+
static_assert( \
38+
__builtin_bit_cast( \
39+
unsigned, \
40+
__builtin_shufflevector( \
41+
from_vector##__size##__typeFrom##_to_vector##__size##char_var, \
42+
from_vector##__size##__typeFrom##_to_vector##__size##char_var, \
43+
0, 1, 2, 3)) == (LITTLE_END ? 0x03020100 : 0x00010203)); \
44+
static_assert( \
45+
__builtin_bit_cast( \
46+
unsigned long long, \
47+
__builtin_shufflevector( \
48+
from_vector##__size##__typeFrom##_to_vector##__size##short_var, \
49+
from_vector##__size##__typeFrom##_to_vector##__size##short_var, \
50+
0, 1, 2, 3)) == \
51+
(LITTLE_END ? 0x0003000200010000 : 0x0000000100020003));
52+
53+
#define CHECK_ALL_COMBINATIONS(__size, ...) \
54+
CHECK_TO_ALL_TYPES(__size, double, __VA_ARGS__) \
55+
CHECK_TO_ALL_TYPES(__size, float, __VA_ARGS__) \
56+
CHECK_TO_ALL_TYPES(__size, long, __VA_ARGS__) \
57+
CHECK_TO_ALL_TYPES(__size, int, __VA_ARGS__) \
58+
CHECK_TO_ALL_TYPES(__size, short, __VA_ARGS__) \
59+
CHECK_TO_ALL_TYPES(__size, char, __VA_ARGS__)
60+
61+
CHECK_ALL_COMBINATIONS(4, 0, 1, 2, 3);
62+
CHECK_ALL_COMBINATIONS(8, 0, 1, 2, 3, 4, 5, 6, 7);
63+
#undef CHECK_ALL_COMBINATIONS
64+
#undef CHECK_TO_ALL_TYPES
65+
#undef CHECK_NUM
66+
67+
// Shuffle vector
68+
constexpr vector4char vector4charConst1 = {0, 1, 2, 3};
69+
constexpr vector4char vector4charConst2 = {4, 5, 6, 7};
70+
constexpr vector8char vector8intConst = {8, 9, 10, 11, 12, 13, 14, 15};
71+
72+
constexpr vector4char vectorShuffle1 =
73+
__builtin_shufflevector(vector4charConst1, vector4charConst2, 0, 1, 2, 3);
74+
static_assert(__builtin_bit_cast(unsigned, vectorShuffle1) ==
75+
(LITTLE_END ? 0x03020100 : 0x00010203));
76+
constexpr vector4char vectorShuffle2 =
77+
__builtin_shufflevector(vector4charConst1, vector4charConst2, 4, 5, 6, 7);
78+
static_assert(__builtin_bit_cast(unsigned, vectorShuffle2) ==
79+
(LITTLE_END ? 0x07060504 : 0x04050607));
80+
constexpr vector4char vectorShuffle3 = __builtin_shufflevector(
81+
vector4charConst1, vector4charConst2, -1, -1, -1, -1);
82+
static_assert(__builtin_bit_cast(unsigned, vectorShuffle3) ==
83+
(LITTLE_END ? 0x00000000 : 0x00000000));
84+
constexpr vector4char vectorShuffle4 =
85+
__builtin_shufflevector(vector4charConst1, vector4charConst2, 0, 2, 4, 6);
86+
static_assert(__builtin_bit_cast(unsigned, vectorShuffle4) ==
87+
(LITTLE_END ? 0x06040200 : 0x00020406));
88+
constexpr vector8char vectorShuffle5 = __builtin_shufflevector(
89+
vector8intConst, vector8intConst, 0, 2, 4, 6, 8, 10, 12, 14);
90+
static_assert(__builtin_bit_cast(unsigned long long, vectorShuffle5) ==
91+
(LITTLE_END ? 0x0E0C0A080E0C0A08 : 0x080A0C0E080A0C0E));
92+
constexpr vector4char vectorShuffle6 =
93+
__builtin_shufflevector(vector8intConst, vector8intConst, 0, 2, 4, 6);
94+
static_assert(__builtin_bit_cast(unsigned, vectorShuffle6) ==
95+
(LITTLE_END ? 0x0E0C0A08 : 0x080A0C0E));
96+
constexpr vector8char vectorShuffle7 = __builtin_shufflevector(
97+
vector4charConst1, vector4charConst2, 0, 2, 4, 6, 1, 3, 5, 7);
98+
static_assert(__builtin_bit_cast(unsigned long long, vectorShuffle7) ==
99+
(LITTLE_END ? 0x0705030106040200 : 0x0002040601030507));

0 commit comments

Comments
 (0)