2007-05-28 09:07:47 +08:00
|
|
|
//===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-05-28 09:07:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This coordinates the per-function state used while generating code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2011-10-07 02:51:56 +08:00
|
|
|
#include "CGCUDARuntime.h"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2008-05-22 09:40:10 +08:00
|
|
|
#include "CGDebugInfo.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CodeGenModule.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2009-04-05 04:47:02 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2009-12-05 07:26:17 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-06-22 08:03:40 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/DataLayout.h"
|
2010-06-22 08:03:40 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2012-07-16 07:28:01 +08:00
|
|
|
#include "llvm/MDBuilder.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Operator.h"
|
2007-05-28 09:07:47 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2012-06-27 00:06:38 +08:00
|
|
|
CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
|
2011-02-15 17:22:45 +08:00
|
|
|
: CodeGenTypeCache(cgm), CGM(cgm),
|
2012-02-11 10:57:39 +08:00
|
|
|
Target(CGM.getContext().getTargetInfo()),
|
|
|
|
Builder(cgm.getModule().getContext()),
|
2012-11-06 06:21:05 +08:00
|
|
|
SanitizePerformTypeCheck(CGM.getLangOpts().SanitizeNull |
|
|
|
|
CGM.getLangOpts().SanitizeAlignment |
|
|
|
|
CGM.getLangOpts().SanitizeObjectSize |
|
|
|
|
CGM.getLangOpts().SanitizeVptr),
|
2011-06-16 07:02:42 +08:00
|
|
|
AutoreleaseResult(false), BlockInfo(0), BlockPointer(0),
|
2012-03-01 12:01:32 +08:00
|
|
|
LambdaThisCaptureField(0), NormalCleanupDest(0), NextCleanupDestIndex(1),
|
2012-02-11 10:57:39 +08:00
|
|
|
FirstBlockInfo(0), EHResumeBlock(0), ExceptionSlot(0), EHSelectorSlot(0),
|
2011-05-29 05:13:02 +08:00
|
|
|
DebugInfo(0), DisableDebugInfo(false), DidCallStackSave(false),
|
|
|
|
IndirectBranch(0), SwitchInsn(0), CaseRangeBlock(0), UnreachableBlock(0),
|
2012-02-11 10:57:39 +08:00
|
|
|
CXXABIThisDecl(0), CXXABIThisValue(0), CXXThisValue(0), CXXVTTDecl(0),
|
|
|
|
CXXVTTValue(0), OutermostConditional(0), TerminateLandingPad(0),
|
|
|
|
TerminateHandler(0), TrapBB(0) {
|
2012-06-27 00:06:38 +08:00
|
|
|
if (!suppressNewContext)
|
|
|
|
CGM.getCXXABI().getMangleContext().startNewFunction();
|
2012-12-04 08:36:06 +08:00
|
|
|
|
|
|
|
llvm::FastMathFlags FMF;
|
|
|
|
if (CGM.getLangOpts().FastMath)
|
|
|
|
FMF.UnsafeAlgebra = true;
|
|
|
|
if (CGM.getLangOpts().FiniteMathOnly) {
|
|
|
|
FMF.NoNaNs = true;
|
|
|
|
FMF.NoInfs = true;
|
|
|
|
}
|
|
|
|
Builder.SetFastMathFlags(FMF);
|
2008-06-18 02:05:57 +08:00
|
|
|
}
|
2007-05-30 07:17:50 +08:00
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
CodeGenFunction::~CodeGenFunction() {
|
|
|
|
// If there are any unclaimed block infos, go ahead and destroy them
|
|
|
|
// now. This can happen if IR-gen gets clever and skips evaluating
|
|
|
|
// something.
|
|
|
|
if (FirstBlockInfo)
|
|
|
|
destroyBlockInfos(FirstBlockInfo);
|
|
|
|
}
|
|
|
|
|
2007-05-30 07:17:50 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) {
|
2009-02-04 07:03:55 +08:00
|
|
|
return CGM.getTypes().ConvertTypeForMem(T);
|
|
|
|
}
|
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *CodeGenFunction::ConvertType(QualType T) {
|
2007-06-23 03:05:19 +08:00
|
|
|
return CGM.getTypes().ConvertType(T);
|
2007-06-09 10:28:57 +08:00
|
|
|
}
|
2007-05-30 07:17:50 +08:00
|
|
|
|
2011-05-15 10:34:36 +08:00
|
|
|
bool CodeGenFunction::hasAggregateLLVMType(QualType type) {
|
|
|
|
switch (type.getCanonicalType()->getTypeClass()) {
|
|
|
|
#define TYPE(name, parent)
|
|
|
|
#define ABSTRACT_TYPE(name, parent)
|
|
|
|
#define NON_CANONICAL_TYPE(name, parent) case Type::name:
|
|
|
|
#define DEPENDENT_TYPE(name, parent) case Type::name:
|
|
|
|
#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
|
|
|
|
#include "clang/AST/TypeNodes.def"
|
|
|
|
llvm_unreachable("non-canonical or dependent type in IR-generation");
|
|
|
|
|
|
|
|
case Type::Builtin:
|
|
|
|
case Type::Pointer:
|
|
|
|
case Type::BlockPointer:
|
|
|
|
case Type::LValueReference:
|
|
|
|
case Type::RValueReference:
|
|
|
|
case Type::MemberPointer:
|
|
|
|
case Type::Vector:
|
|
|
|
case Type::ExtVector:
|
|
|
|
case Type::FunctionProto:
|
|
|
|
case Type::FunctionNoProto:
|
|
|
|
case Type::Enum:
|
|
|
|
case Type::ObjCObjectPointer:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Complexes, arrays, records, and Objective-C objects.
|
|
|
|
case Type::Complex:
|
|
|
|
case Type::ConstantArray:
|
|
|
|
case Type::IncompleteArray:
|
|
|
|
case Type::VariableArray:
|
|
|
|
case Type::Record:
|
|
|
|
case Type::ObjCObject:
|
|
|
|
case Type::ObjCInterface:
|
|
|
|
return true;
|
2011-10-07 07:00:33 +08:00
|
|
|
|
|
|
|
// In IRGen, atomic types are just the underlying type
|
|
|
|
case Type::Atomic:
|
|
|
|
return hasAggregateLLVMType(type->getAs<AtomicType>()->getValueType());
|
2011-05-15 10:34:36 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("unknown type kind!");
|
2008-06-18 02:05:57 +08:00
|
|
|
}
|
2008-03-31 07:03:07 +08:00
|
|
|
|
2009-01-27 07:27:52 +08:00
|
|
|
void CodeGenFunction::EmitReturnBlock() {
|
|
|
|
// For cleanliness, we try to avoid emitting the return block for
|
|
|
|
// simple cases.
|
|
|
|
llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
if (CurBB) {
|
|
|
|
assert(!CurBB->getTerminator() && "Unexpected terminated block.");
|
|
|
|
|
2009-07-19 16:24:34 +08:00
|
|
|
// We have a valid insert point, reuse it if it is empty or there are no
|
|
|
|
// explicit jumps to the return block.
|
2010-07-24 05:56:41 +08:00
|
|
|
if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) {
|
|
|
|
ReturnBlock.getBlock()->replaceAllUsesWith(CurBB);
|
|
|
|
delete ReturnBlock.getBlock();
|
2009-07-19 16:24:34 +08:00
|
|
|
} else
|
2010-07-24 05:56:41 +08:00
|
|
|
EmitBlock(ReturnBlock.getBlock());
|
2009-01-27 07:27:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if the return block is the target of a single direct
|
|
|
|
// branch then we can just put the code in that block instead. This
|
|
|
|
// cleans up functions which started with a unified return block.
|
2010-07-24 05:56:41 +08:00
|
|
|
if (ReturnBlock.getBlock()->hasOneUse()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
llvm::BranchInst *BI =
|
2010-07-24 05:56:41 +08:00
|
|
|
dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->use_begin());
|
2010-07-06 09:34:17 +08:00
|
|
|
if (BI && BI->isUnconditional() &&
|
2010-07-24 05:56:41 +08:00
|
|
|
BI->getSuccessor(0) == ReturnBlock.getBlock()) {
|
2011-09-10 05:53:04 +08:00
|
|
|
// Reset insertion point, including debug location, and delete the branch.
|
|
|
|
Builder.SetCurrentDebugLocation(BI->getDebugLoc());
|
2009-01-27 07:27:52 +08:00
|
|
|
Builder.SetInsertPoint(BI->getParent());
|
|
|
|
BI->eraseFromParent();
|
2010-07-24 05:56:41 +08:00
|
|
|
delete ReturnBlock.getBlock();
|
2009-01-27 07:27:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME: We are at an unreachable point, there is no reason to emit the block
|
|
|
|
// unless it has uses. However, we still need a place to put the debug
|
|
|
|
// region.end for now.
|
2009-01-27 07:27:52 +08:00
|
|
|
|
2010-07-24 05:56:41 +08:00
|
|
|
EmitBlock(ReturnBlock.getBlock());
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) {
|
|
|
|
if (!BB) return;
|
|
|
|
if (!BB->use_empty())
|
|
|
|
return CGF.CurFn->getBasicBlockList().push_back(BB);
|
|
|
|
delete BB;
|
2009-01-27 07:27:52 +08:00
|
|
|
}
|
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
void CodeGenFunction::FinishFunction(SourceLocation EndLoc) {
|
2008-03-31 07:03:07 +08:00
|
|
|
assert(BreakContinueStack.empty() &&
|
|
|
|
"mismatched push/pop in break/continue stack!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// Pop any cleanups that might have been associated with the
|
|
|
|
// parameters. Do this in whatever block we're currently in; it's
|
|
|
|
// important to do this before we enter the return block or return
|
|
|
|
// edges will be *really* confused.
|
|
|
|
if (EHStack.stable_begin() != PrologueCleanupDepth)
|
|
|
|
PopCleanupBlocks(PrologueCleanupDepth);
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Emit function epilog (to return).
|
2009-01-27 07:27:52 +08:00
|
|
|
EmitReturnBlock();
|
2008-11-12 04:59:54 +08:00
|
|
|
|
2011-02-11 01:32:22 +08:00
|
|
|
if (ShouldInstrumentFunction())
|
|
|
|
EmitFunctionInstrumentation("__cyg_profile_func_exit");
|
2010-06-22 08:03:40 +08:00
|
|
|
|
2008-11-12 04:59:54 +08:00
|
|
|
// Emit debug descriptor for function end.
|
2009-02-13 16:11:52 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2008-11-12 04:59:54 +08:00
|
|
|
DI->setLocation(EndLoc);
|
2010-07-23 06:29:16 +08:00
|
|
|
DI->EmitFunctionEnd(Builder);
|
2008-11-12 04:59:54 +08:00
|
|
|
}
|
|
|
|
|
Change IR generation for return (in the simple case) to avoid doing silly
load/store nonsense in the epilog. For example, for:
int foo(int X) {
int A[100];
return A[X];
}
we used to generate:
%arrayidx = getelementptr inbounds [100 x i32]* %A, i32 0, i64 %idxprom ; <i32*> [#uses=1]
%tmp1 = load i32* %arrayidx ; <i32> [#uses=1]
store i32 %tmp1, i32* %retval
%0 = load i32* %retval ; <i32> [#uses=1]
ret i32 %0
}
which codegen'd to this code:
_foo: ## @foo
## BB#0: ## %entry
subq $408, %rsp ## imm = 0x198
movl %edi, 400(%rsp)
movl 400(%rsp), %edi
movslq %edi, %rax
movl (%rsp,%rax,4), %edi
movl %edi, 404(%rsp)
movl 404(%rsp), %eax
addq $408, %rsp ## imm = 0x198
ret
Now we generate:
%arrayidx = getelementptr inbounds [100 x i32]* %A, i32 0, i64 %idxprom ; <i32*> [#uses=1]
%tmp1 = load i32* %arrayidx ; <i32> [#uses=1]
ret i32 %tmp1
}
and:
_foo: ## @foo
## BB#0: ## %entry
subq $408, %rsp ## imm = 0x198
movl %edi, 404(%rsp)
movl 404(%rsp), %edi
movslq %edi, %rax
movl (%rsp,%rax,4), %eax
addq $408, %rsp ## imm = 0x198
ret
This actually does matter, cutting out 2000 lines of IR from CGStmt.ll
for example.
Another interesting effect is that altivec.h functions which are dead
now get dce'd by the inliner. Hence all the changes to
builtins-ppc-altivec.c to ensure the calls aren't dead.
llvm-svn: 106970
2010-06-27 09:06:27 +08:00
|
|
|
EmitFunctionEpilog(*CurFnInfo);
|
2009-12-08 07:38:24 +08:00
|
|
|
EmitEndEHSpec(CurCodeDecl);
|
2008-09-10 05:00:17 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
assert(EHStack.empty() &&
|
|
|
|
"did not remove all scopes from cleanup stack!");
|
|
|
|
|
2009-10-29 07:59:40 +08:00
|
|
|
// If someone did an indirect goto, emit the indirect goto block at the end of
|
|
|
|
// the function.
|
|
|
|
if (IndirectBranch) {
|
|
|
|
EmitBlock(IndirectBranch->getParent());
|
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
}
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2008-03-31 07:03:07 +08:00
|
|
|
// Remove the AllocaInsertPt instruction, which is just a convenience for us.
|
2009-04-01 06:17:44 +08:00
|
|
|
llvm::Instruction *Ptr = AllocaInsertPt;
|
2008-03-31 07:03:07 +08:00
|
|
|
AllocaInsertPt = 0;
|
2009-04-01 06:17:44 +08:00
|
|
|
Ptr->eraseFromParent();
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2009-10-29 07:59:40 +08:00
|
|
|
// If someone took the address of a label but never did an indirect goto, we
|
|
|
|
// made a zero entry PHI node, which is illegal, zap it now.
|
|
|
|
if (IndirectBranch) {
|
|
|
|
llvm::PHINode *PN = cast<llvm::PHINode>(IndirectBranch->getAddress());
|
|
|
|
if (PN->getNumIncomingValues() == 0) {
|
|
|
|
PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType()));
|
|
|
|
PN->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
EmitIfUsed(*this, EHResumeBlock);
|
2010-07-06 09:34:17 +08:00
|
|
|
EmitIfUsed(*this, TerminateLandingPad);
|
|
|
|
EmitIfUsed(*this, TerminateHandler);
|
|
|
|
EmitIfUsed(*this, UnreachableBlock);
|
2010-07-07 07:57:41 +08:00
|
|
|
|
|
|
|
if (CGM.getCodeGenOpts().EmitDeclMetadata)
|
|
|
|
EmitDeclMetadata();
|
2008-04-04 12:07:35 +08:00
|
|
|
}
|
|
|
|
|
2010-06-22 08:03:40 +08:00
|
|
|
/// ShouldInstrumentFunction - Return true if the current function should be
|
|
|
|
/// instrumented with __cyg_profile_func_* calls
|
|
|
|
bool CodeGenFunction::ShouldInstrumentFunction() {
|
|
|
|
if (!CGM.getCodeGenOpts().InstrumentFunctions)
|
|
|
|
return false;
|
2011-05-17 07:49:20 +08:00
|
|
|
if (!CurFuncDecl || CurFuncDecl->hasAttr<NoInstrumentFunctionAttr>())
|
2010-06-22 08:03:40 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitFunctionInstrumentation - Emit LLVM code to call the specified
|
|
|
|
/// instrumentation function with the current function and the call site, if
|
|
|
|
/// function instrumentation is enabled.
|
|
|
|
void CodeGenFunction::EmitFunctionInstrumentation(const char *Fn) {
|
2010-06-23 13:21:28 +08:00
|
|
|
// void __cyg_profile_func_{enter,exit} (void *this_fn, void *call_site);
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::PointerType *PointerTy = Int8PtrTy;
|
|
|
|
llvm::Type *ProfileFuncArgs[] = { PointerTy, PointerTy };
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FunctionTy =
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::FunctionType::get(VoidTy, ProfileFuncArgs, false);
|
2010-06-22 08:03:40 +08:00
|
|
|
|
|
|
|
llvm::Constant *F = CGM.CreateRuntimeFunction(FunctionTy, Fn);
|
|
|
|
llvm::CallInst *CallSite = Builder.CreateCall(
|
2011-07-15 01:45:50 +08:00
|
|
|
CGM.getIntrinsic(llvm::Intrinsic::returnaddress),
|
2010-06-27 15:15:29 +08:00
|
|
|
llvm::ConstantInt::get(Int32Ty, 0),
|
2010-06-22 08:03:40 +08:00
|
|
|
"callsite");
|
|
|
|
|
2010-06-23 13:21:28 +08:00
|
|
|
Builder.CreateCall2(F,
|
|
|
|
llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
|
|
|
|
CallSite);
|
2010-06-22 08:03:40 +08:00
|
|
|
}
|
|
|
|
|
2011-02-11 00:52:03 +08:00
|
|
|
void CodeGenFunction::EmitMCountInstrumentation() {
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
|
2011-02-11 00:52:03 +08:00
|
|
|
|
|
|
|
llvm::Constant *MCountFn = CGM.CreateRuntimeFunction(FTy,
|
|
|
|
Target.getMCountName());
|
|
|
|
Builder.CreateCall(MCountFn);
|
|
|
|
}
|
|
|
|
|
2012-07-12 07:02:10 +08:00
|
|
|
// OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument
|
|
|
|
// information in the program executable. The argument information stored
|
|
|
|
// includes the argument name, its type, the address and access qualifiers used.
|
|
|
|
// FIXME: Add type, address, and access qualifiers.
|
|
|
|
static void GenOpenCLArgMetadata(const FunctionDecl *FD, llvm::Function *Fn,
|
|
|
|
CodeGenModule &CGM,llvm::LLVMContext &Context,
|
|
|
|
llvm::SmallVector <llvm::Value*, 5> &kernelMDArgs) {
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2012-07-12 07:02:10 +08:00
|
|
|
// Create MDNodes that represents the kernel arg metadata.
|
|
|
|
// Each MDNode is a list in the form of "key", N number of values which is
|
|
|
|
// the same number of values as their are kernel arguments.
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2012-07-12 07:02:10 +08:00
|
|
|
// MDNode for the kernel argument names.
|
|
|
|
SmallVector<llvm::Value*, 8> argNames;
|
|
|
|
argNames.push_back(llvm::MDString::get(Context, "kernel_arg_name"));
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2012-07-12 07:02:10 +08:00
|
|
|
for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
|
|
|
|
const ParmVarDecl *parm = FD->getParamDecl(i);
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2012-07-12 07:02:10 +08:00
|
|
|
// Get argument name.
|
|
|
|
argNames.push_back(llvm::MDString::get(Context, parm->getName()));
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2012-07-12 07:02:10 +08:00
|
|
|
}
|
|
|
|
// Add MDNode to the list of all metadata.
|
|
|
|
kernelMDArgs.push_back(llvm::MDNode::get(Context, argNames));
|
|
|
|
}
|
|
|
|
|
2012-12-04 08:29:55 +08:00
|
|
|
void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
|
2012-07-10 06:06:01 +08:00
|
|
|
llvm::Function *Fn)
|
|
|
|
{
|
|
|
|
if (!FD->hasAttr<OpenCLKernelAttr>())
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::LLVMContext &Context = getLLVMContext();
|
|
|
|
|
|
|
|
llvm::SmallVector <llvm::Value*, 5> kernelMDArgs;
|
|
|
|
kernelMDArgs.push_back(Fn);
|
|
|
|
|
2012-07-12 07:02:10 +08:00
|
|
|
if (CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
|
|
|
|
GenOpenCLArgMetadata(FD, Fn, CGM, Context, kernelMDArgs);
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2012-07-10 06:06:01 +08:00
|
|
|
if (FD->hasAttr<WorkGroupSizeHintAttr>()) {
|
|
|
|
WorkGroupSizeHintAttr *attr = FD->getAttr<WorkGroupSizeHintAttr>();
|
2012-11-16 00:40:35 +08:00
|
|
|
llvm::Value *attrMDArgs[] = {
|
|
|
|
llvm::MDString::get(Context, "work_group_size_hint"),
|
|
|
|
Builder.getInt32(attr->getXDim()),
|
|
|
|
Builder.getInt32(attr->getYDim()),
|
|
|
|
Builder.getInt32(attr->getZDim())
|
|
|
|
};
|
2012-07-10 06:06:01 +08:00
|
|
|
kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
|
|
|
|
ReqdWorkGroupSizeAttr *attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
|
2012-11-16 00:40:35 +08:00
|
|
|
llvm::Value *attrMDArgs[] = {
|
|
|
|
llvm::MDString::get(Context, "reqd_work_group_size"),
|
|
|
|
Builder.getInt32(attr->getXDim()),
|
|
|
|
Builder.getInt32(attr->getYDim()),
|
|
|
|
Builder.getInt32(attr->getZDim())
|
|
|
|
};
|
2012-07-10 06:06:01 +08:00
|
|
|
kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::MDNode *kernelMDNode = llvm::MDNode::get(Context, kernelMDArgs);
|
|
|
|
llvm::NamedMDNode *OpenCLKernelMetadata =
|
|
|
|
CGM.getModule().getOrInsertNamedMetadata("opencl.kernels");
|
|
|
|
OpenCLKernelMetadata->addOperand(kernelMDNode);
|
|
|
|
}
|
|
|
|
|
2009-09-11 08:07:24 +08:00
|
|
|
void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
|
2008-09-10 07:14:03 +08:00
|
|
|
llvm::Function *Fn,
|
2011-03-09 12:27:21 +08:00
|
|
|
const CGFunctionInfo &FnInfo,
|
2008-10-19 02:22:23 +08:00
|
|
|
const FunctionArgList &Args,
|
2011-03-03 05:36:49 +08:00
|
|
|
SourceLocation StartLoc) {
|
2009-09-11 08:07:24 +08:00
|
|
|
const Decl *D = GD.getDecl();
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2009-02-10 04:20:56 +08:00
|
|
|
DidCallStackSave = false;
|
2009-04-23 13:30:27 +08:00
|
|
|
CurCodeDecl = CurFuncDecl = D;
|
2008-09-10 07:14:03 +08:00
|
|
|
FnRetTy = RetTy;
|
2008-07-30 07:18:29 +08:00
|
|
|
CurFn = Fn;
|
2011-03-09 12:27:21 +08:00
|
|
|
CurFnInfo = &FnInfo;
|
2007-06-20 12:44:43 +08:00
|
|
|
assert(CurFn->isDeclaration() && "Function already has body?");
|
2008-03-03 11:28:21 +08:00
|
|
|
|
2010-02-09 08:10:00 +08:00
|
|
|
// Pass inline keyword to optimizer if it appears explicitly on any
|
|
|
|
// declaration.
|
2012-12-04 08:29:55 +08:00
|
|
|
if (!CGM.getCodeGenOpts().NoInline)
|
2012-03-15 07:32:11 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
|
|
|
|
for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(),
|
|
|
|
RE = FD->redecls_end(); RI != RE; ++RI)
|
|
|
|
if (RI->isInlineSpecified()) {
|
2012-10-10 11:13:20 +08:00
|
|
|
Fn->addFnAttr(llvm::Attributes::InlineHint);
|
2012-03-15 07:32:11 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-02-09 08:10:00 +08:00
|
|
|
|
2012-11-02 06:30:59 +08:00
|
|
|
if (getLangOpts().OpenCL) {
|
2011-02-14 09:42:53 +08:00
|
|
|
// Add metadata for a kernel function.
|
|
|
|
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
|
2012-07-10 06:06:01 +08:00
|
|
|
EmitOpenCLKernelMetadata(FD, Fn);
|
2011-02-14 09:42:53 +08:00
|
|
|
}
|
|
|
|
|
2008-11-11 10:29:29 +08:00
|
|
|
llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn);
|
2008-09-10 05:00:17 +08:00
|
|
|
|
2007-06-02 12:53:11 +08:00
|
|
|
// Create a marker to make it easy to insert allocas into the entryblock
|
2007-12-18 04:50:59 +08:00
|
|
|
// later. Don't create this with the builder, because we don't want it
|
|
|
|
// folded.
|
2010-06-27 15:15:29 +08:00
|
|
|
llvm::Value *Undef = llvm::UndefValue::get(Int32Ty);
|
|
|
|
AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "", EntryBB);
|
2009-03-22 08:24:14 +08:00
|
|
|
if (Builder.isNamePreserving())
|
|
|
|
AllocaInsertPt->setName("allocapt");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
ReturnBlock = getJumpDestInCurrentScope("return");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 04:50:59 +08:00
|
|
|
Builder.SetInsertPoint(EntryBB);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-04 19:04:26 +08:00
|
|
|
// Emit subprogram debug descriptor.
|
2009-02-13 16:11:52 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2011-10-22 07:30:10 +08:00
|
|
|
unsigned NumArgs = 0;
|
|
|
|
QualType *ArgsArray = new QualType[Args.size()];
|
|
|
|
for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
ArgsArray[NumArgs++] = (*i)->getType();
|
|
|
|
}
|
|
|
|
|
2010-12-14 16:05:40 +08:00
|
|
|
QualType FnType =
|
2011-10-22 07:30:10 +08:00
|
|
|
getContext().getFunctionType(RetTy, ArgsArray, NumArgs,
|
2010-12-14 16:05:40 +08:00
|
|
|
FunctionProtoType::ExtProtoInfo());
|
|
|
|
|
2011-10-25 01:22:36 +08:00
|
|
|
delete[] ArgsArray;
|
2011-10-22 07:30:10 +08:00
|
|
|
|
2008-10-19 02:22:23 +08:00
|
|
|
DI->setLocation(StartLoc);
|
2010-01-14 08:36:21 +08:00
|
|
|
DI->EmitFunctionStart(GD, FnType, CurFn, Builder);
|
2008-07-04 19:04:26 +08:00
|
|
|
}
|
|
|
|
|
2011-02-11 01:32:22 +08:00
|
|
|
if (ShouldInstrumentFunction())
|
|
|
|
EmitFunctionInstrumentation("__cyg_profile_func_enter");
|
2010-06-22 08:03:40 +08:00
|
|
|
|
2011-02-11 00:52:03 +08:00
|
|
|
if (CGM.getCodeGenOpts().InstrumentForProfiling)
|
|
|
|
EmitMCountInstrumentation();
|
|
|
|
|
2009-12-04 10:43:40 +08:00
|
|
|
if (RetTy->isVoidType()) {
|
|
|
|
// Void type; nothing to return.
|
|
|
|
ReturnValue = 0;
|
|
|
|
} else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
|
|
|
|
hasAggregateLLVMType(CurFnInfo->getReturnType())) {
|
|
|
|
// Indirect aggregate return; emit returned value directly into sret slot.
|
2010-02-17 03:45:20 +08:00
|
|
|
// This reduces code size, and affects correctness in C++.
|
2009-12-04 10:43:40 +08:00
|
|
|
ReturnValue = CurFn->arg_begin();
|
|
|
|
} else {
|
2010-02-17 03:45:20 +08:00
|
|
|
ReturnValue = CreateIRTemp(RetTy, "retval");
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
// Tell the epilog emitter to autorelease the result. We do this
|
|
|
|
// now so that various specialized functions can suppress it
|
|
|
|
// during their IR-generation.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2011-06-16 07:02:42 +08:00
|
|
|
!CurFnInfo->isReturnsRetained() &&
|
|
|
|
RetTy->isObjCRetainableType())
|
|
|
|
AutoreleaseResult = true;
|
2009-12-04 10:43:40 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 07:38:24 +08:00
|
|
|
EmitStartEHSpec(CurCodeDecl);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
PrologueCleanupDepth = EHStack.stable_begin();
|
2009-02-03 06:03:45 +08:00
|
|
|
EmitFunctionProlog(*CurFnInfo, CurFn, Args);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-11 10:57:39 +08:00
|
|
|
if (D && isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) {
|
2010-08-31 15:33:07 +08:00
|
|
|
CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
|
2012-02-11 10:57:39 +08:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
|
|
|
|
if (MD->getParent()->isLambda() &&
|
|
|
|
MD->getOverloadedOperator() == OO_Call) {
|
|
|
|
// We're in a lambda; figure out the captures.
|
|
|
|
MD->getParent()->getCaptureFields(LambdaCaptureFields,
|
|
|
|
LambdaThisCaptureField);
|
|
|
|
if (LambdaThisCaptureField) {
|
|
|
|
// If this lambda captures this, load it.
|
2012-04-16 11:54:45 +08:00
|
|
|
QualType LambdaTagType =
|
|
|
|
getContext().getTagDeclType(LambdaThisCaptureField->getParent());
|
|
|
|
LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue,
|
|
|
|
LambdaTagType);
|
|
|
|
LValue ThisLValue = EmitLValueForField(LambdaLV,
|
|
|
|
LambdaThisCaptureField);
|
2012-02-11 10:57:39 +08:00
|
|
|
CXXThisValue = EmitLoadOfLValue(ThisLValue).getScalarVal();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Not in a lambda; just use 'this' from the method.
|
|
|
|
// FIXME: Should we generate a new load for each use of 'this'? The
|
|
|
|
// fast register allocator would be happier...
|
|
|
|
CXXThisValue = CXXABIThisValue;
|
|
|
|
}
|
|
|
|
}
|
2010-02-17 06:04:33 +08:00
|
|
|
|
2008-12-21 05:28:43 +08:00
|
|
|
// If any of the arguments have a variably modified type, make sure to
|
|
|
|
// emit the type size.
|
|
|
|
for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
|
|
|
|
i != e; ++i) {
|
2012-11-15 06:09:59 +08:00
|
|
|
const VarDecl *VD = *i;
|
|
|
|
|
|
|
|
// Dig out the type as written from ParmVarDecls; it's unclear whether
|
|
|
|
// the standard (C99 6.9.1p10) requires this, but we're following the
|
|
|
|
// precedent set by gcc.
|
|
|
|
QualType Ty;
|
|
|
|
if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD))
|
|
|
|
Ty = PVD->getOriginalType();
|
|
|
|
else
|
|
|
|
Ty = VD->getType();
|
2008-12-21 05:28:43 +08:00
|
|
|
|
|
|
|
if (Ty->isVariablyModifiedType())
|
2011-06-25 05:55:10 +08:00
|
|
|
EmitVariablyModifiedType(Ty);
|
2008-12-21 05:28:43 +08:00
|
|
|
}
|
2011-10-14 05:45:18 +08:00
|
|
|
// Emit a location at the end of the prologue.
|
|
|
|
if (CGDebugInfo *DI = getDebugInfo())
|
|
|
|
DI->EmitLocation(Builder, StartLoc);
|
2008-09-10 07:14:03 +08:00
|
|
|
}
|
2008-08-26 05:31:01 +08:00
|
|
|
|
2010-02-19 17:25:03 +08:00
|
|
|
void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args) {
|
|
|
|
const FunctionDecl *FD = cast<FunctionDecl>(CurGD.getDecl());
|
Complete reimplementation of the synthesis for implicitly-defined copy
assignment operators.
Previously, Sema provided type-checking and template instantiation for
copy assignment operators, then CodeGen would synthesize the actual
body of the copy constructor. Unfortunately, the two were not in sync,
and CodeGen might pick a copy-assignment operator that is different
from what Sema chose, leading to strange failures, e.g., link-time
failures when CodeGen called a copy-assignment operator that was not
instantiation, run-time failures when copy-assignment operators were
overloaded for const/non-const references and the wrong one was
picked, and run-time failures when by-value copy-assignment operators
did not have their arguments properly copy-initialized.
This implementation synthesizes the implicitly-defined copy assignment
operator bodies in Sema, so that the resulting ASTs encode exactly
what CodeGen needs to do; there is no longer any special code in
CodeGen to synthesize copy-assignment operators. The synthesis of the
body is relatively simple, and we generate one of three different
kinds of copy statements for each base or member:
- For a class subobject, call the appropriate copy-assignment
operator, after overload resolution has determined what that is.
- For an array of scalar types or an array of class types that have
trivial copy assignment operators, construct a call to
__builtin_memcpy.
- For an array of class types with non-trivial copy assignment
operators, synthesize a (possibly nested!) for loop whose inner
statement calls the copy constructor.
- For a scalar type, use built-in assignment.
This patch fixes at least a few tests cases in Boost.Spirit that were
failing because CodeGen picked the wrong copy-assignment operator
(leading to link-time failures), and I suspect a number of undiagnosed
problems will also go away with this change.
Some of the diagnostics we had previously have gotten worse with this
change, since we're going through generic code for our
type-checking. I will improve this in a subsequent patch.
llvm-svn: 102853
2010-05-02 04:49:11 +08:00
|
|
|
assert(FD->getBody());
|
|
|
|
EmitStmt(FD->getBody());
|
2010-02-18 11:17:58 +08:00
|
|
|
}
|
|
|
|
|
2010-08-04 06:46:07 +08:00
|
|
|
/// Tries to mark the given function nounwind based on the
|
|
|
|
/// non-existence of any throwing calls within it. We believe this is
|
|
|
|
/// lightweight enough to do at -O0.
|
|
|
|
static void TryMarkNoThrow(llvm::Function *F) {
|
2010-08-12 06:38:33 +08:00
|
|
|
// LLVM treats 'nounwind' on a function as part of the type, so we
|
|
|
|
// can't do this on functions that can be overwritten.
|
|
|
|
if (F->mayBeOverridden()) return;
|
|
|
|
|
2010-08-04 06:46:07 +08:00
|
|
|
for (llvm::Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
|
|
|
|
for (llvm::BasicBlock::iterator
|
|
|
|
BI = FI->begin(), BE = FI->end(); BI != BE; ++BI)
|
2011-09-20 04:31:14 +08:00
|
|
|
if (llvm::CallInst *Call = dyn_cast<llvm::CallInst>(&*BI)) {
|
2010-08-04 06:46:07 +08:00
|
|
|
if (!Call->doesNotThrow())
|
|
|
|
return;
|
2011-09-20 04:31:14 +08:00
|
|
|
} else if (isa<llvm::ResumeInst>(&*BI)) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-10 11:13:20 +08:00
|
|
|
F->setDoesNotThrow();
|
2010-08-04 06:46:07 +08:00
|
|
|
}
|
|
|
|
|
2011-03-09 12:27:21 +08:00
|
|
|
void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn,
|
|
|
|
const CGFunctionInfo &FnInfo) {
|
2009-09-11 08:07:24 +08:00
|
|
|
const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2009-02-13 16:11:52 +08:00
|
|
|
// Check if we should generate debug info for this function.
|
2012-10-16 15:22:28 +08:00
|
|
|
if (!FD->hasAttr<NoDebugAttr>())
|
|
|
|
maybeInitializeDebugInfo();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-10 07:14:03 +08:00
|
|
|
FunctionArgList Args;
|
2010-08-31 15:33:07 +08:00
|
|
|
QualType ResTy = FD->getResultType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-05 07:26:17 +08:00
|
|
|
CurGD = GD;
|
2010-08-31 15:33:07 +08:00
|
|
|
if (isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isInstance())
|
|
|
|
CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResTy, Args);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-18 08:37:07 +08:00
|
|
|
for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i)
|
|
|
|
Args.push_back(FD->getParamDecl(i));
|
2008-08-26 16:29:31 +08:00
|
|
|
|
2010-02-18 11:17:58 +08:00
|
|
|
SourceRange BodyRange;
|
|
|
|
if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
|
2009-11-06 10:55:43 +08:00
|
|
|
|
2010-02-18 11:17:58 +08:00
|
|
|
// Emit the standard function prologue.
|
2011-03-09 12:27:21 +08:00
|
|
|
StartFunction(GD, ResTy, Fn, FnInfo, Args, BodyRange.getBegin());
|
2010-02-08 03:45:40 +08:00
|
|
|
|
2010-02-18 11:17:58 +08:00
|
|
|
// Generate the body of the function.
|
2010-02-19 17:25:03 +08:00
|
|
|
if (isa<CXXDestructorDecl>(FD))
|
|
|
|
EmitDestructorBody(Args);
|
|
|
|
else if (isa<CXXConstructorDecl>(FD))
|
|
|
|
EmitConstructorBody(Args);
|
2012-11-02 06:30:59 +08:00
|
|
|
else if (getLangOpts().CUDA &&
|
2011-10-07 02:51:56 +08:00
|
|
|
!CGM.getCodeGenOpts().CUDAIsDevice &&
|
|
|
|
FD->hasAttr<CUDAGlobalAttr>())
|
|
|
|
CGM.getCUDARuntime().EmitDeviceStubBody(*this, Args);
|
2012-02-16 09:37:33 +08:00
|
|
|
else if (isa<CXXConversionDecl>(FD) &&
|
2012-02-17 11:02:34 +08:00
|
|
|
cast<CXXConversionDecl>(FD)->isLambdaToBlockPointerConversion()) {
|
|
|
|
// The lambda conversion to block pointer is special; the semantics can't be
|
|
|
|
// expressed in the AST, so IRGen needs to special-case it.
|
|
|
|
EmitLambdaToBlockPointerBody(Args);
|
|
|
|
} else if (isa<CXXMethodDecl>(FD) &&
|
|
|
|
cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) {
|
|
|
|
// The lambda "__invoke" function is special, because it forwards or
|
|
|
|
// clones the body of the function call operator (but is actually static).
|
|
|
|
EmitLambdaStaticInvokeFunction(cast<CXXMethodDecl>(FD));
|
2012-02-16 09:37:33 +08:00
|
|
|
}
|
2010-02-19 17:25:03 +08:00
|
|
|
else
|
|
|
|
EmitFunctionBody(Args);
|
2009-10-07 02:09:57 +08:00
|
|
|
|
2012-10-05 07:52:29 +08:00
|
|
|
// C++11 [stmt.return]p2:
|
|
|
|
// Flowing off the end of a function [...] results in undefined behavior in
|
|
|
|
// a value-returning function.
|
|
|
|
// C11 6.9.1p12:
|
|
|
|
// If the '}' that terminates a function is reached, and the value of the
|
|
|
|
// function call is used by the caller, the behavior is undefined.
|
2012-11-02 06:30:59 +08:00
|
|
|
if (getLangOpts().CPlusPlus && !FD->hasImplicitReturnZero() &&
|
2012-10-05 07:52:29 +08:00
|
|
|
!FD->getResultType()->isVoidType() && Builder.GetInsertBlock()) {
|
2012-11-06 06:21:05 +08:00
|
|
|
if (getLangOpts().SanitizeReturn)
|
2012-10-10 03:52:38 +08:00
|
|
|
EmitCheck(Builder.getFalse(), "missing_return",
|
|
|
|
EmitCheckSourceLocation(FD->getLocation()),
|
2012-12-03 03:50:33 +08:00
|
|
|
llvm::ArrayRef<llvm::Value*>(), CRK_Unrecoverable);
|
2012-10-15 08:23:07 +08:00
|
|
|
else if (CGM.getCodeGenOpts().OptimizationLevel == 0)
|
|
|
|
Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::trap));
|
2012-10-05 07:52:29 +08:00
|
|
|
Builder.CreateUnreachable();
|
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
}
|
|
|
|
|
2010-02-18 11:17:58 +08:00
|
|
|
// Emit the standard function epilogue.
|
|
|
|
FinishFunction(BodyRange.getEnd());
|
2010-08-04 06:46:07 +08:00
|
|
|
|
|
|
|
// If we haven't marked the function nothrow through other means, do
|
|
|
|
// a quick pass now to see if we can.
|
|
|
|
if (!CurFn->doesNotThrow())
|
|
|
|
TryMarkNoThrow(CurFn);
|
2007-05-30 07:50:05 +08:00
|
|
|
}
|
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
/// ContainsLabel - Return true if the statement contains a label in it. If
|
|
|
|
/// this statement is not executed normally, it not containing a label means
|
|
|
|
/// that we can just remove the code.
|
|
|
|
bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) {
|
|
|
|
// Null statement, not a label!
|
|
|
|
if (S == 0) return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
// If this is a label, we have to emit the code, consider something like:
|
|
|
|
// if (0) { ... foo: bar(); } goto foo;
|
2011-02-28 08:18:40 +08:00
|
|
|
//
|
|
|
|
// TODO: If anyone cared, we could track __label__'s, since we know that you
|
|
|
|
// can't jump to one from outside their declared region.
|
2008-11-11 15:41:27 +08:00
|
|
|
if (isa<LabelStmt>(S))
|
|
|
|
return true;
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
// If this is a case/default statement, and we haven't seen a switch, we have
|
|
|
|
// to emit the code.
|
|
|
|
if (isa<SwitchCase>(S) && !IgnoreCaseStmts)
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
// If this is a switch statement, we want to ignore cases below it.
|
|
|
|
if (isa<SwitchStmt>(S))
|
|
|
|
IgnoreCaseStmts = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
// Scan subexpressions for verboten labels.
|
2011-02-13 12:07:26 +08:00
|
|
|
for (Stmt::const_child_range I = S->children(); I; ++I)
|
2008-11-11 15:41:27 +08:00
|
|
|
if (ContainsLabel(*I, IgnoreCaseStmts))
|
|
|
|
return true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-11 15:41:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-28 08:18:40 +08:00
|
|
|
/// containsBreak - Return true if the statement contains a break out of it.
|
|
|
|
/// If the statement (recursively) contains a switch or loop with a break
|
|
|
|
/// inside of it, this is fine.
|
|
|
|
bool CodeGenFunction::containsBreak(const Stmt *S) {
|
|
|
|
// Null statement, not a label!
|
|
|
|
if (S == 0) return false;
|
|
|
|
|
|
|
|
// If this is a switch or loop that defines its own break scope, then we can
|
|
|
|
// include it and anything inside of it.
|
|
|
|
if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || isa<DoStmt>(S) ||
|
|
|
|
isa<ForStmt>(S))
|
2011-02-28 08:42:31 +08:00
|
|
|
return false;
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2011-02-28 08:42:31 +08:00
|
|
|
if (isa<BreakStmt>(S))
|
2011-02-28 08:18:40 +08:00
|
|
|
return true;
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2011-02-28 08:18:40 +08:00
|
|
|
// Scan subexpressions for verboten breaks.
|
|
|
|
for (Stmt::const_child_range I = S->children(); I; ++I)
|
|
|
|
if (containsBreak(*I))
|
|
|
|
return true;
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2011-02-28 08:18:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
|
2011-02-28 07:02:32 +08:00
|
|
|
/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
|
|
|
|
/// to a constant, or if it does but contains a label, return false. If it
|
|
|
|
/// constant folds return true and set the boolean result in Result.
|
|
|
|
bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond,
|
|
|
|
bool &ResultBool) {
|
2012-07-24 04:21:35 +08:00
|
|
|
llvm::APSInt ResultInt;
|
2011-02-28 08:18:40 +08:00
|
|
|
if (!ConstantFoldsToSimpleInteger(Cond, ResultInt))
|
|
|
|
return false;
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2011-02-28 08:18:40 +08:00
|
|
|
ResultBool = ResultInt.getBoolValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ConstantFoldsToSimpleInteger - If the specified expression does not fold
|
|
|
|
/// to a constant, or if it does but contains a label, return false. If it
|
|
|
|
/// constant folds return true and set the folded value.
|
|
|
|
bool CodeGenFunction::
|
2012-07-24 04:21:35 +08:00
|
|
|
ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &ResultInt) {
|
2008-11-13 06:37:10 +08:00
|
|
|
// FIXME: Rename and handle conversion of other evaluatable things
|
|
|
|
// to bool.
|
2011-12-29 03:48:30 +08:00
|
|
|
llvm::APSInt Int;
|
|
|
|
if (!Cond->EvaluateAsInt(Int, getContext()))
|
2011-02-28 07:02:32 +08:00
|
|
|
return false; // Not foldable, not integer or not fully evaluatable.
|
2011-12-29 03:48:30 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
if (CodeGenFunction::ContainsLabel(Cond))
|
2011-02-28 07:02:32 +08:00
|
|
|
return false; // Contains a label.
|
2011-12-29 03:48:30 +08:00
|
|
|
|
|
|
|
ResultInt = Int;
|
2011-02-28 07:02:32 +08:00
|
|
|
return true;
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-28 08:18:40 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
/// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if
|
|
|
|
/// statement) to the specified blocks. Based on the condition, this might try
|
|
|
|
/// to simplify the codegen of the conditional based on the branch.
|
|
|
|
///
|
|
|
|
void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond,
|
|
|
|
llvm::BasicBlock *TrueBlock,
|
|
|
|
llvm::BasicBlock *FalseBlock) {
|
2011-04-15 08:35:48 +08:00
|
|
|
Cond = Cond->IgnoreParens();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
if (const BinaryOperator *CondBOp = dyn_cast<BinaryOperator>(Cond)) {
|
|
|
|
// Handle X && Y in a condition.
|
2010-08-25 19:45:40 +08:00
|
|
|
if (CondBOp->getOpcode() == BO_LAnd) {
|
2008-11-12 16:04:58 +08:00
|
|
|
// If we have "1 && X", simplify the code. "0 && X" would have constant
|
|
|
|
// folded if the case was simple enough.
|
2011-03-05 05:46:03 +08:00
|
|
|
bool ConstantBool = false;
|
2011-02-28 07:02:32 +08:00
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
|
|
|
|
ConstantBool) {
|
2008-11-12 16:04:58 +08:00
|
|
|
// br(1 && X) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// If we have "X && 1", simplify the code to use an uncond branch.
|
|
|
|
// "X && 0" would have been constant folded to 0.
|
2011-02-28 07:02:32 +08:00
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
|
|
|
|
ConstantBool) {
|
2008-11-12 16:04:58 +08:00
|
|
|
// br(X && 1) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// Emit the LHS as a conditional. If the LHS conditional is false, we
|
|
|
|
// want to jump to the FalseBlock.
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true");
|
2011-01-26 12:00:11 +08:00
|
|
|
|
|
|
|
ConditionalEvaluation eval(*this);
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock);
|
|
|
|
EmitBlock(LHSTrue);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-24 08:20:05 +08:00
|
|
|
// Any temporaries created here are conditional.
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.begin(*this);
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.end(*this);
|
2010-01-24 08:20:05 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
return;
|
2011-02-28 07:02:32 +08:00
|
|
|
}
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2011-02-28 07:02:32 +08:00
|
|
|
if (CondBOp->getOpcode() == BO_LOr) {
|
2008-11-12 16:04:58 +08:00
|
|
|
// If we have "0 || X", simplify the code. "1 || X" would have constant
|
|
|
|
// folded if the case was simple enough.
|
2011-03-05 05:46:03 +08:00
|
|
|
bool ConstantBool = false;
|
2011-02-28 07:02:32 +08:00
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) &&
|
|
|
|
!ConstantBool) {
|
2008-11-12 16:04:58 +08:00
|
|
|
// br(0 || X) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// If we have "X || 0", simplify the code to use an uncond branch.
|
|
|
|
// "X || 1" would have been constant folded to 1.
|
2011-02-28 07:02:32 +08:00
|
|
|
if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) &&
|
|
|
|
!ConstantBool) {
|
2008-11-12 16:04:58 +08:00
|
|
|
// br(X || 0) -> br(X).
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// Emit the LHS as a conditional. If the LHS conditional is true, we
|
|
|
|
// want to jump to the TrueBlock.
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false");
|
2011-01-26 12:00:11 +08:00
|
|
|
|
|
|
|
ConditionalEvaluation eval(*this);
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse);
|
|
|
|
EmitBlock(LHSFalse);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-24 08:20:05 +08:00
|
|
|
// Any temporaries created here are conditional.
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.begin(*this);
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock);
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.end(*this);
|
2010-01-24 08:20:05 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-11-12 16:13:36 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 16:13:36 +08:00
|
|
|
if (const UnaryOperator *CondUOp = dyn_cast<UnaryOperator>(Cond)) {
|
|
|
|
// br(!x, t, f) -> br(x, f, t)
|
2010-08-25 19:45:40 +08:00
|
|
|
if (CondUOp->getOpcode() == UO_LNot)
|
2008-11-12 16:13:36 +08:00
|
|
|
return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock);
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-12 18:30:32 +08:00
|
|
|
if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(Cond)) {
|
2012-02-14 11:54:45 +08:00
|
|
|
// br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f))
|
|
|
|
llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
|
|
|
|
llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
|
2008-11-12 18:30:32 +08:00
|
|
|
|
2012-02-14 11:54:45 +08:00
|
|
|
ConditionalEvaluation cond(*this);
|
|
|
|
EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock);
|
2011-01-26 12:00:11 +08:00
|
|
|
|
2012-02-14 11:54:45 +08:00
|
|
|
cond.begin(*this);
|
|
|
|
EmitBlock(LHSBlock);
|
|
|
|
EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock);
|
|
|
|
cond.end(*this);
|
2011-01-26 12:00:11 +08:00
|
|
|
|
2012-02-14 11:54:45 +08:00
|
|
|
cond.begin(*this);
|
|
|
|
EmitBlock(RHSBlock);
|
|
|
|
EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock);
|
|
|
|
cond.end(*this);
|
2011-01-26 12:00:11 +08:00
|
|
|
|
2012-02-14 11:54:45 +08:00
|
|
|
return;
|
2008-11-12 18:30:32 +08:00
|
|
|
}
|
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// Emit the code with the fully general case.
|
|
|
|
llvm::Value *CondV = EvaluateExprAsBool(Cond);
|
|
|
|
Builder.CreateCondBr(CondV, TrueBlock, FalseBlock);
|
|
|
|
}
|
|
|
|
|
2008-08-16 08:56:44 +08:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2007-12-02 09:43:38 +08:00
|
|
|
/// specified stmt yet.
|
2008-09-04 11:43:08 +08:00
|
|
|
void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type,
|
|
|
|
bool OmitOnError) {
|
|
|
|
CGM.ErrorUnsupported(S, Type, OmitOnError);
|
2007-12-02 09:43:38 +08:00
|
|
|
}
|
|
|
|
|
2011-02-02 05:35:06 +08:00
|
|
|
/// emitNonZeroVLAInit - Emit the "zero" initialization of a
|
|
|
|
/// variable-length array whose elements have a non-zero bit-pattern.
|
|
|
|
///
|
2012-06-16 06:10:14 +08:00
|
|
|
/// \param baseType the inner-most element type of the array
|
2011-02-02 05:35:06 +08:00
|
|
|
/// \param src - a char* pointing to the bit-pattern for a single
|
|
|
|
/// base element of the array
|
|
|
|
/// \param sizeInChars - the total size of the VLA, in chars
|
|
|
|
static void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType,
|
2012-12-04 08:29:55 +08:00
|
|
|
llvm::Value *dest, llvm::Value *src,
|
2011-02-02 05:35:06 +08:00
|
|
|
llvm::Value *sizeInChars) {
|
|
|
|
std::pair<CharUnits,CharUnits> baseSizeAndAlign
|
|
|
|
= CGF.getContext().getTypeInfoInChars(baseType);
|
|
|
|
|
|
|
|
CGBuilderTy &Builder = CGF.Builder;
|
|
|
|
|
|
|
|
llvm::Value *baseSizeInChars
|
|
|
|
= llvm::ConstantInt::get(CGF.IntPtrTy, baseSizeAndAlign.first.getQuantity());
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *i8p = Builder.getInt8PtrTy();
|
2011-02-02 05:35:06 +08:00
|
|
|
|
|
|
|
llvm::Value *begin = Builder.CreateBitCast(dest, i8p, "vla.begin");
|
|
|
|
llvm::Value *end = Builder.CreateInBoundsGEP(dest, sizeInChars, "vla.end");
|
|
|
|
|
|
|
|
llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock();
|
|
|
|
llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop");
|
|
|
|
llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont");
|
|
|
|
|
|
|
|
// Make a loop over the VLA. C99 guarantees that the VLA element
|
|
|
|
// count must be nonzero.
|
|
|
|
CGF.EmitBlock(loopBB);
|
|
|
|
|
2011-03-30 19:28:58 +08:00
|
|
|
llvm::PHINode *cur = Builder.CreatePHI(i8p, 2, "vla.cur");
|
2011-02-02 05:35:06 +08:00
|
|
|
cur->addIncoming(begin, originBB);
|
|
|
|
|
|
|
|
// memcpy the individual element bit-pattern.
|
|
|
|
Builder.CreateMemCpy(cur, src, baseSizeInChars,
|
|
|
|
baseSizeAndAlign.second.getQuantity(),
|
|
|
|
/*volatile*/ false);
|
|
|
|
|
|
|
|
// Go to the next element.
|
|
|
|
llvm::Value *next = Builder.CreateConstInBoundsGEP1_32(cur, 1, "vla.next");
|
|
|
|
|
|
|
|
// Leave if that's the end of the VLA.
|
|
|
|
llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone");
|
|
|
|
Builder.CreateCondBr(done, contBB, loopBB);
|
|
|
|
cur->addIncoming(next, loopBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(contBB);
|
2012-12-04 08:29:55 +08:00
|
|
|
}
|
2011-02-02 05:35:06 +08:00
|
|
|
|
2010-05-23 01:35:42 +08:00
|
|
|
void
|
|
|
|
CodeGenFunction::EmitNullInitialization(llvm::Value *DestPtr, QualType Ty) {
|
2010-05-03 09:20:20 +08:00
|
|
|
// Ignore empty classes in C++.
|
2012-11-02 06:30:59 +08:00
|
|
|
if (getLangOpts().CPlusPlus) {
|
2010-05-03 09:20:20 +08:00
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>()) {
|
|
|
|
if (cast<CXXRecordDecl>(RT->getDecl())->isEmpty())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-08-07 16:21:30 +08:00
|
|
|
|
|
|
|
// Cast the dest ptr to the appropriate i8 pointer type.
|
|
|
|
unsigned DestAS =
|
|
|
|
cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *BP = Builder.getInt8PtrTy(DestAS);
|
2008-08-31 03:51:14 +08:00
|
|
|
if (DestPtr->getType() != BP)
|
2011-09-28 05:06:10 +08:00
|
|
|
DestPtr = Builder.CreateBitCast(DestPtr, BP);
|
2008-08-31 03:51:14 +08:00
|
|
|
|
|
|
|
// Get size and alignment info for this aggregate.
|
2012-12-04 08:29:55 +08:00
|
|
|
std::pair<CharUnits, CharUnits> TypeInfo =
|
2011-04-23 01:51:05 +08:00
|
|
|
getContext().getTypeInfoInChars(Ty);
|
|
|
|
CharUnits Size = TypeInfo.first;
|
|
|
|
CharUnits Align = TypeInfo.second;
|
2008-08-31 03:51:14 +08:00
|
|
|
|
2011-01-14 18:37:58 +08:00
|
|
|
llvm::Value *SizeVal;
|
2011-02-02 05:35:06 +08:00
|
|
|
const VariableArrayType *vla;
|
2010-08-07 16:21:30 +08:00
|
|
|
|
2011-01-14 18:37:58 +08:00
|
|
|
// Don't bother emitting a zero-byte memset.
|
2011-04-23 01:51:05 +08:00
|
|
|
if (Size.isZero()) {
|
2011-01-14 18:37:58 +08:00
|
|
|
// But note that getTypeInfo returns 0 for a VLA.
|
|
|
|
if (const VariableArrayType *vlaType =
|
|
|
|
dyn_cast_or_null<VariableArrayType>(
|
|
|
|
getContext().getAsArrayType(Ty))) {
|
2011-06-25 05:55:10 +08:00
|
|
|
QualType eltType;
|
|
|
|
llvm::Value *numElts;
|
|
|
|
llvm::tie(numElts, eltType) = getVLASize(vlaType);
|
|
|
|
|
|
|
|
SizeVal = numElts;
|
|
|
|
CharUnits eltSize = getContext().getTypeSizeInChars(eltType);
|
|
|
|
if (!eltSize.isOne())
|
|
|
|
SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize));
|
2011-02-02 05:35:06 +08:00
|
|
|
vla = vlaType;
|
2011-01-14 18:37:58 +08:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2011-06-25 05:55:10 +08:00
|
|
|
SizeVal = CGM.getSize(Size);
|
2011-02-02 05:35:06 +08:00
|
|
|
vla = 0;
|
2011-01-14 18:37:58 +08:00
|
|
|
}
|
2010-08-07 16:21:30 +08:00
|
|
|
|
|
|
|
// If the type contains a pointer to data member we can't memset it to zero.
|
|
|
|
// Instead, create a null constant and copy it to the destination.
|
2011-02-02 05:35:06 +08:00
|
|
|
// TODO: there are other patterns besides zero that we can usefully memset,
|
|
|
|
// like -1, which happens to be the pattern used by member-pointers.
|
2010-08-23 05:01:12 +08:00
|
|
|
if (!CGM.getTypes().isZeroInitializable(Ty)) {
|
2011-02-02 05:35:06 +08:00
|
|
|
// For a VLA, emit a single element, then splat that over the VLA.
|
|
|
|
if (vla) Ty = getContext().getBaseElementType(vla);
|
2011-01-14 18:37:58 +08:00
|
|
|
|
2010-08-07 16:21:30 +08:00
|
|
|
llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty);
|
|
|
|
|
2012-12-04 08:29:55 +08:00
|
|
|
llvm::GlobalVariable *NullVariable =
|
2010-08-07 16:21:30 +08:00
|
|
|
new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(),
|
2012-12-04 08:29:55 +08:00
|
|
|
/*isConstant=*/true,
|
2010-08-07 16:21:30 +08:00
|
|
|
llvm::GlobalVariable::PrivateLinkage,
|
2011-07-23 18:55:15 +08:00
|
|
|
NullConstant, Twine());
|
2010-08-07 16:21:30 +08:00
|
|
|
llvm::Value *SrcPtr =
|
|
|
|
Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy());
|
|
|
|
|
2011-02-02 05:35:06 +08:00
|
|
|
if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal);
|
|
|
|
|
2010-08-07 16:21:30 +08:00
|
|
|
// Get and call the appropriate llvm.memcpy overload.
|
2011-04-23 01:51:05 +08:00
|
|
|
Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, Align.getQuantity(), false);
|
2009-04-22 01:59:23 +08:00
|
|
|
return;
|
2012-12-04 08:29:55 +08:00
|
|
|
}
|
|
|
|
|
2010-08-07 16:21:30 +08:00
|
|
|
// Otherwise, just memset the whole thing to zero. This is legal
|
|
|
|
// because in LLVM, all default initializers (other than the ones we just
|
|
|
|
// handled above) are guaranteed to have a bit pattern of all zeros.
|
2012-12-04 08:29:55 +08:00
|
|
|
Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal,
|
2011-04-23 01:51:05 +08:00
|
|
|
Align.getQuantity(), false);
|
2008-08-31 03:51:14 +08:00
|
|
|
}
|
|
|
|
|
2011-02-17 15:39:24 +08:00
|
|
|
llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) {
|
2009-10-29 07:59:40 +08:00
|
|
|
// Make sure that there is a block for the indirect goto.
|
|
|
|
if (IndirectBranch == 0)
|
|
|
|
GetIndirectGotoBlock();
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2010-07-24 05:56:41 +08:00
|
|
|
llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock();
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2009-10-29 07:59:40 +08:00
|
|
|
// Make sure the indirect branch includes all of the address-taken blocks.
|
|
|
|
IndirectBranch->addDestination(BB);
|
|
|
|
return llvm::BlockAddress::get(CurFn, BB);
|
2009-10-13 14:55:33 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-13 14:55:33 +08:00
|
|
|
llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() {
|
2009-10-29 07:59:40 +08:00
|
|
|
// If we already made the indirect branch for indirect goto, return its block.
|
|
|
|
if (IndirectBranch) return IndirectBranch->getParent();
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2009-10-29 07:59:40 +08:00
|
|
|
CGBuilderTy TmpBuilder(createBasicBlock("indirectgoto"));
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2009-10-13 14:55:33 +08:00
|
|
|
// Create the PHI node that indirect gotos will add entries to.
|
2011-03-30 19:28:58 +08:00
|
|
|
llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0,
|
|
|
|
"indirect.goto.dest");
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2009-10-29 07:59:40 +08:00
|
|
|
// Create the indirect branch instruction.
|
|
|
|
IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal);
|
|
|
|
return IndirectBranch->getParent();
|
2008-08-05 00:51:22 +08:00
|
|
|
}
|
2008-11-04 13:30:00 +08:00
|
|
|
|
2011-07-09 09:37:26 +08:00
|
|
|
/// Computes the length of an array in elements, as well as the base
|
|
|
|
/// element type and a properly-typed first element pointer.
|
|
|
|
llvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType,
|
|
|
|
QualType &baseType,
|
|
|
|
llvm::Value *&addr) {
|
|
|
|
const ArrayType *arrayType = origArrayType;
|
|
|
|
|
|
|
|
// If it's a VLA, we have to load the stored size. Note that
|
|
|
|
// this is the size of the VLA in bytes, not its size in elements.
|
|
|
|
llvm::Value *numVLAElements = 0;
|
|
|
|
if (isa<VariableArrayType>(arrayType)) {
|
|
|
|
numVLAElements = getVLASize(cast<VariableArrayType>(arrayType)).first;
|
|
|
|
|
|
|
|
// Walk into all VLAs. This doesn't require changes to addr,
|
|
|
|
// which has type T* where T is the first non-VLA element type.
|
|
|
|
do {
|
|
|
|
QualType elementType = arrayType->getElementType();
|
|
|
|
arrayType = getContext().getAsArrayType(elementType);
|
|
|
|
|
|
|
|
// If we only have VLA components, 'addr' requires no adjustment.
|
|
|
|
if (!arrayType) {
|
|
|
|
baseType = elementType;
|
|
|
|
return numVLAElements;
|
|
|
|
}
|
|
|
|
} while (isa<VariableArrayType>(arrayType));
|
|
|
|
|
|
|
|
// We get out here only if we find a constant array type
|
|
|
|
// inside the VLA.
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have some number of constant-length arrays, so addr should
|
|
|
|
// have LLVM type [M x [N x [...]]]*. Build a GEP that walks
|
|
|
|
// down to the first element of addr.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Value*, 8> gepIndices;
|
2011-07-09 09:37:26 +08:00
|
|
|
|
|
|
|
// GEP down to the array type.
|
|
|
|
llvm::ConstantInt *zero = Builder.getInt32(0);
|
|
|
|
gepIndices.push_back(zero);
|
|
|
|
|
|
|
|
uint64_t countFromCLAs = 1;
|
2012-04-22 13:51:36 +08:00
|
|
|
QualType eltType;
|
2011-07-09 09:37:26 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::ArrayType *llvmArrayType =
|
2012-04-22 13:51:36 +08:00
|
|
|
dyn_cast<llvm::ArrayType>(
|
2011-07-09 09:37:26 +08:00
|
|
|
cast<llvm::PointerType>(addr->getType())->getElementType());
|
2012-04-22 13:51:36 +08:00
|
|
|
while (llvmArrayType) {
|
2011-07-09 09:37:26 +08:00
|
|
|
assert(isa<ConstantArrayType>(arrayType));
|
|
|
|
assert(cast<ConstantArrayType>(arrayType)->getSize().getZExtValue()
|
|
|
|
== llvmArrayType->getNumElements());
|
|
|
|
|
|
|
|
gepIndices.push_back(zero);
|
|
|
|
countFromCLAs *= llvmArrayType->getNumElements();
|
2012-04-22 13:51:36 +08:00
|
|
|
eltType = arrayType->getElementType();
|
2011-07-09 09:37:26 +08:00
|
|
|
|
|
|
|
llvmArrayType =
|
|
|
|
dyn_cast<llvm::ArrayType>(llvmArrayType->getElementType());
|
|
|
|
arrayType = getContext().getAsArrayType(arrayType->getElementType());
|
2012-04-22 13:51:36 +08:00
|
|
|
assert((!llvmArrayType || arrayType) &&
|
|
|
|
"LLVM and Clang types are out-of-synch");
|
2011-07-09 09:37:26 +08:00
|
|
|
}
|
|
|
|
|
2012-04-22 13:51:36 +08:00
|
|
|
if (arrayType) {
|
|
|
|
// From this point onwards, the Clang array type has been emitted
|
|
|
|
// as some other type (probably a packed struct). Compute the array
|
|
|
|
// size, and just emit the 'begin' expression as a bitcast.
|
|
|
|
while (arrayType) {
|
|
|
|
countFromCLAs *=
|
|
|
|
cast<ConstantArrayType>(arrayType)->getSize().getZExtValue();
|
|
|
|
eltType = arrayType->getElementType();
|
|
|
|
arrayType = getContext().getAsArrayType(eltType);
|
|
|
|
}
|
|
|
|
|
2012-10-25 23:39:14 +08:00
|
|
|
unsigned AddressSpace = addr->getType()->getPointerAddressSpace();
|
2012-04-22 13:51:36 +08:00
|
|
|
llvm::Type *BaseType = ConvertType(eltType)->getPointerTo(AddressSpace);
|
|
|
|
addr = Builder.CreateBitCast(addr, BaseType, "array.begin");
|
|
|
|
} else {
|
|
|
|
// Create the actual GEP.
|
|
|
|
addr = Builder.CreateInBoundsGEP(addr, gepIndices, "array.begin");
|
|
|
|
}
|
2011-07-09 09:37:26 +08:00
|
|
|
|
2012-04-22 13:51:36 +08:00
|
|
|
baseType = eltType;
|
2011-07-09 09:37:26 +08:00
|
|
|
|
|
|
|
llvm::Value *numElements
|
|
|
|
= llvm::ConstantInt::get(SizeTy, countFromCLAs);
|
|
|
|
|
|
|
|
// If we had any VLA dimensions, factor them in.
|
|
|
|
if (numVLAElements)
|
|
|
|
numElements = Builder.CreateNUWMul(numVLAElements, numElements);
|
|
|
|
|
|
|
|
return numElements;
|
|
|
|
}
|
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
std::pair<llvm::Value*, QualType>
|
|
|
|
CodeGenFunction::getVLASize(QualType type) {
|
|
|
|
const VariableArrayType *vla = getContext().getAsVariableArrayType(type);
|
|
|
|
assert(vla && "type was not a variable array type!");
|
|
|
|
return getVLASize(vla);
|
2008-12-21 04:27:15 +08:00
|
|
|
}
|
2008-12-12 15:19:02 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
std::pair<llvm::Value*, QualType>
|
|
|
|
CodeGenFunction::getVLASize(const VariableArrayType *type) {
|
|
|
|
// The number of elements so far; always size_t.
|
|
|
|
llvm::Value *numElements = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
QualType elementType;
|
|
|
|
do {
|
|
|
|
elementType = type->getElementType();
|
|
|
|
llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()];
|
|
|
|
assert(vlaSize && "no size for VLA!");
|
|
|
|
assert(vlaSize->getType() == SizeTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
if (!numElements) {
|
|
|
|
numElements = vlaSize;
|
|
|
|
} else {
|
|
|
|
// It's undefined behavior if this wraps around, so mark it that way.
|
2012-10-10 09:11:12 +08:00
|
|
|
// FIXME: Teach -fcatch-undefined-behavior to trap this.
|
2011-06-25 05:55:10 +08:00
|
|
|
numElements = Builder.CreateNUWMul(numElements, vlaSize);
|
|
|
|
}
|
|
|
|
} while ((type = getContext().getAsVariableArrayType(elementType)));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
return std::pair<llvm::Value*,QualType>(numElements, elementType);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
void CodeGenFunction::EmitVariablyModifiedType(QualType type) {
|
|
|
|
assert(type->isVariablyModifiedType() &&
|
|
|
|
"Must pass variably modified type to EmitVLASizes!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
EnsureInsertPoint();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
// We're going to walk down into the type and look for VLA
|
|
|
|
// expressions.
|
|
|
|
do {
|
|
|
|
assert(type->isVariablyModifiedType());
|
|
|
|
|
|
|
|
const Type *ty = type.getTypePtr();
|
|
|
|
switch (ty->getTypeClass()) {
|
2012-01-07 18:52:36 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
#define TYPE(Class, Base)
|
|
|
|
#define ABSTRACT_TYPE(Class, Base)
|
2012-01-07 18:52:36 +08:00
|
|
|
#define NON_CANONICAL_TYPE(Class, Base)
|
2011-06-25 05:55:10 +08:00
|
|
|
#define DEPENDENT_TYPE(Class, Base) case Type::Class:
|
2012-01-07 18:52:36 +08:00
|
|
|
#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
|
2011-06-25 05:55:10 +08:00
|
|
|
#include "clang/AST/TypeNodes.def"
|
2012-01-07 18:52:36 +08:00
|
|
|
llvm_unreachable("unexpected dependent type!");
|
2011-06-25 05:55:10 +08:00
|
|
|
|
|
|
|
// These types are never variably-modified.
|
|
|
|
case Type::Builtin:
|
|
|
|
case Type::Complex:
|
|
|
|
case Type::Vector:
|
|
|
|
case Type::ExtVector:
|
|
|
|
case Type::Record:
|
|
|
|
case Type::Enum:
|
2012-01-11 16:19:46 +08:00
|
|
|
case Type::Elaborated:
|
|
|
|
case Type::TemplateSpecialization:
|
2011-06-25 05:55:10 +08:00
|
|
|
case Type::ObjCObject:
|
|
|
|
case Type::ObjCInterface:
|
|
|
|
case Type::ObjCObjectPointer:
|
|
|
|
llvm_unreachable("type class is never variably-modified!");
|
|
|
|
|
|
|
|
case Type::Pointer:
|
|
|
|
type = cast<PointerType>(ty)->getPointeeType();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::BlockPointer:
|
|
|
|
type = cast<BlockPointerType>(ty)->getPointeeType();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::LValueReference:
|
|
|
|
case Type::RValueReference:
|
|
|
|
type = cast<ReferenceType>(ty)->getPointeeType();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::MemberPointer:
|
|
|
|
type = cast<MemberPointerType>(ty)->getPointeeType();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::ConstantArray:
|
|
|
|
case Type::IncompleteArray:
|
|
|
|
// Losing element qualification here is fine.
|
|
|
|
type = cast<ArrayType>(ty)->getElementType();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::VariableArray: {
|
|
|
|
// Losing element qualification here is fine.
|
|
|
|
const VariableArrayType *vat = cast<VariableArrayType>(ty);
|
|
|
|
|
|
|
|
// Unknown size indication requires no size computation.
|
|
|
|
// Otherwise, evaluate and record it.
|
|
|
|
if (const Expr *size = vat->getSizeExpr()) {
|
|
|
|
// It's possible that we might have emitted this already,
|
|
|
|
// e.g. with a typedef and a pointer to it.
|
|
|
|
llvm::Value *&entry = VLASizeMap[size];
|
|
|
|
if (!entry) {
|
2012-10-10 09:11:12 +08:00
|
|
|
llvm::Value *Size = EmitScalarExpr(size);
|
|
|
|
|
|
|
|
// C11 6.7.6.2p5:
|
|
|
|
// If the size is an expression that is not an integer constant
|
|
|
|
// expression [...] each time it is evaluated it shall have a value
|
|
|
|
// greater than zero.
|
2012-11-06 06:21:05 +08:00
|
|
|
if (getLangOpts().SanitizeVLABound &&
|
|
|
|
size->getType()->isSignedIntegerType()) {
|
2012-10-10 09:11:12 +08:00
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(Size->getType());
|
|
|
|
llvm::Constant *StaticArgs[] = {
|
|
|
|
EmitCheckSourceLocation(size->getLocStart()),
|
|
|
|
EmitCheckTypeDescriptor(size->getType())
|
|
|
|
};
|
|
|
|
EmitCheck(Builder.CreateICmpSGT(Size, Zero),
|
2012-12-03 03:50:33 +08:00
|
|
|
"vla_bound_not_positive", StaticArgs, Size,
|
|
|
|
CRK_Recoverable);
|
2012-10-10 09:11:12 +08:00
|
|
|
}
|
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
// Always zexting here would be wrong if it weren't
|
|
|
|
// undefined behavior to have a negative bound.
|
2012-10-10 09:12:11 +08:00
|
|
|
entry = Builder.CreateIntCast(Size, SizeTy, /*signed*/ false);
|
2011-06-25 05:55:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
type = vat->getElementType();
|
|
|
|
break;
|
2008-12-21 05:51:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-07 18:52:36 +08:00
|
|
|
case Type::FunctionProto:
|
2011-06-25 05:55:10 +08:00
|
|
|
case Type::FunctionNoProto:
|
|
|
|
type = cast<FunctionType>(ty)->getResultType();
|
|
|
|
break;
|
2011-10-07 07:00:33 +08:00
|
|
|
|
2012-01-11 16:19:46 +08:00
|
|
|
case Type::Paren:
|
|
|
|
case Type::TypeOf:
|
|
|
|
case Type::UnaryTransform:
|
|
|
|
case Type::Attributed:
|
|
|
|
case Type::SubstTemplateTypeParm:
|
|
|
|
// Keep walking after single level desugaring.
|
|
|
|
type = type.getSingleStepDesugaredType(getContext());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::Typedef:
|
|
|
|
case Type::Decltype:
|
|
|
|
case Type::Auto:
|
|
|
|
// Stop walking: nothing to do.
|
|
|
|
return;
|
|
|
|
|
|
|
|
case Type::TypeOfExpr:
|
|
|
|
// Stop walking: emit typeof expression.
|
|
|
|
EmitIgnoredExpr(cast<TypeOfExprType>(ty)->getUnderlyingExpr());
|
|
|
|
return;
|
|
|
|
|
2011-10-07 07:00:33 +08:00
|
|
|
case Type::Atomic:
|
|
|
|
type = cast<AtomicType>(ty)->getValueType();
|
|
|
|
break;
|
2011-06-25 05:55:10 +08:00
|
|
|
}
|
|
|
|
} while (type->isVariablyModifiedType());
|
2008-12-12 15:19:02 +08:00
|
|
|
}
|
2009-01-21 01:46:04 +08:00
|
|
|
|
|
|
|
llvm::Value* CodeGenFunction::EmitVAListRef(const Expr* E) {
|
2010-10-30 06:47:07 +08:00
|
|
|
if (getContext().getBuiltinVaListType()->isArrayType())
|
2009-01-21 01:46:04 +08:00
|
|
|
return EmitScalarExpr(E);
|
|
|
|
return EmitLValue(E).getAddress();
|
|
|
|
}
|
2009-02-08 06:53:43 +08:00
|
|
|
|
2012-12-04 08:29:55 +08:00
|
|
|
void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
|
2010-10-09 09:34:31 +08:00
|
|
|
llvm::Constant *Init) {
|
2010-08-11 01:53:33 +08:00
|
|
|
assert (Init && "Invalid DeclRefExpr initializer!");
|
|
|
|
if (CGDebugInfo *Dbg = getDebugInfo())
|
2012-10-24 04:05:01 +08:00
|
|
|
if (CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
|
2012-05-04 15:39:27 +08:00
|
|
|
Dbg->EmitGlobalVariable(E->getDecl(), Init);
|
2010-08-10 15:24:25 +08:00
|
|
|
}
|
2011-02-17 18:25:35 +08:00
|
|
|
|
|
|
|
CodeGenFunction::PeepholeProtection
|
|
|
|
CodeGenFunction::protectFromPeepholes(RValue rvalue) {
|
|
|
|
// At the moment, the only aggressive peephole we do in IR gen
|
|
|
|
// is trunc(zext) folding, but if we add more, we can easily
|
|
|
|
// extend this protection.
|
|
|
|
|
|
|
|
if (!rvalue.isScalar()) return PeepholeProtection();
|
|
|
|
llvm::Value *value = rvalue.getScalarVal();
|
|
|
|
if (!isa<llvm::ZExtInst>(value)) return PeepholeProtection();
|
|
|
|
|
|
|
|
// Just make an extra bitcast.
|
|
|
|
assert(HaveInsertPoint());
|
|
|
|
llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "",
|
|
|
|
Builder.GetInsertBlock());
|
|
|
|
|
|
|
|
PeepholeProtection protection;
|
|
|
|
protection.Inst = inst;
|
|
|
|
return protection;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) {
|
|
|
|
if (!protection.Inst) return;
|
|
|
|
|
|
|
|
// In theory, we could try to duplicate the peepholes now, but whatever.
|
|
|
|
protection.Inst->eraseFromParent();
|
|
|
|
}
|
2011-09-10 06:41:49 +08:00
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Value *AnnotationFn,
|
|
|
|
llvm::Value *AnnotatedVal,
|
|
|
|
llvm::StringRef AnnotationStr,
|
|
|
|
SourceLocation Location) {
|
|
|
|
llvm::Value *Args[4] = {
|
|
|
|
AnnotatedVal,
|
|
|
|
Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy),
|
|
|
|
Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy),
|
|
|
|
CGM.EmitAnnotationLineNo(Location)
|
|
|
|
};
|
|
|
|
return Builder.CreateCall(AnnotationFn, Args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) {
|
|
|
|
assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
|
|
|
|
// FIXME We create a new bitcast for every annotation because that's what
|
|
|
|
// llvm-gcc was doing.
|
|
|
|
for (specific_attr_iterator<AnnotateAttr>
|
|
|
|
ai = D->specific_attr_begin<AnnotateAttr>(),
|
|
|
|
ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai)
|
|
|
|
EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation),
|
|
|
|
Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()),
|
|
|
|
(*ai)->getAnnotation(), D->getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D,
|
|
|
|
llvm::Value *V) {
|
|
|
|
assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
|
|
|
|
llvm::Type *VTy = V->getType();
|
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation,
|
|
|
|
CGM.Int8PtrTy);
|
|
|
|
|
|
|
|
for (specific_attr_iterator<AnnotateAttr>
|
|
|
|
ai = D->specific_attr_begin<AnnotateAttr>(),
|
|
|
|
ae = D->specific_attr_end<AnnotateAttr>(); ai != ae; ++ai) {
|
|
|
|
// FIXME Always emit the cast inst so we can differentiate between
|
|
|
|
// annotation on the first field of a struct and annotation on the struct
|
|
|
|
// itself.
|
|
|
|
if (VTy != CGM.Int8PtrTy)
|
|
|
|
V = Builder.Insert(new llvm::BitCastInst(V, CGM.Int8PtrTy));
|
|
|
|
V = EmitAnnotationCall(F, V, (*ai)->getAnnotation(), D->getLocation());
|
|
|
|
V = Builder.CreateBitCast(V, VTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|