clang 19.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;
54public:
55 ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
56 : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
57 }
58
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) {
148 if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(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) {
160 if (CodeGenFunction::ConstantEmission Constant =
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.
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);
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) {
230 CodeGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE);
231 return Visit(DAE->getExpr());
232 }
233 ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
234 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF, DIE);
235 return Visit(DIE->getExpr());
236 }
237 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
238 CodeGenFunction::RunCleanupsScope Scope(CGF);
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 getPromotionType(QualType Ty) {
291 if (auto *CT = Ty->getAs<ComplexType>()) {
292 QualType ElementType = CT->getElementType();
293 if (ElementType.UseExcessPrecision(CGF.getContext()))
294 return CGF.getContext().getComplexType(CGF.getContext().FloatTy);
295 }
296 if (Ty.UseExcessPrecision(CGF.getContext()))
297 return CGF.getContext().FloatTy;
298 return QualType();
299 }
300
301#define HANDLEBINOP(OP) \
302 ComplexPairTy VisitBin##OP(const BinaryOperator *E) { \
303 QualType promotionTy = getPromotionType(E->getType()); \
304 ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy)); \
305 if (!promotionTy.isNull()) \
306 result = \
307 CGF.EmitUnPromotedValue(result, E->getType()); \
308 return result; \
309 }
310
311 HANDLEBINOP(Mul)
312 HANDLEBINOP(Div)
313 HANDLEBINOP(Add)
314 HANDLEBINOP(Sub)
315#undef HANDLEBINOP
316
317 ComplexPairTy VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E) {
318 return Visit(E->getSemanticForm());
319 }
320
321 // Compound assignments.
322 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
323 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
324 }
325 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
326 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
327 }
328 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
329 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
330 }
331 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
332 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
333 }
334
335 // GCC rejects rem/and/or/xor for integer complex.
336 // Logical and/or always return int, never complex.
337
338 // No comparisons produce a complex result.
339
340 LValue EmitBinAssignLValue(const BinaryOperator *E,
341 ComplexPairTy &Val);
342 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
343 ComplexPairTy VisitBinComma (const BinaryOperator *E);
344
345
347 VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
348 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
349
350 ComplexPairTy VisitInitListExpr(InitListExpr *E);
351
352 ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
353 return EmitLoadOfLValue(E);
354 }
355
356 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
357
358 ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
359 return CGF.EmitAtomicExpr(E).getComplexVal();
360 }
361
362 ComplexPairTy VisitPackIndexingExpr(PackIndexingExpr *E) {
363 return Visit(E->getSelectedExpr());
364 }
365};
366} // end anonymous namespace.
367
368//===----------------------------------------------------------------------===//
369// Utilities
370//===----------------------------------------------------------------------===//
371
372Address CodeGenFunction::emitAddrOfRealComponent(Address addr,
374 return Builder.CreateStructGEP(addr, 0, addr.getName() + ".realp");
375}
376
379 return Builder.CreateStructGEP(addr, 1, addr.getName() + ".imagp");
380}
381
382/// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
383/// load the real and imaginary pieces, returning them as Real/Imag.
384ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
385 SourceLocation loc) {
386 assert(lvalue.isSimple() && "non-simple complex l-value?");
387 if (lvalue.getType()->isAtomicType())
388 return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
389
390 Address SrcPtr = lvalue.getAddress(CGF);
391 bool isVolatile = lvalue.isVolatileQualified();
392
393 llvm::Value *Real = nullptr, *Imag = nullptr;
394
395 if (!IgnoreReal || isVolatile) {
396 Address RealP = CGF.emitAddrOfRealComponent(SrcPtr, lvalue.getType());
397 Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr.getName() + ".real");
398 }
399
400 if (!IgnoreImag || isVolatile) {
401 Address ImagP = CGF.emitAddrOfImagComponent(SrcPtr, lvalue.getType());
402 Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr.getName() + ".imag");
403 }
404
405 return ComplexPairTy(Real, Imag);
406}
407
408/// EmitStoreOfComplex - Store the specified real/imag parts into the
409/// specified value pointer.
410void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
411 bool isInit) {
412 if (lvalue.getType()->isAtomicType() ||
413 (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue)))
414 return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
415
416 Address Ptr = lvalue.getAddress(CGF);
417 Address RealPtr = CGF.emitAddrOfRealComponent(Ptr, lvalue.getType());
418 Address ImagPtr = CGF.emitAddrOfImagComponent(Ptr, lvalue.getType());
419
420 Builder.CreateStore(Val.first, RealPtr, lvalue.isVolatileQualified());
421 Builder.CreateStore(Val.second, ImagPtr, lvalue.isVolatileQualified());
422}
423
424
425
426//===----------------------------------------------------------------------===//
427// Visitor Methods
428//===----------------------------------------------------------------------===//
429
430ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
431 CGF.ErrorUnsupported(E, "complex expression");
432 llvm::Type *EltTy =
434 llvm::Value *U = llvm::UndefValue::get(EltTy);
435 return ComplexPairTy(U, U);
436}
437
438ComplexPairTy ComplexExprEmitter::
439VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
440 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
441 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
442}
443
444
445ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
447 return EmitLoadOfLValue(E);
448
449 return CGF.EmitCallExpr(E).getComplexVal();
450}
451
452ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
453 CodeGenFunction::StmtExprEvaluation eval(CGF);
454 Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
455 assert(RetAlloca.isValid() && "Expected complex return value");
456 return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
457 E->getExprLoc());
458}
459
460/// Emit a cast from complex value Val to DestType.
461ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
462 QualType SrcType,
463 QualType DestType,
464 SourceLocation Loc) {
465 // Get the src/dest element type.
466 SrcType = SrcType->castAs<ComplexType>()->getElementType();
467 DestType = DestType->castAs<ComplexType>()->getElementType();
468
469 // C99 6.3.1.6: When a value of complex type is converted to another
470 // complex type, both the real and imaginary parts follow the conversion
471 // rules for the corresponding real types.
472 if (Val.first)
473 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType, Loc);
474 if (Val.second)
475 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType, Loc);
476 return Val;
477}
478
479ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
480 QualType SrcType,
481 QualType DestType,
482 SourceLocation Loc) {
483 // Convert the input element to the element type of the complex.
484 DestType = DestType->castAs<ComplexType>()->getElementType();
485 Val = CGF.EmitScalarConversion(Val, SrcType, DestType, Loc);
486
487 // Return (realval, 0).
488 return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
489}
490
491ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
492 QualType DestTy) {
493 switch (CK) {
494 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
495
496 // Atomic to non-atomic casts may be more than a no-op for some platforms and
497 // for some types.
498 case CK_AtomicToNonAtomic:
499 case CK_NonAtomicToAtomic:
500 case CK_NoOp:
501 case CK_LValueToRValue:
502 case CK_UserDefinedConversion:
503 return Visit(Op);
504
505 case CK_LValueBitCast: {
506 LValue origLV = CGF.EmitLValue(Op);
507 Address V = origLV.getAddress(CGF).withElementType(CGF.ConvertType(DestTy));
508 return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), Op->getExprLoc());
509 }
510
511 case CK_LValueToRValueBitCast: {
512 LValue SourceLVal = CGF.EmitLValue(Op);
513 Address Addr = SourceLVal.getAddress(CGF).withElementType(
514 CGF.ConvertTypeForMem(DestTy));
515 LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy);
517 return EmitLoadOfLValue(DestLV, Op->getExprLoc());
518 }
519
520 case CK_BitCast:
521 case CK_BaseToDerived:
522 case CK_DerivedToBase:
523 case CK_UncheckedDerivedToBase:
524 case CK_Dynamic:
525 case CK_ToUnion:
526 case CK_ArrayToPointerDecay:
527 case CK_FunctionToPointerDecay:
528 case CK_NullToPointer:
529 case CK_NullToMemberPointer:
530 case CK_BaseToDerivedMemberPointer:
531 case CK_DerivedToBaseMemberPointer:
532 case CK_MemberPointerToBoolean:
533 case CK_ReinterpretMemberPointer:
534 case CK_ConstructorConversion:
535 case CK_IntegralToPointer:
536 case CK_PointerToIntegral:
537 case CK_PointerToBoolean:
538 case CK_ToVoid:
539 case CK_VectorSplat:
540 case CK_IntegralCast:
541 case CK_BooleanToSignedIntegral:
542 case CK_IntegralToBoolean:
543 case CK_IntegralToFloating:
544 case CK_FloatingToIntegral:
545 case CK_FloatingToBoolean:
546 case CK_FloatingCast:
547 case CK_CPointerToObjCPointerCast:
548 case CK_BlockPointerToObjCPointerCast:
549 case CK_AnyPointerToBlockPointerCast:
550 case CK_ObjCObjectLValueCast:
551 case CK_FloatingComplexToReal:
552 case CK_FloatingComplexToBoolean:
553 case CK_IntegralComplexToReal:
554 case CK_IntegralComplexToBoolean:
555 case CK_ARCProduceObject:
556 case CK_ARCConsumeObject:
557 case CK_ARCReclaimReturnedObject:
558 case CK_ARCExtendBlockObject:
559 case CK_CopyAndAutoreleaseBlockObject:
560 case CK_BuiltinFnToFnPtr:
561 case CK_ZeroToOCLOpaqueType:
562 case CK_AddressSpaceConversion:
563 case CK_IntToOCLSampler:
564 case CK_FloatingToFixedPoint:
565 case CK_FixedPointToFloating:
566 case CK_FixedPointCast:
567 case CK_FixedPointToBoolean:
568 case CK_FixedPointToIntegral:
569 case CK_IntegralToFixedPoint:
570 case CK_MatrixCast:
571 case CK_HLSLVectorTruncation:
572 llvm_unreachable("invalid cast kind for complex value");
573
574 case CK_FloatingRealToComplex:
575 case CK_IntegralRealToComplex: {
576 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op);
577 return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op), Op->getType(),
578 DestTy, Op->getExprLoc());
579 }
580
581 case CK_FloatingComplexCast:
582 case CK_FloatingComplexToIntegralComplex:
583 case CK_IntegralComplexCast:
584 case CK_IntegralComplexToFloatingComplex: {
585 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op);
586 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy,
587 Op->getExprLoc());
588 }
589 }
590
591 llvm_unreachable("unknown cast resulting in complex value");
592}
593
594ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E,
595 QualType PromotionType) {
596 QualType promotionTy = PromotionType.isNull()
597 ? getPromotionType(E->getSubExpr()->getType())
598 : PromotionType;
599 ComplexPairTy result = VisitPlus(E, promotionTy);
600 if (!promotionTy.isNull())
601 return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType());
602 return result;
603}
604
605ComplexPairTy ComplexExprEmitter::VisitPlus(const UnaryOperator *E,
606 QualType PromotionType) {
607 TestAndClearIgnoreReal();
608 TestAndClearIgnoreImag();
609 if (!PromotionType.isNull())
610 return CGF.EmitPromotedComplexExpr(E->getSubExpr(), PromotionType);
611 return Visit(E->getSubExpr());
612}
613
614ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
615 QualType PromotionType) {
616 QualType promotionTy = PromotionType.isNull()
617 ? getPromotionType(E->getSubExpr()->getType())
618 : PromotionType;
619 ComplexPairTy result = VisitMinus(E, promotionTy);
620 if (!promotionTy.isNull())
621 return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType());
622 return result;
623}
624ComplexPairTy ComplexExprEmitter::VisitMinus(const UnaryOperator *E,
625 QualType PromotionType) {
626 TestAndClearIgnoreReal();
627 TestAndClearIgnoreImag();
628 ComplexPairTy Op;
629 if (!PromotionType.isNull())
630 Op = CGF.EmitPromotedComplexExpr(E->getSubExpr(), PromotionType);
631 else
632 Op = Visit(E->getSubExpr());
633
634 llvm::Value *ResR, *ResI;
635 if (Op.first->getType()->isFloatingPointTy()) {
636 ResR = Builder.CreateFNeg(Op.first, "neg.r");
637 ResI = Builder.CreateFNeg(Op.second, "neg.i");
638 } else {
639 ResR = Builder.CreateNeg(Op.first, "neg.r");
640 ResI = Builder.CreateNeg(Op.second, "neg.i");
641 }
642 return ComplexPairTy(ResR, ResI);
643}
644
645ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
646 TestAndClearIgnoreReal();
647 TestAndClearIgnoreImag();
648 // ~(a+ib) = a + i*-b
649 ComplexPairTy Op = Visit(E->getSubExpr());
650 llvm::Value *ResI;
651 if (Op.second->getType()->isFloatingPointTy())
652 ResI = Builder.CreateFNeg(Op.second, "conj.i");
653 else
654 ResI = Builder.CreateNeg(Op.second, "conj.i");
655
656 return ComplexPairTy(Op.first, ResI);
657}
658
659ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
660 llvm::Value *ResR, *ResI;
661
662 if (Op.LHS.first->getType()->isFloatingPointTy()) {
663 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
664 ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
665 if (Op.LHS.second && Op.RHS.second)
666 ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
667 else
668 ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
669 assert(ResI && "Only one operand may be real!");
670 } else {
671 ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
672 assert(Op.LHS.second && Op.RHS.second &&
673 "Both operands of integer complex operators must be complex!");
674 ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
675 }
676 return ComplexPairTy(ResR, ResI);
677}
678
679ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
680 llvm::Value *ResR, *ResI;
681 if (Op.LHS.first->getType()->isFloatingPointTy()) {
682 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
683 ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
684 if (Op.LHS.second && Op.RHS.second)
685 ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
686 else
687 ResI = Op.LHS.second ? Op.LHS.second
688 : Builder.CreateFNeg(Op.RHS.second, "sub.i");
689 assert(ResI && "Only one operand may be real!");
690 } else {
691 ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
692 assert(Op.LHS.second && Op.RHS.second &&
693 "Both operands of integer complex operators must be complex!");
694 ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
695 }
696 return ComplexPairTy(ResR, ResI);
697}
698
699/// Emit a libcall for a binary operation on complex types.
700ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
701 const BinOpInfo &Op) {
702 CallArgList Args;
703 Args.add(RValue::get(Op.LHS.first),
704 Op.Ty->castAs<ComplexType>()->getElementType());
705 Args.add(RValue::get(Op.LHS.second),
706 Op.Ty->castAs<ComplexType>()->getElementType());
707 Args.add(RValue::get(Op.RHS.first),
708 Op.Ty->castAs<ComplexType>()->getElementType());
709 Args.add(RValue::get(Op.RHS.second),
710 Op.Ty->castAs<ComplexType>()->getElementType());
711
712 // We *must* use the full CG function call building logic here because the
713 // complex type has special ABI handling. We also should not forget about
714 // special calling convention which may be used for compiler builtins.
715
716 // We create a function qualified type to state that this call does not have
717 // any exceptions.
719 EPI = EPI.withExceptionSpec(
722 4, Op.Ty->castAs<ComplexType>()->getElementType());
723 QualType FQTy = CGF.getContext().getFunctionType(Op.Ty, ArgsQTys, EPI);
724 const CGFunctionInfo &FuncInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall(
725 Args, cast<FunctionType>(FQTy.getTypePtr()), false);
726
727 llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
728 llvm::FunctionCallee Func = CGF.CGM.CreateRuntimeFunction(
729 FTy, LibCallName, llvm::AttributeList(), true);
731
732 llvm::CallBase *Call;
733 RValue Res = CGF.EmitCall(FuncInfo, Callee, ReturnValueSlot(), Args, &Call);
734 Call->setCallingConv(CGF.CGM.getRuntimeCC());
735 return Res.getComplexVal();
736}
737
738/// Lookup the libcall name for a given floating point type complex
739/// multiply.
740static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
741 switch (Ty->getTypeID()) {
742 default:
743 llvm_unreachable("Unsupported floating point type!");
744 case llvm::Type::HalfTyID:
745 return "__mulhc3";
746 case llvm::Type::FloatTyID:
747 return "__mulsc3";
748 case llvm::Type::DoubleTyID:
749 return "__muldc3";
750 case llvm::Type::PPC_FP128TyID:
751 return "__multc3";
752 case llvm::Type::X86_FP80TyID:
753 return "__mulxc3";
754 case llvm::Type::FP128TyID:
755 return "__multc3";
756 }
757}
758
759// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
760// typed values.
761ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
762 using llvm::Value;
763 Value *ResR, *ResI;
764 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
765
766 if (Op.LHS.first->getType()->isFloatingPointTy()) {
767 // The general formulation is:
768 // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
769 //
770 // But we can fold away components which would be zero due to a real
771 // operand according to C11 Annex G.5.1p2.
772 // FIXME: C11 also provides for imaginary types which would allow folding
773 // still more of this within the type system.
774
775 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
776 if (Op.LHS.second && Op.RHS.second) {
777 // If both operands are complex, emit the core math directly, and then
778 // test for NaNs. If we find NaNs in the result, we delegate to a libcall
779 // to carefully re-compute the correct infinity representation if
780 // possible. The expectation is that the presence of NaNs here is
781 // *extremely* rare, and so the cost of the libcall is almost irrelevant.
782 // This is good, because the libcall re-computes the core multiplication
783 // exactly the same as we do here and re-tests for NaNs in order to be
784 // a generic complex*complex libcall.
785
786 // First compute the four products.
787 Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
788 Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
789 Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
790 Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
791
792 // The real part is the difference of the first two, the imaginary part is
793 // the sum of the second.
794 ResR = Builder.CreateFSub(AC, BD, "mul_r");
795 ResI = Builder.CreateFAdd(AD, BC, "mul_i");
796
797 if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Limited ||
798 Op.FPFeatures.getComplexRange() == LangOptions::CX_Fortran)
799 return ComplexPairTy(ResR, ResI);
800
801 // Emit the test for the real part becoming NaN and create a branch to
802 // handle it. We test for NaN by comparing the number to itself.
803 Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
804 llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
805 llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
806 llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
807 llvm::BasicBlock *OrigBB = Branch->getParent();
808
809 // Give hint that we very much don't expect to see NaNs.
810 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
811 llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1);
812 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
813
814 // Now test the imaginary part and create its branch.
815 CGF.EmitBlock(INaNBB);
816 Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
817 llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
818 Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
819 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
820
821 // Now emit the libcall on this slowest of the slow paths.
822 CGF.EmitBlock(LibCallBB);
823 Value *LibCallR, *LibCallI;
824 std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
825 getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
826 Builder.CreateBr(ContBB);
827
828 // Finally continue execution by phi-ing together the different
829 // computation paths.
830 CGF.EmitBlock(ContBB);
831 llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
832 RealPHI->addIncoming(ResR, OrigBB);
833 RealPHI->addIncoming(ResR, INaNBB);
834 RealPHI->addIncoming(LibCallR, LibCallBB);
835 llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
836 ImagPHI->addIncoming(ResI, OrigBB);
837 ImagPHI->addIncoming(ResI, INaNBB);
838 ImagPHI->addIncoming(LibCallI, LibCallBB);
839 return ComplexPairTy(RealPHI, ImagPHI);
840 }
841 assert((Op.LHS.second || Op.RHS.second) &&
842 "At least one operand must be complex!");
843
844 // If either of the operands is a real rather than a complex, the
845 // imaginary component is ignored when computing the real component of the
846 // result.
847 ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
848
849 ResI = Op.LHS.second
850 ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
851 : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
852 } else {
853 assert(Op.LHS.second && Op.RHS.second &&
854 "Both operands of integer complex operators must be complex!");
855 Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
856 Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
857 ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
858
859 Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
860 Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
861 ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
862 }
863 return ComplexPairTy(ResR, ResI);
864}
865
866ComplexPairTy ComplexExprEmitter::EmitAlgebraicDiv(llvm::Value *LHSr,
867 llvm::Value *LHSi,
868 llvm::Value *RHSr,
869 llvm::Value *RHSi) {
870 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
871 llvm::Value *DSTr, *DSTi;
872
873 llvm::Value *AC = Builder.CreateFMul(LHSr, RHSr); // a*c
874 llvm::Value *BD = Builder.CreateFMul(LHSi, RHSi); // b*d
875 llvm::Value *ACpBD = Builder.CreateFAdd(AC, BD); // ac+bd
876
877 llvm::Value *CC = Builder.CreateFMul(RHSr, RHSr); // c*c
878 llvm::Value *DD = Builder.CreateFMul(RHSi, RHSi); // d*d
879 llvm::Value *CCpDD = Builder.CreateFAdd(CC, DD); // cc+dd
880
881 llvm::Value *BC = Builder.CreateFMul(LHSi, RHSr); // b*c
882 llvm::Value *AD = Builder.CreateFMul(LHSr, RHSi); // a*d
883 llvm::Value *BCmAD = Builder.CreateFSub(BC, AD); // bc-ad
884
885 DSTr = Builder.CreateFDiv(ACpBD, CCpDD);
886 DSTi = Builder.CreateFDiv(BCmAD, CCpDD);
887 return ComplexPairTy(DSTr, DSTi);
888}
889
890// EmitFAbs - Emit a call to @llvm.fabs.
891static llvm::Value *EmitllvmFAbs(CodeGenFunction &CGF, llvm::Value *Value) {
892 llvm::Function *Func =
893 CGF.CGM.getIntrinsic(llvm::Intrinsic::fabs, Value->getType());
894 llvm::Value *Call = CGF.Builder.CreateCall(Func, Value);
895 return Call;
896}
897
898// EmitRangeReductionDiv - Implements Smith's algorithm for complex division.
899// SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8 (1962).
900ComplexPairTy ComplexExprEmitter::EmitRangeReductionDiv(llvm::Value *LHSr,
901 llvm::Value *LHSi,
902 llvm::Value *RHSr,
903 llvm::Value *RHSi) {
904 // FIXME: This could eventually be replaced by an LLVM intrinsic to
905 // avoid this long IR sequence.
906
907 // (a + ib) / (c + id) = (e + if)
908 llvm::Value *FAbsRHSr = EmitllvmFAbs(CGF, RHSr); // |c|
909 llvm::Value *FAbsRHSi = EmitllvmFAbs(CGF, RHSi); // |d|
910 // |c| >= |d|
911 llvm::Value *IsR = Builder.CreateFCmpUGT(FAbsRHSr, FAbsRHSi, "abs_cmp");
912
913 llvm::BasicBlock *TrueBB =
914 CGF.createBasicBlock("abs_rhsr_greater_or_equal_abs_rhsi");
915 llvm::BasicBlock *FalseBB =
916 CGF.createBasicBlock("abs_rhsr_less_than_abs_rhsi");
917 llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_div");
918 Builder.CreateCondBr(IsR, TrueBB, FalseBB);
919
920 CGF.EmitBlock(TrueBB);
921 // abs(c) >= abs(d)
922 // r = d/c
923 // tmp = c + rd
924 // e = (a + br)/tmp
925 // f = (b - ar)/tmp
926 llvm::Value *DdC = Builder.CreateFDiv(RHSi, RHSr); // r=d/c
927
928 llvm::Value *RD = Builder.CreateFMul(DdC, RHSi); // rd
929 llvm::Value *CpRD = Builder.CreateFAdd(RHSr, RD); // tmp=c+rd
930
931 llvm::Value *T3 = Builder.CreateFMul(LHSi, DdC); // br
932 llvm::Value *T4 = Builder.CreateFAdd(LHSr, T3); // a+br
933 llvm::Value *DSTTr = Builder.CreateFDiv(T4, CpRD); // (a+br)/tmp
934
935 llvm::Value *T5 = Builder.CreateFMul(LHSr, DdC); // ar
936 llvm::Value *T6 = Builder.CreateFSub(LHSi, T5); // b-ar
937 llvm::Value *DSTTi = Builder.CreateFDiv(T6, CpRD); // (b-ar)/tmp
938 Builder.CreateBr(ContBB);
939
940 CGF.EmitBlock(FalseBB);
941 // abs(c) < abs(d)
942 // r = c/d
943 // tmp = d + rc
944 // e = (ar + b)/tmp
945 // f = (br - a)/tmp
946 llvm::Value *CdD = Builder.CreateFDiv(RHSr, RHSi); // r=c/d
947
948 llvm::Value *RC = Builder.CreateFMul(CdD, RHSr); // rc
949 llvm::Value *DpRC = Builder.CreateFAdd(RHSi, RC); // tmp=d+rc
950
951 llvm::Value *T7 = Builder.CreateFMul(LHSr, CdD); // ar
952 llvm::Value *T8 = Builder.CreateFAdd(T7, LHSi); // ar+b
953 llvm::Value *DSTFr = Builder.CreateFDiv(T8, DpRC); // (ar+b)/tmp
954
955 llvm::Value *T9 = Builder.CreateFMul(LHSi, CdD); // br
956 llvm::Value *T10 = Builder.CreateFSub(T9, LHSr); // br-a
957 llvm::Value *DSTFi = Builder.CreateFDiv(T10, DpRC); // (br-a)/tmp
958 Builder.CreateBr(ContBB);
959
960 // Phi together the computation paths.
961 CGF.EmitBlock(ContBB);
962 llvm::PHINode *VALr = Builder.CreatePHI(DSTTr->getType(), 2);
963 VALr->addIncoming(DSTTr, TrueBB);
964 VALr->addIncoming(DSTFr, FalseBB);
965 llvm::PHINode *VALi = Builder.CreatePHI(DSTTi->getType(), 2);
966 VALi->addIncoming(DSTTi, TrueBB);
967 VALi->addIncoming(DSTFi, FalseBB);
968 return ComplexPairTy(VALr, VALi);
969}
970
971// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
972// typed values.
973ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
974 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
975 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
976 llvm::Value *DSTr, *DSTi;
977 if (LHSr->getType()->isFloatingPointTy()) {
978 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
979 if (!RHSi) {
980 assert(LHSi && "Can have at most one non-complex operand!");
981
982 DSTr = Builder.CreateFDiv(LHSr, RHSr);
983 DSTi = Builder.CreateFDiv(LHSi, RHSr);
984 return ComplexPairTy(DSTr, DSTi);
985 }
986 llvm::Value *OrigLHSi = LHSi;
987 if (!LHSi)
988 LHSi = llvm::Constant::getNullValue(RHSi->getType());
989 if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Fortran)
990 return EmitRangeReductionDiv(LHSr, LHSi, RHSr, RHSi);
991 else if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Limited)
992 return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi);
993 else if (!CGF.getLangOpts().FastMath ||
994 // '-ffast-math' is used in the command line but followed by an
995 // '-fno-cx-limited-range'.
996 Op.FPFeatures.getComplexRange() == LangOptions::CX_Full) {
997 LHSi = OrigLHSi;
998 // If we have a complex operand on the RHS and FastMath is not allowed, we
999 // delegate to a libcall to handle all of the complexities and minimize
1000 // underflow/overflow cases. When FastMath is allowed we construct the
1001 // divide inline using the same algorithm as for integer operands.
1002 //
1003 // FIXME: We would be able to avoid the libcall in many places if we
1004 // supported imaginary types in addition to complex types.
1005 BinOpInfo LibCallOp = Op;
1006 // If LHS was a real, supply a null imaginary part.
1007 if (!LHSi)
1008 LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
1009
1010 switch (LHSr->getType()->getTypeID()) {
1011 default:
1012 llvm_unreachable("Unsupported floating point type!");
1013 case llvm::Type::HalfTyID:
1014 return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
1015 case llvm::Type::FloatTyID:
1016 return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
1017 case llvm::Type::DoubleTyID:
1018 return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
1019 case llvm::Type::PPC_FP128TyID:
1020 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
1021 case llvm::Type::X86_FP80TyID:
1022 return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
1023 case llvm::Type::FP128TyID:
1024 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
1025 }
1026 } else {
1027 return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi);
1028 }
1029 } else {
1030 assert(Op.LHS.second && Op.RHS.second &&
1031 "Both operands of integer complex operators must be complex!");
1032 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
1033 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
1034 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
1035 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
1036
1037 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
1038 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
1039 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
1040
1041 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
1042 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
1043 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
1044
1045 if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
1046 DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
1047 DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
1048 } else {
1049 DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
1050 DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
1051 }
1052 }
1053
1054 return ComplexPairTy(DSTr, DSTi);
1055}
1056
1058 QualType UnPromotionType) {
1059 llvm::Type *ComplexElementTy =
1060 ConvertType(UnPromotionType->castAs<ComplexType>()->getElementType());
1061 if (result.first)
1062 result.first =
1063 Builder.CreateFPTrunc(result.first, ComplexElementTy, "unpromotion");
1064 if (result.second)
1065 result.second =
1066 Builder.CreateFPTrunc(result.second, ComplexElementTy, "unpromotion");
1067 return result;
1068}
1069
1071 QualType PromotionType) {
1072 llvm::Type *ComplexElementTy =
1073 ConvertType(PromotionType->castAs<ComplexType>()->getElementType());
1074 if (result.first)
1075 result.first = Builder.CreateFPExt(result.first, ComplexElementTy, "ext");
1076 if (result.second)
1077 result.second = Builder.CreateFPExt(result.second, ComplexElementTy, "ext");
1078
1079 return result;
1080}
1081
1082ComplexPairTy ComplexExprEmitter::EmitPromoted(const Expr *E,
1083 QualType PromotionType) {
1084 E = E->IgnoreParens();
1085 if (auto BO = dyn_cast<BinaryOperator>(E)) {
1086 switch (BO->getOpcode()) {
1087#define HANDLE_BINOP(OP) \
1088 case BO_##OP: \
1089 return EmitBin##OP(EmitBinOps(BO, PromotionType));
1090 HANDLE_BINOP(Add)
1091 HANDLE_BINOP(Sub)
1092 HANDLE_BINOP(Mul)
1093 HANDLE_BINOP(Div)
1094#undef HANDLE_BINOP
1095 default:
1096 break;
1097 }
1098 } else if (auto UO = dyn_cast<UnaryOperator>(E)) {
1099 switch (UO->getOpcode()) {
1100 case UO_Minus:
1101 return VisitMinus(UO, PromotionType);
1102 case UO_Plus:
1103 return VisitPlus(UO, PromotionType);
1104 default:
1105 break;
1106 }
1107 }
1108 auto result = Visit(const_cast<Expr *>(E));
1109 if (!PromotionType.isNull())
1110 return CGF.EmitPromotedValue(result, PromotionType);
1111 else
1112 return result;
1113}
1114
1116 QualType DstTy) {
1117 return ComplexExprEmitter(*this).EmitPromoted(E, DstTy);
1118}
1119
1121ComplexExprEmitter::EmitPromotedComplexOperand(const Expr *E,
1122 QualType OverallPromotionType) {
1123 if (E->getType()->isAnyComplexType()) {
1124 if (!OverallPromotionType.isNull())
1125 return CGF.EmitPromotedComplexExpr(E, OverallPromotionType);
1126 else
1127 return Visit(const_cast<Expr *>(E));
1128 } else {
1129 if (!OverallPromotionType.isNull()) {
1130 QualType ComplexElementTy =
1131 OverallPromotionType->castAs<ComplexType>()->getElementType();
1132 return ComplexPairTy(CGF.EmitPromotedScalarExpr(E, ComplexElementTy),
1133 nullptr);
1134 } else {
1135 return ComplexPairTy(CGF.EmitScalarExpr(E), nullptr);
1136 }
1137 }
1138}
1139
1140ComplexExprEmitter::BinOpInfo
1141ComplexExprEmitter::EmitBinOps(const BinaryOperator *E,
1142 QualType PromotionType) {
1143 TestAndClearIgnoreReal();
1144 TestAndClearIgnoreImag();
1145 BinOpInfo Ops;
1146
1147 Ops.LHS = EmitPromotedComplexOperand(E->getLHS(), PromotionType);
1148 Ops.RHS = EmitPromotedComplexOperand(E->getRHS(), PromotionType);
1149 if (!PromotionType.isNull())
1150 Ops.Ty = PromotionType;
1151 else
1152 Ops.Ty = E->getType();
1153 Ops.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts());
1154 return Ops;
1155}
1156
1157
1158LValue ComplexExprEmitter::
1159EmitCompoundAssignLValue(const CompoundAssignOperator *E,
1160 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
1161 RValue &Val) {
1162 TestAndClearIgnoreReal();
1163 TestAndClearIgnoreImag();
1164 QualType LHSTy = E->getLHS()->getType();
1165 if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
1166 LHSTy = AT->getValueType();
1167
1168 BinOpInfo OpInfo;
1169 OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts());
1170 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, OpInfo.FPFeatures);
1171
1172 // Load the RHS and LHS operands.
1173 // __block variables need to have the rhs evaluated first, plus this should
1174 // improve codegen a little.
1175 QualType PromotionTypeCR;
1176 PromotionTypeCR = getPromotionType(E->getComputationResultType());
1177 if (PromotionTypeCR.isNull())
1178 PromotionTypeCR = E->getComputationResultType();
1179 OpInfo.Ty = PromotionTypeCR;
1180 QualType ComplexElementTy =
1181 OpInfo.Ty->castAs<ComplexType>()->getElementType();
1182 QualType PromotionTypeRHS = getPromotionType(E->getRHS()->getType());
1183
1184 // The RHS should have been converted to the computation type.
1185 if (E->getRHS()->getType()->isRealFloatingType()) {
1186 if (!PromotionTypeRHS.isNull())
1187 OpInfo.RHS = ComplexPairTy(
1188 CGF.EmitPromotedScalarExpr(E->getRHS(), PromotionTypeRHS), nullptr);
1189 else {
1190 assert(CGF.getContext().hasSameUnqualifiedType(ComplexElementTy,
1191 E->getRHS()->getType()));
1192
1193 OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
1194 }
1195 } else {
1196 if (!PromotionTypeRHS.isNull()) {
1197 OpInfo.RHS = ComplexPairTy(
1198 CGF.EmitPromotedComplexExpr(E->getRHS(), PromotionTypeRHS));
1199 } else {
1200 assert(CGF.getContext().hasSameUnqualifiedType(OpInfo.Ty,
1201 E->getRHS()->getType()));
1202 OpInfo.RHS = Visit(E->getRHS());
1203 }
1204 }
1205
1206 LValue LHS = CGF.EmitLValue(E->getLHS());
1207
1208 // Load from the l-value and convert it.
1209 SourceLocation Loc = E->getExprLoc();
1210 QualType PromotionTypeLHS = getPromotionType(E->getComputationLHSType());
1211 if (LHSTy->isAnyComplexType()) {
1212 ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
1213 if (!PromotionTypeLHS.isNull())
1214 OpInfo.LHS =
1215 EmitComplexToComplexCast(LHSVal, LHSTy, PromotionTypeLHS, Loc);
1216 else
1217 OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
1218 } else {
1219 llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, Loc);
1220 // For floating point real operands we can directly pass the scalar form
1221 // to the binary operator emission and potentially get more efficient code.
1222 if (LHSTy->isRealFloatingType()) {
1223 QualType PromotedComplexElementTy;
1224 if (!PromotionTypeLHS.isNull()) {
1225 PromotedComplexElementTy =
1226 cast<ComplexType>(PromotionTypeLHS)->getElementType();
1227 if (!CGF.getContext().hasSameUnqualifiedType(PromotedComplexElementTy,
1228 PromotionTypeLHS))
1229 LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy,
1230 PromotedComplexElementTy, Loc);
1231 } else {
1232 if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
1233 LHSVal =
1234 CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy, Loc);
1235 }
1236 OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
1237 } else {
1238 OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
1239 }
1240 }
1241
1242 // Expand the binary operator.
1243 ComplexPairTy Result = (this->*Func)(OpInfo);
1244
1245 // Truncate the result and store it into the LHS lvalue.
1246 if (LHSTy->isAnyComplexType()) {
1247 ComplexPairTy ResVal =
1248 EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy, Loc);
1249 EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
1250 Val = RValue::getComplex(ResVal);
1251 } else {
1252 llvm::Value *ResVal =
1253 CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy, Loc);
1254 CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
1255 Val = RValue::get(ResVal);
1256 }
1257
1258 return LHS;
1259}
1260
1261// Compound assignments.
1262ComplexPairTy ComplexExprEmitter::
1263EmitCompoundAssign(const CompoundAssignOperator *E,
1264 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
1265 RValue Val;
1266 LValue LV = EmitCompoundAssignLValue(E, Func, Val);
1267
1268 // The result of an assignment in C is the assigned r-value.
1269 if (!CGF.getLangOpts().CPlusPlus)
1270 return Val.getComplexVal();
1271
1272 // If the lvalue is non-volatile, return the computed value of the assignment.
1273 if (!LV.isVolatileQualified())
1274 return Val.getComplexVal();
1275
1276 return EmitLoadOfLValue(LV, E->getExprLoc());
1277}
1278
1279LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
1280 ComplexPairTy &Val) {
1281 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
1282 E->getRHS()->getType()) &&
1283 "Invalid assignment");
1284 TestAndClearIgnoreReal();
1285 TestAndClearIgnoreImag();
1286
1287 // Emit the RHS. __block variables need the RHS evaluated first.
1288 Val = Visit(E->getRHS());
1289
1290 // Compute the address to store into.
1291 LValue LHS = CGF.EmitLValue(E->getLHS());
1292
1293 // Store the result value into the LHS lvalue.
1294 EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
1295
1296 return LHS;
1297}
1298
1299ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
1300 ComplexPairTy Val;
1301 LValue LV = EmitBinAssignLValue(E, Val);
1302
1303 // The result of an assignment in C is the assigned r-value.
1304 if (!CGF.getLangOpts().CPlusPlus)
1305 return Val;
1306
1307 // If the lvalue is non-volatile, return the computed value of the assignment.
1308 if (!LV.isVolatileQualified())
1309 return Val;
1310
1311 return EmitLoadOfLValue(LV, E->getExprLoc());
1312}
1313
1314ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
1315 CGF.EmitIgnoredExpr(E->getLHS());
1316 return Visit(E->getRHS());
1317}
1318
1319ComplexPairTy ComplexExprEmitter::
1320VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
1321 TestAndClearIgnoreReal();
1322 TestAndClearIgnoreImag();
1323 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
1324 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
1325 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
1326
1327 // Bind the common expression if necessary.
1328 CodeGenFunction::OpaqueValueMapping binding(CGF, E);
1329
1330
1331 CodeGenFunction::ConditionalEvaluation eval(CGF);
1332 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
1333 CGF.getProfileCount(E));
1334
1335 eval.begin(CGF);
1336 CGF.EmitBlock(LHSBlock);
1339 else
1341
1342 ComplexPairTy LHS = Visit(E->getTrueExpr());
1343 LHSBlock = Builder.GetInsertBlock();
1344 CGF.EmitBranch(ContBlock);
1345 eval.end(CGF);
1346
1347 eval.begin(CGF);
1348 CGF.EmitBlock(RHSBlock);
1351 ComplexPairTy RHS = Visit(E->getFalseExpr());
1352 RHSBlock = Builder.GetInsertBlock();
1353 CGF.EmitBlock(ContBlock);
1356 eval.end(CGF);
1357
1358 // Create a PHI node for the real part.
1359 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
1360 RealPN->addIncoming(LHS.first, LHSBlock);
1361 RealPN->addIncoming(RHS.first, RHSBlock);
1362
1363 // Create a PHI node for the imaginary part.
1364 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
1365 ImagPN->addIncoming(LHS.second, LHSBlock);
1366 ImagPN->addIncoming(RHS.second, RHSBlock);
1367
1368 return ComplexPairTy(RealPN, ImagPN);
1369}
1370
1371ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1372 return Visit(E->getChosenSubExpr());
1373}
1374
1375ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
1376 bool Ignore = TestAndClearIgnoreReal();
1377 (void)Ignore;
1378 assert (Ignore == false && "init list ignored");
1379 Ignore = TestAndClearIgnoreImag();
1380 (void)Ignore;
1381 assert (Ignore == false && "init list ignored");
1382
1383 if (E->getNumInits() == 2) {
1384 llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
1385 llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
1386 return ComplexPairTy(Real, Imag);
1387 } else if (E->getNumInits() == 1) {
1388 return Visit(E->getInit(0));
1389 }
1390
1391 // Empty init list initializes to null
1392 assert(E->getNumInits() == 0 && "Unexpected number of inits");
1393 QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
1394 llvm::Type* LTy = CGF.ConvertType(Ty);
1395 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
1396 return ComplexPairTy(zeroConstant, zeroConstant);
1397}
1398
1399ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
1400 Address ArgValue = Address::invalid();
1401 Address ArgPtr = CGF.EmitVAArg(E, ArgValue);
1402
1403 if (!ArgPtr.isValid()) {
1404 CGF.ErrorUnsupported(E, "complex va_arg expression");
1405 llvm::Type *EltTy =
1407 llvm::Value *U = llvm::UndefValue::get(EltTy);
1408 return ComplexPairTy(U, U);
1409 }
1410
1411 return EmitLoadOfLValue(CGF.MakeAddrLValue(ArgPtr, E->getType()),
1412 E->getExprLoc());
1413}
1414
1415//===----------------------------------------------------------------------===//
1416// Entry Point into this File
1417//===----------------------------------------------------------------------===//
1418
1419/// EmitComplexExpr - Emit the computation of the specified expression of
1420/// complex type, ignoring the result.
1421ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
1422 bool IgnoreImag) {
1423 assert(E && getComplexType(E->getType()) &&
1424 "Invalid complex expression to emit");
1425
1426 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
1427 .Visit(const_cast<Expr *>(E));
1428}
1429
1431 bool isInit) {
1432 assert(E && getComplexType(E->getType()) &&
1433 "Invalid complex expression to emit");
1434 ComplexExprEmitter Emitter(*this);
1435 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
1436 Emitter.EmitStoreOfComplex(Val, dest, isInit);
1437}
1438
1439/// EmitStoreOfComplex - Store a complex number into the specified l-value.
1441 bool isInit) {
1442 ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
1443}
1444
1445/// EmitLoadOfComplex - Load a complex number from the specified address.
1447 SourceLocation loc) {
1448 return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
1449}
1450
1452 assert(E->getOpcode() == BO_Assign);
1453 ComplexPairTy Val; // ignored
1454 LValue LVal = ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1455 if (getLangOpts().OpenMP)
1457 E->getLHS());
1458 return LVal;
1459}
1460
1461typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1462 const ComplexExprEmitter::BinOpInfo &);
1463
1465 switch (Op) {
1466 case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1467 case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1468 case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1469 case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
1470 default:
1471 llvm_unreachable("unexpected complex compound assignment");
1472 }
1473}
1474
1478 RValue Val;
1479 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1480}
1481
1484 llvm::Value *&Result) {
1486 RValue Val;
1487 LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1488 Result = Val.getScalarVal();
1489 return Ret;
1490}
#define V(N, I)
Definition: ASTContext.h:3259
#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)
CanQualType FloatTy
Definition: ASTContext.h:1098
bool hasSameUnqualifiedType(QualType T1, QualType T2) const
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
Definition: ASTContext.h:2592
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:1553
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:4135
Expr * getCond() const
getCond - Return the expression representing the condition for the ?: operator.
Definition: Expr.h:4313
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition: Expr.h:4319
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition: Expr.h:4325
AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, __atomic_load,...
Definition: Expr.h:6431
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3834
Expr * getLHS() const
Definition: Expr.h:3883
SourceLocation getExprLoc() const
Definition: Expr.h:3874
Expr * getRHS() const
Definition: Expr.h:3885
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Get the FP features status of this operator.
Definition: Expr.h:4033
Opcode getOpcode() const
Definition: Expr.h:3878
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:1254
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1361
Expr * getExpr()
Get the initialization expression that will be used.
Definition: ExprCXX.cpp:1035
A rewritten comparison expression that was originally written using operator syntax.
Definition: ExprCXX.h:283
Expr * getSemanticForm()
Get an equivalent semantic form for this expression.
Definition: ExprCXX.h:301
An expression "T()" which creates a value-initialized rvalue of type T, which is a non-class type.
Definition: ExprCXX.h:2165
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2819
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
Definition: Expr.cpp:1570
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:3489
CastKind getCastKind() const
Definition: Expr.h:3533
bool changesVolatileQualification() const
Return.
Definition: Expr.h:3606
Expr * getSubExpr()
Definition: Expr.h:3539
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:4552
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition: Expr.h:4588
Represents a 'co_await' expression.
Definition: ExprCXX.h:5144
An aligned address.
Definition: Address.h:29
static Address invalid()
Definition: Address.h:46
Address withElementType(llvm::Type *ElemTy) const
Return address with different element type, but same pointer and alignment.
Definition: Address.h:100
llvm::StringRef getName() const
Return the IR name of the pointer value.
Definition: Address.h:73
bool isValid() const
Definition: Address.h:47
A scoped helper to set the current debug location to the specified location or preferred location of ...
Definition: CGDebugInfo.h:823
Address CreateStructGEP(Address Addr, unsigned Index, const llvm::Twine &Name="")
Definition: CGBuilder.h:172
All available information about a concrete callee.
Definition: CGCall.h:62
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:129
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:258
void add(RValue rvalue, QualType type)
Definition: CGCall.h:282
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.
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.
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **callOrInvoke, bool IsMustTail, SourceLocation Loc)
EmitCall - Generate a call of the given function, expecting the given result type,...
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 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)
Address EmitVAArg(VAArgExpr *VE, Address &VAListAddr)
Generate code to get an argument from the passed in pointer and update it accordingly.
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:1247
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=std::nullopt)
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1625
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:633
LValue - This represents an lvalue references.
Definition: CGValue.h:171
bool isSimple() const
Definition: CGValue.h:266
bool isVolatileQualified() const
Definition: CGValue.h:273
void setTBAAInfo(TBAAAccessInfo Info)
Definition: CGValue.h:324
Address getAddress(CodeGenFunction &CGF) const
Definition: CGValue.h:350
QualType getType() const
Definition: CGValue.h:279
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:39
static RValue get(llvm::Value *V)
Definition: CGValue.h:89
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
Definition: CGValue.h:96
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:61
std::pair< llvm::Value *, llvm::Value * > getComplexVal() const
getComplexVal - Return the real/imag components of this complex value.
Definition: CGValue.h:68
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition: CGCall.h:356
Complex values, per C99 6.2.5p11.
Definition: Type.h:2845
QualType getElementType() const
Definition: Type.h:2855
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:4082
QualType getComputationLHSType() const
Definition: Expr.h:4116
QualType getComputationResultType() const
Definition: Expr.h:4119
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:3419
ConstantExpr - An expression that occurs in a constant context and optionally the result of evaluatin...
Definition: Expr.h:1072
Represents a 'co_yield' expression.
Definition: ExprCXX.h:5225
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1260
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition: ExprCXX.h:3436
This represents one expression.
Definition: Expr.h:110
bool isGLValue() const
Definition: Expr.h:280
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3041
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
const Expr * getSubExpr() const
Definition: Expr.h:1052
Represents a prototype with parameter type info, e.g.
Definition: Type.h:4199
Represents a C11 generic selection.
Definition: Expr.h:5719
ImaginaryLiteral - We support imaginary integer and floating point literals, like "1....
Definition: Expr.h:1712
const Expr * getSubExpr() const
Definition: Expr.h:1724
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition: Expr.h:3649
Represents an implicitly-generated value initialization of an object of a given type.
Definition: Expr.h:5594
Describes an C or C++ initializer list.
Definition: Expr.h:4841
unsigned getNumInits() const
Definition: Expr.h:4871
const Expr * getInit(unsigned Init) const
Definition: Expr.h:4887
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3182
Expr * getBase() const
Definition: Expr.h:3255
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:1168
SourceLocation getExprLoc() const LLVM_READONLY
Definition: Expr.h:1198
Expr * getSelectedExpr() const
Definition: ExprCXX.h:4411
ParenExpr - This represents a parethesized expression, e.g.
Definition: Expr.h:2129
const Expr * getSubExpr() const
Definition: Expr.h:2144
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6299
A (possibly-)qualified type.
Definition: Type.h:737
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:804
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:6902
bool UseExcessPrecision(const ASTContext &Ctx)
Definition: Type.cpp:1509
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:4377
CompoundStmt * getSubStmt()
Definition: Expr.h:4394
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:4435
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7724
bool isReferenceType() const
Definition: Type.h:7166
bool isAnyComplexType() const
Definition: Type.h:7252
bool isAtomicType() const
Definition: Type.h:7295
bool isRealFloatingType() const
Floating point categories.
Definition: Type.cpp:2203
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:2133
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7657
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition: Expr.h:2182
Expr * getSubExpr() const
Definition: Expr.h:2227
Represents a call to the builtin function __builtin_va_arg.
Definition: Expr.h:4661
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:217
bool Null(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1825
bool GE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:886
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
YAML serialization mapping.
Definition: Dominators.h:30
cl::opt< bool > EnableSingleByteCoverage
llvm::CallingConv::ID getRuntimeCC() const
static TBAAAccessInfo getMayAliasInfo()
Definition: CodeGenTBAA.h:63
Holds information about the various types of exception specification.
Definition: Type.h:4250
Extra information about a function prototype.
Definition: Type.h:4278
ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI)
Definition: Type.h:4297