clang 20.0.0git
CGExprComplex.cpp
Go to the documentation of this file.
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"
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.
39 type = type.getCanonicalType();
40 if (const ComplexType *comp = dyn_cast<ComplexType>(type)) {
41 return comp;
42 } else {
43 return cast<ComplexType>(cast<AtomicType>(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(CGF.EmitLValue(E), 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
101 ApplyDebugLocation DL(CGF, E);
103 }
104
105 ComplexPairTy VisitStmt(Stmt *S) {
106 S->dump(llvm::errs(), 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(E))
112 return ComplexPairTy(Result->getAggregateElement(0U),
113 Result->getAggregateElement(1U));
114 return Visit(E->getSubExpr());
115 }
116 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
117 ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
118 return Visit(GE->getResultExpr());
119 }
120 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
122 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
123 return Visit(PE->getReplacement());
124 }
125 ComplexPairTy VisitCoawaitExpr(CoawaitExpr *S) {
126 return CGF.EmitCoawaitExpr(*S).getComplexVal();
127 }
128 ComplexPairTy VisitCoyieldExpr(CoyieldExpr *S) {
129 return CGF.EmitCoyieldExpr(*S).getComplexVal();
130 }
131 ComplexPairTy VisitUnaryCoawait(const UnaryOperator *E) {
132 return Visit(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(Constant.getReferenceLValue(CGF, E),
140 E->getExprLoc());
141
142 llvm::Constant *pair = Constant.getValue();
143 return ComplexPairTy(pair->getAggregateElement(0U),
144 pair->getAggregateElement(1U));
145 }
146
147 // l-values.
148 ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
149 if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(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(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(CGF.getOrCreateOpaqueLValueMapping(E),
171 E->getExprLoc());
173 }
174
175 ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
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(E->getCastKind(), E->getSubExpr(), E->getType());
188 }
189 ComplexPairTy VisitCastExpr(CastExpr *E) {
190 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
191 CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
192 if (E->changesVolatileQualification())
193 return EmitLoadOfLValue(E);
194 return EmitCast(E->getCastKind(), E->getSubExpr(), 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->getSubExpr());
203 return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
204 }
205 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
206 return VisitPrePostIncDec(E, false, false);
207 }
208 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
209 return VisitPrePostIncDec(E, true, false);
210 }
211 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
212 return VisitPrePostIncDec(E, false, true);
213 }
214 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
215 return VisitPrePostIncDec(E, true, 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->getSubExpr());
229 }
230 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
231 CodeGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE);
232 return Visit(DAE->getExpr());
233 }
234 ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
235 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF, DIE);
236 return Visit(DIE->getExpr());
237 }
238 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
239 CodeGenFunction::RunCleanupsScope Scope(CGF);
240 ComplexPairTy Vals = Visit(E->getSubExpr());
241 // Defend against dominance problems caused by jumps out of expression
242 // evaluation through the shared cleanup block.
243 Scope.ForceCleanup({&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(CGF.ConvertType(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(CGF.ConvertType(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>(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(ElementType);
311 const llvm::fltSemantics &HigherElementTypeSemantics =
312 CGF.getContext().getFloatTypeSemantics(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(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(FPOptionsOverride Features, QualType Ty,
332 bool IsDivOpCode = false) {
333 if (auto *CT = Ty->getAs<ComplexType>()) {
334 QualType ElementType = CT->getElementType();
335 bool IsFloatingType = ElementType->isFloatingType();
336 bool IsComplexRangePromoted = CGF.getLangOpts().getComplexRange() ==
337 LangOptions::ComplexRangeKind::CX_Promoted;
338 bool HasNoComplexRangeOverride = !Features.hasComplexRangeOverride();
339 bool HasMatchingComplexRange = Features.hasComplexRangeOverride() &&
340 Features.getComplexRangeOverride() ==
341 CGF.getLangOpts().getComplexRange();
342
343 if (IsDivOpCode && IsFloatingType && IsComplexRangePromoted &&
344 (HasNoComplexRangeOverride || HasMatchingComplexRange))
345 return HigherPrecisionTypeForComplexArithmetic(ElementType,
346 IsDivOpCode);
347 if (ElementType.UseExcessPrecision(CGF.getContext()))
348 return CGF.getContext().getComplexType(CGF.getContext().FloatTy);
349 }
350 if (Ty.UseExcessPrecision(CGF.getContext()))
351 return CGF.getContext().FloatTy;
352 return QualType();
353 }
354
355#define HANDLEBINOP(OP) \
356 ComplexPairTy VisitBin##OP(const BinaryOperator *E) { \
357 QualType promotionTy = getPromotionType( \
358 E->getStoredFPFeaturesOrDefault(), E->getType(), \
359 (E->getOpcode() == BinaryOperatorKind::BO_Div) ? true : false); \
360 ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy)); \
361 if (!promotionTy.isNull()) \
362 result = CGF.EmitUnPromotedValue(result, E->getType()); \
363 return result; \
364 }
365
366 HANDLEBINOP(Mul)
367 HANDLEBINOP(Div)
368 HANDLEBINOP(Add)
369 HANDLEBINOP(Sub)
370#undef HANDLEBINOP
371
372 ComplexPairTy VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E) {
373 return Visit(E->getSemanticForm());
374 }
375
376 // Compound assignments.
377 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
378 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
379 }
380 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
381 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
382 }
383 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
384 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
385 }
386 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
387 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
388 }
389
390 // GCC rejects rem/and/or/xor for integer complex.
391 // Logical and/or always return int, never complex.
392
393 // No comparisons produce a complex result.
394
395 LValue EmitBinAssignLValue(const BinaryOperator *E,
396 ComplexPairTy &Val);
397 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
398 ComplexPairTy VisitBinComma (const BinaryOperator *E);
399
400
402 VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
403 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
404
405 ComplexPairTy VisitInitListExpr(InitListExpr *E);
406
407 ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
408 return EmitLoadOfLValue(E);
409 }
410
411 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
412
413 ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
414 return CGF.EmitAtomicExpr(E).getComplexVal();
415 }
416
417 ComplexPairTy VisitPackIndexingExpr(PackIndexingExpr *E) {
418 return Visit(E->getSelectedExpr());
419 }
420};
421} // end anonymous namespace.
422
423//===----------------------------------------------------------------------===//
424// Utilities
425//===----------------------------------------------------------------------===//
426
427Address CodeGenFunction::emitAddrOfRealComponent(Address addr,
429 return Builder.CreateStructGEP(addr, 0, addr.getName() + ".realp");
430}
431
434 return Builder.CreateStructGEP(addr, 1, addr.getName() + ".imagp");
435}
436
437/// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
438/// load the real and imaginary pieces, returning them as Real/Imag.
439ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
440 SourceLocation loc) {
441 assert(lvalue.isSimple() && "non-simple complex l-value?");
442 if (lvalue.getType()->isAtomicType())
443 return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
444
445 Address SrcPtr = lvalue.getAddress();
446 bool isVolatile = lvalue.isVolatileQualified();
447
448 llvm::Value *Real = nullptr, *Imag = nullptr;
449
450 if (!IgnoreReal || isVolatile) {
451 Address RealP = CGF.emitAddrOfRealComponent(SrcPtr, lvalue.getType());
452 Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr.getName() + ".real");
453 }
454
455 if (!IgnoreImag || isVolatile) {
456 Address ImagP = CGF.emitAddrOfImagComponent(SrcPtr, lvalue.getType());
457 Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr.getName() + ".imag");
458 }
459
460 return ComplexPairTy(Real, Imag);
461}
462
463/// EmitStoreOfComplex - Store the specified real/imag parts into the
464/// specified value pointer.
465void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
466 bool isInit) {
467 if (lvalue.getType()->isAtomicType() ||
468 (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue)))
469 return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
470
471 Address Ptr = lvalue.getAddress();
472 Address RealPtr = CGF.emitAddrOfRealComponent(Ptr, lvalue.getType());
473 Address ImagPtr = CGF.emitAddrOfImagComponent(Ptr, lvalue.getType());
474
475 Builder.CreateStore(Val.first, RealPtr, lvalue.isVolatileQualified());
476 Builder.CreateStore(Val.second, ImagPtr, lvalue.isVolatileQualified());
477}
478
479
480
481//===----------------------------------------------------------------------===//
482// Visitor Methods
483//===----------------------------------------------------------------------===//
484
485ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
486 CGF.ErrorUnsupported(E, "complex expression");
487 llvm::Type *EltTy =
489 llvm::Value *U = llvm::UndefValue::get(EltTy);
490 return ComplexPairTy(U, U);
491}
492
493ComplexPairTy ComplexExprEmitter::
494VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
495 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
496 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
497}
498
499
500ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
501 if (E->getCallReturnType(CGF.getContext())->isReferenceType())
502 return EmitLoadOfLValue(E);
503
504 return CGF.EmitCallExpr(E).getComplexVal();
505}
506
507ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
508 CodeGenFunction::StmtExprEvaluation eval(CGF);
509 Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
510 assert(RetAlloca.isValid() && "Expected complex return value");
511 return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
512 E->getExprLoc());
513}
514
515/// Emit a cast from complex value Val to DestType.
516ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
517 QualType SrcType,
518 QualType DestType,
520 // Get the src/dest element type.
521 SrcType = SrcType->castAs<ComplexType>()->getElementType();
522 DestType = DestType->castAs<ComplexType>()->getElementType();
523
524 // C99 6.3.1.6: When a value of complex type is converted to another
525 // complex type, both the real and imaginary parts follow the conversion
526 // rules for the corresponding real types.
527 if (Val.first)
528 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType, Loc);
529 if (Val.second)
530 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType, Loc);
531 return Val;
532}
533
534ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
535 QualType SrcType,
536 QualType DestType,
538 // Convert the input element to the element type of the complex.
539 DestType = DestType->castAs<ComplexType>()->getElementType();
540 Val = CGF.EmitScalarConversion(Val, SrcType, DestType, Loc);
541
542 // Return (realval, 0).
543 return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
544}
545
546ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
547 QualType DestTy) {
548 switch (CK) {
549 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
550
551 // Atomic to non-atomic casts may be more than a no-op for some platforms and
552 // for some types.
553 case CK_AtomicToNonAtomic:
554 case CK_NonAtomicToAtomic:
555 case CK_NoOp:
556 case CK_LValueToRValue:
557 case CK_UserDefinedConversion:
558 return Visit(Op);
559
560 case CK_LValueBitCast: {
561 LValue origLV = CGF.EmitLValue(Op);
562 Address V = origLV.getAddress().withElementType(CGF.ConvertType(DestTy));
563 return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), Op->getExprLoc());
564 }
565
566 case CK_LValueToRValueBitCast: {
567 LValue SourceLVal = CGF.EmitLValue(Op);
568 Address Addr =
569 SourceLVal.getAddress().withElementType(CGF.ConvertTypeForMem(DestTy));
570 LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy);
572 return EmitLoadOfLValue(DestLV, Op->getExprLoc());
573 }
574
575 case CK_BitCast:
576 case CK_BaseToDerived:
577 case CK_DerivedToBase:
578 case CK_UncheckedDerivedToBase:
579 case CK_Dynamic:
580 case CK_ToUnion:
581 case CK_ArrayToPointerDecay:
582 case CK_FunctionToPointerDecay:
583 case CK_NullToPointer:
584 case CK_NullToMemberPointer:
585 case CK_BaseToDerivedMemberPointer:
586 case CK_DerivedToBaseMemberPointer:
587 case CK_MemberPointerToBoolean:
588 case CK_ReinterpretMemberPointer:
589 case CK_ConstructorConversion:
590 case CK_IntegralToPointer:
591 case CK_PointerToIntegral:
592 case CK_PointerToBoolean:
593 case CK_ToVoid:
594 case CK_VectorSplat:
595 case CK_IntegralCast:
596 case CK_BooleanToSignedIntegral:
597 case CK_IntegralToBoolean:
598 case CK_IntegralToFloating:
599 case CK_FloatingToIntegral:
600 case CK_FloatingToBoolean:
601 case CK_FloatingCast:
602 case CK_CPointerToObjCPointerCast:
603 case CK_BlockPointerToObjCPointerCast:
604 case CK_AnyPointerToBlockPointerCast:
605 case CK_ObjCObjectLValueCast:
606 case CK_FloatingComplexToReal:
607 case CK_FloatingComplexToBoolean:
608 case CK_IntegralComplexToReal:
609 case CK_IntegralComplexToBoolean:
610 case CK_ARCProduceObject:
611 case CK_ARCConsumeObject:
612 case CK_ARCReclaimReturnedObject:
613 case CK_ARCExtendBlockObject:
614 case CK_CopyAndAutoreleaseBlockObject:
615 case CK_BuiltinFnToFnPtr:
616 case CK_ZeroToOCLOpaqueType:
617 case CK_AddressSpaceConversion:
618 case CK_IntToOCLSampler:
619 case CK_FloatingToFixedPoint:
620 case CK_FixedPointToFloating:
621 case CK_FixedPointCast:
622 case CK_FixedPointToBoolean:
623 case CK_FixedPointToIntegral:
624 case CK_IntegralToFixedPoint:
625 case CK_MatrixCast:
626 case CK_HLSLVectorTruncation:
627 case CK_HLSLArrayRValue:
628 llvm_unreachable("invalid cast kind for complex value");
629
630 case CK_FloatingRealToComplex:
631 case CK_IntegralRealToComplex: {
632 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op);
633 return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op), Op->getType(),
634 DestTy, Op->getExprLoc());
635 }
636
637 case CK_FloatingComplexCast:
638 case CK_FloatingComplexToIntegralComplex:
639 case CK_IntegralComplexCast:
640 case CK_IntegralComplexToFloatingComplex: {
641 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op);
642 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy,
643 Op->getExprLoc());
644 }
645 }
646
647 llvm_unreachable("unknown cast resulting in complex value");
648}
649
650ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E,
651 QualType PromotionType) {
652 QualType promotionTy =
653 PromotionType.isNull()
654 ? getPromotionType(E->getStoredFPFeaturesOrDefault(),
655 E->getSubExpr()->getType())
656 : PromotionType;
657 ComplexPairTy result = VisitPlus(E, promotionTy);
658 if (!promotionTy.isNull())
659 return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType());
660 return result;
661}
662
663ComplexPairTy ComplexExprEmitter::VisitPlus(const UnaryOperator *E,
664 QualType PromotionType) {
665 TestAndClearIgnoreReal();
666 TestAndClearIgnoreImag();
667 if (!PromotionType.isNull())
668 return CGF.EmitPromotedComplexExpr(E->getSubExpr(), PromotionType);
669 return Visit(E->getSubExpr());
670}
671
672ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
673 QualType PromotionType) {
674 QualType promotionTy =
675 PromotionType.isNull()
676 ? getPromotionType(E->getStoredFPFeaturesOrDefault(),
677 E->getSubExpr()->getType())
678 : PromotionType;
679 ComplexPairTy result = VisitMinus(E, promotionTy);
680 if (!promotionTy.isNull())
681 return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType());
682 return result;
683}
684ComplexPairTy ComplexExprEmitter::VisitMinus(const UnaryOperator *E,
685 QualType PromotionType) {
686 TestAndClearIgnoreReal();
687 TestAndClearIgnoreImag();
688 ComplexPairTy Op;
689 if (!PromotionType.isNull())
690 Op = CGF.EmitPromotedComplexExpr(E->getSubExpr(), PromotionType);
691 else
692 Op = Visit(E->getSubExpr());
693
694 llvm::Value *ResR, *ResI;
695 if (Op.first->getType()->isFloatingPointTy()) {
696 ResR = Builder.CreateFNeg(Op.first, "neg.r");
697 ResI = Builder.CreateFNeg(Op.second, "neg.i");
698 } else {
699 ResR = Builder.CreateNeg(Op.first, "neg.r");
700 ResI = Builder.CreateNeg(Op.second, "neg.i");
701 }
702 return ComplexPairTy(ResR, ResI);
703}
704
705ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
706 TestAndClearIgnoreReal();
707 TestAndClearIgnoreImag();
708 // ~(a+ib) = a + i*-b
709 ComplexPairTy Op = Visit(E->getSubExpr());
710 llvm::Value *ResI;
711 if (Op.second->getType()->isFloatingPointTy())
712 ResI = Builder.CreateFNeg(Op.second, "conj.i");
713 else
714 ResI = Builder.CreateNeg(Op.second, "conj.i");
715
716 return ComplexPairTy(Op.first, ResI);
717}
718
719ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
720 llvm::Value *ResR, *ResI;
721
722 if (Op.LHS.first->getType()->isFloatingPointTy()) {
723 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
724 ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
725 if (Op.LHS.second && Op.RHS.second)
726 ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
727 else
728 ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
729 assert(ResI && "Only one operand may be real!");
730 } else {
731 ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
732 assert(Op.LHS.second && Op.RHS.second &&
733 "Both operands of integer complex operators must be complex!");
734 ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
735 }
736 return ComplexPairTy(ResR, ResI);
737}
738
739ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
740 llvm::Value *ResR, *ResI;
741 if (Op.LHS.first->getType()->isFloatingPointTy()) {
742 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
743 ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
744 if (Op.LHS.second && Op.RHS.second)
745 ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
746 else
747 ResI = Op.LHS.second ? Op.LHS.second
748 : Builder.CreateFNeg(Op.RHS.second, "sub.i");
749 assert(ResI && "Only one operand may be real!");
750 } else {
751 ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
752 assert(Op.LHS.second && Op.RHS.second &&
753 "Both operands of integer complex operators must be complex!");
754 ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
755 }
756 return ComplexPairTy(ResR, ResI);
757}
758
759/// Emit a libcall for a binary operation on complex types.
760ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
761 const BinOpInfo &Op) {
762 CallArgList Args;
763 Args.add(RValue::get(Op.LHS.first),
764 Op.Ty->castAs<ComplexType>()->getElementType());
765 Args.add(RValue::get(Op.LHS.second),
766 Op.Ty->castAs<ComplexType>()->getElementType());
767 Args.add(RValue::get(Op.RHS.first),
768 Op.Ty->castAs<ComplexType>()->getElementType());
769 Args.add(RValue::get(Op.RHS.second),
770 Op.Ty->castAs<ComplexType>()->getElementType());
771
772 // We *must* use the full CG function call building logic here because the
773 // complex type has special ABI handling. We also should not forget about
774 // special calling convention which may be used for compiler builtins.
775
776 // We create a function qualified type to state that this call does not have
777 // any exceptions.
779 EPI = EPI.withExceptionSpec(
782 4, Op.Ty->castAs<ComplexType>()->getElementType());
783 QualType FQTy = CGF.getContext().getFunctionType(Op.Ty, ArgsQTys, EPI);
784 const CGFunctionInfo &FuncInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall(
785 Args, cast<FunctionType>(FQTy.getTypePtr()), false);
786
787 llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
788 llvm::FunctionCallee Func = CGF.CGM.CreateRuntimeFunction(
789 FTy, LibCallName, llvm::AttributeList(), true);
791
792 llvm::CallBase *Call;
793 RValue Res = CGF.EmitCall(FuncInfo, Callee, ReturnValueSlot(), Args, &Call);
794 Call->setCallingConv(CGF.CGM.getRuntimeCC());
795 return Res.getComplexVal();
796}
797
798/// Lookup the libcall name for a given floating point type complex
799/// multiply.
800static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
801 switch (Ty->getTypeID()) {
802 default:
803 llvm_unreachable("Unsupported floating point type!");
804 case llvm::Type::HalfTyID:
805 return "__mulhc3";
806 case llvm::Type::FloatTyID:
807 return "__mulsc3";
808 case llvm::Type::DoubleTyID:
809 return "__muldc3";
810 case llvm::Type::PPC_FP128TyID:
811 return "__multc3";
812 case llvm::Type::X86_FP80TyID:
813 return "__mulxc3";
814 case llvm::Type::FP128TyID:
815 return "__multc3";
816 }
817}
818
819// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
820// typed values.
821ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
822 using llvm::Value;
823 Value *ResR, *ResI;
824 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
825
826 if (Op.LHS.first->getType()->isFloatingPointTy()) {
827 // The general formulation is:
828 // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
829 //
830 // But we can fold away components which would be zero due to a real
831 // operand according to C11 Annex G.5.1p2.
832
833 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
834 if (Op.LHS.second && Op.RHS.second) {
835 // If both operands are complex, emit the core math directly, and then
836 // test for NaNs. If we find NaNs in the result, we delegate to a libcall
837 // to carefully re-compute the correct infinity representation if
838 // possible. The expectation is that the presence of NaNs here is
839 // *extremely* rare, and so the cost of the libcall is almost irrelevant.
840 // This is good, because the libcall re-computes the core multiplication
841 // exactly the same as we do here and re-tests for NaNs in order to be
842 // a generic complex*complex libcall.
843
844 // First compute the four products.
845 Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
846 Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
847 Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
848 Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
849
850 // The real part is the difference of the first two, the imaginary part is
851 // the sum of the second.
852 ResR = Builder.CreateFSub(AC, BD, "mul_r");
853 ResI = Builder.CreateFAdd(AD, BC, "mul_i");
854
855 if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Basic ||
856 Op.FPFeatures.getComplexRange() == LangOptions::CX_Improved ||
857 Op.FPFeatures.getComplexRange() == LangOptions::CX_Promoted)
858 return ComplexPairTy(ResR, ResI);
859
860 // Emit the test for the real part becoming NaN and create a branch to
861 // handle it. We test for NaN by comparing the number to itself.
862 Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
863 llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
864 llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
865 llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
866 llvm::BasicBlock *OrigBB = Branch->getParent();
867
868 // Give hint that we very much don't expect to see NaNs.
869 llvm::MDNode *BrWeight = MDHelper.createUnlikelyBranchWeights();
870 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
871
872 // Now test the imaginary part and create its branch.
873 CGF.EmitBlock(INaNBB);
874 Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
875 llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
876 Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
877 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
878
879 // Now emit the libcall on this slowest of the slow paths.
880 CGF.EmitBlock(LibCallBB);
881 Value *LibCallR, *LibCallI;
882 std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
883 getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
884 Builder.CreateBr(ContBB);
885
886 // Finally continue execution by phi-ing together the different
887 // computation paths.
888 CGF.EmitBlock(ContBB);
889 llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
890 RealPHI->addIncoming(ResR, OrigBB);
891 RealPHI->addIncoming(ResR, INaNBB);
892 RealPHI->addIncoming(LibCallR, LibCallBB);
893 llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
894 ImagPHI->addIncoming(ResI, OrigBB);
895 ImagPHI->addIncoming(ResI, INaNBB);
896 ImagPHI->addIncoming(LibCallI, LibCallBB);
897 return ComplexPairTy(RealPHI, ImagPHI);
898 }
899 assert((Op.LHS.second || Op.RHS.second) &&
900 "At least one operand must be complex!");
901
902 // If either of the operands is a real rather than a complex, the
903 // imaginary component is ignored when computing the real component of the
904 // result.
905 ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
906
907 ResI = Op.LHS.second
908 ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
909 : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
910 } else {
911 assert(Op.LHS.second && Op.RHS.second &&
912 "Both operands of integer complex operators must be complex!");
913 Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
914 Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
915 ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
916
917 Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
918 Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
919 ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
920 }
921 return ComplexPairTy(ResR, ResI);
922}
923
924ComplexPairTy ComplexExprEmitter::EmitAlgebraicDiv(llvm::Value *LHSr,
925 llvm::Value *LHSi,
926 llvm::Value *RHSr,
927 llvm::Value *RHSi) {
928 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
929 llvm::Value *DSTr, *DSTi;
930
931 llvm::Value *AC = Builder.CreateFMul(LHSr, RHSr); // a*c
932 llvm::Value *BD = Builder.CreateFMul(LHSi, RHSi); // b*d
933 llvm::Value *ACpBD = Builder.CreateFAdd(AC, BD); // ac+bd
934
935 llvm::Value *CC = Builder.CreateFMul(RHSr, RHSr); // c*c
936 llvm::Value *DD = Builder.CreateFMul(RHSi, RHSi); // d*d
937 llvm::Value *CCpDD = Builder.CreateFAdd(CC, DD); // cc+dd
938
939 llvm::Value *BC = Builder.CreateFMul(LHSi, RHSr); // b*c
940 llvm::Value *AD = Builder.CreateFMul(LHSr, RHSi); // a*d
941 llvm::Value *BCmAD = Builder.CreateFSub(BC, AD); // bc-ad
942
943 DSTr = Builder.CreateFDiv(ACpBD, CCpDD);
944 DSTi = Builder.CreateFDiv(BCmAD, CCpDD);
945 return ComplexPairTy(DSTr, DSTi);
946}
947
948// EmitFAbs - Emit a call to @llvm.fabs.
949static llvm::Value *EmitllvmFAbs(CodeGenFunction &CGF, llvm::Value *Value) {
950 llvm::Function *Func =
951 CGF.CGM.getIntrinsic(llvm::Intrinsic::fabs, Value->getType());
952 llvm::Value *Call = CGF.Builder.CreateCall(Func, Value);
953 return Call;
954}
955
956// EmitRangeReductionDiv - Implements Smith's algorithm for complex division.
957// SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8 (1962).
958ComplexPairTy ComplexExprEmitter::EmitRangeReductionDiv(llvm::Value *LHSr,
959 llvm::Value *LHSi,
960 llvm::Value *RHSr,
961 llvm::Value *RHSi) {
962 // FIXME: This could eventually be replaced by an LLVM intrinsic to
963 // avoid this long IR sequence.
964
965 // (a + ib) / (c + id) = (e + if)
966 llvm::Value *FAbsRHSr = EmitllvmFAbs(CGF, RHSr); // |c|
967 llvm::Value *FAbsRHSi = EmitllvmFAbs(CGF, RHSi); // |d|
968 // |c| >= |d|
969 llvm::Value *IsR = Builder.CreateFCmpUGT(FAbsRHSr, FAbsRHSi, "abs_cmp");
970
971 llvm::BasicBlock *TrueBB =
972 CGF.createBasicBlock("abs_rhsr_greater_or_equal_abs_rhsi");
973 llvm::BasicBlock *FalseBB =
974 CGF.createBasicBlock("abs_rhsr_less_than_abs_rhsi");
975 llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_div");
976 Builder.CreateCondBr(IsR, TrueBB, FalseBB);
977
978 CGF.EmitBlock(TrueBB);
979 // abs(c) >= abs(d)
980 // r = d/c
981 // tmp = c + rd
982 // e = (a + br)/tmp
983 // f = (b - ar)/tmp
984 llvm::Value *DdC = Builder.CreateFDiv(RHSi, RHSr); // r=d/c
985
986 llvm::Value *RD = Builder.CreateFMul(DdC, RHSi); // rd
987 llvm::Value *CpRD = Builder.CreateFAdd(RHSr, RD); // tmp=c+rd
988
989 llvm::Value *T3 = Builder.CreateFMul(LHSi, DdC); // br
990 llvm::Value *T4 = Builder.CreateFAdd(LHSr, T3); // a+br
991 llvm::Value *DSTTr = Builder.CreateFDiv(T4, CpRD); // (a+br)/tmp
992
993 llvm::Value *T5 = Builder.CreateFMul(LHSr, DdC); // ar
994 llvm::Value *T6 = Builder.CreateFSub(LHSi, T5); // b-ar
995 llvm::Value *DSTTi = Builder.CreateFDiv(T6, CpRD); // (b-ar)/tmp
996 Builder.CreateBr(ContBB);
997
998 CGF.EmitBlock(FalseBB);
999 // abs(c) < abs(d)
1000 // r = c/d
1001 // tmp = d + rc
1002 // e = (ar + b)/tmp
1003 // f = (br - a)/tmp
1004 llvm::Value *CdD = Builder.CreateFDiv(RHSr, RHSi); // r=c/d
1005
1006 llvm::Value *RC = Builder.CreateFMul(CdD, RHSr); // rc
1007 llvm::Value *DpRC = Builder.CreateFAdd(RHSi, RC); // tmp=d+rc
1008
1009 llvm::Value *T7 = Builder.CreateFMul(LHSr, CdD); // ar
1010 llvm::Value *T8 = Builder.CreateFAdd(T7, LHSi); // ar+b
1011 llvm::Value *DSTFr = Builder.CreateFDiv(T8, DpRC); // (ar+b)/tmp
1012
1013 llvm::Value *T9 = Builder.CreateFMul(LHSi, CdD); // br
1014 llvm::Value *T10 = Builder.CreateFSub(T9, LHSr); // br-a
1015 llvm::Value *DSTFi = Builder.CreateFDiv(T10, DpRC); // (br-a)/tmp
1016 Builder.CreateBr(ContBB);
1017
1018 // Phi together the computation paths.
1019 CGF.EmitBlock(ContBB);
1020 llvm::PHINode *VALr = Builder.CreatePHI(DSTTr->getType(), 2);
1021 VALr->addIncoming(DSTTr, TrueBB);
1022 VALr->addIncoming(DSTFr, FalseBB);
1023 llvm::PHINode *VALi = Builder.CreatePHI(DSTTi->getType(), 2);
1024 VALi->addIncoming(DSTTi, TrueBB);
1025 VALi->addIncoming(DSTFi, FalseBB);
1026 return ComplexPairTy(VALr, VALi);
1027}
1028
1029// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
1030// typed values.
1031ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
1032 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
1033 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
1034 llvm::Value *DSTr, *DSTi;
1035 if (LHSr->getType()->isFloatingPointTy()) {
1036 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
1037 if (!RHSi) {
1038 assert(LHSi && "Can have at most one non-complex operand!");
1039
1040 DSTr = Builder.CreateFDiv(LHSr, RHSr);
1041 DSTi = Builder.CreateFDiv(LHSi, RHSr);
1042 return ComplexPairTy(DSTr, DSTi);
1043 }
1044 llvm::Value *OrigLHSi = LHSi;
1045 if (!LHSi)
1046 LHSi = llvm::Constant::getNullValue(RHSi->getType());
1047 if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Improved ||
1048 (Op.FPFeatures.getComplexRange() == LangOptions::CX_Promoted &&
1049 !FPHasBeenPromoted))
1050 return EmitRangeReductionDiv(LHSr, LHSi, RHSr, RHSi);
1051 else if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Basic ||
1052 Op.FPFeatures.getComplexRange() == LangOptions::CX_Promoted)
1053 return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi);
1054 // '-ffast-math' is used in the command line but followed by an
1055 // '-fno-cx-limited-range' or '-fcomplex-arithmetic=full'.
1056 else if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Full) {
1057 LHSi = OrigLHSi;
1058 // If we have a complex operand on the RHS and FastMath is not allowed, we
1059 // delegate to a libcall to handle all of the complexities and minimize
1060 // underflow/overflow cases. When FastMath is allowed we construct the
1061 // divide inline using the same algorithm as for integer operands.
1062 BinOpInfo LibCallOp = Op;
1063 // If LHS was a real, supply a null imaginary part.
1064 if (!LHSi)
1065 LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
1066
1067 switch (LHSr->getType()->getTypeID()) {
1068 default:
1069 llvm_unreachable("Unsupported floating point type!");
1070 case llvm::Type::HalfTyID:
1071 return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
1072 case llvm::Type::FloatTyID:
1073 return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
1074 case llvm::Type::DoubleTyID:
1075 return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
1076 case llvm::Type::PPC_FP128TyID:
1077 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
1078 case llvm::Type::X86_FP80TyID:
1079 return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
1080 case llvm::Type::FP128TyID:
1081 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
1082 }
1083 } else {
1084 return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi);
1085 }
1086 } else {
1087 assert(Op.LHS.second && Op.RHS.second &&
1088 "Both operands of integer complex operators must be complex!");
1089 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
1090 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
1091 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
1092 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
1093
1094 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
1095 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
1096 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
1097
1098 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
1099 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
1100 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
1101
1102 if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
1103 DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
1104 DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
1105 } else {
1106 DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
1107 DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
1108 }
1109 }
1110
1111 return ComplexPairTy(DSTr, DSTi);
1112}
1113
1115 QualType UnPromotionType) {
1116 llvm::Type *ComplexElementTy =
1117 ConvertType(UnPromotionType->castAs<ComplexType>()->getElementType());
1118 if (result.first)
1119 result.first =
1120 Builder.CreateFPTrunc(result.first, ComplexElementTy, "unpromotion");
1121 if (result.second)
1122 result.second =
1123 Builder.CreateFPTrunc(result.second, ComplexElementTy, "unpromotion");
1124 return result;
1125}
1126
1128 QualType PromotionType) {
1129 llvm::Type *ComplexElementTy =
1130 ConvertType(PromotionType->castAs<ComplexType>()->getElementType());
1131 if (result.first)
1132 result.first = Builder.CreateFPExt(result.first, ComplexElementTy, "ext");
1133 if (result.second)
1134 result.second = Builder.CreateFPExt(result.second, ComplexElementTy, "ext");
1135
1136 return result;
1137}
1138
1139ComplexPairTy ComplexExprEmitter::EmitPromoted(const Expr *E,
1140 QualType PromotionType) {
1141 E = E->IgnoreParens();
1142 if (auto BO = dyn_cast<BinaryOperator>(E)) {
1143 switch (BO->getOpcode()) {
1144#define HANDLE_BINOP(OP) \
1145 case BO_##OP: \
1146 return EmitBin##OP(EmitBinOps(BO, PromotionType));
1147 HANDLE_BINOP(Add)
1148 HANDLE_BINOP(Sub)
1149 HANDLE_BINOP(Mul)
1150 HANDLE_BINOP(Div)
1151#undef HANDLE_BINOP
1152 default:
1153 break;
1154 }
1155 } else if (auto UO = dyn_cast<UnaryOperator>(E)) {
1156 switch (UO->getOpcode()) {
1157 case UO_Minus:
1158 return VisitMinus(UO, PromotionType);
1159 case UO_Plus:
1160 return VisitPlus(UO, PromotionType);
1161 default:
1162 break;
1163 }
1164 }
1165 auto result = Visit(const_cast<Expr *>(E));
1166 if (!PromotionType.isNull())
1167 return CGF.EmitPromotedValue(result, PromotionType);
1168 else
1169 return result;
1170}
1171
1173 QualType DstTy) {
1174 return ComplexExprEmitter(*this).EmitPromoted(E, DstTy);
1175}
1176
1178ComplexExprEmitter::EmitPromotedComplexOperand(const Expr *E,
1179 QualType OverallPromotionType) {
1180 if (E->getType()->isAnyComplexType()) {
1181 if (!OverallPromotionType.isNull())
1182 return CGF.EmitPromotedComplexExpr(E, OverallPromotionType);
1183 else
1184 return Visit(const_cast<Expr *>(E));
1185 } else {
1186 if (!OverallPromotionType.isNull()) {
1187 QualType ComplexElementTy =
1188 OverallPromotionType->castAs<ComplexType>()->getElementType();
1189 return ComplexPairTy(CGF.EmitPromotedScalarExpr(E, ComplexElementTy),
1190 nullptr);
1191 } else {
1192 return ComplexPairTy(CGF.EmitScalarExpr(E), nullptr);
1193 }
1194 }
1195}
1196
1197ComplexExprEmitter::BinOpInfo
1198ComplexExprEmitter::EmitBinOps(const BinaryOperator *E,
1199 QualType PromotionType) {
1200 TestAndClearIgnoreReal();
1201 TestAndClearIgnoreImag();
1202 BinOpInfo Ops;
1203
1204 Ops.LHS = EmitPromotedComplexOperand(E->getLHS(), PromotionType);
1205 Ops.RHS = EmitPromotedComplexOperand(E->getRHS(), PromotionType);
1206 if (!PromotionType.isNull())
1207 Ops.Ty = PromotionType;
1208 else
1209 Ops.Ty = E->getType();
1210 Ops.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts());
1211 return Ops;
1212}
1213
1214
1215LValue ComplexExprEmitter::
1216EmitCompoundAssignLValue(const CompoundAssignOperator *E,
1217 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
1218 RValue &Val) {
1219 TestAndClearIgnoreReal();
1220 TestAndClearIgnoreImag();
1221 QualType LHSTy = E->getLHS()->getType();
1222 if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
1223 LHSTy = AT->getValueType();
1224
1225 BinOpInfo OpInfo;
1226 OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts());
1227 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, OpInfo.FPFeatures);
1228
1229 // Load the RHS and LHS operands.
1230 // __block variables need to have the rhs evaluated first, plus this should
1231 // improve codegen a little.
1232 QualType PromotionTypeCR;
1233 PromotionTypeCR = getPromotionType(E->getStoredFPFeaturesOrDefault(),
1234 E->getComputationResultType());
1235 if (PromotionTypeCR.isNull())
1236 PromotionTypeCR = E->getComputationResultType();
1237 OpInfo.Ty = PromotionTypeCR;
1238 QualType ComplexElementTy =
1239 OpInfo.Ty->castAs<ComplexType>()->getElementType();
1240 QualType PromotionTypeRHS = getPromotionType(
1241 E->getStoredFPFeaturesOrDefault(), E->getRHS()->getType());
1242
1243 // The RHS should have been converted to the computation type.
1244 if (E->getRHS()->getType()->isRealFloatingType()) {
1245 if (!PromotionTypeRHS.isNull())
1246 OpInfo.RHS = ComplexPairTy(
1247 CGF.EmitPromotedScalarExpr(E->getRHS(), PromotionTypeRHS), nullptr);
1248 else {
1249 assert(CGF.getContext().hasSameUnqualifiedType(ComplexElementTy,
1250 E->getRHS()->getType()));
1251
1252 OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
1253 }
1254 } else {
1255 if (!PromotionTypeRHS.isNull()) {
1256 OpInfo.RHS = ComplexPairTy(
1257 CGF.EmitPromotedComplexExpr(E->getRHS(), PromotionTypeRHS));
1258 } else {
1259 assert(CGF.getContext().hasSameUnqualifiedType(OpInfo.Ty,
1260 E->getRHS()->getType()));
1261 OpInfo.RHS = Visit(E->getRHS());
1262 }
1263 }
1264
1265 LValue LHS = CGF.EmitLValue(E->getLHS());
1266
1267 // Load from the l-value and convert it.
1269 QualType PromotionTypeLHS = getPromotionType(
1270 E->getStoredFPFeaturesOrDefault(), E->getComputationLHSType());
1271 if (LHSTy->isAnyComplexType()) {
1272 ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
1273 if (!PromotionTypeLHS.isNull())
1274 OpInfo.LHS =
1275 EmitComplexToComplexCast(LHSVal, LHSTy, PromotionTypeLHS, Loc);
1276 else
1277 OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
1278 } else {
1279 llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, Loc);
1280 // For floating point real operands we can directly pass the scalar form
1281 // to the binary operator emission and potentially get more efficient code.
1282 if (LHSTy->isRealFloatingType()) {
1283 QualType PromotedComplexElementTy;
1284 if (!PromotionTypeLHS.isNull()) {
1285 PromotedComplexElementTy =
1286 cast<ComplexType>(PromotionTypeLHS)->getElementType();
1287 if (!CGF.getContext().hasSameUnqualifiedType(PromotedComplexElementTy,
1288 PromotionTypeLHS))
1289 LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy,
1290 PromotedComplexElementTy, Loc);
1291 } else {
1292 if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
1293 LHSVal =
1294 CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy, Loc);
1295 }
1296 OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
1297 } else {
1298 OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
1299 }
1300 }
1301
1302 // Expand the binary operator.
1303 ComplexPairTy Result = (this->*Func)(OpInfo);
1304
1305 // Truncate the result and store it into the LHS lvalue.
1306 if (LHSTy->isAnyComplexType()) {
1307 ComplexPairTy ResVal =
1308 EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy, Loc);
1309 EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
1310 Val = RValue::getComplex(ResVal);
1311 } else {
1312 llvm::Value *ResVal =
1313 CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy, Loc);
1314 CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
1315 Val = RValue::get(ResVal);
1316 }
1317
1318 return LHS;
1319}
1320
1321// Compound assignments.
1322ComplexPairTy ComplexExprEmitter::
1323EmitCompoundAssign(const CompoundAssignOperator *E,
1324 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
1325 RValue Val;
1326 LValue LV = EmitCompoundAssignLValue(E, Func, Val);
1327
1328 // The result of an assignment in C is the assigned r-value.
1329 if (!CGF.getLangOpts().CPlusPlus)
1330 return Val.getComplexVal();
1331
1332 // If the lvalue is non-volatile, return the computed value of the assignment.
1333 if (!LV.isVolatileQualified())
1334 return Val.getComplexVal();
1335
1336 return EmitLoadOfLValue(LV, E->getExprLoc());
1337}
1338
1339LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
1340 ComplexPairTy &Val) {
1341 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
1342 E->getRHS()->getType()) &&
1343 "Invalid assignment");
1344 TestAndClearIgnoreReal();
1345 TestAndClearIgnoreImag();
1346
1347 // Emit the RHS. __block variables need the RHS evaluated first.
1348 Val = Visit(E->getRHS());
1349
1350 // Compute the address to store into.
1351 LValue LHS = CGF.EmitLValue(E->getLHS());
1352
1353 // Store the result value into the LHS lvalue.
1354 EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
1355
1356 return LHS;
1357}
1358
1359ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1360 ComplexPairTy Val;
1361 LValue LV = EmitBinAssignLValue(E, Val);
1362
1363 // The result of an assignment in C is the assigned r-value.
1364 if (!CGF.getLangOpts().CPlusPlus)
1365 return Val;
1366
1367 // If the lvalue is non-volatile, return the computed value of the assignment.
1368 if (!LV.isVolatileQualified())
1369 return Val;
1370
1371 return EmitLoadOfLValue(LV, E->getExprLoc());
1372}
1373
1374ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
1375 CGF.EmitIgnoredExpr(E->getLHS());
1376 return Visit(E->getRHS());
1377}
1378
1379ComplexPairTy ComplexExprEmitter::
1380VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
1381 TestAndClearIgnoreReal();
1382 TestAndClearIgnoreImag();
1383 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1384 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
1385 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
1386
1387 // Bind the common expression if necessary.
1388 CodeGenFunction::OpaqueValueMapping binding(CGF, E);
1389
1390
1391 CodeGenFunction::ConditionalEvaluation eval(CGF);
1392 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
1393 CGF.getProfileCount(E));
1394
1395 eval.begin(CGF);
1396 CGF.EmitBlock(LHSBlock);
1398 CGF.incrementProfileCounter(E->getTrueExpr());
1399 else
1401
1402 ComplexPairTy LHS = Visit(E->getTrueExpr());
1403 LHSBlock = Builder.GetInsertBlock();
1404 CGF.EmitBranch(ContBlock);
1405 eval.end(CGF);
1406
1407 eval.begin(CGF);
1408 CGF.EmitBlock(RHSBlock);
1410 CGF.incrementProfileCounter(E->getFalseExpr());
1411 ComplexPairTy RHS = Visit(E->getFalseExpr());
1412 RHSBlock = Builder.GetInsertBlock();
1413 CGF.EmitBlock(ContBlock);
1416 eval.end(CGF);
1417
1418 // Create a PHI node for the real part.
1419 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
1420 RealPN->addIncoming(LHS.first, LHSBlock);
1421 RealPN->addIncoming(RHS.first, RHSBlock);
1422
1423 // Create a PHI node for the imaginary part.
1424 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
1425 ImagPN->addIncoming(LHS.second, LHSBlock);
1426 ImagPN->addIncoming(RHS.second, RHSBlock);
1427
1428 return ComplexPairTy(RealPN, ImagPN);
1429}
1430
1431ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1432 return Visit(E->getChosenSubExpr());
1433}
1434
1435ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
1436 bool Ignore = TestAndClearIgnoreReal();
1437 (void)Ignore;
1438 assert (Ignore == false && "init list ignored");
1439 Ignore = TestAndClearIgnoreImag();
1440 (void)Ignore;
1441 assert (Ignore == false && "init list ignored");
1442
1443 if (E->getNumInits() == 2) {
1444 llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
1445 llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
1446 return ComplexPairTy(Real, Imag);
1447 } else if (E->getNumInits() == 1) {
1448 return Visit(E->getInit(0));
1449 }
1450
1451 // Empty init list initializes to null
1452 assert(E->getNumInits() == 0 && "Unexpected number of inits");
1453 QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
1454 llvm::Type* LTy = CGF.ConvertType(Ty);
1455 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
1456 return ComplexPairTy(zeroConstant, zeroConstant);
1457}
1458
1459ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
1460 Address ArgValue = Address::invalid();
1461 RValue RV = CGF.EmitVAArg(E, ArgValue);
1462
1463 if (!ArgValue.isValid()) {
1464 CGF.ErrorUnsupported(E, "complex va_arg expression");
1465 llvm::Type *EltTy =
1467 llvm::Value *U = llvm::UndefValue::get(EltTy);
1468 return ComplexPairTy(U, U);
1469 }
1470
1471 return RV.getComplexVal();
1472}
1473
1474//===----------------------------------------------------------------------===//
1475// Entry Point into this File
1476//===----------------------------------------------------------------------===//
1477
1478/// EmitComplexExpr - Emit the computation of the specified expression of
1479/// complex type, ignoring the result.
1481 bool IgnoreImag) {
1482 assert(E && getComplexType(E->getType()) &&
1483 "Invalid complex expression to emit");
1484
1485 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
1486 .Visit(const_cast<Expr *>(E));
1487}
1488
1490 bool isInit) {
1491 assert(E && getComplexType(E->getType()) &&
1492 "Invalid complex expression to emit");
1493 ComplexExprEmitter Emitter(*this);
1494 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
1495 Emitter.EmitStoreOfComplex(Val, dest, isInit);
1496}
1497
1498/// EmitStoreOfComplex - Store a complex number into the specified l-value.
1500 bool isInit) {
1501 ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
1502}
1503
1504/// EmitLoadOfComplex - Load a complex number from the specified address.
1506 SourceLocation loc) {
1507 return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
1508}
1509
1511 assert(E->getOpcode() == BO_Assign);
1512 ComplexPairTy Val; // ignored
1513 LValue LVal = ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1514 if (getLangOpts().OpenMP)
1516 E->getLHS());
1517 return LVal;
1518}
1519
1520typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1521 const ComplexExprEmitter::BinOpInfo &);
1522
1524 switch (Op) {
1525 case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1526 case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1527 case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1528 case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
1529 default:
1530 llvm_unreachable("unexpected complex compound assignment");
1531 }
1532}
1533
1536 CompoundFunc Op = getComplexOp(E->getOpcode());
1537 RValue Val;
1538 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1539}
1540
1543 llvm::Value *&Result) {
1544 CompoundFunc Op = getComplexOp(E->getOpcode());
1545 RValue Val;
1546 LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1547 Result = Val.getScalarVal();
1548 return Ret;
1549}
#define V(N, I)
Definition: ASTContext.h:3341
#define HANDLEBINOP(OP)
ComplexPairTy(ComplexExprEmitter::* CompoundFunc)(const ComplexExprEmitter::BinOpInfo &)
static const ComplexType * getComplexType(QualType type)
Return the complex type that we are meant to emit.
CodeGenFunction::ComplexPairTy ComplexPairTy
static llvm::Value * EmitllvmFAbs(CodeGenFunction &CGF, llvm::Value *Value)
#define HANDLE_BINOP(OP)
static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty)
Lookup the libcall name for a given floating point type complex multiply.
static CompoundFunc getComplexOp(BinaryOperatorKind Op)
const Decl * D
Expr * E
SourceLocation Loc
Definition: SemaObjC.cpp:759
const llvm::fltSemantics & getFloatTypeSemantics(QualType T) const
Return the APFloat 'semantics' for the specified scalar floating point type.
CanQualType FloatTy
Definition: ASTContext.h:1131
CanQualType DoubleTy
Definition: ASTContext.h:1131
CanQualType LongDoubleTy
Definition: ASTContext.h:1131
bool hasSameUnqualifiedType(QualType T1, QualType T2) const
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
Definition: ASTContext.h:2675
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
Definition: ASTContext.h:1615
QualType getComplexType(QualType T) const
Return the uniqued reference to the type for a complex number with the specified element type.
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
Definition: Expr.h:4175
AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, __atomic_load,...
Definition: Expr.h:6629
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3860
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:1268
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1375
Expr * getExpr()
Get the initialization expression that will be used.
Definition: ExprCXX.cpp:1085
A rewritten comparison expression that was originally written using operator syntax.
Definition: ExprCXX.h:283
An expression "T()" which creates an rvalue of a non-class type T.
Definition: ExprCXX.h:2182
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2830
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:3498
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:4592
Represents a 'co_await' expression.
Definition: ExprCXX.h:5185
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
Definition: Address.h:128
static Address invalid()
Definition: Address.h:176
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition: Address.h:274
llvm::StringRef getName() const
Return the IR name of the pointer value.
Definition: Address.h:216
bool isValid() const
Definition: Address.h:177
A scoped helper to set the current debug location to the specified location or preferred location of ...
Definition: CGDebugInfo.h:855
Address CreateStructGEP(Address Addr, unsigned Index, const llvm::Twine &Name="")
Definition: CGBuilder.h:218
All available information about a concrete callee.
Definition: CGCall.h:63
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:137
CGFunctionInfo - Class to encapsulate the information about a function definition.
virtual void checkAndEmitLastprivateConditional(CodeGenFunction &CGF, const Expr *LHS)
Checks if the provided LVal is lastprivate conditional and emits the code to update the value of the ...
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:274
void add(RValue rvalue, QualType type)
Definition: CGCall.h:298
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount, Stmt::Likelihood LH=Stmt::LH_None, const Expr *ConditionalOp=nullptr)
EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g.
LValue getOrCreateOpaqueLValueMapping(const OpaqueValueExpr *e)
Given an opaque value expression, return its LValue mapping if it exists, otherwise create one.
LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E, llvm::Value *&Result)
LValue EmitLValue(const Expr *E, KnownNonNull_t IsKnownNonNull=NotKnownNonNull)
EmitLValue - Emit code to compute a designator that specifies the location of the expression.
RValue EmitAtomicLoad(LValue LV, SourceLocation SL, AggValueSlot Slot=AggValueSlot::ignored())
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
const LangOptions & getLangOpts() const
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **callOrInvoke, bool IsMustTail, SourceLocation Loc, bool IsVirtualFunctionPointerThunk=false)
EmitCall - Generate a call of the given function, expecting the given result type,...
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type,...
RValue EmitObjCMessageExpr(const ObjCMessageExpr *E, ReturnValueSlot Return=ReturnValueSlot())
void EmitIgnoredExpr(const Expr *E)
EmitIgnoredExpr - Emit an expression in a context which ignores the result.
llvm::Type * ConvertTypeForMem(QualType T)
ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr)
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
EmitComplexExprIntoLValue - Emit the given expression of complex type and place its result into the s...
ComplexPairTy EmitPromotedComplexExpr(const Expr *E, QualType PromotionType)
ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV, bool isInc, bool isPre)
RValue EmitCoyieldExpr(const CoyieldExpr &E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc)
EmitLoadOfComplex - Load a complex number from the specified l-value.
llvm::Value * EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy, QualType DstTy, SourceLocation Loc)
Emit a conversion from the specified complex type to the specified destination type,...
LValue EmitComplexAssignmentLValue(const BinaryOperator *E)
Emit an l-value for an assignment (simple or compound) of complex type.
void ErrorUnsupported(const Stmt *S, const char *Type)
ErrorUnsupported - Print out an error that codegen doesn't support the specified stmt yet.
Address emitAddrOfImagComponent(Address complex, QualType complexType)
void EmitBranch(llvm::BasicBlock *Block)
EmitBranch - Emit a branch to the specified basic block from the current insert block,...
Address EmitCompoundStmt(const CompoundStmt &S, bool GetLast=false, AggValueSlot AVS=AggValueSlot::ignored())
RValue EmitVAArg(VAArgExpr *VE, Address &VAListAddr, AggValueSlot Slot=AggValueSlot::ignored())
Generate code to get an argument from the passed in pointer and update it accordingly.
RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e, AggValueSlot slot=AggValueSlot::ignored())
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
ComplexPairTy EmitUnPromotedValue(ComplexPairTy result, QualType PromotionType)
ComplexPairTy EmitPromotedValue(ComplexPairTy result, QualType PromotionType)
llvm::Value * EmitPromotedScalarExpr(const Expr *E, QualType PromotionType)
llvm::Type * ConvertType(QualType T)
RValue EmitCoawaitExpr(const CoawaitExpr &E, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
uint64_t getProfileCount(const Stmt *S)
Get the profiler's count for the given statement.
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit)
llvm::Value * EmitScalarConversion(llvm::Value *Src, QualType SrcTy, QualType DstTy, SourceLocation Loc)
Emit a conversion from the specified type to the specified destination type, both of which are LLVM s...
RValue getOrCreateOpaqueRValueMapping(const OpaqueValueExpr *e)
Given an opaque value expression, return its RValue mapping if it exists, otherwise create one.
RValue EmitCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue=ReturnValueSlot())
llvm::LLVMContext & getLLVMContext()
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
bool LValueIsSuitableForInlineAtomic(LValue Src)
void incrementProfileCounter(const Stmt *S, llvm::Value *StepV=nullptr)
Increment the profiler's counter for the given statement by StepV.
Address emitAddrOfRealComponent(Address complex, QualType complexType)
LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E)
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
RValue EmitAtomicExpr(AtomicExpr *E)
void EmitExplicitCastExprType(const ExplicitCastExpr *E, CodeGenFunction *CGF=nullptr)
Emit type info if type of an expression is a variably modified type.
Definition: CGExpr.cpp:1244
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
DiagnosticsEngine & getDiags() const
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=std::nullopt)
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1606
const CGFunctionInfo & arrangeFreeFunctionCall(const CallArgList &Args, const FunctionType *Ty, bool ChainCall)
Figure out the rules for calling a function with the given formal type using the given arguments.
Definition: CGCall.cpp:638
LValue - This represents an lvalue references.
Definition: CGValue.h:182
bool isSimple() const
Definition: CGValue.h:278
bool isVolatileQualified() const
Definition: CGValue.h:285
void setTBAAInfo(TBAAAccessInfo Info)
Definition: CGValue.h:336
Address getAddress() const
Definition: CGValue.h:361
QualType getType() const
Definition: CGValue.h:291
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:42
static RValue get(llvm::Value *V)
Definition: CGValue.h:98
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
Definition: CGValue.h:108
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:71
std::pair< llvm::Value *, llvm::Value * > getComplexVal() const
getComplexVal - Return the real/imag components of this complex value.
Definition: CGValue.h:78
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition: CGCall.h:372
Complex values, per C99 6.2.5p11.
Definition: Type.h:3134
QualType getElementType() const
Definition: Type.h:3144
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:4122
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:3428
ConstantExpr - An expression that occurs in a constant context and optionally the result of evaluatin...
Definition: Expr.h:1077
Represents a 'co_yield' expression.
Definition: ExprCXX.h:5266
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1265
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1547
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition: ExprCXX.h:3474
This represents one expression.
Definition: Expr.h:110
bool isGLValue() const
Definition: Expr.h:280
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Returns the set of floating point options that apply to this expression.
Definition: Expr.cpp:3864
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3066
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:277
QualType getType() const
Definition: Expr.h:142
Represents difference between two FPOptions values.
Definition: LangOptions.h:947
Represents a prototype with parameter type info, e.g.
Definition: Type.h:5002
Represents a C11 generic selection.
Definition: Expr.h:5917
ImaginaryLiteral - We support imaginary integer and floating point literals, like "1....
Definition: Expr.h:1717
const Expr * getSubExpr() const
Definition: Expr.h:1729
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition: Expr.h:3675
Represents an implicitly-generated value initialization of an object of a given type.
Definition: Expr.h:5792
Describes an C or C++ initializer list.
Definition: Expr.h:5039
@ CX_Full
Implementation of complex division and multiplication using a call to runtime library functions(gener...
Definition: LangOptions.h:434
@ CX_Basic
Implementation of complex division and multiplication using algebraic formulas at source precision.
Definition: LangOptions.h:453
@ CX_Promoted
Implementation of complex division using algebraic formulas at higher precision.
Definition: LangOptions.h:448
@ CX_Improved
Implementation of complex division offering an improved handling for overflow in intermediate calcula...
Definition: LangOptions.h:439
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3187
Expr * getBase() const
Definition: Expr.h:3264
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition: ExprObjC.h:549
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:945
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition: Expr.h:1173
ParenExpr - This represents a parenthesized expression, e.g.
Definition: Expr.h:2135
const Expr * getSubExpr() const
Definition: Expr.h:2150
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6497
A (possibly-)qualified type.
Definition: Type.h:941
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:1008
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:7750
bool UseExcessPrecision(const ASTContext &Ctx)
Definition: Type.cpp:1571
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
Encodes a location in the source.
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:4417
RetTy Visit(PTR(Stmt) S, ParamTys... P)
Definition: StmtVisitor.h:44
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:185
Stmt - This represents one statement.
Definition: Stmt.h:84
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition: ExprCXX.h:4484
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:8607
bool isAnyComplexType() const
Definition: Type.h:8111
bool isAtomicType() const
Definition: Type.h:8158
bool isRealFloatingType() const
Floating point categories.
Definition: Type.cpp:2266
bool isFloatingType() const
Definition: Type.cpp:2249
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
Definition: Type.cpp:2196
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8540
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition: Expr.h:2188
Represents a call to the builtin function __builtin_va_arg.
Definition: Expr.h:4701
QualType getType() const
Definition: Value.cpp:234
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< ComplexType > complexType
Matches C99 complex types.
bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
Definition: Interp.h:276
bool Null(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition: Interp.h:2259
bool GE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1119
The JSON file list parser is used to communicate input to InstallAPI.
BinaryOperatorKind
@ Result
The result type of a method or function.
CastKind
CastKind - The kind of operation required for a conversion.
@ EST_BasicNoexcept
noexcept
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
cl::opt< bool > EnableSingleByteCoverage
#define false
Definition: stdbool.h:26
llvm::CallingConv::ID getRuntimeCC() const
static TBAAAccessInfo getMayAliasInfo()
Definition: CodeGenTBAA.h:62
Holds information about the various types of exception specification.
Definition: Type.h:5059
Extra information about a function prototype.
Definition: Type.h:5087
ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI)
Definition: Type.h:5107