blob: 226462b125967697622f59ac5c8a3595e4a27138 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGStmt.cpp - Emit LLVM Code from Statements ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source 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 "clang/AST/AST.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19using namespace clang;
20using namespace CodeGen;
21
22//===----------------------------------------------------------------------===//
23// Statement Emission
24//===----------------------------------------------------------------------===//
25
26void CodeGenFunction::EmitStmt(const Stmt *S) {
27 assert(S && "Null statement?");
28
29 switch (S->getStmtClass()) {
30 default:
Chris Lattner1e4d21e2007-08-26 22:58:05 +000031 // Must be an expression in a stmt context. Emit the value (to get
32 // side-effects) and ignore the result.
Reid Spencer5f016e22007-07-11 17:01:13 +000033 if (const Expr *E = dyn_cast<Expr>(S)) {
Chris Lattner1e4d21e2007-08-26 22:58:05 +000034 if (!hasAggregateLLVMType(E->getType()))
35 EmitScalarExpr(E);
36 else if (E->getType()->isComplexType())
37 EmitComplexExpr(E);
38 else
39 EmitAggExpr(E, 0, false);
Reid Spencer5f016e22007-07-11 17:01:13 +000040 } else {
41 printf("Unimplemented stmt!\n");
Chris Lattner419ea7e2007-09-13 01:17:29 +000042 S->dump(getContext().SourceMgr);
Reid Spencer5f016e22007-07-11 17:01:13 +000043 }
44 break;
45 case Stmt::NullStmtClass: break;
46 case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
47 case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
48 case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
49
50 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
51 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
52 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
53 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
54
55 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
56 case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
Chris Lattnerda138702007-07-16 21:28:45 +000057
58 case Stmt::BreakStmtClass: EmitBreakStmt(); break;
59 case Stmt::ContinueStmtClass: EmitContinueStmt(); break;
Devang Patel51b09f22007-10-04 23:45:31 +000060 case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
61 case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
62 case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 }
64}
65
Chris Lattner33793202007-08-31 22:09:40 +000066/// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
67/// this captures the expression result of the last sub-statement and returns it
68/// (for use by the statement expression extension).
Chris Lattner9b655512007-08-31 22:49:20 +000069RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
70 llvm::Value *AggLoc, bool isAggVol) {
Reid Spencer5f016e22007-07-11 17:01:13 +000071 // FIXME: handle vla's etc.
Chris Lattner33793202007-08-31 22:09:40 +000072 if (S.body_empty() || !isa<Expr>(S.body_back())) GetLast = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000073
Chris Lattner33793202007-08-31 22:09:40 +000074 for (CompoundStmt::const_body_iterator I = S.body_begin(),
75 E = S.body_end()-GetLast; I != E; ++I)
Reid Spencer5f016e22007-07-11 17:01:13 +000076 EmitStmt(*I);
Chris Lattner33793202007-08-31 22:09:40 +000077
78
79 if (!GetLast)
80 return RValue::get(0);
Chris Lattner9b655512007-08-31 22:49:20 +000081
82 return EmitAnyExpr(cast<Expr>(S.body_back()), AggLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000083}
84
85void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) {
86 // Emit a branch from this block to the next one if this was a real block. If
87 // this was just a fall-through block after a terminator, don't emit it.
88 llvm::BasicBlock *LastBB = Builder.GetInsertBlock();
89
90 if (LastBB->getTerminator()) {
91 // If the previous block is already terminated, don't touch it.
92 } else if (LastBB->empty() && LastBB->getValueName() == 0) {
93 // If the last block was an empty placeholder, remove it now.
94 // TODO: cache and reuse these.
95 Builder.GetInsertBlock()->eraseFromParent();
96 } else {
97 // Otherwise, create a fall-through branch.
98 Builder.CreateBr(BB);
99 }
100 CurFn->getBasicBlockList().push_back(BB);
101 Builder.SetInsertPoint(BB);
102}
103
104void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
105 llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
106
107 EmitBlock(NextBB);
108 EmitStmt(S.getSubStmt());
109}
110
111void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
112 Builder.CreateBr(getBasicBlockForLabel(S.getLabel()));
113
114 // Emit a block after the branch so that dead code after a goto has some place
115 // to go.
116 Builder.SetInsertPoint(new llvm::BasicBlock("", CurFn));
117}
118
119void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
120 // C99 6.8.4.1: The first substatement is executed if the expression compares
121 // unequal to 0. The condition must be a scalar type.
122 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
123
124 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("ifend");
125 llvm::BasicBlock *ThenBlock = new llvm::BasicBlock("ifthen");
126 llvm::BasicBlock *ElseBlock = ContBlock;
127
128 if (S.getElse())
129 ElseBlock = new llvm::BasicBlock("ifelse");
130
131 // Insert the conditional branch.
132 Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock);
133
134 // Emit the 'then' code.
135 EmitBlock(ThenBlock);
136 EmitStmt(S.getThen());
Devang Pateld9363c32007-09-28 21:49:18 +0000137 llvm::BasicBlock *BB = Builder.GetInsertBlock();
138 if (isDummyBlock(BB)) {
139 BB->eraseFromParent();
140 Builder.SetInsertPoint(ThenBlock);
141 }
142 else
143 Builder.CreateBr(ContBlock);
Reid Spencer5f016e22007-07-11 17:01:13 +0000144
145 // Emit the 'else' code if present.
146 if (const Stmt *Else = S.getElse()) {
147 EmitBlock(ElseBlock);
148 EmitStmt(Else);
Devang Pateld9363c32007-09-28 21:49:18 +0000149 llvm::BasicBlock *BB = Builder.GetInsertBlock();
150 if (isDummyBlock(BB)) {
151 BB->eraseFromParent();
152 Builder.SetInsertPoint(ElseBlock);
153 }
154 else
155 Builder.CreateBr(ContBlock);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157
158 // Emit the continuation block for code after the if.
159 EmitBlock(ContBlock);
160}
161
162void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 // Emit the header for the loop, insert it, which will create an uncond br to
164 // it.
165 llvm::BasicBlock *LoopHeader = new llvm::BasicBlock("whilecond");
166 EmitBlock(LoopHeader);
167
168 // Evaluate the conditional in the while header. C99 6.8.5.1: The evaluation
169 // of the controlling expression takes place before each execution of the loop
170 // body.
171 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
172
173 // TODO: while(1) is common, avoid extra exit blocks, etc. Be sure
174 // to correctly handle break/continue though.
175
176 // Create an exit block for when the condition fails, create a block for the
177 // body of the loop.
178 llvm::BasicBlock *ExitBlock = new llvm::BasicBlock("whileexit");
179 llvm::BasicBlock *LoopBody = new llvm::BasicBlock("whilebody");
180
181 // As long as the condition is true, go to the loop body.
182 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
Chris Lattnerda138702007-07-16 21:28:45 +0000183
184 // Store the blocks to use for break and continue.
185 BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
Reid Spencer5f016e22007-07-11 17:01:13 +0000186
187 // Emit the loop body.
188 EmitBlock(LoopBody);
189 EmitStmt(S.getBody());
Chris Lattnerda138702007-07-16 21:28:45 +0000190
191 BreakContinueStack.pop_back();
Reid Spencer5f016e22007-07-11 17:01:13 +0000192
193 // Cycle to the condition.
194 Builder.CreateBr(LoopHeader);
195
196 // Emit the exit block.
197 EmitBlock(ExitBlock);
198}
199
200void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 // Emit the body for the loop, insert it, which will create an uncond br to
202 // it.
203 llvm::BasicBlock *LoopBody = new llvm::BasicBlock("dobody");
204 llvm::BasicBlock *AfterDo = new llvm::BasicBlock("afterdo");
205 EmitBlock(LoopBody);
Chris Lattnerda138702007-07-16 21:28:45 +0000206
207 llvm::BasicBlock *DoCond = new llvm::BasicBlock("docond");
208
209 // Store the blocks to use for break and continue.
210 BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond));
Reid Spencer5f016e22007-07-11 17:01:13 +0000211
212 // Emit the body of the loop into the block.
213 EmitStmt(S.getBody());
214
Chris Lattnerda138702007-07-16 21:28:45 +0000215 BreakContinueStack.pop_back();
216
217 EmitBlock(DoCond);
218
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 // C99 6.8.5.2: "The evaluation of the controlling expression takes place
220 // after each execution of the loop body."
221
222 // Evaluate the conditional in the while header.
223 // C99 6.8.5p2/p4: The first substatement is executed if the expression
224 // compares unequal to 0. The condition must be a scalar type.
225 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Devang Patel05f6e6b2007-10-09 20:33:39 +0000226
227 // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
228 // to correctly handle break/continue though.
229 bool EmitBoolCondBranch = true;
230 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
231 if (C->isZero())
232 EmitBoolCondBranch = false;
233
234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 // As long as the condition is true, iterate the loop.
Devang Patel05f6e6b2007-10-09 20:33:39 +0000236 if (EmitBoolCondBranch)
237 Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000238
239 // Emit the exit block.
240 EmitBlock(AfterDo);
Devang Patel05f6e6b2007-10-09 20:33:39 +0000241
242 // If DoCond is a simple forwarding block then eliminate it.
243 if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) {
244 DoCond->replaceAllUsesWith(AfterDo);
245 DoCond->getTerminator()->eraseFromParent();
246 DoCond->eraseFromParent();
247 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000248}
249
250void CodeGenFunction::EmitForStmt(const ForStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
252 // which contains a continue/break?
Chris Lattnerda138702007-07-16 21:28:45 +0000253 // TODO: We could keep track of whether the loop body contains any
254 // break/continue statements and not create unnecessary blocks (like
255 // "afterfor" for a condless loop) if it doesn't.
256
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 // Evaluate the first part before the loop.
258 if (S.getInit())
259 EmitStmt(S.getInit());
260
261 // Start the loop with a block that tests the condition.
262 llvm::BasicBlock *CondBlock = new llvm::BasicBlock("forcond");
Chris Lattnerda138702007-07-16 21:28:45 +0000263 llvm::BasicBlock *AfterFor = new llvm::BasicBlock("afterfor");
264
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 EmitBlock(CondBlock);
266
267 // Evaluate the condition if present. If not, treat it as a non-zero-constant
268 // according to 6.8.5.3p2, aka, true.
269 if (S.getCond()) {
270 // C99 6.8.5p2/p4: The first substatement is executed if the expression
271 // compares unequal to 0. The condition must be a scalar type.
272 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
273
274 // As long as the condition is true, iterate the loop.
275 llvm::BasicBlock *ForBody = new llvm::BasicBlock("forbody");
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor);
277 EmitBlock(ForBody);
278 } else {
279 // Treat it as a non-zero constant. Don't even create a new block for the
280 // body, just fall into it.
281 }
282
Chris Lattnerda138702007-07-16 21:28:45 +0000283 // If the for loop doesn't have an increment we can just use the
284 // condition as the continue block.
285 llvm::BasicBlock *ContinueBlock;
286 if (S.getInc())
287 ContinueBlock = new llvm::BasicBlock("forinc");
288 else
289 ContinueBlock = CondBlock;
290
291 // Store the blocks to use for break and continue.
292 BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock));
293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 // If the condition is true, execute the body of the for stmt.
295 EmitStmt(S.getBody());
Chris Lattnerda138702007-07-16 21:28:45 +0000296
297 BreakContinueStack.pop_back();
298
299 if (S.getInc())
300 EmitBlock(ContinueBlock);
Reid Spencer5f016e22007-07-11 17:01:13 +0000301
302 // If there is an increment, emit it next.
303 if (S.getInc())
Chris Lattner883f6a72007-08-11 00:04:45 +0000304 EmitStmt(S.getInc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000305
306 // Finally, branch back up to the condition for the next iteration.
307 Builder.CreateBr(CondBlock);
308
Chris Lattnerda138702007-07-16 21:28:45 +0000309 // Emit the fall-through block.
310 EmitBlock(AfterFor);
Reid Spencer5f016e22007-07-11 17:01:13 +0000311}
312
313/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
314/// if the function returns void, or may be missing one if the function returns
315/// non-void. Fun stuff :).
316void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 // Emit the result value, even if unused, to evalute the side effects.
318 const Expr *RV = S.getRetValue();
Chris Lattner4b0029d2007-08-26 07:14:44 +0000319
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 QualType FnRetTy = CurFuncDecl->getType().getCanonicalType();
321 FnRetTy = cast<FunctionType>(FnRetTy)->getResultType();
322
323 if (FnRetTy->isVoidType()) {
Chris Lattner4b0029d2007-08-26 07:14:44 +0000324 // If the function returns void, emit ret void.
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 Builder.CreateRetVoid();
326 } else if (RV == 0) {
Chris Lattner4b0029d2007-08-26 07:14:44 +0000327 // Handle "return;" in a function that returns a value.
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 const llvm::Type *RetTy = CurFn->getFunctionType()->getReturnType();
329 if (RetTy == llvm::Type::VoidTy)
330 Builder.CreateRetVoid(); // struct return etc.
331 else
332 Builder.CreateRet(llvm::UndefValue::get(RetTy));
Chris Lattner4b0029d2007-08-26 07:14:44 +0000333 } else if (!hasAggregateLLVMType(RV->getType())) {
334 Builder.CreateRet(EmitScalarExpr(RV));
335 } else if (RV->getType()->isComplexType()) {
336 llvm::Value *SRetPtr = CurFn->arg_begin();
Chris Lattner190dbe22007-08-26 16:22:13 +0000337 EmitComplexExprIntoAddr(RV, SRetPtr, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 } else {
Chris Lattner4b0029d2007-08-26 07:14:44 +0000339 llvm::Value *SRetPtr = CurFn->arg_begin();
340 EmitAggExpr(RV, SRetPtr, false);
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 }
342
343 // Emit a block after the branch so that dead code after a return has some
344 // place to go.
345 EmitBlock(new llvm::BasicBlock());
346}
347
348void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
Steve Naroff94745042007-09-13 23:52:58 +0000349 for (const ScopedDecl *Decl = S.getDecl(); Decl;
350 Decl = Decl->getNextDeclarator())
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 EmitDecl(*Decl);
Chris Lattner6fa5f092007-07-12 15:43:07 +0000352}
Chris Lattnerda138702007-07-16 21:28:45 +0000353
354void CodeGenFunction::EmitBreakStmt() {
355 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
356
357 llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
358 Builder.CreateBr(Block);
359 EmitBlock(new llvm::BasicBlock());
360}
361
362void CodeGenFunction::EmitContinueStmt() {
363 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
364
365 llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
366 Builder.CreateBr(Block);
367 EmitBlock(new llvm::BasicBlock());
368}
Devang Patel51b09f22007-10-04 23:45:31 +0000369
Devang Patelc049e4f2007-10-08 20:57:48 +0000370/// EmitCaseStmtRange - If case statement range is not too big then
371/// add multiple cases to switch instruction, one for each value within
372/// the range. If range is too big then emit "if" condition check.
373void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
374 assert (S.getRHS() && "Unexpected RHS value in CaseStmt");
375
376 const Expr *L = S.getLHS();
377 const Expr *R = S.getRHS();
378 llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(L));
379 llvm::ConstantInt *RV = cast<llvm::ConstantInt>(EmitScalarExpr(R));
380 llvm::APInt LHS = LV->getValue();
Devang Patel00ee4e42007-10-09 17:10:59 +0000381 const llvm::APInt &RHS = RV->getValue();
Devang Patelc049e4f2007-10-08 20:57:48 +0000382
383 llvm::APInt Range = RHS - LHS;
384 if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
385 // Range is small enough to add multiple switch instruction cases.
386 StartBlock("sw.bb");
387 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
388 SwitchInsn->addCase(LV, CaseDest);
Devang Patel2d79d0f2007-10-05 20:54:07 +0000389 LHS++;
390 while (LHS != RHS) {
391 SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
392 LHS++;
393 }
Devang Patelc049e4f2007-10-08 20:57:48 +0000394 SwitchInsn->addCase(RV, CaseDest);
395 EmitStmt(S.getSubStmt());
396 return;
397 }
398
399 // The range is too big. Emit "if" condition.
400 llvm::BasicBlock *FalseDest = NULL;
401 llvm::BasicBlock *CaseDest = new llvm::BasicBlock("sw.bb");
Devang Patel2d79d0f2007-10-05 20:54:07 +0000402
Devang Patelc049e4f2007-10-08 20:57:48 +0000403 // If we have already seen one case statement range for this switch
404 // instruction then piggy-back otherwise use default block as false
405 // destination.
406 if (CaseRangeBlock)
407 FalseDest = CaseRangeBlock;
408 else
409 FalseDest = SwitchInsn->getDefaultDest();
410
411 // Start new block to hold case statement range check instructions.
412 StartBlock("case.range");
413 CaseRangeBlock = Builder.GetInsertBlock();
414
415 // Emit range check.
416 llvm::Value *Diff =
417 Builder.CreateSub(SwitchInsn->getCondition(), LV, "tmp");
418 llvm::Value *Cond =
419 Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp");
420 Builder.CreateCondBr(Cond, CaseDest, FalseDest);
421
422 // Now emit case statement body.
423 EmitBlock(CaseDest);
424 EmitStmt(S.getSubStmt());
425}
426
427void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
428 if (S.getRHS()) {
429 EmitCaseStmtRange(S);
430 return;
431 }
432
433 StartBlock("sw.bb");
434 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
435 llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(S.getLHS()));
436 SwitchInsn->addCase(LV, CaseDest);
Devang Patel51b09f22007-10-04 23:45:31 +0000437 EmitStmt(S.getSubStmt());
438}
439
440void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
441 StartBlock("sw.default");
442 // Current insert block is the default destination.
443 SwitchInsn->setSuccessor(0, Builder.GetInsertBlock());
444 EmitStmt(S.getSubStmt());
445}
446
447void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
448 llvm::Value *CondV = EmitScalarExpr(S.getCond());
449
450 // Handle nested switch statements.
451 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
Devang Patelc049e4f2007-10-08 20:57:48 +0000452 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
453 CaseRangeBlock = NULL;
Devang Patel51b09f22007-10-04 23:45:31 +0000454
455 // Create basic block to hold stuff that comes after switch statement.
456 // Initially use it to hold DefaultStmt.
457 llvm::BasicBlock *NextBlock = new llvm::BasicBlock("after.sw", CurFn);
458 SwitchInsn = Builder.CreateSwitch(CondV, NextBlock);
459
460 // All break statements jump to NextBlock. If BreakContinueStack is non empty then
461 // reuse last ContinueBlock.
462 llvm::BasicBlock *ContinueBlock = NULL;
463 if (!BreakContinueStack.empty())
464 ContinueBlock = BreakContinueStack.back().ContinueBlock;
465 BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock));
466
467 // Emit switch body.
468 EmitStmt(S.getBody());
469 BreakContinueStack.pop_back();
470
Devang Patelc049e4f2007-10-08 20:57:48 +0000471 // If one or more case statement range is seen then use CaseRangeBlock
472 // as the default block. False edge of CaseRangeBlock will lead to
473 // original default block.
474 if (CaseRangeBlock)
475 SwitchInsn->setSuccessor(0, CaseRangeBlock);
476
Devang Patel51b09f22007-10-04 23:45:31 +0000477 // Prune insert block if it is dummy.
478 llvm::BasicBlock *BB = Builder.GetInsertBlock();
479 if (isDummyBlock(BB))
480 BB->eraseFromParent();
481
482 // Place NextBlock as the new insert point.
483 Builder.SetInsertPoint(NextBlock);
484 SwitchInsn = SavedSwitchInsn;
Devang Patelc049e4f2007-10-08 20:57:48 +0000485 CaseRangeBlock = SavedCRBlock;
Devang Patel51b09f22007-10-04 23:45:31 +0000486}