Deleted Added
sdiff udiff text old ( 199482 ) new ( 199990 )
full compact
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;
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
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.
297
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)) {
309 if (Executed)
310 EmitStmt(Executed);
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());
327 EmitBranch(ContBlock);
328
329 // Emit the 'else' code if present.
330 if (const Stmt *Else = S.getElse()) {
331 EmitBlock(ElseBlock);
332 EmitStmt(Else);
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");
350
351 // Store the blocks to use for break and continue.
352 BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
353
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());
358
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)
368 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
369
370 // Emit the loop body.
371 EmitBlock(LoopBody);
372 EmitStmt(S.getBody());
373
374 BreakContinueStack.pop_back();
375
376 // Cycle to the condition.
377 EmitBranch(LoopHeader);
378
379 // Emit the exit block.
380 EmitBlock(ExitBlock, true);
381
382 // The LoopHeader typically is just a branch if we skipped emitting
383 // a branch, try to erase it.
384 if (!EmitBoolCondBranch)
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?
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");
446
447 EmitBlock(CondBlock);
448
449 // Evaluate the condition if present. If not, treat it as a
450 // non-zero-constant according to 6.8.5.3p2, aka, true.
451 if (S.getCond()) {
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.
457 EmitBranchOnBoolExpr(S.getCond(), ForBody, AfterFor);
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())
469 ContinueBlock = 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 }
482 EmitStmt(S.getBody());
483
484 BreakContinueStack.pop_back();
485
486 // If there is an increment, emit it next.
487 if (S.getInc()) {
488 EmitBlock(ContinueBlock);
489 EmitStmt(S.getInc());
490 }
491
492 // Finally, branch back up to the condition for the next iteration.
493 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) {
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 ---