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"
|
2014-05-06 18:08:46 +08:00
|
|
|
#include "CGOpenMPRuntime.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CodeGenModule.h"
|
2014-01-07 06:27:43 +08:00
|
|
|
#include "CodeGenPGO.h"
|
2013-10-21 05:29:19 +08:00
|
|
|
#include "TargetInfo.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"
|
2013-10-31 05:53:58 +08:00
|
|
|
#include "clang/CodeGen/CGFunctionInfo.h"
|
2010-06-22 08:03:40 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/MDBuilder.h"
|
|
|
|
#include "llvm/IR/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)
|
2013-08-27 04:33:21 +08:00
|
|
|
: CodeGenTypeCache(cgm), CGM(cgm), Target(cgm.getTarget()),
|
2014-05-22 16:54:05 +08:00
|
|
|
Builder(cgm.getModule().getContext(), llvm::ConstantFolder(),
|
|
|
|
CGBuilderInserterTy(this)), CapturedStmtInfo(nullptr),
|
2013-08-27 04:33:21 +08:00
|
|
|
SanitizePerformTypeCheck(CGM.getSanOpts().Null |
|
|
|
|
CGM.getSanOpts().Alignment |
|
|
|
|
CGM.getSanOpts().ObjectSize |
|
|
|
|
CGM.getSanOpts().Vptr),
|
2014-05-21 13:09:00 +08:00
|
|
|
SanOpts(&CGM.getSanOpts()), AutoreleaseResult(false), BlockInfo(nullptr),
|
|
|
|
BlockPointer(nullptr), LambdaThisCaptureField(nullptr),
|
|
|
|
NormalCleanupDest(nullptr), NextCleanupDestIndex(1),
|
|
|
|
FirstBlockInfo(nullptr), EHResumeBlock(nullptr), ExceptionSlot(nullptr),
|
|
|
|
EHSelectorSlot(nullptr), DebugInfo(CGM.getModuleDebugInfo()),
|
|
|
|
DisableDebugInfo(false), DidCallStackSave(false), IndirectBranch(nullptr),
|
|
|
|
PGO(cgm), SwitchInsn(nullptr), SwitchWeights(nullptr),
|
|
|
|
CaseRangeBlock(nullptr), UnreachableBlock(nullptr), NumReturnExprs(0),
|
|
|
|
NumSimpleReturnExprs(0), CXXABIThisDecl(nullptr),
|
|
|
|
CXXABIThisValue(nullptr), CXXThisValue(nullptr),
|
|
|
|
CXXDefaultInitExprThis(nullptr), CXXStructorImplicitParamDecl(nullptr),
|
|
|
|
CXXStructorImplicitParamValue(nullptr), OutermostConditional(nullptr),
|
|
|
|
CurLexicalScope(nullptr), TerminateLandingPad(nullptr),
|
|
|
|
TerminateHandler(nullptr), TrapBB(nullptr) {
|
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)
|
2012-12-10 05:58:24 +08:00
|
|
|
FMF.setUnsafeAlgebra();
|
2012-12-04 08:36:06 +08:00
|
|
|
if (CGM.getLangOpts().FiniteMathOnly) {
|
2012-12-10 05:58:24 +08:00
|
|
|
FMF.setNoNaNs();
|
|
|
|
FMF.setNoInfs();
|
2012-12-04 08:36:06 +08:00
|
|
|
}
|
|
|
|
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() {
|
2013-06-13 04:42:33 +08:00
|
|
|
assert(LifetimeExtendedCleanupStack.empty() && "failed to emit a cleanup");
|
|
|
|
|
2011-11-10 16:15:53 +08:00
|
|
|
// 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);
|
2014-05-06 18:08:46 +08:00
|
|
|
|
|
|
|
if (getLangOpts().OpenMP) {
|
|
|
|
CGM.getOpenMPRuntime().FunctionFinished(*this);
|
|
|
|
}
|
2011-11-10 16:15:53 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-03-08 05:37:08 +08:00
|
|
|
TypeEvaluationKind CodeGenFunction::getEvaluationKind(QualType type) {
|
|
|
|
type = type.getCanonicalType();
|
|
|
|
while (true) {
|
|
|
|
switch (type->getTypeClass()) {
|
2011-05-15 10:34:36 +08:00
|
|
|
#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"
|
2013-03-08 05:37:08 +08:00
|
|
|
llvm_unreachable("non-canonical or dependent type in IR-generation");
|
|
|
|
|
2013-04-30 21:56:41 +08:00
|
|
|
case Type::Auto:
|
|
|
|
llvm_unreachable("undeduced auto type in IR-generation");
|
|
|
|
|
2013-03-08 05:37:08 +08:00
|
|
|
// Various scalar types.
|
|
|
|
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 TEK_Scalar;
|
|
|
|
|
|
|
|
// Complexes.
|
|
|
|
case Type::Complex:
|
|
|
|
return TEK_Complex;
|
|
|
|
|
|
|
|
// Arrays, records, and Objective-C objects.
|
|
|
|
case Type::ConstantArray:
|
|
|
|
case Type::IncompleteArray:
|
|
|
|
case Type::VariableArray:
|
|
|
|
case Type::Record:
|
|
|
|
case Type::ObjCObject:
|
|
|
|
case Type::ObjCInterface:
|
|
|
|
return TEK_Aggregate;
|
|
|
|
|
|
|
|
// We operate on atomic values according to their underlying type.
|
|
|
|
case Type::Atomic:
|
|
|
|
type = cast<AtomicType>(type)->getValueType();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
llvm_unreachable("unknown type kind!");
|
2011-05-15 10:34:36 +08:00
|
|
|
}
|
2008-06-18 02:05:57 +08:00
|
|
|
}
|
2008-03-31 07:03:07 +08:00
|
|
|
|
2013-02-02 03:09:49 +08:00
|
|
|
void CodeGenFunction::EmitReturnBlock() {
|
2009-01-27 07:27:52 +08:00
|
|
|
// 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());
|
2013-02-02 03:09:49 +08:00
|
|
|
return;
|
2009-01-27 07:27:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 =
|
2014-03-09 11:16:50 +08:00
|
|
|
dyn_cast<llvm::BranchInst>(*ReturnBlock.getBlock()->user_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()) {
|
2013-02-24 03:20:56 +08:00
|
|
|
// Reset insertion point, including debug location, and delete the
|
|
|
|
// branch. This is really subtle and only works because the next change
|
|
|
|
// in location will hit the caching in CGDebugInfo::EmitLocation and not
|
|
|
|
// override this.
|
2011-09-10 05:53:04 +08:00
|
|
|
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();
|
2013-02-02 03:09:49 +08:00
|
|
|
return;
|
2009-01-27 07:27:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-05-04 04:11:48 +08:00
|
|
|
bool OnlySimpleReturnStmts = NumSimpleReturnExprs > 0
|
2013-07-25 08:23:42 +08:00
|
|
|
&& NumSimpleReturnExprs == NumReturnExprs
|
|
|
|
&& ReturnBlock.getBlock()->use_empty();
|
|
|
|
// Usually the return expression is evaluated before the cleanup
|
|
|
|
// code. If the function contains only a simple return statement,
|
|
|
|
// such as a constant, the location before the cleanup code becomes
|
|
|
|
// the last useful breakpoint in the function, because the simple
|
|
|
|
// return expression will be evaluated after the cleanup code. To be
|
|
|
|
// safe, set the debug location for cleanup code to the location of
|
|
|
|
// the return statement. Otherwise the cleanup code should be at the
|
|
|
|
// end of the function's lexical scope.
|
|
|
|
//
|
|
|
|
// If there are multiple branches to the return block, the branch
|
|
|
|
// instructions will get the location of the return statements and
|
|
|
|
// all will be fine.
|
2013-05-03 01:30:20 +08:00
|
|
|
if (CGDebugInfo *DI = getDebugInfo()) {
|
2013-05-04 04:11:48 +08:00
|
|
|
if (OnlySimpleReturnStmts)
|
2014-01-08 06:05:52 +08:00
|
|
|
DI->EmitLocation(Builder, LastStopPoint);
|
2013-05-03 01:30:20 +08:00
|
|
|
else
|
2014-01-08 06:05:52 +08:00
|
|
|
DI->EmitLocation(Builder, EndLoc);
|
2013-05-03 01:30:20 +08:00
|
|
|
}
|
2013-02-02 03:09:49 +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.
|
2013-05-03 01:30:20 +08:00
|
|
|
bool EmitRetDbgLoc = true;
|
|
|
|
if (EHStack.stable_begin() != PrologueCleanupDepth) {
|
2013-05-16 08:41:26 +08:00
|
|
|
PopCleanupBlocks(PrologueCleanupDepth);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2013-05-03 01:30:20 +08:00
|
|
|
// Make sure the line table doesn't jump back into the body for
|
|
|
|
// the ret after it's been at EndLoc.
|
|
|
|
EmitRetDbgLoc = false;
|
|
|
|
|
|
|
|
if (CGDebugInfo *DI = getDebugInfo())
|
2013-05-04 04:11:48 +08:00
|
|
|
if (OnlySimpleReturnStmts)
|
2014-01-08 06:05:52 +08:00
|
|
|
DI->EmitLocation(Builder, EndLoc);
|
2013-05-03 01:30:20 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Emit function epilog (to return).
|
2013-02-02 03:09:49 +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()) {
|
2010-07-23 06:29:16 +08:00
|
|
|
DI->EmitFunctionEnd(Builder);
|
2008-11-12 04:59:54 +08:00
|
|
|
}
|
|
|
|
|
2013-10-02 10:29:49 +08:00
|
|
|
EmitFunctionEpilog(*CurFnInfo, EmitRetDbgLoc, EndLoc);
|
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;
|
2014-05-21 13:09:00 +08:00
|
|
|
AllocaInsertPt = nullptr;
|
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();
|
2014-02-01 08:04:45 +08:00
|
|
|
|
|
|
|
for (SmallVectorImpl<std::pair<llvm::Instruction *, llvm::Value *> >::iterator
|
|
|
|
I = DeferredReplacements.begin(),
|
|
|
|
E = DeferredReplacements.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
I->first->replaceAllUsesWith(I->second);
|
|
|
|
I->first->eraseFromParent();
|
|
|
|
}
|
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");
|
|
|
|
|
2013-03-01 03:01:20 +08:00
|
|
|
llvm::Value *args[] = {
|
|
|
|
llvm::ConstantExpr::getBitCast(CurFn, PointerTy),
|
|
|
|
CallSite
|
|
|
|
};
|
|
|
|
|
|
|
|
EmitNounwindRuntimeCall(F, args);
|
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
|
|
|
|
2013-04-17 06:48:15 +08:00
|
|
|
llvm::Constant *MCountFn =
|
|
|
|
CGM.CreateRuntimeFunction(FTy, getTarget().getMCountName());
|
2013-03-01 03:01:20 +08:00
|
|
|
EmitNounwindRuntimeCall(MCountFn);
|
2011-02-11 00:52:03 +08:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
static void GenOpenCLArgMetadata(const FunctionDecl *FD, llvm::Function *Fn,
|
|
|
|
CodeGenModule &CGM,llvm::LLVMContext &Context,
|
2013-03-24 21:58:12 +08:00
|
|
|
SmallVector <llvm::Value*, 5> &kernelMDArgs,
|
|
|
|
CGBuilderTy& Builder, ASTContext &ASTCtx) {
|
|
|
|
// Create MDNodes that represent the kernel arg metadata.
|
2012-07-12 07:02:10 +08:00
|
|
|
// 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
|
|
|
|
2014-04-04 21:43:57 +08:00
|
|
|
const PrintingPolicy &Policy = ASTCtx.getPrintingPolicy();
|
|
|
|
|
2013-03-24 21:58:12 +08:00
|
|
|
// MDNode for the kernel argument address space qualifiers.
|
|
|
|
SmallVector<llvm::Value*, 8> addressQuals;
|
|
|
|
addressQuals.push_back(llvm::MDString::get(Context, "kernel_arg_addr_space"));
|
|
|
|
|
|
|
|
// MDNode for the kernel argument access qualifiers (images only).
|
|
|
|
SmallVector<llvm::Value*, 8> accessQuals;
|
|
|
|
accessQuals.push_back(llvm::MDString::get(Context, "kernel_arg_access_qual"));
|
|
|
|
|
|
|
|
// MDNode for the kernel argument type names.
|
|
|
|
SmallVector<llvm::Value*, 8> argTypeNames;
|
|
|
|
argTypeNames.push_back(llvm::MDString::get(Context, "kernel_arg_type"));
|
|
|
|
|
|
|
|
// MDNode for the kernel argument type qualifiers.
|
|
|
|
SmallVector<llvm::Value*, 8> argTypeQuals;
|
|
|
|
argTypeQuals.push_back(llvm::MDString::get(Context, "kernel_arg_type_qual"));
|
|
|
|
|
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);
|
2013-03-24 21:58:12 +08:00
|
|
|
QualType ty = parm->getType();
|
|
|
|
std::string typeQuals;
|
|
|
|
|
|
|
|
if (ty->isPointerType()) {
|
|
|
|
QualType pointeeTy = ty->getPointeeType();
|
|
|
|
|
|
|
|
// Get address qualifier.
|
|
|
|
addressQuals.push_back(Builder.getInt32(ASTCtx.getTargetAddressSpace(
|
|
|
|
pointeeTy.getAddressSpace())));
|
|
|
|
|
|
|
|
// Get argument type name.
|
2014-04-04 21:43:57 +08:00
|
|
|
std::string typeName =
|
|
|
|
pointeeTy.getUnqualifiedType().getAsString(Policy) + "*";
|
2013-03-24 21:58:12 +08:00
|
|
|
|
|
|
|
// Turn "unsigned type" to "utype"
|
|
|
|
std::string::size_type pos = typeName.find("unsigned");
|
2013-03-25 00:04:55 +08:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
typeName.erase(pos+1, 8);
|
2013-03-24 21:58:12 +08:00
|
|
|
|
|
|
|
argTypeNames.push_back(llvm::MDString::get(Context, typeName));
|
|
|
|
|
|
|
|
// Get argument type qualifiers:
|
|
|
|
if (ty.isRestrictQualified())
|
|
|
|
typeQuals = "restrict";
|
|
|
|
if (pointeeTy.isConstQualified() ||
|
|
|
|
(pointeeTy.getAddressSpace() == LangAS::opencl_constant))
|
2013-03-25 00:04:55 +08:00
|
|
|
typeQuals += typeQuals.empty() ? "const" : " const";
|
2013-03-24 21:58:12 +08:00
|
|
|
if (pointeeTy.isVolatileQualified())
|
2013-03-25 00:04:55 +08:00
|
|
|
typeQuals += typeQuals.empty() ? "volatile" : " volatile";
|
2013-03-24 21:58:12 +08:00
|
|
|
} else {
|
2014-01-09 21:37:30 +08:00
|
|
|
uint32_t AddrSpc = 0;
|
|
|
|
if (ty->isImageType())
|
|
|
|
AddrSpc =
|
|
|
|
CGM.getContext().getTargetAddressSpace(LangAS::opencl_global);
|
2014-02-18 03:20:59 +08:00
|
|
|
|
2014-01-09 21:37:30 +08:00
|
|
|
addressQuals.push_back(Builder.getInt32(AddrSpc));
|
2013-03-24 21:58:12 +08:00
|
|
|
|
|
|
|
// Get argument type name.
|
2014-04-04 21:43:57 +08:00
|
|
|
std::string typeName = ty.getUnqualifiedType().getAsString(Policy);
|
2013-03-24 21:58:12 +08:00
|
|
|
|
|
|
|
// Turn "unsigned type" to "utype"
|
|
|
|
std::string::size_type pos = typeName.find("unsigned");
|
2013-03-25 00:04:55 +08:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
typeName.erase(pos+1, 8);
|
2013-03-24 21:58:12 +08:00
|
|
|
|
|
|
|
argTypeNames.push_back(llvm::MDString::get(Context, typeName));
|
|
|
|
|
|
|
|
// Get argument type qualifiers:
|
|
|
|
if (ty.isConstQualified())
|
|
|
|
typeQuals = "const";
|
|
|
|
if (ty.isVolatileQualified())
|
2013-03-25 00:04:55 +08:00
|
|
|
typeQuals += typeQuals.empty() ? "volatile" : " volatile";
|
2013-03-24 21:58:12 +08:00
|
|
|
}
|
2013-11-22 18:20:40 +08:00
|
|
|
|
2013-03-24 21:58:12 +08:00
|
|
|
argTypeQuals.push_back(llvm::MDString::get(Context, typeQuals));
|
|
|
|
|
|
|
|
// Get image access qualifier:
|
|
|
|
if (ty->isImageType()) {
|
2013-12-19 11:09:10 +08:00
|
|
|
const OpenCLImageAccessAttr *A = parm->getAttr<OpenCLImageAccessAttr>();
|
2014-01-15 01:41:53 +08:00
|
|
|
if (A && A->isWriteOnly())
|
2013-03-24 21:58:12 +08:00
|
|
|
accessQuals.push_back(llvm::MDString::get(Context, "write_only"));
|
|
|
|
else
|
|
|
|
accessQuals.push_back(llvm::MDString::get(Context, "read_only"));
|
2014-01-15 01:41:53 +08:00
|
|
|
// FIXME: what about read_write?
|
2013-03-24 21:58:12 +08:00
|
|
|
} else
|
|
|
|
accessQuals.push_back(llvm::MDString::get(Context, "none"));
|
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()));
|
|
|
|
}
|
2013-03-24 21:58:12 +08:00
|
|
|
|
|
|
|
kernelMDArgs.push_back(llvm::MDNode::get(Context, addressQuals));
|
|
|
|
kernelMDArgs.push_back(llvm::MDNode::get(Context, accessQuals));
|
|
|
|
kernelMDArgs.push_back(llvm::MDNode::get(Context, argTypeNames));
|
|
|
|
kernelMDArgs.push_back(llvm::MDNode::get(Context, argTypeQuals));
|
2012-07-12 07:02:10 +08:00
|
|
|
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();
|
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector <llvm::Value*, 5> kernelMDArgs;
|
2012-07-10 06:06:01 +08:00
|
|
|
kernelMDArgs.push_back(Fn);
|
|
|
|
|
2012-07-12 07:02:10 +08:00
|
|
|
if (CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
|
2013-03-24 21:58:12 +08:00
|
|
|
GenOpenCLArgMetadata(FD, Fn, CGM, Context, kernelMDArgs,
|
|
|
|
Builder, getContext());
|
2012-12-04 08:29:55 +08:00
|
|
|
|
2013-12-19 11:09:10 +08:00
|
|
|
if (const VecTypeHintAttr *A = FD->getAttr<VecTypeHintAttr>()) {
|
|
|
|
QualType hintQTy = A->getTypeHint();
|
2013-03-08 17:42:32 +08:00
|
|
|
const ExtVectorType *hintEltQTy = hintQTy->getAs<ExtVectorType>();
|
|
|
|
bool isSignedInteger =
|
|
|
|
hintQTy->isSignedIntegerType() ||
|
|
|
|
(hintEltQTy && hintEltQTy->getElementType()->isSignedIntegerType());
|
|
|
|
llvm::Value *attrMDArgs[] = {
|
|
|
|
llvm::MDString::get(Context, "vec_type_hint"),
|
2013-12-19 11:09:10 +08:00
|
|
|
llvm::UndefValue::get(CGM.getTypes().ConvertType(A->getTypeHint())),
|
2013-03-08 17:42:32 +08:00
|
|
|
llvm::ConstantInt::get(
|
|
|
|
llvm::IntegerType::get(Context, 32),
|
|
|
|
llvm::APInt(32, (uint64_t)(isSignedInteger ? 1 : 0)))
|
|
|
|
};
|
|
|
|
kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
|
|
|
|
}
|
|
|
|
|
2013-12-19 11:09:10 +08:00
|
|
|
if (const WorkGroupSizeHintAttr *A = FD->getAttr<WorkGroupSizeHintAttr>()) {
|
2012-11-16 00:40:35 +08:00
|
|
|
llvm::Value *attrMDArgs[] = {
|
|
|
|
llvm::MDString::get(Context, "work_group_size_hint"),
|
2013-12-19 11:09:10 +08:00
|
|
|
Builder.getInt32(A->getXDim()),
|
|
|
|
Builder.getInt32(A->getYDim()),
|
|
|
|
Builder.getInt32(A->getZDim())
|
2012-11-16 00:40:35 +08:00
|
|
|
};
|
2012-07-10 06:06:01 +08:00
|
|
|
kernelMDArgs.push_back(llvm::MDNode::get(Context, attrMDArgs));
|
|
|
|
}
|
|
|
|
|
2013-12-19 11:09:10 +08:00
|
|
|
if (const ReqdWorkGroupSizeAttr *A = FD->getAttr<ReqdWorkGroupSizeAttr>()) {
|
2012-11-16 00:40:35 +08:00
|
|
|
llvm::Value *attrMDArgs[] = {
|
|
|
|
llvm::MDString::get(Context, "reqd_work_group_size"),
|
2013-12-19 11:09:10 +08:00
|
|
|
Builder.getInt32(A->getXDim()),
|
|
|
|
Builder.getInt32(A->getYDim()),
|
|
|
|
Builder.getInt32(A->getZDim())
|
2012-11-16 00:40:35 +08:00
|
|
|
};
|
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);
|
|
|
|
}
|
|
|
|
|
2014-04-29 09:07:59 +08:00
|
|
|
/// Determine whether the function F ends with a return stmt.
|
|
|
|
static bool endsWithReturn(const Decl* F) {
|
|
|
|
const Stmt *Body = nullptr;
|
|
|
|
if (auto *FD = dyn_cast_or_null<FunctionDecl>(F))
|
|
|
|
Body = FD->getBody();
|
|
|
|
else if (auto *OMD = dyn_cast_or_null<ObjCMethodDecl>(F))
|
|
|
|
Body = OMD->getBody();
|
|
|
|
|
|
|
|
if (auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
|
|
|
|
auto LastStmt = CS->body_rbegin();
|
|
|
|
if (LastStmt != CS->body_rend())
|
|
|
|
return isa<ReturnStmt>(*LastStmt);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-03 15:33:41 +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,
|
2014-04-11 07:21:53 +08:00
|
|
|
SourceLocation Loc,
|
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;
|
2013-05-03 15:33:41 +08:00
|
|
|
CurCodeDecl = D;
|
2014-05-21 13:09:00 +08:00
|
|
|
CurFuncDecl = (D ? D->getNonClosureContext() : nullptr);
|
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
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (CGM.getSanitizerBlacklist().isIn(*Fn)) {
|
|
|
|
SanOpts = &SanitizerOptions::Disabled;
|
|
|
|
SanitizePerformTypeCheck = false;
|
|
|
|
}
|
|
|
|
|
2010-02-09 08:10:00 +08:00
|
|
|
// Pass inline keyword to optimizer if it appears explicitly on any
|
2014-01-16 03:07:16 +08:00
|
|
|
// declaration. Also, in the case of -fno-inline attach NoInline
|
2014-02-25 18:51:14 +08:00
|
|
|
// attribute to all function that are not marked AlwaysInline.
|
2014-01-16 03:07:16 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
|
|
|
|
if (!CGM.getCodeGenOpts().NoInline) {
|
2014-03-07 07:45:36 +08:00
|
|
|
for (auto RI : FD->redecls())
|
2012-03-15 07:32:11 +08:00
|
|
|
if (RI->isInlineSpecified()) {
|
2012-12-21 03:27:06 +08:00
|
|
|
Fn->addFnAttr(llvm::Attribute::InlineHint);
|
2012-03-15 07:32:11 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-02-25 17:53:29 +08:00
|
|
|
} else if (!FD->hasAttr<AlwaysInlineAttr>())
|
2014-01-16 03:07:16 +08:00
|
|
|
Fn->addFnAttr(llvm::Attribute::NoInline);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2013-10-21 05:29:19 +08:00
|
|
|
// If we are checking function types, emit a function type signature as
|
|
|
|
// prefix data.
|
|
|
|
if (getLangOpts().CPlusPlus && SanOpts->Function) {
|
|
|
|
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
|
|
|
|
if (llvm::Constant *PrefixSig =
|
|
|
|
CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
|
|
|
|
llvm::Constant *FTRTTIConst =
|
|
|
|
CGM.GetAddrOfRTTIDescriptor(FD->getType(), /*ForEH=*/true);
|
|
|
|
llvm::Constant *PrefixStructElems[] = { PrefixSig, FTRTTIConst };
|
|
|
|
llvm::Constant *PrefixStructConst =
|
|
|
|
llvm::ConstantStruct::getAnon(PrefixStructElems, /*Packed=*/true);
|
|
|
|
Fn->setPrefixData(PrefixStructConst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()) {
|
2013-03-09 05:51:21 +08:00
|
|
|
SmallVector<QualType, 16> ArgTypes;
|
2011-10-22 07:30:10 +08:00
|
|
|
for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
|
|
|
|
i != e; ++i) {
|
2013-03-09 05:51:21 +08:00
|
|
|
ArgTypes.push_back((*i)->getType());
|
2011-10-22 07:30:10 +08:00
|
|
|
}
|
|
|
|
|
2010-12-14 16:05:40 +08:00
|
|
|
QualType FnType =
|
2013-03-09 05:51:21 +08:00
|
|
|
getContext().getFunctionType(RetTy, ArgTypes,
|
2010-12-14 16:05:40 +08:00
|
|
|
FunctionProtoType::ExtProtoInfo());
|
2014-04-11 07:21:53 +08:00
|
|
|
DI->EmitFunctionStart(GD, Loc, StartLoc, 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.
|
2014-05-21 13:09:00 +08:00
|
|
|
ReturnValue = nullptr;
|
2014-04-29 09:07:59 +08:00
|
|
|
|
|
|
|
// Count the implicit return.
|
|
|
|
if (!endsWithReturn(D))
|
|
|
|
++NumReturnExprs;
|
2009-12-04 10:43:40 +08:00
|
|
|
} else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
|
2013-03-08 05:37:08 +08:00
|
|
|
!hasScalarEvaluationKind(CurFnInfo->getReturnType())) {
|
2009-12-04 10:43:40 +08:00
|
|
|
// 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++.
|
2014-05-10 06:46:15 +08:00
|
|
|
auto AI = CurFn->arg_begin();
|
|
|
|
if (CurFnInfo->getReturnInfo().isSRetAfterThis())
|
|
|
|
++AI;
|
|
|
|
ReturnValue = AI;
|
2014-02-01 08:04:45 +08:00
|
|
|
} else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::InAlloca &&
|
|
|
|
!hasScalarEvaluationKind(CurFnInfo->getReturnType())) {
|
|
|
|
// Load the sret pointer from the argument struct and return into that.
|
|
|
|
unsigned Idx = CurFnInfo->getReturnInfo().getInAllocaFieldIndex();
|
|
|
|
llvm::Function::arg_iterator EI = CurFn->arg_end();
|
|
|
|
--EI;
|
|
|
|
llvm::Value *Addr = Builder.CreateStructGEP(EI, Idx);
|
|
|
|
ReturnValue = Builder.CreateLoad(Addr, "agg.result");
|
2009-12-04 10:43:40 +08:00
|
|
|
} 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.
|
2013-05-03 15:33:41 +08:00
|
|
|
LValue ThisLValue = EmitLValueForLambdaField(LambdaThisCaptureField);
|
2013-10-02 10:29:49 +08:00
|
|
|
CXXThisValue = EmitLoadOfLValue(ThisLValue,
|
|
|
|
SourceLocation()).getScalarVal();
|
2012-02-11 10:57:39 +08:00
|
|
|
}
|
|
|
|
} 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
|
|
|
|
2013-11-05 17:12:18 +08:00
|
|
|
void CodeGenFunction::EmitFunctionBody(FunctionArgList &Args,
|
|
|
|
const Stmt *Body) {
|
2014-01-07 06:27:43 +08:00
|
|
|
RegionCounter Cnt = getPGORegionCounter(Body);
|
|
|
|
Cnt.beginRegion(Builder);
|
2013-11-05 17:12:18 +08:00
|
|
|
if (const CompoundStmt *S = dyn_cast<CompoundStmt>(Body))
|
2013-01-27 06:16:26 +08:00
|
|
|
EmitCompoundStmtWithoutScope(*S);
|
|
|
|
else
|
2013-11-05 17:12:18 +08:00
|
|
|
EmitStmt(Body);
|
2010-02-18 11:17:58 +08:00
|
|
|
}
|
|
|
|
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
/// When instrumenting to collect profile data, the counts for some blocks
|
|
|
|
/// such as switch cases need to not include the fall-through counts, so
|
|
|
|
/// emit a branch around the instrumentation code. When not instrumenting,
|
|
|
|
/// this just calls EmitBlock().
|
|
|
|
void CodeGenFunction::EmitBlockWithFallThrough(llvm::BasicBlock *BB,
|
|
|
|
RegionCounter &Cnt) {
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::BasicBlock *SkipCountBB = nullptr;
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
if (HaveInsertPoint() && CGM.getCodeGenOpts().ProfileInstrGenerate) {
|
|
|
|
// When instrumenting for profiling, the fallthrough to certain
|
|
|
|
// statements needs to skip over the instrumentation code so that we
|
|
|
|
// get an accurate count.
|
|
|
|
SkipCountBB = createBasicBlock("skipcount");
|
|
|
|
EmitBranch(SkipCountBB);
|
|
|
|
}
|
|
|
|
EmitBlock(BB);
|
|
|
|
Cnt.beginRegion(Builder, /*AddIncomingFallThrough=*/true);
|
|
|
|
if (SkipCountBB)
|
|
|
|
EmitBlock(SkipCountBB);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-11-05 17:12:18 +08:00
|
|
|
static void EmitSizedDeallocationFunction(CodeGenFunction &CGF,
|
|
|
|
const FunctionDecl *UnsizedDealloc) {
|
|
|
|
// This is a weak discardable definition of the sized deallocation function.
|
|
|
|
CGF.CurFn->setLinkage(llvm::Function::LinkOnceAnyLinkage);
|
|
|
|
|
|
|
|
// Call the unsized deallocation function and forward the first argument
|
|
|
|
// unchanged.
|
|
|
|
llvm::Constant *Unsized = CGF.CGM.GetAddrOfFunction(UnsizedDealloc);
|
|
|
|
CGF.Builder.CreateCall(Unsized, &*CGF.CurFn->arg_begin());
|
|
|
|
}
|
|
|
|
|
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.
|
2013-08-27 04:33:21 +08:00
|
|
|
if (FD->hasAttr<NoDebugAttr>())
|
2014-05-21 13:09:00 +08:00
|
|
|
DebugInfo = nullptr; // disable debug info indefinitely for this function
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-10 07:14:03 +08:00
|
|
|
FunctionArgList Args;
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType ResTy = FD->getReturnType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-05 07:26:17 +08:00
|
|
|
CurGD = GD;
|
2013-12-18 03:46:40 +08:00
|
|
|
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
|
|
|
|
if (MD && MD->isInstance()) {
|
2013-07-01 04:40:16 +08:00
|
|
|
if (CGM.getCXXABI().HasThisReturn(GD))
|
|
|
|
ResTy = MD->getThisType(getContext());
|
2013-12-18 03:46:40 +08:00
|
|
|
CGM.getCXXABI().buildThisParam(*this, Args);
|
2013-07-01 04:40:16 +08:00
|
|
|
}
|
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
|
|
|
|
2013-12-18 03:46:40 +08:00
|
|
|
if (MD && (isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)))
|
|
|
|
CGM.getCXXABI().addImplicitStructorParams(*this, ResTy, Args);
|
|
|
|
|
2010-02-18 11:17:58 +08:00
|
|
|
SourceRange BodyRange;
|
|
|
|
if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange();
|
2013-05-16 08:41:26 +08:00
|
|
|
CurEHLocation = BodyRange.getEnd();
|
2009-11-06 10:55:43 +08:00
|
|
|
|
2014-04-11 07:21:53 +08:00
|
|
|
// Use the location of the start of the function to determine where
|
|
|
|
// the function definition is located. By default use the location
|
|
|
|
// of the declaration as the location for the subprogram. A function
|
|
|
|
// may lack a declaration in the source code if it is created by code
|
|
|
|
// gen. (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk).
|
|
|
|
SourceLocation Loc;
|
|
|
|
if (FD) {
|
|
|
|
Loc = FD->getLocation();
|
|
|
|
|
|
|
|
// If this is a function specialization then use the pattern body
|
|
|
|
// as the location for the function.
|
|
|
|
if (const FunctionDecl *SpecDecl = FD->getTemplateInstantiationPattern())
|
|
|
|
if (SpecDecl->hasBody(SpecDecl))
|
|
|
|
Loc = SpecDecl->getLocation();
|
|
|
|
}
|
|
|
|
|
2010-02-18 11:17:58 +08:00
|
|
|
// Emit the standard function prologue.
|
2014-04-11 07:21:53 +08:00
|
|
|
StartFunction(GD, ResTy, Fn, FnInfo, Args, Loc, BodyRange.getBegin());
|
2010-02-08 03:45:40 +08:00
|
|
|
|
2010-02-18 11:17:58 +08:00
|
|
|
// Generate the body of the function.
|
2014-03-06 12:55:41 +08:00
|
|
|
PGO.assignRegionCounters(GD.getDecl(), CurFn);
|
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()) {
|
Implement a rudimentary form of generic lambdas.
Specifically, the following features are not included in this commit:
- any sort of capturing within generic lambdas
- generic lambdas within template functions and nested
within other generic lambdas
- conversion operator for captureless lambdas
- ensuring all visitors are generic lambda aware
(Although I have gotten some useful feedback on my patches of the above and will be incorporating that as I submit those patches for commit)
As an example of what compiles through this commit:
template <class F1, class F2>
struct overload : F1, F2 {
using F1::operator();
using F2::operator();
overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }
};
auto Recursive = [](auto Self, auto h, auto ... rest) {
return 1 + Self(Self, rest...);
};
auto Base = [](auto Self, auto h) {
return 1;
};
overload<decltype(Base), decltype(Recursive)> O(Base, Recursive);
int num_params = O(O, 5, 3, "abc", 3.14, 'a');
Please see attached tests for more examples.
This patch has been reviewed by Doug and Richard. Minor changes (non-functionality affecting) have been made since both of them formally looked at it, but the changes involve removal of supernumerary return type deduction changes (since they are now redundant, with richard having committed a recent patch to address return type deduction for C++11 lambdas using C++14 semantics).
Some implementation notes:
- Add a new Declarator context => LambdaExprParameterContext to
clang::Declarator to allow the use of 'auto' in declaring generic
lambda parameters
- Add various helpers to CXXRecordDecl to facilitate identifying
and querying a closure class
- LambdaScopeInfo (which maintains the current lambda's Sema state)
was augmented to house the current depth of the template being
parsed (id est the Parser calls Sema::RecordParsingTemplateParameterDepth)
so that SemaType.cpp::ConvertDeclSpecToType may use it to immediately
generate a template-parameter-type when 'auto' is parsed in a generic
lambda parameter context. (i.e we do NOT use AutoType deduced to
a template parameter type - Richard seemed ok with this approach).
We encode that this template type was generated from an auto by simply
adding $auto to the name which can be used for better diagnostics if needed.
- SemaLambda.h was added to hold some common lambda utility
functions (this file is likely to grow ...)
- Teach Sema::ActOnStartOfFunctionDef to check whether it
is being called to instantiate a generic lambda's call
operator, and if so, push an appropriately prepared
LambdaScopeInfo object on the stack.
- various tests were added - but much more will be needed.
There is obviously more work to be done, and both Richard (weakly) and Doug (strongly)
have requested that LambdaExpr be removed form the CXXRecordDecl LambdaDefinitionaData
in a future patch which is forthcoming.
A greatful thanks to all reviewers including Eli Friedman, James Dennett,
and especially the two gracious wizards (Richard Smith and Doug Gregor)
who spent hours providing feedback (in person in Chicago and on the mailing lists).
And yet I am certain that I have allowed unidentified bugs to creep in; bugs, that I will do my best to slay, once identified!
Thanks!
llvm-svn: 191453
2013-09-27 03:54:12 +08:00
|
|
|
// The lambda static invoker function is special, because it forwards or
|
2012-02-17 11:02:34 +08:00
|
|
|
// clones the body of the function call operator (but is actually static).
|
|
|
|
EmitLambdaStaticInvokeFunction(cast<CXXMethodDecl>(FD));
|
2013-02-17 15:22:09 +08:00
|
|
|
} else if (FD->isDefaulted() && isa<CXXMethodDecl>(FD) &&
|
2013-09-10 13:14:39 +08:00
|
|
|
(cast<CXXMethodDecl>(FD)->isCopyAssignmentOperator() ||
|
|
|
|
cast<CXXMethodDecl>(FD)->isMoveAssignmentOperator())) {
|
2013-02-17 15:22:09 +08:00
|
|
|
// Implicit copy-assignment gets the same special treatment as implicit
|
|
|
|
// copy-constructors.
|
|
|
|
emitImplicitAssignmentOperatorBody(Args);
|
2013-11-05 17:12:18 +08:00
|
|
|
} else if (Stmt *Body = FD->getBody()) {
|
|
|
|
EmitFunctionBody(Args, Body);
|
|
|
|
} else if (FunctionDecl *UnsizedDealloc =
|
|
|
|
FD->getCorrespondingUnsizedGlobalDeallocationFunction()) {
|
|
|
|
// Global sized deallocation functions get an implicit weak definition if
|
|
|
|
// they don't have an explicit definition.
|
|
|
|
EmitSizedDeallocationFunction(*this, UnsizedDealloc);
|
|
|
|
} else
|
|
|
|
llvm_unreachable("no definition for emitted function");
|
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() &&
|
2014-01-26 00:55:45 +08:00
|
|
|
!FD->getReturnType()->isVoidType() && Builder.GetInsertBlock()) {
|
2013-01-18 19:30:38 +08:00
|
|
|
if (SanOpts->Return)
|
2012-10-10 03:52:38 +08:00
|
|
|
EmitCheck(Builder.getFalse(), "missing_return",
|
|
|
|
EmitCheckSourceLocation(FD->getLocation()),
|
2013-01-13 03:30:44 +08:00
|
|
|
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);
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2014-03-18 05:18:30 +08:00
|
|
|
PGO.emitInstrumentationData();
|
2014-01-07 06:27:43 +08:00
|
|
|
PGO.destroyRegionCounters();
|
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!
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!S) 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!
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!S) return false;
|
2011-02-28 08:18:40 +08:00
|
|
|
|
|
|
|
// 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,
|
2014-01-07 06:27:43 +08:00
|
|
|
llvm::BasicBlock *FalseBlock,
|
|
|
|
uint64_t TrueCount) {
|
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)) {
|
2014-01-07 06:27:43 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// Handle X && Y in a condition.
|
2010-08-25 19:45:40 +08:00
|
|
|
if (CondBOp->getOpcode() == BO_LAnd) {
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
RegionCounter Cnt = getPGORegionCounter(CondBOp);
|
|
|
|
|
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).
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock,
|
|
|
|
TrueCount);
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
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).
|
2014-01-07 06:27:43 +08:00
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock,
|
|
|
|
TrueCount);
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
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");
|
2014-01-07 06:27:43 +08:00
|
|
|
// The counter tells us how often we evaluate RHS, and all of TrueCount
|
|
|
|
// can be propagated to that branch.
|
|
|
|
uint64_t RHSCount = Cnt.getCount();
|
2011-01-26 12:00:11 +08:00
|
|
|
|
|
|
|
ConditionalEvaluation eval(*this);
|
2014-01-07 06:27:43 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock, RHSCount);
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBlock(LHSTrue);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-24 08:20:05 +08:00
|
|
|
// Any temporaries created here are conditional.
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.begin(*this);
|
2014-01-07 06:27:43 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, TrueCount);
|
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) {
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
RegionCounter Cnt = getPGORegionCounter(CondBOp);
|
|
|
|
|
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).
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock,
|
|
|
|
TrueCount);
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
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).
|
2014-01-07 06:27:43 +08:00
|
|
|
return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock,
|
|
|
|
TrueCount);
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
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");
|
2014-01-07 06:27:43 +08:00
|
|
|
// We have the count for entry to the RHS and for the whole expression
|
|
|
|
// being true, so we can divy up True count between the short circuit and
|
|
|
|
// the RHS.
|
2014-01-23 10:54:30 +08:00
|
|
|
uint64_t LHSCount = Cnt.getParentCount() - Cnt.getCount();
|
2014-01-07 06:27:43 +08:00
|
|
|
uint64_t RHSCount = TrueCount - LHSCount;
|
2011-01-26 12:00:11 +08:00
|
|
|
|
|
|
|
ConditionalEvaluation eval(*this);
|
2014-01-07 06:27:43 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse, LHSCount);
|
2008-11-12 16:04:58 +08:00
|
|
|
EmitBlock(LHSFalse);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-24 08:20:05 +08:00
|
|
|
// Any temporaries created here are conditional.
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.begin(*this);
|
2014-01-07 06:27:43 +08:00
|
|
|
EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, RHSCount);
|
|
|
|
|
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)
|
2014-01-07 06:27:43 +08:00
|
|
|
if (CondUOp->getOpcode() == UO_LNot) {
|
|
|
|
// Negate the count.
|
|
|
|
uint64_t FalseCount = PGO.getCurrentRegionCount() - TrueCount;
|
|
|
|
// Negate the condition and swap the destination blocks.
|
|
|
|
return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock,
|
|
|
|
FalseCount);
|
|
|
|
}
|
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
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
RegionCounter Cnt = getPGORegionCounter(CondOp);
|
2012-02-14 11:54:45 +08:00
|
|
|
ConditionalEvaluation cond(*this);
|
2014-01-07 06:27:43 +08:00
|
|
|
EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock, Cnt.getCount());
|
|
|
|
|
|
|
|
// When computing PGO branch weights, we only know the overall count for
|
|
|
|
// the true block. This code is essentially doing tail duplication of the
|
|
|
|
// naive code-gen, introducing new edges for which counts are not
|
|
|
|
// available. Divide the counts proportionally between the LHS and RHS of
|
|
|
|
// the conditional operator.
|
|
|
|
uint64_t LHSScaledTrueCount = 0;
|
|
|
|
if (TrueCount) {
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
double LHSRatio = Cnt.getCount() / (double) Cnt.getParentCount();
|
2014-01-07 06:27:43 +08:00
|
|
|
LHSScaledTrueCount = TrueCount * LHSRatio;
|
|
|
|
}
|
2011-01-26 12:00:11 +08:00
|
|
|
|
2012-02-14 11:54:45 +08:00
|
|
|
cond.begin(*this);
|
|
|
|
EmitBlock(LHSBlock);
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock,
|
|
|
|
LHSScaledTrueCount);
|
2012-02-14 11:54:45 +08:00
|
|
|
cond.end(*this);
|
2011-01-26 12:00:11 +08:00
|
|
|
|
2012-02-14 11:54:45 +08:00
|
|
|
cond.begin(*this);
|
|
|
|
EmitBlock(RHSBlock);
|
2014-01-07 06:27:43 +08:00
|
|
|
EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock,
|
|
|
|
TrueCount - LHSScaledTrueCount);
|
2012-02-14 11:54:45 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-08 05:53:22 +08:00
|
|
|
if (const CXXThrowExpr *Throw = dyn_cast<CXXThrowExpr>(Cond)) {
|
|
|
|
// Conditional operator handling can give us a throw expression as a
|
|
|
|
// condition for a case like:
|
|
|
|
// br(c ? throw x : y, t, f) -> br(c, br(throw x, t, f), br(y, t, f)
|
|
|
|
// Fold this to:
|
|
|
|
// br(c, throw x, br(y, t, f))
|
|
|
|
EmitCXXThrowExpr(Throw, /*KeepInsertionPoint*/false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
// Create branch weights based on the number of times we get here and the
|
|
|
|
// number of times the condition should be true.
|
Change PGO instrumentation to compute counts in a separate AST traversal.
Previously, we made one traversal of the AST prior to codegen to assign
counters to the ASTs and then propagated the count values during codegen. This
patch now adds a separate AST traversal prior to codegen for the
-fprofile-instr-use option to propagate the count values. The counts are then
saved in a map from which they can be retrieved during codegen.
This new approach has several advantages:
1. It gets rid of a lot of extra PGO-related code that had previously been
added to codegen.
2. It fixes a serious bug. My original implementation (which was mailed to the
list but never committed) used 3 counters for every loop. Justin improved it to
move 2 of those counters into the less-frequently executed breaks and continues,
but that turned out to produce wrong count values in some cases. The solution
requires visiting a loop body before the condition so that the count for the
condition properly includes the break and continue counts. Changing codegen to
visit a loop body first would be a fairly invasive change, but with a separate
AST traversal, it is easy to control the order of traversal. I've added a
testcase (provided by Justin) to make sure this works correctly.
3. It improves the instrumentation overhead, reducing the number of counters for
a loop from 3 to 1. We no longer need dedicated counters for breaks and
continues, since we can just use the propagated count values when visiting
breaks and continues.
To make this work, I needed to make a change to the way we count case
statements, going back to my original approach of not including the fall-through
in the counter values. This was necessary because there isn't always an AST node
that can be used to record the fall-through count. Now case statements are
handled the same as default statements, with the fall-through paths branching
over the counter increments. While I was at it, I also went back to using this
approach for do-loops -- omitting the fall-through count into the loop body
simplifies some of the calculations and make them behave the same as other
loops. Whenever we start using this instrumentation for coverage, we'll need
to add the fall-through counts into the counter values.
llvm-svn: 201528
2014-02-18 03:21:09 +08:00
|
|
|
uint64_t CurrentCount = std::max(PGO.getCurrentRegionCount(), TrueCount);
|
2014-01-07 06:27:43 +08:00
|
|
|
llvm::MDNode *Weights = PGO.createBranchWeights(TrueCount,
|
|
|
|
CurrentCount - TrueCount);
|
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// Emit the code with the fully general case.
|
|
|
|
llvm::Value *CondV = EvaluateExprAsBool(Cond);
|
2014-01-07 06:27:43 +08:00
|
|
|
Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights);
|
2008-11-12 16:04:58 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2013-08-20 05:02:26 +08:00
|
|
|
void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type) {
|
|
|
|
CGM.ErrorUnsupported(S, Type);
|
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;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(numElts, eltType) = getVLASize(vlaType);
|
2011-06-25 05:55:10 +08:00
|
|
|
|
|
|
|
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);
|
2014-05-21 13:09:00 +08:00
|
|
|
vla = nullptr;
|
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.
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!IndirectBranch)
|
2009-10-29 07:59:40 +08:00
|
|
|
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.
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *numVLAElements = nullptr;
|
2011-07-09 09:37:26 +08:00
|
|
|
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.
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *numElements = nullptr;
|
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.
|
2014-03-20 18:48:29 +08:00
|
|
|
// FIXME: Teach -fsanitize=undefined 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!");
|
|
|
|
|
2013-12-05 09:23:43 +08:00
|
|
|
case Type::Adjusted:
|
|
|
|
type = cast<AdjustedType>(ty)->getAdjustedType();
|
|
|
|
break;
|
|
|
|
|
2013-06-25 01:51:48 +08:00
|
|
|
case Type::Decayed:
|
|
|
|
type = cast<DecayedType>(ty)->getPointeeType();
|
|
|
|
break;
|
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
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.
|
2013-01-18 19:30:38 +08:00
|
|
|
if (SanOpts->VLABound &&
|
2012-11-06 06:21:05 +08:00
|
|
|
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:
|
2014-01-26 00:55:45 +08:00
|
|
|
type = cast<FunctionType>(ty)->getReturnType();
|
2011-06-25 05:55:10 +08:00
|
|
|
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:
|
2013-07-14 05:08:08 +08:00
|
|
|
case Type::PackExpansion:
|
2012-01-11 16:19:46 +08:00
|
|
|
// 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
|
|
|
|
2013-08-30 16:53:09 +08:00
|
|
|
void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
|
|
|
|
llvm::Constant *Init) {
|
2010-08-11 01:53:33 +08:00
|
|
|
assert (Init && "Invalid DeclRefExpr initializer!");
|
2013-08-30 16:53:09 +08:00
|
|
|
if (CGDebugInfo *Dbg = getDebugInfo())
|
|
|
|
if (CGM.getCodeGenOpts().getDebugInfo() >= CodeGenOptions::LimitedDebugInfo)
|
|
|
|
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,
|
2013-01-13 03:30:44 +08:00
|
|
|
StringRef AnnotationStr,
|
2011-09-10 06:41:49 +08:00
|
|
|
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.
|
2014-03-11 01:08:28 +08:00
|
|
|
for (const auto *I : D->specific_attrs<AnnotateAttr>())
|
2011-09-10 06:41:49 +08:00
|
|
|
EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation),
|
|
|
|
Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()),
|
2014-03-11 01:08:28 +08:00
|
|
|
I->getAnnotation(), D->getLocation());
|
2011-09-10 06:41:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-03-11 01:08:28 +08:00
|
|
|
for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
|
2011-09-10 06:41:49 +08:00
|
|
|
// 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));
|
2014-03-11 01:08:28 +08:00
|
|
|
V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation());
|
2011-09-10 06:41:49 +08:00
|
|
|
V = Builder.CreateBitCast(V, VTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
2013-05-10 03:17:11 +08:00
|
|
|
|
|
|
|
CodeGenFunction::CGCapturedStmtInfo::~CGCapturedStmtInfo() { }
|
2014-05-22 16:54:05 +08:00
|
|
|
|
|
|
|
void CodeGenFunction::InsertHelper(llvm::Instruction *I,
|
|
|
|
const llvm::Twine &Name,
|
|
|
|
llvm::BasicBlock *BB,
|
|
|
|
llvm::BasicBlock::iterator InsertPt) const {
|
|
|
|
LoopStack.InsertHelper(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool PreserveNames>
|
|
|
|
void CGBuilderInserter<PreserveNames>::InsertHelper(
|
|
|
|
llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB,
|
|
|
|
llvm::BasicBlock::iterator InsertPt) const {
|
|
|
|
llvm::IRBuilderDefaultInserter<PreserveNames>::InsertHelper(I, Name, BB,
|
|
|
|
InsertPt);
|
|
|
|
if (CGF)
|
|
|
|
CGF->InsertHelper(I, Name, BB, InsertPt);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
#define PreserveNames false
|
|
|
|
#else
|
|
|
|
#define PreserveNames true
|
|
|
|
#endif
|
|
|
|
template void CGBuilderInserter<PreserveNames>::InsertHelper(
|
|
|
|
llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB,
|
|
|
|
llvm::BasicBlock::iterator InsertPt) const;
|
|
|
|
#undef PreserveNames
|