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