clang  5.0.0
CGExprComplex.cpp
Go to the documentation of this file.
1 //===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code to emit Expr nodes with complex types as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
16 #include "clang/AST/StmtVisitor.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/MDBuilder.h"
21 #include "llvm/IR/Metadata.h"
22 #include <algorithm>
23 using namespace clang;
24 using namespace CodeGen;
25 
26 //===----------------------------------------------------------------------===//
27 // Complex Expression Emitter
28 //===----------------------------------------------------------------------===//
29 
31 
32 /// Return the complex type that we are meant to emit.
34  type = type.getCanonicalType();
35  if (const ComplexType *comp = dyn_cast<ComplexType>(type)) {
36  return comp;
37  } else {
38  return cast<ComplexType>(cast<AtomicType>(type)->getValueType());
39  }
40 }
41 
42 namespace {
43 class ComplexExprEmitter
44  : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
45  CodeGenFunction &CGF;
47  bool IgnoreReal;
48  bool IgnoreImag;
49 public:
50  ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
51  : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
52  }
53 
54 
55  //===--------------------------------------------------------------------===//
56  // Utilities
57  //===--------------------------------------------------------------------===//
58 
59  bool TestAndClearIgnoreReal() {
60  bool I = IgnoreReal;
61  IgnoreReal = false;
62  return I;
63  }
64  bool TestAndClearIgnoreImag() {
65  bool I = IgnoreImag;
66  IgnoreImag = false;
67  return I;
68  }
69 
70  /// EmitLoadOfLValue - Given an expression with complex type that represents a
71  /// value l-value, this method emits the address of the l-value, then loads
72  /// and returns the result.
73  ComplexPairTy EmitLoadOfLValue(const Expr *E) {
74  return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc());
75  }
76 
77  ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc);
78 
79  /// EmitStoreOfComplex - Store the specified real/imag parts into the
80  /// specified value pointer.
81  void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit);
82 
83  /// Emit a cast from complex value Val to DestType.
84  ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
85  QualType DestType, SourceLocation Loc);
86  /// Emit a cast from scalar value Val to DestType.
87  ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType,
88  QualType DestType, SourceLocation Loc);
89 
90  //===--------------------------------------------------------------------===//
91  // Visitor Methods
92  //===--------------------------------------------------------------------===//
93 
94  ComplexPairTy Visit(Expr *E) {
95  ApplyDebugLocation DL(CGF, E);
97  }
98 
99  ComplexPairTy VisitStmt(Stmt *S) {
100  S->dump(CGF.getContext().getSourceManager());
101  llvm_unreachable("Stmt can't have complex result type!");
102  }
103  ComplexPairTy VisitExpr(Expr *S);
104  ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
105  ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
106  return Visit(GE->getResultExpr());
107  }
108  ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
110  VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
111  return Visit(PE->getReplacement());
112  }
113  ComplexPairTy VisitCoawaitExpr(CoawaitExpr *S) {
114  return CGF.EmitCoawaitExpr(*S).getComplexVal();
115  }
116  ComplexPairTy VisitCoyieldExpr(CoyieldExpr *S) {
117  return CGF.EmitCoyieldExpr(*S).getComplexVal();
118  }
119  ComplexPairTy VisitUnaryCoawait(const UnaryOperator *E) {
120  return Visit(E->getSubExpr());
121  }
122 
123 
124  // l-values.
125  ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
126  if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) {
127  if (result.isReference())
128  return EmitLoadOfLValue(result.getReferenceLValue(CGF, E),
129  E->getExprLoc());
130 
131  llvm::Constant *pair = result.getValue();
132  return ComplexPairTy(pair->getAggregateElement(0U),
133  pair->getAggregateElement(1U));
134  }
135  return EmitLoadOfLValue(E);
136  }
137  ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
138  return EmitLoadOfLValue(E);
139  }
140  ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
141  return CGF.EmitObjCMessageExpr(E).getComplexVal();
142  }
143  ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
144  ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
145  ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
146  if (E->isGLValue())
147  return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->getExprLoc());
148  return CGF.getOpaqueRValueMapping(E).getComplexVal();
149  }
150 
151  ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
152  return CGF.EmitPseudoObjectRValue(E).getComplexVal();
153  }
154 
155  // FIXME: CompoundLiteralExpr
156 
157  ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy);
158  ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
159  // Unlike for scalars, we don't have to worry about function->ptr demotion
160  // here.
161  return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
162  }
163  ComplexPairTy VisitCastExpr(CastExpr *E) {
164  if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
165  CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
166  return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
167  }
168  ComplexPairTy VisitCallExpr(const CallExpr *E);
169  ComplexPairTy VisitStmtExpr(const StmtExpr *E);
170 
171  // Operators.
172  ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
173  bool isInc, bool isPre) {
174  LValue LV = CGF.EmitLValue(E->getSubExpr());
175  return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
176  }
177  ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
178  return VisitPrePostIncDec(E, false, false);
179  }
180  ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
181  return VisitPrePostIncDec(E, true, false);
182  }
183  ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
184  return VisitPrePostIncDec(E, false, true);
185  }
186  ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
187  return VisitPrePostIncDec(E, true, true);
188  }
189  ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
190  ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
191  TestAndClearIgnoreReal();
192  TestAndClearIgnoreImag();
193  return Visit(E->getSubExpr());
194  }
195  ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
196  ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
197  // LNot,Real,Imag never return complex.
198  ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
199  return Visit(E->getSubExpr());
200  }
201  ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
202  return Visit(DAE->getExpr());
203  }
204  ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
206  return Visit(DIE->getExpr());
207  }
208  ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
209  CGF.enterFullExpression(E);
211  ComplexPairTy Vals = Visit(E->getSubExpr());
212  // Defend against dominance problems caused by jumps out of expression
213  // evaluation through the shared cleanup block.
214  Scope.ForceCleanup({&Vals.first, &Vals.second});
215  return Vals;
216  }
217  ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
218  assert(E->getType()->isAnyComplexType() && "Expected complex type!");
219  QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
220  llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
221  return ComplexPairTy(Null, Null);
222  }
223  ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
224  assert(E->getType()->isAnyComplexType() && "Expected complex type!");
225  QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
226  llvm::Constant *Null =
227  llvm::Constant::getNullValue(CGF.ConvertType(Elem));
228  return ComplexPairTy(Null, Null);
229  }
230 
231  struct BinOpInfo {
232  ComplexPairTy LHS;
233  ComplexPairTy RHS;
234  QualType Ty; // Computation Type.
235  };
236 
237  BinOpInfo EmitBinOps(const BinaryOperator *E);
238  LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
239  ComplexPairTy (ComplexExprEmitter::*Func)
240  (const BinOpInfo &),
241  RValue &Val);
242  ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
243  ComplexPairTy (ComplexExprEmitter::*Func)
244  (const BinOpInfo &));
245 
246  ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
247  ComplexPairTy EmitBinSub(const BinOpInfo &Op);
248  ComplexPairTy EmitBinMul(const BinOpInfo &Op);
249  ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
250 
251  ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
252  const BinOpInfo &Op);
253 
254  ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
255  return EmitBinAdd(EmitBinOps(E));
256  }
257  ComplexPairTy VisitBinSub(const BinaryOperator *E) {
258  return EmitBinSub(EmitBinOps(E));
259  }
260  ComplexPairTy VisitBinMul(const BinaryOperator *E) {
261  return EmitBinMul(EmitBinOps(E));
262  }
263  ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
264  return EmitBinDiv(EmitBinOps(E));
265  }
266 
267  // Compound assignments.
268  ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
269  return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
270  }
271  ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
272  return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
273  }
274  ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
275  return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
276  }
277  ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
278  return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
279  }
280 
281  // GCC rejects rem/and/or/xor for integer complex.
282  // Logical and/or always return int, never complex.
283 
284  // No comparisons produce a complex result.
285 
286  LValue EmitBinAssignLValue(const BinaryOperator *E,
287  ComplexPairTy &Val);
288  ComplexPairTy VisitBinAssign (const BinaryOperator *E);
289  ComplexPairTy VisitBinComma (const BinaryOperator *E);
290 
291 
293  VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
294  ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
295 
296  ComplexPairTy VisitInitListExpr(InitListExpr *E);
297 
298  ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
299  return EmitLoadOfLValue(E);
300  }
301 
302  ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
303 
304  ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
305  return CGF.EmitAtomicExpr(E).getComplexVal();
306  }
307 };
308 } // end anonymous namespace.
309 
310 //===----------------------------------------------------------------------===//
311 // Utilities
312 //===----------------------------------------------------------------------===//
313 
315  QualType complexType) {
316  CharUnits offset = CharUnits::Zero();
317  return Builder.CreateStructGEP(addr, 0, offset, addr.getName() + ".realp");
318 }
319 
321  QualType complexType) {
322  QualType eltType = complexType->castAs<ComplexType>()->getElementType();
323  CharUnits offset = getContext().getTypeSizeInChars(eltType);
324  return Builder.CreateStructGEP(addr, 1, offset, addr.getName() + ".imagp");
325 }
326 
327 /// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
328 /// load the real and imaginary pieces, returning them as Real/Imag.
329 ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
330  SourceLocation loc) {
331  assert(lvalue.isSimple() && "non-simple complex l-value?");
332  if (lvalue.getType()->isAtomicType())
333  return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
334 
335  Address SrcPtr = lvalue.getAddress();
336  bool isVolatile = lvalue.isVolatileQualified();
337 
338  llvm::Value *Real = nullptr, *Imag = nullptr;
339 
340  if (!IgnoreReal || isVolatile) {
341  Address RealP = CGF.emitAddrOfRealComponent(SrcPtr, lvalue.getType());
342  Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr.getName() + ".real");
343  }
344 
345  if (!IgnoreImag || isVolatile) {
346  Address ImagP = CGF.emitAddrOfImagComponent(SrcPtr, lvalue.getType());
347  Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr.getName() + ".imag");
348  }
349 
350  return ComplexPairTy(Real, Imag);
351 }
352 
353 /// EmitStoreOfComplex - Store the specified real/imag parts into the
354 /// specified value pointer.
355 void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
356  bool isInit) {
357  if (lvalue.getType()->isAtomicType() ||
358  (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue)))
359  return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
360 
361  Address Ptr = lvalue.getAddress();
362  Address RealPtr = CGF.emitAddrOfRealComponent(Ptr, lvalue.getType());
363  Address ImagPtr = CGF.emitAddrOfImagComponent(Ptr, lvalue.getType());
364 
365  Builder.CreateStore(Val.first, RealPtr, lvalue.isVolatileQualified());
366  Builder.CreateStore(Val.second, ImagPtr, lvalue.isVolatileQualified());
367 }
368 
369 
370 
371 //===----------------------------------------------------------------------===//
372 // Visitor Methods
373 //===----------------------------------------------------------------------===//
374 
375 ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
376  CGF.ErrorUnsupported(E, "complex expression");
377  llvm::Type *EltTy =
378  CGF.ConvertType(getComplexType(E->getType())->getElementType());
379  llvm::Value *U = llvm::UndefValue::get(EltTy);
380  return ComplexPairTy(U, U);
381 }
382 
383 ComplexPairTy ComplexExprEmitter::
384 VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
385  llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
386  return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
387 }
388 
389 
390 ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
391  if (E->getCallReturnType(CGF.getContext())->isReferenceType())
392  return EmitLoadOfLValue(E);
393 
394  return CGF.EmitCallExpr(E).getComplexVal();
395 }
396 
397 ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
399  Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
400  assert(RetAlloca.isValid() && "Expected complex return value");
401  return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
402  E->getExprLoc());
403 }
404 
405 /// Emit a cast from complex value Val to DestType.
406 ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
407  QualType SrcType,
408  QualType DestType,
409  SourceLocation Loc) {
410  // Get the src/dest element type.
411  SrcType = SrcType->castAs<ComplexType>()->getElementType();
412  DestType = DestType->castAs<ComplexType>()->getElementType();
413 
414  // C99 6.3.1.6: When a value of complex type is converted to another
415  // complex type, both the real and imaginary parts follow the conversion
416  // rules for the corresponding real types.
417  Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType, Loc);
418  Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType, Loc);
419  return Val;
420 }
421 
422 ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
423  QualType SrcType,
424  QualType DestType,
425  SourceLocation Loc) {
426  // Convert the input element to the element type of the complex.
427  DestType = DestType->castAs<ComplexType>()->getElementType();
428  Val = CGF.EmitScalarConversion(Val, SrcType, DestType, Loc);
429 
430  // Return (realval, 0).
431  return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
432 }
433 
434 ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
435  QualType DestTy) {
436  switch (CK) {
437  case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
438 
439  // Atomic to non-atomic casts may be more than a no-op for some platforms and
440  // for some types.
441  case CK_AtomicToNonAtomic:
442  case CK_NonAtomicToAtomic:
443  case CK_NoOp:
444  case CK_LValueToRValue:
445  case CK_UserDefinedConversion:
446  return Visit(Op);
447 
448  case CK_LValueBitCast: {
449  LValue origLV = CGF.EmitLValue(Op);
450  Address V = origLV.getAddress();
451  V = Builder.CreateElementBitCast(V, CGF.ConvertType(DestTy));
452  return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), Op->getExprLoc());
453  }
454 
455  case CK_BitCast:
456  case CK_BaseToDerived:
457  case CK_DerivedToBase:
458  case CK_UncheckedDerivedToBase:
459  case CK_Dynamic:
460  case CK_ToUnion:
461  case CK_ArrayToPointerDecay:
462  case CK_FunctionToPointerDecay:
463  case CK_NullToPointer:
464  case CK_NullToMemberPointer:
465  case CK_BaseToDerivedMemberPointer:
466  case CK_DerivedToBaseMemberPointer:
467  case CK_MemberPointerToBoolean:
468  case CK_ReinterpretMemberPointer:
469  case CK_ConstructorConversion:
470  case CK_IntegralToPointer:
471  case CK_PointerToIntegral:
472  case CK_PointerToBoolean:
473  case CK_ToVoid:
474  case CK_VectorSplat:
475  case CK_IntegralCast:
476  case CK_BooleanToSignedIntegral:
477  case CK_IntegralToBoolean:
478  case CK_IntegralToFloating:
479  case CK_FloatingToIntegral:
480  case CK_FloatingToBoolean:
481  case CK_FloatingCast:
482  case CK_CPointerToObjCPointerCast:
483  case CK_BlockPointerToObjCPointerCast:
484  case CK_AnyPointerToBlockPointerCast:
485  case CK_ObjCObjectLValueCast:
486  case CK_FloatingComplexToReal:
487  case CK_FloatingComplexToBoolean:
488  case CK_IntegralComplexToReal:
489  case CK_IntegralComplexToBoolean:
490  case CK_ARCProduceObject:
491  case CK_ARCConsumeObject:
492  case CK_ARCReclaimReturnedObject:
493  case CK_ARCExtendBlockObject:
494  case CK_CopyAndAutoreleaseBlockObject:
495  case CK_BuiltinFnToFnPtr:
496  case CK_ZeroToOCLEvent:
497  case CK_ZeroToOCLQueue:
498  case CK_AddressSpaceConversion:
499  case CK_IntToOCLSampler:
500  llvm_unreachable("invalid cast kind for complex value");
501 
502  case CK_FloatingRealToComplex:
503  case CK_IntegralRealToComplex:
504  return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op), Op->getType(),
505  DestTy, Op->getExprLoc());
506 
507  case CK_FloatingComplexCast:
508  case CK_FloatingComplexToIntegralComplex:
509  case CK_IntegralComplexCast:
510  case CK_IntegralComplexToFloatingComplex:
511  return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy,
512  Op->getExprLoc());
513  }
514 
515  llvm_unreachable("unknown cast resulting in complex value");
516 }
517 
518 ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
519  TestAndClearIgnoreReal();
520  TestAndClearIgnoreImag();
521  ComplexPairTy Op = Visit(E->getSubExpr());
522 
523  llvm::Value *ResR, *ResI;
524  if (Op.first->getType()->isFloatingPointTy()) {
525  ResR = Builder.CreateFNeg(Op.first, "neg.r");
526  ResI = Builder.CreateFNeg(Op.second, "neg.i");
527  } else {
528  ResR = Builder.CreateNeg(Op.first, "neg.r");
529  ResI = Builder.CreateNeg(Op.second, "neg.i");
530  }
531  return ComplexPairTy(ResR, ResI);
532 }
533 
534 ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
535  TestAndClearIgnoreReal();
536  TestAndClearIgnoreImag();
537  // ~(a+ib) = a + i*-b
538  ComplexPairTy Op = Visit(E->getSubExpr());
539  llvm::Value *ResI;
540  if (Op.second->getType()->isFloatingPointTy())
541  ResI = Builder.CreateFNeg(Op.second, "conj.i");
542  else
543  ResI = Builder.CreateNeg(Op.second, "conj.i");
544 
545  return ComplexPairTy(Op.first, ResI);
546 }
547 
548 ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
549  llvm::Value *ResR, *ResI;
550 
551  if (Op.LHS.first->getType()->isFloatingPointTy()) {
552  ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
553  if (Op.LHS.second && Op.RHS.second)
554  ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
555  else
556  ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
557  assert(ResI && "Only one operand may be real!");
558  } else {
559  ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
560  assert(Op.LHS.second && Op.RHS.second &&
561  "Both operands of integer complex operators must be complex!");
562  ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
563  }
564  return ComplexPairTy(ResR, ResI);
565 }
566 
567 ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
568  llvm::Value *ResR, *ResI;
569  if (Op.LHS.first->getType()->isFloatingPointTy()) {
570  ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
571  if (Op.LHS.second && Op.RHS.second)
572  ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
573  else
574  ResI = Op.LHS.second ? Op.LHS.second
575  : Builder.CreateFNeg(Op.RHS.second, "sub.i");
576  assert(ResI && "Only one operand may be real!");
577  } else {
578  ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
579  assert(Op.LHS.second && Op.RHS.second &&
580  "Both operands of integer complex operators must be complex!");
581  ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
582  }
583  return ComplexPairTy(ResR, ResI);
584 }
585 
586 /// \brief Emit a libcall for a binary operation on complex types.
587 ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
588  const BinOpInfo &Op) {
589  CallArgList Args;
590  Args.add(RValue::get(Op.LHS.first),
591  Op.Ty->castAs<ComplexType>()->getElementType());
592  Args.add(RValue::get(Op.LHS.second),
593  Op.Ty->castAs<ComplexType>()->getElementType());
594  Args.add(RValue::get(Op.RHS.first),
595  Op.Ty->castAs<ComplexType>()->getElementType());
596  Args.add(RValue::get(Op.RHS.second),
597  Op.Ty->castAs<ComplexType>()->getElementType());
598 
599  // We *must* use the full CG function call building logic here because the
600  // complex type has special ABI handling. We also should not forget about
601  // special calling convention which may be used for compiler builtins.
602 
603  // We create a function qualified type to state that this call does not have
604  // any exceptions.
606  EPI = EPI.withExceptionSpec(
608  SmallVector<QualType, 4> ArgsQTys(
609  4, Op.Ty->castAs<ComplexType>()->getElementType());
610  QualType FQTy = CGF.getContext().getFunctionType(Op.Ty, ArgsQTys, EPI);
611  const CGFunctionInfo &FuncInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall(
612  Args, cast<FunctionType>(FQTy.getTypePtr()), false);
613 
614  llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
615  llvm::Constant *Func = CGF.CGM.CreateBuiltinFunction(FTy, LibCallName);
616  CGCallee Callee = CGCallee::forDirect(Func, FQTy->getAs<FunctionProtoType>());
617 
618  llvm::Instruction *Call;
619  RValue Res = CGF.EmitCall(FuncInfo, Callee, ReturnValueSlot(), Args, &Call);
620  cast<llvm::CallInst>(Call)->setCallingConv(CGF.CGM.getBuiltinCC());
621  return Res.getComplexVal();
622 }
623 
624 /// \brief Lookup the libcall name for a given floating point type complex
625 /// multiply.
627  switch (Ty->getTypeID()) {
628  default:
629  llvm_unreachable("Unsupported floating point type!");
630  case llvm::Type::HalfTyID:
631  return "__mulhc3";
632  case llvm::Type::FloatTyID:
633  return "__mulsc3";
634  case llvm::Type::DoubleTyID:
635  return "__muldc3";
636  case llvm::Type::PPC_FP128TyID:
637  return "__multc3";
638  case llvm::Type::X86_FP80TyID:
639  return "__mulxc3";
640  case llvm::Type::FP128TyID:
641  return "__multc3";
642  }
643 }
644 
645 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
646 // typed values.
647 ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
648  using llvm::Value;
649  Value *ResR, *ResI;
650  llvm::MDBuilder MDHelper(CGF.getLLVMContext());
651 
652  if (Op.LHS.first->getType()->isFloatingPointTy()) {
653  // The general formulation is:
654  // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
655  //
656  // But we can fold away components which would be zero due to a real
657  // operand according to C11 Annex G.5.1p2.
658  // FIXME: C11 also provides for imaginary types which would allow folding
659  // still more of this within the type system.
660 
661  if (Op.LHS.second && Op.RHS.second) {
662  // If both operands are complex, emit the core math directly, and then
663  // test for NaNs. If we find NaNs in the result, we delegate to a libcall
664  // to carefully re-compute the correct infinity representation if
665  // possible. The expectation is that the presence of NaNs here is
666  // *extremely* rare, and so the cost of the libcall is almost irrelevant.
667  // This is good, because the libcall re-computes the core multiplication
668  // exactly the same as we do here and re-tests for NaNs in order to be
669  // a generic complex*complex libcall.
670 
671  // First compute the four products.
672  Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
673  Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
674  Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
675  Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
676 
677  // The real part is the difference of the first two, the imaginary part is
678  // the sum of the second.
679  ResR = Builder.CreateFSub(AC, BD, "mul_r");
680  ResI = Builder.CreateFAdd(AD, BC, "mul_i");
681 
682  // Emit the test for the real part becoming NaN and create a branch to
683  // handle it. We test for NaN by comparing the number to itself.
684  Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
685  llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
686  llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
687  llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
688  llvm::BasicBlock *OrigBB = Branch->getParent();
689 
690  // Give hint that we very much don't expect to see NaNs.
691  // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
692  llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1);
693  Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
694 
695  // Now test the imaginary part and create its branch.
696  CGF.EmitBlock(INaNBB);
697  Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
698  llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
699  Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
700  Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
701 
702  // Now emit the libcall on this slowest of the slow paths.
703  CGF.EmitBlock(LibCallBB);
704  Value *LibCallR, *LibCallI;
705  std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
706  getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
707  Builder.CreateBr(ContBB);
708 
709  // Finally continue execution by phi-ing together the different
710  // computation paths.
711  CGF.EmitBlock(ContBB);
712  llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
713  RealPHI->addIncoming(ResR, OrigBB);
714  RealPHI->addIncoming(ResR, INaNBB);
715  RealPHI->addIncoming(LibCallR, LibCallBB);
716  llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
717  ImagPHI->addIncoming(ResI, OrigBB);
718  ImagPHI->addIncoming(ResI, INaNBB);
719  ImagPHI->addIncoming(LibCallI, LibCallBB);
720  return ComplexPairTy(RealPHI, ImagPHI);
721  }
722  assert((Op.LHS.second || Op.RHS.second) &&
723  "At least one operand must be complex!");
724 
725  // If either of the operands is a real rather than a complex, the
726  // imaginary component is ignored when computing the real component of the
727  // result.
728  ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
729 
730  ResI = Op.LHS.second
731  ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
732  : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
733  } else {
734  assert(Op.LHS.second && Op.RHS.second &&
735  "Both operands of integer complex operators must be complex!");
736  Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
737  Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
738  ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
739 
740  Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
741  Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
742  ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
743  }
744  return ComplexPairTy(ResR, ResI);
745 }
746 
747 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
748 // typed values.
749 ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
750  llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
751  llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
752 
753 
754  llvm::Value *DSTr, *DSTi;
755  if (LHSr->getType()->isFloatingPointTy()) {
756  // If we have a complex operand on the RHS, we delegate to a libcall to
757  // handle all of the complexities and minimize underflow/overflow cases.
758  //
759  // FIXME: We would be able to avoid the libcall in many places if we
760  // supported imaginary types in addition to complex types.
761  if (RHSi) {
762  BinOpInfo LibCallOp = Op;
763  // If LHS was a real, supply a null imaginary part.
764  if (!LHSi)
765  LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
766 
767  StringRef LibCallName;
768  switch (LHSr->getType()->getTypeID()) {
769  default:
770  llvm_unreachable("Unsupported floating point type!");
771  case llvm::Type::HalfTyID:
772  return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
773  case llvm::Type::FloatTyID:
774  return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
775  case llvm::Type::DoubleTyID:
776  return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
777  case llvm::Type::PPC_FP128TyID:
778  return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
779  case llvm::Type::X86_FP80TyID:
780  return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
781  case llvm::Type::FP128TyID:
782  return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
783  }
784  }
785  assert(LHSi && "Can have at most one non-complex operand!");
786 
787  DSTr = Builder.CreateFDiv(LHSr, RHSr);
788  DSTi = Builder.CreateFDiv(LHSi, RHSr);
789  } else {
790  assert(Op.LHS.second && Op.RHS.second &&
791  "Both operands of integer complex operators must be complex!");
792  // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
793  llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
794  llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
795  llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
796 
797  llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
798  llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
799  llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
800 
801  llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
802  llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
803  llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
804 
805  if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
806  DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
807  DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
808  } else {
809  DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
810  DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
811  }
812  }
813 
814  return ComplexPairTy(DSTr, DSTi);
815 }
816 
817 ComplexExprEmitter::BinOpInfo
818 ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
819  TestAndClearIgnoreReal();
820  TestAndClearIgnoreImag();
821  BinOpInfo Ops;
822  if (E->getLHS()->getType()->isRealFloatingType())
823  Ops.LHS = ComplexPairTy(CGF.EmitScalarExpr(E->getLHS()), nullptr);
824  else
825  Ops.LHS = Visit(E->getLHS());
826  if (E->getRHS()->getType()->isRealFloatingType())
827  Ops.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
828  else
829  Ops.RHS = Visit(E->getRHS());
830 
831  Ops.Ty = E->getType();
832  return Ops;
833 }
834 
835 
836 LValue ComplexExprEmitter::
837 EmitCompoundAssignLValue(const CompoundAssignOperator *E,
838  ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
839  RValue &Val) {
840  TestAndClearIgnoreReal();
841  TestAndClearIgnoreImag();
842  QualType LHSTy = E->getLHS()->getType();
843  if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
844  LHSTy = AT->getValueType();
845 
846  BinOpInfo OpInfo;
847 
848  // Load the RHS and LHS operands.
849  // __block variables need to have the rhs evaluated first, plus this should
850  // improve codegen a little.
851  OpInfo.Ty = E->getComputationResultType();
852  QualType ComplexElementTy = cast<ComplexType>(OpInfo.Ty)->getElementType();
853 
854  // The RHS should have been converted to the computation type.
855  if (E->getRHS()->getType()->isRealFloatingType()) {
856  assert(
857  CGF.getContext()
858  .hasSameUnqualifiedType(ComplexElementTy, E->getRHS()->getType()));
859  OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
860  } else {
861  assert(CGF.getContext()
862  .hasSameUnqualifiedType(OpInfo.Ty, E->getRHS()->getType()));
863  OpInfo.RHS = Visit(E->getRHS());
864  }
865 
866  LValue LHS = CGF.EmitLValue(E->getLHS());
867 
868  // Load from the l-value and convert it.
869  SourceLocation Loc = E->getExprLoc();
870  if (LHSTy->isAnyComplexType()) {
871  ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
872  OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
873  } else {
874  llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, Loc);
875  // For floating point real operands we can directly pass the scalar form
876  // to the binary operator emission and potentially get more efficient code.
877  if (LHSTy->isRealFloatingType()) {
878  if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
879  LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy, Loc);
880  OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
881  } else {
882  OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc);
883  }
884  }
885 
886  // Expand the binary operator.
887  ComplexPairTy Result = (this->*Func)(OpInfo);
888 
889  // Truncate the result and store it into the LHS lvalue.
890  if (LHSTy->isAnyComplexType()) {
891  ComplexPairTy ResVal =
892  EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy, Loc);
893  EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
894  Val = RValue::getComplex(ResVal);
895  } else {
896  llvm::Value *ResVal =
897  CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy, Loc);
898  CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
899  Val = RValue::get(ResVal);
900  }
901 
902  return LHS;
903 }
904 
905 // Compound assignments.
906 ComplexPairTy ComplexExprEmitter::
907 EmitCompoundAssign(const CompoundAssignOperator *E,
908  ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
909  RValue Val;
910  LValue LV = EmitCompoundAssignLValue(E, Func, Val);
911 
912  // The result of an assignment in C is the assigned r-value.
913  if (!CGF.getLangOpts().CPlusPlus)
914  return Val.getComplexVal();
915 
916  // If the lvalue is non-volatile, return the computed value of the assignment.
917  if (!LV.isVolatileQualified())
918  return Val.getComplexVal();
919 
920  return EmitLoadOfLValue(LV, E->getExprLoc());
921 }
922 
923 LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
924  ComplexPairTy &Val) {
925  assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
926  E->getRHS()->getType()) &&
927  "Invalid assignment");
928  TestAndClearIgnoreReal();
929  TestAndClearIgnoreImag();
930 
931  // Emit the RHS. __block variables need the RHS evaluated first.
932  Val = Visit(E->getRHS());
933 
934  // Compute the address to store into.
935  LValue LHS = CGF.EmitLValue(E->getLHS());
936 
937  // Store the result value into the LHS lvalue.
938  EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
939 
940  return LHS;
941 }
942 
943 ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
944  ComplexPairTy Val;
945  LValue LV = EmitBinAssignLValue(E, Val);
946 
947  // The result of an assignment in C is the assigned r-value.
948  if (!CGF.getLangOpts().CPlusPlus)
949  return Val;
950 
951  // If the lvalue is non-volatile, return the computed value of the assignment.
952  if (!LV.isVolatileQualified())
953  return Val;
954 
955  return EmitLoadOfLValue(LV, E->getExprLoc());
956 }
957 
958 ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
959  CGF.EmitIgnoredExpr(E->getLHS());
960  return Visit(E->getRHS());
961 }
962 
963 ComplexPairTy ComplexExprEmitter::
964 VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
965  TestAndClearIgnoreReal();
966  TestAndClearIgnoreImag();
967  llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
968  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
969  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
970 
971  // Bind the common expression if necessary.
972  CodeGenFunction::OpaqueValueMapping binding(CGF, E);
973 
974 
976  CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
977  CGF.getProfileCount(E));
978 
979  eval.begin(CGF);
980  CGF.EmitBlock(LHSBlock);
981  CGF.incrementProfileCounter(E);
982  ComplexPairTy LHS = Visit(E->getTrueExpr());
983  LHSBlock = Builder.GetInsertBlock();
984  CGF.EmitBranch(ContBlock);
985  eval.end(CGF);
986 
987  eval.begin(CGF);
988  CGF.EmitBlock(RHSBlock);
989  ComplexPairTy RHS = Visit(E->getFalseExpr());
990  RHSBlock = Builder.GetInsertBlock();
991  CGF.EmitBlock(ContBlock);
992  eval.end(CGF);
993 
994  // Create a PHI node for the real part.
995  llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
996  RealPN->addIncoming(LHS.first, LHSBlock);
997  RealPN->addIncoming(RHS.first, RHSBlock);
998 
999  // Create a PHI node for the imaginary part.
1000  llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
1001  ImagPN->addIncoming(LHS.second, LHSBlock);
1002  ImagPN->addIncoming(RHS.second, RHSBlock);
1003 
1004  return ComplexPairTy(RealPN, ImagPN);
1005 }
1006 
1007 ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
1008  return Visit(E->getChosenSubExpr());
1009 }
1010 
1011 ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
1012  bool Ignore = TestAndClearIgnoreReal();
1013  (void)Ignore;
1014  assert (Ignore == false && "init list ignored");
1015  Ignore = TestAndClearIgnoreImag();
1016  (void)Ignore;
1017  assert (Ignore == false && "init list ignored");
1018 
1019  if (E->getNumInits() == 2) {
1020  llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
1021  llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
1022  return ComplexPairTy(Real, Imag);
1023  } else if (E->getNumInits() == 1) {
1024  return Visit(E->getInit(0));
1025  }
1026 
1027  // Empty init list intializes to null
1028  assert(E->getNumInits() == 0 && "Unexpected number of inits");
1029  QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
1030  llvm::Type* LTy = CGF.ConvertType(Ty);
1031  llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
1032  return ComplexPairTy(zeroConstant, zeroConstant);
1033 }
1034 
1035 ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
1036  Address ArgValue = Address::invalid();
1037  Address ArgPtr = CGF.EmitVAArg(E, ArgValue);
1038 
1039  if (!ArgPtr.isValid()) {
1040  CGF.ErrorUnsupported(E, "complex va_arg expression");
1041  llvm::Type *EltTy =
1042  CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
1043  llvm::Value *U = llvm::UndefValue::get(EltTy);
1044  return ComplexPairTy(U, U);
1045  }
1046 
1047  return EmitLoadOfLValue(CGF.MakeAddrLValue(ArgPtr, E->getType()),
1048  E->getExprLoc());
1049 }
1050 
1051 //===----------------------------------------------------------------------===//
1052 // Entry Point into this File
1053 //===----------------------------------------------------------------------===//
1054 
1055 /// EmitComplexExpr - Emit the computation of the specified expression of
1056 /// complex type, ignoring the result.
1058  bool IgnoreImag) {
1059  assert(E && getComplexType(E->getType()) &&
1060  "Invalid complex expression to emit");
1061 
1062  return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
1063  .Visit(const_cast<Expr *>(E));
1064 }
1065 
1067  bool isInit) {
1068  assert(E && getComplexType(E->getType()) &&
1069  "Invalid complex expression to emit");
1070  ComplexExprEmitter Emitter(*this);
1071  ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
1072  Emitter.EmitStoreOfComplex(Val, dest, isInit);
1073 }
1074 
1075 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
1077  bool isInit) {
1078  ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
1079 }
1080 
1081 /// EmitLoadOfComplex - Load a complex number from the specified address.
1083  SourceLocation loc) {
1084  return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
1085 }
1086 
1088  assert(E->getOpcode() == BO_Assign);
1089  ComplexPairTy Val; // ignored
1090  return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1091 }
1092 
1093 typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1094  const ComplexExprEmitter::BinOpInfo &);
1095 
1097  switch (Op) {
1098  case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1099  case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1100  case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1101  case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
1102  default:
1103  llvm_unreachable("unexpected complex compound assignment");
1104  }
1105 }
1106 
1109  CompoundFunc Op = getComplexOp(E->getOpcode());
1110  RValue Val;
1111  return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1112 }
1113 
1116  llvm::Value *&Result) {
1117  CompoundFunc Op = getComplexOp(E->getOpcode());
1118  RValue Val;
1119  LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1120  Result = Val.getScalarVal();
1121  return Ret;
1122 }
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:281
unsigned getNumInits() const
Definition: Expr.h:3878
CastKind getCastKind() const
Definition: Expr.h:2749
LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E)
A (possibly-)qualified type.
Definition: Type.h:616
CompoundStmt * getSubStmt()
Definition: Expr.h:3480
Stmt - This represents one statement.
Definition: Stmt.h:60
std::pair< llvm::Value *, llvm::Value * > getComplexVal() const
getComplexVal - Return the real/imag components of this complex value.
Definition: CGValue.h:65
Address getAddress() const
Definition: CGValue.h:346
static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty)
Lookup the libcall name for a given floating point type complex multiply.
ParenExpr - This represents a parethesized expression, e.g.
Definition: Expr.h:1662
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
EmitComplexExprIntoLValue - Emit the given expression of complex type and place its result into the s...
const Expr * getResultExpr() const
The generic selection's result expression.
Definition: Expr.h:4729
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:2628
Extra information about a function prototype.
Definition: Type.h:3234
LValue EmitComplexAssignmentLValue(const BinaryOperator *E)
Emit an l-value for an assignment (simple or compound) of complex type.
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition: ExprCXX.h:2920
Address emitAddrOfImagComponent(Address complex, QualType complexType)
An object to manage conditionally-evaluated expressions.
LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E, llvm::Value *&Result)
bool isVolatileQualified() const
Definition: CGValue.h:271
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
Expr * getSubExpr()
Definition: Expr.h:2753
Expr * getLHS() const
Definition: Expr.h:3011
ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc)
EmitLoadOfComplex - Load a complex number from the specified l-value.
Describes an C or C++ initializer list.
Definition: Expr.h:3848
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition: Expr.h:3681
Address emitAddrOfRealComponent(Address complex, QualType complexType)
BinaryOperatorKind
Expr * getTrueExpr() const
Definition: Expr.h:3407
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2967
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:1784
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:39
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:2701
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
bool isValid() const
Definition: Address.h:36
detail::InMemoryDirectory::const_iterator I
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:982
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3129
CastKind
CastKind - The kind of operation required for a conversion.
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:38
const Expr * getExpr() const
Get the initialization expression that will be used.
Definition: ExprCXX.h:1077
Represents a call to the builtin function __builtin_va_arg.
Definition: Expr.h:3754
bool isRealFloatingType() const
Floating point categories.
Definition: Type.cpp:1837
An expression "T()" which creates a value-initialized rvalue of type T, which is a non-class type...
Definition: ExprCXX.h:1740
Expr - This represents one expression.
Definition: Expr.h:105
static Address invalid()
Definition: Address.h:35
bool isAnyComplexType() const
Definition: Type.h:5775
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:125
bool isAtomicType() const
Definition: Type.h:5794
ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &O)
Definition: Type.h:3243
void add(RValue rvalue, QualType type, bool needscopy=false)
Definition: CGCall.h:207
An RAII object to record that we're evaluating a statement expression.
void dump() const
Dumps the specified AST fragment and all subtrees to llvm::errs().
Definition: ASTDumper.cpp:2584
Expr * getSubExpr() const
Definition: Expr.h:1741
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:860
UnaryOperator - This represents the unary-expression's (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Definition: Expr.h:1714
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition: ExprCXX.h:3751
bool isGLValue() const
Definition: Expr.h:251
ImaginaryLiteral - We support imaginary integer and floating point literals, like "1...
Definition: Expr.h:1460
QualType getComputationResultType() const
Definition: Expr.h:3193
The l-value was considered opaque, so the alignment was determined from a type.
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class...
Definition: Expr.h:865
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:4938
bool isSimple() const
Definition: CGValue.h:265
ASTContext & getContext() const
Encodes a location in the source.
QualType getElementType() const
Definition: Type.h:2176
A scoped helper to set the current debug location to the specified location or preferred location of ...
Definition: CGDebugInfo.h:617
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:178
static const ComplexType * getComplexType(QualType type)
Return the complex type that we are meant to emit.
AtomicExpr - Variadic atomic builtins: __atomic_exchange, __atomic_fetch_*, __atomic_load, __atomic_store, and __atomic_compare_exchange_*, for the similarly-named C++11 instructions, and __c11 variants for <stdatomic.h>.
Definition: Expr.h:5070
An aligned address.
Definition: Address.h:25
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition: Expr.h:2804
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6105
All available information about a concrete callee.
Definition: CGCall.h:66
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:3464
llvm::StringRef getName() const
Return the IR name of the pointer value.
Definition: Address.h:62
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:216
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:3167
Represents a C11 generic selection.
Definition: Expr.h:4653
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
Definition: Expr.cpp:1307
QualType getType() const
Definition: Expr.h:127
CGFunctionInfo - Class to encapsulate the information about a function definition.
SourceLocation getExprLoc() const LLVM_READONLY
Definition: Expr.h:902
static CompoundFunc getComplexOp(BinaryOperatorKind Op)
const Expr * getExpr() const
Definition: ExprCXX.h:1013
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
Definition: CGValue.h:92
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchers.h:2126
CodeGenFunction::ComplexPairTy ComplexPairTy
Address CreateStructGEP(Address Addr, unsigned Index, CharUnits Offset, const llvm::Twine &Name="")
Definition: CGBuilder.h:165
Represents a 'co_yield' expression.
Definition: ExprCXX.h:4276
detail::InMemoryDirectory::const_iterator E
Complex values, per C99 6.2.5p11.
Definition: Type.h:2164
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:6042
Expr * getFalseExpr() const
Definition: Expr.h:3413
QualType getCanonicalType() const
Definition: Type.h:5528
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
Definition: Expr.h:3203
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
Represents a 'co_await' expression.
Definition: ExprCXX.h:4199
const Expr * getSubExpr() const
Definition: Expr.h:1472
ComplexPairTy(ComplexExprEmitter::* CompoundFunc)(const ComplexExprEmitter::BinOpInfo &)
SourceLocation getExprLoc() const LLVM_READONLY
Definition: Expr.h:3004
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type, returning the result.
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition: ExprObjC.h:479
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1052
const Expr * getSubExpr() const
Definition: Expr.h:1678
BoundNodesTreeBuilder *const Builder
Opcode getOpcode() const
Definition: Expr.h:3008
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:3640
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2206
Expr * getRHS() const
Definition: Expr.h:3013
QualType getType() const
Definition: CGValue.h:277
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:953
static RValue get(llvm::Value *V)
Definition: CGValue.h:85
const Expr * getInit(unsigned Init) const
Definition: Expr.h:3896
LValue - This represents an lvalue references.
Definition: CGValue.h:171
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:182
Represents an implicitly-generated value initialization of an object of a given type.
Definition: Expr.h:4549
const NamedDecl * Result
Definition: USRFinder.cpp:70