20#include "mlir/IR/Location.h"
21#include "mlir/IR/Value.h"
43 bool isDivRemOp()
const {
44 return opcode == BO_Div || opcode == BO_Rem || opcode == BO_DivAssign ||
45 opcode == BO_RemAssign;
49 bool mayHaveIntegerOverflow()
const {
51 auto lhsci = lhs.getDefiningOp<cir::ConstantOp>();
52 auto rhsci = rhs.getDefiningOp<cir::ConstantOp>();
64 bool isFixedPointOp()
const {
67 if (
const auto *binOp = llvm::dyn_cast<BinaryOperator>(e)) {
68 QualType lhstype = binOp->getLHS()->getType();
69 QualType rhstype = binOp->getRHS()->getType();
72 if (
const auto *unop = llvm::dyn_cast<UnaryOperator>(e))
73 return unop->getSubExpr()->getType()->isFixedPointType();
78class ScalarExprEmitter :
public StmtVisitor<ScalarExprEmitter, mlir::Value> {
80 CIRGenBuilderTy &builder;
81 bool ignoreResultAssign;
84 ScalarExprEmitter(CIRGenFunction &cgf, CIRGenBuilderTy &builder)
85 : cgf(cgf), builder(builder) {}
91 mlir::Value emitComplexToScalarConversion(mlir::Location loc,
95 mlir::Value emitNullValue(QualType ty, mlir::Location loc) {
96 return cgf.cgm.emitNullConstant(ty, loc);
99 mlir::Value emitPromotedValue(mlir::Value result, QualType promotionType) {
100 return builder.createFloatingCast(result, cgf.convertType(promotionType));
103 mlir::Value emitUnPromotedValue(mlir::Value result, QualType exprType) {
104 return builder.createFloatingCast(result, cgf.convertType(exprType));
107 mlir::Value emitPromoted(
const Expr *e, QualType promotionType);
109 mlir::Value maybePromoteBoolResult(mlir::Value value,
110 mlir::Type dstTy)
const {
111 if (mlir::isa<cir::IntType>(dstTy))
112 return builder.createBoolToInt(value, dstTy);
113 if (mlir::isa<cir::BoolType>(dstTy))
115 llvm_unreachable(
"Can only promote integer or boolean types");
122 mlir::Value Visit(Expr *e) {
123 return StmtVisitor<ScalarExprEmitter, mlir::Value>::Visit(e);
126 mlir::Value VisitStmt(Stmt *
s) {
127 llvm_unreachable(
"Statement passed to ScalarExprEmitter");
130 mlir::Value VisitExpr(Expr *e) {
131 cgf.getCIRGenModule().errorNYI(
136 mlir::Value VisitPackIndexingExpr(PackIndexingExpr *e) {
140 mlir::Value VisitParenExpr(ParenExpr *pe) {
return Visit(pe->
getSubExpr()); }
142 mlir::Value VisitGenericSelectionExpr(GenericSelectionExpr *ge) {
147 mlir::Value emitLoadOfLValue(
const Expr *e) {
148 LValue lv = cgf.emitLValue(e);
150 return cgf.emitLoadOfLValue(lv, e->
getExprLoc()).getValue();
153 mlir::Value emitLoadOfLValue(LValue lv, SourceLocation loc) {
154 return cgf.emitLoadOfLValue(lv, loc).getValue();
158 mlir::Value VisitDeclRefExpr(DeclRefExpr *e) {
159 if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(e))
160 return cgf.emitScalarConstant(constant, e);
162 return emitLoadOfLValue(e);
165 mlir::Value VisitIntegerLiteral(
const IntegerLiteral *e) {
167 return builder.create<cir::ConstantOp>(
171 mlir::Value VisitFloatingLiteral(
const FloatingLiteral *e) {
173 assert(mlir::isa<cir::FPTypeInterface>(
type) &&
174 "expect floating-point type");
175 return builder.create<cir::ConstantOp>(
179 mlir::Value VisitCharacterLiteral(
const CharacterLiteral *e) {
180 mlir::Type ty = cgf.convertType(e->
getType());
181 auto init = cir::IntAttr::get(ty, e->
getValue());
182 return builder.create<cir::ConstantOp>(cgf.getLoc(e->
getExprLoc()), init);
185 mlir::Value VisitCXXBoolLiteralExpr(
const CXXBoolLiteralExpr *e) {
189 mlir::Value VisitCXXScalarValueInitExpr(
const CXXScalarValueInitExpr *e) {
196 mlir::Value VisitOpaqueValueExpr(OpaqueValueExpr *e) {
198 return emitLoadOfLValue(cgf.getOrCreateOpaqueLValueMapping(e),
202 return cgf.getOrCreateOpaqueRValueMapping(e).getValue();
205 mlir::Value VisitCastExpr(
CastExpr *e);
206 mlir::Value VisitCallExpr(
const CallExpr *e);
208 mlir::Value VisitStmtExpr(StmtExpr *e) {
209 CIRGenFunction::StmtExprEvaluation eval(cgf);
217 (void)cgf.emitCompoundStmt(*e->
getSubStmt(), &retAlloca);
219 return cgf.emitLoadOfScalar(cgf.makeAddrLValue(retAlloca, e->
getType()),
223 mlir::Value VisitArraySubscriptExpr(ArraySubscriptExpr *e) {
228 const mlir::Value vecValue = Visit(e->
getBase());
229 const mlir::Value indexValue = Visit(e->
getIdx());
230 return cgf.builder.create<cir::VecExtractOp>(loc, vecValue, indexValue);
233 return emitLoadOfLValue(e);
236 mlir::Value VisitShuffleVectorExpr(ShuffleVectorExpr *e) {
239 mlir::Value inputVec = Visit(e->
getExpr(0));
240 mlir::Value indexVec = Visit(e->
getExpr(1));
241 return cgf.builder.create<cir::VecShuffleDynamicOp>(
245 mlir::Value vec1 = Visit(e->
getExpr(0));
246 mlir::Value vec2 = Visit(e->
getExpr(1));
251 SmallVector<mlir::Attribute, 8> indices;
254 cir::IntAttr::get(cgf.builder.getSInt64Ty(),
260 return cgf.builder.create<cir::VecShuffleOp>(
262 vec2, cgf.builder.getArrayAttr(indices));
265 mlir::Value VisitConvertVectorExpr(ConvertVectorExpr *e) {
268 return emitScalarConversion(Visit(e->
getSrcExpr()),
273 mlir::Value VisitMemberExpr(MemberExpr *e);
275 mlir::Value VisitCompoundLiteralExpr(CompoundLiteralExpr *e) {
276 return emitLoadOfLValue(e);
279 mlir::Value VisitInitListExpr(InitListExpr *e);
281 mlir::Value VisitExplicitCastExpr(ExplicitCastExpr *e) {
282 return VisitCastExpr(e);
285 mlir::Value VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *e) {
286 return cgf.cgm.emitNullConstant(e->
getType(),
291 mlir::Value emitPointerToBoolConversion(mlir::Value v, QualType qt) {
294 return cgf.getBuilder().createPtrToBoolCast(v);
297 mlir::Value emitFloatToBoolConversion(mlir::Value src, mlir::Location loc) {
298 cir::BoolType boolTy = builder.getBoolTy();
299 return builder.create<cir::CastOp>(loc, boolTy,
300 cir::CastKind::float_to_bool, src);
303 mlir::Value emitIntToBoolConversion(mlir::Value srcVal, mlir::Location loc) {
309 cir::BoolType boolTy = builder.getBoolTy();
310 return builder.create<cir::CastOp>(loc, boolTy, cir::CastKind::int_to_bool,
316 mlir::Value emitConversionToBool(mlir::Value src, QualType srcType,
317 mlir::Location loc) {
318 assert(srcType.
isCanonical() &&
"EmitScalarConversion strips typedefs");
321 return emitFloatToBoolConversion(src, loc);
323 if (llvm::isa<MemberPointerType>(srcType)) {
324 cgf.getCIRGenModule().errorNYI(loc,
"member pointer to bool conversion");
325 return builder.getFalse(loc);
329 return emitIntToBoolConversion(src, loc);
331 assert(::mlir::isa<cir::PointerType>(src.getType()));
332 return emitPointerToBoolConversion(src, srcType);
337 struct ScalarConversionOpts {
338 bool treatBooleanAsSigned;
339 bool emitImplicitIntegerTruncationChecks;
340 bool emitImplicitIntegerSignChangeChecks;
342 ScalarConversionOpts()
343 : treatBooleanAsSigned(
false),
344 emitImplicitIntegerTruncationChecks(
false),
345 emitImplicitIntegerSignChangeChecks(
false) {}
347 ScalarConversionOpts(clang::SanitizerSet sanOpts)
348 : treatBooleanAsSigned(
false),
349 emitImplicitIntegerTruncationChecks(
350 sanOpts.hasOneOf(SanitizerKind::ImplicitIntegerTruncation)),
351 emitImplicitIntegerSignChangeChecks(
352 sanOpts.
has(SanitizerKind::ImplicitIntegerSignChange)) {}
359 mlir::Value emitScalarCast(mlir::Value src, QualType srcType,
360 QualType dstType, mlir::Type srcTy,
361 mlir::Type dstTy, ScalarConversionOpts opts) {
363 "Internal error: matrix types not handled by this function.");
364 assert(!(mlir::isa<mlir::IntegerType>(srcTy) ||
365 mlir::isa<mlir::IntegerType>(dstTy)) &&
366 "Obsolete code. Don't use mlir::IntegerType with CIR.");
368 mlir::Type fullDstTy = dstTy;
369 if (mlir::isa<cir::VectorType>(srcTy) &&
370 mlir::isa<cir::VectorType>(dstTy)) {
372 srcTy = mlir::dyn_cast<cir::VectorType>(srcTy).getElementType();
373 dstTy = mlir::dyn_cast<cir::VectorType>(dstTy).getElementType();
376 std::optional<cir::CastKind> castKind;
378 if (mlir::isa<cir::BoolType>(srcTy)) {
379 if (opts.treatBooleanAsSigned)
380 cgf.getCIRGenModule().errorNYI(
"signed bool");
381 if (cgf.getBuilder().isInt(dstTy))
382 castKind = cir::CastKind::bool_to_int;
383 else if (mlir::isa<cir::FPTypeInterface>(dstTy))
384 castKind = cir::CastKind::bool_to_float;
386 llvm_unreachable(
"Internal error: Cast to unexpected type");
387 }
else if (cgf.getBuilder().isInt(srcTy)) {
388 if (cgf.getBuilder().isInt(dstTy))
389 castKind = cir::CastKind::integral;
390 else if (mlir::isa<cir::FPTypeInterface>(dstTy))
391 castKind = cir::CastKind::int_to_float;
393 llvm_unreachable(
"Internal error: Cast to unexpected type");
394 }
else if (mlir::isa<cir::FPTypeInterface>(srcTy)) {
395 if (cgf.getBuilder().isInt(dstTy)) {
399 if (!cgf.cgm.getCodeGenOpts().StrictFloatCastOverflow)
400 cgf.getCIRGenModule().errorNYI(
"strict float cast overflow");
402 castKind = cir::CastKind::float_to_int;
403 }
else if (mlir::isa<cir::FPTypeInterface>(dstTy)) {
405 return builder.createFloatingCast(src, fullDstTy);
407 llvm_unreachable(
"Internal error: Cast to unexpected type");
410 llvm_unreachable(
"Internal error: Cast from unexpected type");
413 assert(castKind.has_value() &&
"Internal error: CastKind not set.");
414 return builder.create<cir::CastOp>(src.getLoc(), fullDstTy, *castKind, src);
418 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *e) {
422 mlir::Value VisitVAArgExpr(VAArgExpr *ve) {
427 "variably modified types in varargs");
430 return cgf.emitVAArg(ve);
433 mlir::Value VisitUnaryExprOrTypeTraitExpr(
const UnaryExprOrTypeTraitExpr *e);
435 VisitAbstractConditionalOperator(
const AbstractConditionalOperator *e);
438 mlir::Value VisitUnaryPostDec(
const UnaryOperator *e) {
440 return emitScalarPrePostIncDec(e, lv, cir::UnaryOpKind::Dec,
false);
442 mlir::Value VisitUnaryPostInc(
const UnaryOperator *e) {
444 return emitScalarPrePostIncDec(e, lv, cir::UnaryOpKind::Inc,
false);
446 mlir::Value VisitUnaryPreDec(
const UnaryOperator *e) {
448 return emitScalarPrePostIncDec(e, lv, cir::UnaryOpKind::Dec,
true);
450 mlir::Value VisitUnaryPreInc(
const UnaryOperator *e) {
452 return emitScalarPrePostIncDec(e, lv, cir::UnaryOpKind::Inc,
true);
454 mlir::Value emitScalarPrePostIncDec(
const UnaryOperator *e, LValue lv,
455 cir::UnaryOpKind kind,
bool isPre) {
456 if (cgf.getLangOpts().OpenMP)
464 if (
type->getAs<AtomicType>()) {
468 value = cgf.emitLoadOfLValue(lv, e->
getExprLoc()).getValue();
471 value = cgf.emitLoadOfLValue(lv, e->
getExprLoc()).getValue();
484 if (kind == cir::UnaryOpKind::Inc &&
type->isBooleanType()) {
485 value = builder.getTrue(cgf.getLoc(e->
getExprLoc()));
486 }
else if (
type->isIntegerType()) {
487 QualType promotedType;
488 [[maybe_unused]]
bool canPerformLossyDemotionCheck =
false;
489 if (cgf.getContext().isPromotableIntegerType(
type)) {
490 promotedType = cgf.getContext().getPromotedIntegerType(
type);
491 assert(promotedType !=
type &&
"Shouldn't promote to the same type.");
492 canPerformLossyDemotionCheck =
true;
493 canPerformLossyDemotionCheck &=
494 cgf.getContext().getCanonicalType(
type) !=
495 cgf.getContext().getCanonicalType(promotedType);
496 canPerformLossyDemotionCheck &=
503 (!canPerformLossyDemotionCheck ||
504 type->isSignedIntegerOrEnumerationType() ||
506 mlir::cast<cir::IntType>(cgf.convertType(
type)).getWidth() ==
507 mlir::cast<cir::IntType>(cgf.convertType(
type)).getWidth()) &&
508 "The following check expects that if we do promotion to different "
509 "underlying canonical type, at least one of the types (either "
510 "base or promoted) will be signed, or the bitwidths will match.");
515 value = emitIncDecConsiderOverflowBehavior(e, value, kind);
517 cir::UnaryOpKind
kind =
518 e->
isIncrementOp() ? cir::UnaryOpKind::Inc : cir::UnaryOpKind::Dec;
520 value = emitUnaryOp(e, kind, input,
false);
522 }
else if (
const PointerType *ptr =
type->getAs<PointerType>()) {
523 QualType
type = ptr->getPointeeType();
524 if (cgf.getContext().getAsVariableArrayType(
type)) {
526 cgf.cgm.errorNYI(e->
getSourceRange(),
"Pointer arithmetic on VLA");
528 }
else if (
type->isFunctionType()) {
531 "Pointer arithmetic on function pointer");
536 CIRGenBuilderTy &builder = cgf.getBuilder();
537 int amount =
kind == cir::UnaryOpKind::Inc ? 1 : -1;
538 mlir::Value amt = builder.getSInt32(amount, loc);
540 value = builder.createPtrStride(loc, value, amt);
542 }
else if (
type->isVectorType()) {
545 }
else if (
type->isRealFloatingType()) {
548 if (
type->isHalfType() &&
549 !cgf.getContext().getLangOpts().NativeHalfType) {
554 if (mlir::isa<cir::SingleType, cir::DoubleType>(value.getType())) {
557 assert(kind == cir::UnaryOpKind::Inc ||
558 kind == cir::UnaryOpKind::Dec &&
"Invalid UnaryOp kind");
559 value = emitUnaryOp(e, kind, value);
561 cgf.cgm.errorNYI(e->
getSourceRange(),
"Unary inc/dec other fp type");
564 }
else if (
type->isFixedPointType()) {
565 cgf.cgm.errorNYI(e->
getSourceRange(),
"Unary inc/dec other fixed point");
568 assert(
type->castAs<ObjCObjectPointerType>());
569 cgf.cgm.errorNYI(e->
getSourceRange(),
"Unary inc/dec ObjectiveC pointer");
573 CIRGenFunction::SourceLocRAIIObject sourceloc{
578 return cgf.emitStoreThroughBitfieldLValue(
RValue::get(value), lv);
580 cgf.emitStoreThroughLValue(
RValue::get(value), lv);
584 return isPre ? value : input;
587 mlir::Value emitIncDecConsiderOverflowBehavior(
const UnaryOperator *e,
589 cir::UnaryOpKind kind) {
590 assert(kind == cir::UnaryOpKind::Inc ||
591 kind == cir::UnaryOpKind::Dec &&
"Invalid UnaryOp kind");
592 switch (cgf.getLangOpts().getSignedOverflowBehavior()) {
593 case LangOptions::SOB_Defined:
594 return emitUnaryOp(e, kind, inVal,
false);
595 case LangOptions::SOB_Undefined:
597 return emitUnaryOp(e, kind, inVal,
true);
598 case LangOptions::SOB_Trapping:
600 return emitUnaryOp(e, kind, inVal,
true);
601 cgf.cgm.errorNYI(e->
getSourceRange(),
"inc/def overflow SOB_Trapping");
604 llvm_unreachable(
"Unexpected signed overflow behavior kind");
607 mlir::Value VisitUnaryAddrOf(
const UnaryOperator *e) {
608 if (llvm::isa<MemberPointerType>(e->
getType())) {
609 cgf.cgm.errorNYI(e->
getSourceRange(),
"Address of member pointer");
610 return builder.getNullPtr(cgf.convertType(e->
getType()),
614 return cgf.emitLValue(e->
getSubExpr()).getPointer();
617 mlir::Value VisitUnaryDeref(
const UnaryOperator *e) {
620 return emitLoadOfLValue(e);
623 mlir::Value VisitUnaryPlus(
const UnaryOperator *e) {
626 emitUnaryPlusOrMinus(e, cir::UnaryOpKind::Plus, promotionType);
627 if (result && !promotionType.
isNull())
628 return emitUnPromotedValue(result, e->
getType());
632 mlir::Value VisitUnaryMinus(
const UnaryOperator *e) {
635 emitUnaryPlusOrMinus(e, cir::UnaryOpKind::Minus, promotionType);
636 if (result && !promotionType.
isNull())
637 return emitUnPromotedValue(result, e->
getType());
641 mlir::Value emitUnaryPlusOrMinus(
const UnaryOperator *e,
642 cir::UnaryOpKind kind,
643 QualType promotionType) {
644 ignoreResultAssign =
false;
646 if (!promotionType.
isNull())
647 operand = cgf.emitPromotedScalarExpr(e->
getSubExpr(), promotionType);
656 return emitUnaryOp(e, kind, operand, nsw);
659 mlir::Value emitUnaryOp(
const UnaryOperator *e, cir::UnaryOpKind kind,
660 mlir::Value input,
bool nsw =
false) {
661 return builder.create<cir::UnaryOp>(
666 mlir::Value VisitUnaryNot(
const UnaryOperator *e) {
667 ignoreResultAssign =
false;
669 return emitUnaryOp(e, cir::UnaryOpKind::Not, op);
672 mlir::Value VisitUnaryLNot(
const UnaryOperator *e);
674 mlir::Value VisitUnaryReal(
const UnaryOperator *e);
675 mlir::Value VisitUnaryImag(
const UnaryOperator *e);
676 mlir::Value VisitRealImag(
const UnaryOperator *e,
677 QualType promotionType = QualType());
679 mlir::Value VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die) {
680 CIRGenFunction::CXXDefaultInitExprScope scope(cgf, die);
684 mlir::Value VisitCXXThisExpr(CXXThisExpr *te) {
return cgf.loadCXXThis(); }
686 mlir::Value VisitExprWithCleanups(ExprWithCleanups *e);
687 mlir::Value VisitCXXNewExpr(
const CXXNewExpr *e) {
688 return cgf.emitCXXNewExpr(e);
691 mlir::Value VisitCXXThrowExpr(
const CXXThrowExpr *e) {
692 cgf.emitCXXThrowExpr(e);
701 emitScalarConversion(mlir::Value src, QualType srcType, QualType dstType,
703 ScalarConversionOpts opts = ScalarConversionOpts()) {
713 cgf.getCIRGenModule().errorNYI(loc,
"fixed point conversions");
719 if (srcType == dstType) {
720 if (opts.emitImplicitIntegerSignChangeChecks)
721 cgf.getCIRGenModule().errorNYI(loc,
722 "implicit integer sign change checks");
729 mlir::Type mlirSrcType = src.getType();
734 return emitConversionToBool(src, srcType, cgf.getLoc(loc));
736 mlir::Type mlirDstType = cgf.convertType(dstType);
739 !cgf.getContext().getLangOpts().NativeHalfType) {
741 if (mlir::isa<cir::FPTypeInterface>(mlirDstType)) {
742 if (cgf.getContext().getTargetInfo().useFP16ConversionIntrinsics())
743 cgf.getCIRGenModule().errorNYI(loc,
744 "cast via llvm.convert.from.fp16");
749 if (cgf.getContext().getTargetInfo().useFP16ConversionIntrinsics())
750 cgf.getCIRGenModule().errorNYI(loc,
751 "cast via llvm.convert.from.fp16");
754 src = builder.createCast(cgf.getLoc(loc), cir::CastKind::floating, src,
756 srcType = cgf.getContext().FloatTy;
757 mlirSrcType = cgf.FloatTy;
763 if (mlirSrcType == mlirDstType) {
764 if (opts.emitImplicitIntegerSignChangeChecks)
765 cgf.getCIRGenModule().errorNYI(loc,
766 "implicit integer sign change checks");
773 if (
auto dstPT = dyn_cast<cir::PointerType>(mlirDstType)) {
774 cgf.getCIRGenModule().errorNYI(loc,
"pointer casts");
775 return builder.getNullPtr(dstPT, src.getLoc());
781 return builder.createPtrToInt(src, mlirDstType);
788 assert(dstType->
castAs<ExtVectorType>()->getElementType().getTypePtr() ==
790 "Splatted expr doesn't match with vector element type?");
792 cgf.getCIRGenModule().errorNYI(loc,
"vector splatting");
797 cgf.getCIRGenModule().errorNYI(loc,
798 "matrix type to matrix type conversion");
802 "Internal error: conversion between matrix type and scalar type");
805 mlir::Value res =
nullptr;
806 mlir::Type resTy = mlirDstType;
808 res = emitScalarCast(src, srcType, dstType, mlirSrcType, mlirDstType, opts);
810 if (mlirDstType != resTy) {
811 if (cgf.getContext().getTargetInfo().useFP16ConversionIntrinsics()) {
812 cgf.getCIRGenModule().errorNYI(loc,
"cast via llvm.convert.to.fp16");
816 res = builder.createCast(cgf.getLoc(loc), cir::CastKind::floating, res,
820 if (opts.emitImplicitIntegerTruncationChecks)
821 cgf.getCIRGenModule().errorNYI(loc,
"implicit integer truncation checks");
823 if (opts.emitImplicitIntegerSignChangeChecks)
824 cgf.getCIRGenModule().errorNYI(loc,
825 "implicit integer sign change checks");
830 BinOpInfo emitBinOps(
const BinaryOperator *e,
831 QualType promotionType = QualType()) {
833 result.lhs = cgf.emitPromotedScalarExpr(e->
getLHS(), promotionType);
834 result.rhs = cgf.emitPromotedScalarExpr(e->
getRHS(), promotionType);
835 if (!promotionType.
isNull())
836 result.fullType = promotionType;
838 result.fullType = e->
getType();
839 result.compType = result.fullType;
840 if (
const auto *vecType = dyn_cast_or_null<VectorType>(result.fullType)) {
841 result.compType = vecType->getElementType();
851 mlir::Value emitMul(
const BinOpInfo &ops);
852 mlir::Value emitDiv(
const BinOpInfo &ops);
853 mlir::Value emitRem(
const BinOpInfo &ops);
854 mlir::Value emitAdd(
const BinOpInfo &ops);
855 mlir::Value emitSub(
const BinOpInfo &ops);
856 mlir::Value emitShl(
const BinOpInfo &ops);
857 mlir::Value emitShr(
const BinOpInfo &ops);
858 mlir::Value emitAnd(
const BinOpInfo &ops);
859 mlir::Value emitXor(
const BinOpInfo &ops);
860 mlir::Value emitOr(
const BinOpInfo &ops);
862 LValue emitCompoundAssignLValue(
863 const CompoundAssignOperator *e,
864 mlir::Value (ScalarExprEmitter::*f)(
const BinOpInfo &),
865 mlir::Value &result);
867 emitCompoundAssign(
const CompoundAssignOperator *e,
868 mlir::Value (ScalarExprEmitter::*f)(
const BinOpInfo &));
872 QualType getPromotionType(QualType ty) {
873 const clang::ASTContext &ctx = cgf.getContext();
874 if (
auto *complexTy = ty->
getAs<ComplexType>()) {
875 QualType elementTy = complexTy->getElementType();
881 if (
auto *vt = ty->
getAs<VectorType>()) {
882 unsigned numElements = vt->getNumElements();
885 return cgf.getContext().FloatTy;
892#define HANDLEBINOP(OP) \
893 mlir::Value VisitBin##OP(const BinaryOperator *e) { \
894 QualType promotionTy = getPromotionType(e->getType()); \
895 auto result = emit##OP(emitBinOps(e, promotionTy)); \
896 if (result && !promotionTy.isNull()) \
897 result = emitUnPromotedValue(result, e->getType()); \
900 mlir::Value VisitBin##OP##Assign(const CompoundAssignOperator *e) { \
901 return emitCompoundAssign(e, &ScalarExprEmitter::emit##OP); \
922 auto clangCmpToCIRCmp =
926 return cir::CmpOpKind::lt;
928 return cir::CmpOpKind::gt;
930 return cir::CmpOpKind::le;
932 return cir::CmpOpKind::ge;
934 return cir::CmpOpKind::eq;
936 return cir::CmpOpKind::ne;
938 llvm_unreachable(
"unsupported comparison kind for cir.cmp");
942 cir::CmpOpKind kind = clangCmpToCIRCmp(e->
getOpcode());
950 BinOpInfo boInfo = emitBinOps(e);
951 mlir::Value lhs = boInfo.lhs;
952 mlir::Value rhs = boInfo.rhs;
962 result = builder.create<cir::VecCmpOp>(
964 boInfo.lhs, boInfo.rhs);
966 }
else if (boInfo.isFixedPointOp()) {
969 result = builder.
getBool(
false, loc);
973 mlir::isa<cir::PointerType>(lhs.getType()) &&
974 mlir::isa<cir::PointerType>(rhs.getType())) {
975 cgf.
cgm.
errorNYI(loc,
"strict vtable pointer comparisons");
978 cir::CmpOpKind kind = clangCmpToCIRCmp(e->
getOpcode());
985 BinOpInfo boInfo = emitBinOps(e);
986 result = builder.create<cir::CmpOp>(loc, kind, boInfo.lhs, boInfo.rhs);
994#define VISITCOMP(CODE) \
995 mlir::Value VisitBin##CODE(const BinaryOperator *E) { return emitCmp(E); }
1005 const bool ignore = std::exchange(ignoreResultAssign,
false);
1020 rhs = Visit(e->
getRHS());
1030 if (lhs.isBitField()) {
1050 if (!lhs.isVolatile())
1054 return emitLoadOfLValue(lhs, e->
getExprLoc());
1057 mlir::Value VisitBinComma(
const BinaryOperator *e) {
1058 cgf.emitIgnoredExpr(e->
getLHS());
1060 return Visit(e->
getRHS());
1063 mlir::Value VisitBinLAnd(
const clang::BinaryOperator *e) {
1065 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1066 auto vecTy = mlir::cast<cir::VectorType>(cgf.convertType(e->
getType()));
1067 mlir::Value zeroValue = builder.getNullValue(vecTy.getElementType(), loc);
1068 SmallVector<mlir::Value, 16> elements(vecTy.getSize(), zeroValue);
1069 auto zeroVec = cir::VecCreateOp::create(builder, loc, vecTy, elements);
1071 mlir::Value lhs = Visit(e->
getLHS());
1072 mlir::Value rhs = Visit(e->
getRHS());
1074 auto cmpOpKind = cir::CmpOpKind::ne;
1075 lhs = cir::VecCmpOp::create(builder, loc, vecTy, cmpOpKind, lhs, zeroVec);
1076 rhs = cir::VecCmpOp::create(builder, loc, vecTy, cmpOpKind, rhs, zeroVec);
1077 mlir::Value vecOr = builder.createAnd(loc, lhs, rhs);
1078 return builder.createIntCast(vecOr, vecTy);
1082 mlir::Type resTy = cgf.convertType(e->
getType());
1083 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1085 CIRGenFunction::ConditionalEvaluation eval(cgf);
1087 mlir::Value lhsCondV = cgf.evaluateExprAsBool(e->
getLHS());
1088 auto resOp = builder.create<cir::TernaryOp>(
1090 [&](mlir::OpBuilder &
b, mlir::Location loc) {
1091 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1092 b.getInsertionBlock()};
1093 cgf.curLexScope->setAsTernary();
1094 b.create<cir::YieldOp>(loc, cgf.evaluateExprAsBool(e->
getRHS()));
1097 [&](mlir::OpBuilder &
b, mlir::Location loc) {
1098 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1099 b.getInsertionBlock()};
1101 auto res =
b.create<cir::ConstantOp>(loc, builder.getFalseAttr());
1102 b.create<cir::YieldOp>(loc, res.getRes());
1104 return maybePromoteBoolResult(resOp.getResult(), resTy);
1107 mlir::Value VisitBinLOr(
const clang::BinaryOperator *e) {
1109 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1110 auto vecTy = mlir::cast<cir::VectorType>(cgf.convertType(e->
getType()));
1111 mlir::Value zeroValue = builder.getNullValue(vecTy.getElementType(), loc);
1112 SmallVector<mlir::Value, 16> elements(vecTy.getSize(), zeroValue);
1113 auto zeroVec = cir::VecCreateOp::create(builder, loc, vecTy, elements);
1115 mlir::Value lhs = Visit(e->
getLHS());
1116 mlir::Value rhs = Visit(e->
getRHS());
1118 auto cmpOpKind = cir::CmpOpKind::ne;
1119 lhs = cir::VecCmpOp::create(builder, loc, vecTy, cmpOpKind, lhs, zeroVec);
1120 rhs = cir::VecCmpOp::create(builder, loc, vecTy, cmpOpKind, rhs, zeroVec);
1121 mlir::Value vecOr = builder.createOr(loc, lhs, rhs);
1122 return builder.createIntCast(vecOr, vecTy);
1126 mlir::Type resTy = cgf.convertType(e->
getType());
1127 mlir::Location loc = cgf.getLoc(e->
getExprLoc());
1129 CIRGenFunction::ConditionalEvaluation eval(cgf);
1131 mlir::Value lhsCondV = cgf.evaluateExprAsBool(e->
getLHS());
1132 auto resOp = builder.create<cir::TernaryOp>(
1134 [&](mlir::OpBuilder &
b, mlir::Location loc) {
1135 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1136 b.getInsertionBlock()};
1138 auto res =
b.create<cir::ConstantOp>(loc, builder.getTrueAttr());
1139 b.create<cir::YieldOp>(loc, res.getRes());
1142 [&](mlir::OpBuilder &
b, mlir::Location loc) {
1143 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1144 b.getInsertionBlock()};
1146 b.create<cir::YieldOp>(loc, cgf.evaluateExprAsBool(e->
getRHS()));
1149 return maybePromoteBoolResult(resOp.getResult(), resTy);
1152 mlir::Value VisitAtomicExpr(AtomicExpr *e) {
1153 return cgf.emitAtomicExpr(e).getValue();
1157LValue ScalarExprEmitter::emitCompoundAssignLValue(
1159 mlir::Value (ScalarExprEmitter::*func)(
const BinOpInfo &),
1160 mlir::Value &result) {
1171 if (promotionTypeCR.
isNull())
1175 QualType promotionTypeRHS = getPromotionType(e->
getRHS()->
getType());
1177 if (!promotionTypeRHS.
isNull())
1180 opInfo.rhs = Visit(e->
getRHS());
1182 opInfo.fullType = promotionTypeCR;
1183 opInfo.compType = opInfo.fullType;
1184 if (
const auto *vecType = dyn_cast_or_null<VectorType>(opInfo.fullType))
1185 opInfo.compType = vecType->getElementType();
1194 if (lhsTy->
getAs<AtomicType>()) {
1195 cgf.
cgm.
errorNYI(result.getLoc(),
"atomic lvalue assign");
1199 opInfo.lhs = emitLoadOfLValue(lhsLV, e->
getExprLoc());
1201 CIRGenFunction::SourceLocRAIIObject sourceloc{
1204 if (!promotionTypeLHS.
isNull())
1205 opInfo.lhs = emitScalarConversion(opInfo.lhs, lhsTy, promotionTypeLHS, loc);
1207 opInfo.lhs = emitScalarConversion(opInfo.lhs, lhsTy,
1211 result = (this->*func)(opInfo);
1215 result = emitScalarConversion(result, promotionTypeCR, lhsTy, loc,
1216 ScalarConversionOpts(cgf.
sanOpts));
1222 if (lhsLV.isBitField())
1233mlir::Value ScalarExprEmitter::emitComplexToScalarConversion(mlir::Location lov,
1237 cir::CastKind castOpKind;
1239 case CK_FloatingComplexToReal:
1240 castOpKind = cir::CastKind::float_complex_to_real;
1242 case CK_IntegralComplexToReal:
1243 castOpKind = cir::CastKind::int_complex_to_real;
1245 case CK_FloatingComplexToBoolean:
1246 castOpKind = cir::CastKind::float_complex_to_bool;
1248 case CK_IntegralComplexToBoolean:
1249 castOpKind = cir::CastKind::int_complex_to_bool;
1252 llvm_unreachable(
"invalid complex-to-scalar cast kind");
1258mlir::Value ScalarExprEmitter::emitPromoted(
const Expr *e,
1259 QualType promotionType) {
1261 if (
const auto *bo = dyn_cast<BinaryOperator>(e)) {
1262 switch (bo->getOpcode()) {
1263#define HANDLE_BINOP(OP) \
1265 return emit##OP(emitBinOps(bo, promotionType));
1274 }
else if (
const auto *uo = dyn_cast<UnaryOperator>(e)) {
1275 switch (uo->getOpcode()) {
1278 "ScalarExprEmitter::emitPromoted unary imag");
1282 "ScalarExprEmitter::emitPromoted unary real");
1285 return emitUnaryPlusOrMinus(uo, cir::UnaryOpKind::Minus, promotionType);
1287 return emitUnaryPlusOrMinus(uo, cir::UnaryOpKind::Plus, promotionType);
1292 mlir::Value result = Visit(
const_cast<Expr *
>(e));
1294 if (!promotionType.
isNull())
1295 return emitPromotedValue(result, promotionType);
1296 return emitUnPromotedValue(result, e->
getType());
1301mlir::Value ScalarExprEmitter::emitCompoundAssign(
1302 const CompoundAssignOperator *e,
1303 mlir::Value (ScalarExprEmitter::*func)(
const BinOpInfo &)) {
1305 bool ignore = std::exchange(ignoreResultAssign,
false);
1307 LValue lhs = emitCompoundAssignLValue(e, func, rhs);
1318 if (!lhs.isVolatile())
1322 return emitLoadOfLValue(lhs, e->
getExprLoc());
1325mlir::Value ScalarExprEmitter::VisitExprWithCleanups(ExprWithCleanups *e) {
1327 mlir::OpBuilder &builder = cgf.builder;
1329 auto scope = cir::ScopeOp::create(
1332 [&](mlir::OpBuilder &
b, mlir::Type &yieldTy, mlir::Location loc) {
1333 CIRGenFunction::LexicalScope lexScope{cgf, loc,
1334 builder.getInsertionBlock()};
1335 mlir::Value scopeYieldVal = Visit(e->
getSubExpr());
1336 if (scopeYieldVal) {
1340 cir::YieldOp::create(builder, loc, scopeYieldVal);
1341 yieldTy = scopeYieldVal.getType();
1345 return scope.getNumResults() > 0 ? scope->getResult(0) :
nullptr;
1355#define COMPOUND_OP(Op) \
1356 case BO_##Op##Assign: \
1357 return emitter.emitCompoundAssignLValue(e, &ScalarExprEmitter::emit##Op, \
1394 llvm_unreachable(
"Not valid compound assignment operators");
1396 llvm_unreachable(
"Unhandled compound assignment operator");
1402 "Invalid scalar expression to emit");
1409 if (!promotionType.
isNull())
1424static std::optional<QualType>
1428 return std::nullopt;
1433 return std::nullopt;
1446 const BinOpInfo &op) {
1448 "Expected a unary or binary operator");
1452 if (!op.mayHaveIntegerOverflow())
1456 if (
const auto *uo = dyn_cast<UnaryOperator>(op.e))
1457 return !uo->canOverflow();
1462 std::optional<QualType> optionalLHSTy =
1467 std::optional<QualType> optionalRHSTy =
1477 if ((op.opcode != BO_Mul && op.opcode != BO_MulAssign) ||
1484 return (2 * astContext.
getTypeSize(lhsTy)) < promotedSize ||
1485 (2 * astContext.
getTypeSize(rhsTy)) < promotedSize;
1490 const BinOpInfo &op,
1491 bool isSubtraction) {
1496 mlir::Value pointer = op.lhs;
1497 Expr *pointerOperand =
expr->getLHS();
1498 mlir::Value
index = op.rhs;
1499 Expr *indexOperand =
expr->getRHS();
1505 if (!isSubtraction && !mlir::isa<cir::PointerType>(pointer.getType())) {
1506 std::swap(pointer,
index);
1507 std::swap(pointerOperand, indexOperand);
1509 assert(mlir::isa<cir::PointerType>(pointer.getType()) &&
1510 "Need a pointer operand");
1511 assert(mlir::isa<cir::IntType>(
index.getType()) &&
"Need an integer operand");
1546 cgf.
cgm.
errorNYI(
"Objective-C:pointer arithmetic with non-pointer type");
1557 cgf.
cgm.
errorNYI(
"void* or function pointer arithmetic");
1562 return cgf.
getBuilder().create<cir::PtrStrideOp>(
1566mlir::Value ScalarExprEmitter::emitMul(
const BinOpInfo &ops) {
1567 const mlir::Location loc = cgf.
getLoc(ops.loc);
1569 switch (cgf.
getLangOpts().getSignedOverflowBehavior()) {
1570 case LangOptions::SOB_Defined:
1571 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
1572 return builder.createMul(loc, ops.lhs, ops.rhs);
1574 case LangOptions::SOB_Undefined:
1575 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
1576 return builder.createNSWMul(loc, ops.lhs, ops.rhs);
1578 case LangOptions::SOB_Trapping:
1580 return builder.createNSWMul(loc, ops.lhs, ops.rhs);
1590 cgf.
sanOpts.
has(SanitizerKind::UnsignedIntegerOverflow) &&
1592 cgf.
cgm.
errorNYI(
"unsigned int overflow sanitizer");
1594 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
1596 return builder.createFMul(loc, ops.lhs, ops.rhs);
1599 if (ops.isFixedPointOp()) {
1605 return builder.create<cir::BinOp>(cgf.
getLoc(ops.loc),
1607 cir::BinOpKind::Mul, ops.lhs, ops.rhs);
1609mlir::Value ScalarExprEmitter::emitDiv(
const BinOpInfo &ops) {
1610 return builder.create<cir::BinOp>(cgf.
getLoc(ops.loc),
1612 cir::BinOpKind::Div, ops.lhs, ops.rhs);
1614mlir::Value ScalarExprEmitter::emitRem(
const BinOpInfo &ops) {
1615 return builder.create<cir::BinOp>(cgf.
getLoc(ops.loc),
1617 cir::BinOpKind::Rem, ops.lhs, ops.rhs);
1620mlir::Value ScalarExprEmitter::emitAdd(
const BinOpInfo &ops) {
1621 if (mlir::isa<cir::PointerType>(ops.lhs.getType()) ||
1622 mlir::isa<cir::PointerType>(ops.rhs.getType()))
1625 const mlir::Location loc = cgf.
getLoc(ops.loc);
1627 switch (cgf.
getLangOpts().getSignedOverflowBehavior()) {
1628 case LangOptions::SOB_Defined:
1629 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
1630 return builder.createAdd(loc, ops.lhs, ops.rhs);
1632 case LangOptions::SOB_Undefined:
1633 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
1634 return builder.createNSWAdd(loc, ops.lhs, ops.rhs);
1636 case LangOptions::SOB_Trapping:
1638 return builder.createNSWAdd(loc, ops.lhs, ops.rhs);
1649 cgf.
sanOpts.
has(SanitizerKind::UnsignedIntegerOverflow) &&
1651 cgf.
cgm.
errorNYI(
"unsigned int overflow sanitizer");
1653 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
1655 return builder.createFAdd(loc, ops.lhs, ops.rhs);
1658 if (ops.isFixedPointOp()) {
1664 return builder.create<cir::BinOp>(loc, cgf.
convertType(ops.fullType),
1665 cir::BinOpKind::Add, ops.lhs, ops.rhs);
1668mlir::Value ScalarExprEmitter::emitSub(
const BinOpInfo &ops) {
1669 const mlir::Location loc = cgf.
getLoc(ops.loc);
1671 if (!mlir::isa<cir::PointerType>(ops.lhs.getType())) {
1673 switch (cgf.
getLangOpts().getSignedOverflowBehavior()) {
1674 case LangOptions::SOB_Defined: {
1675 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
1676 return builder.createSub(loc, ops.lhs, ops.rhs);
1679 case LangOptions::SOB_Undefined:
1680 if (!cgf.
sanOpts.
has(SanitizerKind::SignedIntegerOverflow))
1681 return builder.createNSWSub(loc, ops.lhs, ops.rhs);
1683 case LangOptions::SOB_Trapping:
1685 return builder.createNSWSub(loc, ops.lhs, ops.rhs);
1697 cgf.
sanOpts.
has(SanitizerKind::UnsignedIntegerOverflow) &&
1699 cgf.
cgm.
errorNYI(
"unsigned int overflow sanitizer");
1701 if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
1703 return builder.createFSub(loc, ops.lhs, ops.rhs);
1706 if (ops.isFixedPointOp()) {
1712 return builder.create<cir::BinOp>(cgf.
getLoc(ops.loc),
1714 cir::BinOpKind::Sub, ops.lhs, ops.rhs);
1719 if (!mlir::isa<cir::PointerType>(ops.rhs.getType()))
1735mlir::Value ScalarExprEmitter::emitShl(
const BinOpInfo &ops) {
1737 if (ops.isFixedPointOp()) {
1747 bool sanitizeSignedBase = cgf.
sanOpts.
has(SanitizerKind::ShiftBase) &&
1751 bool sanitizeUnsignedBase =
1752 cgf.
sanOpts.
has(SanitizerKind::UnsignedShiftBase) &&
1754 bool sanitizeBase = sanitizeSignedBase || sanitizeUnsignedBase;
1755 bool sanitizeExponent = cgf.
sanOpts.
has(SanitizerKind::ShiftExponent);
1760 else if ((sanitizeBase || sanitizeExponent) &&
1761 mlir::isa<cir::IntType>(ops.lhs.getType()))
1764 return builder.createShiftLeft(cgf.
getLoc(ops.loc), ops.lhs, ops.rhs);
1767mlir::Value ScalarExprEmitter::emitShr(
const BinOpInfo &ops) {
1769 if (ops.isFixedPointOp()) {
1782 else if (cgf.
sanOpts.
has(SanitizerKind::ShiftExponent) &&
1783 mlir::isa<cir::IntType>(ops.lhs.getType()))
1788 return builder.createShiftRight(cgf.
getLoc(ops.loc), ops.lhs, ops.rhs);
1791mlir::Value ScalarExprEmitter::emitAnd(
const BinOpInfo &ops) {
1792 return builder.create<cir::BinOp>(cgf.
getLoc(ops.loc),
1794 cir::BinOpKind::And, ops.lhs, ops.rhs);
1796mlir::Value ScalarExprEmitter::emitXor(
const BinOpInfo &ops) {
1797 return builder.create<cir::BinOp>(cgf.
getLoc(ops.loc),
1799 cir::BinOpKind::Xor, ops.lhs, ops.rhs);
1801mlir::Value ScalarExprEmitter::emitOr(
const BinOpInfo &ops) {
1802 return builder.create<cir::BinOp>(cgf.
getLoc(ops.loc),
1804 cir::BinOpKind::Or, ops.lhs, ops.rhs);
1811mlir::Value ScalarExprEmitter::VisitCastExpr(
CastExpr *ce) {
1813 QualType destTy = ce->
getType();
1818 ignoreResultAssign =
false;
1821 case clang::CK_Dependent:
1822 llvm_unreachable(
"dependent cast kind in CIR gen!");
1823 case clang::CK_BuiltinFnToFnPtr:
1824 llvm_unreachable(
"builtin functions are handled elsewhere");
1826 case CK_CPointerToObjCPointerCast:
1827 case CK_BlockPointerToObjCPointerCast:
1828 case CK_AnyPointerToBlockPointerCast:
1830 mlir::Value src = Visit(
const_cast<Expr *
>(subExpr));
1835 if (cgf.
sanOpts.
has(SanitizerKind::CFIUnrelatedCast))
1837 "sanitizer support");
1841 "strict vtable pointers");
1868 case CK_AtomicToNonAtomic: {
1874 case CK_NonAtomicToAtomic:
1875 case CK_UserDefinedConversion:
1876 return Visit(
const_cast<Expr *
>(subExpr));
1878 auto v = Visit(
const_cast<Expr *
>(subExpr));
1884 if (t != v.getType())
1890 case CK_ArrayToPointerDecay:
1893 case CK_NullToPointer: {
1903 case CK_LValueToRValue:
1905 assert(subExpr->
isGLValue() &&
"lvalue-to-rvalue applied to r-value!");
1906 return Visit(
const_cast<Expr *
>(subExpr));
1908 case CK_IntegralCast: {
1909 ScalarConversionOpts opts;
1910 if (
auto *ice = dyn_cast<ImplicitCastExpr>(ce)) {
1911 if (!ice->isPartOfExplicitCast())
1912 opts = ScalarConversionOpts(cgf.
sanOpts);
1914 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
1918 case CK_FloatingComplexToReal:
1919 case CK_IntegralComplexToReal:
1920 case CK_FloatingComplexToBoolean:
1921 case CK_IntegralComplexToBoolean: {
1927 case CK_FloatingRealToComplex:
1928 case CK_FloatingComplexCast:
1929 case CK_IntegralRealToComplex:
1930 case CK_IntegralComplexCast:
1931 case CK_IntegralComplexToFloatingComplex:
1932 case CK_FloatingComplexToIntegralComplex:
1933 llvm_unreachable(
"scalar cast to non-scalar value");
1935 case CK_PointerToIntegral: {
1936 assert(!destTy->
isBooleanType() &&
"bool should use PointerToBool");
1939 "strict vtable pointers");
1940 return builder.createPtrToInt(Visit(subExpr), cgf.
convertType(destTy));
1946 case CK_IntegralToFloating:
1947 case CK_FloatingToIntegral:
1948 case CK_FloatingCast:
1949 case CK_FixedPointToFloating:
1950 case CK_FloatingToFixedPoint: {
1951 if (kind == CK_FixedPointToFloating || kind == CK_FloatingToFixedPoint) {
1953 "fixed point casts");
1957 return emitScalarConversion(Visit(subExpr), subExpr->
getType(), destTy,
1961 case CK_IntegralToBoolean:
1962 return emitIntToBoolConversion(Visit(subExpr),
1965 case CK_PointerToBoolean:
1966 return emitPointerToBoolConversion(Visit(subExpr), subExpr->
getType());
1967 case CK_FloatingToBoolean:
1968 return emitFloatToBoolConversion(Visit(subExpr),
1970 case CK_MemberPointerToBoolean: {
1971 mlir::Value memPtr = Visit(subExpr);
1973 cir::CastKind::member_ptr_to_bool, memPtr,
1977 case CK_VectorSplat: {
1979 assert(destTy->
isVectorType() &&
"CK_VectorSplat to non-vector type");
1980 return builder.create<cir::VecSplatOp>(
1984 case CK_FunctionToPointerDecay:
1994mlir::Value ScalarExprEmitter::VisitCallExpr(
const CallExpr *e) {
1996 return emitLoadOfLValue(e);
2003mlir::Value ScalarExprEmitter::VisitMemberExpr(MemberExpr *e) {
2008 Expr::EvalResult result;
2013 return emitLoadOfLValue(e);
2016mlir::Value ScalarExprEmitter::VisitInitListExpr(InitListExpr *e) {
2017 const unsigned numInitElements = e->
getNumInits();
2025 const auto vectorType =
2028 SmallVector<mlir::Value, 16> elements;
2029 for (Expr *init : e->
inits()) {
2030 elements.push_back(Visit(init));
2034 if (numInitElements < vectorType.getSize()) {
2037 std::fill_n(std::back_inserter(elements),
2038 vectorType.getSize() - numInitElements, zeroValue);
2041 return cgf.
getBuilder().create<cir::VecCreateOp>(
2046 if (numInitElements == 0)
2057 "Invalid scalar expression to emit");
2059 .emitScalarConversion(src, srcTy, dstTy, loc);
2067 "Invalid complex -> scalar conversion");
2072 ? cir::CastKind::float_complex_to_bool
2073 : cir::CastKind::int_complex_to_bool;
2078 ? cir::CastKind::float_complex_to_real
2079 : cir::CastKind::int_complex_to_real;
2085mlir::Value ScalarExprEmitter::VisitUnaryLNot(
const UnaryOperator *e) {
2099 boolVal = builder.createNot(boolVal);
2105mlir::Value ScalarExprEmitter::VisitUnaryReal(
const UnaryOperator *e) {
2107 mlir::Value result = VisitRealImag(e, promotionTy);
2108 if (result && !promotionTy.
isNull())
2109 result = emitUnPromotedValue(result, e->
getType());
2113mlir::Value ScalarExprEmitter::VisitUnaryImag(
const UnaryOperator *e) {
2115 mlir::Value result = VisitRealImag(e, promotionTy);
2116 if (result && !promotionTy.
isNull())
2117 result = emitUnPromotedValue(result, e->
getType());
2121mlir::Value ScalarExprEmitter::VisitRealImag(
const UnaryOperator *e,
2122 QualType promotionTy) {
2123 assert(e->
getOpcode() == clang::UO_Real ||
2125 "Invalid UnaryOp kind for ComplexType Real or Imag");
2139 ? builder.createComplexReal(loc, complex)
2140 : builder.createComplexImag(loc, complex);
2146 "VisitRealImag __real or __imag on a scalar");
2152mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
2153 const UnaryExprOrTypeTraitExpr *e) {
2157 kind == UETT_SizeOf || kind == UETT_DataSizeOf) {
2160 "sizeof operator for VariableArrayType",
2162 return builder.getConstant(
2164 llvm::APSInt(llvm::APInt(64, 1),
true)));
2166 }
else if (e->
getKind() == UETT_OpenMPRequiredSimdAlign) {
2168 e->
getSourceRange(),
"sizeof operator for OpenMpRequiredSimdAlign",
2170 return builder.getConstant(
2172 llvm::APSInt(llvm::APInt(64, 1),
true)));
2175 return builder.getConstant(
2197mlir::Value ScalarExprEmitter::VisitAbstractConditionalOperator(
2198 const AbstractConditionalOperator *e) {
2201 ignoreResultAssign =
false;
2204 CIRGenFunction::OpaqueValueMapping binding(cgf, e);
2206 Expr *condExpr = e->
getCond();
2214 Expr *live = lhsExpr, *dead = rhsExpr;
2216 std::swap(live, dead);
2222 mlir::Value result = Visit(live);
2229 "throw expression in conditional operator");
2237 QualType condType = condExpr->
getType();
2250 cgf.
cgm.
errorNYI(loc,
"TernaryOp for SVE vector");
2254 mlir::Value condValue = Visit(condExpr);
2255 mlir::Value lhsValue = Visit(lhsExpr);
2256 mlir::Value rhsValue = Visit(rhsExpr);
2257 return builder.create<cir::VecTernaryOp>(loc, condValue, lhsValue,
2266 bool lhsIsVoid =
false;
2270 mlir::Value lhs = Visit(lhsExpr);
2276 mlir::Value rhs = Visit(rhsExpr);
2278 assert(!rhs &&
"lhs and rhs types must match");
2286 CIRGenFunction::ConditionalEvaluation eval(cgf);
2287 SmallVector<mlir::OpBuilder::InsertPoint, 2> insertPoints{};
2288 mlir::Type yieldTy{};
2290 auto emitBranch = [&](mlir::OpBuilder &
b, mlir::Location loc, Expr *
expr) {
2291 CIRGenFunction::LexicalScope lexScope{cgf, loc,
b.getInsertionBlock()};
2295 eval.beginEvaluation();
2296 mlir::Value branch = Visit(
expr);
2297 eval.endEvaluation();
2300 yieldTy = branch.getType();
2301 b.create<cir::YieldOp>(loc, branch);
2305 insertPoints.push_back(
b.saveInsertionPoint());
2309 mlir::Value result = builder
2310 .create<cir::TernaryOp>(
2313 [&](mlir::OpBuilder &
b, mlir::Location loc) {
2314 emitBranch(
b, loc, lhsExpr);
2317 [&](mlir::OpBuilder &
b, mlir::Location loc) {
2318 emitBranch(
b, loc, rhsExpr);
2322 if (!insertPoints.empty()) {
2328 for (mlir::OpBuilder::InsertPoint &toInsert : insertPoints) {
2329 mlir::OpBuilder::InsertionGuard guard(builder);
2330 builder.restoreInsertionPoint(toInsert);
2333 if (mlir::isa<cir::VoidType>(yieldTy)) {
2334 builder.create<cir::YieldOp>(loc);
2337 builder.create<cir::YieldOp>(loc, op0);
2347 cir::UnaryOpKind kind,
2350 .emitScalarPrePostIncDec(e, lv, kind, isPre);
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 bool isCheapEnoughToEvaluateUnconditionally(const Expr *e, CIRGenFunction &cgf)
Return true if the specified expression is cheap enough and side-effect-free enough to evaluate uncon...
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.
__device__ __2f16 float __ockl_bool s
cir::ConstantOp getBool(bool state, mlir::Location loc)
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc)
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 createBitcast(mlir::Value src, mlir::Type newTy)
cir::CmpOp createCompare(mlir::Location loc, cir::CmpOpKind kind, mlir::Value lhs, mlir::Value rhs)
mlir::Value createSelect(mlir::Location loc, mlir::Value condition, mlir::Value trueValue, mlir::Value falseValue)
llvm::APInt getValue() 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.
bool hasSameUnqualifiedType(QualType T1, QualType T2) const
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
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.
bool isPromotableIntegerType(QualType T) const
More type predicates useful for type checking/promotion.
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...
A builtin binary operation expression such as "x + y" or "x <= y".
SourceLocation getExprLoc() 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
mlir::Value createNeg(mlir::Value value)
void forceCleanup()
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)
const clang::LangOptions & getLangOpts() const
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).
mlir::Value emitScalarPrePostIncDec(const UnaryOperator *e, LValue lv, cir::UnaryOpKind kind, bool isPre)
friend class ::ScalarExprEmitter
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...
clang::SanitizerSet sanOpts
Sanitizers enabled for this function.
mlir::Value createDummyValue(mlir::Location loc, clang::QualType qt)
LValue emitCompoundAssignmentLValue(const clang::CompoundAssignOperator *e)
mlir::Value emitScalarExpr(const clang::Expr *e)
Emit the computation of the specified expression of scalar type.
mlir::Value emitPromotedScalarExpr(const Expr *e, QualType promotionType)
CIRGenBuilderTy & getBuilder()
CIRGenModule & getCIRGenModule()
bool containsLabel(const clang::Stmt *s, bool ignoreCaseStmts=false)
Return true if the statement contains a label in it.
Address emitArrayToPointerDecay(const Expr *e, LValueBaseInfo *baseInfo=nullptr)
LexicalScope * curLexScope
mlir::Value emitStoreThroughBitfieldLValue(RValue src, LValue dstresult)
clang::ASTContext & getContext() const
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.
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
const clang::CodeGenOptions & getCodeGenOpts() const
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.
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
CastKind getCastKind() const
static const char * getCastKindName(CastKind CK)
unsigned getValue() const
Complex values, per C99 6.2.5p11.
CompoundAssignOperator - For compound assignments (e.g.
QualType getComputationLHSType() const
QualType getComputationResultType() const
Expr * getSrcExpr() const
getSrcExpr - Return the Expr to be converted.
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 isEvaluatable(const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects) const
isEvaluatable - Call EvaluateAsRValue to see if this expression can be constant folded without side-e...
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...
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()
bool isSignedOverflowDefined() const
A pointer to member type per C++ 8.3.3 - Pointers to members.
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.
unsigned getNumSubExprs() const
getNumSubExprs - Return the size of the SubExprs array.
Expr * getExpr(unsigned Index)
getExpr - Return the Expr at the specified index.
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 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,...
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
bool isSveVLSBuiltinType() const
Determines if this is a sizeless type supported by the 'arm_sve_vector_bits' type attribute,...
bool hasUnsignedIntegerRepresentation() const
Determine whether this type has an unsigned integer representation of some sort, e....
bool isExtVectorType() const
bool isAnyComplexType() const
bool isFixedPointType() const
Return true if this is a 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 isFunctionType() 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.
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)
bool canOverflow() const
Returns true if the unary operator can cause an overflow.
Represents a GCC generic vector type.
VectorKind getVectorKind() const
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.
unsigned kind
All of the diagnostics that can be emitted by the frontend.
The JSON file list parser is used to communicate input to InstallAPI.
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)
static bool instrumentation()
static bool dataMemberType()
static bool objCLifetime()
static bool addressSpace()
static bool fixedPointType()
static bool vecTernaryOp()
static bool cgFPOptionsRAII()
static bool fpConstraints()
static bool addHeapAllocSiteMetadata()
static bool mayHaveIntegerOverflow()
static bool tryEmitAsConstant()
static bool scalableVectors()
static bool emitLValueAlignmentAssumption()
static bool incrementProfileCounter()
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.