1 //===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This contains code to emit Expr nodes with complex types as LLVM code.
11 //===----------------------------------------------------------------------===//
13 #include "CGOpenMPRuntime.h"
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "clang/AST/StmtVisitor.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/MDBuilder.h"
21 #include "llvm/IR/Metadata.h"
23 using namespace clang;
24 using namespace CodeGen;
26 //===----------------------------------------------------------------------===//
27 // Complex Expression Emitter
28 //===----------------------------------------------------------------------===//
30 typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
32 /// Return the complex type that we are meant to emit.
33 static const ComplexType *getComplexType(QualType type) {
34 type = type.getCanonicalType();
35 if (const ComplexType *comp = dyn_cast<ComplexType>(type)) {
38 return cast<ComplexType>(cast<AtomicType>(type)->getValueType());
43 class ComplexExprEmitter
44 : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
50 ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
51 : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
55 //===--------------------------------------------------------------------===//
57 //===--------------------------------------------------------------------===//
59 bool TestAndClearIgnoreReal() {
64 bool TestAndClearIgnoreImag() {
70 /// EmitLoadOfLValue - Given an expression with complex type that represents a
71 /// value l-value, this method emits the address of the l-value, then loads
72 /// and returns the result.
73 ComplexPairTy EmitLoadOfLValue(const Expr *E) {
74 return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc());
77 ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc);
79 /// EmitStoreOfComplex - Store the specified real/imag parts into the
80 /// specified value pointer.
81 void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit);
83 /// Emit a cast from complex value Val to DestType.
84 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
85 QualType DestType, SourceLocation Loc);
86 /// Emit a cast from scalar value Val to DestType.
87 ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType,
88 QualType DestType, SourceLocation Loc);
90 //===--------------------------------------------------------------------===//
92 //===--------------------------------------------------------------------===//
94 ComplexPairTy Visit(Expr *E) {
95 ApplyDebugLocation DL(CGF, E);
96 return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
99 ComplexPairTy VisitStmt(Stmt *S) {
100 S->dump(CGF.getContext().getSourceManager());
101 llvm_unreachable("Stmt can't have complex result type!");
103 ComplexPairTy VisitExpr(Expr *S);
104 ComplexPairTy VisitConstantExpr(ConstantExpr *E) {
105 return Visit(E->getSubExpr());
107 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
108 ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
109 return Visit(GE->getResultExpr());
111 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
113 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
114 return Visit(PE->getReplacement());
116 ComplexPairTy VisitCoawaitExpr(CoawaitExpr *S) {
117 return CGF.EmitCoawaitExpr(*S).getComplexVal();
119 ComplexPairTy VisitCoyieldExpr(CoyieldExpr *S) {
120 return CGF.EmitCoyieldExpr(*S).getComplexVal();
122 ComplexPairTy VisitUnaryCoawait(const UnaryOperator *E) {
123 return Visit(E->getSubExpr());
126 ComplexPairTy emitConstant(const CodeGenFunction::ConstantEmission &Constant,
128 assert(Constant && "not a constant");
129 if (Constant.isReference())
130 return EmitLoadOfLValue(Constant.getReferenceLValue(CGF, E),
133 llvm::Constant *pair = Constant.getValue();
134 return ComplexPairTy(pair->getAggregateElement(0U),
135 pair->getAggregateElement(1U));
139 ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
140 if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E))
141 return emitConstant(Constant, E);
142 return EmitLoadOfLValue(E);
144 ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
145 return EmitLoadOfLValue(E);
147 ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
148 return CGF.EmitObjCMessageExpr(E).getComplexVal();
150 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
151 ComplexPairTy VisitMemberExpr(MemberExpr *ME) {
152 if (CodeGenFunction::ConstantEmission Constant =
153 CGF.tryEmitAsConstant(ME)) {
154 CGF.EmitIgnoredExpr(ME->getBase());
155 return emitConstant(Constant, ME);
157 return EmitLoadOfLValue(ME);
159 ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
161 return EmitLoadOfLValue(CGF.getOrCreateOpaqueLValueMapping(E),
163 return CGF.getOrCreateOpaqueRValueMapping(E).getComplexVal();
166 ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
167 return CGF.EmitPseudoObjectRValue(E).getComplexVal();
170 // FIXME: CompoundLiteralExpr
172 ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy);
173 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
174 // Unlike for scalars, we don't have to worry about function->ptr demotion
176 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
178 ComplexPairTy VisitCastExpr(CastExpr *E) {
179 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
180 CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
181 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
183 ComplexPairTy VisitCallExpr(const CallExpr *E);
184 ComplexPairTy VisitStmtExpr(const StmtExpr *E);
187 ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
188 bool isInc, bool isPre) {
189 LValue LV = CGF.EmitLValue(E->getSubExpr());
190 return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
192 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
193 return VisitPrePostIncDec(E, false, false);
195 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
196 return VisitPrePostIncDec(E, true, false);
198 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
199 return VisitPrePostIncDec(E, false, true);
201 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
202 return VisitPrePostIncDec(E, true, true);
204 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
205 ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
206 TestAndClearIgnoreReal();
207 TestAndClearIgnoreImag();
208 return Visit(E->getSubExpr());
210 ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
211 ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
212 // LNot,Real,Imag never return complex.
213 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
214 return Visit(E->getSubExpr());
216 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
217 CodeGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE);
218 return Visit(DAE->getExpr());
220 ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
221 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF, DIE);
222 return Visit(DIE->getExpr());
224 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
225 CGF.enterFullExpression(E);
226 CodeGenFunction::RunCleanupsScope Scope(CGF);
227 ComplexPairTy Vals = Visit(E->getSubExpr());
228 // Defend against dominance problems caused by jumps out of expression
229 // evaluation through the shared cleanup block.
230 Scope.ForceCleanup({&Vals.first, &Vals.second});
233 ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
234 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
235 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
236 llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
237 return ComplexPairTy(Null, Null);
239 ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
240 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
241 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
242 llvm::Constant *Null =
243 llvm::Constant::getNullValue(CGF.ConvertType(Elem));
244 return ComplexPairTy(Null, Null);
250 QualType Ty; // Computation Type.
253 BinOpInfo EmitBinOps(const BinaryOperator *E);
254 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
255 ComplexPairTy (ComplexExprEmitter::*Func)
258 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
259 ComplexPairTy (ComplexExprEmitter::*Func)
260 (const BinOpInfo &));
262 ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
263 ComplexPairTy EmitBinSub(const BinOpInfo &Op);
264 ComplexPairTy EmitBinMul(const BinOpInfo &Op);
265 ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
267 ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
268 const BinOpInfo &Op);
270 ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
271 return EmitBinAdd(EmitBinOps(E));
273 ComplexPairTy VisitBinSub(const BinaryOperator *E) {
274 return EmitBinSub(EmitBinOps(E));
276 ComplexPairTy VisitBinMul(const BinaryOperator *E) {
277 return EmitBinMul(EmitBinOps(E));
279 ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
280 return EmitBinDiv(EmitBinOps(E));
283 ComplexPairTy VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E) {
284 return Visit(E->getSemanticForm());
287 // Compound assignments.
288 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
289 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
291 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
292 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
294 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
295 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
297 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
298 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
301 // GCC rejects rem/and/or/xor for integer complex.
302 // Logical and/or always return int, never complex.
304 // No comparisons produce a complex result.
306 LValue EmitBinAssignLValue(const BinaryOperator *E,
308 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
309 ComplexPairTy VisitBinComma (const BinaryOperator *E);
313 VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
314 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
316 ComplexPairTy VisitInitListExpr(InitListExpr *E);
318 ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
319 return EmitLoadOfLValue(E);
322 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
324 ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
325 return CGF.EmitAtomicExpr(E).getComplexVal();
328 } // end anonymous namespace.
330 //===----------------------------------------------------------------------===//
332 //===----------------------------------------------------------------------===//
334 Address CodeGenFunction::emitAddrOfRealComponent(Address addr,
335 QualType complexType) {
336 return Builder.CreateStructGEP(addr, 0, addr.getName() + ".realp");
339 Address CodeGenFunction::emitAddrOfImagComponent(Address addr,
340 QualType complexType) {
341 return Builder.CreateStructGEP(addr, 1, addr.getName() + ".imagp");
344 /// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
345 /// load the real and imaginary pieces, returning them as Real/Imag.
346 ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
347 SourceLocation loc) {
348 assert(lvalue.isSimple() && "non-simple complex l-value?");
349 if (lvalue.getType()->isAtomicType())
350 return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
352 Address SrcPtr = lvalue.getAddress(CGF);
353 bool isVolatile = lvalue.isVolatileQualified();
355 llvm::Value *Real = nullptr, *Imag = nullptr;
357 if (!IgnoreReal || isVolatile) {
358 Address RealP = CGF.emitAddrOfRealComponent(SrcPtr, lvalue.getType());
359 Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr.getName() + ".real");
362 if (!IgnoreImag || isVolatile) {
363 Address ImagP = CGF.emitAddrOfImagComponent(SrcPtr, lvalue.getType());
364 Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr.getName() + ".imag");
367 return ComplexPairTy(Real, Imag);
370 /// EmitStoreOfComplex - Store the specified real/imag parts into the
371 /// specified value pointer.
372 void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
374 if (lvalue.getType()->isAtomicType() ||
375 (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue)))
376 return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
378 Address Ptr = lvalue.getAddress(CGF);
379 Address RealPtr = CGF.emitAddrOfRealComponent(Ptr, lvalue.getType());
380 Address ImagPtr = CGF.emitAddrOfImagComponent(Ptr, lvalue.getType());
382 Builder.CreateStore(Val.first, RealPtr, lvalue.isVolatileQualified());
383 Builder.CreateStore(Val.second, ImagPtr, lvalue.isVolatileQualified());
388 //===----------------------------------------------------------------------===//
390 //===----------------------------------------------------------------------===//
392 ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
393 CGF.ErrorUnsupported(E, "complex expression");
395 CGF.ConvertType(getComplexType(E->getType())->getElementType());
396 llvm::Value *U = llvm::UndefValue::get(EltTy);
397 return ComplexPairTy(U, U);
400 ComplexPairTy ComplexExprEmitter::
401 VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
402 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
403 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
407 ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
408 if (E->getCallReturnType(CGF.getContext())->isReferenceType())
409 return EmitLoadOfLValue(E);
411 return CGF.EmitCallExpr(E).getComplexVal();
414 ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
415 CodeGenFunction::StmtExprEvaluation eval(CGF);
416 Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
417 assert(RetAlloca.isValid() && "Expected complex return value");
418 return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
422 /// Emit a cast from complex value Val to DestType.
423 ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
426 SourceLocation Loc) {
427 // Get the src/dest element type.
428 SrcType = SrcType->castAs<ComplexType>()->getElementType();
429 DestType = DestType->castAs<ComplexType>()->getElementType();
431 // C99 6.3.1.6: When a value of complex type is converted to another
432 // complex type, both the real and imaginary parts follow the conversion
433 // rules for the corresponding real types.
434 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType, Loc);
435 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType, Loc);
439 ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
442 SourceLocation Loc) {
443 // Convert the input element to the element type of the complex.
444 DestType = DestType->castAs<ComplexType>()->getElementType();
445 Val = CGF.EmitScalarConversion(Val, SrcType, DestType, Loc);
447 // Return (realval, 0).
448 return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
451 ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
454 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
456 // Atomic to non-atomic casts may be more than a no-op for some platforms and
458 case CK_AtomicToNonAtomic:
459 case CK_NonAtomicToAtomic:
461 case CK_LValueToRValue:
462 case CK_UserDefinedConversion:
465 case CK_LValueBitCast: {
466 LValue origLV = CGF.EmitLValue(Op);
467 Address V = origLV.getAddress(CGF);
468 V = Builder.CreateElementBitCast(V, CGF.ConvertType(DestTy));
469 return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), Op->getExprLoc());
472 case CK_LValueToRValueBitCast: {
473 LValue SourceLVal = CGF.EmitLValue(Op);
474 Address Addr = Builder.CreateElementBitCast(SourceLVal.getAddress(CGF),
475 CGF.ConvertTypeForMem(DestTy));
476 LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy);
477 DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo());
478 return EmitLoadOfLValue(DestLV, Op->getExprLoc());
482 case CK_BaseToDerived:
483 case CK_DerivedToBase:
484 case CK_UncheckedDerivedToBase:
487 case CK_ArrayToPointerDecay:
488 case CK_FunctionToPointerDecay:
489 case CK_NullToPointer:
490 case CK_NullToMemberPointer:
491 case CK_BaseToDerivedMemberPointer:
492 case CK_DerivedToBaseMemberPointer:
493 case CK_MemberPointerToBoolean:
494 case CK_ReinterpretMemberPointer:
495 case CK_ConstructorConversion:
496 case CK_IntegralToPointer:
497 case CK_PointerToIntegral:
498 case CK_PointerToBoolean:
501 case CK_IntegralCast:
502 case CK_BooleanToSignedIntegral:
503 case CK_IntegralToBoolean:
504 case CK_IntegralToFloating:
505 case CK_FloatingToIntegral:
506 case CK_FloatingToBoolean:
507 case CK_FloatingCast:
508 case CK_CPointerToObjCPointerCast:
509 case CK_BlockPointerToObjCPointerCast:
510 case CK_AnyPointerToBlockPointerCast:
511 case CK_ObjCObjectLValueCast:
512 case CK_FloatingComplexToReal:
513 case CK_FloatingComplexToBoolean:
514 case CK_IntegralComplexToReal:
515 case CK_IntegralComplexToBoolean:
516 case CK_ARCProduceObject:
517 case CK_ARCConsumeObject:
518 case CK_ARCReclaimReturnedObject:
519 case CK_ARCExtendBlockObject:
520 case CK_CopyAndAutoreleaseBlockObject:
521 case CK_BuiltinFnToFnPtr:
522 case CK_ZeroToOCLOpaqueType:
523 case CK_AddressSpaceConversion:
524 case CK_IntToOCLSampler:
525 case CK_FixedPointCast:
526 case CK_FixedPointToBoolean:
527 case CK_FixedPointToIntegral:
528 case CK_IntegralToFixedPoint:
529 llvm_unreachable("invalid cast kind for complex value");
531 case CK_FloatingRealToComplex:
532 case CK_IntegralRealToComplex:
533 return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op), Op->getType(),
534 DestTy, Op->getExprLoc());
536 case CK_FloatingComplexCast:
537 case CK_FloatingComplexToIntegralComplex:
538 case CK_IntegralComplexCast:
539 case CK_IntegralComplexToFloatingComplex:
540 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy,
544 llvm_unreachable("unknown cast resulting in complex value");
547 ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
548 TestAndClearIgnoreReal();
549 TestAndClearIgnoreImag();
550 ComplexPairTy Op = Visit(E->getSubExpr());
552 llvm::Value *ResR, *ResI;
553 if (Op.first->getType()->isFloatingPointTy()) {
554 ResR = Builder.CreateFNeg(Op.first, "neg.r");
555 ResI = Builder.CreateFNeg(Op.second, "neg.i");
557 ResR = Builder.CreateNeg(Op.first, "neg.r");
558 ResI = Builder.CreateNeg(Op.second, "neg.i");
560 return ComplexPairTy(ResR, ResI);
563 ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
564 TestAndClearIgnoreReal();
565 TestAndClearIgnoreImag();
566 // ~(a+ib) = a + i*-b
567 ComplexPairTy Op = Visit(E->getSubExpr());
569 if (Op.second->getType()->isFloatingPointTy())
570 ResI = Builder.CreateFNeg(Op.second, "conj.i");
572 ResI = Builder.CreateNeg(Op.second, "conj.i");
574 return ComplexPairTy(Op.first, ResI);
577 ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
578 llvm::Value *ResR, *ResI;
580 if (Op.LHS.first->getType()->isFloatingPointTy()) {
581 ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
582 if (Op.LHS.second && Op.RHS.second)
583 ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
585 ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
586 assert(ResI && "Only one operand may be real!");
588 ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
589 assert(Op.LHS.second && Op.RHS.second &&
590 "Both operands of integer complex operators must be complex!");
591 ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
593 return ComplexPairTy(ResR, ResI);
596 ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
597 llvm::Value *ResR, *ResI;
598 if (Op.LHS.first->getType()->isFloatingPointTy()) {
599 ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
600 if (Op.LHS.second && Op.RHS.second)
601 ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
603 ResI = Op.LHS.second ? Op.LHS.second
604 : Builder.CreateFNeg(Op.RHS.second, "sub.i");
605 assert(ResI && "Only one operand may be real!");
607 ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
608 assert(Op.LHS.second && Op.RHS.second &&
609 "Both operands of integer complex operators must be complex!");
610 ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
612 return ComplexPairTy(ResR, ResI);
615 /// Emit a libcall for a binary operation on complex types.
616 ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
617 const BinOpInfo &Op) {
619 Args.add(RValue::get(Op.LHS.first),
620 Op.Ty->castAs<ComplexType>()->getElementType());
621 Args.add(RValue::get(Op.LHS.second),
622 Op.Ty->castAs<ComplexType>()->getElementType());
623 Args.add(RValue::get(Op.RHS.first),
624 Op.Ty->castAs<ComplexType>()->getElementType());
625 Args.add(RValue::get(Op.RHS.second),
626 Op.Ty->castAs<ComplexType>()->getElementType());
628 // We *must* use the full CG function call building logic here because the
629 // complex type has special ABI handling. We also should not forget about
630 // special calling convention which may be used for compiler builtins.
632 // We create a function qualified type to state that this call does not have
634 FunctionProtoType::ExtProtoInfo EPI;
635 EPI = EPI.withExceptionSpec(
636 FunctionProtoType::ExceptionSpecInfo(EST_BasicNoexcept));
637 SmallVector<QualType, 4> ArgsQTys(
638 4, Op.Ty->castAs<ComplexType>()->getElementType());
639 QualType FQTy = CGF.getContext().getFunctionType(Op.Ty, ArgsQTys, EPI);
640 const CGFunctionInfo &FuncInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall(
641 Args, cast<FunctionType>(FQTy.getTypePtr()), false);
643 llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
644 llvm::FunctionCallee Func = CGF.CGM.CreateRuntimeFunction(
645 FTy, LibCallName, llvm::AttributeList(), true);
646 CGCallee Callee = CGCallee::forDirect(Func, FQTy->getAs<FunctionProtoType>());
648 llvm::CallBase *Call;
649 RValue Res = CGF.EmitCall(FuncInfo, Callee, ReturnValueSlot(), Args, &Call);
650 Call->setCallingConv(CGF.CGM.getRuntimeCC());
651 return Res.getComplexVal();
654 /// Lookup the libcall name for a given floating point type complex
656 static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
657 switch (Ty->getTypeID()) {
659 llvm_unreachable("Unsupported floating point type!");
660 case llvm::Type::HalfTyID:
662 case llvm::Type::FloatTyID:
664 case llvm::Type::DoubleTyID:
666 case llvm::Type::PPC_FP128TyID:
668 case llvm::Type::X86_FP80TyID:
670 case llvm::Type::FP128TyID:
675 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
677 ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
680 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
682 if (Op.LHS.first->getType()->isFloatingPointTy()) {
683 // The general formulation is:
684 // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
686 // But we can fold away components which would be zero due to a real
687 // operand according to C11 Annex G.5.1p2.
688 // FIXME: C11 also provides for imaginary types which would allow folding
689 // still more of this within the type system.
691 if (Op.LHS.second && Op.RHS.second) {
692 // If both operands are complex, emit the core math directly, and then
693 // test for NaNs. If we find NaNs in the result, we delegate to a libcall
694 // to carefully re-compute the correct infinity representation if
695 // possible. The expectation is that the presence of NaNs here is
696 // *extremely* rare, and so the cost of the libcall is almost irrelevant.
697 // This is good, because the libcall re-computes the core multiplication
698 // exactly the same as we do here and re-tests for NaNs in order to be
699 // a generic complex*complex libcall.
701 // First compute the four products.
702 Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
703 Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
704 Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
705 Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
707 // The real part is the difference of the first two, the imaginary part is
708 // the sum of the second.
709 ResR = Builder.CreateFSub(AC, BD, "mul_r");
710 ResI = Builder.CreateFAdd(AD, BC, "mul_i");
712 // Emit the test for the real part becoming NaN and create a branch to
713 // handle it. We test for NaN by comparing the number to itself.
714 Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
715 llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
716 llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
717 llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
718 llvm::BasicBlock *OrigBB = Branch->getParent();
720 // Give hint that we very much don't expect to see NaNs.
721 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
722 llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1);
723 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
725 // Now test the imaginary part and create its branch.
726 CGF.EmitBlock(INaNBB);
727 Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
728 llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
729 Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
730 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
732 // Now emit the libcall on this slowest of the slow paths.
733 CGF.EmitBlock(LibCallBB);
734 Value *LibCallR, *LibCallI;
735 std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
736 getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
737 Builder.CreateBr(ContBB);
739 // Finally continue execution by phi-ing together the different
740 // computation paths.
741 CGF.EmitBlock(ContBB);
742 llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
743 RealPHI->addIncoming(ResR, OrigBB);
744 RealPHI->addIncoming(ResR, INaNBB);
745 RealPHI->addIncoming(LibCallR, LibCallBB);
746 llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
747 ImagPHI->addIncoming(ResI, OrigBB);
748 ImagPHI->addIncoming(ResI, INaNBB);
749 ImagPHI->addIncoming(LibCallI, LibCallBB);
750 return ComplexPairTy(RealPHI, ImagPHI);
752 assert((Op.LHS.second || Op.RHS.second) &&
753 "At least one operand must be complex!");
755 // If either of the operands is a real rather than a complex, the
756 // imaginary component is ignored when computing the real component of the
758 ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
761 ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
762 : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
764 assert(Op.LHS.second && Op.RHS.second &&
765 "Both operands of integer complex operators must be complex!");
766 Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
767 Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
768 ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
770 Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
771 Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
772 ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
774 return ComplexPairTy(ResR, ResI);
777 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
779 ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
780 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
781 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
783 llvm::Value *DSTr, *DSTi;
784 if (LHSr->getType()->isFloatingPointTy()) {
785 // If we have a complex operand on the RHS and FastMath is not allowed, we
786 // delegate to a libcall to handle all of the complexities and minimize
787 // underflow/overflow cases. When FastMath is allowed we construct the
788 // divide inline using the same algorithm as for integer operands.
790 // FIXME: We would be able to avoid the libcall in many places if we
791 // supported imaginary types in addition to complex types.
792 if (RHSi && !CGF.getLangOpts().FastMath) {
793 BinOpInfo LibCallOp = Op;
794 // If LHS was a real, supply a null imaginary part.
796 LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
798 switch (LHSr->getType()->getTypeID()) {
800 llvm_unreachable("Unsupported floating point type!");
801 case llvm::Type::HalfTyID:
802 return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
803 case llvm::Type::FloatTyID:
804 return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
805 case llvm::Type::DoubleTyID:
806 return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
807 case llvm::Type::PPC_FP128TyID:
808 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
809 case llvm::Type::X86_FP80TyID:
810 return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
811 case llvm::Type::FP128TyID:
812 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
816 LHSi = llvm::Constant::getNullValue(RHSi->getType());
818 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
819 llvm::Value *AC = Builder.CreateFMul(LHSr, RHSr); // a*c
820 llvm::Value *BD = Builder.CreateFMul(LHSi, RHSi); // b*d
821 llvm::Value *ACpBD = Builder.CreateFAdd(AC, BD); // ac+bd
823 llvm::Value *CC = Builder.CreateFMul(RHSr, RHSr); // c*c
824 llvm::Value *DD = Builder.CreateFMul(RHSi, RHSi); // d*d
825 llvm::Value *CCpDD = Builder.CreateFAdd(CC, DD); // cc+dd
827 llvm::Value *BC = Builder.CreateFMul(LHSi, RHSr); // b*c
828 llvm::Value *AD = Builder.CreateFMul(LHSr, RHSi); // a*d
829 llvm::Value *BCmAD = Builder.CreateFSub(BC, AD); // bc-ad
831 DSTr = Builder.CreateFDiv(ACpBD, CCpDD);
832 DSTi = Builder.CreateFDiv(BCmAD, CCpDD);
834 assert(LHSi && "Can have at most one non-complex operand!");
836 DSTr = Builder.CreateFDiv(LHSr, RHSr);
837 DSTi = Builder.CreateFDiv(LHSi, RHSr);
840 assert(Op.LHS.second && Op.RHS.second &&
841 "Both operands of integer complex operators must be complex!");
842 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
843 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
844 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
845 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
847 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
848 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
849 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
851 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
852 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
853 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
855 if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
856 DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
857 DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
859 DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
860 DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
864 return ComplexPairTy(DSTr, DSTi);
867 ComplexExprEmitter::BinOpInfo
868 ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
869 TestAndClearIgnoreReal();
870 TestAndClearIgnoreImag();
872 if (E->getLHS()->getType()->isRealFloatingType())
873 Ops.LHS = ComplexPairTy(CGF.EmitScalarExpr(E->getLHS()), nullptr);
875 Ops.LHS = Visit(E->getLHS());
876 if (E->getRHS()->getType()->isRealFloatingType())
877 Ops.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
879 Ops.RHS = Visit(E->getRHS());
881 Ops.Ty = E->getType();
886 LValue ComplexExprEmitter::
887 EmitCompoundAssignLValue(const CompoundAssignOperator *E,
888 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
890 TestAndClearIgnoreReal();
891 TestAndClearIgnoreImag();
892 QualType LHSTy = E->getLHS()->getType();
893 if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
894 LHSTy = AT->getValueType();
898 // Load the RHS and LHS operands.
899 // __block variables need to have the rhs evaluated first, plus this should
900 // improve codegen a little.
901 OpInfo.Ty = E->getComputationResultType();
902 QualType ComplexElementTy = cast<ComplexType>(OpInfo.Ty)->getElementType();
904 // The RHS should have been converted to the computation type.
905 if (E->getRHS()->getType()->isRealFloatingType()) {
908 .hasSameUnqualifiedType(ComplexElementTy, E->getRHS()->getType()));
909 OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
911 assert(CGF.getContext()
912 .hasSameUnqualifiedType(OpInfo.Ty, E->getRHS()->getType()));
913 OpInfo.RHS = Visit(E->getRHS());
916 LValue LHS = CGF.EmitLValue(E->getLHS());
918 // Load from the l-value and convert it.
919 SourceLocation Loc = E->getExprLoc();
920 if (LHSTy->isAnyComplexType()) {
921 ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
922 OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
924 llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, Loc);
925 // For floating point real operands we can directly pass the scalar form
926 // to the binary operator emission and potentially get more efficient code.
927 if (LHSTy->isRealFloatingType()) {
928 if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
929 LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy, Loc);
930 OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
932 OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
936 // Expand the binary operator.
937 ComplexPairTy Result = (this->*Func)(OpInfo);
939 // Truncate the result and store it into the LHS lvalue.
940 if (LHSTy->isAnyComplexType()) {
941 ComplexPairTy ResVal =
942 EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy, Loc);
943 EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
944 Val = RValue::getComplex(ResVal);
946 llvm::Value *ResVal =
947 CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy, Loc);
948 CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
949 Val = RValue::get(ResVal);
955 // Compound assignments.
956 ComplexPairTy ComplexExprEmitter::
957 EmitCompoundAssign(const CompoundAssignOperator *E,
958 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
960 LValue LV = EmitCompoundAssignLValue(E, Func, Val);
962 // The result of an assignment in C is the assigned r-value.
963 if (!CGF.getLangOpts().CPlusPlus)
964 return Val.getComplexVal();
966 // If the lvalue is non-volatile, return the computed value of the assignment.
967 if (!LV.isVolatileQualified())
968 return Val.getComplexVal();
970 return EmitLoadOfLValue(LV, E->getExprLoc());
973 LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
974 ComplexPairTy &Val) {
975 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
976 E->getRHS()->getType()) &&
977 "Invalid assignment");
978 TestAndClearIgnoreReal();
979 TestAndClearIgnoreImag();
981 // Emit the RHS. __block variables need the RHS evaluated first.
982 Val = Visit(E->getRHS());
984 // Compute the address to store into.
985 LValue LHS = CGF.EmitLValue(E->getLHS());
987 // Store the result value into the LHS lvalue.
988 EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
993 ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
995 LValue LV = EmitBinAssignLValue(E, Val);
997 // The result of an assignment in C is the assigned r-value.
998 if (!CGF.getLangOpts().CPlusPlus)
1001 // If the lvalue is non-volatile, return the computed value of the assignment.
1002 if (!LV.isVolatileQualified())
1005 return EmitLoadOfLValue(LV, E->getExprLoc());
1008 ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
1009 CGF.EmitIgnoredExpr(E->getLHS());
1010 return Visit(E->getRHS());
1013 ComplexPairTy ComplexExprEmitter::
1014 VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
1015 TestAndClearIgnoreReal();
1016 TestAndClearIgnoreImag();
1017 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1018 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
1019 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
1021 // Bind the common expression if necessary.
1022 CodeGenFunction::OpaqueValueMapping binding(CGF, E);
1025 CodeGenFunction::ConditionalEvaluation eval(CGF);
1026 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
1027 CGF.getProfileCount(E));
1030 CGF.EmitBlock(LHSBlock);
1031 CGF.incrementProfileCounter(E);
1032 ComplexPairTy LHS = Visit(E->getTrueExpr());
1033 LHSBlock = Builder.GetInsertBlock();
1034 CGF.EmitBranch(ContBlock);
1038 CGF.EmitBlock(RHSBlock);
1039 ComplexPairTy RHS = Visit(E->getFalseExpr());
1040 RHSBlock = Builder.GetInsertBlock();
1041 CGF.EmitBlock(ContBlock);
1044 // Create a PHI node for the real part.
1045 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
1046 RealPN->addIncoming(LHS.first, LHSBlock);
1047 RealPN->addIncoming(RHS.first, RHSBlock);
1049 // Create a PHI node for the imaginary part.
1050 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
1051 ImagPN->addIncoming(LHS.second, LHSBlock);
1052 ImagPN->addIncoming(RHS.second, RHSBlock);
1054 return ComplexPairTy(RealPN, ImagPN);
1057 ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1058 return Visit(E->getChosenSubExpr());
1061 ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
1062 bool Ignore = TestAndClearIgnoreReal();
1064 assert (Ignore == false && "init list ignored");
1065 Ignore = TestAndClearIgnoreImag();
1067 assert (Ignore == false && "init list ignored");
1069 if (E->getNumInits() == 2) {
1070 llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
1071 llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
1072 return ComplexPairTy(Real, Imag);
1073 } else if (E->getNumInits() == 1) {
1074 return Visit(E->getInit(0));
1077 // Empty init list initializes to null
1078 assert(E->getNumInits() == 0 && "Unexpected number of inits");
1079 QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
1080 llvm::Type* LTy = CGF.ConvertType(Ty);
1081 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
1082 return ComplexPairTy(zeroConstant, zeroConstant);
1085 ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
1086 Address ArgValue = Address::invalid();
1087 Address ArgPtr = CGF.EmitVAArg(E, ArgValue);
1089 if (!ArgPtr.isValid()) {
1090 CGF.ErrorUnsupported(E, "complex va_arg expression");
1092 CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
1093 llvm::Value *U = llvm::UndefValue::get(EltTy);
1094 return ComplexPairTy(U, U);
1097 return EmitLoadOfLValue(CGF.MakeAddrLValue(ArgPtr, E->getType()),
1101 //===----------------------------------------------------------------------===//
1102 // Entry Point into this File
1103 //===----------------------------------------------------------------------===//
1105 /// EmitComplexExpr - Emit the computation of the specified expression of
1106 /// complex type, ignoring the result.
1107 ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
1109 assert(E && getComplexType(E->getType()) &&
1110 "Invalid complex expression to emit");
1112 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
1113 .Visit(const_cast<Expr *>(E));
1116 void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest,
1118 assert(E && getComplexType(E->getType()) &&
1119 "Invalid complex expression to emit");
1120 ComplexExprEmitter Emitter(*this);
1121 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
1122 Emitter.EmitStoreOfComplex(Val, dest, isInit);
1125 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
1126 void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest,
1128 ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
1131 /// EmitLoadOfComplex - Load a complex number from the specified address.
1132 ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src,
1133 SourceLocation loc) {
1134 return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
1137 LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
1138 assert(E->getOpcode() == BO_Assign);
1139 ComplexPairTy Val; // ignored
1140 LValue LVal = ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1141 if (getLangOpts().OpenMP)
1142 CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(*this,
1147 typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1148 const ComplexExprEmitter::BinOpInfo &);
1150 static CompoundFunc getComplexOp(BinaryOperatorKind Op) {
1152 case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1153 case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1154 case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1155 case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
1157 llvm_unreachable("unexpected complex compound assignment");
1161 LValue CodeGenFunction::
1162 EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
1163 CompoundFunc Op = getComplexOp(E->getOpcode());
1165 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1168 LValue CodeGenFunction::
1169 EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
1170 llvm::Value *&Result) {
1171 CompoundFunc Op = getComplexOp(E->getOpcode());
1173 LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1174 Result = Val.getScalarVal();