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)) {
154 mlir::Value val = builder.getConstant(
155 cgf.getLoc(e->
getSourceRange()), mlir::cast<mlir::TypedAttr>(result));
160 return cgf.emitLoadOfScalar(
161 cgf.makeNaturalAlignPointeeAddrLValue(val, e->
getType()),
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,
672 lv.isVolatileQualified());
673 return builder.createCast(cir::CastKind::int_to_bool, xchg.getResult(),
674 builder.getBoolTy());
682 cgf.sanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) &&
683 cgf.getLangOpts().getSignedOverflowBehavior() !=
684 LangOptions::SOB_Trapping) {
685 mlir::Type intTy = cgf.convertType(valType);
686 mlir::Value one = builder.getConstInt(loc, intTy, 1);
687 cir::AtomicFetchKind
kind =
688 isInc ? cir::AtomicFetchKind::Add : cir::AtomicFetchKind::Sub;
689 auto rmw = cir::AtomicFetchOp::create(
690 builder, loc, lv.getPointer(), one,
kind,
691 cir::MemOrder::SequentiallyConsistent, cir::SyncScopeKind::System,
692 builder.getI64IntegerAttr(lv.getAlignment().getQuantity()),
693 lv.isVolatileQualified(),
true);
694 mlir::Value oldVal = rmw->getResult(0);
696 return isPre ? emitIntIncOrDec(e, oldVal) : oldVal;
702 mlir::Type fpTy = cgf.convertType(valType);
703 auto fpType = mlir::cast<cir::FPTypeInterface>(fpTy);
705 if (llvm::has_single_bit(fpType.getWidth())) {
706 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
707 mlir::Value amt = builder.getConstFP(
708 loc, fpTy, llvm::APFloat(fpType.getFloatSemantics(), 1));
709 cir::AtomicFetchKind
kind =
710 isInc ? cir::AtomicFetchKind::Add : cir::AtomicFetchKind::Sub;
711 auto rmw = cir::AtomicFetchOp::create(
712 builder, loc, lv.getPointer(), amt,
kind,
713 cir::MemOrder::SequentiallyConsistent, cir::SyncScopeKind::System,
714 builder.getI64IntegerAttr(lv.getAlignment().getQuantity()),
715 lv.isVolatileQualified(),
true);
716 mlir::Value oldVal = rmw->getResult(0);
718 return isInc ? builder.createFAdd(loc, oldVal, amt)
719 : builder.createFSub(loc, oldVal, amt);
723 "Atomic inc/dec of non-power-of-2 float");
729 value = cgf.emitLoadOfLValue(lv, e->
getExprLoc()).getValue();
732 value = cgf.emitLoadOfLValue(lv, e->
getExprLoc()).getValue();
746 value = builder.getTrue(cgf.getLoc(e->
getExprLoc()));
747 }
else if (
type->isIntegerType()) {
748 QualType promotedType;
749 [[maybe_unused]]
bool canPerformLossyDemotionCheck =
false;
750 if (cgf.getContext().isPromotableIntegerType(
type)) {
751 promotedType = cgf.getContext().getPromotedIntegerType(
type);
752 assert(promotedType !=
type &&
"Shouldn't promote to the same type.");
753 canPerformLossyDemotionCheck =
true;
754 canPerformLossyDemotionCheck &=
755 cgf.getContext().getCanonicalType(
type) !=
756 cgf.getContext().getCanonicalType(promotedType);
757 canPerformLossyDemotionCheck &=
764 (!canPerformLossyDemotionCheck ||
765 type->isSignedIntegerOrEnumerationType() ||
767 mlir::cast<cir::IntType>(cgf.convertType(
type)).getWidth() ==
768 mlir::cast<cir::IntType>(cgf.convertType(
type)).getWidth()) &&
769 "The following check expects that if we do promotion to different "
770 "underlying canonical type, at least one of the types (either "
771 "base or promoted) will be signed, or the bitwidths will match.");
776 value = emitIncDecConsiderOverflowBehavior(e, value);
779 value = emitIntIncOrDec(e, input,
false);
781 }
else if (
const PointerType *ptr =
type->getAs<PointerType>()) {
782 QualType
type = ptr->getPointeeType();
783 if (
const VariableArrayType *vla =
784 cgf.getContext().getAsVariableArrayType(
type)) {
786 mlir::Value numElts = cgf.getVLASize(vla).numElts;
788 numElts = cgf.getBuilder().createNeg(loc, numElts,
true);
790 value = cgf.getBuilder().createPtrStride(loc, value, numElts);
795 mlir::Value amt = builder.getSInt32(amount, loc);
797 value = builder.createPtrStride(loc, value, amt);
799 }
else if (
type->isVectorType()) {
800 if (
type->hasIntegerRepresentation()) {
801 value = emitIntIncOrDec(e, input,
false);
803 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
804 value = emitFloatIncOrDec(e, input);
806 }
else if (
type->isRealFloatingType()) {
807 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
808 value = emitFloatIncOrDec(e, input);
809 }
else if (
type->isFixedPointType()) {
810 value = emitFixedPointIncDec(e, value,
type);
812 assert(
type->castAs<ObjCObjectPointerType>());
813 cgf.cgm.errorNYI(e->
getSourceRange(),
"Unary inc/dec ObjectiveC pointer");
817 CIRGenFunction::SourceLocRAIIObject sourceloc{cgf, e->
getSourceRange()};
821 value = cgf.emitStoreThroughBitfieldLValue(
RValue::get(value), lv);
823 cgf.emitStoreThroughLValue(
RValue::get(value), lv);
827 return e->
isPrefix() ? value : input;
830 mlir::Value emitIncDecConsiderOverflowBehavior(
const UnaryOperator *e,
832 switch (cgf.getLangOpts().getSignedOverflowBehavior()) {
833 case LangOptions::SOB_Defined:
834 return emitIntIncOrDec(e, inVal,
false);
835 case LangOptions::SOB_Undefined:
837 return emitIntIncOrDec(e, inVal,
true);
838 case LangOptions::SOB_Trapping:
840 return emitIntIncOrDec(e, inVal,
true);
841 cgf.cgm.errorNYI(e->
getSourceRange(),
"inc/def overflow SOB_Trapping");
844 llvm_unreachable(
"Unexpected signed overflow behavior kind");
847 mlir::Value VisitUnaryAddrOf(
const UnaryOperator *e) {
848 if (llvm::isa<MemberPointerType>(e->
getType()))
849 return cgf.cgm.emitMemberPointerConstant(e);
851 return cgf.emitLValue(e->
getSubExpr()).getPointer();
854 mlir::Value VisitUnaryDeref(
const UnaryOperator *e) {
857 return emitLoadOfLValue(e);
860 mlir::Value VisitUnaryPlus(
const UnaryOperator *e) {
862 mlir::Value result = VisitUnaryPlus(e, promotionType);
863 if (result && !promotionType.
isNull())
864 return emitUnPromotedValue(result, e->
getType());
868 mlir::Value VisitUnaryPlus(
const UnaryOperator *e, QualType promotionType) {
869 ignoreResultAssign =
false;
870 if (!promotionType.
isNull())
871 return cgf.emitPromotedScalarExpr(e->
getSubExpr(), promotionType);
875 mlir::Value VisitUnaryMinus(
const UnaryOperator *e) {
877 mlir::Value result = VisitUnaryMinus(e, promotionType);
878 if (result && !promotionType.
isNull())
879 return emitUnPromotedValue(result, e->
getType());
883 mlir::Value VisitUnaryMinus(
const UnaryOperator *e, QualType promotionType) {
884 ignoreResultAssign =
false;
886 if (!promotionType.
isNull())
887 operand = cgf.emitPromotedScalarExpr(e->
getSubExpr(), promotionType);
893 if (cir::isFPOrVectorOfFPType(operand.getType())) {
894 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
895 return builder.createOrFold<cir::FNegOp>(loc, operand,
896 builder.getFastMathFlagsAttr());
902 CIRGenFixedPointBuilder fpBuilder(builder, loc);
903 return fpBuilder.createNeg(
904 operand, cgf.getContext().getFixedPointSemantics(e->
getType()));
911 cgf.getLangOpts().getSignedOverflowBehavior() !=
912 LangOptions::SOB_Defined;
914 return builder.createOrFold<cir::MinusOp>(loc, operand, nsw);
917 mlir::Value emitIntIncOrDec(
const UnaryOperator *e, mlir::Value input,
921 ? builder.createOrFold<cir::IncOp>(loc, input, nsw)
922 : builder.createOrFold<cir::DecOp>(loc, input, nsw);
925 mlir::Value emitFloatIncOrDec(
const UnaryOperator *e, mlir::Value input) {
926 assert(cir::isFPOrVectorOfFPType(input.getType()) &&
927 "Expect floating-point operand");
930 if (
auto vecType = mlir::dyn_cast<cir::VectorType>(input.getType())) {
931 mlir::Type fpScalarType = vecType.getElementType();
932 auto fpInterface = mlir::cast<cir::FPTypeInterface>(fpScalarType);
933 auto amount = llvm::APFloat::getOne(fpInterface.getFloatSemantics(),
935 mlir::Value amtValue = builder.getConstFP(loc, fpScalarType, amount);
936 amtValue = cir::VecSplatOp::create(builder, loc, vecType, amtValue);
937 return builder.createFAdd(loc, input, amtValue);
942 if (
type->isHalfType() && !cgf.getContext().getLangOpts().NativeHalfType)
943 input = builder.createFloatingCast(input, builder.getSingleTy());
945 auto fpInterface = mlir::cast<cir::FPTypeInterface>(input.getType());
946 auto amount = llvm::APFloat::getOne(fpInterface.getFloatSemantics(),
948 mlir::Value amtValue = builder.getConstFP(loc, input.getType(), amount);
949 mlir::Value output = builder.createFAdd(loc, input, amtValue);
951 if (
type->isHalfType() && !cgf.getContext().getLangOpts().NativeHalfType)
952 output = builder.createFloatingCast(output, builder.getFp16Ty());
957 mlir::Value VisitUnaryNot(
const UnaryOperator *e) {
958 ignoreResultAssign =
false;
960 return builder.createOrFold<cir::NotOp>(
964 mlir::Value VisitUnaryLNot(
const UnaryOperator *e);
966 mlir::Value VisitUnaryReal(
const UnaryOperator *e);
967 mlir::Value VisitUnaryImag(
const UnaryOperator *e);
968 mlir::Value VisitRealImag(
const UnaryOperator *e,
969 QualType promotionType = QualType());
971 mlir::Value VisitUnaryExtension(
const UnaryOperator *e) {
976 mlir::Value VisitMaterializeTemporaryExpr(
const MaterializeTemporaryExpr *e) {
977 return emitLoadOfLValue(e);
979 mlir::Value VisitSourceLocExpr(SourceLocExpr *e) {
980 ASTContext &ctx = cgf.getContext();
983 mlir::Attribute attribute = ConstantEmitter(cgf).emitAbstract(
985 mlir::TypedAttr typedAttr = mlir::cast<mlir::TypedAttr>(attribute);
986 return cir::ConstantOp::create(builder, cgf.getLoc(e->
getExprLoc()),
989 mlir::Value VisitCXXDefaultArgExpr(CXXDefaultArgExpr *dae) {
990 CIRGenFunction::CXXDefaultArgExprScope scope(cgf, dae);
993 mlir::Value VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die) {
994 CIRGenFunction::CXXDefaultInitExprScope scope(cgf, die);
998 mlir::Value VisitCXXThisExpr(CXXThisExpr *te) {
return cgf.loadCXXThis(); }
1000 mlir::Value VisitExprWithCleanups(ExprWithCleanups *e);
1001 mlir::Value VisitCXXNewExpr(
const CXXNewExpr *e) {
1002 return cgf.emitCXXNewExpr(e);
1004 mlir::Value VisitCXXDeleteExpr(
const CXXDeleteExpr *e) {
1005 cgf.emitCXXDeleteExpr(e);
1008 mlir::Value VisitTypeTraitExpr(
const TypeTraitExpr *e) {
1014 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1019 "Expected int type for TypeTraitExpr");
1020 return builder.getConstInt(loc, cgf.convertType(e->
getType()),
1027 VisitConceptSpecializationExpr(
const ConceptSpecializationExpr *e) {
1033 mlir::Value VisitArrayTypeTraitExpr(
const ArrayTypeTraitExpr *e) {
1035 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1038 mlir::Value VisitExpressionTraitExpr(
const ExpressionTraitExpr *e) {
1041 mlir::Value VisitCXXPseudoDestructorExpr(
const CXXPseudoDestructorExpr *e) {
1043 "ScalarExprEmitter: cxx pseudo destructor");
1046 mlir::Value VisitCXXThrowExpr(
const CXXThrowExpr *e) {
1047 cgf.emitCXXThrowExpr(e);
1051 mlir::Value VisitCXXNoexceptExpr(CXXNoexceptExpr *e) {
1055 mlir::Value emitFixedPointConversion(mlir::Value src, QualType srcTy,
1056 QualType dstTy, mlir::Location loc);
1063 emitScalarConversion(mlir::Value src, QualType srcType, QualType dstType,
1065 ScalarConversionOpts opts = ScalarConversionOpts()) {
1073 cir::BoolType boolTy = builder.getBoolTy();
1074 return cir::CastOp::create(builder, cgf.getLoc(loc), boolTy,
1075 cir::CastKind::int_to_bool, src);
1080 return emitFixedPointConversion(src, srcType, dstType, cgf.getLoc(loc));
1082 llvm_unreachable(
"Unhandled scalar conversion from a fixed point type to "
1088 return emitFixedPointConversion(src, srcType, dstType, cgf.getLoc(loc));
1091 llvm_unreachable(
"Unhandled scalar conversion to a fixed point type from "
1097 if (srcType == dstType) {
1098 if (opts.emitImplicitIntegerSignChangeChecks)
1099 cgf.getCIRGenModule().errorNYI(loc,
1100 "implicit integer sign change checks");
1107 mlir::Type mlirSrcType = src.getType();
1112 return emitConversionToBool(src, srcType, cgf.getLoc(loc));
1114 mlir::Type mlirDstType = cgf.convertType(dstType);
1117 !cgf.getContext().getLangOpts().NativeHalfType) {
1119 if (!mlir::isa<cir::FPTypeInterface>(mlirDstType)) {
1123 src = builder.createCast(cgf.getLoc(loc), cir::CastKind::floating, src,
1125 srcType = cgf.getContext().FloatTy;
1126 mlirSrcType = cgf.floatTy;
1132 if (mlirSrcType == mlirDstType) {
1133 if (opts.emitImplicitIntegerSignChangeChecks)
1134 cgf.getCIRGenModule().errorNYI(loc,
1135 "implicit integer sign change checks");
1142 if (
auto dstPT = dyn_cast<cir::PointerType>(mlirDstType)) {
1143 cgf.getCIRGenModule().errorNYI(loc,
"pointer casts");
1144 return builder.getNullPtr(dstPT, src.getLoc());
1150 return builder.createPtrToInt(src, mlirDstType);
1157 assert(dstType->
castAs<ExtVectorType>()->getElementType().getTypePtr() ==
1159 "Splatted expr doesn't match with vector element type?");
1161 cgf.getCIRGenModule().errorNYI(loc,
"vector splatting");
1166 return emitScalarCast(src, srcType, dstType, mlirSrcType, mlirDstType,
1170 "Internal error: conversion between matrix type and scalar type");
1173 mlir::Value res =
nullptr;
1174 mlir::Type resTy = mlirDstType;
1176 res = emitScalarCast(src, srcType, dstType, mlirSrcType, mlirDstType, opts);
1178 if (mlirDstType != resTy) {
1179 res = builder.createCast(cgf.getLoc(loc), cir::CastKind::floating, res,
1183 if (opts.emitImplicitIntegerTruncationChecks)
1184 cgf.getCIRGenModule().errorNYI(loc,
"implicit integer truncation checks");
1186 if (opts.emitImplicitIntegerSignChangeChecks)
1187 cgf.getCIRGenModule().errorNYI(loc,
1188 "implicit integer sign change checks");
1193 BinOpInfo emitBinOps(
const BinaryOperator *e,
1194 QualType promotionType = QualType()) {
1195 ignoreResultAssign =
false;
1197 result.lhs = cgf.emitPromotedScalarExpr(e->
getLHS(), promotionType);
1198 result.rhs = cgf.emitPromotedScalarExpr(e->
getRHS(), promotionType);
1199 if (!promotionType.
isNull())
1200 result.fullType = promotionType;
1202 result.fullType = e->
getType();
1203 result.compType = result.fullType;
1204 if (
const auto *vecType = result.fullType->
getAs<VectorType>())
1205 result.compType = vecType->getElementType();
1213 mlir::Value emitMul(
const BinOpInfo &ops);
1214 mlir::Value emitDiv(
const BinOpInfo &ops);
1215 mlir::Value emitRem(
const BinOpInfo &ops);
1216 mlir::Value emitAdd(
const BinOpInfo &ops);
1217 mlir::Value emitSub(
const BinOpInfo &ops);
1218 mlir::Value emitShl(
const BinOpInfo &ops);
1219 mlir::Value emitShr(
const BinOpInfo &ops);
1220 mlir::Value emitAnd(
const BinOpInfo &ops);
1221 mlir::Value emitXor(
const BinOpInfo &ops);
1222 mlir::Value emitOr(
const BinOpInfo &ops);
1224 mlir::Value emitFixedPointBinOp(
const BinOpInfo &ops);
1226 LValue emitCompoundAssignLValue(
1227 const CompoundAssignOperator *e,
1228 mlir::Value (ScalarExprEmitter::*f)(
const BinOpInfo &),
1229 mlir::Value &result);
1231 emitCompoundAssign(
const CompoundAssignOperator *e,
1232 mlir::Value (ScalarExprEmitter::*f)(
const BinOpInfo &));
1236 QualType getPromotionType(QualType ty) {
1237 const clang::ASTContext &ctx = cgf.getContext();
1238 if (
auto *complexTy = ty->
getAs<ComplexType>()) {
1239 QualType elementTy = complexTy->getElementType();
1245 if (
auto *vt = ty->
getAs<VectorType>()) {
1246 unsigned numElements = vt->getNumElements();
1249 return cgf.getContext().FloatTy;
1256#define HANDLEBINOP(OP) \
1257 mlir::Value VisitBin##OP(const BinaryOperator *e) { \
1258 QualType promotionTy = getPromotionType(e->getType()); \
1259 auto result = emit##OP(emitBinOps(e, promotionTy)); \
1260 if (result && !promotionTy.isNull()) \
1261 result = emitUnPromotedValue(result, e->getType()); \
1264 mlir::Value VisitBin##OP##Assign(const CompoundAssignOperator *e) { \
1265 return emitCompoundAssign(e, &ScalarExprEmitter::emit##OP); \
1283 return cir::CmpOpKind::lt;
1285 return cir::CmpOpKind::gt;
1287 return cir::CmpOpKind::le;
1289 return cir::CmpOpKind::ge;
1291 return cir::CmpOpKind::eq;
1293 return cir::CmpOpKind::ne;
1295 llvm_unreachable(
"unsupported comparison kind for cir.cmp");
1299 std::pair<bool, cir::FCmpOpKind>
1303 return std::make_pair(
true, cir::FCmpOpKind::olt);
1305 return std::make_pair(
true, cir::FCmpOpKind::ogt);
1307 return std::make_pair(
true, cir::FCmpOpKind::ole);
1309 return std::make_pair(
true, cir::FCmpOpKind::oge);
1311 return std::make_pair(
false, cir::FCmpOpKind::oeq);
1313 return std::make_pair(
false, cir::FCmpOpKind::une);
1315 llvm_unreachable(
"unsupported comparison kind");
1319 mlir::Value emitCmp(
const BinaryOperator *e) {
1320 ignoreResultAssign =
false;
1321 const mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1327 if (lhsTy->
getAs<MemberPointerType>()) {
1330 mlir::Value lhs = cgf.emitScalarExpr(e->
getLHS());
1331 mlir::Value rhs = cgf.emitScalarExpr(e->
getRHS());
1332 result = builder.createCompare(loc,
kind, lhs, rhs);
1334 BinOpInfo boInfo = emitBinOps(e);
1335 mlir::Value lhs = boInfo.lhs;
1336 mlir::Value rhs = boInfo.rhs;
1342 cgf.cgm.errorNYI(loc,
"AltiVec comparison");
1347 mlir::cast<cir::VectorType>(cgf.convertType(boInfo.fullType));
1348 if (cir::isFPOrVectorOfFPType(lhs.getType())) {
1349 std::pair<bool, cir::FCmpOpKind> fpInfo =
1351 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
1352 result = builder.createVecFCompare(cgf.getLoc(boInfo.loc), resultTy,
1353 fpInfo.second, lhs, rhs,
1356 result = builder.createVecCompare(cgf.getLoc(boInfo.loc), resultTy,
1360 }
else if (boInfo.isFixedPointOp()) {
1361 result = emitFixedPointBinOp(boInfo);
1364 if (cgf.cgm.getCodeGenOpts().StrictVTablePointers &&
1365 mlir::isa<cir::PointerType>(lhs.getType()) &&
1366 mlir::isa<cir::PointerType>(rhs.getType())) {
1367 cgf.cgm.errorNYI(loc,
"strict vtable pointer comparisons");
1369 result = builder.createCompare(loc,
kind, lhs, rhs);
1373 "Complex Comparison: can only be an equality comparison");
1377 lhs = cgf.emitComplexExpr(e->
getLHS());
1379 mlir::Value lhsReal = Visit(e->
getLHS());
1380 mlir::Value lhsImag = builder.getNullValue(convertType(lhsTy), loc);
1381 lhs = builder.createComplexCreate(loc, lhsReal, lhsImag);
1386 rhs = cgf.emitComplexExpr(e->
getRHS());
1388 mlir::Value rhsReal = Visit(e->
getRHS());
1389 mlir::Value rhsImag = builder.getNullValue(convertType(rhsTy), loc);
1390 rhs = builder.createComplexCreate(loc, rhsReal, rhsImag);
1393 result = builder.createCompare(loc,
kind, lhs, rhs);
1396 return emitScalarConversion(result, cgf.getContext().BoolTy, e->
getType(),
1401#define VISITCOMP(CODE) \
1402 mlir::Value VisitBin##CODE(const BinaryOperator *E) { return emitCmp(E); }
1412 const bool ignore = std::exchange(ignoreResultAssign,
false);
1427 rhs = Visit(e->
getRHS());
1437 if (lhs.isBitField()) {
1457 if (!lhs.isVolatile())
1461 return emitLoadOfLValue(lhs, e->
getExprLoc());
1464 mlir::Value VisitBinComma(
const BinaryOperator *e) {
1465 cgf.emitIgnoredExpr(e->
getLHS());
1467 return Visit(e->
getRHS());
1470 mlir::Value VisitBinLAnd(
const clang::BinaryOperator *e) {
1472 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1474 mlir::Value zeroVec = builder.getNullValue(lhsTy, loc);
1476 mlir::Value lhs = Visit(e->
getLHS());
1477 mlir::Value rhs = Visit(e->
getRHS());
1480 mlir::cast<cir::VectorType>(cgf.convertType(e->
getType()));
1481 if (cir::isFPOrVectorOfFPType(lhs.getType())) {
1482 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
1483 auto cmpOpKind = cir::FCmpOpKind::une;
1484 lhs = builder.createVecFCompare(loc, resultTy, cmpOpKind, lhs, zeroVec);
1485 rhs = builder.createVecFCompare(loc, resultTy, cmpOpKind, rhs, zeroVec);
1487 auto cmpOpKind = cir::CmpOpKind::ne;
1488 lhs = builder.createVecCompare(loc, resultTy, cmpOpKind, lhs, zeroVec);
1489 rhs = builder.createVecCompare(loc, resultTy, cmpOpKind, rhs, zeroVec);
1491 return builder.createAnd(loc, lhs, rhs);
1495 mlir::Type resTy = cgf.convertType(e->
getType());
1496 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1498 mlir::Value lhsCondV = cgf.evaluateExprAsBool(e->
getLHS());
1500 CIRGenFunction::ConditionalEvaluation eval(cgf, loc);
1502 auto resOp = cir::TernaryOp::create(
1503 builder, loc, lhsCondV,
1504 [&](mlir::OpBuilder &b, mlir::Location loc) {
1505 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1506 b.getInsertionBlock()};
1507 cgf.curLexScope->setAsTernary();
1508 eval.beginEvaluation();
1509 mlir::Value res = cgf.evaluateExprAsBool(e->
getRHS());
1510 eval.endEvaluation();
1512 cir::YieldOp::create(b, loc, res);
1515 [&](mlir::OpBuilder &b, mlir::Location loc) {
1516 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1517 b.getInsertionBlock()};
1519 auto res = cir::ConstantOp::create(b, loc, builder.getFalseAttr());
1520 cir::YieldOp::create(b, loc, res.getRes());
1522 return maybePromoteBoolResult(resOp.getResult(), resTy);
1525 mlir::Value VisitBinLOr(
const clang::BinaryOperator *e) {
1527 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1529 mlir::Value zeroVec = builder.getNullValue(lhsTy, loc);
1531 mlir::Value lhs = Visit(e->
getLHS());
1532 mlir::Value rhs = Visit(e->
getRHS());
1535 mlir::cast<cir::VectorType>(cgf.convertType(e->
getType()));
1536 if (cir::isFPOrVectorOfFPType(lhs.getType())) {
1537 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, e);
1538 auto cmpOpKind = cir::FCmpOpKind::une;
1539 lhs = builder.createVecFCompare(loc, resultTy, cmpOpKind, lhs, zeroVec);
1540 rhs = builder.createVecFCompare(loc, resultTy, cmpOpKind, rhs, zeroVec);
1542 auto cmpOpKind = cir::CmpOpKind::ne;
1543 lhs = builder.createVecCompare(loc, resultTy, cmpOpKind, lhs, zeroVec);
1544 rhs = builder.createVecCompare(loc, resultTy, cmpOpKind, rhs, zeroVec);
1546 return builder.createOr(loc, lhs, rhs);
1550 mlir::Type resTy = cgf.convertType(e->
getType());
1551 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1553 mlir::Value lhsCondV = cgf.evaluateExprAsBool(e->
getLHS());
1555 CIRGenFunction::ConditionalEvaluation eval(cgf, loc);
1557 auto resOp = cir::TernaryOp::create(
1558 builder, loc, lhsCondV,
1559 [&](mlir::OpBuilder &b, mlir::Location loc) {
1560 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1561 b.getInsertionBlock()};
1563 auto res = cir::ConstantOp::create(b, loc, builder.getTrueAttr());
1564 cir::YieldOp::create(b, loc, res.getRes());
1567 [&](mlir::OpBuilder &b, mlir::Location loc) {
1568 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1569 b.getInsertionBlock()};
1571 eval.beginEvaluation();
1572 mlir::Value res = cgf.evaluateExprAsBool(e->
getRHS());
1573 eval.endEvaluation();
1575 cir::YieldOp::create(b, loc, res);
1578 return maybePromoteBoolResult(resOp.getResult(), resTy);
1581 mlir::Value VisitBinPtrMemD(
const BinaryOperator *e) {
1582 return emitLoadOfLValue(e);
1585 mlir::Value VisitBinPtrMemI(
const BinaryOperator *e) {
1586 return emitLoadOfLValue(e);
1590 mlir::Value VisitBlockExpr(
const BlockExpr *e) {
1591 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: block");
1595 mlir::Value VisitChooseExpr(ChooseExpr *e) {
1599 mlir::Value VisitObjCStringLiteral(
const ObjCStringLiteral *e) {
1601 "ScalarExprEmitter: objc string literal");
1604 mlir::Value VisitObjCBoxedExpr(ObjCBoxedExpr *e) {
1605 cgf.cgm.errorNYI(e->
getSourceRange(),
"ScalarExprEmitter: objc boxed");
1608 mlir::Value VisitObjCArrayLiteral(ObjCArrayLiteral *e) {
1610 "ScalarExprEmitter: objc array literal");
1613 mlir::Value VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *e) {
1615 "ScalarExprEmitter: objc dictionary literal");
1619 mlir::Value convertVec3AndVec4(CIRGenBuilderTy &builder, mlir::Location loc,
1620 mlir::Value src,
unsigned numElementsDst) {
1621 static constexpr int64_t mask[] = {0, 1, 2, -1};
1622 return builder.createVecShuffle(
1623 loc, src, llvm::ArrayRef<int64_t>(mask, numElementsDst));
1645 mlir::Type srcTy = src.getType();
1649 return builder.createBitcast(src, dstTy);
1654 "ScalarExprEmitter: createCastsForTypeOfSameSize Case 2");
1662 "ScalarExprEmitter: createCastsForTypeOfSameSize Case 3a");
1666 "ScalarExprEmitter: createCastsForTypeOfSameSize Case 3a and 3b");
1673 "ScalarExprEmitter: createCastsForTypeOfSameSize Case 4a");
1677 return builder.createIntToPtr(src, dstTy);
1680 mlir::Value VisitAsTypeExpr(AsTypeExpr *e) {
1681 mlir::Value src = cgf.emitScalarExpr(e->
getSrcExpr());
1682 mlir::Type srcTy = src.getType();
1683 mlir::Type dstTy = cgf.convertType(e->
getType());
1695 "ScalarExprEmitter: VisitAsTypeExpr ExtVectorBoolType");
1701 if (numElementsSrc == 3 && numElementsDst != 3) {
1703 "ScalarExprEmitter: VisitAsTypeExpr numElemsSrc = 3, "
1704 "numElemsDst != 3");
1711 if (numElementsSrc != 3 && numElementsDst == 3) {
1712 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1714 auto dstVec4Ty = cir::VectorType::getFixed(dstElemTy, 4);
1716 src = convertVec3AndVec4(builder, loc, src, 3);
1723 mlir::Value VisitAtomicExpr(AtomicExpr *e) {
1724 return cgf.emitAtomicExpr(e).getValue();
1728LValue ScalarExprEmitter::emitCompoundAssignLValue(
1730 mlir::Value (ScalarExprEmitter::*func)(
const BinOpInfo &),
1731 mlir::Value &result) {
1742 if (promotionTypeCR.
isNull())
1746 QualType promotionTypeRHS = getPromotionType(e->
getRHS()->
getType());
1748 if (!promotionTypeRHS.
isNull())
1751 opInfo.rhs = Visit(e->
getRHS());
1753 opInfo.fullType = promotionTypeCR;
1754 opInfo.compType = opInfo.fullType;
1755 if (
const auto *vecType = opInfo.fullType->
getAs<VectorType>())
1756 opInfo.compType = vecType->getElementType();
1765 if (lhsTy->
getAs<AtomicType>()) {
1766 cgf.
cgm.
errorNYI(result.getLoc(),
"atomic lvalue assign");
1770 opInfo.lhs = emitLoadOfLValue(lhsLV, e->
getExprLoc());
1772 CIRGenFunction::SourceLocRAIIObject sourceloc{cgf, e->
getSourceRange()};
1774 if (!promotionTypeLHS.
isNull())
1775 opInfo.lhs = emitScalarConversion(opInfo.lhs, lhsTy, promotionTypeLHS, loc);
1777 opInfo.lhs = emitScalarConversion(opInfo.lhs, lhsTy,
1781 result = (this->*func)(opInfo);
1785 result = emitScalarConversion(result, promotionTypeCR, lhsTy, loc,
1786 ScalarConversionOpts(cgf.
sanOpts));
1792 if (lhsLV.isBitField())
1802mlir::Value ScalarExprEmitter::emitComplexToScalarConversion(mlir::Location lov,
1806 cir::CastKind castOpKind;
1808 case CK_FloatingComplexToReal:
1809 castOpKind = cir::CastKind::float_complex_to_real;
1811 case CK_IntegralComplexToReal:
1812 castOpKind = cir::CastKind::int_complex_to_real;
1814 case CK_FloatingComplexToBoolean:
1815 castOpKind = cir::CastKind::float_complex_to_bool;
1817 case CK_IntegralComplexToBoolean:
1818 castOpKind = cir::CastKind::int_complex_to_bool;
1821 llvm_unreachable(
"invalid complex-to-scalar cast kind");
1827mlir::Value ScalarExprEmitter::emitPromoted(
const Expr *e,
1828 QualType promotionType) {
1830 if (
const auto *bo = dyn_cast<BinaryOperator>(e)) {
1831 switch (bo->getOpcode()) {
1832#define HANDLE_BINOP(OP) \
1834 return emit##OP(emitBinOps(bo, promotionType));
1843 }
else if (
const auto *uo = dyn_cast<UnaryOperator>(e)) {
1844 switch (uo->getOpcode()) {
1847 return VisitRealImag(uo, promotionType);
1849 return VisitUnaryMinus(uo, promotionType);
1851 return VisitUnaryPlus(uo, promotionType);
1856 mlir::Value result = Visit(
const_cast<Expr *
>(e));
1858 if (!promotionType.
isNull())
1859 return emitPromotedValue(result, promotionType);
1860 return emitUnPromotedValue(result, e->
getType());
1865mlir::Value ScalarExprEmitter::emitCompoundAssign(
1866 const CompoundAssignOperator *e,
1867 mlir::Value (ScalarExprEmitter::*func)(
const BinOpInfo &)) {
1869 bool ignore = std::exchange(ignoreResultAssign,
false);
1871 LValue lhs = emitCompoundAssignLValue(e, func, rhs);
1882 if (!lhs.isVolatile())
1886 return emitLoadOfLValue(lhs, e->
getExprLoc());
1890ScalarExprEmitter::VisitSYCLUniqueStableNameExpr(SYCLUniqueStableNameExpr *e) {
1893 cir::GlobalOp globalConstStr =
1899 auto exprTy = mlir::cast<cir::PointerType>(convertType(e->
getType()));
1900 auto addrTy = mlir::cast<cir::PointerType>(addr.getType());
1901 if (addrTy.getAddrSpace() != exprTy.getAddrSpace())
1903 addr, builder.
getPointerTo(addrTy.getPointee(), exprTy.getAddrSpace()));
1907mlir::Value ScalarExprEmitter::VisitExprWithCleanups(ExprWithCleanups *e) {
1908 CIRGenFunction::FullExprCleanupScope scope(cgf, e->
getSubExpr());
1920 ScalarExprEmitter emitter(*
this, builder);
1923#define COMPOUND_OP(Op) \
1924 case BO_##Op##Assign: \
1925 return emitter.emitCompoundAssignLValue(e, &ScalarExprEmitter::emit##Op, \
1962 llvm_unreachable(
"Not valid compound assignment operators");
1964 llvm_unreachable(
"Unhandled compound assignment operator");
1969 bool ignoreResultAssign) {
1971 "Invalid scalar expression to emit");
1973 return ScalarExprEmitter(*
this, builder, ignoreResultAssign)
1974 .Visit(
const_cast<Expr *
>(e));
1979 if (!promotionType.
isNull())
1980 return ScalarExprEmitter(*
this, builder).emitPromoted(e, promotionType);
1981 return ScalarExprEmitter(*
this, builder).Visit(
const_cast<Expr *
>(e));
1994static std::optional<QualType>
1998 return std::nullopt;
2003 return std::nullopt;
2016 const BinOpInfo &op) {
2018 "Expected a unary or binary operator");
2022 if (!op.mayHaveIntegerOverflow())
2026 if (
const auto *uo = dyn_cast<UnaryOperator>(op.e))
2027 return !uo->canOverflow();
2032 std::optional<QualType> optionalLHSTy =
2037 std::optional<QualType> optionalRHSTy =
2047 if ((op.opcode != BO_Mul && op.opcode != BO_MulAssign) ||
2054 return (2 * astContext.
getTypeSize(lhsTy)) < promotedSize ||
2055 (2 * astContext.
getTypeSize(rhsTy)) < promotedSize;
2060 const BinOpInfo &op,
2061 bool isSubtraction) {
2066 mlir::Value pointer = op.lhs;
2067 Expr *pointerOperand =
expr->getLHS();
2068 mlir::Value
index = op.rhs;
2069 Expr *indexOperand =
expr->getRHS();
2075 if (!isSubtraction && !mlir::isa<cir::PointerType>(pointer.getType())) {
2076 std::swap(pointer,
index);
2077 std::swap(pointerOperand, indexOperand);
2079 assert(mlir::isa<cir::PointerType>(pointer.getType()) &&
2080 "Need a pointer operand");
2081 assert(mlir::isa<cir::IntType>(
index.getType()) &&
"Need an integer operand");
2116 cgf.
cgm.
errorNYI(
"Objective-C:pointer arithmetic with non-pointer type");
2126 numElements.getType());
2130 cir::OverflowFlags overflowFlags = cgf.
getLangOpts().PointerOverflowDefined
2131 ? cir::OverflowFlags::None
2132 : cir::OverflowFlags::NoSignedWrap;
2135 return cir::PtrStrideOp::create(cgf.
getBuilder(), loc, pointer.getType(),
2140 return cir::PtrStrideOp::create(cgf.
getBuilder(),
2142 pointer.getType(), pointer,
index);
2146 auto vecTy = mlir::dyn_cast<cir::VectorType>(ty);
2147 return vecTy && mlir::isa<cir::IntType>(vecTy.getElementType());
2157 bool negMul,
bool negAdd) {
2158 mlir::Location loc = builder.getFusedLoc({mulOp.getLoc(), addLoc});
2159 mlir::Value mulOp0 = mulOp.getLhs();
2160 mlir::Value mulOp1 = mulOp.getRhs();
2168 mlir::Value fmuladd = cir::FMulAddOp::create(
2169 builder, loc, addend.getType(), mulOp0, mulOp1, addend,
2183 bool isSub =
false) {
2184 assert((op.opcode == BO_Add || op.opcode == BO_AddAssign ||
2185 op.opcode == BO_Sub || op.opcode == BO_SubAssign) &&
2186 "Only fadd/fsub can be the root of an fmuladd.");
2193 mlir::Value lhs = op.lhs;
2194 mlir::Value rhs = op.rhs;
2198 bool negLHS =
false;
2199 if (
auto lhsNeg = lhs.getDefiningOp<cir::FNegOp>()) {
2200 if (lhsNeg.getResult().use_empty() && lhsNeg.getInput().hasOneUse()) {
2201 lhs = lhsNeg.getInput();
2206 bool negRHS =
false;
2207 if (
auto rhsNeg = rhs.getDefiningOp<cir::FNegOp>()) {
2208 if (rhsNeg.getResult().use_empty() && rhsNeg.getInput().hasOneUse()) {
2209 rhs = rhsNeg.getInput();
2217 if (
auto lhsMul = lhs.getDefiningOp<cir::FMulOp>()) {
2218 if (lhsMul.getResult().use_empty() || negLHS) {
2221 op.lhs.getDefiningOp<cir::FNegOp>().erase();
2222 return buildFMulAdd(loc, lhsMul, op.rhs, builder, negLHS, isSub);
2225 if (
auto rhsMul = rhs.getDefiningOp<cir::FMulOp>()) {
2226 if (rhsMul.getResult().use_empty() || negRHS) {
2229 op.rhs.getDefiningOp<cir::FNegOp>().erase();
2230 return buildFMulAdd(loc, rhsMul, op.lhs, builder, isSub ^ negRHS,
false);
2237mlir::Value ScalarExprEmitter::emitFixedPointIncDec(
const UnaryOperator *e,
2256 if (
type->isSignedFixedPointType()) {
2263 CIRGenFixedPointBuilder fpbuilder(builder, cgf.
getLoc(
info.loc));
2266 fpbuilder.createIntegerToFixed(
info.rhs,
true, dstSema);
2267 return emitFixedPointBinOp(info);
2270mlir::Value ScalarExprEmitter::emitFixedPointConversion(mlir::Value src,
2273 mlir::Location loc) {
2276 CIRGenFixedPointBuilder fpBuilder(builder, loc);
2280 result = fpBuilder.createFloatingToFixed(
2284 result = fpBuilder.createFixedToFloating(
2288 llvm::FixedPointSemantics srcFPSema =
2290 llvm::FixedPointSemantics dstFPSema =
2294 result = fpBuilder.createFixedToInteger(
2295 src, srcFPSema, dstFPSema.getWidth(), dstFPSema.isSigned());
2298 fpBuilder.createIntegerToFixed(src, srcFPSema.isSigned(), dstFPSema);
2301 result = fpBuilder.createFixedToFixed(src, srcFPSema, dstFPSema);
2307mlir::Value ScalarExprEmitter::emitMul(
const BinOpInfo &ops) {
2308 const mlir::Location loc = cgf.
getLoc(ops.loc);
2311 switch (cgf.
getLangOpts().getSignedOverflowBehavior()) {
2312 case LangOptions::SOB_Defined:
2313 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2314 return builder.
createMul(loc, ops.lhs, ops.rhs);
2316 case LangOptions::SOB_Undefined:
2317 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2320 case LangOptions::SOB_Trapping:
2328 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, ops.fpFeatures);
2336 if (cir::isFPOrVectorOrMatrixOfFPType(lhs.getType()))
2338 return builder.
createMul(loc, lhs, rhs);
2342 cgf.
sanOpts.
has(SanitizerKind::UnsignedIntegerOverflow) &&
2344 cgf.
cgm.
errorNYI(
"unsigned int overflow sanitizer");
2346 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
2347 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ops.fpFeatures);
2348 return builder.
createFMul(loc, ops.lhs, ops.rhs);
2351 if (ops.isFixedPointOp())
2352 return emitFixedPointBinOp(ops);
2354 return cir::MulOp::create(builder, cgf.
getLoc(ops.loc),
2357mlir::Value ScalarExprEmitter::emitDiv(
const BinOpInfo &ops) {
2358 const mlir::Location loc = cgf.
getLoc(ops.loc);
2359 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
2360 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ops.fpFeatures);
2361 return builder.
createFDiv(loc, ops.lhs, ops.rhs);
2365 assert(mlir::isa<cir::MatrixType>(ops.lhs.getType()) &&
2366 "first operand must be a matrix");
2367 assert(cir::isAnyIntegerOrFloatingPointType(ops.rhs.getType()) &&
2368 "second operand must be an arithmetic type");
2373 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, ops.fpFeatures);
2374 if (cir::isFPOrVectorOrMatrixOfFPType(lhs.getType()))
2376 return builder.
createDiv(loc, lhs, rhs);
2379 if (ops.isFixedPointOp())
2380 return emitFixedPointBinOp(ops);
2382 return cir::DivOp::create(builder, loc, cgf.
convertType(ops.fullType),
2385mlir::Value ScalarExprEmitter::emitRem(
const BinOpInfo &ops) {
2386 const mlir::Location loc = cgf.
getLoc(ops.loc);
2387 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
2388 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ops.fpFeatures);
2389 return builder.
createFRem(loc, ops.lhs, ops.rhs);
2391 return cir::RemOp::create(builder, loc, cgf.
convertType(ops.fullType),
2395mlir::Value ScalarExprEmitter::emitFixedPointBinOp(
const BinOpInfo &ops) {
2400 QualType resultTy = ops.compType;
2401 QualType lhsTy, rhsTy;
2402 if (
const auto *binOp = dyn_cast<BinaryOperator>(ops.e)) {
2403 rhsTy = binOp->getRHS()->getType();
2404 if (
const auto *cao = dyn_cast<CompoundAssignOperator>(binOp)) {
2409 lhsTy = cao->getComputationLHSType();
2410 resultTy = cao->getComputationResultType();
2412 lhsTy = binOp->getLHS()->getType();
2414 }
else if (
const auto *unOp = dyn_cast<UnaryOperator>(ops.e)) {
2415 lhsTy = unOp->getSubExpr()->getType();
2416 rhsTy = unOp->getSubExpr()->getType();
2419 mlir::Value lhs = ops.lhs;
2420 mlir::Value rhs = ops.rhs;
2425 auto commonFixedSema = lhsFixedSema.getCommonSemantics(rhsFixedSema);
2429 CIRGenFixedPointBuilder fpbuilder(builder, cgf.
getLoc(ops.loc));
2430 switch (ops.opcode) {
2433 result = fpbuilder.createAdd(lhs, lhsFixedSema, rhs, rhsFixedSema);
2437 result = fpbuilder.createSub(lhs, lhsFixedSema, rhs, rhsFixedSema);
2441 result = fpbuilder.createMul(lhs, lhsFixedSema, rhs, rhsFixedSema);
2445 result = fpbuilder.createDiv(lhs, lhsFixedSema, rhs, rhsFixedSema);
2449 result = fpbuilder.createShl(lhs, lhsFixedSema, rhs);
2453 result = fpbuilder.createShr(lhs, rhs);
2461 return fpbuilder.createCmp(lhs, lhsFixedSema, rhs, rhsFixedSema,
2462 clangCmpToCIRCmp(ops.opcode));
2466 llvm_unreachable(
"Found unimplemented fixed point binary operation");
2480 "Found unsupported binary operation for fixed point types.");
2486 return fpbuilder.createFixedToFixed(
2487 result, isShift ? lhsFixedSema : commonFixedSema, resultFixedSema);
2490mlir::Value ScalarExprEmitter::emitAdd(
const BinOpInfo &ops) {
2491 if (mlir::isa<cir::PointerType>(ops.lhs.getType()) ||
2492 mlir::isa<cir::PointerType>(ops.rhs.getType()))
2496 const mlir::Location loc = cgf.
getLoc(ops.loc);
2499 switch (cgf.
getLangOpts().getSignedOverflowBehavior()) {
2500 case LangOptions::SOB_Defined:
2501 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2502 return builder.
createAdd(loc, ops.lhs, ops.rhs);
2504 case LangOptions::SOB_Undefined:
2505 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2508 case LangOptions::SOB_Trapping:
2520 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, ops.fpFeatures);
2521 if (cir::isFPOrVectorOrMatrixOfFPType(lhs.getType()))
2523 return builder.
createAdd(loc, lhs, rhs);
2527 cgf.
sanOpts.
has(SanitizerKind::UnsignedIntegerOverflow) &&
2529 cgf.
cgm.
errorNYI(
"unsigned int overflow sanitizer");
2531 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
2532 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ops.fpFeatures);
2536 return builder.
createFAdd(loc, ops.lhs, ops.rhs);
2539 if (ops.isFixedPointOp())
2540 return emitFixedPointBinOp(ops);
2542 return builder.
createAdd(loc, ops.lhs, ops.rhs);
2545mlir::Value ScalarExprEmitter::emitSub(
const BinOpInfo &ops) {
2546 const mlir::Location loc = cgf.
getLoc(ops.loc);
2548 if (!mlir::isa<cir::PointerType>(ops.lhs.getType())) {
2551 switch (cgf.
getLangOpts().getSignedOverflowBehavior()) {
2552 case LangOptions::SOB_Defined: {
2553 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2554 return builder.
createSub(loc, ops.lhs, ops.rhs);
2557 case LangOptions::SOB_Undefined:
2558 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
2561 case LangOptions::SOB_Trapping:
2574 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, ops.fpFeatures);
2575 if (cir::isFPOrVectorOrMatrixOfFPType(lhs.getType()))
2577 return builder.
createSub(loc, lhs, rhs);
2581 cgf.
sanOpts.
has(SanitizerKind::UnsignedIntegerOverflow) &&
2583 cgf.
cgm.
errorNYI(
"unsigned int overflow sanitizer");
2585 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
2586 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ops.fpFeatures);
2588 if (mlir::Value fmuladd =
2591 return builder.
createFSub(loc, ops.lhs, ops.rhs);
2594 if (ops.isFixedPointOp())
2595 return emitFixedPointBinOp(ops);
2597 return builder.
createSub(loc, ops.lhs, ops.rhs);
2602 if (!mlir::isa<cir::PointerType>(ops.rhs.getType()))
2614 return cir::PtrDiffOp::create(builder, cgf.
getLoc(ops.loc), cgf.
ptrDiffTy,
2618mlir::Value ScalarExprEmitter::emitShl(
const BinOpInfo &ops) {
2620 if (ops.isFixedPointOp())
2621 return emitFixedPointBinOp(ops);
2627 bool sanitizeSignedBase = cgf.
sanOpts.
has(SanitizerKind::ShiftBase) &&
2631 bool sanitizeUnsignedBase =
2632 cgf.
sanOpts.
has(SanitizerKind::UnsignedShiftBase) &&
2634 bool sanitizeBase = sanitizeSignedBase || sanitizeUnsignedBase;
2635 bool sanitizeExponent = cgf.
sanOpts.
has(SanitizerKind::ShiftExponent);
2640 else if ((sanitizeBase || sanitizeExponent) &&
2641 mlir::isa<cir::IntType>(ops.lhs.getType()))
2647mlir::Value ScalarExprEmitter::emitShr(
const BinOpInfo &ops) {
2649 if (ops.isFixedPointOp())
2650 return emitFixedPointBinOp(ops);
2659 else if (cgf.
sanOpts.
has(SanitizerKind::ShiftExponent) &&
2660 mlir::isa<cir::IntType>(ops.lhs.getType()))
2668mlir::Value ScalarExprEmitter::emitAnd(
const BinOpInfo &ops) {
2669 return cir::AndOp::create(builder, cgf.
getLoc(ops.loc), ops.lhs, ops.rhs);
2671mlir::Value ScalarExprEmitter::emitXor(
const BinOpInfo &ops) {
2672 return cir::XorOp::create(builder, cgf.
getLoc(ops.loc), ops.lhs, ops.rhs);
2674mlir::Value ScalarExprEmitter::emitOr(
const BinOpInfo &ops) {
2675 return cir::OrOp::create(builder, cgf.
getLoc(ops.loc), ops.lhs, ops.rhs);
2682mlir::Value ScalarExprEmitter::VisitCastExpr(
CastExpr *ce) {
2684 QualType destTy = ce->
getType();
2686 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ce);
2690 ignoreResultAssign =
false;
2693 case clang::CK_Dependent:
2694 llvm_unreachable(
"dependent cast kind in CIR gen!");
2695 case clang::CK_BuiltinFnToFnPtr:
2696 llvm_unreachable(
"builtin functions are handled elsewhere");
2697 case CK_LValueBitCast:
2698 case CK_LValueToRValueBitCast: {
2700 Address sourceAddr = sourceLVal.getAddress();
2706 return emitLoadOfLValue(destLVal, ce->
getExprLoc());
2709 case CK_CPointerToObjCPointerCast:
2710 case CK_BlockPointerToObjCPointerCast:
2711 case CK_AnyPointerToBlockPointerCast:
2713 mlir::Value src = Visit(
const_cast<Expr *
>(subExpr));
2717 auto srcPtrTy = mlir::dyn_cast<cir::PointerType>(src.getType());
2718 auto dstPtrTy = mlir::dyn_cast<cir::PointerType>(dstTy);
2719 if (srcPtrTy && dstPtrTy &&
2720 srcPtrTy.getAddrSpace() != dstPtrTy.getAddrSpace())
2723 builder.
getPointerTo(srcPtrTy.getPointee(), dstPtrTy.getAddrSpace()));
2725 if (cgf.
sanOpts.
has(SanitizerKind::CFIUnrelatedCast))
2727 "sanitizer support");
2731 "strict vtable pointers");
2757 case CK_AddressSpaceConversion: {
2758 Expr::EvalResult result;
2767 mlir::cast<cir::PointerType>(convertType(destTy)), destTy,
2773 case CK_AtomicToNonAtomic:
2774 case CK_NonAtomicToAtomic:
2775 case CK_UserDefinedConversion:
2776 return Visit(
const_cast<Expr *
>(subExpr));
2780 case CK_IntegralToPointer: {
2782 mlir::Value src = Visit(
const_cast<Expr *
>(subExpr));
2791 : cir::CastKind::integral,
2796 "IntegralToPointer: strict vtable pointers");
2803 case CK_BaseToDerived: {
2805 assert(derivedClassDecl &&
"BaseToDerived arg isn't a C++ object pointer!");
2817 case CK_UncheckedDerivedToBase:
2818 case CK_DerivedToBase: {
2829 case CK_ArrayToPointerDecay:
2832 case CK_NullToPointer: {
2838 auto ty = mlir::cast<cir::PointerType>(cgf.
convertType(destTy));
2843 case CK_NullToMemberPointer: {
2849 const MemberPointerType *mpt = ce->
getType()->
getAs<MemberPointerType>();
2855 case CK_ReinterpretMemberPointer: {
2856 mlir::Value src = Visit(subExpr);
2860 case CK_BaseToDerivedMemberPointer:
2861 case CK_DerivedToBaseMemberPointer: {
2862 mlir::Value src = Visit(subExpr);
2866 QualType derivedTy =
2867 kind == CK_DerivedToBaseMemberPointer ? subExpr->
getType() : destTy;
2868 const auto *mpType = derivedTy->
castAs<MemberPointerType>();
2869 NestedNameSpecifier qualifier = mpType->getQualifier();
2870 assert(qualifier &&
"member pointer without class qualifier");
2871 const Type *qualifierType = qualifier.getAsType();
2872 assert(qualifierType &&
"member pointer qualifier is not a type");
2879 mlir::IntegerAttr offsetAttr = builder.getIndexAttr(offset.
getQuantity());
2882 if (
kind == CK_BaseToDerivedMemberPointer)
2883 return cir::DerivedMethodOp::create(builder, loc, resultTy, src,
2885 return cir::BaseMethodOp::create(builder, loc, resultTy, src, offsetAttr);
2888 if (
kind == CK_BaseToDerivedMemberPointer)
2889 return cir::DerivedDataMemberOp::create(builder, loc, resultTy, src,
2891 return cir::BaseDataMemberOp::create(builder, loc, resultTy, src,
2895 case CK_LValueToRValue:
2897 assert(subExpr->
isGLValue() &&
"lvalue-to-rvalue applied to r-value!");
2898 return Visit(
const_cast<Expr *
>(subExpr));
2900 case CK_IntegralCast: {
2901 ScalarConversionOpts opts;
2902 if (
auto *ice = dyn_cast<ImplicitCastExpr>(ce)) {
2903 if (!ice->isPartOfExplicitCast())
2904 opts = ScalarConversionOpts(cgf.
sanOpts);
2906 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2910 case CK_FloatingComplexToReal:
2911 case CK_IntegralComplexToReal:
2912 case CK_FloatingComplexToBoolean:
2913 case CK_IntegralComplexToBoolean: {
2919 case CK_FloatingRealToComplex:
2920 case CK_FloatingComplexCast:
2921 case CK_IntegralRealToComplex:
2922 case CK_IntegralComplexCast:
2923 case CK_IntegralComplexToFloatingComplex:
2924 case CK_FloatingComplexToIntegralComplex:
2925 llvm_unreachable(
"scalar cast to non-scalar value");
2927 case CK_PointerToIntegral: {
2928 assert(!destTy->
isBooleanType() &&
"bool should use PointerToBool");
2931 "strict vtable pointers");
2938 case CK_FixedPointCast:
2939 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2942 case CK_FixedPointToBoolean:
2944 "Expected src type to be fixed point type");
2945 assert(destTy->
isBooleanType() &&
"Expected dest type to be boolean type");
2946 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2949 case CK_FixedPointToIntegral:
2951 "Expected src type to be fixed point type");
2952 assert(destTy->
isIntegerType() &&
"Expected dest type to be an integer");
2953 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2956 case CK_IntegralToFixedPoint:
2958 "Expected src type to be an integer");
2960 "Expected dest type to be fixed point type");
2961 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2964 case CK_IntegralToFloating:
2965 case CK_FloatingToIntegral:
2966 case CK_FloatingCast:
2967 case CK_FixedPointToFloating:
2968 case CK_FloatingToFixedPoint: {
2969 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, ce);
2970 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
2974 case CK_IntegralToBoolean:
2975 return emitIntToBoolConversion(Visit(subExpr),
2978 case CK_PointerToBoolean:
2979 return emitPointerToBoolConversion(Visit(subExpr), subExpr->
getType());
2980 case CK_FloatingToBoolean:
2981 return emitFloatToBoolConversion(Visit(subExpr),
2983 case CK_MemberPointerToBoolean: {
2984 mlir::Value memPtr = Visit(subExpr);
2986 cir::CastKind::member_ptr_to_bool, memPtr,
2990 case CK_HLSLAggregateSplatCast:
2991 case CK_VectorSplat: {
2994 return cir::VecSplatOp::create(builder,
2996 destCIRTy, Visit(subExpr));
2998 case CK_FunctionToPointerDecay:
3001 case CK_HLSLVectorTruncation: {
3003 "Destination type must be a vector or builtin type.");
3004 mlir::Value vec = Visit(subExpr);
3006 if (
auto *vecTy = destTy->
getAs<VectorType>()) {
3007 unsigned numElts = vecTy->getNumElements();
3008 llvm::SmallVector<int64_t, 4> mask(numElts);
3009 std::iota(mask.begin(), mask.end(), 0);
3013 return cir::VecExtractOp::create(builder, loc, vec, idx);
3016 case CK_HLSLMatrixTruncation: {
3018 "Destination type must be a matrix or builtin type.");
3019 mlir::Value matrix = Visit(subExpr);
3021 if (
const auto *matTy = destTy->
getAs<ConstantMatrixType>()) {
3022 llvm::SmallVector<int64_t> mask(matTy->getNumElementsFlattened());
3023 unsigned numCols = matTy->getNumColumns();
3024 unsigned numRows = matTy->getNumRows();
3026 auto *srcMatTy = subExpr->
getType()->
getAs<ConstantMatrixType>();
3027 assert(srcMatTy &&
"Source type must be a matrix type.");
3028 assert(numRows <= srcMatTy->getNumRows());
3029 assert(numCols <= srcMatTy->getNumColumns());
3031 for (
unsigned row = 0; row < numRows; row++)
3032 for (
unsigned column = 0; column < numCols; column++)
3033 mask[matTy->getColumnMajorFlattenedIndex(row, column)] =
3034 srcMatTy->getColumnMajorFlattenedIndex(row, column);
3037 mlir::cast<cir::MatrixType>(matrix.getType()).getElementType();
3038 auto resultTy = cir::MatrixType::get(builder.getContext(), elementTy,
3044 "ScalarExprEmitter::VisitCastExpr: HLSL "
3045 "Matrix truncation with non-matrix dest");
3055mlir::Value ScalarExprEmitter::VisitCallExpr(
const CallExpr *e) {
3057 return emitLoadOfLValue(e);
3064mlir::Value ScalarExprEmitter::VisitMemberExpr(MemberExpr *e) {
3069 Expr::EvalResult result;
3071 llvm::APSInt value = result.
Val.
getInt();
3081 return builder.
getBool(value.getBoolValue(), loc);
3086 return emitLoadOfLValue(e);
3090ScalarExprEmitter::emitVectorInitWithSubvectors(mlir::Location loc,
3091 cir::VectorType vectorType,
3092 ArrayRef<mlir::Value>
inits) {
3093 const unsigned resElts = vectorType.getFixedSize();
3094 mlir::Value result =
3095 builder.
getConstant(loc, cir::PoisonAttr::get(vectorType));
3096 unsigned curIdx = 0;
3097 for (mlir::Value init :
inits) {
3098 auto initType = mlir::dyn_cast<cir::VectorType>(init.getType());
3104 const unsigned initElts = initType.getFixedSize();
3105 assert(curIdx + initElts <= resElts &&
3106 "Sema should reject excess elements");
3107 SmallVector<int64_t, 16> mask(resElts, -1);
3108 std::iota(mask.begin(), mask.begin() + initElts, 0);
3112 const unsigned offset = curIdx == 0 ? 0 : resElts;
3113 std::iota(mask.begin(), mask.begin() + curIdx, 0);
3114 std::iota(mask.begin() + curIdx, mask.begin() + curIdx + initElts, offset);
3116 std::swap(result, ext);
3121 if (curIdx < resElts) {
3122 const mlir::Value zeroValue =
3123 builder.
getNullValue(vectorType.getElementType(), loc);
3124 for (; curIdx < resElts; ++curIdx)
3130mlir::Value ScalarExprEmitter::VisitInitListExpr(InitListExpr *e) {
3131 const unsigned numInitElements = e->
getNumInits();
3133 [[maybe_unused]]
const bool ignore = std::exchange(ignoreResultAssign,
false);
3134 assert((ignore ==
false ||
3136 "init list ignored");
3144 const auto vectorType =
3147 SmallVector<mlir::Value, 16> elements;
3148 bool hasVectorInit =
false;
3149 for (Expr *init : e->
inits()) {
3150 elements.push_back(Visit(init));
3151 hasVectorInit |= mlir::isa<cir::VectorType>(elements.back().getType());
3156 vectorType, elements);
3159 const uint64_t vectorSize = vectorType.getFixedSize();
3160 if (numInitElements < vectorSize) {
3163 std::fill_n(std::back_inserter(elements), vectorSize - numInitElements,
3167 return cir::VecCreateOp::create(cgf.
getBuilder(),
3178 if (numInitElements == 0)
3189 "Invalid scalar expression to emit");
3190 return ScalarExprEmitter(*
this, builder)
3191 .emitScalarConversion(src, srcTy, dstTy, loc);
3199 "Invalid complex -> scalar conversion");
3204 ? cir::CastKind::float_complex_to_bool
3205 : cir::CastKind::int_complex_to_bool;
3210 ? cir::CastKind::float_complex_to_real
3211 : cir::CastKind::int_complex_to_real;
3217mlir::Value ScalarExprEmitter::VisitUnaryLNot(
const UnaryOperator *e) {
3224 auto operVecTy = mlir::cast<cir::VectorType>(oper.getType());
3225 mlir::Value zeroVec = builder.
getNullValue(operVecTy, loc);
3227 if (cir::isFPOrVectorOfFPType(operVecTy.getElementType())) {
3247mlir::Value ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *e) {
3249 Expr::EvalResult evalResult;
3252 llvm::APSInt value = evalResult.
Val.
getInt();
3258 "ScalarExprEmitter::VisitOffsetOfExpr Can't eval expr as int");
3262mlir::Value ScalarExprEmitter::VisitUnaryReal(
const UnaryOperator *e) {
3264 mlir::Value result = VisitRealImag(e, promotionTy);
3265 if (result && !promotionTy.
isNull())
3266 result = emitUnPromotedValue(result, e->
getType());
3270mlir::Value ScalarExprEmitter::VisitUnaryImag(
const UnaryOperator *e) {
3272 mlir::Value result = VisitRealImag(e, promotionTy);
3273 if (result && !promotionTy.
isNull())
3274 result = emitUnPromotedValue(result, e->
getType());
3278mlir::Value ScalarExprEmitter::VisitRealImag(
const UnaryOperator *e,
3279 QualType promotionTy) {
3282 "Invalid UnaryOp kind for ComplexType Real or Imag");
3304 mlir::Value operand = promotionTy.
isNull()
3306 : cgf.emitPromotedScalarExpr(op, promotionTy);
3312 mlir::Value operand;
3315 operand = cir::LoadOp::create(builder, loc, operand);
3316 }
else if (!promotionTy.
isNull()) {
3326mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
3327 const UnaryExprOrTypeTraitExpr *e) {
3331 kind == UETT_SizeOf ||
kind == UETT_DataSizeOf ||
kind == UETT_CountOf) {
3332 if (
const VariableArrayType *vat =
3337 bool evaluateExtent =
true;
3338 if (
kind == UETT_CountOf && vat->getElementType()->isArrayType()) {
3340 !vat->getSizeExpr()->isIntegerConstantExpr(cgf.
getContext());
3343 if (evaluateExtent) {
3354 if (
kind == UETT_CountOf)
3359 CIRGenFunction::VlaSizePair vlaSize = cgf.
getVLASize(vat);
3360 mlir::Value numElts = vlaSize.
numElts;
3364 if (!eltSize.
isOne()) {
3366 mlir::Value eltSizeValue =
3369 return builder.
createMul(loc, eltSizeValue, numElts,
3370 cir::OverflowFlags::NoUnsignedWrap);
3376 }
else if (e->
getKind() == UETT_OpenMPRequiredSimdAlign) {
3383 }
else if (e->
getKind() == UETT_VectorElements) {
3385 if (vecTy.isScalable()) {
3388 "VisitUnaryExprOrTypeTraitExpr: sizeOf scalable vector");
3395 loc, cir::IntAttr::get(cgf.
cgm.
sizeTy, vecTy.getFixedSize()));
3405mlir::Value ScalarExprEmitter::VisitAbstractConditionalOperator(
3406 const AbstractConditionalOperator *e) {
3409 ignoreResultAssign =
false;
3412 CIRGenFunction::OpaqueValueMapping binding(cgf, e);
3414 Expr *condExpr = e->
getCond();
3422 Expr *live = lhsExpr, *dead = rhsExpr;
3424 std::swap(live, dead);
3430 mlir::Value result = Visit(live);
3437 loc, cir::PoisonAttr::get(builder.getContext(),
3445 QualType condType = condExpr->
getType();
3454 mlir::Value lhsValue = Visit(lhsExpr);
3455 mlir::Value rhsValue = Visit(rhsExpr);
3461 mlir::Value zeroVec = builder.
getNullValue(vecTy, loc);
3462 auto testMSB = cir::VecCmpOp::create(
3463 builder, loc, vecTy, cir::CmpOpKind::lt, condValue, zeroVec);
3465 mlir::Value tmp2 = builder.
createNot(tmp);
3468 mlir::Value rhsTmp = rhsValue;
3469 mlir::Value lhsTmp = lhsValue;
3470 bool wasCast =
false;
3471 if (rhsValue.getType() != vecTy) {
3477 mlir::Value tmp3 = builder.
createAnd(loc, rhsTmp, tmp2);
3478 mlir::Value tmp4 = builder.
createAnd(loc, lhsTmp, tmp);
3479 mlir::Value tmp5 = builder.
createOr(loc, tmp3, tmp4);
3488 cgf.
cgm.
errorNYI(loc,
"TernaryOp for SVE vector");
3492 mlir::Value condValue = Visit(condExpr);
3493 mlir::Value lhsValue = Visit(lhsExpr);
3494 mlir::Value rhsValue = Visit(rhsExpr);
3495 return cir::VecTernaryOp::create(builder, loc, condValue, lhsValue,
3509 mlir::Value lhs = Visit(lhsExpr);
3510 mlir::Value rhs = Visit(rhsExpr);
3513 assert(!rhs &&
"lhs and rhs types must match");
3521 CIRGenFunction::ConditionalEvaluation eval(cgf, loc);
3523 auto emitBranch = [&](mlir::OpBuilder &b, mlir::Location loc, Expr *
expr) {
3524 CIRGenFunction::LexicalScope lexScope{cgf, loc, b.getInsertionBlock()};
3531 CIRGenFunction::RunCleanupsScope branchCleanups(cgf);
3533 eval.beginEvaluation();
3534 branch = Visit(
expr);
3535 eval.endEvaluation();
3536 branchCleanups.forceCleanup({&branch});
3540 cir::YieldOp::create(b, loc, branch);
3543 cir::TernaryOp ternary = cir::TernaryOp::create(
3544 builder, loc, condV,
3546 [&](mlir::OpBuilder &b, mlir::Location loc) {
3547 emitBranch(b, loc, lhsExpr);
3550 [&](mlir::OpBuilder &b, mlir::Location loc) {
3551 emitBranch(b, loc, rhsExpr);
3556 for (mlir::Region *region :
3557 {&ternary.getTrueRegion(), &ternary.getFalseRegion()}) {
3558 mlir::Block &lastBlock = region->back();
3559 if (lastBlock.empty() ||
3560 !lastBlock.back().hasTrait<mlir::OpTrait::IsTerminator>()) {
3561 mlir::OpBuilder::InsertionGuard guard(builder);
3562 builder.setInsertionPointToEnd(&lastBlock);
3563 cir::YieldOp::create(builder, loc);
3567 return ternary.getResult();
3572 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)
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.
clang::CharUnits getAlignment() const
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.
APValue getAPValueResult() const
bool hasAPValueResult() const
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 uint32_t int64_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
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.