Deleted Added
full compact
CGStmt.cpp (199482) CGStmt.cpp (199990)
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//===----------------------------------------------------------------------===//

--- 139 unchanged lines hidden (view full) ---

148
149 CGDebugInfo *DI = getDebugInfo();
150 if (DI) {
151 DI->setLocation(S.getLBracLoc());
152 DI->EmitRegionStart(CurFn, Builder);
153 }
154
155 // Keep track of the current cleanup stack depth.
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//===----------------------------------------------------------------------===//

--- 139 unchanged lines hidden (view full) ---

148
149 CGDebugInfo *DI = getDebugInfo();
150 if (DI) {
151 DI->setLocation(S.getLBracLoc());
152 DI->EmitRegionStart(CurFn, Builder);
153 }
154
155 // Keep track of the current cleanup stack depth.
156 size_t CleanupStackDepth = CleanupEntries.size();
157 bool OldDidCallStackSave = DidCallStackSave;
158 DidCallStackSave = false;
156 CleanupScope Scope(*this);
159
160 for (CompoundStmt::const_body_iterator I = S.body_begin(),
161 E = S.body_end()-GetLast; I != E; ++I)
162 EmitStmt(*I);
163
164 if (DI) {
165 DI->setLocation(S.getLBracLoc());
166 DI->EmitRegionEnd(CurFn, Builder);

--- 13 unchanged lines hidden (view full) ---

180 LastStmt = LS->getSubStmt();
181 }
182
183 EnsureInsertPoint();
184
185 RV = EmitAnyExpr(cast<Expr>(LastStmt), AggLoc);
186 }
187
157
158 for (CompoundStmt::const_body_iterator I = S.body_begin(),
159 E = S.body_end()-GetLast; I != E; ++I)
160 EmitStmt(*I);
161
162 if (DI) {
163 DI->setLocation(S.getLBracLoc());
164 DI->EmitRegionEnd(CurFn, Builder);

--- 13 unchanged lines hidden (view full) ---

178 LastStmt = LS->getSubStmt();
179 }
180
181 EnsureInsertPoint();
182
183 RV = EmitAnyExpr(cast<Expr>(LastStmt), AggLoc);
184 }
185
188 DidCallStackSave = OldDidCallStackSave;
189
190 EmitCleanupBlocks(CleanupStackDepth);
191
192 return RV;
193}
194
195void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
196 llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
197
198 // If there is a cleanup stack, then we it isn't worth trying to
199 // simplify this block (we would need to remove it from the scope map

--- 89 unchanged lines hidden (view full) ---

