blob: 4700c7eef35962abe26e319f791f5968e106867b [file] [log] [blame]
Chris Lattner566b6ce2007-08-24 02:22:53 +00001//===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===//
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00007//
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"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/StmtVisitor.h"
Stephen Hines176edba2014-12-01 14:53:08 -080018#include "llvm/ADT/STLExtras.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "llvm/ADT/SmallString.h"
Chandler Carruth3b844ba2013-01-02 11:45:17 +000020#include "llvm/IR/Constants.h"
21#include "llvm/IR/Function.h"
Stephen Hines176edba2014-12-01 14:53:08 -080022#include "llvm/IR/Instructions.h"
23#include "llvm/IR/MDBuilder.h"
24#include "llvm/IR/Metadata.h"
JF Bastienab96e562013-07-17 05:57:42 +000025#include <algorithm>
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000026using namespace clang;
27using namespace CodeGen;
28
29//===----------------------------------------------------------------------===//
Chris Lattner05ba49c2007-08-21 05:54:53 +000030// Complex Expression Emitter
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000031//===----------------------------------------------------------------------===//
32
Chris Lattner58dee102007-08-21 16:57:55 +000033typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000034
John McCall9d232c82013-03-07 21:37:08 +000035/// Return the complex type that we are meant to emit.
36static 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
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000045namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000046class ComplexExprEmitter
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000047 : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> {
48 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000049 CGBuilderTy &Builder;
Mike Stump7f79f9b2009-05-29 15:46:01 +000050 bool IgnoreReal;
51 bool IgnoreImag;
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000052public:
John McCallb418d742010-11-16 10:08:07 +000053 ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false)
54 : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) {
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000055 }
56
Mike Stumpdb52dcd2009-09-09 13:00:44 +000057
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000058 //===--------------------------------------------------------------------===//
59 // Utilities
60 //===--------------------------------------------------------------------===//
61
Mike Stump7f79f9b2009-05-29 15:46:01 +000062 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 }
Mike Stump7f79f9b2009-05-29 15:46:01 +000072
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000073 /// 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.
Chris Lattner46d7d9f2007-08-21 17:28:34 +000076 ComplexPairTy EmitLoadOfLValue(const Expr *E) {
Nick Lewycky4ee7dc22013-10-02 02:29:49 +000077 return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc());
John McCall0e800c92010-12-04 08:14:53 +000078 }
79
Nick Lewycky4ee7dc22013-10-02 02:29:49 +000080 ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc);
John McCall0e800c92010-12-04 08:14:53 +000081
Chris Lattner46d7d9f2007-08-21 17:28:34 +000082 /// EmitStoreOfComplex - Store the specified real/imag parts into the
83 /// specified value pointer.
John McCall9d232c82013-03-07 21:37:08 +000084 void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit);
Mike Stumpdb52dcd2009-09-09 13:00:44 +000085
Chris Lattnerab340c22007-08-26 22:09:01 +000086 /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
87 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType,
88 QualType DestType);
Eli Friedman0934e182013-06-12 01:40:06 +000089 /// EmitComplexToComplexCast - Emit a cast from scalar value Val to DestType.
90 ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType,
91 QualType DestType);
Mike Stumpdb52dcd2009-09-09 13:00:44 +000092
Chris Lattnerb6ef18a2007-08-21 05:54:00 +000093 //===--------------------------------------------------------------------===//
94 // Visitor Methods
95 //===--------------------------------------------------------------------===//
Chris Lattner776c6492007-08-21 18:51:13 +000096
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +000097 ComplexPairTy Visit(Expr *E) {
John McCallcf0b2d82011-02-17 19:02:56 +000098 return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E);
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +000099 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700100
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000101 ComplexPairTy VisitStmt(Stmt *S) {
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000102 S->dump(CGF.getContext().getSourceManager());
David Blaikieb219cfc2011-09-23 05:06:16 +0000103 llvm_unreachable("Stmt can't have complex result type!");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000104 }
Chris Lattner776c6492007-08-21 18:51:13 +0000105 ComplexPairTy VisitExpr(Expr *S);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000106 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());}
Peter Collingbournef111d932011-04-15 00:35:48 +0000107 ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
108 return Visit(GE->getResultExpr());
109 }
Chris Lattnerd272ff02007-08-26 05:57:57 +0000110 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL);
John McCall91a57552011-07-15 05:09:51 +0000111 ComplexPairTy
112 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
113 return Visit(PE->getReplacement());
114 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000115
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000116 // l-values.
John McCallf4b88a42012-03-10 09:33:50 +0000117 ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) {
118 if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) {
John McCalldd2ecee2012-03-10 03:05:10 +0000119 if (result.isReference())
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000120 return EmitLoadOfLValue(result.getReferenceLValue(CGF, E),
121 E->getExprLoc());
John McCalldd2ecee2012-03-10 03:05:10 +0000122
Eli Friedmanbe6d9132013-07-16 20:19:04 +0000123 llvm::Constant *pair = result.getValue();
124 return ComplexPairTy(pair->getAggregateElement(0U),
125 pair->getAggregateElement(1U));
John McCalldd2ecee2012-03-10 03:05:10 +0000126 }
John McCallf4b88a42012-03-10 09:33:50 +0000127 return EmitLoadOfLValue(E);
John McCalldd2ecee2012-03-10 03:05:10 +0000128 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000129 ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000130 return EmitLoadOfLValue(E);
131 }
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000132 ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) {
133 return CGF.EmitObjCMessageExpr(E).getComplexVal();
134 }
Chris Lattner8bcd7232007-08-21 18:03:58 +0000135 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerd272ff02007-08-26 05:57:57 +0000136 ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); }
John McCalle996ffd2011-02-16 08:02:54 +0000137 ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) {
John McCall56ca35d2011-02-17 10:25:35 +0000138 if (E->isGLValue())
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000139 return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->getExprLoc());
John McCall56ca35d2011-02-17 10:25:35 +0000140 return CGF.getOpaqueRValueMapping(E).getComplexVal();
John McCalle996ffd2011-02-16 08:02:54 +0000141 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000142
John McCall4b9c2d22011-11-06 09:01:30 +0000143 ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) {
144 return CGF.EmitPseudoObjectRValue(E).getComplexVal();
145 }
146
Chris Lattnere73e8e22007-08-21 22:33:41 +0000147 // FIXME: CompoundLiteralExpr
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000148
Stephen Hines176edba2014-12-01 14:53:08 -0800149 ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy);
Chris Lattnerd272ff02007-08-26 05:57:57 +0000150 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
151 // Unlike for scalars, we don't have to worry about function->ptr demotion
152 // here.
Douglas Gregor77d439a2010-07-14 21:35:45 +0000153 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
Chris Lattnerd272ff02007-08-26 05:57:57 +0000154 }
155 ComplexPairTy VisitCastExpr(CastExpr *E) {
Douglas Gregor77d439a2010-07-14 21:35:45 +0000156 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
Chris Lattnerd272ff02007-08-26 05:57:57 +0000157 }
Chris Lattner419d25e2007-08-23 21:38:16 +0000158 ComplexPairTy VisitCallExpr(const CallExpr *E);
Chris Lattner7e20d092007-08-31 22:51:38 +0000159 ComplexPairTy VisitStmtExpr(const StmtExpr *E);
160
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000161 // Operators.
Chris Lattner3070f982007-08-21 22:25:29 +0000162 ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E,
Chris Lattnerdd36d322010-01-09 21:40:03 +0000163 bool isInc, bool isPre) {
164 LValue LV = CGF.EmitLValue(E->getSubExpr());
165 return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre);
166 }
Chris Lattner3070f982007-08-21 22:25:29 +0000167 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) {
168 return VisitPrePostIncDec(E, false, false);
169 }
170 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) {
171 return VisitPrePostIncDec(E, true, false);
172 }
173 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) {
174 return VisitPrePostIncDec(E, false, true);
175 }
176 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) {
177 return VisitPrePostIncDec(E, true, true);
178 }
179 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000180 ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000181 TestAndClearIgnoreReal();
182 TestAndClearIgnoreImag();
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000183 return Visit(E->getSubExpr());
184 }
185 ComplexPairTy VisitUnaryMinus (const UnaryOperator *E);
Chris Lattnere98a11c2007-08-21 20:41:44 +0000186 ComplexPairTy VisitUnaryNot (const UnaryOperator *E);
Sebastian Redl05189992008-11-11 17:56:53 +0000187 // LNot,Real,Imag never return complex.
Chris Lattnere98a11c2007-08-21 20:41:44 +0000188 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) {
189 return Visit(E->getSubExpr());
190 }
Chris Lattner04421082008-04-08 04:40:51 +0000191 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
192 return Visit(DAE->getExpr());
193 }
Richard Smithc3bf52c2013-04-20 22:23:05 +0000194 ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
195 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
196 return Visit(DIE->getExpr());
197 }
John McCall4765fa02010-12-06 08:20:24 +0000198 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) {
John McCall1a343eb2011-11-10 08:15:53 +0000199 CGF.enterFullExpression(E);
200 CodeGenFunction::RunCleanupsScope Scope(CGF);
201 return Visit(E->getSubExpr());
Anders Carlsson4b76b4f2009-05-31 00:12:05 +0000202 }
Douglas Gregored8abf12010-07-08 06:14:04 +0000203 ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000204 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
John McCall9d232c82013-03-07 21:37:08 +0000205 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000206 llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem));
Argyrios Kyrtzidis7267f782008-08-23 19:35:47 +0000207 return ComplexPairTy(Null, Null);
208 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000209 ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
210 assert(E->getType()->isAnyComplexType() && "Expected complex type!");
John McCall9d232c82013-03-07 21:37:08 +0000211 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000212 llvm::Constant *Null =
Owen Andersonc9c88b42009-07-31 20:28:54 +0000213 llvm::Constant::getNullValue(CGF.ConvertType(Elem));
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000214 return ComplexPairTy(Null, Null);
215 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000216
Chris Lattnerab340c22007-08-26 22:09:01 +0000217 struct BinOpInfo {
218 ComplexPairTy LHS;
219 ComplexPairTy RHS;
220 QualType Ty; // Computation Type.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000221 };
222
Chris Lattnerab340c22007-08-26 22:09:01 +0000223 BinOpInfo EmitBinOps(const BinaryOperator *E);
John McCallb418d742010-11-16 10:08:07 +0000224 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
225 ComplexPairTy (ComplexExprEmitter::*Func)
226 (const BinOpInfo &),
Eli Friedman0934e182013-06-12 01:40:06 +0000227 RValue &Val);
Chris Lattnerab340c22007-08-26 22:09:01 +0000228 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E,
229 ComplexPairTy (ComplexExprEmitter::*Func)
230 (const BinOpInfo &));
231
232 ComplexPairTy EmitBinAdd(const BinOpInfo &Op);
233 ComplexPairTy EmitBinSub(const BinOpInfo &Op);
234 ComplexPairTy EmitBinMul(const BinOpInfo &Op);
235 ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000236
Stephen Hines176edba2014-12-01 14:53:08 -0800237 ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
238 const BinOpInfo &Op);
239
Chris Lattnerab340c22007-08-26 22:09:01 +0000240 ComplexPairTy VisitBinAdd(const BinaryOperator *E) {
241 return EmitBinAdd(EmitBinOps(E));
242 }
243 ComplexPairTy VisitBinSub(const BinaryOperator *E) {
244 return EmitBinSub(EmitBinOps(E));
245 }
John McCallb418d742010-11-16 10:08:07 +0000246 ComplexPairTy VisitBinMul(const BinaryOperator *E) {
247 return EmitBinMul(EmitBinOps(E));
248 }
Chris Lattnerab340c22007-08-26 22:09:01 +0000249 ComplexPairTy VisitBinDiv(const BinaryOperator *E) {
250 return EmitBinDiv(EmitBinOps(E));
251 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000252
Chris Lattnerab340c22007-08-26 22:09:01 +0000253 // Compound assignments.
254 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) {
255 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd);
256 }
257 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) {
258 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub);
259 }
260 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) {
261 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul);
262 }
263 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) {
264 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv);
265 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000266
Chris Lattner612c40c2007-08-26 21:27:07 +0000267 // GCC rejects rem/and/or/xor for integer complex.
Chris Lattner4034edb2007-08-21 17:12:50 +0000268 // Logical and/or always return int, never complex.
Chris Lattner58dee102007-08-21 16:57:55 +0000269
270 // No comparisons produce a complex result.
John McCallb418d742010-11-16 10:08:07 +0000271
272 LValue EmitBinAssignLValue(const BinaryOperator *E,
273 ComplexPairTy &Val);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000274 ComplexPairTy VisitBinAssign (const BinaryOperator *E);
Chris Lattner756a4d82007-08-21 17:15:50 +0000275 ComplexPairTy VisitBinComma (const BinaryOperator *E);
276
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000277
John McCall56ca35d2011-02-17 10:25:35 +0000278 ComplexPairTy
279 VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO);
Chris Lattnerd0b12032007-08-24 02:18:47 +0000280 ComplexPairTy VisitChooseExpr(ChooseExpr *CE);
Eli Friedmanc4777f12008-05-13 23:11:35 +0000281
282 ComplexPairTy VisitInitListExpr(InitListExpr *E);
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000283
Eli Friedmanfce47152012-02-27 20:26:13 +0000284 ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
285 return EmitLoadOfLValue(E);
286 }
287
Daniel Dunbar4e484b82009-02-10 03:03:30 +0000288 ComplexPairTy VisitVAArgExpr(VAArgExpr *E);
Eli Friedman276b0612011-10-11 02:20:01 +0000289
290 ComplexPairTy VisitAtomicExpr(AtomicExpr *E) {
291 return CGF.EmitAtomicExpr(E).getComplexVal();
292 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000293};
294} // end anonymous namespace.
295
296//===----------------------------------------------------------------------===//
297// Utilities
298//===----------------------------------------------------------------------===//
299
John McCall9d232c82013-03-07 21:37:08 +0000300/// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000301/// load the real and imaginary pieces, returning them as Real/Imag.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000302ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue,
303 SourceLocation loc) {
John McCall9d232c82013-03-07 21:37:08 +0000304 assert(lvalue.isSimple() && "non-simple complex l-value?");
John McCall9eda3ab2013-03-07 21:37:17 +0000305 if (lvalue.getType()->isAtomicType())
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000306 return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal();
John McCall9eda3ab2013-03-07 21:37:17 +0000307
John McCall9d232c82013-03-07 21:37:08 +0000308 llvm::Value *SrcPtr = lvalue.getAddress();
309 bool isVolatile = lvalue.isVolatileQualified();
JF Bastienab96e562013-07-17 05:57:42 +0000310 unsigned AlignR = lvalue.getAlignment().getQuantity();
311 ASTContext &C = CGF.getContext();
312 QualType ComplexTy = lvalue.getType();
313 unsigned ComplexAlign = C.getTypeAlignInChars(ComplexTy).getQuantity();
314 unsigned AlignI = std::min(AlignR, ComplexAlign);
John McCall9d232c82013-03-07 21:37:08 +0000315
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700316 llvm::Value *Real=nullptr, *Imag=nullptr;
Chris Lattner6b5d0bf2007-08-26 22:47:40 +0000317
John McCallff624c12010-11-14 09:40:28 +0000318 if (!IgnoreReal || isVolatile) {
Benjamin Kramer56a44f72009-11-29 19:51:45 +0000319 llvm::Value *RealP = Builder.CreateStructGEP(SrcPtr, 0,
320 SrcPtr->getName() + ".realp");
JF Bastienab96e562013-07-17 05:57:42 +0000321 Real = Builder.CreateAlignedLoad(RealP, AlignR, isVolatile,
322 SrcPtr->getName() + ".real");
Mike Stump7f79f9b2009-05-29 15:46:01 +0000323 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000324
John McCallff624c12010-11-14 09:40:28 +0000325 if (!IgnoreImag || isVolatile) {
Benjamin Kramer56a44f72009-11-29 19:51:45 +0000326 llvm::Value *ImagP = Builder.CreateStructGEP(SrcPtr, 1,
327 SrcPtr->getName() + ".imagp");
JF Bastienab96e562013-07-17 05:57:42 +0000328 Imag = Builder.CreateAlignedLoad(ImagP, AlignI, isVolatile,
329 SrcPtr->getName() + ".imag");
Mike Stump7f79f9b2009-05-29 15:46:01 +0000330 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000331 return ComplexPairTy(Real, Imag);
332}
333
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000334/// EmitStoreOfComplex - Store the specified real/imag parts into the
335/// specified value pointer.
John McCall9d232c82013-03-07 21:37:08 +0000336void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val,
337 LValue lvalue,
338 bool isInit) {
John McCall9eda3ab2013-03-07 21:37:17 +0000339 if (lvalue.getType()->isAtomicType())
340 return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit);
341
John McCall9d232c82013-03-07 21:37:08 +0000342 llvm::Value *Ptr = lvalue.getAddress();
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000343 llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real");
344 llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag");
JF Bastienab96e562013-07-17 05:57:42 +0000345 unsigned AlignR = lvalue.getAlignment().getQuantity();
346 ASTContext &C = CGF.getContext();
347 QualType ComplexTy = lvalue.getType();
348 unsigned ComplexAlign = C.getTypeAlignInChars(ComplexTy).getQuantity();
349 unsigned AlignI = std::min(AlignR, ComplexAlign);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000350
JF Bastienab96e562013-07-17 05:57:42 +0000351 Builder.CreateAlignedStore(Val.first, RealPtr, AlignR,
352 lvalue.isVolatileQualified());
353 Builder.CreateAlignedStore(Val.second, ImagPtr, AlignI,
354 lvalue.isVolatileQualified());
Chris Lattner46d7d9f2007-08-21 17:28:34 +0000355}
356
357
358
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000359//===----------------------------------------------------------------------===//
360// Visitor Methods
361//===----------------------------------------------------------------------===//
362
Chris Lattner776c6492007-08-21 18:51:13 +0000363ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) {
Fariborz Jahanian776b7902011-09-02 20:03:16 +0000364 CGF.ErrorUnsupported(E, "complex expression");
365 llvm::Type *EltTy =
John McCall9d232c82013-03-07 21:37:08 +0000366 CGF.ConvertType(getComplexType(E->getType())->getElementType());
Fariborz Jahanian776b7902011-09-02 20:03:16 +0000367 llvm::Value *U = llvm::UndefValue::get(EltTy);
368 return ComplexPairTy(U, U);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000369}
370
Chris Lattnerd272ff02007-08-26 05:57:57 +0000371ComplexPairTy ComplexExprEmitter::
372VisitImaginaryLiteral(const ImaginaryLiteral *IL) {
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000373 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr());
John McCall8d3d6c92011-01-13 02:03:06 +0000374 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag);
Chris Lattnerdb68f1b2007-08-26 03:51:12 +0000375}
376
377
Chris Lattner419d25e2007-08-23 21:38:16 +0000378ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone9f2f452009-05-27 03:37:57 +0000379 if (E->getCallReturnType()->isReferenceType())
380 return EmitLoadOfLValue(E);
381
Chris Lattner9b655512007-08-31 22:49:20 +0000382 return CGF.EmitCallExpr(E).getComplexVal();
Chris Lattner419d25e2007-08-23 21:38:16 +0000383}
384
Chris Lattner7e20d092007-08-31 22:51:38 +0000385ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) {
John McCall150b4622011-01-26 04:00:11 +0000386 CodeGenFunction::StmtExprEvaluation eval(CGF);
Eli Friedman2ac2fa72013-06-10 22:04:49 +0000387 llvm::Value *RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true);
388 assert(RetAlloca && "Expected complex return value");
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000389 return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()),
390 E->getExprLoc());
Chris Lattner7e20d092007-08-31 22:51:38 +0000391}
392
Chris Lattnerab340c22007-08-26 22:09:01 +0000393/// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType.
394ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
395 QualType SrcType,
396 QualType DestType) {
397 // Get the src/dest element type.
John McCall9d232c82013-03-07 21:37:08 +0000398 SrcType = SrcType->castAs<ComplexType>()->getElementType();
399 DestType = DestType->castAs<ComplexType>()->getElementType();
Chris Lattnerd272ff02007-08-26 05:57:57 +0000400
Daniel Dunbar56f0d162009-01-29 06:44:03 +0000401 // C99 6.3.1.6: When a value of complex type is converted to another
Daniel Dunbare12d8e32009-01-26 18:02:34 +0000402 // complex type, both the real and imaginary parts follow the conversion
Chris Lattnerab340c22007-08-26 22:09:01 +0000403 // rules for the corresponding real types.
404 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType);
405 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType);
406 return Val;
407}
408
Eli Friedman0934e182013-06-12 01:40:06 +0000409ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
410 QualType SrcType,
411 QualType DestType) {
412 // Convert the input element to the element type of the complex.
413 DestType = DestType->castAs<ComplexType>()->getElementType();
414 Val = CGF.EmitScalarConversion(Val, SrcType, DestType);
415
416 // Return (realval, 0).
417 return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType()));
418}
419
Stephen Hines176edba2014-12-01 14:53:08 -0800420ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
Douglas Gregor77d439a2010-07-14 21:35:45 +0000421 QualType DestTy) {
John McCallf6a16482010-12-04 03:47:34 +0000422 switch (CK) {
Eli Friedman65949422011-06-25 02:58:47 +0000423 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
John McCallf6a16482010-12-04 03:47:34 +0000424
David Chisnall7a7ee302012-01-16 17:27:18 +0000425 // Atomic to non-atomic casts may be more than a no-op for some platforms and
426 // for some types.
427 case CK_AtomicToNonAtomic:
428 case CK_NonAtomicToAtomic:
John McCallf6a16482010-12-04 03:47:34 +0000429 case CK_NoOp:
430 case CK_LValueToRValue:
Eli Friedman65949422011-06-25 02:58:47 +0000431 case CK_UserDefinedConversion:
John McCallf6a16482010-12-04 03:47:34 +0000432 return Visit(Op);
433
Eli Friedman65949422011-06-25 02:58:47 +0000434 case CK_LValueBitCast: {
John McCall9d232c82013-03-07 21:37:08 +0000435 LValue origLV = CGF.EmitLValue(Op);
436 llvm::Value *V = origLV.getAddress();
Stephen Hines651f13c2014-04-23 16:59:28 -0700437 V = Builder.CreateBitCast(V,
Eli Friedman65949422011-06-25 02:58:47 +0000438 CGF.ConvertType(CGF.getContext().getPointerType(DestTy)));
John McCall9d232c82013-03-07 21:37:08 +0000439 return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy,
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000440 origLV.getAlignment()),
441 Op->getExprLoc());
Douglas Gregor77d439a2010-07-14 21:35:45 +0000442 }
Chris Lattnerd272ff02007-08-26 05:57:57 +0000443
Eli Friedman65949422011-06-25 02:58:47 +0000444 case CK_BitCast:
445 case CK_BaseToDerived:
446 case CK_DerivedToBase:
447 case CK_UncheckedDerivedToBase:
448 case CK_Dynamic:
449 case CK_ToUnion:
450 case CK_ArrayToPointerDecay:
451 case CK_FunctionToPointerDecay:
452 case CK_NullToPointer:
453 case CK_NullToMemberPointer:
454 case CK_BaseToDerivedMemberPointer:
455 case CK_DerivedToBaseMemberPointer:
456 case CK_MemberPointerToBoolean:
John McCall4d4e5c12012-02-15 01:22:51 +0000457 case CK_ReinterpretMemberPointer:
Eli Friedman65949422011-06-25 02:58:47 +0000458 case CK_ConstructorConversion:
459 case CK_IntegralToPointer:
460 case CK_PointerToIntegral:
461 case CK_PointerToBoolean:
462 case CK_ToVoid:
463 case CK_VectorSplat:
464 case CK_IntegralCast:
465 case CK_IntegralToBoolean:
466 case CK_IntegralToFloating:
467 case CK_FloatingToIntegral:
468 case CK_FloatingToBoolean:
469 case CK_FloatingCast:
John McCall1d9b3b22011-09-09 05:25:32 +0000470 case CK_CPointerToObjCPointerCast:
471 case CK_BlockPointerToObjCPointerCast:
Eli Friedman65949422011-06-25 02:58:47 +0000472 case CK_AnyPointerToBlockPointerCast:
473 case CK_ObjCObjectLValueCast:
474 case CK_FloatingComplexToReal:
475 case CK_FloatingComplexToBoolean:
476 case CK_IntegralComplexToReal:
477 case CK_IntegralComplexToBoolean:
John McCall33e56f32011-09-10 06:18:15 +0000478 case CK_ARCProduceObject:
479 case CK_ARCConsumeObject:
480 case CK_ARCReclaimReturnedObject:
481 case CK_ARCExtendBlockObject:
Douglas Gregorac1303e2012-02-22 05:02:47 +0000482 case CK_CopyAndAutoreleaseBlockObject:
Eli Friedmana6c66ce2012-08-31 00:14:07 +0000483 case CK_BuiltinFnToFnPtr:
Guy Benyeie6b9d802013-01-20 12:31:11 +0000484 case CK_ZeroToOCLEvent:
Stephen Hines651f13c2014-04-23 16:59:28 -0700485 case CK_AddressSpaceConversion:
Eli Friedman65949422011-06-25 02:58:47 +0000486 llvm_unreachable("invalid cast kind for complex value");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000487
Eli Friedman65949422011-06-25 02:58:47 +0000488 case CK_FloatingRealToComplex:
Eli Friedman0934e182013-06-12 01:40:06 +0000489 case CK_IntegralRealToComplex:
490 return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op),
491 Op->getType(), DestTy);
Eli Friedman65949422011-06-25 02:58:47 +0000492
493 case CK_FloatingComplexCast:
494 case CK_FloatingComplexToIntegralComplex:
495 case CK_IntegralComplexCast:
496 case CK_IntegralComplexToFloatingComplex:
497 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
498 }
499
500 llvm_unreachable("unknown cast resulting in complex value");
Chris Lattnerd272ff02007-08-26 05:57:57 +0000501}
502
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000503ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000504 TestAndClearIgnoreReal();
505 TestAndClearIgnoreImag();
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000506 ComplexPairTy Op = Visit(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000507
Chris Lattner87415d22009-06-17 06:36:24 +0000508 llvm::Value *ResR, *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000509 if (Op.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000510 ResR = Builder.CreateFNeg(Op.first, "neg.r");
511 ResI = Builder.CreateFNeg(Op.second, "neg.i");
512 } else {
513 ResR = Builder.CreateNeg(Op.first, "neg.r");
514 ResI = Builder.CreateNeg(Op.second, "neg.i");
515 }
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000516 return ComplexPairTy(ResR, ResI);
517}
518
Chris Lattnere98a11c2007-08-21 20:41:44 +0000519ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000520 TestAndClearIgnoreReal();
521 TestAndClearIgnoreImag();
Chris Lattnere98a11c2007-08-21 20:41:44 +0000522 // ~(a+ib) = a + i*-b
523 ComplexPairTy Op = Visit(E->getSubExpr());
Chris Lattner87415d22009-06-17 06:36:24 +0000524 llvm::Value *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000525 if (Op.second->getType()->isFloatingPointTy())
Chris Lattner87415d22009-06-17 06:36:24 +0000526 ResI = Builder.CreateFNeg(Op.second, "conj.i");
527 else
528 ResI = Builder.CreateNeg(Op.second, "conj.i");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000529
Chris Lattnere98a11c2007-08-21 20:41:44 +0000530 return ComplexPairTy(Op.first, ResI);
531}
Chris Lattnerfa2b9c92007-08-21 20:08:23 +0000532
Chris Lattnerab340c22007-08-26 22:09:01 +0000533ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000534 llvm::Value *ResR, *ResI;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000535
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000536 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Chris Lattner87415d22009-06-17 06:36:24 +0000537 ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r");
Stephen Hines176edba2014-12-01 14:53:08 -0800538 if (Op.LHS.second && Op.RHS.second)
539 ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i");
540 else
541 ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second;
542 assert(ResI && "Only one operand may be real!");
Chris Lattner87415d22009-06-17 06:36:24 +0000543 } else {
544 ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r");
Stephen Hines176edba2014-12-01 14:53:08 -0800545 assert(Op.LHS.second && Op.RHS.second &&
546 "Both operands of integer complex operators must be complex!");
Chris Lattner87415d22009-06-17 06:36:24 +0000547 ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i");
548 }
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000549 return ComplexPairTy(ResR, ResI);
550}
551
Chris Lattnerab340c22007-08-26 22:09:01 +0000552ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000553 llvm::Value *ResR, *ResI;
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000554 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Stephen Hines176edba2014-12-01 14:53:08 -0800555 ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r");
556 if (Op.LHS.second && Op.RHS.second)
557 ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i");
558 else
559 ResI = Op.LHS.second ? Op.LHS.second
560 : Builder.CreateFNeg(Op.RHS.second, "sub.i");
561 assert(ResI && "Only one operand may be real!");
Chris Lattner87415d22009-06-17 06:36:24 +0000562 } else {
Stephen Hines176edba2014-12-01 14:53:08 -0800563 ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r");
564 assert(Op.LHS.second && Op.RHS.second &&
565 "Both operands of integer complex operators must be complex!");
Chris Lattner87415d22009-06-17 06:36:24 +0000566 ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i");
567 }
Chris Lattner6bc1ade2007-08-23 23:46:33 +0000568 return ComplexPairTy(ResR, ResI);
569}
570
Stephen Hines176edba2014-12-01 14:53:08 -0800571/// \brief Emit a libcall for a binary operation on complex types.
572ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName,
573 const BinOpInfo &Op) {
574 CallArgList Args;
575 Args.add(RValue::get(Op.LHS.first),
576 Op.Ty->castAs<ComplexType>()->getElementType());
577 Args.add(RValue::get(Op.LHS.second),
578 Op.Ty->castAs<ComplexType>()->getElementType());
579 Args.add(RValue::get(Op.RHS.first),
580 Op.Ty->castAs<ComplexType>()->getElementType());
581 Args.add(RValue::get(Op.RHS.second),
582 Op.Ty->castAs<ComplexType>()->getElementType());
Chris Lattner6bc1ade2007-08-23 23:46:33 +0000583
Stephen Hines176edba2014-12-01 14:53:08 -0800584 // We *must* use the full CG function call building logic here because the
585 // complex type has special ABI handling.
586 const CGFunctionInfo &FuncInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall(
587 Op.Ty, Args, FunctionType::ExtInfo(), RequiredArgs::All);
588 llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo);
589 llvm::Constant *Func = CGF.CGM.CreateRuntimeFunction(FTy, LibCallName);
590
591 return CGF.EmitCall(FuncInfo, Func, ReturnValueSlot(), Args).getComplexVal();
592}
593
594/// \brief Lookup the libcall name for a given floating point type complex
595/// multiply.
596static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) {
597 switch (Ty->getTypeID()) {
598 default:
599 llvm_unreachable("Unsupported floating point type!");
600 case llvm::Type::HalfTyID:
601 return "__mulhc3";
602 case llvm::Type::FloatTyID:
603 return "__mulsc3";
604 case llvm::Type::DoubleTyID:
605 return "__muldc3";
606 case llvm::Type::PPC_FP128TyID:
607 return "__multc3";
608 case llvm::Type::X86_FP80TyID:
609 return "__mulxc3";
610 case llvm::Type::FP128TyID:
611 return "__multc3";
612 }
613}
614
615// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
616// typed values.
Chris Lattnerab340c22007-08-26 22:09:01 +0000617ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
Chris Lattner87415d22009-06-17 06:36:24 +0000618 using llvm::Value;
619 Value *ResR, *ResI;
Stephen Hines176edba2014-12-01 14:53:08 -0800620 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000621
Duncan Sandsf177d9d2010-02-15 16:14:01 +0000622 if (Op.LHS.first->getType()->isFloatingPointTy()) {
Stephen Hines176edba2014-12-01 14:53:08 -0800623 // The general formulation is:
624 // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c)
625 //
626 // But we can fold away components which would be zero due to a real
627 // operand according to C11 Annex G.5.1p2.
628 // FIXME: C11 also provides for imaginary types which would allow folding
629 // still more of this within the type system.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000630
Stephen Hines176edba2014-12-01 14:53:08 -0800631 if (Op.LHS.second && Op.RHS.second) {
632 // If both operands are complex, emit the core math directly, and then
633 // test for NaNs. If we find NaNs in the result, we delegate to a libcall
634 // to carefully re-compute the correct infinity representation if
635 // possible. The expectation is that the presence of NaNs here is
636 // *extremely* rare, and so the cost of the libcall is almost irrelevant.
637 // This is good, because the libcall re-computes the core multiplication
638 // exactly the same as we do here and re-tests for NaNs in order to be
639 // a generic complex*complex libcall.
640
641 // First compute the four products.
642 Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac");
643 Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd");
644 Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad");
645 Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc");
646
647 // The real part is the difference of the first two, the imaginary part is
648 // the sum of the second.
649 ResR = Builder.CreateFSub(AC, BD, "mul_r");
650 ResI = Builder.CreateFAdd(AD, BC, "mul_i");
651
652 // Emit the test for the real part becoming NaN and create a branch to
653 // handle it. We test for NaN by comparing the number to itself.
654 Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
655 llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont");
656 llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan");
657 llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB);
658 llvm::BasicBlock *OrigBB = Branch->getParent();
659
660 // Give hint that we very much don't expect to see NaNs.
661 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
662 llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1);
663 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
664
665 // Now test the imaginary part and create its branch.
666 CGF.EmitBlock(INaNBB);
667 Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp");
668 llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall");
669 Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB);
670 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight);
671
672 // Now emit the libcall on this slowest of the slow paths.
673 CGF.EmitBlock(LibCallBB);
674 Value *LibCallR, *LibCallI;
675 std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall(
676 getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op);
677 Builder.CreateBr(ContBB);
678
679 // Finally continue execution by phi-ing together the different
680 // computation paths.
681 CGF.EmitBlock(ContBB);
682 llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi");
683 RealPHI->addIncoming(ResR, OrigBB);
684 RealPHI->addIncoming(ResR, INaNBB);
685 RealPHI->addIncoming(LibCallR, LibCallBB);
686 llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi");
687 ImagPHI->addIncoming(ResI, OrigBB);
688 ImagPHI->addIncoming(ResI, INaNBB);
689 ImagPHI->addIncoming(LibCallI, LibCallBB);
690 return ComplexPairTy(RealPHI, ImagPHI);
691 }
692 assert((Op.LHS.second || Op.RHS.second) &&
693 "At least one operand must be complex!");
694
695 // If either of the operands is a real rather than a complex, the
696 // imaginary component is ignored when computing the real component of the
697 // result.
698 ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl");
699
700 ResI = Op.LHS.second
701 ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il")
702 : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir");
Chris Lattner87415d22009-06-17 06:36:24 +0000703 } else {
Stephen Hines176edba2014-12-01 14:53:08 -0800704 assert(Op.LHS.second && Op.RHS.second &&
705 "Both operands of integer complex operators must be complex!");
Chris Lattner87415d22009-06-17 06:36:24 +0000706 Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl");
Stephen Hines176edba2014-12-01 14:53:08 -0800707 Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr");
708 ResR = Builder.CreateSub(ResRl, ResRr, "mul.r");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000709
Chris Lattner87415d22009-06-17 06:36:24 +0000710 Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il");
711 Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir");
Stephen Hines176edba2014-12-01 14:53:08 -0800712 ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i");
Chris Lattner87415d22009-06-17 06:36:24 +0000713 }
Chris Lattner2823c192007-08-21 16:34:16 +0000714 return ComplexPairTy(ResR, ResI);
715}
716
Stephen Hines176edba2014-12-01 14:53:08 -0800717// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
718// typed values.
Chris Lattnerab340c22007-08-26 22:09:01 +0000719ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
720 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
721 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000722
Chris Lattner3219c5d2007-08-26 21:24:19 +0000723
724 llvm::Value *DSTr, *DSTi;
Stephen Hines176edba2014-12-01 14:53:08 -0800725 if (LHSr->getType()->isFloatingPointTy()) {
726 // If we have a complex operand on the RHS, we delegate to a libcall to
727 // handle all of the complexities and minimize underflow/overflow cases.
728 //
729 // FIXME: We would be able to avoid the libcall in many places if we
730 // supported imaginary types in addition to complex types.
731 if (RHSi) {
732 BinOpInfo LibCallOp = Op;
733 // If LHS was a real, supply a null imaginary part.
734 if (!LHSi)
735 LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000736
Stephen Hines176edba2014-12-01 14:53:08 -0800737 StringRef LibCallName;
738 switch (LHSr->getType()->getTypeID()) {
739 default:
740 llvm_unreachable("Unsupported floating point type!");
741 case llvm::Type::HalfTyID:
742 return EmitComplexBinOpLibCall("__divhc3", LibCallOp);
743 case llvm::Type::FloatTyID:
744 return EmitComplexBinOpLibCall("__divsc3", LibCallOp);
745 case llvm::Type::DoubleTyID:
746 return EmitComplexBinOpLibCall("__divdc3", LibCallOp);
747 case llvm::Type::PPC_FP128TyID:
748 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
749 case llvm::Type::X86_FP80TyID:
750 return EmitComplexBinOpLibCall("__divxc3", LibCallOp);
751 case llvm::Type::FP128TyID:
752 return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
753 }
754 }
755 assert(LHSi && "Can have at most one non-complex operand!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000756
Stephen Hines176edba2014-12-01 14:53:08 -0800757 DSTr = Builder.CreateFDiv(LHSr, RHSr);
758 DSTi = Builder.CreateFDiv(LHSi, RHSr);
Chris Lattner3219c5d2007-08-26 21:24:19 +0000759 } else {
Stephen Hines176edba2014-12-01 14:53:08 -0800760 assert(Op.LHS.second && Op.RHS.second &&
761 "Both operands of integer complex operators must be complex!");
Chris Lattner87415d22009-06-17 06:36:24 +0000762 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
Benjamin Kramer578faa82011-09-27 21:06:10 +0000763 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
764 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
765 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000766
Benjamin Kramer578faa82011-09-27 21:06:10 +0000767 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c
768 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d
769 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000770
Benjamin Kramer578faa82011-09-27 21:06:10 +0000771 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c
772 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d
773 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000774
John McCall9d232c82013-03-07 21:37:08 +0000775 if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) {
Benjamin Kramer578faa82011-09-27 21:06:10 +0000776 DSTr = Builder.CreateUDiv(Tmp3, Tmp6);
777 DSTi = Builder.CreateUDiv(Tmp9, Tmp6);
Chris Lattner3219c5d2007-08-26 21:24:19 +0000778 } else {
Benjamin Kramer578faa82011-09-27 21:06:10 +0000779 DSTr = Builder.CreateSDiv(Tmp3, Tmp6);
780 DSTi = Builder.CreateSDiv(Tmp9, Tmp6);
Chris Lattner3219c5d2007-08-26 21:24:19 +0000781 }
782 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000783
Chris Lattner3219c5d2007-08-26 21:24:19 +0000784 return ComplexPairTy(DSTr, DSTi);
785}
786
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000787ComplexExprEmitter::BinOpInfo
Chris Lattnerab340c22007-08-26 22:09:01 +0000788ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000789 TestAndClearIgnoreReal();
790 TestAndClearIgnoreImag();
Chris Lattnerab340c22007-08-26 22:09:01 +0000791 BinOpInfo Ops;
Stephen Hines176edba2014-12-01 14:53:08 -0800792 if (E->getLHS()->getType()->isRealFloatingType())
793 Ops.LHS = ComplexPairTy(CGF.EmitScalarExpr(E->getLHS()), nullptr);
794 else
795 Ops.LHS = Visit(E->getLHS());
796 if (E->getRHS()->getType()->isRealFloatingType())
797 Ops.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
798 else
799 Ops.RHS = Visit(E->getRHS());
800
Chris Lattnerab340c22007-08-26 22:09:01 +0000801 Ops.Ty = E->getType();
802 return Ops;
803}
804
805
John McCallb418d742010-11-16 10:08:07 +0000806LValue ComplexExprEmitter::
807EmitCompoundAssignLValue(const CompoundAssignOperator *E,
808 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&),
Eli Friedman0934e182013-06-12 01:40:06 +0000809 RValue &Val) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000810 TestAndClearIgnoreReal();
811 TestAndClearIgnoreImag();
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000812 QualType LHSTy = E->getLHS()->getType();
Chris Lattnerab340c22007-08-26 22:09:01 +0000813
814 BinOpInfo OpInfo;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000815
Mike Stump7f79f9b2009-05-29 15:46:01 +0000816 // Load the RHS and LHS operands.
817 // __block variables need to have the rhs evaluated first, plus this should
John McCall641ca7d2010-11-16 05:45:35 +0000818 // improve codegen a little.
Eli Friedmanab3a8522009-03-28 01:22:36 +0000819 OpInfo.Ty = E->getComputationResultType();
Stephen Hines176edba2014-12-01 14:53:08 -0800820 QualType ComplexElementTy = cast<ComplexType>(OpInfo.Ty)->getElementType();
John McCall641ca7d2010-11-16 05:45:35 +0000821
822 // The RHS should have been converted to the computation type.
Stephen Hines176edba2014-12-01 14:53:08 -0800823 if (E->getRHS()->getType()->isRealFloatingType()) {
824 assert(
825 CGF.getContext()
826 .hasSameUnqualifiedType(ComplexElementTy, E->getRHS()->getType()));
827 OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr);
828 } else {
829 assert(CGF.getContext()
830 .hasSameUnqualifiedType(OpInfo.Ty, E->getRHS()->getType()));
831 OpInfo.RHS = Visit(E->getRHS());
832 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700833
Daniel Dunbar120bc772010-06-29 22:44:21 +0000834 LValue LHS = CGF.EmitLValue(E->getLHS());
John McCall0e800c92010-12-04 08:14:53 +0000835
Eli Friedman0934e182013-06-12 01:40:06 +0000836 // Load from the l-value and convert it.
837 if (LHSTy->isAnyComplexType()) {
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000838 ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, E->getExprLoc());
Eli Friedman0934e182013-06-12 01:40:06 +0000839 OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty);
840 } else {
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000841 llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, E->getExprLoc());
Stephen Hines176edba2014-12-01 14:53:08 -0800842 // For floating point real operands we can directly pass the scalar form
843 // to the binary operator emission and potentially get more efficient code.
844 if (LHSTy->isRealFloatingType()) {
845 if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy))
846 LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy);
847 OpInfo.LHS = ComplexPairTy(LHSVal, nullptr);
848 } else {
849 OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty);
850 }
Eli Friedman0934e182013-06-12 01:40:06 +0000851 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000852
Chris Lattnerab340c22007-08-26 22:09:01 +0000853 // Expand the binary operator.
854 ComplexPairTy Result = (this->*Func)(OpInfo);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000855
Eli Friedman0934e182013-06-12 01:40:06 +0000856 // Truncate the result and store it into the LHS lvalue.
857 if (LHSTy->isAnyComplexType()) {
858 ComplexPairTy ResVal = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy);
859 EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false);
860 Val = RValue::getComplex(ResVal);
861 } else {
862 llvm::Value *ResVal =
863 CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy);
864 CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false);
865 Val = RValue::get(ResVal);
866 }
Daniel Dunbar120bc772010-06-29 22:44:21 +0000867
John McCallb418d742010-11-16 10:08:07 +0000868 return LHS;
Chris Lattnerab340c22007-08-26 22:09:01 +0000869}
Chris Lattner3219c5d2007-08-26 21:24:19 +0000870
John McCallb418d742010-11-16 10:08:07 +0000871// Compound assignments.
872ComplexPairTy ComplexExprEmitter::
873EmitCompoundAssign(const CompoundAssignOperator *E,
874 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){
Eli Friedman0934e182013-06-12 01:40:06 +0000875 RValue Val;
John McCallb418d742010-11-16 10:08:07 +0000876 LValue LV = EmitCompoundAssignLValue(E, Func, Val);
877
878 // The result of an assignment in C is the assigned r-value.
Richard Smith7edf9e32012-11-01 22:30:59 +0000879 if (!CGF.getLangOpts().CPlusPlus)
Eli Friedman0934e182013-06-12 01:40:06 +0000880 return Val.getComplexVal();
John McCallb418d742010-11-16 10:08:07 +0000881
John McCallb418d742010-11-16 10:08:07 +0000882 // If the lvalue is non-volatile, return the computed value of the assignment.
883 if (!LV.isVolatileQualified())
Eli Friedman0934e182013-06-12 01:40:06 +0000884 return Val.getComplexVal();
John McCallb418d742010-11-16 10:08:07 +0000885
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000886 return EmitLoadOfLValue(LV, E->getExprLoc());
John McCallb418d742010-11-16 10:08:07 +0000887}
888
889LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
890 ComplexPairTy &Val) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700891 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
Douglas Gregor63982352010-07-13 18:40:04 +0000892 E->getRHS()->getType()) &&
Chris Lattner96196622008-07-26 22:37:01 +0000893 "Invalid assignment");
John McCallb418d742010-11-16 10:08:07 +0000894 TestAndClearIgnoreReal();
895 TestAndClearIgnoreImag();
896
John McCallcd940a12010-12-06 06:10:02 +0000897 // Emit the RHS. __block variables need the RHS evaluated first.
John McCallb418d742010-11-16 10:08:07 +0000898 Val = Visit(E->getRHS());
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000899
900 // Compute the address to store into.
901 LValue LHS = CGF.EmitLValue(E->getLHS());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000902
Daniel Dunbar120bc772010-06-29 22:44:21 +0000903 // Store the result value into the LHS lvalue.
John McCall9d232c82013-03-07 21:37:08 +0000904 EmitStoreOfComplex(Val, LHS, /*isInit*/ false);
Daniel Dunbar42f963d2009-06-10 04:38:50 +0000905
John McCallb418d742010-11-16 10:08:07 +0000906 return LHS;
907}
Daniel Dunbar120bc772010-06-29 22:44:21 +0000908
John McCallb418d742010-11-16 10:08:07 +0000909ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) {
910 ComplexPairTy Val;
911 LValue LV = EmitBinAssignLValue(E, Val);
912
913 // The result of an assignment in C is the assigned r-value.
Richard Smith7edf9e32012-11-01 22:30:59 +0000914 if (!CGF.getLangOpts().CPlusPlus)
John McCallb418d742010-11-16 10:08:07 +0000915 return Val;
916
John McCallb418d742010-11-16 10:08:07 +0000917 // If the lvalue is non-volatile, return the computed value of the assignment.
918 if (!LV.isVolatileQualified())
919 return Val;
920
Nick Lewycky4ee7dc22013-10-02 02:29:49 +0000921 return EmitLoadOfLValue(LV, E->getExprLoc());
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000922}
923
Chris Lattner756a4d82007-08-21 17:15:50 +0000924ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) {
John McCall2a416372010-12-05 02:00:02 +0000925 CGF.EmitIgnoredExpr(E->getLHS());
Chris Lattner756a4d82007-08-21 17:15:50 +0000926 return Visit(E->getRHS());
927}
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000928
929ComplexPairTy ComplexExprEmitter::
John McCall56ca35d2011-02-17 10:25:35 +0000930VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000931 TestAndClearIgnoreReal();
932 TestAndClearIgnoreImag();
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000933 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
934 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
935 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000936
John McCall56ca35d2011-02-17 10:25:35 +0000937 // Bind the common expression if necessary.
Eli Friedmand97927d2012-01-06 20:42:20 +0000938 CodeGenFunction::OpaqueValueMapping binding(CGF, E);
John McCall150b4622011-01-26 04:00:11 +0000939
Stephen Hines651f13c2014-04-23 16:59:28 -0700940 RegionCounter Cnt = CGF.getPGORegionCounter(E);
John McCall56ca35d2011-02-17 10:25:35 +0000941 CodeGenFunction::ConditionalEvaluation eval(CGF);
Stephen Hines651f13c2014-04-23 16:59:28 -0700942 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock, Cnt.getCount());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000943
John McCall150b4622011-01-26 04:00:11 +0000944 eval.begin(CGF);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000945 CGF.EmitBlock(LHSBlock);
Stephen Hines651f13c2014-04-23 16:59:28 -0700946 Cnt.beginRegion(Builder);
Fariborz Jahanianaa3b57e2010-09-20 23:50:22 +0000947 ComplexPairTy LHS = Visit(E->getTrueExpr());
Chris Lattner5083a532007-08-21 17:39:38 +0000948 LHSBlock = Builder.GetInsertBlock();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000949 CGF.EmitBranch(ContBlock);
John McCall150b4622011-01-26 04:00:11 +0000950 eval.end(CGF);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000951
John McCall150b4622011-01-26 04:00:11 +0000952 eval.begin(CGF);
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000953 CGF.EmitBlock(RHSBlock);
John McCall56ca35d2011-02-17 10:25:35 +0000954 ComplexPairTy RHS = Visit(E->getFalseExpr());
Chris Lattner5083a532007-08-21 17:39:38 +0000955 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000956 CGF.EmitBlock(ContBlock);
John McCall150b4622011-01-26 04:00:11 +0000957 eval.end(CGF);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000958
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000959 // Create a PHI node for the real part.
Jay Foadbbf3bac2011-03-30 11:28:58 +0000960 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000961 RealPN->addIncoming(LHS.first, LHSBlock);
962 RealPN->addIncoming(RHS.first, RHSBlock);
963
964 // Create a PHI node for the imaginary part.
Jay Foadbbf3bac2011-03-30 11:28:58 +0000965 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i");
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000966 ImagPN->addIncoming(LHS.second, LHSBlock);
967 ImagPN->addIncoming(RHS.second, RHSBlock);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000968
Chris Lattnerb6ef18a2007-08-21 05:54:00 +0000969 return ComplexPairTy(RealPN, ImagPN);
970}
971
Chris Lattnerd0b12032007-08-24 02:18:47 +0000972ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) {
Eli Friedmana5e66012013-07-20 00:40:58 +0000973 return Visit(E->getChosenSubExpr());
Chris Lattnerd0b12032007-08-24 02:18:47 +0000974}
975
Eli Friedmanc4777f12008-05-13 23:11:35 +0000976ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000977 bool Ignore = TestAndClearIgnoreReal();
978 (void)Ignore;
979 assert (Ignore == false && "init list ignored");
980 Ignore = TestAndClearIgnoreImag();
981 (void)Ignore;
982 assert (Ignore == false && "init list ignored");
Eli Friedman0c706c22011-09-19 23:17:44 +0000983
984 if (E->getNumInits() == 2) {
985 llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0));
986 llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1));
987 return ComplexPairTy(Real, Imag);
988 } else if (E->getNumInits() == 1) {
Eli Friedmanc4777f12008-05-13 23:11:35 +0000989 return Visit(E->getInit(0));
Eli Friedman0c706c22011-09-19 23:17:44 +0000990 }
Eli Friedmanc4777f12008-05-13 23:11:35 +0000991
992 // Empty init list intializes to null
Eli Friedman0c706c22011-09-19 23:17:44 +0000993 assert(E->getNumInits() == 0 && "Unexpected number of inits");
John McCall9d232c82013-03-07 21:37:08 +0000994 QualType Ty = E->getType()->castAs<ComplexType>()->getElementType();
Chris Lattner2acc6e32011-07-18 04:24:23 +0000995 llvm::Type* LTy = CGF.ConvertType(Ty);
Owen Andersonc9c88b42009-07-31 20:28:54 +0000996 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy);
Eli Friedmanc4777f12008-05-13 23:11:35 +0000997 return ComplexPairTy(zeroConstant, zeroConstant);
998}
999
Daniel Dunbar4e484b82009-02-10 03:03:30 +00001000ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) {
Daniel Dunbar07855702009-02-11 22:25:55 +00001001 llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr());
Daniel Dunbar4e484b82009-02-10 03:03:30 +00001002 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType());
1003
1004 if (!ArgPtr) {
1005 CGF.ErrorUnsupported(E, "complex va_arg expression");
Chris Lattner2acc6e32011-07-18 04:24:23 +00001006 llvm::Type *EltTy =
John McCall9d232c82013-03-07 21:37:08 +00001007 CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType());
Daniel Dunbar4e484b82009-02-10 03:03:30 +00001008 llvm::Value *U = llvm::UndefValue::get(EltTy);
1009 return ComplexPairTy(U, U);
1010 }
1011
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001012 return EmitLoadOfLValue(CGF.MakeNaturalAlignAddrLValue(ArgPtr, E->getType()),
1013 E->getExprLoc());
Daniel Dunbar4e484b82009-02-10 03:03:30 +00001014}
1015
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00001016//===----------------------------------------------------------------------===//
1017// Entry Point into this File
1018//===----------------------------------------------------------------------===//
1019
1020/// EmitComplexExpr - Emit the computation of the specified expression of
1021/// complex type, ignoring the result.
Mike Stump7f79f9b2009-05-29 15:46:01 +00001022ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal,
John McCallb418d742010-11-16 10:08:07 +00001023 bool IgnoreImag) {
John McCall9d232c82013-03-07 21:37:08 +00001024 assert(E && getComplexType(E->getType()) &&
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00001025 "Invalid complex expression to emit");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001026
John McCallb418d742010-11-16 10:08:07 +00001027 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag)
Mike Stump7f79f9b2009-05-29 15:46:01 +00001028 .Visit(const_cast<Expr*>(E));
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00001029}
1030
John McCall9d232c82013-03-07 21:37:08 +00001031void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest,
1032 bool isInit) {
1033 assert(E && getComplexType(E->getType()) &&
Chris Lattner23b1cdb2007-08-23 23:43:33 +00001034 "Invalid complex expression to emit");
1035 ComplexExprEmitter Emitter(*this);
1036 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E));
John McCall9d232c82013-03-07 21:37:08 +00001037 Emitter.EmitStoreOfComplex(Val, dest, isInit);
Chris Lattner23b1cdb2007-08-23 23:43:33 +00001038}
Chris Lattner9b655512007-08-31 22:49:20 +00001039
John McCall9d232c82013-03-07 21:37:08 +00001040/// EmitStoreOfComplex - Store a complex number into the specified l-value.
1041void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest,
1042 bool isInit) {
1043 ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001044}
1045
John McCall9d232c82013-03-07 21:37:08 +00001046/// EmitLoadOfComplex - Load a complex number from the specified address.
Nick Lewycky4ee7dc22013-10-02 02:29:49 +00001047ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src,
1048 SourceLocation loc) {
1049 return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc);
Chris Lattner9b655512007-08-31 22:49:20 +00001050}
John McCall83ce9d42010-11-16 23:07:28 +00001051
1052LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) {
John McCall2a416372010-12-05 02:00:02 +00001053 assert(E->getOpcode() == BO_Assign);
John McCall83ce9d42010-11-16 23:07:28 +00001054 ComplexPairTy Val; // ignored
John McCall2a416372010-12-05 02:00:02 +00001055 return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val);
1056}
John McCall83ce9d42010-11-16 23:07:28 +00001057
Eli Friedman0934e182013-06-12 01:40:06 +00001058typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)(
1059 const ComplexExprEmitter::BinOpInfo &);
John McCall83ce9d42010-11-16 23:07:28 +00001060
Eli Friedman0934e182013-06-12 01:40:06 +00001061static CompoundFunc getComplexOp(BinaryOperatorKind Op) {
1062 switch (Op) {
1063 case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul;
1064 case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv;
1065 case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub;
1066 case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd;
John McCall83ce9d42010-11-16 23:07:28 +00001067 default:
1068 llvm_unreachable("unexpected complex compound assignment");
John McCall83ce9d42010-11-16 23:07:28 +00001069 }
Eli Friedman0934e182013-06-12 01:40:06 +00001070}
John McCall83ce9d42010-11-16 23:07:28 +00001071
Eli Friedman0934e182013-06-12 01:40:06 +00001072LValue CodeGenFunction::
1073EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) {
1074 CompoundFunc Op = getComplexOp(E->getOpcode());
1075 RValue Val;
John McCall2a416372010-12-05 02:00:02 +00001076 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
John McCall83ce9d42010-11-16 23:07:28 +00001077}
Eli Friedman0934e182013-06-12 01:40:06 +00001078
1079LValue CodeGenFunction::
1080EmitScalarCompooundAssignWithComplex(const CompoundAssignOperator *E,
1081 llvm::Value *&Result) {
1082 CompoundFunc Op = getComplexOp(E->getOpcode());
1083 RValue Val;
1084 LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val);
1085 Result = Val.getScalarVal();
1086 return Ret;
1087}