Coverage Report

Created: 2019-07-24 05:18

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