289 cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
290
291 EmitBranch(IndGotoBB);
292}
293
294void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
295 // C99 6.8.4.1: The first substatement is executed if the expression compares
296 // unequal to 0. The condition must be a scalar type.
186 return RV;
187}
188
189void CodeGenFunction::SimplifyForwardingBlocks(llvm::BasicBlock *BB) {
190 llvm::BranchInst *BI = dyn_cast<llvm::BranchInst>(BB->getTerminator());
191
192 // If there is a cleanup stack, then we it isn't worth trying to
193 // simplify this block (we would need to remove it from the scope map

--- 89 unchanged lines hidden (view full) ---

283 cast<llvm::PHINode>(IndGotoBB->begin())->addIncoming(V, CurBB);
284
285 EmitBranch(IndGotoBB);
286}
287
288void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
289 // C99 6.8.4.1: The first substatement is executed if the expression compares
290 // unequal to 0. The condition must be a scalar type.
291 CleanupScope ConditionScope(*this);
297
292
293 if (S.getConditionVariable())
294 EmitLocalBlockVarDecl(*S.getConditionVariable());
295
298 // If the condition constant folds and can be elided, try to avoid emitting
299 // the condition and the dead arm of the if/else.
300 if (int Cond = ConstantFoldsToSimpleInteger(S.getCond())) {
301 // Figure out which block (then or else) is executed.
302 const Stmt *Executed = S.getThen(), *Skipped = S.getElse();
303 if (Cond == -1) // Condition false?
304 std::swap(Executed, Skipped);
305
306 // If the skipped block has no labels in it, just emit the executed block.
307 // This avoids emitting dead code and simplifies the CFG substantially.
308 if (!ContainsLabel(Skipped)) {
296 // If the condition constant folds and can be elided, try to avoid emitting
297 // the condition and the dead arm of the if/else.
298 if (int Cond = ConstantFoldsToSimpleInteger(S.getCond())) {
299 // Figure out which block (then or else) is executed.
300 const Stmt *Executed = S.getThen(), *Skipped = S.getElse();
301 if (Cond == -1) // Condition false?
302 std::swap(Executed, Skipped);
303
304 // If the skipped block has no labels in it, just emit the executed block.
305 // This avoids emitting dead code and simplifies the CFG substantially.
306 if (!ContainsLabel(Skipped)) {
309 if (Executed)
307 if (Executed) {
308 CleanupScope ExecutedScope(*this);
310 EmitStmt(Executed);
309 EmitStmt(Executed);
310 }
311 return;
312 }
313 }
314
315 // Otherwise, the condition did not fold, or we couldn't elide it. Just emit
316 // the conditional branch.
317 llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
318 llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
319 llvm::BasicBlock *ElseBlock = ContBlock;
320 if (S.getElse())
321 ElseBlock = createBasicBlock("if.else");
322 EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock);
323
324 // Emit the 'then' code.
311 return;
312 }
313 }
314
315 // Otherwise, the condition did not fold, or we couldn't elide it. Just emit
316 // the conditional branch.
317 llvm::BasicBlock *ThenBlock = createBasicBlock("if.then");
318 llvm::BasicBlock *ContBlock = createBasicBlock("if.end");
319 llvm::BasicBlock *ElseBlock = ContBlock;
320 if (S.getElse())
321 ElseBlock = createBasicBlock("if.else");
322 EmitBranchOnBoolExpr(S.getCond(), ThenBlock, ElseBlock);
323
324 // Emit the 'then' code.
325 EmitBlock(ThenBlock);
326 EmitStmt(S.getThen());
325 EmitBlock(ThenBlock);
326 {
327 CleanupScope ThenScope(*this);
328 EmitStmt(S.getThen());
329 }
327 EmitBranch(ContBlock);
328
329 // Emit the 'else' code if present.
330 if (const Stmt *Else = S.getElse()) {
331 EmitBlock(ElseBlock);
330 EmitBranch(ContBlock);
331
332 // Emit the 'else' code if present.
333 if (const Stmt *Else = S.getElse()) {
334 EmitBlock(ElseBlock);
332 EmitStmt(Else);
335 {
336 CleanupScope ElseScope(*this);
337 EmitStmt(Else);
338 }
333 EmitBranch(ContBlock);
334 }
335
336 // Emit the continuation block for code after the if.
337 EmitBlock(ContBlock, true);
338}
339
340void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
341 // Emit the header for the loop, insert it, which will create an uncond br to
342 // it.
343 llvm::BasicBlock *LoopHeader = createBasicBlock("while.cond");
344 EmitBlock(LoopHeader);
345
346 // Create an exit block for when the condition fails, create a block for the
347 // body of the loop.
348 llvm::BasicBlock *ExitBlock = createBasicBlock("while.end");
349 llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
339 EmitBranch(ContBlock);
340 }
341
342 // Emit the continuation block for code after the if.
343 EmitBlock(ContBlock, true);
344}
345
346void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
347 // Emit the header for the loop, insert it, which will create an uncond br to
348 // it.
349 llvm::BasicBlock *LoopHeader = createBasicBlock("while.cond");
350 EmitBlock(LoopHeader);
351
352 // Create an exit block for when the condition fails, create a block for the
353 // body of the loop.
354 llvm::BasicBlock *ExitBlock = createBasicBlock("while.end");
355 llvm::BasicBlock *LoopBody = createBasicBlock("while.body");
356 llvm::BasicBlock *CleanupBlock = 0;
357 llvm::BasicBlock *EffectiveExitBlock = ExitBlock;
350
351 // Store the blocks to use for break and continue.
352 BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
353
358
359 // Store the blocks to use for break and continue.
360 BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
361
362 // C++ [stmt.while]p2:
363 // When the condition of a while statement is a declaration, the
364 // scope of the variable that is declared extends from its point
365 // of declaration (3.3.2) to the end of the while statement.
366 // [...]
367 // The object created in a condition is destroyed and created
368 // with each iteration of the loop.
369 CleanupScope ConditionScope(*this);
370
371 if (S.getConditionVariable()) {
372 EmitLocalBlockVarDecl(*S.getConditionVariable());
373
374 // If this condition variable requires cleanups, create a basic
375 // block to handle those cleanups.
376 if (ConditionScope.requiresCleanups()) {
377 CleanupBlock = createBasicBlock("while.cleanup");
378 EffectiveExitBlock = CleanupBlock;
379 }
380 }
381
354 // Evaluate the conditional in the while header. C99 6.8.5.1: The
355 // evaluation of the controlling expression takes place before each
356 // execution of the loop body.
357 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
382 // Evaluate the conditional in the while header. C99 6.8.5.1: The
383 // evaluation of the controlling expression takes place before each
384 // execution of the loop body.
385 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
358
386
359 // while(1) is common, avoid extra exit blocks. Be sure
360 // to correctly handle break/continue though.
361 bool EmitBoolCondBranch = true;
362 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
363 if (C->isOne())
364 EmitBoolCondBranch = false;
365
366 // As long as the condition is true, go to the loop body.
367 if (EmitBoolCondBranch)
387 // while(1) is common, avoid extra exit blocks. Be sure
388 // to correctly handle break/continue though.
389 bool EmitBoolCondBranch = true;
390 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
391 if (C->isOne())
392 EmitBoolCondBranch = false;
393
394 // As long as the condition is true, go to the loop body.
395 if (EmitBoolCondBranch)
368 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
369
396 Builder.CreateCondBr(BoolCondVal, LoopBody, EffectiveExitBlock);
397
370 // Emit the loop body.
398 // Emit the loop body.
371 EmitBlock(LoopBody);
372 EmitStmt(S.getBody());
399 {
400 CleanupScope BodyScope(*this);
401 EmitBlock(LoopBody);
402 EmitStmt(S.getBody());
403 }
373
374 BreakContinueStack.pop_back();
375
404
405 BreakContinueStack.pop_back();
406
376 // Cycle to the condition.
377 EmitBranch(LoopHeader);
407 if (CleanupBlock) {
408 // If we have a cleanup block, jump there to perform cleanups
409 // before looping.
410 EmitBranch(CleanupBlock);
378
411
412 // Emit the cleanup block, performing cleanups for the condition
413 // and then jumping to either the loop header or the exit block.
414 EmitBlock(CleanupBlock);
415 ConditionScope.ForceCleanup();
416 Builder.CreateCondBr(BoolCondVal, LoopHeader, ExitBlock);
417 } else {
418 // Cycle to the condition.
419 EmitBranch(LoopHeader);
420 }
421
379 // Emit the exit block.
380 EmitBlock(ExitBlock, true);
381
422 // Emit the exit block.
423 EmitBlock(ExitBlock, true);
424
425
382 // The LoopHeader typically is just a branch if we skipped emitting
383 // a branch, try to erase it.
426 // The LoopHeader typically is just a branch if we skipped emitting
427 // a branch, try to erase it.
384 if (!EmitBoolCondBranch)
428 if (!EmitBoolCondBranch && !CleanupBlock)
385 SimplifyForwardingBlocks(LoopHeader);
386}
387
388void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
389 // Emit the body for the loop, insert it, which will create an uncond br to
390 // it.
391 llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
392 llvm::BasicBlock *AfterDo = createBasicBlock("do.end");

