2007-06-02 02:02:12 +08:00
|
|
|
//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
|
|
|
|
//
|
|
|
|
// 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-06-02 02:02:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Expr nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CGCall.h"
|
2011-03-05 02:54:42 +08:00
|
|
|
#include "CGDebugInfo.h"
|
2008-08-13 08:59:25 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2014-11-11 12:05:39 +08:00
|
|
|
#include "CGOpenMPRuntime.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CGRecordLayout.h"
|
|
|
|
#include "CodeGenModule.h"
|
2011-09-21 16:08:30 +08:00
|
|
|
#include "TargetInfo.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-05-20 02:15:42 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-06-16 07:19:56 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/Hashing.h"
|
2014-10-09 16:45:04 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/MDBuilder.h"
|
2013-01-30 20:06:08 +08:00
|
|
|
#include "llvm/Support/ConvertUTF.h"
|
|
|
|
|
2007-06-02 02:02:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2007-06-03 03:33:17 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Miscellaneous Helper Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2011-02-08 16:22:06 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitCastToVoidPtr(llvm::Value *value) {
|
|
|
|
unsigned addressSpace =
|
|
|
|
cast<llvm::PointerType>(value->getType())->getAddressSpace();
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::PointerType *destType = Int8PtrTy;
|
2011-02-08 16:22:06 +08:00
|
|
|
if (addressSpace)
|
|
|
|
destType = llvm::Type::getInt8PtrTy(getLLVMContext(), addressSpace);
|
|
|
|
|
|
|
|
if (value->getType() == destType) return value;
|
|
|
|
return Builder.CreateBitCast(value, destType);
|
|
|
|
}
|
|
|
|
|
2007-06-23 05:44:33 +08:00
|
|
|
/// CreateTempAlloca - This creates a alloca and inserts it into the entry
|
|
|
|
/// block.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(llvm::Type *Ty,
|
2011-07-23 18:55:15 +08:00
|
|
|
const Twine &Name) {
|
2009-03-22 08:24:14 +08:00
|
|
|
if (!Builder.isNamePreserving())
|
2014-05-21 13:09:00 +08:00
|
|
|
return new llvm::AllocaInst(Ty, nullptr, "", AllocaInsertPt);
|
|
|
|
return new llvm::AllocaInst(Ty, nullptr, Name, AllocaInsertPt);
|
2007-06-23 05:44:33 +08:00
|
|
|
}
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2010-04-22 09:10:34 +08:00
|
|
|
void CodeGenFunction::InitTempAlloca(llvm::AllocaInst *Var,
|
|
|
|
llvm::Value *Init) {
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *Store = new llvm::StoreInst(Init, Var);
|
2010-04-22 09:10:34 +08:00
|
|
|
llvm::BasicBlock *Block = AllocaInsertPt->getParent();
|
|
|
|
Block->getInstList().insertAfter(&*AllocaInsertPt, Store);
|
|
|
|
}
|
|
|
|
|
2010-07-06 04:21:00 +08:00
|
|
|
llvm::AllocaInst *CodeGenFunction::CreateIRTemp(QualType Ty,
|
2011-07-23 18:55:15 +08:00
|
|
|
const Twine &Name) {
|
2010-02-17 03:44:13 +08:00
|
|
|
llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertType(Ty), Name);
|
|
|
|
// FIXME: Should we prefer the preferred type alignment here?
|
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(Ty);
|
|
|
|
Alloc->setAlignment(Align.getQuantity());
|
|
|
|
return Alloc;
|
|
|
|
}
|
|
|
|
|
2010-07-06 04:21:00 +08:00
|
|
|
llvm::AllocaInst *CodeGenFunction::CreateMemTemp(QualType Ty,
|
2011-07-23 18:55:15 +08:00
|
|
|
const Twine &Name) {
|
2010-02-09 10:48:28 +08:00
|
|
|
llvm::AllocaInst *Alloc = CreateTempAlloca(ConvertTypeForMem(Ty), Name);
|
|
|
|
// FIXME: Should we prefer the preferred type alignment here?
|
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(Ty);
|
|
|
|
Alloc->setAlignment(Align.getQuantity());
|
|
|
|
return Alloc;
|
|
|
|
}
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
|
|
|
|
/// expression and compare the result against zero, returning an Int1Ty value.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
|
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
|
|
|
PGO.setCurrentStmt(E);
|
2010-08-23 09:21:21 +08:00
|
|
|
if (const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>()) {
|
2010-08-22 18:59:02 +08:00
|
|
|
llvm::Value *MemPtr = EmitScalarExpr(E);
|
2011-02-08 16:22:06 +08:00
|
|
|
return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, MemPtr, MPT);
|
2009-12-11 17:26:29 +08:00
|
|
|
}
|
2010-08-23 09:21:21 +08:00
|
|
|
|
|
|
|
QualType BoolTy = getContext().BoolTy;
|
2008-04-05 00:54:41 +08:00
|
|
|
if (!E->getType()->isAnyComplexType())
|
2007-08-27 00:46:58 +08:00
|
|
|
return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
|
2007-06-06 04:53:16 +08:00
|
|
|
|
2007-08-27 00:46:58 +08:00
|
|
|
return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
|
2007-06-03 03:33:17 +08:00
|
|
|
}
|
|
|
|
|
2010-12-05 10:00:02 +08:00
|
|
|
/// EmitIgnoredExpr - Emit code to compute the specified expression,
|
|
|
|
/// ignoring the result.
|
|
|
|
void CodeGenFunction::EmitIgnoredExpr(const Expr *E) {
|
|
|
|
if (E->isRValue())
|
|
|
|
return (void) EmitAnyExpr(E, AggValueSlot::ignored(), true);
|
|
|
|
|
|
|
|
// Just emit it as an l-value and drop the result.
|
|
|
|
EmitLValue(E);
|
|
|
|
}
|
|
|
|
|
2010-09-15 18:14:12 +08:00
|
|
|
/// EmitAnyExpr - Emit code to compute the specified expression which
|
|
|
|
/// can have any type. The result is returned as an RValue struct.
|
|
|
|
/// If this is an aggregate expression, AggSlot indicates where the
|
2009-09-09 21:00:44 +08:00
|
|
|
/// result should be returned.
|
2012-07-03 07:58:38 +08:00
|
|
|
RValue CodeGenFunction::EmitAnyExpr(const Expr *E,
|
|
|
|
AggValueSlot aggSlot,
|
|
|
|
bool ignoreResult) {
|
2013-03-08 05:37:08 +08:00
|
|
|
switch (getEvaluationKind(E->getType())) {
|
|
|
|
case TEK_Scalar:
|
2012-07-03 07:58:38 +08:00
|
|
|
return RValue::get(EmitScalarExpr(E, ignoreResult));
|
2013-03-08 05:37:08 +08:00
|
|
|
case TEK_Complex:
|
2012-07-03 07:58:38 +08:00
|
|
|
return RValue::getComplex(EmitComplexExpr(E, ignoreResult, ignoreResult));
|
2013-03-08 05:37:08 +08:00
|
|
|
case TEK_Aggregate:
|
|
|
|
if (!ignoreResult && aggSlot.isIgnored())
|
|
|
|
aggSlot = CreateAggTemp(E->getType(), "agg-temp");
|
|
|
|
EmitAggExpr(E, aggSlot);
|
|
|
|
return aggSlot.asRValue();
|
|
|
|
}
|
|
|
|
llvm_unreachable("bad evaluation kind");
|
2007-09-01 06:49:20 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
|
|
|
|
/// always be accessible even if no aggregate location is provided.
|
2010-09-15 18:14:12 +08:00
|
|
|
RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E) {
|
|
|
|
AggValueSlot AggSlot = AggValueSlot::ignored();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2013-03-08 05:37:08 +08:00
|
|
|
if (hasAggregateEvaluationKind(E->getType()))
|
2010-09-15 18:14:12 +08:00
|
|
|
AggSlot = CreateAggTemp(E->getType(), "agg.tmp");
|
|
|
|
return EmitAnyExpr(E, AggSlot);
|
2008-09-09 09:06:48 +08:00
|
|
|
}
|
|
|
|
|
2010-04-21 18:05:39 +08:00
|
|
|
/// EmitAnyExprToMem - Evaluate an expression into a given memory
|
|
|
|
/// location.
|
|
|
|
void CodeGenFunction::EmitAnyExprToMem(const Expr *E,
|
|
|
|
llvm::Value *Location,
|
2012-03-30 01:37:10 +08:00
|
|
|
Qualifiers Quals,
|
|
|
|
bool IsInit) {
|
2011-12-03 08:54:26 +08:00
|
|
|
// FIXME: This function should take an LValue as an argument.
|
2013-03-08 05:37:08 +08:00
|
|
|
switch (getEvaluationKind(E->getType())) {
|
|
|
|
case TEK_Complex:
|
|
|
|
EmitComplexExprIntoLValue(E,
|
|
|
|
MakeNaturalAlignAddrLValue(Location, E->getType()),
|
|
|
|
/*isInit*/ false);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case TEK_Aggregate: {
|
2011-12-03 10:13:40 +08:00
|
|
|
CharUnits Alignment = getContext().getTypeAlignInChars(E->getType());
|
2011-12-03 08:54:26 +08:00
|
|
|
EmitAggExpr(E, AggValueSlot::forAddr(Location, Alignment, Quals,
|
2012-03-30 01:37:10 +08:00
|
|
|
AggValueSlot::IsDestructed_t(IsInit),
|
2011-08-26 13:38:08 +08:00
|
|
|
AggValueSlot::DoesNotNeedGCBarriers,
|
2012-03-30 01:37:10 +08:00
|
|
|
AggValueSlot::IsAliased_t(!IsInit)));
|
2013-03-08 05:37:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TEK_Scalar: {
|
2010-04-21 18:05:39 +08:00
|
|
|
RValue RV = RValue::get(EmitScalarExpr(E, /*Ignore*/ false));
|
2010-08-21 11:08:16 +08:00
|
|
|
LValue LV = MakeAddrLValue(Location, E->getType());
|
2011-06-25 10:11:03 +08:00
|
|
|
EmitStoreThroughLValue(RV, LV);
|
2013-03-08 05:37:08 +08:00
|
|
|
return;
|
2010-04-21 18:05:39 +08:00
|
|
|
}
|
2013-03-08 05:37:08 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("bad evaluation kind");
|
2010-04-21 18:05:39 +08:00
|
|
|
}
|
|
|
|
|
2014-10-10 12:05:00 +08:00
|
|
|
static void
|
|
|
|
pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
|
|
|
|
const Expr *E, llvm::Value *ReferenceTemporary) {
|
2013-06-13 04:42:33 +08:00
|
|
|
// Objective-C++ ARC:
|
|
|
|
// If we are binding a reference to a temporary that has ownership, we
|
|
|
|
// need to perform retain/release operations on the temporary.
|
|
|
|
//
|
|
|
|
// FIXME: This should be looking at E, not M.
|
|
|
|
if (CGF.getLangOpts().ObjCAutoRefCount &&
|
|
|
|
M->getType()->isObjCLifetimeType()) {
|
|
|
|
QualType ObjCARCReferenceLifetimeType = M->getType();
|
|
|
|
switch (Qualifiers::ObjCLifetime Lifetime =
|
|
|
|
ObjCARCReferenceLifetimeType.getObjCLifetime()) {
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
// Carry on to normal cleanup handling.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
// Nothing to do; cleaned up by an autorelease pool.
|
|
|
|
return;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Strong:
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
switch (StorageDuration Duration = M->getStorageDuration()) {
|
|
|
|
case SD_Static:
|
|
|
|
// Note: we intentionally do not register a cleanup to release
|
|
|
|
// the object on program termination.
|
|
|
|
return;
|
|
|
|
|
|
|
|
case SD_Thread:
|
|
|
|
// FIXME: We should probably register a cleanup in this case.
|
|
|
|
return;
|
|
|
|
|
|
|
|
case SD_Automatic:
|
|
|
|
case SD_FullExpression:
|
|
|
|
CodeGenFunction::Destroyer *Destroy;
|
|
|
|
CleanupKind CleanupKind;
|
|
|
|
if (Lifetime == Qualifiers::OCL_Strong) {
|
|
|
|
const ValueDecl *VD = M->getExtendingDecl();
|
|
|
|
bool Precise =
|
|
|
|
VD && isa<VarDecl>(VD) && VD->hasAttr<ObjCPreciseLifetimeAttr>();
|
|
|
|
CleanupKind = CGF.getARCCleanupKind();
|
|
|
|
Destroy = Precise ? &CodeGenFunction::destroyARCStrongPrecise
|
|
|
|
: &CodeGenFunction::destroyARCStrongImprecise;
|
|
|
|
} else {
|
|
|
|
// __weak objects always get EH cleanups; otherwise, exceptions
|
|
|
|
// could cause really nasty crashes instead of mere leaks.
|
|
|
|
CleanupKind = NormalAndEHCleanup;
|
|
|
|
Destroy = &CodeGenFunction::destroyARCWeak;
|
|
|
|
}
|
|
|
|
if (Duration == SD_FullExpression)
|
|
|
|
CGF.pushDestroy(CleanupKind, ReferenceTemporary,
|
|
|
|
ObjCARCReferenceLifetimeType, *Destroy,
|
|
|
|
CleanupKind & EHCleanup);
|
|
|
|
else
|
|
|
|
CGF.pushLifetimeExtendedDestroy(CleanupKind, ReferenceTemporary,
|
|
|
|
ObjCARCReferenceLifetimeType,
|
|
|
|
*Destroy, CleanupKind & EHCleanup);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case SD_Dynamic:
|
|
|
|
llvm_unreachable("temporary cannot have dynamic storage duration");
|
|
|
|
}
|
|
|
|
llvm_unreachable("unknown storage duration");
|
2013-06-11 10:41:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
CXXDestructorDecl *ReferenceTemporaryDtor = nullptr;
|
2013-06-13 04:42:33 +08:00
|
|
|
if (const RecordType *RT =
|
|
|
|
E->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) {
|
|
|
|
// Get the destructor for the reference temporary.
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
|
2013-06-13 04:42:33 +08:00
|
|
|
if (!ClassDecl->hasTrivialDestructor())
|
|
|
|
ReferenceTemporaryDtor = ClassDecl->getDestructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReferenceTemporaryDtor)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Call the destructor for the temporary.
|
|
|
|
switch (M->getStorageDuration()) {
|
|
|
|
case SD_Static:
|
|
|
|
case SD_Thread: {
|
|
|
|
llvm::Constant *CleanupFn;
|
|
|
|
llvm::Constant *CleanupArg;
|
|
|
|
if (E->getType()->isArrayType()) {
|
|
|
|
CleanupFn = CodeGenFunction(CGF.CGM).generateDestroyHelper(
|
|
|
|
cast<llvm::Constant>(ReferenceTemporary), E->getType(),
|
2013-08-28 07:57:18 +08:00
|
|
|
CodeGenFunction::destroyCXXObject, CGF.getLangOpts().Exceptions,
|
|
|
|
dyn_cast_or_null<VarDecl>(M->getExtendingDecl()));
|
2013-06-13 04:42:33 +08:00
|
|
|
CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy);
|
|
|
|
} else {
|
2014-09-11 23:42:06 +08:00
|
|
|
CleanupFn = CGF.CGM.getAddrOfCXXStructor(ReferenceTemporaryDtor,
|
|
|
|
StructorType::Complete);
|
2013-06-13 04:42:33 +08:00
|
|
|
CleanupArg = cast<llvm::Constant>(ReferenceTemporary);
|
|
|
|
}
|
|
|
|
CGF.CGM.getCXXABI().registerGlobalDtor(
|
|
|
|
CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SD_FullExpression:
|
|
|
|
CGF.pushDestroy(NormalAndEHCleanup, ReferenceTemporary, E->getType(),
|
|
|
|
CodeGenFunction::destroyCXXObject,
|
|
|
|
CGF.getLangOpts().Exceptions);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SD_Automatic:
|
|
|
|
CGF.pushLifetimeExtendedDestroy(NormalAndEHCleanup,
|
|
|
|
ReferenceTemporary, E->getType(),
|
|
|
|
CodeGenFunction::destroyCXXObject,
|
|
|
|
CGF.getLangOpts().Exceptions);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SD_Dynamic:
|
|
|
|
llvm_unreachable("temporary cannot have dynamic storage duration");
|
|
|
|
}
|
2013-06-11 10:41:00 +08:00
|
|
|
}
|
2011-11-28 00:50:07 +08:00
|
|
|
|
2013-06-11 10:41:00 +08:00
|
|
|
static llvm::Value *
|
2013-06-13 04:42:33 +08:00
|
|
|
createReferenceTemporary(CodeGenFunction &CGF,
|
2014-10-10 12:05:00 +08:00
|
|
|
const MaterializeTemporaryExpr *M, const Expr *Inner) {
|
2013-06-13 04:42:33 +08:00
|
|
|
switch (M->getStorageDuration()) {
|
|
|
|
case SD_FullExpression:
|
2014-10-10 12:05:00 +08:00
|
|
|
case SD_Automatic:
|
2015-03-07 21:37:13 +08:00
|
|
|
// If we have a constant temporary array or record try to promote it into a
|
|
|
|
// constant global under the same rules a normal constant would've been
|
|
|
|
// promoted. This is easier on the optimizer and generally emits fewer
|
|
|
|
// instructions.
|
|
|
|
if (CGF.CGM.getCodeGenOpts().MergeAllConstants &&
|
|
|
|
(M->getType()->isArrayType() || M->getType()->isRecordType()) &&
|
|
|
|
CGF.CGM.isTypeConstant(M->getType(), true))
|
|
|
|
if (llvm::Constant *Init =
|
|
|
|
CGF.CGM.EmitConstantExpr(Inner, M->getType(), &CGF)) {
|
|
|
|
auto *GV = new llvm::GlobalVariable(
|
|
|
|
CGF.CGM.getModule(), Init->getType(), /*isConstant=*/true,
|
|
|
|
llvm::GlobalValue::PrivateLinkage, Init, ".ref.tmp");
|
|
|
|
GV->setAlignment(
|
|
|
|
CGF.getContext().getTypeAlignInChars(M->getType()).getQuantity());
|
|
|
|
// FIXME: Should we put the new global into a COMDAT?
|
2015-03-18 00:38:58 +08:00
|
|
|
return GV;
|
2015-03-07 21:37:13 +08:00
|
|
|
}
|
2014-10-10 12:05:00 +08:00
|
|
|
return CGF.CreateMemTemp(Inner->getType(), "ref.tmp");
|
2013-06-13 04:42:33 +08:00
|
|
|
|
|
|
|
case SD_Thread:
|
|
|
|
case SD_Static:
|
2015-03-18 00:38:58 +08:00
|
|
|
return CGF.CGM.GetAddrOfGlobalTemporary(M, Inner);
|
2013-06-13 04:42:33 +08:00
|
|
|
|
|
|
|
case SD_Dynamic:
|
|
|
|
llvm_unreachable("temporary can't have dynamic storage duration");
|
|
|
|
}
|
|
|
|
llvm_unreachable("unknown storage duration");
|
|
|
|
}
|
2013-06-12 03:14:25 +08:00
|
|
|
|
2014-10-25 03:54:32 +08:00
|
|
|
LValue CodeGenFunction::
|
|
|
|
EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) {
|
2013-06-13 07:38:09 +08:00
|
|
|
const Expr *E = M->GetTemporaryExpr();
|
2013-06-13 04:42:33 +08:00
|
|
|
|
2014-10-25 04:23:43 +08:00
|
|
|
// FIXME: ideally this would use EmitAnyExprToMem, however, we cannot do so
|
|
|
|
// as that will cause the lifetime adjustment to be lost for ARC
|
2013-06-13 07:38:09 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2013-06-13 04:42:33 +08:00
|
|
|
M->getType()->isObjCLifetimeType() &&
|
|
|
|
M->getType().getObjCLifetime() != Qualifiers::OCL_None &&
|
|
|
|
M->getType().getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
|
2014-10-10 12:05:00 +08:00
|
|
|
llvm::Value *Object = createReferenceTemporary(*this, M, E);
|
2014-05-09 08:08:36 +08:00
|
|
|
if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object)) {
|
2015-03-18 09:06:24 +08:00
|
|
|
Object = llvm::ConstantExpr::getBitCast(
|
|
|
|
Var, ConvertTypeForMem(E->getType())->getPointerTo());
|
2013-06-14 11:07:01 +08:00
|
|
|
// We should not have emitted the initializer for this temporary as a
|
|
|
|
// constant.
|
|
|
|
assert(!Var->hasInitializer());
|
|
|
|
Var->setInitializer(CGM.EmitNullConstant(E->getType()));
|
|
|
|
}
|
2015-03-18 09:06:24 +08:00
|
|
|
LValue RefTempDst = MakeAddrLValue(Object, M->getType());
|
2013-06-14 11:07:01 +08:00
|
|
|
|
2014-10-25 04:23:43 +08:00
|
|
|
switch (getEvaluationKind(E->getType())) {
|
|
|
|
default: llvm_unreachable("expected scalar or aggregate expression");
|
|
|
|
case TEK_Scalar:
|
|
|
|
EmitScalarInit(E, M->getExtendingDecl(), RefTempDst, false);
|
|
|
|
break;
|
|
|
|
case TEK_Aggregate: {
|
|
|
|
CharUnits Alignment = getContext().getTypeAlignInChars(E->getType());
|
|
|
|
EmitAggExpr(E, AggValueSlot::forAddr(Object, Alignment,
|
|
|
|
E->getType().getQualifiers(),
|
|
|
|
AggValueSlot::IsDestructed,
|
|
|
|
AggValueSlot::DoesNotNeedGCBarriers,
|
|
|
|
AggValueSlot::IsNotAliased));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-13 04:42:33 +08:00
|
|
|
|
2014-10-10 12:05:00 +08:00
|
|
|
pushTemporaryCleanup(*this, M, E, Object);
|
2013-06-13 07:38:09 +08:00
|
|
|
return RefTempDst;
|
2013-04-11 08:58:58 +08:00
|
|
|
}
|
|
|
|
|
2013-06-03 08:17:11 +08:00
|
|
|
SmallVector<const Expr *, 2> CommaLHSs;
|
2013-04-11 08:58:58 +08:00
|
|
|
SmallVector<SubobjectAdjustment, 2> Adjustments;
|
2013-06-03 08:17:11 +08:00
|
|
|
E = E->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
|
|
|
|
|
2014-10-25 03:54:32 +08:00
|
|
|
for (const auto &Ignored : CommaLHSs)
|
|
|
|
EmitIgnoredExpr(Ignored);
|
2013-06-03 08:17:11 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *opaque = dyn_cast<OpaqueValueExpr>(E)) {
|
2013-06-13 04:42:33 +08:00
|
|
|
if (opaque->getType()->isRecordType()) {
|
|
|
|
assert(Adjustments.empty());
|
2013-06-13 07:38:09 +08:00
|
|
|
return EmitOpaqueValueLValue(opaque);
|
2013-04-11 08:58:58 +08:00
|
|
|
}
|
|
|
|
}
|
2010-06-28 00:56:04 +08:00
|
|
|
|
2014-10-10 12:05:00 +08:00
|
|
|
// Create and initialize the reference temporary.
|
|
|
|
llvm::Value *Object = createReferenceTemporary(*this, M, E);
|
2014-05-09 08:08:36 +08:00
|
|
|
if (auto *Var = dyn_cast<llvm::GlobalVariable>(Object)) {
|
2015-03-18 09:06:24 +08:00
|
|
|
Object = llvm::ConstantExpr::getBitCast(
|
|
|
|
Var, ConvertTypeForMem(E->getType())->getPointerTo());
|
2015-03-07 21:37:13 +08:00
|
|
|
// If the temporary is a global and has a constant initializer or is a
|
|
|
|
// constant temporary that we promoted to a global, we may have already
|
|
|
|
// initialized it.
|
2013-06-14 11:07:01 +08:00
|
|
|
if (!Var->hasInitializer()) {
|
|
|
|
Var->setInitializer(CGM.EmitNullConstant(E->getType()));
|
|
|
|
EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
|
|
|
|
}
|
2014-10-10 12:05:00 +08:00
|
|
|
pushTemporaryCleanup(*this, M, E, Object);
|
2013-06-13 04:42:33 +08:00
|
|
|
|
|
|
|
// Perform derived-to-base casts and/or field accesses, to get from the
|
|
|
|
// temporary object we created (and, potentially, for which we extended
|
|
|
|
// the lifetime) to the subobject we're binding the reference to.
|
|
|
|
for (unsigned I = Adjustments.size(); I != 0; --I) {
|
|
|
|
SubobjectAdjustment &Adjustment = Adjustments[I-1];
|
|
|
|
switch (Adjustment.Kind) {
|
|
|
|
case SubobjectAdjustment::DerivedToBaseAdjustment:
|
|
|
|
Object =
|
2013-06-13 07:38:09 +08:00
|
|
|
GetAddressOfBaseClass(Object, Adjustment.DerivedToBase.DerivedClass,
|
|
|
|
Adjustment.DerivedToBase.BasePath->path_begin(),
|
|
|
|
Adjustment.DerivedToBase.BasePath->path_end(),
|
2014-10-14 07:59:00 +08:00
|
|
|
/*NullCheckValue=*/ false, E->getExprLoc());
|
2013-06-13 04:42:33 +08:00
|
|
|
break;
|
2011-03-17 06:34:09 +08:00
|
|
|
|
2013-06-13 04:42:33 +08:00
|
|
|
case SubobjectAdjustment::FieldAdjustment: {
|
2013-06-13 07:38:09 +08:00
|
|
|
LValue LV = MakeAddrLValue(Object, E->getType());
|
|
|
|
LV = EmitLValueForField(LV, Adjustment.Field);
|
2013-06-13 04:42:33 +08:00
|
|
|
assert(LV.isSimple() &&
|
|
|
|
"materialized temporary field is not a simple lvalue");
|
|
|
|
Object = LV.getAddress();
|
|
|
|
break;
|
2009-10-15 08:51:46 +08:00
|
|
|
}
|
2013-04-11 08:58:58 +08:00
|
|
|
|
2013-06-13 04:42:33 +08:00
|
|
|
case SubobjectAdjustment::MemberPointerAdjustment: {
|
2013-06-13 07:38:09 +08:00
|
|
|
llvm::Value *Ptr = EmitScalarExpr(Adjustment.Ptr.RHS);
|
|
|
|
Object = CGM.getCXXABI().EmitMemberDataPointerAddress(
|
2014-02-21 07:22:07 +08:00
|
|
|
*this, E, Object, Ptr, Adjustment.Ptr.MPT);
|
2013-06-13 04:42:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-05-20 08:36:58 +08:00
|
|
|
}
|
2009-05-20 10:31:19 +08:00
|
|
|
|
2013-06-13 07:38:09 +08:00
|
|
|
return MakeAddrLValue(Object, M->getType());
|
2010-06-28 00:56:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RValue
|
2013-06-13 07:38:09 +08:00
|
|
|
CodeGenFunction::EmitReferenceBindingToExpr(const Expr *E) {
|
|
|
|
// Emit the expression as an lvalue.
|
|
|
|
LValue LV = EmitLValue(E);
|
|
|
|
assert(LV.isSimple());
|
|
|
|
llvm::Value *Value = LV.getAddress();
|
2013-06-13 04:42:33 +08:00
|
|
|
|
2014-07-08 07:59:57 +08:00
|
|
|
if (sanitizePerformTypeCheck() && !E->getType()->isFunctionType()) {
|
2012-08-24 08:54:33 +08:00
|
|
|
// C++11 [dcl.ref]p5 (as amended by core issue 453):
|
|
|
|
// If a glvalue to which a reference is directly bound designates neither
|
|
|
|
// an existing object or function of an appropriate type nor a region of
|
|
|
|
// storage of suitable size and alignment to contain an object of the
|
|
|
|
// reference's type, the behavior is undefined.
|
|
|
|
QualType Ty = E->getType();
|
2012-10-10 03:52:38 +08:00
|
|
|
EmitTypeCheck(TCK_ReferenceBinding, E->getExprLoc(), Value, Ty);
|
2012-08-24 08:54:33 +08:00
|
|
|
}
|
2010-07-21 14:29:51 +08:00
|
|
|
|
2010-06-28 00:56:04 +08:00
|
|
|
return RValue::get(Value);
|
2009-05-20 08:24:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
/// getAccessedFieldNo - Given an encoded value and a result number, return the
|
|
|
|
/// input field number being accessed.
|
|
|
|
unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
|
2008-05-22 08:50:06 +08:00
|
|
|
const llvm::Constant *Elts) {
|
2012-01-30 14:20:36 +08:00
|
|
|
return cast<llvm::ConstantInt>(Elts->getAggregateElement(Idx))
|
|
|
|
->getZExtValue();
|
2008-05-22 08:50:06 +08:00
|
|
|
}
|
|
|
|
|
2012-10-25 10:14:12 +08:00
|
|
|
/// Emit the hash_16_bytes function from include/llvm/ADT/Hashing.h.
|
|
|
|
static llvm::Value *emitHash16Bytes(CGBuilderTy &Builder, llvm::Value *Low,
|
|
|
|
llvm::Value *High) {
|
|
|
|
llvm::Value *KMul = Builder.getInt64(0x9ddfea08eb382d69ULL);
|
|
|
|
llvm::Value *K47 = Builder.getInt64(47);
|
|
|
|
llvm::Value *A0 = Builder.CreateMul(Builder.CreateXor(Low, High), KMul);
|
|
|
|
llvm::Value *A1 = Builder.CreateXor(Builder.CreateLShr(A0, K47), A0);
|
|
|
|
llvm::Value *B0 = Builder.CreateMul(Builder.CreateXor(High, A1), KMul);
|
|
|
|
llvm::Value *B1 = Builder.CreateXor(Builder.CreateLShr(B0, K47), B0);
|
|
|
|
return Builder.CreateMul(B1, KMul);
|
|
|
|
}
|
|
|
|
|
2014-07-08 07:59:57 +08:00
|
|
|
bool CodeGenFunction::sanitizePerformTypeCheck() const {
|
2014-11-08 06:29:38 +08:00
|
|
|
return SanOpts.has(SanitizerKind::Null) |
|
|
|
|
SanOpts.has(SanitizerKind::Alignment) |
|
|
|
|
SanOpts.has(SanitizerKind::ObjectSize) |
|
|
|
|
SanOpts.has(SanitizerKind::Vptr);
|
2014-07-08 07:59:57 +08:00
|
|
|
}
|
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
|
2014-10-14 07:59:00 +08:00
|
|
|
llvm::Value *Address, QualType Ty,
|
|
|
|
CharUnits Alignment, bool SkipNullCheck) {
|
2014-07-08 07:59:57 +08:00
|
|
|
if (!sanitizePerformTypeCheck())
|
2009-12-16 10:57:00 +08:00
|
|
|
return;
|
|
|
|
|
2012-11-01 15:22:08 +08:00
|
|
|
// Don't check pointers outside the default address space. The null check
|
|
|
|
// isn't correct, the object-size check isn't supported by LLVM, and we can't
|
|
|
|
// communicate the addresses to the runtime handler for the vptr check.
|
|
|
|
if (Address->getType()->getPointerAddressSpace())
|
|
|
|
return;
|
|
|
|
|
2014-07-18 02:46:27 +08:00
|
|
|
SanitizerScope SanScope(this);
|
|
|
|
|
2014-11-12 06:03:54 +08:00
|
|
|
SmallVector<std::pair<llvm::Value *, SanitizerKind>, 3> Checks;
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::BasicBlock *Done = nullptr;
|
2012-08-24 08:54:33 +08:00
|
|
|
|
2014-10-14 07:59:00 +08:00
|
|
|
bool AllowNullPointers = TCK == TCK_DowncastPointer || TCK == TCK_Upcast ||
|
|
|
|
TCK == TCK_UpcastToVirtualBase;
|
2014-11-08 06:29:38 +08:00
|
|
|
if ((SanOpts.has(SanitizerKind::Null) || AllowNullPointers) &&
|
|
|
|
!SkipNullCheck) {
|
2012-11-06 06:21:05 +08:00
|
|
|
// The glvalue must not be an empty glvalue.
|
2014-11-12 06:03:54 +08:00
|
|
|
llvm::Value *IsNonNull = Builder.CreateICmpNE(
|
2012-11-06 06:21:05 +08:00
|
|
|
Address, llvm::Constant::getNullValue(Address->getType()));
|
2013-02-14 05:18:23 +08:00
|
|
|
|
2014-10-14 07:59:00 +08:00
|
|
|
if (AllowNullPointers) {
|
|
|
|
// When performing pointer casts, it's OK if the value is null.
|
2013-02-14 05:18:23 +08:00
|
|
|
// Skip the remaining checks in that case.
|
|
|
|
Done = createBasicBlock("null");
|
|
|
|
llvm::BasicBlock *Rest = createBasicBlock("not.null");
|
2014-11-12 06:03:54 +08:00
|
|
|
Builder.CreateCondBr(IsNonNull, Rest, Done);
|
2013-02-14 05:18:23 +08:00
|
|
|
EmitBlock(Rest);
|
2014-11-11 06:27:30 +08:00
|
|
|
} else {
|
2014-11-12 06:03:54 +08:00
|
|
|
Checks.push_back(std::make_pair(IsNonNull, SanitizerKind::Null));
|
2013-02-14 05:18:23 +08:00
|
|
|
}
|
2012-11-06 06:21:05 +08:00
|
|
|
}
|
2012-10-10 03:52:38 +08:00
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (SanOpts.has(SanitizerKind::ObjectSize) && !Ty->isIncompleteType()) {
|
2012-08-24 08:54:33 +08:00
|
|
|
uint64_t Size = getContext().getTypeSizeInChars(Ty).getQuantity();
|
|
|
|
|
|
|
|
// The glvalue must refer to a large enough storage region.
|
2012-11-06 06:21:05 +08:00
|
|
|
// FIXME: If Address Sanitizer is enabled, insert dynamic instrumentation
|
2012-08-24 08:54:33 +08:00
|
|
|
// to check this.
|
2013-10-08 03:00:18 +08:00
|
|
|
// FIXME: Get object address space
|
|
|
|
llvm::Type *Tys[2] = { IntPtrTy, Int8PtrTy };
|
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, Tys);
|
2012-08-24 08:54:33 +08:00
|
|
|
llvm::Value *Min = Builder.getFalse();
|
2012-11-01 15:22:08 +08:00
|
|
|
llvm::Value *CastAddr = Builder.CreateBitCast(Address, Int8PtrTy);
|
2012-08-24 08:54:33 +08:00
|
|
|
llvm::Value *LargeEnough =
|
2012-11-01 15:22:08 +08:00
|
|
|
Builder.CreateICmpUGE(Builder.CreateCall2(F, CastAddr, Min),
|
2012-08-24 08:54:33 +08:00
|
|
|
llvm::ConstantInt::get(IntPtrTy, Size));
|
2014-11-12 06:03:54 +08:00
|
|
|
Checks.push_back(std::make_pair(LargeEnough, SanitizerKind::ObjectSize));
|
2012-10-10 03:52:38 +08:00
|
|
|
}
|
2012-08-24 08:54:33 +08:00
|
|
|
|
2012-11-06 06:21:05 +08:00
|
|
|
uint64_t AlignVal = 0;
|
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (SanOpts.has(SanitizerKind::Alignment)) {
|
2012-11-06 06:21:05 +08:00
|
|
|
AlignVal = Alignment.getQuantity();
|
|
|
|
if (!Ty->isIncompleteType() && !AlignVal)
|
|
|
|
AlignVal = getContext().getTypeAlignInChars(Ty).getQuantity();
|
|
|
|
|
2012-08-24 08:54:33 +08:00
|
|
|
// The glvalue must be suitably aligned.
|
2012-11-06 06:21:05 +08:00
|
|
|
if (AlignVal) {
|
|
|
|
llvm::Value *Align =
|
|
|
|
Builder.CreateAnd(Builder.CreatePtrToInt(Address, IntPtrTy),
|
|
|
|
llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));
|
|
|
|
llvm::Value *Aligned =
|
|
|
|
Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
|
2014-11-12 06:03:54 +08:00
|
|
|
Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment));
|
2012-11-06 06:21:05 +08:00
|
|
|
}
|
2012-08-24 08:54:33 +08:00
|
|
|
}
|
2010-04-11 02:34:14 +08:00
|
|
|
|
2014-11-12 06:03:54 +08:00
|
|
|
if (Checks.size() > 0) {
|
2012-10-10 03:52:38 +08:00
|
|
|
llvm::Constant *StaticData[] = {
|
|
|
|
EmitCheckSourceLocation(Loc),
|
|
|
|
EmitCheckTypeDescriptor(Ty),
|
|
|
|
llvm::ConstantInt::get(SizeTy, AlignVal),
|
|
|
|
llvm::ConstantInt::get(Int8Ty, TCK)
|
|
|
|
};
|
2014-11-12 06:03:54 +08:00
|
|
|
EmitCheck(Checks, "type_mismatch", StaticData, Address);
|
2012-10-10 03:52:38 +08:00
|
|
|
}
|
2012-10-25 10:14:12 +08:00
|
|
|
|
2012-11-06 06:21:05 +08:00
|
|
|
// If possible, check that the vptr indicates that there is a subobject of
|
|
|
|
// type Ty at offset zero within this object.
|
2012-12-18 08:22:45 +08:00
|
|
|
//
|
|
|
|
// C++11 [basic.life]p5,6:
|
|
|
|
// [For storage which does not refer to an object within its lifetime]
|
|
|
|
// The program has undefined behavior if:
|
|
|
|
// -- the [pointer or glvalue] is used to access a non-static data member
|
2012-12-18 11:04:38 +08:00
|
|
|
// or call a non-static member function
|
2012-10-25 10:14:12 +08:00
|
|
|
CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
|
2014-11-08 06:29:38 +08:00
|
|
|
if (SanOpts.has(SanitizerKind::Vptr) &&
|
2013-02-14 05:18:23 +08:00
|
|
|
(TCK == TCK_MemberAccess || TCK == TCK_MemberCall ||
|
2014-10-14 07:59:00 +08:00
|
|
|
TCK == TCK_DowncastPointer || TCK == TCK_DowncastReference ||
|
|
|
|
TCK == TCK_UpcastToVirtualBase) &&
|
2012-10-25 10:14:12 +08:00
|
|
|
RD && RD->hasDefinition() && RD->isDynamicClass()) {
|
|
|
|
// Compute a hash of the mangled name of the type.
|
|
|
|
//
|
|
|
|
// FIXME: This is not guaranteed to be deterministic! Move to a
|
|
|
|
// fingerprinting mechanism once LLVM provides one. For the time
|
|
|
|
// being the implementation happens to be deterministic.
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<64> MangledName;
|
2012-10-25 10:14:12 +08:00
|
|
|
llvm::raw_svector_ostream Out(MangledName);
|
|
|
|
CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty.getUnqualifiedType(),
|
|
|
|
Out);
|
2014-07-11 06:34:19 +08:00
|
|
|
|
|
|
|
// Blacklist based on the mangled type.
|
SanitizerBlacklist: blacklist functions by their source location.
This commit changes the way we blacklist functions in ASan, TSan,
MSan and UBSan. We used to treat function as "blacklisted"
and turned off instrumentation in it in two cases:
1) Function is explicitly blacklisted by its mangled name.
This part is not changed.
2) Function is located in llvm::Module, whose identifier is
contained in the list of blacklisted sources. This is completely
wrong, as llvm::Module may not correspond to the actual source
file function is defined in. Also, function can be defined in
a header, in which case user had to blacklist the .cpp file
this header was #include'd into, not the header itself.
Such functions could cause other problems - for instance, if the
header was included in multiple source files, compiled
separately and linked into a single executable, we could end up
with both instrumented and non-instrumented version of the same
function participating in the same link.
After this change we will make blacklisting decision based on
the SourceLocation of a function definition. If a function is
not explicitly defined in the source file, (for example, the
function is compiler-generated and responsible for
initialization/destruction of a global variable), then it will
be blacklisted if the corresponding global variable is defined
in blacklisted source file, and will be instrumented otherwise.
After this commit, the active users of blacklist files may have
to revisit them. This is a backwards-incompatible change, but
I don't think it's possible or makes sense to support the
old incorrect behavior.
I plan to make similar change for blacklisting GlobalVariables
(which is ASan-specific).
llvm-svn: 219997
2014-10-17 08:20:19 +08:00
|
|
|
if (!CGM.getContext().getSanitizerBlacklist().isBlacklistedType(
|
|
|
|
Out.str())) {
|
2014-07-11 06:34:19 +08:00
|
|
|
llvm::hash_code TypeHash = hash_value(Out.str());
|
|
|
|
|
|
|
|
// Load the vptr, and compute hash_16_bytes(TypeHash, vptr).
|
|
|
|
llvm::Value *Low = llvm::ConstantInt::get(Int64Ty, TypeHash);
|
|
|
|
llvm::Type *VPtrTy = llvm::PointerType::get(IntPtrTy, 0);
|
|
|
|
llvm::Value *VPtrAddr = Builder.CreateBitCast(Address, VPtrTy);
|
|
|
|
llvm::Value *VPtrVal = Builder.CreateLoad(VPtrAddr);
|
|
|
|
llvm::Value *High = Builder.CreateZExt(VPtrVal, Int64Ty);
|
|
|
|
|
|
|
|
llvm::Value *Hash = emitHash16Bytes(Builder, Low, High);
|
|
|
|
Hash = Builder.CreateTrunc(Hash, IntPtrTy);
|
|
|
|
|
|
|
|
// Look the hash up in our cache.
|
|
|
|
const int CacheSize = 128;
|
|
|
|
llvm::Type *HashTable = llvm::ArrayType::get(IntPtrTy, CacheSize);
|
|
|
|
llvm::Value *Cache = CGM.CreateRuntimeVariable(HashTable,
|
|
|
|
"__ubsan_vptr_type_cache");
|
|
|
|
llvm::Value *Slot = Builder.CreateAnd(Hash,
|
|
|
|
llvm::ConstantInt::get(IntPtrTy,
|
|
|
|
CacheSize-1));
|
|
|
|
llvm::Value *Indices[] = { Builder.getInt32(0), Slot };
|
|
|
|
llvm::Value *CacheVal =
|
|
|
|
Builder.CreateLoad(Builder.CreateInBoundsGEP(Cache, Indices));
|
|
|
|
|
|
|
|
// If the hash isn't in the cache, call a runtime handler to perform the
|
|
|
|
// hard work of checking whether the vptr is for an object of the right
|
|
|
|
// type. This will either fill in the cache and return, or produce a
|
|
|
|
// diagnostic.
|
2014-11-12 06:03:54 +08:00
|
|
|
llvm::Value *EqualHash = Builder.CreateICmpEQ(CacheVal, Hash);
|
2014-07-11 06:34:19 +08:00
|
|
|
llvm::Constant *StaticData[] = {
|
|
|
|
EmitCheckSourceLocation(Loc),
|
|
|
|
EmitCheckTypeDescriptor(Ty),
|
|
|
|
CGM.GetAddrOfRTTIDescriptor(Ty.getUnqualifiedType()),
|
|
|
|
llvm::ConstantInt::get(Int8Ty, TCK)
|
|
|
|
};
|
|
|
|
llvm::Value *DynamicData[] = { Address, Hash };
|
2014-11-12 06:03:54 +08:00
|
|
|
EmitCheck(std::make_pair(EqualHash, SanitizerKind::Vptr),
|
|
|
|
"dynamic_type_cache_miss", StaticData, DynamicData);
|
2014-07-11 06:34:19 +08:00
|
|
|
}
|
2012-10-25 10:14:12 +08:00
|
|
|
}
|
2013-02-14 05:18:23 +08:00
|
|
|
|
|
|
|
if (Done) {
|
|
|
|
Builder.CreateBr(Done);
|
|
|
|
EmitBlock(Done);
|
|
|
|
}
|
2009-12-16 10:57:00 +08:00
|
|
|
}
|
2007-09-01 06:49:20 +08:00
|
|
|
|
2013-02-23 10:53:19 +08:00
|
|
|
/// Determine whether this expression refers to a flexible array member in a
|
|
|
|
/// struct. We disable array bounds checks for such members.
|
|
|
|
static bool isFlexibleArrayMemberExpr(const Expr *E) {
|
|
|
|
// For compatibility with existing code, we treat arrays of length 0 or
|
|
|
|
// 1 as flexible array members.
|
|
|
|
const ArrayType *AT = E->getType()->castAsArrayTypeUnsafe();
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
|
2013-02-23 10:53:19 +08:00
|
|
|
if (CAT->getSize().ugt(1))
|
|
|
|
return false;
|
|
|
|
} else if (!isa<IncompleteArrayType>(AT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
E = E->IgnoreParens();
|
|
|
|
|
|
|
|
// A flexible array member must be the last member in the class.
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *ME = dyn_cast<MemberExpr>(E)) {
|
2013-02-23 10:53:19 +08:00
|
|
|
// FIXME: If the base type of the member expr is not FD->getParent(),
|
|
|
|
// this should not be treated as a flexible array member access.
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
|
2013-02-23 10:53:19 +08:00
|
|
|
RecordDecl::field_iterator FI(
|
|
|
|
DeclContext::decl_iterator(const_cast<FieldDecl *>(FD)));
|
|
|
|
return ++FI == FD->getParent()->field_end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If Base is known to point to the start of an array, return the length of
|
|
|
|
/// that array. Return 0 if the length cannot be determined.
|
2013-03-09 23:15:22 +08:00
|
|
|
static llvm::Value *getArrayIndexingBound(
|
|
|
|
CodeGenFunction &CGF, const Expr *Base, QualType &IndexedType) {
|
2013-02-23 10:53:19 +08:00
|
|
|
// For the vector indexing extension, the bound is the number of elements.
|
|
|
|
if (const VectorType *VT = Base->getType()->getAs<VectorType>()) {
|
|
|
|
IndexedType = Base->getType();
|
|
|
|
return CGF.Builder.getInt32(VT->getNumElements());
|
|
|
|
}
|
|
|
|
|
|
|
|
Base = Base->IgnoreParens();
|
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *CE = dyn_cast<CastExpr>(Base)) {
|
2013-02-23 10:53:19 +08:00
|
|
|
if (CE->getCastKind() == CK_ArrayToPointerDecay &&
|
|
|
|
!isFlexibleArrayMemberExpr(CE->getSubExpr())) {
|
|
|
|
IndexedType = CE->getSubExpr()->getType();
|
|
|
|
const ArrayType *AT = IndexedType->castAsArrayTypeUnsafe();
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *CAT = dyn_cast<ConstantArrayType>(AT))
|
2013-02-23 10:53:19 +08:00
|
|
|
return CGF.Builder.getInt(CAT->getSize());
|
2014-05-09 08:08:36 +08:00
|
|
|
else if (const auto *VAT = dyn_cast<VariableArrayType>(AT))
|
2013-02-23 10:53:19 +08:00
|
|
|
return CGF.getVLASize(VAT).first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2013-02-23 10:53:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitBoundsCheck(const Expr *E, const Expr *Base,
|
|
|
|
llvm::Value *Index, QualType IndexType,
|
|
|
|
bool Accessed) {
|
2014-11-08 06:29:38 +08:00
|
|
|
assert(SanOpts.has(SanitizerKind::ArrayBounds) &&
|
2013-10-23 06:51:04 +08:00
|
|
|
"should not be called unless adding bounds checks");
|
2014-07-18 02:46:27 +08:00
|
|
|
SanitizerScope SanScope(this);
|
2013-02-24 09:56:24 +08:00
|
|
|
|
2013-02-23 10:53:19 +08:00
|
|
|
QualType IndexedType;
|
|
|
|
llvm::Value *Bound = getArrayIndexingBound(*this, Base, IndexedType);
|
|
|
|
if (!Bound)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool IndexSigned = IndexType->isSignedIntegerOrEnumerationType();
|
|
|
|
llvm::Value *IndexVal = Builder.CreateIntCast(Index, SizeTy, IndexSigned);
|
|
|
|
llvm::Value *BoundVal = Builder.CreateIntCast(Bound, SizeTy, false);
|
|
|
|
|
|
|
|
llvm::Constant *StaticData[] = {
|
|
|
|
EmitCheckSourceLocation(E->getExprLoc()),
|
|
|
|
EmitCheckTypeDescriptor(IndexedType),
|
|
|
|
EmitCheckTypeDescriptor(IndexType)
|
|
|
|
};
|
|
|
|
llvm::Value *Check = Accessed ? Builder.CreateICmpULT(IndexVal, BoundVal)
|
|
|
|
: Builder.CreateICmpULE(IndexVal, BoundVal);
|
2014-11-12 06:03:54 +08:00
|
|
|
EmitCheck(std::make_pair(Check, SanitizerKind::ArrayBounds), "out_of_bounds",
|
|
|
|
StaticData, Index);
|
2013-02-23 10:53:19 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 05:40:03 +08:00
|
|
|
|
|
|
|
CodeGenFunction::ComplexPairTy CodeGenFunction::
|
|
|
|
EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
|
|
|
|
bool isInc, bool isPre) {
|
2013-10-02 10:29:49 +08:00
|
|
|
ComplexPairTy InVal = EmitLoadOfComplex(LV, E->getExprLoc());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-01-10 05:40:03 +08:00
|
|
|
llvm::Value *NextVal;
|
|
|
|
if (isa<llvm::IntegerType>(InVal.first->getType())) {
|
|
|
|
uint64_t AmountVal = isInc ? 1 : -1;
|
|
|
|
NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-01-10 05:40:03 +08:00
|
|
|
// Add the inc/dec to the real part.
|
|
|
|
NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
|
|
|
|
} else {
|
|
|
|
QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
|
|
|
|
llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
|
|
|
|
if (!isInc)
|
|
|
|
FVal.changeSign();
|
|
|
|
NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-01-10 05:40:03 +08:00
|
|
|
// Add the inc/dec to the real part.
|
|
|
|
NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
|
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-01-10 05:40:03 +08:00
|
|
|
ComplexPairTy IncVal(NextVal, InVal.second);
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-01-10 05:40:03 +08:00
|
|
|
// Store the updated result through the lvalue.
|
2013-03-08 05:37:08 +08:00
|
|
|
EmitStoreOfComplex(IncVal, LV, /*init*/ false);
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-01-10 05:40:03 +08:00
|
|
|
// If this is a postinc, return the value read from memory, otherwise use the
|
|
|
|
// updated value.
|
|
|
|
return isPre ? IncVal : InVal;
|
|
|
|
}
|
|
|
|
|
2007-06-03 03:47:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-02 13:24:33 +08:00
|
|
|
// LValue Expression Emission
|
2007-06-03 03:47:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-06-02 13:24:33 +08:00
|
|
|
|
2009-02-05 15:09:07 +08:00
|
|
|
RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
|
2009-10-29 01:39:19 +08:00
|
|
|
if (Ty->isVoidType())
|
2014-05-21 13:09:00 +08:00
|
|
|
return RValue::get(nullptr);
|
2013-03-08 05:37:08 +08:00
|
|
|
|
|
|
|
switch (getEvaluationKind(Ty)) {
|
|
|
|
case TEK_Complex: {
|
|
|
|
llvm::Type *EltTy =
|
|
|
|
ConvertType(Ty->castAs<ComplexType>()->getElementType());
|
2009-07-31 07:11:26 +08:00
|
|
|
llvm::Value *U = llvm::UndefValue::get(EltTy);
|
2009-01-10 04:09:28 +08:00
|
|
|
return RValue::getComplex(std::make_pair(U, U));
|
2009-10-29 01:39:19 +08:00
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-08-23 13:26:13 +08:00
|
|
|
// If this is a use of an undefined aggregate type, the aggregate must have an
|
|
|
|
// identifiable address. Just because the contents of the value are undefined
|
|
|
|
// doesn't mean that the address can't be taken and compared.
|
2013-03-08 05:37:08 +08:00
|
|
|
case TEK_Aggregate: {
|
2010-08-23 13:26:13 +08:00
|
|
|
llvm::Value *DestPtr = CreateMemTemp(Ty, "undef.agg.tmp");
|
|
|
|
return RValue::getAggregate(DestPtr);
|
2009-01-10 04:09:28 +08:00
|
|
|
}
|
2013-03-08 05:37:08 +08:00
|
|
|
|
|
|
|
case TEK_Scalar:
|
|
|
|
return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
|
|
|
|
}
|
|
|
|
llvm_unreachable("bad evaluation kind");
|
2009-01-10 00:50:52 +08:00
|
|
|
}
|
|
|
|
|
2009-02-05 15:09:07 +08:00
|
|
|
RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
|
|
|
|
const char *Name) {
|
|
|
|
ErrorUnsupported(E, Name);
|
|
|
|
return GetUndefRValue(E->getType());
|
|
|
|
}
|
|
|
|
|
2008-08-26 04:45:57 +08:00
|
|
|
LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
|
|
|
|
const char *Name) {
|
|
|
|
ErrorUnsupported(E, Name);
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(llvm::UndefValue::get(Ty), E->getType());
|
2008-08-26 04:45:57 +08:00
|
|
|
}
|
|
|
|
|
2012-09-08 10:08:36 +08:00
|
|
|
LValue CodeGenFunction::EmitCheckedLValue(const Expr *E, TypeCheckKind TCK) {
|
2013-02-23 10:53:19 +08:00
|
|
|
LValue LV;
|
2014-11-08 06:29:38 +08:00
|
|
|
if (SanOpts.has(SanitizerKind::ArrayBounds) && isa<ArraySubscriptExpr>(E))
|
2013-02-23 10:53:19 +08:00
|
|
|
LV = EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E), /*Accessed*/true);
|
|
|
|
else
|
|
|
|
LV = EmitLValue(E);
|
2010-04-06 05:36:35 +08:00
|
|
|
if (!isa<DeclRefExpr>(E) && !LV.isBitField() && LV.isSimple())
|
2012-10-10 03:52:38 +08:00
|
|
|
EmitTypeCheck(TCK, E->getExprLoc(), LV.getAddress(),
|
|
|
|
E->getType(), LV.getAlignment());
|
2009-12-16 10:57:00 +08:00
|
|
|
return LV;
|
|
|
|
}
|
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
/// EmitLValue - Emit code to compute a designator that specifies the location
|
|
|
|
/// of the expression.
|
|
|
|
///
|
2009-09-09 21:00:44 +08:00
|
|
|
/// This can return one of two things: a simple address or a bitfield reference.
|
|
|
|
/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
|
|
|
|
/// an LLVM pointer type.
|
2007-06-06 04:53:16 +08:00
|
|
|
///
|
2009-09-09 21:00:44 +08:00
|
|
|
/// If this returns a bitfield reference, nothing about the pointee type of the
|
|
|
|
/// LLVM value is known: For example, it may not be a pointer to an integer.
|
2007-06-06 04:53:16 +08:00
|
|
|
///
|
2009-09-09 21:00:44 +08:00
|
|
|
/// If this returns a normal address, and if the lvalue's C type is fixed size,
|
|
|
|
/// this method guarantees that the returned pointer type will point to an LLVM
|
|
|
|
/// type of the same size of the lvalue's type. If the lvalue has a variable
|
|
|
|
/// length type, this is not possible.
|
2007-06-06 04:53:16 +08:00
|
|
|
///
|
2007-06-02 13:24:33 +08:00
|
|
|
LValue CodeGenFunction::EmitLValue(const Expr *E) {
|
DebugInfo: Use the preferred location rather than the start location for expression line info
This causes things like assignment to refer to the '=' rather than the
LHS when attributing the store instruction, for example.
There were essentially 3 options for this:
* The beginning of an expression (this was the behavior prior to this
commit). This meant that stepping through subexpressions would bounce
around from subexpressions back to the start of the outer expression,
etc. (eg: x + y + z would go x, y, x, z, x (the repeated 'x's would be
where the actual addition occurred)).
* The end of an expression. This seems to be what GCC does /mostly/, and
certainly this for function calls. This has the advantage that
progress is always 'forwards' (never jumping backwards - except for
independent subexpressions if they're evaluated in interesting orders,
etc). "x + y + z" would go "x y z" with the additions occurring at y
and z after the respective loads.
The problem with this is that the user would still have to think
fairly hard about precedence to realize which subexpression is being
evaluated or which operator overload is being called in, say, an asan
backtrace.
* The preferred location or 'exprloc'. In this case you get sort of what
you'd expect, though it's a bit confusing in its own way due to going
'backwards'. In this case the locations would be: "x y + z +" in
lovely postfix arithmetic order. But this does mean that if the op+
were an operator overload, say, and in a backtrace, the backtrace will
point to the exact '+' that's being called, not to the end of one of
its operands.
(actually the operator overload case doesn't work yet for other reasons,
but that's being fixed - but this at least gets scalar/complex
assignments and other plain operators right)
llvm-svn: 227027
2015-01-25 09:19:10 +08:00
|
|
|
ApplyDebugLocation DL(*this, E);
|
2007-06-02 13:24:33 +08:00
|
|
|
switch (E->getStmtClass()) {
|
2008-08-26 04:45:57 +08:00
|
|
|
default: return EmitUnsupportedLValue(E, "l-value expression");
|
2007-06-02 13:24:33 +08:00
|
|
|
|
2011-11-07 11:59:57 +08:00
|
|
|
case Expr::ObjCPropertyRefExprClass:
|
|
|
|
llvm_unreachable("cannot emit a property reference directly");
|
|
|
|
|
2010-06-18 03:56:20 +08:00
|
|
|
case Expr::ObjCSelectorExprClass:
|
2012-10-11 18:13:44 +08:00
|
|
|
return EmitObjCSelectorLValue(cast<ObjCSelectorExpr>(E));
|
2009-12-10 07:35:29 +08:00
|
|
|
case Expr::ObjCIsaExprClass:
|
|
|
|
return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
|
2009-09-09 21:00:44 +08:00
|
|
|
case Expr::BinaryOperatorClass:
|
2008-09-04 11:20:13 +08:00
|
|
|
return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
|
2015-02-14 09:48:17 +08:00
|
|
|
case Expr::CompoundAssignOperatorClass: {
|
|
|
|
QualType Ty = E->getType();
|
|
|
|
if (const AtomicType *AT = Ty->getAs<AtomicType>())
|
|
|
|
Ty = AT->getValueType();
|
|
|
|
if (!Ty->isAnyComplexType())
|
2010-12-05 10:00:02 +08:00
|
|
|
return EmitCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
|
|
|
|
return EmitComplexCompoundAssignmentLValue(cast<CompoundAssignOperator>(E));
|
2015-02-14 09:48:17 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
case Expr::CallExprClass:
|
2009-09-02 05:18:52 +08:00
|
|
|
case Expr::CXXMemberCallExprClass:
|
2008-11-15 00:09:21 +08:00
|
|
|
case Expr::CXXOperatorCallExprClass:
|
2012-03-07 16:35:16 +08:00
|
|
|
case Expr::UserDefinedLiteralClass:
|
2008-11-15 00:09:21 +08:00
|
|
|
return EmitCallExprLValue(cast<CallExpr>(E));
|
2009-02-12 04:59:32 +08:00
|
|
|
case Expr::VAArgExprClass:
|
|
|
|
return EmitVAArgExprLValue(cast<VAArgExpr>(E));
|
2009-09-09 21:00:44 +08:00
|
|
|
case Expr::DeclRefExprClass:
|
2009-01-06 13:10:23 +08:00
|
|
|
return EmitDeclRefLValue(cast<DeclRefExpr>(E));
|
2011-09-09 01:15:04 +08:00
|
|
|
case Expr::ParenExprClass:
|
|
|
|
return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
|
2011-04-15 08:35:48 +08:00
|
|
|
case Expr::GenericSelectionExprClass:
|
|
|
|
return EmitLValue(cast<GenericSelectionExpr>(E)->getResultExpr());
|
2008-08-10 09:53:14 +08:00
|
|
|
case Expr::PredefinedExprClass:
|
|
|
|
return EmitPredefinedLValue(cast<PredefinedExpr>(E));
|
2007-06-06 12:54:52 +08:00
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
return EmitStringLiteralLValue(cast<StringLiteral>(E));
|
2009-02-25 06:18:39 +08:00
|
|
|
case Expr::ObjCEncodeExprClass:
|
|
|
|
return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
|
2011-11-06 17:01:30 +08:00
|
|
|
case Expr::PseudoObjectExprClass:
|
|
|
|
return EmitPseudoObjectLValue(cast<PseudoObjectExpr>(E));
|
2011-11-28 00:50:07 +08:00
|
|
|
case Expr::InitListExprClass:
|
2012-05-15 05:57:21 +08:00
|
|
|
return EmitInitListLValue(cast<InitListExpr>(E));
|
2009-05-31 07:23:33 +08:00
|
|
|
case Expr::CXXTemporaryObjectExprClass:
|
|
|
|
case Expr::CXXConstructExprClass:
|
2009-05-31 07:30:54 +08:00
|
|
|
return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
|
|
|
|
case Expr::CXXBindTemporaryExprClass:
|
|
|
|
return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
|
2012-10-11 18:13:44 +08:00
|
|
|
case Expr::CXXUuidofExprClass:
|
|
|
|
return EmitCXXUuidofLValue(cast<CXXUuidofExpr>(E));
|
2012-02-08 13:34:55 +08:00
|
|
|
case Expr::LambdaExprClass:
|
|
|
|
return EmitLambdaLValue(cast<LambdaExpr>(E));
|
2011-11-10 16:15:53 +08:00
|
|
|
|
|
|
|
case Expr::ExprWithCleanupsClass: {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *cleanups = cast<ExprWithCleanups>(E);
|
2011-11-10 16:15:53 +08:00
|
|
|
enterFullExpression(cleanups);
|
|
|
|
RunCleanupsScope Scope(*this);
|
|
|
|
return EmitLValue(cleanups->getSubExpr());
|
|
|
|
}
|
|
|
|
|
2009-11-14 09:51:50 +08:00
|
|
|
case Expr::CXXDefaultArgExprClass:
|
|
|
|
return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
|
2013-04-21 06:23:05 +08:00
|
|
|
case Expr::CXXDefaultInitExprClass: {
|
|
|
|
CXXDefaultInitExprScope Scope(*this);
|
|
|
|
return EmitLValue(cast<CXXDefaultInitExpr>(E)->getExpr());
|
|
|
|
}
|
2009-11-15 16:09:41 +08:00
|
|
|
case Expr::CXXTypeidExprClass:
|
|
|
|
return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
|
2009-05-31 07:30:54 +08:00
|
|
|
|
2008-08-23 18:51:21 +08:00
|
|
|
case Expr::ObjCMessageExprClass:
|
|
|
|
return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
|
2009-09-09 21:00:44 +08:00
|
|
|
case Expr::ObjCIvarRefExprClass:
|
2008-03-31 07:03:07 +08:00
|
|
|
return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
|
2009-04-26 03:35:26 +08:00
|
|
|
case Expr::StmtExprClass:
|
|
|
|
return EmitStmtExprLValue(cast<StmtExpr>(E));
|
2009-09-09 21:00:44 +08:00
|
|
|
case Expr::UnaryOperatorClass:
|
2007-06-06 04:53:16 +08:00
|
|
|
return EmitUnaryOpLValue(cast<UnaryOperator>(E));
|
2007-06-09 07:31:14 +08:00
|
|
|
case Expr::ArraySubscriptExprClass:
|
|
|
|
return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
|
2008-04-19 07:10:10 +08:00
|
|
|
case Expr::ExtVectorElementExprClass:
|
|
|
|
return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
|
2009-09-09 21:00:44 +08:00
|
|
|
case Expr::MemberExprClass:
|
When a member reference expression includes a qualifier on the member
name, e.g.,
x->Base::f()
retain the qualifier (and its source range information) in a new
subclass of MemberExpr called CXXQualifiedMemberExpr. Provide
construction, transformation, profiling, printing, etc., for this new
expression type.
When a virtual function is called via a qualified name, don't emit a
virtual call. Instead, call that function directly. Mike, could you
add a CodeGen test for this, too?
llvm-svn: 80167
2009-08-27 06:36:53 +08:00
|
|
|
return EmitMemberExpr(cast<MemberExpr>(E));
|
2008-05-14 07:18:27 +08:00
|
|
|
case Expr::CompoundLiteralExprClass:
|
|
|
|
return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
|
2009-03-24 10:38:23 +08:00
|
|
|
case Expr::ConditionalOperatorClass:
|
2009-09-16 00:35:24 +08:00
|
|
|
return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
|
2011-02-17 18:25:35 +08:00
|
|
|
case Expr::BinaryConditionalOperatorClass:
|
|
|
|
return EmitConditionalOperatorLValue(cast<BinaryConditionalOperator>(E));
|
2008-12-12 13:35:08 +08:00
|
|
|
case Expr::ChooseExprClass:
|
2013-07-20 08:40:58 +08:00
|
|
|
return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr());
|
2011-02-16 16:02:54 +08:00
|
|
|
case Expr::OpaqueValueExprClass:
|
|
|
|
return EmitOpaqueValueLValue(cast<OpaqueValueExpr>(E));
|
2011-07-15 13:09:51 +08:00
|
|
|
case Expr::SubstNonTypeTemplateParmExprClass:
|
|
|
|
return EmitLValue(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
|
2009-03-18 12:02:57 +08:00
|
|
|
case Expr::ImplicitCastExprClass:
|
|
|
|
case Expr::CStyleCastExprClass:
|
|
|
|
case Expr::CXXFunctionalCastExprClass:
|
|
|
|
case Expr::CXXStaticCastExprClass:
|
|
|
|
case Expr::CXXDynamicCastExprClass:
|
|
|
|
case Expr::CXXReinterpretCastExprClass:
|
|
|
|
case Expr::CXXConstCastExprClass:
|
2011-06-16 07:02:42 +08:00
|
|
|
case Expr::ObjCBridgedCastExprClass:
|
2009-03-19 02:28:57 +08:00
|
|
|
return EmitCastLValue(cast<CastExpr>(E));
|
2011-11-28 00:50:07 +08:00
|
|
|
|
2011-06-22 01:03:29 +08:00
|
|
|
case Expr::MaterializeTemporaryExprClass:
|
|
|
|
return EmitMaterializeTemporaryExpr(cast<MaterializeTemporaryExpr>(E));
|
2007-06-02 13:24:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 11:05:10 +08:00
|
|
|
/// Given an object of the given canonical type, can we safely copy a
|
|
|
|
/// value out of it based on its initializer?
|
|
|
|
static bool isConstantEmittableObjectType(QualType type) {
|
|
|
|
assert(type.isCanonical());
|
|
|
|
assert(!type->isReferenceType());
|
|
|
|
|
|
|
|
// Must be const-qualified but non-volatile.
|
|
|
|
Qualifiers qs = type.getLocalQualifiers();
|
|
|
|
if (!qs.hasConst() || qs.hasVolatile()) return false;
|
|
|
|
|
|
|
|
// Otherwise, all object types satisfy this except C++ classes with
|
|
|
|
// mutable subobjects or non-trivial copy/destroy behavior.
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *RT = dyn_cast<RecordType>(type))
|
|
|
|
if (const auto *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
|
2012-03-10 11:05:10 +08:00
|
|
|
if (RD->hasMutableFields() || !RD->isTrivial())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Can we constant-emit a load of a reference to a variable of the
|
|
|
|
/// given type? This is different from predicates like
|
|
|
|
/// Decl::isUsableInConstantExpressions because we do want it to apply
|
|
|
|
/// in situations that don't necessarily satisfy the language's rules
|
|
|
|
/// for this (e.g. C++'s ODR-use rules). For example, we want to able
|
|
|
|
/// to do this with const float variables even if those variables
|
|
|
|
/// aren't marked 'constexpr'.
|
|
|
|
enum ConstantEmissionKind {
|
|
|
|
CEK_None,
|
|
|
|
CEK_AsReferenceOnly,
|
|
|
|
CEK_AsValueOrReference,
|
|
|
|
CEK_AsValueOnly
|
|
|
|
};
|
|
|
|
static ConstantEmissionKind checkVarTypeForConstantEmission(QualType type) {
|
|
|
|
type = type.getCanonicalType();
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *ref = dyn_cast<ReferenceType>(type)) {
|
2012-03-10 11:05:10 +08:00
|
|
|
if (isConstantEmittableObjectType(ref->getPointeeType()))
|
|
|
|
return CEK_AsValueOrReference;
|
|
|
|
return CEK_AsReferenceOnly;
|
|
|
|
}
|
|
|
|
if (isConstantEmittableObjectType(type))
|
|
|
|
return CEK_AsValueOnly;
|
|
|
|
return CEK_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to emit a reference to the given value without producing it as
|
|
|
|
/// an l-value. This is actually more than an optimization: we can't
|
|
|
|
/// produce an l-value for variables that we never actually captured
|
|
|
|
/// in a block or lambda, which means const int variables or constexpr
|
|
|
|
/// literals or similar.
|
|
|
|
CodeGenFunction::ConstantEmission
|
2012-03-10 17:33:50 +08:00
|
|
|
CodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) {
|
|
|
|
ValueDecl *value = refExpr->getDecl();
|
|
|
|
|
2012-03-10 11:05:10 +08:00
|
|
|
// The value needs to be an enum constant or a constant variable.
|
|
|
|
ConstantEmissionKind CEK;
|
|
|
|
if (isa<ParmVarDecl>(value)) {
|
|
|
|
CEK = CEK_None;
|
2014-05-09 08:08:36 +08:00
|
|
|
} else if (auto *var = dyn_cast<VarDecl>(value)) {
|
2012-03-10 11:05:10 +08:00
|
|
|
CEK = checkVarTypeForConstantEmission(var->getType());
|
|
|
|
} else if (isa<EnumConstantDecl>(value)) {
|
|
|
|
CEK = CEK_AsValueOnly;
|
|
|
|
} else {
|
|
|
|
CEK = CEK_None;
|
|
|
|
}
|
|
|
|
if (CEK == CEK_None) return ConstantEmission();
|
|
|
|
|
|
|
|
Expr::EvalResult result;
|
|
|
|
bool resultIsReference;
|
|
|
|
QualType resultType;
|
|
|
|
|
|
|
|
// It's best to evaluate all the way as an r-value if that's permitted.
|
|
|
|
if (CEK != CEK_AsReferenceOnly &&
|
2012-03-10 17:33:50 +08:00
|
|
|
refExpr->EvaluateAsRValue(result, getContext())) {
|
2012-03-10 11:05:10 +08:00
|
|
|
resultIsReference = false;
|
|
|
|
resultType = refExpr->getType();
|
|
|
|
|
|
|
|
// Otherwise, try to evaluate as an l-value.
|
|
|
|
} else if (CEK != CEK_AsValueOnly &&
|
2012-03-10 17:33:50 +08:00
|
|
|
refExpr->EvaluateAsLValue(result, getContext())) {
|
2012-03-10 11:05:10 +08:00
|
|
|
resultIsReference = true;
|
|
|
|
resultType = value->getType();
|
|
|
|
|
|
|
|
// Failure.
|
|
|
|
} else {
|
|
|
|
return ConstantEmission();
|
|
|
|
}
|
|
|
|
|
|
|
|
// In any case, if the initializer has side-effects, abandon ship.
|
|
|
|
if (result.HasSideEffects)
|
|
|
|
return ConstantEmission();
|
|
|
|
|
|
|
|
// Emit as a constant.
|
|
|
|
llvm::Constant *C = CGM.EmitConstantValue(result.Val, resultType, this);
|
|
|
|
|
2013-08-30 16:53:09 +08:00
|
|
|
// Make sure we emit a debug reference to the global variable.
|
|
|
|
// This should probably fire even for
|
|
|
|
if (isa<VarDecl>(value)) {
|
|
|
|
if (!getContext().DeclMustBeEmitted(cast<VarDecl>(value)))
|
|
|
|
EmitDeclRefExprDbgValue(refExpr, C);
|
|
|
|
} else {
|
|
|
|
assert(isa<EnumConstantDecl>(value));
|
|
|
|
EmitDeclRefExprDbgValue(refExpr, C);
|
|
|
|
}
|
2012-03-10 11:05:10 +08:00
|
|
|
|
|
|
|
// If we emitted a reference constant, we need to dereference that.
|
|
|
|
if (resultIsReference)
|
|
|
|
return ConstantEmission::forReference(C);
|
|
|
|
|
|
|
|
return ConstantEmission::forValue(C);
|
|
|
|
}
|
|
|
|
|
2013-10-02 10:29:49 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue,
|
|
|
|
SourceLocation Loc) {
|
2011-06-16 12:16:24 +08:00
|
|
|
return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(),
|
2011-12-03 12:14:32 +08:00
|
|
|
lvalue.getAlignment().getQuantity(),
|
2013-10-02 10:29:49 +08:00
|
|
|
lvalue.getType(), Loc, lvalue.getTBAAInfo(),
|
2013-04-05 05:53:22 +08:00
|
|
|
lvalue.getTBAABaseType(), lvalue.getTBAAOffset());
|
2011-06-16 12:16:24 +08:00
|
|
|
}
|
|
|
|
|
2012-03-25 00:50:34 +08:00
|
|
|
static bool hasBooleanRepresentation(QualType Ty) {
|
|
|
|
if (Ty->isBooleanType())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (const EnumType *ET = Ty->getAs<EnumType>())
|
|
|
|
return ET->getDecl()->getIntegerType()->isBooleanType();
|
|
|
|
|
2012-04-13 04:42:30 +08:00
|
|
|
if (const AtomicType *AT = Ty->getAs<AtomicType>())
|
|
|
|
return hasBooleanRepresentation(AT->getValueType());
|
|
|
|
|
2012-03-25 00:50:34 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-13 15:11:50 +08:00
|
|
|
static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
|
|
|
|
llvm::APInt &Min, llvm::APInt &End,
|
|
|
|
bool StrictEnums) {
|
2012-03-25 00:50:34 +08:00
|
|
|
const EnumType *ET = Ty->getAs<EnumType>();
|
2012-12-13 15:11:50 +08:00
|
|
|
bool IsRegularCPlusPlusEnum = CGF.getLangOpts().CPlusPlus && StrictEnums &&
|
|
|
|
ET && !ET->getDecl()->isFixed();
|
2012-03-25 00:50:34 +08:00
|
|
|
bool IsBool = hasBooleanRepresentation(Ty);
|
|
|
|
if (!IsBool && !IsRegularCPlusPlusEnum)
|
2012-12-13 15:11:50 +08:00
|
|
|
return false;
|
2012-03-25 00:50:34 +08:00
|
|
|
|
|
|
|
if (IsBool) {
|
2012-12-13 15:11:50 +08:00
|
|
|
Min = llvm::APInt(CGF.getContext().getTypeSize(Ty), 0);
|
|
|
|
End = llvm::APInt(CGF.getContext().getTypeSize(Ty), 2);
|
2012-03-25 00:50:34 +08:00
|
|
|
} else {
|
|
|
|
const EnumDecl *ED = ET->getDecl();
|
2012-12-13 15:11:50 +08:00
|
|
|
llvm::Type *LTy = CGF.ConvertTypeForMem(ED->getIntegerType());
|
2012-03-25 00:50:34 +08:00
|
|
|
unsigned Bitwidth = LTy->getScalarSizeInBits();
|
|
|
|
unsigned NumNegativeBits = ED->getNumNegativeBits();
|
|
|
|
unsigned NumPositiveBits = ED->getNumPositiveBits();
|
|
|
|
|
|
|
|
if (NumNegativeBits) {
|
|
|
|
unsigned NumBits = std::max(NumNegativeBits, NumPositiveBits + 1);
|
|
|
|
assert(NumBits <= Bitwidth);
|
|
|
|
End = llvm::APInt(Bitwidth, 1) << (NumBits - 1);
|
|
|
|
Min = -End;
|
|
|
|
} else {
|
|
|
|
assert(NumPositiveBits <= Bitwidth);
|
|
|
|
End = llvm::APInt(Bitwidth, 1) << NumPositiveBits;
|
|
|
|
Min = llvm::APInt(Bitwidth, 0);
|
|
|
|
}
|
|
|
|
}
|
2012-12-13 15:11:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::MDNode *CodeGenFunction::getRangeForLoadFromType(QualType Ty) {
|
|
|
|
llvm::APInt Min, End;
|
|
|
|
if (!getRangeForType(*this, Ty, Min, End,
|
|
|
|
CGM.getCodeGenOpts().StrictEnums))
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2012-03-25 00:50:34 +08:00
|
|
|
|
2012-04-16 02:04:54 +08:00
|
|
|
llvm::MDBuilder MDHelper(getLLVMContext());
|
2012-04-17 00:29:47 +08:00
|
|
|
return MDHelper.createRange(Min, End);
|
2012-03-25 00:50:34 +08:00
|
|
|
}
|
|
|
|
|
2009-02-10 08:57:50 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
|
2013-10-02 10:29:49 +08:00
|
|
|
unsigned Alignment, QualType Ty,
|
|
|
|
SourceLocation Loc,
|
|
|
|
llvm::MDNode *TBAAInfo,
|
|
|
|
QualType TBAABaseType,
|
|
|
|
uint64_t TBAAOffset) {
|
2012-08-16 08:10:13 +08:00
|
|
|
// For better performance, handle vector loads differently.
|
|
|
|
if (Ty->isVectorType()) {
|
|
|
|
llvm::Value *V;
|
|
|
|
const llvm::Type *EltTy =
|
|
|
|
cast<llvm::PointerType>(Addr->getType())->getElementType();
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *VTy = cast<llvm::VectorType>(EltTy);
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2012-08-16 08:10:13 +08:00
|
|
|
// Handle vectors of size 3, like size 4 for better performance.
|
|
|
|
if (VTy->getNumElements() == 3) {
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2012-08-16 08:10:13 +08:00
|
|
|
// Bitcast to vec4 type.
|
|
|
|
llvm::VectorType *vec4Ty = llvm::VectorType::get(VTy->getElementType(),
|
|
|
|
4);
|
|
|
|
llvm::PointerType *ptVec4Ty =
|
|
|
|
llvm::PointerType::get(vec4Ty,
|
|
|
|
(cast<llvm::PointerType>(
|
|
|
|
Addr->getType()))->getAddressSpace());
|
|
|
|
llvm::Value *Cast = Builder.CreateBitCast(Addr, ptVec4Ty,
|
|
|
|
"castToVec4");
|
|
|
|
// Now load value.
|
|
|
|
llvm::Value *LoadVal = Builder.CreateLoad(Cast, Volatile, "loadVec4");
|
2012-12-13 13:41:48 +08:00
|
|
|
|
2012-08-16 08:10:13 +08:00
|
|
|
// Shuffle vector to get vec3.
|
2012-12-13 13:41:48 +08:00
|
|
|
llvm::Constant *Mask[] = {
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 0),
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 1),
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(getLLVMContext()), 2)
|
|
|
|
};
|
|
|
|
|
2012-08-16 08:10:13 +08:00
|
|
|
llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
|
|
|
|
V = Builder.CreateShuffleVector(LoadVal,
|
|
|
|
llvm::UndefValue::get(vec4Ty),
|
|
|
|
MaskV, "extractVec");
|
|
|
|
return EmitFromMemory(V, Ty);
|
|
|
|
}
|
|
|
|
}
|
2013-03-08 05:37:17 +08:00
|
|
|
|
|
|
|
// Atomic operations have to be done on integral types.
|
2015-02-14 09:35:12 +08:00
|
|
|
if (Ty->isAtomicType() || typeIsSuitableForInlineAtomic(Ty, Volatile)) {
|
2013-03-08 05:37:17 +08:00
|
|
|
LValue lvalue = LValue::MakeAddr(Addr, Ty,
|
|
|
|
CharUnits::fromQuantity(Alignment),
|
|
|
|
getContext(), TBAAInfo);
|
2015-02-14 10:18:14 +08:00
|
|
|
return EmitAtomicLoad(lvalue, Loc).getScalarVal();
|
2013-03-08 05:37:17 +08:00
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2011-09-28 05:06:10 +08:00
|
|
|
llvm::LoadInst *Load = Builder.CreateLoad(Addr);
|
2009-11-30 05:23:36 +08:00
|
|
|
if (Volatile)
|
|
|
|
Load->setVolatile(true);
|
2010-08-21 10:24:36 +08:00
|
|
|
if (Alignment)
|
|
|
|
Load->setAlignment(Alignment);
|
2013-04-05 05:53:22 +08:00
|
|
|
if (TBAAInfo) {
|
|
|
|
llvm::MDNode *TBAAPath = CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo,
|
|
|
|
TBAAOffset);
|
2013-10-08 08:08:49 +08:00
|
|
|
if (TBAAPath)
|
|
|
|
CGM.DecorateInstruction(Load, TBAAPath, false/*ConvertTypeToTag*/);
|
2013-04-05 05:53:22 +08:00
|
|
|
}
|
2009-02-10 08:57:50 +08:00
|
|
|
|
2014-11-11 06:27:30 +08:00
|
|
|
bool NeedsBoolCheck =
|
|
|
|
SanOpts.has(SanitizerKind::Bool) && hasBooleanRepresentation(Ty);
|
|
|
|
bool NeedsEnumCheck =
|
|
|
|
SanOpts.has(SanitizerKind::Enum) && Ty->getAs<EnumType>();
|
|
|
|
if (NeedsBoolCheck || NeedsEnumCheck) {
|
2014-07-18 02:46:27 +08:00
|
|
|
SanitizerScope SanScope(this);
|
2012-12-13 15:11:50 +08:00
|
|
|
llvm::APInt Min, End;
|
|
|
|
if (getRangeForType(*this, Ty, Min, End, true)) {
|
|
|
|
--End;
|
|
|
|
llvm::Value *Check;
|
|
|
|
if (!Min)
|
|
|
|
Check = Builder.CreateICmpULE(
|
|
|
|
Load, llvm::ConstantInt::get(getLLVMContext(), End));
|
|
|
|
else {
|
|
|
|
llvm::Value *Upper = Builder.CreateICmpSLE(
|
|
|
|
Load, llvm::ConstantInt::get(getLLVMContext(), End));
|
|
|
|
llvm::Value *Lower = Builder.CreateICmpSGE(
|
|
|
|
Load, llvm::ConstantInt::get(getLLVMContext(), Min));
|
|
|
|
Check = Builder.CreateAnd(Upper, Lower);
|
|
|
|
}
|
2013-10-02 10:29:49 +08:00
|
|
|
llvm::Constant *StaticArgs[] = {
|
|
|
|
EmitCheckSourceLocation(Loc),
|
|
|
|
EmitCheckTypeDescriptor(Ty)
|
|
|
|
};
|
2014-11-12 06:03:54 +08:00
|
|
|
SanitizerKind Kind = NeedsEnumCheck ? SanitizerKind::Enum : SanitizerKind::Bool;
|
|
|
|
EmitCheck(std::make_pair(Check, Kind), "load_invalid_value", StaticArgs,
|
|
|
|
EmitCheckValue(Load));
|
2012-12-13 15:11:50 +08:00
|
|
|
}
|
|
|
|
} else if (CGM.getCodeGenOpts().OptimizationLevel > 0)
|
2012-03-25 00:50:34 +08:00
|
|
|
if (llvm::MDNode *RangeInfo = getRangeForLoadFromType(Ty))
|
|
|
|
Load->setMetadata(llvm::LLVMContext::MD_range, RangeInfo);
|
2010-10-09 07:50:27 +08:00
|
|
|
|
2012-03-25 00:50:34 +08:00
|
|
|
return EmitFromMemory(Load, Ty);
|
2012-03-24 22:43:42 +08:00
|
|
|
}
|
|
|
|
|
2010-10-28 04:58:56 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) {
|
|
|
|
// Bool has a different representation in memory than in registers.
|
2012-03-25 00:50:34 +08:00
|
|
|
if (hasBooleanRepresentation(Ty)) {
|
2010-10-28 04:58:56 +08:00
|
|
|
// This should really always be an i1, but sometimes it's already
|
|
|
|
// an i8, and it's awkward to track those cases down.
|
|
|
|
if (Value->getType()->isIntegerTy(1))
|
2012-11-13 10:05:15 +08:00
|
|
|
return Builder.CreateZExt(Value, ConvertTypeForMem(Ty), "frombool");
|
|
|
|
assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&
|
|
|
|
"wrong value rep of bool");
|
2010-10-28 04:58:56 +08:00
|
|
|
}
|
2010-10-28 01:13:49 +08:00
|
|
|
|
2010-10-28 04:58:56 +08:00
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) {
|
|
|
|
// Bool has a different representation in memory than in registers.
|
2012-03-25 00:50:34 +08:00
|
|
|
if (hasBooleanRepresentation(Ty)) {
|
2012-11-13 10:05:15 +08:00
|
|
|
assert(Value->getType()->isIntegerTy(getContext().getTypeSize(Ty)) &&
|
|
|
|
"wrong value rep of bool");
|
2010-10-28 04:58:56 +08:00
|
|
|
return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool");
|
2010-10-28 01:13:49 +08:00
|
|
|
}
|
|
|
|
|
2010-10-28 04:58:56 +08:00
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
|
|
|
|
bool Volatile, unsigned Alignment,
|
2013-10-02 05:51:38 +08:00
|
|
|
QualType Ty, llvm::MDNode *TBAAInfo,
|
2013-04-05 05:53:22 +08:00
|
|
|
bool isInit, QualType TBAABaseType,
|
|
|
|
uint64_t TBAAOffset) {
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2012-08-16 08:10:13 +08:00
|
|
|
// Handle vectors differently to get better performance.
|
|
|
|
if (Ty->isVectorType()) {
|
|
|
|
llvm::Type *SrcTy = Value->getType();
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *VecTy = cast<llvm::VectorType>(SrcTy);
|
2012-08-16 08:10:13 +08:00
|
|
|
// Handle vec3 special.
|
|
|
|
if (VecTy->getNumElements() == 3) {
|
|
|
|
llvm::LLVMContext &VMContext = getLLVMContext();
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2012-08-16 08:10:13 +08:00
|
|
|
// Our source is a vec3, do a shuffle vector to make it a vec4.
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<llvm::Constant*, 4> Mask;
|
2013-10-02 05:51:38 +08:00
|
|
|
Mask.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
2012-08-16 08:10:13 +08:00
|
|
|
0));
|
2013-10-02 05:51:38 +08:00
|
|
|
Mask.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
2012-08-16 08:10:13 +08:00
|
|
|
1));
|
2013-10-02 05:51:38 +08:00
|
|
|
Mask.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
|
2012-08-16 08:10:13 +08:00
|
|
|
2));
|
|
|
|
Mask.push_back(llvm::UndefValue::get(llvm::Type::getInt32Ty(VMContext)));
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2012-08-16 08:10:13 +08:00
|
|
|
llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
|
|
|
|
Value = Builder.CreateShuffleVector(Value,
|
|
|
|
llvm::UndefValue::get(VecTy),
|
|
|
|
MaskV, "extractVec");
|
|
|
|
SrcTy = llvm::VectorType::get(VecTy->getElementType(), 4);
|
|
|
|
}
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *DstPtr = cast<llvm::PointerType>(Addr->getType());
|
2012-08-16 08:10:13 +08:00
|
|
|
if (DstPtr->getElementType() != SrcTy) {
|
|
|
|
llvm::Type *MemTy =
|
|
|
|
llvm::PointerType::get(SrcTy, DstPtr->getAddressSpace());
|
|
|
|
Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-10-28 04:58:56 +08:00
|
|
|
Value = EmitToMemory(Value, Ty);
|
2013-03-08 05:37:08 +08:00
|
|
|
|
2015-02-14 09:35:12 +08:00
|
|
|
if (Ty->isAtomicType() ||
|
|
|
|
(!isInit && typeIsSuitableForInlineAtomic(Ty, Volatile))) {
|
2013-03-08 05:37:17 +08:00
|
|
|
EmitAtomicStore(RValue::get(Value),
|
|
|
|
LValue::MakeAddr(Addr, Ty,
|
|
|
|
CharUnits::fromQuantity(Alignment),
|
|
|
|
getContext(), TBAAInfo),
|
|
|
|
isInit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-21 10:24:36 +08:00
|
|
|
llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
|
|
|
|
if (Alignment)
|
|
|
|
Store->setAlignment(Alignment);
|
2013-04-05 05:53:22 +08:00
|
|
|
if (TBAAInfo) {
|
|
|
|
llvm::MDNode *TBAAPath = CGM.getTBAAStructTagInfo(TBAABaseType, TBAAInfo,
|
|
|
|
TBAAOffset);
|
2013-10-08 08:08:49 +08:00
|
|
|
if (TBAAPath)
|
|
|
|
CGM.DecorateInstruction(Store, TBAAPath, false/*ConvertTypeToTag*/);
|
2013-04-05 05:53:22 +08:00
|
|
|
}
|
2009-02-10 08:57:50 +08:00
|
|
|
}
|
|
|
|
|
2012-01-17 01:27:18 +08:00
|
|
|
void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue,
|
2013-03-08 05:37:08 +08:00
|
|
|
bool isInit) {
|
2011-06-16 12:16:24 +08:00
|
|
|
EmitStoreOfScalar(value, lvalue.getAddress(), lvalue.isVolatile(),
|
2011-12-03 12:14:32 +08:00
|
|
|
lvalue.getAlignment().getQuantity(), lvalue.getType(),
|
2013-04-05 05:53:22 +08:00
|
|
|
lvalue.getTBAAInfo(), isInit, lvalue.getTBAABaseType(),
|
|
|
|
lvalue.getTBAAOffset());
|
2011-06-16 12:16:24 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
|
|
|
|
/// method emits the address of the lvalue, then loads the result as an rvalue,
|
|
|
|
/// returning the rvalue.
|
2013-10-02 10:29:49 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, SourceLocation Loc) {
|
2008-11-20 01:34:06 +08:00
|
|
|
if (LV.isObjCWeak()) {
|
2009-09-09 21:00:44 +08:00
|
|
|
// load of a __weak object.
|
2008-11-19 05:45:40 +08:00
|
|
|
llvm::Value *AddrWeakObj = LV.getAddress();
|
2009-10-29 01:39:19 +08:00
|
|
|
return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
|
|
|
|
AddrWeakObj));
|
2008-11-19 05:45:40 +08:00
|
|
|
}
|
2012-11-28 07:02:53 +08:00
|
|
|
if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
|
|
|
|
llvm::Value *Object = EmitARCLoadWeakRetained(LV.getAddress());
|
|
|
|
Object = EmitObjCConsumeObject(LV.getType(), Object);
|
|
|
|
return RValue::get(Object);
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-07-11 05:17:59 +08:00
|
|
|
if (LV.isSimple()) {
|
2011-06-28 05:24:11 +08:00
|
|
|
assert(!LV.getType()->isFunctionType());
|
2010-08-22 18:59:02 +08:00
|
|
|
|
|
|
|
// Everything needs a load.
|
2013-10-02 10:29:49 +08:00
|
|
|
return RValue::get(EmitLoadOfScalar(LV, Loc));
|
2007-07-11 05:17:59 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-07-11 05:17:59 +08:00
|
|
|
if (LV.isVectorElt()) {
|
2012-03-23 06:36:39 +08:00
|
|
|
llvm::LoadInst *Load = Builder.CreateLoad(LV.getVectorAddr(),
|
|
|
|
LV.isVolatileQualified());
|
|
|
|
Load->setAlignment(LV.getAlignment().getQuantity());
|
|
|
|
return RValue::get(Builder.CreateExtractElement(Load, LV.getVectorIdx(),
|
2007-07-11 05:17:59 +08:00
|
|
|
"vecext"));
|
|
|
|
}
|
implement lvalue to rvalue conversion for ocuvector components. We can now compile stuff
like this:
typedef __attribute__(( ocu_vector_type(4) )) float float4;
float4 test1(float4 V) {
return V.wzyx+V;
}
to:
_test1:
pshufd $27, %xmm0, %xmm1
addps %xmm0, %xmm1
movaps %xmm1, %xmm0
ret
and:
_test1:
mfspr r2, 256
oris r3, r2, 4096
mtspr 256, r3
li r3, lo16(LCPI1_0)
lis r4, ha16(LCPI1_0)
lvx v3, r4, r3
vperm v3, v2, v2, v3
vaddfp v2, v3, v2
mtspr 256, r2
blr
llvm-svn: 40771
2007-08-03 08:16:29 +08:00
|
|
|
|
|
|
|
// If this is a reference to a subset of the elements of a vector, either
|
|
|
|
// shuffle the input or extract/insert them as appropriate.
|
2008-04-19 07:10:10 +08:00
|
|
|
if (LV.isExtVectorElt())
|
2011-06-25 10:11:03 +08:00
|
|
|
return EmitLoadOfExtVectorElementLValue(LV);
|
2008-01-23 04:17:04 +08:00
|
|
|
|
2014-05-20 02:15:42 +08:00
|
|
|
// Global Register variables always invoke intrinsics
|
|
|
|
if (LV.isGlobalReg())
|
|
|
|
return EmitLoadOfGlobalRegLValue(LV);
|
|
|
|
|
2011-11-07 11:59:57 +08:00
|
|
|
assert(LV.isBitField() && "Unknown LValue type!");
|
|
|
|
return EmitLoadOfBitfieldLValue(LV);
|
2007-08-04 00:18:34 +08:00
|
|
|
}
|
2007-08-03 23:52:31 +08:00
|
|
|
|
2011-06-25 10:11:03 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV) {
|
2010-04-06 09:07:44 +08:00
|
|
|
const CGBitFieldInfo &Info = LV.getBitFieldInfo();
|
2008-01-23 04:17:04 +08:00
|
|
|
|
2010-04-14 07:34:15 +08:00
|
|
|
// Get the output type.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ResLTy = ConvertType(LV.getType());
|
2010-04-14 07:34:15 +08:00
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
llvm::Value *Ptr = LV.getBitFieldAddr();
|
|
|
|
llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(),
|
|
|
|
"bf.load");
|
|
|
|
cast<llvm::LoadInst>(Val)->setAlignment(Info.StorageAlignment);
|
|
|
|
|
|
|
|
if (Info.IsSigned) {
|
2013-01-16 07:13:47 +08:00
|
|
|
assert(static_cast<unsigned>(Info.Offset + Info.Size) <= Info.StorageSize);
|
2012-12-06 19:14:44 +08:00
|
|
|
unsigned HighBits = Info.StorageSize - Info.Offset - Info.Size;
|
|
|
|
if (HighBits)
|
|
|
|
Val = Builder.CreateShl(Val, HighBits, "bf.shl");
|
|
|
|
if (Info.Offset + HighBits)
|
|
|
|
Val = Builder.CreateAShr(Val, Info.Offset + HighBits, "bf.ashr");
|
|
|
|
} else {
|
|
|
|
if (Info.Offset)
|
|
|
|
Val = Builder.CreateLShr(Val, Info.Offset, "bf.lshr");
|
2012-12-19 06:22:16 +08:00
|
|
|
if (static_cast<unsigned>(Info.Offset) + Info.Size < Info.StorageSize)
|
2012-12-06 19:14:44 +08:00
|
|
|
Val = Builder.CreateAnd(Val, llvm::APInt::getLowBitsSet(Info.StorageSize,
|
|
|
|
Info.Size),
|
|
|
|
"bf.clear");
|
2010-04-14 07:34:15 +08:00
|
|
|
}
|
2012-12-06 19:14:44 +08:00
|
|
|
Val = Builder.CreateIntCast(Val, ResLTy, Info.IsSigned, "bf.cast");
|
2008-05-18 04:03:47 +08:00
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
return RValue::get(Val);
|
2008-01-23 04:17:04 +08:00
|
|
|
}
|
|
|
|
|
2009-01-18 14:42:49 +08:00
|
|
|
// If this is a reference to a subset of the elements of a vector, create an
|
|
|
|
// appropriate shufflevector.
|
2011-06-25 10:11:03 +08:00
|
|
|
RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV) {
|
2012-03-23 06:36:39 +08:00
|
|
|
llvm::LoadInst *Load = Builder.CreateLoad(LV.getExtVectorAddr(),
|
|
|
|
LV.isVolatileQualified());
|
|
|
|
Load->setAlignment(LV.getAlignment().getQuantity());
|
|
|
|
llvm::Value *Vec = Load;
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-05-09 14:41:27 +08:00
|
|
|
const llvm::Constant *Elts = LV.getExtVectorElts();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
|
|
|
// If the result of the expression is a non-vector type, we must be extracting
|
|
|
|
// a single element. Just codegen as an extractelement.
|
2011-06-25 10:11:03 +08:00
|
|
|
const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
|
2007-08-11 01:10:08 +08:00
|
|
|
if (!ExprVT) {
|
2008-05-22 08:50:06 +08:00
|
|
|
unsigned InIdx = getAccessedFieldNo(0, Elts);
|
2014-05-31 08:22:12 +08:00
|
|
|
llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
|
2011-09-28 05:06:10 +08:00
|
|
|
return RValue::get(Builder.CreateExtractElement(Vec, Elt));
|
2007-08-04 00:18:34 +08:00
|
|
|
}
|
2009-01-18 14:42:49 +08:00
|
|
|
|
|
|
|
// Always use shuffle vector to try to retain the original program structure
|
2007-08-11 01:10:08 +08:00
|
|
|
unsigned NumResultElts = ExprVT->getNumElements();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant*, 4> Mask;
|
2012-01-25 13:34:41 +08:00
|
|
|
for (unsigned i = 0; i != NumResultElts; ++i)
|
|
|
|
Mask.push_back(Builder.getInt32(getAccessedFieldNo(i, Elts)));
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-02-15 08:14:06 +08:00
|
|
|
llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
|
|
|
|
Vec = Builder.CreateShuffleVector(Vec, llvm::UndefValue::get(Vec->getType()),
|
2011-09-28 05:06:10 +08:00
|
|
|
MaskV);
|
2009-01-18 14:42:49 +08:00
|
|
|
return RValue::get(Vec);
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2014-08-20 01:17:40 +08:00
|
|
|
/// @brief Generates lvalue for partial ext_vector access.
|
|
|
|
llvm::Value *CodeGenFunction::EmitExtVectorElementLValue(LValue LV) {
|
|
|
|
llvm::Value *VectorAddress = LV.getExtVectorAddr();
|
|
|
|
const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
|
|
|
|
QualType EQT = ExprVT->getElementType();
|
|
|
|
llvm::Type *VectorElementTy = CGM.getTypes().ConvertType(EQT);
|
|
|
|
llvm::Type *VectorElementPtrToTy = VectorElementTy->getPointerTo();
|
|
|
|
|
|
|
|
llvm::Value *CastToPointerElement =
|
|
|
|
Builder.CreateBitCast(VectorAddress,
|
|
|
|
VectorElementPtrToTy, "conv.ptr.element");
|
|
|
|
|
|
|
|
const llvm::Constant *Elts = LV.getExtVectorElts();
|
|
|
|
unsigned ix = getAccessedFieldNo(0, Elts);
|
|
|
|
|
|
|
|
llvm::Value *VectorBasePtrPlusIx =
|
|
|
|
Builder.CreateInBoundsGEP(CastToPointerElement,
|
|
|
|
llvm::ConstantInt::get(SizeTy, ix), "add.ptr");
|
|
|
|
|
|
|
|
return VectorBasePtrPlusIx;
|
|
|
|
}
|
|
|
|
|
2014-05-20 02:15:42 +08:00
|
|
|
/// @brief Load of global gamed gegisters are always calls to intrinsics.
|
|
|
|
RValue CodeGenFunction::EmitLoadOfGlobalRegLValue(LValue LV) {
|
2014-06-06 00:45:22 +08:00
|
|
|
assert((LV.getType()->isIntegerType() || LV.getType()->isPointerType()) &&
|
|
|
|
"Bad type for register variable");
|
2014-12-10 02:39:32 +08:00
|
|
|
llvm::MDNode *RegName = cast<llvm::MDNode>(
|
|
|
|
cast<llvm::MetadataAsValue>(LV.getGlobalReg())->getMetadata());
|
2014-06-06 00:45:22 +08:00
|
|
|
|
|
|
|
// We accept integer and pointer types only
|
|
|
|
llvm::Type *OrigTy = CGM.getTypes().ConvertType(LV.getType());
|
|
|
|
llvm::Type *Ty = OrigTy;
|
|
|
|
if (OrigTy->isPointerTy())
|
|
|
|
Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
|
|
|
|
llvm::Type *Types[] = { Ty };
|
|
|
|
|
2014-05-20 02:15:42 +08:00
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::read_register, Types);
|
2014-12-10 02:39:32 +08:00
|
|
|
llvm::Value *Call = Builder.CreateCall(
|
|
|
|
F, llvm::MetadataAsValue::get(Ty->getContext(), RegName));
|
2014-06-06 00:45:22 +08:00
|
|
|
if (OrigTy->isPointerTy())
|
|
|
|
Call = Builder.CreateIntToPtr(Call, OrigTy);
|
2014-05-20 02:15:42 +08:00
|
|
|
return RValue::get(Call);
|
|
|
|
}
|
2007-08-04 00:18:34 +08:00
|
|
|
|
2007-06-30 00:31:29 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
/// EmitStoreThroughLValue - Store the specified rvalue into the specified
|
|
|
|
/// lvalue, where both are guaranteed to the have the same type, and that type
|
|
|
|
/// is 'Ty'.
|
2013-10-02 05:51:38 +08:00
|
|
|
void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
|
2015-01-14 15:38:27 +08:00
|
|
|
bool isInit) {
|
2007-08-04 00:28:33 +08:00
|
|
|
if (!Dst.isSimple()) {
|
|
|
|
if (Dst.isVectorElt()) {
|
|
|
|
// Read/modify/write the vector, inserting the new element.
|
2012-03-23 06:36:39 +08:00
|
|
|
llvm::LoadInst *Load = Builder.CreateLoad(Dst.getVectorAddr(),
|
|
|
|
Dst.isVolatileQualified());
|
|
|
|
Load->setAlignment(Dst.getAlignment().getQuantity());
|
|
|
|
llvm::Value *Vec = Load;
|
2007-09-01 06:49:20 +08:00
|
|
|
Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
|
2007-08-04 00:28:33 +08:00
|
|
|
Dst.getVectorIdx(), "vecins");
|
2012-03-23 06:36:39 +08:00
|
|
|
llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getVectorAddr(),
|
|
|
|
Dst.isVolatileQualified());
|
|
|
|
Store->setAlignment(Dst.getAlignment().getQuantity());
|
2007-08-04 00:28:33 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-04-19 07:10:10 +08:00
|
|
|
// If this is an update of extended vector elements, insert them as
|
|
|
|
// appropriate.
|
|
|
|
if (Dst.isExtVectorElt())
|
2011-06-25 10:11:03 +08:00
|
|
|
return EmitStoreThroughExtVectorComponentLValue(Src, Dst);
|
2008-01-23 06:36:45 +08:00
|
|
|
|
2014-05-20 02:15:42 +08:00
|
|
|
if (Dst.isGlobalReg())
|
|
|
|
return EmitStoreThroughGlobalRegLValue(Src, Dst);
|
|
|
|
|
2011-11-07 11:59:57 +08:00
|
|
|
assert(Dst.isBitField() && "Unknown LValue type");
|
|
|
|
return EmitStoreThroughBitfieldLValue(Src, Dst);
|
2007-08-04 00:28:33 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
// There's special magic for assigning into an ARC-qualified l-value.
|
|
|
|
if (Qualifiers::ObjCLifetime Lifetime = Dst.getQuals().getObjCLifetime()) {
|
|
|
|
switch (Lifetime) {
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
llvm_unreachable("present but none");
|
|
|
|
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
// nothing special
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Strong:
|
2011-06-25 10:11:03 +08:00
|
|
|
EmitARCStoreStrong(Dst, Src.getScalarVal(), /*ignore*/ true);
|
2011-06-16 07:02:42 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
EmitARCStoreWeak(Dst.getAddress(), Src.getScalarVal(), /*ignore*/ true);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
2011-06-25 10:11:03 +08:00
|
|
|
Src = RValue::get(EmitObjCExtendObjectLifetime(Dst.getType(),
|
|
|
|
Src.getScalarVal()));
|
2011-06-16 07:02:42 +08:00
|
|
|
// fall into the normal path
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-21 08:30:43 +08:00
|
|
|
if (Dst.isObjCWeak() && !Dst.isNonGC()) {
|
2009-09-09 21:00:44 +08:00
|
|
|
// load of a __weak object.
|
2008-11-20 01:34:06 +08:00
|
|
|
llvm::Value *LvalueDst = Dst.getAddress();
|
|
|
|
llvm::Value *src = Src.getScalarVal();
|
2009-04-14 08:57:29 +08:00
|
|
|
CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
|
2008-11-20 01:34:06 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-02-21 08:30:43 +08:00
|
|
|
if (Dst.isObjCStrong() && !Dst.isNonGC()) {
|
2009-09-09 21:00:44 +08:00
|
|
|
// load of a __strong object.
|
2008-11-20 01:34:06 +08:00
|
|
|
llvm::Value *LvalueDst = Dst.getAddress();
|
|
|
|
llvm::Value *src = Src.getScalarVal();
|
2009-09-25 06:25:38 +08:00
|
|
|
if (Dst.isObjCIvar()) {
|
|
|
|
assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ResultType = ConvertType(getContext().LongTy);
|
2009-09-25 06:25:38 +08:00
|
|
|
llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
|
2009-09-25 08:00:20 +08:00
|
|
|
llvm::Value *dst = RHS;
|
2009-09-25 06:25:38 +08:00
|
|
|
RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
|
2013-07-26 13:59:26 +08:00
|
|
|
llvm::Value *LHS =
|
2009-09-25 06:25:38 +08:00
|
|
|
Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
|
|
|
|
llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
|
2009-09-25 08:00:20 +08:00
|
|
|
CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
|
2009-09-25 06:25:38 +08:00
|
|
|
BytesBetween);
|
2010-07-21 04:30:03 +08:00
|
|
|
} else if (Dst.isGlobalObjCRef()) {
|
|
|
|
CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst,
|
|
|
|
Dst.isThreadLocalRef());
|
|
|
|
}
|
2009-05-05 07:27:20 +08:00
|
|
|
else
|
|
|
|
CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
|
2008-11-20 01:34:06 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-11 08:04:45 +08:00
|
|
|
assert(Src.isScalar() && "Can't emit an agg store with this method");
|
2012-01-17 01:27:18 +08:00
|
|
|
EmitStoreOfScalar(Src.getScalarVal(), Dst, isInit);
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2008-01-23 06:36:45 +08:00
|
|
|
void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
|
2008-11-19 17:36:46 +08:00
|
|
|
llvm::Value **Result) {
|
2010-04-06 09:07:44 +08:00
|
|
|
const CGBitFieldInfo &Info = Dst.getBitFieldInfo();
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ResLTy = ConvertTypeForMem(Dst.getType());
|
2012-12-06 19:14:44 +08:00
|
|
|
llvm::Value *Ptr = Dst.getBitFieldAddr();
|
2008-01-23 06:36:45 +08:00
|
|
|
|
IRgen: (Reapply 101222, with fixes) Move EmitStoreThroughBitfieldLValue to use new CGBitfieldInfo::AccessInfo decomposition, instead of computing the access policy itself.
- Sadly, this doesn't seem to give any .ll size win so far. It is possible to make this routine significantly smarter & avoid various shifting, masking, and zext/sext, but I'm not really convinced it is worth it. It is tricky, and this is really instcombine's job.
- No intended functionality change; the test case is just to increase coverage & serves as a demo file, it worked before this commit.
The new fixes from r101222 are:
1. The shift to the target position needs to occur after the value is extended to the correct size. This broke Clang bootstrap, among other things no doubt.
2. Swap the order of arguments to OR, to get a tad more constant folding.
llvm-svn: 101339
2010-04-15 11:47:33 +08:00
|
|
|
// Get the source value, truncated to the width of the bit-field.
|
2008-11-19 17:36:46 +08:00
|
|
|
llvm::Value *SrcVal = Src.getScalarVal();
|
2010-04-18 05:52:22 +08:00
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
// Cast the source to the storage type and shift it into place.
|
|
|
|
SrcVal = Builder.CreateIntCast(SrcVal,
|
|
|
|
Ptr->getType()->getPointerElementType(),
|
|
|
|
/*IsSigned=*/false);
|
|
|
|
llvm::Value *MaskedVal = SrcVal;
|
|
|
|
|
|
|
|
// See if there are other bits in the bitfield's storage we'll need to load
|
|
|
|
// and mask together with source before storing.
|
|
|
|
if (Info.StorageSize != Info.Size) {
|
|
|
|
assert(Info.StorageSize > Info.Size && "Invalid bitfield size.");
|
|
|
|
llvm::Value *Val = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
|
|
|
|
"bf.load");
|
|
|
|
cast<llvm::LoadInst>(Val)->setAlignment(Info.StorageAlignment);
|
|
|
|
|
|
|
|
// Mask the source value as needed.
|
|
|
|
if (!hasBooleanRepresentation(Dst.getType()))
|
|
|
|
SrcVal = Builder.CreateAnd(SrcVal,
|
|
|
|
llvm::APInt::getLowBitsSet(Info.StorageSize,
|
|
|
|
Info.Size),
|
|
|
|
"bf.value");
|
|
|
|
MaskedVal = SrcVal;
|
|
|
|
if (Info.Offset)
|
|
|
|
SrcVal = Builder.CreateShl(SrcVal, Info.Offset, "bf.shl");
|
|
|
|
|
|
|
|
// Mask out the original value.
|
|
|
|
Val = Builder.CreateAnd(Val,
|
|
|
|
~llvm::APInt::getBitsSet(Info.StorageSize,
|
|
|
|
Info.Offset,
|
|
|
|
Info.Offset + Info.Size),
|
|
|
|
"bf.clear");
|
2008-11-19 17:36:46 +08:00
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
// Or together the unchanged values and the source value.
|
|
|
|
SrcVal = Builder.CreateOr(Val, SrcVal, "bf.set");
|
|
|
|
} else {
|
|
|
|
assert(Info.Offset == 0);
|
2008-11-19 17:36:46 +08:00
|
|
|
}
|
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
// Write the new value back out.
|
|
|
|
llvm::StoreInst *Store = Builder.CreateStore(SrcVal, Ptr,
|
|
|
|
Dst.isVolatileQualified());
|
|
|
|
Store->setAlignment(Info.StorageAlignment);
|
IRgen: (Reapply 101222, with fixes) Move EmitStoreThroughBitfieldLValue to use new CGBitfieldInfo::AccessInfo decomposition, instead of computing the access policy itself.
- Sadly, this doesn't seem to give any .ll size win so far. It is possible to make this routine significantly smarter & avoid various shifting, masking, and zext/sext, but I'm not really convinced it is worth it. It is tricky, and this is really instcombine's job.
- No intended functionality change; the test case is just to increase coverage & serves as a demo file, it worked before this commit.
The new fixes from r101222 are:
1. The shift to the target position needs to occur after the value is extended to the correct size. This broke Clang bootstrap, among other things no doubt.
2. Swap the order of arguments to OR, to get a tad more constant folding.
llvm-svn: 101339
2010-04-15 11:47:33 +08:00
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
// Return the new value of the bit-field, if requested.
|
|
|
|
if (Result) {
|
|
|
|
llvm::Value *ResultVal = MaskedVal;
|
|
|
|
|
|
|
|
// Sign extend the value if needed.
|
|
|
|
if (Info.IsSigned) {
|
|
|
|
assert(Info.Size <= Info.StorageSize);
|
|
|
|
unsigned HighBits = Info.StorageSize - Info.Size;
|
|
|
|
if (HighBits) {
|
|
|
|
ResultVal = Builder.CreateShl(ResultVal, HighBits, "bf.result.shl");
|
|
|
|
ResultVal = Builder.CreateAShr(ResultVal, HighBits, "bf.result.ashr");
|
|
|
|
}
|
IRgen: (Reapply 101222, with fixes) Move EmitStoreThroughBitfieldLValue to use new CGBitfieldInfo::AccessInfo decomposition, instead of computing the access policy itself.
- Sadly, this doesn't seem to give any .ll size win so far. It is possible to make this routine significantly smarter & avoid various shifting, masking, and zext/sext, but I'm not really convinced it is worth it. It is tricky, and this is really instcombine's job.
- No intended functionality change; the test case is just to increase coverage & serves as a demo file, it worked before this commit.
The new fixes from r101222 are:
1. The shift to the target position needs to occur after the value is extended to the correct size. This broke Clang bootstrap, among other things no doubt.
2. Swap the order of arguments to OR, to get a tad more constant folding.
llvm-svn: 101339
2010-04-15 11:47:33 +08:00
|
|
|
}
|
|
|
|
|
2012-12-06 19:14:44 +08:00
|
|
|
ResultVal = Builder.CreateIntCast(ResultVal, ResLTy, Info.IsSigned,
|
|
|
|
"bf.result.cast");
|
2012-12-19 08:26:58 +08:00
|
|
|
*Result = EmitFromMemory(ResultVal, Dst.getType());
|
2008-08-06 13:08:45 +08:00
|
|
|
}
|
2008-01-23 06:36:45 +08:00
|
|
|
}
|
|
|
|
|
2008-04-19 07:10:10 +08:00
|
|
|
void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
|
2011-06-25 10:11:03 +08:00
|
|
|
LValue Dst) {
|
2007-08-04 00:28:33 +08:00
|
|
|
// This access turns into a read/modify/write of the vector. Load the input
|
|
|
|
// value now.
|
2012-03-23 06:36:39 +08:00
|
|
|
llvm::LoadInst *Load = Builder.CreateLoad(Dst.getExtVectorAddr(),
|
|
|
|
Dst.isVolatileQualified());
|
|
|
|
Load->setAlignment(Dst.getAlignment().getQuantity());
|
|
|
|
llvm::Value *Vec = Load;
|
2008-05-09 14:41:27 +08:00
|
|
|
const llvm::Constant *Elts = Dst.getExtVectorElts();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-09-01 06:49:20 +08:00
|
|
|
llvm::Value *SrcVal = Src.getScalarVal();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-06-25 10:11:03 +08:00
|
|
|
if (const VectorType *VTy = Dst.getType()->getAs<VectorType>()) {
|
2007-08-04 00:37:04 +08:00
|
|
|
unsigned NumSrcElts = VTy->getNumElements();
|
2009-01-18 14:42:49 +08:00
|
|
|
unsigned NumDstElts =
|
|
|
|
cast<llvm::VectorType>(Vec->getType())->getNumElements();
|
|
|
|
if (NumDstElts == NumSrcElts) {
|
2009-09-09 21:00:44 +08:00
|
|
|
// Use shuffle vector is the src and destination are the same number of
|
|
|
|
// elements and restore the vector mask since it is on the side it will be
|
|
|
|
// stored.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
|
2012-01-25 13:34:41 +08:00
|
|
|
for (unsigned i = 0; i != NumSrcElts; ++i)
|
|
|
|
Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-02-15 08:14:06 +08:00
|
|
|
llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
|
2009-01-18 14:42:49 +08:00
|
|
|
Vec = Builder.CreateShuffleVector(SrcVal,
|
2009-07-31 07:11:26 +08:00
|
|
|
llvm::UndefValue::get(Vec->getType()),
|
2011-09-28 05:06:10 +08:00
|
|
|
MaskV);
|
2009-07-31 06:28:39 +08:00
|
|
|
} else if (NumDstElts > NumSrcElts) {
|
2009-01-18 14:42:49 +08:00
|
|
|
// Extended the source vector to the same length and then shuffle it
|
|
|
|
// into the destination.
|
|
|
|
// FIXME: since we're shuffling with undef, can we just use the indices
|
|
|
|
// into that? This could be simpler.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant*, 4> ExtMask;
|
2012-02-14 20:06:21 +08:00
|
|
|
for (unsigned i = 0; i != NumSrcElts; ++i)
|
2012-01-25 13:34:41 +08:00
|
|
|
ExtMask.push_back(Builder.getInt32(i));
|
2012-02-14 20:06:21 +08:00
|
|
|
ExtMask.resize(NumDstElts, llvm::UndefValue::get(Int32Ty));
|
2011-02-15 08:14:06 +08:00
|
|
|
llvm::Value *ExtMaskV = llvm::ConstantVector::get(ExtMask);
|
2009-09-09 21:00:44 +08:00
|
|
|
llvm::Value *ExtSrcVal =
|
2009-02-18 02:31:04 +08:00
|
|
|
Builder.CreateShuffleVector(SrcVal,
|
2009-07-31 07:11:26 +08:00
|
|
|
llvm::UndefValue::get(SrcVal->getType()),
|
2011-09-28 05:06:10 +08:00
|
|
|
ExtMaskV);
|
2009-01-18 14:42:49 +08:00
|
|
|
// build identity
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant*, 4> Mask;
|
2009-10-29 01:39:19 +08:00
|
|
|
for (unsigned i = 0; i != NumDstElts; ++i)
|
2012-01-25 13:34:41 +08:00
|
|
|
Mask.push_back(Builder.getInt32(i));
|
2009-10-29 01:39:19 +08:00
|
|
|
|
2013-11-22 01:09:05 +08:00
|
|
|
// When the vector size is odd and .odd or .hi is used, the last element
|
|
|
|
// of the Elts constant array will be one past the size of the vector.
|
|
|
|
// Ignore the last element here, if it is greater than the mask size.
|
|
|
|
if (getAccessedFieldNo(NumSrcElts - 1, Elts) == Mask.size())
|
|
|
|
NumSrcElts--;
|
|
|
|
|
2009-01-18 14:42:49 +08:00
|
|
|
// modify when what gets shuffled in
|
2012-01-25 13:34:41 +08:00
|
|
|
for (unsigned i = 0; i != NumSrcElts; ++i)
|
|
|
|
Mask[getAccessedFieldNo(i, Elts)] = Builder.getInt32(i+NumDstElts);
|
2011-02-15 08:14:06 +08:00
|
|
|
llvm::Value *MaskV = llvm::ConstantVector::get(Mask);
|
2011-09-28 05:06:10 +08:00
|
|
|
Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV);
|
2009-07-31 06:28:39 +08:00
|
|
|
} else {
|
2009-01-18 14:42:49 +08:00
|
|
|
// We should never shorten the vector
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("unexpected shorten vector length");
|
2007-08-04 00:37:04 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If the Src is a scalar (not a vector) it must be updating one element.
|
2008-05-22 08:50:06 +08:00
|
|
|
unsigned InIdx = getAccessedFieldNo(0, Elts);
|
2014-05-31 08:22:12 +08:00
|
|
|
llvm::Value *Elt = llvm::ConstantInt::get(SizeTy, InIdx);
|
2011-09-28 05:06:10 +08:00
|
|
|
Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt);
|
2007-08-04 00:28:33 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2012-03-23 06:36:39 +08:00
|
|
|
llvm::StoreInst *Store = Builder.CreateStore(Vec, Dst.getExtVectorAddr(),
|
|
|
|
Dst.isVolatileQualified());
|
|
|
|
Store->setAlignment(Dst.getAlignment().getQuantity());
|
2007-08-04 00:28:33 +08:00
|
|
|
}
|
|
|
|
|
2014-05-20 02:15:42 +08:00
|
|
|
/// @brief Store of global named registers are always calls to intrinsics.
|
|
|
|
void CodeGenFunction::EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst) {
|
2014-06-06 00:45:22 +08:00
|
|
|
assert((Dst.getType()->isIntegerType() || Dst.getType()->isPointerType()) &&
|
|
|
|
"Bad type for register variable");
|
2014-12-10 02:39:32 +08:00
|
|
|
llvm::MDNode *RegName = cast<llvm::MDNode>(
|
|
|
|
cast<llvm::MetadataAsValue>(Dst.getGlobalReg())->getMetadata());
|
2014-05-20 02:15:42 +08:00
|
|
|
assert(RegName && "Register LValue is not metadata");
|
2014-06-06 00:45:22 +08:00
|
|
|
|
|
|
|
// We accept integer and pointer types only
|
|
|
|
llvm::Type *OrigTy = CGM.getTypes().ConvertType(Dst.getType());
|
|
|
|
llvm::Type *Ty = OrigTy;
|
|
|
|
if (OrigTy->isPointerTy())
|
|
|
|
Ty = CGM.getTypes().getDataLayout().getIntPtrType(OrigTy);
|
|
|
|
llvm::Type *Types[] = { Ty };
|
|
|
|
|
2014-05-20 02:15:42 +08:00
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::write_register, Types);
|
|
|
|
llvm::Value *Value = Src.getScalarVal();
|
2014-06-06 00:45:22 +08:00
|
|
|
if (OrigTy->isPointerTy())
|
|
|
|
Value = Builder.CreatePtrToInt(Value, Ty);
|
2014-12-10 02:39:32 +08:00
|
|
|
Builder.CreateCall2(F, llvm::MetadataAsValue::get(Ty->getContext(), RegName),
|
|
|
|
Value);
|
2014-05-20 02:15:42 +08:00
|
|
|
}
|
|
|
|
|
2014-05-21 01:10:39 +08:00
|
|
|
// setObjCGCLValueClass - sets class of the lvalue for the purpose of
|
2009-09-17 05:37:16 +08:00
|
|
|
// generating write-barries API. It is currently a global, ivar,
|
|
|
|
// or neither.
|
2009-10-29 01:39:19 +08:00
|
|
|
static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
|
2011-10-01 02:23:36 +08:00
|
|
|
LValue &LV,
|
|
|
|
bool IsMemberAccess=false) {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Ctx.getLangOpts().getGC() == LangOptions::NonGC)
|
2009-09-17 05:37:16 +08:00
|
|
|
return;
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2009-09-17 07:11:23 +08:00
|
|
|
if (isa<ObjCIvarRefExpr>(E)) {
|
2011-10-01 02:23:36 +08:00
|
|
|
QualType ExpTy = E->getType();
|
|
|
|
if (IsMemberAccess && ExpTy->isPointerType()) {
|
|
|
|
// If ivar is a structure pointer, assigning to field of
|
2013-07-26 13:59:26 +08:00
|
|
|
// this struct follows gcc's behavior and makes it a non-ivar
|
2011-10-01 02:23:36 +08:00
|
|
|
// writer-barrier conservatively.
|
|
|
|
ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
|
|
|
|
if (ExpTy->isRecordType()) {
|
|
|
|
LV.setObjCIvar(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-08-21 11:51:29 +08:00
|
|
|
LV.setObjCIvar(true);
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr *>(E));
|
2009-09-25 06:25:38 +08:00
|
|
|
LV.setBaseIvarExp(Exp->getBase());
|
2010-08-21 11:51:29 +08:00
|
|
|
LV.setObjCArray(E->getType()->isArrayType());
|
2009-09-17 07:11:23 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *Exp = dyn_cast<DeclRefExpr>(E)) {
|
|
|
|
if (const auto *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
|
2010-10-15 12:57:14 +08:00
|
|
|
if (VD->hasGlobalStorage()) {
|
2010-08-21 11:51:29 +08:00
|
|
|
LV.setGlobalObjCRef(true);
|
2013-04-13 10:43:54 +08:00
|
|
|
LV.setThreadLocalRef(VD->getTLSKind() != VarDecl::TLS_None);
|
2010-07-21 04:30:03 +08:00
|
|
|
}
|
2009-09-17 05:37:16 +08:00
|
|
|
}
|
2010-08-21 11:51:29 +08:00
|
|
|
LV.setObjCArray(E->getType()->isArrayType());
|
2009-10-29 01:39:19 +08:00
|
|
|
return;
|
2009-09-17 05:37:16 +08:00
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *Exp = dyn_cast<UnaryOperator>(E)) {
|
2011-10-01 02:23:36 +08:00
|
|
|
setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
|
2009-10-29 01:39:19 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *Exp = dyn_cast<ParenExpr>(E)) {
|
2011-10-01 02:23:36 +08:00
|
|
|
setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
|
2009-10-01 01:10:29 +08:00
|
|
|
if (LV.isObjCIvar()) {
|
|
|
|
// If cast is to a structure pointer, follow gcc's behavior and make it
|
|
|
|
// a non-ivar write-barrier.
|
|
|
|
QualType ExpTy = E->getType();
|
|
|
|
if (ExpTy->isPointerType())
|
|
|
|
ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
|
|
|
|
if (ExpTy->isRecordType())
|
2013-07-26 13:59:26 +08:00
|
|
|
LV.setObjCIvar(false);
|
2009-10-29 01:39:19 +08:00
|
|
|
}
|
|
|
|
return;
|
2009-10-01 01:10:29 +08:00
|
|
|
}
|
2011-04-15 08:35:48 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *Exp = dyn_cast<GenericSelectionExpr>(E)) {
|
2011-04-15 08:35:48 +08:00
|
|
|
setObjCGCLValueClass(Ctx, Exp->getResultExpr(), LV);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *Exp = dyn_cast<ImplicitCastExpr>(E)) {
|
2011-10-01 02:23:36 +08:00
|
|
|
setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
|
2009-10-29 01:39:19 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *Exp = dyn_cast<CStyleCastExpr>(E)) {
|
2011-10-01 02:23:36 +08:00
|
|
|
setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
|
2009-10-29 01:39:19 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *Exp = dyn_cast<ObjCBridgedCastExpr>(E)) {
|
2011-10-01 02:23:36 +08:00
|
|
|
setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV, IsMemberAccess);
|
2011-06-16 07:02:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
|
2009-09-17 05:37:16 +08:00
|
|
|
setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
|
2013-07-26 13:59:26 +08:00
|
|
|
if (LV.isObjCIvar() && !LV.isObjCArray())
|
|
|
|
// Using array syntax to assigning to what an ivar points to is not
|
2009-09-18 08:04:00 +08:00
|
|
|
// same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
|
2013-07-26 13:59:26 +08:00
|
|
|
LV.setObjCIvar(false);
|
2009-09-22 02:54:29 +08:00
|
|
|
else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
|
2013-07-26 13:59:26 +08:00
|
|
|
// Using array syntax to assigning to what global points to is not
|
2009-09-22 02:54:29 +08:00
|
|
|
// same as assigning to the global itself. {id *G;} G[i] = 0;
|
2010-08-21 11:51:29 +08:00
|
|
|
LV.setGlobalObjCRef(false);
|
2009-10-29 01:39:19 +08:00
|
|
|
return;
|
2009-09-18 08:04:00 +08:00
|
|
|
}
|
2011-10-01 02:23:36 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *Exp = dyn_cast<MemberExpr>(E)) {
|
2011-10-01 02:23:36 +08:00
|
|
|
setObjCGCLValueClass(Ctx, Exp->getBase(), LV, true);
|
2009-09-18 08:04:00 +08:00
|
|
|
// We don't know if member is an 'ivar', but this flag is looked at
|
|
|
|
// only in the context of LV.isObjCIvar().
|
2010-08-21 11:51:29 +08:00
|
|
|
LV.setObjCArray(E->getType()->isArrayType());
|
2009-10-29 01:39:19 +08:00
|
|
|
return;
|
2009-09-17 05:37:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-12 14:52:18 +08:00
|
|
|
static llvm::Value *
|
2011-07-12 16:58:26 +08:00
|
|
|
EmitBitCastOfLValueToProperType(CodeGenFunction &CGF,
|
2011-07-12 14:52:18 +08:00
|
|
|
llvm::Value *V, llvm::Type *IRType,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Name = StringRef()) {
|
2011-07-12 14:52:18 +08:00
|
|
|
unsigned AS = cast<llvm::PointerType>(V->getType())->getAddressSpace();
|
2011-07-12 16:58:26 +08:00
|
|
|
return CGF.Builder.CreateBitCast(V, IRType->getPointerTo(AS), Name);
|
2011-07-12 14:52:18 +08:00
|
|
|
}
|
|
|
|
|
2014-11-11 12:05:39 +08:00
|
|
|
static LValue EmitThreadPrivateVarDeclLValue(
|
|
|
|
CodeGenFunction &CGF, const VarDecl *VD, QualType T, llvm::Value *V,
|
|
|
|
llvm::Type *RealVarTy, CharUnits Alignment, SourceLocation Loc) {
|
2015-02-25 16:32:46 +08:00
|
|
|
V = CGF.CGM.getOpenMPRuntime().getAddrOfThreadPrivate(CGF, VD, V, Loc);
|
2014-11-11 12:05:39 +08:00
|
|
|
V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy);
|
|
|
|
return CGF.MakeAddrLValue(V, T, Alignment);
|
|
|
|
}
|
|
|
|
|
2009-11-08 07:06:58 +08:00
|
|
|
static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
|
|
|
|
const Expr *E, const VarDecl *VD) {
|
2014-03-27 06:48:22 +08:00
|
|
|
QualType T = E->getType();
|
|
|
|
|
|
|
|
// If it's thread_local, emit a call to its wrapper function instead.
|
2014-10-05 13:05:40 +08:00
|
|
|
if (VD->getTLSKind() == VarDecl::TLS_Dynamic &&
|
|
|
|
CGF.CGM.getCXXABI().usesThreadWrapperFunction())
|
2014-03-27 06:48:22 +08:00
|
|
|
return CGF.CGM.getCXXABI().EmitThreadLocalVarDeclLValue(CGF, VD, T);
|
|
|
|
|
2009-11-08 07:06:58 +08:00
|
|
|
llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
|
2011-11-16 08:42:57 +08:00
|
|
|
llvm::Type *RealVarTy = CGF.getTypes().ConvertTypeForMem(VD->getType());
|
|
|
|
V = EmitBitCastOfLValueToProperType(CGF, V, RealVarTy);
|
2011-12-03 12:14:32 +08:00
|
|
|
CharUnits Alignment = CGF.getContext().getDeclAlign(VD);
|
2011-11-16 08:42:57 +08:00
|
|
|
LValue LV;
|
2014-11-11 12:05:39 +08:00
|
|
|
// Emit reference to the private copy of the variable if it is an OpenMP
|
|
|
|
// threadprivate variable.
|
|
|
|
if (CGF.getLangOpts().OpenMP && VD->hasAttr<OMPThreadPrivateDeclAttr>())
|
|
|
|
return EmitThreadPrivateVarDeclLValue(CGF, VD, T, V, RealVarTy, Alignment,
|
|
|
|
E->getExprLoc());
|
2011-11-16 08:42:57 +08:00
|
|
|
if (VD->getType()->isReferenceType()) {
|
|
|
|
llvm::LoadInst *LI = CGF.Builder.CreateLoad(V);
|
2011-12-03 12:14:32 +08:00
|
|
|
LI->setAlignment(Alignment.getQuantity());
|
2011-11-16 08:42:57 +08:00
|
|
|
V = LI;
|
|
|
|
LV = CGF.MakeNaturalAlignAddrLValue(V, T);
|
|
|
|
} else {
|
2014-03-27 06:48:22 +08:00
|
|
|
LV = CGF.MakeAddrLValue(V, T, Alignment);
|
2011-11-16 08:42:57 +08:00
|
|
|
}
|
2009-11-08 07:06:58 +08:00
|
|
|
setObjCGCLValueClass(CGF.getContext(), E, LV);
|
|
|
|
return LV;
|
|
|
|
}
|
|
|
|
|
2009-11-26 14:08:14 +08:00
|
|
|
static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
|
2011-07-10 13:34:54 +08:00
|
|
|
const Expr *E, const FunctionDecl *FD) {
|
2010-09-06 08:11:41 +08:00
|
|
|
llvm::Value *V = CGF.CGM.GetAddrOfFunction(FD);
|
2009-11-26 14:08:14 +08:00
|
|
|
if (!FD->hasPrototype()) {
|
|
|
|
if (const FunctionProtoType *Proto =
|
|
|
|
FD->getType()->getAs<FunctionProtoType>()) {
|
|
|
|
// Ugly case: for a K&R-style definition, the type of the definition
|
|
|
|
// isn't the same as the type of a use. Correct for this with a
|
|
|
|
// bitcast.
|
|
|
|
QualType NoProtoType =
|
2014-01-26 00:55:45 +08:00
|
|
|
CGF.getContext().getFunctionNoProtoType(Proto->getReturnType());
|
2009-11-26 14:08:14 +08:00
|
|
|
NoProtoType = CGF.getContext().getPointerType(NoProtoType);
|
2011-09-28 05:06:10 +08:00
|
|
|
V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType));
|
2009-11-26 14:08:14 +08:00
|
|
|
}
|
|
|
|
}
|
2011-12-03 12:14:32 +08:00
|
|
|
CharUnits Alignment = CGF.getContext().getDeclAlign(FD);
|
2010-08-21 12:20:22 +08:00
|
|
|
return CGF.MakeAddrLValue(V, E->getType(), Alignment);
|
2009-11-26 14:08:14 +08:00
|
|
|
}
|
|
|
|
|
2013-05-10 03:17:11 +08:00
|
|
|
static LValue EmitCapturedFieldLValue(CodeGenFunction &CGF, const FieldDecl *FD,
|
|
|
|
llvm::Value *ThisValue) {
|
|
|
|
QualType TagType = CGF.getContext().getTagDeclType(FD->getParent());
|
|
|
|
LValue LV = CGF.MakeNaturalAlignAddrLValue(ThisValue, TagType);
|
|
|
|
return CGF.EmitLValueForField(LV, FD);
|
|
|
|
}
|
|
|
|
|
2014-05-20 02:15:42 +08:00
|
|
|
/// Named Registers are named metadata pointing to the register name
|
|
|
|
/// which will be read from/written to as an argument to the intrinsic
|
|
|
|
/// @llvm.read/write_register.
|
|
|
|
/// So far, only the name is being passed down, but other options such as
|
|
|
|
/// register type, allocation type or even optimization options could be
|
|
|
|
/// passed down via the metadata node.
|
|
|
|
static LValue EmitGlobalNamedRegister(const VarDecl *VD,
|
|
|
|
CodeGenModule &CGM,
|
|
|
|
CharUnits Alignment) {
|
2014-05-20 07:25:25 +08:00
|
|
|
SmallString<64> Name("llvm.named.register.");
|
2014-05-20 02:15:42 +08:00
|
|
|
AsmLabelAttr *Asm = VD->getAttr<AsmLabelAttr>();
|
2014-05-20 07:25:25 +08:00
|
|
|
assert(Asm->getLabel().size() < 64-Name.size() &&
|
|
|
|
"Register name too big");
|
|
|
|
Name.append(Asm->getLabel());
|
2014-05-20 06:36:19 +08:00
|
|
|
llvm::NamedMDNode *M =
|
2014-05-20 07:25:25 +08:00
|
|
|
CGM.getModule().getOrInsertNamedMetadata(Name);
|
2014-05-20 02:15:42 +08:00
|
|
|
if (M->getNumOperands() == 0) {
|
|
|
|
llvm::MDString *Str = llvm::MDString::get(CGM.getLLVMContext(),
|
|
|
|
Asm->getLabel());
|
2014-12-10 02:39:32 +08:00
|
|
|
llvm::Metadata *Ops[] = {Str};
|
2014-05-20 02:15:42 +08:00
|
|
|
M->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
|
|
|
|
}
|
2014-12-10 02:39:32 +08:00
|
|
|
return LValue::MakeGlobalReg(
|
|
|
|
llvm::MetadataAsValue::get(CGM.getLLVMContext(), M->getOperand(0)),
|
|
|
|
VD->getType(), Alignment);
|
2014-05-20 02:15:42 +08:00
|
|
|
}
|
|
|
|
|
2007-06-02 13:24:33 +08:00
|
|
|
LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
|
2009-11-08 06:53:10 +08:00
|
|
|
const NamedDecl *ND = E->getDecl();
|
2011-12-03 12:14:32 +08:00
|
|
|
CharUnits Alignment = getContext().getDeclAlign(ND);
|
2011-11-16 08:42:57 +08:00
|
|
|
QualType T = E->getType();
|
2014-05-20 02:15:42 +08:00
|
|
|
|
2014-05-28 00:46:27 +08:00
|
|
|
if (const auto *VD = dyn_cast<VarDecl>(ND)) {
|
|
|
|
// Global Named registers access via intrinsics only
|
|
|
|
if (VD->getStorageClass() == SC_Register &&
|
|
|
|
VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())
|
|
|
|
return EmitGlobalNamedRegister(VD, CGM, Alignment);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2014-05-28 00:46:27 +08:00
|
|
|
// A DeclRefExpr for a reference initialized by a constant expression can
|
|
|
|
// appear without being odr-used. Directly emit the constant initializer.
|
2012-10-20 09:38:33 +08:00
|
|
|
const Expr *Init = VD->getAnyInitializer(VD);
|
|
|
|
if (Init && !isa<ParmVarDecl>(VD) && VD->getType()->isReferenceType() &&
|
|
|
|
VD->isUsableInConstantExpressions(getContext()) &&
|
|
|
|
VD->checkInitIsICE()) {
|
|
|
|
llvm::Constant *Val =
|
|
|
|
CGM.EmitConstantValue(*VD->evaluateValue(), VD->getType(), this);
|
|
|
|
assert(Val && "failed to emit reference constant expression");
|
|
|
|
// FIXME: Eventually we will want to emit vector element references.
|
|
|
|
return MakeAddrLValue(Val, T, Alignment);
|
|
|
|
}
|
2015-01-01 17:49:44 +08:00
|
|
|
|
|
|
|
// Check for captured variables.
|
2015-01-12 18:17:46 +08:00
|
|
|
if (E->refersToEnclosingVariableOrCapture()) {
|
2015-01-01 17:49:44 +08:00
|
|
|
if (auto *FD = LambdaCaptureFields.lookup(VD))
|
|
|
|
return EmitCapturedFieldLValue(*this, FD, CXXABIThisValue);
|
|
|
|
else if (CapturedStmtInfo) {
|
|
|
|
if (auto *V = LocalDeclMap.lookup(VD))
|
|
|
|
return MakeAddrLValue(V, T, Alignment);
|
|
|
|
else
|
|
|
|
return EmitCapturedFieldLValue(*this, CapturedStmtInfo->lookup(VD),
|
|
|
|
CapturedStmtInfo->getContextValue());
|
|
|
|
}
|
|
|
|
assert(isa<BlockDecl>(CurCodeDecl));
|
|
|
|
return MakeAddrLValue(GetAddrOfBlockDecl(VD, VD->hasAttr<BlocksAttr>()),
|
|
|
|
T, Alignment);
|
|
|
|
}
|
2012-10-20 09:38:33 +08:00
|
|
|
}
|
|
|
|
|
2012-01-21 12:52:58 +08:00
|
|
|
// FIXME: We should be able to assert this for FunctionDecls as well!
|
|
|
|
// FIXME: We should be able to assert this for all DeclRefExprs, not just
|
|
|
|
// those with a valid source location.
|
|
|
|
assert((ND->isUsed(false) || !isa<VarDecl>(ND) ||
|
|
|
|
!E->getLocation().isValid()) &&
|
|
|
|
"Should not use decl without marking it used!");
|
|
|
|
|
2010-03-05 02:17:24 +08:00
|
|
|
if (ND->hasAttr<WeakRefAttr>()) {
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *VD = cast<ValueDecl>(ND);
|
2010-03-05 02:17:24 +08:00
|
|
|
llvm::Constant *Aliasee = CGM.GetWeakRefReference(VD);
|
2012-10-20 09:38:33 +08:00
|
|
|
return MakeAddrLValue(Aliasee, T, Alignment);
|
2010-03-05 02:17:24 +08:00
|
|
|
}
|
|
|
|
|
2014-05-28 00:46:27 +08:00
|
|
|
if (const auto *VD = dyn_cast<VarDecl>(ND)) {
|
2009-11-08 06:53:10 +08:00
|
|
|
// Check if this is a global variable.
|
2014-03-27 06:48:22 +08:00
|
|
|
if (VD->hasLinkage() || VD->isStaticDataMember())
|
2009-11-08 07:06:58 +08:00
|
|
|
return EmitGlobalVarDeclLValue(*this, E, VD);
|
2009-11-08 06:43:34 +08:00
|
|
|
|
2012-03-10 17:33:50 +08:00
|
|
|
bool isBlockVariable = VD->hasAttr<BlocksAttr>();
|
|
|
|
|
2013-01-10 09:46:29 +08:00
|
|
|
llvm::Value *V = LocalDeclMap.lookup(VD);
|
2013-07-26 13:59:26 +08:00
|
|
|
if (!V && VD->isStaticLocal())
|
2014-10-08 09:07:54 +08:00
|
|
|
V = CGM.getOrCreateStaticVarDecl(
|
|
|
|
*VD, CGM.getLLVMLinkageVarDefinition(VD, /*isConstant=*/false));
|
2012-02-11 10:57:39 +08:00
|
|
|
|
2014-11-11 12:05:39 +08:00
|
|
|
// Check if variable is threadprivate.
|
|
|
|
if (V && getLangOpts().OpenMP && VD->hasAttr<OMPThreadPrivateDeclAttr>())
|
|
|
|
return EmitThreadPrivateVarDeclLValue(
|
|
|
|
*this, VD, T, V, getTypes().ConvertTypeForMem(VD->getType()),
|
|
|
|
Alignment, E->getExprLoc());
|
|
|
|
|
2009-11-08 06:46:42 +08:00
|
|
|
assert(V && "DeclRefExpr not entered in LocalDeclMap?");
|
2009-09-25 03:53:00 +08:00
|
|
|
|
2012-03-10 17:33:50 +08:00
|
|
|
if (isBlockVariable)
|
2011-01-27 07:08:27 +08:00
|
|
|
V = BuildBlockByrefAddress(V, VD);
|
2010-08-21 11:44:13 +08:00
|
|
|
|
2011-11-16 08:42:57 +08:00
|
|
|
LValue LV;
|
|
|
|
if (VD->getType()->isReferenceType()) {
|
|
|
|
llvm::LoadInst *LI = Builder.CreateLoad(V);
|
2011-12-03 12:14:32 +08:00
|
|
|
LI->setAlignment(Alignment.getQuantity());
|
2011-11-16 08:42:57 +08:00
|
|
|
V = LI;
|
|
|
|
LV = MakeNaturalAlignAddrLValue(V, T);
|
|
|
|
} else {
|
|
|
|
LV = MakeAddrLValue(V, T, Alignment);
|
|
|
|
}
|
2011-07-12 14:52:18 +08:00
|
|
|
|
2013-03-13 11:10:54 +08:00
|
|
|
bool isLocalStorage = VD->hasLocalStorage();
|
|
|
|
|
|
|
|
bool NonGCable = isLocalStorage &&
|
|
|
|
!VD->getType()->isReferenceType() &&
|
|
|
|
!isBlockVariable;
|
2010-11-20 02:17:09 +08:00
|
|
|
if (NonGCable) {
|
2010-08-21 11:44:13 +08:00
|
|
|
LV.getQuals().removeObjCGCAttr();
|
2010-08-21 11:22:38 +08:00
|
|
|
LV.setNonGC(true);
|
|
|
|
}
|
2013-03-13 11:10:54 +08:00
|
|
|
|
|
|
|
bool isImpreciseLifetime =
|
|
|
|
(isLocalStorage && !VD->hasAttr<ObjCPreciseLifetimeAttr>());
|
|
|
|
if (isImpreciseLifetime)
|
|
|
|
LV.setARCPreciseLifetime(ARCImpreciseLifetime);
|
2009-09-17 05:37:16 +08:00
|
|
|
setObjCGCLValueClass(getContext(), E, LV);
|
2008-11-20 08:15:42 +08:00
|
|
|
return LV;
|
2009-10-29 01:39:19 +08:00
|
|
|
}
|
2011-02-03 16:15:49 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(ND))
|
2013-11-05 17:12:18 +08:00
|
|
|
return EmitFunctionDeclLValue(*this, E, FD);
|
2011-02-03 16:15:49 +08:00
|
|
|
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unhandled DeclRefExpr");
|
2007-06-02 13:24:33 +08:00
|
|
|
}
|
2007-06-02 02:02:12 +08:00
|
|
|
|
2007-06-06 04:53:16 +08:00
|
|
|
LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
|
|
|
|
// __extension__ doesn't affect lvalue-ness.
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == UO_Extension)
|
2007-06-06 04:53:16 +08:00
|
|
|
return EmitLValue(E->getSubExpr());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-07-27 06:37:01 +08:00
|
|
|
QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
|
2007-10-31 06:53:42 +08:00
|
|
|
switch (E->getOpcode()) {
|
2011-09-23 13:06:16 +08:00
|
|
|
default: llvm_unreachable("Unknown unary operator lvalue!");
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Deref: {
|
2009-10-29 01:39:19 +08:00
|
|
|
QualType T = E->getSubExpr()->getType()->getPointeeType();
|
|
|
|
assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
|
|
|
|
|
2011-12-20 05:16:08 +08:00
|
|
|
LValue LV = MakeNaturalAlignAddrLValue(EmitScalarExpr(E->getSubExpr()), T);
|
2010-08-21 11:44:13 +08:00
|
|
|
LV.getQuals().setAddressSpace(ExprTy.getAddressSpace());
|
2009-10-29 01:39:19 +08:00
|
|
|
|
|
|
|
// We should not generate __weak write barrier on indirect reference
|
|
|
|
// of a pointer to object; as in void foo (__weak id *param); *param = 0;
|
|
|
|
// But, we continue to generate __strong write barrier on indirect write
|
|
|
|
// into a pointer to object.
|
2012-11-02 06:30:59 +08:00
|
|
|
if (getLangOpts().ObjC1 &&
|
|
|
|
getLangOpts().getGC() != LangOptions::NonGC &&
|
2009-10-29 01:39:19 +08:00
|
|
|
LV.isObjCWeak())
|
2010-08-21 11:22:38 +08:00
|
|
|
LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
|
2009-10-29 01:39:19 +08:00
|
|
|
return LV;
|
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Real:
|
|
|
|
case UO_Imag: {
|
2007-10-31 06:53:42 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
2010-12-05 10:00:02 +08:00
|
|
|
assert(LV.isSimple() && "real/imag on non-ordinary l-value");
|
|
|
|
llvm::Value *Addr = LV.getAddress();
|
|
|
|
|
2012-02-19 04:53:32 +08:00
|
|
|
// __real is valid on scalars. This is a faster way of testing that.
|
|
|
|
// __imag can only produce an rvalue on scalars.
|
|
|
|
if (E->getOpcode() == UO_Real &&
|
|
|
|
!cast<llvm::PointerType>(Addr->getType())
|
2010-12-05 10:00:02 +08:00
|
|
|
->getElementType()->isStructTy()) {
|
|
|
|
assert(E->getSubExpr()->getType()->isArithmeticType());
|
|
|
|
return LV;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(E->getSubExpr()->getType()->isAnyComplexType());
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
unsigned Idx = E->getOpcode() == UO_Imag;
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(Builder.CreateStructGEP(LV.getAddress(),
|
2010-12-05 10:00:02 +08:00
|
|
|
Idx, "idx"),
|
2010-08-21 11:08:16 +08:00
|
|
|
ExprTy);
|
2007-10-31 06:53:42 +08:00
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_PreInc:
|
|
|
|
case UO_PreDec: {
|
2010-01-10 05:44:40 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
2010-08-25 19:45:40 +08:00
|
|
|
bool isInc = E->getOpcode() == UO_PreInc;
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-01-10 05:44:40 +08:00
|
|
|
if (E->getType()->isAnyComplexType())
|
|
|
|
EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
|
|
|
|
else
|
|
|
|
EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
|
|
|
|
return LV;
|
|
|
|
}
|
2009-11-09 12:20:47 +08:00
|
|
|
}
|
2007-06-06 04:53:16 +08:00
|
|
|
}
|
|
|
|
|
2007-06-06 12:54:52 +08:00
|
|
|
LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
|
2010-08-21 11:15:20 +08:00
|
|
|
return MakeAddrLValue(CGM.GetAddrOfConstantStringFromLiteral(E),
|
|
|
|
E->getType());
|
2007-06-06 12:54:52 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
|
2010-08-21 11:15:20 +08:00
|
|
|
return MakeAddrLValue(CGM.GetAddrOfConstantStringFromObjCEncode(E),
|
|
|
|
E->getType());
|
2009-02-25 06:18:39 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 11:01:12 +08:00
|
|
|
LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
|
2014-10-09 16:45:04 +08:00
|
|
|
auto SL = E->getFunctionName();
|
|
|
|
assert(SL != nullptr && "No StringLiteral name in PredefinedExpr");
|
|
|
|
StringRef FnName = CurFn->getName();
|
|
|
|
if (FnName.startswith("\01"))
|
|
|
|
FnName = FnName.substr(1);
|
|
|
|
StringRef NameItems[] = {
|
|
|
|
PredefinedExpr::getIdentTypeName(E->getIdentType()), FnName};
|
|
|
|
std::string GVName = llvm::join(NameItems, NameItems + 2, ".");
|
2014-11-15 07:55:27 +08:00
|
|
|
if (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)) {
|
|
|
|
auto C = CGM.GetAddrOfConstantCString(FnName, GVName.c_str(), 1);
|
|
|
|
return MakeAddrLValue(C, E->getType());
|
|
|
|
}
|
2014-10-09 16:45:04 +08:00
|
|
|
auto C = CGM.GetAddrOfConstantStringFromLiteral(SL, GVName);
|
|
|
|
return MakeAddrLValue(C, E->getType());
|
2007-07-21 13:21:51 +08:00
|
|
|
}
|
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
/// Emit a type description suitable for use by a runtime sanitizer library. The
|
|
|
|
/// format of a type descriptor is
|
|
|
|
///
|
|
|
|
/// \code
|
2012-10-10 07:55:19 +08:00
|
|
|
/// { i16 TypeKind, i16 TypeInfo }
|
2012-10-10 03:52:38 +08:00
|
|
|
/// \endcode
|
|
|
|
///
|
2012-10-10 07:55:19 +08:00
|
|
|
/// followed by an array of i8 containing the type name. TypeKind is 0 for an
|
|
|
|
/// integer, 1 for a floating point value, and -1 for anything else.
|
2012-10-10 03:52:38 +08:00
|
|
|
llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
|
2013-11-08 09:09:22 +08:00
|
|
|
// Only emit each type's descriptor once.
|
2014-05-24 00:07:43 +08:00
|
|
|
if (llvm::Constant *C = CGM.getTypeDescriptorFromMap(T))
|
2013-11-08 09:09:22 +08:00
|
|
|
return C;
|
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
uint16_t TypeKind = -1;
|
|
|
|
uint16_t TypeInfo = 0;
|
|
|
|
|
|
|
|
if (T->isIntegerType()) {
|
|
|
|
TypeKind = 0;
|
|
|
|
TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) |
|
2012-12-01 05:44:01 +08:00
|
|
|
(T->isSignedIntegerType() ? 1 : 0);
|
2012-10-10 03:52:38 +08:00
|
|
|
} else if (T->isFloatingType()) {
|
|
|
|
TypeKind = 1;
|
|
|
|
TypeInfo = getContext().getTypeSize(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the type name as if for a diagnostic, including quotes and
|
|
|
|
// optionally an 'aka'.
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<32> Buffer;
|
2012-10-10 03:52:38 +08:00
|
|
|
CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype,
|
|
|
|
(intptr_t)T.getAsOpaquePtr(),
|
2014-06-12 13:32:35 +08:00
|
|
|
StringRef(), StringRef(), None, Buffer,
|
2014-08-27 14:28:36 +08:00
|
|
|
None);
|
2012-10-10 03:52:38 +08:00
|
|
|
|
|
|
|
llvm::Constant *Components[] = {
|
2012-10-10 07:55:19 +08:00
|
|
|
Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo),
|
|
|
|
llvm::ConstantDataArray::getString(getLLVMContext(), Buffer)
|
2012-10-10 03:52:38 +08:00
|
|
|
};
|
|
|
|
llvm::Constant *Descriptor = llvm::ConstantStruct::getAnon(Components);
|
2009-12-15 08:59:40 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *GV = new llvm::GlobalVariable(
|
|
|
|
CGM.getModule(), Descriptor->getType(),
|
|
|
|
/*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, Descriptor);
|
2012-10-10 03:52:38 +08:00
|
|
|
GV->setUnnamedAddr(true);
|
2014-08-02 05:35:28 +08:00
|
|
|
CGM.getSanitizerMetadata()->disableSanitizerForGlobal(GV);
|
2013-11-08 09:09:22 +08:00
|
|
|
|
|
|
|
// Remember the descriptor for this type.
|
2014-05-24 00:07:43 +08:00
|
|
|
CGM.setTypeDescriptorInMap(T, GV);
|
2013-11-08 09:09:22 +08:00
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
return GV;
|
|
|
|
}
|
2012-09-08 10:08:36 +08:00
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) {
|
|
|
|
llvm::Type *TargetTy = IntPtrTy;
|
|
|
|
|
2013-03-22 08:47:07 +08:00
|
|
|
// Floating-point types which fit into intptr_t are bitcast to integers
|
|
|
|
// and then passed directly (after zero-extension, if necessary).
|
|
|
|
if (V->getType()->isFloatingPointTy()) {
|
|
|
|
unsigned Bits = V->getType()->getPrimitiveSizeInBits();
|
|
|
|
if (Bits <= TargetTy->getIntegerBitWidth())
|
|
|
|
V = Builder.CreateBitCast(V, llvm::Type::getIntNTy(getLLVMContext(),
|
|
|
|
Bits));
|
|
|
|
}
|
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
// Integers which fit in intptr_t are zero-extended and passed directly.
|
|
|
|
if (V->getType()->isIntegerTy() &&
|
|
|
|
V->getType()->getIntegerBitWidth() <= TargetTy->getIntegerBitWidth())
|
|
|
|
return Builder.CreateZExt(V, TargetTy);
|
|
|
|
|
|
|
|
// Pointers are passed directly, everything else is passed by address.
|
|
|
|
if (!V->getType()->isPointerTy()) {
|
2013-03-22 08:47:07 +08:00
|
|
|
llvm::Value *Ptr = CreateTempAlloca(V->getType());
|
2012-10-10 03:52:38 +08:00
|
|
|
Builder.CreateStore(V, Ptr);
|
|
|
|
V = Ptr;
|
|
|
|
}
|
|
|
|
return Builder.CreatePtrToInt(V, TargetTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Emit a representation of a SourceLocation for passing to a handler
|
|
|
|
/// in a sanitizer runtime library. The format for this data is:
|
|
|
|
/// \code
|
|
|
|
/// struct SourceLocation {
|
|
|
|
/// const char *Filename;
|
|
|
|
/// int32_t Line, Column;
|
|
|
|
/// };
|
|
|
|
/// \endcode
|
|
|
|
/// For an invalid SourceLocation, the Filename pointer is null.
|
|
|
|
llvm::Constant *CodeGenFunction::EmitCheckSourceLocation(SourceLocation Loc) {
|
2014-07-19 01:50:06 +08:00
|
|
|
llvm::Constant *Filename;
|
|
|
|
int Line, Column;
|
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
PresumedLoc PLoc = getContext().getSourceManager().getPresumedLoc(Loc);
|
2014-07-19 01:50:06 +08:00
|
|
|
if (PLoc.isValid()) {
|
|
|
|
auto FilenameGV = CGM.GetAddrOfConstantCString(PLoc.getFilename(), ".src");
|
2014-08-02 05:35:28 +08:00
|
|
|
CGM.getSanitizerMetadata()->disableSanitizerForGlobal(FilenameGV);
|
2014-07-19 01:50:06 +08:00
|
|
|
Filename = FilenameGV;
|
|
|
|
Line = PLoc.getLine();
|
|
|
|
Column = PLoc.getColumn();
|
|
|
|
} else {
|
|
|
|
Filename = llvm::Constant::getNullValue(Int8PtrTy);
|
|
|
|
Line = Column = 0;
|
|
|
|
}
|
2012-10-10 03:52:38 +08:00
|
|
|
|
2014-07-19 01:50:06 +08:00
|
|
|
llvm::Constant *Data[] = {Filename, Builder.getInt32(Line),
|
|
|
|
Builder.getInt32(Column)};
|
2012-09-08 10:08:36 +08:00
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
return llvm::ConstantStruct::getAnon(Data);
|
|
|
|
}
|
2012-09-08 10:08:36 +08:00
|
|
|
|
2014-11-11 06:27:30 +08:00
|
|
|
namespace {
|
|
|
|
/// \brief Specify under what conditions this check can be recovered
|
|
|
|
enum class CheckRecoverableKind {
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
/// Always terminate program execution if this check fails.
|
2014-11-11 06:27:30 +08:00
|
|
|
Unrecoverable,
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
/// Check supports recovering, runtime has both fatal (noreturn) and
|
|
|
|
/// non-fatal handlers for this check.
|
2014-11-11 06:27:30 +08:00
|
|
|
Recoverable,
|
|
|
|
/// Runtime conditionally aborts, always need to support recovery.
|
|
|
|
AlwaysRecoverable
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static CheckRecoverableKind getRecoverableKind(SanitizerKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case SanitizerKind::Vptr:
|
|
|
|
return CheckRecoverableKind::AlwaysRecoverable;
|
|
|
|
case SanitizerKind::Return:
|
|
|
|
case SanitizerKind::Unreachable:
|
|
|
|
return CheckRecoverableKind::Unrecoverable;
|
|
|
|
default:
|
|
|
|
return CheckRecoverableKind::Recoverable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
static void emitCheckHandlerCall(CodeGenFunction &CGF,
|
|
|
|
llvm::FunctionType *FnType,
|
|
|
|
ArrayRef<llvm::Value *> FnArgs,
|
|
|
|
StringRef CheckName,
|
|
|
|
CheckRecoverableKind RecoverKind, bool IsFatal,
|
|
|
|
llvm::BasicBlock *ContBB) {
|
|
|
|
assert(IsFatal || RecoverKind != CheckRecoverableKind::Unrecoverable);
|
|
|
|
bool NeedsAbortSuffix =
|
|
|
|
IsFatal && RecoverKind != CheckRecoverableKind::Unrecoverable;
|
|
|
|
std::string FnName = ("__ubsan_handle_" + CheckName +
|
|
|
|
(NeedsAbortSuffix ? "_abort" : "")).str();
|
|
|
|
bool MayReturn =
|
|
|
|
!IsFatal || RecoverKind == CheckRecoverableKind::AlwaysRecoverable;
|
|
|
|
|
|
|
|
llvm::AttrBuilder B;
|
|
|
|
if (!MayReturn) {
|
|
|
|
B.addAttribute(llvm::Attribute::NoReturn)
|
|
|
|
.addAttribute(llvm::Attribute::NoUnwind);
|
|
|
|
}
|
|
|
|
B.addAttribute(llvm::Attribute::UWTable);
|
|
|
|
|
|
|
|
llvm::Value *Fn = CGF.CGM.CreateRuntimeFunction(
|
|
|
|
FnType, FnName,
|
|
|
|
llvm::AttributeSet::get(CGF.getLLVMContext(),
|
|
|
|
llvm::AttributeSet::FunctionIndex, B));
|
|
|
|
llvm::CallInst *HandlerCall = CGF.EmitNounwindRuntimeCall(Fn, FnArgs);
|
|
|
|
if (!MayReturn) {
|
|
|
|
HandlerCall->setDoesNotReturn();
|
|
|
|
CGF.Builder.CreateUnreachable();
|
|
|
|
} else {
|
|
|
|
CGF.Builder.CreateBr(ContBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 06:03:54 +08:00
|
|
|
void CodeGenFunction::EmitCheck(
|
|
|
|
ArrayRef<std::pair<llvm::Value *, SanitizerKind>> Checked,
|
|
|
|
StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
|
|
|
|
ArrayRef<llvm::Value *> DynamicArgs) {
|
2014-07-18 02:46:27 +08:00
|
|
|
assert(IsSanitizerScope);
|
2014-11-12 06:03:54 +08:00
|
|
|
assert(Checked.size() > 0);
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
|
|
|
|
llvm::Value *FatalCond = nullptr;
|
|
|
|
llvm::Value *RecoverableCond = nullptr;
|
|
|
|
for (int i = 0, n = Checked.size(); i < n; ++i) {
|
|
|
|
llvm::Value *Check = Checked[i].first;
|
|
|
|
llvm::Value *&Cond =
|
|
|
|
CGM.getCodeGenOpts().SanitizeRecover.has(Checked[i].second)
|
|
|
|
? RecoverableCond
|
|
|
|
: FatalCond;
|
|
|
|
Cond = Cond ? Builder.CreateAnd(Cond, Check) : Check;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *JointCond;
|
|
|
|
if (FatalCond && RecoverableCond)
|
|
|
|
JointCond = Builder.CreateAnd(FatalCond, RecoverableCond);
|
|
|
|
else
|
|
|
|
JointCond = FatalCond ? FatalCond : RecoverableCond;
|
|
|
|
assert(JointCond);
|
|
|
|
|
2014-11-12 06:03:54 +08:00
|
|
|
CheckRecoverableKind RecoverKind = getRecoverableKind(Checked[0].second);
|
|
|
|
assert(SanOpts.has(Checked[0].second));
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
#ifndef NDEBUG
|
2014-11-12 06:03:54 +08:00
|
|
|
for (int i = 1, n = Checked.size(); i < n; ++i) {
|
|
|
|
assert(RecoverKind == getRecoverableKind(Checked[i].second) &&
|
2014-11-11 06:27:30 +08:00
|
|
|
"All recoverable kinds in a single check must be same!");
|
2014-11-12 06:03:54 +08:00
|
|
|
assert(SanOpts.has(Checked[i].second));
|
|
|
|
}
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
#endif
|
2013-01-30 07:31:22 +08:00
|
|
|
|
|
|
|
if (CGM.getCodeGenOpts().SanitizeUndefinedTrapOnError) {
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
assert(RecoverKind != CheckRecoverableKind::AlwaysRecoverable &&
|
|
|
|
"Runtime call required for AlwaysRecoverable kind!");
|
|
|
|
// Assume that -fsanitize-undefined-trap-on-error overrides
|
|
|
|
// -fsanitize-recover= options, as we can only print meaningful error
|
|
|
|
// message and recover if we have a runtime support.
|
|
|
|
return EmitTrapCheck(JointCond);
|
2013-01-30 07:31:22 +08:00
|
|
|
}
|
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
llvm::BasicBlock *Cont = createBasicBlock("cont");
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
llvm::BasicBlock *Handlers = createBasicBlock("handler." + CheckName);
|
|
|
|
llvm::Instruction *Branch = Builder.CreateCondBr(JointCond, Cont, Handlers);
|
2012-12-15 09:39:14 +08:00
|
|
|
// Give hint that we very much don't expect to execute the handler
|
|
|
|
// Value chosen to match UR_NONTAKEN_WEIGHT, see BranchProbabilityInfo.cpp
|
|
|
|
llvm::MDBuilder MDHelper(getLLVMContext());
|
|
|
|
llvm::MDNode *Node = MDHelper.createBranchWeights((1U << 20) - 1, 1);
|
|
|
|
Branch->setMetadata(llvm::LLVMContext::MD_prof, Node);
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
EmitBlock(Handlers);
|
2012-12-15 09:39:14 +08:00
|
|
|
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
// Emit handler arguments and create handler function type.
|
2012-10-10 03:52:38 +08:00
|
|
|
llvm::Constant *Info = llvm::ConstantStruct::getAnon(StaticArgs);
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *InfoPtr =
|
2013-01-09 11:39:41 +08:00
|
|
|
new llvm::GlobalVariable(CGM.getModule(), Info->getType(), false,
|
2012-10-10 03:52:38 +08:00
|
|
|
llvm::GlobalVariable::PrivateLinkage, Info);
|
|
|
|
InfoPtr->setUnnamedAddr(true);
|
2014-08-02 05:35:28 +08:00
|
|
|
CGM.getSanitizerMetadata()->disableSanitizerForGlobal(InfoPtr);
|
2012-10-10 03:52:38 +08:00
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<llvm::Value *, 4> Args;
|
|
|
|
SmallVector<llvm::Type *, 4> ArgTypes;
|
2012-10-10 03:52:38 +08:00
|
|
|
Args.reserve(DynamicArgs.size() + 1);
|
|
|
|
ArgTypes.reserve(DynamicArgs.size() + 1);
|
|
|
|
|
|
|
|
// Handler functions take an i8* pointing to the (handler-specific) static
|
|
|
|
// information block, followed by a sequence of intptr_t arguments
|
|
|
|
// representing operand values.
|
|
|
|
Args.push_back(Builder.CreateBitCast(InfoPtr, Int8PtrTy));
|
|
|
|
ArgTypes.push_back(Int8PtrTy);
|
|
|
|
for (size_t i = 0, n = DynamicArgs.size(); i != n; ++i) {
|
|
|
|
Args.push_back(EmitCheckValue(DynamicArgs[i]));
|
|
|
|
ArgTypes.push_back(IntPtrTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::FunctionType *FnType =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, ArgTypes, false);
|
2012-12-03 03:50:33 +08:00
|
|
|
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
if (!FatalCond || !RecoverableCond) {
|
|
|
|
// Simple case: we need to generate a single handler call, either
|
|
|
|
// fatal, or non-fatal.
|
|
|
|
emitCheckHandlerCall(*this, FnType, Args, CheckName, RecoverKind,
|
|
|
|
(FatalCond != nullptr), Cont);
|
2012-10-25 10:14:12 +08:00
|
|
|
} else {
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
// Emit two handler calls: first one for set of unrecoverable checks,
|
|
|
|
// another one for recoverable.
|
|
|
|
llvm::BasicBlock *NonFatalHandlerBB =
|
|
|
|
createBasicBlock("non_fatal." + CheckName);
|
|
|
|
llvm::BasicBlock *FatalHandlerBB = createBasicBlock("fatal." + CheckName);
|
|
|
|
Builder.CreateCondBr(FatalCond, NonFatalHandlerBB, FatalHandlerBB);
|
|
|
|
EmitBlock(FatalHandlerBB);
|
|
|
|
emitCheckHandlerCall(*this, FnType, Args, CheckName, RecoverKind, true,
|
|
|
|
NonFatalHandlerBB);
|
|
|
|
EmitBlock(NonFatalHandlerBB);
|
|
|
|
emitCheckHandlerCall(*this, FnType, Args, CheckName, RecoverKind, false,
|
|
|
|
Cont);
|
2012-10-25 10:14:12 +08:00
|
|
|
}
|
2012-10-10 03:52:38 +08:00
|
|
|
|
2012-09-08 10:08:36 +08:00
|
|
|
EmitBlock(Cont);
|
2009-12-12 09:27:46 +08:00
|
|
|
}
|
|
|
|
|
2013-01-30 07:31:22 +08:00
|
|
|
void CodeGenFunction::EmitTrapCheck(llvm::Value *Checked) {
|
2012-11-02 06:15:34 +08:00
|
|
|
llvm::BasicBlock *Cont = createBasicBlock("cont");
|
|
|
|
|
|
|
|
// If we're optimizing, collapse all calls to trap down to just one per
|
|
|
|
// function to save on code size.
|
|
|
|
if (!CGM.getCodeGenOpts().OptimizationLevel || !TrapBB) {
|
|
|
|
TrapBB = createBasicBlock("trap");
|
|
|
|
Builder.CreateCondBr(Checked, Cont, TrapBB);
|
|
|
|
EmitBlock(TrapBB);
|
|
|
|
llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap);
|
|
|
|
llvm::CallInst *TrapCall = Builder.CreateCall(F);
|
|
|
|
TrapCall->setDoesNotReturn();
|
|
|
|
TrapCall->setDoesNotThrow();
|
|
|
|
Builder.CreateUnreachable();
|
|
|
|
} else {
|
|
|
|
Builder.CreateCondBr(Checked, Cont, TrapBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitBlock(Cont);
|
|
|
|
}
|
|
|
|
|
2010-06-27 07:03:20 +08:00
|
|
|
/// isSimpleArrayDecayOperand - If the specified expr is a simple decay from an
|
|
|
|
/// array to pointer, return the array subexpression.
|
|
|
|
static const Expr *isSimpleArrayDecayOperand(const Expr *E) {
|
|
|
|
// If this isn't just an array->pointer decay, bail out.
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *CE = dyn_cast<CastExpr>(E);
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!CE || CE->getCastKind() != CK_ArrayToPointerDecay)
|
2014-06-09 10:04:02 +08:00
|
|
|
return nullptr;
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-06-27 07:03:20 +08:00
|
|
|
// If this is a decay from variable width array, bail out.
|
|
|
|
const Expr *SubExpr = CE->getSubExpr();
|
|
|
|
if (SubExpr->getType()->isVariableArrayType())
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-06-27 07:03:20 +08:00
|
|
|
return SubExpr;
|
|
|
|
}
|
|
|
|
|
2013-02-23 10:53:19 +08:00
|
|
|
LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
|
|
|
|
bool Accessed) {
|
2007-08-21 00:18:38 +08:00
|
|
|
// The index must always be an integer, which is not an aggregate. Emit it.
|
2007-08-24 13:35:26 +08:00
|
|
|
llvm::Value *Idx = EmitScalarExpr(E->getIdx());
|
2009-06-07 03:09:26 +08:00
|
|
|
QualType IdxTy = E->getIdx()->getType();
|
2011-05-21 00:38:50 +08:00
|
|
|
bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
|
2009-06-07 03:09:26 +08:00
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (SanOpts.has(SanitizerKind::ArrayBounds))
|
2013-02-23 10:53:19 +08:00
|
|
|
EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, Accessed);
|
|
|
|
|
2007-07-11 05:17:59 +08:00
|
|
|
// If the base is a vector type, then we are forming a vector element lvalue
|
|
|
|
// with this subscript.
|
2014-08-20 01:17:40 +08:00
|
|
|
if (E->getBase()->getType()->isVectorType() &&
|
|
|
|
!isa<ExtVectorElementExpr>(E->getBase())) {
|
2007-07-11 05:17:59 +08:00
|
|
|
// Emit the vector as an lvalue to get its address.
|
2008-06-14 07:01:12 +08:00
|
|
|
LValue LHS = EmitLValue(E->getBase());
|
2007-08-21 00:18:38 +08:00
|
|
|
assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
|
2008-06-14 07:01:12 +08:00
|
|
|
return LValue::MakeVectorElt(LHS.getAddress(), Idx,
|
2012-03-23 06:36:39 +08:00
|
|
|
E->getBase()->getType(), LHS.getAlignment());
|
2007-07-11 05:17:59 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-21 00:18:38 +08:00
|
|
|
// Extend or truncate the index type to 32 or 64-bits.
|
2011-02-15 17:22:45 +08:00
|
|
|
if (Idx->getType() != IntPtrTy)
|
|
|
|
Idx = Builder.CreateIntCast(Idx, IntPtrTy, IdxSigned, "idxprom");
|
2009-12-12 09:27:46 +08:00
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
// We know that the pointer points to a type of the correct size, unless the
|
|
|
|
// size is a VLA or Objective-C interface.
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *Address = nullptr;
|
2011-12-03 12:14:32 +08:00
|
|
|
CharUnits ArrayAlignment;
|
2014-08-20 01:17:40 +08:00
|
|
|
if (isa<ExtVectorElementExpr>(E->getBase())) {
|
|
|
|
LValue LV = EmitLValue(E->getBase());
|
|
|
|
Address = EmitExtVectorElementLValue(LV);
|
|
|
|
Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx");
|
|
|
|
const VectorType *ExprVT = LV.getType()->getAs<VectorType>();
|
|
|
|
QualType EQT = ExprVT->getElementType();
|
|
|
|
return MakeAddrLValue(Address, EQT,
|
|
|
|
getContext().getTypeAlignInChars(EQT));
|
|
|
|
}
|
|
|
|
else if (const VariableArrayType *vla =
|
|
|
|
getContext().getAsVariableArrayType(E->getType())) {
|
2011-06-25 05:55:10 +08:00
|
|
|
// The base must be a pointer, which is not an aggregate. Emit
|
|
|
|
// it. It needs to be emitted first in case it's what captures
|
|
|
|
// the VLA bounds.
|
|
|
|
Address = EmitScalarExpr(E->getBase());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
// The element count here is the total number of non-VLA elements.
|
|
|
|
llvm::Value *numElements = getVLASize(vla).first;
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
// Effectively, the multiply by the VLA size is part of the GEP.
|
|
|
|
// GEP indexes are signed, and scaling an index isn't permitted to
|
|
|
|
// signed-overflow, so we use the same semantics for our explicit
|
|
|
|
// multiply. We suppress this if overflow is not undefined behavior.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().isSignedOverflowDefined()) {
|
2011-06-25 09:32:37 +08:00
|
|
|
Idx = Builder.CreateMul(Idx, numElements);
|
2011-03-01 08:03:48 +08:00
|
|
|
Address = Builder.CreateGEP(Address, Idx, "arrayidx");
|
2011-06-25 09:32:37 +08:00
|
|
|
} else {
|
|
|
|
Idx = Builder.CreateNSWMul(Idx, numElements);
|
2011-03-01 08:03:48 +08:00
|
|
|
Address = Builder.CreateInBoundsGEP(Address, Idx, "arrayidx");
|
2011-06-25 09:32:37 +08:00
|
|
|
}
|
2010-06-27 07:03:20 +08:00
|
|
|
} else if (const ObjCObjectType *OIT = E->getType()->getAs<ObjCObjectType>()){
|
|
|
|
// Indexing over an interface, as in "NSString *P; P[4];"
|
2009-09-09 21:00:44 +08:00
|
|
|
llvm::Value *InterfaceSize =
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(Idx->getType(),
|
2010-01-12 01:06:35 +08:00
|
|
|
getContext().getTypeSizeInChars(OIT).getQuantity());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-04-25 13:08:32 +08:00
|
|
|
Idx = Builder.CreateMul(Idx, InterfaceSize);
|
|
|
|
|
2010-06-27 07:03:20 +08:00
|
|
|
// The base must be a pointer, which is not an aggregate. Emit it.
|
|
|
|
llvm::Value *Base = EmitScalarExpr(E->getBase());
|
2011-02-08 16:22:06 +08:00
|
|
|
Address = EmitCastToVoidPtr(Base);
|
|
|
|
Address = Builder.CreateGEP(Address, Idx, "arrayidx");
|
2009-04-25 13:08:32 +08:00
|
|
|
Address = Builder.CreateBitCast(Address, Base->getType());
|
2010-06-27 07:03:20 +08:00
|
|
|
} else if (const Expr *Array = isSimpleArrayDecayOperand(E->getBase())) {
|
|
|
|
// If this is A[i] where A is an array, the frontend will have decayed the
|
|
|
|
// base to be a ArrayToPointerDecay implicit cast. While correct, it is
|
|
|
|
// inefficient at -O0 to emit a "gep A, 0, 0" when codegen'ing it, then a
|
|
|
|
// "gep x, i" here. Emit one "gep A, 0, i".
|
|
|
|
assert(Array->getType()->isArrayType() &&
|
|
|
|
"Array to pointer decay must have array source type!");
|
2013-02-23 10:53:19 +08:00
|
|
|
LValue ArrayLV;
|
|
|
|
// For simple multidimensional array indexing, set the 'accessed' flag for
|
|
|
|
// better bounds-checking of the base expression.
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(Array))
|
2013-02-23 10:53:19 +08:00
|
|
|
ArrayLV = EmitArraySubscriptExpr(ASE, /*Accessed*/ true);
|
|
|
|
else
|
|
|
|
ArrayLV = EmitLValue(Array);
|
2011-04-01 08:49:43 +08:00
|
|
|
llvm::Value *ArrayPtr = ArrayLV.getAddress();
|
2010-06-27 07:03:20 +08:00
|
|
|
llvm::Value *Zero = llvm::ConstantInt::get(Int32Ty, 0);
|
|
|
|
llvm::Value *Args[] = { Zero, Idx };
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2011-04-01 08:49:43 +08:00
|
|
|
// Propagate the alignment from the array itself to the result.
|
|
|
|
ArrayAlignment = ArrayLV.getAlignment();
|
|
|
|
|
2012-11-02 06:30:59 +08:00
|
|
|
if (getLangOpts().isSignedOverflowDefined())
|
2011-07-22 16:16:57 +08:00
|
|
|
Address = Builder.CreateGEP(ArrayPtr, Args, "arrayidx");
|
2011-03-01 08:03:48 +08:00
|
|
|
else
|
2011-07-22 16:16:57 +08:00
|
|
|
Address = Builder.CreateInBoundsGEP(ArrayPtr, Args, "arrayidx");
|
2009-04-25 13:08:32 +08:00
|
|
|
} else {
|
2010-06-27 07:03:20 +08:00
|
|
|
// The base must be a pointer, which is not an aggregate. Emit it.
|
|
|
|
llvm::Value *Base = EmitScalarExpr(E->getBase());
|
2012-11-02 06:30:59 +08:00
|
|
|
if (getLangOpts().isSignedOverflowDefined())
|
2011-03-01 08:03:48 +08:00
|
|
|
Address = Builder.CreateGEP(Base, Idx, "arrayidx");
|
|
|
|
else
|
|
|
|
Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
|
2008-12-21 08:11:23 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-07-11 07:34:53 +08:00
|
|
|
QualType T = E->getBase()->getType()->getPointeeType();
|
2009-09-09 21:00:44 +08:00
|
|
|
assert(!T.isNull() &&
|
2009-07-11 07:34:53 +08:00
|
|
|
"CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2011-04-01 08:49:43 +08:00
|
|
|
// Limit the alignment to that of the result type.
|
2012-01-05 06:35:55 +08:00
|
|
|
LValue LV;
|
2011-12-03 12:14:32 +08:00
|
|
|
if (!ArrayAlignment.isZero()) {
|
|
|
|
CharUnits Align = getContext().getTypeAlignInChars(T);
|
2011-04-01 08:49:43 +08:00
|
|
|
ArrayAlignment = std::min(Align, ArrayAlignment);
|
2012-01-05 06:35:55 +08:00
|
|
|
LV = MakeAddrLValue(Address, T, ArrayAlignment);
|
|
|
|
} else {
|
|
|
|
LV = MakeNaturalAlignAddrLValue(Address, T);
|
2011-04-01 08:49:43 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 11:44:13 +08:00
|
|
|
LV.getQuals().setAddressSpace(E->getBase()->getType().getAddressSpace());
|
2009-09-25 03:53:00 +08:00
|
|
|
|
2012-11-02 06:30:59 +08:00
|
|
|
if (getLangOpts().ObjC1 &&
|
|
|
|
getLangOpts().getGC() != LangOptions::NonGC) {
|
2010-08-21 11:22:38 +08:00
|
|
|
LV.setNonGC(!E->isOBJCGCCandidate(getContext()));
|
2009-09-17 05:37:16 +08:00
|
|
|
setObjCGCLValueClass(getContext(), E, LV);
|
|
|
|
}
|
2009-02-22 07:37:19 +08:00
|
|
|
return LV;
|
2007-06-09 07:31:14 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
static
|
2012-01-25 16:58:21 +08:00
|
|
|
llvm::Constant *GenerateConstantVector(CGBuilderTy &Builder,
|
2013-07-06 03:34:19 +08:00
|
|
|
SmallVectorImpl<unsigned> &Elts) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant*, 4> CElts;
|
2008-05-14 05:03:02 +08:00
|
|
|
for (unsigned i = 0, e = Elts.size(); i != e; ++i)
|
2012-01-25 13:34:41 +08:00
|
|
|
CElts.push_back(Builder.getInt32(Elts[i]));
|
2008-05-14 05:03:02 +08:00
|
|
|
|
2011-02-15 08:14:06 +08:00
|
|
|
return llvm::ConstantVector::get(CElts);
|
2008-05-14 05:03:02 +08:00
|
|
|
}
|
|
|
|
|
2007-08-03 07:37:31 +08:00
|
|
|
LValue CodeGenFunction::
|
2008-04-19 07:10:10 +08:00
|
|
|
EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
|
2007-08-03 07:37:31 +08:00
|
|
|
// Emit the base vector as an l-value.
|
2009-02-17 05:11:58 +08:00
|
|
|
LValue Base;
|
|
|
|
|
|
|
|
// ExtVectorElementExpr's base can either be a vector or pointer to vector.
|
2009-12-24 05:31:11 +08:00
|
|
|
if (E->isArrow()) {
|
|
|
|
// If it is a pointer to a vector, emit the address and form an lvalue with
|
|
|
|
// it.
|
2009-02-17 06:14:05 +08:00
|
|
|
llvm::Value *Ptr = EmitScalarExpr(E->getBase());
|
2009-12-24 05:31:11 +08:00
|
|
|
const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
|
2010-08-21 11:44:13 +08:00
|
|
|
Base = MakeAddrLValue(Ptr, PT->getPointeeType());
|
|
|
|
Base.getQuals().removeObjCGCAttr();
|
2010-11-24 13:12:34 +08:00
|
|
|
} else if (E->getBase()->isGLValue()) {
|
2009-12-24 05:31:11 +08:00
|
|
|
// Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
|
|
|
|
// emit the base as an lvalue.
|
|
|
|
assert(E->getBase()->getType()->isVectorType());
|
|
|
|
Base = EmitLValue(E->getBase());
|
|
|
|
} else {
|
|
|
|
// Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
|
2011-06-16 12:16:24 +08:00
|
|
|
assert(E->getBase()->getType()->isVectorType() &&
|
2010-01-05 02:02:28 +08:00
|
|
|
"Result must be a vector");
|
2009-12-24 05:31:11 +08:00
|
|
|
llvm::Value *Vec = EmitScalarExpr(E->getBase());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2009-12-24 05:33:41 +08:00
|
|
|
// Store the vector to memory (because LValue wants an address).
|
2010-02-09 10:48:28 +08:00
|
|
|
llvm::Value *VecMem = CreateMemTemp(E->getBase()->getType());
|
2009-12-24 05:31:11 +08:00
|
|
|
Builder.CreateStore(Vec, VecMem);
|
2010-08-21 11:44:13 +08:00
|
|
|
Base = MakeAddrLValue(VecMem, E->getBase()->getType());
|
2009-02-17 05:11:58 +08:00
|
|
|
}
|
2011-06-16 12:16:24 +08:00
|
|
|
|
|
|
|
QualType type =
|
|
|
|
E->getType().withCVRQualifiers(Base.getQuals().getCVRQualifiers());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2008-05-14 05:03:02 +08:00
|
|
|
// Encode the element access list into a vector of unsigned indices.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<unsigned, 4> Indices;
|
2008-05-14 05:03:02 +08:00
|
|
|
E->getEncodedElementAccess(Indices);
|
|
|
|
|
|
|
|
if (Base.isSimple()) {
|
2012-01-25 13:34:41 +08:00
|
|
|
llvm::Constant *CV = GenerateConstantVector(Builder, Indices);
|
2012-03-23 06:36:39 +08:00
|
|
|
return LValue::MakeExtVectorElt(Base.getAddress(), CV, type,
|
|
|
|
Base.getAlignment());
|
2008-05-09 14:41:27 +08:00
|
|
|
}
|
2008-05-14 05:03:02 +08:00
|
|
|
assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
|
|
|
|
|
|
|
|
llvm::Constant *BaseElts = Base.getExtVectorElts();
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant *, 4> CElts;
|
2007-08-03 07:37:31 +08:00
|
|
|
|
2012-01-30 14:20:36 +08:00
|
|
|
for (unsigned i = 0, e = Indices.size(); i != e; ++i)
|
|
|
|
CElts.push_back(BaseElts->getAggregateElement(Indices[i]));
|
2011-02-15 08:14:06 +08:00
|
|
|
llvm::Constant *CV = llvm::ConstantVector::get(CElts);
|
2012-03-23 06:36:39 +08:00
|
|
|
return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV, type,
|
|
|
|
Base.getAlignment());
|
2007-08-03 07:37:31 +08:00
|
|
|
}
|
|
|
|
|
2007-10-24 04:28:39 +08:00
|
|
|
LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
|
2007-10-25 06:26:28 +08:00
|
|
|
Expr *BaseExpr = E->getBase();
|
2008-06-14 07:01:12 +08:00
|
|
|
|
2007-12-03 02:52:07 +08:00
|
|
|
// If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
|
2012-04-16 11:54:45 +08:00
|
|
|
LValue BaseLV;
|
2012-08-24 08:54:33 +08:00
|
|
|
if (E->isArrow()) {
|
|
|
|
llvm::Value *Ptr = EmitScalarExpr(BaseExpr);
|
|
|
|
QualType PtrTy = BaseExpr->getType()->getPointeeType();
|
2012-10-10 03:52:38 +08:00
|
|
|
EmitTypeCheck(TCK_MemberAccess, E->getExprLoc(), Ptr, PtrTy);
|
2012-08-24 08:54:33 +08:00
|
|
|
BaseLV = MakeNaturalAlignAddrLValue(Ptr, PtrTy);
|
|
|
|
} else
|
2012-09-08 10:08:36 +08:00
|
|
|
BaseLV = EmitCheckedLValue(BaseExpr, TCK_MemberAccess);
|
2007-10-24 04:28:39 +08:00
|
|
|
|
2009-11-08 07:06:58 +08:00
|
|
|
NamedDecl *ND = E->getMemberDecl();
|
2014-05-09 08:08:36 +08:00
|
|
|
if (auto *Field = dyn_cast<FieldDecl>(ND)) {
|
2012-04-16 11:54:45 +08:00
|
|
|
LValue LV = EmitLValueForField(BaseLV, Field);
|
2009-11-08 07:06:58 +08:00
|
|
|
setObjCGCLValueClass(getContext(), E, LV);
|
|
|
|
return LV;
|
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (auto *VD = dyn_cast<VarDecl>(ND))
|
2009-11-08 07:16:50 +08:00
|
|
|
return EmitGlobalVarDeclLValue(*this, E, VD);
|
2009-11-26 14:08:14 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(ND))
|
2009-11-26 14:08:14 +08:00
|
|
|
return EmitFunctionDeclLValue(*this, E, FD);
|
|
|
|
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unhandled member declaration!");
|
2008-02-09 16:50:58 +08:00
|
|
|
}
|
2007-10-24 04:28:39 +08:00
|
|
|
|
2013-05-03 15:33:41 +08:00
|
|
|
/// Given that we are currently emitting a lambda, emit an l-value for
|
|
|
|
/// one of its members.
|
|
|
|
LValue CodeGenFunction::EmitLValueForLambdaField(const FieldDecl *Field) {
|
|
|
|
assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent()->isLambda());
|
|
|
|
assert(cast<CXXMethodDecl>(CurCodeDecl)->getParent() == Field->getParent());
|
|
|
|
QualType LambdaTagType =
|
|
|
|
getContext().getTagDeclType(Field->getParent());
|
|
|
|
LValue LambdaLV = MakeNaturalAlignAddrLValue(CXXABIThisValue, LambdaTagType);
|
|
|
|
return EmitLValueForField(LambdaLV, Field);
|
|
|
|
}
|
|
|
|
|
2012-04-16 11:54:45 +08:00
|
|
|
LValue CodeGenFunction::EmitLValueForField(LValue base,
|
|
|
|
const FieldDecl *field) {
|
2012-06-28 05:19:48 +08:00
|
|
|
if (field->isBitField()) {
|
|
|
|
const CGRecordLayout &RL =
|
|
|
|
CGM.getTypes().getCGRecordLayout(field->getParent());
|
|
|
|
const CGBitFieldInfo &Info = RL.getBitFieldInfo(field);
|
2012-12-06 19:14:44 +08:00
|
|
|
llvm::Value *Addr = base.getAddress();
|
|
|
|
unsigned Idx = RL.getLLVMFieldNo(field);
|
|
|
|
if (Idx != 0)
|
|
|
|
// For structs, we GEP to the field that the record layout suggests.
|
|
|
|
Addr = Builder.CreateStructGEP(Addr, Idx, field->getName());
|
|
|
|
// Get the access type.
|
|
|
|
llvm::Type *PtrTy = llvm::Type::getIntNPtrTy(
|
|
|
|
getLLVMContext(), Info.StorageSize,
|
|
|
|
CGM.getContext().getTargetAddressSpace(base.getType()));
|
|
|
|
if (Addr->getType() != PtrTy)
|
|
|
|
Addr = Builder.CreateBitCast(Addr, PtrTy);
|
|
|
|
|
2012-06-28 05:19:48 +08:00
|
|
|
QualType fieldType =
|
|
|
|
field->getType().withCVRQualifiers(base.getVRQualifiers());
|
2012-12-06 19:14:44 +08:00
|
|
|
return LValue::MakeBitfield(Addr, Info, fieldType, base.getAlignment());
|
2012-06-28 05:19:48 +08:00
|
|
|
}
|
2011-02-26 16:07:02 +08:00
|
|
|
|
|
|
|
const RecordDecl *rec = field->getParent();
|
|
|
|
QualType type = field->getType();
|
2011-12-03 12:14:32 +08:00
|
|
|
CharUnits alignment = getContext().getDeclAlign(field);
|
2011-02-26 16:07:02 +08:00
|
|
|
|
2012-04-16 11:54:45 +08:00
|
|
|
// FIXME: It should be impossible to have an LValue without alignment for a
|
|
|
|
// complete type.
|
|
|
|
if (!base.getAlignment().isZero())
|
|
|
|
alignment = std::min(alignment, base.getAlignment());
|
|
|
|
|
2011-02-26 16:07:02 +08:00
|
|
|
bool mayAlias = rec->hasAttr<MayAliasAttr>();
|
|
|
|
|
2012-04-16 11:54:45 +08:00
|
|
|
llvm::Value *addr = base.getAddress();
|
|
|
|
unsigned cvr = base.getVRQualifiers();
|
2013-04-05 05:53:22 +08:00
|
|
|
bool TBAAPath = CGM.getCodeGenOpts().StructPathTBAA;
|
2011-02-26 16:07:02 +08:00
|
|
|
if (rec->isUnion()) {
|
2011-07-10 13:34:54 +08:00
|
|
|
// For unions, there is no pointer adjustment.
|
2011-02-26 16:07:02 +08:00
|
|
|
assert(!type->isReferenceType() && "union has reference member");
|
2013-04-05 05:53:22 +08:00
|
|
|
// TODO: handle path-aware TBAA for union.
|
|
|
|
TBAAPath = false;
|
2011-02-26 16:07:02 +08:00
|
|
|
} else {
|
|
|
|
// For structs, we GEP to the field that the record layout suggests.
|
|
|
|
unsigned idx = CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
|
2011-07-10 13:34:54 +08:00
|
|
|
addr = Builder.CreateStructGEP(addr, idx, field->getName());
|
2011-02-26 16:07:02 +08:00
|
|
|
|
|
|
|
// If this is a reference field, load the reference right now.
|
|
|
|
if (const ReferenceType *refType = type->getAs<ReferenceType>()) {
|
|
|
|
llvm::LoadInst *load = Builder.CreateLoad(addr, "ref");
|
|
|
|
if (cvr & Qualifiers::Volatile) load->setVolatile(true);
|
2011-12-03 12:14:32 +08:00
|
|
|
load->setAlignment(alignment.getQuantity());
|
2011-02-26 16:07:02 +08:00
|
|
|
|
2013-04-05 05:53:22 +08:00
|
|
|
// Loading the reference will disable path-aware TBAA.
|
|
|
|
TBAAPath = false;
|
2011-02-26 16:07:02 +08:00
|
|
|
if (CGM.shouldUseTBAA()) {
|
|
|
|
llvm::MDNode *tbaa;
|
|
|
|
if (mayAlias)
|
|
|
|
tbaa = CGM.getTBAAInfo(getContext().CharTy);
|
|
|
|
else
|
|
|
|
tbaa = CGM.getTBAAInfo(type);
|
2013-10-08 08:08:49 +08:00
|
|
|
if (tbaa)
|
|
|
|
CGM.DecorateInstruction(load, tbaa);
|
2011-02-26 16:07:02 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-02-26 16:07:02 +08:00
|
|
|
addr = load;
|
|
|
|
mayAlias = false;
|
|
|
|
type = refType->getPointeeType();
|
2011-11-16 08:42:57 +08:00
|
|
|
if (type->isIncompleteType())
|
2011-12-03 12:14:32 +08:00
|
|
|
alignment = CharUnits();
|
2011-11-16 08:42:57 +08:00
|
|
|
else
|
2011-12-03 12:14:32 +08:00
|
|
|
alignment = getContext().getTypeAlignInChars(type);
|
2011-02-26 16:07:02 +08:00
|
|
|
cvr = 0; // qualifiers don't recursively apply to referencee
|
|
|
|
}
|
2007-10-27 03:42:18 +08:00
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2011-07-10 13:34:54 +08:00
|
|
|
// Make sure that the address is pointing to the right type. This is critical
|
|
|
|
// for both unions and structs. A union needs a bitcast, a struct element
|
|
|
|
// will need a bitcast if the LLVM type laid out doesn't match the desired
|
|
|
|
// type.
|
2011-07-12 16:58:26 +08:00
|
|
|
addr = EmitBitCastOfLValueToProperType(*this, addr,
|
2011-07-12 14:52:18 +08:00
|
|
|
CGM.getTypes().ConvertTypeForMem(type),
|
|
|
|
field->getName());
|
2009-09-25 03:53:00 +08:00
|
|
|
|
2011-09-10 06:41:49 +08:00
|
|
|
if (field->hasAttr<AnnotateAttr>())
|
|
|
|
addr = EmitFieldAnnotations(field, addr);
|
|
|
|
|
2011-02-26 16:07:02 +08:00
|
|
|
LValue LV = MakeAddrLValue(addr, type, alignment);
|
|
|
|
LV.getQuals().addCVRQualifiers(cvr);
|
2013-04-05 05:53:22 +08:00
|
|
|
if (TBAAPath) {
|
|
|
|
const ASTRecordLayout &Layout =
|
|
|
|
getContext().getASTRecordLayout(field->getParent());
|
|
|
|
// Set the base type to be the base type of the base LValue and
|
|
|
|
// update offset to be relative to the base type.
|
2013-04-27 08:39:37 +08:00
|
|
|
LV.setTBAABaseType(mayAlias ? getContext().CharTy : base.getTBAABaseType());
|
|
|
|
LV.setTBAAOffset(mayAlias ? 0 : base.getTBAAOffset() +
|
2013-04-05 05:53:22 +08:00
|
|
|
Layout.getFieldOffset(field->getFieldIndex()) /
|
|
|
|
getContext().getCharWidth());
|
|
|
|
}
|
2010-08-21 11:44:13 +08:00
|
|
|
|
2009-09-22 02:54:29 +08:00
|
|
|
// __weak attribute on a field is ignored.
|
2010-08-21 11:44:13 +08:00
|
|
|
if (LV.getQuals().getObjCGCAttr() == Qualifiers::Weak)
|
|
|
|
LV.getQuals().removeObjCGCAttr();
|
2011-02-26 16:07:02 +08:00
|
|
|
|
|
|
|
// Fields of may_alias structs act like 'char' for TBAA purposes.
|
|
|
|
// FIXME: this should get propagated down through anonymous structs
|
|
|
|
// and unions.
|
|
|
|
if (mayAlias && LV.getTBAAInfo())
|
|
|
|
LV.setTBAAInfo(CGM.getTBAAInfo(getContext().CharTy));
|
|
|
|
|
2010-08-21 11:44:13 +08:00
|
|
|
return LV;
|
2007-10-24 04:28:39 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 13:59:26 +08:00
|
|
|
LValue
|
|
|
|
CodeGenFunction::EmitLValueForFieldInitialization(LValue Base,
|
2012-04-16 11:54:45 +08:00
|
|
|
const FieldDecl *Field) {
|
2010-01-29 13:24:29 +08:00
|
|
|
QualType FieldType = Field->getType();
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-01-29 13:24:29 +08:00
|
|
|
if (!FieldType->isReferenceType())
|
2012-04-16 11:54:45 +08:00
|
|
|
return EmitLValueForField(Base, Field);
|
2010-01-29 13:24:29 +08:00
|
|
|
|
2010-03-31 09:09:11 +08:00
|
|
|
const CGRecordLayout &RL =
|
|
|
|
CGM.getTypes().getCGRecordLayout(Field->getParent());
|
|
|
|
unsigned idx = RL.getLLVMFieldNo(Field);
|
2012-04-16 11:54:45 +08:00
|
|
|
llvm::Value *V = Builder.CreateStructGEP(Base.getAddress(), idx);
|
2010-01-29 13:24:29 +08:00
|
|
|
assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
|
|
|
|
|
2011-07-10 13:53:24 +08:00
|
|
|
// Make sure that the address is pointing to the right type. This is critical
|
|
|
|
// for both unions and structs. A union needs a bitcast, a struct element
|
|
|
|
// will need a bitcast if the LLVM type laid out doesn't match the desired
|
|
|
|
// type.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *llvmType = ConvertTypeForMem(FieldType);
|
2012-04-16 11:54:45 +08:00
|
|
|
V = EmitBitCastOfLValueToProperType(*this, V, llvmType, Field->getName());
|
|
|
|
|
2011-12-03 12:14:32 +08:00
|
|
|
CharUnits Alignment = getContext().getDeclAlign(Field);
|
2012-04-16 11:54:45 +08:00
|
|
|
|
|
|
|
// FIXME: It should be impossible to have an LValue without alignment for a
|
|
|
|
// complete type.
|
|
|
|
if (!Base.getAlignment().isZero())
|
|
|
|
Alignment = std::min(Alignment, Base.getAlignment());
|
|
|
|
|
2010-08-21 12:20:22 +08:00
|
|
|
return MakeAddrLValue(V, FieldType, Alignment);
|
2010-01-29 13:24:29 +08:00
|
|
|
}
|
|
|
|
|
2010-09-06 08:11:41 +08:00
|
|
|
LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr *E){
|
2011-11-23 06:48:32 +08:00
|
|
|
if (E->isFileScope()) {
|
|
|
|
llvm::Value *GlobalPtr = CGM.GetAddrOfConstantCompoundLiteral(E);
|
|
|
|
return MakeAddrLValue(GlobalPtr, E->getType());
|
|
|
|
}
|
2012-06-08 02:15:55 +08:00
|
|
|
if (E->getType()->isVariablyModifiedType())
|
|
|
|
// make sure to emit the VLA size.
|
|
|
|
EmitVariablyModifiedType(E->getType());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-02-17 03:43:39 +08:00
|
|
|
llvm::Value *DeclPtr = CreateMemTemp(E->getType(), ".compoundliteral");
|
2010-09-06 08:11:41 +08:00
|
|
|
const Expr *InitExpr = E->getInitializer();
|
2010-08-21 11:08:16 +08:00
|
|
|
LValue Result = MakeAddrLValue(DeclPtr, E->getType());
|
2008-05-14 07:18:27 +08:00
|
|
|
|
2012-03-30 01:37:10 +08:00
|
|
|
EmitAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
|
|
|
|
/*Init*/ true);
|
2008-05-14 07:18:27 +08:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2012-05-15 05:57:21 +08:00
|
|
|
LValue CodeGenFunction::EmitInitListLValue(const InitListExpr *E) {
|
|
|
|
if (!E->isGLValue())
|
|
|
|
// Initializing an aggregate temporary in C++11: T{...}.
|
|
|
|
return EmitAggExprToLValue(E);
|
|
|
|
|
|
|
|
// An lvalue initializer list must be initializing a reference.
|
|
|
|
assert(E->getNumInits() == 1 && "reference init with multiple values");
|
|
|
|
return EmitLValue(E->getInit(0));
|
|
|
|
}
|
|
|
|
|
2014-06-21 02:43:47 +08:00
|
|
|
/// Emit the operand of a glvalue conditional operator. This is either a glvalue
|
|
|
|
/// or a (possibly-parenthesized) throw-expression. If this is a throw, no
|
|
|
|
/// LValue is returned and the current block has been terminated.
|
|
|
|
static Optional<LValue> EmitLValueOrThrowExpression(CodeGenFunction &CGF,
|
|
|
|
const Expr *Operand) {
|
|
|
|
if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(Operand->IgnoreParens())) {
|
|
|
|
CGF.EmitCXXThrowExpr(ThrowExpr, /*KeepInsertionPoint*/false);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CGF.EmitLValue(Operand);
|
|
|
|
}
|
|
|
|
|
2011-02-17 18:25:35 +08:00
|
|
|
LValue CodeGenFunction::
|
|
|
|
EmitConditionalOperatorLValue(const AbstractConditionalOperator *expr) {
|
|
|
|
if (!expr->isGLValue()) {
|
2011-01-27 03:21:13 +08:00
|
|
|
// ?: here should be an aggregate.
|
2013-03-08 05:37:08 +08:00
|
|
|
assert(hasAggregateEvaluationKind(expr->getType()) &&
|
2011-01-27 03:21:13 +08:00
|
|
|
"Unexpected conditional operator!");
|
2011-02-17 18:25:35 +08:00
|
|
|
return EmitAggExprToLValue(expr);
|
2011-01-27 03:21:13 +08:00
|
|
|
}
|
2009-12-25 13:29:40 +08:00
|
|
|
|
2012-01-25 13:04:17 +08:00
|
|
|
OpaqueValueMapping binding(*this, expr);
|
2014-01-07 06:27:43 +08:00
|
|
|
RegionCounter Cnt = getPGORegionCounter(expr);
|
2012-01-25 13:04:17 +08:00
|
|
|
|
2011-02-17 18:25:35 +08:00
|
|
|
const Expr *condExpr = expr->getCond();
|
2011-02-28 07:02:32 +08:00
|
|
|
bool CondExprBool;
|
|
|
|
if (ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
|
2011-02-17 18:25:35 +08:00
|
|
|
const Expr *live = expr->getTrueExpr(), *dead = expr->getFalseExpr();
|
2011-02-28 07:02:32 +08:00
|
|
|
if (!CondExprBool) std::swap(live, dead);
|
2011-02-17 18:25:35 +08:00
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
if (!ContainsLabel(dead)) {
|
2014-01-07 08:20:28 +08:00
|
|
|
// If the true case is live, we need to track its region.
|
2014-01-07 06:27:43 +08:00
|
|
|
if (CondExprBool)
|
|
|
|
Cnt.beginRegion(Builder);
|
2011-02-17 18:25:35 +08:00
|
|
|
return EmitLValue(live);
|
2014-01-07 06:27:43 +08:00
|
|
|
}
|
2011-01-27 03:21:13 +08:00
|
|
|
}
|
|
|
|
|
2011-02-17 18:25:35 +08:00
|
|
|
llvm::BasicBlock *lhsBlock = createBasicBlock("cond.true");
|
|
|
|
llvm::BasicBlock *rhsBlock = createBasicBlock("cond.false");
|
|
|
|
llvm::BasicBlock *contBlock = createBasicBlock("cond.end");
|
2011-01-26 12:00:11 +08:00
|
|
|
|
2011-01-27 03:21:13 +08:00
|
|
|
ConditionalEvaluation eval(*this);
|
2014-01-07 06:27:43 +08:00
|
|
|
EmitBranchOnBoolExpr(condExpr, lhsBlock, rhsBlock, Cnt.getCount());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2011-01-27 03:21:13 +08:00
|
|
|
// Any temporaries created here are conditional.
|
2011-02-17 18:25:35 +08:00
|
|
|
EmitBlock(lhsBlock);
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
2011-01-27 03:21:13 +08:00
|
|
|
eval.begin(*this);
|
2014-06-21 02:43:47 +08:00
|
|
|
Optional<LValue> lhs =
|
|
|
|
EmitLValueOrThrowExpression(*this, expr->getTrueExpr());
|
2011-01-27 03:21:13 +08:00
|
|
|
eval.end(*this);
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2014-06-21 02:43:47 +08:00
|
|
|
if (lhs && !lhs->isSimple())
|
2011-02-17 18:25:35 +08:00
|
|
|
return EmitUnsupportedLValue(expr, "conditional operator");
|
2009-09-16 00:35:24 +08:00
|
|
|
|
2011-02-17 18:25:35 +08:00
|
|
|
lhsBlock = Builder.GetInsertBlock();
|
2014-06-21 02:43:47 +08:00
|
|
|
if (lhs)
|
|
|
|
Builder.CreateBr(contBlock);
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2011-01-27 03:21:13 +08:00
|
|
|
// Any temporaries created here are conditional.
|
2011-02-17 18:25:35 +08:00
|
|
|
EmitBlock(rhsBlock);
|
2011-01-27 03:21:13 +08:00
|
|
|
eval.begin(*this);
|
2014-06-21 02:43:47 +08:00
|
|
|
Optional<LValue> rhs =
|
|
|
|
EmitLValueOrThrowExpression(*this, expr->getFalseExpr());
|
2011-01-27 03:21:13 +08:00
|
|
|
eval.end(*this);
|
2014-06-21 02:43:47 +08:00
|
|
|
if (rhs && !rhs->isSimple())
|
2011-02-17 18:25:35 +08:00
|
|
|
return EmitUnsupportedLValue(expr, "conditional operator");
|
|
|
|
rhsBlock = Builder.GetInsertBlock();
|
2011-01-27 03:21:13 +08:00
|
|
|
|
2011-02-17 18:25:35 +08:00
|
|
|
EmitBlock(contBlock);
|
2011-01-27 03:21:13 +08:00
|
|
|
|
2014-06-21 02:43:47 +08:00
|
|
|
if (lhs && rhs) {
|
|
|
|
llvm::PHINode *phi = Builder.CreatePHI(lhs->getAddress()->getType(),
|
|
|
|
2, "cond-lvalue");
|
|
|
|
phi->addIncoming(lhs->getAddress(), lhsBlock);
|
|
|
|
phi->addIncoming(rhs->getAddress(), rhsBlock);
|
|
|
|
return MakeAddrLValue(phi, expr->getType());
|
|
|
|
} else {
|
|
|
|
assert((lhs || rhs) &&
|
|
|
|
"both operands of glvalue conditional are throw-expressions?");
|
|
|
|
return lhs ? *lhs : *rhs;
|
|
|
|
}
|
2009-03-24 10:38:23 +08:00
|
|
|
}
|
|
|
|
|
2012-05-15 05:57:21 +08:00
|
|
|
/// EmitCastLValue - Casts are never lvalues unless that cast is to a reference
|
|
|
|
/// type. If the cast is to a reference, we can have the usual lvalue result,
|
2009-11-16 14:50:58 +08:00
|
|
|
/// otherwise if a cast is needed by the code generator in an lvalue context,
|
|
|
|
/// then it must mean that we need the address of an aggregate in order to
|
2012-05-15 05:57:21 +08:00
|
|
|
/// access one of its members. This can happen for all the reasons that casts
|
2009-11-16 14:50:58 +08:00
|
|
|
/// are permitted with aggregate result, including noop aggregate casts, and
|
|
|
|
/// cast from scalar to union.
|
2009-03-19 02:28:57 +08:00
|
|
|
LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
|
2009-09-13 00:16:49 +08:00
|
|
|
switch (E->getCastKind()) {
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ToVoid:
|
|
|
|
case CK_BitCast:
|
|
|
|
case CK_ArrayToPointerDecay:
|
|
|
|
case CK_FunctionToPointerDecay:
|
|
|
|
case CK_NullToMemberPointer:
|
2010-11-13 09:35:44 +08:00
|
|
|
case CK_NullToPointer:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_IntegralToPointer:
|
|
|
|
case CK_PointerToIntegral:
|
2010-11-15 17:13:47 +08:00
|
|
|
case CK_PointerToBoolean:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_VectorSplat:
|
|
|
|
case CK_IntegralCast:
|
2010-11-15 17:13:47 +08:00
|
|
|
case CK_IntegralToBoolean:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_IntegralToFloating:
|
|
|
|
case CK_FloatingToIntegral:
|
2010-11-15 17:13:47 +08:00
|
|
|
case CK_FloatingToBoolean:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_FloatingCast:
|
2010-11-13 17:02:35 +08:00
|
|
|
case CK_FloatingRealToComplex:
|
2010-11-14 16:17:51 +08:00
|
|
|
case CK_FloatingComplexToReal:
|
|
|
|
case CK_FloatingComplexToBoolean:
|
2010-11-13 17:02:35 +08:00
|
|
|
case CK_FloatingComplexCast:
|
2010-11-14 16:17:51 +08:00
|
|
|
case CK_FloatingComplexToIntegralComplex:
|
2010-11-13 17:02:35 +08:00
|
|
|
case CK_IntegralRealToComplex:
|
2010-11-14 16:17:51 +08:00
|
|
|
case CK_IntegralComplexToReal:
|
|
|
|
case CK_IntegralComplexToBoolean:
|
2010-11-13 17:02:35 +08:00
|
|
|
case CK_IntegralComplexCast:
|
2010-11-14 16:17:51 +08:00
|
|
|
case CK_IntegralComplexToFloatingComplex:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_DerivedToBaseMemberPointer:
|
|
|
|
case CK_BaseToDerivedMemberPointer:
|
|
|
|
case CK_MemberPointerToBoolean:
|
2012-02-15 09:22:51 +08:00
|
|
|
case CK_ReinterpretMemberPointer:
|
2011-06-16 07:02:42 +08:00
|
|
|
case CK_AnyPointerToBlockPointerCast:
|
2011-09-10 14:18:15 +08:00
|
|
|
case CK_ARCProduceObject:
|
|
|
|
case CK_ARCConsumeObject:
|
|
|
|
case CK_ARCReclaimReturnedObject:
|
2013-07-26 13:59:26 +08:00
|
|
|
case CK_ARCExtendBlockObject:
|
2013-06-28 08:23:34 +08:00
|
|
|
case CK_CopyAndAutoreleaseBlockObject:
|
2013-12-11 21:39:46 +08:00
|
|
|
case CK_AddressSpaceConversion:
|
2013-06-28 08:23:34 +08:00
|
|
|
return EmitUnsupportedLValue(E, "unexpected cast lvalue");
|
|
|
|
|
|
|
|
case CK_Dependent:
|
|
|
|
llvm_unreachable("dependent cast kind in IR gen!");
|
|
|
|
|
|
|
|
case CK_BuiltinFnToFnPtr:
|
|
|
|
llvm_unreachable("builtin functions are handled elsewhere");
|
|
|
|
|
2013-07-11 09:32:21 +08:00
|
|
|
// These are never l-values; just use the aggregate emission code.
|
2013-06-28 08:23:34 +08:00
|
|
|
case CK_NonAtomicToAtomic:
|
|
|
|
case CK_AtomicToNonAtomic:
|
2013-07-11 09:32:21 +08:00
|
|
|
return EmitAggExprToLValue(E);
|
2009-11-16 13:48:01 +08:00
|
|
|
|
2011-04-11 10:03:26 +08:00
|
|
|
case CK_Dynamic: {
|
2009-11-16 14:50:58 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
llvm::Value *V = LV.getAddress();
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *DCE = cast<CXXDynamicCastExpr>(E);
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(EmitDynamicCast(V, DCE), E->getType());
|
2009-11-16 14:50:58 +08:00
|
|
|
}
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ConstructorConversion:
|
|
|
|
case CK_UserDefinedConversion:
|
2011-09-09 13:25:32 +08:00
|
|
|
case CK_CPointerToObjCPointerCast:
|
|
|
|
case CK_BlockPointerToObjCPointerCast:
|
2013-06-28 08:23:34 +08:00
|
|
|
case CK_NoOp:
|
|
|
|
case CK_LValueToRValue:
|
2009-03-19 02:28:57 +08:00
|
|
|
return EmitLValue(E->getSubExpr());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_UncheckedDerivedToBase:
|
|
|
|
case CK_DerivedToBase: {
|
2013-07-26 13:59:26 +08:00
|
|
|
const RecordType *DerivedClassTy =
|
2009-09-13 00:16:49 +08:00
|
|
|
E->getSubExpr()->getType()->getAs<RecordType>();
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2009-09-13 00:16:49 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
2010-12-04 16:14:53 +08:00
|
|
|
llvm::Value *This = LV.getAddress();
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2009-09-13 00:16:49 +08:00
|
|
|
// Perform the derived-to-base conversion
|
2014-10-14 07:59:00 +08:00
|
|
|
llvm::Value *Base = GetAddressOfBaseClass(
|
|
|
|
This, DerivedClassDecl, E->path_begin(), E->path_end(),
|
|
|
|
/*NullCheckValue=*/false, E->getExprLoc());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(Base, E->getType());
|
2009-09-13 00:16:49 +08:00
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ToUnion:
|
2010-02-06 04:02:42 +08:00
|
|
|
return EmitAggExprToLValue(E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_BaseToDerived: {
|
2009-11-24 01:57:54 +08:00
|
|
|
const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
|
2014-05-09 08:08:36 +08:00
|
|
|
auto *DerivedClassDecl = cast<CXXRecordDecl>(DerivedClassTy->getDecl());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2009-11-24 01:57:54 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
2013-02-14 05:18:23 +08:00
|
|
|
|
2009-11-24 01:57:54 +08:00
|
|
|
// Perform the base-to-derived conversion
|
2013-07-26 13:59:26 +08:00
|
|
|
llvm::Value *Derived =
|
|
|
|
GetAddressOfDerivedClass(LV.getAddress(), DerivedClassDecl,
|
2010-08-07 14:22:56 +08:00
|
|
|
E->path_begin(), E->path_end(),
|
|
|
|
/*NullCheckValue=*/false);
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2013-08-08 09:08:17 +08:00
|
|
|
// C++11 [expr.static.cast]p2: Behavior is undefined if a downcast is
|
|
|
|
// performed and the object is not of the derived type.
|
2014-07-08 07:59:57 +08:00
|
|
|
if (sanitizePerformTypeCheck())
|
2013-08-08 09:08:17 +08:00
|
|
|
EmitTypeCheck(TCK_DowncastReference, E->getExprLoc(),
|
|
|
|
Derived, E->getType());
|
|
|
|
|
2015-03-14 10:42:25 +08:00
|
|
|
if (SanOpts.has(SanitizerKind::CFIDerivedCast))
|
|
|
|
EmitVTablePtrCheckForCast(E->getType(), Derived, /*MayBeNull=*/false);
|
|
|
|
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(Derived, E->getType());
|
2009-11-16 13:48:01 +08:00
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_LValueBitCast: {
|
2009-11-16 13:48:01 +08:00
|
|
|
// This must be a reinterpret_cast (or c-style equivalent).
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *CE = cast<ExplicitCastExpr>(E);
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2009-11-15 05:21:42 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
|
|
|
|
ConvertType(CE->getTypeAsWritten()));
|
2015-03-14 10:42:25 +08:00
|
|
|
|
|
|
|
if (SanOpts.has(SanitizerKind::CFIUnrelatedCast))
|
|
|
|
EmitVTablePtrCheckForCast(E->getType(), V, /*MayBeNull=*/false);
|
|
|
|
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(V, E->getType());
|
2009-11-15 05:21:42 +08:00
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ObjCObjectLValueCast: {
|
2010-08-07 19:51:51 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
QualType ToType = getContext().getLValueReferenceType(E->getType());
|
2013-07-26 13:59:26 +08:00
|
|
|
llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
|
2010-08-07 19:51:51 +08:00
|
|
|
ConvertType(ToType));
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(V, E->getType());
|
2010-08-07 19:51:51 +08:00
|
|
|
}
|
2013-01-20 20:31:11 +08:00
|
|
|
case CK_ZeroToOCLEvent:
|
|
|
|
llvm_unreachable("NULL to OpenCL event lvalue cast is not valid");
|
2009-09-13 00:16:49 +08:00
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-07-16 02:58:16 +08:00
|
|
|
llvm_unreachable("Unhandled lvalue cast kind?");
|
2009-03-19 02:28:57 +08:00
|
|
|
}
|
|
|
|
|
2011-02-16 16:02:54 +08:00
|
|
|
LValue CodeGenFunction::EmitOpaqueValueLValue(const OpaqueValueExpr *e) {
|
2011-11-09 06:54:08 +08:00
|
|
|
assert(OpaqueValueMappingData::shouldBindAsLValue(e));
|
2011-02-17 18:25:35 +08:00
|
|
|
return getOpaqueLValueMapping(e);
|
2011-02-16 16:02:54 +08:00
|
|
|
}
|
|
|
|
|
2012-04-16 11:54:45 +08:00
|
|
|
RValue CodeGenFunction::EmitRValueForField(LValue LV,
|
2013-10-02 10:29:49 +08:00
|
|
|
const FieldDecl *FD,
|
|
|
|
SourceLocation Loc) {
|
2012-04-13 19:22:00 +08:00
|
|
|
QualType FT = FD->getType();
|
2012-04-16 11:54:45 +08:00
|
|
|
LValue FieldLV = EmitLValueForField(LV, FD);
|
2013-03-08 05:37:08 +08:00
|
|
|
switch (getEvaluationKind(FT)) {
|
|
|
|
case TEK_Complex:
|
2013-10-02 10:29:49 +08:00
|
|
|
return RValue::getComplex(EmitLoadOfComplex(FieldLV, Loc));
|
2013-03-08 05:37:08 +08:00
|
|
|
case TEK_Aggregate:
|
2012-04-16 11:54:45 +08:00
|
|
|
return FieldLV.asAggregateRValue();
|
2013-03-08 05:37:08 +08:00
|
|
|
case TEK_Scalar:
|
2013-10-02 10:29:49 +08:00
|
|
|
return EmitLoadOfLValue(FieldLV, Loc);
|
2013-03-08 05:37:08 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("bad evaluation kind");
|
2012-04-13 19:22:00 +08:00
|
|
|
}
|
2011-06-22 01:03:29 +08:00
|
|
|
|
2007-06-02 02:02:12 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Expression Emission
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2013-07-26 13:59:26 +08:00
|
|
|
RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
|
2009-12-25 04:40:36 +08:00
|
|
|
ReturnValueSlot ReturnValue) {
|
2009-02-21 02:06:48 +08:00
|
|
|
// Builtins never have block type.
|
|
|
|
if (E->getCallee()->getType()->isBlockPointerType())
|
2009-12-25 05:13:40 +08:00
|
|
|
return EmitBlockCallExpr(E, ReturnValue);
|
2009-02-21 02:06:48 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
|
2009-12-25 05:13:40 +08:00
|
|
|
return EmitCXXMemberCallExpr(CE, ReturnValue);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *CE = dyn_cast<CUDAKernelCallExpr>(E))
|
2011-10-07 02:29:37 +08:00
|
|
|
return EmitCUDAKernelCallExpr(CE, ReturnValue);
|
|
|
|
|
2011-09-07 05:41:04 +08:00
|
|
|
const Decl *TargetDecl = E->getCalleeDecl();
|
|
|
|
if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
|
|
|
|
if (unsigned builtinID = FD->getBuiltinID())
|
2014-12-13 07:41:25 +08:00
|
|
|
return EmitBuiltinExpr(FD, builtinID, E, ReturnValue);
|
2009-02-21 02:06:48 +08:00
|
|
|
}
|
2009-01-10 00:50:52 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E))
|
2009-05-27 12:18:27 +08:00
|
|
|
if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
|
2009-12-25 05:13:40 +08:00
|
|
|
return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *PseudoDtor =
|
|
|
|
dyn_cast<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
|
2011-06-16 07:02:42 +08:00
|
|
|
QualType DestroyedType = PseudoDtor->getDestroyedType();
|
2012-11-02 06:30:59 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2011-06-16 07:02:42 +08:00
|
|
|
DestroyedType->isObjCLifetimeType() &&
|
|
|
|
(DestroyedType.getObjCLifetime() == Qualifiers::OCL_Strong ||
|
|
|
|
DestroyedType.getObjCLifetime() == Qualifiers::OCL_Weak)) {
|
2011-06-18 18:34:00 +08:00
|
|
|
// Automatic Reference Counting:
|
|
|
|
// If the pseudo-expression names a retainable object with weak or
|
|
|
|
// strong lifetime, the object shall be released.
|
2011-06-16 07:02:42 +08:00
|
|
|
Expr *BaseExpr = PseudoDtor->getBase();
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *BaseValue = nullptr;
|
2011-06-16 07:02:42 +08:00
|
|
|
Qualifiers BaseQuals;
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2011-06-18 18:34:00 +08:00
|
|
|
// If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
|
2011-06-16 07:02:42 +08:00
|
|
|
if (PseudoDtor->isArrow()) {
|
|
|
|
BaseValue = EmitScalarExpr(BaseExpr);
|
|
|
|
const PointerType *PTy = BaseExpr->getType()->getAs<PointerType>();
|
|
|
|
BaseQuals = PTy->getPointeeType().getQualifiers();
|
|
|
|
} else {
|
|
|
|
LValue BaseLV = EmitLValue(BaseExpr);
|
|
|
|
BaseValue = BaseLV.getAddress();
|
|
|
|
QualType BaseTy = BaseExpr->getType();
|
|
|
|
BaseQuals = BaseTy.getQualifiers();
|
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
switch (PseudoDtor->getDestroyedType().getObjCLifetime()) {
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
break;
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
case Qualifiers::OCL_Strong:
|
2013-07-26 13:59:26 +08:00
|
|
|
EmitARCRelease(Builder.CreateLoad(BaseValue,
|
2011-06-18 18:34:00 +08:00
|
|
|
PseudoDtor->getDestroyedType().isVolatileQualified()),
|
2013-03-13 11:10:54 +08:00
|
|
|
ARCPreciseLifetime);
|
2011-06-16 07:02:42 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
EmitARCDestroyWeak(BaseValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// C++ [expr.pseudo]p1:
|
|
|
|
// The result shall only be used as the operand for the function call
|
|
|
|
// operator (), and the result of such a call has type void. The only
|
|
|
|
// effect is the evaluation of the postfix-expression before the dot or
|
2013-07-26 13:59:26 +08:00
|
|
|
// arrow.
|
2011-06-16 07:02:42 +08:00
|
|
|
EmitScalarExpr(E->getCallee());
|
|
|
|
}
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
return RValue::get(nullptr);
|
2009-09-05 01:36:40 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
llvm::Value *Callee = EmitScalarExpr(E->getCallee());
|
2014-08-22 04:26:47 +08:00
|
|
|
return EmitCall(E->getCallee()->getType(), Callee, E, ReturnValue,
|
|
|
|
TargetDecl);
|
2007-08-31 12:44:06 +08:00
|
|
|
}
|
|
|
|
|
2008-09-04 11:20:13 +08:00
|
|
|
LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
|
2009-05-13 05:28:12 +08:00
|
|
|
// Comma expressions just emit their LHS then their RHS as an l-value.
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Comma) {
|
2010-12-05 10:00:02 +08:00
|
|
|
EmitIgnoredExpr(E->getLHS());
|
2009-12-08 04:18:11 +08:00
|
|
|
EnsureInsertPoint();
|
2009-05-13 05:28:12 +08:00
|
|
|
return EmitLValue(E->getRHS());
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_PtrMemD ||
|
|
|
|
E->getOpcode() == BO_PtrMemI)
|
2009-10-23 06:57:31 +08:00
|
|
|
return EmitPointerToDataMemberBinaryExpr(E);
|
2008-09-04 11:20:13 +08:00
|
|
|
|
2010-12-05 10:00:02 +08:00
|
|
|
assert(E->getOpcode() == BO_Assign && "unexpected binary l-value");
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
// Note that in all of these cases, __block variables need the RHS
|
|
|
|
// evaluated first just in case the variable gets moved by the RHS.
|
2013-03-08 05:37:08 +08:00
|
|
|
|
|
|
|
switch (getEvaluationKind(E->getType())) {
|
|
|
|
case TEK_Scalar: {
|
2011-06-16 07:02:42 +08:00
|
|
|
switch (E->getLHS()->getType().getObjCLifetime()) {
|
|
|
|
case Qualifiers::OCL_Strong:
|
|
|
|
return EmitARCStoreStrong(E, /*ignored*/ false).first;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
return EmitARCStoreAutoreleasing(E).first;
|
|
|
|
|
|
|
|
// No reason to do any of these differently.
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-12-06 14:10:02 +08:00
|
|
|
RValue RV = EmitAnyExpr(E->getRHS());
|
2012-10-10 03:52:38 +08:00
|
|
|
LValue LV = EmitCheckedLValue(E->getLHS(), TCK_Store);
|
2011-06-25 10:11:03 +08:00
|
|
|
EmitStoreThroughLValue(RV, LV);
|
2009-10-20 02:28:22 +08:00
|
|
|
return LV;
|
|
|
|
}
|
2010-11-17 07:07:28 +08:00
|
|
|
|
2013-03-08 05:37:08 +08:00
|
|
|
case TEK_Complex:
|
2010-11-17 07:07:28 +08:00
|
|
|
return EmitComplexAssignmentLValue(E);
|
|
|
|
|
2013-03-08 05:37:08 +08:00
|
|
|
case TEK_Aggregate:
|
|
|
|
return EmitAggExprToLValue(E);
|
|
|
|
}
|
|
|
|
llvm_unreachable("bad evaluation kind");
|
2008-09-04 11:20:13 +08:00
|
|
|
}
|
|
|
|
|
2007-12-29 13:02:41 +08:00
|
|
|
LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
|
|
|
|
RValue RV = EmitCallExpr(E);
|
2009-05-27 09:45:47 +08:00
|
|
|
|
2009-10-29 01:39:19 +08:00
|
|
|
if (!RV.isScalar())
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2015-02-26 01:36:15 +08:00
|
|
|
assert(E->getCallReturnType(getContext())->isReferenceType() &&
|
2009-10-29 01:39:19 +08:00
|
|
|
"Can't have a scalar return unless the return type is a "
|
|
|
|
"reference type!");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(RV.getScalarVal(), E->getType());
|
2007-12-29 13:02:41 +08:00
|
|
|
}
|
|
|
|
|
2009-02-12 04:59:32 +08:00
|
|
|
LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
|
|
|
|
// FIXME: This shouldn't require another copy.
|
2010-02-06 03:38:31 +08:00
|
|
|
return EmitAggExprToLValue(E);
|
2009-02-12 04:59:32 +08:00
|
|
|
}
|
|
|
|
|
2009-05-31 07:23:33 +08:00
|
|
|
LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
|
2010-09-18 08:58:34 +08:00
|
|
|
assert(E->getType()->getAsCXXRecordDecl()->hasTrivialDestructor()
|
|
|
|
&& "binding l-value to type which needs a temporary");
|
2011-09-28 05:06:10 +08:00
|
|
|
AggValueSlot Slot = CreateAggTemp(E->getType());
|
2010-09-15 18:14:12 +08:00
|
|
|
EmitCXXConstructExpr(E, Slot);
|
|
|
|
return MakeAddrLValue(Slot.getAddr(), E->getType());
|
2009-05-31 07:23:33 +08:00
|
|
|
}
|
|
|
|
|
2009-11-15 16:09:41 +08:00
|
|
|
LValue
|
|
|
|
CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(EmitCXXTypeidExpr(E), E->getType());
|
2009-11-15 16:09:41 +08:00
|
|
|
}
|
|
|
|
|
2012-10-11 18:13:44 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitCXXUuidofExpr(const CXXUuidofExpr *E) {
|
2013-08-16 03:59:14 +08:00
|
|
|
return Builder.CreateBitCast(CGM.GetAddrOfUuidDescriptor(E),
|
|
|
|
ConvertType(E->getType())->getPointerTo());
|
2012-10-11 18:13:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LValue CodeGenFunction::EmitCXXUuidofLValue(const CXXUuidofExpr *E) {
|
|
|
|
return MakeAddrLValue(EmitCXXUuidofExpr(E), E->getType());
|
|
|
|
}
|
|
|
|
|
2009-05-31 07:30:54 +08:00
|
|
|
LValue
|
|
|
|
CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
|
2010-09-18 08:58:34 +08:00
|
|
|
AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
|
2011-08-26 16:02:37 +08:00
|
|
|
Slot.setExternallyDestructed();
|
2010-09-18 08:58:34 +08:00
|
|
|
EmitAggExpr(E->getSubExpr(), Slot);
|
2011-11-28 06:09:22 +08:00
|
|
|
EmitCXXTemporary(E->getTemporary(), E->getType(), Slot.getAddr());
|
2010-09-18 08:58:34 +08:00
|
|
|
return MakeAddrLValue(Slot.getAddr(), E->getType());
|
2009-05-31 07:30:54 +08:00
|
|
|
}
|
|
|
|
|
2012-02-08 13:34:55 +08:00
|
|
|
LValue
|
|
|
|
CodeGenFunction::EmitLambdaLValue(const LambdaExpr *E) {
|
|
|
|
AggValueSlot Slot = CreateAggTemp(E->getType(), "temp.lvalue");
|
2012-02-09 11:32:31 +08:00
|
|
|
EmitLambdaExpr(E, Slot);
|
2012-02-08 13:34:55 +08:00
|
|
|
return MakeAddrLValue(Slot.getAddr(), E->getType());
|
|
|
|
}
|
|
|
|
|
2008-08-23 18:51:21 +08:00
|
|
|
LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
|
|
|
|
RValue RV = EmitObjCMessageExpr(E);
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-06-22 04:59:55 +08:00
|
|
|
if (!RV.isScalar())
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2014-01-26 00:55:45 +08:00
|
|
|
assert(E->getMethodDecl()->getReturnType()->isReferenceType() &&
|
2010-06-22 04:59:55 +08:00
|
|
|
"Can't have a scalar return unless the return type is a "
|
|
|
|
"reference type!");
|
2013-07-26 13:59:26 +08:00
|
|
|
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(RV.getScalarVal(), E->getType());
|
2008-08-23 18:51:21 +08:00
|
|
|
}
|
|
|
|
|
2010-06-18 03:56:20 +08:00
|
|
|
LValue CodeGenFunction::EmitObjCSelectorLValue(const ObjCSelectorExpr *E) {
|
2013-07-26 13:59:26 +08:00
|
|
|
llvm::Value *V =
|
2013-03-01 03:01:20 +08:00
|
|
|
CGM.getObjCRuntime().GetSelector(*this, E->getSelector(), true);
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(V, E->getType());
|
2010-06-18 03:56:20 +08:00
|
|
|
}
|
|
|
|
|
2009-04-22 13:08:15 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
|
2008-09-24 12:00:38 +08:00
|
|
|
const ObjCIvarDecl *Ivar) {
|
2009-02-11 03:02:04 +08:00
|
|
|
return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
|
2008-09-24 12:00:38 +08:00
|
|
|
}
|
2008-08-25 09:53:23 +08:00
|
|
|
|
2009-02-03 08:09:52 +08:00
|
|
|
LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
|
|
|
|
llvm::Value *BaseValue,
|
2008-09-24 12:00:38 +08:00
|
|
|
const ObjCIvarDecl *Ivar,
|
|
|
|
unsigned CVRQualifiers) {
|
2009-04-18 01:44:48 +08:00
|
|
|
return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
|
2009-04-21 09:19:28 +08:00
|
|
|
Ivar, CVRQualifiers);
|
2008-09-24 12:00:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
|
2008-08-25 09:53:23 +08:00
|
|
|
// FIXME: A lot of the code below could be shared with EmitMemberExpr.
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *BaseValue = nullptr;
|
2008-08-25 09:53:23 +08:00
|
|
|
const Expr *BaseExpr = E->getBase();
|
2009-09-25 03:53:00 +08:00
|
|
|
Qualifiers BaseQuals;
|
2009-02-03 08:09:52 +08:00
|
|
|
QualType ObjectTy;
|
2008-08-25 09:53:23 +08:00
|
|
|
if (E->isArrow()) {
|
|
|
|
BaseValue = EmitScalarExpr(BaseExpr);
|
2009-07-11 07:34:53 +08:00
|
|
|
ObjectTy = BaseExpr->getType()->getPointeeType();
|
2009-09-25 03:53:00 +08:00
|
|
|
BaseQuals = ObjectTy.getQualifiers();
|
2008-08-25 09:53:23 +08:00
|
|
|
} else {
|
|
|
|
LValue BaseLV = EmitLValue(BaseExpr);
|
|
|
|
// FIXME: this isn't right for bitfields.
|
|
|
|
BaseValue = BaseLV.getAddress();
|
2009-02-03 08:09:52 +08:00
|
|
|
ObjectTy = BaseExpr->getType();
|
2009-09-25 03:53:00 +08:00
|
|
|
BaseQuals = ObjectTy.getQualifiers();
|
2008-08-25 09:53:23 +08:00
|
|
|
}
|
2008-09-24 12:00:38 +08:00
|
|
|
|
2013-07-26 13:59:26 +08:00
|
|
|
LValue LV =
|
2009-09-25 03:53:00 +08:00
|
|
|
EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
|
|
|
|
BaseQuals.getCVRQualifiers());
|
2009-09-17 07:11:23 +08:00
|
|
|
setObjCGCLValueClass(getContext(), E, LV);
|
|
|
|
return LV;
|
2008-03-31 07:03:07 +08:00
|
|
|
}
|
|
|
|
|
2009-04-26 03:35:26 +08:00
|
|
|
LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
|
|
|
|
// Can only get l-value for message expression returning aggregate type
|
|
|
|
RValue RV = EmitAnyExprToTemp(E);
|
2010-08-21 11:08:16 +08:00
|
|
|
return MakeAddrLValue(RV.getAggregateAddr(), E->getType());
|
2009-04-26 03:35:26 +08:00
|
|
|
}
|
|
|
|
|
2009-12-25 03:08:58 +08:00
|
|
|
RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
|
2014-08-22 04:26:47 +08:00
|
|
|
const CallExpr *E, ReturnValueSlot ReturnValue,
|
2014-12-13 07:41:25 +08:00
|
|
|
const Decl *TargetDecl, llvm::Value *Chain) {
|
2009-09-09 21:00:44 +08:00
|
|
|
// Get the actual function type. The callee type will always be a pointer to
|
|
|
|
// function type or a block pointer type.
|
|
|
|
assert(CalleeType->isFunctionPointerType() &&
|
2009-04-08 02:53:02 +08:00
|
|
|
"Call must have function pointer type!");
|
|
|
|
|
2009-10-23 16:22:42 +08:00
|
|
|
CalleeType = getContext().getCanonicalType(CalleeType);
|
|
|
|
|
2014-05-09 08:08:36 +08:00
|
|
|
const auto *FnType =
|
|
|
|
cast<FunctionType>(cast<PointerType>(CalleeType)->getPointeeType());
|
2008-08-30 11:02:31 +08:00
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function) &&
|
2013-10-21 05:29:19 +08:00
|
|
|
(!TargetDecl || !isa<FunctionDecl>(TargetDecl))) {
|
|
|
|
if (llvm::Constant *PrefixSig =
|
|
|
|
CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM)) {
|
2014-07-18 02:46:27 +08:00
|
|
|
SanitizerScope SanScope(this);
|
2013-10-21 05:29:19 +08:00
|
|
|
llvm::Constant *FTRTTIConst =
|
|
|
|
CGM.GetAddrOfRTTIDescriptor(QualType(FnType, 0), /*ForEH=*/true);
|
|
|
|
llvm::Type *PrefixStructTyElems[] = {
|
|
|
|
PrefixSig->getType(),
|
|
|
|
FTRTTIConst->getType()
|
|
|
|
};
|
|
|
|
llvm::StructType *PrefixStructTy = llvm::StructType::get(
|
|
|
|
CGM.getLLVMContext(), PrefixStructTyElems, /*isPacked=*/true);
|
|
|
|
|
|
|
|
llvm::Value *CalleePrefixStruct = Builder.CreateBitCast(
|
|
|
|
Callee, llvm::PointerType::getUnqual(PrefixStructTy));
|
|
|
|
llvm::Value *CalleeSigPtr =
|
|
|
|
Builder.CreateConstGEP2_32(CalleePrefixStruct, 0, 0);
|
|
|
|
llvm::Value *CalleeSig = Builder.CreateLoad(CalleeSigPtr);
|
|
|
|
llvm::Value *CalleeSigMatch = Builder.CreateICmpEQ(CalleeSig, PrefixSig);
|
|
|
|
|
|
|
|
llvm::BasicBlock *Cont = createBasicBlock("cont");
|
|
|
|
llvm::BasicBlock *TypeCheck = createBasicBlock("typecheck");
|
|
|
|
Builder.CreateCondBr(CalleeSigMatch, TypeCheck, Cont);
|
|
|
|
|
|
|
|
EmitBlock(TypeCheck);
|
|
|
|
llvm::Value *CalleeRTTIPtr =
|
|
|
|
Builder.CreateConstGEP2_32(CalleePrefixStruct, 0, 1);
|
|
|
|
llvm::Value *CalleeRTTI = Builder.CreateLoad(CalleeRTTIPtr);
|
|
|
|
llvm::Value *CalleeRTTIMatch =
|
|
|
|
Builder.CreateICmpEQ(CalleeRTTI, FTRTTIConst);
|
|
|
|
llvm::Constant *StaticData[] = {
|
2014-08-22 04:26:47 +08:00
|
|
|
EmitCheckSourceLocation(E->getLocStart()),
|
2013-10-21 05:29:19 +08:00
|
|
|
EmitCheckTypeDescriptor(CalleeType)
|
|
|
|
};
|
2014-11-12 06:03:54 +08:00
|
|
|
EmitCheck(std::make_pair(CalleeRTTIMatch, SanitizerKind::Function),
|
|
|
|
"function_type_mismatch", StaticData, Callee);
|
2013-10-21 05:29:19 +08:00
|
|
|
|
|
|
|
Builder.CreateBr(Cont);
|
|
|
|
EmitBlock(Cont);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-30 11:02:31 +08:00
|
|
|
CallArgList Args;
|
2014-12-13 07:41:25 +08:00
|
|
|
if (Chain)
|
|
|
|
Args.add(RValue::get(Builder.CreateBitCast(Chain, CGM.VoidPtrTy)),
|
|
|
|
CGM.getContext().VoidPtrTy);
|
2014-08-22 04:26:47 +08:00
|
|
|
EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), E->arg_begin(),
|
2015-01-22 07:08:17 +08:00
|
|
|
E->arg_end(), E->getDirectCallee(), /*ParamsToSkip*/ 0);
|
2008-08-30 11:02:31 +08:00
|
|
|
|
2014-12-13 07:41:25 +08:00
|
|
|
const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionCall(
|
|
|
|
Args, FnType, /*isChainCall=*/Chain);
|
2011-09-21 16:08:30 +08:00
|
|
|
|
|
|
|
// C99 6.5.2.2p6:
|
|
|
|
// If the expression that denotes the called function has a type
|
|
|
|
// that does not include a prototype, [the default argument
|
|
|
|
// promotions are performed]. If the number of arguments does not
|
|
|
|
// equal the number of parameters, the behavior is undefined. If
|
|
|
|
// the function is defined with a type that includes a prototype,
|
|
|
|
// and either the prototype ends with an ellipsis (, ...) or the
|
|
|
|
// types of the arguments after promotion are not compatible with
|
|
|
|
// the types of the parameters, the behavior is undefined. If the
|
|
|
|
// function is defined with a type that does not include a
|
|
|
|
// prototype, and the types of the arguments after promotion are
|
|
|
|
// not compatible with those of the parameters after promotion,
|
|
|
|
// the behavior is undefined [except in some trivial cases].
|
|
|
|
// That is, in the general case, we should assume that a call
|
|
|
|
// through an unprototyped function type works like a *non-variadic*
|
|
|
|
// call. The way we make this work is to cast to the exact type
|
|
|
|
// of the promoted arguments.
|
2014-12-13 07:41:25 +08:00
|
|
|
//
|
|
|
|
// Chain calls use this same code path to add the invisible chain parameter
|
|
|
|
// to the function type.
|
|
|
|
if (isa<FunctionNoProtoType>(FnType) || Chain) {
|
2012-02-17 11:33:10 +08:00
|
|
|
llvm::Type *CalleeTy = getTypes().GetFunctionType(FnInfo);
|
2011-09-21 16:08:30 +08:00
|
|
|
CalleeTy = CalleeTy->getPointerTo();
|
|
|
|
Callee = Builder.CreateBitCast(Callee, CalleeTy, "callee.knr.cast");
|
|
|
|
}
|
|
|
|
|
|
|
|
return EmitCall(FnInfo, Callee, ReturnValue, Args, TargetDecl);
|
2008-08-23 11:46:30 +08:00
|
|
|
}
|
2009-10-23 06:57:31 +08:00
|
|
|
|
2009-10-29 01:39:19 +08:00
|
|
|
LValue CodeGenFunction::
|
|
|
|
EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
|
2009-11-18 13:01:17 +08:00
|
|
|
llvm::Value *BaseV;
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_PtrMemI)
|
2009-11-18 13:01:17 +08:00
|
|
|
BaseV = EmitScalarExpr(E->getLHS());
|
|
|
|
else
|
|
|
|
BaseV = EmitLValue(E->getLHS()).getAddress();
|
2010-09-01 05:07:20 +08:00
|
|
|
|
2009-11-18 13:01:17 +08:00
|
|
|
llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
|
2009-10-29 01:39:19 +08:00
|
|
|
|
2010-09-01 05:07:20 +08:00
|
|
|
const MemberPointerType *MPT
|
|
|
|
= E->getRHS()->getType()->getAs<MemberPointerType>();
|
|
|
|
|
2014-02-21 07:22:07 +08:00
|
|
|
llvm::Value *AddV = CGM.getCXXABI().EmitMemberDataPointerAddress(
|
|
|
|
*this, E, BaseV, OffsetV, MPT);
|
2010-09-01 05:07:20 +08:00
|
|
|
|
|
|
|
return MakeAddrLValue(AddV, MPT->getPointeeType());
|
2009-10-23 06:57:31 +08:00
|
|
|
}
|
2011-10-11 10:20:01 +08:00
|
|
|
|
2013-03-08 05:37:08 +08:00
|
|
|
/// Given the address of a temporary variable, produce an r-value of
|
|
|
|
/// its type.
|
|
|
|
RValue CodeGenFunction::convertTempToRValue(llvm::Value *addr,
|
2013-10-02 10:29:49 +08:00
|
|
|
QualType type,
|
|
|
|
SourceLocation loc) {
|
2013-03-08 05:37:08 +08:00
|
|
|
LValue lvalue = MakeNaturalAlignAddrLValue(addr, type);
|
|
|
|
switch (getEvaluationKind(type)) {
|
|
|
|
case TEK_Complex:
|
2013-10-02 10:29:49 +08:00
|
|
|
return RValue::getComplex(EmitLoadOfComplex(lvalue, loc));
|
2013-03-08 05:37:08 +08:00
|
|
|
case TEK_Aggregate:
|
|
|
|
return lvalue.asAggregateRValue();
|
|
|
|
case TEK_Scalar:
|
2013-10-02 10:29:49 +08:00
|
|
|
return RValue::get(EmitLoadOfScalar(lvalue, loc));
|
2013-03-08 05:37:08 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("bad evaluation kind");
|
2011-10-11 10:20:01 +08:00
|
|
|
}
|
|
|
|
|
2012-04-10 16:23:07 +08:00
|
|
|
void CodeGenFunction::SetFPAccuracy(llvm::Value *Val, float Accuracy) {
|
2011-10-28 03:19:51 +08:00
|
|
|
assert(Val->getType()->isFPOrFPVectorTy());
|
2012-04-10 16:23:07 +08:00
|
|
|
if (Accuracy == 0.0 || !isa<llvm::Instruction>(Val))
|
2011-10-28 03:19:51 +08:00
|
|
|
return;
|
|
|
|
|
2012-04-17 00:29:47 +08:00
|
|
|
llvm::MDBuilder MDHelper(getLLVMContext());
|
|
|
|
llvm::MDNode *Node = MDHelper.createFPMath(Accuracy);
|
2011-10-28 03:19:51 +08:00
|
|
|
|
2012-04-14 20:37:26 +08:00
|
|
|
cast<llvm::Instruction>(Val)->setMetadata(llvm::LLVMContext::MD_fpmath, Node);
|
2011-10-28 03:19:51 +08:00
|
|
|
}
|
2011-11-06 17:01:30 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct LValueOrRValue {
|
|
|
|
LValue LV;
|
|
|
|
RValue RV;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static LValueOrRValue emitPseudoObjectExpr(CodeGenFunction &CGF,
|
|
|
|
const PseudoObjectExpr *E,
|
|
|
|
bool forLValue,
|
|
|
|
AggValueSlot slot) {
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<CodeGenFunction::OpaqueValueMappingData, 4> opaques;
|
2011-11-06 17:01:30 +08:00
|
|
|
|
|
|
|
// Find the result expression, if any.
|
|
|
|
const Expr *resultExpr = E->getResultExpr();
|
|
|
|
LValueOrRValue result;
|
|
|
|
|
|
|
|
for (PseudoObjectExpr::const_semantics_iterator
|
|
|
|
i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
|
|
|
|
const Expr *semantic = *i;
|
|
|
|
|
|
|
|
// If this semantic expression is an opaque value, bind it
|
|
|
|
// to the result of its source expression.
|
2014-05-09 08:08:36 +08:00
|
|
|
if (const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
|
2011-11-06 17:01:30 +08:00
|
|
|
|
|
|
|
// If this is the result expression, we may need to evaluate
|
|
|
|
// directly into the slot.
|
|
|
|
typedef CodeGenFunction::OpaqueValueMappingData OVMA;
|
|
|
|
OVMA opaqueData;
|
|
|
|
if (ov == resultExpr && ov->isRValue() && !forLValue &&
|
2013-03-08 05:37:08 +08:00
|
|
|
CodeGenFunction::hasAggregateEvaluationKind(ov->getType())) {
|
2011-11-06 17:01:30 +08:00
|
|
|
CGF.EmitAggExpr(ov->getSourceExpr(), slot);
|
|
|
|
|
|
|
|
LValue LV = CGF.MakeAddrLValue(slot.getAddr(), ov->getType());
|
|
|
|
opaqueData = OVMA::bind(CGF, ov, LV);
|
|
|
|
result.RV = slot.asRValue();
|
|
|
|
|
|
|
|
// Otherwise, emit as normal.
|
|
|
|
} else {
|
|
|
|
opaqueData = OVMA::bind(CGF, ov, ov->getSourceExpr());
|
|
|
|
|
|
|
|
// If this is the result, also evaluate the result now.
|
|
|
|
if (ov == resultExpr) {
|
|
|
|
if (forLValue)
|
|
|
|
result.LV = CGF.EmitLValue(ov);
|
|
|
|
else
|
|
|
|
result.RV = CGF.EmitAnyExpr(ov, slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opaques.push_back(opaqueData);
|
|
|
|
|
|
|
|
// Otherwise, if the expression is the result, evaluate it
|
|
|
|
// and remember the result.
|
|
|
|
} else if (semantic == resultExpr) {
|
|
|
|
if (forLValue)
|
|
|
|
result.LV = CGF.EmitLValue(semantic);
|
|
|
|
else
|
|
|
|
result.RV = CGF.EmitAnyExpr(semantic, slot);
|
|
|
|
|
|
|
|
// Otherwise, evaluate the expression in an ignored context.
|
|
|
|
} else {
|
|
|
|
CGF.EmitIgnoredExpr(semantic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unbind all the opaques now.
|
|
|
|
for (unsigned i = 0, e = opaques.size(); i != e; ++i)
|
|
|
|
opaques[i].unbind(CGF);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
RValue CodeGenFunction::EmitPseudoObjectRValue(const PseudoObjectExpr *E,
|
|
|
|
AggValueSlot slot) {
|
|
|
|
return emitPseudoObjectExpr(*this, E, false, slot).RV;
|
|
|
|
}
|
|
|
|
|
|
|
|
LValue CodeGenFunction::EmitPseudoObjectLValue(const PseudoObjectExpr *E) {
|
|
|
|
return emitPseudoObjectExpr(*this, E, true, AggValueSlot::ignored()).LV;
|
|
|
|
}
|