Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/CodeGen/CGStmt.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
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 Stmt nodes as LLVM code.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "CodeGenFunction.h"
14
#include "CGDebugInfo.h"
15
#include "CodeGenModule.h"
16
#include "TargetInfo.h"
17
#include "clang/AST/StmtVisitor.h"
18
#include "clang/Basic/Builtins.h"
19
#include "clang/Basic/PrettyStackTrace.h"
20
#include "clang/Basic/TargetInfo.h"
21
#include "llvm/ADT/StringExtras.h"
22
#include "llvm/IR/DataLayout.h"
23
#include "llvm/IR/InlineAsm.h"
24
#include "llvm/IR/Intrinsics.h"
25
#include "llvm/IR/MDBuilder.h"
26
27
using namespace clang;
28
using namespace CodeGen;
29
30
//===----------------------------------------------------------------------===//
31
//                              Statement Emission
32
//===----------------------------------------------------------------------===//
33
34
1.83M
void CodeGenFunction::EmitStopPoint(const Stmt *S) {
35
1.83M
  if (CGDebugInfo *DI = getDebugInfo()) {
36
647k
    SourceLocation Loc;
37
647k
    Loc = S->getBeginLoc();
38
647k
    DI->EmitLocation(Builder, Loc);
39
647k
40
647k
    LastStopPoint = Loc;
41
647k
  }
42
1.83M
}
43
44
2.15M
void CodeGenFunction::EmitStmt(const Stmt *S, ArrayRef<const Attr *> Attrs) {
45
2.15M
  assert(S && "Null statement?");
46
2.15M
  PGO.setCurrentStmt(S);
47
2.15M
48
2.15M
  // These statements have their own debug info handling.
49
2.15M
  if (EmitSimpleStmt(S))
50
767k
    return;
51
1.39M
52
1.39M
  // Check if we are generating unreachable code.
53
1.39M
  if (!HaveInsertPoint()) {
54
2.38k
    // If so, and the statement doesn't contain a label, then we do not need to
55
2.38k
    // generate actual code. This is safe because (1) the current point is
56
2.38k
    // unreachable, so we don't need to execute the code, and (2) we've already
57
2.38k
    // handled the statements which update internal data structures (like the
58
2.38k
    // local variable map) which could be used by subsequent statements.
59
2.38k
    if (!ContainsLabel(S)) {
60
2.38k
      // Verify that any decl statements were handled as simple, they may be in
61
2.38k
      // scope of subsequent reachable statements.
62
2.38k
      assert(!isa<DeclStmt>(*S) && "Unexpected DeclStmt!");
63
2.38k
      return;
64
2.38k
    }
65
4
66
4
    // Otherwise, make a new block to hold the code.
67
4
    EnsureInsertPoint();
68
4
  }
69
1.39M
70
1.39M
  // Generate a stoppoint if we are emitting debug info.
71
1.39M
  EmitStopPoint(S);
72
1.38M
73
1.38M
  // Ignore all OpenMP directives except for simd if OpenMP with Simd is
74
1.38M
  // enabled.
75
1.38M
  if (getLangOpts().OpenMP && 
getLangOpts().OpenMPSimd56.5k
) {
76
31.1k
    if (const auto *D = dyn_cast<OMPExecutableDirective>(S)) {
77
9.43k
      EmitSimpleOMPExecutableDirective(*D);
78
9.43k
      return;
79
9.43k
    }
80
1.37M
  }
81
1.37M
82
1.37M
  switch (S->getStmtClass()) {
83
1.37M
  case Stmt::NoStmtClass:
84
0
  case Stmt::CXXCatchStmtClass:
85
0
  case Stmt::SEHExceptStmtClass:
86
0
  case Stmt::SEHFinallyStmtClass:
87
0
  case Stmt::MSDependentExistsStmtClass:
88
0
    llvm_unreachable("invalid statement class to emit generically");
89
0
  case Stmt::NullStmtClass:
90
0
  case Stmt::CompoundStmtClass:
91
0
  case Stmt::DeclStmtClass:
92
0
  case Stmt::LabelStmtClass:
93
0
  case Stmt::AttributedStmtClass:
94
0
  case Stmt::GotoStmtClass:
95
0
  case Stmt::BreakStmtClass:
96
0
  case Stmt::ContinueStmtClass:
97
0
  case Stmt::DefaultStmtClass:
98
0
  case Stmt::CaseStmtClass:
99
0
  case Stmt::SEHLeaveStmtClass:
100
0
    llvm_unreachable("should have emitted these statements as simple");
101
0
102
0
#define STMT(Type, Base)
103
0
#define ABSTRACT_STMT(Op)
104
0
#define EXPR(Type, Base) \
105
90.3M
  case Stmt::Type##Class:
106
792k
#include 
"clang/AST/StmtNodes.inc"0
107
792k
  {
108
792k
    // Remember the block we came in on.
109
792k
    llvm::BasicBlock *incoming = Builder.GetInsertBlock();
110
792k
    assert(incoming && "expression emission must have an insertion point");
111
792k
112
792k
    EmitIgnoredExpr(cast<Expr>(S));
113
792k
114
792k
    llvm::BasicBlock *outgoing = Builder.GetInsertBlock();
115
792k
    assert(outgoing && "expression emission cleared block!");
116
792k
117
792k
    // The expression emitters assume (reasonably!) that the insertion
118
792k
    // point is always set.  To maintain that, the call-emission code
119
792k
    // for noreturn functions has to enter a new block with no
120
792k
    // predecessors.  We want to kill that block and mark the current
121
792k
    // insertion point unreachable in the common case of a call like
122
792k
    // "exit();".  Since expression emission doesn't otherwise create
123
792k
    // blocks with no predecessors, we can just test for that.
124
792k
    // However, we must be careful not to do this to our incoming
125
792k
    // block, because *statement* emission does sometimes create
126
792k
    // reachable blocks which will have no predecessors until later in
127
792k
    // the function.  This occurs with, e.g., labels that are not
128
792k
    // reachable by fallthrough.
129
792k
    if (incoming != outgoing && 
outgoing->use_empty()49.1k
) {
130
23.5k
      outgoing->eraseFromParent();
131
23.5k
      Builder.ClearInsertionPoint();
132
23.5k
    }
133
792k
    break;
134
89.6M
  }
135
89.6M
136
89.6M
  case Stmt::IndirectGotoStmtClass:
137
54
    EmitIndirectGotoStmt(cast<IndirectGotoStmt>(*S)); break;
138
89.6M
139
89.6M
  
case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break245k
;
140
89.6M
  
case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S), Attrs); break8.85k
;
141
89.6M
  
case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S), Attrs); break28.0k
;
142
89.6M
  
case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S), Attrs); break36.0k
;
143
89.6M
144
89.6M
  
case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break244k
;
145
89.6M
146
89.6M
  
case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break3.80k
;
147
89.6M
  case Stmt::GCCAsmStmtClass:  // Intentional fall-through.
148
8.19k
  case Stmt::MSAsmStmtClass:   EmitAsmStmt(cast<AsmStmt>(*S));            break;
149
8.19k
  case Stmt::CoroutineBodyStmtClass:
150
41
    EmitCoroutineBody(cast<CoroutineBodyStmt>(*S));
151
41
    break;
152
8.19k
  case Stmt::CoreturnStmtClass:
153
39
    EmitCoreturnStmt(cast<CoreturnStmt>(*S));
154
39
    break;
155
8.19k
  case Stmt::CapturedStmtClass: {
156
27
    const CapturedStmt *CS = cast<CapturedStmt>(S);
157
27
    EmitCapturedStmt(*CS, CS->getCapturedRegionKind());
158
27
    }
159
27
    break;
160
8.19k
  case Stmt::ObjCAtTryStmtClass:
161
214
    EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
162
214
    break;
163
8.19k
  case Stmt::ObjCAtCatchStmtClass:
164
0
    llvm_unreachable(
165
8.19k
                    "@catch statements should be handled by EmitObjCAtTryStmt");
166
8.19k
  case Stmt::ObjCAtFinallyStmtClass:
167
0
    llvm_unreachable(
168
8.19k
                  "@finally statements should be handled by EmitObjCAtTryStmt");
169
8.19k
  case Stmt::ObjCAtThrowStmtClass:
170
31
    EmitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(*S));
171
31
    break;
172
8.19k
  case Stmt::ObjCAtSynchronizedStmtClass:
173
14
    EmitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(*S));
174
14
    break;
175
8.19k
  case Stmt::ObjCForCollectionStmtClass:
176
41
    EmitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(*S));
177
41
    break;
178
8.19k
  case Stmt::ObjCAutoreleasePoolStmtClass:
179
23
    EmitObjCAutoreleasePoolStmt(cast<ObjCAutoreleasePoolStmt>(*S));
180
23
    break;
181
8.19k
182
8.19k
  case Stmt::CXXTryStmtClass:
183
491
    EmitCXXTryStmt(cast<CXXTryStmt>(*S));
184
491
    break;
185
8.19k
  case Stmt::CXXForRangeStmtClass:
186
836
    EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S), Attrs);
187
836
    break;
188
8.19k
  case Stmt::SEHTryStmtClass:
189
124
    EmitSEHTryStmt(cast<SEHTryStmt>(*S));
190
124
    break;
191
8.19k
  case Stmt::OMPParallelDirectiveClass:
192
648
    EmitOMPParallelDirective(cast<OMPParallelDirective>(*S));
193
648
    break;
194
8.19k
  case Stmt::OMPSimdDirectiveClass:
195
118
    EmitOMPSimdDirective(cast<OMPSimdDirective>(*S));
196
118
    break;
197
8.19k
  case Stmt::OMPForDirectiveClass:
198
233
    EmitOMPForDirective(cast<OMPForDirective>(*S));
199
233
    break;
200
8.19k
  case Stmt::OMPForSimdDirectiveClass:
201
202
    EmitOMPForSimdDirective(cast<OMPForSimdDirective>(*S));
202
202
    break;
203
8.19k
  case Stmt::OMPSectionsDirectiveClass:
204
40
    EmitOMPSectionsDirective(cast<OMPSectionsDirective>(*S));
205
40
    break;
206
8.19k
  case Stmt::OMPSectionDirectiveClass:
207
36
    EmitOMPSectionDirective(cast<OMPSectionDirective>(*S));
208
36
    break;
209
8.19k
  case Stmt::OMPSingleDirectiveClass:
210
39
    EmitOMPSingleDirective(cast<OMPSingleDirective>(*S));
211
39
    break;
212
8.19k
  case Stmt::OMPMasterDirectiveClass:
213
9
    EmitOMPMasterDirective(cast<OMPMasterDirective>(*S));
214
9
    break;
215
8.19k
  case Stmt::OMPCriticalDirectiveClass:
216
26
    EmitOMPCriticalDirective(cast<OMPCriticalDirective>(*S));
217
26
    break;
218
8.19k
  case Stmt::OMPParallelForDirectiveClass:
219
137
    EmitOMPParallelForDirective(cast<OMPParallelForDirective>(*S));
220
137
    break;
221
8.19k
  case Stmt::OMPParallelForSimdDirectiveClass:
222
39
    EmitOMPParallelForSimdDirective(cast<OMPParallelForSimdDirective>(*S));
223
39
    break;
224
8.19k
  case Stmt::OMPParallelSectionsDirectiveClass:
225
12
    EmitOMPParallelSectionsDirective(cast<OMPParallelSectionsDirective>(*S));
226
12
    break;
227
8.19k
  case Stmt::OMPTaskDirectiveClass:
228
85
    EmitOMPTaskDirective(cast<OMPTaskDirective>(*S));
229
85
    break;
230
8.19k
  case Stmt::OMPTaskyieldDirectiveClass:
231
8
    EmitOMPTaskyieldDirective(cast<OMPTaskyieldDirective>(*S));
232
8
    break;
233
8.19k
  case Stmt::OMPBarrierDirectiveClass:
234
13
    EmitOMPBarrierDirective(cast<OMPBarrierDirective>(*S));
235
13
    break;
236
8.19k
  case Stmt::OMPTaskwaitDirectiveClass:
237
6
    EmitOMPTaskwaitDirective(cast<OMPTaskwaitDirective>(*S));
238
6
    break;
239
8.19k
  case Stmt::OMPTaskgroupDirectiveClass:
240
31
    EmitOMPTaskgroupDirective(cast<OMPTaskgroupDirective>(*S));
241
31
    break;
242
8.19k
  case Stmt::OMPFlushDirectiveClass:
243
8
    EmitOMPFlushDirective(cast<OMPFlushDirective>(*S));
244
8
    break;
245
8.19k
  case Stmt::OMPOrderedDirectiveClass:
246
24
    EmitOMPOrderedDirective(cast<OMPOrderedDirective>(*S));
247
24
    break;
248
8.19k
  case Stmt::OMPAtomicDirectiveClass:
249
418
    EmitOMPAtomicDirective(cast<OMPAtomicDirective>(*S));
250
418
    break;
251
8.19k
  case Stmt::OMPTargetDirectiveClass:
252
2.28k
    EmitOMPTargetDirective(cast<OMPTargetDirective>(*S));
253
2.28k
    break;
254
8.19k
  case Stmt::OMPTeamsDirectiveClass:
255
745
    EmitOMPTeamsDirective(cast<OMPTeamsDirective>(*S));
256
745
    break;
257
8.19k
  case Stmt::OMPCancellationPointDirectiveClass:
258
32
    EmitOMPCancellationPointDirective(cast<OMPCancellationPointDirective>(*S));
259
32
    break;
260
8.19k
  case Stmt::OMPCancelDirectiveClass:
261
66
    EmitOMPCancelDirective(cast<OMPCancelDirective>(*S));
262
66
    break;
263
8.19k
  case Stmt::OMPTargetDataDirectiveClass:
264
95
    EmitOMPTargetDataDirective(cast<OMPTargetDataDirective>(*S));
265
95
    break;
266
8.19k
  case Stmt::OMPTargetEnterDataDirectiveClass:
267
53
    EmitOMPTargetEnterDataDirective(cast<OMPTargetEnterDataDirective>(*S));
268
53
    break;
269
8.19k
  case Stmt::OMPTargetExitDataDirectiveClass:
270
47
    EmitOMPTargetExitDataDirective(cast<OMPTargetExitDataDirective>(*S));
271
47
    break;
272
8.19k
  case Stmt::OMPTargetParallelDirectiveClass:
273
304
    EmitOMPTargetParallelDirective(cast<OMPTargetParallelDirective>(*S));
274
304
    break;
275
8.19k
  case Stmt::OMPTargetParallelForDirectiveClass:
276
200
    EmitOMPTargetParallelForDirective(cast<OMPTargetParallelForDirective>(*S));
277
200
    break;
278
8.19k
  case Stmt::OMPTaskLoopDirectiveClass:
279
33
    EmitOMPTaskLoopDirective(cast<OMPTaskLoopDirective>(*S));
280
33
    break;
281
8.19k
  case Stmt::OMPTaskLoopSimdDirectiveClass:
282
32
    EmitOMPTaskLoopSimdDirective(cast<OMPTaskLoopSimdDirective>(*S));
283
32
    break;
284
8.19k
  case Stmt::OMPDistributeDirectiveClass:
285
90
    EmitOMPDistributeDirective(cast<OMPDistributeDirective>(*S));
286
90
    break;
287
8.19k
  case Stmt::OMPTargetUpdateDirectiveClass:
288
61
    EmitOMPTargetUpdateDirective(cast<OMPTargetUpdateDirective>(*S));
289
61
    break;
290
8.19k
  case Stmt::OMPDistributeParallelForDirectiveClass:
291
307
    EmitOMPDistributeParallelForDirective(
292
307
        cast<OMPDistributeParallelForDirective>(*S));
293
307
    break;
294
8.19k
  case Stmt::OMPDistributeParallelForSimdDirectiveClass:
295
224
    EmitOMPDistributeParallelForSimdDirective(
296
224
        cast<OMPDistributeParallelForSimdDirective>(*S));