--- 37 unchanged lines hidden (view full) ---

430 // emitting a branch, try to erase it.
431 if (!EmitBoolCondBranch)
432 SimplifyForwardingBlocks(DoCond);
433}
434
435void CodeGenFunction::EmitForStmt(const ForStmt &S) {
436 // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
437 // which contains a continue/break?
429 SimplifyForwardingBlocks(LoopHeader);
430}
431
432void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
433 // Emit the body for the loop, insert it, which will create an uncond br to
434 // it.
435 llvm::BasicBlock *LoopBody = createBasicBlock("do.body");
436 llvm::BasicBlock *AfterDo = createBasicBlock("do.end");

--- 37 unchanged lines hidden (view full) ---

474 // emitting a branch, try to erase it.
475 if (!EmitBoolCondBranch)
476 SimplifyForwardingBlocks(DoCond);
477}
478
479void CodeGenFunction::EmitForStmt(const ForStmt &S) {
480 // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
481 // which contains a continue/break?
482 CleanupScope ForScope(*this);
438
439 // Evaluate the first part before the loop.
440 if (S.getInit())
441 EmitStmt(S.getInit());
442
443 // Start the loop with a block that tests the condition.
444 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
445 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
483
484 // Evaluate the first part before the loop.
485 if (S.getInit())
486 EmitStmt(S.getInit());
487
488 // Start the loop with a block that tests the condition.
489 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond");
490 llvm::BasicBlock *AfterFor = createBasicBlock("for.end");
446
491 llvm::BasicBlock *IncBlock = 0;
492 llvm::BasicBlock *CondCleanup = 0;
493 llvm::BasicBlock *EffectiveExitBlock = AfterFor;
447 EmitBlock(CondBlock);
448
494 EmitBlock(CondBlock);
495
449 // Evaluate the condition if present. If not, treat it as a
450 // non-zero-constant according to 6.8.5.3p2, aka, true.
496 // Create a cleanup scope for the condition variable cleanups.
497 CleanupScope ConditionScope(*this);
498
499 llvm::Value *BoolCondVal = 0;
451 if (S.getCond()) {
500 if (S.getCond()) {
501 // If the for statement has a condition scope, emit the local variable
502 // declaration.
503 if (S.getConditionVariable()) {
504 EmitLocalBlockVarDecl(*S.getConditionVariable());
505
506 if (ConditionScope.requiresCleanups()) {
507 CondCleanup = createBasicBlock("for.cond.cleanup");
508 EffectiveExitBlock = CondCleanup;
509 }
510 }
511
452 // As long as the condition is true, iterate the loop.
453 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
454
455 // C99 6.8.5p2/p4: The first substatement is executed if the expression
456 // compares unequal to 0. The condition must be a scalar type.
512 // As long as the condition is true, iterate the loop.
513 llvm::BasicBlock *ForBody = createBasicBlock("for.body");
514
515 // C99 6.8.5p2/p4: The first substatement is executed if the expression
516 // compares unequal to 0. The condition must be a scalar type.
457 EmitBranchOnBoolExpr(S.getCond(), ForBody, AfterFor);
517 BoolCondVal = EvaluateExprAsBool(S.getCond());
518 Builder.CreateCondBr(BoolCondVal, ForBody, EffectiveExitBlock);
458
459 EmitBlock(ForBody);
460 } else {
461 // Treat it as a non-zero constant. Don't even create a new block for the
462 // body, just fall into it.
463 }
464
465 // If the for loop doesn't have an increment we can just use the
466 // condition as the continue block.
467 llvm::BasicBlock *ContinueBlock;
468 if (S.getInc())
519
520 EmitBlock(ForBody);
521 } else {
522 // Treat it as a non-zero constant. Don't even create a new block for the
523 // body, just fall into it.
524 }
525
526 // If the for loop doesn't have an increment we can just use the
527 // condition as the continue block.
528 llvm::BasicBlock *ContinueBlock;
529 if (S.getInc())
469 ContinueBlock = createBasicBlock("for.inc");
530 ContinueBlock = IncBlock = createBasicBlock("for.inc");
470 else
471 ContinueBlock = CondBlock;
472
473 // Store the blocks to use for break and continue.
474 BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock));
475
476 // If the condition is true, execute the body of the for stmt.
477 CGDebugInfo *DI = getDebugInfo();
478 if (DI) {
479 DI->setLocation(S.getSourceRange().getBegin());
480 DI->EmitRegionStart(CurFn, Builder);
481 }
531 else
532 ContinueBlock = CondBlock;
533
534 // Store the blocks to use for break and continue.
535 BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock));
536
537 // If the condition is true, execute the body of the for stmt.
538 CGDebugInfo *DI = getDebugInfo();
539 if (DI) {
540 DI->setLocation(S.getSourceRange().getBegin());
541 DI->EmitRegionStart(CurFn, Builder);
542 }
482 EmitStmt(S.getBody());
483
543
544 {
545 // Create a separate cleanup scope for the body, in case it is not
546 // a compound statement.
547 CleanupScope BodyScope(*this);
548 EmitStmt(S.getBody());
549 }
550
484 BreakContinueStack.pop_back();
485
486 // If there is an increment, emit it next.
487 if (S.getInc()) {
551 BreakContinueStack.pop_back();
552
553 // If there is an increment, emit it next.
554 if (S.getInc()) {
488 EmitBlock(ContinueBlock);
555 EmitBlock(IncBlock);
489 EmitStmt(S.getInc());
490 }
491
492 // Finally, branch back up to the condition for the next iteration.
556 EmitStmt(S.getInc());
557 }
558
559 // Finally, branch back up to the condition for the next iteration.
493 EmitBranch(CondBlock);
560 if (CondCleanup) {
561 // Branch to the cleanup block.
562 EmitBranch(CondCleanup);
563
564 // Emit the cleanup block, which branches back to the loop body or
565 // outside of the for statement once it is done.
566 EmitBlock(CondCleanup);
567 ConditionScope.ForceCleanup();
568 Builder.CreateCondBr(BoolCondVal, CondBlock, AfterFor);
569 } else
570 EmitBranch(CondBlock);
494 if (DI) {
495 DI->setLocation(S.getSourceRange().getEnd());
496 DI->EmitRegionEnd(CurFn, Builder);
497 }
498
499 // Emit the fall-through block.
500 EmitBlock(AfterFor, true);
501}

