Coverage Report

Created: 2020-09-19 12:23

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