297
224
    break;
298
8.19k
  case Stmt::OMPDistributeSimdDirectiveClass:
299
100
    EmitOMPDistributeSimdDirective(cast<OMPDistributeSimdDirective>(*S));
300
100
    break;
301
8.19k
  case Stmt::OMPTargetParallelForSimdDirectiveClass:
302
169
    EmitOMPTargetParallelForSimdDirective(
303
169
        cast<OMPTargetParallelForSimdDirective>(*S));
304
169
    break;
305
8.19k
  case Stmt::OMPTargetSimdDirectiveClass:
306
177
    EmitOMPTargetSimdDirective(cast<OMPTargetSimdDirective>(*S));
307
177
    break;
308
8.19k
  case Stmt::OMPTeamsDistributeDirectiveClass:
309
114
    EmitOMPTeamsDistributeDirective(cast<OMPTeamsDistributeDirective>(*S));
310
114
    break;
311
8.19k
  case Stmt::OMPTeamsDistributeSimdDirectiveClass:
312
114
    EmitOMPTeamsDistributeSimdDirective(
313
114
        cast<OMPTeamsDistributeSimdDirective>(*S));
314
114
    break;
315
8.19k
  case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
316
208
    EmitOMPTeamsDistributeParallelForSimdDirective(
317
208
        cast<OMPTeamsDistributeParallelForSimdDirective>(*S));
318
208
    break;
319
8.19k
  case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
320
218
    EmitOMPTeamsDistributeParallelForDirective(
321
218
        cast<OMPTeamsDistributeParallelForDirective>(*S));
322
218
    break;
323
8.19k
  case Stmt::OMPTargetTeamsDirectiveClass:
324
321
    EmitOMPTargetTeamsDirective(cast<OMPTargetTeamsDirective>(*S));
325
321
    break;
326
8.19k
  case Stmt::OMPTargetTeamsDistributeDirectiveClass:
327
261
    EmitOMPTargetTeamsDistributeDirective(
328
261
        cast<OMPTargetTeamsDistributeDirective>(*S));
329
261
    break;
330
8.19k
  case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
331
264
    EmitOMPTargetTeamsDistributeParallelForDirective(
332
264
        cast<OMPTargetTeamsDistributeParallelForDirective>(*S));
333
264
    break;
334
8.19k
  case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
335
351
    EmitOMPTargetTeamsDistributeParallelForSimdDirective(
336
351
        cast<OMPTargetTeamsDistributeParallelForSimdDirective>(*S));
337
351
    break;
338
8.19k
  case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
339
267
    EmitOMPTargetTeamsDistributeSimdDirective(
340
267
        cast<OMPTargetTeamsDistributeSimdDirective>(*S));
341
267
    break;
342
1.37M
  }
343
1.37M
}
344
345
2.15M
bool CodeGenFunction::EmitSimpleStmt(const Stmt *S) {
346
2.15M
  switch (S->getStmtClass()) {
347
2.15M
  
default: return false1.39M
;
348
2.15M
  
case Stmt::NullStmtClass: break58.5k
;
349
2.15M
  
case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break295k
;
350
2.15M
  
case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break368k
;
351
2.15M
  
case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break1.00k
;
352
2.15M
  case Stmt::AttributedStmtClass:
353
71
                            EmitAttributedStmt(cast<AttributedStmt>(*S)); break;
354
2.15M
  
case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break5.16k
;
355
2.15M
  
case Stmt::BreakStmtClass: EmitBreakStmt(cast<BreakStmt>(*S)); break16.7k
;
356
2.15M
  
case Stmt::ContinueStmtClass: EmitContinueStmt(cast<ContinueStmt>(*S)); break3.08k
;
357
2.15M
  
case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break1.99k
;
358
2.15M
  
case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break17.3k
;
359
2.15M
  
case Stmt::SEHLeaveStmtClass: EmitSEHLeaveStmt(cast<SEHLeaveStmt>(*S)); break9
;
360
767k
  }
361
767k
362
767k
  return true;
363
767k
}
364
365
/// EmitCompoundStmt - Emit a compound statement {..} node.  If GetLast is true,
366
/// this captures the expression result of the last sub-statement and returns it
367
/// (for use by the statement expression extension).
368
Address CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
369
305k
                                          AggValueSlot AggSlot) {
370
305k
  PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),S.getLBracLoc(),
371
305k
                             "LLVM IR generation of compound statement ('{}')");
372
305k
373
305k
  // Keep track of the current cleanup stack depth, including debug scopes.
374
305k
  LexicalScope Scope(*this, S.getSourceRange());
375
305k
376
305k
  return EmitCompoundStmtWithoutScope(S, GetLast, AggSlot);
377
305k
}
378
379
Address
380
CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S,
381
                                              bool GetLast,
382
584k
                                              AggValueSlot AggSlot) {
383
584k
384
584k
  const Stmt *ExprResult = S.getStmtExprResult();
385
584k
  assert((!GetLast || (GetLast && ExprResult)) &&
386
584k
         "If GetLast is true then the CompoundStmt must have a StmtExprResult");
387
584k
388
584k
  Address RetAlloca = Address::invalid();
389
584k
390
1.64M
  for (auto *CurStmt : S.body()) {
391
1.64M
    if (GetLast && 
ExprResult == CurStmt37.7k
) {
392
9.12k
      // We have to special case labels here.  They are statements, but when put
393
9.12k
      // at the end of a statement expression, they yield the value of their
394
9.12k
      // subexpression.  Handle this by walking through all labels we encounter,
395
9.12k
      // emitting them before we evaluate the subexpr.
396
9.12k
      // Similar issues arise for attributed statements.
397
9.12k
      while (!isa<Expr>(ExprResult)) {
398
5
        if (const auto *LS = dyn_cast<LabelStmt>(ExprResult)) {
399
3
          EmitLabel(LS->getDecl());
400
3
          ExprResult = LS->getSubStmt();
401
3
        } else 
if (const auto *2
AS2
= dyn_cast<AttributedStmt>(ExprResult)) {
402
2
          // FIXME: Update this if we ever have attributes that affect the
403
2
          // semantics of an expression.
404
2
          ExprResult = AS->getSubStmt();
405
2
        } else {
406
0
          llvm_unreachable("unknown value statement");
407
0
        }
408
5
      }
409
9.12k
410
9.12k
      EnsureInsertPoint();
411
9.12k
412
9.12k
      const Expr *E = cast<Expr>(ExprResult);
413
9.12k
      QualType ExprTy = E->getType();
414
9.12k
      if (hasAggregateEvaluationKind(ExprTy)) {
415
837
        EmitAggExpr(E, AggSlot);
416
8.28k
      } else {
417
8.28k
        // We can't return an RValue here because there might be cleanups at
418
8.28k
        // the end of the StmtExpr.  Because of that, we have to emit the result
419
8.28k
        // here into a temporary alloca.
420
8.28k
        RetAlloca = CreateMemTemp(ExprTy);
421
8.28k
        EmitAnyExprToMem(E, RetAlloca, Qualifiers(),
422
8.28k
                         /*IsInit*/ false);
423
8.28k
      }
424
1.63M
    } else {
425
1.63M
      EmitStmt(CurStmt);
426
1.63M
    }
427
1.64M
  }
428
584k
429
584k
  return RetAlloca;
430
584k
}
431
432
27.0k
void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
433
27.0k
  llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
434
27.0k
435
27.0k
  // If there is a cleanup stack, then we it isn't worth trying to
436
27.0k
  // simplify this block (we would need to remove it from the scope map
437
27.0k
  // and cleanup entry).
438
27.0k
  if (!EHStack.empty())
439
16.5k
    return;
440
10.5k
441
10.5k
  // Can only simplify direct branches.
442
10.5k
  if (!BI || !BI->isUnconditional())
443
0
    return;
444
10.5k
445
10.5k
  // Can only simplify empty blocks.
446
10.5k
  if (BI->getIterator() != BB->begin())
447
1
    return;
448
10.5k
449
10.5k
  BB->replaceAllUsesWith(BI->getSuccessor(0));
450
10.5k
  BI->eraseFromParent();
451
10.5k
  BB->eraseFromParent();
452
10.5k
}
453
454
1.32M
void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB, bool IsFinished) {
455
1.32M
  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
456
1.32M
457
1.32M
  // Fall out of the current block (if necessary).
458
1.32M
  EmitBranch(BB);
459
1.32M
460
1.32M
  if (IsFinished && 
BB->use_empty()293k
) {
461
4.77k
    delete BB;
462
4.77k
    return;
463
4.77k
  }
464
1.31M
465
1.31M
  // Place the block after the current block, if possible, or else at
466
1.31M
  // the end of the function.
467
1.31M
  if (CurBB && 
CurBB->getParent()721k
)
468
721k
    CurFn->getBasicBlockList().insertAfter(CurBB->getIterator(), BB);
469
594k
  else
470
594k
    CurFn->getBasicBlockList().push_back(BB);
471
1.31M
  Builder.SetInsertPoint(BB);
472
1.31M
}
473
474
1.66M
void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
475
1.66M
  // Emit a branch from the current block to the target one if this
476
1.66M
  // was a real block.  If this was just a fall-through block after a
477
1.66M
  // terminator, don't emit it.
478
1.66M
  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
479
1.66M
480
1.66M
  if (!CurBB || 
CurBB->getTerminator()976k
) {
481
1.16M
    // If there is no insert point or the previous block is already
482
1.16M
    // terminated, don't touch it.
483
1.16M
  } else {
484
503k
    // Otherwise, create a fall-through branch.
485
503k
    Builder.CreateBr(Target);
486
503k
  }
487
1.66M
488
1.66M
  Builder.ClearInsertionPoint();
489
1.66M
}
490
491
1.11k
void CodeGenFunction::EmitBlockAfterUses(llvm::BasicBlock *block) {
492
1.11k
  bool inserted = false;
493
1.11k
  for (llvm::User *u : block->users()) {
494
1.11k
    if (llvm::Instruction *insn = dyn_cast<llvm::Instruction>(u)) {
495
1.11k
      CurFn->getBasicBlockList().insertAfter(insn->getParent()->getIterator(),
496
1.11k
                                             block);
497
1.11k
      inserted = true;
498
1.11k
      break;
499
1.11k
    }
500
1.11k
  }
501
1.11k
502
1.11k
  if (!inserted)
503
0
    CurFn->getBasicBlockList().push_back(block);
504
1.11k
505
1.11k
  Builder.SetInsertPoint(block);
506
1.11k
}
507
508
CodeGenFunction::JumpDest
509
5.26k
CodeGenFunction::getJumpDestForLabel(const LabelDecl *D) {
510
5.26k
  JumpDest &Dest = LabelMap[D];
511
5.26k
  if (Dest.isValid()) 
return Dest4.60k
;
512
665
513
665
  // Create, but don't insert, the new block.
514
665
  Dest = JumpDest(createBasicBlock(D->getName()),
515
665
                  EHScopeStack::stable_iterator::invalid(),
516
665
                  NextCleanupDestIndex++);
517
665
  return Dest;
518
665
}
519
520
1.00k
void CodeGenFunction::EmitLabel(const LabelDecl *D) {
521
1.00k
  // Add this label to the current lexical scope if we're within any
522
1.00k
  // normal cleanups.  Jumps "in" to this label --- when permitted by
523
1.00k
  // the language --- may need to be routed around such cleanups.
524
1.00k
  if (EHStack.hasNormalCleanups() && 
CurLexicalScope769
)
525
363
    CurLexicalScope->addLabel(D);
526
1.00k
527
1.00k
  JumpDest &Dest = LabelMap[D];
528
1.00k
529
1.00k
  // If we didn't need a forward reference to this label, just go
530
1.00k
  // ahead and create a destination at the current scope.
531
1.00k
  if (!Dest.isValid()) {
532
338
    Dest = getJumpDestInCurrentScope(D->getName());
533
338
534
338
  // Otherwise, we need to give this label a target depth and remove
535
338
  // it from the branch-fixups list.
536
665
  } else {
537
665
    assert(!Dest.getScopeDepth().isValid() && "already emitted label!");
538
665
    Dest.setScopeDepth(EHStack.stable_begin());
539
665
    ResolveBranchFixups(Dest.getBlock());
540
665
  }
541
1.00k
542
1.00k
  EmitBlock(Dest.getBlock());
543
1.00k
544
1.00k
  // Emit debug info for labels.
545
1.00k
  if (CGDebugInfo *DI = getDebugInfo()) {
546
107
    if (CGM.getCodeGenOpts().getDebugInfo() >=
547
107
        codegenoptions::LimitedDebugInfo) {
548
7
      DI->setLocation(D->getLocation());
549
7
      DI->EmitLabel(D, Builder);
550
7
    }
551
107
  }
552
1.00k
553
1.00k
  incrementProfileCounter(D->getStmt());
554
1.00k
}
555
556
/// Change the cleanup scope of the labels in this lexical scope to
557
/// match the scope of the enclosing context.
558
1.04k
void CodeGenFunction::LexicalScope::rescopeLabels() {
559
1.04k
  assert(!Labels.empty());
560
1.04k
  EHScopeStack::stable_iterator innermostScope
561
1.04k
    = CGF.EHStack.getInnermostNormalCleanup();
562
1.04k
563
1.04k
  // Change the scope depth of all the labels.
564
1.04k
  for (SmallVectorImpl<const LabelDecl*>::const_iterator
565
2.60k
         i = Labels.begin(), e = Labels.end(); i != e; 
++i1.56k
) {
566
1.56k
    assert(CGF.LabelMap.count(*i));
567
1.56k
    JumpDest &dest = CGF.LabelMap.find(*i)->second;
568
1.56k
    assert(dest.getScopeDepth().isValid());
569
1.56k
    assert(innermostScope.encloses(dest.getScopeDepth()));
570
1.56k
    dest.setScopeDepth(innermostScope);
571
1.56k
  }
572
1.04k
573
1.04k
  // Reparent the labels if the new scope also has cleanups.
574
1.04k
  if (innermostScope != EHScopeStack::stable_end() && 
ParentScope1.04k
) {
575
830
    ParentScope->Labels.append(Labels.begin(), Labels.end());
576
830
  }
577
1.04k
}
578
579
580
1.00k
void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
581
1.00k
  EmitLabel(S.getDecl());
582
1.00k
  EmitStmt(S.getSubStmt());
583
1.00k
}
584
585
71
void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
586
71
  EmitStmt(S.getSubStmt(), S.getAttrs());
587
71
}
588
589
5.16k
void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
590
5.16k
  // If this code is reachable then emit a stop point (if generating
591
5.16k
  // debug info). We have to do this ourselves because we are on the
592
5.16k
  // "simple" statement path.
593
5.16k
  if (HaveInsertPoint())
594
5.15k
    EmitStopPoint(&S);
595
5.16k
596
5.16k
  EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel()));
597
5.16k
}
598
599
600
54
void CodeGenFunction::EmitIndirectGotoStmt(const IndirectGotoStmt &S) {
601
54
  if (const LabelDecl *Target = S.getConstantTarget()) {
602
0
    EmitBranchThroughCleanup(getJumpDestForLabel(Target));
603
0
    return;
604
0
  }
605
54
606
54
  // Ensure that we have an i8* for our PHI node.
607
54
  llvm::Value *V = Builder.CreateBitCast(EmitScalarExpr(S.getTarget()),
608
54
                                         Int8PtrTy, "addr");
609
54
  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
610
54
611
54
  // Get the basic block for the indirect goto.
612
54
  llvm::BasicBlock *IndGotoBB = GetIndirectGotoBlock();
613
54
614
54
  // The first instruction in the block has to be the PHI for the switch dest,
615
54
  // add an entry for this branch.
616
54
  cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
617
54
618
54
  EmitBranch(IndGotoBB);
619
54
}
620
621
245k
void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
622
245k
  // C99 6.8.4.1: The first substatement is executed if the expression compares
623
245k
  // unequal to 0.  The condition must be a scalar type.
