}
void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) {
- // Emit a branch from this block to the next one if this was a real block. If
- // this was just a fall-through block after a terminator, don't emit it.
- llvm::BasicBlock *LastBB = Builder.GetInsertBlock();
-
- if (LastBB->getTerminator()) {
- // If the previous block is already terminated, don't touch it.
- } else if (isDummyBlock(LastBB)) {
+ // Fall out of the current block (if necessary).
+ EmitBranch(BB);
+ CurFn->getBasicBlockList().push_back(BB);
+ Builder.SetInsertPoint(BB);
+}
+
+void CodeGenFunction::EmitBranch(llvm::BasicBlock *Target) {
+ // Emit a branch from the current block to the target one if this
+ // was a real block. If this was just a fall-through block after a
+ // terminator, don't emit it.
+ llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
+
+ if (!CurBB || CurBB->getTerminator()) {
+ // If there is no insert point or the previous block is already
+ // terminated, don't touch it.
+ } else if (isDummyBlock(CurBB)) {
// If the last block was an empty placeholder, remove it now.
// TODO: cache and reuse these.
- LastBB->eraseFromParent();
+ CurBB->eraseFromParent();
+ Builder.ClearInsertionPoint();
} else {
// Otherwise, create a fall-through branch.
- Builder.CreateBr(BB);
+ Builder.CreateBr(Target);
}
- CurFn->getBasicBlockList().push_back(BB);
- Builder.SetInsertPoint(BB);
}
void CodeGenFunction::EmitDummyBlock() {
return;
}
- Builder.CreateBr(getBasicBlockForLabel(S.getLabel()));
+ EmitBranch(getBasicBlockForLabel(S.getLabel()));
// Emit a block after the branch so that dead code after a goto has some place
// to go.
// Emit the 'then' code.
EmitBlock(ThenBlock);
EmitStmt(S.getThen());
- llvm::BasicBlock *BB = Builder.GetInsertBlock();
- if (isDummyBlock(BB)) {
- BB->eraseFromParent();
- Builder.SetInsertPoint(ThenBlock);
- } else {
- Builder.CreateBr(ContBlock);
- }
+ EmitBranch(ContBlock);
// Emit the 'else' code if present.
if (const Stmt *Else = S.getElse()) {
EmitBlock(ElseBlock);
EmitStmt(Else);
- llvm::BasicBlock *BB = Builder.GetInsertBlock();
- if (isDummyBlock(BB)) {
- BB->eraseFromParent();
- Builder.SetInsertPoint(ElseBlock);
- } else {
- Builder.CreateBr(ContBlock);
- }
+ EmitBranch(ContBlock);
}
// Emit the continuation block for code after the if.
BreakContinueStack.pop_back();
// Cycle to the condition.
- Builder.CreateBr(LoopHeader);
+ EmitBranch(LoopHeader);
// Emit the exit block.
EmitBlock(ExitBlock);
}
// Finally, branch back up to the condition for the next iteration.
- Builder.CreateBr(CondBlock);
+ EmitBranch(CondBlock);
// Emit the fall-through block.
EmitBlock(AfterFor);
} else {
StoreComplexToAddr(RV.getComplexVal(), ReturnValue, false);
}
- Builder.CreateBr(ReturnBlock);
+ EmitBranch(ReturnBlock);
// Emit a block after the branch so that dead code after a return has some
// place to go.
}
}
- Builder.CreateBr(ReturnBlock);
+ EmitBranch(ReturnBlock);
// Emit a block after the branch so that dead code after a return has some
// place to go.
assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
- Builder.CreateBr(Block);
+ EmitBranch(Block);
EmitDummyBlock();
}
assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
- Builder.CreateBr(Block);
+ EmitBranch(Block);
EmitDummyBlock();
}