Coverage Report

Created: 2020-02-25 14:32

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