624
245k
  LexicalScope ConditionScope(*this, S.getCond()->getSourceRange());
625
245k
626
245k
  if (S.getInit())
627
6
    EmitStmt(S.getInit());
628
245k
629
245k
  if (S.getConditionVariable())
630
1.05k
    EmitDecl(*S.getConditionVariable());
631
245k
632
245k
  // If the condition constant folds and can be elided, try to avoid emitting
633
245k
  // the condition and the dead arm of the if/else.
634
245k
  bool CondConstant;
635
245k
  if (ConstantFoldsToSimpleInteger(S.getCond(), CondConstant,
636
245k
                                   S.isConstexpr())) {
637
13.2k
    // Figure out which block (then or else) is executed.
638
13.2k
    const Stmt *Executed = S.getThen();
639
13.2k
    const Stmt *Skipped  = S.getElse();
640
13.2k
    if (!CondConstant)  // Condition false?
641
10.5k
      std::swap(Executed, Skipped);
642
13.2k
643
13.2k
    // If the skipped block has no labels in it, just emit the executed block.
644
13.2k
    // This avoids emitting dead code and simplifies the CFG substantially.
645
13.2k
    if (S.isConstexpr() || 
!ContainsLabel(Skipped)13.2k
) {
646
13.2k
      if (CondConstant)
647
2.72k
        incrementProfileCounter(&S);
648
13.2k
      if (Executed) {
649
9.08k
        RunCleanupsScope ExecutedScope(*this);
650
9.08k
        EmitStmt(Executed);
651
9.08k
      }
652
13.2k
      return;
653
13.2k
    }
654
232k
  }
655
232k
656
232k
  // Otherwise, the condition did not fold, or we couldn't elide it.  Just emit
657
232k
  // the conditional branch.
658
232k
  llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
659
232k
  llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
660
232k
  llvm::BasicBlock *ElseBlock = ContBlock;
661
232k
  if (S.getElse())
662
42.2k
    ElseBlock = createBasicBlock("if.else");
663
232k
664
232k
  EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock,
665
232k
                       getProfileCount(S.getThen()));
666
232k
667
232k
  // Emit the 'then' code.
668
232k
  EmitBlock(ThenBlock);
669
232k
  incrementProfileCounter(&S);
670
232k
  {
671
232k
    RunCleanupsScope ThenScope(*this);
672
232k
    EmitStmt(S.getThen());
673
232k
  }
674
232k
  EmitBranch(ContBlock);
675
232k
676
232k
  // Emit the 'else' code if present.
677
232k
  if (const Stmt *Else = S.getElse()) {
678
42.2k
    {
679
42.2k
      // There is no need to emit line number for an unconditional branch.
680
42.2k
      auto NL = ApplyDebugLocation::CreateEmpty(*this);
681
42.2k
      EmitBlock(ElseBlock);
682
42.2k
    }
683
42.2k
    {
684
42.2k
      RunCleanupsScope ElseScope(*this);
685
42.2k
      EmitStmt(Else);
686
42.2k
    }
687
42.2k
    {
688
42.2k
      // There is no need to emit line number for an unconditional branch.
689
42.2k
      auto NL = ApplyDebugLocation::CreateEmpty(*this);
690
42.2k
      EmitBranch(ContBlock);
691
42.2k
    }
692
42.2k
  }
693
232k
694
232k
  // Emit the continuation block for code after the if.
695
232k
  EmitBlock(ContBlock, true);
696
232k
}
697
698
void CodeGenFunction::EmitWhileStmt(const WhileStmt &S,
699
8.85k
                                    ArrayRef<const Attr *> WhileAttrs) {
700
8.85k
  // Emit the header for the loop, which will also become
701
8.85k
  // the continue target.
702
8.85k
  JumpDest LoopHeader = getJumpDestInCurrentScope("while.cond");
703
8.85k
  EmitBlock(LoopHeader.getBlock());
704
8.85k
705
8.85k
  const SourceRange &R = S.getSourceRange();
706
8.85k
  LoopStack.push(LoopHeader.getBlock(), CGM.getContext(), WhileAttrs,
707
8.85k
                 SourceLocToDebugLoc(R.getBegin()),
708
8.85k
                 SourceLocToDebugLoc(R.getEnd()));
709
8.85k
710
8.85k
  // Create an exit block for when the condition fails, which will
711
8.85k
  // also become the break target.
712
8.85k
  JumpDest LoopExit = getJumpDestInCurrentScope("while.end");
713
8.85k
714
8.85k
  // Store the blocks to use for break and continue.
715
8.85k
  BreakContinueStack.push_back(BreakContinue(LoopExit, LoopHeader));
716
8.85k
717
8.85k
  // C++ [stmt.while]p2:
718
8.85k
  //   When the condition of a while statement is a declaration, the
719
8.85k
  //   scope of the variable that is declared extends from its point
720
8.85k
  //   of declaration (3.3.2) to the end of the while statement.
721
8.85k
  //   [...]
722
8.85k
  //   The object created in a condition is destroyed and created
723
8.85k
  //   with each iteration of the loop.
724
8.85k
  RunCleanupsScope ConditionScope(*this);
725
8.85k
726
8.85k
  if (S.getConditionVariable())
727
27
    EmitDecl(*S.getConditionVariable());
728
8.85k
729
8.85k
  // Evaluate the conditional in the while header.  C99 6.8.5.1: The
730
8.85k
  // evaluation of the controlling expression takes place before each
731
8.85k
  // execution of the loop body.
732
8.85k
  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
733
8.85k
734
8.85k
  // while(1) is common, avoid extra exit blocks.  Be sure
735
8.85k
  // to correctly handle break/continue though.
736
8.85k
  bool EmitBoolCondBranch = true;
737
8.85k
  if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
738
836
    if (C->isOne())
739
820
      EmitBoolCondBranch = false;
740
8.85k
741
8.85k
  // As long as the condition is true, go to the loop body.
742
8.85k
  llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
743
8.85k
  if (EmitBoolCondBranch) {
744
8.03k
    llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
745
8.03k
    if (ConditionScope.requiresCleanups())
746
25
      ExitBlock = createBasicBlock("while.exit");
747
8.03k
    Builder.CreateCondBr(
748
8.03k
        BoolCondVal, LoopBody, ExitBlock,
749
8.03k
        createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())));
750
8.03k
751
8.03k
    if (ExitBlock != LoopExit.getBlock()) {
752
25
      EmitBlock(ExitBlock);
753
25
      EmitBranchThroughCleanup(LoopExit);
754
25
    }
755
8.03k
  }
756
8.85k
757
8.85k
  // Emit the loop body.  We have to emit this in a cleanup scope
758
8.85k
  // because it might be a singleton DeclStmt.
759
8.85k
  {
760
8.85k
    RunCleanupsScope BodyScope(*this);
761
8.85k
    EmitBlock(LoopBody);
762
8.85k
    incrementProfileCounter(&S);
763
8.85k
    EmitStmt(S.getBody());
764
8.85k
  }
765
8.85k
766
8.85k
  BreakContinueStack.pop_back();
767
8.85k
768
8.85k
  // Immediately force cleanup.
769
8.85k
  ConditionScope.ForceCleanup();
770
8.85k
771
8.85k
  EmitStopPoint(&S);
772
8.85k
  // Branch to the loop header again.
773
8.85k
  EmitBranch(LoopHeader.getBlock());
774
8.85k
775
8.85k
  LoopStack.pop();
776
8.85k
777
8.85k
  // Emit the exit block.
778
8.85k
  EmitBlock(LoopExit.getBlock(), true);
779
8.85k
780
8.85k
  // The LoopHeader typically is just a branch if we skipped emitting
781
8.85k
  // a branch, try to erase it.
782
8.85k
  if (!EmitBoolCondBranch)
783
820
    SimplifyForwardingBlocks(LoopHeader.getBlock());
784
8.85k
}
785
786
void CodeGenFunction::EmitDoStmt(const DoStmt &S,
787
28.0k
                                 ArrayRef<const Attr *> DoAttrs) {
788
28.0k
  JumpDest LoopExit = getJumpDestInCurrentScope("do.end");
789
28.0k
  JumpDest LoopCond = getJumpDestInCurrentScope("do.cond");
790
28.0k
791
28.0k
  uint64_t ParentCount = getCurrentProfileCount();
792
28.0k
793
28.0k
  // Store the blocks to use for break and continue.
794
28.0k
  BreakContinueStack.push_back(BreakContinue(LoopExit, LoopCond));
795
28.0k
796
28.0k
  // Emit the body of the loop.
797
28.0k
  llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
798
28.0k
799
28.0k
  EmitBlockWithFallThrough(LoopBody, &S);
800
28.0k
  {
801
28.0k
    RunCleanupsScope BodyScope(*this);
802
28.0k
    EmitStmt(S.getBody());
803
28.0k
  }
804
28.0k
805
28.0k
  EmitBlock(LoopCond.getBlock());
806
28.0k
807
28.0k
  const SourceRange &R = S.getSourceRange();
808
28.0k
  LoopStack.push(LoopBody, CGM.getContext(), DoAttrs,
809
28.0k
                 SourceLocToDebugLoc(R.getBegin()),
810
28.0k
                 SourceLocToDebugLoc(R.getEnd()));
811
28.0k
812
28.0k
  // C99 6.8.5.2: "The evaluation of the controlling expression takes place
813
28.0k
  // after each execution of the loop body."
814
28.0k
815
28.0k
  // Evaluate the conditional in the while header.
816
28.0k
  // C99 6.8.5p2/p4: The first substatement is executed if the expression
817
28.0k
  // compares unequal to 0.  The condition must be a scalar type.
818
28.0k
  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
819
28.0k
820
28.0k
  BreakContinueStack.pop_back();
821
28.0k
822
28.0k
  // "do {} while (0)" is common in macros, avoid extra blocks.  Be sure
823
28.0k
  // to correctly handle break/continue though.
824
28.0k
  bool EmitBoolCondBranch = true;
825
28.0k
  if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
826
26.2k
    if (C->isZero())
827
26.2k
      EmitBoolCondBranch = false;
828
28.0k
829
28.0k
  // As long as the condition is true, iterate the loop.
830
28.0k
  if (EmitBoolCondBranch) {
831
1.79k
    uint64_t BackedgeCount = getProfileCount(S.getBody()) - ParentCount;
832
1.79k
    Builder.CreateCondBr(
833
1.79k
        BoolCondVal, LoopBody, LoopExit.getBlock(),
834
1.79k
        createProfileWeightsForLoop(S.getCond(), BackedgeCount));
835
1.79k
  }
836
28.0k
837
28.0k
  LoopStack.pop();
838
28.0k
839
28.0k
  // Emit the exit block.
840
28.0k
  EmitBlock(LoopExit.getBlock());
841
28.0k
842
28.0k
  // The DoCond block typically is just a branch if we skipped
843
28.0k
  // emitting a branch, try to erase it.
844
28.0k
  if (!EmitBoolCondBranch)
845
26.2k
    SimplifyForwardingBlocks(LoopCond.getBlock());
846
28.0k
}
847
848
void CodeGenFunction::EmitForStmt(const ForStmt &S,
849
36.0k
                                  ArrayRef<const Attr *> ForAttrs) {
850
36.0k
  JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
851
36.0k
852
36.0k
  LexicalScope ForScope(*this, S.getSourceRange());
853
36.0k
854
36.0k
  // Evaluate the first part before the loop.
855
36.0k
  if (S.getInit())
856
33.3k
    EmitStmt(S.getInit());
857
36.0k
858
36.0k
  // Start the loop with a block that tests the condition.
859
36.0k
  // If there's an increment, the continue scope will be overwritten
860
36.0k
  // later.
861
36.0k
  JumpDest Continue = getJumpDestInCurrentScope("for.cond");
862
36.0k
  llvm::BasicBlock *CondBlock = Continue.getBlock();
863
36.0k
  EmitBlock(CondBlock);
864
36.0k
865
36.0k
  const SourceRange &R = S.getSourceRange();
866
36.0k
  LoopStack.push(CondBlock, CGM.getContext(), ForAttrs,
867
36.0k
                 SourceLocToDebugLoc(R.getBegin()),
868
36.0k
                 SourceLocToDebugLoc(R.getEnd()));
869
36.0k
870
36.0k
  // If the for loop doesn't have an increment we can just use the
871
36.0k
  // condition as the continue block.  Otherwise we'll need to create
872
36.0k
  // a block for it (in the current scope, i.e. in the scope of the
873
36.0k
  // condition), and that we will become our continue block.
874
36.0k
  if (S.getInc())
875
35.1k
    Continue = getJumpDestInCurrentScope("for.inc");
876
36.0k
877
36.0k
  // Store the blocks to use for break and continue.
878
36.0k
  BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
879
36.0k
880
36.0k
  // Create a cleanup scope for the condition variable cleanups.
881
36.0k
  LexicalScope ConditionScope(*this, S.getSourceRange());
882
36.0k
883
36.0k
  if (S.getCond()) {
884
35.0k
    // If the for statement has a condition scope, emit the local variable
885
35.0k
    // declaration.
886
35.0k
    if (S.getConditionVariable()) {
887
3
      EmitDecl(*S.getConditionVariable());
888
3
    }
889
35.0k
890
35.0k
    llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
891
35.0k
    // If there are any cleanups between here and the loop-exit scope,
892
35.0k
    // create a block to stage a loop exit along.
893
35.0k
    if (ForScope.requiresCleanups())
894
11.6k
      ExitBlock = createBasicBlock("for.cond.cleanup");
895
35.0k
896
35.0k
    // As long as the condition is true, iterate the loop.
897
35.0k
    llvm::BasicBlock *ForBody = createBasicBlock("for.body");
898
35.0k
899
35.0k
    // C99 6.8.5p2/p4: The first substatement is executed if the expression
900
35.0k
    // compares unequal to 0.  The condition must be a scalar type.
901
35.0k
    llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
902
35.0k
    Builder.CreateCondBr(
903
35.0k
        BoolCondVal, ForBody, ExitBlock,
904
35.0k
        createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())));
905
35.0k
906
35.0k
    if (ExitBlock != LoopExit.getBlock()) {
907
11.6k
      EmitBlock(ExitBlock);
908
11.6k
      EmitBranchThroughCleanup(LoopExit);
909
11.6k
    }
910
35.0k
911
35.0k
    EmitBlock(ForBody);
912
35.0k
  } else {
913
1.05k
    // Treat it as a non-zero constant.  Don't even create a new block for the
914
1.05k
    // body, just fall into it.
915
1.05k
  }
916
36.0k
  incrementProfileCounter(&S);
917
36.0k
918
36.0k
  {
919
36.0k
    // Create a separate cleanup scope for the body, in case it is not
920
36.0k
    // a compound statement.
921
36.0k
    RunCleanupsScope BodyScope(*this);
922
36.0k
    EmitStmt(S.getBody());
923
36.0k
  }
924
36.0k
925
36.0k
  // If there is an increment, emit it next.
926
36.0k
  if (S.getInc()) {
927
35.1k
    EmitBlock(Continue.getBlock());
928
35.1k
    EmitStmt(S.getInc());
929
35.1k
  }
930
36.0k
931
36.0k
  BreakContinueStack.pop_back();
932
36.0k
933
36.0k
  ConditionScope.ForceCleanup();
934
36.0k
935
36.0k
  EmitStopPoint(&S);
936
36.0k
  EmitBranch(CondBlock);
937
36.0k
938
36.0k
  ForScope.ForceCleanup();
939
36.0k
940
36.0k
  LoopStack.pop();
941
36.0k
942
36.0k
  // Emit the fall-through block.
943
36.0k
  EmitBlock(LoopExit.getBlock(), true);
