clang  3.7.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/ASTContext.h"
17 #include "clang/AST/StmtVisitor.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/MDBuilder.h"
24 #include "llvm/IR/Metadata.h"
25 #include <algorithm>
26 using namespace clang;
27 using namespace CodeGen;
28 
29 //===----------------------------------------------------------------------===//
30 // Complex Expression Emitter
31 //===----------------------------------------------------------------------===//
32 
34 
35 /// Return the complex type that we are meant to emit.
37  type = type.getCanonicalType();
38  if (const ComplexType *comp = dyn_cast<ComplexType>(type)) {
39  return comp;
40  } else {
41  return cast<ComplexType>(cast<AtomicType>(type)->getValueType());
42  }
43 }
44 
45 namespace {
46 class ComplexExprEmitter
47  : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
48  CodeGenFunction &CGF;
50  bool IgnoreReal;
51  bool IgnoreImag;
52 public:
53  ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
54  : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
55  }
56 
57 
58  //===--------------------------------------------------------------------===//
59  // Utilities
60  //===--------------------------------------------------------------------===//
61 
62  bool TestAndClearIgnoreReal() {
63  bool I = IgnoreReal;
64  IgnoreReal = false;
65  return I;
66  }
67  bool TestAndClearIgnoreImag() {
68  bool I = IgnoreImag;
69  IgnoreImag = false;
70  return I;
71  }
72 
73  /// EmitLoadOfLValue - Given an expression with complex type that represents a
74  /// value l-value, this method emits the address of the l-value, then loads
75  /// and returns the result.
76  ComplexPairTy EmitLoadOfLValue(const Expr *E) {
77  return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc());
78  }
79 
80  ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc);
81 
82  /// EmitStoreOfComplex - Store the specified real/imag parts into the
83  /// specified value pointer.
84  void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit);
85 
86  /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
87  ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
88  QualType DestType);
89  /// EmitComplexToComplexCast - Emit a cast from scalar value Val to DestType.
90  ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType,
91  QualType DestType);
92 
93  //===--------------------------------------------------------------------===//
94  // Visitor Methods
95  //===--------------------------------------------------------------------===//
96 
97  ComplexPairTy Visit(Expr *E) {
98  ApplyDebugLocation DL(CGF, E);
100  }
101 
102  ComplexPairTy VisitStmt(Stmt *S) {
103  S->dump(CGF.getContext().getSourceManager());
104  llvm_unreachable("Stmt can't have complex result type!");
105  }
106  ComplexPairTy VisitExpr(Expr *S);
107  ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
108  ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
109  return Visit(GE->getResultExpr());
110  }
111  ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
113  VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
114  return Visit(PE->getReplacement());
115  }
116 
117  // l-values.
118  ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
119  if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) {
120  if (result.isReference())
121  return EmitLoadOfLValue(result.getReferenceLValue(CGF, E),
122  E->getExprLoc());
123 
124  llvm::Constant *pair = result.getValue();
125  return ComplexPairTy(pair->getAggregateElement(0U),
126  pair->getAggregateElement(1U));
127  }
128  return EmitLoadOfLValue(E);
129  }
130  ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
131  return EmitLoadOfLValue(E);
132  }
133  ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
134  return CGF.EmitObjCMessageExpr(E).getComplexVal();
135  }
136  ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
137  ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
138  ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
139  if (E->isGLValue())
140  return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->getExprLoc());
141  return CGF.getOpaqueRValueMapping(E).getComplexVal();
142  }
143 
144  ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
145  return CGF.EmitPseudoObjectRValue(E).getComplexVal();
146  }
147 
148  // FIXME: CompoundLiteralExpr
149 
150  ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy);
151  ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
152  // Unlike for scalars, we don't have to worry about function->ptr demotion
153  // here.
154  return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
155  }
156  ComplexPairTy VisitCastExpr(CastExpr *E) {
157  return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
158  }
159  ComplexPairTy VisitCallExpr(const CallExpr *E);
160  ComplexPairTy VisitStmtExpr(const StmtExpr *E);
161 
162  // Operators.
163  ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
164  bool isInc, bool isPre) {
165  LValue LV = CGF.EmitLValue(E->getSubExpr());
166  return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
167  }
168  ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
169  return VisitPrePostIncDec(E, false, false);
170  }
171  ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
172  return VisitPrePostIncDec(E, true, false);
173  }
174  ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
175  return VisitPrePostIncDec(E, false, true);
176  }
177  ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
178  return VisitPrePostIncDec(E, true, true);
179  }
180  ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
181  ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
182  TestAndClearIgnoreReal();
183  TestAndClearIgnoreImag();
184  return Visit(E->getSubExpr());
185  }
186  ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
187  ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
188  // LNot,Real,Imag never return complex.
189  ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
190  return Visit(E->getSubExpr());
191  }
192  ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
193  return Visit(DAE->getExpr());
194  }
195  ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
197  return Visit(DIE->getExpr());
198  }
199  ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
200  CGF.enterFullExpression(E);
202  return Visit(E->getSubExpr());
203  }
204  ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
205  assert(E->getType()->isAnyComplexType() && "Expected complex type!");
206  QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
207  llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
208  return ComplexPairTy(Null, Null);
209  }
210  ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
211  assert(E->getType()->isAnyComplexType() && "Expected complex type!");
212  QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
213  llvm::Constant *Null =
214  llvm::Constant::getNullValue(CGF.ConvertType(Elem));
215  return ComplexPairTy(Null, Null);
216  }
217 
218  struct BinOpInfo {
219  ComplexPairTy LHS;
220  ComplexPairTy RHS;
221  QualType Ty; // Computation Type.
222  };
223 
224  BinOpInfo EmitBinOps(const BinaryOperator *E);
225  LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
226  ComplexPairTy (ComplexExprEmitter::*Func)
227  (const BinOpInfo &),
228  RValue &Val);
229  ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
230  ComplexPairTy (ComplexExprEmitter::*Func)
231  (const BinOpInfo &));
232 
233  ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
234  ComplexPairTy EmitBinSub(const BinOpInfo &Op);
235  ComplexPairTy EmitBinMul(const BinOpInfo &Op);
236  ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
237 
238  ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
239  const BinOpInfo &Op);
240 
241  ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
242  return EmitBinAdd(EmitBinOps(E));
243  }
244  ComplexPairTy VisitBinSub(const BinaryOperator *E) {
245  return EmitBinSub(EmitBinOps(E));
246  }
247  ComplexPairTy VisitBinMul(const BinaryOperator *E) {
248  return EmitBinMul(EmitBinOps(E));
249  }
250  ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
251  return EmitBinDiv(EmitBinOps(E));
252  }
253 
254  // Compound assignments.
255  ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
256  return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
257  }
258  ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
259  return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
260  }
261  ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
262  return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
263  }
264  ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
265  return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
266  }
267 
268  // GCC rejects rem/and/or/xor for integer complex.
269  // Logical and/or always return int, never complex.
270 
271  // No comparisons produce a complex result.
272 
273  LValue EmitBinAssignLValue(const BinaryOperator *E,
274  ComplexPairTy &Val);
275  ComplexPairTy VisitBinAssign (const BinaryOperator *E);
276  ComplexPairTy VisitBinComma (const BinaryOperator *E);
277 
278 
280  VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
281  ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
282 
283  ComplexPairTy VisitInitListExpr(InitListExpr *E);
284 
285  ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
286  return EmitLoadOfLValue(E);
287  }
288 
289  ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
290 
291  ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
292  return CGF.EmitAtomicExpr(E).getComplexVal();
293  }
294 };
295 } // end anonymous namespace.
296 
297 //===----------------------------------------------------------------------===//
298 // Utilities
299 //===----------------------------------------------------------------------===//
300 
301 /// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
302 /// load the real and imaginary pieces, returning them as Real/Imag.
303 ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
304  SourceLocation loc) {
305  assert(lvalue.isSimple() && "non-simple complex l-value?");
306  if (lvalue.getType()->isAtomicType())
307  return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
308 
309  llvm::Value *SrcPtr = lvalue.getAddress();
310  bool isVolatile = lvalue.isVolatileQualified();
311  unsigned AlignR = lvalue.getAlignment().getQuantity();
312  ASTContext &C = CGF.getContext();
313  QualType ComplexTy = lvalue.getType();
314  unsigned ComplexAlign = C.getTypeAlignInChars(ComplexTy).getQuantity();
315  unsigned AlignI = std::min(AlignR, ComplexAlign);
316 
317  llvm::Value *Real=nullptr, *Imag=nullptr;
318 
319  if (!IgnoreReal || isVolatile) {
320  llvm::Value *RealP = Builder.CreateStructGEP(nullptr, SrcPtr, 0,
321  SrcPtr->getName() + ".realp");
322  Real = Builder.CreateAlignedLoad(RealP, AlignR, isVolatile,
323  SrcPtr->getName() + ".real");
324  }
325 
326  if (!IgnoreImag || isVolatile) {
327  llvm::Value *ImagP = Builder.CreateStructGEP(nullptr, SrcPtr, 1,
328  SrcPtr->getName() + ".imagp");
329  Imag = Builder.CreateAlignedLoad(ImagP, AlignI, isVolatile,
330  SrcPtr->getName() + ".imag");
331  }
332  return ComplexPairTy(Real, Imag);
333 }
334 
335 /// EmitStoreOfComplex - Store the specified real/imag parts into the
336 /// specified value pointer.
337 void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue,
338  bool isInit) {
339  if (lvalue.getType()->isAtomicType() ||
340  (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue)))
341  return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
342 
343  llvm::Value *Ptr = lvalue.getAddress();
344  llvm::Value *RealPtr = Builder.CreateStructGEP(nullptr, Ptr, 0, "real");
345  llvm::Value *ImagPtr = Builder.CreateStructGEP(nullptr, Ptr, 1, "imag");
346  unsigned AlignR = lvalue.getAlignment().getQuantity();
347  ASTContext &C = CGF.getContext();
348  QualType ComplexTy = lvalue.getType();
349  unsigned ComplexAlign = C.getTypeAlignInChars(ComplexTy).getQuantity();
350  unsigned AlignI = std::min(AlignR, ComplexAlign);
351 
352  Builder.CreateAlignedStore(Val.first, RealPtr, AlignR,
353  lvalue.isVolatileQualified());
354  Builder.CreateAlignedStore(Val.second, ImagPtr, AlignI,
355  lvalue.isVolatileQualified());
356 }
357 
358 
359 
360 //===----------------------------------------------------------------------===//
361 // Visitor Methods
362 //===----------------------------------------------------------------------===//
363 
364 ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
365  CGF.ErrorUnsupported(E, "complex expression");
366  llvm::Type *EltTy =
367  CGF.ConvertType(getComplexType(E->getType())->getElementType());
368  llvm::Value *U = llvm::UndefValue::get(EltTy);
369  return ComplexPairTy(U, U);
370 }
371 
372 ComplexPairTy ComplexExprEmitter::
373 VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
374  llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
375  return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
376 }
377 
378 
379 ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
380  if (E->getCallReturnType(CGF.getContext())->isReferenceType())
381  return EmitLoadOfLValue(E);
382 
383  return CGF.EmitCallExpr(E).getComplexVal();
384 }
385 
386 ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
388  llvm::Value *RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
389  assert(RetAlloca && "Expected complex return value");
390  return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
391  E->getExprLoc());
392 }
393 
394 /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
395 ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
396  QualType SrcType,
397  QualType DestType) {
398  // Get the src/dest element type.
399  SrcType = SrcType->castAs<ComplexType>()->getElementType();
400  DestType = DestType->castAs<ComplexType>()->getElementType();
401 
402  // C99 6.3.1.6: When a value of complex type is converted to another
403  // complex type, both the real and imaginary parts follow the conversion
404  // rules for the corresponding real types.
405  Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType);
406  Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType);
407  return Val;
408 }
409 
410 ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
411  QualType SrcType,
412  QualType DestType) {
413  // Convert the input element to the element type of the complex.
414  DestType = DestType->castAs<ComplexType>()->getElementType();
415  Val = CGF.EmitScalarConversion(Val, SrcType, DestType);
416 
417  // Return (realval, 0).
418  return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
419 }
420 
421 ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
422  QualType DestTy) {
423  switch (CK) {
424  case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
425 
426  // Atomic to non-atomic casts may be more than a no-op for some platforms and
427  // for some types.
430  case CK_NoOp:
431  case CK_LValueToRValue:
433  return Visit(Op);
434 
435  case CK_LValueBitCast: {
436  LValue origLV = CGF.EmitLValue(Op);
437  llvm::Value *V = origLV.getAddress();
438  V = Builder.CreateBitCast(V,
439  CGF.ConvertType(CGF.getContext().getPointerType(DestTy)));
440  return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy,
441  origLV.getAlignment()),
442  Op->getExprLoc());
443  }
444 
445  case CK_BitCast:
446  case CK_BaseToDerived:
447  case CK_DerivedToBase:
449  case CK_Dynamic:
450  case CK_ToUnion:
453  case CK_NullToPointer:
462  case CK_PointerToBoolean:
463  case CK_ToVoid:
464  case CK_VectorSplat:
465  case CK_IntegralCast:
470  case CK_FloatingCast:
479  case CK_ARCProduceObject:
480  case CK_ARCConsumeObject:
484  case CK_BuiltinFnToFnPtr:
485  case CK_ZeroToOCLEvent:
487  llvm_unreachable("invalid cast kind for complex value");
488 
491  return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op),
492  Op->getType(), DestTy);
493 
498  return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
499  }
500 
501  llvm_unreachable("unknown cast resulting in complex value");
502 }
503 
504 ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
505  TestAndClearIgnoreReal();
506  TestAndClearIgnoreImag();
507  ComplexPairTy Op = Visit(E->getSubExpr());
508 
509  llvm::Value *ResR, *ResI;
510  if (Op.first->getType()->isFloatingPointTy()) {
511  ResR = Builder.CreateFNeg(Op.first, "neg.r");
512  ResI = Builder.CreateFNeg(Op.second, "neg.i");
513  } else {
514  ResR = Builder.CreateNeg(Op.first, "neg.r");
515  ResI = Builder.CreateNeg(Op.second, "neg.i");
516  }
517  return ComplexPairTy(ResR, ResI);
518 }
519 
520 ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
521  TestAndClearIgnoreReal();
522  TestAndClearIgnoreImag();
523  // ~(a+ib) = a + i*-b
524  ComplexPairTy Op = Visit(E->getSubExpr());
525  llvm::Value *ResI;
526  if (Op.second->getType()->isFloatingPointTy())
527  ResI = Builder.CreateFNeg(Op.second, "conj.i");
528  else
529  ResI = Builder.CreateNeg(Op.second, "conj.i");
530 
531  return ComplexPairTy(Op.first, ResI);
532 }
533 
534 ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
535  llvm::Value *ResR, *ResI;
536 
537  if (Op.LHS.first->getType()->isFloatingPointTy()) {
538  ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
539  if (Op.LHS.second && Op.RHS.second)
540  ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
541  else
542  ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
543  assert(ResI && "Only one operand may be real!");
544  } else {
545  ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
546  assert(Op.LHS.second && Op.RHS.second &&
547  "Both operands of integer complex operators must be complex!");
548  ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
549  }
550  return ComplexPairTy(ResR, ResI);
551 }
552 
553 ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
554  llvm::Value *ResR, *ResI;
555  if (Op.LHS.first->getType()->isFloatingPointTy()) {
556  ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
557  if (Op.LHS.second && Op.RHS.second)
558  ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
559  else
560  ResI = Op.LHS.second ? Op.LHS.second
561  : Builder.CreateFNeg(Op.RHS.second, "sub.i");
562  assert(ResI && "Only one operand may be real!");
563  } else {
564  ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
565  assert(Op.LHS.second && Op.RHS.second &&
566  "Both operands of integer complex operators must be complex!");
567  ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
568  }
569  return ComplexPairTy(ResR, ResI);
570 }
571 
572 /// \brief Emit a libcall for a binary operation on complex types.
573 ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
574  const BinOpInfo &Op) {
575  CallArgList Args;
576  Args.add(RValue::get(Op.LHS.first),
577  Op.Ty->castAs<ComplexType>()->getElementType());
578  Args.add(RValue::get(Op.LHS.second),
579  Op.Ty->castAs<ComplexType>()->getElementType());
580  Args.add(RValue::get(Op.RHS.first),
581  Op.Ty->castAs<ComplexType>()->getElementType());
582  Args.add(RValue::get(Op.RHS.second),
583  Op.Ty->castAs<ComplexType>()->getElementType());
584 
585  // We *must* use the full CG function call building logic here because the
586  // complex type has special ABI handling. We also should not forget about
587  // special calling convention which may be used for compiler builtins.
588  const CGFunctionInfo &FuncInfo =
589  CGF.CGM.getTypes().arrangeFreeFunctionCall(
590  Op.Ty, Args, FunctionType::ExtInfo(/* No CC here - will be added later */),
592  llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
593  llvm::Constant *Func = CGF.CGM.CreateBuiltinFunction(FTy, LibCallName);
594  llvm::Instruction *Call;
595 
596  RValue Res = CGF.EmitCall(FuncInfo, Func, ReturnValueSlot(), Args,
597  nullptr, &Call);
598  cast<llvm::CallInst>(Call)->setCallingConv(CGF.CGM.getBuiltinCC());
599  cast<llvm::CallInst>(Call)->setDoesNotThrow();
600 
601  return Res.getComplexVal();
602 }
603 
604 /// \brief Lookup the libcall name for a given floating point type complex
605 /// multiply.
606 static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
607  switch (Ty->getTypeID()) {
608  default:
609  llvm_unreachable("Unsupported floating point type!");
610  case llvm::Type::HalfTyID:
611  return "__mulhc3";
612  case llvm::Type::FloatTyID:
613  return "__mulsc3";
614  case llvm::Type::DoubleTyID:
615  return "__muldc3";
616  case llvm::Type::PPC_FP128TyID:
617  return "__multc3";
618  case llvm::Type::X86_FP80TyID:
619  return "__mulxc3";
620  case llvm::Type::FP128TyID:
621  return "__multc3";
622  }
623 }
624 
625 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
626 // typed values.
627 ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
628  using llvm::Value;
629  Value *ResR, *ResI;
630  llvm::MDBuilder MDHelper(CGF.getLLVMContext());
631 
632  if (Op.LHS.first->getType()->isFloatingPointTy()) {
633  // The general formulation is:
634  // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
635  //
636  // But we can fold away components which would be zero due to a real
637  // operand according to C11 Annex G.5.1p2.
638  // FIXME: C11 also provides for imaginary types which would allow folding
639  // still more of this within the type system.
640 
641  if (Op.LHS.second && Op.RHS.second) {
642  // If both operands are complex, emit the core math directly, and then
643  // test for NaNs. If we find NaNs in the result, we delegate to a libcall
644  // to carefully re-compute the correct infinity representation if
645  // possible. The expectation is that the presence of NaNs here is
646  // *extremely* rare, and so the cost of the libcall is almost irrelevant.
647  // This is good, because the libcall re-computes the core multiplication
648  // exactly the same as we do here and re-tests for NaNs in order to be
649  // a generic complex*complex libcall.
650 
651  // First compute the four products.
652  Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
653  Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
654  Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
655  Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
656 
657  // The real part is the difference of the first two, the imaginary part is
658  // the sum of the second.
659  ResR = Builder.CreateFSub(AC, BD, "mul_r");
660  ResI = Builder.CreateFAdd(AD, BC, "mul_i");
661 
662  // Emit the test for the real part becoming NaN and create a branch to
663  // handle it. We test for NaN by comparing the number to itself.
664  Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
665  llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
666  llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
667  llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
668  llvm::BasicBlock *OrigBB = Branch->getParent();
669 
670  // Give hint that we very much don't expect to see NaNs.
671  // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
672  llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1);
673  Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
674 
675  // Now test the imaginary part and create its branch.
676  CGF.EmitBlock(INaNBB);
677  Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
678  llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
679  Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
680  Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
681 
682  // Now emit the libcall on this slowest of the slow paths.
683  CGF.EmitBlock(LibCallBB);
684  Value *LibCallR, *LibCallI;
685  std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
686  getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
687  Builder.CreateBr(ContBB);
688 
689  // Finally continue execution by phi-ing together the different
690  // computation paths.
691  CGF.EmitBlock(ContBB);
692  llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
693  RealPHI->addIncoming(ResR, OrigBB);
694  RealPHI->addIncoming(ResR, INaNBB);
695  RealPHI->addIncoming(LibCallR, LibCallBB);
696  llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
697  ImagPHI->addIncoming(ResI, OrigBB);
698  ImagPHI->addIncoming(ResI, INaNBB);
699  ImagPHI->addIncoming(LibCallI, LibCallBB);
700  return ComplexPairTy(RealPHI, ImagPHI);
701  }
702  assert((Op.LHS.second || Op.RHS.second) &&
703  "At least one operand must be complex!");
704 
705  // If either of the operands is a real rather than a complex, the
706  // imaginary component is ignored when computing the real component of the
707  // result.
708  ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
709 
710  ResI = Op.LHS.second
711  ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
712  : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
713  } else {
714  assert(Op.LHS.second && Op.RHS.second &&
715  "Both operands of integer complex operators must be complex!");
716  Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
717  Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
718  ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
719 
720  Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
721  Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
722  ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
723  }
724  return ComplexPairTy(ResR, ResI);
725 }
726 
727 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
728 // typed values.
729 ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
730  llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
731  llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
732 
733 
734  llvm::Value *DSTr, *DSTi;
735  if (LHSr->getType()->isFloatingPointTy()) {
736  // If we have a complex operand on the RHS, we delegate to a libcall to
737  // handle all of the complexities and minimize underflow/overflow cases.
738  //
739  // FIXME: We would be able to avoid the libcall in many places if we
740  // supported imaginary types in addition to complex types.
741  if (RHSi) {
742  BinOpInfo LibCallOp = Op;
743  // If LHS was a real, supply a null imaginary part.
744  if (!LHSi)
745  LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
746 
747  StringRef LibCallName;
748  switch (LHSr->getType()->getTypeID()) {
749  default:
750  llvm_unreachable("Unsupported floating point type!");
751  case llvm::Type::HalfTyID:
752  return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
753  case llvm::Type::FloatTyID:
754  return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
755  case llvm::Type::DoubleTyID:
756  return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
757  case llvm::Type::PPC_FP128TyID:
758  return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
759  case llvm::Type::X86_FP80TyID:
760  return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
761  case llvm::Type::FP128TyID:
762  return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
763  }
764  }
765  assert(LHSi && "Can have at most one non-complex operand!");
766 
767  DSTr = Builder.CreateFDiv(LHSr, RHSr);
768  DSTi = Builder.CreateFDiv(LHSi, RHSr);
769  } else {
770  assert(Op.LHS.second && Op.RHS.second &&
771  "Both operands of integer complex operators must be complex!");
772  // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
773  llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
774  llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
775  llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
776 
777  llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
778  llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
779  llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
780 
781  llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
782  llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
783  llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
784 
785  if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
786  DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
787  DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
788  } else {
789  DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
790  DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
791  }
792  }
793 
794  return ComplexPairTy(DSTr, DSTi);
795 }
796 
797 ComplexExprEmitter::BinOpInfo
798 ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
799  TestAndClearIgnoreReal();
800  TestAndClearIgnoreImag();
801  BinOpInfo Ops;
802  if (E->getLHS()->getType()->isRealFloatingType())
803  Ops.LHS = ComplexPairTy(CGF.EmitScalarExpr(E->getLHS()), nullptr);
804  else
805  Ops.LHS = Visit(E->getLHS());
806  if (E->getRHS()->getType()->isRealFloatingType())
807  Ops.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
808  else
809  Ops.RHS = Visit(E->getRHS());
810 
811  Ops.Ty = E->getType();
812  return Ops;
813 }
814 
815 
816 LValue ComplexExprEmitter::
817 EmitCompoundAssignLValue(const CompoundAssignOperator *E,
818  ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
819  RValue &Val) {
820  TestAndClearIgnoreReal();
821  TestAndClearIgnoreImag();
822  QualType LHSTy = E->getLHS()->getType();
823  if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
824  LHSTy = AT->getValueType();
825 
826  BinOpInfo OpInfo;
827 
828  // Load the RHS and LHS operands.
829  // __block variables need to have the rhs evaluated first, plus this should
830  // improve codegen a little.
831  OpInfo.Ty = E->getComputationResultType();
832  QualType ComplexElementTy = cast<ComplexType>(OpInfo.Ty)->getElementType();
833 
834  // The RHS should have been converted to the computation type.
835  if (E->getRHS()->getType()->isRealFloatingType()) {
836  assert(
837  CGF.getContext()
838  .hasSameUnqualifiedType(ComplexElementTy, E->getRHS()->getType()));
839  OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
840  } else {
841  assert(CGF.getContext()
842  .hasSameUnqualifiedType(OpInfo.Ty, E->getRHS()->getType()));
843  OpInfo.RHS = Visit(E->getRHS());
844  }
845 
846  LValue LHS = CGF.EmitLValue(E->getLHS());
847 
848  // Load from the l-value and convert it.
849  if (LHSTy->isAnyComplexType()) {
850  ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, E->getExprLoc());
851  OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty);
852  } else {
853  llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, E->getExprLoc());
854  // For floating point real operands we can directly pass the scalar form
855  // to the binary operator emission and potentially get more efficient code.
856  if (LHSTy->isRealFloatingType()) {
857  if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
858  LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy);
859  OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
860  } else {
861  OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty);
862  }
863  }
864 
865  // Expand the binary operator.
866  ComplexPairTy Result = (this->*Func)(OpInfo);
867 
868  // Truncate the result and store it into the LHS lvalue.
869  if (LHSTy->isAnyComplexType()) {
870  ComplexPairTy ResVal = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy);
871  EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
872  Val = RValue::getComplex(ResVal);
873  } else {
874  llvm::Value *ResVal =
875  CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy);
876  CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
877  Val = RValue::get(ResVal);
878  }
879 
880  return LHS;
881 }
882 
883 // Compound assignments.
884 ComplexPairTy ComplexExprEmitter::
885 EmitCompoundAssign(const CompoundAssignOperator *E,
886  ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
887  RValue Val;
888  LValue LV = EmitCompoundAssignLValue(E, Func, Val);
889 
890  // The result of an assignment in C is the assigned r-value.
891  if (!CGF.getLangOpts().CPlusPlus)
892  return Val.getComplexVal();
893 
894  // If the lvalue is non-volatile, return the computed value of the assignment.
895  if (!LV.isVolatileQualified())
896  return Val.getComplexVal();
897 
898  return EmitLoadOfLValue(LV, E->getExprLoc());
899 }
900 
901 LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
902  ComplexPairTy &Val) {
903  assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
904  E->getRHS()->getType()) &&
905  "Invalid assignment");
906  TestAndClearIgnoreReal();
907  TestAndClearIgnoreImag();
908 
909  // Emit the RHS. __block variables need the RHS evaluated first.
910  Val = Visit(E->getRHS());
911 
912  // Compute the address to store into.
913  LValue LHS = CGF.EmitLValue(E->getLHS());
914 
915  // Store the result value into the LHS lvalue.
916  EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
917 
918  return LHS;
919 }
920 
921 ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
922  ComplexPairTy Val;
923  LValue LV = EmitBinAssignLValue(E, Val);
924 
925  // The result of an assignment in C is the assigned r-value.
926  if (!CGF.getLangOpts().CPlusPlus)
927  return Val;
928 
929  // If the lvalue is non-volatile, return the computed value of the assignment.
930  if (!LV.isVolatileQualified())
931  return Val;
932 
933  return EmitLoadOfLValue(LV, E->getExprLoc());
934 }
935 
936 ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
937  CGF.EmitIgnoredExpr(E->getLHS());
938  return Visit(E->getRHS());
939 }
940 
941 ComplexPairTy ComplexExprEmitter::
942 VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
943  TestAndClearIgnoreReal();
944  TestAndClearIgnoreImag();
945  llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
946  llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
947  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
948 
949  // Bind the common expression if necessary.
950  CodeGenFunction::OpaqueValueMapping binding(CGF, E);
951 
952 
954  CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock,
955  CGF.getProfileCount(E));
956 
957  eval.begin(CGF);
958  CGF.EmitBlock(LHSBlock);
959  CGF.incrementProfileCounter(E);
960  ComplexPairTy LHS = Visit(E->getTrueExpr());
961  LHSBlock = Builder.GetInsertBlock();
962  CGF.EmitBranch(ContBlock);
963  eval.end(CGF);
964 
965  eval.begin(CGF);
966  CGF.EmitBlock(RHSBlock);
967  ComplexPairTy RHS = Visit(E->getFalseExpr());
968  RHSBlock = Builder.GetInsertBlock();
969  CGF.EmitBlock(ContBlock);
970  eval.end(CGF);
971 
972  // Create a PHI node for the real part.
973  llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
974  RealPN->addIncoming(LHS.first, LHSBlock);
975  RealPN->addIncoming(RHS.first, RHSBlock);
976 
977  // Create a PHI node for the imaginary part.
978  llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
979  ImagPN->addIncoming(LHS.second, LHSBlock);
980  ImagPN->addIncoming(RHS.second, RHSBlock);
981 
982  return ComplexPairTy(RealPN, ImagPN);
983 }
984 
985 ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
986  return Visit(E->getChosenSubExpr());
987 }
988 
989 ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
990  bool Ignore = TestAndClearIgnoreReal();
991  (void)Ignore;
992  assert (Ignore == false && "init list ignored");
993  Ignore = TestAndClearIgnoreImag();
994  (void)Ignore;
995  assert (Ignore == false && "init list ignored");
996 
997  if (E->getNumInits() == 2) {
998  llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
999  llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
1000  return ComplexPairTy(Real, Imag);
1001  } else if (E->getNumInits() == 1) {
1002  return Visit(E->getInit(0));
1003  }
1004 
1005  // Empty init list intializes to null
1006  assert(E->getNumInits() == 0 && "Unexpected number of inits");
1008  llvm::Type* LTy = CGF.ConvertType(Ty);
1009  llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
1010  return ComplexPairTy(zeroConstant, zeroConstant);
1011 }
1012 
1013 ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
1014  llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr());
1015  llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType());
1016 
1017  if (!ArgPtr) {
1018  CGF.ErrorUnsupported(E, "complex va_arg expression");
1019  llvm::Type *EltTy =
1020  CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
1021  llvm::Value *U = llvm::UndefValue::get(EltTy);
1022  return ComplexPairTy(U, U);
1023  }
1024 
1025  return EmitLoadOfLValue(CGF.MakeNaturalAlignAddrLValue(ArgPtr, E->getType()),
1026  E->getExprLoc());
1027 }
1028 
1029 //===----------------------------------------------------------------------===//
1030 // Entry Point into this File
1031 //===----------------------------------------------------------------------===//
1032 
1033 /// EmitComplexExpr - Emit the computation of the specified expression of
1034 /// complex type, ignoring the result.
1036  bool IgnoreImag) {
1037  assert(E && getComplexType(E->getType()) &&
1038  "Invalid complex expression to emit");
1039 
1040  return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
1041  .Visit(const_cast<Expr *>(E));
1042 }
1043 
1045  bool isInit) {
1046  assert(E && getComplexType(E->getType()) &&
1047  "Invalid complex expression to emit");
1048  ComplexExprEmitter Emitter(*this);
1049  ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
1050  Emitter.EmitStoreOfComplex(Val, dest, isInit);
1051 }
1052 
1053 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
1055  bool isInit) {
1056  ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
1057 }
1058 
1059 /// EmitLoadOfComplex - Load a complex number from the specified address.
1061  SourceLocation loc) {
1062  return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
1063 }
1064 
1066  assert(E->getOpcode() == BO_Assign);
1067  ComplexPairTy Val; // ignored
1068  return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1069 }
1070 
1071 typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1072  const ComplexExprEmitter::BinOpInfo &);
1073 
1075  switch (Op) {
1076  case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1077  case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1078  case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1079  case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
1080  default:
1081  llvm_unreachable("unexpected complex compound assignment");
1082  }
1083 }
1084 
1087  CompoundFunc Op = getComplexOp(E->getOpcode());
1088  RValue Val;
1089  return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1090 }
1091 
1094  llvm::Value *&Result) {
1095  CompoundFunc Op = getComplexOp(E->getOpcode());
1096  RValue Val;
1097  LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1098  Result = Val.getScalarVal();
1099  return Ret;
1100 }
Defines the clang::ASTContext interface.
unsigned getNumInits() const
Definition: Expr.h:3789
CastKind getCastKind() const
Definition: Expr.h:2709
LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E)
CompoundStmt * getSubStmt()
Definition: Expr.h:3412
std::pair< llvm::Value *, llvm::Value * > getComplexVal() const
Definition: CGValue.h:61
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:163
static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty)
Lookup the libcall name for a given floating point type complex multiply.
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
const Expr * getResultExpr() const
Definition: Expr.h:4514
[ARC] Consumes a retainable object pointer that has just been produced, e.g. as the return value of a...
llvm::Value * getAddress() const
Definition: CGValue.h:265
CK_Dynamic - A C++ dynamic_cast.
LValue EmitComplexAssignmentLValue(const BinaryOperator *E)
Emit an l-value for an assignment (simple or compound) of complex type.
An object to manage conditionally-evaluated expressions.
LValue EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E, llvm::Value *&Result)
bool isVolatileQualified() const
Definition: CGValue.h:199
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:89
Converts between different integral complex types. _Complex char -> _Complex long long _Complex unsig...
An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
Converting between two Objective-C object types, which can occur when performing reference binding to...
[ARC] Causes a value of block type to be copied to the heap, if it is not already there...
Expr * getSubExpr()
Definition: Expr.h:2713
Expr * getLHS() const
Definition: Expr.h:2964
ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc)
EmitLoadOfComplex - Load a complex number from the specified l-value.
Converts a floating point complex to bool by comparing against 0+0i.
Describes an C or C++ initializer list.
Definition: Expr.h:3759
Expr * getChosenSubExpr() const
Definition: Expr.h:3605
BinaryOperatorKind
CharUnits getAlignment() const
Definition: CGValue.h:261
Expr * getTrueExpr() const
Definition: Expr.h:3344
const Expr * getSubExpr() const
Definition: Expr.h:3690
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2918
bool isUnsignedIntegerType() const
Definition: Type.cpp:1723
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:862
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
Causes a block literal to by copied to the heap and then autoreleased.
CastKind
CastKind - The kind of operation required for a conversion.
const Expr * getExpr() const
Get the initialization expression that will be used.
Definition: ExprCXX.h:977
VAArgExpr, used for the builtin function __builtin_va_arg.
Definition: Expr.h:3670
Converts between different floating point complex types. _Complex float -> _Complex double...
bool isRealFloatingType() const
Floating point categories.
Definition: Type.cpp:1776
Converts an integral complex to an integral real of the source's element type by discarding the imagi...
bool isAnyComplexType() const
Definition: Type.h:5295
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
bool isAtomicType() const
Definition: Type.h:5314
void add(RValue rvalue, QualType type, bool needscopy=false)
Definition: CGCall.h:81
Expr * getSubExpr() const
Definition: Expr.h:1699
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:858
Converts from an integral complex to a floating complex. _Complex unsigned -> _Complex float...
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition: ExprCXX.h:3558
bool isGLValue() const
Definition: Expr.h:253
The result type of a method or function.
llvm::IRBuilder< PreserveNames, llvm::ConstantFolder, CGBuilderInserterTy > CGBuilderTy
Definition: CGBuilder.h:49
QualType getComputationResultType() const
Definition: Expr.h:3136
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
bool isSimple() const
Definition: CGValue.h:193
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
QualType getElementType() const
Definition: Type.h:2077
static const ComplexType * getComplexType(QualType type)
Return the complex type that we are meant to emit.
Converts from an integral real to an integral complex whose element type matches the source...
const T * castAs() const
Definition: Type.h:5586
SourceLocation getExprLoc() const LLVM_READONLY
Definition: Expr.cpp:193
Represents a C11 generic selection.
Definition: Expr.h:4446
QualType getCallReturnType(const ASTContext &Ctx) const
Definition: Expr.cpp:1247
QualType getType() const
Definition: Expr.h:125
Converts a floating point complex to floating point real of the source's element type. Just discards the imaginary component. _Complex long double -> long double.
SourceLocation getExprLoc() const LLVM_READONLY
Definition: Expr.h:854
static const Type * getElementType(const Expr *BaseExpr)
static CompoundFunc getComplexOp(BinaryOperatorKind Op)
const Expr * getExpr() const
Definition: ExprCXX.h:911
static RValue getComplex(llvm::Value *V1, llvm::Value *V2)
Definition: CGValue.h:78
Converts an integral complex to bool by comparing against 0+0i.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchers.h:1639
CodeGenFunction::ComplexPairTy ComplexPairTy
A conversion of a floating point real to a floating point complex of the original type...
[ARC] Reclaim a retainable object pointer object that may have been produced and autoreleased as part...
const T * getAs() const
Definition: Type.h:5555
Expr * getFalseExpr() const
Definition: Expr.h:3350
QualType getCanonicalType() const
Definition: Type.h:5055
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
[ARC] Produces a retainable object pointer so that it may be consumed, e.g. by being passed to a cons...
Converts from T to _Atomic(T).
Converts from a floating complex to an integral complex. _Complex float -> _Complex int...
const Expr * getSubExpr() const
Definition: Expr.h:1442
ComplexPairTy(ComplexExprEmitter::* CompoundFunc)(const ComplexExprEmitter::BinOpInfo &)
SourceLocation getExprLoc() const LLVM_READONLY
Definition: Expr.h:2957
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
ObjCIvarRefExpr - A reference to an ObjC instance variable.
Definition: ExprObjC.h:474
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:952
const Expr * getSubExpr() const
Definition: Expr.h:1638
BoundNodesTreeBuilder *const Builder
Opcode getOpcode() const
Definition: Expr.h:2961
Converts from _Atomic(T) to T.
Expr * getRHS() const
Definition: Expr.h:2966
QualType getType() const
Definition: CGValue.h:205
A reference to a declared variable, function, enum, etc. [C99 6.5.1p2].
Definition: Expr.h:899
static RValue get(llvm::Value *V)
Definition: CGValue.h:71
const Expr * getInit(unsigned Init) const
Definition: Expr.h:3794
Represents an implicitly-generated value initialization of an object of a given type.
Definition: Expr.h:4352