blob: d41d2dec690c4f17ef48e85f321dc5cf35d70ffe [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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
Chris Lattner22595b82007-12-02 01:40:18 +000022#include "clang/Basic/Diagnostic.h"
23#include "CodeGenModule.h"
24
Chris Lattner4b009652007-07-25 00:24:17 +000025//===----------------------------------------------------------------------===//
26// Statement Emission
27//===----------------------------------------------------------------------===//
28
29void CodeGenFunction::EmitStmt(const Stmt *S) {
30 assert(S && "Null statement?");
31
32 switch (S->getStmtClass()) {
33 default:
Chris Lattner35055b82007-08-26 22:58:05 +000034 // Must be an expression in a stmt context. Emit the value (to get
35 // side-effects) and ignore the result.
Chris Lattner4b009652007-07-25 00:24:17 +000036 if (const Expr *E = dyn_cast<Expr>(S)) {
Chris Lattner35055b82007-08-26 22:58:05 +000037 if (!hasAggregateLLVMType(E->getType()))
38 EmitScalarExpr(E);
39 else if (E->getType()->isComplexType())
40 EmitComplexExpr(E);
41 else
42 EmitAggExpr(E, 0, false);
Chris Lattner4b009652007-07-25 00:24:17 +000043 } else {
Chris Lattner22595b82007-12-02 01:40:18 +000044
45 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Warning,
46 "cannot codegen this yet");
47 SourceRange Range = S->getSourceRange();
48 CGM.getDiags().Report(S->getLocStart(), DiagID, 0, 0, &Range, 1);
Chris Lattner4b009652007-07-25 00:24:17 +000049 }
50 break;
51 case Stmt::NullStmtClass: break;
52 case Stmt::CompoundStmtClass: EmitCompoundStmt(cast<CompoundStmt>(*S)); break;
53 case Stmt::LabelStmtClass: EmitLabelStmt(cast<LabelStmt>(*S)); break;
54 case Stmt::GotoStmtClass: EmitGotoStmt(cast<GotoStmt>(*S)); break;
55
56 case Stmt::IfStmtClass: EmitIfStmt(cast<IfStmt>(*S)); break;
57 case Stmt::WhileStmtClass: EmitWhileStmt(cast<WhileStmt>(*S)); break;
58 case Stmt::DoStmtClass: EmitDoStmt(cast<DoStmt>(*S)); break;
59 case Stmt::ForStmtClass: EmitForStmt(cast<ForStmt>(*S)); break;
60
61 case Stmt::ReturnStmtClass: EmitReturnStmt(cast<ReturnStmt>(*S)); break;
62 case Stmt::DeclStmtClass: EmitDeclStmt(cast<DeclStmt>(*S)); break;
63
64 case Stmt::BreakStmtClass: EmitBreakStmt(); break;
65 case Stmt::ContinueStmtClass: EmitContinueStmt(); break;
Devang Patele58e0802007-10-04 23:45:31 +000066 case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
67 case Stmt::DefaultStmtClass: EmitDefaultStmt(cast<DefaultStmt>(*S)); break;
68 case Stmt::CaseStmtClass: EmitCaseStmt(cast<CaseStmt>(*S)); break;
Chris Lattner4b009652007-07-25 00:24:17 +000069 }
70}
71
Chris Lattnerea6cdd72007-08-31 22:09:40 +000072/// EmitCompoundStmt - Emit a compound statement {..} node. If GetLast is true,
73/// this captures the expression result of the last sub-statement and returns it
74/// (for use by the statement expression extension).
Chris Lattnere24c4cf2007-08-31 22:49:20 +000075RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast,
76 llvm::Value *AggLoc, bool isAggVol) {
Chris Lattner4b009652007-07-25 00:24:17 +000077 // FIXME: handle vla's etc.
Chris Lattnerea6cdd72007-08-31 22:09:40 +000078 if (S.body_empty() || !isa<Expr>(S.body_back())) GetLast = false;
Chris Lattner4b009652007-07-25 00:24:17 +000079
Chris Lattnerea6cdd72007-08-31 22:09:40 +000080 for (CompoundStmt::const_body_iterator I = S.body_begin(),
81 E = S.body_end()-GetLast; I != E; ++I)
Chris Lattner4b009652007-07-25 00:24:17 +000082 EmitStmt(*I);
Chris Lattnerea6cdd72007-08-31 22:09:40 +000083
84
85 if (!GetLast)
86 return RValue::get(0);
Chris Lattnere24c4cf2007-08-31 22:49:20 +000087
88 return EmitAnyExpr(cast<Expr>(S.body_back()), AggLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000089}
90
91void CodeGenFunction::EmitBlock(llvm::BasicBlock *BB) {
92 // Emit a branch from this block to the next one if this was a real block. If
93 // this was just a fall-through block after a terminator, don't emit it.
94 llvm::BasicBlock *LastBB = Builder.GetInsertBlock();
95
96 if (LastBB->getTerminator()) {
97 // If the previous block is already terminated, don't touch it.
98 } else if (LastBB->empty() && LastBB->getValueName() == 0) {
99 // If the last block was an empty placeholder, remove it now.
100 // TODO: cache and reuse these.
101 Builder.GetInsertBlock()->eraseFromParent();
102 } else {
103 // Otherwise, create a fall-through branch.
104 Builder.CreateBr(BB);
105 }
106 CurFn->getBasicBlockList().push_back(BB);
107 Builder.SetInsertPoint(BB);
108}
109
110void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) {
111 llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S);
112
113 EmitBlock(NextBB);
114 EmitStmt(S.getSubStmt());
115}
116
117void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
118 Builder.CreateBr(getBasicBlockForLabel(S.getLabel()));
119
120 // Emit a block after the branch so that dead code after a goto has some place
121 // to go.
122 Builder.SetInsertPoint(new llvm::BasicBlock("", CurFn));
123}
124
125void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
126 // C99 6.8.4.1: The first substatement is executed if the expression compares
127 // unequal to 0. The condition must be a scalar type.
128 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
129
130 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("ifend");
131 llvm::BasicBlock *ThenBlock = new llvm::BasicBlock("ifthen");
132 llvm::BasicBlock *ElseBlock = ContBlock;
133
134 if (S.getElse())
135 ElseBlock = new llvm::BasicBlock("ifelse");
136
137 // Insert the conditional branch.
138 Builder.CreateCondBr(BoolCondVal, ThenBlock, ElseBlock);
139
140 // Emit the 'then' code.
141 EmitBlock(ThenBlock);
142 EmitStmt(S.getThen());
Devang Patel97299362007-09-28 21:49:18 +0000143 llvm::BasicBlock *BB = Builder.GetInsertBlock();
144 if (isDummyBlock(BB)) {
145 BB->eraseFromParent();
146 Builder.SetInsertPoint(ThenBlock);
147 }
148 else
149 Builder.CreateBr(ContBlock);
Chris Lattner4b009652007-07-25 00:24:17 +0000150
151 // Emit the 'else' code if present.
152 if (const Stmt *Else = S.getElse()) {
153 EmitBlock(ElseBlock);
154 EmitStmt(Else);
Devang Patel97299362007-09-28 21:49:18 +0000155 llvm::BasicBlock *BB = Builder.GetInsertBlock();
156 if (isDummyBlock(BB)) {
157 BB->eraseFromParent();
158 Builder.SetInsertPoint(ElseBlock);
159 }
160 else
161 Builder.CreateBr(ContBlock);
Chris Lattner4b009652007-07-25 00:24:17 +0000162 }
163
164 // Emit the continuation block for code after the if.
165 EmitBlock(ContBlock);
166}
167
168void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) {
169 // Emit the header for the loop, insert it, which will create an uncond br to
170 // it.
171 llvm::BasicBlock *LoopHeader = new llvm::BasicBlock("whilecond");
172 EmitBlock(LoopHeader);
173
174 // Evaluate the conditional in the while header. C99 6.8.5.1: The evaluation
175 // of the controlling expression takes place before each execution of the loop
176 // body.
177 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Devang Patel706f8442007-10-09 20:51:27 +0000178
179 // while(1) is common, avoid extra exit blocks. Be sure
Chris Lattner4b009652007-07-25 00:24:17 +0000180 // to correctly handle break/continue though.
Devang Patel706f8442007-10-09 20:51:27 +0000181 bool EmitBoolCondBranch = true;
182 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
183 if (C->isOne())
184 EmitBoolCondBranch = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000185
186 // Create an exit block for when the condition fails, create a block for the
187 // body of the loop.
188 llvm::BasicBlock *ExitBlock = new llvm::BasicBlock("whileexit");
189 llvm::BasicBlock *LoopBody = new llvm::BasicBlock("whilebody");
190
191 // As long as the condition is true, go to the loop body.
Devang Patel706f8442007-10-09 20:51:27 +0000192 if (EmitBoolCondBranch)
193 Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock);
Chris Lattner4b009652007-07-25 00:24:17 +0000194
195 // Store the blocks to use for break and continue.
196 BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader));
197
198 // Emit the loop body.
199 EmitBlock(LoopBody);
200 EmitStmt(S.getBody());
201
202 BreakContinueStack.pop_back();
203
204 // Cycle to the condition.
205 Builder.CreateBr(LoopHeader);
206
207 // Emit the exit block.
208 EmitBlock(ExitBlock);
Devang Patel706f8442007-10-09 20:51:27 +0000209
210 // If LoopHeader is a simple forwarding block then eliminate it.
211 if (!EmitBoolCondBranch
212 && &LoopHeader->front() == LoopHeader->getTerminator()) {
213 LoopHeader->replaceAllUsesWith(LoopBody);
214 LoopHeader->getTerminator()->eraseFromParent();
215 LoopHeader->eraseFromParent();
216 }
Chris Lattner4b009652007-07-25 00:24:17 +0000217}
218
219void CodeGenFunction::EmitDoStmt(const DoStmt &S) {
Chris Lattner4b009652007-07-25 00:24:17 +0000220 // Emit the body for the loop, insert it, which will create an uncond br to
221 // it.
222 llvm::BasicBlock *LoopBody = new llvm::BasicBlock("dobody");
223 llvm::BasicBlock *AfterDo = new llvm::BasicBlock("afterdo");
224 EmitBlock(LoopBody);
225
226 llvm::BasicBlock *DoCond = new llvm::BasicBlock("docond");
227
228 // Store the blocks to use for break and continue.
229 BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond));
230
231 // Emit the body of the loop into the block.
232 EmitStmt(S.getBody());
233
234 BreakContinueStack.pop_back();
235
236 EmitBlock(DoCond);
237
238 // C99 6.8.5.2: "The evaluation of the controlling expression takes place
239 // after each execution of the loop body."
240
241 // Evaluate the conditional in the while header.
242 // C99 6.8.5p2/p4: The first substatement is executed if the expression
243 // compares unequal to 0. The condition must be a scalar type.
244 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
Devang Patel716d02c2007-10-09 20:33:39 +0000245
246 // "do {} while (0)" is common in macros, avoid extra blocks. Be sure
247 // to correctly handle break/continue though.
248 bool EmitBoolCondBranch = true;
249 if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal))
250 if (C->isZero())
251 EmitBoolCondBranch = false;
252
Chris Lattner4b009652007-07-25 00:24:17 +0000253 // As long as the condition is true, iterate the loop.
Devang Patel716d02c2007-10-09 20:33:39 +0000254 if (EmitBoolCondBranch)
255 Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo);
Chris Lattner4b009652007-07-25 00:24:17 +0000256
257 // Emit the exit block.
258 EmitBlock(AfterDo);
Devang Patel716d02c2007-10-09 20:33:39 +0000259
260 // If DoCond is a simple forwarding block then eliminate it.
261 if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) {
262 DoCond->replaceAllUsesWith(AfterDo);
263 DoCond->getTerminator()->eraseFromParent();
264 DoCond->eraseFromParent();
265 }
Chris Lattner4b009652007-07-25 00:24:17 +0000266}
267
268void CodeGenFunction::EmitForStmt(const ForStmt &S) {
269 // FIXME: What do we do if the increment (f.e.) contains a stmt expression,
270 // which contains a continue/break?
271 // TODO: We could keep track of whether the loop body contains any
272 // break/continue statements and not create unnecessary blocks (like
273 // "afterfor" for a condless loop) if it doesn't.
274
275 // Evaluate the first part before the loop.
276 if (S.getInit())
277 EmitStmt(S.getInit());
278
279 // Start the loop with a block that tests the condition.
280 llvm::BasicBlock *CondBlock = new llvm::BasicBlock("forcond");
281 llvm::BasicBlock *AfterFor = new llvm::BasicBlock("afterfor");
282
283 EmitBlock(CondBlock);
284
285 // Evaluate the condition if present. If not, treat it as a non-zero-constant
286 // according to 6.8.5.3p2, aka, true.
287 if (S.getCond()) {
288 // C99 6.8.5p2/p4: The first substatement is executed if the expression
289 // compares unequal to 0. The condition must be a scalar type.
290 llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond());
291
292 // As long as the condition is true, iterate the loop.
293 llvm::BasicBlock *ForBody = new llvm::BasicBlock("forbody");
294 Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor);
295 EmitBlock(ForBody);
296 } else {
297 // Treat it as a non-zero constant. Don't even create a new block for the
298 // body, just fall into it.
299 }
300
301 // If the for loop doesn't have an increment we can just use the
302 // condition as the continue block.
303 llvm::BasicBlock *ContinueBlock;
304 if (S.getInc())
305 ContinueBlock = new llvm::BasicBlock("forinc");
306 else
307 ContinueBlock = CondBlock;
308
309 // Store the blocks to use for break and continue.
310 BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock));
311
312 // If the condition is true, execute the body of the for stmt.
313 EmitStmt(S.getBody());
314
315 BreakContinueStack.pop_back();
316
317 if (S.getInc())
318 EmitBlock(ContinueBlock);
319
320 // If there is an increment, emit it next.
321 if (S.getInc())
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000322 EmitStmt(S.getInc());
Chris Lattner4b009652007-07-25 00:24:17 +0000323
324 // Finally, branch back up to the condition for the next iteration.
325 Builder.CreateBr(CondBlock);
326
327 // Emit the fall-through block.
328 EmitBlock(AfterFor);
329}
330
331/// EmitReturnStmt - Note that due to GCC extensions, this can have an operand
332/// if the function returns void, or may be missing one if the function returns
333/// non-void. Fun stuff :).
334void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
Chris Lattner4b009652007-07-25 00:24:17 +0000335 // Emit the result value, even if unused, to evalute the side effects.
336 const Expr *RV = S.getRetValue();
Chris Lattner74b93032007-08-26 07:14:44 +0000337
Chris Lattner4b009652007-07-25 00:24:17 +0000338 QualType FnRetTy = CurFuncDecl->getType().getCanonicalType();
339 FnRetTy = cast<FunctionType>(FnRetTy)->getResultType();
340
341 if (FnRetTy->isVoidType()) {
Chris Lattner74b93032007-08-26 07:14:44 +0000342 // If the function returns void, emit ret void.
Chris Lattner4b009652007-07-25 00:24:17 +0000343 Builder.CreateRetVoid();
344 } else if (RV == 0) {
Chris Lattner74b93032007-08-26 07:14:44 +0000345 // Handle "return;" in a function that returns a value.
Chris Lattner4b009652007-07-25 00:24:17 +0000346 const llvm::Type *RetTy = CurFn->getFunctionType()->getReturnType();
347 if (RetTy == llvm::Type::VoidTy)
348 Builder.CreateRetVoid(); // struct return etc.
349 else
350 Builder.CreateRet(llvm::UndefValue::get(RetTy));
Chris Lattner74b93032007-08-26 07:14:44 +0000351 } else if (!hasAggregateLLVMType(RV->getType())) {
352 Builder.CreateRet(EmitScalarExpr(RV));
353 } else if (RV->getType()->isComplexType()) {
354 llvm::Value *SRetPtr = CurFn->arg_begin();
Chris Lattner8e1f6e02007-08-26 16:22:13 +0000355 EmitComplexExprIntoAddr(RV, SRetPtr, false);
Chris Lattner4b009652007-07-25 00:24:17 +0000356 } else {
Chris Lattner74b93032007-08-26 07:14:44 +0000357 llvm::Value *SRetPtr = CurFn->arg_begin();
358 EmitAggExpr(RV, SRetPtr, false);
Chris Lattner4b009652007-07-25 00:24:17 +0000359 }
360
361 // Emit a block after the branch so that dead code after a return has some
362 // place to go.
363 EmitBlock(new llvm::BasicBlock());
364}
365
366void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) {
Steve Naroff2591e1b2007-09-13 23:52:58 +0000367 for (const ScopedDecl *Decl = S.getDecl(); Decl;
368 Decl = Decl->getNextDeclarator())
Chris Lattner4b009652007-07-25 00:24:17 +0000369 EmitDecl(*Decl);
370}
371
372void CodeGenFunction::EmitBreakStmt() {
373 assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!");
374
375 llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock;
376 Builder.CreateBr(Block);
377 EmitBlock(new llvm::BasicBlock());
378}
379
380void CodeGenFunction::EmitContinueStmt() {
381 assert(!BreakContinueStack.empty() && "continue stmt not in a loop!");
382
383 llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock;
384 Builder.CreateBr(Block);
385 EmitBlock(new llvm::BasicBlock());
386}
Devang Patele58e0802007-10-04 23:45:31 +0000387
Devang Patel347ca322007-10-08 20:57:48 +0000388/// EmitCaseStmtRange - If case statement range is not too big then
389/// add multiple cases to switch instruction, one for each value within
390/// the range. If range is too big then emit "if" condition check.
391void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) {
392 assert (S.getRHS() && "Unexpected RHS value in CaseStmt");
393
394 const Expr *L = S.getLHS();
395 const Expr *R = S.getRHS();
396 llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(L));
397 llvm::ConstantInt *RV = cast<llvm::ConstantInt>(EmitScalarExpr(R));
398 llvm::APInt LHS = LV->getValue();
Devang Patel74f35352007-10-09 17:10:59 +0000399 const llvm::APInt &RHS = RV->getValue();
Devang Patel347ca322007-10-08 20:57:48 +0000400
401 llvm::APInt Range = RHS - LHS;
402 if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) {
403 // Range is small enough to add multiple switch instruction cases.
404 StartBlock("sw.bb");
405 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
406 SwitchInsn->addCase(LV, CaseDest);
Devang Patelcf9dbf22007-10-05 20:54:07 +0000407 LHS++;
408 while (LHS != RHS) {
409 SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
410 LHS++;
411 }
Devang Patel347ca322007-10-08 20:57:48 +0000412 SwitchInsn->addCase(RV, CaseDest);
413 EmitStmt(S.getSubStmt());
414 return;
415 }
416
417 // The range is too big. Emit "if" condition.
418 llvm::BasicBlock *FalseDest = NULL;
419 llvm::BasicBlock *CaseDest = new llvm::BasicBlock("sw.bb");
Devang Patelcf9dbf22007-10-05 20:54:07 +0000420
Devang Patel347ca322007-10-08 20:57:48 +0000421 // If we have already seen one case statement range for this switch
422 // instruction then piggy-back otherwise use default block as false
423 // destination.
424 if (CaseRangeBlock)
425 FalseDest = CaseRangeBlock;
426 else
427 FalseDest = SwitchInsn->getDefaultDest();
428
429 // Start new block to hold case statement range check instructions.
430 StartBlock("case.range");
431 CaseRangeBlock = Builder.GetInsertBlock();
432
433 // Emit range check.
434 llvm::Value *Diff =
435 Builder.CreateSub(SwitchInsn->getCondition(), LV, "tmp");
436 llvm::Value *Cond =
437 Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp");
438 Builder.CreateCondBr(Cond, CaseDest, FalseDest);
439
440 // Now emit case statement body.
441 EmitBlock(CaseDest);
442 EmitStmt(S.getSubStmt());
443}
444
445void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
446 if (S.getRHS()) {
447 EmitCaseStmtRange(S);
448 return;
449 }
450
451 StartBlock("sw.bb");
452 llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
Chris Lattner860c6c92007-11-30 17:44:57 +0000453 llvm::APSInt CaseVal(32);
454 S.getLHS()->isIntegerConstantExpr(CaseVal, getContext());
455 llvm::ConstantInt *LV = llvm::ConstantInt::get(CaseVal);
Devang Patel347ca322007-10-08 20:57:48 +0000456 SwitchInsn->addCase(LV, CaseDest);
Devang Patele58e0802007-10-04 23:45:31 +0000457 EmitStmt(S.getSubStmt());
458}
459
460void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) {
461 StartBlock("sw.default");
462 // Current insert block is the default destination.
463 SwitchInsn->setSuccessor(0, Builder.GetInsertBlock());
464 EmitStmt(S.getSubStmt());
465}
466
467void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
468 llvm::Value *CondV = EmitScalarExpr(S.getCond());
469
470 // Handle nested switch statements.
471 llvm::SwitchInst *SavedSwitchInsn = SwitchInsn;
Devang Patel347ca322007-10-08 20:57:48 +0000472 llvm::BasicBlock *SavedCRBlock = CaseRangeBlock;
473 CaseRangeBlock = NULL;
Devang Patele58e0802007-10-04 23:45:31 +0000474
475 // Create basic block to hold stuff that comes after switch statement.
476 // Initially use it to hold DefaultStmt.
Chris Lattnercc5c8df2007-12-01 05:27:33 +0000477 llvm::BasicBlock *NextBlock = new llvm::BasicBlock("after.sw");
Devang Patele58e0802007-10-04 23:45:31 +0000478 SwitchInsn = Builder.CreateSwitch(CondV, NextBlock);
479
Devang Patel0f2a8fb2007-10-30 20:59:40 +0000480 // All break statements jump to NextBlock. If BreakContinueStack is non empty
481 // then reuse last ContinueBlock.
Devang Patele58e0802007-10-04 23:45:31 +0000482 llvm::BasicBlock *ContinueBlock = NULL;
483 if (!BreakContinueStack.empty())
484 ContinueBlock = BreakContinueStack.back().ContinueBlock;
485 BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock));
486
487 // Emit switch body.
488 EmitStmt(S.getBody());
489 BreakContinueStack.pop_back();
490
Devang Patel347ca322007-10-08 20:57:48 +0000491 // If one or more case statement range is seen then use CaseRangeBlock
492 // as the default block. False edge of CaseRangeBlock will lead to
493 // original default block.
494 if (CaseRangeBlock)
495 SwitchInsn->setSuccessor(0, CaseRangeBlock);
496
Devang Patele58e0802007-10-04 23:45:31 +0000497 // Prune insert block if it is dummy.
498 llvm::BasicBlock *BB = Builder.GetInsertBlock();
499 if (isDummyBlock(BB))
500 BB->eraseFromParent();
Chris Lattnercc5c8df2007-12-01 05:27:33 +0000501 else // Otherwise, branch to continuation.
502 Builder.CreateBr(NextBlock);
Devang Patele58e0802007-10-04 23:45:31 +0000503
504 // Place NextBlock as the new insert point.
Chris Lattnercc5c8df2007-12-01 05:27:33 +0000505 CurFn->getBasicBlockList().push_back(NextBlock);
Devang Patele58e0802007-10-04 23:45:31 +0000506 Builder.SetInsertPoint(NextBlock);
507 SwitchInsn = SavedSwitchInsn;
Devang Patel347ca322007-10-08 20:57:48 +0000508 CaseRangeBlock = SavedCRBlock;
Devang Patele58e0802007-10-04 23:45:31 +0000509}