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" |
Peter Collingbourne | a4ae229 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 16 | #include "CGCUDARuntime.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Eli Friedman | 3f2af10 | 2008-05-22 01:40:10 +0000 | [diff] [blame] | 18 | #include "CGDebugInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Anders Carlsson | 2b77ba8 | 2009-04-04 20:47:02 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtCXX.h" |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CodeGenOptions.h" |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Duncan Sands | 10e675d | 2012-04-16 17:24:31 +0000 | [diff] [blame^] | 26 | #include "llvm/Support/MDBuilder.h" |
| 27 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | using namespace CodeGen; |
| 30 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | CodeGenFunction::CodeGenFunction(CodeGenModule &cgm) |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 32 | : CodeGenTypeCache(cgm), CGM(cgm), |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 33 | Target(CGM.getContext().getTargetInfo()), |
| 34 | Builder(cgm.getModule().getContext()), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 35 | AutoreleaseResult(false), BlockInfo(0), BlockPointer(0), |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 36 | LambdaThisCaptureField(0), NormalCleanupDest(0), NextCleanupDestIndex(1), |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 37 | FirstBlockInfo(0), EHResumeBlock(0), ExceptionSlot(0), EHSelectorSlot(0), |
John McCall | 93c332a | 2011-05-28 21:13:02 +0000 | [diff] [blame] | 38 | DebugInfo(0), DisableDebugInfo(false), DidCallStackSave(false), |
| 39 | IndirectBranch(0), SwitchInsn(0), CaseRangeBlock(0), UnreachableBlock(0), |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 40 | CXXABIThisDecl(0), CXXABIThisValue(0), CXXThisValue(0), CXXVTTDecl(0), |
| 41 | CXXVTTValue(0), OutermostConditional(0), TerminateLandingPad(0), |
| 42 | TerminateHandler(0), TrapBB(0) { |
Anders Carlsson | c1cfdf8 | 2011-02-20 00:20:27 +0000 | [diff] [blame] | 43 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 44 | CatchUndefined = getContext().getLangOpts().CatchUndefined; |
Duncan Sands | 10e675d | 2012-04-16 17:24:31 +0000 | [diff] [blame^] | 45 | if (getContext().getLangOpts().FastMath) { |
| 46 | llvm::MDBuilder MDHelper(Builder.getContext()); |
| 47 | Builder.SetDefaultFPMathTag(MDHelper.createFastFPMath()); |
| 48 | } |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 49 | CGM.getCXXABI().getMangleContext().startNewFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 50 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 52 | CodeGenFunction::~CodeGenFunction() { |
| 53 | // If there are any unclaimed block infos, go ahead and destroy them |
| 54 | // now. This can happen if IR-gen gets clever and skips evaluating |
| 55 | // something. |
| 56 | if (FirstBlockInfo) |
| 57 | destroyBlockInfos(FirstBlockInfo); |
| 58 | } |
| 59 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 61 | llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { |
Daniel Dunbar | 8b1a343 | 2009-02-03 23:03:55 +0000 | [diff] [blame] | 62 | return CGM.getTypes().ConvertTypeForMem(T); |
| 63 | } |
| 64 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 65 | llvm::Type *CodeGenFunction::ConvertType(QualType T) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | return CGM.getTypes().ConvertType(T); |
| 67 | } |
| 68 | |
John McCall | f2aac84 | 2011-05-15 02:34:36 +0000 | [diff] [blame] | 69 | bool CodeGenFunction::hasAggregateLLVMType(QualType type) { |
| 70 | switch (type.getCanonicalType()->getTypeClass()) { |
| 71 | #define TYPE(name, parent) |
| 72 | #define ABSTRACT_TYPE(name, parent) |
| 73 | #define NON_CANONICAL_TYPE(name, parent) case Type::name: |
| 74 | #define DEPENDENT_TYPE(name, parent) case Type::name: |
| 75 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name: |
| 76 | #include "clang/AST/TypeNodes.def" |
| 77 | llvm_unreachable("non-canonical or dependent type in IR-generation"); |
| 78 | |
| 79 | case Type::Builtin: |
| 80 | case Type::Pointer: |
| 81 | case Type::BlockPointer: |
| 82 | case Type::LValueReference: |
| 83 | case Type::RValueReference: |
| 84 | case Type::MemberPointer: |
| 85 | case Type::Vector: |
| 86 | case Type::ExtVector: |
| 87 | case Type::FunctionProto: |
| 88 | case Type::FunctionNoProto: |
| 89 | case Type::Enum: |
| 90 | case Type::ObjCObjectPointer: |
| 91 | return false; |
| 92 | |
| 93 | // Complexes, arrays, records, and Objective-C objects. |
| 94 | case Type::Complex: |
| 95 | case Type::ConstantArray: |
| 96 | case Type::IncompleteArray: |
| 97 | case Type::VariableArray: |
| 98 | case Type::Record: |
| 99 | case Type::ObjCObject: |
| 100 | case Type::ObjCInterface: |
| 101 | return true; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 102 | |
| 103 | // In IRGen, atomic types are just the underlying type |
| 104 | case Type::Atomic: |
| 105 | return hasAggregateLLVMType(type->getAs<AtomicType>()->getValueType()); |
John McCall | f2aac84 | 2011-05-15 02:34:36 +0000 | [diff] [blame] | 106 | } |
| 107 | llvm_unreachable("unknown type kind!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 110 | void CodeGenFunction::EmitReturnBlock() { |
| 111 | // For cleanliness, we try to avoid emitting the return block for |
| 112 | // simple cases. |
| 113 | llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); |
| 114 | |
| 115 | if (CurBB) { |
| 116 | assert(!CurBB->getTerminator() && "Unexpected terminated block."); |
| 117 | |
Daniel Dunbar | 96e18b0 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 118 | // We have a valid insert point, reuse it if it is empty or there are no |
| 119 | // explicit jumps to the return block. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 120 | if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) { |
| 121 | ReturnBlock.getBlock()->replaceAllUsesWith(CurBB); |
| 122 | delete ReturnBlock.getBlock(); |
Daniel Dunbar | 96e18b0 | 2009-07-19 08:24:34 +0000 | [diff] [blame] | 123 | } else |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 124 | EmitBlock(ReturnBlock.getBlock()); |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 125 | return; |
| 126 | } |
| 127 | |
| 128 | // Otherwise, if the return block is the target of a single direct |
| 129 | // branch then we can just put the code in that block instead. This |
| 130 | // cleans up functions which started with a unified return block. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 131 | if (ReturnBlock.getBlock()->hasOneUse()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | llvm::BranchInst *BI = |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 133 | dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 134 | if (BI && BI->isUnconditional() && |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 135 | BI->getSuccessor(0) == ReturnBlock.getBlock()) { |
Eric Christopher | acae011 | 2011-09-09 21:53:04 +0000 | [diff] [blame] | 136 | // Reset insertion point, including debug location, and delete the branch. |
| 137 | Builder.SetCurrentDebugLocation(BI->getDebugLoc()); |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 138 | Builder.SetInsertPoint(BI->getParent()); |
| 139 | BI->eraseFromParent(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 140 | delete ReturnBlock.getBlock(); |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | } |
| 144 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 145 | // FIXME: We are at an unreachable point, there is no reason to emit the block |
| 146 | // unless it has uses. However, we still need a place to put the debug |
| 147 | // region.end for now. |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 148 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 149 | EmitBlock(ReturnBlock.getBlock()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) { |
| 153 | if (!BB) return; |
| 154 | if (!BB->use_empty()) |
| 155 | return CGF.CurFn->getBasicBlockList().push_back(BB); |
| 156 | delete BB; |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 159 | void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { |
Chris Lattner | da13870 | 2007-07-16 21:28:45 +0000 | [diff] [blame] | 160 | assert(BreakContinueStack.empty() && |
| 161 | "mismatched push/pop in break/continue stack!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 163 | // Pop any cleanups that might have been associated with the |
| 164 | // parameters. Do this in whatever block we're currently in; it's |
| 165 | // important to do this before we enter the return block or return |
| 166 | // edges will be *really* confused. |
| 167 | if (EHStack.stable_begin() != PrologueCleanupDepth) |
| 168 | PopCleanupBlocks(PrologueCleanupDepth); |
| 169 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | // Emit function epilog (to return). |
Daniel Dunbar | 1c1d607 | 2009-01-26 23:27:52 +0000 | [diff] [blame] | 171 | EmitReturnBlock(); |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 172 | |
Daniel Dunbar | a18652f | 2011-02-10 17:32:22 +0000 | [diff] [blame] | 173 | if (ShouldInstrumentFunction()) |
| 174 | EmitFunctionInstrumentation("__cyg_profile_func_exit"); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 176 | // Emit debug descriptor for function end. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 177 | if (CGDebugInfo *DI = getDebugInfo()) { |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 178 | DI->setLocation(EndLoc); |
Devang Patel | 5a6fbcf | 2010-07-22 22:29:16 +0000 | [diff] [blame] | 179 | DI->EmitFunctionEnd(Builder); |
Daniel Dunbar | f5bd45c | 2008-11-11 20:59:54 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Chris Lattner | 35b21b8 | 2010-06-27 01:06:27 +0000 | [diff] [blame] | 182 | EmitFunctionEpilog(*CurFnInfo); |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 183 | EmitEndEHSpec(CurCodeDecl); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 184 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 185 | assert(EHStack.empty() && |
| 186 | "did not remove all scopes from cleanup stack!"); |
| 187 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 188 | // If someone did an indirect goto, emit the indirect goto block at the end of |
| 189 | // the function. |
| 190 | if (IndirectBranch) { |
| 191 | EmitBlock(IndirectBranch->getParent()); |
| 192 | Builder.ClearInsertionPoint(); |
| 193 | } |
| 194 | |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 195 | // Remove the AllocaInsertPt instruction, which is just a convenience for us. |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 196 | llvm::Instruction *Ptr = AllocaInsertPt; |
Chris Lattner | 5a2fa14 | 2007-12-02 06:32:24 +0000 | [diff] [blame] | 197 | AllocaInsertPt = 0; |
Chris Lattner | 481769b | 2009-03-31 22:17:44 +0000 | [diff] [blame] | 198 | Ptr->eraseFromParent(); |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 199 | |
| 200 | // If someone took the address of a label but never did an indirect goto, we |
| 201 | // made a zero entry PHI node, which is illegal, zap it now. |
| 202 | if (IndirectBranch) { |
| 203 | llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress()); |
| 204 | if (PN->getNumIncomingValues() == 0) { |
| 205 | PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); |
| 206 | PN->eraseFromParent(); |
| 207 | } |
| 208 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 209 | |
John McCall | 777d6e5 | 2011-08-11 02:22:43 +0000 | [diff] [blame] | 210 | EmitIfUsed(*this, EHResumeBlock); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 211 | EmitIfUsed(*this, TerminateLandingPad); |
| 212 | EmitIfUsed(*this, TerminateHandler); |
| 213 | EmitIfUsed(*this, UnreachableBlock); |
John McCall | 744016d | 2010-07-06 23:57:41 +0000 | [diff] [blame] | 214 | |
| 215 | if (CGM.getCodeGenOpts().EmitDeclMetadata) |
| 216 | EmitDeclMetadata(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 219 | /// ShouldInstrumentFunction - Return true if the current function should be |
| 220 | /// instrumented with __cyg_profile_func_* calls |
| 221 | bool CodeGenFunction::ShouldInstrumentFunction() { |
| 222 | if (!CGM.getCodeGenOpts().InstrumentFunctions) |
| 223 | return false; |
Ted Kremenek | 7aa488a | 2011-05-16 23:49:20 +0000 | [diff] [blame] | 224 | if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>()) |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 225 | return false; |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | /// EmitFunctionInstrumentation - Emit LLVM code to call the specified |
| 230 | /// instrumentation function with the current function and the call site, if |
| 231 | /// function instrumentation is enabled. |
| 232 | void CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) { |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 233 | // void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 234 | llvm::PointerType *PointerTy = Int8PtrTy; |
| 235 | llvm::Type *ProfileFuncArgs[] = { PointerTy, PointerTy }; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 236 | llvm::FunctionType *FunctionTy = |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 237 | llvm::FunctionType::get(VoidTy, ProfileFuncArgs, false); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 238 | |
| 239 | llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn); |
| 240 | llvm::CallInst *CallSite = Builder.CreateCall( |
Benjamin Kramer | 8dd55a3 | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 241 | CGM.getIntrinsic(llvm::Intrinsic::returnaddress), |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 242 | llvm::ConstantInt::get(Int32Ty, 0), |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 243 | "callsite"); |
| 244 | |
Chris Lattner | 8dab657 | 2010-06-23 05:21:28 +0000 | [diff] [blame] | 245 | Builder.CreateCall2(F, |
| 246 | llvm::ConstantExpr::getBitCast(CurFn, PointerTy), |
| 247 | CallSite); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Roman Divacky | be4c870 | 2011-02-10 16:52:03 +0000 | [diff] [blame] | 250 | void CodeGenFunction::EmitMCountInstrumentation() { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 251 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
Roman Divacky | be4c870 | 2011-02-10 16:52:03 +0000 | [diff] [blame] | 252 | |
| 253 | llvm::Constant *MCountFn = CGM.CreateRuntimeFunction(FTy, |
| 254 | Target.getMCountName()); |
| 255 | Builder.CreateCall(MCountFn); |
| 256 | } |
| 257 | |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 258 | void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 259 | llvm::Function *Fn, |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 260 | const CGFunctionInfo &FnInfo, |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 261 | const FunctionArgList &Args, |
Tilmann Scheller | 9c6082f | 2011-03-02 21:36:49 +0000 | [diff] [blame] | 262 | SourceLocation StartLoc) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 263 | const Decl *D = GD.getDecl(); |
| 264 | |
Anders Carlsson | 4cc1a47 | 2009-02-09 20:20:56 +0000 | [diff] [blame] | 265 | DidCallStackSave = false; |
Chris Lattner | b5437d2 | 2009-04-23 05:30:27 +0000 | [diff] [blame] | 266 | CurCodeDecl = CurFuncDecl = D; |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 267 | FnRetTy = RetTy; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 268 | CurFn = Fn; |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 269 | CurFnInfo = &FnInfo; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 270 | assert(CurFn->isDeclaration() && "Function already has body?"); |
| 271 | |
Jakob Stoklund Olesen | a3fe284 | 2010-02-09 00:10:00 +0000 | [diff] [blame] | 272 | // Pass inline keyword to optimizer if it appears explicitly on any |
| 273 | // declaration. |
Chad Rosier | 8fbe385 | 2012-03-14 23:32:11 +0000 | [diff] [blame] | 274 | if (!CGM.getCodeGenOpts().NoInline) |
| 275 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 276 | for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(), |
| 277 | RE = FD->redecls_end(); RI != RE; ++RI) |
| 278 | if (RI->isInlineSpecified()) { |
| 279 | Fn->addFnAttr(llvm::Attribute::InlineHint); |
| 280 | break; |
| 281 | } |
Jakob Stoklund Olesen | a3fe284 | 2010-02-09 00:10:00 +0000 | [diff] [blame] | 282 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 283 | if (getContext().getLangOpts().OpenCL) { |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 284 | // Add metadata for a kernel function. |
| 285 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 286 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 287 | llvm::LLVMContext &Context = getLLVMContext(); |
| 288 | llvm::NamedMDNode *OpenCLMetadata = |
| 289 | CGM.getModule().getOrInsertNamedMetadata("opencl.kernels"); |
| 290 | |
| 291 | llvm::Value *Op = Fn; |
Jay Foad | 6f14165 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 292 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Op)); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 296 | llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); |
Daniel Dunbar | 5ca2084 | 2008-09-09 21:00:17 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 298 | // Create a marker to make it easy to insert allocas into the entryblock |
| 299 | // later. Don't create this with the builder, because we don't want it |
| 300 | // folded. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 301 | llvm::Value *Undef = llvm::UndefValue::get(Int32Ty); |
| 302 | AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB); |
Chris Lattner | f146684 | 2009-03-22 00:24:14 +0000 | [diff] [blame] | 303 | if (Builder.isNamePreserving()) |
| 304 | AllocaInsertPt->setName("allocapt"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 306 | ReturnBlock = getJumpDestInCurrentScope("return"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 308 | Builder.SetInsertPoint(EntryBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 310 | // Emit subprogram debug descriptor. |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 311 | if (CGDebugInfo *DI = getDebugInfo()) { |
Eric Christopher | 0625366 | 2011-10-21 23:30:10 +0000 | [diff] [blame] | 312 | unsigned NumArgs = 0; |
| 313 | QualType *ArgsArray = new QualType[Args.size()]; |
| 314 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 315 | i != e; ++i) { |
| 316 | ArgsArray[NumArgs++] = (*i)->getType(); |
| 317 | } |
| 318 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 319 | QualType FnType = |
Eric Christopher | 0625366 | 2011-10-21 23:30:10 +0000 | [diff] [blame] | 320 | getContext().getFunctionType(RetTy, ArgsArray, NumArgs, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 321 | FunctionProtoType::ExtProtoInfo()); |
| 322 | |
Benjamin Kramer | 2a04f1c | 2011-10-24 17:22:36 +0000 | [diff] [blame] | 323 | delete[] ArgsArray; |
Eric Christopher | 0625366 | 2011-10-21 23:30:10 +0000 | [diff] [blame] | 324 | |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 325 | DI->setLocation(StartLoc); |
Devang Patel | 9c6c3a0 | 2010-01-14 00:36:21 +0000 | [diff] [blame] | 326 | DI->EmitFunctionStart(GD, FnType, CurFn, Builder); |
Sanjiv Gupta | af99417 | 2008-07-04 11:04:26 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Daniel Dunbar | a18652f | 2011-02-10 17:32:22 +0000 | [diff] [blame] | 329 | if (ShouldInstrumentFunction()) |
| 330 | EmitFunctionInstrumentation("__cyg_profile_func_enter"); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 331 | |
Roman Divacky | be4c870 | 2011-02-10 16:52:03 +0000 | [diff] [blame] | 332 | if (CGM.getCodeGenOpts().InstrumentForProfiling) |
| 333 | EmitMCountInstrumentation(); |
| 334 | |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 335 | if (RetTy->isVoidType()) { |
| 336 | // Void type; nothing to return. |
| 337 | ReturnValue = 0; |
| 338 | } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect && |
| 339 | hasAggregateLLVMType(CurFnInfo->getReturnType())) { |
| 340 | // Indirect aggregate return; emit returned value directly into sret slot. |
Daniel Dunbar | 647a1ec | 2010-02-16 19:45:20 +0000 | [diff] [blame] | 341 | // This reduces code size, and affects correctness in C++. |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 342 | ReturnValue = CurFn->arg_begin(); |
| 343 | } else { |
Daniel Dunbar | 647a1ec | 2010-02-16 19:45:20 +0000 | [diff] [blame] | 344 | ReturnValue = CreateIRTemp(RetTy, "retval"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 345 | |
| 346 | // Tell the epilog emitter to autorelease the result. We do this |
| 347 | // now so that various specialized functions can suppress it |
| 348 | // during their IR-generation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 349 | if (getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 350 | !CurFnInfo->isReturnsRetained() && |
| 351 | RetTy->isObjCRetainableType()) |
| 352 | AutoreleaseResult = true; |
Eli Friedman | b17daf9 | 2009-12-04 02:43:40 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Mike Stump | cce3d4f | 2009-12-07 23:38:24 +0000 | [diff] [blame] | 355 | EmitStartEHSpec(CurCodeDecl); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 356 | |
| 357 | PrologueCleanupDepth = EHStack.stable_begin(); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 358 | EmitFunctionProlog(*CurFnInfo, CurFn, Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 360 | if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) { |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 361 | CGM.getCXXABI().EmitInstanceFunctionProlog(*this); |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 362 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(D); |
| 363 | if (MD->getParent()->isLambda() && |
| 364 | MD->getOverloadedOperator() == OO_Call) { |
| 365 | // We're in a lambda; figure out the captures. |
| 366 | MD->getParent()->getCaptureFields(LambdaCaptureFields, |
| 367 | LambdaThisCaptureField); |
| 368 | if (LambdaThisCaptureField) { |
| 369 | // If this lambda captures this, load it. |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 370 | QualType LambdaTagType = |
| 371 | getContext().getTagDeclType(LambdaThisCaptureField->getParent()); |
| 372 | LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue, |
| 373 | LambdaTagType); |
| 374 | LValue ThisLValue = EmitLValueForField(LambdaLV, |
| 375 | LambdaThisCaptureField); |
Eli Friedman | cec5ebd | 2012-02-11 02:57:39 +0000 | [diff] [blame] | 376 | CXXThisValue = EmitLoadOfLValue(ThisLValue).getScalarVal(); |
| 377 | } |
| 378 | } else { |
| 379 | // Not in a lambda; just use 'this' from the method. |
| 380 | // FIXME: Should we generate a new load for each use of 'this'? The |
| 381 | // fast register allocator would be happier... |
| 382 | CXXThisValue = CXXABIThisValue; |
| 383 | } |
| 384 | } |
John McCall | 2504941 | 2010-02-16 22:04:33 +0000 | [diff] [blame] | 385 | |
Anders Carlsson | 751358f | 2008-12-20 21:28:43 +0000 | [diff] [blame] | 386 | // If any of the arguments have a variably modified type, make sure to |
| 387 | // emit the type size. |
| 388 | for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); |
| 389 | i != e; ++i) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 390 | QualType Ty = (*i)->getType(); |
Anders Carlsson | 751358f | 2008-12-20 21:28:43 +0000 | [diff] [blame] | 391 | |
| 392 | if (Ty->isVariablyModifiedType()) |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 393 | EmitVariablyModifiedType(Ty); |
Anders Carlsson | 751358f | 2008-12-20 21:28:43 +0000 | [diff] [blame] | 394 | } |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 395 | // Emit a location at the end of the prologue. |
| 396 | if (CGDebugInfo *DI = getDebugInfo()) |
| 397 | DI->EmitLocation(Builder, StartLoc); |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 398 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 399 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 400 | void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) { |
| 401 | const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl()); |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 402 | assert(FD->getBody()); |
| 403 | EmitStmt(FD->getBody()); |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 404 | } |
| 405 | |
John McCall | 39dad53 | 2010-08-03 22:46:07 +0000 | [diff] [blame] | 406 | /// Tries to mark the given function nounwind based on the |
| 407 | /// non-existence of any throwing calls within it. We believe this is |
| 408 | /// lightweight enough to do at -O0. |
| 409 | static void TryMarkNoThrow(llvm::Function *F) { |
John McCall | b3a29f1 | 2010-08-11 22:38:33 +0000 | [diff] [blame] | 410 | // LLVM treats 'nounwind' on a function as part of the type, so we |
| 411 | // can't do this on functions that can be overwritten. |
| 412 | if (F->mayBeOverridden()) return; |
| 413 | |
John McCall | 39dad53 | 2010-08-03 22:46:07 +0000 | [diff] [blame] | 414 | for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI) |
| 415 | for (llvm::BasicBlock::iterator |
| 416 | BI = FI->begin(), BE = FI->end(); BI != BE; ++BI) |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 417 | if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI)) { |
John McCall | 39dad53 | 2010-08-03 22:46:07 +0000 | [diff] [blame] | 418 | if (!Call->doesNotThrow()) |
| 419 | return; |
Bill Wendling | 285cfd8 | 2011-09-19 20:31:14 +0000 | [diff] [blame] | 420 | } else if (isa<llvm::ResumeInst>(&*BI)) { |
| 421 | return; |
| 422 | } |
John McCall | 39dad53 | 2010-08-03 22:46:07 +0000 | [diff] [blame] | 423 | F->setDoesNotThrow(true); |
| 424 | } |
| 425 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 426 | void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn, |
| 427 | const CGFunctionInfo &FnInfo) { |
Anders Carlsson | 0ff8baf | 2009-09-11 00:07:24 +0000 | [diff] [blame] | 428 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 429 | |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 430 | // Check if we should generate debug info for this function. |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 431 | if (CGM.getModuleDebugInfo() && !FD->hasAttr<NoDebugAttr>()) |
| 432 | DebugInfo = CGM.getModuleDebugInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 434 | FunctionArgList Args; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 435 | QualType ResTy = FD->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Mike Stump | 6a1e0eb | 2009-12-04 23:26:17 +0000 | [diff] [blame] | 437 | CurGD = GD; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 438 | if (isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isInstance()) |
| 439 | CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResTy, Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | |
Chad Rosier | 6e94f6c | 2012-02-18 00:37:07 +0000 | [diff] [blame] | 441 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) |
| 442 | Args.push_back(FD->getParamDecl(i)); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 443 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 444 | SourceRange BodyRange; |
| 445 | if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange(); |
Anders Carlsson | 4365bba | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 446 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 447 | // Emit the standard function prologue. |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 448 | StartFunction(GD, ResTy, Fn, FnInfo, Args, BodyRange.getBegin()); |
Anders Carlsson | 4365bba | 2009-11-06 02:55:43 +0000 | [diff] [blame] | 449 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 450 | // Generate the body of the function. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 451 | if (isa<CXXDestructorDecl>(FD)) |
| 452 | EmitDestructorBody(Args); |
| 453 | else if (isa<CXXConstructorDecl>(FD)) |
| 454 | EmitConstructorBody(Args); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 455 | else if (getContext().getLangOpts().CUDA && |
Peter Collingbourne | a4ae229 | 2011-10-06 18:51:56 +0000 | [diff] [blame] | 456 | !CGM.getCodeGenOpts().CUDAIsDevice && |
| 457 | FD->hasAttr<CUDAGlobalAttr>()) |
| 458 | CGM.getCUDARuntime().EmitDeviceStubBody(*this, Args); |
Eli Friedman | bd89f8c | 2012-02-16 01:37:33 +0000 | [diff] [blame] | 459 | else if (isa<CXXConversionDecl>(FD) && |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 460 | cast<CXXConversionDecl>(FD)->isLambdaToBlockPointerConversion()) { |
| 461 | // The lambda conversion to block pointer is special; the semantics can't be |
| 462 | // expressed in the AST, so IRGen needs to special-case it. |
| 463 | EmitLambdaToBlockPointerBody(Args); |
| 464 | } else if (isa<CXXMethodDecl>(FD) && |
| 465 | cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) { |
| 466 | // The lambda "__invoke" function is special, because it forwards or |
| 467 | // clones the body of the function call operator (but is actually static). |
| 468 | EmitLambdaStaticInvokeFunction(cast<CXXMethodDecl>(FD)); |
Eli Friedman | bd89f8c | 2012-02-16 01:37:33 +0000 | [diff] [blame] | 469 | } |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 470 | else |
| 471 | EmitFunctionBody(Args); |
Anders Carlsson | 1851a12 | 2010-02-07 19:45:40 +0000 | [diff] [blame] | 472 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 473 | // Emit the standard function epilogue. |
| 474 | FinishFunction(BodyRange.getEnd()); |
John McCall | 39dad53 | 2010-08-03 22:46:07 +0000 | [diff] [blame] | 475 | |
| 476 | // If we haven't marked the function nothrow through other means, do |
| 477 | // a quick pass now to see if we can. |
| 478 | if (!CurFn->doesNotThrow()) |
| 479 | TryMarkNoThrow(CurFn); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 482 | /// ContainsLabel - Return true if the statement contains a label in it. If |
| 483 | /// this statement is not executed normally, it not containing a label means |
| 484 | /// that we can just remove the code. |
| 485 | bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { |
| 486 | // Null statement, not a label! |
| 487 | if (S == 0) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 489 | // If this is a label, we have to emit the code, consider something like: |
| 490 | // if (0) { ... foo: bar(); } goto foo; |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 491 | // |
| 492 | // TODO: If anyone cared, we could track __label__'s, since we know that you |
| 493 | // can't jump to one from outside their declared region. |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 494 | if (isa<LabelStmt>(S)) |
| 495 | return true; |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 496 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 497 | // If this is a case/default statement, and we haven't seen a switch, we have |
| 498 | // to emit the code. |
| 499 | if (isa<SwitchCase>(S) && !IgnoreCaseStmts) |
| 500 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 501 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 502 | // If this is a switch statement, we want to ignore cases below it. |
| 503 | if (isa<SwitchStmt>(S)) |
| 504 | IgnoreCaseStmts = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 506 | // Scan subexpressions for verboten labels. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 507 | for (Stmt::const_child_range I = S->children(); I; ++I) |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 508 | if (ContainsLabel(*I, IgnoreCaseStmts)) |
| 509 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 514 | /// containsBreak - Return true if the statement contains a break out of it. |
| 515 | /// If the statement (recursively) contains a switch or loop with a break |
| 516 | /// inside of it, this is fine. |
| 517 | bool CodeGenFunction::containsBreak(const Stmt *S) { |
| 518 | // Null statement, not a label! |
| 519 | if (S == 0) return false; |
| 520 | |
| 521 | // If this is a switch or loop that defines its own break scope, then we can |
| 522 | // include it and anything inside of it. |
| 523 | if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) || |
| 524 | isa<ForStmt>(S)) |
Chris Lattner | 2bef7f5 | 2011-02-28 00:42:31 +0000 | [diff] [blame] | 525 | return false; |
| 526 | |
| 527 | if (isa<BreakStmt>(S)) |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 528 | return true; |
| 529 | |
| 530 | // Scan subexpressions for verboten breaks. |
| 531 | for (Stmt::const_child_range I = S->children(); I; ++I) |
| 532 | if (containsBreak(*I)) |
| 533 | return true; |
| 534 | |
| 535 | return false; |
| 536 | } |
| 537 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 538 | |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 539 | /// ConstantFoldsToSimpleInteger - If the specified expression does not fold |
| 540 | /// to a constant, or if it does but contains a label, return false. If it |
| 541 | /// constant folds return true and set the boolean result in Result. |
| 542 | bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond, |
| 543 | bool &ResultBool) { |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 544 | llvm::APInt ResultInt; |
| 545 | if (!ConstantFoldsToSimpleInteger(Cond, ResultInt)) |
| 546 | return false; |
| 547 | |
| 548 | ResultBool = ResultInt.getBoolValue(); |
| 549 | return true; |
| 550 | } |
| 551 | |
| 552 | /// ConstantFoldsToSimpleInteger - If the specified expression does not fold |
| 553 | /// to a constant, or if it does but contains a label, return false. If it |
| 554 | /// constant folds return true and set the folded value. |
| 555 | bool CodeGenFunction:: |
| 556 | ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APInt &ResultInt) { |
Daniel Dunbar | 36bc14c | 2008-11-12 22:37:10 +0000 | [diff] [blame] | 557 | // FIXME: Rename and handle conversion of other evaluatable things |
| 558 | // to bool. |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 559 | llvm::APSInt Int; |
| 560 | if (!Cond->EvaluateAsInt(Int, getContext())) |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 561 | return false; // Not foldable, not integer or not fully evaluatable. |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 563 | if (CodeGenFunction::ContainsLabel(Cond)) |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 564 | return false; // Contains a label. |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 565 | |
| 566 | ResultInt = Int; |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 567 | return true; |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | |
Chris Lattner | ef425a6 | 2011-02-28 00:18:40 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 572 | /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if |
| 573 | /// statement) to the specified blocks. Based on the condition, this might try |
| 574 | /// to simplify the codegen of the conditional based on the branch. |
| 575 | /// |
| 576 | void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, |
| 577 | llvm::BasicBlock *TrueBlock, |
| 578 | llvm::BasicBlock *FalseBlock) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 579 | Cond = Cond->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 581 | if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) { |
| 582 | // Handle X && Y in a condition. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 583 | if (CondBOp->getOpcode() == BO_LAnd) { |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 584 | // If we have "1 && X", simplify the code. "0 && X" would have constant |
| 585 | // folded if the case was simple enough. |
Bill Wendling | e3eb83b | 2011-03-04 21:46:03 +0000 | [diff] [blame] | 586 | bool ConstantBool = false; |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 587 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) && |
| 588 | ConstantBool) { |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 589 | // br(1 && X) -> br(X). |
| 590 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 591 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 593 | // If we have "X && 1", simplify the code to use an uncond branch. |
| 594 | // "X && 0" would have been constant folded to 0. |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 595 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) && |
| 596 | ConstantBool) { |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 597 | // br(X && 1) -> br(X). |
| 598 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 599 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 601 | // Emit the LHS as a conditional. If the LHS conditional is false, we |
| 602 | // want to jump to the FalseBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 603 | llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 604 | |
| 605 | ConditionalEvaluation eval(*this); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 606 | EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock); |
| 607 | EmitBlock(LHSTrue); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 609 | // Any temporaries created here are conditional. |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 610 | eval.begin(*this); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 611 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 612 | eval.end(*this); |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 613 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 614 | return; |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | if (CondBOp->getOpcode() == BO_LOr) { |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 618 | // If we have "0 || X", simplify the code. "1 || X" would have constant |
| 619 | // folded if the case was simple enough. |
Bill Wendling | e3eb83b | 2011-03-04 21:46:03 +0000 | [diff] [blame] | 620 | bool ConstantBool = false; |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 621 | if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) && |
| 622 | !ConstantBool) { |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 623 | // br(0 || X) -> br(X). |
| 624 | return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
| 625 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 627 | // If we have "X || 0", simplify the code to use an uncond branch. |
| 628 | // "X || 1" would have been constant folded to 1. |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 629 | if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) && |
| 630 | !ConstantBool) { |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 631 | // br(X || 0) -> br(X). |
| 632 | return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock); |
| 633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 635 | // Emit the LHS as a conditional. If the LHS conditional is true, we |
| 636 | // want to jump to the TrueBlock. |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 637 | llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 638 | |
| 639 | ConditionalEvaluation eval(*this); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 640 | EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse); |
| 641 | EmitBlock(LHSFalse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 643 | // Any temporaries created here are conditional. |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 644 | eval.begin(*this); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 645 | EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 646 | eval.end(*this); |
Anders Carlsson | 08e9e45 | 2010-01-24 00:20:05 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 648 | return; |
| 649 | } |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 650 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 652 | if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) { |
| 653 | // br(!x, t, f) -> br(x, f, t) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 654 | if (CondUOp->getOpcode() == UO_LNot) |
Chris Lattner | 552f4c4 | 2008-11-12 08:13:36 +0000 | [diff] [blame] | 655 | return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 656 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
Daniel Dunbar | 09b1489 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 658 | if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) { |
Eli Friedman | df33a35 | 2012-02-14 03:54:45 +0000 | [diff] [blame] | 659 | // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) |
| 660 | llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); |
| 661 | llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); |
Daniel Dunbar | 09b1489 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 662 | |
Eli Friedman | df33a35 | 2012-02-14 03:54:45 +0000 | [diff] [blame] | 663 | ConditionalEvaluation cond(*this); |
| 664 | EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 665 | |
Eli Friedman | df33a35 | 2012-02-14 03:54:45 +0000 | [diff] [blame] | 666 | cond.begin(*this); |
| 667 | EmitBlock(LHSBlock); |
| 668 | EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock); |
| 669 | cond.end(*this); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 670 | |
Eli Friedman | df33a35 | 2012-02-14 03:54:45 +0000 | [diff] [blame] | 671 | cond.begin(*this); |
| 672 | EmitBlock(RHSBlock); |
| 673 | EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock); |
| 674 | cond.end(*this); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 675 | |
Eli Friedman | df33a35 | 2012-02-14 03:54:45 +0000 | [diff] [blame] | 676 | return; |
Daniel Dunbar | 09b1489 | 2008-11-12 10:30:32 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 679 | // Emit the code with the fully general case. |
| 680 | llvm::Value *CondV = EvaluateExprAsBool(Cond); |
| 681 | Builder.CreateCondBr(CondV, TrueBlock, FalseBlock); |
| 682 | } |
| 683 | |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 684 | /// ErrorUnsupported - Print out an error that codegen doesn't support the |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 685 | /// specified stmt yet. |
Daniel Dunbar | 90df4b6 | 2008-09-04 03:43:08 +0000 | [diff] [blame] | 686 | void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type, |
| 687 | bool OmitOnError) { |
| 688 | CGM.ErrorUnsupported(S, Type, OmitOnError); |
Chris Lattner | dc5e826 | 2007-12-02 01:43:38 +0000 | [diff] [blame] | 689 | } |
| 690 | |
John McCall | 7143325 | 2011-02-01 21:35:06 +0000 | [diff] [blame] | 691 | /// emitNonZeroVLAInit - Emit the "zero" initialization of a |
| 692 | /// variable-length array whose elements have a non-zero bit-pattern. |
| 693 | /// |
| 694 | /// \param src - a char* pointing to the bit-pattern for a single |
| 695 | /// base element of the array |
| 696 | /// \param sizeInChars - the total size of the VLA, in chars |
| 697 | /// \param align - the total alignment of the VLA |
| 698 | static void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType, |
| 699 | llvm::Value *dest, llvm::Value *src, |
| 700 | llvm::Value *sizeInChars) { |
| 701 | std::pair<CharUnits,CharUnits> baseSizeAndAlign |
| 702 | = CGF.getContext().getTypeInfoInChars(baseType); |
| 703 | |
| 704 | CGBuilderTy &Builder = CGF.Builder; |
| 705 | |
| 706 | llvm::Value *baseSizeInChars |
| 707 | = llvm::ConstantInt::get(CGF.IntPtrTy, baseSizeAndAlign.first.getQuantity()); |
| 708 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 709 | llvm::Type *i8p = Builder.getInt8PtrTy(); |
John McCall | 7143325 | 2011-02-01 21:35:06 +0000 | [diff] [blame] | 710 | |
| 711 | llvm::Value *begin = Builder.CreateBitCast(dest, i8p, "vla.begin"); |
| 712 | llvm::Value *end = Builder.CreateInBoundsGEP(dest, sizeInChars, "vla.end"); |
| 713 | |
| 714 | llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock(); |
| 715 | llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop"); |
| 716 | llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont"); |
| 717 | |
| 718 | // Make a loop over the VLA. C99 guarantees that the VLA element |
| 719 | // count must be nonzero. |
| 720 | CGF.EmitBlock(loopBB); |
| 721 | |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 722 | llvm::PHINode *cur = Builder.CreatePHI(i8p, 2, "vla.cur"); |
John McCall | 7143325 | 2011-02-01 21:35:06 +0000 | [diff] [blame] | 723 | cur->addIncoming(begin, originBB); |
| 724 | |
| 725 | // memcpy the individual element bit-pattern. |
| 726 | Builder.CreateMemCpy(cur, src, baseSizeInChars, |
| 727 | baseSizeAndAlign.second.getQuantity(), |
| 728 | /*volatile*/ false); |
| 729 | |
| 730 | // Go to the next element. |
| 731 | llvm::Value *next = Builder.CreateConstInBoundsGEP1_32(cur, 1, "vla.next"); |
| 732 | |
| 733 | // Leave if that's the end of the VLA. |
| 734 | llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone"); |
| 735 | Builder.CreateCondBr(done, contBB, loopBB); |
| 736 | cur->addIncoming(next, loopBB); |
| 737 | |
| 738 | CGF.EmitBlock(contBB); |
| 739 | } |
| 740 | |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 741 | void |
| 742 | CodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) { |
Anders Carlsson | 0d7c583 | 2010-05-03 01:20:20 +0000 | [diff] [blame] | 743 | // Ignore empty classes in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 744 | if (getContext().getLangOpts().CPlusPlus) { |
Anders Carlsson | 0d7c583 | 2010-05-03 01:20:20 +0000 | [diff] [blame] | 745 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 746 | if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty()) |
| 747 | return; |
| 748 | } |
| 749 | } |
John McCall | 9021718 | 2010-08-07 08:21:30 +0000 | [diff] [blame] | 750 | |
| 751 | // Cast the dest ptr to the appropriate i8 pointer type. |
| 752 | unsigned DestAS = |
| 753 | cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 754 | llvm::Type *BP = Builder.getInt8PtrTy(DestAS); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 755 | if (DestPtr->getType() != BP) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 756 | DestPtr = Builder.CreateBitCast(DestPtr, BP); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 757 | |
| 758 | // Get size and alignment info for this aggregate. |
Ken Dyck | 79be76c | 2011-04-22 17:51:05 +0000 | [diff] [blame] | 759 | std::pair<CharUnits, CharUnits> TypeInfo = |
| 760 | getContext().getTypeInfoInChars(Ty); |
| 761 | CharUnits Size = TypeInfo.first; |
| 762 | CharUnits Align = TypeInfo.second; |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 763 | |
John McCall | 5576d9b | 2011-01-14 10:37:58 +0000 | [diff] [blame] | 764 | llvm::Value *SizeVal; |
John McCall | 7143325 | 2011-02-01 21:35:06 +0000 | [diff] [blame] | 765 | const VariableArrayType *vla; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
John McCall | 5576d9b | 2011-01-14 10:37:58 +0000 | [diff] [blame] | 767 | // Don't bother emitting a zero-byte memset. |
Ken Dyck | 79be76c | 2011-04-22 17:51:05 +0000 | [diff] [blame] | 768 | if (Size.isZero()) { |
John McCall | 5576d9b | 2011-01-14 10:37:58 +0000 | [diff] [blame] | 769 | // But note that getTypeInfo returns 0 for a VLA. |
| 770 | if (const VariableArrayType *vlaType = |
| 771 | dyn_cast_or_null<VariableArrayType>( |
| 772 | getContext().getAsArrayType(Ty))) { |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 773 | QualType eltType; |
| 774 | llvm::Value *numElts; |
| 775 | llvm::tie(numElts, eltType) = getVLASize(vlaType); |
| 776 | |
| 777 | SizeVal = numElts; |
| 778 | CharUnits eltSize = getContext().getTypeSizeInChars(eltType); |
| 779 | if (!eltSize.isOne()) |
| 780 | SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize)); |
John McCall | 7143325 | 2011-02-01 21:35:06 +0000 | [diff] [blame] | 781 | vla = vlaType; |
John McCall | 5576d9b | 2011-01-14 10:37:58 +0000 | [diff] [blame] | 782 | } else { |
| 783 | return; |
| 784 | } |
| 785 | } else { |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 786 | SizeVal = CGM.getSize(Size); |
John McCall | 7143325 | 2011-02-01 21:35:06 +0000 | [diff] [blame] | 787 | vla = 0; |
John McCall | 5576d9b | 2011-01-14 10:37:58 +0000 | [diff] [blame] | 788 | } |
John McCall | 9021718 | 2010-08-07 08:21:30 +0000 | [diff] [blame] | 789 | |
| 790 | // If the type contains a pointer to data member we can't memset it to zero. |
| 791 | // Instead, create a null constant and copy it to the destination. |
John McCall | 7143325 | 2011-02-01 21:35:06 +0000 | [diff] [blame] | 792 | // TODO: there are other patterns besides zero that we can usefully memset, |
| 793 | // like -1, which happens to be the pattern used by member-pointers. |
John McCall | f16aa10 | 2010-08-22 21:01:12 +0000 | [diff] [blame] | 794 | if (!CGM.getTypes().isZeroInitializable(Ty)) { |
John McCall | 7143325 | 2011-02-01 21:35:06 +0000 | [diff] [blame] | 795 | // For a VLA, emit a single element, then splat that over the VLA. |
| 796 | if (vla) Ty = getContext().getBaseElementType(vla); |
John McCall | 5576d9b | 2011-01-14 10:37:58 +0000 | [diff] [blame] | 797 | |
John McCall | 9021718 | 2010-08-07 08:21:30 +0000 | [diff] [blame] | 798 | llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty); |
| 799 | |
| 800 | llvm::GlobalVariable *NullVariable = |
| 801 | new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(), |
| 802 | /*isConstant=*/true, |
| 803 | llvm::GlobalVariable::PrivateLinkage, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 804 | NullConstant, Twine()); |
John McCall | 9021718 | 2010-08-07 08:21:30 +0000 | [diff] [blame] | 805 | llvm::Value *SrcPtr = |
| 806 | Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy()); |
| 807 | |
John McCall | 7143325 | 2011-02-01 21:35:06 +0000 | [diff] [blame] | 808 | if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal); |
| 809 | |
John McCall | 9021718 | 2010-08-07 08:21:30 +0000 | [diff] [blame] | 810 | // Get and call the appropriate llvm.memcpy overload. |
Ken Dyck | 79be76c | 2011-04-22 17:51:05 +0000 | [diff] [blame] | 811 | Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity(), false); |
John McCall | 9021718 | 2010-08-07 08:21:30 +0000 | [diff] [blame] | 812 | return; |
| 813 | } |
| 814 | |
| 815 | // Otherwise, just memset the whole thing to zero. This is legal |
| 816 | // because in LLVM, all default initializers (other than the ones we just |
| 817 | // handled above) are guaranteed to have a bit pattern of all zeros. |
Ken Dyck | 79be76c | 2011-04-22 17:51:05 +0000 | [diff] [blame] | 818 | Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, |
| 819 | Align.getQuantity(), false); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 822 | llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) { |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 823 | // Make sure that there is a block for the indirect goto. |
| 824 | if (IndirectBranch == 0) |
| 825 | GetIndirectGotoBlock(); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 826 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 827 | llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock(); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 828 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 829 | // Make sure the indirect branch includes all of the address-taken blocks. |
| 830 | IndirectBranch->addDestination(BB); |
| 831 | return llvm::BlockAddress::get(CurFn, BB); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 835 | // If we already made the indirect branch for indirect goto, return its block. |
| 836 | if (IndirectBranch) return IndirectBranch->getParent(); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 837 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 838 | CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto")); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 839 | |
| 840 | // Create the PHI node that indirect gotos will add entries to. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 841 | llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0, |
| 842 | "indirect.goto.dest"); |
Chris Lattner | 3d00fdc | 2009-10-13 06:55:33 +0000 | [diff] [blame] | 843 | |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 844 | // Create the indirect branch instruction. |
| 845 | IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); |
| 846 | return IndirectBranch->getParent(); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 847 | } |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 848 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 849 | /// Computes the length of an array in elements, as well as the base |
| 850 | /// element type and a properly-typed first element pointer. |
| 851 | llvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType, |
| 852 | QualType &baseType, |
| 853 | llvm::Value *&addr) { |
| 854 | const ArrayType *arrayType = origArrayType; |
| 855 | |
| 856 | // If it's a VLA, we have to load the stored size. Note that |
| 857 | // this is the size of the VLA in bytes, not its size in elements. |
| 858 | llvm::Value *numVLAElements = 0; |
| 859 | if (isa<VariableArrayType>(arrayType)) { |
| 860 | numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).first; |
| 861 | |
| 862 | // Walk into all VLAs. This doesn't require changes to addr, |
| 863 | // which has type T* where T is the first non-VLA element type. |
| 864 | do { |
| 865 | QualType elementType = arrayType->getElementType(); |
| 866 | arrayType = getContext().getAsArrayType(elementType); |
| 867 | |
| 868 | // If we only have VLA components, 'addr' requires no adjustment. |
| 869 | if (!arrayType) { |
| 870 | baseType = elementType; |
| 871 | return numVLAElements; |
| 872 | } |
| 873 | } while (isa<VariableArrayType>(arrayType)); |
| 874 | |
| 875 | // We get out here only if we find a constant array type |
| 876 | // inside the VLA. |
| 877 | } |
| 878 | |
| 879 | // We have some number of constant-length arrays, so addr should |
| 880 | // have LLVM type [M x [N x [...]]]*. Build a GEP that walks |
| 881 | // down to the first element of addr. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 882 | SmallVector<llvm::Value*, 8> gepIndices; |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 883 | |
| 884 | // GEP down to the array type. |
| 885 | llvm::ConstantInt *zero = Builder.getInt32(0); |
| 886 | gepIndices.push_back(zero); |
| 887 | |
| 888 | // It's more efficient to calculate the count from the LLVM |
| 889 | // constant-length arrays than to re-evaluate the array bounds. |
| 890 | uint64_t countFromCLAs = 1; |
| 891 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 892 | llvm::ArrayType *llvmArrayType = |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 893 | cast<llvm::ArrayType>( |
| 894 | cast<llvm::PointerType>(addr->getType())->getElementType()); |
| 895 | while (true) { |
| 896 | assert(isa<ConstantArrayType>(arrayType)); |
| 897 | assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue() |
| 898 | == llvmArrayType->getNumElements()); |
| 899 | |
| 900 | gepIndices.push_back(zero); |
| 901 | countFromCLAs *= llvmArrayType->getNumElements(); |
| 902 | |
| 903 | llvmArrayType = |
| 904 | dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType()); |
| 905 | if (!llvmArrayType) break; |
| 906 | |
| 907 | arrayType = getContext().getAsArrayType(arrayType->getElementType()); |
| 908 | assert(arrayType && "LLVM and Clang types are out-of-synch"); |
| 909 | } |
| 910 | |
| 911 | baseType = arrayType->getElementType(); |
| 912 | |
| 913 | // Create the actual GEP. |
Jay Foad | 0f6ac7c | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 914 | addr = Builder.CreateInBoundsGEP(addr, gepIndices, "array.begin"); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 915 | |
| 916 | llvm::Value *numElements |
| 917 | = llvm::ConstantInt::get(SizeTy, countFromCLAs); |
| 918 | |
| 919 | // If we had any VLA dimensions, factor them in. |
| 920 | if (numVLAElements) |
| 921 | numElements = Builder.CreateNUWMul(numVLAElements, numElements); |
| 922 | |
| 923 | return numElements; |
| 924 | } |
| 925 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 926 | std::pair<llvm::Value*, QualType> |
| 927 | CodeGenFunction::getVLASize(QualType type) { |
| 928 | const VariableArrayType *vla = getContext().getAsVariableArrayType(type); |
| 929 | assert(vla && "type was not a variable array type!"); |
| 930 | return getVLASize(vla); |
Anders Carlsson | f666b77 | 2008-12-20 20:27:15 +0000 | [diff] [blame] | 931 | } |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 932 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 933 | std::pair<llvm::Value*, QualType> |
| 934 | CodeGenFunction::getVLASize(const VariableArrayType *type) { |
| 935 | // The number of elements so far; always size_t. |
| 936 | llvm::Value *numElements = 0; |
| 937 | |
| 938 | QualType elementType; |
| 939 | do { |
| 940 | elementType = type->getElementType(); |
| 941 | llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()]; |
| 942 | assert(vlaSize && "no size for VLA!"); |
| 943 | assert(vlaSize->getType() == SizeTy); |
| 944 | |
| 945 | if (!numElements) { |
| 946 | numElements = vlaSize; |
| 947 | } else { |
| 948 | // It's undefined behavior if this wraps around, so mark it that way. |
| 949 | numElements = Builder.CreateNUWMul(numElements, vlaSize); |
| 950 | } |
| 951 | } while ((type = getContext().getAsVariableArrayType(elementType))); |
| 952 | |
| 953 | return std::pair<llvm::Value*,QualType>(numElements, elementType); |
| 954 | } |
| 955 | |
| 956 | void CodeGenFunction::EmitVariablyModifiedType(QualType type) { |
| 957 | assert(type->isVariablyModifiedType() && |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 958 | "Must pass variably modified type to EmitVLASizes!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
Daniel Dunbar | d286f05 | 2009-07-19 06:58:07 +0000 | [diff] [blame] | 960 | EnsureInsertPoint(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 962 | // We're going to walk down into the type and look for VLA |
| 963 | // expressions. |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 964 | do { |
| 965 | assert(type->isVariablyModifiedType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 967 | const Type *ty = type.getTypePtr(); |
| 968 | switch (ty->getTypeClass()) { |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 969 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 970 | #define TYPE(Class, Base) |
| 971 | #define ABSTRACT_TYPE(Class, Base) |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 972 | #define NON_CANONICAL_TYPE(Class, Base) |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 973 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 974 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 975 | #include "clang/AST/TypeNodes.def" |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 976 | llvm_unreachable("unexpected dependent type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 978 | // These types are never variably-modified. |
| 979 | case Type::Builtin: |
| 980 | case Type::Complex: |
| 981 | case Type::Vector: |
| 982 | case Type::ExtVector: |
| 983 | case Type::Record: |
| 984 | case Type::Enum: |
Abramo Bagnara | 5ff53b2 | 2012-01-11 08:19:46 +0000 | [diff] [blame] | 985 | case Type::Elaborated: |
| 986 | case Type::TemplateSpecialization: |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 987 | case Type::ObjCObject: |
| 988 | case Type::ObjCInterface: |
| 989 | case Type::ObjCObjectPointer: |
| 990 | llvm_unreachable("type class is never variably-modified!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 991 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 992 | case Type::Pointer: |
| 993 | type = cast<PointerType>(ty)->getPointeeType(); |
| 994 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 996 | case Type::BlockPointer: |
| 997 | type = cast<BlockPointerType>(ty)->getPointeeType(); |
| 998 | break; |
| 999 | |
| 1000 | case Type::LValueReference: |
| 1001 | case Type::RValueReference: |
| 1002 | type = cast<ReferenceType>(ty)->getPointeeType(); |
| 1003 | break; |
| 1004 | |
| 1005 | case Type::MemberPointer: |
| 1006 | type = cast<MemberPointerType>(ty)->getPointeeType(); |
| 1007 | break; |
| 1008 | |
| 1009 | case Type::ConstantArray: |
| 1010 | case Type::IncompleteArray: |
| 1011 | // Losing element qualification here is fine. |
| 1012 | type = cast<ArrayType>(ty)->getElementType(); |
| 1013 | break; |
| 1014 | |
| 1015 | case Type::VariableArray: { |
| 1016 | // Losing element qualification here is fine. |
| 1017 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1018 | |
| 1019 | // Unknown size indication requires no size computation. |
| 1020 | // Otherwise, evaluate and record it. |
| 1021 | if (const Expr *size = vat->getSizeExpr()) { |
| 1022 | // It's possible that we might have emitted this already, |
| 1023 | // e.g. with a typedef and a pointer to it. |
| 1024 | llvm::Value *&entry = VLASizeMap[size]; |
| 1025 | if (!entry) { |
| 1026 | // Always zexting here would be wrong if it weren't |
| 1027 | // undefined behavior to have a negative bound. |
| 1028 | entry = Builder.CreateIntCast(EmitScalarExpr(size), SizeTy, |
| 1029 | /*signed*/ false); |
| 1030 | } |
| 1031 | } |
| 1032 | type = vat->getElementType(); |
| 1033 | break; |
Anders Carlsson | 60d3541 | 2008-12-20 20:46:34 +0000 | [diff] [blame] | 1034 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1036 | case Type::FunctionProto: |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1037 | case Type::FunctionNoProto: |
| 1038 | type = cast<FunctionType>(ty)->getResultType(); |
| 1039 | break; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1040 | |
Abramo Bagnara | 5ff53b2 | 2012-01-11 08:19:46 +0000 | [diff] [blame] | 1041 | case Type::Paren: |
| 1042 | case Type::TypeOf: |
| 1043 | case Type::UnaryTransform: |
| 1044 | case Type::Attributed: |
| 1045 | case Type::SubstTemplateTypeParm: |
| 1046 | // Keep walking after single level desugaring. |
| 1047 | type = type.getSingleStepDesugaredType(getContext()); |
| 1048 | break; |
| 1049 | |
| 1050 | case Type::Typedef: |
| 1051 | case Type::Decltype: |
| 1052 | case Type::Auto: |
| 1053 | // Stop walking: nothing to do. |
| 1054 | return; |
| 1055 | |
| 1056 | case Type::TypeOfExpr: |
| 1057 | // Stop walking: emit typeof expression. |
| 1058 | EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr()); |
| 1059 | return; |
| 1060 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1061 | case Type::Atomic: |
| 1062 | type = cast<AtomicType>(ty)->getValueType(); |
| 1063 | break; |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1064 | } |
| 1065 | } while (type->isVariablyModifiedType()); |
Anders Carlsson | dcc90d8 | 2008-12-12 07:19:02 +0000 | [diff] [blame] | 1066 | } |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 1067 | |
| 1068 | llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) { |
Dan Gohman | bc07a55 | 2010-10-29 22:47:07 +0000 | [diff] [blame] | 1069 | if (getContext().getBuiltinVaListType()->isArrayType()) |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 1070 | return EmitScalarExpr(E); |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 1071 | return EmitLValue(E).getAddress(); |
| 1072 | } |
Anders Carlsson | 6ccc476 | 2009-02-07 22:53:43 +0000 | [diff] [blame] | 1073 | |
Devang Patel | 8d30838 | 2010-08-10 07:24:25 +0000 | [diff] [blame] | 1074 | void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E, |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 1075 | llvm::Constant *Init) { |
Devang Patel | 25c2c8f | 2010-08-10 17:53:33 +0000 | [diff] [blame] | 1076 | assert (Init && "Invalid DeclRefExpr initializer!"); |
| 1077 | if (CGDebugInfo *Dbg = getDebugInfo()) |
Devang Patel | d2829b7 | 2010-10-06 15:58:57 +0000 | [diff] [blame] | 1078 | Dbg->EmitGlobalVariable(E->getDecl(), Init); |
Devang Patel | 8d30838 | 2010-08-10 07:24:25 +0000 | [diff] [blame] | 1079 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1080 | |
| 1081 | CodeGenFunction::PeepholeProtection |
| 1082 | CodeGenFunction::protectFromPeepholes(RValue rvalue) { |
| 1083 | // At the moment, the only aggressive peephole we do in IR gen |
| 1084 | // is trunc(zext) folding, but if we add more, we can easily |
| 1085 | // extend this protection. |
| 1086 | |
| 1087 | if (!rvalue.isScalar()) return PeepholeProtection(); |
| 1088 | llvm::Value *value = rvalue.getScalarVal(); |
| 1089 | if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection(); |
| 1090 | |
| 1091 | // Just make an extra bitcast. |
| 1092 | assert(HaveInsertPoint()); |
| 1093 | llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "", |
| 1094 | Builder.GetInsertBlock()); |
| 1095 | |
| 1096 | PeepholeProtection protection; |
| 1097 | protection.Inst = inst; |
| 1098 | return protection; |
| 1099 | } |
| 1100 | |
| 1101 | void CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) { |
| 1102 | if (!protection.Inst) return; |
| 1103 | |
| 1104 | // In theory, we could try to duplicate the peepholes now, but whatever. |
| 1105 | protection.Inst->eraseFromParent(); |
| 1106 | } |
Julien Lerouge | 77f68bb | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 1107 | |
| 1108 | llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Value *AnnotationFn, |
| 1109 | llvm::Value *AnnotatedVal, |
| 1110 | llvm::StringRef AnnotationStr, |
| 1111 | SourceLocation Location) { |
| 1112 | llvm::Value *Args[4] = { |
| 1113 | AnnotatedVal, |
| 1114 | Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy), |
| 1115 | Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy), |
| 1116 | CGM.EmitAnnotationLineNo(Location) |
| 1117 | }; |
| 1118 | return Builder.CreateCall(AnnotationFn, Args); |
| 1119 | } |
| 1120 | |
| 1121 | void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) { |
| 1122 | assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); |
| 1123 | // FIXME We create a new bitcast for every annotation because that's what |
| 1124 | // llvm-gcc was doing. |
| 1125 | for (specific_attr_iterator<AnnotateAttr> |
| 1126 | ai = D->specific_attr_begin<AnnotateAttr>(), |
| 1127 | ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai) |
| 1128 | EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation), |
| 1129 | Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()), |
| 1130 | (*ai)->getAnnotation(), D->getLocation()); |
| 1131 | } |
| 1132 | |
| 1133 | llvm::Value *CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D, |
| 1134 | llvm::Value *V) { |
| 1135 | assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); |
| 1136 | llvm::Type *VTy = V->getType(); |
| 1137 | llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation, |
| 1138 | CGM.Int8PtrTy); |
| 1139 | |
| 1140 | for (specific_attr_iterator<AnnotateAttr> |
| 1141 | ai = D->specific_attr_begin<AnnotateAttr>(), |
| 1142 | ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai) { |
| 1143 | // FIXME Always emit the cast inst so we can differentiate between |
| 1144 | // annotation on the first field of a struct and annotation on the struct |
| 1145 | // itself. |
| 1146 | if (VTy != CGM.Int8PtrTy) |
| 1147 | V = Builder.Insert(new llvm::BitCastInst(V, CGM.Int8PtrTy)); |
| 1148 | V = EmitAnnotationCall(F, V, (*ai)->getAnnotation(), D->getLocation()); |
| 1149 | V = Builder.CreateBitCast(V, VTy); |
| 1150 | } |
| 1151 | |
| 1152 | return V; |
| 1153 | } |