24#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
25#include "mlir/IR/Location.h"
26#include "mlir/IR/Value.h"
49 bool isDivRemOp()
const {
50 return opcode == BO_Div || opcode == BO_Rem || opcode == BO_DivAssign ||
51 opcode == BO_RemAssign;
55 bool mayHaveIntegerOverflow()
const {
57 auto lhsci = lhs.getDefiningOp<cir::ConstantOp>();
58 auto rhsci = rhs.getDefiningOp<cir::ConstantOp>();
70 bool isFixedPointOp()
const {
73 if (
const auto *binOp = llvm::dyn_cast<BinaryOperator>(e)) {
74 QualType lhstype = binOp->getLHS()->getType();
75 QualType rhstype = binOp->getRHS()->getType();
78 if (
const auto *unop = llvm::dyn_cast<UnaryOperator>(e))
79 return unop->getSubExpr()->getType()->isFixedPointType();
84class ScalarExprEmitter :
public StmtVisitor<ScalarExprEmitter, mlir::Value> {
86 CIRGenBuilderTy &builder;
90 bool ignoreResultAssign;
93 ScalarExprEmitter(CIRGenFunction &cgf, CIRGenBuilderTy &builder,
94 bool ignoreResultAssign =
false)
95 : cgf(cgf), builder(builder), ignoreResultAssign(ignoreResultAssign) {}
100 mlir::Type convertType(QualType ty) {
return cgf.convertType(ty); }
102 mlir::Value emitComplexToScalarConversion(mlir::Location loc,
106 mlir::Value emitNullValue(QualType ty, mlir::Location loc) {
107 return cgf.cgm.emitNullConstant(ty, loc);
110 mlir::Value emitPromotedValue(mlir::Value result, QualType promotionType) {
111 return builder.createFloatingCast(result, cgf.convertType(promotionType));
114 mlir::Value emitUnPromotedValue(mlir::Value result, QualType exprType) {
115 return builder.createFloatingCast(result, cgf.convertType(exprType));
118 mlir::Value emitPromoted(
const Expr *e, QualType promotionType);
120 mlir::Value maybePromoteBoolResult(mlir::Value value,
121 mlir::Type dstTy)
const {
122 if (mlir::isa<cir::IntType>(dstTy))
123 return builder.createBoolToInt(value, dstTy);
124 if (mlir::isa<cir::BoolType>(dstTy))
126 llvm_unreachable(
"Can only promote integer or boolean types");
133 mlir::Value Visit(Expr *e) {
134 return StmtVisitor<ScalarExprEmitter, mlir::Value>::Visit(e);
137 mlir::Value VisitStmt(Stmt *s) {
138 llvm_unreachable(
"Statement passed to ScalarExprEmitter");
141 mlir::Value VisitExpr(Expr *e) {
142 cgf.getCIRGenModule().errorNYI(
147 mlir::Value VisitConstantExpr(ConstantExpr *e) {
153 if (mlir::Attribute result = ConstantEmitter(cgf).tryEmitConstantExpr(e)) {
156 "ScalarExprEmitter: constant expr GL Value");
161 mlir::cast<mlir::TypedAttr>(result));
164 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: constant expr");
168 mlir::Value VisitPackIndexingExpr(PackIndexingExpr *e) {
172 mlir::Value VisitParenExpr(ParenExpr *pe) {
return Visit(pe->
getSubExpr()); }
174 mlir::Value VisitGenericSelectionExpr(GenericSelectionExpr *ge) {
179 mlir::Value emitLoadOfLValue(
const Expr *e) {
180 LValue lv = cgf.emitLValue(e);
182 return cgf.emitLoadOfLValue(lv, e->
getExprLoc()).getValue();
185 mlir::Value VisitCoawaitExpr(CoawaitExpr *s) {
186 return cgf.emitCoawaitExpr(*s).getValue();
189 mlir::Value VisitCoyieldExpr(CoyieldExpr *e) {
190 return cgf.emitCoyieldExpr(*e).getValue();
193 mlir::Value VisitUnaryCoawait(
const UnaryOperator *e) {
194 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: unary coawait");
198 mlir::Value emitLoadOfLValue(LValue lv, SourceLocation loc) {
199 return cgf.emitLoadOfLValue(lv, loc).getValue();
203 mlir::Value VisitDeclRefExpr(DeclRefExpr *e) {
204 if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(e))
205 return cgf.emitScalarConstant(constant, e);
207 return emitLoadOfLValue(e);
210 mlir::Value VisitAddrLabelExpr(
const AddrLabelExpr *e) {
212 cir::BlockAddrInfoAttr blockInfoAttr = cir::BlockAddrInfoAttr::get(
216 return cir::BlockAddressOp::create(builder, cgf.getLoc(e->
getSourceRange()),
221 mlir::Value VisitIntegerLiteral(
const IntegerLiteral *e) {
223 return cir::ConstantOp::create(builder, cgf.getLoc(e->
getExprLoc()),
227 mlir::Value VisitFixedPointLiteral(
const FixedPointLiteral *e) {
229 return cir::ConstantOp::create(builder, cgf.getLoc(e->
getExprLoc()),
233 mlir::Value VisitFloatingLiteral(
const FloatingLiteral *e) {
235 assert(mlir::isa<cir::FPTypeInterface>(
type) &&
236 "expect floating-point type");
237 return cir::ConstantOp::create(builder, cgf.getLoc(e->
getExprLoc()),
241 mlir::Value VisitCharacterLiteral(
const CharacterLiteral *e) {
242 mlir::Type ty = cgf.convertType(e->
getType());
245 auto intTy = mlir::cast<cir::IntTypeInterface>(ty);
246 llvm::APInt apValue(intTy.getWidth(), e->
getValue(),
248 return cir::ConstantOp::create(builder, cgf.getLoc(e->
getExprLoc()),
249 cir::IntAttr::get(ty, apValue));
252 mlir::Value VisitCXXBoolLiteralExpr(
const CXXBoolLiteralExpr *e) {
256 mlir::Value VisitCXXScalarValueInitExpr(
const CXXScalarValueInitExpr *e) {
263 mlir::Value VisitGNUNullExpr(
const GNUNullExpr *e) {
267 mlir::Value VisitOffsetOfExpr(OffsetOfExpr *e);
269 mlir::Value VisitSizeOfPackExpr(SizeOfPackExpr *e) {
270 return builder.getConstInt(cgf.getLoc(e->
getExprLoc()),
273 mlir::Value VisitPseudoObjectExpr(PseudoObjectExpr *e) {
274 return cgf.emitPseudoObjectRValue(e).getValue();
276 mlir::Value VisitSYCLUniqueStableNameExpr(SYCLUniqueStableNameExpr *e);
277 mlir::Value VisitEmbedExpr(EmbedExpr *e) {
279 auto it = e->
begin();
280 llvm::APInt value = (*it)->getValue();
281 return builder.getConstInt(cgf.getLoc(e->
getExprLoc()), value,
284 mlir::Value VisitOpaqueValueExpr(OpaqueValueExpr *e) {
286 return emitLoadOfLValue(cgf.getOrCreateOpaqueLValueMapping(e),
290 return cgf.getOrCreateOpaqueRValueMapping(e).getValue();
293 mlir::Value VisitObjCSelectorExpr(ObjCSelectorExpr *e) {
294 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: objc selector");
297 mlir::Value VisitObjCProtocolExpr(ObjCProtocolExpr *e) {
298 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: objc protocol");
301 mlir::Value VisitObjCIVarRefExpr(ObjCIvarRefExpr *e) {
302 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: objc ivar ref");
305 mlir::Value VisitObjCMessageExpr(ObjCMessageExpr *e) {
306 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: objc message");
309 mlir::Value VisitObjCIsaExpr(ObjCIsaExpr *e) {
310 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: objc isa");
313 mlir::Value VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *e) {
315 "ScalarExprEmitter: objc availability check");
319 mlir::Value VisitMatrixSubscriptExpr(MatrixSubscriptExpr *e) {
320 mlir::Value rowIdx = cgf.emitScalarExpr(e->
getRowIdx());
321 mlir::Value columnIdx = cgf.emitScalarExpr(e->
getColumnIdx());
322 mlir::Value matrix = Visit(e->
getBase());
325 return builder.createMatrixExtract(loc, matrix, rowIdx, columnIdx);
328 mlir::Value VisitMatrixSingleSubscriptExpr(MatrixSingleSubscriptExpr *e) {
329 mlir::Value rowIdx = cgf.emitScalarExpr(e->
getRowIdx());
331 mlir::Value matrix = Visit(e->
getBase());
333 return builder.createMatrixExtractRow(loc, matrix, rowIdx);
336 mlir::Value VisitCastExpr(
CastExpr *e);
337 mlir::Value VisitCallExpr(
const CallExpr *e);
339 mlir::Value VisitStmtExpr(StmtExpr *e) {
340 CIRGenFunction::StmtExprEvaluation eval(cgf);
348 (void)cgf.emitCompoundStmt(*e->
getSubStmt(), &retAlloca);
350 return cgf.emitLoadOfScalar(cgf.makeAddrLValue(retAlloca, e->
getType()),
354 mlir::Value VisitArraySubscriptExpr(ArraySubscriptExpr *e) {
355 ignoreResultAssign =
false;
361 const mlir::Value vecValue = Visit(e->
getBase());
362 const mlir::Value indexValue = Visit(e->
getIdx());
363 return cir::VecExtractOp::create(cgf.getBuilder(), loc, vecValue,
367 return emitLoadOfLValue(e);
370 mlir::Value VisitShuffleVectorExpr(ShuffleVectorExpr *e) {
373 mlir::Value inputVec = Visit(e->
getExpr(0));
374 mlir::Value indexVec = Visit(e->
getExpr(1));
375 return cir::VecShuffleDynamicOp::create(cgf.getBuilder(),
380 mlir::Value vec1 = Visit(e->
getExpr(0));
381 mlir::Value vec2 = Visit(e->
getExpr(1));
386 SmallVector<mlir::Attribute, 8> indices;
389 cir::IntAttr::get(cgf.getBuilder().getSInt64Ty(),
395 return cir::VecShuffleOp::create(cgf.getBuilder(),
397 cgf.convertType(e->
getType()), vec1, vec2,
398 cgf.getBuilder().getArrayAttr(indices));
401 mlir::Value VisitConvertVectorExpr(ConvertVectorExpr *e) {
404 return emitScalarConversion(Visit(e->
getSrcExpr()),
409 mlir::Value VisitExtVectorElementExpr(Expr *e) {
return emitLoadOfLValue(e); }
411 mlir::Value VisitMatrixElementExpr(Expr *e) {
return emitLoadOfLValue(e); }
413 mlir::Value VisitMemberExpr(MemberExpr *e);
415 mlir::Value VisitCompoundLiteralExpr(CompoundLiteralExpr *e) {
416 return emitLoadOfLValue(e);
419 mlir::Value VisitInitListExpr(InitListExpr *e);
420 mlir::Value emitVectorInitWithSubvectors(mlir::Location loc,
421 cir::VectorType vectorType,
422 ArrayRef<mlir::Value>
inits);
424 mlir::Value VisitArrayInitIndexExpr(ArrayInitIndexExpr *e) {
425 assert(cgf.getArrayInitIndex() &&
426 "ArrayInitIndexExpr not inside an ArrayInitLoopExpr?");
427 return cgf.getArrayInitIndex();
430 mlir::Value VisitImplicitValueInitExpr(
const ImplicitValueInitExpr *e) {
434 mlir::Value VisitExplicitCastExpr(ExplicitCastExpr *e) {
435 return VisitCastExpr(e);
438 mlir::Value VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *e) {
439 return cgf.cgm.emitNullConstant(e->
getType(),
444 mlir::Value emitPointerToBoolConversion(mlir::Value v, QualType qt) {
447 return cgf.getBuilder().createPtrToBoolCast(v);
450 mlir::Value emitFloatToBoolConversion(mlir::Value src, mlir::Location loc) {
451 cir::BoolType boolTy = builder.getBoolTy();
452 return cir::CastOp::create(builder, loc, boolTy,
453 cir::CastKind::float_to_bool, src);
456 mlir::Value emitIntToBoolConversion(mlir::Value srcVal, mlir::Location loc) {
460 if (mlir::isa<cir::BoolType>(srcVal.getType()))
468 cir::BoolType boolTy = builder.getBoolTy();
469 return cir::CastOp::create(builder, loc, boolTy, cir::CastKind::int_to_bool,
475 mlir::Value emitConversionToBool(mlir::Value src, QualType srcType,
476 mlir::Location loc) {
477 assert(srcType.
isCanonical() &&
"EmitScalarConversion strips typedefs");
480 return emitFloatToBoolConversion(src, loc);
482 if (llvm::isa<MemberPointerType>(srcType)) {
483 cgf.getCIRGenModule().errorNYI(loc,
"member pointer to bool conversion");
484 return builder.getFalse(loc);
488 return emitIntToBoolConversion(src, loc);
490 assert(::mlir::isa<cir::PointerType>(src.getType()));
491 return emitPointerToBoolConversion(src, srcType);
496 struct ScalarConversionOpts {
497 bool treatBooleanAsSigned;
498 bool emitImplicitIntegerTruncationChecks;
499 bool emitImplicitIntegerSignChangeChecks;
501 ScalarConversionOpts()
502 : treatBooleanAsSigned(
false),
503 emitImplicitIntegerTruncationChecks(
false),
504 emitImplicitIntegerSignChangeChecks(
false) {}
506 ScalarConversionOpts(clang::SanitizerSet sanOpts)
507 : treatBooleanAsSigned(
false),
508 emitImplicitIntegerTruncationChecks(
509 sanOpts.hasOneOf(SanitizerKind::ImplicitIntegerTruncation)),
510 emitImplicitIntegerSignChangeChecks(
511 sanOpts.
has(SanitizerKind::ImplicitIntegerSignChange)) {}
518 mlir::Value emitScalarCast(mlir::Value src, QualType srcType,
519 QualType dstType, mlir::Type srcTy,
520 mlir::Type dstTy, ScalarConversionOpts opts) {
521 assert(!(mlir::isa<mlir::IntegerType>(srcTy) ||
522 mlir::isa<mlir::IntegerType>(dstTy)) &&
523 "Obsolete code. Don't use mlir::IntegerType with CIR.");
525 mlir::Type fullDstTy = dstTy;
526 if (mlir::isa<cir::MatrixType>(srcTy) &&
527 mlir::isa<cir::MatrixType>(dstTy)) {
529 srcTy = mlir::dyn_cast<cir::MatrixType>(srcTy).getElementType();
530 dstTy = mlir::dyn_cast<cir::MatrixType>(dstTy).getElementType();
531 }
else if (mlir::isa<cir::VectorType>(srcTy) &&
532 mlir::isa<cir::VectorType>(dstTy)) {
534 srcTy = mlir::dyn_cast<cir::VectorType>(srcTy).getElementType();
535 dstTy = mlir::dyn_cast<cir::VectorType>(dstTy).getElementType();
538 std::optional<cir::CastKind> castKind;
542 cir::FenvAttr fenvAttr;
544 if (mlir::isa<cir::BoolType>(srcTy)) {
545 if (opts.treatBooleanAsSigned)
546 cgf.getCIRGenModule().errorNYI(
"signed bool");
547 if (cgf.getBuilder().isInt(dstTy)) {
548 castKind = cir::CastKind::bool_to_int;
549 }
else if (mlir::isa<cir::FPTypeInterface>(dstTy)) {
550 fenvAttr = cgf.getBuilder().getConstrainedFPAttr();
551 castKind = cir::CastKind::bool_to_float;
553 llvm_unreachable(
"Internal error: Cast to unexpected type");
555 }
else if (cgf.getBuilder().isInt(srcTy)) {
556 if (cgf.getBuilder().isInt(dstTy)) {
557 castKind = cir::CastKind::integral;
558 }
else if (mlir::isa<cir::FPTypeInterface>(dstTy)) {
559 fenvAttr = cgf.getBuilder().getConstrainedFPAttr();
560 castKind = cir::CastKind::int_to_float;
561 }
else if (mlir::isa<cir::BoolType>(dstTy)) {
562 castKind = cir::CastKind::int_to_bool;
564 llvm_unreachable(
"Internal error: Cast to unexpected type");
566 }
else if (mlir::isa<cir::FPTypeInterface>(srcTy)) {
567 fenvAttr = cgf.getBuilder().getConstrainedFPAttr();
568 if (cgf.getBuilder().isInt(dstTy)) {
572 if (!cgf.cgm.getCodeGenOpts().StrictFloatCastOverflow)
573 cgf.getCIRGenModule().errorNYI(
"strict float cast overflow");
574 castKind = cir::CastKind::float_to_int;
575 }
else if (mlir::isa<cir::FPTypeInterface>(dstTy)) {
577 return builder.createFloatingCast(src, fullDstTy);
578 }
else if (mlir::isa<cir::BoolType>(dstTy)) {
579 castKind = cir::CastKind::float_to_bool;
581 llvm_unreachable(
"Internal error: Cast to unexpected type");
584 llvm_unreachable(
"Internal error: Cast from unexpected type");
587 assert(castKind.has_value() &&
"Internal error: CastKind not set.");
588 return builder.createOrFold<cir::CastOp>(src.getLoc(), fullDstTy, *castKind,
593 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *e) {
597 mlir::Value VisitVAArgExpr(VAArgExpr *ve) {
602 "variably modified types in varargs");
605 return cgf.emitVAArg(ve);
608 mlir::Value VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *e) {
612 mlir::Value VisitUnaryExprOrTypeTraitExpr(
const UnaryExprOrTypeTraitExpr *e);
614 VisitAbstractConditionalOperator(
const AbstractConditionalOperator *e);
617 mlir::Value VisitUnaryPrePostIncDec(
const UnaryOperator *e) {
619 return emitScalarPrePostIncDec(e, lv);
621 mlir::Value VisitUnaryPostDec(
const UnaryOperator *e) {
622 return VisitUnaryPrePostIncDec(e);
624 mlir::Value VisitUnaryPostInc(
const UnaryOperator *e) {
625 return VisitUnaryPrePostIncDec(e);
627 mlir::Value VisitUnaryPreDec(
const UnaryOperator *e) {
628 return VisitUnaryPrePostIncDec(e);
630 mlir::Value VisitUnaryPreInc(
const UnaryOperator *e) {
631 return VisitUnaryPrePostIncDec(e);
634 mlir::Value emitFixedPointIncDec(
const UnaryOperator *e, mlir::Value value,
636 mlir::Value emitScalarPrePostIncDec(
const UnaryOperator *e, LValue lv) {
644 if (
const AtomicType *atomicTy =
type->getAs<AtomicType>()) {
645 QualType valType = atomicTy->getValueType();
655 cir::IntType intTy = builder.getBoolMemoryTy();
656 mlir::Value one = builder.getConstInt(loc, intTy, 1);
657 Address intAddr = lv.getAddress().withElementType(builder, intTy);
661 cir::StoreOp store = builder.createStore(loc, one, intAddr);
662 store.setMemOrder(cir::MemOrder::SequentiallyConsistent);
663 if (lv.isVolatileQualified())
664 store.setIsVolatile(
true);
665 return builder.getTrue(loc);
668 auto xchg = cir::AtomicXchgOp::create(
670 cir::MemOrder::SequentiallyConsistent, cir::SyncScopeKind::System,
671 lv.isVolatileQualified());
672 return builder.createCast(cir::CastKind::int_to_bool, xchg.getResult(),
673 builder.getBoolTy());
681 cgf.sanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
682 cgf.getLangOpts().getSignedOverflowBehavior() !=
683 LangOptions::SOB_Trapping) {
684 mlir::Type intTy = cgf.convertType(valType);
685 mlir::Value one = builder.getConstInt(loc, intTy, 1);
686 cir::AtomicFetchKind
kind =
687 isInc ? cir::AtomicFetchKind::Add : cir::AtomicFetchKind::Sub;
688 auto rmw = cir::AtomicFetchOp::create(
689 builder, loc, lv.getPointer(), one,
kind,
690 cir::MemOrder::SequentiallyConsistent, cir::SyncScopeKind::System,
691 lv.isVolatileQualified(),
true);
692 mlir::Value oldVal = rmw->getResult(0);
694 return isPre ? emitIntIncOrDec(e, oldVal) : oldVal;
700 mlir::Type fpTy = cgf.convertType(valType);
701 auto fpType = mlir::cast<cir::FPTypeInterface>(fpTy);
703 if (llvm::has_single_bit(fpType.getWidth())) {
704 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
705 mlir::Value amt = builder.getConstFP(
706 loc, fpTy, llvm::APFloat(fpType.getFloatSemantics(), 1));
707 cir::AtomicFetchKind
kind =
708 isInc ? cir::AtomicFetchKind::Add : cir::AtomicFetchKind::Sub;
709 auto rmw = cir::AtomicFetchOp::create(
710 builder, loc, lv.getPointer(), amt,
kind,
711 cir::MemOrder::SequentiallyConsistent, cir::SyncScopeKind::System,
712 lv.isVolatileQualified(),
true);
713 mlir::Value oldVal = rmw->getResult(0);
715 return isInc ? builder.createFAdd(loc, oldVal, amt)
716 : builder.createFSub(loc, oldVal, amt);
720 "Atomic inc/dec of non-power-of-2 float");
726 value = cgf.emitLoadOfLValue(lv, e->
getExprLoc()).getValue();
729 value = cgf.emitLoadOfLValue(lv, e->
getExprLoc()).getValue();
743 value = builder.getTrue(cgf.getLoc(e->
getExprLoc()));
744 }
else if (
type->isIntegerType()) {
745 QualType promotedType;
746 [[maybe_unused]]
bool canPerformLossyDemotionCheck =
false;
747 if (cgf.getContext().isPromotableIntegerType(
type)) {
748 promotedType = cgf.getContext().getPromotedIntegerType(
type);
749 assert(promotedType !=
type &&
"Shouldn't promote to the same type.");
750 canPerformLossyDemotionCheck =
true;
751 canPerformLossyDemotionCheck &=
752 cgf.getContext().getCanonicalType(
type) !=
753 cgf.getContext().getCanonicalType(promotedType);
754 canPerformLossyDemotionCheck &=
761 (!canPerformLossyDemotionCheck ||
762 type->isSignedIntegerOrEnumerationType() ||
764 mlir::cast<cir::IntType>(cgf.convertType(
type)).getWidth() ==
765 mlir::cast<cir::IntType>(cgf.convertType(
type)).getWidth()) &&
766 "The following check expects that if we do promotion to different "
767 "underlying canonical type, at least one of the types (either "
768 "base or promoted) will be signed, or the bitwidths will match.");
773 value = emitIncDecConsiderOverflowBehavior(e, value);
776 value = emitIntIncOrDec(e, input,
false);
778 }
else if (
const PointerType *ptr =
type->getAs<PointerType>()) {
779 QualType
type = ptr->getPointeeType();
780 if (
const VariableArrayType *vla =
781 cgf.getContext().getAsVariableArrayType(
type)) {
783 mlir::Value numElts = cgf.getVLASize(vla).numElts;
785 numElts = cgf.getBuilder().createNeg(loc, numElts,
true);
787 value = cgf.getBuilder().createPtrStride(loc, value, numElts);
792 mlir::Value amt = builder.getSInt32(amount, loc);
794 value = builder.createPtrStride(loc, value, amt);
796 }
else if (
type->isVectorType()) {
797 if (
type->hasIntegerRepresentation()) {
798 value = emitIntIncOrDec(e, input,
false);
800 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
801 value = emitFloatIncOrDec(e, input);
803 }
else if (
type->isRealFloatingType()) {
804 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
805 value = emitFloatIncOrDec(e, input);
806 }
else if (
type->isFixedPointType()) {
807 value = emitFixedPointIncDec(e, value,
type);
809 assert(
type->castAs<ObjCObjectPointerType>());
810 cgf.cgm.errorNYI(e->
getSourceRange(),
"Unary inc/dec ObjectiveC pointer");
814 CIRGenFunction::SourceLocRAIIObject sourceloc{cgf, e->
getSourceRange()};
818 value = cgf.emitStoreThroughBitfieldLValue(
RValue::get(value), lv);
820 cgf.emitStoreThroughLValue(
RValue::get(value), lv);
824 return e->
isPrefix() ? value : input;
827 mlir::Value emitIncDecConsiderOverflowBehavior(
const UnaryOperator *e,
829 switch (cgf.getLangOpts().getSignedOverflowBehavior()) {
830 case LangOptions::SOB_Defined:
831 return emitIntIncOrDec(e, inVal,
false);
832 case LangOptions::SOB_Undefined:
834 return emitIntIncOrDec(e, inVal,
true);
835 case LangOptions::SOB_Trapping:
837 return emitIntIncOrDec(e, inVal,
true);
838 cgf.cgm.errorNYI(e->
getSourceRange(),
"inc/def overflow SOB_Trapping");
841 llvm_unreachable(
"Unexpected signed overflow behavior kind");
844 mlir::Value VisitUnaryAddrOf(
const UnaryOperator *e) {
845 if (llvm::isa<MemberPointerType>(e->
getType()))
846 return cgf.cgm.emitMemberPointerConstant(e);
848 return cgf.emitLValue(e->
getSubExpr()).getPointer();
851 mlir::Value VisitUnaryDeref(
const UnaryOperator *e) {
854 return emitLoadOfLValue(e);
857 mlir::Value VisitUnaryPlus(
const UnaryOperator *e) {
859 mlir::Value result = VisitUnaryPlus(e, promotionType);
860 if (result && !promotionType.
isNull())
861 return emitUnPromotedValue(result, e->
getType());
865 mlir::Value VisitUnaryPlus(
const UnaryOperator *e, QualType promotionType) {
866 ignoreResultAssign =
false;
867 if (!promotionType.
isNull())
868 return cgf.emitPromotedScalarExpr(e->
getSubExpr(), promotionType);
872 mlir::Value VisitUnaryMinus(
const UnaryOperator *e) {
874 mlir::Value result = VisitUnaryMinus(e, promotionType);
875 if (result && !promotionType.
isNull())
876 return emitUnPromotedValue(result, e->
getType());
880 mlir::Value VisitUnaryMinus(
const UnaryOperator *e, QualType promotionType) {
881 ignoreResultAssign =
false;
883 if (!promotionType.
isNull())
884 operand = cgf.emitPromotedScalarExpr(e->
getSubExpr(), promotionType);
890 if (cir::isFPOrVectorOfFPType(operand.getType())) {
891 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
892 return builder.createOrFold<cir::FNegOp>(loc, operand,
893 builder.getFastMathFlagsAttr());
899 CIRGenFixedPointBuilder fpBuilder(builder, loc);
900 return fpBuilder.createNeg(
901 operand, cgf.getContext().getFixedPointSemantics(e->
getType()));
908 cgf.getLangOpts().getSignedOverflowBehavior() !=
909 LangOptions::SOB_Defined;
911 return builder.createOrFold<cir::MinusOp>(loc, operand, nsw);
914 mlir::Value emitIntIncOrDec(
const UnaryOperator *e, mlir::Value input,
918 ? builder.createOrFold<cir::IncOp>(loc, input, nsw)
919 : builder.createOrFold<cir::DecOp>(loc, input, nsw);
922 mlir::Value emitFloatIncOrDec(
const UnaryOperator *e, mlir::Value input) {
923 assert(cir::isFPOrVectorOfFPType(input.getType()) &&
924 "Expect floating-point operand");
927 if (
auto vecType = mlir::dyn_cast<cir::VectorType>(input.getType())) {
928 mlir::Type fpScalarType = vecType.getElementType();
929 auto fpInterface = mlir::cast<cir::FPTypeInterface>(fpScalarType);
930 auto amount = llvm::APFloat::getOne(fpInterface.getFloatSemantics(),
932 mlir::Value amtValue = builder.getConstFP(loc, fpScalarType, amount);
933 amtValue = cir::VecSplatOp::create(builder, loc, vecType, amtValue);
934 return builder.createFAdd(loc, input, amtValue);
939 if (
type->isHalfType() && !cgf.getContext().getLangOpts().NativeHalfType)
940 input = builder.createFloatingCast(input, builder.getSingleTy());
942 auto fpInterface = mlir::cast<cir::FPTypeInterface>(input.getType());
943 auto amount = llvm::APFloat::getOne(fpInterface.getFloatSemantics(),
945 mlir::Value amtValue = builder.getConstFP(loc, input.getType(), amount);
946 mlir::Value output = builder.createFAdd(loc, input, amtValue);
948 if (
type->isHalfType() && !cgf.getContext().getLangOpts().NativeHalfType)
949 output = builder.createFloatingCast(output, builder.getFp16Ty());
954 mlir::Value VisitUnaryNot(
const UnaryOperator *e) {
955 ignoreResultAssign =
false;
957 return builder.createOrFold<cir::NotOp>(
961 mlir::Value VisitUnaryLNot(
const UnaryOperator *e);
963 mlir::Value VisitUnaryReal(
const UnaryOperator *e);
964 mlir::Value VisitUnaryImag(
const UnaryOperator *e);
965 mlir::Value VisitRealImag(
const UnaryOperator *e,
966 QualType promotionType = QualType());
968 mlir::Value VisitUnaryExtension(
const UnaryOperator *e) {
973 mlir::Value VisitMaterializeTemporaryExpr(
const MaterializeTemporaryExpr *e) {
974 return emitLoadOfLValue(e);
976 mlir::Value VisitSourceLocExpr(SourceLocExpr *e) {
977 ASTContext &ctx = cgf.getContext();
980 mlir::Attribute attribute = ConstantEmitter(cgf).emitAbstract(
982 mlir::TypedAttr typedAttr = mlir::cast<mlir::TypedAttr>(attribute);
983 return cir::ConstantOp::create(builder, cgf.getLoc(e->
getExprLoc()),
986 mlir::Value VisitCXXDefaultArgExpr(CXXDefaultArgExpr *dae) {
987 CIRGenFunction::CXXDefaultArgExprScope scope(cgf, dae);
990 mlir::Value VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die) {
991 CIRGenFunction::CXXDefaultInitExprScope scope(cgf, die);
995 mlir::Value VisitCXXThisExpr(CXXThisExpr *te) {
return cgf.loadCXXThis(); }
997 mlir::Value VisitExprWithCleanups(ExprWithCleanups *e);
998 mlir::Value VisitCXXNewExpr(
const CXXNewExpr *e) {
999 return cgf.emitCXXNewExpr(e);
1001 mlir::Value VisitCXXDeleteExpr(
const CXXDeleteExpr *e) {
1002 cgf.emitCXXDeleteExpr(e);
1005 mlir::Value VisitTypeTraitExpr(
const TypeTraitExpr *e) {
1011 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1016 "Expected int type for TypeTraitExpr");
1017 return builder.getConstInt(loc, cgf.convertType(e->
getType()),
1024 VisitConceptSpecializationExpr(
const ConceptSpecializationExpr *e) {
1030 mlir::Value VisitArrayTypeTraitExpr(
const ArrayTypeTraitExpr *e) {
1032 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1035 mlir::Value VisitExpressionTraitExpr(
const ExpressionTraitExpr *e) {
1038 mlir::Value VisitCXXPseudoDestructorExpr(
const CXXPseudoDestructorExpr *e) {
1040 "ScalarExprEmitter: cxx pseudo destructor");
1043 mlir::Value VisitCXXThrowExpr(
const CXXThrowExpr *e) {
1044 cgf.emitCXXThrowExpr(e);
1048 mlir::Value VisitCXXNoexceptExpr(CXXNoexceptExpr *e) {
1052 mlir::Value emitFixedPointConversion(mlir::Value src, QualType srcTy,
1053 QualType dstTy, mlir::Location loc);
1060 emitScalarConversion(mlir::Value src, QualType srcType, QualType dstType,
1062 ScalarConversionOpts opts = ScalarConversionOpts()) {
1070 cir::BoolType boolTy = builder.getBoolTy();
1071 return cir::CastOp::create(builder, cgf.getLoc(loc), boolTy,
1072 cir::CastKind::int_to_bool, src);
1077 return emitFixedPointConversion(src, srcType, dstType, cgf.getLoc(loc));
1079 llvm_unreachable(
"Unhandled scalar conversion from a fixed point type to "
1085 return emitFixedPointConversion(src, srcType, dstType, cgf.getLoc(loc));
1088 llvm_unreachable(
"Unhandled scalar conversion to a fixed point type from "
1094 if (srcType == dstType) {
1095 if (opts.emitImplicitIntegerSignChangeChecks)
1096 cgf.getCIRGenModule().errorNYI(loc,
1097 "implicit integer sign change checks");
1104 mlir::Type mlirSrcType = src.getType();
1109 return emitConversionToBool(src, srcType, cgf.getLoc(loc));
1111 mlir::Type mlirDstType = cgf.convertType(dstType);
1114 !cgf.getContext().getLangOpts().NativeHalfType) {
1116 if (!mlir::isa<cir::FPTypeInterface>(mlirDstType)) {
1120 src = builder.createCast(cgf.getLoc(loc), cir::CastKind::floating, src,
1122 srcType = cgf.getContext().FloatTy;
1123 mlirSrcType = cgf.floatTy;
1129 if (mlirSrcType == mlirDstType) {
1130 if (opts.emitImplicitIntegerSignChangeChecks)
1131 cgf.getCIRGenModule().errorNYI(loc,
1132 "implicit integer sign change checks");
1139 if (
auto dstPT = dyn_cast<cir::PointerType>(mlirDstType)) {
1140 cgf.getCIRGenModule().errorNYI(loc,
"pointer casts");
1141 return builder.getNullPtr(dstPT, src.getLoc());
1147 return builder.createPtrToInt(src, mlirDstType);
1154 assert(dstType->
castAs<ExtVectorType>()->getElementType().getTypePtr() ==
1156 "Splatted expr doesn't match with vector element type?");
1158 cgf.getCIRGenModule().errorNYI(loc,
"vector splatting");
1163 return emitScalarCast(src, srcType, dstType, mlirSrcType, mlirDstType,
1167 "Internal error: conversion between matrix type and scalar type");
1170 mlir::Value res =
nullptr;
1171 mlir::Type resTy = mlirDstType;
1173 res = emitScalarCast(src, srcType, dstType, mlirSrcType, mlirDstType, opts);
1175 if (mlirDstType != resTy) {
1176 res = builder.createCast(cgf.getLoc(loc), cir::CastKind::floating, res,
1180 if (opts.emitImplicitIntegerTruncationChecks)
1181 cgf.getCIRGenModule().errorNYI(loc,
"implicit integer truncation checks");
1183 if (opts.emitImplicitIntegerSignChangeChecks)
1184 cgf.getCIRGenModule().errorNYI(loc,
1185 "implicit integer sign change checks");
1190 BinOpInfo emitBinOps(
const BinaryOperator *e,
1191 QualType promotionType = QualType()) {
1192 ignoreResultAssign =
false;
1194 result.lhs = cgf.emitPromotedScalarExpr(e->
getLHS(), promotionType);
1195 result.rhs = cgf.emitPromotedScalarExpr(e->
getRHS(), promotionType);
1196 if (!promotionType.
isNull())
1197 result.fullType = promotionType;
1199 result.fullType = e->
getType();
1200 result.compType = result.fullType;
1201 if (
const auto *vecType = result.fullType->
getAs<VectorType>())
1202 result.compType = vecType->getElementType();
1210 mlir::Value emitMul(
const BinOpInfo &ops);
1211 mlir::Value emitDiv(
const BinOpInfo &ops);
1212 mlir::Value emitRem(
const BinOpInfo &ops);
1213 mlir::Value emitAdd(
const BinOpInfo &ops);
1214 mlir::Value emitSub(
const BinOpInfo &ops);
1215 mlir::Value emitShl(
const BinOpInfo &ops);
1216 mlir::Value emitShr(
const BinOpInfo &ops);
1217 mlir::Value emitAnd(
const BinOpInfo &ops);
1218 mlir::Value emitXor(
const BinOpInfo &ops);
1219 mlir::Value emitOr(
const BinOpInfo &ops);
1221 mlir::Value emitFixedPointBinOp(
const BinOpInfo &ops);
1223 LValue emitCompoundAssignLValue(
1224 const CompoundAssignOperator *e,
1225 mlir::Value (ScalarExprEmitter::*f)(
const BinOpInfo &),
1226 mlir::Value &result);
1228 emitCompoundAssign(
const CompoundAssignOperator *e,
1229 mlir::Value (ScalarExprEmitter::*f)(
const BinOpInfo &));
1233 QualType getPromotionType(QualType ty) {
1234 const clang::ASTContext &ctx = cgf.getContext();
1235 if (
auto *complexTy = ty->
getAs<ComplexType>()) {
1236 QualType elementTy = complexTy->getElementType();
1242 if (
auto *vt = ty->
getAs<VectorType>()) {
1243 unsigned numElements = vt->getNumElements();
1246 return cgf.getContext().FloatTy;
1253#define HANDLEBINOP(OP) \
1254 mlir::Value VisitBin##OP(const BinaryOperator *e) { \
1255 QualType promotionTy = getPromotionType(e->getType()); \
1256 auto result = emit##OP(emitBinOps(e, promotionTy)); \
1257 if (result && !promotionTy.isNull()) \
1258 result = emitUnPromotedValue(result, e->getType()); \
1261 mlir::Value VisitBin##OP##Assign(const CompoundAssignOperator *e) { \
1262 return emitCompoundAssign(e, &ScalarExprEmitter::emit##OP); \
1280 return cir::CmpOpKind::lt;
1282 return cir::CmpOpKind::gt;
1284 return cir::CmpOpKind::le;
1286 return cir::CmpOpKind::ge;
1288 return cir::CmpOpKind::eq;
1290 return cir::CmpOpKind::ne;
1292 llvm_unreachable(
"unsupported comparison kind for cir.cmp");
1296 mlir::Value emitCmp(
const BinaryOperator *e) {
1297 ignoreResultAssign =
false;
1298 const mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1304 if (lhsTy->
getAs<MemberPointerType>()) {
1307 mlir::Value lhs = cgf.emitScalarExpr(e->
getLHS());
1308 mlir::Value rhs = cgf.emitScalarExpr(e->
getRHS());
1309 result = builder.createCompare(loc,
kind, lhs, rhs);
1311 BinOpInfo boInfo = emitBinOps(e);
1312 mlir::Value lhs = boInfo.lhs;
1313 mlir::Value rhs = boInfo.rhs;
1319 cgf.cgm.errorNYI(loc,
"AltiVec comparison");
1323 result = cir::VecCmpOp::create(builder, cgf.getLoc(boInfo.loc),
1324 cgf.convertType(boInfo.fullType),
kind,
1325 boInfo.lhs, boInfo.rhs);
1327 }
else if (boInfo.isFixedPointOp()) {
1328 result = emitFixedPointBinOp(boInfo);
1331 if (cgf.cgm.getCodeGenOpts().StrictVTablePointers &&
1332 mlir::isa<cir::PointerType>(lhs.getType()) &&
1333 mlir::isa<cir::PointerType>(rhs.getType())) {
1334 cgf.cgm.errorNYI(loc,
"strict vtable pointer comparisons");
1336 result = builder.createCompare(loc,
kind, lhs, rhs);
1340 "Complex Comparison: can only be an equality comparison");
1344 lhs = cgf.emitComplexExpr(e->
getLHS());
1346 mlir::Value lhsReal = Visit(e->
getLHS());
1347 mlir::Value lhsImag = builder.getNullValue(convertType(lhsTy), loc);
1348 lhs = builder.createComplexCreate(loc, lhsReal, lhsImag);
1353 rhs = cgf.emitComplexExpr(e->
getRHS());
1355 mlir::Value rhsReal = Visit(e->
getRHS());
1356 mlir::Value rhsImag = builder.getNullValue(convertType(rhsTy), loc);
1357 rhs = builder.createComplexCreate(loc, rhsReal, rhsImag);
1360 result = builder.createCompare(loc,
kind, lhs, rhs);
1363 return emitScalarConversion(result, cgf.getContext().BoolTy, e->
getType(),
1368#define VISITCOMP(CODE) \
1369 mlir::Value VisitBin##CODE(const BinaryOperator *E) { return emitCmp(E); }
1379 const bool ignore = std::exchange(ignoreResultAssign,
false);
1394 rhs = Visit(e->
getRHS());
1404 if (lhs.isBitField()) {
1424 if (!lhs.isVolatile())
1428 return emitLoadOfLValue(lhs, e->
getExprLoc());
1431 mlir::Value VisitBinComma(
const BinaryOperator *e) {
1432 cgf.emitIgnoredExpr(e->
getLHS());
1434 return Visit(e->
getRHS());
1437 mlir::Value VisitBinLAnd(
const clang::BinaryOperator *e) {
1439 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1441 mlir::Value zeroVec = builder.getNullValue(lhsTy, loc);
1443 mlir::Value lhs = Visit(e->
getLHS());
1444 mlir::Value rhs = Visit(e->
getRHS());
1447 mlir::cast<cir::VectorType>(cgf.convertType(e->
getType()));
1448 if (cir::isFPOrVectorOfFPType(lhs.getType())) {
1449 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
1450 auto cmpOpKind = cir::FCmpOpKind::une;
1451 lhs = builder.createVecFCompare(loc, resultTy, cmpOpKind, lhs, zeroVec);
1452 rhs = builder.createVecFCompare(loc, resultTy, cmpOpKind, rhs, zeroVec);
1454 auto cmpOpKind = cir::CmpOpKind::ne;
1455 lhs = builder.createVecCompare(loc, resultTy, cmpOpKind, lhs, zeroVec);
1456 rhs = builder.createVecCompare(loc, resultTy, cmpOpKind, rhs, zeroVec);
1458 return builder.createAnd(loc, lhs, rhs);
1462 mlir::Type resTy = cgf.convertType(e->
getType());
1463 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1465 mlir::Value lhsCondV = cgf.evaluateExprAsBool(e->
getLHS());
1467 CIRGenFunction::ConditionalEvaluation eval(cgf, loc);
1469 auto resOp = cir::TernaryOp::create(
1470 builder, loc, lhsCondV,
1471 [&](mlir::OpBuilder &b, mlir::Location loc) {
1472 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1473 b.getInsertionBlock()};
1474 cgf.curLexScope->setAsTernary();
1475 eval.beginEvaluation();
1476 mlir::Value res = cgf.evaluateExprAsBool(e->
getRHS());
1477 eval.endEvaluation();
1479 cir::YieldOp::create(b, loc, res);
1482 [&](mlir::OpBuilder &b, mlir::Location loc) {
1483 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1484 b.getInsertionBlock()};
1486 auto res = cir::ConstantOp::create(b, loc, builder.getFalseAttr());
1487 cir::YieldOp::create(b, loc, res.getRes());
1489 return maybePromoteBoolResult(resOp.getResult(), resTy);
1492 mlir::Value VisitBinLOr(
const clang::BinaryOperator *e) {
1494 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1496 mlir::Value zeroVec = builder.getNullValue(lhsTy, loc);
1498 mlir::Value lhs = Visit(e->
getLHS());
1499 mlir::Value rhs = Visit(e->
getRHS());
1502 mlir::cast<cir::VectorType>(cgf.convertType(e->
getType()));
1503 if (cir::isFPOrVectorOfFPType(lhs.getType())) {
1504 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
1505 auto cmpOpKind = cir::FCmpOpKind::une;
1506 lhs = builder.createVecFCompare(loc, resultTy, cmpOpKind, lhs, zeroVec);
1507 rhs = builder.createVecFCompare(loc, resultTy, cmpOpKind, rhs, zeroVec);
1509 auto cmpOpKind = cir::CmpOpKind::ne;
1510 lhs = builder.createVecCompare(loc, resultTy, cmpOpKind, lhs, zeroVec);
1511 rhs = builder.createVecCompare(loc, resultTy, cmpOpKind, rhs, zeroVec);
1513 return builder.createOr(loc, lhs, rhs);
1517 mlir::Type resTy = cgf.convertType(e->
getType());
1518 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1520 mlir::Value lhsCondV = cgf.evaluateExprAsBool(e->
getLHS());
1522 CIRGenFunction::ConditionalEvaluation eval(cgf, loc);
1524 auto resOp = cir::TernaryOp::create(
1525 builder, loc, lhsCondV,
1526 [&](mlir::OpBuilder &b, mlir::Location loc) {
1527 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1528 b.getInsertionBlock()};
1530 auto res = cir::ConstantOp::create(b, loc, builder.getTrueAttr());
1531 cir::YieldOp::create(b, loc, res.getRes());
1534 [&](mlir::OpBuilder &b, mlir::Location loc) {
1535 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1536 b.getInsertionBlock()};
1538 eval.beginEvaluation();
1539 mlir::Value res = cgf.evaluateExprAsBool(e->
getRHS());
1540 eval.endEvaluation();
1542 cir::YieldOp::create(b, loc, res);
1545 return maybePromoteBoolResult(resOp.getResult(), resTy);
1548 mlir::Value VisitBinPtrMemD(
const BinaryOperator *e) {
1549 return emitLoadOfLValue(e);
1552 mlir::Value VisitBinPtrMemI(
const BinaryOperator *e) {
1553 return emitLoadOfLValue(e);
1557 mlir::Value VisitBlockExpr(
const BlockExpr *e) {
1558 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: block");
1562 mlir::Value VisitChooseExpr(ChooseExpr *e) {
1566 mlir::Value VisitObjCStringLiteral(
const ObjCStringLiteral *e) {
1568 "ScalarExprEmitter: objc string literal");
1571 mlir::Value VisitObjCBoxedExpr(ObjCBoxedExpr *e) {
1572 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: objc boxed");
1575 mlir::Value VisitObjCArrayLiteral(ObjCArrayLiteral *e) {
1577 "ScalarExprEmitter: objc array literal");
1580 mlir::Value VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *e) {
1582 "ScalarExprEmitter: objc dictionary literal");
1586 mlir::Value convertVec3AndVec4(CIRGenBuilderTy &builder, mlir::Location loc,
1587 mlir::Value src,
unsigned numElementsDst) {
1588 static constexpr int64_t mask[] = {0, 1, 2, -1};
1589 return builder.createVecShuffle(
1590 loc, src, llvm::ArrayRef<int64_t>(mask, numElementsDst));
1612 mlir::Type srcTy = src.getType();
1616 return builder.createBitcast(src, dstTy);
1621 "ScalarExprEmitter: createCastsForTypeOfSameSize Case 2");
1629 "ScalarExprEmitter: createCastsForTypeOfSameSize Case 3a");
1633 "ScalarExprEmitter: createCastsForTypeOfSameSize Case 3a and 3b");
1640 "ScalarExprEmitter: createCastsForTypeOfSameSize Case 4a");
1644 return builder.createIntToPtr(src, dstTy);
1647 mlir::Value VisitAsTypeExpr(AsTypeExpr *e) {
1648 mlir::Value src = cgf.emitScalarExpr(e->
getSrcExpr());
1649 mlir::Type srcTy = src.getType();
1650 mlir::Type dstTy = cgf.convertType(e->
getType());
1662 "ScalarExprEmitter: VisitAsTypeExpr ExtVectorBoolType");
1668 if (numElementsSrc == 3 && numElementsDst != 3) {
1670 "ScalarExprEmitter: VisitAsTypeExpr numElemsSrc = 3, "
1671 "numElemsDst != 3");
1678 if (numElementsSrc != 3 && numElementsDst == 3) {
1679 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1681 auto dstVec4Ty = cir::VectorType::getFixed(dstElemTy, 4);
1683 src = convertVec3AndVec4(builder, loc, src, 3);
1690 mlir::Value VisitAtomicExpr(AtomicExpr *e) {
1691 return cgf.emitAtomicExpr(e).getValue();
1695LValue ScalarExprEmitter::emitCompoundAssignLValue(
1697 mlir::Value (ScalarExprEmitter::*func)(
const BinOpInfo &),
1698 mlir::Value &result) {
1709 if (promotionTypeCR.
isNull())
1713 QualType promotionTypeRHS = getPromotionType(e->
getRHS()->
getType());
1715 if (!promotionTypeRHS.
isNull())
1718 opInfo.rhs = Visit(e->
getRHS());
1720 opInfo.fullType = promotionTypeCR;
1721 opInfo.compType = opInfo.fullType;
1722 if (
const auto *vecType = opInfo.fullType->
getAs<VectorType>())
1723 opInfo.compType = vecType->getElementType();
1732 if (lhsTy->
getAs<AtomicType>()) {
1733 cgf.
cgm.
errorNYI(result.getLoc(),
"atomic lvalue assign");
1737 opInfo.lhs = emitLoadOfLValue(lhsLV, e->
getExprLoc());
1739 CIRGenFunction::SourceLocRAIIObject sourceloc{cgf, e->
getSourceRange()};
1741 if (!promotionTypeLHS.
isNull())
1742 opInfo.lhs = emitScalarConversion(opInfo.lhs, lhsTy, promotionTypeLHS, loc);
1744 opInfo.lhs = emitScalarConversion(opInfo.lhs, lhsTy,
1748 result = (this->*func)(opInfo);
1752 result = emitScalarConversion(result, promotionTypeCR, lhsTy, loc,
1753 ScalarConversionOpts(cgf.
sanOpts));
1759 if (lhsLV.isBitField())
1769mlir::Value ScalarExprEmitter::emitComplexToScalarConversion(mlir::Location lov,
1773 cir::CastKind castOpKind;
1775 case CK_FloatingComplexToReal:
1776 castOpKind = cir::CastKind::float_complex_to_real;
1778 case CK_IntegralComplexToReal:
1779 castOpKind = cir::CastKind::int_complex_to_real;
1781 case CK_FloatingComplexToBoolean:
1782 castOpKind = cir::CastKind::float_complex_to_bool;
1784 case CK_IntegralComplexToBoolean:
1785 castOpKind = cir::CastKind::int_complex_to_bool;
1788 llvm_unreachable(
"invalid complex-to-scalar cast kind");
1794mlir::Value ScalarExprEmitter::emitPromoted(
const Expr *e,
1795 QualType promotionType) {
1797 if (
const auto *bo = dyn_cast<BinaryOperator>(e)) {
1798 switch (bo->getOpcode()) {
1799#define HANDLE_BINOP(OP) \
1801 return emit##OP(emitBinOps(bo, promotionType));
1810 }
else if (
const auto *uo = dyn_cast<UnaryOperator>(e)) {
1811 switch (uo->getOpcode()) {
1814 return VisitRealImag(uo, promotionType);
1816 return VisitUnaryMinus(uo, promotionType);
1818 return VisitUnaryPlus(uo, promotionType);
1823 mlir::Value result = Visit(
const_cast<Expr *
>(e));
1825 if (!promotionType.
isNull())
1826 return emitPromotedValue(result, promotionType);
1827 return emitUnPromotedValue(result, e->
getType());
1832mlir::Value ScalarExprEmitter::emitCompoundAssign(
1833 const CompoundAssignOperator *e,
1834 mlir::Value (ScalarExprEmitter::*func)(
const BinOpInfo &)) {
1836 bool ignore = std::exchange(ignoreResultAssign,
false);
1838 LValue lhs = emitCompoundAssignLValue(e, func, rhs);
1849 if (!lhs.isVolatile())
1853 return emitLoadOfLValue(lhs, e->
getExprLoc());
1857ScalarExprEmitter::VisitSYCLUniqueStableNameExpr(SYCLUniqueStableNameExpr *e) {
1860 cir::GlobalOp globalConstStr =
1866 auto exprTy = mlir::cast<cir::PointerType>(convertType(e->
getType()));
1867 auto addrTy = mlir::cast<cir::PointerType>(addr.getType());
1868 if (addrTy.getAddrSpace() != exprTy.getAddrSpace())
1870 addr, builder.
getPointerTo(addrTy.getPointee(), exprTy.getAddrSpace()));
1874mlir::Value ScalarExprEmitter::VisitExprWithCleanups(ExprWithCleanups *e) {
1875 CIRGenFunction::FullExprCleanupScope scope(cgf, e->
getSubExpr());
1887 ScalarExprEmitter emitter(*
this, builder);
1890#define COMPOUND_OP(Op) \
1891 case BO_##Op##Assign: \
1892 return emitter.emitCompoundAssignLValue(e, &ScalarExprEmitter::emit##Op, \
1929 llvm_unreachable(
"Not valid compound assignment operators");
1931 llvm_unreachable(
"Unhandled compound assignment operator");
1936 bool ignoreResultAssign) {
1938 "Invalid scalar expression to emit");
1940 return ScalarExprEmitter(*
this, builder, ignoreResultAssign)
1941 .Visit(
const_cast<Expr *
>(e));
1946 if (!promotionType.
isNull())
1947 return ScalarExprEmitter(*
this, builder).emitPromoted(e, promotionType);
1948 return ScalarExprEmitter(*
this, builder).Visit(
const_cast<Expr *
>(e));
1961static std::optional<QualType>
1965 return std::nullopt;
1970 return std::nullopt;
1983 const BinOpInfo &op) {
1985 "Expected a unary or binary operator");
1989 if (!op.mayHaveIntegerOverflow())
1993 if (
const auto *uo = dyn_cast<UnaryOperator>(op.e))
1994 return !uo->canOverflow();
1999 std::optional<QualType> optionalLHSTy =
2004 std::optional<QualType> optionalRHSTy =
2014 if ((op.opcode != BO_Mul && op.opcode != BO_MulAssign) ||
2021 return (2 * astContext.
getTypeSize(lhsTy)) < promotedSize ||
2022 (2 * astContext.
getTypeSize(rhsTy)) < promotedSize;
2027 const BinOpInfo &op,
2028 bool isSubtraction) {
2033 mlir::Value pointer = op.lhs;
2034 Expr *pointerOperand =
expr->getLHS();
2035 mlir::Value
index = op.rhs;
2036 Expr *indexOperand =
expr->getRHS();
2042 if (!isSubtraction && !mlir::isa<cir::PointerType>(pointer.getType())) {
2043 std::swap(pointer,
index);
2044 std::swap(pointerOperand, indexOperand);
2046 assert(mlir::isa<cir::PointerType>(pointer.getType()) &&
2047 "Need a pointer operand");
2048 assert(mlir::isa<cir::IntType>(
index.getType()) &&
"Need an integer operand");
2083 cgf.
cgm.
errorNYI(
"Objective-C:pointer arithmetic with non-pointer type");
2093 numElements.getType());
2097 cir::OverflowFlags overflowFlags = cgf.
getLangOpts().PointerOverflowDefined
2098 ? cir::OverflowFlags::None
2099 : cir::OverflowFlags::NoSignedWrap;
2102 return cir::PtrStrideOp::create(cgf.
getBuilder(), loc, pointer.getType(),
2107 return cir::PtrStrideOp::create(cgf.
getBuilder(),
2109 pointer.getType(), pointer,
index);
2113 auto vecTy = mlir::dyn_cast<cir::VectorType>(ty);
2114 return vecTy && mlir::isa<cir::IntType>(vecTy.getElementType());
2124 bool negMul,
bool negAdd) {
2125 mlir::Location loc = builder.getFusedLoc({mulOp.getLoc(), addLoc});
2126 mlir::Value mulOp0 = mulOp.getLhs();
2127 mlir::Value mulOp1 = mulOp.getRhs();
2135 mlir::Value fmuladd = cir::FMulAddOp::create(
2136 builder, loc, addend.getType(), mulOp0, mulOp1, addend,
2150 bool isSub =
false) {
2151 assert((op.opcode == BO_Add || op.opcode == BO_AddAssign ||
2152 op.opcode == BO_Sub || op.opcode == BO_SubAssign) &&
2153 "Only fadd/fsub can be the root of an fmuladd.");
2160 mlir::Value lhs = op.lhs;
2161 mlir::Value rhs = op.rhs;
2165 bool negLHS =
false;
2166 if (
auto lhsNeg = lhs.getDefiningOp<cir::FNegOp>()) {
2167 if (lhsNeg.getResult().use_empty() && lhsNeg.getInput().hasOneUse()) {
2168 lhs = lhsNeg.getInput();
2173 bool negRHS =
false;
2174 if (
auto rhsNeg = rhs.getDefiningOp<cir::FNegOp>()) {
2175 if (rhsNeg.getResult().use_empty() && rhsNeg.getInput().hasOneUse()) {
2176 rhs = rhsNeg.getInput();
2184 if (
auto lhsMul = lhs.getDefiningOp<cir::FMulOp>()) {
2185 if (lhsMul.getResult().use_empty() || negLHS) {
2188 op.lhs.getDefiningOp<cir::FNegOp>().erase();
2189 return buildFMulAdd(loc, lhsMul, op.rhs, builder, negLHS, isSub);
2192 if (
auto rhsMul = rhs.getDefiningOp<cir::FMulOp>()) {
2193 if (rhsMul.getResult().use_empty() || negRHS) {
2196 op.rhs.getDefiningOp<cir::FNegOp>().erase();
2197 return buildFMulAdd(loc, rhsMul, op.lhs, builder, isSub ^ negRHS,
false);
2204mlir::Value ScalarExprEmitter::emitFixedPointIncDec(
const UnaryOperator *e,
2223 if (
type->isSignedFixedPointType()) {
2230 CIRGenFixedPointBuilder fpbuilder(builder, cgf.
getLoc(
info.loc));
2233 fpbuilder.createIntegerToFixed(
info.rhs,
true, dstSema);
2234 return emitFixedPointBinOp(info);
2237mlir::Value ScalarExprEmitter::emitFixedPointConversion(mlir::Value src,
2240 mlir::Location loc) {
2243 CIRGenFixedPointBuilder fpBuilder(builder, loc);
2247 result = fpBuilder.createFloatingToFixed(
2251 result = fpBuilder.createFixedToFloating(
2255 llvm::FixedPointSemantics srcFPSema =
2257 llvm::FixedPointSemantics dstFPSema =
2261 result = fpBuilder.createFixedToInteger(
2262 src, srcFPSema, dstFPSema.getWidth(), dstFPSema.isSigned());
2265 fpBuilder.createIntegerToFixed(src, srcFPSema.isSigned(), dstFPSema);
2268 result = fpBuilder.createFixedToFixed(src, srcFPSema, dstFPSema);
2274mlir::Value ScalarExprEmitter::emitMul(
const BinOpInfo &ops) {
2275 const mlir::Location loc = cgf.
getLoc(ops.loc);
2278 switch (cgf.
getLangOpts().getSignedOverflowBehavior()) {
2279 case LangOptions::SOB_Defined:
2280 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2281 return builder.
createMul(loc, ops.lhs, ops.rhs);
2283 case LangOptions::SOB_Undefined:
2284 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2287 case LangOptions::SOB_Trapping:
2295 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, ops.fpFeatures);
2303 if (cir::isFPOrVectorOrMatrixOfFPType(lhs.getType()))
2305 return builder.
createMul(loc, lhs, rhs);
2309 cgf.
sanOpts.
has(SanitizerKind::UnsignedIntegerOverflow) &&
2311 cgf.
cgm.
errorNYI(
"unsigned int overflow sanitizer");
2313 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
2314 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ops.fpFeatures);
2315 return builder.
createFMul(loc, ops.lhs, ops.rhs);
2318 if (ops.isFixedPointOp())
2319 return emitFixedPointBinOp(ops);
2321 return cir::MulOp::create(builder, cgf.
getLoc(ops.loc),
2324mlir::Value ScalarExprEmitter::emitDiv(
const BinOpInfo &ops) {
2325 const mlir::Location loc = cgf.
getLoc(ops.loc);
2326 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
2327 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ops.fpFeatures);
2328 return builder.
createFDiv(loc, ops.lhs, ops.rhs);
2332 assert(mlir::isa<cir::MatrixType>(ops.lhs.getType()) &&
2333 "first operand must be a matrix");
2334 assert(cir::isAnyIntegerOrFloatingPointType(ops.rhs.getType()) &&
2335 "second operand must be an arithmetic type");
2340 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, ops.fpFeatures);
2341 if (cir::isFPOrVectorOrMatrixOfFPType(lhs.getType()))
2343 return builder.
createDiv(loc, lhs, rhs);
2346 if (ops.isFixedPointOp())
2347 return emitFixedPointBinOp(ops);
2349 return cir::DivOp::create(builder, loc, cgf.
convertType(ops.fullType),
2352mlir::Value ScalarExprEmitter::emitRem(
const BinOpInfo &ops) {
2353 const mlir::Location loc = cgf.
getLoc(ops.loc);
2354 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
2355 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ops.fpFeatures);
2356 return builder.
createFRem(loc, ops.lhs, ops.rhs);
2358 return cir::RemOp::create(builder, loc, cgf.
convertType(ops.fullType),
2362mlir::Value ScalarExprEmitter::emitFixedPointBinOp(
const BinOpInfo &ops) {
2367 QualType resultTy = ops.compType;
2368 QualType lhsTy, rhsTy;
2369 if (
const auto *binOp = dyn_cast<BinaryOperator>(ops.e)) {
2370 rhsTy = binOp->getRHS()->getType();
2371 if (
const auto *cao = dyn_cast<CompoundAssignOperator>(binOp)) {
2376 lhsTy = cao->getComputationLHSType();
2377 resultTy = cao->getComputationResultType();
2379 lhsTy = binOp->getLHS()->getType();
2381 }
else if (
const auto *unOp = dyn_cast<UnaryOperator>(ops.e)) {
2382 lhsTy = unOp->getSubExpr()->getType();
2383 rhsTy = unOp->getSubExpr()->getType();
2386 mlir::Value lhs = ops.lhs;
2387 mlir::Value rhs = ops.rhs;
2392 auto commonFixedSema = lhsFixedSema.getCommonSemantics(rhsFixedSema);
2396 CIRGenFixedPointBuilder fpbuilder(builder, cgf.
getLoc(ops.loc));
2397 switch (ops.opcode) {
2400 result = fpbuilder.createAdd(lhs, lhsFixedSema, rhs, rhsFixedSema);
2404 result = fpbuilder.createSub(lhs, lhsFixedSema, rhs, rhsFixedSema);
2408 result = fpbuilder.createMul(lhs, lhsFixedSema, rhs, rhsFixedSema);
2412 result = fpbuilder.createDiv(lhs, lhsFixedSema, rhs, rhsFixedSema);
2416 result = fpbuilder.createShl(lhs, lhsFixedSema, rhs);
2420 result = fpbuilder.createShr(lhs, rhs);
2428 return fpbuilder.createCmp(lhs, lhsFixedSema, rhs, rhsFixedSema,
2429 clangCmpToCIRCmp(ops.opcode));
2433 llvm_unreachable(
"Found unimplemented fixed point binary operation");
2447 "Found unsupported binary operation for fixed point types.");
2453 return fpbuilder.createFixedToFixed(
2454 result, isShift ? lhsFixedSema : commonFixedSema, resultFixedSema);
2457mlir::Value ScalarExprEmitter::emitAdd(
const BinOpInfo &ops) {
2458 if (mlir::isa<cir::PointerType>(ops.lhs.getType()) ||
2459 mlir::isa<cir::PointerType>(ops.rhs.getType()))
2463 const mlir::Location loc = cgf.
getLoc(ops.loc);
2466 switch (cgf.
getLangOpts().getSignedOverflowBehavior()) {
2467 case LangOptions::SOB_Defined:
2468 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2469 return builder.
createAdd(loc, ops.lhs, ops.rhs);
2471 case LangOptions::SOB_Undefined:
2472 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2475 case LangOptions::SOB_Trapping:
2487 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, ops.fpFeatures);
2488 if (cir::isFPOrVectorOrMatrixOfFPType(lhs.getType()))
2490 return builder.
createAdd(loc, lhs, rhs);
2494 cgf.
sanOpts.
has(SanitizerKind::UnsignedIntegerOverflow) &&
2496 cgf.
cgm.
errorNYI(
"unsigned int overflow sanitizer");
2498 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
2499 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ops.fpFeatures);
2503 return builder.
createFAdd(loc, ops.lhs, ops.rhs);
2506 if (ops.isFixedPointOp())
2507 return emitFixedPointBinOp(ops);
2509 return builder.
createAdd(loc, ops.lhs, ops.rhs);
2512mlir::Value ScalarExprEmitter::emitSub(
const BinOpInfo &ops) {
2513 const mlir::Location loc = cgf.
getLoc(ops.loc);
2515 if (!mlir::isa<cir::PointerType>(ops.lhs.getType())) {
2518 switch (cgf.
getLangOpts().getSignedOverflowBehavior()) {
2519 case LangOptions::SOB_Defined: {
2520 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2521 return builder.
createSub(loc, ops.lhs, ops.rhs);
2524 case LangOptions::SOB_Undefined:
2525 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2528 case LangOptions::SOB_Trapping:
2541 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, ops.fpFeatures);
2542 if (cir::isFPOrVectorOrMatrixOfFPType(lhs.getType()))
2544 return builder.
createSub(loc, lhs, rhs);
2548 cgf.
sanOpts.
has(SanitizerKind::UnsignedIntegerOverflow) &&
2550 cgf.
cgm.
errorNYI(
"unsigned int overflow sanitizer");
2552 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
2553 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ops.fpFeatures);
2555 if (mlir::Value fmuladd =
2558 return builder.
createFSub(loc, ops.lhs, ops.rhs);
2561 if (ops.isFixedPointOp())
2562 return emitFixedPointBinOp(ops);
2564 return builder.
createSub(loc, ops.lhs, ops.rhs);
2569 if (!mlir::isa<cir::PointerType>(ops.rhs.getType()))
2581 return cir::PtrDiffOp::create(builder, cgf.
getLoc(ops.loc), cgf.
ptrDiffTy,
2585mlir::Value ScalarExprEmitter::emitShl(
const BinOpInfo &ops) {
2587 if (ops.isFixedPointOp())
2588 return emitFixedPointBinOp(ops);
2594 bool sanitizeSignedBase = cgf.
sanOpts.
has(SanitizerKind::ShiftBase) &&
2598 bool sanitizeUnsignedBase =
2599 cgf.
sanOpts.
has(SanitizerKind::UnsignedShiftBase) &&
2601 bool sanitizeBase = sanitizeSignedBase || sanitizeUnsignedBase;
2602 bool sanitizeExponent = cgf.
sanOpts.
has(SanitizerKind::ShiftExponent);
2607 else if ((sanitizeBase || sanitizeExponent) &&
2608 mlir::isa<cir::IntType>(ops.lhs.getType()))
2614mlir::Value ScalarExprEmitter::emitShr(
const BinOpInfo &ops) {
2616 if (ops.isFixedPointOp())
2617 return emitFixedPointBinOp(ops);
2626 else if (cgf.
sanOpts.
has(SanitizerKind::ShiftExponent) &&
2627 mlir::isa<cir::IntType>(ops.lhs.getType()))
2635mlir::Value ScalarExprEmitter::emitAnd(
const BinOpInfo &ops) {
2636 return cir::AndOp::create(builder, cgf.
getLoc(ops.loc), ops.lhs, ops.rhs);
2638mlir::Value ScalarExprEmitter::emitXor(
const BinOpInfo &ops) {
2639 return cir::XorOp::create(builder, cgf.
getLoc(ops.loc), ops.lhs, ops.rhs);
2641mlir::Value ScalarExprEmitter::emitOr(
const BinOpInfo &ops) {
2642 return cir::OrOp::create(builder, cgf.
getLoc(ops.loc), ops.lhs, ops.rhs);
2649mlir::Value ScalarExprEmitter::VisitCastExpr(
CastExpr *ce) {
2651 QualType destTy = ce->
getType();
2653 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ce);
2657 ignoreResultAssign =
false;
2660 case clang::CK_Dependent:
2661 llvm_unreachable(
"dependent cast kind in CIR gen!");
2662 case clang::CK_BuiltinFnToFnPtr:
2663 llvm_unreachable(
"builtin functions are handled elsewhere");
2664 case CK_LValueBitCast:
2665 case CK_LValueToRValueBitCast: {
2667 Address sourceAddr = sourceLVal.getAddress();
2673 return emitLoadOfLValue(destLVal, ce->
getExprLoc());
2676 case CK_CPointerToObjCPointerCast:
2677 case CK_BlockPointerToObjCPointerCast:
2678 case CK_AnyPointerToBlockPointerCast:
2680 mlir::Value src = Visit(
const_cast<Expr *
>(subExpr));
2684 auto srcPtrTy = mlir::dyn_cast<cir::PointerType>(src.getType());
2685 auto dstPtrTy = mlir::dyn_cast<cir::PointerType>(dstTy);
2686 if (srcPtrTy && dstPtrTy &&
2687 srcPtrTy.getAddrSpace() != dstPtrTy.getAddrSpace())
2690 builder.
getPointerTo(srcPtrTy.getPointee(), dstPtrTy.getAddrSpace()));
2692 if (cgf.
sanOpts.
has(SanitizerKind::CFIUnrelatedCast))
2694 "sanitizer support");
2698 "strict vtable pointers");
2724 case CK_AddressSpaceConversion: {
2725 Expr::EvalResult result;
2734 mlir::cast<cir::PointerType>(convertType(destTy)), destTy,
2740 case CK_AtomicToNonAtomic:
2741 case CK_NonAtomicToAtomic:
2742 case CK_UserDefinedConversion:
2743 return Visit(
const_cast<Expr *
>(subExpr));
2747 case CK_IntegralToPointer: {
2749 mlir::Value src = Visit(
const_cast<Expr *
>(subExpr));
2758 : cir::CastKind::integral,
2763 "IntegralToPointer: strict vtable pointers");
2770 case CK_BaseToDerived: {
2772 assert(derivedClassDecl &&
"BaseToDerived arg isn't a C++ object pointer!");
2784 case CK_UncheckedDerivedToBase:
2785 case CK_DerivedToBase: {
2796 case CK_ArrayToPointerDecay:
2799 case CK_NullToPointer: {
2805 auto ty = mlir::cast<cir::PointerType>(cgf.
convertType(destTy));
2810 case CK_NullToMemberPointer: {
2816 const MemberPointerType *mpt = ce->
getType()->
getAs<MemberPointerType>();
2822 case CK_ReinterpretMemberPointer: {
2823 mlir::Value src = Visit(subExpr);
2827 case CK_BaseToDerivedMemberPointer:
2828 case CK_DerivedToBaseMemberPointer: {
2829 mlir::Value src = Visit(subExpr);
2833 QualType derivedTy =
2834 kind == CK_DerivedToBaseMemberPointer ? subExpr->
getType() : destTy;
2835 const auto *mpType = derivedTy->
castAs<MemberPointerType>();
2836 NestedNameSpecifier qualifier = mpType->getQualifier();
2837 assert(qualifier &&
"member pointer without class qualifier");
2838 const Type *qualifierType = qualifier.getAsType();
2839 assert(qualifierType &&
"member pointer qualifier is not a type");
2846 mlir::IntegerAttr offsetAttr = builder.getIndexAttr(offset.
getQuantity());
2849 if (
kind == CK_BaseToDerivedMemberPointer)
2850 return cir::DerivedMethodOp::create(builder, loc, resultTy, src,
2852 return cir::BaseMethodOp::create(builder, loc, resultTy, src, offsetAttr);
2855 if (
kind == CK_BaseToDerivedMemberPointer)
2856 return cir::DerivedDataMemberOp::create(builder, loc, resultTy, src,
2858 return cir::BaseDataMemberOp::create(builder, loc, resultTy, src,
2862 case CK_LValueToRValue:
2864 assert(subExpr->
isGLValue() &&
"lvalue-to-rvalue applied to r-value!");
2865 return Visit(
const_cast<Expr *
>(subExpr));
2867 case CK_IntegralCast: {
2868 ScalarConversionOpts opts;
2869 if (
auto *ice = dyn_cast<ImplicitCastExpr>(ce)) {
2870 if (!ice->isPartOfExplicitCast())
2871 opts = ScalarConversionOpts(cgf.
sanOpts);
2873 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2877 case CK_FloatingComplexToReal:
2878 case CK_IntegralComplexToReal:
2879 case CK_FloatingComplexToBoolean:
2880 case CK_IntegralComplexToBoolean: {
2886 case CK_FloatingRealToComplex:
2887 case CK_FloatingComplexCast:
2888 case CK_IntegralRealToComplex:
2889 case CK_IntegralComplexCast:
2890 case CK_IntegralComplexToFloatingComplex:
2891 case CK_FloatingComplexToIntegralComplex:
2892 llvm_unreachable(
"scalar cast to non-scalar value");
2894 case CK_PointerToIntegral: {
2895 assert(!destTy->
isBooleanType() &&
"bool should use PointerToBool");
2898 "strict vtable pointers");
2905 case CK_FixedPointCast:
2906 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2909 case CK_FixedPointToBoolean:
2911 "Expected src type to be fixed point type");
2912 assert(destTy->
isBooleanType() &&
"Expected dest type to be boolean type");
2913 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2916 case CK_FixedPointToIntegral:
2918 "Expected src type to be fixed point type");
2919 assert(destTy->
isIntegerType() &&
"Expected dest type to be an integer");
2920 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2923 case CK_IntegralToFixedPoint:
2925 "Expected src type to be an integer");
2927 "Expected dest type to be fixed point type");
2928 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2931 case CK_IntegralToFloating:
2932 case CK_FloatingToIntegral:
2933 case CK_FloatingCast:
2934 case CK_FixedPointToFloating:
2935 case CK_FloatingToFixedPoint: {
2936 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ce);
2937 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2941 case CK_IntegralToBoolean:
2942 return emitIntToBoolConversion(Visit(subExpr),
2945 case CK_PointerToBoolean:
2946 return emitPointerToBoolConversion(Visit(subExpr), subExpr->
getType());
2947 case CK_FloatingToBoolean:
2948 return emitFloatToBoolConversion(Visit(subExpr),
2950 case CK_MemberPointerToBoolean: {
2951 mlir::Value memPtr = Visit(subExpr);
2953 cir::CastKind::member_ptr_to_bool, memPtr,
2957 case CK_HLSLAggregateSplatCast:
2958 case CK_VectorSplat: {
2961 return cir::VecSplatOp::create(builder,
2963 destCIRTy, Visit(subExpr));
2965 case CK_FunctionToPointerDecay:
2968 case CK_HLSLVectorTruncation: {
2970 "Destination type must be a vector or builtin type.");
2971 mlir::Value vec = Visit(subExpr);
2973 if (
auto *vecTy = destTy->
getAs<VectorType>()) {
2974 unsigned numElts = vecTy->getNumElements();
2975 llvm::SmallVector<int64_t, 4> mask(numElts);
2976 std::iota(mask.begin(), mask.end(), 0);
2981 case CK_HLSLMatrixTruncation: {
2983 "Destination type must be a matrix or builtin type.");
2984 mlir::Value matrix = Visit(subExpr);
2986 if (
const auto *matTy = destTy->
getAs<ConstantMatrixType>()) {
2987 llvm::SmallVector<int64_t> mask(matTy->getNumElementsFlattened());
2988 unsigned numCols = matTy->getNumColumns();
2989 unsigned numRows = matTy->getNumRows();
2991 auto *srcMatTy = subExpr->
getType()->
getAs<ConstantMatrixType>();
2992 assert(srcMatTy &&
"Source type must be a matrix type.");
2993 assert(numRows <= srcMatTy->getNumRows());
2994 assert(numCols <= srcMatTy->getNumColumns());
2996 for (
unsigned row = 0; row < numRows; row++)
2997 for (
unsigned column = 0; column < numCols; column++)
2998 mask[matTy->getColumnMajorFlattenedIndex(row, column)] =
2999 srcMatTy->getColumnMajorFlattenedIndex(row, column);
3002 mlir::cast<cir::MatrixType>(matrix.getType()).getElementType();
3003 auto resultTy = cir::MatrixType::get(builder.getContext(), elementTy,
3009 "ScalarExprEmitter::VisitCastExpr: HLSL "
3010 "Matrix truncation with non-matrix dest");
3020mlir::Value ScalarExprEmitter::VisitCallExpr(
const CallExpr *e) {
3022 return emitLoadOfLValue(e);
3029mlir::Value ScalarExprEmitter::VisitMemberExpr(MemberExpr *e) {
3034 Expr::EvalResult result;
3036 llvm::APSInt value = result.
Val.
getInt();
3046 return builder.
getBool(value.getBoolValue(), loc);
3051 return emitLoadOfLValue(e);
3055ScalarExprEmitter::emitVectorInitWithSubvectors(mlir::Location loc,
3056 cir::VectorType vectorType,
3057 ArrayRef<mlir::Value>
inits) {
3058 const unsigned resElts = vectorType.getFixedSize();
3059 mlir::Value result =
3060 builder.
getConstant(loc, cir::PoisonAttr::get(vectorType));
3061 unsigned curIdx = 0;
3062 for (mlir::Value init :
inits) {
3063 auto initType = mlir::dyn_cast<cir::VectorType>(init.getType());
3069 const unsigned initElts = initType.getFixedSize();
3070 assert(curIdx + initElts <= resElts &&
3071 "Sema should reject excess elements");
3072 SmallVector<int64_t, 16> mask(resElts, -1);
3073 std::iota(mask.begin(), mask.begin() + initElts, 0);
3077 const unsigned offset = curIdx == 0 ? 0 : resElts;
3078 std::iota(mask.begin(), mask.begin() + curIdx, 0);
3079 std::iota(mask.begin() + curIdx, mask.begin() + curIdx + initElts, offset);
3081 std::swap(result, ext);
3086 if (curIdx < resElts) {
3087 const mlir::Value zeroValue =
3088 builder.
getNullValue(vectorType.getElementType(), loc);
3089 for (; curIdx < resElts; ++curIdx)
3095mlir::Value ScalarExprEmitter::VisitInitListExpr(InitListExpr *e) {
3096 const unsigned numInitElements = e->
getNumInits();
3098 [[maybe_unused]]
const bool ignore = std::exchange(ignoreResultAssign,
false);
3099 assert((ignore ==
false ||
3101 "init list ignored");
3109 const auto vectorType =
3112 SmallVector<mlir::Value, 16> elements;
3113 bool hasVectorInit =
false;
3114 for (Expr *init : e->
inits()) {
3115 elements.push_back(Visit(init));
3116 hasVectorInit |= mlir::isa<cir::VectorType>(elements.back().getType());
3121 vectorType, elements);
3124 const uint64_t vectorSize = vectorType.getFixedSize();
3125 if (numInitElements < vectorSize) {
3128 std::fill_n(std::back_inserter(elements), vectorSize - numInitElements,
3132 return cir::VecCreateOp::create(cgf.
getBuilder(),
3143 if (numInitElements == 0)
3154 "Invalid scalar expression to emit");
3155 return ScalarExprEmitter(*
this, builder)
3156 .emitScalarConversion(src, srcTy, dstTy, loc);
3164 "Invalid complex -> scalar conversion");
3169 ? cir::CastKind::float_complex_to_bool
3170 : cir::CastKind::int_complex_to_bool;
3175 ? cir::CastKind::float_complex_to_real
3176 : cir::CastKind::int_complex_to_real;
3182mlir::Value ScalarExprEmitter::VisitUnaryLNot(
const UnaryOperator *e) {
3189 auto operVecTy = mlir::cast<cir::VectorType>(oper.getType());
3190 mlir::Value zeroVec = builder.
getNullValue(operVecTy, loc);
3192 if (cir::isFPOrVectorOfFPType(operVecTy.getElementType())) {
3212mlir::Value ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *e) {
3214 Expr::EvalResult evalResult;
3217 llvm::APSInt value = evalResult.
Val.
getInt();
3223 "ScalarExprEmitter::VisitOffsetOfExpr Can't eval expr as int");
3227mlir::Value ScalarExprEmitter::VisitUnaryReal(
const UnaryOperator *e) {
3229 mlir::Value result = VisitRealImag(e, promotionTy);
3230 if (result && !promotionTy.
isNull())
3231 result = emitUnPromotedValue(result, e->
getType());
3235mlir::Value ScalarExprEmitter::VisitUnaryImag(
const UnaryOperator *e) {
3237 mlir::Value result = VisitRealImag(e, promotionTy);
3238 if (result && !promotionTy.
isNull())
3239 result = emitUnPromotedValue(result, e->
getType());
3243mlir::Value ScalarExprEmitter::VisitRealImag(
const UnaryOperator *e,
3244 QualType promotionTy) {
3247 "Invalid UnaryOp kind for ComplexType Real or Imag");
3269 mlir::Value operand = promotionTy.
isNull()
3271 : cgf.emitPromotedScalarExpr(op, promotionTy);
3277 mlir::Value operand;
3280 operand = cir::LoadOp::create(builder, loc, operand);
3281 }
else if (!promotionTy.
isNull()) {
3291mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
3292 const UnaryExprOrTypeTraitExpr *e) {
3296 kind == UETT_SizeOf ||
kind == UETT_DataSizeOf ||
kind == UETT_CountOf) {
3297 if (
const VariableArrayType *vat =
3302 bool evaluateExtent =
true;
3303 if (
kind == UETT_CountOf && vat->getElementType()->isArrayType()) {
3305 !vat->getSizeExpr()->isIntegerConstantExpr(cgf.
getContext());
3308 if (evaluateExtent) {
3319 if (
kind == UETT_CountOf)
3324 CIRGenFunction::VlaSizePair vlaSize = cgf.
getVLASize(vat);
3325 mlir::Value numElts = vlaSize.
numElts;
3329 if (!eltSize.
isOne()) {
3331 mlir::Value eltSizeValue =
3334 return builder.
createMul(loc, eltSizeValue, numElts,
3335 cir::OverflowFlags::NoUnsignedWrap);
3341 }
else if (e->
getKind() == UETT_OpenMPRequiredSimdAlign) {
3348 }
else if (e->
getKind() == UETT_VectorElements) {
3350 if (vecTy.isScalable()) {
3353 "VisitUnaryExprOrTypeTraitExpr: sizeOf scalable vector");
3360 loc, cir::IntAttr::get(cgf.
cgm.
sizeTy, vecTy.getFixedSize()));
3370mlir::Value ScalarExprEmitter::VisitAbstractConditionalOperator(
3371 const AbstractConditionalOperator *e) {
3374 ignoreResultAssign =
false;
3377 CIRGenFunction::OpaqueValueMapping binding(cgf, e);
3379 Expr *condExpr = e->
getCond();
3387 Expr *live = lhsExpr, *dead = rhsExpr;
3389 std::swap(live, dead);
3395 mlir::Value result = Visit(live);
3402 loc, cir::PoisonAttr::get(builder.getContext(),
3410 QualType condType = condExpr->
getType();
3419 mlir::Value lhsValue = Visit(lhsExpr);
3420 mlir::Value rhsValue = Visit(rhsExpr);
3426 mlir::Value zeroVec = builder.
getNullValue(vecTy, loc);
3427 auto testMSB = cir::VecCmpOp::create(
3428 builder, loc, vecTy, cir::CmpOpKind::lt, condValue, zeroVec);
3430 mlir::Value tmp2 = builder.
createNot(tmp);
3433 mlir::Value rhsTmp = rhsValue;
3434 mlir::Value lhsTmp = lhsValue;
3435 bool wasCast =
false;
3436 if (rhsValue.getType() != vecTy) {
3442 mlir::Value tmp3 = builder.
createAnd(loc, rhsTmp, tmp2);
3443 mlir::Value tmp4 = builder.
createAnd(loc, lhsTmp, tmp);
3444 mlir::Value tmp5 = builder.
createOr(loc, tmp3, tmp4);
3453 cgf.
cgm.
errorNYI(loc,
"TernaryOp for SVE vector");
3457 mlir::Value condValue = Visit(condExpr);
3458 mlir::Value lhsValue = Visit(lhsExpr);
3459 mlir::Value rhsValue = Visit(rhsExpr);
3460 return cir::VecTernaryOp::create(builder, loc, condValue, lhsValue,
3474 mlir::Value lhs = Visit(lhsExpr);
3475 mlir::Value rhs = Visit(rhsExpr);
3478 assert(!rhs &&
"lhs and rhs types must match");
3486 CIRGenFunction::ConditionalEvaluation eval(cgf, loc);
3488 auto emitBranch = [&](mlir::OpBuilder &b, mlir::Location loc, Expr *
expr) {
3489 CIRGenFunction::LexicalScope lexScope{cgf, loc, b.getInsertionBlock()};
3496 CIRGenFunction::RunCleanupsScope branchCleanups(cgf);
3498 eval.beginEvaluation();
3499 branch = Visit(
expr);
3500 eval.endEvaluation();
3501 branchCleanups.forceCleanup({&branch});
3505 cir::YieldOp::create(b, loc, branch);
3508 cir::TernaryOp ternary = cir::TernaryOp::create(
3509 builder, loc, condV,
3511 [&](mlir::OpBuilder &b, mlir::Location loc) {
3512 emitBranch(b, loc, lhsExpr);
3515 [&](mlir::OpBuilder &b, mlir::Location loc) {
3516 emitBranch(b, loc, rhsExpr);
3521 for (mlir::Region *region :
3522 {&ternary.getTrueRegion(), &ternary.getFalseRegion()}) {
3523 mlir::Block &lastBlock = region->back();
3524 if (lastBlock.empty() ||
3525 !lastBlock.back().hasTrait<mlir::OpTrait::IsTerminator>()) {
3526 mlir::OpBuilder::InsertionGuard guard(builder);
3527 builder.setInsertionPointToEnd(&lastBlock);
3528 cir::YieldOp::create(builder, loc);
3532 return ternary.getResult();
3537 return ScalarExprEmitter(*
this, builder).emitScalarPrePostIncDec(e, lv);
static Value * createCastsForTypeOfSameSize(CGBuilderTy &Builder, const llvm::DataLayout &DL, Value *Src, llvm::Type *DstTy, StringRef Name="")
static bool mustVisitNullValue(const Expr *e)
static bool isWidenedIntegerOp(const ASTContext &astContext, const Expr *e)
Check if e is a widened promoted integer.
static mlir::Value emitPointerArithmetic(CIRGenFunction &cgf, const BinOpInfo &op, bool isSubtraction)
Emit pointer + index arithmetic.
static mlir::Value tryEmitFMulAdd(mlir::Location loc, const BinOpInfo &op, CIRGenBuilderTy &builder, bool isSub=false)
static mlir::Value buildFMulAdd(mlir::Location addLoc, cir::FMulOp mulOp, mlir::Value addend, CIRGenBuilderTy &builder, bool negMul, bool negAdd)
static bool canElideOverflowCheck(const ASTContext &astContext, const BinOpInfo &op)
Check if we can skip the overflow check for Op.
static std::optional< QualType > getUnwidenedIntegerType(const ASTContext &astContext, const Expr *e)
If e is a widened promoted integer, get its base (unpromoted) type.
static bool isIntegerVectorBinOp(mlir::Type ty)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
mlir::Value createNSWSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::ConstantOp getBool(bool state, mlir::Location loc)
mlir::Value createDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowFlags flags=OverflowFlags::None)
mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ, const llvm::APInt &val)
mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowFlags flags=OverflowFlags::None)
mlir::Value createNSWAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc)
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr)
mlir::Value createOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createCast(mlir::Location loc, cir::CastKind kind, mlir::Value src, mlir::Type newTy)
mlir::Value createIntToPtr(mlir::Value src, mlir::Type newTy)
mlir::Value createPtrToInt(mlir::Value src, mlir::Type newTy)
mlir::Value createFDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::PointerType getPointerTo(mlir::Type ty)
cir::VecFCmpOp createVecFCompare(mlir::Location loc, cir::FCmpOpKind kind, mlir::Value lhs, mlir::Value rhs, bool signaling=false)
Compares lhs and rhs element-wise.
mlir::Value createFNeg(mlir::Location loc, mlir::Value operand)
mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowFlags flags=OverflowFlags::None)
mlir::Value createFAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand)
mlir::Value createNSWMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::GetGlobalOp createGetGlobal(mlir::Location loc, cir::GlobalOp global, bool threadLocal=false)
mlir::Value createShiftLeft(mlir::Location loc, mlir::Value lhs, unsigned bits)
mlir::Value createAnd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createExtractElement(mlir::Location loc, mlir::Value vec, uint64_t idx)
cir::VecCmpOp createVecCompare(mlir::Location loc, cir::CmpOpKind kind, mlir::Value lhs, mlir::Value rhs)
Compares lhs and rhs element-wise.
mlir::Value createIntCast(mlir::Value src, mlir::Type newTy)
mlir::Value createInsertElement(mlir::Location loc, mlir::Value vec, mlir::Value newElt, uint64_t idx)
cir::FastMathFlagsAttr getFastMathFlagsAttr()
Returns a null attribute when no fast-math flags are set.
mlir::Value createBitcast(mlir::Value src, mlir::Type newTy)
mlir::Value createFMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createNot(mlir::Location loc, mlir::Value value)
mlir::Value createSelect(mlir::Location loc, mlir::Value condition, mlir::Value trueValue, mlir::Value falseValue)
cir::ConstantOp getConstantInt(mlir::Location loc, mlir::Type ty, int64_t value)
mlir::Value createFRem(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createShiftRight(mlir::Location loc, mlir::Value lhs, unsigned bits)
mlir::Value createFSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createComplexReal(mlir::Location loc, mlir::Value operand)
mlir::Type getIntPtrType(mlir::Type ty) const
llvm::APInt getValue() const
bool isNullPointer() const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
QualType getVectorType(QualType VectorType, unsigned NumElts, VectorKind VecKind) const
Return the unique reference to a vector type of the specified element type and size.
unsigned getOpenMPDefaultSimdAlign(QualType T) const
Get default simd alignment of the specified complete type in bits.
llvm::FixedPointSemantics getFixedPointSemantics(QualType Ty) const
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
const VariableArrayType * getAsVariableArrayType(QualType T) const
QualType getComplexType(QualType T) const
Return the uniqued reference to the type for a complex number with the specified element type.
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
QualType getCorrespondingSignedType(QualType T) const
bool isPromotableIntegerType(QualType T) const
More type predicates useful for type checking/promotion.
static bool hasSameUnqualifiedType(QualType T1, QualType T2)
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
Expr * getCond() const
getCond - Return the expression representing the condition for the ?
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
LabelDecl * getLabel() const
uint64_t getValue() const
Expr * getSrcExpr() const
getSrcExpr - Return the Expr to be converted.
A builtin binary operation expression such as "x + y" or "x <= y".
SourceLocation getExprLoc() const
bool isShiftAssignOp() const
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Get the FP features status of this operator.
static bool isNullPointerArithmeticExtension(ASTContext &Ctx, Opcode Opc, const Expr *LHS, const Expr *RHS)
Return true if a binary operator using the specified opcode and operands would match the 'p = (i8*)nu...
BinaryOperatorKind Opcode
mlir::Value getPointer() const
Address withElementType(CIRGenBuilderTy &builder, mlir::Type ElemTy) const
Return address with different element type, a bitcast pointer, and the same alignment.
std::pair< mlir::Value, mlir::Value > splatMatrixOpOperandsIfNecessary(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::VecShuffleOp createVecShuffle(mlir::Location loc, mlir::Value vec1, mlir::Value vec2, llvm::ArrayRef< mlir::Attribute > maskAttrs)
mlir::Value createNeg(mlir::Location loc, mlir::Value value, bool nsw=false)
cir::MatrixMultiplyOp createMatrixMultiply(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
Multiply an MxK matrix by a KxN matrix, producing an MxN matrix.
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal)
void forceCleanup(ArrayRef< mlir::Value * > valuesToReload={})
Force the emission of cleanups now, instead of waiting until this object is destroyed.
static bool hasScalarEvaluationKind(clang::QualType type)
mlir::Value emitComplexToScalarConversion(mlir::Value src, QualType srcTy, QualType dstTy, SourceLocation loc)
Emit a conversion from the specified complex type to the specified destination type,...
mlir::Type convertType(clang::QualType t)
mlir::Value emitPromotedValue(mlir::Value result, QualType promotionType)
Address emitPointerWithAlignment(const clang::Expr *expr, LValueBaseInfo *baseInfo=nullptr)
Given an expression with a pointer type, emit the value and compute our best estimate of the alignmen...
void emitVariablyModifiedType(QualType ty)
const clang::LangOptions & getLangOpts() const
VlaSizePair getVLASize(const VariableArrayType *type)
Returns an MLIR::Value+QualType pair that corresponds to the size, in non-variably-sized elements,...
LValue emitScalarCompoundAssignWithComplex(const CompoundAssignOperator *e, mlir::Value &result)
mlir::Value emitComplexExpr(const Expr *e)
Emit the computation of the specified expression of complex type, returning the result.
RValue emitCallExpr(const clang::CallExpr *e, ReturnValueSlot returnValue=ReturnValueSlot())
LValue emitLValue(const clang::Expr *e)
Emit code to compute a designator that specifies the location of the expression.
mlir::Value evaluateExprAsBool(const clang::Expr *e)
Perform the usual unary conversions on the specified expression and compare the result against zero,...
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
bool constantFoldsToBool(const clang::Expr *cond, bool &resultBool, bool allowLabels=false)
If the specified expression does not fold to a constant, or if it does but contains a label,...
mlir::Value emitOpOnBoolExpr(mlir::Location loc, const clang::Expr *cond)
TODO(cir): see EmitBranchOnBoolExpr for extra ideas).
VlaSizePair getVLAElements1D(const VariableArrayType *vla)
Return the number of elements for a single dimension for the given array type.
mlir::Value performAddrSpaceCast(mlir::Value v, mlir::Type destTy) const
mlir::Value emitScalarConversion(mlir::Value src, clang::QualType srcType, clang::QualType dstType, clang::SourceLocation loc)
Emit a conversion from the specified type to the specified destination type, both of which are CIR sc...
Address getAddressOfDerivedClass(mlir::Location loc, Address baseAddr, const CXXRecordDecl *derived, llvm::iterator_range< CastExpr::path_const_iterator > path, bool nullCheckValue)
clang::SanitizerSet sanOpts
Sanitizers enabled for this function.
mlir::Type convertTypeForMem(QualType t)
LValue emitCompoundAssignmentLValue(const clang::CompoundAssignOperator *e)
mlir::Value getAsNaturalPointerTo(Address addr, QualType pointeeType)
mlir::Value emitScalarExpr(const clang::Expr *e, bool ignoreResultAssign=false)
Emit the computation of the specified expression of scalar type.
mlir::Value emitPromotedScalarExpr(const Expr *e, QualType promotionType)
bool shouldNullCheckClassCastValue(const CastExpr *ce)
CIRGenBuilderTy & getBuilder()
CIRGenModule & getCIRGenModule()
mlir::Value emitScalarPrePostIncDec(const UnaryOperator *e, LValue lv)
bool containsLabel(const clang::Stmt *s, bool ignoreCaseStmts=false)
Return true if the statement contains a label in it.
ConstantEmission tryEmitAsConstant(const DeclRefExpr *refExpr)
Try to emit a reference to the given value without producing it as an l-value.
LValue makeAddrLValue(Address addr, QualType ty, AlignmentSource source=AlignmentSource::Type)
Address emitArrayToPointerDecay(const Expr *e, LValueBaseInfo *baseInfo=nullptr)
LexicalScope * curLexScope
mlir::Value emitStoreThroughBitfieldLValue(RValue src, LValue dstresult)
clang::ASTContext & getContext() const
mlir::Value emitScalarConstant(const ConstantEmission &constant, Expr *e)
void emitNullabilityCheck(LValue lhs, mlir::Value rhs, clang::SourceLocation loc)
Given an assignment *lhs = rhs, emit a test that checks if rhs is nonnull, if 1LHS is marked _Nonnull...
void emitStoreThroughLValue(RValue src, LValue dst, bool isInit=false)
Store the specified rvalue into the specified lvalue, where both are guaranteed to the have the same ...
void emitIgnoredExpr(const clang::Expr *e)
Emit code to compute the specified expression, ignoring the result.
mlir::Value emitDynamicCast(Address thisAddr, const CXXDynamicCastExpr *dce)
mlir::Value getNullPointer(cir::PointerType ptrTy, QualType qt, mlir::Location loc)
Get target specific null pointer.
CharUnits computeNonVirtualBaseClassOffset(const CXXRecordDecl *derivedClass, llvm::iterator_range< CastExpr::path_const_iterator > path)
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
mlir::IntegerAttr getSize(CharUnits size)
cir::GlobalOp createGlobalString(mlir::Location loc, llvm::StringRef str, llvm::StringRef name)
Create a private constant global holding the null-terminated string str, mirroring llvm::IRBuilder::C...
const cir::CIRDataLayout & getDataLayout() const
const clang::CodeGenOptions & getCodeGenOpts() const
mlir::TypedAttr emitNullMemberAttr(QualType t, const MemberPointerType *mpt)
Returns a null attribute to represent either a null method or null data member, depending on the type...
mlir::Value getPointer() const
static RValue get(mlir::Value v)
mlir::Value getValue() const
Return the value of this scalar value.
Expr * getExpr()
Get the initialization expression that will be used.
Expr * getSemanticForm()
Get an equivalent semantic form for this expression.
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
CastKind getCastKind() const
llvm::iterator_range< path_iterator > path()
Path through the class hierarchy taken by casts between base and derived classes (see implementation ...
bool changesVolatileQualification() const
Return.
static const char * getCastKindName(CastKind CK)
QuantityType getQuantity() const
Get the raw integer representation of this quantity.
bool isOne() const
Test whether the quantity equals one.
unsigned getValue() const
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Complex values, per C99 6.2.5p11.
CompoundAssignOperator - For compound assignments (e.g.
QualType getComputationLHSType() const
QualType getComputationResultType() const
SourceLocation getExprLoc() const LLVM_READONLY
bool isSatisfied() const
Whether or not the concept with the given arguments was satisfied when the expression was created.
Expr * getSrcExpr() const
getSrcExpr - Return the Expr to be converted.
ChildElementIter< false > begin()
size_t getDataElementCount() const
This represents one expression.
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer,...
@ SE_AllowSideEffects
Allow any unmodeled side effect.
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsRValue - Return true if this is a constant which we can fold to an rvalue using any crazy t...
Expr * IgnoreImpCasts() LLVM_READONLY
Skip past any implicit casts which might surround this expression until reaching a fixed point.
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
bool allowFPContractWithinStatement() const
llvm::APInt getValue() const
Returns an internal integer representation of the literal.
llvm::APFloat getValue() const
const Expr * getSubExpr() const
Expr * getResultExpr()
Return the result expression of this controlling expression.
unsigned getNumInits() const
bool hadArrayRangeDesignator() const
const Expr * getInit(unsigned Init) const
ArrayRef< Expr * > inits() const
bool isSignedOverflowDefined() const
SourceLocation getExprLoc() const LLVM_READONLY
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
SourceRange getSourceRange() const LLVM_READONLY
SourceRange getSourceRange() const
SourceRange getSourceRange() const LLVM_READONLY
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation getExprLoc() const LLVM_READONLY
Expr * getSelectedExpr() const
const Expr * getSubExpr() const
PointerType - C99 6.7.5.1 - Pointer Declarators.
A (possibly-)qualified type.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
QualType getCanonicalType() const
bool UseExcessPrecision(const ASTContext &Ctx)
@ OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained.
@ OCL_ExplicitNone
This object can be modified without requiring retains or releases.
@ OCL_None
There is no lifetime qualification on this type.
@ OCL_Weak
Reading or writing from this object requires a barrier call.
@ OCL_Autoreleasing
Assigning into this object requires a lifetime extension.
bool isSatisfied() const
Whether or not the requires clause is satisfied.
std::string ComputeName(ASTContext &Context) const
unsigned getNumSubExprs() const
getNumSubExprs - Return the size of the SubExprs array.
Expr * getExpr(unsigned Index)
getExpr - Return the Expr at the specified index.
unsigned getPackLength() const
Retrieve the length of the parameter pack.
APValue EvaluateInContext(const ASTContext &Ctx, const Expr *DefaultExpr) const
Return the result of evaluating this SourceLocExpr in the specified (and possibly null) default argum...
SourceLocation getLocation() const
Encodes a location in the source.
SourceLocation getBegin() const
CompoundStmt * getSubStmt()
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
const char * getStmtClassName() const
Expr * getReplacement() const
bool getBoolValue() const
const APValue & getAPValue() const
bool isStoredAsBoolean() const
bool isBooleanType() const
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
bool isConstantMatrixType() const
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
const T * castAs() const
Member-template castAs<specific type>.
bool isReferenceType() const
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that the type refers to.
bool isSveVLSBuiltinType() const
Determines if this is a sizeless type supported by the 'arm_sve_vector_bits' type attribute,...
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
bool hasUnsignedIntegerRepresentation() const
Determine whether this type has an unsigned integer representation of some sort, e....
bool isExtVectorType() const
bool isExtVectorBoolType() const
bool isBuiltinType() const
Helper methods to distinguish type categories.
bool isAnyComplexType() const
bool isFixedPointType() const
Return true if this is a fixed point type according to ISO/IEC JTC1 SC22 WG14 N1169.
bool isSaturatedFixedPointType() const
Return true if this is a saturated fixed point type according to ISO/IEC JTC1 SC22 WG14 N1169.
bool hasSignedIntegerRepresentation() const
Determine whether this type has an signed integer representation of some sort, e.g....
bool isMatrixType() const
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
bool isMemberFunctionPointerType() const
bool isVectorType() const
bool isRealFloatingType() const
Floating point categories.
bool isFloatingType() const
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
const T * getAs() const
Member-template getAs<specific type>'.
bool isNullPtrType() const
QualType getTypeOfArgument() const
Gets the argument type, or the type of the argument expression, whichever is appropriate.
bool isArgumentType() const
UnaryExprOrTypeTrait getKind() const
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
SourceLocation getExprLoc() const
Expr * getSubExpr() const
static bool isIncrementOp(Opcode Op)
static bool isDecrementOp(Opcode Op)
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Get the FP features status of this operator.
static bool isPrefix(Opcode Op)
isPrefix - Return true if this is a prefix operation, like –x.
bool canOverflow() const
Returns true if the unary operator can cause an overflow.
Represents a C array with a specified size that is not an integer-constant-expression.
Represents a GCC generic vector type.
VectorKind getVectorKind() const
bool isCheapEnoughToEvaluateUnconditionally(const Expr *E, const ASTContext &Ctx)
Check whether E is cheap enough and side-effect-free enough to evaluate unconditionally instead of co...
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::ArgumentAdaptingMatcherFunc< internal::HasMatcher > has
Matches AST nodes that have child AST nodes that match the provided matcher.
const AstTypeMatcher< PointerType > pointerType
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
@ Address
A pointer to a ValueDecl.
void info(bool Verbose, unsigned Level, const char *Fmt, Ts &&...Args)
Prints an indented note to stderr when Verbose is set.
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
CastKind
CastKind - The kind of operation required for a conversion.
@ Generic
not a target-specific vector type
U cast(CodeGen::Address addr)
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 __builtin_riscv_nclipu_u32 int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
static bool instrumentation()
static bool dataMemberType()
static bool objCLifetime()
static bool vecTernaryOp()
static bool addHeapAllocSiteMetadata()
static bool mayHaveIntegerOverflow()
static bool emitMatrixIndexAssumption()
static bool openMPLastprivateConditional()
static bool llvmLoweringPtrDiffConsidersPointee()
static bool scalableVectors()
static bool memberFuncPtrAuthInfo()
static bool emitLValueAlignmentAssumption()
static bool incrementProfileCounter()
APValue Val
Val - This is the value the expression can be folded to.
bool HasSideEffects
Whether the evaluated expression has side effects.
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.