X86: add GATHER intrinsics (AVX2) in Clang

Support the following intrinsics:
  _mm_mask_i32gather_pd, _mm256_mask_i32gather_pd, _mm_mask_i64gather_pd
  _mm256_mask_i64gather_pd, _mm_mask_i32gather_ps, _mm256_mask_i32gather_ps
  _mm_mask_i64gather_ps, _mm256_mask_i64gather_ps


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159222 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/BuiltinsX86.def b/include/clang/Basic/BuiltinsX86.def
index bd7f984..a331cde 100644
--- a/include/clang/Basic/BuiltinsX86.def
+++ b/include/clang/Basic/BuiltinsX86.def
@@ -595,6 +595,16 @@
 BUILTIN(__builtin_ia32_psrlv4di, "V4LLiV4LLiV4LLi", "")
 BUILTIN(__builtin_ia32_psrlv2di, "V2LLiV2LLiV2LLi", "")
 
+// GATHER
+BUILTIN(__builtin_ia32_gatherd_pd, "V2dV2dV2dC*V4iV2dIc", "")
+BUILTIN(__builtin_ia32_gatherd_pd256, "V4dV4dV4dC*V8iV4dIc", "")
+BUILTIN(__builtin_ia32_gatherq_pd, "V2dV2dV2dC*V2LLiV2dIc", "")
+BUILTIN(__builtin_ia32_gatherq_pd256, "V4dV4dV4dC*V4LLiV4dIc", "")
+BUILTIN(__builtin_ia32_gatherd_ps, "V4fV4fV4fC*V4iV4fIc", "")
+BUILTIN(__builtin_ia32_gatherd_ps256, "V8fV8fV8fC*V8iV8fIc", "")
+BUILTIN(__builtin_ia32_gatherq_ps, "V4fV4fV4fC*V2LLiV4fIc", "")
+BUILTIN(__builtin_ia32_gatherq_ps256, "V8fV8fV8fC*V4LLiV8fIc", "")
+
 // BMI
 BUILTIN(__builtin_ia32_bextr_u32, "UiUiUi", "")
 BUILTIN(__builtin_ia32_bextr_u64, "ULLiULLiULLi", "")
diff --git a/lib/Headers/avx2intrin.h b/lib/Headers/avx2intrin.h
index 884c46d..2a27bef 100644
--- a/lib/Headers/avx2intrin.h
+++ b/lib/Headers/avx2intrin.h
@@ -959,3 +959,67 @@
 {
   return (__m128i)__builtin_ia32_psrlv2di(__X, __Y);
 }