944
36.0k
}
945
946
void
947
CodeGenFunction::EmitCXXForRangeStmt(const CXXForRangeStmt &S,
948
836
                                     ArrayRef<const Attr *> ForAttrs) {
949
836
  JumpDest LoopExit = getJumpDestInCurrentScope("for.end");
950
836
951
836
  LexicalScope ForScope(*this, S.getSourceRange());
952
836
953
836
  // Evaluate the first pieces before the loop.
954
836
  if (S.getInit())
955
2
    EmitStmt(S.getInit());
956
836
  EmitStmt(S.getRangeStmt());
957
836
  EmitStmt(S.getBeginStmt());
958
836
  EmitStmt(S.getEndStmt());
959
836
960
836
  // Start the loop with a block that tests the condition.
961
836
  // If there's an increment, the continue scope will be overwritten
962
836
  // later.
963
836
  llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
964
836
  EmitBlock(CondBlock);
965
836
966
836
  const SourceRange &R = S.getSourceRange();
967
836
  LoopStack.push(CondBlock, CGM.getContext(), ForAttrs,
968
836
                 SourceLocToDebugLoc(R.getBegin()),
969
836
                 SourceLocToDebugLoc(R.getEnd()));
970
836
971
836
  // If there are any cleanups between here and the loop-exit scope,
972
836
  // create a block to stage a loop exit along.
973
836
  llvm::BasicBlock *ExitBlock = LoopExit.getBlock();
974
836
  if (ForScope.requiresCleanups())
975
778
    ExitBlock = createBasicBlock("for.cond.cleanup");
976
836
977
836
  // The loop body, consisting of the specified body and the loop variable.
978
836
  llvm::BasicBlock *ForBody = createBasicBlock("for.body");
979
836
980
836
  // The body is executed if the expression, contextually converted
981
836
  // to bool, is true.
982
836
  llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
983
836
  Builder.CreateCondBr(
984
836
      BoolCondVal, ForBody, ExitBlock,
985
836
      createProfileWeightsForLoop(S.getCond(), getProfileCount(S.getBody())));
986
836
987
836
  if (ExitBlock != LoopExit.getBlock()) {
988
778
    EmitBlock(ExitBlock);
989
778
    EmitBranchThroughCleanup(LoopExit);
990
778
  }
991
836
992
836
  EmitBlock(ForBody);
993
836
  incrementProfileCounter(&S);
994
836
995
836
  // Create a block for the increment. In case of a 'continue', we jump there.
996
836
  JumpDest Continue = getJumpDestInCurrentScope("for.inc");
997
836
998
836
  // Store the blocks to use for break and continue.
999
836
  BreakContinueStack.push_back(BreakContinue(LoopExit, Continue));
1000
836
1001
836
  {
1002
836
    // Create a separate cleanup scope for the loop variable and body.
1003
836
    LexicalScope BodyScope(*this, S.getSourceRange());
1004
836
    EmitStmt(S.getLoopVarStmt());
1005
836
    EmitStmt(S.getBody());
1006
836
  }
1007
836
1008
836
  EmitStopPoint(&S);
1009
836
  // If there is an increment, emit it next.
1010
836
  EmitBlock(Continue.getBlock());
1011
836
  EmitStmt(S.getInc());
1012
836
1013
836
  BreakContinueStack.pop_back();
1014
836
1015
836
  EmitBranch(CondBlock);
1016
836
1017
836
  ForScope.ForceCleanup();
1018
836
1019
836
  LoopStack.pop();
1020
836
1021
836
  // Emit the fall-through block.
1022
836
  EmitBlock(LoopExit.getBlock(), true);
1023
836
}
1024
1025
1.14k
void CodeGenFunction::EmitReturnOfRValue(RValue RV, QualType Ty) {
1026
1.14k
  if (RV.isScalar()) {
1027
1.14k
    Builder.CreateStore(RV.getScalarVal(), ReturnValue);
1028
1.14k
  } else 
if (0
RV.isAggregate()0
) {
1029
0
    LValue Dest = MakeAddrLValue(ReturnValue, Ty);
1030
0
    LValue Src = MakeAddrLValue(RV.getAggregateAddress(), Ty);
1031
0
    EmitAggregateCopy(Dest, Src, Ty, getOverlapForReturnValue());
1032
0
  } else {
1033
0
    EmitStoreOfComplex(RV.getComplexVal(), MakeAddrLValue(ReturnValue, Ty),
1034
0
                       /*init*/ true);
1035
0
  }
1036
1.14k
  EmitBranchThroughCleanup(ReturnBlock);
1037
1.14k
}
1038
1039
/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
1040
/// if the function returns void, or may be missing one if the function returns
1041
/// non-void.  Fun stuff :).
1042
244k
void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
1043
244k
  if (requiresReturnValueCheck()) {
1044
3
    llvm::Constant *SLoc = EmitCheckSourceLocation(S.getBeginLoc());
1045
3
    auto *SLocPtr =
1046
3
        new llvm::GlobalVariable(CGM.getModule(), SLoc->getType(), false,
1047
3
                                 llvm::GlobalVariable::PrivateLinkage, SLoc);
1048
3
    SLocPtr->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1049
3
    CGM.getSanitizerMetadata()->disableSanitizerForGlobal(SLocPtr);
1050
3
    assert(ReturnLocation.isValid() && "No valid return location");
1051
3
    Builder.CreateStore(Builder.CreateBitCast(SLocPtr, Int8PtrTy),
1052
3
                        ReturnLocation);
1053
3
  }
1054
244k
1055
244k
  // Returning from an outlined SEH helper is UB, and we already warn on it.
1056
244k
  if (IsOutlinedSEHHelper) {
1057
8
    Builder.CreateUnreachable();
1058
8
    Builder.ClearInsertionPoint();
1059
8
  }
1060
244k
1061
244k
  // Emit the result value, even if unused, to evaluate the side effects.
1062
244k
  const Expr *RV = S.getRetValue();
1063
244k
1064
244k
  // Treat block literals in a return expression as if they appeared
1065
244k
  // in their own scope.  This permits a small, easily-implemented
1066
244k
  // exception to our over-conservative rules about not jumping to
1067
244k
  // statements following block literals with non-trivial cleanups.
1068
244k
  RunCleanupsScope cleanupScope(*this);
1069
244k
  if (const FullExpr *fe = dyn_cast_or_null<FullExpr>(RV)) {
1070
9.58k
    enterFullExpression(fe);
1071
9.58k
    RV = fe->getSubExpr();
1072
9.58k
  }
1073
244k
1074
244k
  // FIXME: Clean this up by using an LValue for ReturnTemp,
1075
244k
  // EmitStoreThroughLValue, and EmitAnyExpr.
1076
244k
  if (getLangOpts().ElideConstructors &&
1077
244k
      
S.getNRVOCandidate()244k
&&
S.getNRVOCandidate()->isNRVOVariable()1.86k
) {
1078
1.86k
    // Apply the named return value optimization for this return statement,
1079
1.86k
    // which means doing nothing: the appropriate result has already been
1080
1.86k
    // constructed into the NRVO variable.
1081
1.86k
1082
1.86k
    // If there is an NRVO flag for this variable, set it to 1 into indicate
1083
1.86k
    // that the cleanup code should not destroy the variable.
1084
1.86k
    if (llvm::Value *NRVOFlag = NRVOFlags[S.getNRVOCandidate()])
1085
387
      Builder.CreateFlagStore(Builder.getTrue(), NRVOFlag);
1086
242k
  } else if (!ReturnValue.isValid() || 
(230k
RV230k
&&
RV->getType()->isVoidType()230k
)) {
1087
11.5k
    // Make sure not to return anything, but evaluate the expression
1088
11.5k
    // for side effects.
1089
11.5k
    if (RV)
1090
2.40k
      EmitAnyExpr(RV);
1091
230k
  } else if (!RV) {
1092
58
    // Do nothing (return value is left uninitialized)
1093
230k
  } else if (FnRetTy->isReferenceType()) {
1094
30.2k
    // If this function returns a reference, take the address of the expression
1095
30.2k
    // rather than the value.
1096
30.2k
    RValue Result = EmitReferenceBindingToExpr(RV);
1097
30.2k
    Builder.CreateStore(Result.getScalarVal(), ReturnValue);
1098
200k
  } else {
1099
200k
    switch (getEvaluationKind(RV->getType())) {
1100
200k
    case TEK_Scalar:
1101
189k
      Builder.CreateStore(EmitScalarExpr(RV), ReturnValue);
1102
189k
      break;
1103
200k
    case TEK_Complex:
1104
679
      EmitComplexExprIntoLValue(RV, MakeAddrLValue(ReturnValue, RV->getType()),
1105
679
                                /*isInit*/ true);
1106
679
      break;
1107
200k
    case TEK_Aggregate:
1108
10.2k
      EmitAggExpr(RV, AggValueSlot::forAddr(
1109
10.2k
                          ReturnValue, Qualifiers(),
1110
10.2k
                          AggValueSlot::IsDestructed,
1111
10.2k
                          AggValueSlot::DoesNotNeedGCBarriers,
1112
10.2k
                          AggValueSlot::IsNotAliased,
1113
10.2k
                          getOverlapForReturnValue()));
1114
10.2k
      break;
1115
244k
    }
1116
244k
  }
1117
244k
1118
244k
  ++NumReturnExprs;
1119
244k
  if (!RV || 
RV->isEvaluatable(getContext())235k
)
1120
52.0k
    ++NumSimpleReturnExprs;
1121
244k
1122
244k
  cleanupScope.ForceCleanup();
1123
244k
  EmitBranchThroughCleanup(ReturnBlock);
1124
244k
}
1125
1126
368k
void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
1127
368k
  // As long as debug info is modeled with instructions, we have to ensure we
1128
368k
  // have a place to insert here and write the stop point here.
1129
368k
  if (HaveInsertPoint())
1130
368k
    EmitStopPoint(&S);
1131
368k
1132
368k
  for (const auto *I : S.decls())
1133
392k
    EmitDecl(*I);
1134
368k
}
1135
1136
16.7k
void CodeGenFunction::EmitBreakStmt(const BreakStmt &S) {
1137
16.7k
  assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
1138
16.7k
1139
16.7k
  // If this code is reachable then emit a stop point (if generating
1140
16.7k
  // debug info). We have to do this ourselves because we are on the
1141
16.7k
  // "simple" statement path.
1142
16.7k
  if (HaveInsertPoint())
1143
16.3k
    EmitStopPoint(&S);
1144
16.7k
1145
16.7k
  EmitBranchThroughCleanup(BreakContinueStack.back().BreakBlock);
1146
16.7k
}
1147
1148
3.08k
void CodeGenFunction::EmitContinueStmt(const ContinueStmt &S) {
1149
3.08k
  assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
1150
3.08k
1151
3.08k
  // If this code is reachable then emit a stop point (if generating
1152
3.08k
  // debug info). We have to do this ourselves because we are on the
1153
3.08k
  // "simple" statement path.
1154
3.08k
  if (HaveInsertPoint())
1155
3.08k
    EmitStopPoint(&S);
1156
3.08k
1157
3.08k
  EmitBranchThroughCleanup(BreakContinueStack.back().ContinueBlock);
1158
3.08k
}
1159
1160
/// EmitCaseStmtRange - If case statement range is not too big then
1161
/// add multiple cases to switch instruction, one for each value within
1162
/// the range. If range is too big then emit "if" condition check.
1163
35
void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
1164
35
  assert(S.getRHS() && "Expected RHS value in CaseStmt");
1165
35
1166
35
  llvm::APSInt LHS = S.getLHS()->EvaluateKnownConstInt(getContext());
1167
35
  llvm::APSInt RHS = S.getRHS()->EvaluateKnownConstInt(getContext());
1168
35
1169
35
  // Emit the code for this case. We do this first to make sure it is
1170
35
  // properly chained from our predecessor before generating the
1171
35
  // switch machinery to enter this block.
1172
35
  llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb");
1173
35
  EmitBlockWithFallThrough(CaseDest, &S);
1174
35
  EmitStmt(S.getSubStmt());
1175
35
1176
35
  // If range is empty, do nothing.
1177
35
  if (LHS.isSigned() ? 
RHS.slt(LHS)31
:
RHS.ult(LHS)4
)
1178
4
    return;
1179
31
1180
31
  llvm::APInt Range = RHS - LHS;
1181
31
  // FIXME: parameters such as this should not be hardcoded.
1182
31
  if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
1183
18
    // Range is small enough to add multiple switch instruction cases.
1184
18
    uint64_t Total = getProfileCount(&S);
1185
18
    unsigned NCases = Range.getZExtValue() + 1;
1186
18
    // We only have one region counter for the entire set of cases here, so we
1187
18
    // need to divide the weights evenly between the generated cases, ensuring
1188
18
    // that the total weight is preserved. E.g., a weight of 5 over three cases
1189
18
    // will be distributed as weights of 2, 2, and 1.
1190
18
    uint64_t Weight = Total / NCases, Rem = Total % NCases;
1191
83
    for (unsigned I = 0; I != NCases; 
++I65
) {
1192
65
      if (SwitchWeights)
1193
18
        SwitchWeights->push_back(Weight + (Rem ? 
13
:
015
));
1194
65
      if (Rem)
1195
3
        Rem--;
1196
65
      SwitchInsn->addCase(Builder.getInt(LHS), CaseDest);
1197
65
      ++LHS;
1198
65
    }
1199
18
    return;
1200
18
  }
1201
13
1202
13
  // The range is too big. Emit "if" condition into a new block,
1203
13
  // making sure to save and restore the current insertion point.
1204
13
  llvm::BasicBlock *RestoreBB = Builder.GetInsertBlock();
1205
13
1206
13
  // Push this test onto the chain of range checks (which terminates
1207
13
  // in the default basic block). The switch's default will be changed
1208
13
  // to the top of this chain after switch emission is complete.
1209
13
  llvm::BasicBlock *FalseDest = CaseRangeBlock;
1210
13
  CaseRangeBlock = createBasicBlock("sw.caserange");
1211
13
1212
13
  CurFn->getBasicBlockList().push_back(CaseRangeBlock);
1213
13
  Builder.SetInsertPoint(CaseRangeBlock);
1214
13
1215
13
  // Emit range check.
1216
13
  llvm::Value *Diff =
1217
13
    Builder.CreateSub(SwitchInsn->getCondition(), Builder.getInt(LHS));
1218
13
  llvm::Value *Cond =
1219
13
    Builder.CreateICmpULE(Diff, Builder.getInt(Range), "inbounds");
1220
13
1221
13
  llvm::MDNode *Weights = nullptr;
1222
13
  if (SwitchWeights) {
1223
6
    uint64_t ThisCount = getProfileCount(&S);
1224
6
    uint64_t DefaultCount = (*SwitchWeights)[0];
1225
6
    Weights = createProfileWeights(ThisCount, DefaultCount);
1226
6
1227
6
    // Since we're chaining the switch default through each large case range, we
1228
6
    // need to update the weight for the default, ie, the first case, to include
1229
6
    // this case.
1230
6
    (*SwitchWeights)[0] += ThisCount;
1231
6
  }
1232
13
  Builder.CreateCondBr(Cond, CaseDest, FalseDest, Weights);
1233
13
1234
13
  // Restore the appropriate insertion point.
1235
13
  if (RestoreBB)
1236
11
    Builder.SetInsertPoint(RestoreBB);
1237
2
  else
1238
2
    Builder.ClearInsertionPoint();
1239
13
}
1240
1241
17.3k
void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
1242
17.3k
  // If there is no enclosing switch instance that we're aware of, then this
1243
17.3k
  // case statement and its block can be elided.  This situation only happens
1244
17.3k
  // when we've constant-folded the switch, are emitting the constant case,
1245
17.3k
  // and part of the constant case includes another case statement.  For
1246
17.3k
  // instance: switch (4) { case 4: do { case 5: } while (1); }
1247
17.3k
  if (!SwitchInsn) {
1248
2
    EmitStmt(S.getSubStmt());
1249
2
    return;
1250
2
  }
1251
17.3k
1252
17.3k
  // Handle case ranges.
1253
17.3k
  if (S.getRHS()) {
1254
35
    EmitCaseStmtRange(S);
1255
35
    return;
1256
35
  }
