1 //===--- CGExprComplex.cpp - Emit LLVM Code for Complex Exprs -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This contains code to emit Expr nodes with complex types as LLVM code. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CGOpenMPRuntime.h" 14 #include "CodeGenFunction.h" 15 #include "CodeGenModule.h" 16 #include "ConstantEmitter.h" 17 #include "clang/AST/StmtVisitor.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/IR/Constants.h" 20 #include "llvm/IR/Instructions.h" 21 #include "llvm/IR/MDBuilder.h" 22 #include "llvm/IR/Metadata.h" 23 #include <algorithm> 24 using namespace clang; 25 using namespace CodeGen; 26 27 //===----------------------------------------------------------------------===// 28 // Complex Expression Emitter 29 //===----------------------------------------------------------------------===// 30 31 typedef CodeGenFunction::ComplexPairTy ComplexPairTy; 32 33 /// Return the complex type that we are meant to emit. 34 static const ComplexType *getComplexType(QualType type) { 35 type = type.getCanonicalType(); 36 if (const ComplexType *comp = dyn_cast<ComplexType>(type)) { 37 return comp; 38 } else { 39 return cast<ComplexType>(cast<AtomicType>(type)->getValueType()); 40 } 41 } 42 43 namespace { 44 class ComplexExprEmitter 45 : public StmtVisitor<ComplexExprEmitter, ComplexPairTy> { 46 CodeGenFunction &CGF; 47 CGBuilderTy &Builder; 48 bool IgnoreReal; 49 bool IgnoreImag; 50 public: 51 ComplexExprEmitter(CodeGenFunction &cgf, bool ir=false, bool ii=false) 52 : CGF(cgf), Builder(CGF.Builder), IgnoreReal(ir), IgnoreImag(ii) { 53 } 54 55 56 //===--------------------------------------------------------------------===// 57 // Utilities 58 //===--------------------------------------------------------------------===// 59 60 bool TestAndClearIgnoreReal() { 61 bool I = IgnoreReal; 62 IgnoreReal = false; 63 return I; 64 } 65 bool TestAndClearIgnoreImag() { 66 bool I = IgnoreImag; 67 IgnoreImag = false; 68 return I; 69 } 70 71 /// EmitLoadOfLValue - Given an expression with complex type that represents a 72 /// value l-value, this method emits the address of the l-value, then loads 73 /// and returns the result. 74 ComplexPairTy EmitLoadOfLValue(const Expr *E) { 75 return EmitLoadOfLValue(CGF.EmitLValue(E), E->getExprLoc()); 76 } 77 78 ComplexPairTy EmitLoadOfLValue(LValue LV, SourceLocation Loc); 79 80 /// EmitStoreOfComplex - Store the specified real/imag parts into the 81 /// specified value pointer. 82 void EmitStoreOfComplex(ComplexPairTy Val, LValue LV, bool isInit); 83 84 /// Emit a cast from complex value Val to DestType. 85 ComplexPairTy EmitComplexToComplexCast(ComplexPairTy Val, QualType SrcType, 86 QualType DestType, SourceLocation Loc); 87 /// Emit a cast from scalar value Val to DestType. 88 ComplexPairTy EmitScalarToComplexCast(llvm::Value *Val, QualType SrcType, 89 QualType DestType, SourceLocation Loc); 90 91 //===--------------------------------------------------------------------===// 92 // Visitor Methods 93 //===--------------------------------------------------------------------===// 94 95 ComplexPairTy Visit(Expr *E) { 96 ApplyDebugLocation DL(CGF, E); 97 return StmtVisitor<ComplexExprEmitter, ComplexPairTy>::Visit(E); 98 } 99 100 ComplexPairTy VisitStmt(Stmt *S) { 101 S->dump(llvm::errs(), CGF.getContext()); 102 llvm_unreachable("Stmt can't have complex result type!"); 103 } 104 ComplexPairTy VisitExpr(Expr *S); 105 ComplexPairTy VisitConstantExpr(ConstantExpr *E) { 106 if (llvm::Constant *Result = ConstantEmitter(CGF).tryEmitConstantExpr(E)) 107 return ComplexPairTy(Result->getAggregateElement(0U), 108 Result->getAggregateElement(1U)); 109 return Visit(E->getSubExpr()); 110 } 111 ComplexPairTy VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr());} 112 ComplexPairTy VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 113 return Visit(GE->getResultExpr()); 114 } 115 ComplexPairTy VisitImaginaryLiteral(const ImaginaryLiteral *IL); 116 ComplexPairTy 117 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) { 118 return Visit(PE->getReplacement()); 119 } 120 ComplexPairTy VisitCoawaitExpr(CoawaitExpr *S) { 121 return CGF.EmitCoawaitExpr(*S).getComplexVal(); 122 } 123 ComplexPairTy VisitCoyieldExpr(CoyieldExpr *S) { 124 return CGF.EmitCoyieldExpr(*S).getComplexVal(); 125 } 126 ComplexPairTy VisitUnaryCoawait(const UnaryOperator *E) { 127 return Visit(E->getSubExpr()); 128 } 129 130 ComplexPairTy emitConstant(const CodeGenFunction::ConstantEmission &Constant, 131 Expr *E) { 132 assert(Constant && "not a constant"); 133 if (Constant.isReference()) 134 return EmitLoadOfLValue(Constant.getReferenceLValue(CGF, E), 135 E->getExprLoc()); 136 137 llvm::Constant *pair = Constant.getValue(); 138 return ComplexPairTy(pair->getAggregateElement(0U), 139 pair->getAggregateElement(1U)); 140 } 141 142 // l-values. 143 ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) { 144 if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E)) 145 return emitConstant(Constant, E); 146 return EmitLoadOfLValue(E); 147 } 148 ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 149 return EmitLoadOfLValue(E); 150 } 151 ComplexPairTy VisitObjCMessageExpr(ObjCMessageExpr *E) { 152 return CGF.EmitObjCMessageExpr(E).getComplexVal(); 153 } 154 ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); } 155 ComplexPairTy VisitMemberExpr(MemberExpr *ME) { 156 if (CodeGenFunction::ConstantEmission Constant = 157 CGF.tryEmitAsConstant(ME)) { 158 CGF.EmitIgnoredExpr(ME->getBase()); 159 return emitConstant(Constant, ME); 160 } 161 return EmitLoadOfLValue(ME); 162 } 163 ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) { 164 if (E->isGLValue()) 165 return EmitLoadOfLValue(CGF.getOrCreateOpaqueLValueMapping(E), 166 E->getExprLoc()); 167 return CGF.getOrCreateOpaqueRValueMapping(E).getComplexVal(); 168 } 169 170 ComplexPairTy VisitPseudoObjectExpr(PseudoObjectExpr *E) { 171 return CGF.EmitPseudoObjectRValue(E).getComplexVal(); 172 } 173 174 // FIXME: CompoundLiteralExpr 175 176 ComplexPairTy EmitCast(CastKind CK, Expr *Op, QualType DestTy); 177 ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) { 178 // Unlike for scalars, we don't have to worry about function->ptr demotion 179 // here. 180 if (E->changesVolatileQualification()) 181 return EmitLoadOfLValue(E); 182 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType()); 183 } 184 ComplexPairTy VisitCastExpr(CastExpr *E) { 185 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E)) 186 CGF.CGM.EmitExplicitCastExprType(ECE, &CGF); 187 if (E->changesVolatileQualification()) 188 return EmitLoadOfLValue(E); 189 return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType()); 190 } 191 ComplexPairTy VisitCallExpr(const CallExpr *E); 192 ComplexPairTy VisitStmtExpr(const StmtExpr *E); 193 194 // Operators. 195 ComplexPairTy VisitPrePostIncDec(const UnaryOperator *E, 196 bool isInc, bool isPre) { 197 LValue LV = CGF.EmitLValue(E->getSubExpr()); 198 return CGF.EmitComplexPrePostIncDec(E, LV, isInc, isPre); 199 } 200 ComplexPairTy VisitUnaryPostDec(const UnaryOperator *E) { 201 return VisitPrePostIncDec(E, false, false); 202 } 203 ComplexPairTy VisitUnaryPostInc(const UnaryOperator *E) { 204 return VisitPrePostIncDec(E, true, false); 205 } 206 ComplexPairTy VisitUnaryPreDec(const UnaryOperator *E) { 207 return VisitPrePostIncDec(E, false, true); 208 } 209 ComplexPairTy VisitUnaryPreInc(const UnaryOperator *E) { 210 return VisitPrePostIncDec(E, true, true); 211 } 212 ComplexPairTy VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } 213 214 ComplexPairTy VisitUnaryPlus(const UnaryOperator *E, 215 QualType PromotionType = QualType()); 216 ComplexPairTy VisitPlus(const UnaryOperator *E, QualType PromotionType); 217 ComplexPairTy VisitUnaryMinus(const UnaryOperator *E, 218 QualType PromotionType = QualType()); 219 ComplexPairTy VisitMinus(const UnaryOperator *E, QualType PromotionType); 220 ComplexPairTy VisitUnaryNot (const UnaryOperator *E); 221 // LNot,Real,Imag never return complex. 222 ComplexPairTy VisitUnaryExtension(const UnaryOperator *E) { 223 return Visit(E->getSubExpr()); 224 } 225 ComplexPairTy VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 226 CodeGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE); 227 return Visit(DAE->getExpr()); 228 } 229 ComplexPairTy VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) { 230 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF, DIE); 231 return Visit(DIE->getExpr()); 232 } 233 ComplexPairTy VisitExprWithCleanups(ExprWithCleanups *E) { 234 CodeGenFunction::RunCleanupsScope Scope(CGF); 235 ComplexPairTy Vals = Visit(E->getSubExpr()); 236 // Defend against dominance problems caused by jumps out of expression 237 // evaluation through the shared cleanup block. 238 Scope.ForceCleanup({&Vals.first, &Vals.second}); 239 return Vals; 240 } 241 ComplexPairTy VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { 242 assert(E->getType()->isAnyComplexType() && "Expected complex type!"); 243 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType(); 244 llvm::Constant *Null = llvm::Constant::getNullValue(CGF.ConvertType(Elem)); 245 return ComplexPairTy(Null, Null); 246 } 247 ComplexPairTy VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { 248 assert(E->getType()->isAnyComplexType() && "Expected complex type!"); 249 QualType Elem = E->getType()->castAs<ComplexType>()->getElementType(); 250 llvm::Constant *Null = 251 llvm::Constant::getNullValue(CGF.ConvertType(Elem)); 252 return ComplexPairTy(Null, Null); 253 } 254 255 struct BinOpInfo { 256 ComplexPairTy LHS; 257 ComplexPairTy RHS; 258 QualType Ty; // Computation Type. 259 FPOptions FPFeatures; 260 }; 261 262 BinOpInfo EmitBinOps(const BinaryOperator *E, 263 QualType PromotionTy = QualType()); 264 ComplexPairTy EmitPromoted(const Expr *E, QualType PromotionTy); 265 ComplexPairTy EmitPromotedComplexOperand(const Expr *E, QualType PromotionTy); 266 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E, 267 ComplexPairTy (ComplexExprEmitter::*Func) 268 (const BinOpInfo &), 269 RValue &Val); 270 ComplexPairTy EmitCompoundAssign(const CompoundAssignOperator *E, 271 ComplexPairTy (ComplexExprEmitter::*Func) 272 (const BinOpInfo &)); 273 274 ComplexPairTy EmitBinAdd(const BinOpInfo &Op); 275 ComplexPairTy EmitBinSub(const BinOpInfo &Op); 276 ComplexPairTy EmitBinMul(const BinOpInfo &Op); 277 ComplexPairTy EmitBinDiv(const BinOpInfo &Op); 278 ComplexPairTy EmitAlgebraicDiv(llvm::Value *A, llvm::Value *B, llvm::Value *C, 279 llvm::Value *D); 280 ComplexPairTy EmitRangeReductionDiv(llvm::Value *A, llvm::Value *B, 281 llvm::Value *C, llvm::Value *D); 282 283 ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName, 284 const BinOpInfo &Op); 285 286 QualType getPromotionType(QualType Ty) { 287 if (auto *CT = Ty->getAs<ComplexType>()) { 288 QualType ElementType = CT->getElementType(); 289 if (ElementType.UseExcessPrecision(CGF.getContext())) 290 return CGF.getContext().getComplexType(CGF.getContext().FloatTy); 291 } 292 if (Ty.UseExcessPrecision(CGF.getContext())) 293 return CGF.getContext().FloatTy; 294 return QualType(); 295 } 296 297 #define HANDLEBINOP(OP) \ 298 ComplexPairTy VisitBin##OP(const BinaryOperator *E) { \ 299 QualType promotionTy = getPromotionType(E->getType()); \ 300 ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy)); \ 301 if (!promotionTy.isNull()) \ 302 result = \ 303 CGF.EmitUnPromotedValue(result, E->getType()); \ 304 return result; \ 305 } 306 307 HANDLEBINOP(Mul) 308 HANDLEBINOP(Div) 309 HANDLEBINOP(Add) 310 HANDLEBINOP(Sub) 311 #undef HANDLEBINOP 312 313 ComplexPairTy VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E) { 314 return Visit(E->getSemanticForm()); 315 } 316 317 // Compound assignments. 318 ComplexPairTy VisitBinAddAssign(const CompoundAssignOperator *E) { 319 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinAdd); 320 } 321 ComplexPairTy VisitBinSubAssign(const CompoundAssignOperator *E) { 322 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinSub); 323 } 324 ComplexPairTy VisitBinMulAssign(const CompoundAssignOperator *E) { 325 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinMul); 326 } 327 ComplexPairTy VisitBinDivAssign(const CompoundAssignOperator *E) { 328 return EmitCompoundAssign(E, &ComplexExprEmitter::EmitBinDiv); 329 } 330 331 // GCC rejects rem/and/or/xor for integer complex. 332 // Logical and/or always return int, never complex. 333 334 // No comparisons produce a complex result. 335 336 LValue EmitBinAssignLValue(const BinaryOperator *E, 337 ComplexPairTy &Val); 338 ComplexPairTy VisitBinAssign (const BinaryOperator *E); 339 ComplexPairTy VisitBinComma (const BinaryOperator *E); 340 341 342 ComplexPairTy 343 VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO); 344 ComplexPairTy VisitChooseExpr(ChooseExpr *CE); 345 346 ComplexPairTy VisitInitListExpr(InitListExpr *E); 347 348 ComplexPairTy VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 349 return EmitLoadOfLValue(E); 350 } 351 352 ComplexPairTy VisitVAArgExpr(VAArgExpr *E); 353 354 ComplexPairTy VisitAtomicExpr(AtomicExpr *E) { 355 return CGF.EmitAtomicExpr(E).getComplexVal(); 356 } 357 }; 358 } // end anonymous namespace. 359 360 //===----------------------------------------------------------------------===// 361 // Utilities 362 //===----------------------------------------------------------------------===// 363 364 Address CodeGenFunction::emitAddrOfRealComponent(Address addr, 365 QualType complexType) { 366 return Builder.CreateStructGEP(addr, 0, addr.getName() + ".realp"); 367 } 368 369 Address CodeGenFunction::emitAddrOfImagComponent(Address addr, 370 QualType complexType) { 371 return Builder.CreateStructGEP(addr, 1, addr.getName() + ".imagp"); 372 } 373 374 /// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to 375 /// load the real and imaginary pieces, returning them as Real/Imag. 376 ComplexPairTy ComplexExprEmitter::EmitLoadOfLValue(LValue lvalue, 377 SourceLocation loc) { 378 assert(lvalue.isSimple() && "non-simple complex l-value?"); 379 if (lvalue.getType()->isAtomicType()) 380 return CGF.EmitAtomicLoad(lvalue, loc).getComplexVal(); 381 382 Address SrcPtr = lvalue.getAddress(CGF); 383 bool isVolatile = lvalue.isVolatileQualified(); 384 385 llvm::Value *Real = nullptr, *Imag = nullptr; 386 387 if (!IgnoreReal || isVolatile) { 388 Address RealP = CGF.emitAddrOfRealComponent(SrcPtr, lvalue.getType()); 389 Real = Builder.CreateLoad(RealP, isVolatile, SrcPtr.getName() + ".real"); 390 } 391 392 if (!IgnoreImag || isVolatile) { 393 Address ImagP = CGF.emitAddrOfImagComponent(SrcPtr, lvalue.getType()); 394 Imag = Builder.CreateLoad(ImagP, isVolatile, SrcPtr.getName() + ".imag"); 395 } 396 397 return ComplexPairTy(Real, Imag); 398 } 399 400 /// EmitStoreOfComplex - Store the specified real/imag parts into the 401 /// specified value pointer. 402 void ComplexExprEmitter::EmitStoreOfComplex(ComplexPairTy Val, LValue lvalue, 403 bool isInit) { 404 if (lvalue.getType()->isAtomicType() || 405 (!isInit && CGF.LValueIsSuitableForInlineAtomic(lvalue))) 406 return CGF.EmitAtomicStore(RValue::getComplex(Val), lvalue, isInit); 407 408 Address Ptr = lvalue.getAddress(CGF); 409 Address RealPtr = CGF.emitAddrOfRealComponent(Ptr, lvalue.getType()); 410 Address ImagPtr = CGF.emitAddrOfImagComponent(Ptr, lvalue.getType()); 411 412 Builder.CreateStore(Val.first, RealPtr, lvalue.isVolatileQualified()); 413 Builder.CreateStore(Val.second, ImagPtr, lvalue.isVolatileQualified()); 414 } 415 416 417 418 //===----------------------------------------------------------------------===// 419 // Visitor Methods 420 //===----------------------------------------------------------------------===// 421 422 ComplexPairTy ComplexExprEmitter::VisitExpr(Expr *E) { 423 CGF.ErrorUnsupported(E, "complex expression"); 424 llvm::Type *EltTy = 425 CGF.ConvertType(getComplexType(E->getType())->getElementType()); 426 llvm::Value *U = llvm::UndefValue::get(EltTy); 427 return ComplexPairTy(U, U); 428 } 429 430 ComplexPairTy ComplexExprEmitter:: 431 VisitImaginaryLiteral(const ImaginaryLiteral *IL) { 432 llvm::Value *Imag = CGF.EmitScalarExpr(IL->getSubExpr()); 433 return ComplexPairTy(llvm::Constant::getNullValue(Imag->getType()), Imag); 434 } 435 436 437 ComplexPairTy ComplexExprEmitter::VisitCallExpr(const CallExpr *E) { 438 if (E->getCallReturnType(CGF.getContext())->isReferenceType()) 439 return EmitLoadOfLValue(E); 440 441 return CGF.EmitCallExpr(E).getComplexVal(); 442 } 443 444 ComplexPairTy ComplexExprEmitter::VisitStmtExpr(const StmtExpr *E) { 445 CodeGenFunction::StmtExprEvaluation eval(CGF); 446 Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), true); 447 assert(RetAlloca.isValid() && "Expected complex return value"); 448 return EmitLoadOfLValue(CGF.MakeAddrLValue(RetAlloca, E->getType()), 449 E->getExprLoc()); 450 } 451 452 /// Emit a cast from complex value Val to DestType. 453 ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val, 454 QualType SrcType, 455 QualType DestType, 456 SourceLocation Loc) { 457 // Get the src/dest element type. 458 SrcType = SrcType->castAs<ComplexType>()->getElementType(); 459 DestType = DestType->castAs<ComplexType>()->getElementType(); 460 461 // C99 6.3.1.6: When a value of complex type is converted to another 462 // complex type, both the real and imaginary parts follow the conversion 463 // rules for the corresponding real types. 464 if (Val.first) 465 Val.first = CGF.EmitScalarConversion(Val.first, SrcType, DestType, Loc); 466 if (Val.second) 467 Val.second = CGF.EmitScalarConversion(Val.second, SrcType, DestType, Loc); 468 return Val; 469 } 470 471 ComplexPairTy ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val, 472 QualType SrcType, 473 QualType DestType, 474 SourceLocation Loc) { 475 // Convert the input element to the element type of the complex. 476 DestType = DestType->castAs<ComplexType>()->getElementType(); 477 Val = CGF.EmitScalarConversion(Val, SrcType, DestType, Loc); 478 479 // Return (realval, 0). 480 return ComplexPairTy(Val, llvm::Constant::getNullValue(Val->getType())); 481 } 482 483 ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op, 484 QualType DestTy) { 485 switch (CK) { 486 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!"); 487 488 // Atomic to non-atomic casts may be more than a no-op for some platforms and 489 // for some types. 490 case CK_AtomicToNonAtomic: 491 case CK_NonAtomicToAtomic: 492 case CK_NoOp: 493 case CK_LValueToRValue: 494 case CK_UserDefinedConversion: 495 return Visit(Op); 496 497 case CK_LValueBitCast: { 498 LValue origLV = CGF.EmitLValue(Op); 499 Address V = origLV.getAddress(CGF).withElementType(CGF.ConvertType(DestTy)); 500 return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), Op->getExprLoc()); 501 } 502 503 case CK_LValueToRValueBitCast: { 504 LValue SourceLVal = CGF.EmitLValue(Op); 505 Address Addr = SourceLVal.getAddress(CGF).withElementType( 506 CGF.ConvertTypeForMem(DestTy)); 507 LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy); 508 DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo()); 509 return EmitLoadOfLValue(DestLV, Op->getExprLoc()); 510 } 511 512 case CK_BitCast: 513 case CK_BaseToDerived: 514 case CK_DerivedToBase: 515 case CK_UncheckedDerivedToBase: 516 case CK_Dynamic: 517 case CK_ToUnion: 518 case CK_ArrayToPointerDecay: 519 case CK_FunctionToPointerDecay: 520 case CK_NullToPointer: 521 case CK_NullToMemberPointer: 522 case CK_BaseToDerivedMemberPointer: 523 case CK_DerivedToBaseMemberPointer: 524 case CK_MemberPointerToBoolean: 525 case CK_ReinterpretMemberPointer: 526 case CK_ConstructorConversion: 527 case CK_IntegralToPointer: 528 case CK_PointerToIntegral: 529 case CK_PointerToBoolean: 530 case CK_ToVoid: 531 case CK_VectorSplat: 532 case CK_IntegralCast: 533 case CK_BooleanToSignedIntegral: 534 case CK_IntegralToBoolean: 535 case CK_IntegralToFloating: 536 case CK_FloatingToIntegral: 537 case CK_FloatingToBoolean: 538 case CK_FloatingCast: 539 case CK_CPointerToObjCPointerCast: 540 case CK_BlockPointerToObjCPointerCast: 541 case CK_AnyPointerToBlockPointerCast: 542 case CK_ObjCObjectLValueCast: 543 case CK_FloatingComplexToReal: 544 case CK_FloatingComplexToBoolean: 545 case CK_IntegralComplexToReal: 546 case CK_IntegralComplexToBoolean: 547 case CK_ARCProduceObject: 548 case CK_ARCConsumeObject: 549 case CK_ARCReclaimReturnedObject: 550 case CK_ARCExtendBlockObject: 551 case CK_CopyAndAutoreleaseBlockObject: 552 case CK_BuiltinFnToFnPtr: 553 case CK_ZeroToOCLOpaqueType: 554 case CK_AddressSpaceConversion: 555 case CK_IntToOCLSampler: 556 case CK_FloatingToFixedPoint: 557 case CK_FixedPointToFloating: 558 case CK_FixedPointCast: 559 case CK_FixedPointToBoolean: 560 case CK_FixedPointToIntegral: 561 case CK_IntegralToFixedPoint: 562 case CK_MatrixCast: 563 llvm_unreachable("invalid cast kind for complex value"); 564 565 case CK_FloatingRealToComplex: 566 case CK_IntegralRealToComplex: { 567 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op); 568 return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op), Op->getType(), 569 DestTy, Op->getExprLoc()); 570 } 571 572 case CK_FloatingComplexCast: 573 case CK_FloatingComplexToIntegralComplex: 574 case CK_IntegralComplexCast: 575 case CK_IntegralComplexToFloatingComplex: { 576 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op); 577 return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy, 578 Op->getExprLoc()); 579 } 580 } 581 582 llvm_unreachable("unknown cast resulting in complex value"); 583 } 584 585 ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E, 586 QualType PromotionType) { 587 QualType promotionTy = PromotionType.isNull() 588 ? getPromotionType(E->getSubExpr()->getType()) 589 : PromotionType; 590 ComplexPairTy result = VisitPlus(E, promotionTy); 591 if (!promotionTy.isNull()) 592 return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType()); 593 return result; 594 } 595 596 ComplexPairTy ComplexExprEmitter::VisitPlus(const UnaryOperator *E, 597 QualType PromotionType) { 598 TestAndClearIgnoreReal(); 599 TestAndClearIgnoreImag(); 600 if (!PromotionType.isNull()) 601 return CGF.EmitPromotedComplexExpr(E->getSubExpr(), PromotionType); 602 return Visit(E->getSubExpr()); 603 } 604 605 ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E, 606 QualType PromotionType) { 607 QualType promotionTy = PromotionType.isNull() 608 ? getPromotionType(E->getSubExpr()->getType()) 609 : PromotionType; 610 ComplexPairTy result = VisitMinus(E, promotionTy); 611 if (!promotionTy.isNull()) 612 return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType()); 613 return result; 614 } 615 ComplexPairTy ComplexExprEmitter::VisitMinus(const UnaryOperator *E, 616 QualType PromotionType) { 617 TestAndClearIgnoreReal(); 618 TestAndClearIgnoreImag(); 619 ComplexPairTy Op; 620 if (!PromotionType.isNull()) 621 Op = CGF.EmitPromotedComplexExpr(E->getSubExpr(), PromotionType); 622 else 623 Op = Visit(E->getSubExpr()); 624 625 llvm::Value *ResR, *ResI; 626 if (Op.first->getType()->isFloatingPointTy()) { 627 ResR = Builder.CreateFNeg(Op.first, "neg.r"); 628 ResI = Builder.CreateFNeg(Op.second, "neg.i"); 629 } else { 630 ResR = Builder.CreateNeg(Op.first, "neg.r"); 631 ResI = Builder.CreateNeg(Op.second, "neg.i"); 632 } 633 return ComplexPairTy(ResR, ResI); 634 } 635 636 ComplexPairTy ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *E) { 637 TestAndClearIgnoreReal(); 638 TestAndClearIgnoreImag(); 639 // ~(a+ib) = a + i*-b 640 ComplexPairTy Op = Visit(E->getSubExpr()); 641 llvm::Value *ResI; 642 if (Op.second->getType()->isFloatingPointTy()) 643 ResI = Builder.CreateFNeg(Op.second, "conj.i"); 644 else 645 ResI = Builder.CreateNeg(Op.second, "conj.i"); 646 647 return ComplexPairTy(Op.first, ResI); 648 } 649 650 ComplexPairTy ComplexExprEmitter::EmitBinAdd(const BinOpInfo &Op) { 651 llvm::Value *ResR, *ResI; 652 653 if (Op.LHS.first->getType()->isFloatingPointTy()) { 654 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures); 655 ResR = Builder.CreateFAdd(Op.LHS.first, Op.RHS.first, "add.r"); 656 if (Op.LHS.second && Op.RHS.second) 657 ResI = Builder.CreateFAdd(Op.LHS.second, Op.RHS.second, "add.i"); 658 else 659 ResI = Op.LHS.second ? Op.LHS.second : Op.RHS.second; 660 assert(ResI && "Only one operand may be real!"); 661 } else { 662 ResR = Builder.CreateAdd(Op.LHS.first, Op.RHS.first, "add.r"); 663 assert(Op.LHS.second && Op.RHS.second && 664 "Both operands of integer complex operators must be complex!"); 665 ResI = Builder.CreateAdd(Op.LHS.second, Op.RHS.second, "add.i"); 666 } 667 return ComplexPairTy(ResR, ResI); 668 } 669 670 ComplexPairTy ComplexExprEmitter::EmitBinSub(const BinOpInfo &Op) { 671 llvm::Value *ResR, *ResI; 672 if (Op.LHS.first->getType()->isFloatingPointTy()) { 673 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures); 674 ResR = Builder.CreateFSub(Op.LHS.first, Op.RHS.first, "sub.r"); 675 if (Op.LHS.second && Op.RHS.second) 676 ResI = Builder.CreateFSub(Op.LHS.second, Op.RHS.second, "sub.i"); 677 else 678 ResI = Op.LHS.second ? Op.LHS.second 679 : Builder.CreateFNeg(Op.RHS.second, "sub.i"); 680 assert(ResI && "Only one operand may be real!"); 681 } else { 682 ResR = Builder.CreateSub(Op.LHS.first, Op.RHS.first, "sub.r"); 683 assert(Op.LHS.second && Op.RHS.second && 684 "Both operands of integer complex operators must be complex!"); 685 ResI = Builder.CreateSub(Op.LHS.second, Op.RHS.second, "sub.i"); 686 } 687 return ComplexPairTy(ResR, ResI); 688 } 689 690 /// Emit a libcall for a binary operation on complex types. 691 ComplexPairTy ComplexExprEmitter::EmitComplexBinOpLibCall(StringRef LibCallName, 692 const BinOpInfo &Op) { 693 CallArgList Args; 694 Args.add(RValue::get(Op.LHS.first), 695 Op.Ty->castAs<ComplexType>()->getElementType()); 696 Args.add(RValue::get(Op.LHS.second), 697 Op.Ty->castAs<ComplexType>()->getElementType()); 698 Args.add(RValue::get(Op.RHS.first), 699 Op.Ty->castAs<ComplexType>()->getElementType()); 700 Args.add(RValue::get(Op.RHS.second), 701 Op.Ty->castAs<ComplexType>()->getElementType()); 702 703 // We *must* use the full CG function call building logic here because the 704 // complex type has special ABI handling. We also should not forget about 705 // special calling convention which may be used for compiler builtins. 706 707 // We create a function qualified type to state that this call does not have 708 // any exceptions. 709 FunctionProtoType::ExtProtoInfo EPI; 710 EPI = EPI.withExceptionSpec( 711 FunctionProtoType::ExceptionSpecInfo(EST_BasicNoexcept)); 712 SmallVector<QualType, 4> ArgsQTys( 713 4, Op.Ty->castAs<ComplexType>()->getElementType()); 714 QualType FQTy = CGF.getContext().getFunctionType(Op.Ty, ArgsQTys, EPI); 715 const CGFunctionInfo &FuncInfo = CGF.CGM.getTypes().arrangeFreeFunctionCall( 716 Args, cast<FunctionType>(FQTy.getTypePtr()), false); 717 718 llvm::FunctionType *FTy = CGF.CGM.getTypes().GetFunctionType(FuncInfo); 719 llvm::FunctionCallee Func = CGF.CGM.CreateRuntimeFunction( 720 FTy, LibCallName, llvm::AttributeList(), true); 721 CGCallee Callee = CGCallee::forDirect(Func, FQTy->getAs<FunctionProtoType>()); 722 723 llvm::CallBase *Call; 724 RValue Res = CGF.EmitCall(FuncInfo, Callee, ReturnValueSlot(), Args, &Call); 725 Call->setCallingConv(CGF.CGM.getRuntimeCC()); 726 return Res.getComplexVal(); 727 } 728 729 /// Lookup the libcall name for a given floating point type complex 730 /// multiply. 731 static StringRef getComplexMultiplyLibCallName(llvm::Type *Ty) { 732 switch (Ty->getTypeID()) { 733 default: 734 llvm_unreachable("Unsupported floating point type!"); 735 case llvm::Type::HalfTyID: 736 return "__mulhc3"; 737 case llvm::Type::FloatTyID: 738 return "__mulsc3"; 739 case llvm::Type::DoubleTyID: 740 return "__muldc3"; 741 case llvm::Type::PPC_FP128TyID: 742 return "__multc3"; 743 case llvm::Type::X86_FP80TyID: 744 return "__mulxc3"; 745 case llvm::Type::FP128TyID: 746 return "__multc3"; 747 } 748 } 749 750 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex 751 // typed values. 752 ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) { 753 using llvm::Value; 754 Value *ResR, *ResI; 755 llvm::MDBuilder MDHelper(CGF.getLLVMContext()); 756 757 if (Op.LHS.first->getType()->isFloatingPointTy()) { 758 // The general formulation is: 759 // (a + ib) * (c + id) = (a * c - b * d) + i(a * d + b * c) 760 // 761 // But we can fold away components which would be zero due to a real 762 // operand according to C11 Annex G.5.1p2. 763 // FIXME: C11 also provides for imaginary types which would allow folding 764 // still more of this within the type system. 765 766 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures); 767 if (Op.LHS.second && Op.RHS.second) { 768 // If both operands are complex, emit the core math directly, and then 769 // test for NaNs. If we find NaNs in the result, we delegate to a libcall 770 // to carefully re-compute the correct infinity representation if 771 // possible. The expectation is that the presence of NaNs here is 772 // *extremely* rare, and so the cost of the libcall is almost irrelevant. 773 // This is good, because the libcall re-computes the core multiplication 774 // exactly the same as we do here and re-tests for NaNs in order to be 775 // a generic complex*complex libcall. 776 777 // First compute the four products. 778 Value *AC = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul_ac"); 779 Value *BD = Builder.CreateFMul(Op.LHS.second, Op.RHS.second, "mul_bd"); 780 Value *AD = Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul_ad"); 781 Value *BC = Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul_bc"); 782 783 // The real part is the difference of the first two, the imaginary part is 784 // the sum of the second. 785 ResR = Builder.CreateFSub(AC, BD, "mul_r"); 786 ResI = Builder.CreateFAdd(AD, BC, "mul_i"); 787 788 if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Limited || 789 Op.FPFeatures.getComplexRange() == LangOptions::CX_Fortran) 790 return ComplexPairTy(ResR, ResI); 791 792 // Emit the test for the real part becoming NaN and create a branch to 793 // handle it. We test for NaN by comparing the number to itself. 794 Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp"); 795 llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_mul_cont"); 796 llvm::BasicBlock *INaNBB = CGF.createBasicBlock("complex_mul_imag_nan"); 797 llvm::Instruction *Branch = Builder.CreateCondBr(IsRNaN, INaNBB, ContBB); 798 llvm::BasicBlock *OrigBB = Branch->getParent(); 799 800 // Give hint that we very much don't expect to see NaNs. 801 // Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp 802 llvm::MDNode *BrWeight = MDHelper.createBranchWeights(1, (1U << 20) - 1); 803 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight); 804 805 // Now test the imaginary part and create its branch. 806 CGF.EmitBlock(INaNBB); 807 Value *IsINaN = Builder.CreateFCmpUNO(ResI, ResI, "isnan_cmp"); 808 llvm::BasicBlock *LibCallBB = CGF.createBasicBlock("complex_mul_libcall"); 809 Branch = Builder.CreateCondBr(IsINaN, LibCallBB, ContBB); 810 Branch->setMetadata(llvm::LLVMContext::MD_prof, BrWeight); 811 812 // Now emit the libcall on this slowest of the slow paths. 813 CGF.EmitBlock(LibCallBB); 814 Value *LibCallR, *LibCallI; 815 std::tie(LibCallR, LibCallI) = EmitComplexBinOpLibCall( 816 getComplexMultiplyLibCallName(Op.LHS.first->getType()), Op); 817 Builder.CreateBr(ContBB); 818 819 // Finally continue execution by phi-ing together the different 820 // computation paths. 821 CGF.EmitBlock(ContBB); 822 llvm::PHINode *RealPHI = Builder.CreatePHI(ResR->getType(), 3, "real_mul_phi"); 823 RealPHI->addIncoming(ResR, OrigBB); 824 RealPHI->addIncoming(ResR, INaNBB); 825 RealPHI->addIncoming(LibCallR, LibCallBB); 826 llvm::PHINode *ImagPHI = Builder.CreatePHI(ResI->getType(), 3, "imag_mul_phi"); 827 ImagPHI->addIncoming(ResI, OrigBB); 828 ImagPHI->addIncoming(ResI, INaNBB); 829 ImagPHI->addIncoming(LibCallI, LibCallBB); 830 return ComplexPairTy(RealPHI, ImagPHI); 831 } 832 assert((Op.LHS.second || Op.RHS.second) && 833 "At least one operand must be complex!"); 834 835 // If either of the operands is a real rather than a complex, the 836 // imaginary component is ignored when computing the real component of the 837 // result. 838 ResR = Builder.CreateFMul(Op.LHS.first, Op.RHS.first, "mul.rl"); 839 840 ResI = Op.LHS.second 841 ? Builder.CreateFMul(Op.LHS.second, Op.RHS.first, "mul.il") 842 : Builder.CreateFMul(Op.LHS.first, Op.RHS.second, "mul.ir"); 843 } else { 844 assert(Op.LHS.second && Op.RHS.second && 845 "Both operands of integer complex operators must be complex!"); 846 Value *ResRl = Builder.CreateMul(Op.LHS.first, Op.RHS.first, "mul.rl"); 847 Value *ResRr = Builder.CreateMul(Op.LHS.second, Op.RHS.second, "mul.rr"); 848 ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); 849 850 Value *ResIl = Builder.CreateMul(Op.LHS.second, Op.RHS.first, "mul.il"); 851 Value *ResIr = Builder.CreateMul(Op.LHS.first, Op.RHS.second, "mul.ir"); 852 ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); 853 } 854 return ComplexPairTy(ResR, ResI); 855 } 856 857 ComplexPairTy ComplexExprEmitter::EmitAlgebraicDiv(llvm::Value *LHSr, 858 llvm::Value *LHSi, 859 llvm::Value *RHSr, 860 llvm::Value *RHSi) { 861 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) 862 llvm::Value *DSTr, *DSTi; 863 864 llvm::Value *AC = Builder.CreateFMul(LHSr, RHSr); // a*c 865 llvm::Value *BD = Builder.CreateFMul(LHSi, RHSi); // b*d 866 llvm::Value *ACpBD = Builder.CreateFAdd(AC, BD); // ac+bd 867 868 llvm::Value *CC = Builder.CreateFMul(RHSr, RHSr); // c*c 869 llvm::Value *DD = Builder.CreateFMul(RHSi, RHSi); // d*d 870 llvm::Value *CCpDD = Builder.CreateFAdd(CC, DD); // cc+dd 871 872 llvm::Value *BC = Builder.CreateFMul(LHSi, RHSr); // b*c 873 llvm::Value *AD = Builder.CreateFMul(LHSr, RHSi); // a*d 874 llvm::Value *BCmAD = Builder.CreateFSub(BC, AD); // bc-ad 875 876 DSTr = Builder.CreateFDiv(ACpBD, CCpDD); 877 DSTi = Builder.CreateFDiv(BCmAD, CCpDD); 878 return ComplexPairTy(DSTr, DSTi); 879 } 880 881 // EmitFAbs - Emit a call to @llvm.fabs. 882 static llvm::Value *EmitllvmFAbs(CodeGenFunction &CGF, llvm::Value *Value) { 883 llvm::Function *Func = 884 CGF.CGM.getIntrinsic(llvm::Intrinsic::fabs, Value->getType()); 885 llvm::Value *Call = CGF.Builder.CreateCall(Func, Value); 886 return Call; 887 } 888 889 // EmitRangeReductionDiv - Implements Smith's algorithm for complex division. 890 // SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8 (1962). 891 ComplexPairTy ComplexExprEmitter::EmitRangeReductionDiv(llvm::Value *LHSr, 892 llvm::Value *LHSi, 893 llvm::Value *RHSr, 894 llvm::Value *RHSi) { 895 // FIXME: This could eventually be replaced by an LLVM intrinsic to 896 // avoid this long IR sequence. 897 898 // (a + ib) / (c + id) = (e + if) 899 llvm::Value *FAbsRHSr = EmitllvmFAbs(CGF, RHSr); // |c| 900 llvm::Value *FAbsRHSi = EmitllvmFAbs(CGF, RHSi); // |d| 901 // |c| >= |d| 902 llvm::Value *IsR = Builder.CreateFCmpUGT(FAbsRHSr, FAbsRHSi, "abs_cmp"); 903 904 llvm::BasicBlock *TrueBB = 905 CGF.createBasicBlock("abs_rhsr_greater_or_equal_abs_rhsi"); 906 llvm::BasicBlock *FalseBB = 907 CGF.createBasicBlock("abs_rhsr_less_than_abs_rhsi"); 908 llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_div"); 909 Builder.CreateCondBr(IsR, TrueBB, FalseBB); 910 911 CGF.EmitBlock(TrueBB); 912 // abs(c) >= abs(d) 913 // r = d/c 914 // tmp = c + rd 915 // e = (a + br)/tmp 916 // f = (b - ar)/tmp 917 llvm::Value *DdC = Builder.CreateFDiv(RHSi, RHSr); // r=d/c 918 919 llvm::Value *RD = Builder.CreateFMul(DdC, RHSi); // rd 920 llvm::Value *CpRD = Builder.CreateFAdd(RHSr, RD); // tmp=c+rd 921 922 llvm::Value *T3 = Builder.CreateFMul(LHSi, DdC); // br 923 llvm::Value *T4 = Builder.CreateFAdd(LHSr, T3); // a+br 924 llvm::Value *DSTTr = Builder.CreateFDiv(T4, CpRD); // (a+br)/tmp 925 926 llvm::Value *T5 = Builder.CreateFMul(LHSr, DdC); // ar 927 llvm::Value *T6 = Builder.CreateFSub(LHSi, T5); // b-ar 928 llvm::Value *DSTTi = Builder.CreateFDiv(T6, CpRD); // (b-ar)/tmp 929 Builder.CreateBr(ContBB); 930 931 CGF.EmitBlock(FalseBB); 932 // abs(c) < abs(d) 933 // r = c/d 934 // tmp = d + rc 935 // e = (ar + b)/tmp 936 // f = (br - a)/tmp 937 llvm::Value *CdD = Builder.CreateFDiv(RHSr, RHSi); // r=c/d 938 939 llvm::Value *RC = Builder.CreateFMul(CdD, RHSr); // rc 940 llvm::Value *DpRC = Builder.CreateFAdd(RHSi, RC); // tmp=d+rc 941 942 llvm::Value *T7 = Builder.CreateFMul(LHSr, CdD); // ar 943 llvm::Value *T8 = Builder.CreateFAdd(T7, LHSi); // ar+b 944 llvm::Value *DSTFr = Builder.CreateFDiv(T8, DpRC); // (ar+b)/tmp 945 946 llvm::Value *T9 = Builder.CreateFMul(LHSi, CdD); // br 947 llvm::Value *T10 = Builder.CreateFSub(T9, LHSr); // br-a 948 llvm::Value *DSTFi = Builder.CreateFDiv(T10, DpRC); // (br-a)/tmp 949 Builder.CreateBr(ContBB); 950 951 // Phi together the computation paths. 952 CGF.EmitBlock(ContBB); 953 llvm::PHINode *VALr = Builder.CreatePHI(DSTTr->getType(), 2); 954 VALr->addIncoming(DSTTr, TrueBB); 955 VALr->addIncoming(DSTFr, FalseBB); 956 llvm::PHINode *VALi = Builder.CreatePHI(DSTTi->getType(), 2); 957 VALi->addIncoming(DSTTi, TrueBB); 958 VALi->addIncoming(DSTFi, FalseBB); 959 return ComplexPairTy(VALr, VALi); 960 } 961 962 // See C11 Annex G.5.1 for the semantics of multiplicative operators on complex 963 // typed values. 964 ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) { 965 llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second; 966 llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second; 967 llvm::Value *DSTr, *DSTi; 968 if (LHSr->getType()->isFloatingPointTy()) { 969 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures); 970 if (!RHSi) { 971 assert(LHSi && "Can have at most one non-complex operand!"); 972 973 DSTr = Builder.CreateFDiv(LHSr, RHSr); 974 DSTi = Builder.CreateFDiv(LHSi, RHSr); 975 return ComplexPairTy(DSTr, DSTi); 976 } 977 llvm::Value *OrigLHSi = LHSi; 978 if (!LHSi) 979 LHSi = llvm::Constant::getNullValue(RHSi->getType()); 980 if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Fortran) 981 return EmitRangeReductionDiv(LHSr, LHSi, RHSr, RHSi); 982 else if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Limited) 983 return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi); 984 else if (!CGF.getLangOpts().FastMath || 985 // '-ffast-math' is used in the command line but followed by an 986 // '-fno-cx-limited-range'. 987 Op.FPFeatures.getComplexRange() == LangOptions::CX_Full) { 988 LHSi = OrigLHSi; 989 // If we have a complex operand on the RHS and FastMath is not allowed, we 990 // delegate to a libcall to handle all of the complexities and minimize 991 // underflow/overflow cases. When FastMath is allowed we construct the 992 // divide inline using the same algorithm as for integer operands. 993 // 994 // FIXME: We would be able to avoid the libcall in many places if we 995 // supported imaginary types in addition to complex types. 996 BinOpInfo LibCallOp = Op; 997 // If LHS was a real, supply a null imaginary part. 998 if (!LHSi) 999 LibCallOp.LHS.second = llvm::Constant::getNullValue(LHSr->getType()); 1000 1001 switch (LHSr->getType()->getTypeID()) { 1002 default: 1003 llvm_unreachable("Unsupported floating point type!"); 1004 case llvm::Type::HalfTyID: 1005 return EmitComplexBinOpLibCall("__divhc3", LibCallOp); 1006 case llvm::Type::FloatTyID: 1007 return EmitComplexBinOpLibCall("__divsc3", LibCallOp); 1008 case llvm::Type::DoubleTyID: 1009 return EmitComplexBinOpLibCall("__divdc3", LibCallOp); 1010 case llvm::Type::PPC_FP128TyID: 1011 return EmitComplexBinOpLibCall("__divtc3", LibCallOp); 1012 case llvm::Type::X86_FP80TyID: 1013 return EmitComplexBinOpLibCall("__divxc3", LibCallOp); 1014 case llvm::Type::FP128TyID: 1015 return EmitComplexBinOpLibCall("__divtc3", LibCallOp); 1016 } 1017 } else { 1018 return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi); 1019 } 1020 } else { 1021 assert(Op.LHS.second && Op.RHS.second && 1022 "Both operands of integer complex operators must be complex!"); 1023 // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) 1024 llvm::Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c 1025 llvm::Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d 1026 llvm::Value *Tmp3 = Builder.CreateAdd(Tmp1, Tmp2); // ac+bd 1027 1028 llvm::Value *Tmp4 = Builder.CreateMul(RHSr, RHSr); // c*c 1029 llvm::Value *Tmp5 = Builder.CreateMul(RHSi, RHSi); // d*d 1030 llvm::Value *Tmp6 = Builder.CreateAdd(Tmp4, Tmp5); // cc+dd 1031 1032 llvm::Value *Tmp7 = Builder.CreateMul(LHSi, RHSr); // b*c 1033 llvm::Value *Tmp8 = Builder.CreateMul(LHSr, RHSi); // a*d 1034 llvm::Value *Tmp9 = Builder.CreateSub(Tmp7, Tmp8); // bc-ad 1035 1036 if (Op.Ty->castAs<ComplexType>()->getElementType()->isUnsignedIntegerType()) { 1037 DSTr = Builder.CreateUDiv(Tmp3, Tmp6); 1038 DSTi = Builder.CreateUDiv(Tmp9, Tmp6); 1039 } else { 1040 DSTr = Builder.CreateSDiv(Tmp3, Tmp6); 1041 DSTi = Builder.CreateSDiv(Tmp9, Tmp6); 1042 } 1043 } 1044 1045 return ComplexPairTy(DSTr, DSTi); 1046 } 1047 1048 ComplexPairTy CodeGenFunction::EmitUnPromotedValue(ComplexPairTy result, 1049 QualType UnPromotionType) { 1050 llvm::Type *ComplexElementTy = 1051 ConvertType(UnPromotionType->castAs<ComplexType>()->getElementType()); 1052 if (result.first) 1053 result.first = 1054 Builder.CreateFPTrunc(result.first, ComplexElementTy, "unpromotion"); 1055 if (result.second) 1056 result.second = 1057 Builder.CreateFPTrunc(result.second, ComplexElementTy, "unpromotion"); 1058 return result; 1059 } 1060 1061 ComplexPairTy CodeGenFunction::EmitPromotedValue(ComplexPairTy result, 1062 QualType PromotionType) { 1063 llvm::Type *ComplexElementTy = 1064 ConvertType(PromotionType->castAs<ComplexType>()->getElementType()); 1065 if (result.first) 1066 result.first = Builder.CreateFPExt(result.first, ComplexElementTy, "ext"); 1067 if (result.second) 1068 result.second = Builder.CreateFPExt(result.second, ComplexElementTy, "ext"); 1069 1070 return result; 1071 } 1072 1073 ComplexPairTy ComplexExprEmitter::EmitPromoted(const Expr *E, 1074 QualType PromotionType) { 1075 E = E->IgnoreParens(); 1076 if (auto BO = dyn_cast<BinaryOperator>(E)) { 1077 switch (BO->getOpcode()) { 1078 #define HANDLE_BINOP(OP) \ 1079 case BO_##OP: \ 1080 return EmitBin##OP(EmitBinOps(BO, PromotionType)); 1081 HANDLE_BINOP(Add) 1082 HANDLE_BINOP(Sub) 1083 HANDLE_BINOP(Mul) 1084 HANDLE_BINOP(Div) 1085 #undef HANDLE_BINOP 1086 default: 1087 break; 1088 } 1089 } else if (auto UO = dyn_cast<UnaryOperator>(E)) { 1090 switch (UO->getOpcode()) { 1091 case UO_Minus: 1092 return VisitMinus(UO, PromotionType); 1093 case UO_Plus: 1094 return VisitPlus(UO, PromotionType); 1095 default: 1096 break; 1097 } 1098 } 1099 auto result = Visit(const_cast<Expr *>(E)); 1100 if (!PromotionType.isNull()) 1101 return CGF.EmitPromotedValue(result, PromotionType); 1102 else 1103 return result; 1104 } 1105 1106 ComplexPairTy CodeGenFunction::EmitPromotedComplexExpr(const Expr *E, 1107 QualType DstTy) { 1108 return ComplexExprEmitter(*this).EmitPromoted(E, DstTy); 1109 } 1110 1111 ComplexPairTy 1112 ComplexExprEmitter::EmitPromotedComplexOperand(const Expr *E, 1113 QualType OverallPromotionType) { 1114 if (E->getType()->isAnyComplexType()) { 1115 if (!OverallPromotionType.isNull()) 1116 return CGF.EmitPromotedComplexExpr(E, OverallPromotionType); 1117 else 1118 return Visit(const_cast<Expr *>(E)); 1119 } else { 1120 if (!OverallPromotionType.isNull()) { 1121 QualType ComplexElementTy = 1122 OverallPromotionType->castAs<ComplexType>()->getElementType(); 1123 return ComplexPairTy(CGF.EmitPromotedScalarExpr(E, ComplexElementTy), 1124 nullptr); 1125 } else { 1126 return ComplexPairTy(CGF.EmitScalarExpr(E), nullptr); 1127 } 1128 } 1129 } 1130 1131 ComplexExprEmitter::BinOpInfo 1132 ComplexExprEmitter::EmitBinOps(const BinaryOperator *E, 1133 QualType PromotionType) { 1134 TestAndClearIgnoreReal(); 1135 TestAndClearIgnoreImag(); 1136 BinOpInfo Ops; 1137 1138 Ops.LHS = EmitPromotedComplexOperand(E->getLHS(), PromotionType); 1139 Ops.RHS = EmitPromotedComplexOperand(E->getRHS(), PromotionType); 1140 if (!PromotionType.isNull()) 1141 Ops.Ty = PromotionType; 1142 else 1143 Ops.Ty = E->getType(); 1144 Ops.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); 1145 return Ops; 1146 } 1147 1148 1149 LValue ComplexExprEmitter:: 1150 EmitCompoundAssignLValue(const CompoundAssignOperator *E, 1151 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&), 1152 RValue &Val) { 1153 TestAndClearIgnoreReal(); 1154 TestAndClearIgnoreImag(); 1155 QualType LHSTy = E->getLHS()->getType(); 1156 if (const AtomicType *AT = LHSTy->getAs<AtomicType>()) 1157 LHSTy = AT->getValueType(); 1158 1159 BinOpInfo OpInfo; 1160 OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); 1161 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, OpInfo.FPFeatures); 1162 1163 // Load the RHS and LHS operands. 1164 // __block variables need to have the rhs evaluated first, plus this should 1165 // improve codegen a little. 1166 QualType PromotionTypeCR; 1167 PromotionTypeCR = getPromotionType(E->getComputationResultType()); 1168 if (PromotionTypeCR.isNull()) 1169 PromotionTypeCR = E->getComputationResultType(); 1170 OpInfo.Ty = PromotionTypeCR; 1171 QualType ComplexElementTy = 1172 OpInfo.Ty->castAs<ComplexType>()->getElementType(); 1173 QualType PromotionTypeRHS = getPromotionType(E->getRHS()->getType()); 1174 1175 // The RHS should have been converted to the computation type. 1176 if (E->getRHS()->getType()->isRealFloatingType()) { 1177 if (!PromotionTypeRHS.isNull()) 1178 OpInfo.RHS = ComplexPairTy( 1179 CGF.EmitPromotedScalarExpr(E->getRHS(), PromotionTypeRHS), nullptr); 1180 else { 1181 assert(CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, 1182 E->getRHS()->getType())); 1183 1184 OpInfo.RHS = ComplexPairTy(CGF.EmitScalarExpr(E->getRHS()), nullptr); 1185 } 1186 } else { 1187 if (!PromotionTypeRHS.isNull()) { 1188 OpInfo.RHS = ComplexPairTy( 1189 CGF.EmitPromotedComplexExpr(E->getRHS(), PromotionTypeRHS)); 1190 } else { 1191 assert(CGF.getContext().hasSameUnqualifiedType(OpInfo.Ty, 1192 E->getRHS()->getType())); 1193 OpInfo.RHS = Visit(E->getRHS()); 1194 } 1195 } 1196 1197 LValue LHS = CGF.EmitLValue(E->getLHS()); 1198 1199 // Load from the l-value and convert it. 1200 SourceLocation Loc = E->getExprLoc(); 1201 QualType PromotionTypeLHS = getPromotionType(E->getComputationLHSType()); 1202 if (LHSTy->isAnyComplexType()) { 1203 ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc); 1204 if (!PromotionTypeLHS.isNull()) 1205 OpInfo.LHS = 1206 EmitComplexToComplexCast(LHSVal, LHSTy, PromotionTypeLHS, Loc); 1207 else 1208 OpInfo.LHS = EmitComplexToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc); 1209 } else { 1210 llvm::Value *LHSVal = CGF.EmitLoadOfScalar(LHS, Loc); 1211 // For floating point real operands we can directly pass the scalar form 1212 // to the binary operator emission and potentially get more efficient code. 1213 if (LHSTy->isRealFloatingType()) { 1214 QualType PromotedComplexElementTy; 1215 if (!PromotionTypeLHS.isNull()) { 1216 PromotedComplexElementTy = 1217 cast<ComplexType>(PromotionTypeLHS)->getElementType(); 1218 if (!CGF.getContext().hasSameUnqualifiedType(PromotedComplexElementTy, 1219 PromotionTypeLHS)) 1220 LHSVal = CGF.EmitScalarConversion(LHSVal, LHSTy, 1221 PromotedComplexElementTy, Loc); 1222 } else { 1223 if (!CGF.getContext().hasSameUnqualifiedType(ComplexElementTy, LHSTy)) 1224 LHSVal = 1225 CGF.EmitScalarConversion(LHSVal, LHSTy, ComplexElementTy, Loc); 1226 } 1227 OpInfo.LHS = ComplexPairTy(LHSVal, nullptr); 1228 } else { 1229 OpInfo.LHS = EmitScalarToComplexCast(LHSVal, LHSTy, OpInfo.Ty, Loc); 1230 } 1231 } 1232 1233 // Expand the binary operator. 1234 ComplexPairTy Result = (this->*Func)(OpInfo); 1235 1236 // Truncate the result and store it into the LHS lvalue. 1237 if (LHSTy->isAnyComplexType()) { 1238 ComplexPairTy ResVal = 1239 EmitComplexToComplexCast(Result, OpInfo.Ty, LHSTy, Loc); 1240 EmitStoreOfComplex(ResVal, LHS, /*isInit*/ false); 1241 Val = RValue::getComplex(ResVal); 1242 } else { 1243 llvm::Value *ResVal = 1244 CGF.EmitComplexToScalarConversion(Result, OpInfo.Ty, LHSTy, Loc); 1245 CGF.EmitStoreOfScalar(ResVal, LHS, /*isInit*/ false); 1246 Val = RValue::get(ResVal); 1247 } 1248 1249 return LHS; 1250 } 1251 1252 // Compound assignments. 1253 ComplexPairTy ComplexExprEmitter:: 1254 EmitCompoundAssign(const CompoundAssignOperator *E, 1255 ComplexPairTy (ComplexExprEmitter::*Func)(const BinOpInfo&)){ 1256 RValue Val; 1257 LValue LV = EmitCompoundAssignLValue(E, Func, Val); 1258 1259 // The result of an assignment in C is the assigned r-value. 1260 if (!CGF.getLangOpts().CPlusPlus) 1261 return Val.getComplexVal(); 1262 1263 // If the lvalue is non-volatile, return the computed value of the assignment. 1264 if (!LV.isVolatileQualified()) 1265 return Val.getComplexVal(); 1266 1267 return EmitLoadOfLValue(LV, E->getExprLoc()); 1268 } 1269 1270 LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E, 1271 ComplexPairTy &Val) { 1272 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(), 1273 E->getRHS()->getType()) && 1274 "Invalid assignment"); 1275 TestAndClearIgnoreReal(); 1276 TestAndClearIgnoreImag(); 1277 1278 // Emit the RHS. __block variables need the RHS evaluated first. 1279 Val = Visit(E->getRHS()); 1280 1281 // Compute the address to store into. 1282 LValue LHS = CGF.EmitLValue(E->getLHS()); 1283 1284 // Store the result value into the LHS lvalue. 1285 EmitStoreOfComplex(Val, LHS, /*isInit*/ false); 1286 1287 return LHS; 1288 } 1289 1290 ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { 1291 ComplexPairTy Val; 1292 LValue LV = EmitBinAssignLValue(E, Val); 1293 1294 // The result of an assignment in C is the assigned r-value. 1295 if (!CGF.getLangOpts().CPlusPlus) 1296 return Val; 1297 1298 // If the lvalue is non-volatile, return the computed value of the assignment. 1299 if (!LV.isVolatileQualified()) 1300 return Val; 1301 1302 return EmitLoadOfLValue(LV, E->getExprLoc()); 1303 } 1304 1305 ComplexPairTy ComplexExprEmitter::VisitBinComma(const BinaryOperator *E) { 1306 CGF.EmitIgnoredExpr(E->getLHS()); 1307 return Visit(E->getRHS()); 1308 } 1309 1310 ComplexPairTy ComplexExprEmitter:: 1311 VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 1312 TestAndClearIgnoreReal(); 1313 TestAndClearIgnoreImag(); 1314 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 1315 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 1316 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 1317 1318 // Bind the common expression if necessary. 1319 CodeGenFunction::OpaqueValueMapping binding(CGF, E); 1320 1321 1322 CodeGenFunction::ConditionalEvaluation eval(CGF); 1323 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock, 1324 CGF.getProfileCount(E)); 1325 1326 eval.begin(CGF); 1327 CGF.EmitBlock(LHSBlock); 1328 CGF.incrementProfileCounter(E); 1329 ComplexPairTy LHS = Visit(E->getTrueExpr()); 1330 LHSBlock = Builder.GetInsertBlock(); 1331 CGF.EmitBranch(ContBlock); 1332 eval.end(CGF); 1333 1334 eval.begin(CGF); 1335 CGF.EmitBlock(RHSBlock); 1336 ComplexPairTy RHS = Visit(E->getFalseExpr()); 1337 RHSBlock = Builder.GetInsertBlock(); 1338 CGF.EmitBlock(ContBlock); 1339 eval.end(CGF); 1340 1341 // Create a PHI node for the real part. 1342 llvm::PHINode *RealPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.r"); 1343 RealPN->addIncoming(LHS.first, LHSBlock); 1344 RealPN->addIncoming(RHS.first, RHSBlock); 1345 1346 // Create a PHI node for the imaginary part. 1347 llvm::PHINode *ImagPN = Builder.CreatePHI(LHS.first->getType(), 2, "cond.i"); 1348 ImagPN->addIncoming(LHS.second, LHSBlock); 1349 ImagPN->addIncoming(RHS.second, RHSBlock); 1350 1351 return ComplexPairTy(RealPN, ImagPN); 1352 } 1353 1354 ComplexPairTy ComplexExprEmitter::VisitChooseExpr(ChooseExpr *E) { 1355 return Visit(E->getChosenSubExpr()); 1356 } 1357 1358 ComplexPairTy ComplexExprEmitter::VisitInitListExpr(InitListExpr *E) { 1359 bool Ignore = TestAndClearIgnoreReal(); 1360 (void)Ignore; 1361 assert (Ignore == false && "init list ignored"); 1362 Ignore = TestAndClearIgnoreImag(); 1363 (void)Ignore; 1364 assert (Ignore == false && "init list ignored"); 1365 1366 if (E->getNumInits() == 2) { 1367 llvm::Value *Real = CGF.EmitScalarExpr(E->getInit(0)); 1368 llvm::Value *Imag = CGF.EmitScalarExpr(E->getInit(1)); 1369 return ComplexPairTy(Real, Imag); 1370 } else if (E->getNumInits() == 1) { 1371 return Visit(E->getInit(0)); 1372 } 1373 1374 // Empty init list initializes to null 1375 assert(E->getNumInits() == 0 && "Unexpected number of inits"); 1376 QualType Ty = E->getType()->castAs<ComplexType>()->getElementType(); 1377 llvm::Type* LTy = CGF.ConvertType(Ty); 1378 llvm::Value* zeroConstant = llvm::Constant::getNullValue(LTy); 1379 return ComplexPairTy(zeroConstant, zeroConstant); 1380 } 1381 1382 ComplexPairTy ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *E) { 1383 Address ArgValue = Address::invalid(); 1384 Address ArgPtr = CGF.EmitVAArg(E, ArgValue); 1385 1386 if (!ArgPtr.isValid()) { 1387 CGF.ErrorUnsupported(E, "complex va_arg expression"); 1388 llvm::Type *EltTy = 1389 CGF.ConvertType(E->getType()->castAs<ComplexType>()->getElementType()); 1390 llvm::Value *U = llvm::UndefValue::get(EltTy); 1391 return ComplexPairTy(U, U); 1392 } 1393 1394 return EmitLoadOfLValue(CGF.MakeAddrLValue(ArgPtr, E->getType()), 1395 E->getExprLoc()); 1396 } 1397 1398 //===----------------------------------------------------------------------===// 1399 // Entry Point into this File 1400 //===----------------------------------------------------------------------===// 1401 1402 /// EmitComplexExpr - Emit the computation of the specified expression of 1403 /// complex type, ignoring the result. 1404 ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E, bool IgnoreReal, 1405 bool IgnoreImag) { 1406 assert(E && getComplexType(E->getType()) && 1407 "Invalid complex expression to emit"); 1408 1409 return ComplexExprEmitter(*this, IgnoreReal, IgnoreImag) 1410 .Visit(const_cast<Expr *>(E)); 1411 } 1412 1413 void CodeGenFunction::EmitComplexExprIntoLValue(const Expr *E, LValue dest, 1414 bool isInit) { 1415 assert(E && getComplexType(E->getType()) && 1416 "Invalid complex expression to emit"); 1417 ComplexExprEmitter Emitter(*this); 1418 ComplexPairTy Val = Emitter.Visit(const_cast<Expr*>(E)); 1419 Emitter.EmitStoreOfComplex(Val, dest, isInit); 1420 } 1421 1422 /// EmitStoreOfComplex - Store a complex number into the specified l-value. 1423 void CodeGenFunction::EmitStoreOfComplex(ComplexPairTy V, LValue dest, 1424 bool isInit) { 1425 ComplexExprEmitter(*this).EmitStoreOfComplex(V, dest, isInit); 1426 } 1427 1428 /// EmitLoadOfComplex - Load a complex number from the specified address. 1429 ComplexPairTy CodeGenFunction::EmitLoadOfComplex(LValue src, 1430 SourceLocation loc) { 1431 return ComplexExprEmitter(*this).EmitLoadOfLValue(src, loc); 1432 } 1433 1434 LValue CodeGenFunction::EmitComplexAssignmentLValue(const BinaryOperator *E) { 1435 assert(E->getOpcode() == BO_Assign); 1436 ComplexPairTy Val; // ignored 1437 LValue LVal = ComplexExprEmitter(*this).EmitBinAssignLValue(E, Val); 1438 if (getLangOpts().OpenMP) 1439 CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(*this, 1440 E->getLHS()); 1441 return LVal; 1442 } 1443 1444 typedef ComplexPairTy (ComplexExprEmitter::*CompoundFunc)( 1445 const ComplexExprEmitter::BinOpInfo &); 1446 1447 static CompoundFunc getComplexOp(BinaryOperatorKind Op) { 1448 switch (Op) { 1449 case BO_MulAssign: return &ComplexExprEmitter::EmitBinMul; 1450 case BO_DivAssign: return &ComplexExprEmitter::EmitBinDiv; 1451 case BO_SubAssign: return &ComplexExprEmitter::EmitBinSub; 1452 case BO_AddAssign: return &ComplexExprEmitter::EmitBinAdd; 1453 default: 1454 llvm_unreachable("unexpected complex compound assignment"); 1455 } 1456 } 1457 1458 LValue CodeGenFunction:: 1459 EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E) { 1460 CompoundFunc Op = getComplexOp(E->getOpcode()); 1461 RValue Val; 1462 return ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val); 1463 } 1464 1465 LValue CodeGenFunction:: 1466 EmitScalarCompoundAssignWithComplex(const CompoundAssignOperator *E, 1467 llvm::Value *&Result) { 1468 CompoundFunc Op = getComplexOp(E->getOpcode()); 1469 RValue Val; 1470 LValue Ret = ComplexExprEmitter(*this).EmitCompoundAssignLValue(E, Op, Val); 1471 Result = Val.getScalarVal(); 1472 return Ret; 1473 } 1474