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