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