Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This coordinates the per-function state used while generating code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Eli Friedman | 3f2af10 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 18 | #include "clang/AST/APValue.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Devang Patel | d9363c3 | 2007-09-28 21:49:18 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CFG.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
| 25 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
Devang Patel | c049e4f | 2007-10-08 20:57:48 +0000 | [diff] [blame] | 26 | : CGM(cgm), Target(CGM.getContext().Target), SwitchInsn(NULL), |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 27 | CaseRangeBlock(NULL) { |
| 28 | LLVMIntTy = ConvertType(getContext().IntTy); |
| 29 | LLVMPointerWidth = Target.getPointerWidth(0); |
| 30 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | |
| 32 | ASTContext &CodeGenFunction::getContext() const { |
| 33 | return CGM.getContext(); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | llvm::BasicBlock *CodeGenFunction::getBasicBlockForLabel(const LabelStmt *S) { |
| 38 | llvm::BasicBlock *&BB = LabelMap[S]; |
| 39 | if (BB) return BB; |
| 40 | |
| 41 | // Create, but don't insert, the new block. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 42 | return BB = createBasicBlock(S->getName()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 45 | llvm::Constant * |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 46 | CodeGenFunction::GetAddrOfStaticLocalVar(const VarDecl *BVD) { |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 47 | return cast<llvm::Constant>(LocalDeclMap[BVD]); |
| 48 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 50 | llvm::Value *CodeGenFunction::GetAddrOfLocalVar(const VarDecl *VD) |
| 51 | { |
| 52 | return LocalDeclMap[VD]; |
| 53 | } |
| 54 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | const llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
| 56 | return CGM.getTypes().ConvertType(T); |
| 57 | } |
| 58 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 59 | bool CodeGenFunction::isObjCPointerType(QualType T) { |
| 60 | // All Objective-C types are pointers. |
| 61 | return T->isObjCInterfaceType() || |
| 62 | T->isObjCQualifiedInterfaceType() || T->isObjCQualifiedIdType(); |
| 63 | } |
| 64 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | bool CodeGenFunction::hasAggregateLLVMType(QualType T) { |
Daniel Dunbar | a782ca7 | 2009-01-09 02:44:18 +0000 | [diff] [blame] | 66 | // FIXME: Use positive checks instead of negative ones to be more |
| 67 | // robust in the face of extension. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 68 | return !isObjCPointerType(T) &&!T->isRealType() && !T->isPointerLikeType() && |
Daniel Dunbar | a782ca7 | 2009-01-09 02:44:18 +0000 | [diff] [blame] | 69 | !T->isVoidType() && !T->isVectorType() && !T->isFunctionType() && |
| 70 | !T->isBlockPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 73 | void CodeGenFunction::EmitReturnBlock() { |
| 74 | // For cleanliness, we try to avoid emitting the return block for |
| 75 | // simple cases. |
| 76 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 77 | |
| 78 | if (CurBB) { |
| 79 | assert(!CurBB->getTerminator() && "Unexpected terminated block."); |
| 80 | |
| 81 | // We have a valid insert point, reuse it if there are no explicit |
| 82 | // jumps to the return block. |
| 83 | if (ReturnBlock->use_empty()) |
| 84 | delete ReturnBlock; |
| 85 | else |
| 86 | EmitBlock(ReturnBlock); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // Otherwise, if the return block is the target of a single direct |
| 91 | // branch then we can just put the code in that block instead. This |
| 92 | // cleans up functions which started with a unified return block. |
| 93 | if (ReturnBlock->hasOneUse()) { |
| 94 | llvm::BranchInst *BI = |
| 95 | dyn_cast<llvm::BranchInst>(*ReturnBlock->use_begin()); |
| 96 | if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock) { |
| 97 | // Reset insertion point and delete the branch. |
| 98 | Builder.SetInsertPoint(BI->getParent()); |
| 99 | BI->eraseFromParent(); |
| 100 | delete ReturnBlock; |
| 101 | return; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // FIXME: We are at an unreachable point, there is no reason to emit |
| 106 | // the block unless it has uses. However, we still need a place to |
| 107 | // put the debug region.end for now. |
| 108 | |
| 109 | EmitBlock(ReturnBlock); |
| 110 | } |
| 111 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 112 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 113 | // Finish emission of indirect switches. |
| 114 | EmitIndirectSwitches(); |
| 115 | |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 116 | assert(BreakContinueStack.empty() && |
| 117 | "mismatched push/pop in break/continue stack!"); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 118 | |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 119 | // Emit function epilog (to return). |
| 120 | EmitReturnBlock(); |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 121 | |
| 122 | // Emit debug descriptor for function end. |
| 123 | if (CGDebugInfo *DI = CGM.getDebugInfo()) { |
| 124 | DI->setLocation(EndLoc); |
| 125 | DI->EmitRegionEnd(CurFn, Builder); |
| 126 | } |
| 127 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 128 | EmitFunctionEpilog(FnRetTy, ReturnValue); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 130 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
| 131 | AllocaInsertPt->eraseFromParent(); |
| 132 | AllocaInsertPt = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 135 | void CodeGenFunction::StartFunction(const Decl *D, QualType RetTy, |
| 136 | llvm::Function *Fn, |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 137 | const FunctionArgList &Args, |
| 138 | SourceLocation StartLoc) { |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 139 | CurFuncDecl = D; |
| 140 | FnRetTy = RetTy; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 141 | CurFn = Fn; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 142 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 143 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 144 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 146 | // Create a marker to make it easy to insert allocas into the entryblock |
| 147 | // later. Don't create this with the builder, because we don't want it |
| 148 | // folded. |
| 149 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 150 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 151 | EntryBB); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 152 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 153 | ReturnBlock = createBasicBlock("return"); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 154 | ReturnValue = 0; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 155 | if (!RetTy->isVoidType()) |
| 156 | ReturnValue = CreateTempAlloca(ConvertType(RetTy), "retval"); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 158 | Builder.SetInsertPoint(EntryBB); |
| 159 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 160 | // Emit subprogram debug descriptor. |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 161 | // FIXME: The cast here is a huge hack. |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 162 | if (CGDebugInfo *DI = CGM.getDebugInfo()) { |
| 163 | DI->setLocation(StartLoc); |
| 164 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 165 | DI->EmitFunctionStart(FD->getIdentifier()->getName(), |
| 166 | RetTy, CurFn, Builder); |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 167 | } else { |
| 168 | // Just use LLVM function name. |
| 169 | DI->EmitFunctionStart(Fn->getName().c_str(), |
| 170 | RetTy, CurFn, Builder); |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 171 | } |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Daniel Dunbar | 17b708d | 2008-09-09 23:27:19 +0000 | [diff] [blame] | 174 | EmitFunctionProlog(CurFn, FnRetTy, Args); |
Anders Carlsson | 751358f | 2008-12-20 21:28:43 +0000 | [diff] [blame] | 175 | |
| 176 | // If any of the arguments have a variably modified type, make sure to |
| 177 | // emit the type size. |
| 178 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 179 | i != e; ++i) { |
| 180 | QualType Ty = i->second; |
| 181 | |
| 182 | if (Ty->isVariablyModifiedType()) |
| 183 | EmitVLASize(Ty); |
| 184 | } |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 185 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 186 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 187 | void CodeGenFunction::GenerateCode(const FunctionDecl *FD, |
| 188 | llvm::Function *Fn) { |
| 189 | FunctionArgList Args; |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 190 | if (FD->getNumParams()) { |
| 191 | const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto(); |
| 192 | assert(FProto && "Function def must have prototype!"); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 193 | |
| 194 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) |
| 195 | Args.push_back(std::make_pair(FD->getParamDecl(i), |
| 196 | FProto->getArgType(i))); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 197 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 198 | |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 199 | StartFunction(FD, FD->getResultType(), Fn, Args, |
| 200 | cast<CompoundStmt>(FD->getBody())->getLBracLoc()); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 201 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 202 | EmitStmt(FD->getBody()); |
| 203 | |
| 204 | const CompoundStmt *S = dyn_cast<CompoundStmt>(FD->getBody()); |
| 205 | if (S) { |
| 206 | FinishFunction(S->getRBracLoc()); |
| 207 | } else { |
| 208 | FinishFunction(); |
| 209 | } |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 212 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 213 | /// this statement is not executed normally, it not containing a label means |
| 214 | /// that we can just remove the code. |
| 215 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 216 | // Null statement, not a label! |
| 217 | if (S == 0) return false; |
| 218 | |
| 219 | // If this is a label, we have to emit the code, consider something like: |
| 220 | // if (0) { ... foo: bar(); } goto foo; |
| 221 | if (isa<LabelStmt>(S)) |
| 222 | return true; |
| 223 | |
| 224 | // If this is a case/default statement, and we haven't seen a switch, we have |
| 225 | // to emit the code. |
| 226 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 227 | return true; |
| 228 | |
| 229 | // If this is a switch statement, we want to ignore cases below it. |
| 230 | if (isa<SwitchStmt>(S)) |
| 231 | IgnoreCaseStmts = true; |
| 232 | |
| 233 | // Scan subexpressions for verboten labels. |
| 234 | for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end(); |
| 235 | I != E; ++I) |
| 236 | if (ContainsLabel(*I, IgnoreCaseStmts)) |
| 237 | return true; |
| 238 | |
| 239 | return false; |
| 240 | } |
| 241 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 242 | |
| 243 | /// ConstantFoldsToSimpleInteger - If the sepcified expression does not fold to |
| 244 | /// a constant, or if it does but contains a label, return 0. If it constant |
| 245 | /// folds to 'true' and does not contain a label, return 1, if it constant folds |
| 246 | /// to 'false' and does not contain a label, return -1. |
| 247 | int CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond) { |
Daniel Dunbar | 36bc14c | 2008-11-12 22:37:10 +0000 | [diff] [blame] | 248 | // FIXME: Rename and handle conversion of other evaluatable things |
| 249 | // to bool. |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 250 | Expr::EvalResult Result; |
| 251 | if (!Cond->Evaluate(Result, getContext()) || !Result.Val.isInt() || |
| 252 | Result.HasSideEffects) |
Anders Carlsson | ef5a66d | 2008-11-22 22:32:07 +0000 | [diff] [blame] | 253 | return 0; // Not foldable, not integer or not fully evaluatable. |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 254 | |
| 255 | if (CodeGenFunction::ContainsLabel(Cond)) |
| 256 | return 0; // Contains a label. |
| 257 | |
Anders Carlsson | 64712f1 | 2008-12-01 02:46:24 +0000 | [diff] [blame] | 258 | return Result.Val.getInt().getBoolValue() ? 1 : -1; |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if |
| 263 | /// statement) to the specified blocks. Based on the condition, this might try |
| 264 | /// to simplify the codegen of the conditional based on the branch. |
| 265 | /// |
| 266 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 267 | llvm::BasicBlock *TrueBlock, |
| 268 | llvm::BasicBlock *FalseBlock) { |
| 269 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(Cond)) |
| 270 | return EmitBranchOnBoolExpr(PE->getSubExpr(), TrueBlock, FalseBlock); |
| 271 | |
| 272 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 273 | // Handle X && Y in a condition. |
| 274 | if (CondBOp->getOpcode() == BinaryOperator::LAnd) { |
| 275 | // If we have "1 && X", simplify the code. "0 && X" would have constant |
| 276 | // folded if the case was simple enough. |
| 277 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == 1) { |
| 278 | // br(1 && X) -> br(X). |
| 279 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 280 | } |
| 281 | |
| 282 | // If we have "X && 1", simplify the code to use an uncond branch. |
| 283 | // "X && 0" would have been constant folded to 0. |
| 284 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == 1) { |
| 285 | // br(X && 1) -> br(X). |
| 286 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 287 | } |
| 288 | |
| 289 | // Emit the LHS as a conditional. If the LHS conditional is false, we |
| 290 | // want to jump to the FalseBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 291 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 292 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); |
| 293 | EmitBlock(LHSTrue); |
| 294 | |
| 295 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 296 | return; |
| 297 | } else if (CondBOp->getOpcode() == BinaryOperator::LOr) { |
| 298 | // If we have "0 || X", simplify the code. "1 || X" would have constant |
| 299 | // folded if the case was simple enough. |
| 300 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS()) == -1) { |
| 301 | // br(0 || X) -> br(X). |
| 302 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 303 | } |
| 304 | |
| 305 | // If we have "X || 0", simplify the code to use an uncond branch. |
| 306 | // "X || 1" would have been constant folded to 1. |
| 307 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS()) == -1) { |
| 308 | // br(X || 0) -> br(X). |
| 309 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 310 | } |
| 311 | |
| 312 | // Emit the LHS as a conditional. If the LHS conditional is true, we |
| 313 | // want to jump to the TrueBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 314 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 315 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); |
| 316 | EmitBlock(LHSFalse); |
| 317 | |
| 318 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 319 | return; |
| 320 | } |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 324 | // br(!x, t, f) -> br(x, f, t) |
| 325 | if (CondUOp->getOpcode() == UnaryOperator::LNot) |
| 326 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Daniel Dunbar | 09b1489 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 329 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
| 330 | // Handle ?: operator. |
| 331 | |
| 332 | // Just ignore GNU ?: extension. |
| 333 | if (CondOp->getLHS()) { |
| 334 | // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) |
| 335 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 336 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
| 337 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); |
| 338 | EmitBlock(LHSBlock); |
| 339 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); |
| 340 | EmitBlock(RHSBlock); |
| 341 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); |
| 342 | return; |
| 343 | } |
| 344 | } |
| 345 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 346 | // Emit the code with the fully general case. |
| 347 | llvm::Value *CondV = EvaluateExprAsBool(Cond); |
| 348 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); |
| 349 | } |
| 350 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 351 | /// getCGRecordLayout - Return record layout info. |
| 352 | const CGRecordLayout *CodeGenFunction::getCGRecordLayout(CodeGenTypes &CGT, |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 353 | QualType Ty) { |
| 354 | const RecordType *RTy = Ty->getAsRecordType(); |
| 355 | assert (RTy && "Unexpected type. RecordType expected here."); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 356 | |
Chris Lattner | af31913 | 2008-02-05 06:55:31 +0000 | [diff] [blame] | 357 | return CGT.getCGRecordLayout(RTy->getDecl()); |
Devang Patel | b84a06e | 2007-10-23 02:10:49 +0000 | [diff] [blame] | 358 | } |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 359 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 360 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 361 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 362 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 363 | bool OmitOnError) { |
| 364 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 367 | unsigned CodeGenFunction::GetIDForAddrOfLabel(const LabelStmt *L) { |
| 368 | // Use LabelIDs.size() as the new ID if one hasn't been assigned. |
| 369 | return LabelIDs.insert(std::make_pair(L, LabelIDs.size())).first->second; |
| 370 | } |
| 371 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 372 | void CodeGenFunction::EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty) |
| 373 | { |
| 374 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 375 | if (DestPtr->getType() != BP) |
| 376 | DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp"); |
| 377 | |
| 378 | // Get size and alignment info for this aggregate. |
| 379 | std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty); |
| 380 | |
| 381 | // FIXME: Handle variable sized types. |
| 382 | const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth); |
| 383 | |
| 384 | Builder.CreateCall4(CGM.getMemSetFn(), DestPtr, |
| 385 | llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty), |
| 386 | // TypeInfo.first describes size in bits. |
| 387 | llvm::ConstantInt::get(IntPtr, TypeInfo.first/8), |
| 388 | llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 389 | TypeInfo.second/8)); |
| 390 | } |
| 391 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 392 | void CodeGenFunction::EmitIndirectSwitches() { |
| 393 | llvm::BasicBlock *Default; |
| 394 | |
Daniel Dunbar | 76526a5 | 2008-08-04 17:24:44 +0000 | [diff] [blame] | 395 | if (IndirectSwitches.empty()) |
| 396 | return; |
| 397 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 398 | if (!LabelIDs.empty()) { |
| 399 | Default = getBasicBlockForLabel(LabelIDs.begin()->first); |
| 400 | } else { |
| 401 | // No possible targets for indirect goto, just emit an infinite |
| 402 | // loop. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 403 | Default = createBasicBlock("indirectgoto.loop", CurFn); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 404 | llvm::BranchInst::Create(Default, Default); |
| 405 | } |
| 406 | |
| 407 | for (std::vector<llvm::SwitchInst*>::iterator i = IndirectSwitches.begin(), |
| 408 | e = IndirectSwitches.end(); i != e; ++i) { |
| 409 | llvm::SwitchInst *I = *i; |
| 410 | |
| 411 | I->setSuccessor(0, Default); |
| 412 | for (std::map<const LabelStmt*,unsigned>::iterator LI = LabelIDs.begin(), |
| 413 | LE = LabelIDs.end(); LI != LE; ++LI) { |
| 414 | I->addCase(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 415 | LI->second), |
| 416 | getBasicBlockForLabel(LI->first)); |
| 417 | } |
| 418 | } |
| 419 | } |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 420 | |
| 421 | llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) |
| 422 | { |
| 423 | // FIXME: This entire method is hardcoded for 32-bit X86. |
| 424 | |
| 425 | const char *TargetPrefix = getContext().Target.getTargetPrefix(); |
| 426 | |
| 427 | if (strcmp(TargetPrefix, "x86") != 0 || |
| 428 | getContext().Target.getPointerWidth(0) != 32) |
| 429 | return 0; |
| 430 | |
| 431 | const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 432 | const llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 433 | |
| 434 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 435 | "ap"); |
| 436 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 437 | llvm::Value *AddrTyped = |
| 438 | Builder.CreateBitCast(Addr, |
| 439 | llvm::PointerType::getUnqual(ConvertType(Ty))); |
| 440 | |
| 441 | uint64_t SizeInBytes = getContext().getTypeSize(Ty) / 8; |
| 442 | const unsigned ArgumentSizeInBytes = 4; |
| 443 | if (SizeInBytes < ArgumentSizeInBytes) |
| 444 | SizeInBytes = ArgumentSizeInBytes; |
| 445 | |
| 446 | llvm::Value *NextAddr = |
| 447 | Builder.CreateGEP(Addr, |
| 448 | llvm::ConstantInt::get(llvm::Type::Int32Ty, SizeInBytes), |
| 449 | "ap.next"); |
| 450 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 451 | |
| 452 | return AddrTyped; |
| 453 | } |
| 454 | |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 455 | |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 456 | llvm::Value *CodeGenFunction::GetVLASize(const VariableArrayType *VAT) |
| 457 | { |
| 458 | llvm::Value *&SizeEntry = VLASizeMap[VAT]; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 459 | |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 460 | assert(SizeEntry && "Did not emit size for type"); |
| 461 | return SizeEntry; |
| 462 | } |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 463 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 464 | llvm::Value *CodeGenFunction::EmitVLASize(QualType Ty) |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 465 | { |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 466 | assert(Ty->isVariablyModifiedType() && |
| 467 | "Must pass variably modified type to EmitVLASizes!"); |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 468 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 469 | if (const VariableArrayType *VAT = getContext().getAsVariableArrayType(Ty)) { |
| 470 | llvm::Value *&SizeEntry = VLASizeMap[VAT]; |
| 471 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 472 | if (!SizeEntry) { |
| 473 | // Get the element size; |
| 474 | llvm::Value *ElemSize; |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 475 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 476 | QualType ElemTy = VAT->getElementType(); |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 477 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 478 | if (ElemTy->isVariableArrayType()) |
| 479 | ElemSize = EmitVLASize(ElemTy); |
| 480 | else { |
| 481 | // FIXME: We use Int32Ty here because the alloca instruction takes a |
| 482 | // 32-bit integer. What should we do about overflow? |
| 483 | ElemSize = llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 484 | getContext().getTypeSize(ElemTy) / 8); |
| 485 | } |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 486 | |
Anders Carlsson | fcdbb93 | 2008-12-20 21:51:53 +0000 | [diff] [blame] | 487 | llvm::Value *NumElements = EmitScalarExpr(VAT->getSizeExpr()); |
| 488 | |
| 489 | SizeEntry = Builder.CreateMul(ElemSize, NumElements); |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 492 | return SizeEntry; |
| 493 | } else if (const PointerType *PT = Ty->getAsPointerType()) |
| 494 | EmitVLASize(PT->getPointeeType()); |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 495 | else { |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 496 | assert(0 && "unknown VM type!"); |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 497 | } |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 498 | |
| 499 | return 0; |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 500 | } |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 501 | |
| 502 | llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { |
| 503 | if (CGM.getContext().getBuiltinVaListType()->isArrayType()) { |
| 504 | return EmitScalarExpr(E); |
| 505 | } |
| 506 | return EmitLValue(E).getAddress(); |
| 507 | } |