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