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