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 "ConstantEmitter.h"
17 #include "clang/AST/StmtVisitor.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/MDBuilder.h"
22 #include "llvm/IR/Metadata.h"
24 using namespace clang;
25 using namespace CodeGen;
27 //===----------------------------------------------------------------------===//
28 // Complex Expression Emitter
29 //===----------------------------------------------------------------------===//
31 typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
33 /// Return the complex type that we are meant to emit.
34 static const ComplexType *getComplexType(QualType type) {
35 type = type.getCanonicalType();
36 if (const ComplexType *comp = dyn_cast<ComplexType>(type)) {
39 return cast<ComplexType>(cast<AtomicType>(type)->getValueType());
44 class ComplexExprEmitter
45 : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
51 ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
52 : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
56 //===--------------------------------------------------------------------===//
58 //===--------------------------------------------------------------------===//
60 bool TestAndClearIgnoreReal() {
65 bool TestAndClearIgnoreImag() {
71 /// EmitLoadOfLValue - Given an expression with complex type that represents a
72 /// value l-value, this method emits the address of the l-value, then loads
73 /// and returns the result.
74 ComplexPairTy EmitLoadOfLValue(const Expr *E) {
75 return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc());
78 ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc);
80 /// EmitStoreOfComplex - Store the specified real/imag parts into the
81 /// specified value pointer.
82 void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit);
84 /// Emit a cast from complex value Val to DestType.
85 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
86 QualType DestType, SourceLocation Loc);
87 /// Emit a cast from scalar value Val to DestType.
88 ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType,
89 QualType DestType, SourceLocation Loc);
91 //===--------------------------------------------------------------------===//
93 //===--------------------------------------------------------------------===//
95 ComplexPairTy Visit(Expr *E) {
96 ApplyDebugLocation DL(CGF, E);
97 return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
100 ComplexPairTy VisitStmt(Stmt *S) {
101 S->dump(llvm::errs(), CGF.getContext());
102 llvm_unreachable("Stmt can't have complex result type!");
104 ComplexPairTy VisitExpr(Expr *S);
105 ComplexPairTy VisitConstantExpr(ConstantExpr *E) {
106 if (llvm::Constant *Result = ConstantEmitter(CGF).tryEmitConstantExpr(E))
107 return ComplexPairTy(Result->getAggregateElement(0U),
108 Result->getAggregateElement(1U));
109 return Visit(E->getSubExpr());
111 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
112 ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
113 return Visit(GE->getResultExpr());
115 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
117 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
118 return Visit(PE->getReplacement());
120 ComplexPairTy VisitCoawaitExpr(CoawaitExpr *S) {
121 return CGF.EmitCoawaitExpr(*S).getComplexVal();
123 ComplexPairTy VisitCoyieldExpr(CoyieldExpr *S) {
124 return CGF.EmitCoyieldExpr(*S).getComplexVal();
126 ComplexPairTy VisitUnaryCoawait(const UnaryOperator *E) {
127 return Visit(E->getSubExpr());
130 ComplexPairTy emitConstant(const CodeGenFunction::ConstantEmission &Constant,
132 assert(Constant && "not a constant");
133 if (Constant.isReference())
134 return EmitLoadOfLValue(Constant.getReferenceLValue(CGF, E),
137 llvm::Constant *pair = Constant.getValue();
138 return ComplexPairTy(pair->getAggregateElement(0U),
139 pair->getAggregateElement(1U));
143 ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
144 if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E))
145 return emitConstant(Constant, E);
146 return EmitLoadOfLValue(E);
148 ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
149 return EmitLoadOfLValue(E);
151 ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
152 return CGF.EmitObjCMessageExpr(E).getComplexVal();
154 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
155 ComplexPairTy VisitMemberExpr(MemberExpr *ME) {
156 if (CodeGenFunction::ConstantEmission Constant =
157 CGF.tryEmitAsConstant(ME)) {
158 CGF.EmitIgnoredExpr(ME->getBase());
159 return emitConstant(Constant, ME);
161 return EmitLoadOfLValue(ME);
163 ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
165 return EmitLoadOfLValue(CGF.getOrCreateOpaqueLValueMapping(E),
167 return CGF.getOrCreateOpaqueRValueMapping(E).getComplexVal();
170 ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
171 return CGF.EmitPseudoObjectRValue(E).getComplexVal();
174 // FIXME: CompoundLiteralExpr
176 ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy);
177 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
178 // Unlike for scalars, we don't have to worry about function->ptr demotion
180 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
182 ComplexPairTy VisitCastExpr(CastExpr *E) {
183 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
184 CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
185 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
187 ComplexPairTy VisitCallExpr(const CallExpr *E);
188 ComplexPairTy VisitStmtExpr(const StmtExpr *E);
191 ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
192 bool isInc, bool isPre) {
193 LValue LV = CGF.EmitLValue(E->getSubExpr());
194 return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
196 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
197 return VisitPrePostIncDec(E, false, false);
199 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
200 return VisitPrePostIncDec(E, true, false);
202 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
203 return VisitPrePostIncDec(E, false, true);
205 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
206 return VisitPrePostIncDec(E, true, true);
208 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
209 ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
210 TestAndClearIgnoreReal();
211 TestAndClearIgnoreImag();
212 return Visit(E->getSubExpr());
214 ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
215 ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
216 // LNot,Real,Imag never return complex.
217 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
218 return Visit(E->getSubExpr());
220 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
221 CodeGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE);
222 return Visit(DAE->getExpr());
224 ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
225 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF, DIE);
226 return Visit(DIE->getExpr());
228 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
229 CodeGenFunction::RunCleanupsScope Scope(CGF);
230 ComplexPairTy Vals = Visit(E->getSubExpr());
231 // Defend against dominance problems caused by jumps out of expression
232 // evaluation through the shared cleanup block.
233 Scope.ForceCleanup({&Vals.first, &Vals.second});
236 ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
237 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
238 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
239 llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
240 return ComplexPairTy(Null, Null);
242 ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
243 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
244 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
245 llvm::Constant *Null =
246 llvm::Constant::getNullValue(CGF.ConvertType(Elem));
247 return ComplexPairTy(Null, Null);
253 QualType Ty; // Computation Type.
256 BinOpInfo EmitBinOps(const BinaryOperator *E);
257 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
258 ComplexPairTy (ComplexExprEmitter::*Func)
261 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
262 ComplexPairTy (ComplexExprEmitter::*Func)
263 (const BinOpInfo &));
265 ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
266 ComplexPairTy EmitBinSub(const BinOpInfo &Op);
267 ComplexPairTy EmitBinMul(const BinOpInfo &Op);
268 ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
270 ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
271 const BinOpInfo &Op);
273 ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
274 return EmitBinAdd(EmitBinOps(E));
276 ComplexPairTy VisitBinSub(const BinaryOperator *E) {
277 return EmitBinSub(EmitBinOps(E));
279 ComplexPairTy VisitBinMul(const BinaryOperator *E) {
280 return EmitBinMul(EmitBinOps(E));
282 ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
283 return EmitBinDiv(EmitBinOps(E));
286 ComplexPairTy VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E) {
287 return Visit(E->getSemanticForm());
290 // Compound assignments.
291 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
292 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
294 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
295 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
297 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
298 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
300 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
301 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
304 // GCC rejects rem/and/or/xor for integer complex.
305 // Logical and/or always return int, never complex.
307 // No comparisons produce a complex result.
309 LValue EmitBinAssignLValue(const BinaryOperator *E,
311 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
312 ComplexPairTy VisitBinComma (const BinaryOperator *E);
316 VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
317 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
319 ComplexPairTy VisitInitListExpr(InitListExpr *E);
321 ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
322 return EmitLoadOfLValue(E);
325 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
327 ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
328 return CGF.EmitAtomicExpr(E).getComplexVal();
331 } // end anonymous namespace.
333 //===----------------------------------------------------------------------===//
335 //===----------------------------------------------------------------------===//
337 Address CodeGenFunction::emitAddrOfRealComponent(Address addr,
338 QualType complexType) {
339 return Builder.CreateStructGEP(addr, 0, addr.getName() + ".realp");
342 Address CodeGenFunction::emitAddrOfImagComponent(Address addr,
343 QualType complexType) {
344 return Builder.CreateStructGEP(addr, 1, addr.getName() + ".imagp");
347 /// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
348 /// load the real and imaginary pieces, returning them as Real/Imag.
349 ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
350 SourceLocation loc) {
351 assert(lvalue.isSimple() && "non-simple complex l-value?");
352 if (lvalue.getType()->isAtomicType())
353 return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
355 Address SrcPtr = lvalue.getAddress(CGF);
356 bool isVolatile = lvalue.isVolatileQualified();
358 llvm::Value *Real = nullptr, *Imag = nullptr;
360 if (!IgnoreReal || isVolatile) {
361 Address RealP = CGF.emitAddrOfRealComponent(SrcPtr, lvalue.getType());
362 Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr.getName() + ".real");
365 if (!IgnoreImag || isVolatile) {
366 Address ImagP = CGF.emitAddrOfImagComponent(SrcPtr, lvalue.getType());
367 Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr.getName() + ".imag");
370 return ComplexPairTy(Real, Imag);
373 /// EmitStoreOfComplex - Store the specified real/imag parts into the
374 /// specified value pointer.
375 void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
377 if (lvalue.getType()->isAtomicType() ||
378 (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue)))
379 return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
381 Address Ptr = lvalue.getAddress(CGF);
382 Address RealPtr = CGF.emitAddrOfRealComponent(Ptr, lvalue.getType());
383 Address ImagPtr = CGF.emitAddrOfImagComponent(Ptr, lvalue.getType());
385 Builder.CreateStore(Val.first, RealPtr, lvalue.isVolatileQualified());
386 Builder.CreateStore(Val.second, ImagPtr, lvalue.isVolatileQualified());
391 //===----------------------------------------------------------------------===//
393 //===----------------------------------------------------------------------===//
395 ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
396 CGF.ErrorUnsupported(E, "complex expression");
398 CGF.ConvertType(getComplexType(E->getType())->getElementType());
399 llvm::Value *U = llvm::UndefValue::get(EltTy);
400 return ComplexPairTy(U, U);
403 ComplexPairTy ComplexExprEmitter::
404 VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
405 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
406 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
410 ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
411 if (E->getCallReturnType(CGF.getContext())->isReferenceType())
412 return EmitLoadOfLValue(E);
414 return CGF.EmitCallExpr(E).getComplexVal();
417 ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
418 CodeGenFunction::StmtExprEvaluation eval(CGF);
419 Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
420 assert(RetAlloca.isValid() && "Expected complex return value");
421 return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
425 /// Emit a cast from complex value Val to DestType.
426 ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
429 SourceLocation Loc) {
430 // Get the src/dest element type.
431 SrcType = SrcType->castAs<ComplexType>()->getElementType();
432 DestType = DestType->castAs<ComplexType>()->getElementType();
434 // C99 6.3.1.6: When a value of complex type is converted to another
435 // complex type, both the real and imaginary parts follow the conversion
436 // rules for the corresponding real types.
438 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType, Loc);
440 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType, Loc);
444 ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
447 SourceLocation Loc) {
448 // Convert the input element to the element type of the complex.
449 DestType = DestType->castAs<ComplexType>()->getElementType();
450 Val = CGF.EmitScalarConversion(Val, SrcType, DestType, Loc);
452 // Return (realval, 0).
453 return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
456 ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
459 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
461 // Atomic to non-atomic casts may be more than a no-op for some platforms and
463 case CK_AtomicToNonAtomic:
464 case CK_NonAtomicToAtomic:
466 case CK_LValueToRValue:
467 case CK_UserDefinedConversion:
470 case CK_LValueBitCast: {
471 LValue origLV = CGF.EmitLValue(Op);
472 Address V = origLV.getAddress(CGF);
473 V = Builder.CreateElementBitCast(V, CGF.ConvertType(DestTy));
474 return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), Op->getExprLoc());
477 case CK_LValueToRValueBitCast: {
478 LValue SourceLVal = CGF.EmitLValue(Op);
479 Address Addr = Builder.CreateElementBitCast(SourceLVal.getAddress(CGF),
480 CGF.ConvertTypeForMem(DestTy));
481 LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy);
482 DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo());
483 return EmitLoadOfLValue(DestLV, Op->getExprLoc());
487 case CK_BaseToDerived:
488 case CK_DerivedToBase:
489 case CK_UncheckedDerivedToBase:
492 case CK_ArrayToPointerDecay:
493 case CK_FunctionToPointerDecay:
494 case CK_NullToPointer:
495 case CK_NullToMemberPointer:
496 case CK_BaseToDerivedMemberPointer:
497 case CK_DerivedToBaseMemberPointer:
498 case CK_MemberPointerToBoolean:
499 case CK_ReinterpretMemberPointer:
500 case CK_ConstructorConversion:
501 case CK_IntegralToPointer:
502 case CK_PointerToIntegral:
503 case CK_PointerToBoolean:
506 case CK_IntegralCast:
507 case CK_BooleanToSignedIntegral:
508 case CK_IntegralToBoolean:
509 case CK_IntegralToFloating:
510 case CK_FloatingToIntegral:
511 case CK_FloatingToBoolean:
512 case CK_FloatingCast:
513 case CK_CPointerToObjCPointerCast:
514 case CK_BlockPointerToObjCPointerCast:
515 case CK_AnyPointerToBlockPointerCast:
516 case CK_ObjCObjectLValueCast:
517 case CK_FloatingComplexToReal:
518 case CK_FloatingComplexToBoolean:
519 case CK_IntegralComplexToReal:
520 case CK_IntegralComplexToBoolean:
521 case CK_ARCProduceObject:
522 case CK_ARCConsumeObject:
523 case CK_ARCReclaimReturnedObject:
524 case CK_ARCExtendBlockObject:
525 case CK_CopyAndAutoreleaseBlockObject:
526 case CK_BuiltinFnToFnPtr:
527 case CK_ZeroToOCLOpaqueType:
528 case CK_AddressSpaceConversion:
529 case CK_IntToOCLSampler:
530 case CK_FixedPointCast:
531 case CK_FixedPointToBoolean:
532 case CK_FixedPointToIntegral:
533 case CK_IntegralToFixedPoint:
534 llvm_unreachable("invalid cast kind for complex value");
536 case CK_FloatingRealToComplex:
537 case CK_IntegralRealToComplex:
538 return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op), Op->getType(),
539 DestTy, Op->getExprLoc());
541 case CK_FloatingComplexCast:
542 case CK_FloatingComplexToIntegralComplex:
543 case CK_IntegralComplexCast:
544 case CK_IntegralComplexToFloatingComplex:
545 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy,
549 llvm_unreachable("unknown cast resulting in complex value");
552 ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
553 TestAndClearIgnoreReal();
554 TestAndClearIgnoreImag();
555 ComplexPairTy Op = Visit(E->getSubExpr());
557 llvm::Value *ResR, *ResI;
558 if (Op.first->getType()->isFloatingPointTy()) {
559 ResR = Builder.CreateFNeg(Op.first, "neg.r");
560 ResI = Builder.CreateFNeg(Op.second, "neg.i");
562 ResR = Builder.CreateNeg(Op.first, "neg.r");
563 ResI = Builder.CreateNeg(Op.second, "neg.i");
565 return ComplexPairTy(ResR, ResI);
568 ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
569 TestAndClearIgnoreReal();
570 TestAndClearIgnoreImag();
571 // ~(a+ib) = a + i*-b
572 ComplexPairTy Op = Visit(E->getSubExpr());
574 if (Op.second->getType()->isFloatingPointTy())
575 ResI = Builder.CreateFNeg(Op.second, "conj.i");
577 ResI = Builder.CreateNeg(Op.second, "conj.i");
579 return ComplexPairTy(Op.first, ResI);
582 ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
583 llvm::Value *ResR, *ResI;
585 if (Op.LHS.first->getType()->isFloatingPointTy()) {
586 ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
587 if (Op.LHS.second && Op.RHS.second)
588 ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
590 ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
591 assert(ResI && "Only one operand may be real!");
593 ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
594 assert(Op.LHS.second && Op.RHS.second &&
595 "Both operands of integer complex operators must be complex!");
596 ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
598 return ComplexPairTy(ResR, ResI);
601 ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
602 llvm::Value *ResR, *ResI;
603 if (Op.LHS.first->getType()->isFloatingPointTy()) {
604 ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
605 if (Op.LHS.second && Op.RHS.second)
606 ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
608 ResI = Op.LHS.second ? Op.LHS.second
609 : Builder.CreateFNeg(Op.RHS.second, "sub.i");
610 assert(ResI && "Only one operand may be real!");
612 ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
613 assert(Op.LHS.second && Op.RHS.second &&
614 "Both operands of integer complex operators must be complex!");
615 ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
617 return ComplexPairTy(ResR, ResI);
620 /// Emit a libcall for a binary operation on complex types.
621 ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
622 const BinOpInfo &Op) {
624 Args.add(RValue::get(Op.LHS.first),
625 Op.Ty->castAs<ComplexType>()->getElementType());
626 Args.add(RValue::get(Op.LHS.second),
627 Op.Ty->castAs<ComplexType>()->getElementType());
628 Args.add(RValue::get(Op.RHS.first),
629 Op.Ty->castAs<ComplexType>()->getElementType());
630 Args.add(RValue::get(Op.RHS.second),
631 Op.Ty->castAs<ComplexType>()->getElementType());
633 // We *must* use the full CG function call building logic here because the
634 // complex type has special ABI handling. We also should not forget about
635 // special calling convention which may be used for compiler builtins.
637 // We create a function qualified type to state that this call does not have
639 FunctionProtoType::ExtProtoInfo EPI;
640 EPI = EPI.withExceptionSpec(
641 FunctionProtoType::ExceptionSpecInfo(EST_BasicNoexcept));
642 SmallVector<QualType, 4> ArgsQTys(
643 4, Op.Ty->castAs<ComplexType>()->getElementType());
644 QualType FQTy = CGF.getContext().getFunctionType(Op.Ty, ArgsQTys, EPI);
645 const CGFunctionInfo &FuncInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall(
646 Args, cast<FunctionType>(FQTy.getTypePtr()), false);
648 llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
649 llvm::FunctionCallee Func = CGF.CGM.CreateRuntimeFunction(
650 FTy, LibCallName, llvm::AttributeList(), true);
651 CGCallee Callee = CGCallee::forDirect(Func, FQTy->getAs<FunctionProtoType>());
653 llvm::CallBase *Call;
654 RValue Res = CGF.EmitCall(FuncInfo, Callee, ReturnValueSlot(), Args, &Call);
655 Call->setCallingConv(CGF.CGM.getRuntimeCC());
656 return Res.getComplexVal();
659 /// Lookup the libcall name for a given floating point type complex
661 static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
662 switch (Ty->getTypeID()) {
664 llvm_unreachable("Unsupported floating point type!");
665 case llvm::Type::HalfTyID:
667 case llvm::Type::FloatTyID:
669 case llvm::Type::DoubleTyID:
671 case llvm::Type::PPC_FP128TyID:
673 case llvm::Type::X86_FP80TyID:
675 case llvm::Type::FP128TyID:
680 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
682 ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
685 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
687 if (Op.LHS.first->getType()->isFloatingPointTy()) {
688 // The general formulation is:
689 // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
691 // But we can fold away components which would be zero due to a real
692 // operand according to C11 Annex G.5.1p2.
693 // FIXME: C11 also provides for imaginary types which would allow folding
694 // still more of this within the type system.
696 if (Op.LHS.second && Op.RHS.second) {
697 // If both operands are complex, emit the core math directly, and then
698 // test for NaNs. If we find NaNs in the result, we delegate to a libcall
699 // to carefully re-compute the correct infinity representation if
700 // possible. The expectation is that the presence of NaNs here is
701 // *extremely* rare, and so the cost of the libcall is almost irrelevant.
702 // This is good, because the libcall re-computes the core multiplication
703 // exactly the same as we do here and re-tests for NaNs in order to be
704 // a generic complex*complex libcall.
706 // First compute the four products.
707 Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
708 Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
709 Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
710 Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
712 // The real part is the difference of the first two, the imaginary part is
713 // the sum of the second.
714 ResR = Builder.CreateFSub(AC, BD, "mul_r");
715 ResI = Builder.CreateFAdd(AD, BC, "mul_i");
717 // Emit the test for the real part becoming NaN and create a branch to
718 // handle it. We test for NaN by comparing the number to itself.
719 Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
720 llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
721 llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
722 llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
723 llvm::BasicBlock *OrigBB = Branch->getParent();
725 // Give hint that we very much don't expect to see NaNs.
726 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
727 llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1);
728 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
730 // Now test the imaginary part and create its branch.
731 CGF.EmitBlock(INaNBB);
732 Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
733 llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
734 Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
735 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
737 // Now emit the libcall on this slowest of the slow paths.
738 CGF.EmitBlock(LibCallBB);
739 Value *LibCallR, *LibCallI;
740 std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
741 getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
742 Builder.CreateBr(ContBB);
744 // Finally continue execution by phi-ing together the different
745 // computation paths.
746 CGF.EmitBlock(ContBB);
747 llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
748 RealPHI->addIncoming(ResR, OrigBB);
749 RealPHI->addIncoming(ResR, INaNBB);
750 RealPHI->addIncoming(LibCallR, LibCallBB);
751 llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
752 ImagPHI->addIncoming(ResI, OrigBB);
753 ImagPHI->addIncoming(ResI, INaNBB);
754 ImagPHI->addIncoming(LibCallI, LibCallBB);
755 return ComplexPairTy(RealPHI, ImagPHI);
757 assert((Op.LHS.second || Op.RHS.second) &&
758 "At least one operand must be complex!");
760 // If either of the operands is a real rather than a complex, the
761 // imaginary component is ignored when computing the real component of the
763 ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
766 ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
767 : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
769 assert(Op.LHS.second && Op.RHS.second &&
770 "Both operands of integer complex operators must be complex!");
771 Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
772 Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
773 ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
775 Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
776 Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
777 ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
779 return ComplexPairTy(ResR, ResI);
782 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
784 ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
785 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
786 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
788 llvm::Value *DSTr, *DSTi;
789 if (LHSr->getType()->isFloatingPointTy()) {
790 // If we have a complex operand on the RHS and FastMath is not allowed, we
791 // delegate to a libcall to handle all of the complexities and minimize
792 // underflow/overflow cases. When FastMath is allowed we construct the
793 // divide inline using the same algorithm as for integer operands.
795 // FIXME: We would be able to avoid the libcall in many places if we
796 // supported imaginary types in addition to complex types.
797 if (RHSi && !CGF.getLangOpts().FastMath) {
798 BinOpInfo LibCallOp = Op;
799 // If LHS was a real, supply a null imaginary part.
801 LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
803 switch (LHSr->getType()->getTypeID()) {
805 llvm_unreachable("Unsupported floating point type!");
806 case llvm::Type::HalfTyID:
807 return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
808 case llvm::Type::FloatTyID:
809 return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
810 case llvm::Type::DoubleTyID:
811 return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
812 case llvm::Type::PPC_FP128TyID:
813 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
814 case llvm::Type::X86_FP80TyID:
815 return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
816 case llvm::Type::FP128TyID:
817 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
821 LHSi = llvm::Constant::getNullValue(RHSi->getType());
823 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
824 llvm::Value *AC = Builder.CreateFMul(LHSr, RHSr); // a*c
825 llvm::Value *BD = Builder.CreateFMul(LHSi, RHSi); // b*d
826 llvm::Value *ACpBD = Builder.CreateFAdd(AC, BD); // ac+bd
828 llvm::Value *CC = Builder.CreateFMul(RHSr, RHSr); // c*c
829 llvm::Value *DD = Builder.CreateFMul(RHSi, RHSi); // d*d
830 llvm::Value *CCpDD = Builder.CreateFAdd(CC, DD); // cc+dd
832 llvm::Value *BC = Builder.CreateFMul(LHSi, RHSr); // b*c
833 llvm::Value *AD = Builder.CreateFMul(LHSr, RHSi); // a*d
834 llvm::Value *BCmAD = Builder.CreateFSub(BC, AD); // bc-ad
836 DSTr = Builder.CreateFDiv(ACpBD, CCpDD);
837 DSTi = Builder.CreateFDiv(BCmAD, CCpDD);
839 assert(LHSi && "Can have at most one non-complex operand!");
841 DSTr = Builder.CreateFDiv(LHSr, RHSr);
842 DSTi = Builder.CreateFDiv(LHSi, RHSr);
845 assert(Op.LHS.second && Op.RHS.second &&
846 "Both operands of integer complex operators must be complex!");
847 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
848 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
849 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
850 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
852 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
853 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
854 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
856 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
857 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
858 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
860 if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
861 DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
862 DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
864 DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
865 DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
869 return ComplexPairTy(DSTr, DSTi);
872 ComplexExprEmitter::BinOpInfo
873 ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
874 TestAndClearIgnoreReal();
875 TestAndClearIgnoreImag();
877 if (E->getLHS()->getType()->isRealFloatingType())
878 Ops.LHS = ComplexPairTy(CGF.EmitScalarExpr(E->getLHS()), nullptr);
880 Ops.LHS = Visit(E->getLHS());
881 if (E->getRHS()->getType()->isRealFloatingType())
882 Ops.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
884 Ops.RHS = Visit(E->getRHS());
886 Ops.Ty = E->getType();
891 LValue ComplexExprEmitter::
892 EmitCompoundAssignLValue(const CompoundAssignOperator *E,
893 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
895 TestAndClearIgnoreReal();
896 TestAndClearIgnoreImag();
897 QualType LHSTy = E->getLHS()->getType();
898 if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
899 LHSTy = AT->getValueType();
903 // Load the RHS and LHS operands.
904 // __block variables need to have the rhs evaluated first, plus this should
905 // improve codegen a little.
906 OpInfo.Ty = E->getComputationResultType();
907 QualType ComplexElementTy = cast<ComplexType>(OpInfo.Ty)->getElementType();
909 // The RHS should have been converted to the computation type.
910 if (E->getRHS()->getType()->isRealFloatingType()) {
913 .hasSameUnqualifiedType(ComplexElementTy, E->getRHS()->getType()));
914 OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
916 assert(CGF.getContext()
917 .hasSameUnqualifiedType(OpInfo.Ty, E->getRHS()->getType()));
918 OpInfo.RHS = Visit(E->getRHS());
921 LValue LHS = CGF.EmitLValue(E->getLHS());
923 // Load from the l-value and convert it.
924 SourceLocation Loc = E->getExprLoc();
925 if (LHSTy->isAnyComplexType()) {
926 ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
927 OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
929 llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, Loc);
930 // For floating point real operands we can directly pass the scalar form
931 // to the binary operator emission and potentially get more efficient code.
932 if (LHSTy->isRealFloatingType()) {
933 if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
934 LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy, Loc);
935 OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
937 OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
941 // Expand the binary operator.
942 ComplexPairTy Result = (this->*Func)(OpInfo);
944 // Truncate the result and store it into the LHS lvalue.
945 if (LHSTy->isAnyComplexType()) {
946 ComplexPairTy ResVal =
947 EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy, Loc);
948 EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
949 Val = RValue::getComplex(ResVal);
951 llvm::Value *ResVal =
952 CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy, Loc);
953 CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
954 Val = RValue::get(ResVal);
960 // Compound assignments.
961 ComplexPairTy ComplexExprEmitter::
962 EmitCompoundAssign(const CompoundAssignOperator *E,
963 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
965 LValue LV = EmitCompoundAssignLValue(E, Func, Val);
967 // The result of an assignment in C is the assigned r-value.
968 if (!CGF.getLangOpts().CPlusPlus)
969 return Val.getComplexVal();
971 // If the lvalue is non-volatile, return the computed value of the assignment.
972 if (!LV.isVolatileQualified())
973 return Val.getComplexVal();
975 return EmitLoadOfLValue(LV, E->getExprLoc());
978 LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
979 ComplexPairTy &Val) {
980 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
981 E->getRHS()->getType()) &&
982 "Invalid assignment");
983 TestAndClearIgnoreReal();
984 TestAndClearIgnoreImag();
986 // Emit the RHS. __block variables need the RHS evaluated first.
987 Val = Visit(E->getRHS());
989 // Compute the address to store into.
990 LValue LHS = CGF.EmitLValue(E->getLHS());
992 // Store the result value into the LHS lvalue.
993 EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
998 ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1000 LValue LV = EmitBinAssignLValue(E, Val);
1002 // The result of an assignment in C is the assigned r-value.
1003 if (!CGF.getLangOpts().CPlusPlus)
1006 // If the lvalue is non-volatile, return the computed value of the assignment.
1007 if (!LV.isVolatileQualified())
1010 return EmitLoadOfLValue(LV, E->getExprLoc());
1013 ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
1014 CGF.EmitIgnoredExpr(E->getLHS());
1015 return Visit(E->getRHS());
1018 ComplexPairTy ComplexExprEmitter::
1019 VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
1020 TestAndClearIgnoreReal();
1021 TestAndClearIgnoreImag();
1022 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1023 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
1024 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
1026 // Bind the common expression if necessary.
1027 CodeGenFunction::OpaqueValueMapping binding(CGF, E);
1030 CodeGenFunction::ConditionalEvaluation eval(CGF);
1031 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
1032 CGF.getProfileCount(E));
1035 CGF.EmitBlock(LHSBlock);
1036 CGF.incrementProfileCounter(E);
1037 ComplexPairTy LHS = Visit(E->getTrueExpr());
1038 LHSBlock = Builder.GetInsertBlock();
1039 CGF.EmitBranch(ContBlock);
1043 CGF.EmitBlock(RHSBlock);
1044 ComplexPairTy RHS = Visit(E->getFalseExpr());
1045 RHSBlock = Builder.GetInsertBlock();
1046 CGF.EmitBlock(ContBlock);
1049 // Create a PHI node for the real part.
1050 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
1051 RealPN->addIncoming(LHS.first, LHSBlock);
1052 RealPN->addIncoming(RHS.first, RHSBlock);
1054 // Create a PHI node for the imaginary part.
1055 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
1056 ImagPN->addIncoming(LHS.second, LHSBlock);
1057 ImagPN->addIncoming(RHS.second, RHSBlock);
1059 return ComplexPairTy(RealPN, ImagPN);
1062 ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1063 return Visit(E->getChosenSubExpr());
1066 ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
1067 bool Ignore = TestAndClearIgnoreReal();
1069 assert (Ignore == false && "init list ignored");
1070 Ignore = TestAndClearIgnoreImag();
1072 assert (Ignore == false && "init list ignored");
1074 if (E->getNumInits() == 2) {
1075 llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
1076 llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
1077 return ComplexPairTy(Real, Imag);
1078 } else if (E->getNumInits() == 1) {
1079 return Visit(E->getInit(0));
1082 // Empty init list initializes to null
1083 assert(E->getNumInits() == 0 && "Unexpected number of inits");
1084 QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
1085 llvm::Type* LTy = CGF.ConvertType(Ty);
1086 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
1087 return ComplexPairTy(zeroConstant, zeroConstant);
1090 ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
1091 Address ArgValue = Address::invalid();
1092 Address ArgPtr = CGF.EmitVAArg(E, ArgValue);
1094 if (!ArgPtr.isValid()) {
1095 CGF.ErrorUnsupported(E, "complex va_arg expression");
1097 CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
1098 llvm::Value *U = llvm::UndefValue::get(EltTy);
1099 return ComplexPairTy(U, U);
1102 return EmitLoadOfLValue(CGF.MakeAddrLValue(ArgPtr, E->getType()),
1106 //===----------------------------------------------------------------------===//
1107 // Entry Point into this File
1108 //===----------------------------------------------------------------------===//
1110 /// EmitComplexExpr - Emit the computation of the specified expression of
1111 /// complex type, ignoring the result.
1112 ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
1114 assert(E && getComplexType(E->getType()) &&
1115 "Invalid complex expression to emit");
1117 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
1118 .Visit(const_cast<Expr *>(E));
1121 void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest,
1123 assert(E && getComplexType(E->getType()) &&
1124 "Invalid complex expression to emit");
1125 ComplexExprEmitter Emitter(*this);
1126 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
1127 Emitter.EmitStoreOfComplex(Val, dest, isInit);
1130 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
1131 void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest,
1133 ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
1136 /// EmitLoadOfComplex - Load a complex number from the specified address.
1137 ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src,
1138 SourceLocation loc) {
1139 return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
1142 LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
1143 assert(E->getOpcode() == BO_Assign);
1144 ComplexPairTy Val; // ignored
1145 LValue LVal = ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1146 if (getLangOpts().OpenMP)
1147 CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(*this,
1152 typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1153 const ComplexExprEmitter::BinOpInfo &);
1155 static CompoundFunc getComplexOp(BinaryOperatorKind Op) {
1157 case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1158 case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1159 case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1160 case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
1162 llvm_unreachable("unexpected complex compound assignment");
1166 LValue CodeGenFunction::
1167 EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
1168 CompoundFunc Op = getComplexOp(E->getOpcode());
1170 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1173 LValue CodeGenFunction::
1174 EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
1175 llvm::Value *&Result) {
1176 CompoundFunc Op = getComplexOp(E->getOpcode());
1178 LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1179 Result = Val.getScalarVal();