1//===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code to emit Expr nodes with complex types as LLVM code.
10//
11//===----------------------------------------------------------------------===//
12
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"
23#include <algorithm>
24using namespace clang;
25using namespace CodeGen;
26
27//===----------------------------------------------------------------------===//
28// Complex Expression Emitter
29//===----------------------------------------------------------------------===//
30
31namespace llvm {
32extern cl::opt<bool> EnableSingleByteCoverage;
33} // namespace llvm
34
35typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
36
37/// Return the complex type that we are meant to emit.
38static const ComplexType *getComplexType(QualType type) {
39 type = type.getCanonicalType();
40 if (const ComplexType *comp = dyn_cast<ComplexType>(Val&: type)) {
41 return comp;
42 } else {
43 return cast<ComplexType>(Val: cast<AtomicType>(Val&: type)->getValueType());
44 }
45}
46
47namespace {
48class ComplexExprEmitter
49 : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
50 CodeGenFunction &CGF;
51 CGBuilderTy &Builder;
52 bool IgnoreReal;
53 bool IgnoreImag;
54 bool FPHasBeenPromoted;
55
56public:
57 ComplexExprEmitter(CodeGenFunction &cgf, bool ir = false, bool ii = false)
58 : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii),
59 FPHasBeenPromoted(false) {}
60
61 //===--------------------------------------------------------------------===//
62 // Utilities
63 //===--------------------------------------------------------------------===//
64
65 bool TestAndClearIgnoreReal() {
66 bool I = IgnoreReal;
67 IgnoreReal = false;
68 return I;
69 }
70 bool TestAndClearIgnoreImag() {
71 bool I = IgnoreImag;
72 IgnoreImag = false;
73 return I;
74 }
75
76 /// EmitLoadOfLValue - Given an expression with complex type that represents a
77 /// value l-value, this method emits the address of the l-value, then loads
78 /// and returns the result.
79 ComplexPairTy EmitLoadOfLValue(const Expr *E) {
80 return EmitLoadOfLValue(LV: CGF.EmitLValue(E), Loc: E->getExprLoc());
81 }
82
83 ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc);
84
85 /// EmitStoreOfComplex - Store the specified real/imag parts into the
86 /// specified value pointer.
87 void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit);
88
89 /// Emit a cast from complex value Val to DestType.
90 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
91 QualType DestType, SourceLocation Loc);
92 /// Emit a cast from scalar value Val to DestType.
93 ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType,
94 QualType DestType, SourceLocation Loc);
95
96 //===--------------------------------------------------------------------===//
97 // Visitor Methods
98 //===--------------------------------------------------------------------===//
99
100 ComplexPairTy Visit(Expr *E) {
101 ApplyDebugLocation DL(CGF, E);
102 return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
103 }
104
105 ComplexPairTy VisitStmt(Stmt *S) {
106 S->dump(OS&: llvm::errs(), Context: CGF.getContext());
107 llvm_unreachable("Stmt can't have complex result type!");
108 }
109 ComplexPairTy VisitExpr(Expr *S);
110 ComplexPairTy VisitConstantExpr(ConstantExpr *E) {
111 if (llvm::Constant *Result = ConstantEmitter(CGF).tryEmitConstantExpr(CE: E))
112 return ComplexPairTy(Result->getAggregateElement(Elt: 0U),
113 Result->getAggregateElement(Elt: 1U));
114 return Visit(E: E->getSubExpr());
115 }
116 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(E: PE->getSubExpr());}
117 ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
118 return Visit(E: GE->getResultExpr());
119 }
120 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
121 ComplexPairTy
122 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
123 return Visit(E: PE->getReplacement());
124 }
125 ComplexPairTy VisitCoawaitExpr(CoawaitExpr *S) {
126 return CGF.EmitCoawaitExpr(E: *S).getComplexVal();
127 }
128 ComplexPairTy VisitCoyieldExpr(CoyieldExpr *S) {
129 return CGF.EmitCoyieldExpr(E: *S).getComplexVal();
130 }
131 ComplexPairTy VisitUnaryCoawait(const UnaryOperator *E) {
132 return Visit(E: E->getSubExpr());
133 }
134
135 ComplexPairTy emitConstant(const CodeGenFunction::ConstantEmission &Constant,
136 Expr *E) {
137 assert(Constant && "not a constant");
138 if (Constant.isReference())
139 return EmitLoadOfLValue(LV: Constant.getReferenceLValue(CGF, refExpr: E),
140 Loc: E->getExprLoc());
141
142 llvm::Constant *pair = Constant.getValue();
143 return ComplexPairTy(pair->getAggregateElement(Elt: 0U),
144 pair->getAggregateElement(Elt: 1U));
145 }
146
147 // l-values.
148 ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
149 if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(refExpr: E))
150 return emitConstant(Constant, E);
151 return EmitLoadOfLValue(E);
152 }
153 ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
154 return EmitLoadOfLValue(E);
155 }
156 ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
157 return CGF.EmitObjCMessageExpr(E).getComplexVal();
158 }
159 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
160 ComplexPairTy VisitMemberExpr(MemberExpr *ME) {
161 if (CodeGenFunction::ConstantEmission Constant =
162 CGF.tryEmitAsConstant(ME)) {
163 CGF.EmitIgnoredExpr(E: ME->getBase());
164 return emitConstant(Constant, ME);
165 }
166 return EmitLoadOfLValue(ME);
167 }
168 ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
169 if (E->isGLValue())
170 return EmitLoadOfLValue(LV: CGF.getOrCreateOpaqueLValueMapping(e: E),
171 Loc: E->getExprLoc());
172 return CGF.getOrCreateOpaqueRValueMapping(e: E).getComplexVal();
173 }
174
175 ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
176 return CGF.EmitPseudoObjectRValue(e: E).getComplexVal();
177 }
178
179 // FIXME: CompoundLiteralExpr
180
181 ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy);
182 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
183 // Unlike for scalars, we don't have to worry about function->ptr demotion
184 // here.
185 if (E->changesVolatileQualification())
186 return EmitLoadOfLValue(E);
187 return EmitCast(CK: E->getCastKind(), Op: E->getSubExpr(), DestTy: E->getType());
188 }
189 ComplexPairTy VisitCastExpr(CastExpr *E) {
190 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(Val: E))
191 CGF.CGM.EmitExplicitCastExprType(E: ECE, CGF: &CGF);
192 if (E->changesVolatileQualification())
193 return EmitLoadOfLValue(E);
194 return EmitCast(CK: E->getCastKind(), Op: E->getSubExpr(), DestTy: E->getType());
195 }
196 ComplexPairTy VisitCallExpr(const CallExpr *E);
197 ComplexPairTy VisitStmtExpr(const StmtExpr *E);
198
199 // Operators.
200 ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
201 bool isInc, bool isPre) {
202 LValue LV = CGF.EmitLValue(E: E->getSubExpr());
203 return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
204 }
205 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
206 return VisitPrePostIncDec(E, isInc: false, isPre: false);
207 }
208 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
209 return VisitPrePostIncDec(E, isInc: true, isPre: false);
210 }
211 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
212 return VisitPrePostIncDec(E, isInc: false, isPre: true);
213 }
214 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
215 return VisitPrePostIncDec(E, isInc: true, isPre: true);
216 }
217 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
218
219 ComplexPairTy VisitUnaryPlus(const UnaryOperator *E,
220 QualType PromotionType = QualType());
221 ComplexPairTy VisitPlus(const UnaryOperator *E, QualType PromotionType);
222 ComplexPairTy VisitUnaryMinus(const UnaryOperator *E,
223 QualType PromotionType = QualType());
224 ComplexPairTy VisitMinus(const UnaryOperator *E, QualType PromotionType);
225 ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
226 // LNot,Real,Imag never return complex.
227 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
228 return Visit(E: E->getSubExpr());
229 }
230 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
231 CodeGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE);
232 return Visit(E: DAE->getExpr());
233 }
234 ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
235 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF, DIE);
236 return Visit(E: DIE->getExpr());
237 }
238 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
239 CodeGenFunction::RunCleanupsScope Scope(CGF);
240 ComplexPairTy Vals = Visit(E: E->getSubExpr());
241 // Defend against dominance problems caused by jumps out of expression
242 // evaluation through the shared cleanup block.
243 Scope.ForceCleanup(ValuesToReload: {&Vals.first, &Vals.second});
244 return Vals;
245 }
246 ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
247 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
248 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
249 llvm::Constant *Null = llvm::Constant::getNullValue(Ty: CGF.ConvertType(T: Elem));
250 return ComplexPairTy(Null, Null);
251 }
252 ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
253 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
254 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
255 llvm::Constant *Null =
256 llvm::Constant::getNullValue(Ty: CGF.ConvertType(T: Elem));
257 return ComplexPairTy(Null, Null);
258 }
259
260 struct BinOpInfo {
261 ComplexPairTy LHS;
262 ComplexPairTy RHS;
263 QualType Ty; // Computation Type.
264 FPOptions FPFeatures;
265 };
266
267 BinOpInfo EmitBinOps(const BinaryOperator *E,
268 QualType PromotionTy = QualType());
269 ComplexPairTy EmitPromoted(const Expr *E, QualType PromotionTy);
270 ComplexPairTy EmitPromotedComplexOperand(const Expr *E, QualType PromotionTy);
271 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
272 ComplexPairTy (ComplexExprEmitter::*Func)
273 (const BinOpInfo &),
274 RValue &Val);
275 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
276 ComplexPairTy (ComplexExprEmitter::*Func)
277 (const BinOpInfo &));
278
279 ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
280 ComplexPairTy EmitBinSub(const BinOpInfo &Op);
281 ComplexPairTy EmitBinMul(const BinOpInfo &Op);
282 ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
283 ComplexPairTy EmitAlgebraicDiv(llvm::Value *A, llvm::Value *B, llvm::Value *C,
284 llvm::Value *D);
285 ComplexPairTy EmitRangeReductionDiv(llvm::Value *A, llvm::Value *B,
286 llvm::Value *C, llvm::Value *D);
287
288 ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
289 const BinOpInfo &Op);
290
291 QualType GetHigherPrecisionFPType(QualType ElementType) {
292 const auto *CurrentBT = cast<BuiltinType>(Val&: ElementType);
293 switch (CurrentBT->getKind()) {
294 case BuiltinType::Kind::Float16:
295 return CGF.getContext().FloatTy;
296 case BuiltinType::Kind::Float:
297 case BuiltinType::Kind::BFloat16:
298 return CGF.getContext().DoubleTy;
299 case BuiltinType::Kind::Double:
300 return CGF.getContext().LongDoubleTy;
301 default:
302 return ElementType;
303 }
304 }
305
306 QualType HigherPrecisionTypeForComplexArithmetic(QualType ElementType,
307 bool IsDivOpCode) {
308 QualType HigherElementType = GetHigherPrecisionFPType(ElementType);
309 const llvm::fltSemantics &ElementTypeSemantics =
310 CGF.getContext().getFloatTypeSemantics(T: ElementType);
311 const llvm::fltSemantics &HigherElementTypeSemantics =
312 CGF.getContext().getFloatTypeSemantics(T: HigherElementType);
313 // Check that the promoted type can handle the intermediate values without
314 // overflowing. This can be interpreted as:
315 // (SmallerType.LargestFiniteVal * SmallerType.LargestFiniteVal) * 2 <=
316 // LargerType.LargestFiniteVal.
317 // In terms of exponent it gives this formula:
318 // (SmallerType.LargestFiniteVal * SmallerType.LargestFiniteVal
319 // doubles the exponent of SmallerType.LargestFiniteVal)
320 if (llvm::APFloat::semanticsMaxExponent(ElementTypeSemantics) * 2 + 1 <=
321 llvm::APFloat::semanticsMaxExponent(HigherElementTypeSemantics)) {
322 FPHasBeenPromoted = true;
323 return CGF.getContext().getComplexType(T: HigherElementType);
324 } else {
325 DiagnosticsEngine &Diags = CGF.CGM.getDiags();
326 Diags.Report(diag::warn_next_larger_fp_type_same_size_than_fp);
327 return QualType();
328 }
329 }
330
331 QualType getPromotionType(QualType Ty, bool IsDivOpCode = false) {
332 if (auto *CT = Ty->getAs<ComplexType>()) {
333 QualType ElementType = CT->getElementType();
334 if (IsDivOpCode && ElementType->isFloatingType() &&
335 CGF.getLangOpts().getComplexRange() ==
336 LangOptions::ComplexRangeKind::CX_Promoted)
337 return HigherPrecisionTypeForComplexArithmetic(ElementType,
338 IsDivOpCode);
339 if (ElementType.UseExcessPrecision(Ctx: CGF.getContext()))
340 return CGF.getContext().getComplexType(CGF.getContext().FloatTy);
341 }
342 if (Ty.UseExcessPrecision(Ctx: CGF.getContext()))
343 return CGF.getContext().FloatTy;
344 return QualType();
345 }
346
347#define HANDLEBINOP(OP) \
348 ComplexPairTy VisitBin##OP(const BinaryOperator *E) { \
349 QualType promotionTy = getPromotionType( \
350 E->getType(), \
351 (E->getOpcode() == BinaryOperatorKind::BO_Div) ? true : false); \
352 ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy)); \
353 if (!promotionTy.isNull()) \
354 result = CGF.EmitUnPromotedValue(result, E->getType()); \
355 return result; \
356 }
357
358 HANDLEBINOP(Mul)
359 HANDLEBINOP(Div)
360 HANDLEBINOP(Add)
361 HANDLEBINOP(Sub)
362#undef HANDLEBINOP
363
364 ComplexPairTy VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E) {
365 return Visit(E: E->getSemanticForm());
366 }
367
368 // Compound assignments.
369 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
370 return EmitCompoundAssign(E, Func: &ComplexExprEmitter::EmitBinAdd);
371 }
372 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
373 return EmitCompoundAssign(E, Func: &ComplexExprEmitter::EmitBinSub);
374 }
375 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
376 return EmitCompoundAssign(E, Func: &ComplexExprEmitter::EmitBinMul);
377 }
378 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
379 return EmitCompoundAssign(E, Func: &ComplexExprEmitter::EmitBinDiv);
380 }
381
382 // GCC rejects rem/and/or/xor for integer complex.
383 // Logical and/or always return int, never complex.
384
385 // No comparisons produce a complex result.
386
387 LValue EmitBinAssignLValue(const BinaryOperator *E,
388 ComplexPairTy &Val);
389 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
390 ComplexPairTy VisitBinComma (const BinaryOperator *E);
391
392
393 ComplexPairTy
394 VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
395 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
396
397 ComplexPairTy VisitInitListExpr(InitListExpr *E);
398
399 ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
400 return EmitLoadOfLValue(E);
401 }
402
403 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
404
405 ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
406 return CGF.EmitAtomicExpr(E).getComplexVal();
407 }
408
409 ComplexPairTy VisitPackIndexingExpr(PackIndexingExpr *E) {
410 return Visit(E: E->getSelectedExpr());
411 }
412};
413} // end anonymous namespace.
414
415//===----------------------------------------------------------------------===//
416// Utilities
417//===----------------------------------------------------------------------===//
418
419Address CodeGenFunction::emitAddrOfRealComponent(Address addr,
420 QualType complexType) {
421 return Builder.CreateStructGEP(Addr: addr, Index: 0, Name: addr.getName() + ".realp");
422}
423
424Address CodeGenFunction::emitAddrOfImagComponent(Address addr,
425 QualType complexType) {
426 return Builder.CreateStructGEP(Addr: addr, Index: 1, Name: addr.getName() + ".imagp");
427}
428
429/// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
430/// load the real and imaginary pieces, returning them as Real/Imag.
431ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
432 SourceLocation loc) {
433 assert(lvalue.isSimple() && "non-simple complex l-value?");
434 if (lvalue.getType()->isAtomicType())
435 return CGF.EmitAtomicLoad(LV: lvalue, SL: loc).getComplexVal();
436
437 Address SrcPtr = lvalue.getAddress(CGF);
438 bool isVolatile = lvalue.isVolatileQualified();
439
440 llvm::Value *Real = nullptr, *Imag = nullptr;
441
442 if (!IgnoreReal || isVolatile) {
443 Address RealP = CGF.emitAddrOfRealComponent(addr: SrcPtr, complexType: lvalue.getType());
444 Real = Builder.CreateLoad(Addr: RealP, IsVolatile: isVolatile, Name: SrcPtr.getName() + ".real");
445 }
446
447 if (!IgnoreImag || isVolatile) {
448 Address ImagP = CGF.emitAddrOfImagComponent(addr: SrcPtr, complexType: lvalue.getType());
449 Imag = Builder.CreateLoad(Addr: ImagP, IsVolatile: isVolatile, Name: SrcPtr.getName() + ".imag");
450 }
451
452 return ComplexPairTy(Real, Imag);
453}
454
455/// EmitStoreOfComplex - Store the specified real/imag parts into the
456/// specified value pointer.
457void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
458 bool isInit) {
459 if (lvalue.getType()->isAtomicType() ||
460 (!isInit && CGF.LValueIsSuitableForInlineAtomic(Src: lvalue)))
461 return CGF.EmitAtomicStore(rvalue: RValue::getComplex(C: Val), lvalue, isInit);
462
463 Address Ptr = lvalue.getAddress(CGF);
464 Address RealPtr = CGF.emitAddrOfRealComponent(addr: Ptr, complexType: lvalue.getType());
465 Address ImagPtr = CGF.emitAddrOfImagComponent(addr: Ptr, complexType: lvalue.getType());
466
467 Builder.CreateStore(Val: Val.first, Addr: RealPtr, IsVolatile: lvalue.isVolatileQualified());
468 Builder.CreateStore(Val: Val.second, Addr: ImagPtr, IsVolatile: lvalue.isVolatileQualified());
469}
470
471
472
473//===----------------------------------------------------------------------===//
474// Visitor Methods
475//===----------------------------------------------------------------------===//
476
477ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
478 CGF.ErrorUnsupported(E, "complex expression");
479 llvm::Type *EltTy =
480 CGF.ConvertType(T: getComplexType(type: E->getType())->getElementType());
481 llvm::Value *U = llvm::UndefValue::get(T: EltTy);
482 return ComplexPairTy(U, U);
483}
484
485ComplexPairTy ComplexExprEmitter::
486VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
487 llvm::Value *Imag = CGF.EmitScalarExpr(E: IL->getSubExpr());
488 return ComplexPairTy(llvm::Constant::getNullValue(Ty: Imag->getType()), Imag);
489}
490
491
492ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
493 if (E->getCallReturnType(Ctx: CGF.getContext())->isReferenceType())
494 return EmitLoadOfLValue(E);
495
496 return CGF.EmitCallExpr(E).getComplexVal();
497}
498
499ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
500 CodeGenFunction::StmtExprEvaluation eval(CGF);
501 Address RetAlloca = CGF.EmitCompoundStmt(S: *E->getSubStmt(), GetLast: true);
502 assert(RetAlloca.isValid() && "Expected complex return value");
503 return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
504 E->getExprLoc());
505}
506
507/// Emit a cast from complex value Val to DestType.
508ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
509 QualType SrcType,
510 QualType DestType,
511 SourceLocation Loc) {
512 // Get the src/dest element type.
513 SrcType = SrcType->castAs<ComplexType>()->getElementType();
514 DestType = DestType->castAs<ComplexType>()->getElementType();
515
516 // C99 6.3.1.6: When a value of complex type is converted to another
517 // complex type, both the real and imaginary parts follow the conversion
518 // rules for the corresponding real types.
519 if (Val.first)
520 Val.first = CGF.EmitScalarConversion(Src: Val.first, SrcTy: SrcType, DstTy: DestType, Loc);
521 if (Val.second)
522 Val.second = CGF.EmitScalarConversion(Src: Val.second, SrcTy: SrcType, DstTy: DestType, Loc);
523 return Val;
524}
525
526ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
527 QualType SrcType,
528 QualType DestType,
529 SourceLocation Loc) {
530 // Convert the input element to the element type of the complex.
531 DestType = DestType->castAs<ComplexType>()->getElementType();
532 Val = CGF.EmitScalarConversion(Src: Val, SrcTy: SrcType, DstTy: DestType, Loc);
533
534 // Return (realval, 0).
535 return ComplexPairTy(Val, llvm::Constant::getNullValue(Ty: Val->getType()));
536}
537
538ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
539 QualType DestTy) {
540 switch (CK) {
541 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
542
543 // Atomic to non-atomic casts may be more than a no-op for some platforms and
544 // for some types.
545 case CK_AtomicToNonAtomic:
546 case CK_NonAtomicToAtomic:
547 case CK_NoOp:
548 case CK_LValueToRValue:
549 case CK_UserDefinedConversion:
550 return Visit(E: Op);
551
552 case CK_LValueBitCast: {
553 LValue origLV = CGF.EmitLValue(E: Op);
554 Address V = origLV.getAddress(CGF).withElementType(ElemTy: CGF.ConvertType(T: DestTy));
555 return EmitLoadOfLValue(lvalue: CGF.MakeAddrLValue(Addr: V, T: DestTy), loc: Op->getExprLoc());
556 }
557
558 case CK_LValueToRValueBitCast: {
559 LValue SourceLVal = CGF.EmitLValue(E: Op);
560 Address Addr = SourceLVal.getAddress(CGF).withElementType(
561 ElemTy: CGF.ConvertTypeForMem(T: DestTy));
562 LValue DestLV = CGF.MakeAddrLValue(Addr, T: DestTy);
563 DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo());
564 return EmitLoadOfLValue(lvalue: DestLV, loc: Op->getExprLoc());
565 }
566
567 case CK_BitCast:
568 case CK_BaseToDerived:
569 case CK_DerivedToBase:
570 case CK_UncheckedDerivedToBase:
571 case CK_Dynamic:
572 case CK_ToUnion:
573 case CK_ArrayToPointerDecay:
574 case CK_FunctionToPointerDecay:
575 case CK_NullToPointer:
576 case CK_NullToMemberPointer:
577 case CK_BaseToDerivedMemberPointer:
578 case CK_DerivedToBaseMemberPointer:
579 case CK_MemberPointerToBoolean:
580 case CK_ReinterpretMemberPointer:
581 case CK_ConstructorConversion:
582 case CK_IntegralToPointer:
583 case CK_PointerToIntegral:
584 case CK_PointerToBoolean:
585 case CK_ToVoid:
586 case CK_VectorSplat:
587 case CK_IntegralCast:
588 case CK_BooleanToSignedIntegral:
589 case CK_IntegralToBoolean:
590 case CK_IntegralToFloating:
591 case CK_FloatingToIntegral:
592 case CK_FloatingToBoolean:
593 case CK_FloatingCast:
594 case CK_CPointerToObjCPointerCast:
595 case CK_BlockPointerToObjCPointerCast:
596 case CK_AnyPointerToBlockPointerCast:
597 case CK_ObjCObjectLValueCast:
598 case CK_FloatingComplexToReal:
599 case CK_FloatingComplexToBoolean:
600 case CK_IntegralComplexToReal:
601 case CK_IntegralComplexToBoolean:
602 case CK_ARCProduceObject:
603 case CK_ARCConsumeObject:
604 case CK_ARCReclaimReturnedObject:
605 case CK_ARCExtendBlockObject:
606 case CK_CopyAndAutoreleaseBlockObject:
607 case CK_BuiltinFnToFnPtr:
608 case CK_ZeroToOCLOpaqueType:
609 case CK_AddressSpaceConversion:
610 case CK_IntToOCLSampler:
611 case CK_FloatingToFixedPoint:
612 case CK_FixedPointToFloating:
613 case CK_FixedPointCast:
614 case CK_FixedPointToBoolean:
615 case CK_FixedPointToIntegral:
616 case CK_IntegralToFixedPoint:
617 case CK_MatrixCast:
618 case CK_HLSLVectorTruncation:
619 case CK_HLSLArrayRValue:
620 llvm_unreachable("invalid cast kind for complex value");
621
622 case CK_FloatingRealToComplex:
623 case CK_IntegralRealToComplex: {
624 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op);
625 return EmitScalarToComplexCast(Val: CGF.EmitScalarExpr(E: Op), SrcType: Op->getType(),
626 DestType: DestTy, Loc: Op->getExprLoc());
627 }
628
629 case CK_FloatingComplexCast:
630 case CK_FloatingComplexToIntegralComplex:
631 case CK_IntegralComplexCast:
632 case CK_IntegralComplexToFloatingComplex: {
633 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op);
634 return EmitComplexToComplexCast(Val: Visit(E: Op), SrcType: Op->getType(), DestType: DestTy,
635 Loc: Op->getExprLoc());
636 }
637 }
638
639 llvm_unreachable("unknown cast resulting in complex value");
640}
641
642ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E,
643 QualType PromotionType) {
644 QualType promotionTy = PromotionType.isNull()
645 ? getPromotionType(Ty: E->getSubExpr()->getType())
646 : PromotionType;
647 ComplexPairTy result = VisitPlus(E, PromotionType: promotionTy);
648 if (!promotionTy.isNull())
649 return CGF.EmitUnPromotedValue(result, PromotionType: E->getSubExpr()->getType());
650 return result;
651}
652
653ComplexPairTy ComplexExprEmitter::VisitPlus(const UnaryOperator *E,
654 QualType PromotionType) {
655 TestAndClearIgnoreReal();
656 TestAndClearIgnoreImag();
657 if (!PromotionType.isNull())
658 return CGF.EmitPromotedComplexExpr(E: E->getSubExpr(), PromotionType);
659 return Visit(E: E->getSubExpr());
660}
661
662ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
663 QualType PromotionType) {
664 QualType promotionTy = PromotionType.isNull()
665 ? getPromotionType(Ty: E->getSubExpr()->getType())
666 : PromotionType;
667 ComplexPairTy result = VisitMinus(E, PromotionType: promotionTy);
668 if (!promotionTy.isNull())
669 return CGF.EmitUnPromotedValue(result, PromotionType: E->getSubExpr()->getType());
670 return result;
671}
672ComplexPairTy ComplexExprEmitter::VisitMinus(const UnaryOperator *E,
673 QualType PromotionType) {
674 TestAndClearIgnoreReal();
675 TestAndClearIgnoreImag();
676 ComplexPairTy Op;
677 if (!PromotionType.isNull())
678 Op = CGF.EmitPromotedComplexExpr(E: E->getSubExpr(), PromotionType);
679 else
680 Op = Visit(E: E->getSubExpr());
681
682 llvm::Value *ResR, *ResI;
683 if (Op.first->getType()->isFloatingPointTy()) {
684 ResR = Builder.CreateFNeg(V: Op.first, Name: "neg.r");
685 ResI = Builder.CreateFNeg(V: Op.second, Name: "neg.i");
686 } else {
687 ResR = Builder.CreateNeg(V: Op.first, Name: "neg.r");
688 ResI = Builder.CreateNeg(V: Op.second, Name: "neg.i");
689 }
690 return ComplexPairTy(ResR, ResI);
691}
692
693ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
694 TestAndClearIgnoreReal();
695 TestAndClearIgnoreImag();
696 // ~(a+ib) = a + i*-b
697 ComplexPairTy Op = Visit(E: E->getSubExpr());
698 llvm::Value *ResI;
699 if (Op.second->getType()->isFloatingPointTy())
700 ResI = Builder.CreateFNeg(V: Op.second, Name: "conj.i");
701 else
702 ResI = Builder.CreateNeg(V: Op.second, Name: "conj.i");
703
704 return ComplexPairTy(Op.first, ResI);
705}
706
707ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
708 llvm::Value *ResR, *ResI;
709
710 if (Op.LHS.first->getType()->isFloatingPointTy()) {
711 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
712 ResR = Builder.CreateFAdd(L: Op.LHS.first, R: Op.RHS.first, Name: "add.r");
713 if (Op.LHS.second && Op.RHS.second)
714 ResI = Builder.CreateFAdd(L: Op.LHS.second, R: Op.RHS.second, Name: "add.i");
715 else
716 ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
717 assert(ResI && "Only one operand may be real!");
718 } else {
719 ResR = Builder.CreateAdd(LHS: Op.LHS.first, RHS: Op.RHS.first, Name: "add.r");
720 assert(Op.LHS.second && Op.RHS.second &&
721 "Both operands of integer complex operators must be complex!");
722 ResI = Builder.CreateAdd(LHS: Op.LHS.second, RHS: Op.RHS.second, Name: "add.i");
723 }
724 return ComplexPairTy(ResR, ResI);
725}
726
727ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
728 llvm::Value *ResR, *ResI;
729 if (Op.LHS.first->getType()->isFloatingPointTy()) {
730 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
731 ResR = Builder.CreateFSub(L: Op.LHS.first, R: Op.RHS.first, Name: "sub.r");
732 if (Op.LHS.second && Op.RHS.second)
733 ResI = Builder.CreateFSub(L: Op.LHS.second, R: Op.RHS.second, Name: "sub.i");
734 else
735 ResI = Op.LHS.second ? Op.LHS.second
736 : Builder.CreateFNeg(V: Op.RHS.second, Name: "sub.i");
737 assert(ResI && "Only one operand may be real!");
738 } else {
739 ResR = Builder.CreateSub(LHS: Op.LHS.first, RHS: Op.RHS.first, Name: "sub.r");
740 assert(Op.LHS.second && Op.RHS.second &&
741 "Both operands of integer complex operators must be complex!");
742 ResI = Builder.CreateSub(LHS: Op.LHS.second, RHS: Op.RHS.second, Name: "sub.i");
743 }
744 return ComplexPairTy(ResR, ResI);
745}
746
747/// Emit a libcall for a binary operation on complex types.
748ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
749 const BinOpInfo &Op) {
750 CallArgList Args;
751 Args.add(rvalue: RValue::get(V: Op.LHS.first),
752 type: Op.Ty->castAs<ComplexType>()->getElementType());
753 Args.add(rvalue: RValue::get(V: Op.LHS.second),
754 type: Op.Ty->castAs<ComplexType>()->getElementType());
755 Args.add(rvalue: RValue::get(V: Op.RHS.first),
756 type: Op.Ty->castAs<ComplexType>()->getElementType());
757 Args.add(rvalue: RValue::get(V: Op.RHS.second),
758 type: Op.Ty->castAs<ComplexType>()->getElementType());
759
760 // We *must* use the full CG function call building logic here because the
761 // complex type has special ABI handling. We also should not forget about
762 // special calling convention which may be used for compiler builtins.
763
764 // We create a function qualified type to state that this call does not have
765 // any exceptions.
766 FunctionProtoType::ExtProtoInfo EPI;
767 EPI = EPI.withExceptionSpec(
768 ESI: FunctionProtoType::ExceptionSpecInfo(EST_BasicNoexcept));
769 SmallVector<QualType, 4> ArgsQTys(
770 4, Op.Ty->castAs<ComplexType>()->getElementType());
771 QualType FQTy = CGF.getContext().getFunctionType(ResultTy: Op.Ty, Args: ArgsQTys, EPI);
772 const CGFunctionInfo &FuncInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall(
773 Args, Ty: cast<FunctionType>(Val: FQTy.getTypePtr()), ChainCall: false);
774
775 llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(Info: FuncInfo);
776 llvm::FunctionCallee Func = CGF.CGM.CreateRuntimeFunction(
777 Ty: FTy, Name: LibCallName, ExtraAttrs: llvm::AttributeList(), Local: true);
778 CGCallee Callee = CGCallee::forDirect(functionPtr: Func, abstractInfo: FQTy->getAs<FunctionProtoType>());
779
780 llvm::CallBase *Call;
781 RValue Res = CGF.EmitCall(CallInfo: FuncInfo, Callee, ReturnValue: ReturnValueSlot(), Args, callOrInvoke: &Call);
782 Call->setCallingConv(CGF.CGM.getRuntimeCC());
783 return Res.getComplexVal();
784}
785
786/// Lookup the libcall name for a given floating point type complex
787/// multiply.
788static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
789 switch (Ty->getTypeID()) {
790 default:
791 llvm_unreachable("Unsupported floating point type!");
792 case llvm::Type::HalfTyID:
793 return "__mulhc3";
794 case llvm::Type::FloatTyID:
795 return "__mulsc3";
796 case llvm::Type::DoubleTyID:
797 return "__muldc3";
798 case llvm::Type::PPC_FP128TyID:
799 return "__multc3";
800 case llvm::Type::X86_FP80TyID:
801 return "__mulxc3";
802 case llvm::Type::FP128TyID:
803 return "__multc3";
804 }
805}
806
807// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
808// typed values.
809ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
810 using llvm::Value;
811 Value *ResR, *ResI;
812 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
813
814 if (Op.LHS.first->getType()->isFloatingPointTy()) {
815 // The general formulation is:
816 // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
817 //
818 // But we can fold away components which would be zero due to a real
819 // operand according to C11 Annex G.5.1p2.
820 // FIXME: C11 also provides for imaginary types which would allow folding
821 // still more of this within the type system.
822
823 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
824 if (Op.LHS.second && Op.RHS.second) {
825 // If both operands are complex, emit the core math directly, and then
826 // test for NaNs. If we find NaNs in the result, we delegate to a libcall
827 // to carefully re-compute the correct infinity representation if
828 // possible. The expectation is that the presence of NaNs here is
829 // *extremely* rare, and so the cost of the libcall is almost irrelevant.
830 // This is good, because the libcall re-computes the core multiplication
831 // exactly the same as we do here and re-tests for NaNs in order to be
832 // a generic complex*complex libcall.
833
834 // First compute the four products.
835 Value *AC = Builder.CreateFMul(L: Op.LHS.first, R: Op.RHS.first, Name: "mul_ac");
836 Value *BD = Builder.CreateFMul(L: Op.LHS.second, R: Op.RHS.second, Name: "mul_bd");
837 Value *AD = Builder.CreateFMul(L: Op.LHS.first, R: Op.RHS.second, Name: "mul_ad");
838 Value *BC = Builder.CreateFMul(L: Op.LHS.second, R: Op.RHS.first, Name: "mul_bc");
839
840 // The real part is the difference of the first two, the imaginary part is
841 // the sum of the second.
842 ResR = Builder.CreateFSub(L: AC, R: BD, Name: "mul_r");
843 ResI = Builder.CreateFAdd(L: AD, R: BC, Name: "mul_i");
844
845 if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Basic ||
846 Op.FPFeatures.getComplexRange() == LangOptions::CX_Improved ||
847 Op.FPFeatures.getComplexRange() == LangOptions::CX_Promoted)
848 return ComplexPairTy(ResR, ResI);
849
850 // Emit the test for the real part becoming NaN and create a branch to
851 // handle it. We test for NaN by comparing the number to itself.
852 Value *IsRNaN = Builder.CreateFCmpUNO(LHS: ResR, RHS: ResR, Name: "isnan_cmp");
853 llvm::BasicBlock *ContBB = CGF.createBasicBlock(name: "complex_mul_cont");
854 llvm::BasicBlock *INaNBB = CGF.createBasicBlock(name: "complex_mul_imag_nan");
855 llvm::Instruction *Branch = Builder.CreateCondBr(Cond: IsRNaN, True: INaNBB, False: ContBB);
856 llvm::BasicBlock *OrigBB = Branch->getParent();
857
858 // Give hint that we very much don't expect to see NaNs.
859 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
860 llvm::MDNode *BrWeight = MDHelper.createBranchWeights(TrueWeight: 1, FalseWeight: (1U << 20) - 1);
861 Branch->setMetadata(KindID: llvm::LLVMContext::MD_prof, Node: BrWeight);
862
863 // Now test the imaginary part and create its branch.
864 CGF.EmitBlock(BB: INaNBB);
865 Value *IsINaN = Builder.CreateFCmpUNO(LHS: ResI, RHS: ResI, Name: "isnan_cmp");
866 llvm::BasicBlock *LibCallBB = CGF.createBasicBlock(name: "complex_mul_libcall");
867 Branch = Builder.CreateCondBr(Cond: IsINaN, True: LibCallBB, False: ContBB);
868 Branch->setMetadata(KindID: llvm::LLVMContext::MD_prof, Node: BrWeight);
869
870 // Now emit the libcall on this slowest of the slow paths.
871 CGF.EmitBlock(BB: LibCallBB);
872 Value *LibCallR, *LibCallI;
873 std::tie(args&: LibCallR, args&: LibCallI) = EmitComplexBinOpLibCall(
874 LibCallName: getComplexMultiplyLibCallName(Ty: Op.LHS.first->getType()), Op);
875 Builder.CreateBr(Dest: ContBB);
876
877 // Finally continue execution by phi-ing together the different
878 // computation paths.
879 CGF.EmitBlock(BB: ContBB);
880 llvm::PHINode *RealPHI = Builder.CreatePHI(Ty: ResR->getType(), NumReservedValues: 3, Name: "real_mul_phi");
881 RealPHI->addIncoming(V: ResR, BB: OrigBB);
882 RealPHI->addIncoming(V: ResR, BB: INaNBB);
883 RealPHI->addIncoming(V: LibCallR, BB: LibCallBB);
884 llvm::PHINode *ImagPHI = Builder.CreatePHI(Ty: ResI->getType(), NumReservedValues: 3, Name: "imag_mul_phi");
885 ImagPHI->addIncoming(V: ResI, BB: OrigBB);
886 ImagPHI->addIncoming(V: ResI, BB: INaNBB);
887 ImagPHI->addIncoming(V: LibCallI, BB: LibCallBB);
888 return ComplexPairTy(RealPHI, ImagPHI);
889 }
890 assert((Op.LHS.second || Op.RHS.second) &&
891 "At least one operand must be complex!");
892
893 // If either of the operands is a real rather than a complex, the
894 // imaginary component is ignored when computing the real component of the
895 // result.
896 ResR = Builder.CreateFMul(L: Op.LHS.first, R: Op.RHS.first, Name: "mul.rl");
897
898 ResI = Op.LHS.second
899 ? Builder.CreateFMul(L: Op.LHS.second, R: Op.RHS.first, Name: "mul.il")
900 : Builder.CreateFMul(L: Op.LHS.first, R: Op.RHS.second, Name: "mul.ir");
901 } else {
902 assert(Op.LHS.second && Op.RHS.second &&
903 "Both operands of integer complex operators must be complex!");
904 Value *ResRl = Builder.CreateMul(LHS: Op.LHS.first, RHS: Op.RHS.first, Name: "mul.rl");
905 Value *ResRr = Builder.CreateMul(LHS: Op.LHS.second, RHS: Op.RHS.second, Name: "mul.rr");
906 ResR = Builder.CreateSub(LHS: ResRl, RHS: ResRr, Name: "mul.r");
907
908 Value *ResIl = Builder.CreateMul(LHS: Op.LHS.second, RHS: Op.RHS.first, Name: "mul.il");
909 Value *ResIr = Builder.CreateMul(LHS: Op.LHS.first, RHS: Op.RHS.second, Name: "mul.ir");
910 ResI = Builder.CreateAdd(LHS: ResIl, RHS: ResIr, Name: "mul.i");
911 }
912 return ComplexPairTy(ResR, ResI);
913}
914
915ComplexPairTy ComplexExprEmitter::EmitAlgebraicDiv(llvm::Value *LHSr,
916 llvm::Value *LHSi,
917 llvm::Value *RHSr,
918 llvm::Value *RHSi) {
919 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
920 llvm::Value *DSTr, *DSTi;
921
922 llvm::Value *AC = Builder.CreateFMul(L: LHSr, R: RHSr); // a*c
923 llvm::Value *BD = Builder.CreateFMul(L: LHSi, R: RHSi); // b*d
924 llvm::Value *ACpBD = Builder.CreateFAdd(L: AC, R: BD); // ac+bd
925
926 llvm::Value *CC = Builder.CreateFMul(L: RHSr, R: RHSr); // c*c
927 llvm::Value *DD = Builder.CreateFMul(L: RHSi, R: RHSi); // d*d
928 llvm::Value *CCpDD = Builder.CreateFAdd(L: CC, R: DD); // cc+dd
929
930 llvm::Value *BC = Builder.CreateFMul(L: LHSi, R: RHSr); // b*c
931 llvm::Value *AD = Builder.CreateFMul(L: LHSr, R: RHSi); // a*d
932 llvm::Value *BCmAD = Builder.CreateFSub(L: BC, R: AD); // bc-ad
933
934 DSTr = Builder.CreateFDiv(L: ACpBD, R: CCpDD);
935 DSTi = Builder.CreateFDiv(L: BCmAD, R: CCpDD);
936 return ComplexPairTy(DSTr, DSTi);
937}
938
939// EmitFAbs - Emit a call to @llvm.fabs.
940static llvm::Value *EmitllvmFAbs(CodeGenFunction &CGF, llvm::Value *Value) {
941 llvm::Function *Func =
942 CGF.CGM.getIntrinsic(llvm::Intrinsic::fabs, Value->getType());
943 llvm::Value *Call = CGF.Builder.CreateCall(Callee: Func, Args: Value);
944 return Call;
945}
946
947// EmitRangeReductionDiv - Implements Smith's algorithm for complex division.
948// SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8 (1962).
949ComplexPairTy ComplexExprEmitter::EmitRangeReductionDiv(llvm::Value *LHSr,
950 llvm::Value *LHSi,
951 llvm::Value *RHSr,
952 llvm::Value *RHSi) {
953 // FIXME: This could eventually be replaced by an LLVM intrinsic to
954 // avoid this long IR sequence.
955
956 // (a + ib) / (c + id) = (e + if)
957 llvm::Value *FAbsRHSr = EmitllvmFAbs(CGF, Value: RHSr); // |c|
958 llvm::Value *FAbsRHSi = EmitllvmFAbs(CGF, Value: RHSi); // |d|
959 // |c| >= |d|
960 llvm::Value *IsR = Builder.CreateFCmpUGT(LHS: FAbsRHSr, RHS: FAbsRHSi, Name: "abs_cmp");
961
962 llvm::BasicBlock *TrueBB =
963 CGF.createBasicBlock(name: "abs_rhsr_greater_or_equal_abs_rhsi");
964 llvm::BasicBlock *FalseBB =
965 CGF.createBasicBlock(name: "abs_rhsr_less_than_abs_rhsi");
966 llvm::BasicBlock *ContBB = CGF.createBasicBlock(name: "complex_div");
967 Builder.CreateCondBr(Cond: IsR, True: TrueBB, False: FalseBB);
968
969 CGF.EmitBlock(BB: TrueBB);
970 // abs(c) >= abs(d)
971 // r = d/c
972 // tmp = c + rd
973 // e = (a + br)/tmp
974 // f = (b - ar)/tmp
975 llvm::Value *DdC = Builder.CreateFDiv(L: RHSi, R: RHSr); // r=d/c
976
977 llvm::Value *RD = Builder.CreateFMul(L: DdC, R: RHSi); // rd
978 llvm::Value *CpRD = Builder.CreateFAdd(L: RHSr, R: RD); // tmp=c+rd
979
980 llvm::Value *T3 = Builder.CreateFMul(L: LHSi, R: DdC); // br
981 llvm::Value *T4 = Builder.CreateFAdd(L: LHSr, R: T3); // a+br
982 llvm::Value *DSTTr = Builder.CreateFDiv(L: T4, R: CpRD); // (a+br)/tmp
983
984 llvm::Value *T5 = Builder.CreateFMul(L: LHSr, R: DdC); // ar
985 llvm::Value *T6 = Builder.CreateFSub(L: LHSi, R: T5); // b-ar
986 llvm::Value *DSTTi = Builder.CreateFDiv(L: T6, R: CpRD); // (b-ar)/tmp
987 Builder.CreateBr(Dest: ContBB);
988
989 CGF.EmitBlock(BB: FalseBB);
990 // abs(c) < abs(d)
991 // r = c/d
992 // tmp = d + rc
993 // e = (ar + b)/tmp
994 // f = (br - a)/tmp
995 llvm::Value *CdD = Builder.CreateFDiv(L: RHSr, R: RHSi); // r=c/d
996
997 llvm::Value *RC = Builder.CreateFMul(L: CdD, R: RHSr); // rc
998 llvm::Value *DpRC = Builder.CreateFAdd(L: RHSi, R: RC); // tmp=d+rc
999
1000 llvm::Value *T7 = Builder.CreateFMul(L: LHSr, R: CdD); // ar
1001 llvm::Value *T8 = Builder.CreateFAdd(L: T7, R: LHSi); // ar+b
1002 llvm::Value *DSTFr = Builder.CreateFDiv(L: T8, R: DpRC); // (ar+b)/tmp
1003
1004 llvm::Value *T9 = Builder.CreateFMul(L: LHSi, R: CdD); // br
1005 llvm::Value *T10 = Builder.CreateFSub(L: T9, R: LHSr); // br-a
1006 llvm::Value *DSTFi = Builder.CreateFDiv(L: T10, R: DpRC); // (br-a)/tmp
1007 Builder.CreateBr(Dest: ContBB);
1008
1009 // Phi together the computation paths.
1010 CGF.EmitBlock(BB: ContBB);
1011 llvm::PHINode *VALr = Builder.CreatePHI(Ty: DSTTr->getType(), NumReservedValues: 2);
1012 VALr->addIncoming(V: DSTTr, BB: TrueBB);
1013 VALr->addIncoming(V: DSTFr, BB: FalseBB);
1014 llvm::PHINode *VALi = Builder.CreatePHI(Ty: DSTTi->getType(), NumReservedValues: 2);
1015 VALi->addIncoming(V: DSTTi, BB: TrueBB);
1016 VALi->addIncoming(V: DSTFi, BB: FalseBB);
1017 return ComplexPairTy(VALr, VALi);
1018}
1019
1020// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
1021// typed values.
1022ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
1023 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
1024 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
1025 llvm::Value *DSTr, *DSTi;
1026 if (LHSr->getType()->isFloatingPointTy()) {
1027 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
1028 if (!RHSi) {
1029 assert(LHSi && "Can have at most one non-complex operand!");
1030
1031 DSTr = Builder.CreateFDiv(L: LHSr, R: RHSr);
1032 DSTi = Builder.CreateFDiv(L: LHSi, R: RHSr);
1033 return ComplexPairTy(DSTr, DSTi);
1034 }
1035 llvm::Value *OrigLHSi = LHSi;
1036 if (!LHSi)
1037 LHSi = llvm::Constant::getNullValue(Ty: RHSi->getType());
1038 if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Improved ||
1039 (Op.FPFeatures.getComplexRange() == LangOptions::CX_Promoted &&
1040 !FPHasBeenPromoted))
1041 return EmitRangeReductionDiv(LHSr, LHSi, RHSr, RHSi);
1042 else if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Basic ||
1043 Op.FPFeatures.getComplexRange() == LangOptions::CX_Promoted)
1044 return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi);
1045 // '-ffast-math' is used in the command line but followed by an
1046 // '-fno-cx-limited-range' or '-fcomplex-arithmetic=full'.
1047 else if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Full) {
1048 LHSi = OrigLHSi;
1049 // If we have a complex operand on the RHS and FastMath is not allowed, we
1050 // delegate to a libcall to handle all of the complexities and minimize
1051 // underflow/overflow cases. When FastMath is allowed we construct the
1052 // divide inline using the same algorithm as for integer operands.
1053 //
1054 // FIXME: We would be able to avoid the libcall in many places if we
1055 // supported imaginary types in addition to complex types.
1056 BinOpInfo LibCallOp = Op;
1057 // If LHS was a real, supply a null imaginary part.
1058 if (!LHSi)
1059 LibCallOp.LHS.second = llvm::Constant::getNullValue(Ty: LHSr->getType());
1060
1061 switch (LHSr->getType()->getTypeID()) {
1062 default:
1063 llvm_unreachable("Unsupported floating point type!");
1064 case llvm::Type::HalfTyID:
1065 return EmitComplexBinOpLibCall(LibCallName: "__divhc3", Op: LibCallOp);
1066 case llvm::Type::FloatTyID:
1067 return EmitComplexBinOpLibCall(LibCallName: "__divsc3", Op: LibCallOp);
1068 case llvm::Type::DoubleTyID:
1069 return EmitComplexBinOpLibCall(LibCallName: "__divdc3", Op: LibCallOp);
1070 case llvm::Type::PPC_FP128TyID:
1071 return EmitComplexBinOpLibCall(LibCallName: "__divtc3", Op: LibCallOp);
1072 case llvm::Type::X86_FP80TyID:
1073 return EmitComplexBinOpLibCall(LibCallName: "__divxc3", Op: LibCallOp);
1074 case llvm::Type::FP128TyID:
1075 return EmitComplexBinOpLibCall(LibCallName: "__divtc3", Op: LibCallOp);
1076 }
1077 } else {
1078 return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi);
1079 }
1080 } else {
1081 assert(Op.LHS.second && Op.RHS.second &&
1082 "Both operands of integer complex operators must be complex!");
1083 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
1084 llvm::Value *Tmp1 = Builder.CreateMul(LHS: LHSr, RHS: RHSr); // a*c
1085 llvm::Value *Tmp2 = Builder.CreateMul(LHS: LHSi, RHS: RHSi); // b*d
1086 llvm::Value *Tmp3 = Builder.CreateAdd(LHS: Tmp1, RHS: Tmp2); // ac+bd
1087
1088 llvm::Value *Tmp4 = Builder.CreateMul(LHS: RHSr, RHS: RHSr); // c*c
1089 llvm::Value *Tmp5 = Builder.CreateMul(LHS: RHSi, RHS: RHSi); // d*d
1090 llvm::Value *Tmp6 = Builder.CreateAdd(LHS: Tmp4, RHS: Tmp5); // cc+dd
1091
1092 llvm::Value *Tmp7 = Builder.CreateMul(LHS: LHSi, RHS: RHSr); // b*c
1093 llvm::Value *Tmp8 = Builder.CreateMul(LHS: LHSr, RHS: RHSi); // a*d
1094 llvm::Value *Tmp9 = Builder.CreateSub(LHS: Tmp7, RHS: Tmp8); // bc-ad
1095
1096 if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
1097 DSTr = Builder.CreateUDiv(LHS: Tmp3, RHS: Tmp6);
1098 DSTi = Builder.CreateUDiv(LHS: Tmp9, RHS: Tmp6);
1099 } else {
1100 DSTr = Builder.CreateSDiv(LHS: Tmp3, RHS: Tmp6);
1101 DSTi = Builder.CreateSDiv(LHS: Tmp9, RHS: Tmp6);
1102 }
1103 }
1104
1105 return ComplexPairTy(DSTr, DSTi);
1106}
1107
1108ComplexPairTy CodeGenFunction::EmitUnPromotedValue(ComplexPairTy result,
1109 QualType UnPromotionType) {
1110 llvm::Type *ComplexElementTy =
1111 ConvertType(T: UnPromotionType->castAs<ComplexType>()->getElementType());
1112 if (result.first)
1113 result.first =
1114 Builder.CreateFPTrunc(V: result.first, DestTy: ComplexElementTy, Name: "unpromotion");
1115 if (result.second)
1116 result.second =
1117 Builder.CreateFPTrunc(V: result.second, DestTy: ComplexElementTy, Name: "unpromotion");
1118 return result;
1119}
1120
1121ComplexPairTy CodeGenFunction::EmitPromotedValue(ComplexPairTy result,
1122 QualType PromotionType) {
1123 llvm::Type *ComplexElementTy =
1124 ConvertType(T: PromotionType->castAs<ComplexType>()->getElementType());
1125 if (result.first)
1126 result.first = Builder.CreateFPExt(V: result.first, DestTy: ComplexElementTy, Name: "ext");
1127 if (result.second)
1128 result.second = Builder.CreateFPExt(V: result.second, DestTy: ComplexElementTy, Name: "ext");
1129
1130 return result;
1131}
1132
1133ComplexPairTy ComplexExprEmitter::EmitPromoted(const Expr *E,
1134 QualType PromotionType) {
1135 E = E->IgnoreParens();
1136 if (auto BO = dyn_cast<BinaryOperator>(Val: E)) {
1137 switch (BO->getOpcode()) {
1138#define HANDLE_BINOP(OP) \
1139 case BO_##OP: \
1140 return EmitBin##OP(EmitBinOps(BO, PromotionType));
1141 HANDLE_BINOP(Add)
1142 HANDLE_BINOP(Sub)
1143 HANDLE_BINOP(Mul)
1144 HANDLE_BINOP(Div)
1145#undef HANDLE_BINOP
1146 default:
1147 break;
1148 }
1149 } else if (auto UO = dyn_cast<UnaryOperator>(Val: E)) {
1150 switch (UO->getOpcode()) {
1151 case UO_Minus:
1152 return VisitMinus(E: UO, PromotionType);
1153 case UO_Plus:
1154 return VisitPlus(E: UO, PromotionType);
1155 default:
1156 break;
1157 }
1158 }
1159 auto result = Visit(E: const_cast<Expr *>(E));
1160 if (!PromotionType.isNull())
1161 return CGF.EmitPromotedValue(result, PromotionType);
1162 else
1163 return result;
1164}
1165
1166ComplexPairTy CodeGenFunction::EmitPromotedComplexExpr(const Expr *E,
1167 QualType DstTy) {
1168 return ComplexExprEmitter(*this).EmitPromoted(E, PromotionType: DstTy);
1169}
1170
1171ComplexPairTy
1172ComplexExprEmitter::EmitPromotedComplexOperand(const Expr *E,
1173 QualType OverallPromotionType) {
1174 if (E->getType()->isAnyComplexType()) {
1175 if (!OverallPromotionType.isNull())
1176 return CGF.EmitPromotedComplexExpr(E, DstTy: OverallPromotionType);
1177 else
1178 return Visit(E: const_cast<Expr *>(E));
1179 } else {
1180 if (!OverallPromotionType.isNull()) {
1181 QualType ComplexElementTy =
1182 OverallPromotionType->castAs<ComplexType>()->getElementType();
1183 return ComplexPairTy(CGF.EmitPromotedScalarExpr(E, PromotionType: ComplexElementTy),
1184 nullptr);
1185 } else {
1186 return ComplexPairTy(CGF.EmitScalarExpr(E), nullptr);
1187 }
1188 }
1189}
1190
1191ComplexExprEmitter::BinOpInfo
1192ComplexExprEmitter::EmitBinOps(const BinaryOperator *E,
1193 QualType PromotionType) {
1194 TestAndClearIgnoreReal();
1195 TestAndClearIgnoreImag();
1196 BinOpInfo Ops;
1197
1198 Ops.LHS = EmitPromotedComplexOperand(E: E->getLHS(), OverallPromotionType: PromotionType);
1199 Ops.RHS = EmitPromotedComplexOperand(E: E->getRHS(), OverallPromotionType: PromotionType);
1200 if (!PromotionType.isNull())
1201 Ops.Ty = PromotionType;
1202 else
1203 Ops.Ty = E->getType();
1204 Ops.FPFeatures = E->getFPFeaturesInEffect(LO: CGF.getLangOpts());
1205 return Ops;
1206}
1207
1208
1209LValue ComplexExprEmitter::
1210EmitCompoundAssignLValue(const CompoundAssignOperator *E,
1211 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
1212 RValue &Val) {
1213 TestAndClearIgnoreReal();
1214 TestAndClearIgnoreImag();
1215 QualType LHSTy = E->getLHS()->getType();
1216 if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
1217 LHSTy = AT->getValueType();
1218
1219 BinOpInfo OpInfo;
1220 OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts());
1221 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, OpInfo.FPFeatures);
1222
1223 // Load the RHS and LHS operands.
1224 // __block variables need to have the rhs evaluated first, plus this should
1225 // improve codegen a little.
1226 QualType PromotionTypeCR;
1227 PromotionTypeCR = getPromotionType(Ty: E->getComputationResultType());
1228 if (PromotionTypeCR.isNull())
1229 PromotionTypeCR = E->getComputationResultType();
1230 OpInfo.Ty = PromotionTypeCR;
1231 QualType ComplexElementTy =
1232 OpInfo.Ty->castAs<ComplexType>()->getElementType();
1233 QualType PromotionTypeRHS = getPromotionType(Ty: E->getRHS()->getType());
1234
1235 // The RHS should have been converted to the computation type.
1236 if (E->getRHS()->getType()->isRealFloatingType()) {
1237 if (!PromotionTypeRHS.isNull())
1238 OpInfo.RHS = ComplexPairTy(
1239 CGF.EmitPromotedScalarExpr(E: E->getRHS(), PromotionType: PromotionTypeRHS), nullptr);
1240 else {
1241 assert(CGF.getContext().hasSameUnqualifiedType(ComplexElementTy,
1242 E->getRHS()->getType()));
1243
1244 OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E: E->getRHS()), nullptr);
1245 }
1246 } else {
1247 if (!PromotionTypeRHS.isNull()) {
1248 OpInfo.RHS = ComplexPairTy(
1249 CGF.EmitPromotedComplexExpr(E: E->getRHS(), DstTy: PromotionTypeRHS));
1250 } else {
1251 assert(CGF.getContext().hasSameUnqualifiedType(OpInfo.Ty,
1252 E->getRHS()->getType()));
1253 OpInfo.RHS = Visit(E: E->getRHS());
1254 }
1255 }
1256
1257 LValue LHS = CGF.EmitLValue(E: E->getLHS());
1258
1259 // Load from the l-value and convert it.
1260 SourceLocation Loc = E->getExprLoc();
1261 QualType PromotionTypeLHS = getPromotionType(Ty: E->getComputationLHSType());
1262 if (LHSTy->isAnyComplexType()) {
1263 ComplexPairTy LHSVal = EmitLoadOfLValue(lvalue: LHS, loc: Loc);
1264 if (!PromotionTypeLHS.isNull())
1265 OpInfo.LHS =
1266 EmitComplexToComplexCast(Val: LHSVal, SrcType: LHSTy, DestType: PromotionTypeLHS, Loc);
1267 else
1268 OpInfo.LHS = EmitComplexToComplexCast(Val: LHSVal, SrcType: LHSTy, DestType: OpInfo.Ty, Loc);
1269 } else {
1270 llvm::Value *LHSVal = CGF.EmitLoadOfScalar(lvalue: LHS, Loc);
1271 // For floating point real operands we can directly pass the scalar form
1272 // to the binary operator emission and potentially get more efficient code.
1273 if (LHSTy->isRealFloatingType()) {
1274 QualType PromotedComplexElementTy;
1275 if (!PromotionTypeLHS.isNull()) {
1276 PromotedComplexElementTy =
1277 cast<ComplexType>(Val&: PromotionTypeLHS)->getElementType();
1278 if (!CGF.getContext().hasSameUnqualifiedType(T1: PromotedComplexElementTy,
1279 T2: PromotionTypeLHS))
1280 LHSVal = CGF.EmitScalarConversion(Src: LHSVal, SrcTy: LHSTy,
1281 DstTy: PromotedComplexElementTy, Loc);
1282 } else {
1283 if (!CGF.getContext().hasSameUnqualifiedType(T1: ComplexElementTy, T2: LHSTy))
1284 LHSVal =
1285 CGF.EmitScalarConversion(Src: LHSVal, SrcTy: LHSTy, DstTy: ComplexElementTy, Loc);
1286 }
1287 OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
1288 } else {
1289 OpInfo.LHS = EmitScalarToComplexCast(Val: LHSVal, SrcType: LHSTy, DestType: OpInfo.Ty, Loc);
1290 }
1291 }
1292
1293 // Expand the binary operator.
1294 ComplexPairTy Result = (this->*Func)(OpInfo);
1295
1296 // Truncate the result and store it into the LHS lvalue.
1297 if (LHSTy->isAnyComplexType()) {
1298 ComplexPairTy ResVal =
1299 EmitComplexToComplexCast(Val: Result, SrcType: OpInfo.Ty, DestType: LHSTy, Loc);
1300 EmitStoreOfComplex(Val: ResVal, lvalue: LHS, /*isInit*/ false);
1301 Val = RValue::getComplex(C: ResVal);
1302 } else {
1303 llvm::Value *ResVal =
1304 CGF.EmitComplexToScalarConversion(Src: Result, SrcTy: OpInfo.Ty, DstTy: LHSTy, Loc);
1305 CGF.EmitStoreOfScalar(value: ResVal, lvalue: LHS, /*isInit*/ false);
1306 Val = RValue::get(V: ResVal);
1307 }
1308
1309 return LHS;
1310}
1311
1312// Compound assignments.
1313ComplexPairTy ComplexExprEmitter::
1314EmitCompoundAssign(const CompoundAssignOperator *E,
1315 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
1316 RValue Val;
1317 LValue LV = EmitCompoundAssignLValue(E, Func, Val);
1318
1319 // The result of an assignment in C is the assigned r-value.
1320 if (!CGF.getLangOpts().CPlusPlus)
1321 return Val.getComplexVal();
1322
1323 // If the lvalue is non-volatile, return the computed value of the assignment.
1324 if (!LV.isVolatileQualified())
1325 return Val.getComplexVal();
1326
1327 return EmitLoadOfLValue(LV, E->getExprLoc());
1328}
1329
1330LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
1331 ComplexPairTy &Val) {
1332 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
1333 E->getRHS()->getType()) &&
1334 "Invalid assignment");
1335 TestAndClearIgnoreReal();
1336 TestAndClearIgnoreImag();
1337
1338 // Emit the RHS. __block variables need the RHS evaluated first.
1339 Val = Visit(E: E->getRHS());
1340
1341 // Compute the address to store into.
1342 LValue LHS = CGF.EmitLValue(E: E->getLHS());
1343
1344 // Store the result value into the LHS lvalue.
1345 EmitStoreOfComplex(Val, lvalue: LHS, /*isInit*/ false);
1346
1347 return LHS;
1348}
1349
1350ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1351 ComplexPairTy Val;
1352 LValue LV = EmitBinAssignLValue(E, Val);
1353
1354 // The result of an assignment in C is the assigned r-value.
1355 if (!CGF.getLangOpts().CPlusPlus)
1356 return Val;
1357
1358 // If the lvalue is non-volatile, return the computed value of the assignment.
1359 if (!LV.isVolatileQualified())
1360 return Val;
1361
1362 return EmitLoadOfLValue(lvalue: LV, loc: E->getExprLoc());
1363}
1364
1365ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
1366 CGF.EmitIgnoredExpr(E: E->getLHS());
1367 return Visit(E: E->getRHS());
1368}
1369
1370ComplexPairTy ComplexExprEmitter::
1371VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
1372 TestAndClearIgnoreReal();
1373 TestAndClearIgnoreImag();
1374 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock(name: "cond.true");
1375 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock(name: "cond.false");
1376 llvm::BasicBlock *ContBlock = CGF.createBasicBlock(name: "cond.end");
1377
1378 // Bind the common expression if necessary.
1379 CodeGenFunction::OpaqueValueMapping binding(CGF, E);
1380
1381
1382 CodeGenFunction::ConditionalEvaluation eval(CGF);
1383 CGF.EmitBranchOnBoolExpr(Cond: E->getCond(), TrueBlock: LHSBlock, FalseBlock: RHSBlock,
1384 TrueCount: CGF.getProfileCount(E));
1385
1386 eval.begin(CGF);
1387 CGF.EmitBlock(BB: LHSBlock);
1388 if (llvm::EnableSingleByteCoverage)
1389 CGF.incrementProfileCounter(E->getTrueExpr());
1390 else
1391 CGF.incrementProfileCounter(E);
1392
1393 ComplexPairTy LHS = Visit(E: E->getTrueExpr());
1394 LHSBlock = Builder.GetInsertBlock();
1395 CGF.EmitBranch(Block: ContBlock);
1396 eval.end(CGF);
1397
1398 eval.begin(CGF);
1399 CGF.EmitBlock(BB: RHSBlock);
1400 if (llvm::EnableSingleByteCoverage)
1401 CGF.incrementProfileCounter(E->getFalseExpr());
1402 ComplexPairTy RHS = Visit(E: E->getFalseExpr());
1403 RHSBlock = Builder.GetInsertBlock();
1404 CGF.EmitBlock(BB: ContBlock);
1405 if (llvm::EnableSingleByteCoverage)
1406 CGF.incrementProfileCounter(E);
1407 eval.end(CGF);
1408
1409 // Create a PHI node for the real part.
1410 llvm::PHINode *RealPN = Builder.CreatePHI(Ty: LHS.first->getType(), NumReservedValues: 2, Name: "cond.r");
1411 RealPN->addIncoming(V: LHS.first, BB: LHSBlock);
1412 RealPN->addIncoming(V: RHS.first, BB: RHSBlock);
1413
1414 // Create a PHI node for the imaginary part.
1415 llvm::PHINode *ImagPN = Builder.CreatePHI(Ty: LHS.first->getType(), NumReservedValues: 2, Name: "cond.i");
1416 ImagPN->addIncoming(V: LHS.second, BB: LHSBlock);
1417 ImagPN->addIncoming(V: RHS.second, BB: RHSBlock);
1418
1419 return ComplexPairTy(RealPN, ImagPN);
1420}
1421
1422ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1423 return Visit(E: E->getChosenSubExpr());
1424}
1425
1426ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
1427 bool Ignore = TestAndClearIgnoreReal();
1428 (void)Ignore;
1429 assert (Ignore == false && "init list ignored");
1430 Ignore = TestAndClearIgnoreImag();
1431 (void)Ignore;
1432 assert (Ignore == false && "init list ignored");
1433
1434 if (E->getNumInits() == 2) {
1435 llvm::Value *Real = CGF.EmitScalarExpr(E: E->getInit(Init: 0));
1436 llvm::Value *Imag = CGF.EmitScalarExpr(E: E->getInit(Init: 1));
1437 return ComplexPairTy(Real, Imag);
1438 } else if (E->getNumInits() == 1) {
1439 return Visit(E: E->getInit(Init: 0));
1440 }
1441
1442 // Empty init list initializes to null
1443 assert(E->getNumInits() == 0 && "Unexpected number of inits");
1444 QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
1445 llvm::Type* LTy = CGF.ConvertType(T: Ty);
1446 llvm::Value* zeroConstant = llvm::Constant::getNullValue(Ty: LTy);
1447 return ComplexPairTy(zeroConstant, zeroConstant);
1448}
1449
1450ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
1451 Address ArgValue = Address::invalid();
1452 Address ArgPtr = CGF.EmitVAArg(VE: E, VAListAddr&: ArgValue);
1453
1454 if (!ArgPtr.isValid()) {
1455 CGF.ErrorUnsupported(E, "complex va_arg expression");
1456 llvm::Type *EltTy =
1457 CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
1458 llvm::Value *U = llvm::UndefValue::get(T: EltTy);
1459 return ComplexPairTy(U, U);
1460 }
1461
1462 return EmitLoadOfLValue(CGF.MakeAddrLValue(ArgPtr, E->getType()),
1463 E->getExprLoc());
1464}
1465
1466//===----------------------------------------------------------------------===//
1467// Entry Point into this File
1468//===----------------------------------------------------------------------===//
1469
1470/// EmitComplexExpr - Emit the computation of the specified expression of
1471/// complex type, ignoring the result.
1472ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
1473 bool IgnoreImag) {
1474 assert(E && getComplexType(E->getType()) &&
1475 "Invalid complex expression to emit");
1476
1477 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
1478 .Visit(E: const_cast<Expr *>(E));
1479}
1480
1481void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest,
1482 bool isInit) {
1483 assert(E && getComplexType(E->getType()) &&
1484 "Invalid complex expression to emit");
1485 ComplexExprEmitter Emitter(*this);
1486 ComplexPairTy Val = Emitter.Visit(E: const_cast<Expr*>(E));
1487 Emitter.EmitStoreOfComplex(Val, lvalue: dest, isInit);
1488}
1489
1490/// EmitStoreOfComplex - Store a complex number into the specified l-value.
1491void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest,
1492 bool isInit) {
1493 ComplexExprEmitter(*this).EmitStoreOfComplex(Val: V, lvalue: dest, isInit);
1494}
1495
1496/// EmitLoadOfComplex - Load a complex number from the specified address.
1497ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src,
1498 SourceLocation loc) {
1499 return ComplexExprEmitter(*this).EmitLoadOfLValue(lvalue: src, loc);
1500}
1501
1502LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
1503 assert(E->getOpcode() == BO_Assign);
1504 ComplexPairTy Val; // ignored
1505 LValue LVal = ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1506 if (getLangOpts().OpenMP)
1507 CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF&: *this,
1508 LHS: E->getLHS());
1509 return LVal;
1510}
1511
1512typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1513 const ComplexExprEmitter::BinOpInfo &);
1514
1515static CompoundFunc getComplexOp(BinaryOperatorKind Op) {
1516 switch (Op) {
1517 case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1518 case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1519 case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1520 case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
1521 default:
1522 llvm_unreachable("unexpected complex compound assignment");
1523 }
1524}
1525
1526LValue CodeGenFunction::
1527EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
1528 CompoundFunc Op = getComplexOp(E->getOpcode());
1529 RValue Val;
1530 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Func: Op, Val);
1531}
1532
1533LValue CodeGenFunction::
1534EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
1535 llvm::Value *&Result) {
1536 CompoundFunc Op = getComplexOp(E->getOpcode());
1537 RValue Val;
1538 LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Func: Op, Val);
1539 Result = Val.getScalarVal();
1540 return Ret;
1541}
1542

source code of clang/lib/CodeGen/CGExprComplex.cpp