--- 179 unchanged lines hidden (view full) ---

681 llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
682 assert(DefaultBlock->empty() &&
683 "EmitDefaultStmt: Default block already defined?");
684 EmitBlock(DefaultBlock);
685 EmitStmt(S.getSubStmt());
686}
687
688void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
571 if (DI) {
572 DI->setLocation(S.getSourceRange().getEnd());
573 DI->EmitRegionEnd(CurFn, Builder);
574 }
575
576 // Emit the fall-through block.
577 EmitBlock(AfterFor, true);
578}

--- 179 unchanged lines hidden (view full) ---

758 llvm::BasicBlock *DefaultBlock = SwitchInsn->getDefaultDest();
759 assert(DefaultBlock->empty() &&
760 "EmitDefaultStmt: Default block already defined?");
761 EmitBlock(DefaultBlock);
762 EmitStmt(S.getSubStmt());
763}
764
765void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
766 CleanupScope ConditionScope(*this);
767
768 if (S.getConditionVariable())
769 EmitLocalBlockVarDecl(*S.getConditionVariable());
770
689 llvm::Value *CondV = EmitScalarExpr(S.getCond());
690
691 // Handle nested switch statements.
692 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
693 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
694
695 // Create basic block to hold stuff that comes after switch
696 // statement. We also need to create a default block now so that

--- 362 unchanged lines hidden ---
771 llvm::Value *CondV = EmitScalarExpr(S.getCond());
772
773 // Handle nested switch statements.
774 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
775 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
776
777 // Create basic block to hold stuff that comes after switch
778 // statement. We also need to create a default block now so that

--- 362 unchanged lines hidden ---