clang 24.0.0git
CIRGenBuiltinX86.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code to emit x86/x86_64 Builtin calls as CIR or a function
10// call to be later resolved.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CIRGenBuilder.h"
15#include "CIRGenFunction.h"
16#include "CIRGenModule.h"
17#include "mlir/IR/Attributes.h"
18#include "mlir/IR/BuiltinAttributes.h"
19#include "mlir/IR/Location.h"
20#include "mlir/IR/Types.h"
21#include "mlir/IR/ValueRange.h"
28#include "llvm/ADT/Sequence.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/TargetParser/X86TargetParser.h"
31#include <string>
32
33using namespace clang;
34using namespace clang::CIRGen;
35
36static mlir::Value emitVectorFCmp(CIRGenFunction &cgf, const CallExpr &expr,
38 cir::FCmpOpKind pred, bool signaling) {
41 mlir::Value cmp = builder.createVecFCompare(cgf.getLoc(expr.getExprLoc()),
42 pred, ops[0], ops[1], signaling);
43 return builder.createBitcast(cmp, ops[0].getType());
44}
45
47 cir::FCmpOpKind pred;
49};
50
51/// Decodes comparison condition of x86 cmppps/cmppd* builtins which is an
52/// integer in the range [0, 31]. Predicates for 16-31 repeat the 0-15
53/// predicates and differ only in signaling behavior.
54static X86FCmpInfo getX86FCmpInfo(unsigned cc) {
55 bool inverseSig = cc & 0x10;
56 const auto createVal = [inverseSig](cir::FCmpOpKind kind, bool isSignaling) {
57 return X86FCmpInfo{kind, static_cast<bool>(inverseSig ^ isSignaling)};
58 };
59 switch (cc & 0xf) {
60 case 0x00:
61 return createVal(cir::FCmpOpKind::oeq, /*isSignaling=*/false);
62 case 0x01:
63 return createVal(cir::FCmpOpKind::olt, /*isSignaling=*/true);
64 case 0x02:
65 return createVal(cir::FCmpOpKind::ole, /*isSignaling=*/true);
66 case 0x03:
67 return createVal(cir::FCmpOpKind::uno, /*isSignaling=*/false);
68 case 0x04:
69 return createVal(cir::FCmpOpKind::une, /*isSignaling=*/false);
70 case 0x05:
71 return createVal(cir::FCmpOpKind::uge, /*isSignaling=*/true);
72 case 0x06:
73 return createVal(cir::FCmpOpKind::ugt, /*isSignaling=*/true);
74 case 0x07:
75 return createVal(cir::FCmpOpKind::ord, /*isSignaling=*/false);
76 case 0x08:
77 return createVal(cir::FCmpOpKind::ueq, /*isSignaling=*/false);
78 case 0x09:
79 return createVal(cir::FCmpOpKind::ult, /*isSignaling=*/true);
80 case 0x0a:
81 return createVal(cir::FCmpOpKind::ule, /*isSignaling=*/true);
82 case 0x0b:
83 return createVal(cir::FCmpOpKind::_false, /*isSignaling=*/false);
84 case 0x0c:
85 return createVal(cir::FCmpOpKind::one, /*isSignaling=*/false);
86 case 0x0d:
87 return createVal(cir::FCmpOpKind::oge, /*isSignaling=*/true);
88 case 0x0e:
89 return createVal(cir::FCmpOpKind::ogt, /*isSignaling=*/true);
90 case 0x0f:
91 return createVal(cir::FCmpOpKind::_true, /*isSignaling=*/false);
92 default:
93 llvm_unreachable("Unhandled cc");
94 }
95}
96
97/// Returns the legacy x86 intrinsic that implements the given x86 cmppps/cmppd*
98/// builtin.
99static llvm::StringRef getX86FCmpIntrinsicName(unsigned builtinID) {
100 switch (builtinID) {
101 default:
102 llvm_unreachable("Unexpected builtin");
103 case X86::BI__builtin_ia32_cmpps:
104 return "x86.sse.cmp.ps";
105 case X86::BI__builtin_ia32_cmpps256:
106 return "x86.avx.cmp.ps.256";
107 case X86::BI__builtin_ia32_cmppd:
108 return "x86.sse2.cmp.pd";
109 case X86::BI__builtin_ia32_cmppd256:
110 return "x86.avx.cmp.pd.256";
111 case X86::BI__builtin_ia32_cmpph128_mask:
112 return "x86.avx512fp16.mask.cmp.ph.128";
113 case X86::BI__builtin_ia32_cmpph256_mask:
114 return "x86.avx512fp16.mask.cmp.ph.256";
115 case X86::BI__builtin_ia32_cmpph512_mask:
116 return "x86.avx512fp16.mask.cmp.ph.512";
117 case X86::BI__builtin_ia32_cmpps512_mask:
118 return "x86.avx512.mask.cmp.ps.512";
119 case X86::BI__builtin_ia32_cmppd512_mask:
120 return "x86.avx512.mask.cmp.pd.512";
121 case X86::BI__builtin_ia32_cmpps128_mask:
122 return "x86.avx512.mask.cmp.ps.128";
123 case X86::BI__builtin_ia32_cmpps256_mask:
124 return "x86.avx512.mask.cmp.ps.256";
125 case X86::BI__builtin_ia32_cmppd128_mask:
126 return "x86.avx512.mask.cmp.pd.128";
127 case X86::BI__builtin_ia32_cmppd256_mask:
128 return "x86.avx512.mask.cmp.pd.256";
129 }
130}
131
132static mlir::Value getMaskVecValue(CIRGenBuilderTy &builder, mlir::Location loc,
133 mlir::Value mask, unsigned numElems) {
134 auto maskTy = cir::VectorType::getFixed(
135 builder.getSIntNTy(1), cast<cir::IntType>(mask.getType()).getWidth());
136 mlir::Value maskVec = builder.createBitcast(mask, maskTy);
137
138 // If we have less than 8 elements, then the starting mask was an i8 and
139 // we need to extract down to the right number of elements.
140 if (numElems < 8) {
142 mlir::Type i32Ty = builder.getSInt32Ty();
143 for (auto i : llvm::seq<unsigned>(0, numElems))
144 indices.push_back(cir::IntAttr::get(i32Ty, i));
145
146 maskVec = builder.createVecShuffle(loc, maskVec, maskVec, indices);
147 }
148 return maskVec;
149}
150
151static mlir::Value emitX86Round(CIRGenFunction &cgf, unsigned builtinID,
152 const CallExpr *expr,
154 bool isScalar = builtinID == X86::BI__builtin_ia32_roundss ||
155 builtinID == X86::BI__builtin_ia32_roundsd;
156
157 unsigned roundingControl =
158 cgf.getZExtIntValueFromConstOp(isScalar ? ops[2] : ops[1]);
159
160 constexpr unsigned mxcsrMask = 0b100;
161 constexpr unsigned fRoundNoExcMask = 0b1000;
162 unsigned useMXCSR = mxcsrMask & roundingControl;
163 unsigned fRoundNoExc = fRoundNoExcMask & roundingControl;
164
165 CIRGenBuilderTy &builder = cgf.getBuilder();
166 mlir::Location loc = cgf.getLoc(expr->getExprLoc());
167
168 if (useMXCSR || !fRoundNoExc) {
169 StringRef intrinsicName;
170 switch (builtinID) {
171 default:
172 llvm_unreachable("Unexpected builtin");
173 case X86::BI__builtin_ia32_roundps:
174 intrinsicName = "x86.sse41.round.ps";
175 break;
176 case X86::BI__builtin_ia32_roundpd:
177 intrinsicName = "x86.sse41.round.pd";
178 break;
179 case X86::BI__builtin_ia32_roundps256:
180 intrinsicName = "x86.avx.round.ps.256";
181 break;
182 case X86::BI__builtin_ia32_roundpd256:
183 intrinsicName = "x86.avx.round.pd.256";
184 break;
185 case X86::BI__builtin_ia32_roundss:
186 intrinsicName = "x86.sse41.round.ss";
187 break;
188 case X86::BI__builtin_ia32_roundsd:
189 intrinsicName = "x86.sse41.round.sd";
190 break;
191 }
192
193 mlir::Type resTy = ops[0].getType();
194 return builder.emitIntrinsicCallOp(loc, intrinsicName, resTy, ops);
195 }
196
197 auto emitRoundOp = [&](mlir::Value x) -> mlir::Value {
198 constexpr unsigned roundingMask = 0b11;
199 unsigned roundingMode = roundingControl & roundingMask;
200
201 if (builder.getIsFPConstrained()) {
203
205 cir::FenvAttr fenv = builder.getConstrainedFPAttr();
206
207 switch (roundingMode) {
208 default:
209 llvm_unreachable("Invalid rounding mode");
210 case 0b00:
211 return cir::RoundEvenOp::create(builder, loc, x, fenv);
212 case 0b01:
213 return cir::FloorOp::create(builder, loc, x, fenv);
214 case 0b10:
215 return cir::CeilOp::create(builder, loc, x, fenv);
216 case 0b11:
217 return cir::TruncOp::create(builder, loc, x, fenv);
218 }
219 }
220
221 switch (roundingMode) {
222 default:
223 llvm_unreachable("Invalid rounding mode");
224 case 0b00:
225 return cir::RoundEvenOp::create(builder, loc, x);
226 case 0b01:
227 return cir::FloorOp::create(builder, loc, x);
228 case 0b10:
229 return cir::CeilOp::create(builder, loc, x);
230 case 0b11:
231 return cir::TruncOp::create(builder, loc, x);
232 }
233 };
234
235 if (isScalar) {
236 mlir::Value valAt0 = builder.createExtractElement(loc, ops[1], 0);
237 mlir::Value roundedAt0 = emitRoundOp(valAt0);
238 return builder.createInsertElement(loc, ops[0], roundedAt0, 0);
239 }
240
241 return emitRoundOp(ops[0]);
242}
243
244static mlir::Value emitX86CompressStore(CIRGenBuilderTy &builder,
245 mlir::Location loc,
247 auto resultTy = cast<cir::VectorType>(ops[1].getType());
248 mlir::Value maskValue =
249 getMaskVecValue(builder, loc, ops[2], resultTy.getFixedSize());
250
251 return builder.emitIntrinsicCallOp(
252 loc, "masked.compressstore", cir::VoidType::get(builder.getContext()),
253 mlir::ValueRange{ops[1], ops[0], maskValue});
254}
255
256// Builds the VecShuffleOp for pshuflw and pshufhw x86 builtins.
257//
258// The vector is split into lanes of 8 word elements (16 bits). The lower or
259// upper half of each lane, controlled by `isLow`, is shuffled in the following
260// way: The immediate is truncated to 8 bits, separated into 4 2-bit fields. The
261// i-th field's value represents the resulting index of the i-th element in the
262// half lane after shuffling. The other half of the lane remains unchanged.
263static cir::VecShuffleOp emitPshufWord(CIRGenBuilderTy &builder,
264 const mlir::Value vec,
265 const mlir::Value immediate,
266 const mlir::Location loc,
267 const bool isLow) {
269
270 auto vecTy = cast<cir::VectorType>(vec.getType());
271 unsigned numElts = vecTy.getFixedSize();
272
273 unsigned firstHalfStart = isLow ? 0 : 4;
274 unsigned secondHalfStart = 4 - firstHalfStart;
275
276 // Splat the 8-bits of immediate 4 times to help the loop wrap around.
277 imm = (imm & 0xff) * 0x01010101;
278
279 int64_t indices[32];
280 for (unsigned l = 0; l != numElts; l += 8) {
281 for (unsigned i = firstHalfStart; i != firstHalfStart + 4; ++i) {
282 indices[l + i] = l + (imm & 3) + firstHalfStart;
283 imm >>= 2;
284 }
285 for (unsigned i = secondHalfStart; i != secondHalfStart + 4; ++i)
286 indices[l + i] = l + i;
287 }
288
289 return builder.createVecShuffle(loc, vec, ArrayRef(indices, numElts));
290}
291
292// Builds the shuffle mask for pshufd and shufpd/shufps x86 builtins.
293// The shuffle mask is written to outIndices.
294static void
295computeFullLaneShuffleMask(CIRGenFunction &cgf, const mlir::Value vec,
296 uint32_t imm, const bool isShufP,
297 llvm::SmallVectorImpl<int64_t> &outIndices) {
298 auto vecTy = cast<cir::VectorType>(vec.getType());
299 unsigned numElts = vecTy.getFixedSize();
300 unsigned numLanes = cgf.cgm.getDataLayout().getTypeSizeInBits(vecTy) / 128;
301 unsigned numLaneElts = numElts / numLanes;
302
303 // Splat the 8-bits of immediate 4 times to help the loop wrap around.
304 imm = (imm & 0xff) * 0x01010101;
305
306 for (unsigned l = 0; l != numElts; l += numLaneElts) {
307 for (unsigned i = 0; i != numLaneElts; ++i) {
308 uint32_t idx = imm % numLaneElts;
309 imm /= numLaneElts;
310 if (isShufP && i >= (numLaneElts / 2))
311 idx += numElts;
312 outIndices[l + i] = l + idx;
313 }
314 }
315
316 outIndices.resize(numElts);
317}
318
319static mlir::Value emitPrefetch(CIRGenFunction &cgf, unsigned builtinID,
320 const CallExpr *e,
321 const SmallVector<mlir::Value> &ops) {
322 CIRGenBuilderTy &builder = cgf.getBuilder();
323 mlir::Location location = cgf.getLoc(e->getExprLoc());
324 mlir::Type voidTy = builder.getVoidTy();
325 mlir::Value address = builder.createPtrBitcast(ops[0], voidTy);
326 bool isWrite{};
327 int locality{};
328
329 assert(builtinID == X86::BI_mm_prefetch || builtinID == X86::BI_m_prefetchw ||
330 builtinID == X86::BI_m_prefetch && "Expected prefetch builtin");
331
332 if (builtinID == X86::BI_mm_prefetch) {
333 int hint = cgf.getSExtIntValueFromConstOp(ops[1]);
334 isWrite = (hint >> 2) & 0x1;
335 locality = hint & 0x3;
336 } else {
337 isWrite = (builtinID == X86::BI_m_prefetchw);
338 locality = 0x3;
339 }
340
341 cir::PrefetchOp::create(builder, location, address, locality, isWrite);
342 return {};
343}
344
345static mlir::Value emitX86CompressExpand(CIRGenBuilderTy &builder,
346 mlir::Location loc, mlir::Value source,
347 mlir::Value mask,
348 mlir::Value inputVector,
349 const std::string &id) {
350 auto resultTy = cast<cir::VectorType>(mask.getType());
351 mlir::Value maskValue =
352 getMaskVecValue(builder, loc, inputVector,
353 cast<cir::VectorType>(resultTy).getFixedSize());
354 return builder.emitIntrinsicCallOp(loc, id, resultTy,
355 mlir::ValueRange{source, mask, maskValue});
356}
357
358static mlir::Value
359emitEncodeKey(mlir::MLIRContext *context, CIRGenBuilderTy &builder,
360 const mlir::Location &location, mlir::ValueRange inputOperands,
361 mlir::Value outputOperand, std::uint8_t vecOutputCount,
362 const std::string &intrinsicName, std::uint8_t numResults) {
363 cir::VectorType resVector =
364 cir::VectorType::getFixed(builder.getUInt64Ty(), 2);
366 llvm::append_range(members,
367 llvm::SmallVector<mlir::Type>(vecOutputCount, resVector));
368 cir::StructType resRecord = cir::StructType::get(
369 context, members, /*packed=*/false,
370 /*is_class=*/false, cir::RecordType::getAllDataKinds(members));
371
372 mlir::Value outputPtr =
373 builder.createBitcast(outputOperand, cir::PointerType::get(resVector));
374 mlir::Value call = builder.emitIntrinsicCallOp(location, intrinsicName,
375 resRecord, inputOperands);
376 for (std::uint8_t i = 0; i < numResults; ++i) {
377 mlir::Value vecValue =
378 cir::ExtractMemberOp::create(builder, location, call, i + 1);
379 mlir::Value index = builder.getSInt32(i, location);
380 mlir::Value ptr = builder.createPtrStride(location, outputPtr, index);
381 builder.createStore(location, vecValue, Address{ptr, CharUnits::One()});
382 }
383 return cir::ExtractMemberOp::create(builder, location, call, 0);
384}
385
386static mlir::Value emitX86Select(CIRGenBuilderTy &builder, mlir::Location loc,
387 mlir::Value mask, mlir::Value op0,
388 mlir::Value op1) {
389 auto constOp = mlir::dyn_cast_or_null<cir::ConstantOp>(mask.getDefiningOp());
390 // If the mask is all ones just return first argument.
391 if (constOp && constOp.isAllOnesValue())
392 return op0;
393
394 mask = getMaskVecValue(builder, loc, mask,
395 cast<cir::VectorType>(op0.getType()).getFixedSize());
396
397 return cir::VecTernaryOp::create(builder, loc, mask, op0, op1);
398}
399
400// Helper function to extract zero-bit from a mask as a boolean
401static mlir::Value getMaskZeroBitAsBool(CIRGenBuilderTy &builder,
402 mlir::Location loc, mlir::Value mask) {
403 // Get the mask as a vector of i1 and extract bit 0
404 auto intTy = mlir::dyn_cast<cir::IntType>(mask.getType());
405 assert(intTy && "mask must be an integer type");
406 unsigned width = intTy.getWidth();
407
408 auto maskVecTy = cir::VectorType::getFixed(builder.getSIntNTy(1), width);
409 mlir::Value maskVec = builder.createBitcast(mask, maskVecTy);
410
411 // Extract bit 0 from the mask vector
412 mlir::Value bit0 = builder.createExtractElement(loc, maskVec, uint64_t(0));
413
414 // Convert i1 to bool for select
415 auto boolTy = cir::BoolType::get(builder.getContext());
416 return cir::CastOp::create(builder, loc, boolTy, cir::CastKind::int_to_bool,
417 bit0);
418}
419
420static mlir::Value emitX86ScalarSelect(CIRGenBuilderTy &builder,
421 mlir::Location loc, mlir::Value mask,
422 mlir::Value op0, mlir::Value op1) {
423
424 // If the mask is all ones just return first argument.
425 if (auto c = mlir::dyn_cast_or_null<cir::ConstantOp>(mask.getDefiningOp()))
426 if (c.isAllOnesValue())
427 return op0;
428
429 mlir::Value cond = getMaskZeroBitAsBool(builder, loc, mask);
430 return builder.createSelect(loc, cond, op0, op1);
431}
432
433static mlir::Value emitX86MaskAddLogic(CIRGenBuilderTy &builder,
434 mlir::Location loc,
435 const std::string &intrinsicName,
437
438 auto intTy = cast<cir::IntType>(ops[0].getType());
439 unsigned numElts = intTy.getWidth();
440 mlir::Value lhsVec = getMaskVecValue(builder, loc, ops[0], numElts);
441 mlir::Value rhsVec = getMaskVecValue(builder, loc, ops[1], numElts);
442 mlir::Type vecTy = lhsVec.getType();
443 mlir::Value resVec = builder.emitIntrinsicCallOp(
444 loc, intrinsicName, vecTy, mlir::ValueRange{lhsVec, rhsVec});
445 return builder.createBitcast(resVec, ops[0].getType());
446}
447
448static mlir::Value emitX86MaskUnpack(CIRGenBuilderTy &builder,
449 mlir::Location loc,
450 const std::string &intrinsicName,
452 unsigned numElems = cast<cir::IntType>(ops[0].getType()).getWidth();
453
454 // Convert both operands to mask vectors.
455 mlir::Value lhs = getMaskVecValue(builder, loc, ops[0], numElems);
456 mlir::Value rhs = getMaskVecValue(builder, loc, ops[1], numElems);
457
458 mlir::Type i32Ty = builder.getSInt32Ty();
459
460 // Create indices for extracting the first half of each vector.
462 for (auto i : llvm::seq<unsigned>(0, numElems / 2))
463 halfIndices.push_back(cir::IntAttr::get(i32Ty, i));
464
465 // Extract first half of each vector. This gives better codegen than
466 // doing it in a single shuffle.
467 mlir::Value lhsHalf = builder.createVecShuffle(loc, lhs, lhs, halfIndices);
468 mlir::Value rhsHalf = builder.createVecShuffle(loc, rhs, rhs, halfIndices);
469
470 // Create indices for concatenating the vectors.
471 // NOTE: Operands are swapped to match the intrinsic definition.
472 // After the half extraction, both vectors have numElems/2 elements.
473 // In createVecShuffle(rhsHalf, lhsHalf, indices), indices [0..numElems/2-1]
474 // select from rhsHalf, and indices [numElems/2..numElems-1] select from
475 // lhsHalf.
477 for (auto i : llvm::seq<unsigned>(0, numElems))
478 concatIndices.push_back(cir::IntAttr::get(i32Ty, i));
479
480 // Concat the vectors (RHS first, then LHS).
481 mlir::Value res =
482 builder.createVecShuffle(loc, rhsHalf, lhsHalf, concatIndices);
483 return builder.createBitcast(res, ops[0].getType());
484}
485
486template <typename BinOp>
487static mlir::Value
488emitX86MaskLogic(CIRGenBuilderTy &builder, mlir::Location loc,
489 SmallVectorImpl<mlir::Value> &ops, bool invertLHS = false) {
490 unsigned numElts = cast<cir::IntType>(ops[0].getType()).getWidth();
491 mlir::Value lhs = getMaskVecValue(builder, loc, ops[0], numElts);
492 mlir::Value rhs = getMaskVecValue(builder, loc, ops[1], numElts);
493
494 if (invertLHS)
495 lhs = builder.createNot(lhs);
496 return builder.createBitcast(BinOp::create(builder, loc, lhs, rhs),
497 ops[0].getType());
498}
499
500static mlir::Value emitX86MaskTest(CIRGenBuilderTy &builder, mlir::Location loc,
501 const std::string &intrinsicName,
503 auto intTy = cast<cir::IntType>(ops[0].getType());
504 unsigned numElts = intTy.getWidth();
505 mlir::Value lhsVec = getMaskVecValue(builder, loc, ops[0], numElts);
506 mlir::Value rhsVec = getMaskVecValue(builder, loc, ops[1], numElts);
507 mlir::Type resTy = builder.getSInt32Ty();
508 return builder.emitIntrinsicCallOp(loc, intrinsicName, resTy,
509 mlir::ValueRange{lhsVec, rhsVec});
510}
511
512static mlir::Value emitX86MaskedCompareResult(CIRGenBuilderTy &builder,
513 mlir::Value cmp, unsigned numElts,
514 mlir::Value maskIn,
515 mlir::Location loc) {
516 if (maskIn) {
517 auto c = mlir::dyn_cast_or_null<cir::ConstantOp>(maskIn.getDefiningOp());
518 if (!c || !c.isAllOnesValue())
519 cmp = builder.createAnd(loc, cmp,
520 getMaskVecValue(builder, loc, maskIn, numElts));
521 }
522 if (numElts < 8) {
524 mlir::Type i64Ty = builder.getSInt64Ty();
525
526 for (unsigned i = 0; i != numElts; ++i)
527 indices.push_back(cir::IntAttr::get(i64Ty, i));
528 for (unsigned i = numElts; i != 8; ++i)
529 indices.push_back(cir::IntAttr::get(i64Ty, i % numElts + numElts));
530
531 // This should shuffle between cmp (first vector) and null (second vector)
532 mlir::Value nullVec = builder.getNullValue(cmp.getType(), loc);
533 cmp = builder.createVecShuffle(loc, cmp, nullVec, indices);
534 }
535 return builder.createBitcast(cmp, builder.getUIntNTy(std::max(numElts, 8U)));
536}
537
538// TODO: The cgf parameter should be removed when all the NYI cases are
539// implemented.
540static std::optional<mlir::Value>
541emitX86MaskedCompare(CIRGenBuilderTy &builder, unsigned cc, bool isSigned,
542 ArrayRef<mlir::Value> ops, mlir::Location loc) {
543 assert((ops.size() == 2 || ops.size() == 4) &&
544 "Unexpected number of arguments");
545 unsigned numElts = cast<cir::VectorType>(ops[0].getType()).getFixedSize();
546 mlir::Value cmp;
547 if (cc == 3) {
548 cmp = builder.getNullValue(
549 cir::VectorType::getFixed(builder.getSIntNTy(1), numElts), loc);
550 } else if (cc == 7) {
551 cir::VectorType resultTy =
552 cir::VectorType::getFixed(builder.getSIntNTy(1), numElts);
553 llvm::APInt allOnes = llvm::APInt::getAllOnes(1);
554 cmp = cir::VecSplatOp::create(
555 builder, loc, resultTy,
556 builder.getConstAPInt(loc, builder.getSIntNTy(1), allOnes));
557 } else {
558 cir::CmpOpKind pred;
559 switch (cc) {
560 default:
561 llvm_unreachable("Unknown condition code");
562 case 0:
563 pred = cir::CmpOpKind::eq;
564 break;
565 case 1:
566 pred = cir::CmpOpKind::lt;
567 break;
568 case 2:
569 pred = cir::CmpOpKind::le;
570 break;
571 case 4:
572 pred = cir::CmpOpKind::ne;
573 break;
574 case 5:
575 pred = cir::CmpOpKind::ge;
576 break;
577 case 6:
578 pred = cir::CmpOpKind::gt;
579 break;
580 }
581
582 auto resultTy = cir::VectorType::getFixed(builder.getSIntNTy(1), numElts);
583 cmp = cir::VecCmpOp::create(builder, loc, resultTy, pred, ops[0], ops[1]);
584 }
585
586 mlir::Value maskIn;
587 if (ops.size() == 4)
588 maskIn = ops[3];
589
590 return emitX86MaskedCompareResult(builder, cmp, numElts, maskIn, loc);
591}
592
593// TODO: The cgf parameter should be removed when all the NYI cases are
594// implemented.
595static std::optional<mlir::Value> emitX86ConvertToMask(CIRGenFunction &cgf,
596 CIRGenBuilderTy &builder,
597 mlir::Value in,
598 mlir::Location loc) {
599 cir::ConstantOp zero = builder.getNullValue(in.getType(), loc);
600 return emitX86MaskedCompare(builder, 1, true, {in, zero}, loc);
601}
602
603static std::optional<mlir::Value> emitX86SExtMask(CIRGenBuilderTy &builder,
604 mlir::Value op,
605 mlir::Type dstTy,
606 mlir::Location loc) {
607 unsigned numberOfElements = cast<cir::VectorType>(dstTy).getFixedSize();
608 mlir::Value mask = getMaskVecValue(builder, loc, op, numberOfElements);
609
610 return builder.createCast(loc, cir::CastKind::integral, mask, dstTy);
611}
612
613static mlir::Value emitVecInsert(CIRGenBuilderTy &builder, mlir::Location loc,
614 mlir::Value vec, mlir::Value value,
615 mlir::Value indexOp) {
616 unsigned numElts = cast<cir::VectorType>(vec.getType()).getFixedSize();
617
619 indexOp.getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue();
620
621 index &= numElts - 1;
622
623 cir::ConstantOp indexVal = builder.getUInt64(index, loc);
624
625 return cir::VecInsertOp::create(builder, loc, vec, value, indexVal);
626}
627
628static mlir::Value emitX86FunnelShift(CIRGenBuilderTy &builder,
629 mlir::Location location, mlir::Value &op0,
630 mlir::Value &op1, mlir::Value &amt,
631 bool isRight) {
632 mlir::Type op0Ty = op0.getType();
633
634 // Amount may be scalar immediate, in which case create a splat vector.
635 // Funnel shifts amounts are treated as modulo and types are all power-of-2
636 // so we only care about the lowest log2 bits anyway.
637 if (amt.getType() != op0Ty) {
638 auto vecTy = mlir::cast<cir::VectorType>(op0Ty);
639 uint64_t numElems = vecTy.getFixedSize();
640
641 auto amtTy = mlir::cast<cir::IntType>(amt.getType());
642 auto vecElemTy = mlir::cast<cir::IntType>(vecTy.getElementType());
643
644 // If signed, cast to the same width but unsigned first to
645 // ensure zero-extension when casting to a bigger unsigned `vecElemeTy`.
646 if (amtTy.isSigned()) {
647 cir::IntType unsignedAmtTy = builder.getUIntNTy(amtTy.getWidth());
648 amt = builder.createIntCast(amt, unsignedAmtTy);
649 }
650 cir::IntType unsignedVecElemType = builder.getUIntNTy(vecElemTy.getWidth());
651 amt = builder.createIntCast(amt, unsignedVecElemType);
652 amt = cir::VecSplatOp::create(
653 builder, location,
654 cir::VectorType::getFixed(unsignedVecElemType, numElems), amt);
655 }
656
657 const StringRef intrinsicName = isRight ? "fshr" : "fshl";
658 return builder.emitIntrinsicCallOp(location, intrinsicName, op0Ty,
659 mlir::ValueRange{op0, op1, amt});
660}
661
662static mlir::Value emitX86Muldq(CIRGenBuilderTy &builder, mlir::Location loc,
663 bool isSigned,
665 unsigned opTypePrimitiveSizeInBits) {
666 mlir::Type ty = cir::VectorType::getFixed(builder.getSInt64Ty(),
667 opTypePrimitiveSizeInBits / 64);
668 mlir::Value lhs = builder.createBitcast(loc, ops[0], ty);
669 mlir::Value rhs = builder.createBitcast(loc, ops[1], ty);
670 if (isSigned) {
671 cir::ConstantOp shiftAmt =
672 builder.getConstant(loc, cir::IntAttr::get(builder.getSInt64Ty(), 32));
673 cir::VecSplatOp shiftSplatVecOp =
674 cir::VecSplatOp::create(builder, loc, ty, shiftAmt.getResult());
675 mlir::Value shiftSplatValue = shiftSplatVecOp.getResult();
676 // In CIR, right-shift operations are automatically lowered to either an
677 // arithmetic or logical shift depending on the operand type. The purpose
678 // of the shifts here is to propagate the sign bit of the 32-bit input
679 // into the upper bits of each vector lane.
680 lhs = builder.createShift(loc, lhs, shiftSplatValue, true);
681 lhs = builder.createShift(loc, lhs, shiftSplatValue, false);
682 rhs = builder.createShift(loc, rhs, shiftSplatValue, true);
683 rhs = builder.createShift(loc, rhs, shiftSplatValue, false);
684 } else {
685 cir::ConstantOp maskScalar = builder.getConstant(
686 loc, cir::IntAttr::get(builder.getSInt64Ty(), 0xffffffff));
687 cir::VecSplatOp mask =
688 cir::VecSplatOp::create(builder, loc, ty, maskScalar.getResult());
689 // Clear the upper bits
690 lhs = builder.createAnd(loc, lhs, mask);
691 rhs = builder.createAnd(loc, rhs, mask);
692 }
693 return builder.createMul(loc, lhs, rhs);
694}
695
696// Convert f16 half values to floats.
697static mlir::Value emitX86CvtF16ToFloatExpr(CIRGenBuilderTy &builder,
698 mlir::Location loc,
700 mlir::Type dstTy) {
701 assert((ops.size() == 1 || ops.size() == 3 || ops.size() == 4) &&
702 "Unknown cvtph2ps intrinsic");
703
704 // If the SAE intrinsic doesn't use default rounding then we can't upgrade.
705 if (ops.size() == 4) {
706 auto constOp = ops[3].getDefiningOp<cir::ConstantOp>();
707 assert(constOp && "Expected constant operand");
708 if (constOp.getIntValue().getZExtValue() != 4) {
709 return builder.emitIntrinsicCallOp(loc, "x86.avx512.mask.vcvtph2ps.512",
710 dstTy, ops);
711 }
712 }
713
714 unsigned numElts = cast<cir::VectorType>(dstTy).getFixedSize();
715 mlir::Value src = ops[0];
716
717 // Extract the subvector
718 if (numElts != cast<cir::VectorType>(src.getType()).getFixedSize()) {
719 assert(numElts == 4 && "Unexpected vector size");
720 src = builder.createVecShuffle(loc, src, {0, 1, 2, 3});
721 }
722
723 // Bitcast from vXi16 to vXf16.
724 cir::VectorType halfTy = cir::VectorType::getFixed(
725 cir::FP16Type::get(builder.getContext()), numElts);
726
727 src = builder.createCast(cir::CastKind::bitcast, src, halfTy);
728
729 // Perform the fp-extension
730 mlir::Value res = builder.createCast(cir::CastKind::floating, src, dstTy);
731
732 if (ops.size() >= 3)
733 res = emitX86Select(builder, loc, ops[2], res, ops[1]);
734 return res;
735}
736
737static mlir::Value emitX86vpcom(CIRGenBuilderTy &builder, mlir::Location loc,
739 bool isSigned) {
740 mlir::Value op0 = ops[0];
741 mlir::Value op1 = ops[1];
742
743 cir::VectorType ty = cast<cir::VectorType>(op0.getType());
744 cir::IntType elementTy = cast<cir::IntType>(ty.getElementType());
745
747
748 cir::CmpOpKind pred;
749 switch (imm) {
750 case 0x0:
751 pred = cir::CmpOpKind::lt;
752 break;
753 case 0x1:
754 pred = cir::CmpOpKind::le;
755 break;
756 case 0x2:
757 pred = cir::CmpOpKind::gt;
758 break;
759 case 0x3:
760 pred = cir::CmpOpKind::ge;
761 break;
762 case 0x4:
763 pred = cir::CmpOpKind::eq;
764 break;
765 case 0x5:
766 pred = cir::CmpOpKind::ne;
767 break;
768 case 0x6:
769 return builder.getNullValue(ty, loc); // FALSE
770 case 0x7: {
771 llvm::APInt allOnes = llvm::APInt::getAllOnes(elementTy.getWidth());
772 return cir::VecSplatOp::create(
773 builder, loc, ty,
774 builder.getConstAPInt(loc, elementTy, allOnes)); // TRUE
775 }
776 default:
777 llvm_unreachable("Unexpected XOP vpcom/vpcomu predicate");
778 }
779
780 if ((!isSigned && elementTy.isSigned()) ||
781 (isSigned && elementTy.isUnsigned())) {
782 elementTy = elementTy.isSigned() ? builder.getUIntNTy(elementTy.getWidth())
783 : builder.getSIntNTy(elementTy.getWidth());
784 ty = cir::VectorType::get(elementTy, ty.getSize());
785 op0 = builder.createBitcast(op0, ty);
786 op1 = builder.createBitcast(op1, ty);
787 }
788
789 return builder.createVecCompare(loc, pred, op0, op1);
790}
791
792static mlir::Value emitX86Fpclass(CIRGenBuilderTy &builder, mlir::Location loc,
793 unsigned builtinID,
795 unsigned numElts = cast<cir::VectorType>(ops[0].getType()).getFixedSize();
796 mlir::Value maskIn = ops[2];
797 ops.erase(ops.begin() + 2);
798
799 StringRef intrinsicName;
800 switch (builtinID) {
801 default:
802 llvm_unreachable("Unsupported fpclass builtin");
803 case X86::BI__builtin_ia32_vfpclassbf16128_mask:
804 intrinsicName = "x86.avx10.fpclass.bf16.128";
805 break;
806 case X86::BI__builtin_ia32_vfpclassbf16256_mask:
807 intrinsicName = "x86.avx10.fpclass.bf16.256";
808 break;
809 case X86::BI__builtin_ia32_vfpclassbf16512_mask:
810 intrinsicName = "x86.avx10.fpclass.bf16.512";
811 break;
812 case X86::BI__builtin_ia32_fpclassph128_mask:
813 intrinsicName = "x86.avx512fp16.fpclass.ph.128";
814 break;
815 case X86::BI__builtin_ia32_fpclassph256_mask:
816 intrinsicName = "x86.avx512fp16.fpclass.ph.256";
817 break;
818 case X86::BI__builtin_ia32_fpclassph512_mask:
819 intrinsicName = "x86.avx512fp16.fpclass.ph.512";
820 break;
821 case X86::BI__builtin_ia32_fpclassps128_mask:
822 intrinsicName = "x86.avx512.fpclass.ps.128";
823 break;
824 case X86::BI__builtin_ia32_fpclassps256_mask:
825 intrinsicName = "x86.avx512.fpclass.ps.256";
826 break;
827 case X86::BI__builtin_ia32_fpclassps512_mask:
828 intrinsicName = "x86.avx512.fpclass.ps.512";
829 break;
830 case X86::BI__builtin_ia32_fpclasspd128_mask:
831 intrinsicName = "x86.avx512.fpclass.pd.128";
832 break;
833 case X86::BI__builtin_ia32_fpclasspd256_mask:
834 intrinsicName = "x86.avx512.fpclass.pd.256";
835 break;
836 case X86::BI__builtin_ia32_fpclasspd512_mask:
837 intrinsicName = "x86.avx512.fpclass.pd.512";
838 break;
839 }
840
841 auto cmpResultTy = cir::VectorType::getFixed(builder.getSIntNTy(1), numElts);
842 mlir::Value fpclass =
843 builder.emitIntrinsicCallOp(loc, intrinsicName, cmpResultTy, ops);
844 return emitX86MaskedCompareResult(builder, fpclass, numElts, maskIn, loc);
845}
846
847static mlir::Value emitX86Aes(CIRGenBuilderTy &builder, mlir::Location loc,
848 llvm::StringRef intrinsicName, mlir::Type retType,
850 // Create return struct type and call intrinsic function.
851 mlir::Type vecType =
852 mlir::cast<cir::PointerType>(ops[0].getType()).getPointee();
853 mlir::Type rstMembers[] = {retType, vecType};
854 cir::RecordType rstRecTy =
855 builder.getAnonRecordTy(rstMembers, /*packed=*/false,
857 mlir::Value rstValueRec = builder.emitIntrinsicCallOp(
858 loc, intrinsicName, rstRecTy, mlir::ValueRange{ops[1], ops[2]});
859
860 // Extract the first return value and truncate it to 1 bit, then cast result
861 // to bool value.
862 mlir::Value flag =
863 cir::ExtractMemberOp::create(builder, loc, rstValueRec, /*index=*/0);
864 mlir::Value flagBit0 = builder.createCast(loc, cir::CastKind::integral, flag,
865 builder.getUIntNTy(1));
866 mlir::Value succ = builder.createCast(loc, cir::CastKind::int_to_bool,
867 flagBit0, builder.getBoolTy());
868
869 // Extract the second return value, store it to output address if success.
870 mlir::Value out =
871 cir::ExtractMemberOp::create(builder, loc, rstValueRec, /*index=*/1);
872 Address outAddr(ops[0], /*align=*/CharUnits::fromQuantity(16));
873 cir::IfOp::create(
874 builder, loc, succ, /*withElseRegion=*/true,
875 /*thenBuilder=*/
876 [&](mlir::OpBuilder &b, mlir::Location) {
877 builder.createStore(loc, out, outAddr);
878 builder.createYield(loc);
879 },
880 /*elseBuilder=*/
881 [&](mlir::OpBuilder &b, mlir::Location) {
882 mlir::Value zero = builder.getNullValue(vecType, loc);
883 builder.createStore(loc, zero, outAddr);
884 builder.createYield(loc);
885 });
886
887 return cir::ExtractMemberOp::create(builder, loc, rstValueRec, /*index=*/0);
888}
889
890static mlir::Value emitX86Aeswide(CIRGenBuilderTy &builder, mlir::Location loc,
891 llvm::StringRef intrinsicName,
892 mlir::Type retType,
894 mlir::Type vecType =
895 mlir::cast<cir::PointerType>(ops[1].getType()).getPointee();
896
897 // Create struct for return type and load input arguments, then call
898 // intrinsic function.
899 mlir::Type recTypes[9] = {retType, vecType, vecType, vecType, vecType,
900 vecType, vecType, vecType, vecType};
901 mlir::Value arguments[9];
902 arguments[0] = ops[2];
903 for (int i = 0; i < 8; i++) {
904 // Loading each vector argument from input address.
905 cir::ConstantOp idx = builder.getUInt32(i, loc);
906 mlir::Value nextInElePtr =
907 builder.getArrayElement(loc, loc, ops[1], vecType, idx,
908 /*shouldDecay=*/false);
909 arguments[i + 1] =
910 builder.createAlignedLoad(loc, vecType, nextInElePtr,
911 /*align=*/CharUnits::fromQuantity(16));
912 }
913 cir::RecordType rstRecTy = builder.getAnonRecordTy(
914 recTypes, /*packed=*/false, cir::RecordType::getAllDataKinds(recTypes));
915 mlir::Value rstValueRec =
916 builder.emitIntrinsicCallOp(loc, intrinsicName, rstRecTy, arguments);
917
918 // Extract the first return value and truncate it to 1 bit, then cast result
919 // to bool value.
920 mlir::Value flag =
921 cir::ExtractMemberOp::create(builder, loc, rstValueRec, /*index=*/0);
922 mlir::Value flagBit0 = builder.createCast(loc, cir::CastKind::integral, flag,
923 builder.getUIntNTy(1));
924 mlir::Value succ = builder.createCast(loc, cir::CastKind::int_to_bool,
925 flagBit0, builder.getBoolTy());
926
927 // Extract other return values, store those to output address if success.
928 cir::IfOp::create(
929 builder, loc, succ, /*withElseRegion=*/true,
930 /*thenBuilder=*/
931 [&](mlir::OpBuilder &b, mlir::Location) {
932 for (int i = 0; i < 8; i++) {
933 mlir::Value out =
934 cir::ExtractMemberOp::create(builder, loc, rstValueRec,
935 /*index=*/i + 1);
936 cir::ConstantOp idx = builder.getUInt32(i, loc);
937 mlir::Value nextOutEleAddr =
938 builder.getArrayElement(loc, loc, ops[0], vecType, idx,
939 /*shouldDecay=*/false);
940 Address outAddr(nextOutEleAddr,
941 /*align=*/CharUnits::fromQuantity(16));
942 builder.createStore(loc, out, outAddr);
943 }
944 builder.createYield(loc);
945 },
946 /*elseBuilder=*/
947 [&](mlir::OpBuilder &b, mlir::Location) {
948 mlir::Value zero = builder.getNullValue(vecType, loc);
949 for (int i = 0; i < 8; i++) {
950 cir::ConstantOp idx = builder.getUInt32(i, loc);
951 mlir::Value nextOutEleAddr =
952 builder.getArrayElement(loc, loc, ops[0], vecType, idx,
953 /*shouldDecay=*/false);
954 Address outAddr(nextOutEleAddr,
955 /*align=*/CharUnits::fromQuantity(16));
956 builder.createStore(loc, zero, outAddr);
957 }
958 builder.createYield(loc);
959 });
960
961 return cir::ExtractMemberOp::create(builder, loc, rstValueRec, /*index=*/0);
962}
963
964static mlir::Value emitX86MaskedLoad(CIRGenBuilderTy &builder,
966 llvm::Align alignment,
967 mlir::Location loc) {
968 mlir::Type ty = ops[1].getType();
969 mlir::Value ptr = ops[0];
970 mlir::Value maskVec = getMaskVecValue(
971 builder, loc, ops[2], cast<cir::VectorType>(ty).getFixedSize());
972
973 return builder.createMaskedLoad(loc, ty, ptr, alignment, maskVec, ops[1]);
974}
975
976static mlir::Value emitX86VPerm2f128(CIRGenBuilderTy &builder,
977 mlir::Location loc,
979 auto inputType = cast<cir::VectorType>(ops[0].getType());
980 assert(!inputType.isScalable() &&
981 "This is only intended for fixed-width vectors");
982
984 mlir::Value zeroVec = builder.getZero(loc, inputType);
985
986 // If both lanes are zero, return a zero result.
987 if ((imm & 0x80) && (imm & 0x08))
988 return zeroVec;
989
990 mlir::Value lanes[2];
992
993 cir::IntType i32Ty = builder.getSInt32Ty();
994 const unsigned numElts = inputType.getFixedSize();
995 // We must evaluated each lane(128 bits) separetely
996 for (auto lane : llvm::seq(0, 2)) {
997 bool isZeroBit = imm & (1 << ((lane * 4) + 3)),
998 isSourceB = imm & (1 << ((lane * 4) + 1)),
999 isUpperHalf = imm & (1 << (lane * 4));
1000
1001 // Determine the source for this lane
1002 if (isZeroBit)
1003 lanes[lane] = zeroVec;
1004 else
1005 lanes[lane] = isSourceB ? ops[1] : ops[0];
1006
1007 // We need to built the shuffle mask selecting the right half
1008 for (auto elt : llvm::seq(0u, numElts / 2u)) {
1009 unsigned idx = (lane * numElts) + elt;
1010 if (isUpperHalf)
1011 idx += numElts / 2;
1012 mask.push_back(cir::IntAttr::get(i32Ty, idx));
1013 }
1014 }
1015
1016 return builder.createVecShuffle(loc, lanes[0], lanes[1], mask);
1017}
1018
1019static mlir::Value emitX86PackedByteShift(CIRGenBuilderTy &builder,
1020 unsigned builtinID,
1021 mlir::Location loc,
1023 llvm::Boolean isLeftShift) {
1024 auto byteVecType = cast<cir::VectorType>(ops[0].getType());
1025 assert(!byteVecType.isScalable() &&
1026 "This is only intended for fixed-width vectors");
1027
1028 unsigned shiftVal = CIRGenFunction::getZExtIntValueFromConstOp(ops[1]) & 0xFF;
1029 mlir::Value zeroVector = builder.getZero(loc, byteVecType);
1030
1031 // If pslldq is shifting the vector more than 15 bytes, emit zero.
1032 // This matches the hardware behavior where shifting by 16+ bytes
1033 // clears the entire 128-bit lane.
1034 if (shiftVal >= 16)
1035 return zeroVector;
1036
1037 uint64_t numElts = byteVecType.getFixedSize();
1038 assert(numElts % 16 == 0 && "Expected a multiple of 16");
1039
1041
1042 constexpr unsigned laneSize = 16;
1043 const int switchOperand = numElts - laneSize;
1044
1045 // 256/512-bit pslldq/psrldq operates on 128-bit lanes so we need to
1046 // handle that
1047 for (auto laneOffset = 0ull; laneOffset < numElts; laneOffset += laneSize) {
1048 for (auto elt : llvm::seq<unsigned>(0, laneSize)) {
1049 unsigned idx =
1050 isLeftShift ? (numElts + elt - shiftVal) : (elt + shiftVal);
1051
1052 bool isZeroPadding = isLeftShift ? (idx < numElts) : (idx >= laneSize);
1053 if (isZeroPadding)
1054 idx += isLeftShift ? (-switchOperand) : switchOperand;
1055
1056 shuffleMask.push_back(idx + laneOffset);
1057 }
1058 }
1059
1060 // Perform the shuffle
1061 // (left concatenating zeros on left, right concatenating zeros on right)
1062 auto [firstOperand, secondOperand] = isLeftShift
1063 ? std::make_pair(zeroVector, ops[0])
1064 : std::make_pair(ops[0], zeroVector);
1065
1066 // Mask the result using circular arithmetic on concatenated buffer
1067 mlir::Value shuffleResult =
1068 builder.createVecShuffle(loc, firstOperand, secondOperand, shuffleMask);
1069
1070 return shuffleResult;
1071}
1072
1074 const Expr *cpuExpr = expr->getArg(0)->IgnoreParenCasts();
1075 StringRef cpuStr = cast<clang::StringLiteral>(cpuExpr)->getString();
1076 return emitX86CpuIs(getLoc(expr->getExprLoc()), cpuStr);
1077}
1078
1079cir::GetGlobalOp CIRGenFunction::createGetCpuModel(mlir::Location loc) {
1080 mlir::Type u32 = builder.getUInt32Ty();
1081 auto cpuModel =
1082 mlir::dyn_cast_or_null<cir::GlobalOp>(cgm.getGlobalValue("__cpu_model"));
1083
1084 if (!cpuModel) {
1085 // Matching the struct layout from the compiler-rt/libgcc structure that is
1086 // filled in:
1087 // unsigned int __cpu_vendor;
1088 // unsigned int __cpu_type;
1089 // unsigned int __cpu_subtype;
1090 // unsigned int __cpu_features[1];
1091 mlir::Type tys[] = {u32, u32, u32, cir::ArrayType::get(u32, 1)};
1092 mlir::Type modelTy = builder.getAnonRecordTy(
1093 tys, /*packed=*/false, cir::RecordType::getAllDataKinds(tys));
1094 cpuModel =
1095 cgm.createGlobalOp(loc, "__cpu_model", modelTy, /*isConstant=*/false);
1096 cpuModel.setDsoLocal(true);
1097 }
1098
1099 return cir::GetGlobalOp::create(builder, loc,
1100 builder.getPointerTo(cpuModel.getSymType()),
1101 cpuModel.getSymName());
1102}
1103
1104cir::GetGlobalOp CIRGenFunction::createGetCpuFeatures2(mlir::Location loc) {
1105 mlir::Type u32 = builder.getUInt32Ty();
1106 auto cpuFeatures2 = mlir::dyn_cast_or_null<cir::GlobalOp>(
1107 cgm.getGlobalValue("__cpu_features2"));
1108
1109 if (!cpuFeatures2) {
1110 // This is just an array of 3 uint32s.
1111 mlir::Type arrTy = cir::ArrayType::get(u32, 3);
1112 cpuFeatures2 =
1113 cgm.createGlobalOp(loc, "__cpu_features2", arrTy, /*isConstant=*/false);
1114 cpuFeatures2.setDsoLocal(true);
1115 }
1116
1117 return cir::GetGlobalOp::create(
1118 builder, loc, builder.getPointerTo(cpuFeatures2.getSymType()),
1119 cpuFeatures2.getSymName());
1120}
1121
1122mlir::Value CIRGenFunction::emitX86CpuIs(mlir::Location loc, StringRef cpuStr) {
1123 mlir::Type u32 = builder.getUInt32Ty();
1124 // Calculate the index needed to access the correct field based on the
1125 // range. ABI_VALUE matches with compiler-rt/libgcc values.
1126 auto [fieldName, index, value] =
1127 llvm::StringSwitch<std::tuple<llvm::StringLiteral, unsigned, unsigned>>(
1128 cpuStr)
1129#define X86_VENDOR(ENUM, STRING, ABI_VALUE) \
1130 .Case(STRING, {"__cpu_vendor", 0u, ABI_VALUE})
1131#define X86_CPU_TYPE(ENUM, STR, ABI_VALUE) \
1132 .Case(STR, {"__cpu_type", 1u, ABI_VALUE})
1133#define X86_CPU_SUBTYPE(ENUM, STR, ABI_VALUE) \
1134 .Case(STR, {"__cpu_subtype", 2u, ABI_VALUE})
1135#include "llvm/TargetParser/X86TargetParser.def"
1136 .Default({"", 0, 0});
1137 assert(value != 0 && "Invalid CPUStr passed to CpuIs");
1138
1139 cir::GetGlobalOp getCpuModel = createGetCpuModel(loc);
1140
1141 // Note: the StringSwitch above ONLY has the ability to get the first 3
1142 // fields, so we don't have to worry about it being the array field. So we
1143 // can continue assuming everything is an int.
1144 cir::GetMemberOp cpuValuePtr = builder.createGetMember(
1145 loc, builder.getPointerTo(u32), getCpuModel, fieldName, index);
1146 cir::LoadOp getVal = builder.createAlignedLoad(loc, u32, cpuValuePtr,
1148
1149 return cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, getVal,
1150 builder.getUInt32(value, loc));
1151}
1152
1154 const Expr *featureExpr = expr->getArg(0)->IgnoreParenCasts();
1155 StringRef featureStr = cast<StringLiteral>(featureExpr)->getString();
1156 if (!getContext().getTargetInfo().validateCpuSupports(featureStr))
1157 return builder.getFalse(getLoc(expr->getExprLoc()));
1158 return emitX86CpuSupports(getLoc(expr->getExprLoc()), featureStr);
1159}
1160
1161mlir::Value
1163 ArrayRef<StringRef> featureStrs) {
1164 return emitX86CpuSupports(loc, llvm::X86::getCpuSupportsMask(featureStrs));
1165}
1166
1167mlir::Value
1169 std::array<uint32_t, 4> featureMask) {
1170 mlir::Type u32 = builder.getUInt32Ty();
1171 mlir::Value result;
1172
1173 auto addCondition = [&](unsigned maskVal, mlir::Value features) {
1174 cir::ConstantOp mask = builder.getUInt32(maskVal, loc);
1175 mlir::Value bitset = builder.createAnd(loc, features, mask);
1176 mlir::Value cmp =
1177 cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, bitset, mask);
1178
1179 if (result)
1180 result = builder.createAnd(loc, result, cmp);
1181 else
1182 result = cmp;
1183 };
1184
1185 // only check the '__cpu_features[0]' if we have a non-zero value. A zero
1186 // here means to JUST check __cpu_features2.
1187 if (featureMask[0] != 0) {
1188 cir::GetGlobalOp getCpuModel = createGetCpuModel(loc);
1189 mlir::Type u32 = builder.getUInt32Ty();
1190 auto arrTy = cir::ArrayType::get(u32, 1);
1191
1192 // Pick out the __cpu_features field, the 4th field in the struct.
1193 cir::GetMemberOp cpuFeatArrPtr = builder.createGetMember(
1194 loc, builder.getPointerTo(arrTy), getCpuModel, "__cpu_features", 3);
1195
1196 mlir::Value cpuFeatVal = builder.getArrayElement(
1197 loc, loc, cpuFeatArrPtr, arrTy,
1198 /*idx=*/builder.getUInt32(0, loc), /*shouldDecay=*/true,
1200 cir::LoadOp features = builder.createAlignedLoad(
1201 loc, u32, cpuFeatVal, CharUnits::fromQuantity(4));
1202
1203 addCondition(featureMask[0], features);
1204 }
1205
1206 // the 0th index is looked up in the __cpu_model field, the rest come from an
1207 // array.
1208 cir::GetGlobalOp getCpuFeatures2; // = createGetCpuFeatures2(loc);
1209 for (int i = 1; i != 4; ++i) {
1210 const uint32_t val = featureMask[i];
1211 if (!val)
1212 continue;
1213 if (!getCpuFeatures2)
1214 getCpuFeatures2 = createGetCpuFeatures2(loc);
1215 mlir::Value getArrayElt = builder.getArrayElement(
1216 loc, loc, getCpuFeatures2,
1217 cast<cir::PointerType>(getCpuFeatures2.getType()).getPointee(),
1218 builder.getUInt32(/*index=*/i - 1, loc),
1219 /*shouldDecay=*/true, cir::PtrOffsetFlags::InBounds);
1220 cir::LoadOp features = builder.createAlignedLoad(
1221 loc, u32, getArrayElt, CharUnits::fromQuantity(4));
1222
1223 addCondition(val, features);
1224 }
1225
1226 return result;
1227}
1228
1229mlir::Value CIRGenFunction::emitX86CpuInit(mlir::Location loc) {
1230 cir::FuncOp initFunc =
1231 cgm.createRuntimeFunction(builder.getVoidFnTy(), "__cpu_indicator_init");
1232 initFunc.setDsoLocal(true);
1234
1235 return builder.createCallOp(loc, initFunc, {}).getResult();
1236}
1237
1239 const CallExpr *E) {
1240 switch (builtinID) {
1241 case MSVCIntrin::_BitScanForward:
1242 case MSVCIntrin::_BitScanReverse: {
1243 Address indexAddress(emitPointerWithAlignment(E->getArg(0)));
1244 mlir::Value argValue = emitScalarExpr(E->getArg(1));
1245
1246 mlir::Location loc = getLoc(E->getExprLoc());
1247 mlir::Type argType = argValue.getType();
1248 mlir::Type indexType = indexAddress.getElementType();
1249 mlir::Type resultType = convertType(E->getType());
1250
1251 mlir::Value argZero = builder.getNullValue(argType, loc);
1252 mlir::Value isZero =
1253 cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, argValue, argZero);
1254 cir::IfOp::create(
1255 builder, loc, isZero, /*withElseRegion=*/true,
1256 [&](mlir::OpBuilder &b, mlir::Location) { builder.createYield(loc); },
1257 [&](mlir::OpBuilder &b, mlir::Location) {
1258 if (builtinID == MSVCIntrin::_BitScanForward) {
1259 mlir::Value zeroCount =
1260 cir::BitCtzOp::create(builder, loc, argValue, true);
1261 mlir::Value castCount = builder.createCast(
1262 loc, cir::CastKind::integral, zeroCount, indexType);
1263 builder.createStore(loc, castCount, indexAddress, false);
1264 } else {
1265 unsigned argWidth = mlir::cast<cir::IntType>(argType).getWidth();
1266 mlir::Value argTypeLastIndex =
1267 builder.getConstantInt(loc, indexType, argWidth - 1);
1268
1269 mlir::Value zeroCount =
1270 cir::BitClzOp::create(builder, loc, argValue, true);
1271 mlir::Value castCount = builder.createCast(
1272 loc, cir::CastKind::integral, zeroCount, indexType);
1273 mlir::Value index =
1274 builder.createNSWSub(loc, argTypeLastIndex, castCount);
1275 builder.createStore(loc, index, indexAddress, false);
1276 }
1277 builder.createYield(loc);
1278 });
1279 mlir::Value notZero = builder.createNot(loc, isZero);
1280 return builder.createCast(loc, cir::CastKind::bool_to_int, notZero,
1281 resultType);
1282 }
1283 case MSVCIntrin::_CopyDoubleFromInt64:
1284 case MSVCIntrin::_CopyFloatFromInt32:
1285 case MSVCIntrin::_CopyInt32FromFloat:
1286 case MSVCIntrin::_CopyInt64FromDouble:
1287 case MSVCIntrin::_CountLeadingOnes:
1288 case MSVCIntrin::_CountLeadingZeros:
1289 case MSVCIntrin::_CountOneBits:
1290 case MSVCIntrin::_CountTrailingZeros:
1291 case MSVCIntrin::_InterlockedAdd:
1292 case MSVCIntrin::_InterlockedAdd_acq:
1293 case MSVCIntrin::_InterlockedAdd_rel:
1294 case MSVCIntrin::_InterlockedAdd_nf:
1295 case MSVCIntrin::__prefetch:
1296 case MSVCIntrin::_InterlockedAnd:
1297 case MSVCIntrin::_InterlockedExchange:
1298 case MSVCIntrin::_InterlockedExchangeAdd:
1299 case MSVCIntrin::_InterlockedExchangeSub:
1300 case MSVCIntrin::_InterlockedOr:
1301 case MSVCIntrin::_InterlockedXor:
1302 case MSVCIntrin::_InterlockedExchangeAdd_acq:
1303 case MSVCIntrin::_InterlockedExchangeAdd_rel:
1304 case MSVCIntrin::_InterlockedExchangeAdd_nf:
1305 case MSVCIntrin::_InterlockedExchange_acq:
1306 case MSVCIntrin::_InterlockedExchange_rel:
1307 case MSVCIntrin::_InterlockedExchange_nf:
1308 case MSVCIntrin::_InterlockedCompareExchange:
1309 case MSVCIntrin::_InterlockedCompareExchange_acq:
1310 case MSVCIntrin::_InterlockedCompareExchange_rel:
1311 case MSVCIntrin::_InterlockedCompareExchange_nf:
1312 case MSVCIntrin::_InterlockedCompareExchange128:
1313 case MSVCIntrin::_InterlockedCompareExchange128_acq:
1314 case MSVCIntrin::_InterlockedCompareExchange128_rel:
1315 case MSVCIntrin::_InterlockedCompareExchange128_nf:
1316 case MSVCIntrin::_InterlockedOr_acq:
1317 case MSVCIntrin::_InterlockedOr_rel:
1318 case MSVCIntrin::_InterlockedOr_nf:
1319 case MSVCIntrin::_InterlockedXor_acq:
1320 case MSVCIntrin::_InterlockedXor_rel:
1321 case MSVCIntrin::_InterlockedXor_nf:
1322 case MSVCIntrin::_InterlockedAnd_acq:
1323 case MSVCIntrin::_InterlockedAnd_rel:
1324 case MSVCIntrin::_InterlockedAnd_nf:
1325 case MSVCIntrin::_InterlockedIncrement_acq:
1326 case MSVCIntrin::_InterlockedIncrement_rel:
1327 case MSVCIntrin::_InterlockedIncrement_nf:
1328 case MSVCIntrin::_InterlockedDecrement_acq:
1329 case MSVCIntrin::_InterlockedDecrement_rel:
1330 case MSVCIntrin::_InterlockedDecrement_nf:
1331 case MSVCIntrin::_InterlockedDecrement:
1332 case MSVCIntrin::_InterlockedIncrement:
1333 case MSVCIntrin::__fastfail:
1334 cgm.errorNYI(E->getExprLoc(), "MSVC intrinsic");
1335 return {};
1336 case MSVCIntrin::_ReadWriteBarrier:
1337 cir::AtomicFenceOp::create(
1338 builder, getLoc(E->getExprLoc()), cir::MemOrder::SequentiallyConsistent,
1339 cir::SyncScopeKindAttr::get(&getMLIRContext(),
1340 cir::SyncScopeKind::SingleThread));
1341 return mlir::Value{};
1342 }
1343 llvm_unreachable("Incorrect MSVC intrinsic!");
1344}
1345
1346std::optional<mlir::Value>
1348 if (builtinID == Builtin::BI__builtin_cpu_is)
1349 return emitX86CpuIs(expr);
1350 if (builtinID == Builtin::BI__builtin_cpu_supports)
1351 return emitX86CpuSupports(expr);
1352 if (builtinID == Builtin::BI__builtin_cpu_init)
1353 return emitX86CpuInit(getLoc(expr->getExprLoc()));
1354
1355 // Handle MSVC intrinsics before argument evaluation to prevent double
1356 // evaluation.
1357
1358 if (std::optional<MSVCIntrin> MsvcIntId =
1360 return emitX86MSVCIntrin(*MsvcIntId, expr);
1361
1362 // Find out if any arguments are required to be integer constant expressions.
1364
1365 // The operands of the builtin call
1367 bool isMaskFCmp = false;
1368
1369 // `ICEArguments` is a bitmap indicating whether the argument at the i-th bit
1370 // is required to be a constant integer expression.
1371 unsigned iceArguments = 0;
1373 getContext().GetBuiltinType(builtinID, error, &iceArguments);
1374 assert(error == ASTContext::GE_None && "Error while getting builtin type.");
1375
1376 for (auto [idx, arg] : llvm::enumerate(expr->arguments()))
1377 ops.push_back(emitScalarOrConstFoldImmArg(iceArguments, idx, arg));
1378
1379 CIRGenBuilderTy &builder = getBuilder();
1380 mlir::Type voidTy = builder.getVoidTy();
1381
1382 switch (builtinID) {
1383 default:
1384 return std::nullopt;
1385 case X86::BI_mm_clflush:
1386 return builder.emitIntrinsicCallOp(getLoc(expr->getExprLoc()),
1387 "x86.sse2.clflush", voidTy, ops[0]);
1388 case X86::BI_mm_lfence:
1389 return builder.emitIntrinsicCallOp(getLoc(expr->getExprLoc()),
1390 "x86.sse2.lfence", voidTy);
1391 case X86::BI_mm_pause:
1392 return builder.emitIntrinsicCallOp(getLoc(expr->getExprLoc()),
1393 "x86.sse2.pause", voidTy);
1394 case X86::BI_mm_mfence:
1395 return builder.emitIntrinsicCallOp(getLoc(expr->getExprLoc()),
1396 "x86.sse2.mfence", voidTy);
1397 case X86::BI_mm_sfence:
1398 return builder.emitIntrinsicCallOp(getLoc(expr->getExprLoc()),
1399 "x86.sse.sfence", voidTy);
1400 case X86::BI_mm_prefetch:
1401 case X86::BI_m_prefetch:
1402 case X86::BI_m_prefetchw:
1403 return emitPrefetch(*this, builtinID, expr, ops);
1404 case X86::BI__rdtsc:
1405 return builder.emitIntrinsicCallOp(getLoc(expr->getExprLoc()), "x86.rdtsc",
1406 builder.getUInt64Ty());
1407 case X86::BI__builtin_ia32_rdtscp: {
1408 mlir::Location loc = getLoc(expr->getExprLoc());
1409 mlir::Type i64Ty = builder.getUInt64Ty();
1410 mlir::Type i32Ty = builder.getUInt32Ty();
1411 mlir::Type members[] = {i64Ty, i32Ty};
1412 mlir::Type structTy = builder.getAnonRecordTy(
1413 members, /*packed=*/false, cir::RecordType::getAllDataKinds(members));
1414 mlir::Value result =
1415 builder.emitIntrinsicCallOp(loc, "x86.rdtscp", structTy);
1416
1417 // Extract and store processor_id (element 1 of the returned struct)
1418 mlir::Value processorId =
1419 cir::ExtractMemberOp::create(builder, loc, i32Ty, result, 1);
1420 // ops[0] is the address to store the processor ID
1421 builder.createStore(loc, processorId, Address{ops[0], CharUnits::One()});
1422
1423 // Return timestamp (element 0 of the returned struct)
1424 return cir::ExtractMemberOp::create(builder, loc, i64Ty, result, 0);
1425 }
1426 case X86::BI__builtin_ia32_roundps:
1427 case X86::BI__builtin_ia32_roundpd:
1428 case X86::BI__builtin_ia32_roundps256:
1429 case X86::BI__builtin_ia32_roundpd256:
1430 case X86::BI__builtin_ia32_roundss:
1431 case X86::BI__builtin_ia32_roundsd:
1432 return emitX86Round(*this, builtinID, expr, ops);
1433 case X86::BI__builtin_ia32_lzcnt_u16:
1434 case X86::BI__builtin_ia32_lzcnt_u32:
1435 case X86::BI__builtin_ia32_lzcnt_u64:
1436 return cir::BitClzOp::create(builder, getLoc(expr->getExprLoc()), ops[0],
1437 /*poisonZero=*/false)
1438 .getResult();
1439 case X86::BI__builtin_ia32_tzcnt_u16:
1440 case X86::BI__builtin_ia32_tzcnt_u32:
1441 case X86::BI__builtin_ia32_tzcnt_u64:
1442 return cir::BitCtzOp::create(builder, getLoc(expr->getExprLoc()), ops[0],
1443 /*poisonZero=*/false)
1444 .getResult();
1445 case X86::BI__builtin_ia32_undef128:
1446 case X86::BI__builtin_ia32_undef256:
1447 case X86::BI__builtin_ia32_undef512:
1448 // The x86 definition of "undef" is not the same as the LLVM definition
1449 // (PR32176). We leave optimizing away an unnecessary zero constant to the
1450 // IR optimizer and backend.
1451 // TODO: If we had a "freeze" IR instruction to generate a fixed undef
1452 // value, we should use that here instead of a zero.
1453 return builder.getNullValue(convertType(expr->getType()),
1454 getLoc(expr->getExprLoc()));
1455 case X86::BI__builtin_ia32_vec_ext_v4hi:
1456 case X86::BI__builtin_ia32_vec_ext_v16qi:
1457 case X86::BI__builtin_ia32_vec_ext_v8hi:
1458 case X86::BI__builtin_ia32_vec_ext_v4si:
1459 case X86::BI__builtin_ia32_vec_ext_v4sf:
1460 case X86::BI__builtin_ia32_vec_ext_v2di:
1461 case X86::BI__builtin_ia32_vec_ext_v32qi:
1462 case X86::BI__builtin_ia32_vec_ext_v16hi:
1463 case X86::BI__builtin_ia32_vec_ext_v8si:
1464 case X86::BI__builtin_ia32_vec_ext_v4di: {
1465 unsigned numElts = cast<cir::VectorType>(ops[0].getType()).getFixedSize();
1466
1467 uint64_t index = getZExtIntValueFromConstOp(ops[1]);
1468 index &= numElts - 1;
1469
1470 cir::ConstantOp indexVal =
1471 builder.getUInt64(index, getLoc(expr->getExprLoc()));
1472
1473 // These builtins exist so we can ensure the index is an ICE and in range.
1474 // Otherwise we could just do this in the header file.
1475 return cir::VecExtractOp::create(builder, getLoc(expr->getExprLoc()),
1476 ops[0], indexVal);
1477 }
1478 case X86::BI__builtin_ia32_vec_set_v4hi:
1479 case X86::BI__builtin_ia32_vec_set_v16qi:
1480 case X86::BI__builtin_ia32_vec_set_v8hi:
1481 case X86::BI__builtin_ia32_vec_set_v4si:
1482 case X86::BI__builtin_ia32_vec_set_v2di:
1483 case X86::BI__builtin_ia32_vec_set_v32qi:
1484 case X86::BI__builtin_ia32_vec_set_v16hi:
1485 case X86::BI__builtin_ia32_vec_set_v8si:
1486 case X86::BI__builtin_ia32_vec_set_v4di: {
1487 return emitVecInsert(builder, getLoc(expr->getExprLoc()), ops[0], ops[1],
1488 ops[2]);
1489 }
1490 case X86::BI__builtin_ia32_kunpckhi:
1491 return emitX86MaskUnpack(builder, getLoc(expr->getExprLoc()),
1492 "x86.avx512.kunpackb", ops);
1493 case X86::BI__builtin_ia32_kunpcksi:
1494 return emitX86MaskUnpack(builder, getLoc(expr->getExprLoc()),
1495 "x86.avx512.kunpackw", ops);
1496 case X86::BI__builtin_ia32_kunpckdi:
1497 return emitX86MaskUnpack(builder, getLoc(expr->getExprLoc()),
1498 "x86.avx512.kunpackd", ops);
1499 case X86::BI_mm_setcsr:
1500 case X86::BI__builtin_ia32_ldmxcsr: {
1501 mlir::Location loc = getLoc(expr->getExprLoc());
1502 Address tmp = createMemTemp(expr->getArg(0)->getType(), loc);
1503 builder.createStore(loc, ops[0], tmp);
1504 return builder.emitIntrinsicCallOp(loc, "x86.sse.ldmxcsr",
1505 builder.getVoidTy(), tmp.getPointer());
1506 }
1507 case X86::BI_mm_getcsr:
1508 case X86::BI__builtin_ia32_stmxcsr: {
1509 mlir::Location loc = getLoc(expr->getExprLoc());
1510 Address tmp = createMemTemp(expr->getType(), loc);
1511 builder.emitIntrinsicCallOp(loc, "x86.sse.stmxcsr", builder.getVoidTy(),
1512 tmp.getPointer());
1513 return builder.createLoad(loc, tmp);
1514 }
1515 case X86::BI__builtin_ia32_xsave:
1516 case X86::BI__builtin_ia32_xsave64:
1517 case X86::BI__builtin_ia32_xrstor:
1518 case X86::BI__builtin_ia32_xrstor64:
1519 case X86::BI__builtin_ia32_xsaveopt:
1520 case X86::BI__builtin_ia32_xsaveopt64:
1521 case X86::BI__builtin_ia32_xrstors:
1522 case X86::BI__builtin_ia32_xrstors64:
1523 case X86::BI__builtin_ia32_xsavec:
1524 case X86::BI__builtin_ia32_xsavec64:
1525 case X86::BI__builtin_ia32_xsaves:
1526 case X86::BI__builtin_ia32_xsaves64:
1527 case X86::BI__builtin_ia32_xsetbv:
1528 case X86::BI_xsetbv: {
1529 mlir::Location loc = getLoc(expr->getExprLoc());
1530 StringRef intrinsicName;
1531 switch (builtinID) {
1532 default:
1533 llvm_unreachable("Unexpected builtin");
1534 case X86::BI__builtin_ia32_xsave:
1535 intrinsicName = "x86.xsave";
1536 break;
1537 case X86::BI__builtin_ia32_xsave64:
1538 intrinsicName = "x86.xsave64";
1539 break;
1540 case X86::BI__builtin_ia32_xrstor:
1541 intrinsicName = "x86.xrstor";
1542 break;
1543 case X86::BI__builtin_ia32_xrstor64:
1544 intrinsicName = "x86.xrstor64";
1545 break;
1546 case X86::BI__builtin_ia32_xsaveopt:
1547 intrinsicName = "x86.xsaveopt";
1548 break;
1549 case X86::BI__builtin_ia32_xsaveopt64:
1550 intrinsicName = "x86.xsaveopt64";
1551 break;
1552 case X86::BI__builtin_ia32_xrstors:
1553 intrinsicName = "x86.xrstors";
1554 break;
1555 case X86::BI__builtin_ia32_xrstors64:
1556 intrinsicName = "x86.xrstors64";
1557 break;
1558 case X86::BI__builtin_ia32_xsavec:
1559 intrinsicName = "x86.xsavec";
1560 break;
1561 case X86::BI__builtin_ia32_xsavec64:
1562 intrinsicName = "x86.xsavec64";
1563 break;
1564 case X86::BI__builtin_ia32_xsaves:
1565 intrinsicName = "x86.xsaves";
1566 break;
1567 case X86::BI__builtin_ia32_xsaves64:
1568 intrinsicName = "x86.xsaves64";
1569 break;
1570 case X86::BI__builtin_ia32_xsetbv:
1571 case X86::BI_xsetbv:
1572 intrinsicName = "x86.xsetbv";
1573 break;
1574 }
1575
1576 // The xsave family of instructions take a 64-bit mask that specifies
1577 // which processor state components to save/restore. The hardware expects
1578 // this mask split into two 32-bit registers: EDX (high 32 bits) and
1579 // EAX (low 32 bits).
1580 mlir::Type i32Ty = builder.getSInt32Ty();
1581
1582 // Mhi = (uint32_t)(ops[1] >> 32) - extract high 32 bits via right shift
1583 cir::ConstantOp shift32 = builder.getSInt64(32, loc);
1584 mlir::Value mhi = builder.createShift(loc, ops[1], shift32.getResult(),
1585 /*isShiftLeft=*/false);
1586 mhi = builder.createIntCast(mhi, i32Ty);
1587
1588 // Mlo = (uint32_t)ops[1] - extract low 32 bits by truncation
1589 mlir::Value mlo = builder.createIntCast(ops[1], i32Ty);
1590
1591 return builder.emitIntrinsicCallOp(loc, intrinsicName, voidTy,
1592 mlir::ValueRange{ops[0], mhi, mlo});
1593 }
1594 case X86::BI__builtin_ia32_xgetbv:
1595 case X86::BI_xgetbv:
1596 // xgetbv reads the extended control register specified by ops[0] (ECX)
1597 // and returns the 64-bit value
1598 return builder.emitIntrinsicCallOp(getLoc(expr->getExprLoc()), "x86.xgetbv",
1599 builder.getUInt64Ty(), ops[0]);
1600 case X86::BI__builtin_ia32_storedqudi128_mask:
1601 case X86::BI__builtin_ia32_storedqusi128_mask:
1602 case X86::BI__builtin_ia32_storedquhi128_mask:
1603 case X86::BI__builtin_ia32_storedquqi128_mask:
1604 case X86::BI__builtin_ia32_storeupd128_mask:
1605 case X86::BI__builtin_ia32_storeups128_mask:
1606 case X86::BI__builtin_ia32_storedqudi256_mask:
1607 case X86::BI__builtin_ia32_storedqusi256_mask:
1608 case X86::BI__builtin_ia32_storedquhi256_mask:
1609 case X86::BI__builtin_ia32_storedquqi256_mask:
1610 case X86::BI__builtin_ia32_storeupd256_mask:
1611 case X86::BI__builtin_ia32_storeups256_mask:
1612 case X86::BI__builtin_ia32_storedqudi512_mask:
1613 case X86::BI__builtin_ia32_storedqusi512_mask:
1614 case X86::BI__builtin_ia32_storedquhi512_mask:
1615 case X86::BI__builtin_ia32_storedquqi512_mask:
1616 case X86::BI__builtin_ia32_storeupd512_mask:
1617 case X86::BI__builtin_ia32_storeups512_mask:
1618 case X86::BI__builtin_ia32_storesbf16128_mask:
1619 case X86::BI__builtin_ia32_storesh128_mask:
1620 case X86::BI__builtin_ia32_storess128_mask:
1621 case X86::BI__builtin_ia32_storesd128_mask:
1622 cgm.errorNYI(expr->getSourceRange(),
1623 std::string("unimplemented x86 builtin call: ") +
1624 getContext().BuiltinInfo.getName(builtinID));
1625 return mlir::Value{};
1626 case X86::BI__builtin_ia32_cvtmask2b128:
1627 case X86::BI__builtin_ia32_cvtmask2b256:
1628 case X86::BI__builtin_ia32_cvtmask2b512:
1629 case X86::BI__builtin_ia32_cvtmask2w128:
1630 case X86::BI__builtin_ia32_cvtmask2w256:
1631 case X86::BI__builtin_ia32_cvtmask2w512:
1632 case X86::BI__builtin_ia32_cvtmask2d128:
1633 case X86::BI__builtin_ia32_cvtmask2d256:
1634 case X86::BI__builtin_ia32_cvtmask2d512:
1635 case X86::BI__builtin_ia32_cvtmask2q128:
1636 case X86::BI__builtin_ia32_cvtmask2q256:
1637 case X86::BI__builtin_ia32_cvtmask2q512:
1638 return emitX86SExtMask(this->getBuilder(), ops[0],
1639 convertType(expr->getType()),
1640 getLoc(expr->getExprLoc()));
1641 case X86::BI__builtin_ia32_cvtb2mask128:
1642 case X86::BI__builtin_ia32_cvtb2mask256:
1643 case X86::BI__builtin_ia32_cvtb2mask512:
1644 case X86::BI__builtin_ia32_cvtw2mask128:
1645 case X86::BI__builtin_ia32_cvtw2mask256:
1646 case X86::BI__builtin_ia32_cvtw2mask512:
1647 case X86::BI__builtin_ia32_cvtd2mask128:
1648 case X86::BI__builtin_ia32_cvtd2mask256:
1649 case X86::BI__builtin_ia32_cvtd2mask512:
1650 case X86::BI__builtin_ia32_cvtq2mask128:
1651 case X86::BI__builtin_ia32_cvtq2mask256:
1652 case X86::BI__builtin_ia32_cvtq2mask512:
1653 return emitX86ConvertToMask(*this, this->getBuilder(), ops[0],
1654 getLoc(expr->getExprLoc()));
1655 case X86::BI__builtin_ia32_cvtdq2ps512_mask:
1656 case X86::BI__builtin_ia32_cvtqq2ps512_mask:
1657 case X86::BI__builtin_ia32_cvtqq2pd512_mask:
1658 case X86::BI__builtin_ia32_vcvtw2ph512_mask:
1659 case X86::BI__builtin_ia32_vcvtdq2ph512_mask:
1660 case X86::BI__builtin_ia32_vcvtqq2ph512_mask:
1661 case X86::BI__builtin_ia32_cvtudq2ps512_mask:
1662 case X86::BI__builtin_ia32_cvtuqq2ps512_mask:
1663 case X86::BI__builtin_ia32_cvtuqq2pd512_mask:
1664 case X86::BI__builtin_ia32_vcvtuw2ph512_mask:
1665 case X86::BI__builtin_ia32_vcvtudq2ph512_mask:
1666 case X86::BI__builtin_ia32_vcvtuqq2ph512_mask:
1667 case X86::BI__builtin_ia32_vfmaddsh3_mask:
1668 case X86::BI__builtin_ia32_vfmaddss3_mask:
1669 case X86::BI__builtin_ia32_vfmaddsd3_mask:
1670 case X86::BI__builtin_ia32_vfmaddsh3_maskz:
1671 case X86::BI__builtin_ia32_vfmaddss3_maskz:
1672 case X86::BI__builtin_ia32_vfmaddsd3_maskz:
1673 case X86::BI__builtin_ia32_vfmaddsh3_mask3:
1674 case X86::BI__builtin_ia32_vfmaddss3_mask3:
1675 case X86::BI__builtin_ia32_vfmaddsd3_mask3:
1676 case X86::BI__builtin_ia32_vfmsubsh3_mask3:
1677 case X86::BI__builtin_ia32_vfmsubss3_mask3:
1678 case X86::BI__builtin_ia32_vfmsubsd3_mask3:
1679 case X86::BI__builtin_ia32_vfmaddph512_mask:
1680 case X86::BI__builtin_ia32_vfmaddph512_maskz:
1681 case X86::BI__builtin_ia32_vfmaddph512_mask3:
1682 case X86::BI__builtin_ia32_vfmaddps512_mask:
1683 case X86::BI__builtin_ia32_vfmaddps512_maskz:
1684 case X86::BI__builtin_ia32_vfmaddps512_mask3:
1685 case X86::BI__builtin_ia32_vfmsubps512_mask3:
1686 case X86::BI__builtin_ia32_vfmaddpd512_mask:
1687 case X86::BI__builtin_ia32_vfmaddpd512_maskz:
1688 case X86::BI__builtin_ia32_vfmaddpd512_mask3:
1689 case X86::BI__builtin_ia32_vfmsubpd512_mask3:
1690 case X86::BI__builtin_ia32_vfmsubph512_mask3:
1691 case X86::BI__builtin_ia32_vfmaddsubph512_mask:
1692 case X86::BI__builtin_ia32_vfmaddsubph512_maskz:
1693 case X86::BI__builtin_ia32_vfmaddsubph512_mask3:
1694 case X86::BI__builtin_ia32_vfmsubaddph512_mask3:
1695 case X86::BI__builtin_ia32_vfmaddsubps512_mask:
1696 case X86::BI__builtin_ia32_vfmaddsubps512_maskz:
1697 case X86::BI__builtin_ia32_vfmaddsubps512_mask3:
1698 case X86::BI__builtin_ia32_vfmsubaddps512_mask3:
1699 case X86::BI__builtin_ia32_vfmaddsubpd512_mask:
1700 case X86::BI__builtin_ia32_vfmaddsubpd512_maskz:
1701 case X86::BI__builtin_ia32_vfmaddsubpd512_mask3:
1702 case X86::BI__builtin_ia32_vfmsubaddpd512_mask3:
1703 case X86::BI__builtin_ia32_movdqa32store128_mask:
1704 case X86::BI__builtin_ia32_movdqa64store128_mask:
1705 case X86::BI__builtin_ia32_storeaps128_mask:
1706 case X86::BI__builtin_ia32_storeapd128_mask:
1707 case X86::BI__builtin_ia32_movdqa32store256_mask:
1708 case X86::BI__builtin_ia32_movdqa64store256_mask:
1709 case X86::BI__builtin_ia32_storeaps256_mask:
1710 case X86::BI__builtin_ia32_storeapd256_mask:
1711 case X86::BI__builtin_ia32_movdqa32store512_mask:
1712 case X86::BI__builtin_ia32_movdqa64store512_mask:
1713 case X86::BI__builtin_ia32_storeaps512_mask:
1714 case X86::BI__builtin_ia32_storeapd512_mask:
1715 cgm.errorNYI(expr->getSourceRange(),
1716 std::string("unimplemented X86 builtin call: ") +
1717 getContext().BuiltinInfo.getName(builtinID));
1718 return {};
1719
1720 case X86::BI__builtin_ia32_loadups128_mask:
1721 case X86::BI__builtin_ia32_loadups256_mask:
1722 case X86::BI__builtin_ia32_loadups512_mask:
1723 case X86::BI__builtin_ia32_loadupd128_mask:
1724 case X86::BI__builtin_ia32_loadupd256_mask:
1725 case X86::BI__builtin_ia32_loadupd512_mask:
1726 case X86::BI__builtin_ia32_loaddquqi128_mask:
1727 case X86::BI__builtin_ia32_loaddquqi256_mask:
1728 case X86::BI__builtin_ia32_loaddquqi512_mask:
1729 case X86::BI__builtin_ia32_loaddquhi128_mask:
1730 case X86::BI__builtin_ia32_loaddquhi256_mask:
1731 case X86::BI__builtin_ia32_loaddquhi512_mask:
1732 case X86::BI__builtin_ia32_loaddqusi128_mask:
1733 case X86::BI__builtin_ia32_loaddqusi256_mask:
1734 case X86::BI__builtin_ia32_loaddqusi512_mask:
1735 case X86::BI__builtin_ia32_loaddqudi128_mask:
1736 case X86::BI__builtin_ia32_loaddqudi256_mask:
1737 case X86::BI__builtin_ia32_loaddqudi512_mask:
1738 case X86::BI__builtin_ia32_loadsbf16128_mask:
1739 case X86::BI__builtin_ia32_loadsh128_mask:
1740 case X86::BI__builtin_ia32_loadss128_mask:
1741 case X86::BI__builtin_ia32_loadsd128_mask:
1742 return emitX86MaskedLoad(builder, ops, llvm::Align(1),
1743 getLoc(expr->getExprLoc()));
1744
1745 case X86::BI__builtin_ia32_loadaps128_mask:
1746 case X86::BI__builtin_ia32_loadaps256_mask:
1747 case X86::BI__builtin_ia32_loadaps512_mask:
1748 case X86::BI__builtin_ia32_loadapd128_mask:
1749 case X86::BI__builtin_ia32_loadapd256_mask:
1750 case X86::BI__builtin_ia32_loadapd512_mask:
1751 case X86::BI__builtin_ia32_movdqa32load128_mask:
1752 case X86::BI__builtin_ia32_movdqa32load256_mask:
1753 case X86::BI__builtin_ia32_movdqa32load512_mask:
1754 case X86::BI__builtin_ia32_movdqa64load128_mask:
1755 case X86::BI__builtin_ia32_movdqa64load256_mask:
1756 case X86::BI__builtin_ia32_movdqa64load512_mask:
1757 return emitX86MaskedLoad(
1758 builder, ops,
1759 getContext()
1760 .getTypeAlignInChars(expr->getArg(1)->getType())
1761 .getAsAlign(),
1762 getLoc(expr->getExprLoc()));
1763
1764 case X86::BI__builtin_ia32_expandloaddf128_mask:
1765 case X86::BI__builtin_ia32_expandloaddf256_mask:
1766 case X86::BI__builtin_ia32_expandloaddf512_mask:
1767 case X86::BI__builtin_ia32_expandloadsf128_mask:
1768 case X86::BI__builtin_ia32_expandloadsf256_mask:
1769 case X86::BI__builtin_ia32_expandloadsf512_mask:
1770 case X86::BI__builtin_ia32_expandloaddi128_mask:
1771 case X86::BI__builtin_ia32_expandloaddi256_mask:
1772 case X86::BI__builtin_ia32_expandloaddi512_mask:
1773 case X86::BI__builtin_ia32_expandloadsi128_mask:
1774 case X86::BI__builtin_ia32_expandloadsi256_mask:
1775 case X86::BI__builtin_ia32_expandloadsi512_mask:
1776 case X86::BI__builtin_ia32_expandloadhi128_mask:
1777 case X86::BI__builtin_ia32_expandloadhi256_mask:
1778 case X86::BI__builtin_ia32_expandloadhi512_mask:
1779 case X86::BI__builtin_ia32_expandloadqi128_mask:
1780 case X86::BI__builtin_ia32_expandloadqi256_mask:
1781 case X86::BI__builtin_ia32_expandloadqi512_mask: {
1782 cgm.errorNYI(expr->getSourceRange(),
1783 std::string("unimplemented X86 builtin call: ") +
1784 getContext().BuiltinInfo.getName(builtinID));
1785 return {};
1786 }
1787 case X86::BI__builtin_ia32_compressstoredf128_mask:
1788 case X86::BI__builtin_ia32_compressstoredf256_mask:
1789 case X86::BI__builtin_ia32_compressstoredf512_mask:
1790 case X86::BI__builtin_ia32_compressstoresf128_mask:
1791 case X86::BI__builtin_ia32_compressstoresf256_mask:
1792 case X86::BI__builtin_ia32_compressstoresf512_mask:
1793 case X86::BI__builtin_ia32_compressstoredi128_mask:
1794 case X86::BI__builtin_ia32_compressstoredi256_mask:
1795 case X86::BI__builtin_ia32_compressstoredi512_mask:
1796 case X86::BI__builtin_ia32_compressstoresi128_mask:
1797 case X86::BI__builtin_ia32_compressstoresi256_mask:
1798 case X86::BI__builtin_ia32_compressstoresi512_mask:
1799 case X86::BI__builtin_ia32_compressstorehi128_mask:
1800 case X86::BI__builtin_ia32_compressstorehi256_mask:
1801 case X86::BI__builtin_ia32_compressstorehi512_mask:
1802 case X86::BI__builtin_ia32_compressstoreqi128_mask:
1803 case X86::BI__builtin_ia32_compressstoreqi256_mask:
1804 case X86::BI__builtin_ia32_compressstoreqi512_mask:
1805 return emitX86CompressStore(builder, getLoc(expr->getExprLoc()), ops);
1806 case X86::BI__builtin_ia32_expanddf128_mask:
1807 case X86::BI__builtin_ia32_expanddf256_mask:
1808 case X86::BI__builtin_ia32_expanddf512_mask:
1809 case X86::BI__builtin_ia32_expandsf128_mask:
1810 case X86::BI__builtin_ia32_expandsf256_mask:
1811 case X86::BI__builtin_ia32_expandsf512_mask:
1812 case X86::BI__builtin_ia32_expanddi128_mask:
1813 case X86::BI__builtin_ia32_expanddi256_mask:
1814 case X86::BI__builtin_ia32_expanddi512_mask:
1815 case X86::BI__builtin_ia32_expandsi128_mask:
1816 case X86::BI__builtin_ia32_expandsi256_mask:
1817 case X86::BI__builtin_ia32_expandsi512_mask:
1818 case X86::BI__builtin_ia32_expandhi128_mask:
1819 case X86::BI__builtin_ia32_expandhi256_mask:
1820 case X86::BI__builtin_ia32_expandhi512_mask:
1821 case X86::BI__builtin_ia32_expandqi128_mask:
1822 case X86::BI__builtin_ia32_expandqi256_mask:
1823 case X86::BI__builtin_ia32_expandqi512_mask: {
1824 mlir::Location loc = getLoc(expr->getExprLoc());
1825 return emitX86CompressExpand(builder, loc, ops[0], ops[1], ops[2],
1826 "x86.avx512.mask.expand");
1827 }
1828 case X86::BI__builtin_ia32_compressdf128_mask:
1829 case X86::BI__builtin_ia32_compressdf256_mask:
1830 case X86::BI__builtin_ia32_compressdf512_mask:
1831 case X86::BI__builtin_ia32_compresssf128_mask:
1832 case X86::BI__builtin_ia32_compresssf256_mask:
1833 case X86::BI__builtin_ia32_compresssf512_mask:
1834 case X86::BI__builtin_ia32_compressdi128_mask:
1835 case X86::BI__builtin_ia32_compressdi256_mask:
1836 case X86::BI__builtin_ia32_compressdi512_mask:
1837 case X86::BI__builtin_ia32_compresssi128_mask:
1838 case X86::BI__builtin_ia32_compresssi256_mask:
1839 case X86::BI__builtin_ia32_compresssi512_mask:
1840 case X86::BI__builtin_ia32_compresshi128_mask:
1841 case X86::BI__builtin_ia32_compresshi256_mask:
1842 case X86::BI__builtin_ia32_compresshi512_mask:
1843 case X86::BI__builtin_ia32_compressqi128_mask:
1844 case X86::BI__builtin_ia32_compressqi256_mask:
1845 case X86::BI__builtin_ia32_compressqi512_mask: {
1846 mlir::Location loc = getLoc(expr->getExprLoc());
1847 return emitX86CompressExpand(builder, loc, ops[0], ops[1], ops[2],
1848 "x86.avx512.mask.compress");
1849 }
1850 case X86::BI__builtin_ia32_gather3div2df:
1851 case X86::BI__builtin_ia32_gather3div2di:
1852 case X86::BI__builtin_ia32_gather3div4df:
1853 case X86::BI__builtin_ia32_gather3div4di:
1854 case X86::BI__builtin_ia32_gather3div4sf:
1855 case X86::BI__builtin_ia32_gather3div4si:
1856 case X86::BI__builtin_ia32_gather3div8sf:
1857 case X86::BI__builtin_ia32_gather3div8si:
1858 case X86::BI__builtin_ia32_gather3siv2df:
1859 case X86::BI__builtin_ia32_gather3siv2di:
1860 case X86::BI__builtin_ia32_gather3siv4df:
1861 case X86::BI__builtin_ia32_gather3siv4di:
1862 case X86::BI__builtin_ia32_gather3siv4sf:
1863 case X86::BI__builtin_ia32_gather3siv4si:
1864 case X86::BI__builtin_ia32_gather3siv8sf:
1865 case X86::BI__builtin_ia32_gather3siv8si:
1866 case X86::BI__builtin_ia32_gathersiv8df:
1867 case X86::BI__builtin_ia32_gathersiv16sf:
1868 case X86::BI__builtin_ia32_gatherdiv8df:
1869 case X86::BI__builtin_ia32_gatherdiv16sf:
1870 case X86::BI__builtin_ia32_gathersiv8di:
1871 case X86::BI__builtin_ia32_gathersiv16si:
1872 case X86::BI__builtin_ia32_gatherdiv8di:
1873 case X86::BI__builtin_ia32_gatherdiv16si: {
1874 StringRef intrinsicName;
1875 switch (builtinID) {
1876 default:
1877 llvm_unreachable("Unexpected builtin");
1878 case X86::BI__builtin_ia32_gather3div2df:
1879 intrinsicName = "x86.avx512.mask.gather3div2.df";
1880 break;
1881 case X86::BI__builtin_ia32_gather3div2di:
1882 intrinsicName = "x86.avx512.mask.gather3div2.di";
1883 break;
1884 case X86::BI__builtin_ia32_gather3div4df:
1885 intrinsicName = "x86.avx512.mask.gather3div4.df";
1886 break;
1887 case X86::BI__builtin_ia32_gather3div4di:
1888 intrinsicName = "x86.avx512.mask.gather3div4.di";
1889 break;
1890 case X86::BI__builtin_ia32_gather3div4sf:
1891 intrinsicName = "x86.avx512.mask.gather3div4.sf";
1892 break;
1893 case X86::BI__builtin_ia32_gather3div4si:
1894 intrinsicName = "x86.avx512.mask.gather3div4.si";
1895 break;
1896 case X86::BI__builtin_ia32_gather3div8sf:
1897 intrinsicName = "x86.avx512.mask.gather3div8.sf";
1898 break;
1899 case X86::BI__builtin_ia32_gather3div8si:
1900 intrinsicName = "x86.avx512.mask.gather3div8.si";
1901 break;
1902 case X86::BI__builtin_ia32_gather3siv2df:
1903 intrinsicName = "x86.avx512.mask.gather3siv2.df";
1904 break;
1905 case X86::BI__builtin_ia32_gather3siv2di:
1906 intrinsicName = "x86.avx512.mask.gather3siv2.di";
1907 break;
1908 case X86::BI__builtin_ia32_gather3siv4df:
1909 intrinsicName = "x86.avx512.mask.gather3siv4.df";
1910 break;
1911 case X86::BI__builtin_ia32_gather3siv4di:
1912 intrinsicName = "x86.avx512.mask.gather3siv4.di";
1913 break;
1914 case X86::BI__builtin_ia32_gather3siv4sf:
1915 intrinsicName = "x86.avx512.mask.gather3siv4.sf";
1916 break;
1917 case X86::BI__builtin_ia32_gather3siv4si:
1918 intrinsicName = "x86.avx512.mask.gather3siv4.si";
1919 break;
1920 case X86::BI__builtin_ia32_gather3siv8sf:
1921 intrinsicName = "x86.avx512.mask.gather3siv8.sf";
1922 break;
1923 case X86::BI__builtin_ia32_gather3siv8si:
1924 intrinsicName = "x86.avx512.mask.gather3siv8.si";
1925 break;
1926 case X86::BI__builtin_ia32_gathersiv8df:
1927 intrinsicName = "x86.avx512.mask.gather.dpd.512";
1928 break;
1929 case X86::BI__builtin_ia32_gathersiv16sf:
1930 intrinsicName = "x86.avx512.mask.gather.dps.512";
1931 break;
1932 case X86::BI__builtin_ia32_gatherdiv8df:
1933 intrinsicName = "x86.avx512.mask.gather.qpd.512";
1934 break;
1935 case X86::BI__builtin_ia32_gatherdiv16sf:
1936 intrinsicName = "x86.avx512.mask.gather.qps.512";
1937 break;
1938 case X86::BI__builtin_ia32_gathersiv8di:
1939 intrinsicName = "x86.avx512.mask.gather.dpq.512";
1940 break;
1941 case X86::BI__builtin_ia32_gathersiv16si:
1942 intrinsicName = "x86.avx512.mask.gather.dpi.512";
1943 break;
1944 case X86::BI__builtin_ia32_gatherdiv8di:
1945 intrinsicName = "x86.avx512.mask.gather.qpq.512";
1946 break;
1947 case X86::BI__builtin_ia32_gatherdiv16si:
1948 intrinsicName = "x86.avx512.mask.gather.qpi.512";
1949 break;
1950 }
1951
1952 mlir::Location loc = getLoc(expr->getExprLoc());
1953 unsigned minElts =
1954 std::min(cast<cir::VectorType>(ops[0].getType()).getFixedSize(),
1955 cast<cir::VectorType>(ops[2].getType()).getFixedSize());
1956 ops[3] = getMaskVecValue(builder, loc, ops[3], minElts);
1957 return builder.emitIntrinsicCallOp(loc, intrinsicName,
1958 convertType(expr->getType()), ops);
1959 }
1960 case X86::BI__builtin_ia32_scattersiv8df:
1961 case X86::BI__builtin_ia32_scattersiv16sf:
1962 case X86::BI__builtin_ia32_scatterdiv8df:
1963 case X86::BI__builtin_ia32_scatterdiv16sf:
1964 case X86::BI__builtin_ia32_scattersiv8di:
1965 case X86::BI__builtin_ia32_scattersiv16si:
1966 case X86::BI__builtin_ia32_scatterdiv8di:
1967 case X86::BI__builtin_ia32_scatterdiv16si:
1968 case X86::BI__builtin_ia32_scatterdiv2df:
1969 case X86::BI__builtin_ia32_scatterdiv2di:
1970 case X86::BI__builtin_ia32_scatterdiv4df:
1971 case X86::BI__builtin_ia32_scatterdiv4di:
1972 case X86::BI__builtin_ia32_scatterdiv4sf:
1973 case X86::BI__builtin_ia32_scatterdiv4si:
1974 case X86::BI__builtin_ia32_scatterdiv8sf:
1975 case X86::BI__builtin_ia32_scatterdiv8si:
1976 case X86::BI__builtin_ia32_scattersiv2df:
1977 case X86::BI__builtin_ia32_scattersiv2di:
1978 case X86::BI__builtin_ia32_scattersiv4df:
1979 case X86::BI__builtin_ia32_scattersiv4di:
1980 case X86::BI__builtin_ia32_scattersiv4sf:
1981 case X86::BI__builtin_ia32_scattersiv4si:
1982 case X86::BI__builtin_ia32_scattersiv8sf:
1983 case X86::BI__builtin_ia32_scattersiv8si: {
1984 llvm::StringRef intrinsicName;
1985 switch (builtinID) {
1986 default:
1987 llvm_unreachable("Unexpected builtin");
1988 case X86::BI__builtin_ia32_scattersiv8df:
1989 intrinsicName = "x86.avx512.mask.scatter.dpd.512";
1990 break;
1991 case X86::BI__builtin_ia32_scattersiv16sf:
1992 intrinsicName = "x86.avx512.mask.scatter.dps.512";
1993 break;
1994 case X86::BI__builtin_ia32_scatterdiv8df:
1995 intrinsicName = "x86.avx512.mask.scatter.qpd.512";
1996 break;
1997 case X86::BI__builtin_ia32_scatterdiv16sf:
1998 intrinsicName = "x86.avx512.mask.scatter.qps.512";
1999 break;
2000 case X86::BI__builtin_ia32_scattersiv8di:
2001 intrinsicName = "x86.avx512.mask.scatter.dpq.512";
2002 break;
2003 case X86::BI__builtin_ia32_scattersiv16si:
2004 intrinsicName = "x86.avx512.mask.scatter.dpi.512";
2005 break;
2006 case X86::BI__builtin_ia32_scatterdiv8di:
2007 intrinsicName = "x86.avx512.mask.scatter.qpq.512";
2008 break;
2009 case X86::BI__builtin_ia32_scatterdiv16si:
2010 intrinsicName = "x86.avx512.mask.scatter.qpi.512";
2011 break;
2012 case X86::BI__builtin_ia32_scatterdiv2df:
2013 intrinsicName = "x86.avx512.mask.scatterdiv2.df";
2014 break;
2015 case X86::BI__builtin_ia32_scatterdiv2di:
2016 intrinsicName = "x86.avx512.mask.scatterdiv2.di";
2017 break;
2018 case X86::BI__builtin_ia32_scatterdiv4df:
2019 intrinsicName = "x86.avx512.mask.scatterdiv4.df";
2020 break;
2021 case X86::BI__builtin_ia32_scatterdiv4di:
2022 intrinsicName = "x86.avx512.mask.scatterdiv4.di";
2023 break;
2024 case X86::BI__builtin_ia32_scatterdiv4sf:
2025 intrinsicName = "x86.avx512.mask.scatterdiv4.sf";
2026 break;
2027 case X86::BI__builtin_ia32_scatterdiv4si:
2028 intrinsicName = "x86.avx512.mask.scatterdiv4.si";
2029 break;
2030 case X86::BI__builtin_ia32_scatterdiv8sf:
2031 intrinsicName = "x86.avx512.mask.scatterdiv8.sf";
2032 break;
2033 case X86::BI__builtin_ia32_scatterdiv8si:
2034 intrinsicName = "x86.avx512.mask.scatterdiv8.si";
2035 break;
2036 case X86::BI__builtin_ia32_scattersiv2df:
2037 intrinsicName = "x86.avx512.mask.scattersiv2.df";
2038 break;
2039 case X86::BI__builtin_ia32_scattersiv2di:
2040 intrinsicName = "x86.avx512.mask.scattersiv2.di";
2041 break;
2042 case X86::BI__builtin_ia32_scattersiv4df:
2043 intrinsicName = "x86.avx512.mask.scattersiv4.df";
2044 break;
2045 case X86::BI__builtin_ia32_scattersiv4di:
2046 intrinsicName = "x86.avx512.mask.scattersiv4.di";
2047 break;
2048 case X86::BI__builtin_ia32_scattersiv4sf:
2049 intrinsicName = "x86.avx512.mask.scattersiv4.sf";
2050 break;
2051 case X86::BI__builtin_ia32_scattersiv4si:
2052 intrinsicName = "x86.avx512.mask.scattersiv4.si";
2053 break;
2054 case X86::BI__builtin_ia32_scattersiv8sf:
2055 intrinsicName = "x86.avx512.mask.scattersiv8.sf";
2056 break;
2057 case X86::BI__builtin_ia32_scattersiv8si:
2058 intrinsicName = "x86.avx512.mask.scattersiv8.si";
2059 break;
2060 }
2061
2062 mlir::Location loc = getLoc(expr->getExprLoc());
2063 unsigned minElts =
2064 std::min(cast<cir::VectorType>(ops[2].getType()).getFixedSize(),
2065 cast<cir::VectorType>(ops[3].getType()).getFixedSize());
2066 ops[1] = getMaskVecValue(builder, loc, ops[1], minElts);
2067
2068 return builder.emitIntrinsicCallOp(loc, intrinsicName,
2069 convertType(expr->getType()), ops);
2070 }
2071 case X86::BI__builtin_ia32_vextractf128_pd256:
2072 case X86::BI__builtin_ia32_vextractf128_ps256:
2073 case X86::BI__builtin_ia32_vextractf128_si256:
2074 case X86::BI__builtin_ia32_extract128i256:
2075 case X86::BI__builtin_ia32_extractf64x4_mask:
2076 case X86::BI__builtin_ia32_extractf32x4_mask:
2077 case X86::BI__builtin_ia32_extracti64x4_mask:
2078 case X86::BI__builtin_ia32_extracti32x4_mask:
2079 case X86::BI__builtin_ia32_extractf32x8_mask:
2080 case X86::BI__builtin_ia32_extracti32x8_mask:
2081 case X86::BI__builtin_ia32_extractf32x4_256_mask:
2082 case X86::BI__builtin_ia32_extracti32x4_256_mask:
2083 case X86::BI__builtin_ia32_extractf64x2_256_mask:
2084 case X86::BI__builtin_ia32_extracti64x2_256_mask:
2085 case X86::BI__builtin_ia32_extractf64x2_512_mask:
2086 case X86::BI__builtin_ia32_extracti64x2_512_mask: {
2087 mlir::Location loc = getLoc(expr->getExprLoc());
2088 cir::VectorType dstTy = cast<cir::VectorType>(convertType(expr->getType()));
2089 unsigned numElts = dstTy.getFixedSize();
2090 unsigned srcNumElts =
2091 cast<cir::VectorType>(ops[0].getType()).getFixedSize();
2092 unsigned subVectors = srcNumElts / numElts;
2093 assert(llvm::isPowerOf2_32(subVectors) && "Expected power of 2 subvectors");
2094 unsigned index =
2095 ops[1].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue();
2096
2097 index &= subVectors - 1; // Remove any extra bits.
2098 index *= numElts;
2099
2100 int64_t indices[16];
2101 std::iota(indices, indices + numElts, index);
2102
2103 mlir::Value poison =
2104 builder.getConstant(loc, cir::PoisonAttr::get(ops[0].getType()));
2105 mlir::Value res = builder.createVecShuffle(loc, ops[0], poison,
2106 ArrayRef(indices, numElts));
2107 if (ops.size() == 4)
2108 res = emitX86Select(builder, loc, ops[3], res, ops[2]);
2109
2110 return res;
2111 }
2112 case X86::BI__builtin_ia32_vinsertf128_pd256:
2113 case X86::BI__builtin_ia32_vinsertf128_ps256:
2114 case X86::BI__builtin_ia32_vinsertf128_si256:
2115 case X86::BI__builtin_ia32_insert128i256:
2116 case X86::BI__builtin_ia32_insertf64x4:
2117 case X86::BI__builtin_ia32_insertf32x4:
2118 case X86::BI__builtin_ia32_inserti64x4:
2119 case X86::BI__builtin_ia32_inserti32x4:
2120 case X86::BI__builtin_ia32_insertf32x8:
2121 case X86::BI__builtin_ia32_inserti32x8:
2122 case X86::BI__builtin_ia32_insertf32x4_256:
2123 case X86::BI__builtin_ia32_inserti32x4_256:
2124 case X86::BI__builtin_ia32_insertf64x2_256:
2125 case X86::BI__builtin_ia32_inserti64x2_256:
2126 case X86::BI__builtin_ia32_insertf64x2_512:
2127 case X86::BI__builtin_ia32_inserti64x2_512: {
2128 unsigned dstNumElts =
2129 cast<cir::VectorType>(ops[0].getType()).getFixedSize();
2130 unsigned srcNumElts =
2131 cast<cir::VectorType>(ops[1].getType()).getFixedSize();
2132 unsigned subVectors = dstNumElts / srcNumElts;
2133 assert(llvm::isPowerOf2_32(subVectors) && "Expected power of 2 subvectors");
2134 assert(dstNumElts <= 16);
2135
2136 uint64_t index = getZExtIntValueFromConstOp(ops[2]);
2137 index &= subVectors - 1; // Remove any extra bits.
2138 index *= srcNumElts;
2139
2140 llvm::SmallVector<int64_t, 16> mask(dstNumElts);
2141 for (unsigned i = 0; i != dstNumElts; ++i)
2142 mask[i] = (i >= srcNumElts) ? srcNumElts + (i % srcNumElts) : i;
2143
2144 mlir::Value op1 =
2145 builder.createVecShuffle(getLoc(expr->getExprLoc()), ops[1], mask);
2146
2147 for (unsigned i = 0; i != dstNumElts; ++i) {
2148 if (i >= index && i < (index + srcNumElts))
2149 mask[i] = (i - index) + dstNumElts;
2150 else
2151 mask[i] = i;
2152 }
2153
2154 return builder.createVecShuffle(getLoc(expr->getExprLoc()), ops[0], op1,
2155 mask);
2156 }
2157 case X86::BI__builtin_ia32_pmovqd512_mask:
2158 case X86::BI__builtin_ia32_pmovwb512_mask: {
2159 mlir::Value Res =
2160 builder.createIntCast(ops[0], cast<cir::VectorType>(ops[1].getType()));
2161 return emitX86Select(builder, getLoc(expr->getExprLoc()), ops[2], Res,
2162 ops[1]);
2163 }
2164 case X86::BI__builtin_ia32_pmovdb512_mask:
2165 case X86::BI__builtin_ia32_pmovdw512_mask:
2166 case X86::BI__builtin_ia32_pmovqw512_mask: {
2167 // These only require AVX512F, but a byte/word select would need AVX512BW,
2168 // so use the masked intrinsic unless the mask is all ones.
2169 mlir::Location loc = getLoc(expr->getExprLoc());
2170 auto c = mlir::dyn_cast_or_null<cir::ConstantOp>(ops[2].getDefiningOp());
2171 if (c && c.isAllOnesValue())
2172 return builder.createIntCast(ops[0],
2173 cast<cir::VectorType>(ops[1].getType()));
2174
2175 llvm::StringRef intrinsicName;
2176 switch (builtinID) {
2177 default:
2178 llvm_unreachable("Unsupported intrinsic!");
2179 case X86::BI__builtin_ia32_pmovdb512_mask:
2180 intrinsicName = "x86.avx512.mask.pmov.db.512";
2181 break;
2182 case X86::BI__builtin_ia32_pmovdw512_mask:
2183 intrinsicName = "x86.avx512.mask.pmov.dw.512";
2184 break;
2185 case X86::BI__builtin_ia32_pmovqw512_mask:
2186 intrinsicName = "x86.avx512.mask.pmov.qw.512";
2187 break;
2188 }
2189 return builder.emitIntrinsicCallOp(loc, intrinsicName, ops[1].getType(),
2190 ops);
2191 }
2192 case X86::BI__builtin_ia32_pblendw128:
2193 case X86::BI__builtin_ia32_blendpd:
2194 case X86::BI__builtin_ia32_blendps:
2195 case X86::BI__builtin_ia32_blendpd256:
2196 case X86::BI__builtin_ia32_blendps256:
2197 case X86::BI__builtin_ia32_pblendw256:
2198 case X86::BI__builtin_ia32_pblendd128:
2199 case X86::BI__builtin_ia32_pblendd256: {
2200 uint32_t imm = getZExtIntValueFromConstOp(ops[2]);
2201 unsigned numElts = cast<cir::VectorType>(ops[0].getType()).getFixedSize();
2202
2204 // If there are more than 8 elements, the immediate is used twice so make
2205 // sure we handle that.
2206 mlir::Type i32Ty = builder.getSInt32Ty();
2207 for (unsigned i = 0; i != numElts; ++i)
2208 indices.push_back(
2209 cir::IntAttr::get(i32Ty, ((imm >> (i % 8)) & 0x1) ? numElts + i : i));
2210
2211 return builder.createVecShuffle(getLoc(expr->getExprLoc()), ops[0], ops[1],
2212 indices);
2213 }
2214 case X86::BI__builtin_ia32_pshuflw:
2215 case X86::BI__builtin_ia32_pshuflw256:
2216 case X86::BI__builtin_ia32_pshuflw512:
2217 return emitPshufWord(builder, ops[0], ops[1], getLoc(expr->getExprLoc()),
2218 true);
2219 case X86::BI__builtin_ia32_pshufhw:
2220 case X86::BI__builtin_ia32_pshufhw256:
2221 case X86::BI__builtin_ia32_pshufhw512:
2222 return emitPshufWord(builder, ops[0], ops[1], getLoc(expr->getExprLoc()),
2223 false);
2224 case X86::BI__builtin_ia32_pshufd:
2225 case X86::BI__builtin_ia32_pshufd256:
2226 case X86::BI__builtin_ia32_pshufd512:
2227 case X86::BI__builtin_ia32_vpermilpd:
2228 case X86::BI__builtin_ia32_vpermilps:
2229 case X86::BI__builtin_ia32_vpermilpd256:
2230 case X86::BI__builtin_ia32_vpermilps256:
2231 case X86::BI__builtin_ia32_vpermilpd512:
2232 case X86::BI__builtin_ia32_vpermilps512: {
2233 const uint32_t imm = getSExtIntValueFromConstOp(ops[1]);
2234
2236 computeFullLaneShuffleMask(*this, ops[0], imm, false, mask);
2237
2238 return builder.createVecShuffle(getLoc(expr->getExprLoc()), ops[0], mask);
2239 }
2240 case X86::BI__builtin_ia32_shufpd:
2241 case X86::BI__builtin_ia32_shufpd256:
2242 case X86::BI__builtin_ia32_shufpd512:
2243 case X86::BI__builtin_ia32_shufps:
2244 case X86::BI__builtin_ia32_shufps256:
2245 case X86::BI__builtin_ia32_shufps512: {
2246 const uint32_t imm = getZExtIntValueFromConstOp(ops[2]);
2247
2249 computeFullLaneShuffleMask(*this, ops[0], imm, true, mask);
2250
2251 return builder.createVecShuffle(getLoc(expr->getExprLoc()), ops[0], ops[1],
2252 mask);
2253 }
2254 case X86::BI__builtin_ia32_permdi256:
2255 case X86::BI__builtin_ia32_permdf256:
2256 case X86::BI__builtin_ia32_permdi512:
2257 case X86::BI__builtin_ia32_permdf512: {
2258 unsigned imm =
2259 ops[1].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue();
2260 unsigned numElts = cast<cir::VectorType>(ops[0].getType()).getFixedSize();
2261
2262 // These intrinsics operate on 256-bit lanes of four 64-bit elements.
2263 int64_t Indices[8];
2264
2265 for (unsigned l = 0; l != numElts; l += 4)
2266 for (unsigned i = 0; i != 4; ++i)
2267 Indices[l + i] = l + ((imm >> (2 * i)) & 0x3);
2268
2269 return builder.createVecShuffle(getLoc(expr->getExprLoc()), ops[0],
2270 ArrayRef(Indices, numElts));
2271 }
2272 case X86::BI__builtin_ia32_palignr128:
2273 case X86::BI__builtin_ia32_palignr256:
2274 case X86::BI__builtin_ia32_palignr512: {
2275 uint32_t shiftVal = getZExtIntValueFromConstOp(ops[2]) & 0xff;
2276
2277 unsigned numElts = cast<cir::VectorType>(ops[0].getType()).getFixedSize();
2278 assert(numElts % 16 == 0);
2279
2280 // If palignr is shifting the pair of vectors more than the size of two
2281 // lanes, emit zero.
2282 if (shiftVal >= 32)
2283 return builder.getNullValue(convertType(expr->getType()),
2284 getLoc(expr->getExprLoc()));
2285
2286 // If palignr is shifting the pair of input vectors more than one lane,
2287 // but less than two lanes, convert to shifting in zeroes.
2288 if (shiftVal > 16) {
2289 shiftVal -= 16;
2290 ops[1] = ops[0];
2291 ops[0] =
2292 builder.getNullValue(ops[0].getType(), getLoc(expr->getExprLoc()));
2293 }
2294
2295 int64_t indices[64];
2296 // 256-bit palignr operates on 128-bit lanes so we need to handle that
2297 for (unsigned l = 0; l != numElts; l += 16) {
2298 for (unsigned i = 0; i != 16; ++i) {
2299 uint32_t idx = shiftVal + i;
2300 if (idx >= 16)
2301 idx += numElts - 16; // End of lane, switch operand.
2302 indices[l + i] = l + idx;
2303 }
2304 }
2305
2306 return builder.createVecShuffle(getLoc(expr->getExprLoc()), ops[1], ops[0],
2307 ArrayRef(indices, numElts));
2308 }
2309 case X86::BI__builtin_ia32_alignd128:
2310 case X86::BI__builtin_ia32_alignd256:
2311 case X86::BI__builtin_ia32_alignd512:
2312 case X86::BI__builtin_ia32_alignq128:
2313 case X86::BI__builtin_ia32_alignq256:
2314 case X86::BI__builtin_ia32_alignq512: {
2315 unsigned numElts = cast<cir::VectorType>(ops[0].getType()).getFixedSize();
2316 unsigned shiftVal =
2317 ops[2].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue() &
2318 0xff;
2319
2320 // Mask the shift amount to width of a vector.
2321 shiftVal &= numElts - 1;
2322
2324 mlir::Type i32Ty = builder.getSInt32Ty();
2325 for (unsigned i = 0; i != numElts; ++i)
2326 indices.push_back(cir::IntAttr::get(i32Ty, i + shiftVal));
2327
2328 return builder.createVecShuffle(getLoc(expr->getExprLoc()), ops[0], ops[1],
2329 indices);
2330 }
2331 case X86::BI__builtin_ia32_shuf_f32x4_256:
2332 case X86::BI__builtin_ia32_shuf_f64x2_256:
2333 case X86::BI__builtin_ia32_shuf_i32x4_256:
2334 case X86::BI__builtin_ia32_shuf_i64x2_256:
2335 case X86::BI__builtin_ia32_shuf_f32x4:
2336 case X86::BI__builtin_ia32_shuf_f64x2:
2337 case X86::BI__builtin_ia32_shuf_i32x4:
2338 case X86::BI__builtin_ia32_shuf_i64x2: {
2339 mlir::Value src1 = ops[0];
2340 mlir::Value src2 = ops[1];
2341
2342 unsigned imm =
2343 ops[2].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue();
2344
2345 unsigned numElems = cast<cir::VectorType>(src1.getType()).getFixedSize();
2346 unsigned totalBits = getContext().getTypeSize(expr->getArg(0)->getType());
2347 unsigned numLanes = totalBits == 512 ? 4 : 2;
2348 unsigned numElemsPerLane = numElems / numLanes;
2349
2351 mlir::Type i32Ty = builder.getSInt32Ty();
2352
2353 for (unsigned l = 0; l != numElems; l += numElemsPerLane) {
2354 unsigned index = (imm % numLanes) * numElemsPerLane;
2355 imm /= numLanes;
2356 if (l >= (numElems / 2))
2357 index += numElems;
2358 for (unsigned i = 0; i != numElemsPerLane; ++i) {
2359 indices.push_back(cir::IntAttr::get(i32Ty, index + i));
2360 }
2361 }
2362
2363 return builder.createVecShuffle(getLoc(expr->getExprLoc()), src1, src2,
2364 indices);
2365 }
2366 case X86::BI__builtin_ia32_vperm2f128_pd256:
2367 case X86::BI__builtin_ia32_vperm2f128_ps256:
2368 case X86::BI__builtin_ia32_vperm2f128_si256:
2369 case X86::BI__builtin_ia32_permti256:
2370 return emitX86VPerm2f128(builder, getLoc(expr->getExprLoc()), ops);
2371 case X86::BI__builtin_ia32_pslldqi128_byteshift:
2372 case X86::BI__builtin_ia32_pslldqi256_byteshift:
2373 case X86::BI__builtin_ia32_pslldqi512_byteshift:
2374 return emitX86PackedByteShift(builder, builtinID,
2375 getLoc(expr->getExprLoc()), ops,
2376 /**isLeftShift=*/true);
2377 case X86::BI__builtin_ia32_psrldqi128_byteshift:
2378 case X86::BI__builtin_ia32_psrldqi256_byteshift:
2379 case X86::BI__builtin_ia32_psrldqi512_byteshift:
2380 return emitX86PackedByteShift(builder, builtinID,
2381 getLoc(expr->getExprLoc()), ops,
2382 /**isLeftShift=*/false);
2383 case X86::BI__builtin_ia32_kshiftliqi:
2384 case X86::BI__builtin_ia32_kshiftlihi:
2385 case X86::BI__builtin_ia32_kshiftlisi:
2386 case X86::BI__builtin_ia32_kshiftlidi: {
2387 mlir::Location loc = getLoc(expr->getExprLoc());
2388 unsigned shiftVal =
2389 ops[1].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue() &
2390 0xff;
2391 unsigned numElems = cast<cir::IntType>(ops[0].getType()).getWidth();
2392
2393 if (shiftVal >= numElems)
2394 return builder.getNullValue(ops[0].getType(), loc);
2395
2396 mlir::Value in = getMaskVecValue(builder, loc, ops[0], numElems);
2397
2399 mlir::Type i32Ty = builder.getSInt32Ty();
2400 for (auto i : llvm::seq<unsigned>(0, numElems))
2401 indices.push_back(cir::IntAttr::get(i32Ty, numElems + i - shiftVal));
2402
2403 mlir::Value zero = builder.getNullValue(in.getType(), loc);
2404 mlir::Value sv = builder.createVecShuffle(loc, zero, in, indices);
2405 return builder.createBitcast(sv, ops[0].getType());
2406 }
2407 case X86::BI__builtin_ia32_kshiftriqi:
2408 case X86::BI__builtin_ia32_kshiftrihi:
2409 case X86::BI__builtin_ia32_kshiftrisi:
2410 case X86::BI__builtin_ia32_kshiftridi: {
2411 mlir::Location loc = getLoc(expr->getExprLoc());
2412 unsigned shiftVal =
2413 ops[1].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue() &
2414 0xff;
2415 unsigned numElems = cast<cir::IntType>(ops[0].getType()).getWidth();
2416
2417 if (shiftVal >= numElems)
2418 return builder.getNullValue(ops[0].getType(), loc);
2419
2420 mlir::Value in = getMaskVecValue(builder, loc, ops[0], numElems);
2421
2423 mlir::Type i32Ty = builder.getSInt32Ty();
2424 for (auto i : llvm::seq<unsigned>(0, numElems))
2425 indices.push_back(cir::IntAttr::get(i32Ty, i + shiftVal));
2426
2427 mlir::Value zero = builder.getNullValue(in.getType(), loc);
2428 mlir::Value sv = builder.createVecShuffle(loc, in, zero, indices);
2429 return builder.createBitcast(sv, ops[0].getType());
2430 }
2431 case X86::BI__builtin_ia32_movnti:
2432 case X86::BI__builtin_ia32_movnti64:
2433 case X86::BI__builtin_ia32_movntsd:
2434 case X86::BI__builtin_ia32_movntss: {
2435 mlir::Location loc = getLoc(expr->getExprLoc());
2436
2437 Address dest = Address{ops[0], CharUnits::One()};
2438 mlir::Value src = ops[1];
2439
2440 if (builtinID == X86::BI__builtin_ia32_movntsd ||
2441 builtinID == X86::BI__builtin_ia32_movntss)
2442 src = builder.createExtractElement(loc, ops[1], 0);
2443
2444 cir::StoreOp so =
2445 builder.createStore(loc, src, dest,
2446 /*isVolatile=*/false, /*isNontemporal=*/true);
2447 return so.getValue();
2448 }
2449 case X86::BI__builtin_ia32_vprotbi:
2450 case X86::BI__builtin_ia32_vprotwi:
2451 case X86::BI__builtin_ia32_vprotdi:
2452 case X86::BI__builtin_ia32_vprotqi:
2453 case X86::BI__builtin_ia32_prold128:
2454 case X86::BI__builtin_ia32_prold256:
2455 case X86::BI__builtin_ia32_prold512:
2456 case X86::BI__builtin_ia32_prolq128:
2457 case X86::BI__builtin_ia32_prolq256:
2458 case X86::BI__builtin_ia32_prolq512:
2459 return emitX86FunnelShift(builder, getLoc(expr->getExprLoc()), ops[0],
2460 ops[0], ops[1], false);
2461 case X86::BI__builtin_ia32_prord128:
2462 case X86::BI__builtin_ia32_prord256:
2463 case X86::BI__builtin_ia32_prord512:
2464 case X86::BI__builtin_ia32_prorq128:
2465 case X86::BI__builtin_ia32_prorq256:
2466 case X86::BI__builtin_ia32_prorq512:
2467 return emitX86FunnelShift(builder, getLoc(expr->getExprLoc()), ops[0],
2468 ops[0], ops[1], true);
2469 case X86::BI__builtin_ia32_selectb_128:
2470 case X86::BI__builtin_ia32_selectb_256:
2471 case X86::BI__builtin_ia32_selectb_512:
2472 case X86::BI__builtin_ia32_selectw_128:
2473 case X86::BI__builtin_ia32_selectw_256:
2474 case X86::BI__builtin_ia32_selectw_512:
2475 case X86::BI__builtin_ia32_selectd_128:
2476 case X86::BI__builtin_ia32_selectd_256:
2477 case X86::BI__builtin_ia32_selectd_512:
2478 case X86::BI__builtin_ia32_selectq_128:
2479 case X86::BI__builtin_ia32_selectq_256:
2480 case X86::BI__builtin_ia32_selectq_512:
2481 case X86::BI__builtin_ia32_selectph_128:
2482 case X86::BI__builtin_ia32_selectph_256:
2483 case X86::BI__builtin_ia32_selectph_512:
2484 case X86::BI__builtin_ia32_selectpbf_128:
2485 case X86::BI__builtin_ia32_selectpbf_256:
2486 case X86::BI__builtin_ia32_selectpbf_512:
2487 case X86::BI__builtin_ia32_selectps_128:
2488 case X86::BI__builtin_ia32_selectps_256:
2489 case X86::BI__builtin_ia32_selectps_512:
2490 case X86::BI__builtin_ia32_selectpd_128:
2491 case X86::BI__builtin_ia32_selectpd_256:
2492 case X86::BI__builtin_ia32_selectpd_512:
2493 return emitX86Select(builder, getLoc(expr->getExprLoc()), ops[0], ops[1],
2494 ops[2]);
2495 case X86::BI__builtin_ia32_selectsh_128:
2496 case X86::BI__builtin_ia32_selectsbf_128:
2497 case X86::BI__builtin_ia32_selectss_128:
2498 case X86::BI__builtin_ia32_selectsd_128: {
2499 mlir::Location loc = getLoc(expr->getExprLoc());
2500 mlir::Value scalar1 =
2501 builder.createExtractElement(loc, ops[1], uint64_t(0));
2502 mlir::Value scalar2 =
2503 builder.createExtractElement(loc, ops[2], uint64_t(0));
2504 mlir::Value result =
2505 emitX86ScalarSelect(builder, loc, ops[0], scalar1, scalar2);
2506 return builder.createInsertElement(loc, ops[1], result, uint64_t(0));
2507 }
2508 case X86::BI__builtin_ia32_cmpb128_mask:
2509 case X86::BI__builtin_ia32_cmpb256_mask:
2510 case X86::BI__builtin_ia32_cmpb512_mask:
2511 case X86::BI__builtin_ia32_cmpw128_mask:
2512 case X86::BI__builtin_ia32_cmpw256_mask:
2513 case X86::BI__builtin_ia32_cmpw512_mask:
2514 case X86::BI__builtin_ia32_cmpd128_mask:
2515 case X86::BI__builtin_ia32_cmpd256_mask:
2516 case X86::BI__builtin_ia32_cmpd512_mask:
2517 case X86::BI__builtin_ia32_cmpq128_mask:
2518 case X86::BI__builtin_ia32_cmpq256_mask:
2519 case X86::BI__builtin_ia32_cmpq512_mask:
2520 case X86::BI__builtin_ia32_ucmpb128_mask:
2521 case X86::BI__builtin_ia32_ucmpb256_mask:
2522 case X86::BI__builtin_ia32_ucmpb512_mask:
2523 case X86::BI__builtin_ia32_ucmpw128_mask:
2524 case X86::BI__builtin_ia32_ucmpw256_mask:
2525 case X86::BI__builtin_ia32_ucmpw512_mask:
2526 case X86::BI__builtin_ia32_ucmpd128_mask:
2527 case X86::BI__builtin_ia32_ucmpd256_mask:
2528 case X86::BI__builtin_ia32_ucmpd512_mask:
2529 case X86::BI__builtin_ia32_ucmpq128_mask:
2530 case X86::BI__builtin_ia32_ucmpq256_mask:
2531 case X86::BI__builtin_ia32_ucmpq512_mask: {
2532 int64_t cc = CIRGenFunction::getZExtIntValueFromConstOp(ops[2]) & 0x7;
2533 return emitX86MaskedCompare(builder, cc, 1, ops,
2534 getLoc(expr->getExprLoc()));
2535 }
2536 case X86::BI__builtin_ia32_vpcomb:
2537 case X86::BI__builtin_ia32_vpcomw:
2538 case X86::BI__builtin_ia32_vpcomd:
2539 case X86::BI__builtin_ia32_vpcomq:
2540 return emitX86vpcom(builder, getLoc(expr->getExprLoc()), ops, true);
2541 case X86::BI__builtin_ia32_vpcomub:
2542 case X86::BI__builtin_ia32_vpcomuw:
2543 case X86::BI__builtin_ia32_vpcomud:
2544 case X86::BI__builtin_ia32_vpcomuq:
2545 return emitX86vpcom(builder, getLoc(expr->getExprLoc()), ops, false);
2546 case X86::BI__builtin_ia32_kortestcqi:
2547 case X86::BI__builtin_ia32_kortestchi:
2548 case X86::BI__builtin_ia32_kortestcsi:
2549 case X86::BI__builtin_ia32_kortestcdi: {
2550 mlir::Location loc = getLoc(expr->getExprLoc());
2551 cir::IntType ty = cast<cir::IntType>(ops[0].getType());
2552 mlir::Value allOnesOp =
2553 builder.getConstAPInt(loc, ty, APInt::getAllOnes(ty.getWidth()));
2554 mlir::Value orOp = emitX86MaskLogic<cir::OrOp>(builder, loc, ops);
2555 mlir::Value cmp =
2556 cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, orOp, allOnesOp);
2557 return builder.createCast(cir::CastKind::bool_to_int, cmp,
2558 cgm.convertType(expr->getType()));
2559 }
2560 case X86::BI__builtin_ia32_kortestzqi:
2561 case X86::BI__builtin_ia32_kortestzhi:
2562 case X86::BI__builtin_ia32_kortestzsi:
2563 case X86::BI__builtin_ia32_kortestzdi: {
2564 mlir::Location loc = getLoc(expr->getExprLoc());
2565 cir::IntType ty = cast<cir::IntType>(ops[0].getType());
2566 mlir::Value allZerosOp = builder.getNullValue(ty, loc).getResult();
2567 mlir::Value orOp = emitX86MaskLogic<cir::OrOp>(builder, loc, ops);
2568 mlir::Value cmp =
2569 cir::CmpOp::create(builder, loc, cir::CmpOpKind::eq, orOp, allZerosOp);
2570 return builder.createCast(cir::CastKind::bool_to_int, cmp,
2571 cgm.convertType(expr->getType()));
2572 }
2573 case X86::BI__builtin_ia32_ktestcqi:
2574 return emitX86MaskTest(builder, getLoc(expr->getExprLoc()),
2575 "x86.avx512.ktestc.b", ops);
2576 case X86::BI__builtin_ia32_ktestzqi:
2577 return emitX86MaskTest(builder, getLoc(expr->getExprLoc()),
2578 "x86.avx512.ktestz.b", ops);
2579 case X86::BI__builtin_ia32_ktestchi:
2580 return emitX86MaskTest(builder, getLoc(expr->getExprLoc()),
2581 "x86.avx512.ktestc.w", ops);
2582 case X86::BI__builtin_ia32_ktestzhi:
2583 return emitX86MaskTest(builder, getLoc(expr->getExprLoc()),
2584 "x86.avx512.ktestz.w", ops);
2585 case X86::BI__builtin_ia32_ktestcsi:
2586 return emitX86MaskTest(builder, getLoc(expr->getExprLoc()),
2587 "x86.avx512.ktestc.d", ops);
2588 case X86::BI__builtin_ia32_ktestzsi:
2589 return emitX86MaskTest(builder, getLoc(expr->getExprLoc()),
2590 "x86.avx512.ktestz.d", ops);
2591 case X86::BI__builtin_ia32_ktestcdi:
2592 return emitX86MaskTest(builder, getLoc(expr->getExprLoc()),
2593 "x86.avx512.ktestc.q", ops);
2594 case X86::BI__builtin_ia32_ktestzdi:
2595 return emitX86MaskTest(builder, getLoc(expr->getExprLoc()),
2596 "x86.avx512.ktestz.q", ops);
2597 case X86::BI__builtin_ia32_kaddqi:
2598 return emitX86MaskAddLogic(builder, getLoc(expr->getExprLoc()),
2599 "x86.avx512.kadd.b", ops);
2600 case X86::BI__builtin_ia32_kaddhi:
2601 return emitX86MaskAddLogic(builder, getLoc(expr->getExprLoc()),
2602 "x86.avx512.kadd.w", ops);
2603 case X86::BI__builtin_ia32_kaddsi:
2604 return emitX86MaskAddLogic(builder, getLoc(expr->getExprLoc()),
2605 "x86.avx512.kadd.d", ops);
2606 case X86::BI__builtin_ia32_kadddi:
2607 return emitX86MaskAddLogic(builder, getLoc(expr->getExprLoc()),
2608 "x86.avx512.kadd.q", ops);
2609 case X86::BI__builtin_ia32_kandqi:
2610 case X86::BI__builtin_ia32_kandhi:
2611 case X86::BI__builtin_ia32_kandsi:
2612 case X86::BI__builtin_ia32_kanddi:
2613 return emitX86MaskLogic<cir::AndOp>(builder, getLoc(expr->getExprLoc()),
2614 ops);
2615 case X86::BI__builtin_ia32_kandnqi:
2616 case X86::BI__builtin_ia32_kandnhi:
2617 case X86::BI__builtin_ia32_kandnsi:
2618 case X86::BI__builtin_ia32_kandndi:
2619 return emitX86MaskLogic<cir::AndOp>(builder, getLoc(expr->getExprLoc()),
2620 ops, /*invertLHS=*/true);
2621 case X86::BI__builtin_ia32_korqi:
2622 case X86::BI__builtin_ia32_korhi:
2623 case X86::BI__builtin_ia32_korsi:
2624 case X86::BI__builtin_ia32_kordi:
2625 return emitX86MaskLogic<cir::OrOp>(builder, getLoc(expr->getExprLoc()),
2626 ops);
2627 case X86::BI__builtin_ia32_kxnorqi:
2628 case X86::BI__builtin_ia32_kxnorhi:
2629 case X86::BI__builtin_ia32_kxnorsi:
2630 case X86::BI__builtin_ia32_kxnordi:
2631 return emitX86MaskLogic<cir::XorOp>(builder, getLoc(expr->getExprLoc()),
2632 ops, /*invertLHS=*/true);
2633 case X86::BI__builtin_ia32_kxorqi:
2634 case X86::BI__builtin_ia32_kxorhi:
2635 case X86::BI__builtin_ia32_kxorsi:
2636 case X86::BI__builtin_ia32_kxordi:
2637 return emitX86MaskLogic<cir::XorOp>(builder, getLoc(expr->getExprLoc()),
2638 ops);
2639 case X86::BI__builtin_ia32_knotqi:
2640 case X86::BI__builtin_ia32_knothi:
2641 case X86::BI__builtin_ia32_knotsi:
2642 case X86::BI__builtin_ia32_knotdi: {
2643 cir::IntType intTy = cast<cir::IntType>(ops[0].getType());
2644 unsigned numElts = intTy.getWidth();
2645 mlir::Value resVec =
2646 getMaskVecValue(builder, getLoc(expr->getExprLoc()), ops[0], numElts);
2647 return builder.createBitcast(builder.createNot(resVec), ops[0].getType());
2648 }
2649 case X86::BI__builtin_ia32_kmovb:
2650 case X86::BI__builtin_ia32_kmovw:
2651 case X86::BI__builtin_ia32_kmovd:
2652 case X86::BI__builtin_ia32_kmovq: {
2653 // Bitcast to vXi1 type and then back to integer. This gets the mask
2654 // register type into the IR, but might be optimized out depending on
2655 // what's around it.
2656 cir::IntType intTy = cast<cir::IntType>(ops[0].getType());
2657 unsigned numElts = intTy.getWidth();
2658 mlir::Value resVec =
2659 getMaskVecValue(builder, getLoc(expr->getExprLoc()), ops[0], numElts);
2660 return builder.createBitcast(resVec, ops[0].getType());
2661 }
2662 case X86::BI__builtin_ia32_sqrtsh_round_mask:
2663 case X86::BI__builtin_ia32_sqrtsd_round_mask:
2664 case X86::BI__builtin_ia32_sqrtss_round_mask:
2665 cgm.errorNYI(expr->getSourceRange(),
2666 std::string("unimplemented X86 builtin call: ") +
2667 getContext().BuiltinInfo.getName(builtinID));
2668 return mlir::Value{};
2669 case X86::BI__builtin_ia32_sqrtph512:
2670 case X86::BI__builtin_ia32_sqrtps512:
2671 case X86::BI__builtin_ia32_sqrtpd512: {
2672 mlir::Location loc = getLoc(expr->getExprLoc());
2673 mlir::Value arg = ops[0];
2674 return cir::SqrtOp::create(builder, loc, arg).getResult();
2675 }
2676 case X86::BI__builtin_ia32_pmulhw128:
2677 case X86::BI__builtin_ia32_pmulhw256:
2678 case X86::BI__builtin_ia32_pmulhw512:
2679 return builder.emitIntrinsicCallOp(getLoc(expr->getExprLoc()), "smulh",
2680 ops[0].getType(), ops);
2681 case X86::BI__builtin_ia32_pmulhuw128:
2682 case X86::BI__builtin_ia32_pmulhuw256:
2683 case X86::BI__builtin_ia32_pmulhuw512:
2684 return builder.emitIntrinsicCallOp(getLoc(expr->getExprLoc()), "umulh",
2685 ops[0].getType(), ops);
2686 case X86::BI__builtin_ia32_pmuludq128:
2687 case X86::BI__builtin_ia32_pmuludq256:
2688 case X86::BI__builtin_ia32_pmuludq512: {
2689 unsigned opTypePrimitiveSizeInBits =
2690 cgm.getDataLayout().getTypeSizeInBits(ops[0].getType());
2691 return emitX86Muldq(builder, getLoc(expr->getExprLoc()), /*isSigned*/ false,
2692 ops, opTypePrimitiveSizeInBits);
2693 }
2694 case X86::BI__builtin_ia32_pmuldq128:
2695 case X86::BI__builtin_ia32_pmuldq256:
2696 case X86::BI__builtin_ia32_pmuldq512: {
2697 unsigned opTypePrimitiveSizeInBits =
2698 cgm.getDataLayout().getTypeSizeInBits(ops[0].getType());
2699 return emitX86Muldq(builder, getLoc(expr->getExprLoc()), /*isSigned*/ true,
2700 ops, opTypePrimitiveSizeInBits);
2701 }
2702 case X86::BI__builtin_ia32_pternlogd512_mask:
2703 case X86::BI__builtin_ia32_pternlogq512_mask:
2704 case X86::BI__builtin_ia32_pternlogd128_mask:
2705 case X86::BI__builtin_ia32_pternlogd256_mask:
2706 case X86::BI__builtin_ia32_pternlogq128_mask:
2707 case X86::BI__builtin_ia32_pternlogq256_mask:
2708 case X86::BI__builtin_ia32_pternlogd512_maskz:
2709 case X86::BI__builtin_ia32_pternlogq512_maskz:
2710 case X86::BI__builtin_ia32_pternlogd128_maskz:
2711 case X86::BI__builtin_ia32_pternlogd256_maskz:
2712 case X86::BI__builtin_ia32_pternlogq128_maskz:
2713 case X86::BI__builtin_ia32_pternlogq256_maskz:
2714 cgm.errorNYI(expr->getSourceRange(),
2715 std::string("unimplemented X86 builtin call: ") +
2716 getContext().BuiltinInfo.getName(builtinID));
2717 return mlir::Value{};
2718 case X86::BI__builtin_ia32_vpshldd128:
2719 case X86::BI__builtin_ia32_vpshldd256:
2720 case X86::BI__builtin_ia32_vpshldd512:
2721 case X86::BI__builtin_ia32_vpshldq128:
2722 case X86::BI__builtin_ia32_vpshldq256:
2723 case X86::BI__builtin_ia32_vpshldq512:
2724 case X86::BI__builtin_ia32_vpshldw128:
2725 case X86::BI__builtin_ia32_vpshldw256:
2726 case X86::BI__builtin_ia32_vpshldw512:
2727 return emitX86FunnelShift(builder, getLoc(expr->getExprLoc()), ops[0],
2728 ops[1], ops[2], false);
2729 case X86::BI__builtin_ia32_vpshrdd128:
2730 case X86::BI__builtin_ia32_vpshrdd256:
2731 case X86::BI__builtin_ia32_vpshrdd512:
2732 case X86::BI__builtin_ia32_vpshrdq128:
2733 case X86::BI__builtin_ia32_vpshrdq256:
2734 case X86::BI__builtin_ia32_vpshrdq512:
2735 case X86::BI__builtin_ia32_vpshrdw128:
2736 case X86::BI__builtin_ia32_vpshrdw256:
2737 case X86::BI__builtin_ia32_vpshrdw512:
2738 // Ops 0 and 1 are swapped.
2739 return emitX86FunnelShift(builder, getLoc(expr->getExprLoc()), ops[1],
2740 ops[0], ops[2], true);
2741 case X86::BI__builtin_ia32_reduce_fadd_pd512:
2742 case X86::BI__builtin_ia32_reduce_fadd_ps512:
2743 case X86::BI__builtin_ia32_reduce_fadd_ph512:
2744 case X86::BI__builtin_ia32_reduce_fadd_ph256:
2745 case X86::BI__builtin_ia32_reduce_fadd_ph128: {
2746 CIRGenFPOptionsRAII FPOptsRAII(*this, expr);
2747 cir::FastMathFlags fastMathFlags = getCurrentFastMathFlags();
2748 fastMathFlags |= cir::FastMathFlags::reassoc;
2749 cir::FastMathFlagsAttr fastMath = getFastMathFlagsAttr(fastMathFlags);
2750 return cir::VecReduceFAddOp::create(builder, getLoc(expr->getExprLoc()),
2751 ops[0], ops[1], fastMath)
2752 .getResult();
2753 }
2754 case X86::BI__builtin_ia32_reduce_fmul_pd512:
2755 case X86::BI__builtin_ia32_reduce_fmul_ps512:
2756 case X86::BI__builtin_ia32_reduce_fmul_ph512:
2757 case X86::BI__builtin_ia32_reduce_fmul_ph256:
2758 case X86::BI__builtin_ia32_reduce_fmul_ph128: {
2759 CIRGenFPOptionsRAII FPOptsRAII(*this, expr);
2760 cir::FastMathFlags fastMathFlags = getCurrentFastMathFlags();
2761 fastMathFlags |= cir::FastMathFlags::reassoc;
2762 cir::FastMathFlagsAttr fastMath = getFastMathFlagsAttr(fastMathFlags);
2763 return cir::VecReduceFMulOp::create(builder, getLoc(expr->getExprLoc()),
2764 ops[0], ops[1], fastMath)
2765 .getResult();
2766 }
2767 case X86::BI__builtin_ia32_reduce_fmax_pd512:
2768 case X86::BI__builtin_ia32_reduce_fmax_ps512:
2769 case X86::BI__builtin_ia32_reduce_fmax_ph512:
2770 case X86::BI__builtin_ia32_reduce_fmax_ph256:
2771 case X86::BI__builtin_ia32_reduce_fmax_ph128: {
2772 CIRGenFPOptionsRAII FPOptsRAII(*this, expr);
2773 cir::FastMathFlags fastMathFlags = getCurrentFastMathFlags();
2774 fastMathFlags |= cir::FastMathFlags::nnan;
2775 cir::FastMathFlagsAttr fastMath = getFastMathFlagsAttr(fastMathFlags);
2776 return cir::VecReduceFMaxOp::create(builder, getLoc(expr->getExprLoc()),
2777 ops[0], fastMath)
2778 .getResult();
2779 }
2780 case X86::BI__builtin_ia32_reduce_fmin_pd512:
2781 case X86::BI__builtin_ia32_reduce_fmin_ps512:
2782 case X86::BI__builtin_ia32_reduce_fmin_ph512:
2783 case X86::BI__builtin_ia32_reduce_fmin_ph256:
2784 case X86::BI__builtin_ia32_reduce_fmin_ph128: {
2785 CIRGenFPOptionsRAII FPOptsRAII(*this, expr);
2786 cir::FastMathFlags fastMathFlags = getCurrentFastMathFlags();
2787 fastMathFlags |= cir::FastMathFlags::nnan;
2788 cir::FastMathFlagsAttr fastMath = getFastMathFlagsAttr(fastMathFlags);
2789 return cir::VecReduceFMinOp::create(builder, getLoc(expr->getExprLoc()),
2790 ops[0], fastMath)
2791 .getResult();
2792 }
2793 case X86::BI__builtin_ia32_rdrand16_step:
2794 case X86::BI__builtin_ia32_rdrand32_step:
2795 case X86::BI__builtin_ia32_rdrand64_step:
2796 case X86::BI__builtin_ia32_rdseed16_step:
2797 case X86::BI__builtin_ia32_rdseed32_step:
2798 case X86::BI__builtin_ia32_rdseed64_step: {
2799 llvm::StringRef intrinsicName;
2800 switch (builtinID) {
2801 default:
2802 llvm_unreachable("Unsupported intrinsic!");
2803 case X86::BI__builtin_ia32_rdrand16_step:
2804 intrinsicName = "x86.rdrand.16";
2805 break;
2806 case X86::BI__builtin_ia32_rdrand32_step:
2807 intrinsicName = "x86.rdrand.32";
2808 break;
2809 case X86::BI__builtin_ia32_rdrand64_step:
2810 intrinsicName = "x86.rdrand.64";
2811 break;
2812 case X86::BI__builtin_ia32_rdseed16_step:
2813 intrinsicName = "x86.rdseed.16";
2814 break;
2815 case X86::BI__builtin_ia32_rdseed32_step:
2816 intrinsicName = "x86.rdseed.32";
2817 break;
2818 case X86::BI__builtin_ia32_rdseed64_step:
2819 intrinsicName = "x86.rdseed.64";
2820 break;
2821 }
2822
2823 mlir::Location loc = getLoc(expr->getExprLoc());
2824 mlir::Type randTy = cast<cir::PointerType>(ops[0].getType()).getPointee();
2825 llvm::SmallVector<mlir::Type, 2> resultTypes = {randTy,
2826 builder.getUInt32Ty()};
2827 cir::StructType resRecord = cir::StructType::get(
2828 &getMLIRContext(), resultTypes, /*packed=*/false,
2829 /*is_class=*/false, cir::RecordType::getAllDataKinds(resultTypes));
2830
2831 mlir::Value call =
2832 builder.emitIntrinsicCallOp(loc, intrinsicName, resRecord);
2833 mlir::Value rand =
2834 cir::ExtractMemberOp::create(builder, loc, randTy, call, 0);
2835 builder.CIRBaseBuilderTy::createStore(loc, rand, ops[0]);
2836
2837 return cir::ExtractMemberOp::create(builder, loc, builder.getUInt32Ty(),
2838 call, 1);
2839 }
2840 case X86::BI__builtin_ia32_addcarryx_u32:
2841 case X86::BI__builtin_ia32_addcarryx_u64:
2842 case X86::BI__builtin_ia32_subborrow_u32:
2843 case X86::BI__builtin_ia32_subborrow_u64: {
2844 llvm::StringRef intrinsicName;
2845 switch (builtinID) {
2846 default:
2847 llvm_unreachable("Unsupported intrinsic!");
2848 case X86::BI__builtin_ia32_addcarryx_u32:
2849 intrinsicName = "x86.addcarry.32";
2850 break;
2851 case X86::BI__builtin_ia32_addcarryx_u64:
2852 intrinsicName = "x86.addcarry.64";
2853 break;
2854 case X86::BI__builtin_ia32_subborrow_u32:
2855 intrinsicName = "x86.subborrow.32";
2856 break;
2857 case X86::BI__builtin_ia32_subborrow_u64:
2858 intrinsicName = "x86.subborrow.64";
2859 break;
2860 }
2861
2862 mlir::Location loc = getLoc(expr->getExprLoc());
2863 mlir::Type carryTy = ops[0].getType();
2864 mlir::Type valueTy = ops[1].getType();
2865 llvm::SmallVector<mlir::Type, 2> resultTypes = {carryTy, valueTy};
2866 cir::StructType resRecord =
2867 builder.getAnonRecordTy(resultTypes, /*packed=*/false,
2869
2870 mlir::Value call =
2871 builder.emitIntrinsicCallOp(loc, intrinsicName, resRecord,
2872 mlir::ValueRange{ops[0], ops[1], ops[2]});
2873 mlir::Value result =
2874 cir::ExtractMemberOp::create(builder, loc, valueTy, call, 1);
2875 builder.CIRBaseBuilderTy::createStore(loc, result, ops[3]);
2876
2877 return cir::ExtractMemberOp::create(builder, loc, carryTy, call, 0);
2878 }
2879 case X86::BI__builtin_ia32_fpclassps128_mask:
2880 case X86::BI__builtin_ia32_fpclassps256_mask:
2881 case X86::BI__builtin_ia32_fpclassps512_mask:
2882 case X86::BI__builtin_ia32_vfpclassbf16128_mask:
2883 case X86::BI__builtin_ia32_vfpclassbf16256_mask:
2884 case X86::BI__builtin_ia32_vfpclassbf16512_mask:
2885 case X86::BI__builtin_ia32_fpclassph128_mask:
2886 case X86::BI__builtin_ia32_fpclassph256_mask:
2887 case X86::BI__builtin_ia32_fpclassph512_mask:
2888 case X86::BI__builtin_ia32_fpclasspd128_mask:
2889 case X86::BI__builtin_ia32_fpclasspd256_mask:
2890 case X86::BI__builtin_ia32_fpclasspd512_mask:
2891 return emitX86Fpclass(builder, getLoc(expr->getExprLoc()), builtinID, ops);
2892 case X86::BI__builtin_ia32_vp2intersect_q_512:
2893 case X86::BI__builtin_ia32_vp2intersect_q_256:
2894 case X86::BI__builtin_ia32_vp2intersect_q_128:
2895 case X86::BI__builtin_ia32_vp2intersect_d_512:
2896 case X86::BI__builtin_ia32_vp2intersect_d_256:
2897 case X86::BI__builtin_ia32_vp2intersect_d_128: {
2898 unsigned numElts = cast<cir::VectorType>(ops[0].getType()).getFixedSize();
2899 mlir::Location loc = getLoc(expr->getExprLoc());
2900 StringRef intrinsicName;
2901
2902 switch (builtinID) {
2903 default:
2904 llvm_unreachable("Unexpected builtin");
2905 case X86::BI__builtin_ia32_vp2intersect_q_512:
2906 intrinsicName = "x86.avx512.vp2intersect.q.512";
2907 break;
2908 case X86::BI__builtin_ia32_vp2intersect_q_256:
2909 intrinsicName = "x86.avx512.vp2intersect.q.256";
2910 break;
2911 case X86::BI__builtin_ia32_vp2intersect_q_128:
2912 intrinsicName = "x86.avx512.vp2intersect.q.128";
2913 break;
2914 case X86::BI__builtin_ia32_vp2intersect_d_512:
2915 intrinsicName = "x86.avx512.vp2intersect.d.512";
2916 break;
2917 case X86::BI__builtin_ia32_vp2intersect_d_256:
2918 intrinsicName = "x86.avx512.vp2intersect.d.256";
2919 break;
2920 case X86::BI__builtin_ia32_vp2intersect_d_128:
2921 intrinsicName = "x86.avx512.vp2intersect.d.128";
2922 break;
2923 }
2924
2925 auto resVector = cir::VectorType::getFixed(builder.getSIntNTy(1), numElts);
2926
2927 mlir::Type resMembers[] = {resVector, resVector};
2928 cir::StructType resRecord = cir::StructType::get(
2929 &getMLIRContext(), resMembers, /*packed=*/false, /*is_class=*/false,
2931
2932 mlir::Value call = builder.emitIntrinsicCallOp(
2933 getLoc(expr->getExprLoc()), intrinsicName, resRecord,
2934 mlir::ValueRange{ops[0], ops[1]});
2935 mlir::Value result =
2936 cir::ExtractMemberOp::create(builder, loc, resVector, call, 0);
2937 result = emitX86MaskedCompareResult(builder, result, numElts, nullptr, loc);
2938 Address addr = Address(
2939 ops[2], clang::CharUnits::fromQuantity(std::max(1U, numElts / 8)));
2940 builder.createStore(loc, result, addr);
2941
2942 result = cir::ExtractMemberOp::create(builder, loc, resVector, call, 1);
2943 result = emitX86MaskedCompareResult(builder, result, numElts, nullptr, loc);
2944 addr = Address(ops[3],
2945 clang::CharUnits::fromQuantity(std::max(1U, numElts / 8)));
2946 builder.createStore(loc, result, addr);
2947 return mlir::Value{};
2948 }
2949 case X86::BI__builtin_ia32_vpmultishiftqb128:
2950 case X86::BI__builtin_ia32_vpmultishiftqb256:
2951 case X86::BI__builtin_ia32_vpmultishiftqb512:
2952 case X86::BI__builtin_ia32_vpshufbitqmb128_mask:
2953 case X86::BI__builtin_ia32_vpshufbitqmb256_mask:
2954 case X86::BI__builtin_ia32_vpshufbitqmb512_mask:
2955 cgm.errorNYI(expr->getSourceRange(),
2956 std::string("unimplemented X86 builtin call: ") +
2957 getContext().BuiltinInfo.getName(builtinID));
2958 return {};
2959 case X86::BI__builtin_ia32_cmpeqps:
2960 case X86::BI__builtin_ia32_cmpeqpd:
2961 return emitVectorFCmp(*this, *expr, ops, cir::FCmpOpKind::oeq,
2962 /*signaling=*/false);
2963 case X86::BI__builtin_ia32_cmpltps:
2964 case X86::BI__builtin_ia32_cmpltpd:
2965 return emitVectorFCmp(*this, *expr, ops, cir::FCmpOpKind::olt,
2966 /*signaling=*/true);
2967 case X86::BI__builtin_ia32_cmpleps:
2968 case X86::BI__builtin_ia32_cmplepd:
2969 return emitVectorFCmp(*this, *expr, ops, cir::FCmpOpKind::ole,
2970 /*signaling=*/true);
2971 case X86::BI__builtin_ia32_cmpunordps:
2972 case X86::BI__builtin_ia32_cmpunordpd:
2973 return emitVectorFCmp(*this, *expr, ops, cir::FCmpOpKind::uno,
2974 /*signaling=*/false);
2975 case X86::BI__builtin_ia32_cmpneqps:
2976 case X86::BI__builtin_ia32_cmpneqpd:
2977 return emitVectorFCmp(*this, *expr, ops, cir::FCmpOpKind::une,
2978 /*signaling=*/false);
2979 case X86::BI__builtin_ia32_cmpnltps:
2980 case X86::BI__builtin_ia32_cmpnltpd:
2981 return emitVectorFCmp(*this, *expr, ops, cir::FCmpOpKind::uge,
2982 /*signaling=*/true);
2983 case X86::BI__builtin_ia32_cmpnleps:
2984 case X86::BI__builtin_ia32_cmpnlepd:
2985 return emitVectorFCmp(*this, *expr, ops, cir::FCmpOpKind::ugt,
2986 /*signaling=*/true);
2987 case X86::BI__builtin_ia32_cmpordps:
2988 case X86::BI__builtin_ia32_cmpordpd:
2989 return emitVectorFCmp(*this, *expr, ops, cir::FCmpOpKind::ord,
2990 /*signaling=*/false);
2991 case X86::BI__builtin_ia32_cmpph128_mask:
2992 case X86::BI__builtin_ia32_cmpph256_mask:
2993 case X86::BI__builtin_ia32_cmpph512_mask:
2994 case X86::BI__builtin_ia32_cmpps128_mask:
2995 case X86::BI__builtin_ia32_cmpps256_mask:
2996 case X86::BI__builtin_ia32_cmpps512_mask:
2997 case X86::BI__builtin_ia32_cmppd128_mask:
2998 case X86::BI__builtin_ia32_cmppd256_mask:
2999 case X86::BI__builtin_ia32_cmppd512_mask:
3000 case X86::BI__builtin_ia32_vcmpbf16512_mask:
3001 case X86::BI__builtin_ia32_vcmpbf16256_mask:
3002 case X86::BI__builtin_ia32_vcmpbf16128_mask:
3003 isMaskFCmp = true;
3004 [[fallthrough]];
3005 case X86::BI__builtin_ia32_cmpps:
3006 case X86::BI__builtin_ia32_cmpps256:
3007 case X86::BI__builtin_ia32_cmppd:
3008 case X86::BI__builtin_ia32_cmppd256: {
3009 mlir::Location loc = getLoc(expr->getExprLoc());
3010
3011 // The third argument is the comparison condition.
3012 unsigned cc =
3013 ops[2].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue() &
3014 0x1f;
3015
3016 X86FCmpInfo cmpInfo = getX86FCmpInfo(cc);
3017
3018 auto opsTy = cast<cir::VectorType>(ops[0].getType());
3019 unsigned numElts = opsTy.getFixedSize();
3020
3021 // If the predicate is true or false, or the builtin is mask enabled, and
3022 // we're using constrained intrinsics, there is no compare operation we can
3023 // use. Just use the legacy X86 specific intrinsic.
3024 if (builder.getIsFPConstrained() &&
3025 (cmpInfo.pred == cir::FCmpOpKind::_true ||
3026 cmpInfo.pred == cir::FCmpOpKind::_false || isMaskFCmp)) {
3027 llvm::StringRef intrinsicName = getX86FCmpIntrinsicName(builtinID);
3028
3029 if (isMaskFCmp) {
3030 ops[3] = getMaskVecValue(builder, loc, ops[3], numElts);
3031 mlir::Value cmp = builder.emitIntrinsicCallOp(
3032 loc, intrinsicName,
3033 cir::VectorType::getFixed(builder.getSIntNTy(1), numElts), ops);
3034 return emitX86MaskedCompareResult(builder, cmp, numElts, nullptr, loc);
3035 }
3036
3037 return builder.emitIntrinsicCallOp(loc, intrinsicName, ops[0].getType(),
3038 ops);
3039 }
3040
3041 unsigned width = builder.getCIRIntOrFloatBitWidth(opsTy.getElementType());
3042 cir::IntType cmpEltTy = builder.getSIntNTy(isMaskFCmp ? 1 : width);
3043 auto cmpTy = cir::VectorType::getFixed(cmpEltTy, numElts);
3044
3045 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(*this, expr);
3046 mlir::Value cmp = builder.createVecFCompare(
3047 loc, cmpTy, cmpInfo.pred, ops[0], ops[1], cmpInfo.isSignaling);
3048
3049 if (isMaskFCmp)
3050 return emitX86MaskedCompareResult(builder, cmp, numElts, ops[3], loc);
3051
3052 return builder.createBitcast(cmp, opsTy);
3053 }
3054 case X86::BI__builtin_ia32_cmpeqss:
3055 case X86::BI__builtin_ia32_cmpltss:
3056 case X86::BI__builtin_ia32_cmpless:
3057 case X86::BI__builtin_ia32_cmpunordss:
3058 case X86::BI__builtin_ia32_cmpneqss:
3059 case X86::BI__builtin_ia32_cmpnltss:
3060 case X86::BI__builtin_ia32_cmpnless:
3061 case X86::BI__builtin_ia32_cmpordss:
3062 case X86::BI__builtin_ia32_cmpeqsd:
3063 case X86::BI__builtin_ia32_cmpltsd:
3064 case X86::BI__builtin_ia32_cmplesd:
3065 case X86::BI__builtin_ia32_cmpunordsd:
3066 case X86::BI__builtin_ia32_cmpneqsd:
3067 case X86::BI__builtin_ia32_cmpnltsd:
3068 case X86::BI__builtin_ia32_cmpnlesd:
3069 case X86::BI__builtin_ia32_cmpordsd:
3070 cgm.errorNYI(expr->getSourceRange(),
3071 std::string("unimplemented X86 builtin call: ") +
3072 getContext().BuiltinInfo.getName(builtinID));
3073 return {};
3074 case X86::BI__builtin_ia32_vcvtph2ps_mask:
3075 case X86::BI__builtin_ia32_vcvtph2ps256_mask:
3076 case X86::BI__builtin_ia32_vcvtph2ps512_mask: {
3077 mlir::Location loc = getLoc(expr->getExprLoc());
3078 return emitX86CvtF16ToFloatExpr(builder, loc, ops,
3079 convertType(expr->getType()));
3080 }
3081 case X86::BI__builtin_ia32_cvtneps2bf16_128_mask: {
3082 mlir::Location loc = getLoc(expr->getExprLoc());
3083 cir::VectorType resTy = cast<cir::VectorType>(convertType(expr->getType()));
3084
3085 cir::VectorType inputTy = cast<cir::VectorType>(ops[0].getType());
3086 unsigned numElts = inputTy.getFixedSize();
3087
3088 mlir::Value mask = getMaskVecValue(builder, loc, ops[2], numElts);
3089
3091 args.push_back(ops[0]);
3092 args.push_back(ops[1]);
3093 args.push_back(mask);
3094
3095 return builder.emitIntrinsicCallOp(
3096 loc, "x86.avx512bf16.mask.cvtneps2bf16.128", resTy, args);
3097 }
3098 case X86::BI__builtin_ia32_cvtneps2bf16_256_mask:
3099 case X86::BI__builtin_ia32_cvtneps2bf16_512_mask: {
3100 mlir::Location loc = getLoc(expr->getExprLoc());
3101 cir::VectorType resTy = cast<cir::VectorType>(convertType(expr->getType()));
3102 StringRef intrinsicName;
3103 if (builtinID == X86::BI__builtin_ia32_cvtneps2bf16_256_mask) {
3104 intrinsicName = "x86.avx512bf16.cvtneps2bf16.256";
3105 } else {
3106 assert(builtinID == X86::BI__builtin_ia32_cvtneps2bf16_512_mask);
3107 intrinsicName = "x86.avx512bf16.cvtneps2bf16.512";
3108 }
3109
3110 mlir::Value res = builder.emitIntrinsicCallOp(loc, intrinsicName, resTy,
3111 mlir::ValueRange{ops[0]});
3112
3113 return emitX86Select(builder, loc, ops[2], res, ops[1]);
3114 }
3115 case X86::BI__cpuid:
3116 case X86::BI__cpuidex: {
3117 mlir::Location loc = getLoc(expr->getExprLoc());
3118 mlir::Value subFuncId = builtinID == X86::BI__cpuidex
3119 ? ops[2]
3120 : builder.getConstInt(loc, sInt32Ty, 0);
3121 cir::CpuIdOp::create(builder, loc, /*cpuInfo=*/ops[0],
3122 /*functionId=*/ops[1], /*subFunctionId=*/subFuncId);
3123 return mlir::Value{};
3124 }
3125 case X86::BI__emul:
3126 case X86::BI__emulu:
3127 case X86::BI__mulh:
3128 case X86::BI__umulh:
3129 case X86::BI_mul128:
3130 case X86::BI_umul128: {
3131 cgm.errorNYI(expr->getSourceRange(),
3132 std::string("unimplemented X86 builtin call: ") +
3133 getContext().BuiltinInfo.getName(builtinID));
3134 return mlir::Value{};
3135 }
3136 case X86::BI__faststorefence: {
3137 cir::AtomicFenceOp::create(
3138 builder, getLoc(expr->getExprLoc()),
3139 cir::MemOrder::SequentiallyConsistent,
3140 cir::SyncScopeKindAttr::get(&getMLIRContext(),
3141 cir::SyncScopeKind::System));
3142 return mlir::Value{};
3143 }
3144 case X86::BI__shiftleft128:
3145 case X86::BI__shiftright128: {
3146 // Flip low/high ops and zero-extend amount to matching type.
3147 // shiftleft128(Low, High, Amt) -> fshl(High, Low, Amt)
3148 // shiftright128(Low, High, Amt) -> fshr(High, Low, Amt)
3149 std::swap(ops[0], ops[1]);
3150
3151 // Zero-extend shift amount to i64 if needed
3152 auto amtTy = mlir::cast<cir::IntType>(ops[2].getType());
3153 cir::IntType i64Ty = builder.getUInt64Ty();
3154
3155 if (amtTy != i64Ty)
3156 ops[2] = builder.createIntCast(ops[2], i64Ty);
3157
3158 const StringRef intrinsicName =
3159 (builtinID == X86::BI__shiftleft128) ? "fshl" : "fshr";
3160 return builder.emitIntrinsicCallOp(
3161 getLoc(expr->getExprLoc()), intrinsicName, i64Ty,
3162 mlir::ValueRange{ops[0], ops[1], ops[2]});
3163 }
3164 case X86::BI_AddressOfReturnAddress: {
3165 mlir::Location loc = getLoc(expr->getExprLoc());
3166 mlir::Value addr =
3167 cir::AddrOfReturnAddrOp::create(builder, loc, allocaInt8PtrTy);
3168 return builder.createCast(loc, cir::CastKind::bitcast, addr, voidPtrTy);
3169 }
3170 case X86::BI__stosb:
3171 case X86::BI__ud2:
3172 case X86::BI__int2c:
3173 case X86::BI__readfsbyte:
3174 case X86::BI__readfsword:
3175 case X86::BI__readfsdword:
3176 case X86::BI__readfsqword:
3177 case X86::BI__readgsbyte:
3178 case X86::BI__readgsword:
3179 case X86::BI__readgsdword:
3180 case X86::BI__readgsqword: {
3181 cgm.errorNYI(expr->getSourceRange(),
3182 std::string("unimplemented X86 builtin call: ") +
3183 getContext().BuiltinInfo.getName(builtinID));
3184 return mlir::Value{};
3185 }
3186 case X86::BI__builtin_ia32_encodekey128_u32: {
3187 return emitEncodeKey(&getMLIRContext(), builder, getLoc(expr->getExprLoc()),
3188 {ops[0], ops[1]}, ops[2], 6, "x86.encodekey128", 3);
3189 }
3190 case X86::BI__builtin_ia32_encodekey256_u32: {
3191
3192 return emitEncodeKey(&getMLIRContext(), builder, getLoc(expr->getExprLoc()),
3193 {ops[0], ops[1], ops[2]}, ops[3], 7,
3194 "x86.encodekey256", 4);
3195 }
3196
3197 case X86::BI__builtin_ia32_aesenc128kl_u8:
3198 case X86::BI__builtin_ia32_aesdec128kl_u8:
3199 case X86::BI__builtin_ia32_aesenc256kl_u8:
3200 case X86::BI__builtin_ia32_aesdec256kl_u8: {
3201 llvm::StringRef intrinsicName;
3202 switch (builtinID) {
3203 default:
3204 llvm_unreachable("Unexpected builtin");
3205 case X86::BI__builtin_ia32_aesenc128kl_u8:
3206 intrinsicName = "x86.aesenc128kl";
3207 break;
3208 case X86::BI__builtin_ia32_aesdec128kl_u8:
3209 intrinsicName = "x86.aesdec128kl";
3210 break;
3211 case X86::BI__builtin_ia32_aesenc256kl_u8:
3212 intrinsicName = "x86.aesenc256kl";
3213 break;
3214 case X86::BI__builtin_ia32_aesdec256kl_u8:
3215 intrinsicName = "x86.aesdec256kl";
3216 break;
3217 }
3218
3219 return emitX86Aes(builder, getLoc(expr->getExprLoc()), intrinsicName,
3220 convertType(expr->getType()), ops);
3221 }
3222 case X86::BI__builtin_ia32_aesencwide128kl_u8:
3223 case X86::BI__builtin_ia32_aesdecwide128kl_u8:
3224 case X86::BI__builtin_ia32_aesencwide256kl_u8:
3225 case X86::BI__builtin_ia32_aesdecwide256kl_u8: {
3226 llvm::StringRef intrinsicName;
3227 switch (builtinID) {
3228 default:
3229 llvm_unreachable("Unexpected builtin");
3230 case X86::BI__builtin_ia32_aesencwide128kl_u8:
3231 intrinsicName = "x86.aesencwide128kl";
3232 break;
3233 case X86::BI__builtin_ia32_aesdecwide128kl_u8:
3234 intrinsicName = "x86.aesdecwide128kl";
3235 break;
3236 case X86::BI__builtin_ia32_aesencwide256kl_u8:
3237 intrinsicName = "x86.aesencwide256kl";
3238 break;
3239 case X86::BI__builtin_ia32_aesdecwide256kl_u8:
3240 intrinsicName = "x86.aesdecwide256kl";
3241 break;
3242 }
3243
3244 return emitX86Aeswide(builder, getLoc(expr->getExprLoc()), intrinsicName,
3245 convertType(expr->getType()), ops);
3246 }
3247 case X86::BI__builtin_ia32_vfcmaddcph512_mask:
3248 case X86::BI__builtin_ia32_vfmaddcph512_mask:
3249 case X86::BI__builtin_ia32_vfcmaddcsh_round_mask:
3250 case X86::BI__builtin_ia32_vfmaddcsh_round_mask:
3251 case X86::BI__builtin_ia32_vfcmaddcsh_round_mask3:
3252 case X86::BI__builtin_ia32_vfmaddcsh_round_mask3:
3253 case X86::BI__builtin_ia32_prefetchi:
3254 cgm.errorNYI(expr->getSourceRange(),
3255 std::string("unimplemented X86 builtin call: ") +
3256 getContext().BuiltinInfo.getName(builtinID));
3257 return mlir::Value{};
3258 }
3259}
#define X86_CPU_TYPE(ENUM, STR, ABI_VALUE)
#define X86_CPU_SUBTYPE(ENUM, STR, ABI_VALUE)
#define X86_VENDOR(ENUM, STRING, ABI_VALUE)
Defines enum values for all the target-independent builtin functions.
static mlir::Value emitX86MaskLogic(CIRGenBuilderTy &builder, mlir::Location loc, SmallVectorImpl< mlir::Value > &ops, bool invertLHS=false)
static mlir::Value emitVectorFCmp(CIRGenFunction &cgf, const CallExpr &expr, llvm::SmallVector< mlir::Value > &ops, cir::FCmpOpKind pred, bool signaling)
static X86FCmpInfo getX86FCmpInfo(unsigned cc)
Decodes comparison condition of x86 cmppps/cmppd* builtins which is an integer in the range [0,...
static mlir::Value emitX86vpcom(CIRGenBuilderTy &builder, mlir::Location loc, llvm::SmallVector< mlir::Value > ops, bool isSigned)
static llvm::StringRef getX86FCmpIntrinsicName(unsigned builtinID)
Returns the legacy x86 intrinsic that implements the given x86 cmppps/cmppd* builtin.
static std::optional< mlir::Value > emitX86ConvertToMask(CIRGenFunction &cgf, CIRGenBuilderTy &builder, mlir::Value in, mlir::Location loc)
static mlir::Value emitX86CompressExpand(CIRGenBuilderTy &builder, mlir::Location loc, mlir::Value source, mlir::Value mask, mlir::Value inputVector, const std::string &id)
static void computeFullLaneShuffleMask(CIRGenFunction &cgf, const mlir::Value vec, uint32_t imm, const bool isShufP, llvm::SmallVectorImpl< int64_t > &outIndices)
static mlir::Value emitX86VPerm2f128(CIRGenBuilderTy &builder, mlir::Location loc, llvm::SmallVector< mlir::Value > ops)
static std::optional< mlir::Value > emitX86SExtMask(CIRGenBuilderTy &builder, mlir::Value op, mlir::Type dstTy, mlir::Location loc)
static mlir::Value emitX86Round(CIRGenFunction &cgf, unsigned builtinID, const CallExpr *expr, SmallVector< mlir::Value > &ops)
static mlir::Value emitX86PackedByteShift(CIRGenBuilderTy &builder, unsigned builtinID, mlir::Location loc, llvm::ArrayRef< mlir::Value > ops, llvm::Boolean isLeftShift)
static std::optional< mlir::Value > emitX86MaskedCompare(CIRGenBuilderTy &builder, unsigned cc, bool isSigned, ArrayRef< mlir::Value > ops, mlir::Location loc)
static mlir::Value emitPrefetch(CIRGenFunction &cgf, unsigned builtinID, const CallExpr *e, const SmallVector< mlir::Value > &ops)
static mlir::Value getMaskZeroBitAsBool(CIRGenBuilderTy &builder, mlir::Location loc, mlir::Value mask)
static mlir::Value emitX86Aeswide(CIRGenBuilderTy &builder, mlir::Location loc, llvm::StringRef intrinsicName, mlir::Type retType, llvm::ArrayRef< mlir::Value > ops)
static mlir::Value emitX86MaskTest(CIRGenBuilderTy &builder, mlir::Location loc, const std::string &intrinsicName, SmallVectorImpl< mlir::Value > &ops)
static mlir::Value emitEncodeKey(mlir::MLIRContext *context, CIRGenBuilderTy &builder, const mlir::Location &location, mlir::ValueRange inputOperands, mlir::Value outputOperand, std::uint8_t vecOutputCount, const std::string &intrinsicName, std::uint8_t numResults)
static mlir::Value emitVecInsert(CIRGenBuilderTy &builder, mlir::Location loc, mlir::Value vec, mlir::Value value, mlir::Value indexOp)
static mlir::Value emitX86Fpclass(CIRGenBuilderTy &builder, mlir::Location loc, unsigned builtinID, SmallVectorImpl< mlir::Value > &ops)
static mlir::Value emitX86MaskedLoad(CIRGenBuilderTy &builder, ArrayRef< mlir::Value > ops, llvm::Align alignment, mlir::Location loc)
static mlir::Value emitX86MaskUnpack(CIRGenBuilderTy &builder, mlir::Location loc, const std::string &intrinsicName, SmallVectorImpl< mlir::Value > &ops)
static cir::VecShuffleOp emitPshufWord(CIRGenBuilderTy &builder, const mlir::Value vec, const mlir::Value immediate, const mlir::Location loc, const bool isLow)
static mlir::Value emitX86MaskedCompareResult(CIRGenBuilderTy &builder, mlir::Value cmp, unsigned numElts, mlir::Value maskIn, mlir::Location loc)
static mlir::Value emitX86Select(CIRGenBuilderTy &builder, mlir::Location loc, mlir::Value mask, mlir::Value op0, mlir::Value op1)
static mlir::Value emitX86Aes(CIRGenBuilderTy &builder, mlir::Location loc, llvm::StringRef intrinsicName, mlir::Type retType, llvm::ArrayRef< mlir::Value > ops)
static mlir::Value emitX86CvtF16ToFloatExpr(CIRGenBuilderTy &builder, mlir::Location loc, llvm::ArrayRef< mlir::Value > ops, mlir::Type dstTy)
static mlir::Value emitX86FunnelShift(CIRGenBuilderTy &builder, mlir::Location location, mlir::Value &op0, mlir::Value &op1, mlir::Value &amt, bool isRight)
static mlir::Value emitX86Muldq(CIRGenBuilderTy &builder, mlir::Location loc, bool isSigned, SmallVectorImpl< mlir::Value > &ops, unsigned opTypePrimitiveSizeInBits)
static mlir::Value getMaskVecValue(CIRGenBuilderTy &builder, mlir::Location loc, mlir::Value mask, unsigned numElems)
static mlir::Value emitX86MaskAddLogic(CIRGenBuilderTy &builder, mlir::Location loc, const std::string &intrinsicName, SmallVectorImpl< mlir::Value > &ops)
static mlir::Value emitX86CompressStore(CIRGenBuilderTy &builder, mlir::Location loc, ArrayRef< mlir::Value > ops)
static mlir::Value emitX86ScalarSelect(CIRGenBuilderTy &builder, mlir::Location loc, mlir::Value mask, mlir::Value op0, mlir::Value op1)
TokenType getType() const
Returns the token's type, e.g.
#define ENUM(NAME, LIT)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
Enumerates target-specific builtins in their own namespaces within namespace clang.
cir::FenvAttr getConstrainedFPAttr()
Build the #cir.fenv attribute describing the constrained floating-point environment currently in effe...
mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ, const llvm::APInt &val)
mlir::Value createShift(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, bool isShiftLeft)
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc)
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr)
mlir::Value createCast(mlir::Location loc, cir::CastKind kind, mlir::Value src, mlir::Type newTy)
cir::PtrStrideOp createPtrStride(mlir::Location loc, mlir::Value base, mlir::Value stride)
cir::VecFCmpOp createVecFCompare(mlir::Location loc, cir::FCmpOpKind kind, mlir::Value lhs, mlir::Value rhs, bool signaling=false)
Compares lhs and rhs element-wise.
mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowFlags flags=OverflowFlags::None)
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy)
mlir::Value createAnd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createExtractElement(mlir::Location loc, mlir::Value vec, uint64_t idx)
cir::VecCmpOp createVecCompare(mlir::Location loc, cir::CmpOpKind kind, mlir::Value lhs, mlir::Value rhs)
Compares lhs and rhs element-wise.
mlir::Value createIntCast(mlir::Value src, mlir::Type newTy)
mlir::Value createInsertElement(mlir::Location loc, mlir::Value vec, mlir::Value newElt, uint64_t idx)
mlir::Value createBitcast(mlir::Value src, mlir::Type newTy)
mlir::Value createNot(mlir::Location loc, mlir::Value value)
mlir::Value createSelect(mlir::Location loc, mlir::Value condition, mlir::Value trueValue, mlir::Value falseValue)
void setDefaultConstrainedExcept(clang::LangOptions::FPExceptionModeKind newExcept)
Set the exception handling to be used with constrained floating point.
bool getIsFPConstrained() const
Query for the use of constrained floating point math.
cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value={})
Create a yield operation.
cir::BoolType getBoolTy()
llvm::TypeSize getTypeSizeInBits(mlir::Type ty) const
C++ view class that accepts both !cir.struct and !cir.union types.
Definition CIRTypes.h:153
static llvm::SmallVector< RecordMemberKind > getAllDataKinds(llvm::ArrayRef< mlir::Type > members)
One Data kind per member.
Definition CIRTypes.cpp:156
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, unsigned *IntegerConstantArgs=nullptr) const
Return the type for the specified builtin.
@ GE_None
No error.
mlir::Value getPointer() const
Definition Address.h:98
mlir::Type getElementType() const
Definition Address.h:125
cir::ConstantOp getUInt64(uint64_t c, mlir::Location loc)
mlir::Value emitIntrinsicCallOp(mlir::Location loc, const llvm::StringRef str, const mlir::Type &resTy, Operands &&...op)
cir::IntType getSIntNTy(int n)
cir::ConstantOp getUInt32(uint32_t c, mlir::Location loc)
cir::VecShuffleOp createVecShuffle(mlir::Location loc, mlir::Value vec1, mlir::Value vec2, llvm::ArrayRef< mlir::Attribute > maskAttrs)
mlir::Value createMaskedLoad(mlir::Location loc, mlir::Type ty, mlir::Value ptr, llvm::Align alignment, mlir::Value mask, mlir::Value passThru)
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, Address dst, bool isVolatile=false, bool isNontemporal=false, mlir::IntegerAttr align={}, cir::SyncScopeKindAttr scope={}, cir::MemOrderAttr order={})
cir::LoadOp createAlignedLoad(mlir::Location loc, mlir::Type ty, mlir::Value ptr, llvm::MaybeAlign align)
cir::ConstantOp getZero(mlir::Location loc, mlir::Type ty)
cir::StructType getAnonRecordTy(llvm::ArrayRef< mlir::Type > members, bool packed, llvm::ArrayRef< cir::RecordMemberKind > memberKinds)
Get a CIR anonymous struct type.
mlir::Value getArrayElement(mlir::Location arrayLocBegin, mlir::Location arrayLocEnd, mlir::Value arrayPtr, mlir::Type eltTy, mlir::Value idx, bool shouldDecay, cir::PtrOffsetFlags flags=cir::PtrOffsetFlags::None)
Create a cir.get_element or cir.ptr_stride operation to get access to an array element.
cir::ConstantOp getSInt32(int32_t c, mlir::Location loc)
cir::IntType getUIntNTy(int n)
mlir::Type convertType(clang::QualType t)
Address emitPointerWithAlignment(const clang::Expr *expr, LValueBaseInfo *baseInfo=nullptr)
Given an expression with a pointer type, emit the value and compute our best estimate of the alignmen...
mlir::Value emitX86CpuIs(const CallExpr *expr)
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
mlir::Value emitX86CpuInit(mlir::Location loc)
mlir::Value emitX86CpuSupports(const CallExpr *expr)
static int64_t getZExtIntValueFromConstOp(mlir::Value val)
Get zero-extended integer from a mlir::Value that is an int constant or a constant op.
cir::FastMathFlagsAttr getFastMathFlagsAttr(cir::FastMathFlags fastMathFlags)
Create a CIR fast-math attribute from the specified flags.
cir::FastMathFlags getCurrentFastMathFlags() const
Convert the active Clang floating-point options to CIR fast-math flags.
CodeGenUtils::MSVCIntrin MSVCIntrin
static int64_t getSExtIntValueFromConstOp(mlir::Value val)
Get integer from a mlir::Value that is an int constant or a constant op.
std::optional< mlir::Value > emitX86BuiltinExpr(unsigned builtinID, const CallExpr *expr)
mlir::Value emitX86MSVCIntrin(MSVCIntrin builtinID, const CallExpr *E)
mlir::Value emitScalarExpr(const clang::Expr *e, bool ignoreResultAssign=false)
Emit the computation of the specified expression of scalar type.
cir::GetGlobalOp createGetCpuModel(mlir::Location loc)
CIRGenBuilderTy & getBuilder()
mlir::MLIRContext & getMLIRContext()
cir::GetGlobalOp createGetCpuFeatures2(mlir::Location loc)
clang::ASTContext & getContext() const
Address createMemTemp(QualType t, mlir::Location loc, const Twine &name="tmp", Address *alloca=nullptr, mlir::OpBuilder::InsertPoint ip={})
Create a temporary memory object of the given type, with appropriate alignmen and cast it to the defa...
mlir::Value emitScalarOrConstFoldImmArg(unsigned iceArguments, unsigned idx, const Expr *argExpr)
const cir::CIRDataLayout & getDataLayout() const
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:3012
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3216
static CharUnits One()
Construct a CharUnits quantity of one.
Definition CharUnits.h:55
static CharUnits fromQuantity(QuantityType Quantity)
Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:58
This represents one expression.
Definition Expr.h:114
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:283
QualType getType() const
Definition Expr.h:146
@ FPE_Ignore
Assume that floating-point exceptions are masked.
std::optional< MSVCIntrin > translateX86ToMsvcIntrin(unsigned BuiltinID)
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
Top level wrappers for InstallAPI frontend operations.
@ Default
Set to the current date and time.
U cast(CodeGen::Address addr)
Definition Address.h:327
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
int32_t uint32_t uint32_t __builtin_riscv_mhracc_i32 __builtin_riscv_mhraccu_u32 __builtin_riscv_mhraccsu_i32 __builtin_riscv_mhacc_h1_i32 __builtin_riscv_mhaccsu_h1_i32 __builtin_riscv_mulqr_i32 __builtin_riscv_mqrwacc_i64 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t
cir::FCmpOpKind pred
static bool handleBuiltinICEArguments()
static bool setDLLStorageClass()
cir::PointerType allocaInt8PtrTy
void* in alloca address space
cir::PointerType voidPtrTy
void* in address space 0