Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // Statement Emission |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | void CodeGenFunction::EmitStmt(const Stmt *S) { |
| 27 | assert(S && "Null statement?"); |
| 28 | |
| 29 | switch (S->getStmtClass()) { |
| 30 | default: |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 31 | // Must be an expression in a stmt context. Emit the value (to get |
| 32 | // side-effects) and ignore the result. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Chris Lattner | 1e4d21e | 2007-08-26 22:58:05 +0000 | [diff] [blame] | 34 | if (!hasAggregateLLVMType(E->getType())) |
| 35 | EmitScalarExpr(E); |
| 36 | else if (E->getType()->isComplexType()) |
| 37 | EmitComplexExpr(E); |
| 38 | else |
| 39 | EmitAggExpr(E, 0, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | } else { |
| 41 | printf("Unimplemented stmt!\n"); |
Chris Lattner | 419ea7e | 2007-09-13 01:17:29 +0000 | [diff] [blame] | 42 | S->dump(getContext().SourceMgr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | } |
| 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 Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 57 | |
| 58 | case Stmt::BreakStmtClass: EmitBreakStmt(); break; |
| 59 | case Stmt::ContinueStmtClass: EmitContinueStmt(); break; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 60 | 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 Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 66 | /// 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 Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 69 | RValue CodeGenFunction::EmitCompoundStmt(const CompoundStmt &S, bool GetLast, |
| 70 | llvm::Value *AggLoc, bool isAggVol) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | // FIXME: handle vla's etc. |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 72 | if (S.body_empty() || !isa<Expr>(S.body_back())) GetLast = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 74 | for (CompoundStmt::const_body_iterator I = S.body_begin(), |
| 75 | E = S.body_end()-GetLast; I != E; ++I) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | EmitStmt(*I); |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 77 | |
| 78 | |
| 79 | if (!GetLast) |
| 80 | return RValue::get(0); |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 81 | |
| 82 | return EmitAnyExpr(cast<Expr>(S.body_back()), AggLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void 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 | |
| 104 | void CodeGenFunction::EmitLabelStmt(const LabelStmt &S) { |
| 105 | llvm::BasicBlock *NextBB = getBasicBlockForLabel(&S); |
| 106 | |
| 107 | EmitBlock(NextBB); |
| 108 | EmitStmt(S.getSubStmt()); |
| 109 | } |
| 110 | |
| 111 | void 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 | |
| 119 | void 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 Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 137 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 138 | if (isDummyBlock(BB)) { |
| 139 | BB->eraseFromParent(); |
| 140 | Builder.SetInsertPoint(ThenBlock); |
| 141 | } |
| 142 | else |
| 143 | Builder.CreateBr(ContBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | |
| 145 | // Emit the 'else' code if present. |
| 146 | if (const Stmt *Else = S.getElse()) { |
| 147 | EmitBlock(ElseBlock); |
| 148 | EmitStmt(Else); |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 149 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 150 | if (isDummyBlock(BB)) { |
| 151 | BB->eraseFromParent(); |
| 152 | Builder.SetInsertPoint(ElseBlock); |
| 153 | } |
| 154 | else |
| 155 | Builder.CreateBr(ContBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // Emit the continuation block for code after the if. |
| 159 | EmitBlock(ContBlock); |
| 160 | } |
| 161 | |
| 162 | void CodeGenFunction::EmitWhileStmt(const WhileStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | // 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()); |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame^] | 172 | |
| 173 | // while(1) is common, avoid extra exit blocks. Be sure |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | // to correctly handle break/continue though. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame^] | 175 | bool EmitBoolCondBranch = true; |
| 176 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 177 | if (C->isOne()) |
| 178 | EmitBoolCondBranch = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | |
| 180 | // Create an exit block for when the condition fails, create a block for the |
| 181 | // body of the loop. |
| 182 | llvm::BasicBlock *ExitBlock = new llvm::BasicBlock("whileexit"); |
| 183 | llvm::BasicBlock *LoopBody = new llvm::BasicBlock("whilebody"); |
| 184 | |
| 185 | // As long as the condition is true, go to the loop body. |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame^] | 186 | if (EmitBoolCondBranch) |
| 187 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 188 | |
| 189 | // Store the blocks to use for break and continue. |
| 190 | BreakContinueStack.push_back(BreakContinue(ExitBlock, LoopHeader)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | |
| 192 | // Emit the loop body. |
| 193 | EmitBlock(LoopBody); |
| 194 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 195 | |
| 196 | BreakContinueStack.pop_back(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | |
| 198 | // Cycle to the condition. |
| 199 | Builder.CreateBr(LoopHeader); |
| 200 | |
| 201 | // Emit the exit block. |
| 202 | EmitBlock(ExitBlock); |
Devang Patel | 2c30d8f | 2007-10-09 20:51:27 +0000 | [diff] [blame^] | 203 | |
| 204 | // If LoopHeader is a simple forwarding block then eliminate it. |
| 205 | if (!EmitBoolCondBranch |
| 206 | && &LoopHeader->front() == LoopHeader->getTerminator()) { |
| 207 | LoopHeader->replaceAllUsesWith(LoopBody); |
| 208 | LoopHeader->getTerminator()->eraseFromParent(); |
| 209 | LoopHeader->eraseFromParent(); |
| 210 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void CodeGenFunction::EmitDoStmt(const DoStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | // Emit the body for the loop, insert it, which will create an uncond br to |
| 215 | // it. |
| 216 | llvm::BasicBlock *LoopBody = new llvm::BasicBlock("dobody"); |
| 217 | llvm::BasicBlock *AfterDo = new llvm::BasicBlock("afterdo"); |
| 218 | EmitBlock(LoopBody); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 219 | |
| 220 | llvm::BasicBlock *DoCond = new llvm::BasicBlock("docond"); |
| 221 | |
| 222 | // Store the blocks to use for break and continue. |
| 223 | BreakContinueStack.push_back(BreakContinue(AfterDo, DoCond)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | |
| 225 | // Emit the body of the loop into the block. |
| 226 | EmitStmt(S.getBody()); |
| 227 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 228 | BreakContinueStack.pop_back(); |
| 229 | |
| 230 | EmitBlock(DoCond); |
| 231 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 232 | // C99 6.8.5.2: "The evaluation of the controlling expression takes place |
| 233 | // after each execution of the loop body." |
| 234 | |
| 235 | // Evaluate the conditional in the while header. |
| 236 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 237 | // compares unequal to 0. The condition must be a scalar type. |
| 238 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 239 | |
| 240 | // "do {} while (0)" is common in macros, avoid extra blocks. Be sure |
| 241 | // to correctly handle break/continue though. |
| 242 | bool EmitBoolCondBranch = true; |
| 243 | if (llvm::ConstantInt *C = dyn_cast<llvm::ConstantInt>(BoolCondVal)) |
| 244 | if (C->isZero()) |
| 245 | EmitBoolCondBranch = false; |
| 246 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | // As long as the condition is true, iterate the loop. |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 248 | if (EmitBoolCondBranch) |
| 249 | Builder.CreateCondBr(BoolCondVal, LoopBody, AfterDo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | |
| 251 | // Emit the exit block. |
| 252 | EmitBlock(AfterDo); |
Devang Patel | 05f6e6b | 2007-10-09 20:33:39 +0000 | [diff] [blame] | 253 | |
| 254 | // If DoCond is a simple forwarding block then eliminate it. |
| 255 | if (!EmitBoolCondBranch && &DoCond->front() == DoCond->getTerminator()) { |
| 256 | DoCond->replaceAllUsesWith(AfterDo); |
| 257 | DoCond->getTerminator()->eraseFromParent(); |
| 258 | DoCond->eraseFromParent(); |
| 259 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void CodeGenFunction::EmitForStmt(const ForStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | // FIXME: What do we do if the increment (f.e.) contains a stmt expression, |
| 264 | // which contains a continue/break? |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 265 | // TODO: We could keep track of whether the loop body contains any |
| 266 | // break/continue statements and not create unnecessary blocks (like |
| 267 | // "afterfor" for a condless loop) if it doesn't. |
| 268 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | // Evaluate the first part before the loop. |
| 270 | if (S.getInit()) |
| 271 | EmitStmt(S.getInit()); |
| 272 | |
| 273 | // Start the loop with a block that tests the condition. |
| 274 | llvm::BasicBlock *CondBlock = new llvm::BasicBlock("forcond"); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 275 | llvm::BasicBlock *AfterFor = new llvm::BasicBlock("afterfor"); |
| 276 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | EmitBlock(CondBlock); |
| 278 | |
| 279 | // Evaluate the condition if present. If not, treat it as a non-zero-constant |
| 280 | // according to 6.8.5.3p2, aka, true. |
| 281 | if (S.getCond()) { |
| 282 | // C99 6.8.5p2/p4: The first substatement is executed if the expression |
| 283 | // compares unequal to 0. The condition must be a scalar type. |
| 284 | llvm::Value *BoolCondVal = EvaluateExprAsBool(S.getCond()); |
| 285 | |
| 286 | // As long as the condition is true, iterate the loop. |
| 287 | llvm::BasicBlock *ForBody = new llvm::BasicBlock("forbody"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | Builder.CreateCondBr(BoolCondVal, ForBody, AfterFor); |
| 289 | EmitBlock(ForBody); |
| 290 | } else { |
| 291 | // Treat it as a non-zero constant. Don't even create a new block for the |
| 292 | // body, just fall into it. |
| 293 | } |
| 294 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 295 | // If the for loop doesn't have an increment we can just use the |
| 296 | // condition as the continue block. |
| 297 | llvm::BasicBlock *ContinueBlock; |
| 298 | if (S.getInc()) |
| 299 | ContinueBlock = new llvm::BasicBlock("forinc"); |
| 300 | else |
| 301 | ContinueBlock = CondBlock; |
| 302 | |
| 303 | // Store the blocks to use for break and continue. |
| 304 | BreakContinueStack.push_back(BreakContinue(AfterFor, ContinueBlock)); |
| 305 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | // If the condition is true, execute the body of the for stmt. |
| 307 | EmitStmt(S.getBody()); |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 308 | |
| 309 | BreakContinueStack.pop_back(); |
| 310 | |
| 311 | if (S.getInc()) |
| 312 | EmitBlock(ContinueBlock); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 313 | |
| 314 | // If there is an increment, emit it next. |
| 315 | if (S.getInc()) |
Chris Lattner | 883f6a7 | 2007-08-11 00:04:45 +0000 | [diff] [blame] | 316 | EmitStmt(S.getInc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 317 | |
| 318 | // Finally, branch back up to the condition for the next iteration. |
| 319 | Builder.CreateBr(CondBlock); |
| 320 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 321 | // Emit the fall-through block. |
| 322 | EmitBlock(AfterFor); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /// EmitReturnStmt - Note that due to GCC extensions, this can have an operand |
| 326 | /// if the function returns void, or may be missing one if the function returns |
| 327 | /// non-void. Fun stuff :). |
| 328 | void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | // Emit the result value, even if unused, to evalute the side effects. |
| 330 | const Expr *RV = S.getRetValue(); |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 331 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 332 | QualType FnRetTy = CurFuncDecl->getType().getCanonicalType(); |
| 333 | FnRetTy = cast<FunctionType>(FnRetTy)->getResultType(); |
| 334 | |
| 335 | if (FnRetTy->isVoidType()) { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 336 | // If the function returns void, emit ret void. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | Builder.CreateRetVoid(); |
| 338 | } else if (RV == 0) { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 339 | // Handle "return;" in a function that returns a value. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | const llvm::Type *RetTy = CurFn->getFunctionType()->getReturnType(); |
| 341 | if (RetTy == llvm::Type::VoidTy) |
| 342 | Builder.CreateRetVoid(); // struct return etc. |
| 343 | else |
| 344 | Builder.CreateRet(llvm::UndefValue::get(RetTy)); |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 345 | } else if (!hasAggregateLLVMType(RV->getType())) { |
| 346 | Builder.CreateRet(EmitScalarExpr(RV)); |
| 347 | } else if (RV->getType()->isComplexType()) { |
| 348 | llvm::Value *SRetPtr = CurFn->arg_begin(); |
Chris Lattner | 190dbe2 | 2007-08-26 16:22:13 +0000 | [diff] [blame] | 349 | EmitComplexExprIntoAddr(RV, SRetPtr, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 350 | } else { |
Chris Lattner | 4b0029d | 2007-08-26 07:14:44 +0000 | [diff] [blame] | 351 | llvm::Value *SRetPtr = CurFn->arg_begin(); |
| 352 | EmitAggExpr(RV, SRetPtr, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Emit a block after the branch so that dead code after a return has some |
| 356 | // place to go. |
| 357 | EmitBlock(new llvm::BasicBlock()); |
| 358 | } |
| 359 | |
| 360 | void CodeGenFunction::EmitDeclStmt(const DeclStmt &S) { |
Steve Naroff | 9474504 | 2007-09-13 23:52:58 +0000 | [diff] [blame] | 361 | for (const ScopedDecl *Decl = S.getDecl(); Decl; |
| 362 | Decl = Decl->getNextDeclarator()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | EmitDecl(*Decl); |
Chris Lattner | 6fa5f09 | 2007-07-12 15:43:07 +0000 | [diff] [blame] | 364 | } |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 365 | |
| 366 | void CodeGenFunction::EmitBreakStmt() { |
| 367 | assert(!BreakContinueStack.empty() && "break stmt not in a loop or switch!"); |
| 368 | |
| 369 | llvm::BasicBlock *Block = BreakContinueStack.back().BreakBlock; |
| 370 | Builder.CreateBr(Block); |
| 371 | EmitBlock(new llvm::BasicBlock()); |
| 372 | } |
| 373 | |
| 374 | void CodeGenFunction::EmitContinueStmt() { |
| 375 | assert(!BreakContinueStack.empty() && "continue stmt not in a loop!"); |
| 376 | |
| 377 | llvm::BasicBlock *Block = BreakContinueStack.back().ContinueBlock; |
| 378 | Builder.CreateBr(Block); |
| 379 | EmitBlock(new llvm::BasicBlock()); |
| 380 | } |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 381 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 382 | /// EmitCaseStmtRange - If case statement range is not too big then |
| 383 | /// add multiple cases to switch instruction, one for each value within |
| 384 | /// the range. If range is too big then emit "if" condition check. |
| 385 | void CodeGenFunction::EmitCaseStmtRange(const CaseStmt &S) { |
| 386 | assert (S.getRHS() && "Unexpected RHS value in CaseStmt"); |
| 387 | |
| 388 | const Expr *L = S.getLHS(); |
| 389 | const Expr *R = S.getRHS(); |
| 390 | llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(L)); |
| 391 | llvm::ConstantInt *RV = cast<llvm::ConstantInt>(EmitScalarExpr(R)); |
| 392 | llvm::APInt LHS = LV->getValue(); |
Devang Patel | 00ee4e4 | 2007-10-09 17:10:59 +0000 | [diff] [blame] | 393 | const llvm::APInt &RHS = RV->getValue(); |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 394 | |
| 395 | llvm::APInt Range = RHS - LHS; |
| 396 | if (Range.ult(llvm::APInt(Range.getBitWidth(), 64))) { |
| 397 | // Range is small enough to add multiple switch instruction cases. |
| 398 | StartBlock("sw.bb"); |
| 399 | llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); |
| 400 | SwitchInsn->addCase(LV, CaseDest); |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 401 | LHS++; |
| 402 | while (LHS != RHS) { |
| 403 | SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest); |
| 404 | LHS++; |
| 405 | } |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 406 | SwitchInsn->addCase(RV, CaseDest); |
| 407 | EmitStmt(S.getSubStmt()); |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | // The range is too big. Emit "if" condition. |
| 412 | llvm::BasicBlock *FalseDest = NULL; |
| 413 | llvm::BasicBlock *CaseDest = new llvm::BasicBlock("sw.bb"); |
Devang Patel | 2d79d0f | 2007-10-05 20:54:07 +0000 | [diff] [blame] | 414 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 415 | // If we have already seen one case statement range for this switch |
| 416 | // instruction then piggy-back otherwise use default block as false |
| 417 | // destination. |
| 418 | if (CaseRangeBlock) |
| 419 | FalseDest = CaseRangeBlock; |
| 420 | else |
| 421 | FalseDest = SwitchInsn->getDefaultDest(); |
| 422 | |
| 423 | // Start new block to hold case statement range check instructions. |
| 424 | StartBlock("case.range"); |
| 425 | CaseRangeBlock = Builder.GetInsertBlock(); |
| 426 | |
| 427 | // Emit range check. |
| 428 | llvm::Value *Diff = |
| 429 | Builder.CreateSub(SwitchInsn->getCondition(), LV, "tmp"); |
| 430 | llvm::Value *Cond = |
| 431 | Builder.CreateICmpULE(Diff, llvm::ConstantInt::get(Range), "tmp"); |
| 432 | Builder.CreateCondBr(Cond, CaseDest, FalseDest); |
| 433 | |
| 434 | // Now emit case statement body. |
| 435 | EmitBlock(CaseDest); |
| 436 | EmitStmt(S.getSubStmt()); |
| 437 | } |
| 438 | |
| 439 | void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) { |
| 440 | if (S.getRHS()) { |
| 441 | EmitCaseStmtRange(S); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | StartBlock("sw.bb"); |
| 446 | llvm::BasicBlock *CaseDest = Builder.GetInsertBlock(); |
| 447 | llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(S.getLHS())); |
| 448 | SwitchInsn->addCase(LV, CaseDest); |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 449 | EmitStmt(S.getSubStmt()); |
| 450 | } |
| 451 | |
| 452 | void CodeGenFunction::EmitDefaultStmt(const DefaultStmt &S) { |
| 453 | StartBlock("sw.default"); |
| 454 | // Current insert block is the default destination. |
| 455 | SwitchInsn->setSuccessor(0, Builder.GetInsertBlock()); |
| 456 | EmitStmt(S.getSubStmt()); |
| 457 | } |
| 458 | |
| 459 | void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) { |
| 460 | llvm::Value *CondV = EmitScalarExpr(S.getCond()); |
| 461 | |
| 462 | // Handle nested switch statements. |
| 463 | llvm::SwitchInst *SavedSwitchInsn = SwitchInsn; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 464 | llvm::BasicBlock *SavedCRBlock = CaseRangeBlock; |
| 465 | CaseRangeBlock = NULL; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 466 | |
| 467 | // Create basic block to hold stuff that comes after switch statement. |
| 468 | // Initially use it to hold DefaultStmt. |
| 469 | llvm::BasicBlock *NextBlock = new llvm::BasicBlock("after.sw", CurFn); |
| 470 | SwitchInsn = Builder.CreateSwitch(CondV, NextBlock); |
| 471 | |
| 472 | // All break statements jump to NextBlock. If BreakContinueStack is non empty then |
| 473 | // reuse last ContinueBlock. |
| 474 | llvm::BasicBlock *ContinueBlock = NULL; |
| 475 | if (!BreakContinueStack.empty()) |
| 476 | ContinueBlock = BreakContinueStack.back().ContinueBlock; |
| 477 | BreakContinueStack.push_back(BreakContinue(NextBlock, ContinueBlock)); |
| 478 | |
| 479 | // Emit switch body. |
| 480 | EmitStmt(S.getBody()); |
| 481 | BreakContinueStack.pop_back(); |
| 482 | |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 483 | // If one or more case statement range is seen then use CaseRangeBlock |
| 484 | // as the default block. False edge of CaseRangeBlock will lead to |
| 485 | // original default block. |
| 486 | if (CaseRangeBlock) |
| 487 | SwitchInsn->setSuccessor(0, CaseRangeBlock); |
| 488 | |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 489 | // Prune insert block if it is dummy. |
| 490 | llvm::BasicBlock *BB = Builder.GetInsertBlock(); |
| 491 | if (isDummyBlock(BB)) |
| 492 | BB->eraseFromParent(); |
| 493 | |
| 494 | // Place NextBlock as the new insert point. |
| 495 | Builder.SetInsertPoint(NextBlock); |
| 496 | SwitchInsn = SavedSwitchInsn; |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 497 | CaseRangeBlock = SavedCRBlock; |
Devang Patel | 51b09f2 | 2007-10-04 23:45:31 +0000 | [diff] [blame] | 498 | } |