Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- 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" |
| 19 | using namespace clang; |
| 20 | using namespace CodeGen; |
| 21 | |
Chris Lattner | 22595b8 | 2007-12-02 01:40:18 +0000 | [diff] [blame^] | 22 | #include "clang/Basic/Diagnostic.h" |
| 23 | #include "CodeGenModule.h" |
| 24 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // Statement Emission |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | void CodeGenFunction::EmitStmt(const Stmt *S) { |
| 30 | assert(S && "Null statement?"); |
| 31 | |
| 32 | switch (S->getStmtClass()) { |
| 33 | default: |
Chris Lattner | 35055b8 | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 34 | // Must be an expression in a stmt context. Emit the value (to get |
| 35 | // side-effects) and ignore the result. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 36 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Chris Lattner | 35055b8 | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 37 | if (!hasAggregateLLVMType(E->getType())) |
| 38 | EmitScalarExpr(E); |
| 39 | else if (E->getType()->isComplexType()) |
| 40 | EmitComplexExpr(E); |
| 41 | else |
| 42 | EmitAggExpr(E, 0, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 43 | } else { |
Chris Lattner | 22595b8 | 2007-12-02 01:40:18 +0000 | [diff] [blame^] | 44 | |
| 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 Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 49 | } |
| 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 Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 66 | 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 Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 72 | /// 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 Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 75 | RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, |
| 76 | llvm::Value *AggLoc, bool isAggVol) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 77 | // FIXME: handle vla's etc. |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 78 | if (S.body_empty() || !isa<Expr>(S.body_back())) GetLast = false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 79 | |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 80 | for (CompoundStmt::const_body_iterator I = S.body_begin(), |
| 81 | E = S.body_end()-GetLast; I != E; ++I) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 82 | EmitStmt(*I); |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | if (!GetLast) |
| 86 | return RValue::get(0); |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 87 | |
| 88 | return EmitAnyExpr(cast<Expr>(S.body_back()), AggLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void 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 | |
| 110 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
| 111 | llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S); |
| 112 | |
| 113 | EmitBlock(NextBB); |
| 114 | EmitStmt(S.getSubStmt()); |
| 115 | } |
| 116 | |
| 117 | void 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 | |
| 125 | void 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 Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 143 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 144 | if (isDummyBlock(BB)) { |
| 145 | BB->eraseFromParent(); |
| 146 | Builder.SetInsertPoint(ThenBlock); |
| 147 | } |
| 148 | else |
| 149 | Builder.CreateBr(ContBlock); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 150 | |
| 151 | // Emit the 'else' code if present. |
| 152 | if (const Stmt *Else = S.getElse()) { |
| 153 | EmitBlock(ElseBlock); |
| 154 | EmitStmt(Else); |
Devang Patel | 9729936 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 155 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 156 | if (isDummyBlock(BB)) { |
| 157 | BB->eraseFromParent(); |
| 158 | Builder.SetInsertPoint(ElseBlock); |
| 159 | } |
| 160 | else |
| 161 | Builder.CreateBr(ContBlock); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Emit the continuation block for code after the if. |
| 165 | EmitBlock(ContBlock); |
| 166 | } |
| 167 | |
| 168 | void 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 Patel | 706f844 | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 178 | |
| 179 | // while(1) is common, avoid extra exit blocks. Be sure |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 180 | // to correctly handle break/continue though. |
Devang Patel | 706f844 | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 181 | bool EmitBoolCondBranch = true; |
| 182 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 183 | if (C->isOne()) |
| 184 | EmitBoolCondBranch = false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 185 | |
| 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 Patel | 706f844 | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 192 | if (EmitBoolCondBranch) |
| 193 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 194 | |
| 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 Patel | 706f844 | 2007-10-09 20:51:27 +0000 | [diff] [blame] | 209 | |
| 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 Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void CodeGenFunction::EmitDoStmt(const DoStmt &S) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 220 | // 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 Patel | 716d02c | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 245 | |
| 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 Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 253 | // As long as the condition is true, iterate the loop. |
Devang Patel | 716d02c | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 254 | if (EmitBoolCondBranch) |
| 255 | Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 256 | |
| 257 | // Emit the exit block. |
| 258 | EmitBlock(AfterDo); |
Devang Patel | 716d02c | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 259 | |
| 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 Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void 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 Lattner | bdb8ffb | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 322 | EmitStmt(S.getInc()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 323 | |
| 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 :). |
| 334 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 335 | // Emit the result value, even if unused, to evalute the side effects. |
| 336 | const Expr *RV = S.getRetValue(); |
Chris Lattner | 74b9303 | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 338 | QualType FnRetTy = CurFuncDecl->getType().getCanonicalType(); |
| 339 | FnRetTy = cast<FunctionType>(FnRetTy)->getResultType(); |
| 340 | |
| 341 | if (FnRetTy->isVoidType()) { |
Chris Lattner | 74b9303 | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 342 | // If the function returns void, emit ret void. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 343 | Builder.CreateRetVoid(); |
| 344 | } else if (RV == 0) { |
Chris Lattner | 74b9303 | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 345 | // Handle "return;" in a function that returns a value. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 346 | 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 Lattner | 74b9303 | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 351 | } else if (!hasAggregateLLVMType(RV->getType())) { |
| 352 | Builder.CreateRet(EmitScalarExpr(RV)); |
| 353 | } else if (RV->getType()->isComplexType()) { |
| 354 | llvm::Value *SRetPtr = CurFn->arg_begin(); |
Chris Lattner | 8e1f6e0 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 355 | EmitComplexExprIntoAddr(RV, SRetPtr, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 356 | } else { |
Chris Lattner | 74b9303 | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 357 | llvm::Value *SRetPtr = CurFn->arg_begin(); |
| 358 | EmitAggExpr(RV, SRetPtr, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 359 | } |
| 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 | |
| 366 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
Steve Naroff | 2591e1b | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 367 | for (const ScopedDecl *Decl = S.getDecl(); Decl; |
| 368 | Decl = Decl->getNextDeclarator()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 369 | EmitDecl(*Decl); |
| 370 | } |
| 371 | |
| 372 | void 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 | |
| 380 | void 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 Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 387 | |
Devang Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 388 | /// 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. |
| 391 | void 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 Patel | 74f3535 | 2007-10-09 17:10:59 +0000 | [diff] [blame] | 399 | const llvm::APInt &RHS = RV->getValue(); |
Devang Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 400 | |
| 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 Patel | cf9dbf2 | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 407 | LHS++; |
| 408 | while (LHS != RHS) { |
| 409 | SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest); |
| 410 | LHS++; |
| 411 | } |
Devang Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 412 | 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 Patel | cf9dbf2 | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 420 | |
Devang Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 421 | // 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 | |
| 445 | void 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 Lattner | 860c6c9 | 2007-11-30 17:44:57 +0000 | [diff] [blame] | 453 | llvm::APSInt CaseVal(32); |
| 454 | S.getLHS()->isIntegerConstantExpr(CaseVal, getContext()); |
| 455 | llvm::ConstantInt *LV = llvm::ConstantInt::get(CaseVal); |
Devang Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 456 | SwitchInsn->addCase(LV, CaseDest); |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 457 | EmitStmt(S.getSubStmt()); |
| 458 | } |
| 459 | |
| 460 | void 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 | |
| 467 | void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { |
| 468 | llvm::Value *CondV = EmitScalarExpr(S.getCond()); |
| 469 | |
| 470 | // Handle nested switch statements. |
| 471 | llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; |
Devang Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 472 | llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; |
| 473 | CaseRangeBlock = NULL; |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 474 | |
| 475 | // Create basic block to hold stuff that comes after switch statement. |
| 476 | // Initially use it to hold DefaultStmt. |
Chris Lattner | cc5c8df | 2007-12-01 05:27:33 +0000 | [diff] [blame] | 477 | llvm::BasicBlock *NextBlock = new llvm::BasicBlock("after.sw"); |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 478 | SwitchInsn = Builder.CreateSwitch(CondV, NextBlock); |
| 479 | |
Devang Patel | 0f2a8fb | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 480 | // All break statements jump to NextBlock. If BreakContinueStack is non empty |
| 481 | // then reuse last ContinueBlock. |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 482 | 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 Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 491 | // 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 Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 497 | // Prune insert block if it is dummy. |
| 498 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 499 | if (isDummyBlock(BB)) |
| 500 | BB->eraseFromParent(); |
Chris Lattner | cc5c8df | 2007-12-01 05:27:33 +0000 | [diff] [blame] | 501 | else // Otherwise, branch to continuation. |
| 502 | Builder.CreateBr(NextBlock); |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 503 | |
| 504 | // Place NextBlock as the new insert point. |
Chris Lattner | cc5c8df | 2007-12-01 05:27:33 +0000 | [diff] [blame] | 505 | CurFn->getBasicBlockList().push_back(NextBlock); |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 506 | Builder.SetInsertPoint(NextBlock); |
| 507 | SwitchInsn = SavedSwitchInsn; |
Devang Patel | 347ca32 | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 508 | CaseRangeBlock = SavedCRBlock; |
Devang Patel | e58e080 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 509 | } |