1257
17.2k
1258
17.2k
  llvm::ConstantInt *CaseVal =
1259
17.2k
    Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
1260
17.2k
1261
17.2k
  // If the body of the case is just a 'break', try to not emit an empty block.
1262
17.2k
  // If we're profiling or we're not optimizing, leave the block in for better
1263
17.2k
  // debug and coverage analysis.
1264
17.2k
  if (!CGM.getCodeGenOpts().hasProfileClangInstr() &&
1265
17.2k
      
CGM.getCodeGenOpts().OptimizationLevel > 017.2k
&&
1266
17.2k
      
isa<BreakStmt>(S.getSubStmt())17.1k
) {
1267
203
    JumpDest Block = BreakContinueStack.back().BreakBlock;
1268
203
1269
203
    // Only do this optimization if there are no cleanups that need emitting.
1270
203
    if (isObviouslyBranchWithoutCleanups(Block)) {
1271
203
      if (SwitchWeights)
1272
0
        SwitchWeights->push_back(getProfileCount(&S));
1273
203
      SwitchInsn->addCase(CaseVal, Block.getBlock());
1274
203
1275
203
      // If there was a fallthrough into this case, make sure to redirect it to
1276
203
      // the end of the switch as well.
1277
203
      if (Builder.GetInsertBlock()) {
1278
7
        Builder.CreateBr(Block.getBlock());
1279
7
        Builder.ClearInsertionPoint();
1280
7
      }
1281
203
      return;
1282
203
    }
1283
17.0k
  }
1284
17.0k
1285
17.0k
  llvm::BasicBlock *CaseDest = createBasicBlock("sw.bb");
1286
17.0k
  EmitBlockWithFallThrough(CaseDest, &S);
1287
17.0k
  if (SwitchWeights)
1288
37
    SwitchWeights->push_back(getProfileCount(&S));
1289
17.0k
  SwitchInsn->addCase(CaseVal, CaseDest);
1290
17.0k
1291
17.0k
  // Recursively emitting the statement is acceptable, but is not wonderful for
1292
17.0k
  // code where we have many case statements nested together, i.e.:
1293
17.0k
  //  case 1:
1294
17.0k
  //    case 2:
1295
17.0k
  //      case 3: etc.
1296
17.0k
  // Handling this recursively will create a new block for each case statement
1297
17.0k
  // that falls through to the next case which is IR intensive.  It also causes
1298
17.0k
  // deep recursion which can run into stack depth limitations.  Handle
1299
17.0k
  // sequential non-range case statements specially.
1300
17.0k
  const CaseStmt *CurCase = &S;
1301
17.0k
  const CaseStmt *NextCase = dyn_cast<CaseStmt>(S.getSubStmt());
1302
17.0k
1303
17.0k
  // Otherwise, iteratively add consecutive cases to this switch stmt.
1304
20.4k
  while (NextCase && 
NextCase->getRHS() == nullptr3.32k
) {
1305
3.31k
    CurCase = NextCase;
1306
3.31k
    llvm::ConstantInt *CaseVal =
1307
3.31k
      Builder.getInt(CurCase->getLHS()->EvaluateKnownConstInt(getContext()));
1308
3.31k
1309
3.31k
    if (SwitchWeights)
1310
0
      SwitchWeights->push_back(getProfileCount(NextCase));
1311
3.31k
    if (CGM.getCodeGenOpts().hasProfileClangInstr()) {
1312
3
      CaseDest = createBasicBlock("sw.bb");
1313
3
      EmitBlockWithFallThrough(CaseDest, &S);
1314
3
    }
1315
3.31k
1316
3.31k
    SwitchInsn->addCase(CaseVal, CaseDest);
1317
3.31k
    NextCase = dyn_cast<CaseStmt>(CurCase->getSubStmt());
1318
3.31k
  }
1319
17.0k
1320
17.0k
  // Normal default recursion for non-cases.
1321
17.0k
  EmitStmt(CurCase->getSubStmt());
1322
17.0k
}
1323
1324
1.99k
void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
1325
1.99k
  // If there is no enclosing switch instance that we're aware of, then this
1326
1.99k
  // default statement can be elided. This situation only happens when we've
1327
1.99k
  // constant-folded the switch.
1328
1.99k
  if (!SwitchInsn) {
1329
1
    EmitStmt(S.getSubStmt());
1330
1
    return;
1331
1
  }
1332
1.99k
1333
1.99k
  llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
1334
1.99k
  assert(DefaultBlock->empty() &&
1335
1.99k
         "EmitDefaultStmt: Default block already defined?");
1336
1.99k
1337
1.99k
  EmitBlockWithFallThrough(DefaultBlock, &S);
1338
1.99k
1339
1.99k
  EmitStmt(S.getSubStmt());
1340
1.99k
}
1341
1342
/// CollectStatementsForCase - Given the body of a 'switch' statement and a
1343
/// constant value that is being switched on, see if we can dead code eliminate
1344
/// the body of the switch to a simple series of statements to emit.  Basically,
1345
/// on a switch (5) we want to find these statements:
1346
///    case 5:
1347
///      printf(...);    <--
1348
///      ++i;            <--
1349
///      break;
1350
///
1351
/// and add them to the ResultStmts vector.  If it is unsafe to do this
1352
/// transformation (for example, one of the elided statements contains a label
1353
/// that might be jumped to), return CSFC_Failure.  If we handled it and 'S'
1354
/// should include statements after it (e.g. the printf() line is a substmt of
1355
/// the case) then return CSFC_FallThrough.  If we handled it and found a break
1356
/// statement, then return CSFC_Success.
1357
///
1358
/// If Case is non-null, then we are looking for the specified case, checking
1359
/// that nothing we jump over contains labels.  If Case is null, then we found
1360
/// the case and are looking for the break.
1361
///
1362
/// If the recursive walk actually finds our Case, then we set FoundCase to
1363
/// true.
1364
///
1365
enum CSFC_Result { CSFC_Failure, CSFC_FallThrough, CSFC_Success };
1366
static CSFC_Result CollectStatementsForCase(const Stmt *S,
1367
                                            const SwitchCase *Case,
1368
                                            bool &FoundCase,
1369
9.22k
                              SmallVectorImpl<const Stmt*> &ResultStmts) {
1370
9.22k
  // If this is a null statement, just succeed.
1371
9.22k
  if (!S)
1372
0
    return Case ? CSFC_Success : CSFC_FallThrough;
1373
9.22k
1374
9.22k
  // If this is the switchcase (case 4: or default) that we're looking for, then
1375
9.22k
  // we're in business.  Just add the substatement.
1376
9.22k
  if (const SwitchCase *SC = dyn_cast<SwitchCase>(S)) {
1377
4.28k
    if (S == Case) {
1378
410
      FoundCase = true;
1379
410
      return CollectStatementsForCase(SC->getSubStmt(), nullptr, FoundCase,
1380
410
                                      ResultStmts);
1381
410
    }
1382
3.87k
1383
3.87k
    // Otherwise, this is some other case or default statement, just ignore it.
1384
3.87k
    return CollectStatementsForCase(SC->getSubStmt(), Case, FoundCase,
1385
3.87k
                                    ResultStmts);
1386
3.87k
  }
1387
4.93k
1388
4.93k
  // If we are in the live part of the code and we found our break statement,
1389
4.93k
  // return a success!
1390
4.93k
  if (!Case && 
isa<BreakStmt>(S)2.36k
)
1391
92
    return CSFC_Success;
1392
4.84k
1393
4.84k
  // If this is a switch statement, then it might contain the SwitchCase, the
1394
4.84k
  // break, or neither.
1395
4.84k
  if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
1396
415
    // Handle this as two cases: we might be looking for the SwitchCase (if so
1397
415
    // the skipped statements must be skippable) or we might already have it.
1398
415
    CompoundStmt::const_body_iterator I = CS->body_begin(), E = CS->body_end();
1399
415
    bool StartedInLiveCode = FoundCase;
1400
415
    unsigned StartSize = ResultStmts.size();
1401
415
1402
415
    // If we've not found the case yet, scan through looking for it.
1403
415
    if (Case) {
1404
408
      // Keep track of whether we see a skipped declaration.  The code could be
1405
408
      // using the declaration even if it is skipped, so we can't optimize out
1406
408
      // the decl if the kept statements might refer to it.
1407
408
      bool HadSkippedDecl = false;
1408
408
1409
408
      // If we're looking for the case, just see if we can skip each of the
1410
408
      // substatements.
1411
2.96k
      for (; Case && 
I != E2.57k
;
++I2.55k
) {
1412
2.57k
        HadSkippedDecl |= CodeGenFunction::mightAddDeclToScope(*I);
1413
2.57k
1414
2.57k
        switch (CollectStatementsForCase(*I, Case, FoundCase, ResultStmts)) {
1415
2.57k
        
case CSFC_Failure: return CSFC_Failure2
;
1416
2.57k
        case CSFC_Success:
1417
2.17k
          // A successful result means that either 1) that the statement doesn't
1418
2.17k
          // have the case and is skippable, or 2) does contain the case value
1419
2.17k
          // and also contains the break to exit the switch.  In the later case,
1420
2.17k
          // we just verify the rest of the statements are elidable.
1421
2.17k
          if (FoundCase) {
1422
8
            // If we found the case and skipped declarations, we can't do the
1423
8
            // optimization.
1424
8
            if (HadSkippedDecl)
1425
0
              return CSFC_Failure;
1426
8
1427
18
            
for (++I; 8
I != E;
++I10
)
1428
10
              if (CodeGenFunction::ContainsLabel(*I, true))
1429
0
                return CSFC_Failure;
1430
8
            return CSFC_Success;
1431
2.16k
          }
1432
2.16k
          break;
1433
2.16k
        case CSFC_FallThrough:
1434
397
          // If we have a fallthrough condition, then we must have found the
1435
397
          // case started to include statements.  Consider the rest of the
1436
397
          // statements in the compound statement as candidates for inclusion.
1437
397
          assert(FoundCase && "Didn't find case but returned fallthrough?");
1438
397
          // We recursively found Case, so we're not looking for it anymore.
1439
397
          Case = nullptr;
1440
397
1441
397
          // If we found the case and skipped declarations, we can't do the
1442
397
          // optimization.
1443
397
          if (HadSkippedDecl)
1444
3
            return CSFC_Failure;
1445
394
          break;
1446
2.57k
        }
1447
2.57k
      }
1448
408
1449
408
      
if (395
!FoundCase395
)
1450
1
        return CSFC_Success;
1451
394
1452
394
      assert(!HadSkippedDecl && "fallthrough after skipping decl");
1453
394
    }
1454
415
1455
415
    // If we have statements in our range, then we know that the statements are
1456
415
    // live and need to be added to the set of statements we're tracking.
1457
415
    bool AnyDecls = false;
1458
2.26k
    for (; I != E; 
++I1.86k
) {
1459
1.95k
      AnyDecls |= CodeGenFunction::mightAddDeclToScope(*I);
1460
1.95k
1461
1.95k
      switch (CollectStatementsForCase(*I, nullptr, FoundCase, ResultStmts)) {
1462
1.95k
      
case CSFC_Failure: return CSFC_Failure0
;
1463
1.95k
      case CSFC_FallThrough:
1464
1.86k
        // A fallthrough result means that the statement was simple and just
1465
1.86k
        // included in ResultStmt, keep adding them afterwards.
1466
1.86k
        break;
1467
1.95k
      case CSFC_Success:
1468
85
        // A successful result means that we found the break statement and
1469
85
        // stopped statement inclusion.  We just ensure that any leftover stmts
1470
85
        // are skippable and return success ourselves.
1471
343
        for (++I; I != E; 
++I258
)
1472
258
          if (CodeGenFunction::ContainsLabel(*I, true))
1473
0
            return CSFC_Failure;
1474
85
        return CSFC_Success;
1475
1.95k
      }
1476
1.95k
    }
1477
401
1478
401
    // If we're about to fall out of a scope without hitting a 'break;', we
1479
401
    // can't perform the optimization if there were any decls in that scope
1480
401
    // (we'd lose their end-of-lifetime).
1481
401
    
if (316
AnyDecls316
) {
1482
3
      // If the entire compound statement was live, there's one more thing we
1483
3
      // can try before giving up: emit the whole thing as a single statement.
1484
3
      // We can do that unless the statement contains a 'break;'.
1485
3
      // FIXME: Such a break must be at the end of a construct within this one.
1486
3
      // We could emit this by just ignoring the BreakStmts entirely.
1487
3
      if (StartedInLiveCode && !CodeGenFunction::containsBreak(S)) {
1488
3
        ResultStmts.resize(StartSize);
1489
3
        ResultStmts.push_back(S);
1490
3
      } else {
1491
0
        return CSFC_Failure;
1492
0
      }
1493
316
    }
1494
316
1495
316
    return CSFC_FallThrough;
1496
316
  }
1497
4.42k
1498
4.42k
  // Okay, this is some other statement that we don't handle explicitly, like a
1499
4.42k
  // for statement or increment etc.  If we are skipping over this statement,
1500
4.42k
  // just verify it doesn't have labels, which would make it invalid to elide.
1501
4.42k
  if (Case) {
1502
2.16k
    if (CodeGenFunction::ContainsLabel(S, true))
1503
0
      return CSFC_Failure;
1504
2.16k
    return CSFC_Success;
1505
2.16k
  }
1506
2.26k
1507
2.26k
  // Otherwise, we want to include this statement.  Everything is cool with that
1508
2.26k
  // so long as it doesn't contain a break out of the switch we're in.
1509
2.26k
  if (CodeGenFunction::containsBreak(S)) 
return CSFC_Failure1
;
1510
2.26k
1511
2.26k
  // Otherwise, everything is great.  Include the statement and tell the caller
1512
2.26k
  // that we fall through and include the next statement as well.
1513
2.26k
  ResultStmts.push_back(S);
1514
2.26k
  return CSFC_FallThrough;
1515
2.26k
}
1516
1517
/// FindCaseStatementsForValue - Find the case statement being jumped to and
1518
/// then invoke CollectStatementsForCase to find the list of statements to emit
1519
/// for a switch on constant.  See the comment above CollectStatementsForCase
1520
/// for more details.
1521
static bool FindCaseStatementsForValue(const SwitchStmt &S,
1522
                                       const llvm::APSInt &ConstantCondValue,
1523
                                SmallVectorImpl<const Stmt*> &ResultStmts,
1524
                                       ASTContext &C,
1525
426
                                       const SwitchCase *&ResultCase) {
1526
426
  // First step, find the switch case that is being branched to.  We can do this
1527
426
  // efficiently by scanning the SwitchCase list.
1528
426
  const SwitchCase *Case = S.getSwitchCaseList();
1529
426
  const DefaultStmt *DefaultCase = nullptr;
1530
426
1531
2.57k
  for (; Case; 
Case = Case->getNextSwitchCase()2.15k
) {
1532
2.51k
    // It's either a default or case.  Just remember the default statement in
1533
2.51k
    // case we're not jumping to any numbered cases.
1534
2.51k
    if (const DefaultStmt *DS = dyn_cast<DefaultStmt>(Case)) {
1535
128
      DefaultCase = DS;
1536
128
      continue;
1537
128
    }
1538
2.38k
1539
2.38k
    // Check to see if this case is the one we're looking for.
1540
2.38k
    const CaseStmt *CS = cast<CaseStmt>(Case);
1541
2.38k
    // Don't handle case ranges yet.
1542
2.38k
    if (CS->getRHS()) 
return false7
;
1543
2.37k
1544
2.37k
    // If we found our case, remember it as 'case'.
1545
2.37k
    if (CS->getLHS()->EvaluateKnownConstInt(C) == ConstantCondValue)
1546
355
      break;
1547
2.37k
  }
1548
426
1549
426
  // If we didn't find a matching case, we use a default if it exists, or we
1550
426
  // elide the whole switch body!
1551
426
  
if (419
!Case419
) {
1552
64
    // It is safe to elide the body of the switch if it doesn't contain labels
1553
64
    // etc.  If it is safe, return successfully with an empty ResultStmts list.
1554
64
    if (!DefaultCase)
1555
9
      return !CodeGenFunction::ContainsLabel(&S);
1556
55
    Case = DefaultCase;
1557
55
  }
1558
419
1559
419
  // Ok, we know which case is being jumped to, try to collect all the
1560
419
  // statements that follow it.  This can fail for a variety of reasons.  Also,
1561
419
  // check to see that the recursive walk actually found our case statement.
1562
419
  // Insane cases like this can fail to find it in the recursive walk since we
1563
419
  // don't handle every stmt kind:
1564
419
  // switch (4) {
1565
419
  //   while (1) {
1566
419
  //     case 4: ...
1567
419
  bool FoundCase = false;
1568
410
  ResultCase = Case;
1569
410
  return CollectStatementsForCase(S.getBody(), Case, FoundCase,
1570
410
                                  ResultStmts) != CSFC_Failure &&
1571
410
         
FoundCase406
;
1572
419
}
1573
1574
3.80k
void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
1575
3.80k
  // Handle nested switch statements.
