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