+
+#define _mm_mask_i32gather_pd(a, m, i, mask, s) __extension__ ({ \
+  __m128d __a = (a); \
+  double const *__m = (m); \
+  __m128i __i = (i); \
+  __m128d __mask = (mask); \
+  (__m128d)__builtin_ia32_gatherd_pd((__v2df)__a, (const __v2df *)__m, \
+             (__v4si)__i, (__v2df)__mask, (s)); })
+
+#define _mm256_mask_i32gather_pd(a, m, i, mask, s) __extension__ ({ \
+  __m256d __a = (a); \
+  double const *__m = (m); \
+  __m256i __i = (i); \
+  __m256d __mask = (mask); \
+  (__m256d)__builtin_ia32_gatherd_pd256((__v4df)__a, (const __v4df *)__m, \
+             (__v8si)__i, (__v4df)__mask, (s)); })
+
+#define _mm_mask_i64gather_pd(a, m, i, mask, s) __extension__ ({ \
+  __m128d __a = (a); \
+  double const *__m = (m); \
+  __m128i __i = (i); \
+  __m128d __mask = (mask); \
+  (__m128d)__builtin_ia32_gatherq_pd((__v2df)__a, (const __v2df *)__m, \
+             (__v2di)__i, (__v2df)__mask, (s)); })
+
+#define _mm256_mask_i64gather_pd(a, m, i, mask, s) __extension__ ({ \
+  __m256d __a = (a); \
+  double const *__m = (m); \
+  __m256i __i = (i); \
+  __m256d __mask = (mask); \
+  (__m256d)__builtin_ia32_gatherq_pd256((__v4df)__a, (const __v4df *)__m, \
+             (__v4di)__i, (__v4df)__mask, (s)); })
+
+#define _mm_mask_i32gather_ps(a, m, i, mask, s) __extension__ ({ \
+  __m128 __a = (a); \
+  float const *__m = (m); \
+  __m128i __i = (i); \
+  __m128 __mask = (mask); \
+  (__m128)__builtin_ia32_gatherd_ps((__v4sf)__a, (const __v4sf *)__m, \
+            (__v4si)__i, (__v4sf)__mask, (s)); })
+
+#define _mm256_mask_i32gather_ps(a, m, i, mask, s) __extension__ ({ \
+  __m256 __a = (a); \
+  float const *__m = (m); \
+  __m256i __i = (i); \
+  __m256 __mask = (mask); \
+  (__m256)__builtin_ia32_gatherd_ps256((__v8sf)__a, (const __v8sf *)__m, \
+            (__v8si)__i, (__v8sf)__mask, (s)); })
+
+#define _mm_mask_i64gather_ps(a, m, i, mask, s) __extension__ ({ \
+  __m128 __a = (a); \
+  float const *__m = (m); \
+  __m128i __i = (i); \
+  __m128 __mask = (mask); \
+  (__m128)__builtin_ia32_gatherq_ps((__v4sf)__a, (const __v4sf *)__m, \
+            (__v2di)__i, (__v4sf)__mask, (s)); })
+
+#define _mm256_mask_i64gather_ps(a, m, i, mask, s) __extension__ ({ \
+  __m256 __a = (a); \
+  float const *__m = (m); \
+  __m256i __i = (i); \
+  __m256 __mask = (mask); \
+  (__m256)__builtin_ia32_gatherq_ps256((__v8sf)__a, (const __v8sf *)__m, \
+            (__v4di)__i, (__v8sf)__mask, (s)); })
diff --git a/test/CodeGen/avx2-builtins.c b/test/CodeGen/avx2-builtins.c
index 7d166b5..c6953b3 100644
--- a/test/CodeGen/avx2-builtins.c
+++ b/test/CodeGen/avx2-builtins.c
@@ -780,3 +780,46 @@
   // CHECK: @llvm.x86.avx2.psrlv.q
   return _mm_srlv_epi64(a, b);
 }
+
+__m128d test_mm_mask_i32gather_pd(__m128d a, double const *b, __m128i c,
+                                  __m128d d) {
+  // CHECK: @llvm.x86.avx2.gather.d.pd
+  return _mm_mask_i32gather_pd(a, b, c, d, 2);
+}
+
+__m256d test_mm256_mask_i32gather_pd(__m256d a, double const *b, __m256i c,
+                                      __m256d d) {
+  // CHECK: @llvm.x86.avx2.gather.d.pd.256
+  return _mm256_mask_i32gather_pd(a, b, c, d, 2);
+}
+__m128d test_mm_mask_i64gather_pd(__m128d a, double const *b, __m128i c,
+                                  __m128d d) {
+  // CHECK: @llvm.x86.avx2.gather.q.pd
+  return _mm_mask_i64gather_pd(a, b, c, d, 2);
+}
+__m256d test_mm256_mask_i64gather_pd(__m256d a, double const *b, __m256i c,
+                                      __m256d d) {
+  // CHECK: @llvm.x86.avx2.gather.q.pd.256
+  return _mm256_mask_i64gather_pd(a, b, c, d, 2);
+}
+
+__m128 test_mm_mask_i32gather_ps(__m128 a, float const *b, __m128i c,
+                                 __m128 d) {
+  // CHECK: @llvm.x86.avx2.gather.d.ps
+  return _mm_mask_i32gather_ps(a, b, c, d, 2);
+}
+__m256 test_mm256_mask_i32gather_ps(__m256 a, float const *b, __m256i c,
+                                     __m256 d) {
+  // CHECK: @llvm.x86.avx2.gather.d.ps.256
+  return _mm256_mask_i32gather_ps(a, b, c, d, 2);
+}
+__m128 test_mm_mask_i64gather_ps(__m128 a, float const *b, __m128i c,
+                                 __m128 d) {
+  // CHECK: @llvm.x86.avx2.gather.q.ps
+  return _mm_mask_i64gather_ps(a, b, c, d, 2);
+}
+__m256 test_mm256_mask_i64gather_ps(__m256 a, float const *b, __m256i c,
+                                     __m256 d) {
+  // CHECK: @llvm.x86.avx2.gather.q.ps.256
+  return _mm256_mask_i64gather_ps(a, b, c, d, 2);
+}