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" |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Function.h" |
Chris Lattner | 6b5d0bf | 2007-08-26 22:47:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05ba49c | 2007-08-21 05:54:53 +0000 | [diff] [blame] | 25 | // Complex Expression Emitter |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 28 | typedef CodeGenFunction::ComplexPairTy ComplexPairTy; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 29 | |
| 30 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 31 | class ComplexExprEmitter |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 32 | : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> { |
| 33 | CodeGenFunction &CGF; |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 34 | CGBuilderTy &Builder; |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 35 | // True is we should ignore the value of a |
| 36 | bool IgnoreReal; |
| 37 | bool IgnoreImag; |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 38 | public: |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 39 | ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false) |
| 40 | : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) { |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 43 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 44 | //===--------------------------------------------------------------------===// |
| 45 | // Utilities |
| 46 | //===--------------------------------------------------------------------===// |
| 47 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 48 | bool TestAndClearIgnoreReal() { |
| 49 | bool I = IgnoreReal; |
| 50 | IgnoreReal = false; |
| 51 | return I; |
| 52 | } |
| 53 | bool TestAndClearIgnoreImag() { |
| 54 | bool I = IgnoreImag; |
| 55 | IgnoreImag = false; |
| 56 | return I; |
| 57 | } |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 58 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 59 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 60 | /// value l-value, this method emits the address of the l-value, then loads |
| 61 | /// and returns the result. |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 62 | ComplexPairTy EmitLoadOfLValue(const Expr *E) { |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 63 | return EmitLoadOfLValue(CGF.EmitLValue(E)); |
| 64 | } |
| 65 | |
| 66 | ComplexPairTy EmitLoadOfLValue(LValue LV) { |
John McCall | db45806 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 67 | assert(LV.isSimple() && "complex l-value must be simple"); |
| 68 | return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified()); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 69 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 71 | /// EmitLoadOfComplex - Given a pointer to a complex value, emit code to load |
| 72 | /// the real and imaginary pieces. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 73 | ComplexPairTy EmitLoadOfComplex(llvm::Value *SrcPtr, bool isVolatile); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 74 | |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 75 | /// EmitStoreThroughLValue - Given an l-value of complex type, store |
| 76 | /// a complex number into it. |
| 77 | void EmitStoreThroughLValue(ComplexPairTy Val, LValue LV) { |
John McCall | db45806 | 2011-11-07 03:59:57 +0000 | [diff] [blame] | 78 | assert(LV.isSimple() && "complex l-value must be simple"); |
| 79 | return EmitStoreOfComplex(Val, LV.getAddress(), LV.isVolatileQualified()); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 80 | } |
| 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. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 84 | void EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *ResPtr, bool isVol); |
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); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 89 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 90 | //===--------------------------------------------------------------------===// |
| 91 | // Visitor Methods |
| 92 | //===--------------------------------------------------------------------===// |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 93 | |
Fariborz Jahanian | aa3b57e | 2010-09-20 23:50:22 +0000 | [diff] [blame] | 94 | ComplexPairTy Visit(Expr *E) { |
John McCall | cf0b2d8 | 2011-02-17 19:02:56 +0000 | [diff] [blame] | 95 | return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E); |
Fariborz Jahanian | aa3b57e | 2010-09-20 23:50:22 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 98 | ComplexPairTy VisitStmt(Stmt *S) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 99 | S->dump(CGF.getContext().getSourceManager()); |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 100 | llvm_unreachable("Stmt can't have complex result type!"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 101 | } |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 102 | ComplexPairTy VisitExpr(Expr *S); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 103 | ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());} |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 104 | ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) { |
| 105 | return Visit(GE->getResultExpr()); |
| 106 | } |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 107 | ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL); |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 108 | ComplexPairTy |
| 109 | VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) { |
| 110 | return Visit(PE->getReplacement()); |
| 111 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 112 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 113 | // l-values. |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 114 | ComplexPairTy VisitDeclRefExpr(const Expr *E) { return EmitLoadOfLValue(E); } |
Fariborz Jahanian | d295b82 | 2011-09-02 21:33:44 +0000 | [diff] [blame] | 115 | ComplexPairTy VisitBlockDeclRefExpr(const Expr *E) { return EmitLoadOfLValue(E); } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 116 | ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 117 | return EmitLoadOfLValue(E); |
| 118 | } |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 119 | ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 120 | return CGF.EmitObjCMessageExpr(E).getComplexVal(); |
| 121 | } |
Chris Lattner | 8bcd723 | 2007-08-21 18:03:58 +0000 | [diff] [blame] | 122 | ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 123 | ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); } |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 124 | ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 125 | if (E->isGLValue()) |
| 126 | return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E)); |
| 127 | return CGF.getOpaqueRValueMapping(E).getComplexVal(); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 128 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 129 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 130 | ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) { |
| 131 | return CGF.EmitPseudoObjectRValue(E).getComplexVal(); |
| 132 | } |
| 133 | |
Chris Lattner | e73e8e2 | 2007-08-21 22:33:41 +0000 | [diff] [blame] | 134 | // FIXME: CompoundLiteralExpr |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 136 | ComplexPairTy EmitCast(CastExpr::CastKind CK, Expr *Op, QualType DestTy); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 137 | ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 138 | // Unlike for scalars, we don't have to worry about function->ptr demotion |
| 139 | // here. |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 140 | return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType()); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 141 | } |
| 142 | ComplexPairTy VisitCastExpr(CastExpr *E) { |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 143 | return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType()); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 145 | ComplexPairTy VisitCallExpr(const CallExpr *E); |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 146 | ComplexPairTy VisitStmtExpr(const StmtExpr *E); |
| 147 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 148 | // Operators. |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 149 | ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | dd36d32 | 2010-01-09 21:40:03 +0000 | [diff] [blame] | 150 | bool isInc, bool isPre) { |
| 151 | LValue LV = CGF.EmitLValue(E->getSubExpr()); |
| 152 | return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre); |
| 153 | } |
Chris Lattner | 3070f98 | 2007-08-21 22:25:29 +0000 | [diff] [blame] | 154 | ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) { |
| 155 | return VisitPrePostIncDec(E, false, false); |
| 156 | } |
| 157 | ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) { |
| 158 | return VisitPrePostIncDec(E, true, false); |
| 159 | } |
| 160 | ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) { |
| 161 | return VisitPrePostIncDec(E, false, true); |
| 162 | } |
| 163 | ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) { |
| 164 | return VisitPrePostIncDec(E, true, true); |
| 165 | } |
| 166 | ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 167 | ComplexPairTy VisitUnaryPlus (const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 168 | TestAndClearIgnoreReal(); |
| 169 | TestAndClearIgnoreImag(); |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 170 | return Visit(E->getSubExpr()); |
| 171 | } |
| 172 | ComplexPairTy VisitUnaryMinus (const UnaryOperator *E); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 173 | ComplexPairTy VisitUnaryNot (const UnaryOperator *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 174 | // LNot,Real,Imag never return complex. |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 175 | ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) { |
| 176 | return Visit(E->getSubExpr()); |
| 177 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 178 | ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 179 | return Visit(DAE->getExpr()); |
| 180 | } |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 181 | ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) { |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 182 | CGF.enterFullExpression(E); |
| 183 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 184 | return Visit(E->getSubExpr()); |
Anders Carlsson | 4b76b4f | 2009-05-31 00:12:05 +0000 | [diff] [blame] | 185 | } |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 186 | ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 187 | assert(E->getType()->isAnyComplexType() && "Expected complex type!"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 188 | QualType Elem = E->getType()->getAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 189 | llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem)); |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 190 | return ComplexPairTy(Null, Null); |
| 191 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 192 | ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { |
| 193 | assert(E->getType()->isAnyComplexType() && "Expected complex type!"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 194 | QualType Elem = E->getType()->getAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 195 | llvm::Constant *Null = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 196 | llvm::Constant::getNullValue(CGF.ConvertType(Elem)); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 197 | return ComplexPairTy(Null, Null); |
| 198 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 199 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 200 | struct BinOpInfo { |
| 201 | ComplexPairTy LHS; |
| 202 | ComplexPairTy RHS; |
| 203 | QualType Ty; // Computation Type. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 206 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 207 | LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E, |
| 208 | ComplexPairTy (ComplexExprEmitter::*Func) |
| 209 | (const BinOpInfo &), |
| 210 | ComplexPairTy &Val); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 211 | ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E, |
| 212 | ComplexPairTy (ComplexExprEmitter::*Func) |
| 213 | (const BinOpInfo &)); |
| 214 | |
| 215 | ComplexPairTy EmitBinAdd(const BinOpInfo &Op); |
| 216 | ComplexPairTy EmitBinSub(const BinOpInfo &Op); |
| 217 | ComplexPairTy EmitBinMul(const BinOpInfo &Op); |
| 218 | ComplexPairTy EmitBinDiv(const BinOpInfo &Op); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 219 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 220 | ComplexPairTy VisitBinAdd(const BinaryOperator *E) { |
| 221 | return EmitBinAdd(EmitBinOps(E)); |
| 222 | } |
| 223 | ComplexPairTy VisitBinSub(const BinaryOperator *E) { |
| 224 | return EmitBinSub(EmitBinOps(E)); |
| 225 | } |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 226 | ComplexPairTy VisitBinMul(const BinaryOperator *E) { |
| 227 | return EmitBinMul(EmitBinOps(E)); |
| 228 | } |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 229 | ComplexPairTy VisitBinDiv(const BinaryOperator *E) { |
| 230 | return EmitBinDiv(EmitBinOps(E)); |
| 231 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 232 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 233 | // Compound assignments. |
| 234 | ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) { |
| 235 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd); |
| 236 | } |
| 237 | ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) { |
| 238 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub); |
| 239 | } |
| 240 | ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) { |
| 241 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul); |
| 242 | } |
| 243 | ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) { |
| 244 | return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv); |
| 245 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 612c40c | 2007-08-26 21:27:07 +0000 | [diff] [blame] | 247 | // GCC rejects rem/and/or/xor for integer complex. |
Chris Lattner | 4034edb | 2007-08-21 17:12:50 +0000 | [diff] [blame] | 248 | // Logical and/or always return int, never complex. |
Chris Lattner | 58dee10 | 2007-08-21 16:57:55 +0000 | [diff] [blame] | 249 | |
| 250 | // No comparisons produce a complex result. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 251 | |
| 252 | LValue EmitBinAssignLValue(const BinaryOperator *E, |
| 253 | ComplexPairTy &Val); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 254 | ComplexPairTy VisitBinAssign (const BinaryOperator *E); |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 255 | ComplexPairTy VisitBinComma (const BinaryOperator *E); |
| 256 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 257 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 258 | ComplexPairTy |
| 259 | VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 260 | ComplexPairTy VisitChooseExpr(ChooseExpr *CE); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 261 | |
| 262 | ComplexPairTy VisitInitListExpr(InitListExpr *E); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 263 | |
| 264 | ComplexPairTy VisitVAArgExpr(VAArgExpr *E); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 265 | |
| 266 | ComplexPairTy VisitAtomicExpr(AtomicExpr *E) { |
| 267 | return CGF.EmitAtomicExpr(E).getComplexVal(); |
| 268 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 269 | }; |
| 270 | } // end anonymous namespace. |
| 271 | |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | // Utilities |
| 274 | //===----------------------------------------------------------------------===// |
| 275 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 276 | /// EmitLoadOfComplex - Given an RValue reference for a complex, emit code to |
| 277 | /// load the real and imaginary pieces, returning them as Real/Imag. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 278 | ComplexPairTy ComplexExprEmitter::EmitLoadOfComplex(llvm::Value *SrcPtr, |
| 279 | bool isVolatile) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 280 | llvm::Value *Real=0, *Imag=0; |
Chris Lattner | 6b5d0bf | 2007-08-26 22:47:40 +0000 | [diff] [blame] | 281 | |
John McCall | ff624c1 | 2010-11-14 09:40:28 +0000 | [diff] [blame] | 282 | if (!IgnoreReal || isVolatile) { |
Benjamin Kramer | 56a44f7 | 2009-11-29 19:51:45 +0000 | [diff] [blame] | 283 | llvm::Value *RealP = Builder.CreateStructGEP(SrcPtr, 0, |
| 284 | SrcPtr->getName() + ".realp"); |
| 285 | Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr->getName() + ".real"); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 286 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 287 | |
John McCall | ff624c1 | 2010-11-14 09:40:28 +0000 | [diff] [blame] | 288 | if (!IgnoreImag || isVolatile) { |
Benjamin Kramer | 56a44f7 | 2009-11-29 19:51:45 +0000 | [diff] [blame] | 289 | llvm::Value *ImagP = Builder.CreateStructGEP(SrcPtr, 1, |
| 290 | SrcPtr->getName() + ".imagp"); |
| 291 | Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr->getName() + ".imag"); |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 292 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 293 | return ComplexPairTy(Real, Imag); |
| 294 | } |
| 295 | |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 296 | /// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 297 | /// specified value pointer. |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 298 | void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, llvm::Value *Ptr, |
| 299 | bool isVolatile) { |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 300 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, "real"); |
| 301 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, "imag"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 76e8033 | 2007-08-21 18:02:02 +0000 | [diff] [blame] | 303 | Builder.CreateStore(Val.first, RealPtr, isVolatile); |
| 304 | Builder.CreateStore(Val.second, ImagPtr, isVolatile); |
Chris Lattner | 46d7d9f | 2007-08-21 17:28:34 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | |
| 308 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 309 | //===----------------------------------------------------------------------===// |
| 310 | // Visitor Methods |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | |
Chris Lattner | 776c649 | 2007-08-21 18:51:13 +0000 | [diff] [blame] | 313 | ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) { |
Fariborz Jahanian | 776b790 | 2011-09-02 20:03:16 +0000 | [diff] [blame] | 314 | CGF.ErrorUnsupported(E, "complex expression"); |
| 315 | llvm::Type *EltTy = |
| 316 | CGF.ConvertType(E->getType()->getAs<ComplexType>()->getElementType()); |
| 317 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 318 | return ComplexPairTy(U, U); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 321 | ComplexPairTy ComplexExprEmitter:: |
| 322 | VisitImaginaryLiteral(const ImaginaryLiteral *IL) { |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 323 | llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr()); |
John McCall | 8d3d6c9 | 2011-01-13 02:03:06 +0000 | [diff] [blame] | 324 | return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag); |
Chris Lattner | db68f1b | 2007-08-26 03:51:12 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 328 | ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | e9f2f45 | 2009-05-27 03:37:57 +0000 | [diff] [blame] | 329 | if (E->getCallReturnType()->isReferenceType()) |
| 330 | return EmitLoadOfLValue(E); |
| 331 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 332 | return CGF.EmitCallExpr(E).getComplexVal(); |
Chris Lattner | 419d25e | 2007-08-23 21:38:16 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 335 | ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 336 | CodeGenFunction::StmtExprEvaluation eval(CGF); |
| 337 | return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getComplexVal(); |
Chris Lattner | 7e20d09 | 2007-08-31 22:51:38 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 340 | /// EmitComplexToComplexCast - Emit a cast from complex value Val to DestType. |
| 341 | ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val, |
| 342 | QualType SrcType, |
| 343 | QualType DestType) { |
| 344 | // Get the src/dest element type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 345 | SrcType = SrcType->getAs<ComplexType>()->getElementType(); |
| 346 | DestType = DestType->getAs<ComplexType>()->getElementType(); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 347 | |
Daniel Dunbar | 56f0d16 | 2009-01-29 06:44:03 +0000 | [diff] [blame] | 348 | // 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] | 349 | // complex type, both the real and imaginary parts follow the conversion |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 350 | // rules for the corresponding real types. |
| 351 | Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType); |
| 352 | Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType); |
| 353 | return Val; |
| 354 | } |
| 355 | |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 356 | ComplexPairTy ComplexExprEmitter::EmitCast(CastExpr::CastKind CK, Expr *Op, |
| 357 | QualType DestTy) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 358 | switch (CK) { |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 359 | case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 360 | |
| 361 | case CK_NoOp: |
| 362 | case CK_LValueToRValue: |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 363 | case CK_UserDefinedConversion: |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 364 | return Visit(Op); |
| 365 | |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 366 | case CK_LValueBitCast: { |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 367 | llvm::Value *V = CGF.EmitLValue(Op).getAddress(); |
| 368 | V = Builder.CreateBitCast(V, |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 369 | CGF.ConvertType(CGF.getContext().getPointerType(DestTy))); |
Douglas Gregor | 77d439a | 2010-07-14 21:35:45 +0000 | [diff] [blame] | 370 | // FIXME: Are the qualifiers correct here? |
| 371 | return EmitLoadOfComplex(V, DestTy.isVolatileQualified()); |
| 372 | } |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 373 | |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 374 | case CK_BitCast: |
| 375 | case CK_BaseToDerived: |
| 376 | case CK_DerivedToBase: |
| 377 | case CK_UncheckedDerivedToBase: |
| 378 | case CK_Dynamic: |
| 379 | case CK_ToUnion: |
| 380 | case CK_ArrayToPointerDecay: |
| 381 | case CK_FunctionToPointerDecay: |
| 382 | case CK_NullToPointer: |
| 383 | case CK_NullToMemberPointer: |
| 384 | case CK_BaseToDerivedMemberPointer: |
| 385 | case CK_DerivedToBaseMemberPointer: |
| 386 | case CK_MemberPointerToBoolean: |
| 387 | case CK_ConstructorConversion: |
| 388 | case CK_IntegralToPointer: |
| 389 | case CK_PointerToIntegral: |
| 390 | case CK_PointerToBoolean: |
| 391 | case CK_ToVoid: |
| 392 | case CK_VectorSplat: |
| 393 | case CK_IntegralCast: |
| 394 | case CK_IntegralToBoolean: |
| 395 | case CK_IntegralToFloating: |
| 396 | case CK_FloatingToIntegral: |
| 397 | case CK_FloatingToBoolean: |
| 398 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 399 | case CK_CPointerToObjCPointerCast: |
| 400 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 401 | case CK_AnyPointerToBlockPointerCast: |
| 402 | case CK_ObjCObjectLValueCast: |
| 403 | case CK_FloatingComplexToReal: |
| 404 | case CK_FloatingComplexToBoolean: |
| 405 | case CK_IntegralComplexToReal: |
| 406 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 407 | case CK_ARCProduceObject: |
| 408 | case CK_ARCConsumeObject: |
| 409 | case CK_ARCReclaimReturnedObject: |
| 410 | case CK_ARCExtendBlockObject: |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 411 | llvm_unreachable("invalid cast kind for complex value"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 412 | |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 413 | case CK_FloatingRealToComplex: |
| 414 | case CK_IntegralRealToComplex: { |
| 415 | llvm::Value *Elt = CGF.EmitScalarExpr(Op); |
| 416 | |
| 417 | // Convert the input element to the element type of the complex. |
| 418 | DestTy = DestTy->getAs<ComplexType>()->getElementType(); |
| 419 | Elt = CGF.EmitScalarConversion(Elt, Op->getType(), DestTy); |
| 420 | |
| 421 | // Return (realval, 0). |
| 422 | return ComplexPairTy(Elt, llvm::Constant::getNullValue(Elt->getType())); |
| 423 | } |
| 424 | |
| 425 | case CK_FloatingComplexCast: |
| 426 | case CK_FloatingComplexToIntegralComplex: |
| 427 | case CK_IntegralComplexCast: |
| 428 | case CK_IntegralComplexToFloatingComplex: |
| 429 | return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy); |
| 430 | } |
| 431 | |
| 432 | llvm_unreachable("unknown cast resulting in complex value"); |
Chris Lattner | d272ff0 | 2007-08-26 05:57:57 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 435 | ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 436 | TestAndClearIgnoreReal(); |
| 437 | TestAndClearIgnoreImag(); |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 438 | ComplexPairTy Op = Visit(E->getSubExpr()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 439 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 440 | llvm::Value *ResR, *ResI; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 441 | if (Op.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 442 | ResR = Builder.CreateFNeg(Op.first, "neg.r"); |
| 443 | ResI = Builder.CreateFNeg(Op.second, "neg.i"); |
| 444 | } else { |
| 445 | ResR = Builder.CreateNeg(Op.first, "neg.r"); |
| 446 | ResI = Builder.CreateNeg(Op.second, "neg.i"); |
| 447 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 448 | return ComplexPairTy(ResR, ResI); |
| 449 | } |
| 450 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 451 | ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 452 | TestAndClearIgnoreReal(); |
| 453 | TestAndClearIgnoreImag(); |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 454 | // ~(a+ib) = a + i*-b |
| 455 | ComplexPairTy Op = Visit(E->getSubExpr()); |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 456 | llvm::Value *ResI; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 457 | if (Op.second->getType()->isFloatingPointTy()) |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 458 | ResI = Builder.CreateFNeg(Op.second, "conj.i"); |
| 459 | else |
| 460 | ResI = Builder.CreateNeg(Op.second, "conj.i"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 461 | |
Chris Lattner | e98a11c | 2007-08-21 20:41:44 +0000 | [diff] [blame] | 462 | return ComplexPairTy(Op.first, ResI); |
| 463 | } |
Chris Lattner | fa2b9c9 | 2007-08-21 20:08:23 +0000 | [diff] [blame] | 464 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 465 | ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 466 | llvm::Value *ResR, *ResI; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 467 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 468 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 469 | ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r"); |
| 470 | ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i"); |
| 471 | } else { |
| 472 | ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r"); |
| 473 | ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i"); |
| 474 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 475 | return ComplexPairTy(ResR, ResI); |
| 476 | } |
| 477 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 478 | ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 479 | llvm::Value *ResR, *ResI; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 480 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 481 | ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r"); |
| 482 | ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i"); |
| 483 | } else { |
| 484 | ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r"); |
| 485 | ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i"); |
| 486 | } |
Chris Lattner | 6bc1ade | 2007-08-23 23:46:33 +0000 | [diff] [blame] | 487 | return ComplexPairTy(ResR, ResI); |
| 488 | } |
| 489 | |
| 490 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 491 | ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 492 | using llvm::Value; |
| 493 | Value *ResR, *ResI; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 494 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 495 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 496 | Value *ResRl = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl"); |
| 497 | Value *ResRr = Builder.CreateFMul(Op.LHS.second, Op.RHS.second,"mul.rr"); |
| 498 | ResR = Builder.CreateFSub(ResRl, ResRr, "mul.r"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 500 | Value *ResIl = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il"); |
| 501 | Value *ResIr = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir"); |
| 502 | ResI = Builder.CreateFAdd(ResIl, ResIr, "mul.i"); |
| 503 | } else { |
| 504 | Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl"); |
| 505 | Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second,"mul.rr"); |
| 506 | ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); |
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 | Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il"); |
| 509 | Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir"); |
| 510 | ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); |
| 511 | } |
Chris Lattner | 2823c19 | 2007-08-21 16:34:16 +0000 | [diff] [blame] | 512 | return ComplexPairTy(ResR, ResI); |
| 513 | } |
| 514 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 515 | ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) { |
| 516 | llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second; |
| 517 | llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 519 | |
| 520 | llvm::Value *DSTr, *DSTi; |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 521 | if (Op.LHS.first->getType()->isFloatingPointTy()) { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 522 | // (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] | 523 | llvm::Value *Tmp1 = Builder.CreateFMul(LHSr, RHSr); // a*c |
| 524 | llvm::Value *Tmp2 = Builder.CreateFMul(LHSi, RHSi); // b*d |
| 525 | llvm::Value *Tmp3 = Builder.CreateFAdd(Tmp1, Tmp2); // ac+bd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 526 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 527 | llvm::Value *Tmp4 = Builder.CreateFMul(RHSr, RHSr); // c*c |
| 528 | llvm::Value *Tmp5 = Builder.CreateFMul(RHSi, RHSi); // d*d |
| 529 | llvm::Value *Tmp6 = Builder.CreateFAdd(Tmp4, Tmp5); // cc+dd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 530 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 531 | llvm::Value *Tmp7 = Builder.CreateFMul(LHSi, RHSr); // b*c |
| 532 | llvm::Value *Tmp8 = Builder.CreateFMul(LHSr, RHSi); // a*d |
| 533 | llvm::Value *Tmp9 = Builder.CreateFSub(Tmp7, Tmp8); // bc-ad |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 534 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 535 | DSTr = Builder.CreateFDiv(Tmp3, Tmp6); |
| 536 | DSTi = Builder.CreateFDiv(Tmp9, Tmp6); |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 537 | } else { |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 538 | // (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] | 539 | llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c |
| 540 | llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d |
| 541 | llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 542 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 543 | llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c |
| 544 | llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d |
| 545 | llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 546 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 547 | llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c |
| 548 | llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d |
| 549 | llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 550 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 551 | if (Op.Ty->getAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) { |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 552 | DSTr = Builder.CreateUDiv(Tmp3, Tmp6); |
| 553 | DSTi = Builder.CreateUDiv(Tmp9, Tmp6); |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 554 | } else { |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 555 | DSTr = Builder.CreateSDiv(Tmp3, Tmp6); |
| 556 | DSTi = Builder.CreateSDiv(Tmp9, Tmp6); |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 560 | return ComplexPairTy(DSTr, DSTi); |
| 561 | } |
| 562 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 563 | ComplexExprEmitter::BinOpInfo |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 564 | ComplexExprEmitter::EmitBinOps(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 565 | TestAndClearIgnoreReal(); |
| 566 | TestAndClearIgnoreImag(); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 567 | BinOpInfo Ops; |
| 568 | Ops.LHS = Visit(E->getLHS()); |
| 569 | Ops.RHS = Visit(E->getRHS()); |
| 570 | Ops.Ty = E->getType(); |
| 571 | return Ops; |
| 572 | } |
| 573 | |
| 574 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 575 | LValue ComplexExprEmitter:: |
| 576 | EmitCompoundAssignLValue(const CompoundAssignOperator *E, |
| 577 | ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&), |
| 578 | ComplexPairTy &Val) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 579 | TestAndClearIgnoreReal(); |
| 580 | TestAndClearIgnoreImag(); |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 581 | QualType LHSTy = E->getLHS()->getType(); |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 582 | |
| 583 | BinOpInfo OpInfo; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 584 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 585 | // Load the RHS and LHS operands. |
| 586 | // __block variables need to have the rhs evaluated first, plus this should |
John McCall | 641ca7d | 2010-11-16 05:45:35 +0000 | [diff] [blame] | 587 | // improve codegen a little. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 588 | OpInfo.Ty = E->getComputationResultType(); |
John McCall | 641ca7d | 2010-11-16 05:45:35 +0000 | [diff] [blame] | 589 | |
| 590 | // The RHS should have been converted to the computation type. |
| 591 | assert(OpInfo.Ty->isAnyComplexType()); |
| 592 | assert(CGF.getContext().hasSameUnqualifiedType(OpInfo.Ty, |
| 593 | E->getRHS()->getType())); |
| 594 | OpInfo.RHS = Visit(E->getRHS()); |
Fariborz Jahanian | dfb1eb2 | 2010-03-23 18:08:50 +0000 | [diff] [blame] | 595 | |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 596 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 597 | |
| 598 | // Load from the l-value. |
| 599 | ComplexPairTy LHSComplexPair = EmitLoadOfLValue(LHS); |
Fariborz Jahanian | dfb1eb2 | 2010-03-23 18:08:50 +0000 | [diff] [blame] | 600 | |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 601 | OpInfo.LHS = EmitComplexToComplexCast(LHSComplexPair, LHSTy, OpInfo.Ty); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 602 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 603 | // Expand the binary operator. |
| 604 | ComplexPairTy Result = (this->*Func)(OpInfo); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 605 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 606 | // Truncate the result back to the LHS type. |
| 607 | Result = EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 608 | Val = Result; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 609 | |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 610 | // Store the result value into the LHS lvalue. |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 611 | EmitStoreThroughLValue(Result, LHS); |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 612 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 613 | return LHS; |
Chris Lattner | ab340c2 | 2007-08-26 22:09:01 +0000 | [diff] [blame] | 614 | } |
Chris Lattner | 3219c5d | 2007-08-26 21:24:19 +0000 | [diff] [blame] | 615 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 616 | // Compound assignments. |
| 617 | ComplexPairTy ComplexExprEmitter:: |
| 618 | EmitCompoundAssign(const CompoundAssignOperator *E, |
| 619 | ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){ |
| 620 | ComplexPairTy Val; |
| 621 | LValue LV = EmitCompoundAssignLValue(E, Func, Val); |
| 622 | |
| 623 | // The result of an assignment in C is the assigned r-value. |
| 624 | if (!CGF.getContext().getLangOptions().CPlusPlus) |
| 625 | return Val; |
| 626 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 627 | // If the lvalue is non-volatile, return the computed value of the assignment. |
| 628 | if (!LV.isVolatileQualified()) |
| 629 | return Val; |
| 630 | |
| 631 | return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified()); |
| 632 | } |
| 633 | |
| 634 | LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E, |
| 635 | ComplexPairTy &Val) { |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 636 | assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), |
| 637 | E->getRHS()->getType()) && |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 638 | "Invalid assignment"); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 639 | TestAndClearIgnoreReal(); |
| 640 | TestAndClearIgnoreImag(); |
| 641 | |
John McCall | cd940a1 | 2010-12-06 06:10:02 +0000 | [diff] [blame] | 642 | // Emit the RHS. __block variables need the RHS evaluated first. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 643 | Val = Visit(E->getRHS()); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 644 | |
| 645 | // Compute the address to store into. |
| 646 | LValue LHS = CGF.EmitLValue(E->getLHS()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 647 | |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 648 | // Store the result value into the LHS lvalue. |
John McCall | 0e800c9 | 2010-12-04 08:14:53 +0000 | [diff] [blame] | 649 | EmitStoreThroughLValue(Val, LHS); |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 650 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 651 | return LHS; |
| 652 | } |
Daniel Dunbar | 120bc77 | 2010-06-29 22:44:21 +0000 | [diff] [blame] | 653 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 654 | ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 655 | ComplexPairTy Val; |
| 656 | LValue LV = EmitBinAssignLValue(E, Val); |
| 657 | |
| 658 | // The result of an assignment in C is the assigned r-value. |
| 659 | if (!CGF.getContext().getLangOptions().CPlusPlus) |
| 660 | return Val; |
| 661 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 662 | // If the lvalue is non-volatile, return the computed value of the assignment. |
| 663 | if (!LV.isVolatileQualified()) |
| 664 | return Val; |
| 665 | |
| 666 | return EmitLoadOfComplex(LV.getAddress(), LV.isVolatileQualified()); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 669 | ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 670 | CGF.EmitIgnoredExpr(E->getLHS()); |
Chris Lattner | 756a4d8 | 2007-08-21 17:15:50 +0000 | [diff] [blame] | 671 | return Visit(E->getRHS()); |
| 672 | } |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 673 | |
| 674 | ComplexPairTy ComplexExprEmitter:: |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 675 | VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 676 | TestAndClearIgnoreReal(); |
| 677 | TestAndClearIgnoreImag(); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 678 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 679 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
| 680 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 681 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 682 | // Bind the common expression if necessary. |
Eli Friedman | d97927d | 2012-01-06 20:42:20 +0000 | [diff] [blame] | 683 | CodeGenFunction::OpaqueValueMapping binding(CGF, E); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 684 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 685 | CodeGenFunction::ConditionalEvaluation eval(CGF); |
| 686 | CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 687 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 688 | eval.begin(CGF); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 689 | CGF.EmitBlock(LHSBlock); |
Fariborz Jahanian | aa3b57e | 2010-09-20 23:50:22 +0000 | [diff] [blame] | 690 | ComplexPairTy LHS = Visit(E->getTrueExpr()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 691 | LHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 692 | CGF.EmitBranch(ContBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 693 | eval.end(CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 694 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 695 | eval.begin(CGF); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 696 | CGF.EmitBlock(RHSBlock); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 697 | ComplexPairTy RHS = Visit(E->getFalseExpr()); |
Chris Lattner | 5083a53 | 2007-08-21 17:39:38 +0000 | [diff] [blame] | 698 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 699 | CGF.EmitBlock(ContBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 700 | eval.end(CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 701 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 702 | // Create a PHI node for the real part. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 703 | llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 704 | RealPN->addIncoming(LHS.first, LHSBlock); |
| 705 | RealPN->addIncoming(RHS.first, RHSBlock); |
| 706 | |
| 707 | // Create a PHI node for the imaginary part. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 708 | llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i"); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 709 | ImagPN->addIncoming(LHS.second, LHSBlock); |
| 710 | ImagPN->addIncoming(RHS.second, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 711 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 712 | return ComplexPairTy(RealPN, ImagPN); |
| 713 | } |
| 714 | |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 715 | ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 716 | return Visit(E->getChosenSubExpr(CGF.getContext())); |
Chris Lattner | d0b1203 | 2007-08-24 02:18:47 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 719 | ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 720 | bool Ignore = TestAndClearIgnoreReal(); |
| 721 | (void)Ignore; |
| 722 | assert (Ignore == false && "init list ignored"); |
| 723 | Ignore = TestAndClearIgnoreImag(); |
| 724 | (void)Ignore; |
| 725 | assert (Ignore == false && "init list ignored"); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 726 | |
| 727 | if (E->getNumInits() == 2) { |
| 728 | llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0)); |
| 729 | llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1)); |
| 730 | return ComplexPairTy(Real, Imag); |
| 731 | } else if (E->getNumInits() == 1) { |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 732 | return Visit(E->getInit(0)); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 733 | } |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 734 | |
| 735 | // Empty init list intializes to null |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 736 | assert(E->getNumInits() == 0 && "Unexpected number of inits"); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 737 | QualType Ty = E->getType()->getAs<ComplexType>()->getElementType(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 738 | llvm::Type* LTy = CGF.ConvertType(Ty); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 739 | llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy); |
Eli Friedman | c4777f1 | 2008-05-13 23:11:35 +0000 | [diff] [blame] | 740 | return ComplexPairTy(zeroConstant, zeroConstant); |
| 741 | } |
| 742 | |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 743 | ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) { |
Daniel Dunbar | 0785570 | 2009-02-11 22:25:55 +0000 | [diff] [blame] | 744 | llvm::Value *ArgValue = CGF.EmitVAListRef(E->getSubExpr()); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 745 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, E->getType()); |
| 746 | |
| 747 | if (!ArgPtr) { |
| 748 | CGF.ErrorUnsupported(E, "complex va_arg expression"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 749 | llvm::Type *EltTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 750 | CGF.ConvertType(E->getType()->getAs<ComplexType>()->getElementType()); |
Daniel Dunbar | 4e484b8 | 2009-02-10 03:03:30 +0000 | [diff] [blame] | 751 | llvm::Value *U = llvm::UndefValue::get(EltTy); |
| 752 | return ComplexPairTy(U, U); |
| 753 | } |
| 754 | |
| 755 | // FIXME Volatility. |
| 756 | return EmitLoadOfComplex(ArgPtr, false); |
| 757 | } |
| 758 | |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 759 | //===----------------------------------------------------------------------===// |
| 760 | // Entry Point into this File |
| 761 | //===----------------------------------------------------------------------===// |
| 762 | |
| 763 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 764 | /// complex type, ignoring the result. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 765 | ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal, |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 766 | bool IgnoreImag) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 767 | assert(E && E->getType()->isAnyComplexType() && |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 768 | "Invalid complex expression to emit"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 769 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 770 | return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag) |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 771 | .Visit(const_cast<Expr*>(E)); |
Chris Lattner | b6ef18a | 2007-08-21 05:54:00 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 774 | /// EmitComplexExprIntoAddr - Emit the computation of the specified expression |
| 775 | /// of complex type, storing into the specified Value*. |
| 776 | void CodeGenFunction::EmitComplexExprIntoAddr(const Expr *E, |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 777 | llvm::Value *DestAddr, |
| 778 | bool DestIsVolatile) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 779 | assert(E && E->getType()->isAnyComplexType() && |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 780 | "Invalid complex expression to emit"); |
| 781 | ComplexExprEmitter Emitter(*this); |
| 782 | ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E)); |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 783 | Emitter.EmitStoreOfComplex(Val, DestAddr, DestIsVolatile); |
Chris Lattner | 23b1cdb | 2007-08-23 23:43:33 +0000 | [diff] [blame] | 784 | } |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 785 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 786 | /// StoreComplexToAddr - Store a complex number into the specified address. |
| 787 | void CodeGenFunction::StoreComplexToAddr(ComplexPairTy V, |
| 788 | llvm::Value *DestAddr, |
| 789 | bool DestIsVolatile) { |
| 790 | ComplexExprEmitter(*this).EmitStoreOfComplex(V, DestAddr, DestIsVolatile); |
| 791 | } |
| 792 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 793 | /// LoadComplexFromAddr - Load a complex number from the specified address. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 794 | ComplexPairTy CodeGenFunction::LoadComplexFromAddr(llvm::Value *SrcAddr, |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 795 | bool SrcIsVolatile) { |
| 796 | return ComplexExprEmitter(*this).EmitLoadOfComplex(SrcAddr, SrcIsVolatile); |
| 797 | } |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 798 | |
| 799 | LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 800 | assert(E->getOpcode() == BO_Assign); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 801 | ComplexPairTy Val; // ignored |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 802 | return ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val); |
| 803 | } |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 804 | |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 805 | LValue CodeGenFunction:: |
| 806 | EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) { |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 807 | ComplexPairTy(ComplexExprEmitter::*Op)(const ComplexExprEmitter::BinOpInfo &); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 808 | switch (E->getOpcode()) { |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 809 | case BO_MulAssign: Op = &ComplexExprEmitter::EmitBinMul; break; |
| 810 | case BO_DivAssign: Op = &ComplexExprEmitter::EmitBinDiv; break; |
| 811 | case BO_SubAssign: Op = &ComplexExprEmitter::EmitBinSub; break; |
| 812 | case BO_AddAssign: Op = &ComplexExprEmitter::EmitBinAdd; break; |
| 813 | |
| 814 | default: |
| 815 | llvm_unreachable("unexpected complex compound assignment"); |
| 816 | Op = 0; |
| 817 | } |
| 818 | |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 819 | ComplexPairTy Val; // ignored |
| 820 | return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val); |
John McCall | 83ce9d4 | 2010-11-16 23:07:28 +0000 | [diff] [blame] | 821 | } |