clang 24.0.0git
CIRGenExprComplex.cpp
Go to the documentation of this file.
1#include "CIRGenBuilder.h"
3#include "CIRGenFunction.h"
4
6
7using namespace clang;
8using namespace clang::CIRGen;
9
10#ifndef NDEBUG
11/// Return the complex type that we are meant to emit.
13 type = type.getCanonicalType();
14 if (const ComplexType *comp = dyn_cast<ComplexType>(type))
15 return comp;
16 return cast<ComplexType>(cast<AtomicType>(type)->getValueType());
17}
18#endif // NDEBUG
19
20namespace {
21class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> {
22 CIRGenFunction &cgf;
23 CIRGenBuilderTy &builder;
24
25public:
26 explicit ComplexExprEmitter(CIRGenFunction &cgf)
27 : cgf(cgf), builder(cgf.getBuilder()) {}
28
29 //===--------------------------------------------------------------------===//
30 // Utilities
31 //===--------------------------------------------------------------------===//
32
33 /// Given an expression with complex type that represents a value l-value,
34 /// this method emits the address of the l-value, then loads and returns the
35 /// result.
36 mlir::Value emitLoadOfLValue(const Expr *e) {
37 return emitLoadOfLValue(cgf.emitLValue(e), e->getExprLoc());
38 }
39
40 mlir::Value emitLoadOfLValue(LValue lv, SourceLocation loc);
41
42 /// Store the specified real/imag parts into the
43 /// specified value pointer.
44 void emitStoreOfComplex(mlir::Location loc, mlir::Value val, LValue lv,
45 bool isInit);
46
47 /// Emit a cast from complex value Val to DestType.
48 mlir::Value emitComplexToComplexCast(mlir::Value value, QualType srcType,
49 QualType destType, SourceLocation loc);
50
51 /// Emit a cast from scalar value Val to DestType.
52 mlir::Value emitScalarToComplexCast(mlir::Value value, QualType srcType,
53 QualType destType, SourceLocation loc);
54
55 //===--------------------------------------------------------------------===//
56 // Visitor Methods
57 //===--------------------------------------------------------------------===//
58
59 mlir::Value Visit(Expr *e) {
60 return StmtVisitor<ComplexExprEmitter, mlir::Value>::Visit(e);
61 }
62
63 mlir::Value VisitStmt(Stmt *s) {
64 s->dump(llvm::errs(), cgf.getContext());
65 llvm_unreachable("Stmt can't have complex result type!");
66 }
67
68 mlir::Value VisitExpr(Expr *e);
69 mlir::Value VisitConstantExpr(ConstantExpr *e) {
70 if (mlir::Attribute result = ConstantEmitter(cgf).tryEmitConstantExpr(e))
71 return builder.getConstant(cgf.getLoc(e->getSourceRange()),
72 mlir::cast<mlir::TypedAttr>(result));
73
74 cgf.cgm.errorNYI(e->getExprLoc(),
75 "ComplexExprEmitter VisitConstantExpr non constantexpr");
76 return {};
77 }
78
79 mlir::Value VisitParenExpr(ParenExpr *pe) { return Visit(pe->getSubExpr()); }
80 mlir::Value VisitGenericSelectionExpr(GenericSelectionExpr *ge) {
81 return Visit(ge->getResultExpr());
82 }
83 mlir::Value VisitImaginaryLiteral(const ImaginaryLiteral *il);
84 mlir::Value
85 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *pe) {
86 return Visit(pe->getReplacement());
87 }
88 mlir::Value VisitCoawaitExpr(CoawaitExpr *s) {
89 return cgf.emitCoawaitExpr(*s).getComplexValue();
90 }
91 mlir::Value VisitCoyieldExpr(CoyieldExpr *s) {
92 return cgf.emitCoyieldExpr(*s).getComplexValue();
93 }
94 mlir::Value VisitUnaryCoawait(const UnaryOperator *e) {
95 cgf.cgm.errorNYI(e->getExprLoc(), "ComplexExprEmitter VisitUnaryCoawait");
96 return {};
97 }
98
99 mlir::Value emitConstant(const CIRGenFunction::ConstantEmission &constant,
100 Expr *e) {
101 assert(constant && "not a constant");
102 if (constant.isReference())
103 return emitLoadOfLValue(constant.getReferenceLValue(cgf, e),
104 e->getExprLoc());
105
106 mlir::TypedAttr valueAttr = constant.getValue();
107 return builder.getConstant(cgf.getLoc(e->getSourceRange()), valueAttr);
108 }
109
110 // l-values.
111 mlir::Value VisitDeclRefExpr(DeclRefExpr *e) {
112 if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(e))
113 return emitConstant(constant, e);
114 return emitLoadOfLValue(e);
115 }
116 mlir::Value VisitObjCIvarRefExpr(ObjCIvarRefExpr *e) {
117 cgf.cgm.errorNYI(e->getExprLoc(),
118 "ComplexExprEmitter VisitObjCIvarRefExpr");
119 return {};
120 }
121 mlir::Value VisitObjCMessageExpr(ObjCMessageExpr *e) {
122 cgf.cgm.errorNYI(e->getExprLoc(),
123 "ComplexExprEmitter VisitObjCMessageExpr");
124 return {};
125 }
126 mlir::Value VisitArraySubscriptExpr(Expr *e) { return emitLoadOfLValue(e); }
127 mlir::Value VisitMemberExpr(MemberExpr *me) {
128 if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(me)) {
129 cgf.emitIgnoredExpr(me->getBase());
130 return emitConstant(constant, me);
131 }
132 return emitLoadOfLValue(me);
133 }
134 mlir::Value VisitOpaqueValueExpr(OpaqueValueExpr *e) {
135 if (e->isGLValue())
136 return emitLoadOfLValue(cgf.getOrCreateOpaqueLValueMapping(e),
137 e->getExprLoc());
138 return cgf.getOrCreateOpaqueRValueMapping(e).getComplexValue();
139 }
140
141 mlir::Value VisitPseudoObjectExpr(PseudoObjectExpr *e) {
142 cgf.cgm.errorNYI(e->getExprLoc(),
143 "ComplexExprEmitter VisitPseudoObjectExpr");
144 return {};
145 }
146
147 mlir::Value emitCast(CastKind ck, Expr *op, QualType destTy);
148 mlir::Value VisitImplicitCastExpr(ImplicitCastExpr *e) {
149 // Unlike for scalars, we don't have to worry about function->ptr demotion
150 // here.
152 return emitLoadOfLValue(e);
153 return emitCast(e->getCastKind(), e->getSubExpr(), e->getType());
154 }
155 mlir::Value VisitCastExpr(CastExpr *e) {
156 if (const auto *ece = dyn_cast<ExplicitCastExpr>(e))
157 cgf.cgm.emitExplicitCastExprType(ece);
159 return emitLoadOfLValue(e);
160 return emitCast(e->getCastKind(), e->getSubExpr(), e->getType());
161 }
162 mlir::Value VisitCallExpr(const CallExpr *e);
163 mlir::Value VisitStmtExpr(const StmtExpr *e);
164
165 // Operators.
166 mlir::Value VisitPrePostIncDec(const UnaryOperator *e) {
167 LValue lv = cgf.emitLValue(e->getSubExpr());
168 return cgf.emitComplexPrePostIncDec(e, lv);
169 }
170 mlir::Value VisitUnaryPostDec(const UnaryOperator *e) {
171 return VisitPrePostIncDec(e);
172 }
173 mlir::Value VisitUnaryPostInc(const UnaryOperator *e) {
174 return VisitPrePostIncDec(e);
175 }
176 mlir::Value VisitUnaryPreDec(const UnaryOperator *e) {
177 return VisitPrePostIncDec(e);
178 }
179 mlir::Value VisitUnaryPreInc(const UnaryOperator *e) {
180 return VisitPrePostIncDec(e);
181 }
182 mlir::Value VisitUnaryDeref(const Expr *e) { return emitLoadOfLValue(e); }
183
184 mlir::Value VisitUnaryPlus(const UnaryOperator *e);
185 mlir::Value VisitUnaryPlus(const UnaryOperator *e, QualType promotionType);
186 mlir::Value VisitUnaryMinus(const UnaryOperator *e);
187 mlir::Value VisitUnaryMinus(const UnaryOperator *e, QualType promotionType);
188 mlir::Value VisitUnaryNot(const UnaryOperator *e);
189 // LNot,Real,Imag never return complex.
190 mlir::Value VisitUnaryExtension(const UnaryOperator *e) {
191 return Visit(e->getSubExpr());
192 }
193 mlir::Value VisitCXXDefaultArgExpr(CXXDefaultArgExpr *dae) {
194 CIRGenFunction::CXXDefaultArgExprScope scope(cgf, dae);
195 return Visit(dae->getExpr());
196 }
197 mlir::Value VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die) {
198 CIRGenFunction::CXXDefaultInitExprScope scope(cgf, die);
199 return Visit(die->getExpr());
200 }
201 mlir::Value VisitExprWithCleanups(ExprWithCleanups *e) {
202 CIRGenFunction::FullExprCleanupScope scope(cgf, e->getSubExpr());
203 mlir::Value complexVal = Visit(e->getSubExpr());
204 // Defend against dominance problems caused by jumps out of expression
205 // evaluation through the shared cleanup block.
206 scope.exit({&complexVal});
207 return complexVal;
208 }
209 mlir::Value VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e) {
210 mlir::Location loc = cgf.getLoc(e->getExprLoc());
211 mlir::Type complexTy = cgf.convertType(e->getType());
212 return builder.getNullValue(complexTy, loc);
213 }
214 mlir::Value VisitImplicitValueInitExpr(ImplicitValueInitExpr *e) {
215 mlir::Location loc = cgf.getLoc(e->getExprLoc());
216 mlir::Type complexTy = cgf.convertType(e->getType());
217 return builder.getNullValue(complexTy, loc);
218 }
219
220 struct BinOpInfo {
221 mlir::Location loc;
222 mlir::Value lhs{};
223 mlir::Value rhs{};
224 QualType ty{}; // Computation Type.
225 FPOptions fpFeatures{};
226 };
227
228 BinOpInfo emitBinOps(const BinaryOperator *e,
229 QualType promotionTy = QualType());
230
231 mlir::Value emitPromoted(const Expr *e, QualType promotionTy);
232 mlir::Value emitPromotedComplexOperand(const Expr *e, QualType promotionTy);
233 LValue emitCompoundAssignLValue(
234 const CompoundAssignOperator *e,
235 mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &),
236 RValue &value);
237 mlir::Value emitCompoundAssign(
238 const CompoundAssignOperator *e,
239 mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &));
240
241 mlir::Value emitBinAdd(const BinOpInfo &op);
242 mlir::Value emitBinSub(const BinOpInfo &op);
243 mlir::Value emitBinMul(const BinOpInfo &op);
244 mlir::Value emitBinDiv(const BinOpInfo &op);
245
246 QualType getPromotionType(QualType ty, bool isDivOpCode = false) {
247 if (auto *complexTy = ty->getAs<ComplexType>()) {
248 QualType elementTy = complexTy->getElementType();
249 if (elementTy.UseExcessPrecision(cgf.getContext()))
250 return cgf.getContext().getComplexType(cgf.getContext().FloatTy);
251 }
252
253 if (ty.UseExcessPrecision(cgf.getContext()))
254 return cgf.getContext().FloatTy;
255 return QualType();
256 }
257
258#define HANDLEBINOP(OP) \
259 mlir::Value VisitBin##OP(const BinaryOperator *e) { \
260 QualType promotionTy = getPromotionType( \
261 e->getType(), e->getOpcode() == BinaryOperatorKind::BO_Div); \
262 mlir::Value result = emitBin##OP(emitBinOps(e, promotionTy)); \
263 if (!promotionTy.isNull()) \
264 result = cgf.emitUnPromotedValue(result, e->getType()); \
265 return result; \
266 }
267
268 HANDLEBINOP(Add)
269 HANDLEBINOP(Sub)
270 HANDLEBINOP(Mul)
271 HANDLEBINOP(Div)
272#undef HANDLEBINOP
273
274 mlir::Value VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *e) {
275 return Visit(e->getSemanticForm());
276 }
277
278 // Compound assignments.
279 mlir::Value VisitBinAddAssign(const CompoundAssignOperator *e) {
280 return emitCompoundAssign(e, &ComplexExprEmitter::emitBinAdd);
281 }
282 mlir::Value VisitBinSubAssign(const CompoundAssignOperator *e) {
283 return emitCompoundAssign(e, &ComplexExprEmitter::emitBinSub);
284 }
285 mlir::Value VisitBinMulAssign(const CompoundAssignOperator *e) {
286 return emitCompoundAssign(e, &ComplexExprEmitter::emitBinMul);
287 }
288 mlir::Value VisitBinDivAssign(const CompoundAssignOperator *e) {
289 return emitCompoundAssign(e, &ComplexExprEmitter::emitBinDiv);
290 }
291
292 // GCC rejects rem/and/or/xor for integer complex.
293 // Logical and/or always return int, never complex.
294
295 // No comparisons produce a complex result.
296
297 LValue emitBinAssignLValue(const BinaryOperator *e, mlir::Value &val);
298 mlir::Value VisitBinAssign(const BinaryOperator *e);
299 mlir::Value VisitBinComma(const BinaryOperator *e);
300
301 mlir::Value
302 VisitAbstractConditionalOperator(const AbstractConditionalOperator *e);
303 mlir::Value VisitChooseExpr(ChooseExpr *e);
304
305 mlir::Value VisitInitListExpr(InitListExpr *e);
306
307 mlir::Value VisitCompoundLiteralExpr(CompoundLiteralExpr *e) {
308 return emitLoadOfLValue(e);
309 }
310
311 mlir::Value VisitVAArgExpr(VAArgExpr *e);
312
313 mlir::Value VisitAtomicExpr(AtomicExpr *e) {
314 return cgf.emitAtomicExpr(e).getComplexValue();
315 }
316
317 mlir::Value VisitPackIndexingExpr(PackIndexingExpr *e) {
318 return Visit(e->getSelectedExpr());
319 }
320};
321} // namespace
322
323//===----------------------------------------------------------------------===//
324// Utilities
325//===----------------------------------------------------------------------===//
326
327/// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
328/// load the real and imaginary pieces, returning them as Real/Imag.
329mlir::Value ComplexExprEmitter::emitLoadOfLValue(LValue lv,
330 SourceLocation loc) {
331 assert(lv.isSimple() && "non-simple complex l-value?");
332 if (lv.getType()->isAtomicType())
333 return cgf.emitAtomicLoad(lv, loc).getComplexValue();
334
335 const Address srcAddr = lv.getAddress();
336 return builder.createLoad(cgf.getLoc(loc), srcAddr, lv.isVolatileQualified());
337}
338
339/// EmitStoreOfComplex - Store the specified real/imag parts into the
340/// specified value pointer.
341void ComplexExprEmitter::emitStoreOfComplex(mlir::Location loc, mlir::Value val,
342 LValue lv, bool isInit) {
343 if (lv.getType()->isAtomicType() ||
344 (!isInit && cgf.isLValueSuitableForInlineAtomic(lv))) {
345 return cgf.emitAtomicStore(RValue::getComplex(val), lv, isInit);
346 }
347
348 const Address destAddr = lv.getAddress();
349 builder.createStore(loc, val, destAddr, lv.isVolatileQualified());
350}
351
352//===----------------------------------------------------------------------===//
353// Visitor Methods
354//===----------------------------------------------------------------------===//
355
356mlir::Value ComplexExprEmitter::VisitExpr(Expr *e) {
357 cgf.cgm.errorUnsupported(e, "complex expression");
358 mlir::Type complexTy = cgf.convertType(e->getType());
359 mlir::Location loc = cgf.getLoc(e->getExprLoc());
360 return builder.getConstant(loc, cir::PoisonAttr::get(complexTy));
361}
362
363mlir::Value
364ComplexExprEmitter::VisitImaginaryLiteral(const ImaginaryLiteral *il) {
365 auto ty = mlir::cast<cir::ComplexType>(cgf.convertType(il->getType()));
366 mlir::Type elementTy = ty.getElementType();
367 mlir::Location loc = cgf.getLoc(il->getExprLoc());
368
369 mlir::TypedAttr realValueAttr;
370 mlir::TypedAttr imagValueAttr;
371
372 if (mlir::isa<cir::IntType>(elementTy)) {
373 llvm::APInt imagValue = cast<IntegerLiteral>(il->getSubExpr())->getValue();
374 realValueAttr = cir::IntAttr::get(elementTy, 0);
375 imagValueAttr = cir::IntAttr::get(elementTy, imagValue);
376 } else {
377 assert(mlir::isa<cir::FPTypeInterface>(elementTy) &&
378 "Expected complex element type to be floating-point");
379
380 llvm::APFloat imagValue =
381 cast<FloatingLiteral>(il->getSubExpr())->getValue();
382 realValueAttr = cir::FPAttr::get(
383 elementTy, llvm::APFloat::getZero(imagValue.getSemantics()));
384 imagValueAttr = cir::FPAttr::get(elementTy, imagValue);
385 }
386
387 auto complexAttr = cir::ConstComplexAttr::get(realValueAttr, imagValueAttr);
388 return cir::ConstantOp::create(builder, loc, complexAttr);
389}
390
391mlir::Value ComplexExprEmitter::VisitCallExpr(const CallExpr *e) {
393 return emitLoadOfLValue(e);
394 return cgf.emitCallExpr(e).getComplexValue();
395}
396
397mlir::Value ComplexExprEmitter::VisitStmtExpr(const StmtExpr *e) {
398 CIRGenFunction::StmtExprEvaluation eval(cgf);
399 Address retAlloca =
400 cgf.createMemTemp(e->getType(), cgf.getLoc(e->getSourceRange()));
401 (void)cgf.emitCompoundStmt(*e->getSubStmt(), &retAlloca);
402 assert(retAlloca.isValid() && "Expected complex return value");
403 return emitLoadOfLValue(cgf.makeAddrLValue(retAlloca, e->getType()),
404 e->getExprLoc());
405}
406
407mlir::Value ComplexExprEmitter::emitComplexToComplexCast(mlir::Value val,
408 QualType srcType,
409 QualType destType,
410 SourceLocation loc) {
411 if (srcType == destType)
412 return val;
413
414 // Get the src/dest element type.
415 QualType srcElemTy = srcType.getAtomicUnqualifiedType()
416 ->castAs<ComplexType>()
417 ->getElementType();
418 QualType destElemTy = destType.getAtomicUnqualifiedType()
419 ->castAs<ComplexType>()
420 ->getElementType();
421
422 cir::CastKind castOpKind;
423 if (srcElemTy->isFloatingType() && destElemTy->isFloatingType())
424 castOpKind = cir::CastKind::float_complex;
425 else if (srcElemTy->isFloatingType() && destElemTy->isIntegerType())
426 castOpKind = cir::CastKind::float_complex_to_int_complex;
427 else if (srcElemTy->isIntegerType() && destElemTy->isFloatingType())
428 castOpKind = cir::CastKind::int_complex_to_float_complex;
429 else if (srcElemTy->isIntegerType() && destElemTy->isIntegerType())
430 castOpKind = cir::CastKind::int_complex;
431 else
432 llvm_unreachable("unexpected src type or dest type");
433
434 return builder.createCast(cgf.getLoc(loc), castOpKind, val,
435 cgf.convertType(destType));
436}
437
438mlir::Value ComplexExprEmitter::emitScalarToComplexCast(mlir::Value val,
439 QualType srcType,
440 QualType destType,
441 SourceLocation loc) {
442 cir::CastKind castOpKind;
443 if (srcType->isFloatingType())
444 castOpKind = cir::CastKind::float_to_complex;
445 else if (srcType->isIntegerType())
446 castOpKind = cir::CastKind::int_to_complex;
447 else
448 llvm_unreachable("unexpected src type");
449
450 return builder.createCast(cgf.getLoc(loc), castOpKind, val,
451 cgf.convertType(destType));
452}
453
454mlir::Value ComplexExprEmitter::emitCast(CastKind ck, Expr *op,
455 QualType destTy) {
456 destTy = destTy.getAtomicUnqualifiedType();
457 switch (ck) {
458 case CK_Dependent:
459 llvm_unreachable("dependent type must be resolved before the CIR codegen");
460
461 // Atomic to non-atomic casts may be more than a no-op for some platforms
462 // and for some types.
463 case CK_NonAtomicToAtomic:
464 case CK_AtomicToNonAtomic:
465 case CK_NoOp:
466 case CK_LValueToRValue:
467 case CK_UserDefinedConversion:
468 return Visit(op);
469
470 case CK_LValueBitCast: {
471 LValue origLV = cgf.emitLValue(op);
472 Address addr =
473 origLV.getAddress().withElementType(builder, cgf.convertType(destTy));
474 LValue destLV = cgf.makeAddrLValue(addr, destTy);
475 return emitLoadOfLValue(destLV, op->getExprLoc());
476 }
477
478 case CK_LValueToRValueBitCast: {
479 LValue sourceLVal = cgf.emitLValue(op);
480 Address addr = sourceLVal.getAddress().withElementType(
481 builder, cgf.convertTypeForMem(destTy));
482 LValue destLV = cgf.makeAddrLValue(addr, destTy);
484 return emitLoadOfLValue(destLV, op->getExprLoc());
485 }
486
487 case CK_BitCast:
488 case CK_BaseToDerived:
489 case CK_DerivedToBase:
490 case CK_UncheckedDerivedToBase:
491 case CK_Dynamic:
492 case CK_ToUnion:
493 case CK_ArrayToPointerDecay:
494 case CK_FunctionToPointerDecay:
495 case CK_NullToPointer:
496 case CK_NullToMemberPointer:
497 case CK_BaseToDerivedMemberPointer:
498 case CK_DerivedToBaseMemberPointer:
499 case CK_MemberPointerToBoolean:
500 case CK_ReinterpretMemberPointer:
501 case CK_ConstructorConversion:
502 case CK_IntegralToPointer:
503 case CK_PointerToIntegral:
504 case CK_PointerToBoolean:
505 case CK_ToVoid:
506 case CK_VectorSplat:
507 case CK_IntegralCast:
508 case CK_BooleanToSignedIntegral:
509 case CK_IntegralToBoolean:
510 case CK_IntegralToFloating:
511 case CK_FloatingToIntegral:
512 case CK_FloatingToBoolean:
513 case CK_FloatingCast:
514 case CK_CPointerToObjCPointerCast:
515 case CK_BlockPointerToObjCPointerCast:
516 case CK_AnyPointerToBlockPointerCast:
517 case CK_ObjCObjectLValueCast:
518 case CK_FloatingComplexToReal:
519 case CK_FloatingComplexToBoolean:
520 case CK_IntegralComplexToReal:
521 case CK_IntegralComplexToBoolean:
522 case CK_ARCProduceObject:
523 case CK_ARCConsumeObject:
524 case CK_ARCReclaimReturnedObject:
525 case CK_ARCExtendBlockObject:
526 case CK_CopyAndAutoreleaseBlockObject:
527 case CK_BuiltinFnToFnPtr:
528 case CK_ZeroToOCLOpaqueType:
529 case CK_AddressSpaceConversion:
530 case CK_IntToOCLSampler:
531 case CK_FloatingToFixedPoint:
532 case CK_FixedPointToFloating:
533 case CK_FixedPointCast:
534 case CK_FixedPointToBoolean:
535 case CK_FixedPointToIntegral:
536 case CK_IntegralToFixedPoint:
537 case CK_MatrixCast:
538 case CK_HLSLVectorTruncation:
539 case CK_HLSLMatrixTruncation:
540 case CK_HLSLArrayRValue:
541 case CK_HLSLElementwiseCast:
542 case CK_HLSLAggregateSplatCast:
543 llvm_unreachable("invalid cast kind for complex value");
544
545 case CK_FloatingRealToComplex:
546 case CK_IntegralRealToComplex: {
547 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, op);
548 return emitScalarToComplexCast(cgf.emitScalarExpr(op), op->getType(),
549 destTy, op->getExprLoc());
550 }
551
552 case CK_FloatingComplexCast:
553 case CK_FloatingComplexToIntegralComplex:
554 case CK_IntegralComplexCast:
555 case CK_IntegralComplexToFloatingComplex: {
556 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, op);
557 return emitComplexToComplexCast(Visit(op), op->getType(), destTy,
558 op->getExprLoc());
559 }
560 }
561
562 llvm_unreachable("unknown cast resulting in complex value");
563}
564
565mlir::Value ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *e) {
566 QualType promotionTy = getPromotionType(e->getSubExpr()->getType());
567 mlir::Value result = VisitUnaryPlus(e, promotionTy);
568 if (!promotionTy.isNull())
569 return cgf.emitUnPromotedValue(result, e->getSubExpr()->getType());
570 return result;
571}
572
573mlir::Value ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *e,
574 QualType promotionType) {
575 if (!promotionType.isNull())
576 return cgf.emitPromotedComplexExpr(e->getSubExpr(), promotionType);
577 return Visit(e->getSubExpr());
578}
579
580mlir::Value ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *e) {
581 QualType promotionTy = getPromotionType(e->getSubExpr()->getType());
582 mlir::Value result = VisitUnaryMinus(e, promotionTy);
583 if (!promotionTy.isNull())
584 return cgf.emitUnPromotedValue(result, e->getSubExpr()->getType());
585 return result;
586}
587
588mlir::Value ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *e,
589 QualType promotionType) {
590 mlir::Value op;
591 if (!promotionType.isNull())
592 op = cgf.emitPromotedComplexExpr(e->getSubExpr(), promotionType);
593 else
594 op = Visit(e->getSubExpr());
595
596 // Negate each component of the complex value.
597 mlir::Location loc = cgf.getLoc(e->getExprLoc());
598 mlir::Value real = builder.createComplexReal(loc, op);
599 mlir::Value imag = builder.createComplexImag(loc, op);
600
601 mlir::Value resultReal;
602 mlir::Value resultImag;
603 if (cir::isFPOrVectorOfFPType(real.getType())) {
604 resultReal = builder.createFNeg(loc, real);
605 resultImag = builder.createFNeg(loc, imag);
606 } else {
607 resultReal = builder.createMinus(loc, real);
608 resultImag = builder.createMinus(loc, imag);
609 }
610
611 return builder.createComplexCreate(loc, resultReal, resultImag);
612}
613
614mlir::Value ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *e) {
615 mlir::Value op = Visit(e->getSubExpr());
616 return builder.createComplexConj(cgf.getLoc(e->getExprLoc()), op);
617}
618
619mlir::Value ComplexExprEmitter::emitBinAdd(const BinOpInfo &op) {
621 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, op.fpFeatures);
622
623 if (mlir::isa<cir::ComplexType>(op.lhs.getType()) &&
624 mlir::isa<cir::ComplexType>(op.rhs.getType()))
625 return builder.createComplexAdd(op.loc, op.lhs, op.rhs);
626
627 auto createAdd = [&](mlir::Location loc, mlir::Value a, mlir::Value b) {
628 return cir::isFPOrVectorOfFPType(a.getType())
629 ? builder.createFAdd(loc, a, b)
630 : builder.createAdd(loc, a, b);
631 };
632
633 if (mlir::isa<cir::ComplexType>(op.lhs.getType())) {
634 mlir::Value real = builder.createComplexReal(op.loc, op.lhs);
635 mlir::Value imag = builder.createComplexImag(op.loc, op.lhs);
636 mlir::Value newReal = createAdd(op.loc, real, op.rhs);
637 return builder.createComplexCreate(op.loc, newReal, imag);
638 }
639
640 assert(mlir::isa<cir::ComplexType>(op.rhs.getType()));
641 mlir::Value real = builder.createComplexReal(op.loc, op.rhs);
642 mlir::Value imag = builder.createComplexImag(op.loc, op.rhs);
643 mlir::Value newReal = createAdd(op.loc, op.lhs, real);
644 return builder.createComplexCreate(op.loc, newReal, imag);
645}
646
647mlir::Value ComplexExprEmitter::emitBinSub(const BinOpInfo &op) {
649 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, op.fpFeatures);
650
651 if (mlir::isa<cir::ComplexType>(op.lhs.getType()) &&
652 mlir::isa<cir::ComplexType>(op.rhs.getType()))
653 return builder.createComplexSub(op.loc, op.lhs, op.rhs);
654
655 auto createSub = [&](mlir::Location loc, mlir::Value a, mlir::Value b) {
656 return cir::isFPOrVectorOfFPType(a.getType())
657 ? builder.createFSub(loc, a, b)
658 : builder.createSub(loc, a, b);
659 };
660
661 if (mlir::isa<cir::ComplexType>(op.lhs.getType())) {
662 mlir::Value real = builder.createComplexReal(op.loc, op.lhs);
663 mlir::Value imag = builder.createComplexImag(op.loc, op.lhs);
664 mlir::Value newReal = createSub(op.loc, real, op.rhs);
665 return builder.createComplexCreate(op.loc, newReal, imag);
666 }
667
668 auto createNeg = [&](mlir::Location loc, mlir::Value a) {
669 return cir::isFPOrVectorOfFPType(a.getType()) ? builder.createFNeg(loc, a)
670 : builder.createNeg(loc, a);
671 };
672
673 assert(mlir::isa<cir::ComplexType>(op.rhs.getType()));
674 mlir::Value real = builder.createComplexReal(op.loc, op.rhs);
675 mlir::Value imag = builder.createComplexImag(op.loc, op.rhs);
676 mlir::Value newReal = createSub(op.loc, op.lhs, real);
677 mlir::Value newImag = createNeg(op.loc, imag);
678 return builder.createComplexCreate(op.loc, newReal, newImag);
679}
680
681static cir::ComplexRangeKind
683 switch (range) {
685 return cir::ComplexRangeKind::Full;
687 return cir::ComplexRangeKind::Improved;
689 return cir::ComplexRangeKind::Promoted;
691 return cir::ComplexRangeKind::Basic;
693 // The default value for ComplexRangeKind is Full if no option is selected
694 return cir::ComplexRangeKind::Full;
695 }
696 llvm_unreachable("fully covered switch");
697}
698
699mlir::Value ComplexExprEmitter::emitBinMul(const BinOpInfo &op) {
701 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, op.fpFeatures);
702
703 if (mlir::isa<cir::ComplexType>(op.lhs.getType()) &&
704 mlir::isa<cir::ComplexType>(op.rhs.getType())) {
705 cir::ComplexRangeKind rangeKind =
706 getComplexRangeAttr(op.fpFeatures.getComplexRange());
707 return builder.createComplexMul(op.loc, op.lhs, op.rhs, rangeKind);
708 }
709
710 auto createMul = [&](mlir::Location loc, mlir::Value a, mlir::Value b) {
711 return cir::isFPOrVectorOfFPType(a.getType())
712 ? builder.createFMul(loc, a, b)
713 : builder.createMul(loc, a, b);
714 };
715
716 if (mlir::isa<cir::ComplexType>(op.lhs.getType())) {
717 mlir::Value real = builder.createComplexReal(op.loc, op.lhs);
718 mlir::Value imag = builder.createComplexImag(op.loc, op.lhs);
719 mlir::Value newReal = createMul(op.loc, real, op.rhs);
720 mlir::Value newImag = createMul(op.loc, imag, op.rhs);
721 return builder.createComplexCreate(op.loc, newReal, newImag);
722 }
723
724 assert(mlir::isa<cir::ComplexType>(op.rhs.getType()));
725 mlir::Value real = builder.createComplexReal(op.loc, op.rhs);
726 mlir::Value imag = builder.createComplexImag(op.loc, op.rhs);
727 mlir::Value newReal = createMul(op.loc, op.lhs, real);
728 mlir::Value newImag = createMul(op.loc, op.lhs, imag);
729 return builder.createComplexCreate(op.loc, newReal, newImag);
730}
731
732mlir::Value ComplexExprEmitter::emitBinDiv(const BinOpInfo &op) {
734 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, op.fpFeatures);
735
736 // Handle division between two complex values. In the case of complex integer
737 // types mixed with scalar integers, the scalar integer type will always be
738 // promoted to a complex integer value with a zero imaginary component when
739 // the AST is formed.
740 if (mlir::isa<cir::ComplexType>(op.lhs.getType()) &&
741 mlir::isa<cir::ComplexType>(op.rhs.getType())) {
742 cir::ComplexRangeKind rangeKind =
743 getComplexRangeAttr(op.fpFeatures.getComplexRange());
744 return builder.createComplexDiv(op.loc, op.lhs, op.rhs, rangeKind);
745 }
746
747 // The C99 standard (G.5.1) defines division of a complex value by a real
748 // value in the following simplified form.
749 if (mlir::isa<cir::ComplexType>(op.lhs.getType())) {
750 assert(mlir::cast<cir::ComplexType>(op.lhs.getType()).getElementType() ==
751 op.rhs.getType());
752 mlir::Value real = builder.createComplexReal(op.loc, op.lhs);
753 mlir::Value imag = builder.createComplexImag(op.loc, op.lhs);
754 mlir::Value newReal = builder.createFDiv(op.loc, real, op.rhs);
755 mlir::Value newImag = builder.createFDiv(op.loc, imag, op.rhs);
756 return builder.createComplexCreate(op.loc, newReal, newImag);
757 }
758
759 assert(mlir::isa<cir::ComplexType>(op.rhs.getType()));
760 cir::ConstantOp nullValue = builder.getNullValue(op.lhs.getType(), op.loc);
761 mlir::Value lhs = builder.createComplexCreate(op.loc, op.lhs, nullValue);
762 cir::ComplexRangeKind rangeKind =
763 getComplexRangeAttr(op.fpFeatures.getComplexRange());
764 return builder.createComplexDiv(op.loc, lhs, op.rhs, rangeKind);
765}
766
767mlir::Value CIRGenFunction::emitUnPromotedValue(mlir::Value result,
768 QualType unPromotionType) {
769 assert(!mlir::cast<cir::ComplexType>(result.getType()).isIntegerComplex() &&
770 "integral complex will never be promoted");
771 return builder.createCast(cir::CastKind::float_complex, result,
772 convertType(unPromotionType));
773}
774
775mlir::Value CIRGenFunction::emitPromotedValue(mlir::Value result,
776 QualType promotionType) {
777 assert(!mlir::cast<cir::ComplexType>(result.getType()).isIntegerComplex() &&
778 "integral complex will never be promoted");
779 return builder.createCast(cir::CastKind::float_complex, result,
780 convertType(promotionType));
781}
782
783mlir::Value ComplexExprEmitter::emitPromoted(const Expr *e,
784 QualType promotionTy) {
785 e = e->IgnoreParens();
786 if (const auto *bo = dyn_cast<BinaryOperator>(e)) {
787 switch (bo->getOpcode()) {
788#define HANDLE_BINOP(OP) \
789 case BO_##OP: \
790 return emitBin##OP(emitBinOps(bo, promotionTy));
791 HANDLE_BINOP(Add)
792 HANDLE_BINOP(Sub)
793 HANDLE_BINOP(Mul)
794 HANDLE_BINOP(Div)
795#undef HANDLE_BINOP
796 default:
797 break;
798 }
799 } else if (const auto *unaryOp = dyn_cast<UnaryOperator>(e)) {
800 switch (unaryOp->getOpcode()) {
801 case UO_Plus:
802 return VisitUnaryPlus(unaryOp, promotionTy);
803 case UO_Minus:
804 return VisitUnaryMinus(unaryOp, promotionTy);
805 default:
806 break;
807 }
808 }
809
810 mlir::Value result = Visit(const_cast<Expr *>(e));
811 if (!promotionTy.isNull())
812 return cgf.emitPromotedValue(result, promotionTy);
813
814 return result;
815}
816
818 QualType promotionType) {
819 return ComplexExprEmitter(*this).emitPromoted(e, promotionType);
820}
821
822mlir::Value
823ComplexExprEmitter::emitPromotedComplexOperand(const Expr *e,
824 QualType promotionTy) {
825 if (e->getType()->isAnyComplexType()) {
826 if (!promotionTy.isNull())
827 return cgf.emitPromotedComplexExpr(e, promotionTy);
828 return Visit(const_cast<Expr *>(e));
829 }
830
831 if (!promotionTy.isNull()) {
832 QualType complexElementTy =
833 promotionTy->castAs<ComplexType>()->getElementType();
834 return cgf.emitPromotedScalarExpr(e, complexElementTy);
835 }
836 return cgf.emitScalarExpr(e);
837}
838
839ComplexExprEmitter::BinOpInfo
840ComplexExprEmitter::emitBinOps(const BinaryOperator *e, QualType promotionTy) {
841 BinOpInfo binOpInfo{cgf.getLoc(e->getExprLoc())};
842 binOpInfo.lhs = emitPromotedComplexOperand(e->getLHS(), promotionTy);
843 binOpInfo.rhs = emitPromotedComplexOperand(e->getRHS(), promotionTy);
844 binOpInfo.ty = promotionTy.isNull() ? e->getType() : promotionTy;
845 binOpInfo.fpFeatures = e->getFPFeaturesInEffect(cgf.getLangOpts());
846 return binOpInfo;
847}
848
849LValue ComplexExprEmitter::emitCompoundAssignLValue(
850 const CompoundAssignOperator *e,
851 mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &), RValue &value) {
852 QualType lhsTy = e->getLHS()->getType().getAtomicUnqualifiedType();
853 QualType rhsTy = e->getRHS()->getType();
854 SourceLocation exprLoc = e->getExprLoc();
855 mlir::Location loc = cgf.getLoc(exprLoc);
856
857 BinOpInfo opInfo{loc};
858 opInfo.fpFeatures = e->getFPFeaturesInEffect(cgf.getLangOpts());
859
860 CIRGenFunction::CIRGenFPOptionsRAII fpOptsRAII(cgf, opInfo.fpFeatures);
861
862 // Load the RHS and LHS operands.
863 // __block variables need to have the rhs evaluated first, plus this should
864 // improve codegen a little.
865 QualType promotionTypeCR = getPromotionType(e->getComputationResultType());
866 opInfo.ty = promotionTypeCR.isNull() ? e->getComputationResultType()
867 : promotionTypeCR;
868
869 QualType complexElementTy =
870 opInfo.ty->castAs<ComplexType>()->getElementType();
871 QualType promotionTypeRHS = getPromotionType(rhsTy);
872
873 // The RHS should have been converted to the computation type.
874 if (e->getRHS()->getType()->isRealFloatingType()) {
875 if (!promotionTypeRHS.isNull()) {
876 opInfo.rhs = cgf.emitPromotedScalarExpr(e->getRHS(), promotionTypeRHS);
877 } else {
878 assert(cgf.getContext().hasSameUnqualifiedType(complexElementTy, rhsTy));
879 opInfo.rhs = cgf.emitScalarExpr(e->getRHS());
880 }
881 } else {
882 if (!promotionTypeRHS.isNull()) {
883 opInfo.rhs = cgf.emitPromotedComplexExpr(e->getRHS(), promotionTypeRHS);
884 } else {
885 assert(cgf.getContext().hasSameUnqualifiedType(opInfo.ty, rhsTy));
886 opInfo.rhs = Visit(e->getRHS());
887 }
888 }
889
890 LValue lhs = cgf.emitLValue(e->getLHS());
891
892 // Load from the l-value and convert it.
893 QualType promotionTypeLHS = getPromotionType(e->getComputationLHSType());
894 if (lhsTy->isAnyComplexType()) {
895 mlir::Value lhsValue = emitLoadOfLValue(lhs, exprLoc);
896 QualType destTy = promotionTypeLHS.isNull() ? opInfo.ty : promotionTypeLHS;
897 opInfo.lhs = emitComplexToComplexCast(lhsValue, lhsTy, destTy, exprLoc);
898 } else {
899 mlir::Value lhsVal = cgf.emitLoadOfScalar(lhs, exprLoc);
900 // For floating point real operands we can directly pass the scalar form
901 // to the binary operator emission and potentially get more efficient code.
902 if (lhsTy->isRealFloatingType()) {
903 QualType promotedComplexElementTy;
904 if (!promotionTypeLHS.isNull()) {
905 promotedComplexElementTy =
906 cast<ComplexType>(promotionTypeLHS)->getElementType();
907 if (!cgf.getContext().hasSameUnqualifiedType(promotedComplexElementTy,
908 promotionTypeLHS))
909 lhsVal = cgf.emitScalarConversion(lhsVal, lhsTy,
910 promotedComplexElementTy, exprLoc);
911 } else {
912 if (!cgf.getContext().hasSameUnqualifiedType(complexElementTy, lhsTy))
913 lhsVal = cgf.emitScalarConversion(lhsVal, lhsTy, complexElementTy,
914 exprLoc);
915 }
916 opInfo.lhs = lhsVal;
917 } else {
918 opInfo.lhs = emitScalarToComplexCast(lhsVal, lhsTy, opInfo.ty, exprLoc);
919 }
920 }
921
922 // Expand the binary operator.
923 mlir::Value result = (this->*func)(opInfo);
924
925 // Truncate the result and store it into the LHS lvalue.
926 if (lhsTy->isAnyComplexType()) {
927 mlir::Value resultValue =
928 emitComplexToComplexCast(result, opInfo.ty, lhsTy, exprLoc);
929 emitStoreOfComplex(loc, resultValue, lhs, /*isInit*/ false);
930 value = RValue::getComplex(resultValue);
931 } else {
932 mlir::Value resultValue =
933 cgf.emitComplexToScalarConversion(result, opInfo.ty, lhsTy, exprLoc);
934 cgf.emitStoreOfScalar(resultValue, lhs, /*isInit*/ false);
935 value = RValue::get(resultValue);
936 }
937
938 return lhs;
939}
940
941mlir::Value ComplexExprEmitter::emitCompoundAssign(
942 const CompoundAssignOperator *e,
943 mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &)) {
944 RValue val;
945 LValue lv = emitCompoundAssignLValue(e, func, val);
946
947 // The result of an assignment in C is the assigned r-value.
948 if (!cgf.getLangOpts().CPlusPlus)
949 return val.getComplexValue();
950
951 // If the lvalue is non-volatile, return the computed value of the assignment.
952 if (!lv.isVolatileQualified())
953 return val.getComplexValue();
954
955 return emitLoadOfLValue(lv, e->getExprLoc());
956}
957
958LValue ComplexExprEmitter::emitBinAssignLValue(const BinaryOperator *e,
959 mlir::Value &value) {
960 assert(cgf.getContext().hasSameUnqualifiedType(e->getLHS()->getType(),
961 e->getRHS()->getType()) &&
962 "Invalid assignment");
963
964 // Emit the RHS. __block variables need the RHS evaluated first.
965 value = Visit(e->getRHS());
966
967 // Compute the address to store into.
968 LValue lhs = cgf.emitLValue(e->getLHS());
969
970 // Store the result value into the LHS lvalue.
971 emitStoreOfComplex(cgf.getLoc(e->getExprLoc()), value, lhs,
972 /*isInit*/ false);
973 return lhs;
974}
975
976mlir::Value ComplexExprEmitter::VisitBinAssign(const BinaryOperator *e) {
977 mlir::Value value;
978 LValue lv = emitBinAssignLValue(e, value);
979
980 // The result of an assignment in C is the assigned r-value.
981 if (!cgf.getLangOpts().CPlusPlus)
982 return value;
983
984 // If the lvalue is non-volatile, return the computed value of the
985 // assignment.
986 if (!lv.isVolatile())
987 return value;
988
989 return emitLoadOfLValue(lv, e->getExprLoc());
990}
991
992mlir::Value ComplexExprEmitter::VisitBinComma(const BinaryOperator *e) {
993 cgf.emitIgnoredExpr(e->getLHS());
994 return Visit(e->getRHS());
995}
996
997mlir::Value ComplexExprEmitter::VisitAbstractConditionalOperator(
998 const AbstractConditionalOperator *e) {
999 mlir::Location loc = cgf.getLoc(e->getSourceRange());
1000
1001 // Bind the common expression if necessary.
1002 CIRGenFunction::OpaqueValueMapping binding(cgf, e);
1003
1004 Expr *cond = e->getCond()->IgnoreParens();
1005 mlir::Value condValue = cgf.evaluateExprAsBool(cond);
1006
1007 CIRGenFunction::ConditionalEvaluation eval(cgf, loc);
1008
1009 return cir::TernaryOp::create(
1010 builder, loc, condValue,
1011 /*trueBuilder=*/
1012 [&](mlir::OpBuilder &b, mlir::Location loc) {
1013 eval.beginEvaluation();
1014 mlir::Value trueValue = Visit(e->getTrueExpr());
1015 cir::YieldOp::create(b, loc, trueValue);
1016 eval.endEvaluation();
1017 },
1018 /*falseBuilder=*/
1019 [&](mlir::OpBuilder &b, mlir::Location loc) {
1020 eval.beginEvaluation();
1021 mlir::Value falseValue = Visit(e->getFalseExpr());
1022 cir::YieldOp::create(b, loc, falseValue);
1023 eval.endEvaluation();
1024 })
1025 .getResult();
1026}
1027
1028mlir::Value ComplexExprEmitter::VisitChooseExpr(ChooseExpr *e) {
1029 return Visit(e->getChosenSubExpr());
1030}
1031
1032mlir::Value ComplexExprEmitter::VisitInitListExpr(InitListExpr *e) {
1033 mlir::Location loc = cgf.getLoc(e->getExprLoc());
1034 if (e->getNumInits() == 2) {
1035 mlir::Value real = cgf.emitScalarExpr(e->getInit(0));
1036 mlir::Value imag = cgf.emitScalarExpr(e->getInit(1));
1037 return builder.createComplexCreate(loc, real, imag);
1038 }
1039
1040 if (e->getNumInits() == 1)
1041 return Visit(e->getInit(0));
1042
1043 assert(e->getNumInits() == 0 && "Unexpected number of inits");
1044 mlir::Type complexTy = cgf.convertType(e->getType());
1045 return builder.getNullValue(complexTy, loc);
1046}
1047
1048mlir::Value ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *e) {
1049 return cgf.emitVAArg(e);
1050}
1051
1052//===----------------------------------------------------------------------===//
1053// Entry Point into this File
1054//===----------------------------------------------------------------------===//
1055
1056/// EmitComplexExpr - Emit the computation of the specified expression of
1057/// complex type, ignoring the result.
1059 assert(e && getComplexType(e->getType()) &&
1060 "Invalid complex expression to emit");
1061
1062 return ComplexExprEmitter(*this).Visit(const_cast<Expr *>(e));
1063}
1064
1066 bool isInit) {
1067 assert(e && getComplexType(e->getType()) &&
1068 "Invalid complex expression to emit");
1069 ComplexExprEmitter emitter(*this);
1070 mlir::Value value = emitter.Visit(const_cast<Expr *>(e));
1071 emitter.emitStoreOfComplex(getLoc(e->getExprLoc()), value, dest, isInit);
1072}
1073
1074/// EmitStoreOfComplex - Store a complex number into the specified l-value.
1075void CIRGenFunction::emitStoreOfComplex(mlir::Location loc, mlir::Value v,
1076 LValue dest, bool isInit) {
1077 ComplexExprEmitter(*this).emitStoreOfComplex(loc, v, dest, isInit);
1078}
1079
1081 return ComplexExprEmitter(*this).emitLoadOfLValue(src, loc);
1082}
1083
1085 assert(e->getOpcode() == BO_Assign && "Expected assign op");
1086
1087 mlir::Value value; // ignored
1088 LValue lvalue = ComplexExprEmitter(*this).emitBinAssignLValue(e, value);
1090
1091 return lvalue;
1092}
1093
1095 mlir::Value (ComplexExprEmitter::*)(const ComplexExprEmitter::BinOpInfo &);
1096
1098 switch (op) {
1099 case BO_MulAssign:
1100 return &ComplexExprEmitter::emitBinMul;
1101 case BO_DivAssign:
1102 return &ComplexExprEmitter::emitBinDiv;
1103 case BO_SubAssign:
1104 return &ComplexExprEmitter::emitBinSub;
1105 case BO_AddAssign:
1106 return &ComplexExprEmitter::emitBinAdd;
1107 default:
1108 llvm_unreachable("unexpected complex compound assignment");
1109 }
1110}
1111
1113 const CompoundAssignOperator *e) {
1115 RValue val;
1116 return ComplexExprEmitter(*this).emitCompoundAssignLValue(e, op, val);
1117}
1118
1120 LValue lv) {
1121 mlir::Value inVal = emitLoadOfComplex(lv, e->getExprLoc());
1122 mlir::Location loc = getLoc(e->getExprLoc());
1123
1124 // Increment/decrement only the real component of the complex value.
1125 mlir::Value real = builder.createComplexReal(loc, inVal);
1126 mlir::Value imag = builder.createComplexImag(loc, inVal);
1127
1128 mlir::Value resultReal;
1129 if (cir::isFPOrVectorOfFPType(real.getType())) {
1130 auto fpType = mlir::cast<cir::FPTypeInterface>(real.getType());
1131 mlir::Value amount = builder.getConstFP(
1132 loc, real.getType(), llvm::APFloat(fpType.getFloatSemantics(), 1));
1133 resultReal = e->isIncrementOp() ? builder.createFAdd(loc, real, amount)
1134 : builder.createFSub(loc, real, amount);
1135 } else {
1136 resultReal = e->isIncrementOp() ? builder.createInc(loc, real)
1137 : builder.createDec(loc, real);
1138 }
1139
1140 mlir::Value incVal = builder.createComplexCreate(loc, resultReal, imag);
1141
1142 // Store the updated result through the lvalue.
1143 emitStoreOfComplex(loc, incVal, lv, /*isInit=*/false);
1144
1146
1147 // If this is a postinc, return the value read from memory, otherwise use the
1148 // updated value.
1149 return e->isPrefix() ? incVal : inVal;
1150}
1151
1153 const CompoundAssignOperator *e, mlir::Value &result) {
1154 // Key Instructions: Don't need to create an atom group here; one will already
1155 // be active through scalar handling code.
1157 RValue value;
1158 LValue ret = ComplexExprEmitter(*this).emitCompoundAssignLValue(e, op, value);
1159 result = value.getValue();
1160 return ret;
1161}
#define HANDLEBINOP(OP)
static CompoundFunc getComplexOp(BinaryOperatorKind op)
static const ComplexType * getComplexType(QualType type)
Return the complex type that we are meant to emit.
mlir::Value(ComplexExprEmitter::*)(const ComplexExprEmitter::BinOpInfo &) CompoundFunc
static cir::ComplexRangeKind getComplexRangeAttr(LangOptions::ComplexRangeKind range)
#define HANDLE_BINOP(OP)
mlir::Value createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowFlags flags=OverflowFlags::None)
mlir::Value createComplexMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, cir::ComplexRangeKind rangeKind)
mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowFlags flags=OverflowFlags::None)
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc)
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr)
mlir::Value createCast(mlir::Location loc, cir::CastKind kind, mlir::Value src, mlir::Type newTy)
mlir::Value createFDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createFNeg(mlir::Location loc, mlir::Value operand)
mlir::Value createComplexSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
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 createComplexDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, cir::ComplexRangeKind rangeKind)
mlir::Value createFMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createMinus(mlir::Location loc, mlir::Value input, bool nsw=false)
mlir::Value createComplexCreate(mlir::Location loc, mlir::Value real, mlir::Value imag)
mlir::Value createComplexConj(mlir::Location loc, mlir::Value operand)
mlir::Value createFSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createComplexAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createComplexReal(mlir::Location loc, mlir::Value operand)
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 ?
Definition Expr.h:4600
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition Expr.h:4606
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition Expr.h:4612
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4107
Expr * getLHS() const
Definition Expr.h:4157
SourceLocation getExprLoc() const
Definition Expr.h:4148
Expr * getRHS() const
Definition Expr.h:4159
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Get the FP features status of this operator.
Definition Expr.h:4320
Opcode getOpcode() const
Definition Expr.h:4152
bool isValid() const
Definition Address.h:77
mlir::Value createNeg(mlir::Location loc, mlir::Value value, bool nsw=false)
cir::LoadOp createLoad(mlir::Location loc, Address addr, bool isVolatile=false, bool isNontemporal=false)
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, Address dst, bool isVolatile=false, bool isNontemporal=false, mlir::IntegerAttr align={}, cir::SyncScopeKindAttr scope={}, cir::MemOrderAttr order={})
LValue getReferenceLValue(CIRGenFunction &cgf, const Expr *refExpr) const
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,...
LValue emitComplexCompoundAssignmentLValue(const CompoundAssignOperator *e)
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
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...
mlir::Value emitPromotedComplexExpr(const Expr *e, QualType promotionType)
mlir::Value emitUnPromotedValue(mlir::Value result, QualType unPromotionType)
RValue emitAtomicLoad(LValue lvalue, SourceLocation loc, AggValueSlot slot=AggValueSlot::ignored())
mlir::Type convertTypeForMem(QualType t)
mlir::Value emitComplexPrePostIncDec(const UnaryOperator *e, LValue lv)
mlir::Value emitLoadOfComplex(LValue src, SourceLocation loc)
Load a complex number from the specified l-value.
void emitStoreOfScalar(mlir::Value value, Address addr, bool isVolatile, clang::QualType ty, LValueBaseInfo baseInfo, bool isInit=false, bool isNontemporal=false)
void emitStoreOfComplex(mlir::Location loc, mlir::Value v, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
LValue emitComplexAssignmentLValue(const BinaryOperator *e)
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)
mlir::Value emitLoadOfScalar(LValue lvalue, SourceLocation loc)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
void emitComplexExprIntoLValue(const Expr *e, LValue dest, bool isInit)
LValue makeAddrLValue(Address addr, QualType ty, AlignmentSource source=AlignmentSource::Type)
void emitAtomicStore(RValue rvalue, LValue dest, bool isInit)
clang::ASTContext & getContext() const
mlir::LogicalResult emitCompoundStmt(const clang::CompoundStmt &s, Address *lastValue=nullptr, AggValueSlot slot=AggValueSlot::ignored())
bool isLValueSuitableForInlineAtomic(LValue lv)
An LValue is a candidate for having its loads and stores be made atomic if we are operating under /vo...
void emitIgnoredExpr(const clang::Expr *e)
Emit code to compute the specified expression, ignoring the result.
Address createMemTemp(QualType t, mlir::Location loc, const Twine &name="tmp", Address *alloca=nullptr, mlir::OpBuilder::InsertPoint ip={})
Create a temporary memory object of the given type, with appropriate alignmen and cast it to the defa...
mlir::Value emitVAArg(VAArgExpr *ve)
Generate code to get an argument from the passed in pointer and update it accordingly.
void errorUnsupported(const Stmt *s, llvm::StringRef type)
Print out an error that codegen doesn't support the specified stmt yet.
This trivial value class is used to represent the result of an expression that is evaluated.
Definition CIRGenValue.h:33
static RValue get(mlir::Value v)
Definition CIRGenValue.h:83
static RValue getComplex(mlir::Value v)
Definition CIRGenValue.h:91
mlir::Value getValue() const
Return the value of this scalar value.
Definition CIRGenValue.h:57
mlir::Value getComplexValue() const
Return the value of this complex value.
Definition CIRGenValue.h:63
Expr * getExpr()
Get the initialization expression that will be used.
Definition ExprCXX.cpp:1137
A rewritten comparison expression that was originally written using operator syntax.
Definition ExprCXX.h:291
Expr * getSemanticForm()
Get an equivalent semantic form for this expression.
Definition ExprCXX.h:309
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
Definition Expr.cpp:1635
CastKind getCastKind() const
Definition Expr.h:3789
bool changesVolatileQualification() const
Return.
Definition Expr.h:3879
Expr * getSubExpr()
Definition Expr.h:3795
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition Expr.h:4953
Complex values, per C99 6.2.5p11.
Definition TypeBase.h:3362
CompoundAssignOperator - For compound assignments (e.g.
Definition Expr.h:4369
QualType getComputationLHSType() const
Definition Expr.h:4403
QualType getComputationResultType() const
Definition Expr.h:4406
This represents one expression.
Definition Expr.h:114
bool isGLValue() const
Definition Expr.h:289
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3125
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:283
QualType getType() const
Definition Expr.h:146
const Expr * getSubExpr() const
Definition Expr.h:1107
Expr * getResultExpr()
Return the result expression of this controlling expression.
Definition Expr.h:6554
const Expr * getSubExpr() const
Definition Expr.h:1788
unsigned getNumInits() const
Definition Expr.h:5410
const Expr * getInit(unsigned Init) const
Definition Expr.h:5443
ComplexRangeKind
Controls the various implementations for complex multiplication and.
@ CX_Full
Implementation of complex division and multiplication using a call to runtime library functions(gener...
@ CX_Basic
Implementation of complex division and multiplication using algebraic formulas at source precision.
@ CX_Promoted
Implementation of complex division using algebraic formulas at higher precision.
@ CX_None
No range rule is enabled.
@ CX_Improved
Implementation of complex division offering an improved handling for overflow in intermediate calcula...
Expr * getBase() const
Definition Expr.h:3510
SourceLocation getExprLoc() const LLVM_READONLY
Definition Expr.h:1253
Expr * getSelectedExpr() const
Definition ExprCXX.h:4693
const Expr * getSubExpr() const
Definition Expr.h:2268
SourceLocation getExprLoc() const LLVM_READONLY
Definition Expr.h:6976
A (possibly-)qualified type.
Definition TypeBase.h:938
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1005
bool UseExcessPrecision(const ASTContext &Ctx)
Definition Type.cpp:1775
QualType getAtomicUnqualifiedType() const
Remove all qualifiers including _Atomic.
Definition Type.cpp:1841
Encodes a location in the source.
CompoundStmt * getSubStmt()
Definition Expr.h:4681
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...
Definition Stmt.cpp:343
void dump() const
Dumps the specified AST fragment and all subtrees to llvm::errs().
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9248
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9498
bool isReferenceType() const
Definition TypeBase.h:8839
bool isAnyComplexType() const
Definition TypeBase.h:8950
bool isRealFloatingType() const
Floating point categories.
Definition Type.cpp:2535
bool isFloatingType() const
Definition Type.cpp:2519
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9431
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2313
SourceLocation getExprLoc() const
Definition Expr.h:2437
Expr * getSubExpr() const
Definition Expr.h:2354
static bool isIncrementOp(Opcode Op)
Definition Expr.h:2395
static bool isPrefix(Opcode Op)
isPrefix - Return true if this is a prefix operation, like –x.
Definition Expr.h:2388
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
Top level wrappers for InstallAPI frontend operations.
CastKind
CastKind - The kind of operation required for a conversion.
U cast(CodeGen::Address addr)
Definition Address.h:327
static bool fastMathFlags()
static bool openMPLastprivateConditional()