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