clang 23.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 cgf.cgm.errorNYI(s->getExprLoc(), "ComplexExprEmitter VisitCoawaitExpr");
90 return {};
91 }
92 mlir::Value VisitCoyieldExpr(CoyieldExpr *s) {
93 cgf.cgm.errorNYI(s->getExprLoc(), "ComplexExprEmitter VisitCoyieldExpr");
94 return {};
95 }
96 mlir::Value VisitUnaryCoawait(const UnaryOperator *e) {
97 cgf.cgm.errorNYI(e->getExprLoc(), "ComplexExprEmitter VisitUnaryCoawait");
98 return {};
99 }
100
101 mlir::Value emitConstant(const CIRGenFunction::ConstantEmission &constant,
102 Expr *e) {
103 assert(constant && "not a constant");
104 if (constant.isReference())
105 return emitLoadOfLValue(constant.getReferenceLValue(cgf, e),
106 e->getExprLoc());
107
108 mlir::TypedAttr valueAttr = constant.getValue();
109 return builder.getConstant(cgf.getLoc(e->getSourceRange()), valueAttr);
110 }
111
112 // l-values.
113 mlir::Value VisitDeclRefExpr(DeclRefExpr *e) {
114 if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(e))
115 return emitConstant(constant, e);
116 return emitLoadOfLValue(e);
117 }
118 mlir::Value VisitObjCIvarRefExpr(ObjCIvarRefExpr *e) {
119 cgf.cgm.errorNYI(e->getExprLoc(),
120 "ComplexExprEmitter VisitObjCIvarRefExpr");
121 return {};
122 }
123 mlir::Value VisitObjCMessageExpr(ObjCMessageExpr *e) {
124 cgf.cgm.errorNYI(e->getExprLoc(),
125 "ComplexExprEmitter VisitObjCMessageExpr");
126 return {};
127 }
128 mlir::Value VisitArraySubscriptExpr(Expr *e) { return emitLoadOfLValue(e); }
129 mlir::Value VisitMemberExpr(MemberExpr *me) {
130 if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(me)) {
131 cgf.emitIgnoredExpr(me->getBase());
132 return emitConstant(constant, me);
133 }
134 return emitLoadOfLValue(me);
135 }
136 mlir::Value VisitOpaqueValueExpr(OpaqueValueExpr *e) {
137 if (e->isGLValue())
138 return emitLoadOfLValue(cgf.getOrCreateOpaqueLValueMapping(e),
139 e->getExprLoc());
140 return cgf.getOrCreateOpaqueRValueMapping(e).getComplexValue();
141 }
142
143 mlir::Value VisitPseudoObjectExpr(PseudoObjectExpr *e) {
144 cgf.cgm.errorNYI(e->getExprLoc(),
145 "ComplexExprEmitter VisitPseudoObjectExpr");
146 return {};
147 }
148
149 mlir::Value emitCast(CastKind ck, Expr *op, QualType destTy);
150 mlir::Value VisitImplicitCastExpr(ImplicitCastExpr *e) {
151 // Unlike for scalars, we don't have to worry about function->ptr demotion
152 // here.
154 return emitLoadOfLValue(e);
155 return emitCast(e->getCastKind(), e->getSubExpr(), e->getType());
156 }
157 mlir::Value VisitCastExpr(CastExpr *e) {
158 if (const auto *ece = dyn_cast<ExplicitCastExpr>(e)) {
159 // Bind VLAs in the cast type.
160 if (ece->getType()->isVariablyModifiedType()) {
161 cgf.cgm.errorNYI(e->getExprLoc(),
162 "VisitCastExpr Bind VLAs in the cast type");
163 return {};
164 }
165 }
166
168 return emitLoadOfLValue(e);
169
170 return emitCast(e->getCastKind(), e->getSubExpr(), e->getType());
171 }
172 mlir::Value VisitCallExpr(const CallExpr *e);
173 mlir::Value VisitStmtExpr(const StmtExpr *e);
174
175 // Operators.
176 mlir::Value VisitPrePostIncDec(const UnaryOperator *e) {
177 LValue lv = cgf.emitLValue(e->getSubExpr());
178 return cgf.emitComplexPrePostIncDec(e, lv);
179 }
180 mlir::Value VisitUnaryPostDec(const UnaryOperator *e) {
181 return VisitPrePostIncDec(e);
182 }
183 mlir::Value VisitUnaryPostInc(const UnaryOperator *e) {
184 return VisitPrePostIncDec(e);
185 }
186 mlir::Value VisitUnaryPreDec(const UnaryOperator *e) {
187 return VisitPrePostIncDec(e);
188 }
189 mlir::Value VisitUnaryPreInc(const UnaryOperator *e) {
190 return VisitPrePostIncDec(e);
191 }
192 mlir::Value VisitUnaryDeref(const Expr *e) { return emitLoadOfLValue(e); }
193
194 mlir::Value VisitUnaryPlus(const UnaryOperator *e);
195 mlir::Value VisitUnaryPlus(const UnaryOperator *e, QualType promotionType);
196 mlir::Value VisitUnaryMinus(const UnaryOperator *e);
197 mlir::Value VisitUnaryMinus(const UnaryOperator *e, QualType promotionType);
198 mlir::Value VisitUnaryNot(const UnaryOperator *e);
199 // LNot,Real,Imag never return complex.
200 mlir::Value VisitUnaryExtension(const UnaryOperator *e) {
201 return Visit(e->getSubExpr());
202 }
203 mlir::Value VisitCXXDefaultArgExpr(CXXDefaultArgExpr *dae) {
204 CIRGenFunction::CXXDefaultArgExprScope scope(cgf, dae);
205 return Visit(dae->getExpr());
206 }
207 mlir::Value VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die) {
208 CIRGenFunction::CXXDefaultInitExprScope scope(cgf, die);
209 return Visit(die->getExpr());
210 }
211 mlir::Value VisitExprWithCleanups(ExprWithCleanups *e) {
212 CIRGenFunction::RunCleanupsScope scope(cgf);
213 mlir::Value complexVal = Visit(e->getSubExpr());
214 // Defend against dominance problems caused by jumps out of expression
215 // evaluation through the shared cleanup block.
216 scope.forceCleanup({&complexVal});
217 return complexVal;
218 }
219 mlir::Value VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e) {
220 mlir::Location loc = cgf.getLoc(e->getExprLoc());
221 mlir::Type complexTy = cgf.convertType(e->getType());
222 return builder.getNullValue(complexTy, loc);
223 }
224 mlir::Value VisitImplicitValueInitExpr(ImplicitValueInitExpr *e) {
225 mlir::Location loc = cgf.getLoc(e->getExprLoc());
226 mlir::Type complexTy = cgf.convertType(e->getType());
227 return builder.getNullValue(complexTy, loc);
228 }
229
230 struct BinOpInfo {
231 mlir::Location loc;
232 mlir::Value lhs{};
233 mlir::Value rhs{};
234 QualType ty{}; // Computation Type.
235 FPOptions fpFeatures{};
236 };
237
238 BinOpInfo emitBinOps(const BinaryOperator *e,
239 QualType promotionTy = QualType());
240
241 mlir::Value emitPromoted(const Expr *e, QualType promotionTy);
242 mlir::Value emitPromotedComplexOperand(const Expr *e, QualType promotionTy);
243 LValue emitCompoundAssignLValue(
244 const CompoundAssignOperator *e,
245 mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &),
246 RValue &value);
247 mlir::Value emitCompoundAssign(
248 const CompoundAssignOperator *e,
249 mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &));
250
251 mlir::Value emitBinAdd(const BinOpInfo &op);
252 mlir::Value emitBinSub(const BinOpInfo &op);
253 mlir::Value emitBinMul(const BinOpInfo &op);
254 mlir::Value emitBinDiv(const BinOpInfo &op);
255
256 QualType getPromotionType(QualType ty, bool isDivOpCode = false) {
257 if (auto *complexTy = ty->getAs<ComplexType>()) {
258 QualType elementTy = complexTy->getElementType();
259 if (elementTy.UseExcessPrecision(cgf.getContext()))
260 return cgf.getContext().getComplexType(cgf.getContext().FloatTy);
261 }
262
263 if (ty.UseExcessPrecision(cgf.getContext()))
264 return cgf.getContext().FloatTy;
265 return QualType();
266 }
267
268#define HANDLEBINOP(OP) \
269 mlir::Value VisitBin##OP(const BinaryOperator *e) { \
270 QualType promotionTy = getPromotionType( \
271 e->getType(), e->getOpcode() == BinaryOperatorKind::BO_Div); \
272 mlir::Value result = emitBin##OP(emitBinOps(e, promotionTy)); \
273 if (!promotionTy.isNull()) \
274 result = cgf.emitUnPromotedValue(result, e->getType()); \
275 return result; \
276 }
277
278 HANDLEBINOP(Add)
279 HANDLEBINOP(Sub)
280 HANDLEBINOP(Mul)
281 HANDLEBINOP(Div)
282#undef HANDLEBINOP
283
284 mlir::Value VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *e) {
285 cgf.cgm.errorNYI(e->getExprLoc(),
286 "ComplexExprEmitter VisitCXXRewrittenBinaryOperator");
287 return {};
288 }
289
290 // Compound assignments.
291 mlir::Value VisitBinAddAssign(const CompoundAssignOperator *e) {
292 return emitCompoundAssign(e, &ComplexExprEmitter::emitBinAdd);
293 }
294 mlir::Value VisitBinSubAssign(const CompoundAssignOperator *e) {
295 return emitCompoundAssign(e, &ComplexExprEmitter::emitBinSub);
296 }
297 mlir::Value VisitBinMulAssign(const CompoundAssignOperator *e) {
298 return emitCompoundAssign(e, &ComplexExprEmitter::emitBinMul);
299 }
300 mlir::Value VisitBinDivAssign(const CompoundAssignOperator *e) {
301 return emitCompoundAssign(e, &ComplexExprEmitter::emitBinDiv);
302 }
303
304 // GCC rejects rem/and/or/xor for integer complex.
305 // Logical and/or always return int, never complex.
306
307 // No comparisons produce a complex result.
308
309 LValue emitBinAssignLValue(const BinaryOperator *e, mlir::Value &val);
310 mlir::Value VisitBinAssign(const BinaryOperator *e);
311 mlir::Value VisitBinComma(const BinaryOperator *e);
312
313 mlir::Value
314 VisitAbstractConditionalOperator(const AbstractConditionalOperator *e);
315 mlir::Value VisitChooseExpr(ChooseExpr *e);
316
317 mlir::Value VisitInitListExpr(InitListExpr *e);
318
319 mlir::Value VisitCompoundLiteralExpr(CompoundLiteralExpr *e) {
320 return emitLoadOfLValue(e);
321 }
322
323 mlir::Value VisitVAArgExpr(VAArgExpr *e);
324
325 mlir::Value VisitAtomicExpr(AtomicExpr *e) {
326 return cgf.emitAtomicExpr(e).getComplexValue();
327 }
328
329 mlir::Value VisitPackIndexingExpr(PackIndexingExpr *e) {
330 return Visit(e->getSelectedExpr());
331 }
332};
333} // namespace
334
335//===----------------------------------------------------------------------===//
336// Utilities
337//===----------------------------------------------------------------------===//
338
339/// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
340/// load the real and imaginary pieces, returning them as Real/Imag.
341mlir::Value ComplexExprEmitter::emitLoadOfLValue(LValue lv,
342 SourceLocation loc) {
343 assert(lv.isSimple() && "non-simple complex l-value?");
344 if (lv.getType()->isAtomicType())
345 cgf.cgm.errorNYI(loc, "emitLoadOfLValue with Atomic LV");
346
347 const Address srcAddr = lv.getAddress();
348 return builder.createLoad(cgf.getLoc(loc), srcAddr, lv.isVolatileQualified());
349}
350
351/// EmitStoreOfComplex - Store the specified real/imag parts into the
352/// specified value pointer.
353void ComplexExprEmitter::emitStoreOfComplex(mlir::Location loc, mlir::Value val,
354 LValue lv, bool isInit) {
355 if (lv.getType()->isAtomicType() ||
356 (!isInit && cgf.isLValueSuitableForInlineAtomic(lv))) {
357 cgf.cgm.errorNYI(loc, "StoreOfComplex with Atomic LV");
358 return;
359 }
360
361 const Address destAddr = lv.getAddress();
362 builder.createStore(loc, val, destAddr, lv.isVolatileQualified());
363}
364
365//===----------------------------------------------------------------------===//
366// Visitor Methods
367//===----------------------------------------------------------------------===//
368
369mlir::Value ComplexExprEmitter::VisitExpr(Expr *e) {
370 cgf.cgm.errorNYI(e->getExprLoc(), "ComplexExprEmitter VisitExpr");
371 return {};
372}
373
374mlir::Value
375ComplexExprEmitter::VisitImaginaryLiteral(const ImaginaryLiteral *il) {
376 auto ty = mlir::cast<cir::ComplexType>(cgf.convertType(il->getType()));
377 mlir::Type elementTy = ty.getElementType();
378 mlir::Location loc = cgf.getLoc(il->getExprLoc());
379
380 mlir::TypedAttr realValueAttr;
381 mlir::TypedAttr imagValueAttr;
382
383 if (mlir::isa<cir::IntType>(elementTy)) {
384 llvm::APInt imagValue = cast<IntegerLiteral>(il->getSubExpr())->getValue();
385 realValueAttr = cir::IntAttr::get(elementTy, 0);
386 imagValueAttr = cir::IntAttr::get(elementTy, imagValue);
387 } else {
388 assert(mlir::isa<cir::FPTypeInterface>(elementTy) &&
389 "Expected complex element type to be floating-point");
390
391 llvm::APFloat imagValue =
392 cast<FloatingLiteral>(il->getSubExpr())->getValue();
393 realValueAttr = cir::FPAttr::get(
394 elementTy, llvm::APFloat::getZero(imagValue.getSemantics()));
395 imagValueAttr = cir::FPAttr::get(elementTy, imagValue);
396 }
397
398 auto complexAttr = cir::ConstComplexAttr::get(realValueAttr, imagValueAttr);
399 return cir::ConstantOp::create(builder, loc, complexAttr);
400}
401
402mlir::Value ComplexExprEmitter::VisitCallExpr(const CallExpr *e) {
404 return emitLoadOfLValue(e);
405 return cgf.emitCallExpr(e).getComplexValue();
406}
407
408mlir::Value ComplexExprEmitter::VisitStmtExpr(const StmtExpr *e) {
409 CIRGenFunction::StmtExprEvaluation eval(cgf);
410 Address retAlloca =
411 cgf.createMemTemp(e->getType(), cgf.getLoc(e->getSourceRange()));
412 (void)cgf.emitCompoundStmt(*e->getSubStmt(), &retAlloca);
413 assert(retAlloca.isValid() && "Expected complex return value");
414 return emitLoadOfLValue(cgf.makeAddrLValue(retAlloca, e->getType()),
415 e->getExprLoc());
416}
417
418mlir::Value ComplexExprEmitter::emitComplexToComplexCast(mlir::Value val,
419 QualType srcType,
420 QualType destType,
421 SourceLocation loc) {
422 if (srcType == destType)
423 return val;
424
425 // Get the src/dest element type.
426 QualType srcElemTy = srcType->castAs<ComplexType>()->getElementType();
427 QualType destElemTy = destType->castAs<ComplexType>()->getElementType();
428
429 cir::CastKind castOpKind;
430 if (srcElemTy->isFloatingType() && destElemTy->isFloatingType())
431 castOpKind = cir::CastKind::float_complex;
432 else if (srcElemTy->isFloatingType() && destElemTy->isIntegerType())
433 castOpKind = cir::CastKind::float_complex_to_int_complex;
434 else if (srcElemTy->isIntegerType() && destElemTy->isFloatingType())
435 castOpKind = cir::CastKind::int_complex_to_float_complex;
436 else if (srcElemTy->isIntegerType() && destElemTy->isIntegerType())
437 castOpKind = cir::CastKind::int_complex;
438 else
439 llvm_unreachable("unexpected src type or dest type");
440
441 return builder.createCast(cgf.getLoc(loc), castOpKind, val,
442 cgf.convertType(destType));
443}
444
445mlir::Value ComplexExprEmitter::emitScalarToComplexCast(mlir::Value val,
446 QualType srcType,
447 QualType destType,
448 SourceLocation loc) {
449 cir::CastKind castOpKind;
450 if (srcType->isFloatingType())
451 castOpKind = cir::CastKind::float_to_complex;
452 else if (srcType->isIntegerType())
453 castOpKind = cir::CastKind::int_to_complex;
454 else
455 llvm_unreachable("unexpected src type");
456
457 return builder.createCast(cgf.getLoc(loc), castOpKind, val,
458 cgf.convertType(destType));
459}
460
461mlir::Value ComplexExprEmitter::emitCast(CastKind ck, Expr *op,
462 QualType destTy) {
463 switch (ck) {
464 case CK_Dependent:
465 llvm_unreachable("dependent type must be resolved before the CIR codegen");
466
467 case CK_NoOp:
468 case CK_LValueToRValue:
469 case CK_UserDefinedConversion:
470 return Visit(op);
471
472 case CK_AtomicToNonAtomic:
473 case CK_NonAtomicToAtomic: {
474 cgf.cgm.errorNYI("ComplexExprEmitter::emitCast Atmoic");
475 return {};
476 }
477
478 case CK_LValueBitCast: {
479 LValue origLV = cgf.emitLValue(op);
480 Address addr =
481 origLV.getAddress().withElementType(builder, cgf.convertType(destTy));
482 LValue destLV = cgf.makeAddrLValue(addr, destTy);
483 return emitLoadOfLValue(destLV, op->getExprLoc());
484 }
485
486 case CK_LValueToRValueBitCast: {
487 LValue sourceLVal = cgf.emitLValue(op);
488 Address addr = sourceLVal.getAddress().withElementType(
489 builder, cgf.convertTypeForMem(destTy));
490 LValue destLV = cgf.makeAddrLValue(addr, destTy);
492 return emitLoadOfLValue(destLV, op->getExprLoc());
493 }
494
495 case CK_BitCast:
496 case CK_BaseToDerived:
497 case CK_DerivedToBase:
498 case CK_UncheckedDerivedToBase:
499 case CK_Dynamic:
500 case CK_ToUnion:
501 case CK_ArrayToPointerDecay:
502 case CK_FunctionToPointerDecay:
503 case CK_NullToPointer:
504 case CK_NullToMemberPointer:
505 case CK_BaseToDerivedMemberPointer:
506 case CK_DerivedToBaseMemberPointer:
507 case CK_MemberPointerToBoolean:
508 case CK_ReinterpretMemberPointer:
509 case CK_ConstructorConversion:
510 case CK_IntegralToPointer:
511 case CK_PointerToIntegral:
512 case CK_PointerToBoolean:
513 case CK_ToVoid:
514 case CK_VectorSplat:
515 case CK_IntegralCast:
516 case CK_BooleanToSignedIntegral:
517 case CK_IntegralToBoolean:
518 case CK_IntegralToFloating:
519 case CK_FloatingToIntegral:
520 case CK_FloatingToBoolean:
521 case CK_FloatingCast:
522 case CK_CPointerToObjCPointerCast:
523 case CK_BlockPointerToObjCPointerCast:
524 case CK_AnyPointerToBlockPointerCast:
525 case CK_ObjCObjectLValueCast:
526 case CK_FloatingComplexToReal:
527 case CK_FloatingComplexToBoolean:
528 case CK_IntegralComplexToReal:
529 case CK_IntegralComplexToBoolean:
530 case CK_ARCProduceObject:
531 case CK_ARCConsumeObject:
532 case CK_ARCReclaimReturnedObject:
533 case CK_ARCExtendBlockObject:
534 case CK_CopyAndAutoreleaseBlockObject:
535 case CK_BuiltinFnToFnPtr:
536 case CK_ZeroToOCLOpaqueType:
537 case CK_AddressSpaceConversion:
538 case CK_IntToOCLSampler:
539 case CK_FloatingToFixedPoint:
540 case CK_FixedPointToFloating:
541 case CK_FixedPointCast:
542 case CK_FixedPointToBoolean:
543 case CK_FixedPointToIntegral:
544 case CK_IntegralToFixedPoint:
545 case CK_MatrixCast:
546 case CK_HLSLVectorTruncation:
547 case CK_HLSLMatrixTruncation:
548 case CK_HLSLArrayRValue:
549 case CK_HLSLElementwiseCast:
550 case CK_HLSLAggregateSplatCast:
551 llvm_unreachable("invalid cast kind for complex value");
552
553 case CK_FloatingRealToComplex:
554 case CK_IntegralRealToComplex: {
555 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, op);
556 return emitScalarToComplexCast(cgf.emitScalarExpr(op), op->getType(),
557 destTy, op->getExprLoc());
558 }
559
560 case CK_FloatingComplexCast:
561 case CK_FloatingComplexToIntegralComplex:
562 case CK_IntegralComplexCast:
563 case CK_IntegralComplexToFloatingComplex: {
564 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, op);
565 return emitComplexToComplexCast(Visit(op), op->getType(), destTy,
566 op->getExprLoc());
567 }
568 }
569
570 llvm_unreachable("unknown cast resulting in complex value");
571}
572
573mlir::Value ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *e) {
574 QualType promotionTy = getPromotionType(e->getSubExpr()->getType());
575 mlir::Value result = VisitUnaryPlus(e, promotionTy);
576 if (!promotionTy.isNull())
577 return cgf.emitUnPromotedValue(result, e->getSubExpr()->getType());
578 return result;
579}
580
581mlir::Value ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *e,
582 QualType promotionType) {
583 if (!promotionType.isNull())
584 return cgf.emitPromotedComplexExpr(e->getSubExpr(), promotionType);
585 return Visit(e->getSubExpr());
586}
587
588mlir::Value ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *e) {
589 QualType promotionTy = getPromotionType(e->getSubExpr()->getType());
590 mlir::Value result = VisitUnaryMinus(e, promotionTy);
591 if (!promotionTy.isNull())
592 return cgf.emitUnPromotedValue(result, e->getSubExpr()->getType());
593 return result;
594}
595
596mlir::Value ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *e,
597 QualType promotionType) {
598 mlir::Value op;
599 if (!promotionType.isNull())
600 op = cgf.emitPromotedComplexExpr(e->getSubExpr(), promotionType);
601 else
602 op = Visit(e->getSubExpr());
603 return builder.createMinus(cgf.getLoc(e->getExprLoc()), op);
604}
605
606mlir::Value ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *e) {
607 mlir::Value op = Visit(e->getSubExpr());
608 return builder.createNot(op);
609}
610
611mlir::Value ComplexExprEmitter::emitBinAdd(const BinOpInfo &op) {
613 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, op.fpFeatures);
614
615 if (mlir::isa<cir::ComplexType>(op.lhs.getType()) &&
616 mlir::isa<cir::ComplexType>(op.rhs.getType()))
617 return cir::ComplexAddOp::create(builder, op.loc, op.lhs, op.rhs);
618
619 if (mlir::isa<cir::ComplexType>(op.lhs.getType())) {
620 mlir::Value real = builder.createComplexReal(op.loc, op.lhs);
621 mlir::Value imag = builder.createComplexImag(op.loc, op.lhs);
622 mlir::Value newReal = builder.createAdd(op.loc, real, op.rhs);
623 return builder.createComplexCreate(op.loc, newReal, imag);
624 }
625
626 assert(mlir::isa<cir::ComplexType>(op.rhs.getType()));
627 mlir::Value real = builder.createComplexReal(op.loc, op.rhs);
628 mlir::Value imag = builder.createComplexImag(op.loc, op.rhs);
629 mlir::Value newReal = builder.createAdd(op.loc, op.lhs, real);
630 return builder.createComplexCreate(op.loc, newReal, imag);
631}
632
633mlir::Value ComplexExprEmitter::emitBinSub(const BinOpInfo &op) {
635 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, op.fpFeatures);
636
637 if (mlir::isa<cir::ComplexType>(op.lhs.getType()) &&
638 mlir::isa<cir::ComplexType>(op.rhs.getType()))
639 return cir::ComplexSubOp::create(builder, op.loc, op.lhs, op.rhs);
640
641 if (mlir::isa<cir::ComplexType>(op.lhs.getType())) {
642 mlir::Value real = builder.createComplexReal(op.loc, op.lhs);
643 mlir::Value imag = builder.createComplexImag(op.loc, op.lhs);
644 mlir::Value newReal = builder.createSub(op.loc, real, op.rhs);
645 return builder.createComplexCreate(op.loc, newReal, imag);
646 }
647
648 assert(mlir::isa<cir::ComplexType>(op.rhs.getType()));
649 mlir::Value real = builder.createComplexReal(op.loc, op.rhs);
650 mlir::Value imag = builder.createComplexImag(op.loc, op.rhs);
651 mlir::Value newReal = builder.createSub(op.loc, op.lhs, real);
652 return builder.createComplexCreate(op.loc, newReal, imag);
653}
654
655static cir::ComplexRangeKind
657 switch (range) {
659 return cir::ComplexRangeKind::Full;
661 return cir::ComplexRangeKind::Improved;
663 return cir::ComplexRangeKind::Promoted;
665 return cir::ComplexRangeKind::Basic;
667 // The default value for ComplexRangeKind is Full if no option is selected
668 return cir::ComplexRangeKind::Full;
669 }
670}
671
672mlir::Value ComplexExprEmitter::emitBinMul(const BinOpInfo &op) {
674 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, op.fpFeatures);
675
676 if (mlir::isa<cir::ComplexType>(op.lhs.getType()) &&
677 mlir::isa<cir::ComplexType>(op.rhs.getType())) {
678 cir::ComplexRangeKind rangeKind =
679 getComplexRangeAttr(op.fpFeatures.getComplexRange());
680 return cir::ComplexMulOp::create(builder, op.loc, op.lhs, op.rhs,
681 rangeKind);
682 }
683
684 if (mlir::isa<cir::ComplexType>(op.lhs.getType())) {
685 mlir::Value real = builder.createComplexReal(op.loc, op.lhs);
686 mlir::Value imag = builder.createComplexImag(op.loc, op.lhs);
687 mlir::Value newReal = builder.createMul(op.loc, real, op.rhs);
688 mlir::Value newImag = builder.createMul(op.loc, imag, op.rhs);
689 return builder.createComplexCreate(op.loc, newReal, newImag);
690 }
691
692 assert(mlir::isa<cir::ComplexType>(op.rhs.getType()));
693 mlir::Value real = builder.createComplexReal(op.loc, op.rhs);
694 mlir::Value imag = builder.createComplexImag(op.loc, op.rhs);
695 mlir::Value newReal = builder.createMul(op.loc, op.lhs, real);
696 mlir::Value newImag = builder.createMul(op.loc, op.lhs, imag);
697 return builder.createComplexCreate(op.loc, newReal, newImag);
698}
699
700mlir::Value ComplexExprEmitter::emitBinDiv(const BinOpInfo &op) {
702 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, op.fpFeatures);
703
704 // Handle division between two complex values. In the case of complex integer
705 // types mixed with scalar integers, the scalar integer type will always be
706 // promoted to a complex integer value with a zero imaginary component when
707 // the AST is formed.
708 if (mlir::isa<cir::ComplexType>(op.lhs.getType()) &&
709 mlir::isa<cir::ComplexType>(op.rhs.getType())) {
710 cir::ComplexRangeKind rangeKind =
711 getComplexRangeAttr(op.fpFeatures.getComplexRange());
712 return cir::ComplexDivOp::create(builder, op.loc, op.lhs, op.rhs,
713 rangeKind);
714 }
715
716 // The C99 standard (G.5.1) defines division of a complex value by a real
717 // value in the following simplified form.
718 if (mlir::isa<cir::ComplexType>(op.lhs.getType())) {
719 assert(mlir::cast<cir::ComplexType>(op.lhs.getType()).getElementType() ==
720 op.rhs.getType());
721 mlir::Value real = builder.createComplexReal(op.loc, op.lhs);
722 mlir::Value imag = builder.createComplexImag(op.loc, op.lhs);
723 mlir::Value newReal = builder.createFDiv(op.loc, real, op.rhs);
724 mlir::Value newImag = builder.createFDiv(op.loc, imag, op.rhs);
725 return builder.createComplexCreate(op.loc, newReal, newImag);
726 }
727
728 assert(mlir::isa<cir::ComplexType>(op.rhs.getType()));
729 cir::ConstantOp nullValue = builder.getNullValue(op.lhs.getType(), op.loc);
730 mlir::Value lhs = builder.createComplexCreate(op.loc, op.lhs, nullValue);
731 cir::ComplexRangeKind rangeKind =
732 getComplexRangeAttr(op.fpFeatures.getComplexRange());
733 return cir::ComplexDivOp::create(builder, op.loc, lhs, op.rhs, rangeKind);
734}
735
736mlir::Value CIRGenFunction::emitUnPromotedValue(mlir::Value result,
737 QualType unPromotionType) {
738 assert(!mlir::cast<cir::ComplexType>(result.getType()).isIntegerComplex() &&
739 "integral complex will never be promoted");
740 return builder.createCast(cir::CastKind::float_complex, result,
741 convertType(unPromotionType));
742}
743
744mlir::Value CIRGenFunction::emitPromotedValue(mlir::Value result,
745 QualType promotionType) {
746 assert(!mlir::cast<cir::ComplexType>(result.getType()).isIntegerComplex() &&
747 "integral complex will never be promoted");
748 return builder.createCast(cir::CastKind::float_complex, result,
749 convertType(promotionType));
750}
751
752mlir::Value ComplexExprEmitter::emitPromoted(const Expr *e,
753 QualType promotionTy) {
754 e = e->IgnoreParens();
755 if (const auto *bo = dyn_cast<BinaryOperator>(e)) {
756 switch (bo->getOpcode()) {
757#define HANDLE_BINOP(OP) \
758 case BO_##OP: \
759 return emitBin##OP(emitBinOps(bo, promotionTy));
760 HANDLE_BINOP(Add)
761 HANDLE_BINOP(Sub)
762 HANDLE_BINOP(Mul)
763 HANDLE_BINOP(Div)
764#undef HANDLE_BINOP
765 default:
766 break;
767 }
768 } else if (const auto *unaryOp = dyn_cast<UnaryOperator>(e)) {
769 switch (unaryOp->getOpcode()) {
770 case UO_Plus:
771 return VisitUnaryPlus(unaryOp, promotionTy);
772 case UO_Minus:
773 return VisitUnaryMinus(unaryOp, promotionTy);
774 default:
775 break;
776 }
777 }
778
779 mlir::Value result = Visit(const_cast<Expr *>(e));
780 if (!promotionTy.isNull())
781 return cgf.emitPromotedValue(result, promotionTy);
782
783 return result;
784}
785
787 QualType promotionType) {
788 return ComplexExprEmitter(*this).emitPromoted(e, promotionType);
789}
790
791mlir::Value
792ComplexExprEmitter::emitPromotedComplexOperand(const Expr *e,
793 QualType promotionTy) {
794 if (e->getType()->isAnyComplexType()) {
795 if (!promotionTy.isNull())
796 return cgf.emitPromotedComplexExpr(e, promotionTy);
797 return Visit(const_cast<Expr *>(e));
798 }
799
800 if (!promotionTy.isNull()) {
801 QualType complexElementTy =
802 promotionTy->castAs<ComplexType>()->getElementType();
803 return cgf.emitPromotedScalarExpr(e, complexElementTy);
804 }
805 return cgf.emitScalarExpr(e);
806}
807
808ComplexExprEmitter::BinOpInfo
809ComplexExprEmitter::emitBinOps(const BinaryOperator *e, QualType promotionTy) {
810 BinOpInfo binOpInfo{cgf.getLoc(e->getExprLoc())};
811 binOpInfo.lhs = emitPromotedComplexOperand(e->getLHS(), promotionTy);
812 binOpInfo.rhs = emitPromotedComplexOperand(e->getRHS(), promotionTy);
813 binOpInfo.ty = promotionTy.isNull() ? e->getType() : promotionTy;
814 binOpInfo.fpFeatures = e->getFPFeaturesInEffect(cgf.getLangOpts());
815 return binOpInfo;
816}
817
818LValue ComplexExprEmitter::emitCompoundAssignLValue(
819 const CompoundAssignOperator *e,
820 mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &), RValue &value) {
821 QualType lhsTy = e->getLHS()->getType();
822 QualType rhsTy = e->getRHS()->getType();
823 SourceLocation exprLoc = e->getExprLoc();
824 mlir::Location loc = cgf.getLoc(exprLoc);
825
826 if (lhsTy->getAs<AtomicType>()) {
827 cgf.cgm.errorNYI("emitCompoundAssignLValue AtmoicType");
828 return {};
829 }
830
831 BinOpInfo opInfo{loc};
832 opInfo.fpFeatures = e->getFPFeaturesInEffect(cgf.getLangOpts());
833
834 CIRGenFunction::CIRGenFPOptionsRAII FPOptsRAII(cgf, opInfo.fpFeatures);
835
836 // Load the RHS and LHS operands.
837 // __block variables need to have the rhs evaluated first, plus this should
838 // improve codegen a little.
839 QualType promotionTypeCR = getPromotionType(e->getComputationResultType());
840 opInfo.ty = promotionTypeCR.isNull() ? e->getComputationResultType()
841 : promotionTypeCR;
842
843 QualType complexElementTy =
844 opInfo.ty->castAs<ComplexType>()->getElementType();
845 QualType promotionTypeRHS = getPromotionType(rhsTy);
846
847 // The RHS should have been converted to the computation type.
848 if (e->getRHS()->getType()->isRealFloatingType()) {
849 if (!promotionTypeRHS.isNull()) {
850 opInfo.rhs = cgf.emitPromotedScalarExpr(e->getRHS(), promotionTypeRHS);
851 } else {
852 assert(cgf.getContext().hasSameUnqualifiedType(complexElementTy, rhsTy));
853 opInfo.rhs = cgf.emitScalarExpr(e->getRHS());
854 }
855 } else {
856 if (!promotionTypeRHS.isNull()) {
857 opInfo.rhs = cgf.emitPromotedComplexExpr(e->getRHS(), promotionTypeRHS);
858 } else {
859 assert(cgf.getContext().hasSameUnqualifiedType(opInfo.ty, rhsTy));
860 opInfo.rhs = Visit(e->getRHS());
861 }
862 }
863
864 LValue lhs = cgf.emitLValue(e->getLHS());
865
866 // Load from the l-value and convert it.
867 QualType promotionTypeLHS = getPromotionType(e->getComputationLHSType());
868 if (lhsTy->isAnyComplexType()) {
869 mlir::Value lhsValue = emitLoadOfLValue(lhs, exprLoc);
870 QualType destTy = promotionTypeLHS.isNull() ? opInfo.ty : promotionTypeLHS;
871 opInfo.lhs = emitComplexToComplexCast(lhsValue, lhsTy, destTy, exprLoc);
872 } else {
873 mlir::Value lhsVal = cgf.emitLoadOfScalar(lhs, exprLoc);
874 // For floating point real operands we can directly pass the scalar form
875 // to the binary operator emission and potentially get more efficient code.
876 if (lhsTy->isRealFloatingType()) {
877 QualType promotedComplexElementTy;
878 if (!promotionTypeLHS.isNull()) {
879 promotedComplexElementTy =
880 cast<ComplexType>(promotionTypeLHS)->getElementType();
881 if (!cgf.getContext().hasSameUnqualifiedType(promotedComplexElementTy,
882 promotionTypeLHS))
883 lhsVal = cgf.emitScalarConversion(lhsVal, lhsTy,
884 promotedComplexElementTy, exprLoc);
885 } else {
886 if (!cgf.getContext().hasSameUnqualifiedType(complexElementTy, lhsTy))
887 lhsVal = cgf.emitScalarConversion(lhsVal, lhsTy, complexElementTy,
888 exprLoc);
889 }
890 opInfo.lhs = lhsVal;
891 } else {
892 opInfo.lhs = emitScalarToComplexCast(lhsVal, lhsTy, opInfo.ty, exprLoc);
893 }
894 }
895
896 // Expand the binary operator.
897 mlir::Value result = (this->*func)(opInfo);
898
899 // Truncate the result and store it into the LHS lvalue.
900 if (lhsTy->isAnyComplexType()) {
901 mlir::Value resultValue =
902 emitComplexToComplexCast(result, opInfo.ty, lhsTy, exprLoc);
903 emitStoreOfComplex(loc, resultValue, lhs, /*isInit*/ false);
904 value = RValue::getComplex(resultValue);
905 } else {
906 mlir::Value resultValue =
907 cgf.emitComplexToScalarConversion(result, opInfo.ty, lhsTy, exprLoc);
908 cgf.emitStoreOfScalar(resultValue, lhs, /*isInit*/ false);
909 value = RValue::get(resultValue);
910 }
911
912 return lhs;
913}
914
915mlir::Value ComplexExprEmitter::emitCompoundAssign(
916 const CompoundAssignOperator *e,
917 mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &)) {
918 RValue val;
919 LValue lv = emitCompoundAssignLValue(e, func, val);
920
921 // The result of an assignment in C is the assigned r-value.
922 if (!cgf.getLangOpts().CPlusPlus)
923 return val.getComplexValue();
924
925 // If the lvalue is non-volatile, return the computed value of the assignment.
926 if (!lv.isVolatileQualified())
927 return val.getComplexValue();
928
929 return emitLoadOfLValue(lv, e->getExprLoc());
930}
931
932LValue ComplexExprEmitter::emitBinAssignLValue(const BinaryOperator *e,
933 mlir::Value &value) {
934 assert(cgf.getContext().hasSameUnqualifiedType(e->getLHS()->getType(),
935 e->getRHS()->getType()) &&
936 "Invalid assignment");
937
938 // Emit the RHS. __block variables need the RHS evaluated first.
939 value = Visit(e->getRHS());
940
941 // Compute the address to store into.
942 LValue lhs = cgf.emitLValue(e->getLHS());
943
944 // Store the result value into the LHS lvalue.
945 emitStoreOfComplex(cgf.getLoc(e->getExprLoc()), value, lhs,
946 /*isInit*/ false);
947 return lhs;
948}
949
950mlir::Value ComplexExprEmitter::VisitBinAssign(const BinaryOperator *e) {
951 mlir::Value value;
952 LValue lv = emitBinAssignLValue(e, value);
953
954 // The result of an assignment in C is the assigned r-value.
955 if (!cgf.getLangOpts().CPlusPlus)
956 return value;
957
958 // If the lvalue is non-volatile, return the computed value of the
959 // assignment.
960 if (!lv.isVolatile())
961 return value;
962
963 return emitLoadOfLValue(lv, e->getExprLoc());
964}
965
966mlir::Value ComplexExprEmitter::VisitBinComma(const BinaryOperator *e) {
967 cgf.emitIgnoredExpr(e->getLHS());
968 return Visit(e->getRHS());
969}
970
971mlir::Value ComplexExprEmitter::VisitAbstractConditionalOperator(
972 const AbstractConditionalOperator *e) {
973 mlir::Location loc = cgf.getLoc(e->getSourceRange());
974
975 // Bind the common expression if necessary.
976 CIRGenFunction::OpaqueValueMapping binding(cgf, e);
977
978 CIRGenFunction::ConditionalEvaluation eval(cgf);
979
980 Expr *cond = e->getCond()->IgnoreParens();
981 mlir::Value condValue = cgf.evaluateExprAsBool(cond);
982
983 return cir::TernaryOp::create(
984 builder, loc, condValue,
985 /*thenBuilder=*/
986 [&](mlir::OpBuilder &b, mlir::Location loc) {
987 eval.beginEvaluation();
988 mlir::Value trueValue = Visit(e->getTrueExpr());
989 cir::YieldOp::create(b, loc, trueValue);
990 eval.endEvaluation();
991 },
992 /*elseBuilder=*/
993 [&](mlir::OpBuilder &b, mlir::Location loc) {
994 eval.beginEvaluation();
995 mlir::Value falseValue = Visit(e->getFalseExpr());
996 cir::YieldOp::create(b, loc, falseValue);
997 eval.endEvaluation();
998 })
999 .getResult();
1000}
1001
1002mlir::Value ComplexExprEmitter::VisitChooseExpr(ChooseExpr *e) {
1003 return Visit(e->getChosenSubExpr());
1004}
1005
1006mlir::Value ComplexExprEmitter::VisitInitListExpr(InitListExpr *e) {
1007 mlir::Location loc = cgf.getLoc(e->getExprLoc());
1008 if (e->getNumInits() == 2) {
1009 mlir::Value real = cgf.emitScalarExpr(e->getInit(0));
1010 mlir::Value imag = cgf.emitScalarExpr(e->getInit(1));
1011 return builder.createComplexCreate(loc, real, imag);
1012 }
1013
1014 if (e->getNumInits() == 1)
1015 return Visit(e->getInit(0));
1016
1017 assert(e->getNumInits() == 0 && "Unexpected number of inits");
1018 mlir::Type complexTy = cgf.convertType(e->getType());
1019 return builder.getNullValue(complexTy, loc);
1020}
1021
1022mlir::Value ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *e) {
1023 return cgf.emitVAArg(e);
1024}
1025
1026//===----------------------------------------------------------------------===//
1027// Entry Point into this File
1028//===----------------------------------------------------------------------===//
1029
1030/// EmitComplexExpr - Emit the computation of the specified expression of
1031/// complex type, ignoring the result.
1033 assert(e && getComplexType(e->getType()) &&
1034 "Invalid complex expression to emit");
1035
1036 return ComplexExprEmitter(*this).Visit(const_cast<Expr *>(e));
1037}
1038
1040 bool isInit) {
1041 assert(e && getComplexType(e->getType()) &&
1042 "Invalid complex expression to emit");
1043 ComplexExprEmitter emitter(*this);
1044 mlir::Value value = emitter.Visit(const_cast<Expr *>(e));
1045 emitter.emitStoreOfComplex(getLoc(e->getExprLoc()), value, dest, isInit);
1046}
1047
1048/// EmitStoreOfComplex - Store a complex number into the specified l-value.
1049void CIRGenFunction::emitStoreOfComplex(mlir::Location loc, mlir::Value v,
1050 LValue dest, bool isInit) {
1051 ComplexExprEmitter(*this).emitStoreOfComplex(loc, v, dest, isInit);
1052}
1053
1055 return ComplexExprEmitter(*this).emitLoadOfLValue(src, loc);
1056}
1057
1059 assert(e->getOpcode() == BO_Assign && "Expected assign op");
1060
1061 mlir::Value value; // ignored
1062 LValue lvalue = ComplexExprEmitter(*this).emitBinAssignLValue(e, value);
1063 if (getLangOpts().OpenMP)
1064 cgm.errorNYI("emitComplexAssignmentLValue OpenMP");
1065
1066 return lvalue;
1067}
1068
1070 mlir::Value (ComplexExprEmitter::*)(const ComplexExprEmitter::BinOpInfo &);
1071
1073 switch (op) {
1074 case BO_MulAssign:
1075 return &ComplexExprEmitter::emitBinMul;
1076 case BO_DivAssign:
1077 return &ComplexExprEmitter::emitBinDiv;
1078 case BO_SubAssign:
1079 return &ComplexExprEmitter::emitBinSub;
1080 case BO_AddAssign:
1081 return &ComplexExprEmitter::emitBinAdd;
1082 default:
1083 llvm_unreachable("unexpected complex compound assignment");
1084 }
1085}
1086
1088 const CompoundAssignOperator *e) {
1090 RValue val;
1091 return ComplexExprEmitter(*this).emitCompoundAssignLValue(e, op, val);
1092}
1093
1095 LValue lv) {
1096 mlir::Value inVal = emitLoadOfComplex(lv, e->getExprLoc());
1097 mlir::Location loc = getLoc(e->getExprLoc());
1098 mlir::Value incVal = e->isIncrementOp() ? builder.createInc(loc, inVal)
1099 : builder.createDec(loc, inVal);
1100
1101 // Store the updated result through the lvalue.
1102 emitStoreOfComplex(loc, incVal, lv, /*isInit=*/false);
1103
1104 if (getLangOpts().OpenMP)
1105 cgm.errorNYI(loc, "emitComplexPrePostIncDec OpenMP");
1106
1107 // If this is a postinc, return the value read from memory, otherwise use the
1108 // updated value.
1109 return e->isPrefix() ? incVal : inVal;
1110}
1111
1113 const CompoundAssignOperator *e, mlir::Value &result) {
1114 // Key Instructions: Don't need to create an atom group here; one will already
1115 // be active through scalar handling code.
1117 RValue value;
1118 LValue ret = ComplexExprEmitter(*this).emitCompoundAssignLValue(e, op, value);
1119 result = value.getValue();
1120 return ret;
1121}
#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)
__device__ __2f16 b
__device__ __2f16 float __ockl_bool s
mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
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 createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand)
mlir::Value createNot(mlir::Location loc, mlir::Value value)
mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
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 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:4534
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition Expr.h:4540
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition Expr.h:4546
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4041
Expr * getLHS() const
Definition Expr.h:4091
SourceLocation getExprLoc() const
Definition Expr.h:4082
Expr * getRHS() const
Definition Expr.h:4093
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Get the FP features status of this operator.
Definition Expr.h:4254
Opcode getOpcode() const
Definition Expr.h:4086
bool isValid() const
Definition Address.h:75
mlir::Value createFDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, Address dst, bool isVolatile=false, mlir::IntegerAttr align={}, cir::SyncScopeKindAttr scope={}, cir::MemOrderAttr order={})
cir::LoadOp createLoad(mlir::Location loc, Address addr, bool isVolatile=false)
LValue getReferenceLValue(CIRGenFunction &cgf, 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)
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)
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.
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
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:1105
A rewritten comparison expression that was originally written using operator syntax.
Definition ExprCXX.h:287
SourceLocation getExprLoc() const LLVM_READONLY
Definition ExprCXX.h:342
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
Definition Expr.cpp:1603
CastKind getCastKind() const
Definition Expr.h:3723
bool changesVolatileQualification() const
Return.
Definition Expr.h:3813
Expr * getSubExpr()
Definition Expr.h:3729
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition Expr.h:4887
Complex values, per C99 6.2.5p11.
Definition TypeBase.h:3325
CompoundAssignOperator - For compound assignments (e.g.
Definition Expr.h:4303
QualType getComputationLHSType() const
Definition Expr.h:4337
QualType getComputationResultType() const
Definition Expr.h:4340
This represents one expression.
Definition Expr.h:112
bool isGLValue() const
Definition Expr.h:287
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3086
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:277
QualType getType() const
Definition Expr.h:144
const Expr * getSubExpr() const
Definition Expr.h:1065
Expr * getResultExpr()
Return the result expression of this controlling expression.
Definition Expr.h:6467
const Expr * getSubExpr() const
Definition Expr.h:1746
unsigned getNumInits() const
Definition Expr.h:5332
const Expr * getInit(unsigned Init) const
Definition Expr.h:5356
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:3444
SourceLocation getExprLoc() const LLVM_READONLY
Definition Expr.h:1211
Expr * getSelectedExpr() const
Definition ExprCXX.h:4640
const Expr * getSubExpr() const
Definition Expr.h:2202
SourceLocation getExprLoc() const LLVM_READONLY
Definition Expr.h:6889
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
bool UseExcessPrecision(const ASTContext &Ctx)
Definition Type.cpp:1626
Encodes a location in the source.
CompoundStmt * getSubStmt()
Definition Expr.h:4615
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
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9078
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9328
bool isReferenceType() const
Definition TypeBase.h:8692
bool isAnyComplexType() const
Definition TypeBase.h:8803
bool isRealFloatingType() const
Floating point categories.
Definition Type.cpp:2358
bool isFloatingType() const
Definition Type.cpp:2342
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9261
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2247
SourceLocation getExprLoc() const
Definition Expr.h:2371
Expr * getSubExpr() const
Definition Expr.h:2288
static bool isIncrementOp(Opcode Op)
Definition Expr.h:2329
static bool isPrefix(Opcode Op)
isPrefix - Return true if this is a prefix operation, like –x.
Definition Expr.h:2322
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
The JSON file list parser is used to communicate input to InstallAPI.
CastKind
CastKind - The kind of operation required for a conversion.
U cast(CodeGen::Address addr)
Definition Address.h:327
static bool fastMathFlags()