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
33 typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
34
35 /// Return the complex type that we are meant to emit.
getComplexType(QualType type)36 static const ComplexType *getComplexType(QualType type) {
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;
49 CGBuilderTy &Builder;
50 bool IgnoreReal;
51 bool IgnoreImag;
52 public:
ComplexExprEmitter(CodeGenFunction & cgf,bool ir=false,bool ii=false)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
TestAndClearIgnoreReal()62 bool TestAndClearIgnoreReal() {
63 bool I = IgnoreReal;
64 IgnoreReal = false;
65 return I;
66 }
TestAndClearIgnoreImag()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.
EmitLoadOfLValue(const Expr * E)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
Visit(Expr * E)97 ComplexPairTy Visit(Expr *E) {
98 ApplyDebugLocation DL(CGF, E);
99 return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
100 }
101
VisitStmt(Stmt * S)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);
VisitParenExpr(ParenExpr * PE)107 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
VisitGenericSelectionExpr(GenericSelectionExpr * GE)108 ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
109 return Visit(GE->getResultExpr());
110 }
111 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
112 ComplexPairTy
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr * PE)113 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
114 return Visit(PE->getReplacement());
115 }
116
117 // l-values.
VisitDeclRefExpr(DeclRefExpr * E)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 }
VisitObjCIvarRefExpr(ObjCIvarRefExpr * E)130 ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
131 return EmitLoadOfLValue(E);
132 }
VisitObjCMessageExpr(ObjCMessageExpr * E)133 ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
134 return CGF.EmitObjCMessageExpr(E).getComplexVal();
135 }
VisitArraySubscriptExpr(Expr * E)136 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
VisitMemberExpr(const Expr * E)137 ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
VisitOpaqueValueExpr(OpaqueValueExpr * 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
VisitPseudoObjectExpr(PseudoObjectExpr * E)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);
VisitImplicitCastExpr(ImplicitCastExpr * E)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 }
VisitCastExpr(CastExpr * E)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.
VisitPrePostIncDec(const UnaryOperator * E,bool isInc,bool isPre)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 }
VisitUnaryPostDec(const UnaryOperator * E)168 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
169 return VisitPrePostIncDec(E, false, false);
170 }
VisitUnaryPostInc(const UnaryOperator * E)171 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
172 return VisitPrePostIncDec(E, true, false);
173 }
VisitUnaryPreDec(const UnaryOperator * E)174 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
175 return VisitPrePostIncDec(E, false, true);
176 }
VisitUnaryPreInc(const UnaryOperator * E)177 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
178 return VisitPrePostIncDec(E, true, true);
179 }
VisitUnaryDeref(const Expr * E)180 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
VisitUnaryPlus(const UnaryOperator * 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.
VisitUnaryExtension(const UnaryOperator * E)189 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
190 return Visit(E->getSubExpr());
191 }
VisitCXXDefaultArgExpr(CXXDefaultArgExpr * DAE)192 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
193 return Visit(DAE->getExpr());
194 }
VisitCXXDefaultInitExpr(CXXDefaultInitExpr * DIE)195 ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
196 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
197 return Visit(DIE->getExpr());
198 }
VisitExprWithCleanups(ExprWithCleanups * E)199 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
200 CGF.enterFullExpression(E);
201 CodeGenFunction::RunCleanupsScope Scope(CGF);
202 return Visit(E->getSubExpr());
203 }
VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr * E)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 }
VisitImplicitValueInitExpr(ImplicitValueInitExpr * E)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
VisitBinAdd(const BinaryOperator * E)241 ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
242 return EmitBinAdd(EmitBinOps(E));
243 }
VisitBinSub(const BinaryOperator * E)244 ComplexPairTy VisitBinSub(const BinaryOperator *E) {
245 return EmitBinSub(EmitBinOps(E));
246 }
VisitBinMul(const BinaryOperator * E)247 ComplexPairTy VisitBinMul(const BinaryOperator *E) {
248 return EmitBinMul(EmitBinOps(E));
249 }
VisitBinDiv(const BinaryOperator * E)250 ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
251 return EmitBinDiv(EmitBinOps(E));
252 }
253
254 // Compound assignments.
VisitBinAddAssign(const CompoundAssignOperator * E)255 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
256 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
257 }
VisitBinSubAssign(const CompoundAssignOperator * E)258 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
259 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
260 }
VisitBinMulAssign(const CompoundAssignOperator * E)261 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
262 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
263 }
VisitBinDivAssign(const CompoundAssignOperator * E)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
279 ComplexPairTy
280 VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
281 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
282
283 ComplexPairTy VisitInitListExpr(InitListExpr *E);
284
VisitCompoundLiteralExpr(CompoundLiteralExpr * E)285 ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
286 return EmitLoadOfLValue(E);
287 }
288
289 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
290
VisitAtomicExpr(AtomicExpr * E)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.
EmitLoadOfLValue(LValue lvalue,SourceLocation loc)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.
EmitStoreOfComplex(ComplexPairTy Val,LValue lvalue,bool isInit)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
VisitExpr(Expr * E)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::
VisitImaginaryLiteral(const ImaginaryLiteral * IL)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
VisitCallExpr(const CallExpr * E)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
VisitStmtExpr(const StmtExpr * E)386 ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
387 CodeGenFunction::StmtExprEvaluation eval(CGF);
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.
EmitComplexToComplexCast(ComplexPairTy Val,QualType SrcType,QualType 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
EmitScalarToComplexCast(llvm::Value * Val,QualType SrcType,QualType DestType)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
EmitCast(CastKind CK,Expr * Op,QualType DestTy)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.
428 case CK_AtomicToNonAtomic:
429 case CK_NonAtomicToAtomic:
430 case CK_NoOp:
431 case CK_LValueToRValue:
432 case CK_UserDefinedConversion:
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:
448 case CK_UncheckedDerivedToBase:
449 case CK_Dynamic:
450 case CK_ToUnion:
451 case CK_ArrayToPointerDecay:
452 case CK_FunctionToPointerDecay:
453 case CK_NullToPointer:
454 case CK_NullToMemberPointer:
455 case CK_BaseToDerivedMemberPointer:
456 case CK_DerivedToBaseMemberPointer:
457 case CK_MemberPointerToBoolean:
458 case CK_ReinterpretMemberPointer:
459 case CK_ConstructorConversion:
460 case CK_IntegralToPointer:
461 case CK_PointerToIntegral:
462 case CK_PointerToBoolean:
463 case CK_ToVoid:
464 case CK_VectorSplat:
465 case CK_IntegralCast:
466 case CK_IntegralToBoolean:
467 case CK_IntegralToFloating:
468 case CK_FloatingToIntegral:
469 case CK_FloatingToBoolean:
470 case CK_FloatingCast:
471 case CK_CPointerToObjCPointerCast:
472 case CK_BlockPointerToObjCPointerCast:
473 case CK_AnyPointerToBlockPointerCast:
474 case CK_ObjCObjectLValueCast:
475 case CK_FloatingComplexToReal:
476 case CK_FloatingComplexToBoolean:
477 case CK_IntegralComplexToReal:
478 case CK_IntegralComplexToBoolean:
479 case CK_ARCProduceObject:
480 case CK_ARCConsumeObject:
481 case CK_ARCReclaimReturnedObject:
482 case CK_ARCExtendBlockObject:
483 case CK_CopyAndAutoreleaseBlockObject:
484 case CK_BuiltinFnToFnPtr:
485 case CK_ZeroToOCLEvent:
486 case CK_AddressSpaceConversion:
487 llvm_unreachable("invalid cast kind for complex value");
488
489 case CK_FloatingRealToComplex:
490 case CK_IntegralRealToComplex:
491 return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op),
492 Op->getType(), DestTy);
493
494 case CK_FloatingComplexCast:
495 case CK_FloatingComplexToIntegralComplex:
496 case CK_IntegralComplexCast:
497 case CK_IntegralComplexToFloatingComplex:
498 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
499 }
500
501 llvm_unreachable("unknown cast resulting in complex value");
502 }
503
VisitUnaryMinus(const UnaryOperator * E)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
VisitUnaryNot(const UnaryOperator * E)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
EmitBinAdd(const BinOpInfo & Op)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
EmitBinSub(const BinOpInfo & Op)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.
EmitComplexBinOpLibCall(StringRef LibCallName,const BinOpInfo & Op)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 */),
591 RequiredArgs::All);
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.
getComplexMultiplyLibCallName(llvm::Type * Ty)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.
EmitBinMul(const BinOpInfo & Op)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.
EmitBinDiv(const BinOpInfo & Op)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
EmitBinOps(const BinaryOperator * E)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::
EmitCompoundAssignLValue(const CompoundAssignOperator * E,ComplexPairTy (ComplexExprEmitter::* Func)(const BinOpInfo &),RValue & Val)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::
EmitCompoundAssign(const CompoundAssignOperator * E,ComplexPairTy (ComplexExprEmitter::* Func)(const BinOpInfo &))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
EmitBinAssignLValue(const BinaryOperator * E,ComplexPairTy & Val)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
VisitBinAssign(const BinaryOperator * E)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
VisitBinComma(const BinaryOperator * E)936 ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
937 CGF.EmitIgnoredExpr(E->getLHS());
938 return Visit(E->getRHS());
939 }
940
941 ComplexPairTy ComplexExprEmitter::
VisitAbstractConditionalOperator(const AbstractConditionalOperator * E)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 RegionCounter Cnt = CGF.getPGORegionCounter(E);
953 CodeGenFunction::ConditionalEvaluation eval(CGF);
954 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock, Cnt.getCount());
955
956 eval.begin(CGF);
957 CGF.EmitBlock(LHSBlock);
958 Cnt.beginRegion(Builder);
959 ComplexPairTy LHS = Visit(E->getTrueExpr());
960 LHSBlock = Builder.GetInsertBlock();
961 CGF.EmitBranch(ContBlock);
962 eval.end(CGF);
963
964 eval.begin(CGF);
965 CGF.EmitBlock(RHSBlock);
966 ComplexPairTy RHS = Visit(E->getFalseExpr());
967 RHSBlock = Builder.GetInsertBlock();
968 CGF.EmitBlock(ContBlock);
969 eval.end(CGF);
970
971 // Create a PHI node for the real part.
972 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
973 RealPN->addIncoming(LHS.first, LHSBlock);
974 RealPN->addIncoming(RHS.first, RHSBlock);
975
976 // Create a PHI node for the imaginary part.
977 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
978 ImagPN->addIncoming(LHS.second, LHSBlock);
979 ImagPN->addIncoming(RHS.second, RHSBlock);
980
981 return ComplexPairTy(RealPN, ImagPN);
982 }
983
VisitChooseExpr(ChooseExpr * E)984 ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
985 return Visit(E->getChosenSubExpr());
986 }
987
VisitInitListExpr(InitListExpr * E)988 ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
989 bool Ignore = TestAndClearIgnoreReal();
990 (void)Ignore;
991 assert (Ignore == false && "init list ignored");
992 Ignore = TestAndClearIgnoreImag();
993 (void)Ignore;
994 assert (Ignore == false && "init list ignored");
995
996 if (E->getNumInits() == 2) {
997 llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
998 llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
999 return ComplexPairTy(Real, Imag);
1000 } else if (E->getNumInits() == 1) {
1001 return Visit(E->getInit(0));
1002 }
1003
1004 // Empty init list intializes to null
1005 assert(E->getNumInits() == 0 && "Unexpected number of inits");
1006 QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
1007 llvm::Type* LTy = CGF.ConvertType(Ty);
1008 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
1009 return ComplexPairTy(zeroConstant, zeroConstant);
1010 }
1011
VisitVAArgExpr(VAArgExpr * E)1012 ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
1013 llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr());
1014 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType());
1015
1016 if (!ArgPtr) {
1017 CGF.ErrorUnsupported(E, "complex va_arg expression");
1018 llvm::Type *EltTy =
1019 CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
1020 llvm::Value *U = llvm::UndefValue::get(EltTy);
1021 return ComplexPairTy(U, U);
1022 }
1023
1024 return EmitLoadOfLValue(CGF.MakeNaturalAlignAddrLValue(ArgPtr, E->getType()),
1025 E->getExprLoc());
1026 }
1027
1028 //===----------------------------------------------------------------------===//
1029 // Entry Point into this File
1030 //===----------------------------------------------------------------------===//
1031
1032 /// EmitComplexExpr - Emit the computation of the specified expression of
1033 /// complex type, ignoring the result.
EmitComplexExpr(const Expr * E,bool IgnoreReal,bool IgnoreImag)1034 ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
1035 bool IgnoreImag) {
1036 assert(E && getComplexType(E->getType()) &&
1037 "Invalid complex expression to emit");
1038
1039 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
1040 .Visit(const_cast<Expr *>(E));
1041 }
1042
EmitComplexExprIntoLValue(const Expr * E,LValue dest,bool isInit)1043 void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest,
1044 bool isInit) {
1045 assert(E && getComplexType(E->getType()) &&
1046 "Invalid complex expression to emit");
1047 ComplexExprEmitter Emitter(*this);
1048 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
1049 Emitter.EmitStoreOfComplex(Val, dest, isInit);
1050 }
1051
1052 /// EmitStoreOfComplex - Store a complex number into the specified l-value.
EmitStoreOfComplex(ComplexPairTy V,LValue dest,bool isInit)1053 void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest,
1054 bool isInit) {
1055 ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
1056 }
1057
1058 /// EmitLoadOfComplex - Load a complex number from the specified address.
EmitLoadOfComplex(LValue src,SourceLocation loc)1059 ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src,
1060 SourceLocation loc) {
1061 return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
1062 }
1063
EmitComplexAssignmentLValue(const BinaryOperator * E)1064 LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
1065 assert(E->getOpcode() == BO_Assign);
1066 ComplexPairTy Val; // ignored
1067 return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1068 }
1069
1070 typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1071 const ComplexExprEmitter::BinOpInfo &);
1072
getComplexOp(BinaryOperatorKind Op)1073 static CompoundFunc getComplexOp(BinaryOperatorKind Op) {
1074 switch (Op) {
1075 case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1076 case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1077 case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1078 case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
1079 default:
1080 llvm_unreachable("unexpected complex compound assignment");
1081 }
1082 }
1083
1084 LValue CodeGenFunction::
EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator * E)1085 EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
1086 CompoundFunc Op = getComplexOp(E->getOpcode());
1087 RValue Val;
1088 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1089 }
1090
1091 LValue CodeGenFunction::
EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator * E,llvm::Value * & Result)1092 EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E,
1093 llvm::Value *&Result) {
1094 CompoundFunc Op = getComplexOp(E->getOpcode());
1095 RValue Val;
1096 LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1097 Result = Val.getScalarVal();
1098 return Ret;
1099 }
1100