1576
3.80k
  llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
1577
3.80k
  SmallVector<uint64_t, 16> *SavedSwitchWeights = SwitchWeights;
1578
3.80k
  llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
1579
3.80k
1580
3.80k
  // See if we can constant fold the condition of the switch and therefore only
1581
3.80k
  // emit the live case statement (if any) of the switch.
1582
3.80k
  llvm::APSInt ConstantCondValue;
1583
3.80k
  if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue)) {
1584
426
    SmallVector<const Stmt*, 4> CaseStmts;
1585
426
    const SwitchCase *Case = nullptr;
1586
426
    if (FindCaseStatementsForValue(S, ConstantCondValue, CaseStmts,
1587
426
                                   getContext(), Case)) {
1588
415
      if (Case)
1589
406
        incrementProfileCounter(Case);
1590
415
      RunCleanupsScope ExecutedScope(*this);
1591
415
1592
415
      if (S.getInit())
1593
0
        EmitStmt(S.getInit());
1594
415
1595
415
      // Emit the condition variable if needed inside the entire cleanup scope
1596
415
      // used by this special case for constant folded switches.
1597
415
      if (S.getConditionVariable())
1598
0
        EmitDecl(*S.getConditionVariable());
1599
415
1600
415
      // At this point, we are no longer "within" a switch instance, so
1601
415
      // we can temporarily enforce this to ensure that any embedded case
1602
415
      // statements are not emitted.
1603
415
      SwitchInsn = nullptr;
1604
415
1605
415
      // Okay, we can dead code eliminate everything except this case.  Emit the
1606
415
      // specified series of statements and we're good.
1607
2.67k
      for (unsigned i = 0, e = CaseStmts.size(); i != e; 
++i2.25k
)
1608
2.25k
        EmitStmt(CaseStmts[i]);
1609
415
      incrementProfileCounter(&S);
1610
415
1611
415
      // Now we want to restore the saved switch instance so that nested
1612
415
      // switches continue to function properly
1613
415
      SwitchInsn = SavedSwitchInsn;
1614
415
1615
415
      return;
1616
415
    }
1617
3.38k
  }
1618
3.38k
1619
3.38k
  JumpDest SwitchExit = getJumpDestInCurrentScope("sw.epilog");
1620
3.38k
1621
3.38k
  RunCleanupsScope ConditionScope(*this);
1622
3.38k
1623
3.38k
  if (S.getInit())
1624
7
    EmitStmt(S.getInit());
1625
3.38k
1626
3.38k
  if (S.getConditionVariable())
1627
4
    EmitDecl(*S.getConditionVariable());
1628
3.38k
  llvm::Value *CondV = EmitScalarExpr(S.getCond());
1629
3.38k
1630
3.38k
  // Create basic block to hold stuff that comes after switch
1631
3.38k
  // statement. We also need to create a default block now so that
1632
3.38k
  // explicit case ranges tests can have a place to jump to on
1633
3.38k
  // failure.
1634
3.38k
  llvm::BasicBlock *DefaultBlock = createBasicBlock("sw.default");
1635
3.38k
  SwitchInsn = Builder.CreateSwitch(CondV, DefaultBlock);
1636
3.38k
  if (PGO.haveRegionCounts()) {
1637
18
    // Walk the SwitchCase list to find how many there are.
1638
18
    uint64_t DefaultCount = 0;
1639
18
    unsigned NumCases = 0;
1640
18
    for (const SwitchCase *Case = S.getSwitchCaseList();
1641
79
         Case;
1642
61
         Case = Case->getNextSwitchCase()) {
1643
61
      if (isa<DefaultStmt>(Case))
1644
12
        DefaultCount = getProfileCount(Case);
1645
61
      NumCases += 1;
1646
61
    }
1647
18
    SwitchWeights = new SmallVector<uint64_t, 16>();
1648
18
    SwitchWeights->reserve(NumCases);
1649
18
    // The default needs to be first. We store the edge count, so we already
1650
18
    // know the right weight.
1651
18
    SwitchWeights->push_back(DefaultCount);
1652
18
  }
1653
3.38k
  CaseRangeBlock = DefaultBlock;
1654
3.38k
1655
3.38k
  // Clear the insertion point to indicate we are in unreachable code.
1656
3.38k
  Builder.ClearInsertionPoint();
1657
3.38k
1658
3.38k
  // All break statements jump to NextBlock. If BreakContinueStack is non-empty
1659
3.38k
  // then reuse last ContinueBlock.
1660
3.38k
  JumpDest OuterContinue;
1661
3.38k
  if (!BreakContinueStack.empty())
1662
825
    OuterContinue = BreakContinueStack.back().ContinueBlock;
1663
3.38k
1664
3.38k
  BreakContinueStack.push_back(BreakContinue(SwitchExit, OuterContinue));
1665
3.38k
1666
3.38k
  // Emit switch body.
1667
3.38k
  EmitStmt(S.getBody());
1668
3.38k
1669
3.38k
  BreakContinueStack.pop_back();
1670
3.38k
1671
3.38k
  // Update the default block in case explicit case range tests have
1672
3.38k
  // been chained on top.
1673
3.38k
  SwitchInsn->setDefaultDest(CaseRangeBlock);
1674
3.38k
1675
3.38k
  // If a default was never emitted:
1676
3.38k
  if (!DefaultBlock->getParent()) {
1677
1.39k
    // If we have cleanups, emit the default block so that there's a
1678
1.39k
    // place to jump through the cleanups from.
1679
1.39k
    if (ConditionScope.requiresCleanups()) {
1680
0
      EmitBlock(DefaultBlock);
1681
0
1682
0
    // Otherwise, just forward the default block to the switch end.
1683
1.39k
    } else {
1684
1.39k
      DefaultBlock->replaceAllUsesWith(SwitchExit.getBlock());
1685
1.39k
      delete DefaultBlock;
1686
1.39k
    }
1687
1.39k
  }
1688
3.38k
1689
3.38k
  ConditionScope.ForceCleanup();
1690
3.38k
1691
3.38k
  // Emit continuation.
1692
3.38k
  EmitBlock(SwitchExit.getBlock(), true);
1693
3.38k
  incrementProfileCounter(&S);
1694
3.38k
1695
3.38k
  // If the switch has a condition wrapped by __builtin_unpredictable,
1696
3.38k
  // create metadata that specifies that the switch is unpredictable.
1697
3.38k
  // Don't bother if not optimizing because that metadata would not be used.
1698
3.38k
  auto *Call = dyn_cast<CallExpr>(S.getCond());
1699
3.38k
  if (Call && 
CGM.getCodeGenOpts().OptimizationLevel != 0186
) {
1700
182
    auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl());
1701
182
    if (FD && 
FD->getBuiltinID() == Builtin::BI__builtin_unpredictable181
) {
1702
2
      llvm::MDBuilder MDHelper(getLLVMContext());
1703
2
      SwitchInsn->setMetadata(llvm::LLVMContext::MD_unpredictable,
1704
2
                              MDHelper.createUnpredictable());
1705
2
    }
1706
182
  }
1707
3.38k
1708
3.38k
  if (SwitchWeights) {
1709
18
    assert(SwitchWeights->size() == 1 + SwitchInsn->getNumCases() &&
1710
18
           "switch weights do not match switch cases");
1711
18
    // If there's only one jump destination there's no sense weighting it.
1712
18
    if (SwitchWeights->size() > 1)
1713
15
      SwitchInsn->setMetadata(llvm::LLVMContext::MD_prof,
1714
15
                              createProfileWeights(*SwitchWeights));
1715
18
    delete SwitchWeights;
1716
18
  }
1717
3.38k
  SwitchInsn = SavedSwitchInsn;
1718
3.38k
  SwitchWeights = SavedSwitchWeights;
1719
3.38k
  CaseRangeBlock = SavedCRBlock;
1720
3.38k
}
1721
1722
static std::string
1723
SimplifyConstraint(const char *Constraint, const TargetInfo &Target,
1724
2.32k
                 SmallVectorImpl<TargetInfo::ConstraintInfo> *OutCons=nullptr) {
1725
2.32k
  std::string Result;
1726
2.32k
1727
6.00k
  while (*Constraint) {
1728
3.68k
    switch (*Constraint) {
1729
3.68k
    default:
1730
2.95k
      Result += Target.convertConstraint(Constraint);
1731
2.95k
      break;
1732
3.68k
    // Ignore these
1733
3.68k
    case '*':
1734
3
    case '?':
1735
3
    case '!':
1736
3
    case '=': // Will see this and the following in mult-alt constraints.
1737
3
    case '+':
1738
3
      break;
1739
3
    case '#': // Ignore the rest of the constraint alternative.
1740
3
      while (Constraint[1] && Constraint[1] != ',')
1741
2
        Constraint++;
1742
1
      break;
1743
16
    case '&':
1744
16
    case '%':
1745
16
      Result += *Constraint;
1746
18
      while (Constraint[1] && Constraint[1] == *Constraint)
1747
2
        Constraint++;
1748
16
      break;
1749
621
    case ',':
1750
621
      Result += "|";
1751
621
      break;
1752
92
    case 'g':
1753
92
      Result += "imr";
1754
92
      break;
1755
16
    case '[': {
1756
0
      assert(OutCons &&
1757
0
             "Must pass output names to constraints with a symbolic name");
1758
0
      unsigned Index;
1759
0
      bool result = Target.resolveSymbolicName(Constraint, *OutCons, Index);
1760
0
      assert(result && "Could not resolve symbolic name"); (void)result;
1761
0
      Result += llvm::utostr(Index);
1762
0
      break;
1763
3.68k
    }
1764
3.68k
    }
1765
3.68k
1766
3.68k
    Constraint++;
1767
3.68k
  }
1768
2.32k
1769
2.32k
  return Result;
1770
2.32k
}
1771
1772
/// AddVariableConstraints - Look at AsmExpr and if it is a variable declared
1773
/// as using a particular register add that as a constraint that will be used
1774
/// in this asm stmt.
1775
static std::string
1776
AddVariableConstraints(const std::string &Constraint, const Expr &AsmExpr,
1777
                       const TargetInfo &Target, CodeGenModule &CGM,
1778
2.32k
                       const AsmStmt &Stmt, const bool EarlyClobber) {
1779
2.32k
  const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(&AsmExpr);
1780
2.32k
  if (!AsmDeclRef)
1781
597
    return Constraint;
1782
1.72k
  const ValueDecl &Value = *AsmDeclRef->getDecl();
1783
1.72k
  const VarDecl *Variable = dyn_cast<VarDecl>(&Value);
1784
1.72k
  if (!Variable)
1785
9
    return Constraint;
1786
1.71k
  if (Variable->getStorageClass() != SC_Register)
1787
1.08k
    return Constraint;
1788
636
  AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>();
1789
636
  if (!Attr)
1790
575
    return Constraint;
1791
61
  StringRef Register = Attr->getLabel();
1792
61
  assert(Target.isValidGCCRegisterName(Register));
1793
61
  // We're using validateOutputConstraint here because we only care if
1794
61
  // this is a register constraint.
1795
61
  TargetInfo::ConstraintInfo Info(Constraint, "");
1796
61
  if (Target.validateOutputConstraint(Info) &&
1797
61
      
!Info.allowsRegister()0
) {
1798
0
    CGM.ErrorUnsupported(&Stmt, "__asm__");
1799
0
    return Constraint;
1800
0
  }
1801
61
  // Canonicalize the register here before returning it.
1802
61
  Register = Target.getNormalizedGCCRegisterName(Register);
1803
61
  return (EarlyClobber ? 
"&{"4
:
"{"57
) + Register.str() + "}";
1804
61
}
1805
1806
llvm::Value*
1807
CodeGenFunction::EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
1808
                                    LValue InputValue, QualType InputType,
1809
                                    std::string &ConstraintStr,
1810
262
                                    SourceLocation Loc) {
1811
262
  llvm::Value *Arg;
1812
262
  if (Info.allowsRegister() || 
!Info.allowsMemory()151
) {
1813
111
    if (CodeGenFunction::hasScalarEvaluationKind(InputType)) {
1814
105
      Arg = EmitLoadOfLValue(InputValue, Loc).getScalarVal();
1815
105
    } else {
1816
6
      llvm::Type *Ty = ConvertType(InputType);
1817
6
      uint64_t Size = CGM.getDataLayout().getTypeSizeInBits(Ty);
1818
6
      if (Size <= 64 && 
llvm::isPowerOf2_64(Size)5
) {
1819
5
        Ty = llvm::IntegerType::get(getLLVMContext(), Size);
1820
5
        Ty = llvm::PointerType::getUnqual(Ty);
1821
5
1822
5
        Arg = Builder.CreateLoad(Builder.CreateBitCast(InputValue.getAddress(),
1823
5
                                                       Ty));
1824
5
      } else {
1825
1
        Arg = InputValue.getPointer();
1826
1
        ConstraintStr += '*';
1827
1
      }
1828
6
    }
1829
151
  } else {
1830
151
    Arg = InputValue.getPointer();
1831
151
    ConstraintStr += '*';
1832
151
  }
1833
262
1834
262
  return Arg;
1835
262
}
1836
1837
llvm::Value* CodeGenFunction::EmitAsmInput(
1838
                                         const TargetInfo::ConstraintInfo &Info,
1839
                                           const Expr *InputExpr,
1840
1.21k
                                           std::string &ConstraintStr) {
1841
1.21k
  // If this can't be a register or memory, i.e., has to be a constant
1842
1.21k
  // (immediate or symbolic), try to emit it as such.
1843
1.21k
  if (!Info.allowsRegister() && 
!Info.allowsMemory()261
) {
1844
122
    if (Info.requiresImmediateConstant()) {
1845
57
      Expr::EvalResult EVResult;
1846
57
      InputExpr->EvaluateAsRValue(EVResult, getContext(), true);
1847
57
1848
57
      llvm::APSInt IntResult;
1849
57
      if (!EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(),
1850
57
                                           getContext()))
1851
57
        
llvm_unreachable0
("Invalid immediate constant!");
1852
57
1853
57
      return llvm::ConstantInt::get(getLLVMContext(), IntResult);
1854
65
    }
1855
65
1856
65
    Expr::EvalResult Result;
1857
65
    if (InputExpr->EvaluateAsInt(Result, getContext()))
1858
24
      return llvm::ConstantInt::get(getLLVMContext(), Result.Val.getInt());
1859
1.13k
  }
1860
1.13k
1861
1.13k
  if (Info.allowsRegister() || 
!Info.allowsMemory()180
)
1862
999
    if (CodeGenFunction::hasScalarEvaluationKind(InputExpr->getType()))
1863
993
      return EmitScalarExpr(InputExpr);
1864
145
  if (InputExpr->getStmtClass() == Expr::CXXThisExprClass)
1865
3
    return EmitScalarExpr(InputExpr);
