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