Chris Lattner | 566b6ce | 2007-08-24 02:22:53 +0000 | [diff] [blame] | 1 | //===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===// |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 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" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/StmtVisitor.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Constants.h" |
| 21 | #include "llvm/IR/Function.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 22 | #include "llvm/IR/Instructions.h" |
| 23 | #include "llvm/IR/MDBuilder.h" |
| 24 | #include "llvm/IR/Metadata.h" |
JF Bastien | ab96e56 | 2013-07-17 05:57:42 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 30 | // Complex Expression Emitter |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 33 | typedef CodeGenFunction::ComplexPairTy ComplexPairTy; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 34 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 35 | /// Return the complex type that we are meant to emit. |
| 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 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 45 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 46 | class ComplexExprEmitter |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 47 | : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> { |
| 48 | CodeGenFunction &CGF; |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 49 | CGBuilderTy &Builder; |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 50 | bool IgnoreReal; |
| 51 | bool IgnoreImag; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 52 | public: |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 53 | ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false) |
| 54 | : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 57 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 58 | //===--------------------------------------------------------------------===// |
| 59 | // Utilities |
| 60 | //===--------------------------------------------------------------------===// |
| 61 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 62 | bool TestAndClearIgnoreReal() { |
| 63 | bool I = IgnoreReal; |
| 64 | IgnoreReal = false; |
| 65 | return I; |
| 66 | } |
| 67 | bool TestAndClearIgnoreImag() { |
| 68 | bool I = IgnoreImag; |
| 69 | IgnoreImag = false; |
| 70 | return I; |
| 71 | } |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 72 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 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. |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 76 | ComplexPairTy EmitLoadOfLValue(const Expr *E) { |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 77 | return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc()); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 80 | ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 82 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 83 | /// specified value pointer. |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 84 | void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 85 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 86 | /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType. |
| 87 | ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType, |
| 88 | QualType DestType); |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 89 | /// EmitComplexToComplexCast - Emit a cast from scalar value Val to DestType. |
| 90 | ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType, |
| 91 | QualType DestType); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 92 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 93 | //===--------------------------------------------------------------------===// |
| 94 | // Visitor Methods |
| 95 | //===--------------------------------------------------------------------===// |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 96 | |
Fariborz Jahanian | aa3b57e | 2010-09-20 23:50:22 +0000 | [diff] [blame] | 97 | ComplexPairTy Visit(Expr *E) { |
John McCall | cf0b2d8 | 2011-02-17 19:02:56 +0000 | [diff] [blame] | 98 | return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E); |
Fariborz Jahanian | aa3b57e | 2010-09-20 23:50:22 +0000 | [diff] [blame] | 99 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 100 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 101 | ComplexPairTy VisitStmt(Stmt *S) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 102 | S->dump(CGF.getContext().getSourceManager()); |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 103 | llvm_unreachable("Stmt can't have complex result type!"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 105 | ComplexPairTy VisitExpr(Expr *S); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 106 | ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());} |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 107 | ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) { |
| 108 | return Visit(GE->getResultExpr()); |
| 109 | } |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 110 | ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL); |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 111 | ComplexPairTy |
| 112 | VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) { |
| 113 | return Visit(PE->getReplacement()); |
| 114 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 115 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 116 | // l-values. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 117 | ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) { |
| 118 | if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) { |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 119 | if (result.isReference()) |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 120 | return EmitLoadOfLValue(result.getReferenceLValue(CGF, E), |
| 121 | E->getExprLoc()); |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 122 | |
Eli Friedman | be6d913 | 2013-07-16 20:19:04 +0000 | [diff] [blame] | 123 | llvm::Constant *pair = result.getValue(); |
| 124 | return ComplexPairTy(pair->getAggregateElement(0U), |
| 125 | pair->getAggregateElement(1U)); |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 126 | } |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 127 | return EmitLoadOfLValue(E); |
John McCall | dd2ecee | 2012-03-10 03:05:10 +0000 | [diff] [blame] | 128 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 129 | ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 130 | return EmitLoadOfLValue(E); |
| 131 | } |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 132 | ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 133 | return CGF.EmitObjCMessageExpr(E).getComplexVal(); |
| 134 | } |
Chris Lattner | 8bcd723 | 2007-08-21 18:03:58 +0000 | [diff] [blame] | 135 | ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 136 | ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); } |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 137 | ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 138 | if (E->isGLValue()) |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 139 | return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->getExprLoc()); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 140 | return CGF.getOpaqueRValueMapping(E).getComplexVal(); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 141 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 142 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 143 | ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) { |
| 144 | return CGF.EmitPseudoObjectRValue(E).getComplexVal(); |
| 145 | } |
| 146 | |
Chris Lattner | e73e8e2 | 2007-08-21 22:33:41 +0000 | [diff] [blame] | 147 | // FIXME: CompoundLiteralExpr |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 148 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 149 | ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 150 | ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 151 | // Unlike for scalars, we don't have to worry about function->ptr demotion |
| 152 | // here. |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 153 | return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType()); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 154 | } |
| 155 | ComplexPairTy VisitCastExpr(CastExpr *E) { |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 156 | return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType()); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 157 | } |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 158 | ComplexPairTy VisitCallExpr(const CallExpr *E); |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 159 | ComplexPairTy VisitStmtExpr(const StmtExpr *E); |
| 160 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 161 | // Operators. |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 162 | ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | dd36d32 | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 163 | bool isInc, bool isPre) { |
| 164 | LValue LV = CGF.EmitLValue(E->getSubExpr()); |
| 165 | return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre); |
| 166 | } |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 167 | 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 Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 180 | ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 181 | TestAndClearIgnoreReal(); |
| 182 | TestAndClearIgnoreImag(); |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 183 | return Visit(E->getSubExpr()); |
| 184 | } |
| 185 | ComplexPairTy VisitUnaryMinus (const UnaryOperator *E); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 186 | ComplexPairTy VisitUnaryNot (const UnaryOperator *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 187 | // LNot,Real,Imag never return complex. |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 188 | ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) { |
| 189 | return Visit(E->getSubExpr()); |
| 190 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 191 | ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 192 | return Visit(DAE->getExpr()); |
| 193 | } |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 194 | ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) { |
| 195 | CodeGenFunction::CXXDefaultInitExprScope Scope(CGF); |
| 196 | return Visit(DIE->getExpr()); |
| 197 | } |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 198 | ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) { |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 199 | CGF.enterFullExpression(E); |
| 200 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 201 | return Visit(E->getSubExpr()); |
Anders Carlsson | 4b76b4f | 2009-05-31 00:12:05 +0000 | [diff] [blame] | 202 | } |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 203 | ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 204 | assert(E->getType()->isAnyComplexType() && "Expected complex type!"); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 205 | QualType Elem = E->getType()->castAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 206 | llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem)); |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 207 | return ComplexPairTy(Null, Null); |
| 208 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 209 | ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
| 210 | assert(E->getType()->isAnyComplexType() && "Expected complex type!"); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 211 | QualType Elem = E->getType()->castAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 212 | llvm::Constant *Null = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 213 | llvm::Constant::getNullValue(CGF.ConvertType(Elem)); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 214 | return ComplexPairTy(Null, Null); |
| 215 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 216 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 217 | struct BinOpInfo { |
| 218 | ComplexPairTy LHS; |
| 219 | ComplexPairTy RHS; |
| 220 | QualType Ty; // Computation Type. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 223 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 224 | LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E, |
| 225 | ComplexPairTy (ComplexExprEmitter::*Func) |
| 226 | (const BinOpInfo &), |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 227 | RValue &Val); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 228 | 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 Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 236 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 237 | ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName, |
| 238 | const BinOpInfo &Op); |
| 239 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 240 | ComplexPairTy VisitBinAdd(const BinaryOperator *E) { |
| 241 | return EmitBinAdd(EmitBinOps(E)); |
| 242 | } |
| 243 | ComplexPairTy VisitBinSub(const BinaryOperator *E) { |
| 244 | return EmitBinSub(EmitBinOps(E)); |
| 245 | } |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 246 | ComplexPairTy VisitBinMul(const BinaryOperator *E) { |
| 247 | return EmitBinMul(EmitBinOps(E)); |
| 248 | } |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 249 | ComplexPairTy VisitBinDiv(const BinaryOperator *E) { |
| 250 | return EmitBinDiv(EmitBinOps(E)); |
| 251 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 252 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 253 | // 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 Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 266 | |
Chris Lattner | 612c40c | 2007-08-26 21:27:07 +0000 | [diff] [blame] | 267 | // GCC rejects rem/and/or/xor for integer complex. |
Chris Lattner | 4034edb | 2007-08-21 17:12:50 +0000 | [diff] [blame] | 268 | // Logical and/or always return int, never complex. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 269 | |
| 270 | // No comparisons produce a complex result. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 271 | |
| 272 | LValue EmitBinAssignLValue(const BinaryOperator *E, |
| 273 | ComplexPairTy &Val); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 274 | ComplexPairTy VisitBinAssign (const BinaryOperator *E); |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 275 | ComplexPairTy VisitBinComma (const BinaryOperator *E); |
| 276 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 277 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 278 | ComplexPairTy |
| 279 | VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 280 | ComplexPairTy VisitChooseExpr(ChooseExpr *CE); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 281 | |
| 282 | ComplexPairTy VisitInitListExpr(InitListExpr *E); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 283 | |
Eli Friedman | fce4715 | 2012-02-27 20:26:13 +0000 | [diff] [blame] | 284 | ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 285 | return EmitLoadOfLValue(E); |
| 286 | } |
| 287 | |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 288 | ComplexPairTy VisitVAArgExpr(VAArgExpr *E); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 289 | |
| 290 | ComplexPairTy VisitAtomicExpr(AtomicExpr *E) { |
| 291 | return CGF.EmitAtomicExpr(E).getComplexVal(); |
| 292 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 293 | }; |
| 294 | } // end anonymous namespace. |
| 295 | |
| 296 | //===----------------------------------------------------------------------===// |
| 297 | // Utilities |
| 298 | //===----------------------------------------------------------------------===// |
| 299 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 300 | /// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 301 | /// load the real and imaginary pieces, returning them as Real/Imag. |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 302 | ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue, |
| 303 | SourceLocation loc) { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 304 | assert(lvalue.isSimple() && "non-simple complex l-value?"); |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 305 | if (lvalue.getType()->isAtomicType()) |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 306 | return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal(); |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 307 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 308 | llvm::Value *SrcPtr = lvalue.getAddress(); |
| 309 | bool isVolatile = lvalue.isVolatileQualified(); |
JF Bastien | ab96e56 | 2013-07-17 05:57:42 +0000 | [diff] [blame] | 310 | 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 McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 315 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 316 | llvm::Value *Real=nullptr, *Imag=nullptr; |
Chris Lattner | 6b5d0bf | 2007-08-26 22:47:40 +0000 | [diff] [blame] | 317 | |
John McCall | ff624c1 | 2010-11-14 09:40:28 +0000 | [diff] [blame] | 318 | if (!IgnoreReal || isVolatile) { |
Benjamin Kramer | 56a44f7 | 2009-11-29 19:51:45 +0000 | [diff] [blame] | 319 | llvm::Value *RealP = Builder.CreateStructGEP(SrcPtr, 0, |
| 320 | SrcPtr->getName() + ".realp"); |
JF Bastien | ab96e56 | 2013-07-17 05:57:42 +0000 | [diff] [blame] | 321 | Real = Builder.CreateAlignedLoad(RealP, AlignR, isVolatile, |
| 322 | SrcPtr->getName() + ".real"); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 323 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 324 | |
John McCall | ff624c1 | 2010-11-14 09:40:28 +0000 | [diff] [blame] | 325 | if (!IgnoreImag || isVolatile) { |
Benjamin Kramer | 56a44f7 | 2009-11-29 19:51:45 +0000 | [diff] [blame] | 326 | llvm::Value *ImagP = Builder.CreateStructGEP(SrcPtr, 1, |
| 327 | SrcPtr->getName() + ".imagp"); |
JF Bastien | ab96e56 | 2013-07-17 05:57:42 +0000 | [diff] [blame] | 328 | Imag = Builder.CreateAlignedLoad(ImagP, AlignI, isVolatile, |
| 329 | SrcPtr->getName() + ".imag"); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 330 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 331 | return ComplexPairTy(Real, Imag); |
| 332 | } |
| 333 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 334 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 335 | /// specified value pointer. |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 336 | void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, |
| 337 | LValue lvalue, |
| 338 | bool isInit) { |
John McCall | 9eda3ab | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 339 | if (lvalue.getType()->isAtomicType()) |
| 340 | return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit); |
| 341 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 342 | llvm::Value *Ptr = lvalue.getAddress(); |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 343 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real"); |
| 344 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag"); |
JF Bastien | ab96e56 | 2013-07-17 05:57:42 +0000 | [diff] [blame] | 345 | 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 Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 350 | |
JF Bastien | ab96e56 | 2013-07-17 05:57:42 +0000 | [diff] [blame] | 351 | Builder.CreateAlignedStore(Val.first, RealPtr, AlignR, |
| 352 | lvalue.isVolatileQualified()); |
| 353 | Builder.CreateAlignedStore(Val.second, ImagPtr, AlignI, |
| 354 | lvalue.isVolatileQualified()); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | |
| 358 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 359 | //===----------------------------------------------------------------------===// |
| 360 | // Visitor Methods |
| 361 | //===----------------------------------------------------------------------===// |
| 362 | |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 363 | ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) { |
Fariborz Jahanian | 776b790 | 2011-09-02 20:03:16 +0000 | [diff] [blame] | 364 | CGF.ErrorUnsupported(E, "complex expression"); |
| 365 | llvm::Type *EltTy = |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 366 | CGF.ConvertType(getComplexType(E->getType())->getElementType()); |
Fariborz Jahanian | 776b790 | 2011-09-02 20:03:16 +0000 | [diff] [blame] | 367 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 368 | return ComplexPairTy(U, U); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 371 | ComplexPairTy ComplexExprEmitter:: |
| 372 | VisitImaginaryLiteral(const ImaginaryLiteral *IL) { |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 373 | llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr()); |
John McCall | 8d3d6c9 | 2011-01-13 02:03:06 +0000 | [diff] [blame] | 374 | return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag); |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 378 | ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | e9f2f45 | 2009-05-27 03:37:57 +0000 | [diff] [blame] | 379 | if (E->getCallReturnType()->isReferenceType()) |
| 380 | return EmitLoadOfLValue(E); |
| 381 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 382 | return CGF.EmitCallExpr(E).getComplexVal(); |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 385 | ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 386 | CodeGenFunction::StmtExprEvaluation eval(CGF); |
Eli Friedman | 2ac2fa7 | 2013-06-10 22:04:49 +0000 | [diff] [blame] | 387 | llvm::Value *RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true); |
| 388 | assert(RetAlloca && "Expected complex return value"); |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 389 | return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()), |
| 390 | E->getExprLoc()); |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 393 | /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType. |
| 394 | ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val, |
| 395 | QualType SrcType, |
| 396 | QualType DestType) { |
| 397 | // Get the src/dest element type. |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 398 | SrcType = SrcType->castAs<ComplexType>()->getElementType(); |
| 399 | DestType = DestType->castAs<ComplexType>()->getElementType(); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 400 | |
Daniel Dunbar | 56f0d16 | 2009-01-29 06:44:03 +0000 | [diff] [blame] | 401 | // C99 6.3.1.6: When a value of complex type is converted to another |
Daniel Dunbar | e12d8e3 | 2009-01-26 18:02:34 +0000 | [diff] [blame] | 402 | // complex type, both the real and imaginary parts follow the conversion |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 403 | // 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 Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 409 | ComplexPairTy 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 Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 420 | ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op, |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 421 | QualType DestTy) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 422 | switch (CK) { |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 423 | case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 424 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 425 | // 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 McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 429 | case CK_NoOp: |
| 430 | case CK_LValueToRValue: |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 431 | case CK_UserDefinedConversion: |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 432 | return Visit(Op); |
| 433 | |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 434 | case CK_LValueBitCast: { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 435 | LValue origLV = CGF.EmitLValue(Op); |
| 436 | llvm::Value *V = origLV.getAddress(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 437 | V = Builder.CreateBitCast(V, |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 438 | CGF.ConvertType(CGF.getContext().getPointerType(DestTy))); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 439 | return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy, |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 440 | origLV.getAlignment()), |
| 441 | Op->getExprLoc()); |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 442 | } |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 443 | |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 444 | 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 McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 457 | case CK_ReinterpretMemberPointer: |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 458 | 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 McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 470 | case CK_CPointerToObjCPointerCast: |
| 471 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 472 | case CK_AnyPointerToBlockPointerCast: |
| 473 | case CK_ObjCObjectLValueCast: |
| 474 | case CK_FloatingComplexToReal: |
| 475 | case CK_FloatingComplexToBoolean: |
| 476 | case CK_IntegralComplexToReal: |
| 477 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 478 | case CK_ARCProduceObject: |
| 479 | case CK_ARCConsumeObject: |
| 480 | case CK_ARCReclaimReturnedObject: |
| 481 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 482 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 483 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 484 | case CK_ZeroToOCLEvent: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 485 | case CK_AddressSpaceConversion: |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 486 | llvm_unreachable("invalid cast kind for complex value"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 487 | |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 488 | case CK_FloatingRealToComplex: |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 489 | case CK_IntegralRealToComplex: |
| 490 | return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op), |
| 491 | Op->getType(), DestTy); |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 492 | |
| 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 Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 503 | ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 504 | TestAndClearIgnoreReal(); |
| 505 | TestAndClearIgnoreImag(); |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 506 | ComplexPairTy Op = Visit(E->getSubExpr()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 508 | llvm::Value *ResR, *ResI; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 509 | if (Op.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 510 | 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 Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 516 | return ComplexPairTy(ResR, ResI); |
| 517 | } |
| 518 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 519 | ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 520 | TestAndClearIgnoreReal(); |
| 521 | TestAndClearIgnoreImag(); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 522 | // ~(a+ib) = a + i*-b |
| 523 | ComplexPairTy Op = Visit(E->getSubExpr()); |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 524 | llvm::Value *ResI; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 525 | if (Op.second->getType()->isFloatingPointTy()) |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 526 | ResI = Builder.CreateFNeg(Op.second, "conj.i"); |
| 527 | else |
| 528 | ResI = Builder.CreateNeg(Op.second, "conj.i"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 529 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 530 | return ComplexPairTy(Op.first, ResI); |
| 531 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 532 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 533 | ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 534 | llvm::Value *ResR, *ResI; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 535 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 536 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 537 | ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 538 | 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 Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 543 | } else { |
| 544 | ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 545 | assert(Op.LHS.second && Op.RHS.second && |
| 546 | "Both operands of integer complex operators must be complex!"); |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 547 | ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i"); |
| 548 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 549 | return ComplexPairTy(ResR, ResI); |
| 550 | } |
| 551 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 552 | ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 553 | llvm::Value *ResR, *ResI; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 554 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 555 | 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 Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 562 | } else { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 563 | 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 Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 566 | ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i"); |
| 567 | } |
Chris Lattner | 6bc1ade | 2007-08-23 23:46:33 +0000 | [diff] [blame] | 568 | return ComplexPairTy(ResR, ResI); |
| 569 | } |
| 570 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 571 | /// \brief Emit a libcall for a binary operation on complex types. |
| 572 | ComplexPairTy 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 Lattner | 6bc1ade | 2007-08-23 23:46:33 +0000 | [diff] [blame] | 583 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 584 | // 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. |
| 596 | static 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 Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 617 | ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 618 | using llvm::Value; |
| 619 | Value *ResR, *ResI; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 620 | llvm::MDBuilder MDHelper(CGF.getLLVMContext()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 621 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 622 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 623 | // 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 Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 630 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 631 | 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 Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 703 | } else { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 704 | assert(Op.LHS.second && Op.RHS.second && |
| 705 | "Both operands of integer complex operators must be complex!"); |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 706 | Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 707 | Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr"); |
| 708 | ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 709 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 710 | 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 Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 712 | ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 713 | } |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 714 | return ComplexPairTy(ResR, ResI); |
| 715 | } |
| 716 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 717 | // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex |
| 718 | // typed values. |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 719 | ComplexPairTy 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 Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 722 | |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 723 | |
| 724 | llvm::Value *DSTr, *DSTi; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 725 | 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 Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 736 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 737 | 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 Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 756 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 757 | DSTr = Builder.CreateFDiv(LHSr, RHSr); |
| 758 | DSTi = Builder.CreateFDiv(LHSi, RHSr); |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 759 | } else { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 760 | assert(Op.LHS.second && Op.RHS.second && |
| 761 | "Both operands of integer complex operators must be complex!"); |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 762 | // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 763 | 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 Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 766 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 767 | 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 Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 770 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 771 | 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 Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 774 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 775 | if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) { |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 776 | DSTr = Builder.CreateUDiv(Tmp3, Tmp6); |
| 777 | DSTi = Builder.CreateUDiv(Tmp9, Tmp6); |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 778 | } else { |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 779 | DSTr = Builder.CreateSDiv(Tmp3, Tmp6); |
| 780 | DSTi = Builder.CreateSDiv(Tmp9, Tmp6); |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 784 | return ComplexPairTy(DSTr, DSTi); |
| 785 | } |
| 786 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 787 | ComplexExprEmitter::BinOpInfo |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 788 | ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 789 | TestAndClearIgnoreReal(); |
| 790 | TestAndClearIgnoreImag(); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 791 | BinOpInfo Ops; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 792 | 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 Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 801 | Ops.Ty = E->getType(); |
| 802 | return Ops; |
| 803 | } |
| 804 | |
| 805 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 806 | LValue ComplexExprEmitter:: |
| 807 | EmitCompoundAssignLValue(const CompoundAssignOperator *E, |
| 808 | ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&), |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 809 | RValue &Val) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 810 | TestAndClearIgnoreReal(); |
| 811 | TestAndClearIgnoreImag(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 812 | QualType LHSTy = E->getLHS()->getType(); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 813 | |
| 814 | BinOpInfo OpInfo; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 815 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 816 | // Load the RHS and LHS operands. |
| 817 | // __block variables need to have the rhs evaluated first, plus this should |
John McCall | 641ca7d | 2010-11-16 05:45:35 +0000 | [diff] [blame] | 818 | // improve codegen a little. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 819 | OpInfo.Ty = E->getComputationResultType(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 820 | QualType ComplexElementTy = cast<ComplexType>(OpInfo.Ty)->getElementType(); |
John McCall | 641ca7d | 2010-11-16 05:45:35 +0000 | [diff] [blame] | 821 | |
| 822 | // The RHS should have been converted to the computation type. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 823 | 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 Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 833 | |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 834 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 835 | |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 836 | // Load from the l-value and convert it. |
| 837 | if (LHSTy->isAnyComplexType()) { |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 838 | ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, E->getExprLoc()); |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 839 | OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty); |
| 840 | } else { |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 841 | llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, E->getExprLoc()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 842 | // 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 Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 851 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 852 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 853 | // Expand the binary operator. |
| 854 | ComplexPairTy Result = (this->*Func)(OpInfo); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 855 | |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 856 | // 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 Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 867 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 868 | return LHS; |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 869 | } |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 870 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 871 | // Compound assignments. |
| 872 | ComplexPairTy ComplexExprEmitter:: |
| 873 | EmitCompoundAssign(const CompoundAssignOperator *E, |
| 874 | ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){ |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 875 | RValue Val; |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 876 | LValue LV = EmitCompoundAssignLValue(E, Func, Val); |
| 877 | |
| 878 | // The result of an assignment in C is the assigned r-value. |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 879 | if (!CGF.getLangOpts().CPlusPlus) |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 880 | return Val.getComplexVal(); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 881 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 882 | // If the lvalue is non-volatile, return the computed value of the assignment. |
| 883 | if (!LV.isVolatileQualified()) |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 884 | return Val.getComplexVal(); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 885 | |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 886 | return EmitLoadOfLValue(LV, E->getExprLoc()); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E, |
| 890 | ComplexPairTy &Val) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 891 | assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 892 | E->getRHS()->getType()) && |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 893 | "Invalid assignment"); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 894 | TestAndClearIgnoreReal(); |
| 895 | TestAndClearIgnoreImag(); |
| 896 | |
John McCall | cd940a1 | 2010-12-06 06:10:02 +0000 | [diff] [blame] | 897 | // Emit the RHS. __block variables need the RHS evaluated first. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 898 | Val = Visit(E->getRHS()); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 899 | |
| 900 | // Compute the address to store into. |
| 901 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 902 | |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 903 | // Store the result value into the LHS lvalue. |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 904 | EmitStoreOfComplex(Val, LHS, /*isInit*/ false); |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 905 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 906 | return LHS; |
| 907 | } |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 908 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 909 | ComplexPairTy 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 Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 914 | if (!CGF.getLangOpts().CPlusPlus) |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 915 | return Val; |
| 916 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 917 | // If the lvalue is non-volatile, return the computed value of the assignment. |
| 918 | if (!LV.isVolatileQualified()) |
| 919 | return Val; |
| 920 | |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 921 | return EmitLoadOfLValue(LV, E->getExprLoc()); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 924 | ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 925 | CGF.EmitIgnoredExpr(E->getLHS()); |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 926 | return Visit(E->getRHS()); |
| 927 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 928 | |
| 929 | ComplexPairTy ComplexExprEmitter:: |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 930 | VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 931 | TestAndClearIgnoreReal(); |
| 932 | TestAndClearIgnoreImag(); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 933 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 934 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
| 935 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 936 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 937 | // Bind the common expression if necessary. |
Eli Friedman | d97927d | 2012-01-06 20:42:20 +0000 | [diff] [blame] | 938 | CodeGenFunction::OpaqueValueMapping binding(CGF, E); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 939 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 940 | RegionCounter Cnt = CGF.getPGORegionCounter(E); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 941 | CodeGenFunction::ConditionalEvaluation eval(CGF); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 942 | CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock, Cnt.getCount()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 943 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 944 | eval.begin(CGF); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 945 | CGF.EmitBlock(LHSBlock); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 946 | Cnt.beginRegion(Builder); |
Fariborz Jahanian | aa3b57e | 2010-09-20 23:50:22 +0000 | [diff] [blame] | 947 | ComplexPairTy LHS = Visit(E->getTrueExpr()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 948 | LHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 949 | CGF.EmitBranch(ContBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 950 | eval.end(CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 951 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 952 | eval.begin(CGF); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 953 | CGF.EmitBlock(RHSBlock); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 954 | ComplexPairTy RHS = Visit(E->getFalseExpr()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 955 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 956 | CGF.EmitBlock(ContBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 957 | eval.end(CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 958 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 959 | // Create a PHI node for the real part. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 960 | llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 961 | RealPN->addIncoming(LHS.first, LHSBlock); |
| 962 | RealPN->addIncoming(RHS.first, RHSBlock); |
| 963 | |
| 964 | // Create a PHI node for the imaginary part. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 965 | llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 966 | ImagPN->addIncoming(LHS.second, LHSBlock); |
| 967 | ImagPN->addIncoming(RHS.second, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 968 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 969 | return ComplexPairTy(RealPN, ImagPN); |
| 970 | } |
| 971 | |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 972 | ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Eli Friedman | a5e6601 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 973 | return Visit(E->getChosenSubExpr()); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 976 | ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 977 | 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 Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 983 | |
| 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 Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 989 | return Visit(E->getInit(0)); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 990 | } |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 991 | |
| 992 | // Empty init list intializes to null |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 993 | assert(E->getNumInits() == 0 && "Unexpected number of inits"); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 994 | QualType Ty = E->getType()->castAs<ComplexType>()->getElementType(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 995 | llvm::Type* LTy = CGF.ConvertType(Ty); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 996 | llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 997 | return ComplexPairTy(zeroConstant, zeroConstant); |
| 998 | } |
| 999 | |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 1000 | ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 1001 | llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr()); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 1002 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType()); |
| 1003 | |
| 1004 | if (!ArgPtr) { |
| 1005 | CGF.ErrorUnsupported(E, "complex va_arg expression"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1006 | llvm::Type *EltTy = |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1007 | CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType()); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 1008 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 1009 | return ComplexPairTy(U, U); |
| 1010 | } |
| 1011 | |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1012 | return EmitLoadOfLValue(CGF.MakeNaturalAlignAddrLValue(ArgPtr, E->getType()), |
| 1013 | E->getExprLoc()); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 1016 | //===----------------------------------------------------------------------===// |
| 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 Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1022 | ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal, |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1023 | bool IgnoreImag) { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1024 | assert(E && getComplexType(E->getType()) && |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 1025 | "Invalid complex expression to emit"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1026 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1027 | return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag) |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1028 | .Visit(const_cast<Expr*>(E)); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1031 | void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest, |
| 1032 | bool isInit) { |
| 1033 | assert(E && getComplexType(E->getType()) && |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 1034 | "Invalid complex expression to emit"); |
| 1035 | ComplexExprEmitter Emitter(*this); |
| 1036 | ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E)); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1037 | Emitter.EmitStoreOfComplex(Val, dest, isInit); |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 1038 | } |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1039 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1040 | /// EmitStoreOfComplex - Store a complex number into the specified l-value. |
| 1041 | void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest, |
| 1042 | bool isInit) { |
| 1043 | ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 1046 | /// EmitLoadOfComplex - Load a complex number from the specified address. |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1047 | ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src, |
| 1048 | SourceLocation loc) { |
| 1049 | return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 1050 | } |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 1051 | |
| 1052 | LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1053 | assert(E->getOpcode() == BO_Assign); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 1054 | ComplexPairTy Val; // ignored |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1055 | return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val); |
| 1056 | } |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 1057 | |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 1058 | typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)( |
| 1059 | const ComplexExprEmitter::BinOpInfo &); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 1060 | |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 1061 | static 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 McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 1067 | default: |
| 1068 | llvm_unreachable("unexpected complex compound assignment"); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 1069 | } |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 1070 | } |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 1071 | |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 1072 | LValue CodeGenFunction:: |
| 1073 | EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) { |
| 1074 | CompoundFunc Op = getComplexOp(E->getOpcode()); |
| 1075 | RValue Val; |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1076 | return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 1077 | } |
Eli Friedman | 0934e18 | 2013-06-12 01:40:06 +0000 | [diff] [blame] | 1078 | |
| 1079 | LValue CodeGenFunction:: |
| 1080 | EmitScalarCompooundAssignWithComplex(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 | } |