1866
142
  InputExpr = InputExpr->IgnoreParenNoopCasts(getContext());
1867
142
  LValue Dest = EmitLValue(InputExpr);
1868
142
  return EmitAsmInputLValue(Info, Dest, InputExpr->getType(), ConstraintStr,
1869
142
                            InputExpr->getExprLoc());
1870
142
}
1871
1872
/// getAsmSrcLocInfo - Return the !srcloc metadata node to attach to an inline
1873
/// asm call instruction.  The !srcloc MDNode contains a list of constant
1874
/// integers which are the source locations of the start of each line in the
1875
/// asm.
1876
static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
1877
8.04k
                                      CodeGenFunction &CGF) {
1878
8.04k
  SmallVector<llvm::Metadata *, 8> Locs;
1879
8.04k
  // Add the location of the first line to the MDNode.
1880
8.04k
  Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1881
8.04k
      CGF.Int32Ty, Str->getBeginLoc().getRawEncoding())));
1882
8.04k
  StringRef StrVal = Str->getString();
1883
8.04k
  if (!StrVal.empty()) {
1884
1.34k
    const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
1885
1.34k
    const LangOptions &LangOpts = CGF.CGM.getLangOpts();
1886
1.34k
    unsigned StartToken = 0;
1887
1.34k
    unsigned ByteOffset = 0;
1888
1.34k
1889
1.34k
    // Add the location of the start of each subsequent line of the asm to the
1890
1.34k
    // MDNode.
1891
15.6k
    for (unsigned i = 0, e = StrVal.size() - 1; i != e; 
++i14.3k
) {
1892
14.3k
      if (StrVal[i] != '\n') 
continue14.2k
;
1893
118
      SourceLocation LineLoc = Str->getLocationOfByte(
1894
118
          i + 1, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
1895
118
      Locs.push_back(llvm::ConstantAsMetadata::get(
1896
118
          llvm::ConstantInt::get(CGF.Int32Ty, LineLoc.getRawEncoding())));
1897
118
    }
1898
1.34k
  }
1899
8.04k
1900
8.04k
  return llvm::MDNode::get(CGF.getLLVMContext(), Locs);
1901
8.04k
}
1902
1903
static void UpdateAsmCallInst(llvm::CallBase &Result, bool HasSideEffect,
1904
                              bool ReadOnly, bool ReadNone, const AsmStmt &S,
1905
                              const std::vector<llvm::Type *> &ResultRegTypes,
1906
                              CodeGenFunction &CGF,
1907
8.19k
                              std::vector<llvm::Value *> &RegResults) {
1908
8.19k
  Result.addAttribute(llvm::AttributeList::FunctionIndex,
1909
8.19k
                      llvm::Attribute::NoUnwind);
1910
8.19k
  // Attach readnone and readonly attributes.
1911
8.19k
  if (!HasSideEffect) {
1912
745
    if (ReadNone)
1913
299
      Result.addAttribute(llvm::AttributeList::FunctionIndex,
1914
299
                          llvm::Attribute::ReadNone);
1915
446
    else if (ReadOnly)
1916
314
      Result.addAttribute(llvm::AttributeList::FunctionIndex,
1917
314
                          llvm::Attribute::ReadOnly);
1918
745
  }
1919
8.19k
1920
8.19k
  // Slap the source location of the inline asm into a !srcloc metadata on the
1921
8.19k
  // call.
1922
8.19k
  if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(&S))
1923
8.04k
    Result.setMetadata("srcloc",
1924
8.04k
                       getAsmSrcLocInfo(gccAsmStmt->getAsmString(), CGF));
1925
153
  else {
1926
153
    // At least put the line number on MS inline asm blobs.
1927
153
    llvm::Constant *Loc = llvm::ConstantInt::get(CGF.Int32Ty,
1928
153
                                        S.getAsmLoc().getRawEncoding());
1929
153
    Result.setMetadata("srcloc",
1930
153
                       llvm::MDNode::get(CGF.getLLVMContext(),
1931
153
                                         llvm::ConstantAsMetadata::get(Loc)));
1932
153
  }
1933
8.19k
1934
8.19k
  if (CGF.getLangOpts().assumeFunctionsAreConvergent())
1935
8
    // Conservatively, mark all inline asm blocks in CUDA or OpenCL as
1936
8
    // convergent (meaning, they may call an intrinsically convergent op, such
1937
8
    // as bar.sync, and so can't have certain optimizations applied around
1938
8
    // them).
1939
8
    Result.addAttribute(llvm::AttributeList::FunctionIndex,
1940
8
                        llvm::Attribute::Convergent);
1941
8.19k
  // Extract all of the register value results from the asm.
1942
8.19k
  if (ResultRegTypes.size() == 1) {
1943
681
    RegResults.push_back(&Result);
1944
7.51k
  } else {
1945
7.78k
    for (unsigned i = 0, e = ResultRegTypes.size(); i != e; 
++i274
) {
1946
274
      llvm::Value *Tmp = CGF.Builder.CreateExtractValue(&Result, i, "asmresult");
1947
274
      RegResults.push_back(Tmp);
1948
274
    }
1949
7.51k
  }
1950
8.19k
}
1951
1952
8.19k
void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) {
1953
8.19k
  // Assemble the final asm string.
1954
8.19k
  std::string AsmString = S.generateAsmString(getContext());
1955
8.19k
1956
8.19k
  // Get all the output and input constraints together.
1957
8.19k
  SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
1958
8.19k
  SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
1959
8.19k
1960
9.29k
  for (unsigned i = 0, e = S.getNumOutputs(); i != e; 
i++1.10k
) {
1961
1.10k
    StringRef Name;
1962
1.10k
    if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S))
1963
1.08k
      Name = GAS->getOutputName(i);
1964
1.10k
    TargetInfo::ConstraintInfo Info(S.getOutputConstraint(i), Name);
1965
1.10k
    bool IsValid = getTarget().validateOutputConstraint(Info); (void)IsValid;
1966
1.10k
    assert(IsValid && "Failed to parse output constraint");
1967
1.10k
    OutputConstraintInfos.push_back(Info);
1968
1.10k
  }
1969
8.19k
1970
9.41k
  for (unsigned i = 0, e = S.getNumInputs(); i != e; 
i++1.21k
) {
1971
1.21k
    StringRef Name;
1972
1.21k
    if (const GCCAsmStmt *GAS = dyn_cast<GCCAsmStmt>(&S))
1973
1.11k
      Name = GAS->getInputName(i);
1974
1.21k
    TargetInfo::ConstraintInfo Info(S.getInputConstraint(i), Name);
1975
1.21k
    bool IsValid =
1976
1.21k
      getTarget().validateInputConstraint(OutputConstraintInfos, Info);
1977
1.21k
    assert(IsValid && "Failed to parse input constraint"); (void)IsValid;
1978
1.21k
    InputConstraintInfos.push_back(Info);
1979
1.21k
  }
1980
8.19k
1981
8.19k
  std::string Constraints;
1982
8.19k
1983
8.19k
  std::vector<LValue> ResultRegDests;
1984
8.19k
  std::vector<QualType> ResultRegQualTys;
1985
8.19k
  std::vector<llvm::Type *> ResultRegTypes;
1986
8.19k
  std::vector<llvm::Type *> ResultTruncRegTypes;
1987
8.19k
  std::vector<llvm::Type *> ArgTypes;
1988
8.19k
  std::vector<llvm::Value*> Args;
1989
8.19k
1990
8.19k
  // Keep track of inout constraints.
1991
8.19k
  std::string InOutConstraints;
1992
8.19k
  std::vector<llvm::Value*> InOutArgs;
1993
8.19k
  std::vector<llvm::Type*> InOutArgTypes;
1994
8.19k
1995
8.19k
  // Keep track of out constraints for tied input operand.
1996
8.19k
  std::vector<std::string> OutputConstraints;
1997
8.19k
1998
8.19k
  // An inline asm can be marked readonly if it meets the following conditions:
1999
8.19k
  //  - it doesn't have any sideeffects
2000
8.19k
  //  - it doesn't clobber memory
2001
8.19k
  //  - it doesn't return a value by-reference
2002
8.19k
  // It can be marked readnone if it doesn't have any input memory constraints
2003
8.19k
  // in addition to meeting the conditions listed above.
2004
8.19k
  bool ReadOnly = true, ReadNone = true;
2005
8.19k
2006
9.29k
  for (unsigned i = 0, e = S.getNumOutputs(); i != e; 
i++1.10k
) {
2007
1.10k
    TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
2008
1.10k
2009
1.10k
    // Simplify the output constraint.
2010
1.10k
    std::string OutputConstraint(S.getOutputConstraint(i));
2011
1.10k
    OutputConstraint = SimplifyConstraint(OutputConstraint.c_str() + 1,
2012
1.10k
                                          getTarget(), &OutputConstraintInfos);
2013
1.10k
2014
1.10k
    const Expr *OutExpr = S.getOutputExpr(i);
2015
1.10k
    OutExpr = OutExpr->IgnoreParenNoopCasts(getContext());
2016
1.10k
2017
1.10k
    OutputConstraint = AddVariableConstraints(OutputConstraint, *OutExpr,
2018
1.10k
                                              getTarget(), CGM, S,
2019
1.10k
                                              Info.earlyClobber());
2020
1.10k
    OutputConstraints.push_back(OutputConstraint);
2021
1.10k
    LValue Dest = EmitLValue(OutExpr);
2022
1.10k
    if (!Constraints.empty())
2023
236
      Constraints += ',';
2024
1.10k
2025
1.10k
    // If this is a register output, then make the inline asm return it
2026
1.10k
    // by-value.  If this is a memory result, return the value by-reference.
2027
1.10k
    if (!Info.allowsMemory() && 
hasScalarEvaluationKind(OutExpr->getType())936
) {
2028
932
      Constraints += "=" + OutputConstraint;
2029
932
      ResultRegQualTys.push_back(OutExpr->getType());
2030
932
      ResultRegDests.push_back(Dest);
2031
932
      ResultRegTypes.push_back(ConvertTypeForMem(OutExpr->getType()));
2032
932
      ResultTruncRegTypes.push_back(ResultRegTypes.back());
2033
932
2034
932
      // If this output is tied to an input, and if the input is larger, then
2035
932
      // we need to set the actual result type of the inline asm node to be the
2036
932
      // same as the input type.
2037
932
      if (Info.hasMatchingInput()) {
2038
33
        unsigned InputNo;
2039
36
        for (InputNo = 0; InputNo != S.getNumInputs(); 
++InputNo3
) {
2040
36
          TargetInfo::ConstraintInfo &Input = InputConstraintInfos[InputNo];
2041
36
          if (Input.hasTiedOperand() && 
Input.getTiedOperand() == i35
)
2042
33
            break;
2043
36
        }
2044
33
        assert(InputNo != S.getNumInputs() && "Didn't find matching input!");
2045
33
2046
33
        QualType InputTy = S.getInputExpr(InputNo)->getType();
2047
33
        QualType OutputType = OutExpr->getType();
2048
33
2049
33
        uint64_t InputSize = getContext().getTypeSize(InputTy);
2050
33
        if (getContext().getTypeSize(OutputType) < InputSize) {
2051
4
          // Form the asm to return the value as a larger integer or fp type.
2052
4
          ResultRegTypes.back() = ConvertType(InputTy);
2053
4
        }
2054
33
      }
2055
932
      if (llvm::Type* AdjTy =
2056
932
            getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
2057
932
                                                 ResultRegTypes.back()))
2058
932
        ResultRegTypes.back() = AdjTy;
2059
0
      else {
2060
0
        CGM.getDiags().Report(S.getAsmLoc(),
2061
0
                              diag::err_asm_invalid_type_in_input)
2062
0
            << OutExpr->getType() << OutputConstraint;
2063
0
      }
2064
932
2065
932
      // Update largest vector width for any vector types.
2066
932
      if (auto *VT = dyn_cast<llvm::VectorType>(ResultRegTypes.back()))
2067
51
        LargestVectorWidth = std::max(LargestVectorWidth,
2068
51
                                      VT->getPrimitiveSizeInBits());
2069
932
    } else {
2070
172
      ArgTypes.push_back(Dest.getAddress().getType());
2071
172
      Args.push_back(Dest.getPointer());
2072
172
      Constraints += "=*";
2073
172
      Constraints += OutputConstraint;
2074
172
      ReadOnly = ReadNone = false;
2075
172
    }
2076
1.10k
2077
1.10k
    if (Info.isReadWrite()) {
2078
120
      InOutConstraints += ',';
2079
120
2080
120
      const Expr *InputExpr = S.getOutputExpr(i);
2081
120
      llvm::Value *Arg = EmitAsmInputLValue(Info, Dest, InputExpr->getType(),
2082
120
                                            InOutConstraints,
2083
120
                                            InputExpr->getExprLoc());
2084
120
2085
120
      if (llvm::Type* AdjTy =
2086
120
          getTargetHooks().adjustInlineAsmType(*this, OutputConstraint,
2087
120
                                               Arg->getType()))
2088
120
        Arg = Builder.CreateBitCast(Arg, AdjTy);
2089
120
2090
120
      // Update largest vector width for any vector types.
2091
120
      if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType()))
2092
6
        LargestVectorWidth = std::max(LargestVectorWidth,
2093
6
                                      VT->getPrimitiveSizeInBits());
2094
120
      if (Info.allowsRegister())
2095
105
        InOutConstraints += llvm::utostr(i);
2096
15
      else
2097
15
        InOutConstraints += OutputConstraint;
2098
120
2099
120
      InOutArgTypes.push_back(Arg->getType());
2100
120
      InOutArgs.push_back(Arg);
2101
120
    }
2102
1.10k
  }
2103
8.19k
2104
8.19k
  // If this is a Microsoft-style asm blob, store the return registers (EAX:EDX)
2105
8.19k
  // to the return value slot. Only do this when returning in registers.
2106
8.19k
  if (isa<MSAsmStmt>(&S)) {
2107
153
    const ABIArgInfo &RetAI = CurFnInfo->getReturnInfo();
2108
153
    if (RetAI.isDirect() || 
RetAI.isExtend()130
) {
2109
26
      // Make a fake lvalue for the return value slot.
2110
26
      LValue ReturnSlot = MakeAddrLValue(ReturnValue, FnRetTy);
2111
26
      CGM.getTargetCodeGenInfo().addReturnRegisterOutputs(
2112
26
          *this, ReturnSlot, Constraints, ResultRegTypes, ResultTruncRegTypes,
2113
26
          ResultRegDests, AsmString, S.getNumOutputs());
2114
26
      SawAsmBlock = true;
2115
26
    }
2116
153
  }
2117
8.19k
2118
9.41k
  for (unsigned i = 0, e = S.getNumInputs(); i != e; 
i++1.21k
) {
2119
1.21k
    const Expr *InputExpr = S.getInputExpr(i);
2120
1.21k
2121
1.21k
    TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
2122
1.21k
2123
1.21k
    if (Info.allowsMemory())
2124
521
      ReadNone = false;
2125
1.21k
2126
1.21k
    if (!Constraints.empty())
2127
1.00k
      Constraints += ',';
2128
1.21k
2129
1.21k
    // Simplify the input constraint.
2130
1.21k
    std::string InputConstraint(S.getInputConstraint(i));
2131
1.21k
    InputConstraint = SimplifyConstraint(InputConstraint.c_str(), getTarget(),
2132
1.21k
                                         &OutputConstraintInfos);
2133
1.21k
2134
1.21k
    InputConstraint = AddVariableConstraints(
2135
1.21k
        InputConstraint, *InputExpr->IgnoreParenNoopCasts(getContext()),
2136
1.21k
        getTarget(), CGM, S, false /* No EarlyClobber */);
2137
1.21k
2138
1.21k
    std::string ReplaceConstraint (InputConstraint);
2139
1.21k
    llvm::Value *Arg = EmitAsmInput(Info, InputExpr, Constraints);
2140
1.21k
2141
1.21k
    // If this input argument is tied to a larger output result, extend the
2142
1.21k
    // input to be the same size as the output.  The LLVM backend wants to see
2143
1.21k
    // the input and output of a matching constraint be the same size.  Note
2144
1.21k
    // that GCC does not define what the top bits are here.  We use zext because
2145
1.21k
    // that is usually cheaper, but LLVM IR should really get an anyext someday.
2146
1.21k
    if (Info.hasTiedOperand()) {
2147
34
      unsigned Output = Info.getTiedOperand();
2148
34
      QualType OutputType = S.getOutputExpr(Output)->getType();
2149
34
      QualType InputTy = InputExpr->getType();
2150
34
2151
34
      if (getContext().getTypeSize(OutputType) >
2152
34
          getContext().getTypeSize(InputTy)) {
2153
5
        // Use ptrtoint as appropriate so that we can do our extension.
2154
5
        if (isa<llvm::PointerType>(Arg->getType()))
2155
0
          Arg = Builder.CreatePtrToInt(Arg, IntPtrTy);
2156
5
        llvm::Type *OutputTy = ConvertType(OutputType);
2157
5
        if (isa<llvm::IntegerType>(OutputTy))
2158
2
          Arg = Builder.CreateZExt(Arg, OutputTy);
2159
3
        else if (isa<llvm::PointerType>(OutputTy))
2160
1
          Arg = Builder.CreateZExt(Arg, IntPtrTy);
2161
2
        else {
2162
2
          assert(OutputTy->isFloatingPointTy() && "Unexpected output type");
2163
2
          Arg = Builder.CreateFPExt(Arg, OutputTy);
2164
2
        }
2165
5
      }
2166
34
      // Deal with the tied operands' constraint code in adjustInlineAsmType.
2167
34
      ReplaceConstraint = OutputConstraints[Output];
2168
34
    }
2169
1.21k
    if (llvm::Type* AdjTy =
2170
1.21k
          getTargetHooks().adjustInlineAsmType(*this, ReplaceConstraint,
2171
1.21k
                                                   Arg->getType()))
2172
1.21k
      Arg = Builder.CreateBitCast(Arg, AdjTy);
2173
0
    else
2174
0
      CGM.getDiags().Report(S.getAsmLoc(), diag::err_asm_invalid_type_in_input)
2175
0
          << InputExpr->getType() << InputConstraint;
2176
1.21k
2177
1.21k
    // Update largest vector width for any vector types.
2178
1.21k
    if (auto *VT = dyn_cast<llvm::VectorType>(Arg->getType()))
2179
47
      LargestVectorWidth = std::max(LargestVectorWidth,
2180
47
                                    VT->getPrimitiveSizeInBits());
2181
1.21k
2182
1.21k
    ArgTypes.push_back(Arg->getType());
2183
1.21k
    Args.push_back(Arg);
2184
1.21k
    Constraints += InputConstraint;
2185
1.21k
  }
2186
8.19k
2187
8.19k
  // Append the "input" part of inout constraints last.
2188
8.31k
  for (unsigned i = 0, e = InOutArgs.size(); i != e; 
i++120
) {
2189
120
    ArgTypes.push_back(InOutArgTypes[i]);
2190
120
    Args.push_back(InOutArgs[i]);
2191
120
  }
2192
8.19k
  Constraints += InOutConstraints;
2193
8.19k
2194
8.19k
  // Labels
2195
8.19k
  SmallVector<llvm::BasicBlock *, 16> Transfer;
2196
8.19k
  llvm::BasicBlock *Fallthrough = nullptr;
2197
8.19k
  bool IsGCCAsmGoto = false;
2198
8.19k
  if (const auto *GS =  dyn_cast<GCCAsmStmt>(&S)) {
2199
8.04k
    IsGCCAsmGoto = GS->isAsmGoto();
2200
8.04k
    if (IsGCCAsmGoto) {
2201
15
      for (auto *E : GS->labels()) {
2202
15
        JumpDest Dest = getJumpDestForLabel(E->getLabel());
2203
15
        Transfer.push_back(Dest.getBlock());
2204
15
        llvm::BlockAddress *BA =
2205
15
            llvm::BlockAddress::get(CurFn, Dest.getBlock());
2206
15
        Args.push_back(BA);
2207
15
        ArgTypes.push_back(BA->getType());
2208
15
        if (!Constraints.empty())
2209
10
          Constraints += ',';
2210
15
        Constraints += 'X';
2211
15
      }
2212
10
      StringRef Name = "asm.fallthrough";
2213
10
      Fallthrough = createBasicBlock(Name);
2214
10
    }
2215
8.04k
  }
2216
8.19k
2217
8.19k
  // Clobbers
2218
15.6k
  for (unsigned i = 0, e = S.getNumClobbers(); i != e; 
i++7.50k
) {
2219
7.50k
    StringRef Clobber = S.getClobber(i);
2220
7.50k
2221
7.50k
    if (Clobber == "memory")
2222
6.72k
      ReadOnly = ReadNone = false;
2223
779
    else if (Clobber != "cc")
2224
738
      Clobber = getTarget().getNormalizedGCCRegisterName(Clobber);
2225
7.50k
2226
7.50k
    if (!Constraints.empty())
2227
689
      Constraints += ',';
2228
7.50k
2229
7.50k
    Constraints += "~{";
2230
7.50k
    Constraints += Clobber;
2231
7.50k
    Constraints += '}';
2232
7.50k
  }
2233
8.19k
2234
8.19k
  // Add machine specific clobbers
2235
8.19k
  std::string MachineClobbers = getTarget().getClobbers();
2236
8.19k
  if (!MachineClobbers.empty()) {
2237
6.22k
    if (!Constraints.empty())
2238
5.95k
      Constraints += ',';
2239
6.22k
    Constraints += MachineClobbers;
2240
6.22k
  }
2241
8.19k
2242
8.19k
  llvm::Type *ResultType;
2243
8.19k
  if (ResultRegTypes.empty())
2244
7.42k
    ResultType = VoidTy;
2245
769
  else if (ResultRegTypes.size() == 1)
2246
681
    ResultType = ResultRegTypes[0];
2247
88
  else
2248
88
    ResultType = llvm::StructType::get(getLLVMContext(), ResultRegTypes);
2249
8.19k
2250
8.19k
  llvm::FunctionType *FTy =
2251
8.19k
    llvm::FunctionType::get(ResultType, ArgTypes, false);
2252
8.19k
2253
8.19k
  bool HasSideEffect = S.isVolatile() || 
S.getNumOutputs() == 0881
;
2254
8.19k
  llvm::InlineAsm::AsmDialect AsmDialect = isa<MSAsmStmt>(&S) ?
2255
8.04k
    
llvm::InlineAsm::AD_Intel153
: llvm::InlineAsm::AD_ATT;
2256
8.19k
  llvm::InlineAsm *IA =
2257
8.19k
    llvm::InlineAsm::get(FTy, AsmString, Constraints, HasSideEffect,
2258
8.19k
                         /* IsAlignStack */ false, AsmDialect);
2259
8.19k
  std::vector<llvm::Value*> RegResults;
2260
8.19k
  if (IsGCCAsmGoto) {
2261
10
    llvm::CallBrInst *Result =
2262
10
        Builder.CreateCallBr(IA, Fallthrough, Transfer, Args);
2263
10
    UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, ReadOnly,
2264
10
                      ReadNone, S, ResultRegTypes, *this, RegResults);
2265
10
    EmitBlock(Fallthrough);
2266
8.18k
  } else {
2267
8.18k
    llvm::CallInst *Result =
2268
8.18k
        Builder.CreateCall(IA, Args, getBundlesForFunclet(IA));
2269
8.18k
    UpdateAsmCallInst(cast<llvm::CallBase>(*Result), HasSideEffect, ReadOnly,
2270
8.18k
                      ReadNone, S, ResultRegTypes, *this, RegResults);
2271
8.18k
  }
2272
8.19k
2273
8.19k
  assert(RegResults.size() == ResultRegTypes.size());
2274
8.19k
  assert(RegResults.size() == ResultTruncRegTypes.size());
2275
8.19k
  assert(RegResults.size() == ResultRegDests.size());
2276
9.15k
  for (unsigned i = 0, e = RegResults.size(); i != e; 
++i955
) {
2277
955
    llvm::Value *Tmp = RegResults[i];
2278
955
2279
955
    // If the result type of the LLVM IR asm doesn't match the result type of
2280
955
    // the expression, do the conversion.
2281
955
    if (ResultRegTypes[i] != ResultTruncRegTypes[i]) {
2282
18
      llvm::Type *TruncTy = ResultTruncRegTypes[i];
2283
18
2284
18
      // Truncate the integer result to the right size, note that TruncTy can be
2285
18
      // a pointer.
2286
18
      if (TruncTy->isFloatingPointTy())
2287
1
        Tmp = Builder.CreateFPTrunc(Tmp, TruncTy);
2288
17
      else if (TruncTy->isPointerTy() && 
Tmp->getType()->isIntegerTy()0
) {
2289
0
        uint64_t ResSize = CGM.getDataLayout().getTypeSizeInBits(TruncTy);
2290
0
        Tmp = Builder.CreateTrunc(Tmp,
2291
0
                   llvm::IntegerType::get(getLLVMContext(), (unsigned)ResSize));
2292
0
        Tmp = Builder.CreateIntToPtr(Tmp, TruncTy);
2293
17
      } else if (Tmp->getType()->isPointerTy() && 
TruncTy->isIntegerTy()0
) {
2294
0
        uint64_t TmpSize =CGM.getDataLayout().getTypeSizeInBits(Tmp->getType());
2295
0
        Tmp = Builder.CreatePtrToInt(Tmp,
2296
0
                   llvm::IntegerType::get(getLLVMContext(), (unsigned)TmpSize));
2297
0
        Tmp = Builder.CreateTrunc(Tmp, TruncTy);
2298
17
      } else if (TruncTy->isIntegerTy()) {
2299
7
        Tmp = Builder.CreateZExtOrTrunc(Tmp, TruncTy);
2300
10
      } else if (TruncTy->isVectorTy()) {
2301
10
        Tmp = Builder.CreateBitCast(Tmp, TruncTy);
2302
10
      }
2303
18
    }
2304
955
2305
955
    EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i]);
2306
955
  }
2307
8.19k
}
2308
2309
469
LValue CodeGenFunction::InitCapturedStruct(const CapturedStmt &S) {
2310
469
  const RecordDecl *RD = S.getCapturedRecordDecl();
2311
469
  QualType RecordTy = getContext().getRecordType(RD);
2312
469
2313
469
  // Initialize the captured struct.
2314
469
  LValue SlotLV =
2315
469
    MakeAddrLValue(CreateMemTemp(RecordTy, "agg.captured"), RecordTy);
2316
469
2317
469
  RecordDecl::field_iterator CurField = RD->field_begin();
2318
469
  for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(),
2319
469
                                                 E = S.capture_init_end();
2320
1.24k
       I != E; 
++I, ++CurField771
) {
2321
771
    LValue LV = EmitLValueForFieldInitialization(SlotLV, *CurField);
2322
771
    if (CurField->hasCapturedVLAType()) {
2323
27
      auto VAT = CurField->getCapturedVLAType();
2324
27
      EmitStoreThroughLValue(RValue::get(VLASizeMap[VAT->getSizeExpr()]), LV);
2325
744
    } else {
2326
744
      EmitInitializerForField(*CurField, LV, *I);
2327
744
    }
2328
771
  }
2329
469
2330
469
  return SlotLV;
2331
469
}
2332
2333
/// Generate an outlined function for the body of a CapturedStmt, store any
2334
/// captured variables into the captured struct, and call the outlined function.
2335
llvm::Function *
2336
27
CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K) {
2337
27
  LValue CapStruct = InitCapturedStruct(S);
2338
27
2339
27
  // Emit the CapturedDecl
2340
27
  CodeGenFunction CGF(CGM, true);
2341
27
  CGCapturedStmtRAII CapInfoRAII(CGF, new CGCapturedStmtInfo(S, K));
2342
27
  llvm::Function *F = CGF.GenerateCapturedStmtFunction(S);
2343
27
  delete CGF.CapturedStmtInfo;
2344
27
2345
27
  // Emit call to the helper function.
2346
27
  EmitCallOrInvoke(F, CapStruct.getPointer());
2347
27
2348
27
  return F;
2349
27
}
2350
2351
442
Address CodeGenFunction::GenerateCapturedStmtArgument(const CapturedStmt &S) {
2352
442
  LValue CapStruct = InitCapturedStruct(S);
2353
442
  return CapStruct.getAddress();
2354
442
}
2355
2356
/// Creates the outlined function for a CapturedStmt.
2357
llvm::Function *
2358
469
CodeGenFunction::GenerateCapturedStmtFunction(const CapturedStmt &S) {
2359
469
  assert(CapturedStmtInfo &&
2360
469
    "CapturedStmtInfo should be set when generating the captured function");
2361
469
  const CapturedDecl *CD = S.getCapturedDecl();
2362
469
  const RecordDecl *RD = S.getCapturedRecordDecl();
2363
469
  SourceLocation Loc = S.getBeginLoc();
2364
469
  assert(CD->hasBody() && "missing CapturedDecl body");
2365
469
2366
469
  // Build the argument list.
2367
469
  ASTContext &Ctx = CGM.getContext();
2368
469
  FunctionArgList Args;
2369
469
  Args.append(CD->param_begin(), CD->param_end());
2370
469
2371
469
  // Create the function declaration.
2372
469
  const CGFunctionInfo &FuncInfo =
2373
469
    CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
2374
469
  llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo);
2375
469
2376
469
  llvm::Function *F =
2377
469
    llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage,
2378
469
                           CapturedStmtInfo->getHelperName(), &CGM.getModule());
2379
469
  CGM.SetInternalFunctionAttributes(CD, F, FuncInfo);
2380
469
  if (CD->isNothrow())
2381
377
    F->addFnAttr(llvm::Attribute::NoUnwind);
2382
469
2383
469
  // Generate the function.
2384
469
  StartFunction(CD, Ctx.VoidTy, F, FuncInfo, Args, CD->getLocation(),
2385
469
                CD->getBody()->getBeginLoc());
2386
469
  // Set the context parameter in CapturedStmtInfo.
2387
469
  Address DeclPtr = GetAddrOfLocalVar(CD->getContextParam());
2388
469
  CapturedStmtInfo->setContextValue(Builder.CreateLoad(DeclPtr));
2389
469
2390
469
  // Initialize variable-length arrays.
2391
469
  LValue Base = MakeNaturalAlignAddrLValue(CapturedStmtInfo->getContextValue(),
2392
469
                                           Ctx.getTagDeclType(RD));
2393
771
  for (auto *FD : RD->fields()) {
2394
771
    if (FD->hasCapturedVLAType()) {
2395
27
      auto *ExprArg =
2396
27
          EmitLoadOfLValue(EmitLValueForField(Base, FD), S.getBeginLoc())
2397
27
              .getScalarVal();
2398
27
      auto VAT = FD->getCapturedVLAType();
2399
27
      VLASizeMap[VAT->getSizeExpr()] = ExprArg;
2400
27
    }
2401
771
  }
2402
469
2403
469
  // If 'this' is captured, load it into CXXThisValue.
2404
469
  if (CapturedStmtInfo->isCXXThisExprCaptured()) {
2405
8
    FieldDecl *FD = CapturedStmtInfo->getThisFieldDecl();
2406
8
    LValue ThisLValue = EmitLValueForField(Base, FD);
2407
8
    CXXThisValue = EmitLoadOfLValue(ThisLValue, Loc).getScalarVal();
2408
8
  }
2409
469
2410
469
  PGO.assignRegionCounters(GlobalDecl(CD), F);
2411
469
  CapturedStmtInfo->EmitBody(*this, CD->getBody());
2412
469
  FinishFunction(CD->getBodyRBrace());
2413
469
2414
469
  return F;
2415
469
}