2007-08-24 13:35:26 +08:00
|
|
|
//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===//
|
|
|
|
//
|
|
|
|
// 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-08-24 13:35:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Expr nodes with scalar LLVM types as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2009-10-11 04:07:56 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2007-08-24 13:35:26 +08:00
|
|
|
#include "CodeGenModule.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-12 13:08:18 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-07-19 03:43:29 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2008-08-11 13:00:27 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2008-04-20 08:50:39 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2007-08-24 13:35:26 +08:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
2007-10-29 13:01:08 +08:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2007-10-16 04:28:48 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2009-04-02 04:28:16 +08:00
|
|
|
#include "llvm/Module.h"
|
2008-11-12 16:38:24 +08:00
|
|
|
#include "llvm/Support/CFG.h"
|
2009-02-22 04:00:35 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2008-01-03 15:05:49 +08:00
|
|
|
#include <cstdarg>
|
2007-12-11 07:44:32 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
using llvm::Value;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Scalar Expression Emitter
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
struct BinOpInfo {
|
|
|
|
Value *LHS;
|
|
|
|
Value *RHS;
|
2007-08-25 05:00:35 +08:00
|
|
|
QualType Ty; // Computation Type.
|
2007-08-24 13:35:26 +08:00
|
|
|
const BinaryOperator *E;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:45:26 +08:00
|
|
|
class ScalarExprEmitter
|
2007-08-24 13:35:26 +08:00
|
|
|
: public StmtVisitor<ScalarExprEmitter, Value*> {
|
|
|
|
CodeGenFunction &CGF;
|
2008-11-01 09:53:16 +08:00
|
|
|
CGBuilderTy &Builder;
|
2009-05-29 23:46:01 +08:00
|
|
|
bool IgnoreResultAssign;
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::LLVMContext &VMContext;
|
2007-08-24 13:35:26 +08:00
|
|
|
public:
|
|
|
|
|
2009-05-29 23:46:01 +08:00
|
|
|
ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false)
|
2009-09-09 21:00:44 +08:00
|
|
|
: CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira),
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext(cgf.getLLVMContext()) {
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Utilities
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2009-05-29 23:46:01 +08:00
|
|
|
bool TestAndClearIgnoreResultAssign() {
|
2009-07-08 09:08:03 +08:00
|
|
|
bool I = IgnoreResultAssign;
|
|
|
|
IgnoreResultAssign = false;
|
|
|
|
return I;
|
|
|
|
}
|
2009-05-29 23:46:01 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
|
|
|
|
LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
|
2009-12-16 10:57:00 +08:00
|
|
|
LValue EmitCheckedLValue(const Expr *E) { return CGF.EmitCheckedLValue(E); }
|
2007-08-24 13:35:26 +08:00
|
|
|
|
|
|
|
Value *EmitLoadOfLValue(LValue LV, QualType T) {
|
2007-09-01 06:49:20 +08:00
|
|
|
return CGF.EmitLoadOfLValue(LV, T).getScalarVal();
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
/// EmitLoadOfLValue - Given an expression with complex type that represents a
|
|
|
|
/// value l-value, this method emits the address of the l-value, then loads
|
|
|
|
/// and returns the result.
|
|
|
|
Value *EmitLoadOfLValue(const Expr *E) {
|
2009-12-16 10:57:00 +08:00
|
|
|
return EmitLoadOfLValue(EmitCheckedLValue(E), E->getType());
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-27 00:42:57 +08:00
|
|
|
/// EmitConversionToBool - Convert the specified expression value to a
|
2007-08-27 01:25:57 +08:00
|
|
|
/// boolean (i1) truth value. This is equivalent to "Val != 0".
|
2007-08-27 00:42:57 +08:00
|
|
|
Value *EmitConversionToBool(Value *Src, QualType DstTy);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-26 14:48:56 +08:00
|
|
|
/// EmitScalarConversion - Emit a conversion from the specified type to the
|
|
|
|
/// specified destination type, both of which are LLVM scalar types.
|
2007-08-27 00:34:22 +08:00
|
|
|
Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
|
|
|
|
|
|
|
|
/// EmitComplexToScalarConversion - Emit a conversion from the specified
|
2009-09-09 21:00:44 +08:00
|
|
|
/// complex type to the specified destination type, where the destination type
|
|
|
|
/// is an LLVM scalar type.
|
2007-08-27 00:34:22 +08:00
|
|
|
Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
|
|
|
|
QualType SrcTy, QualType DstTy);
|
2009-02-13 02:29:15 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Value *VisitStmt(Stmt *S) {
|
2007-12-12 05:27:55 +08:00
|
|
|
S->dump(CGF.getContext().getSourceManager());
|
2007-08-24 13:35:26 +08:00
|
|
|
assert(0 && "Stmt can't have complex result type!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Value *VisitExpr(Expr *S);
|
2009-10-22 07:45:42 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); }
|
|
|
|
|
|
|
|
// Leaves.
|
|
|
|
Value *VisitIntegerLiteral(const IntegerLiteral *E) {
|
2009-07-25 07:12:58 +08:00
|
|
|
return llvm::ConstantInt::get(VMContext, E->getValue());
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
Value *VisitFloatingLiteral(const FloatingLiteral *E) {
|
2009-07-28 05:00:51 +08:00
|
|
|
return llvm::ConstantFP::get(VMContext, E->getValue());
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
Value *VisitCharacterLiteral(const CharacterLiteral *E) {
|
2009-07-25 07:12:58 +08:00
|
|
|
return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
|
2007-11-15 13:40:03 +08:00
|
|
|
}
|
|
|
|
Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
|
2009-07-25 07:12:58 +08:00
|
|
|
return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2008-08-24 03:35:47 +08:00
|
|
|
Value *VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
|
2009-08-01 04:28:54 +08:00
|
|
|
return llvm::Constant::getNullValue(ConvertType(E->getType()));
|
2008-08-24 03:35:47 +08:00
|
|
|
}
|
2008-12-22 06:39:40 +08:00
|
|
|
Value *VisitGNUNullExpr(const GNUNullExpr *E) {
|
2009-08-01 04:28:54 +08:00
|
|
|
return llvm::Constant::getNullValue(ConvertType(E->getType()));
|
2008-12-22 06:39:40 +08:00
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
|
2009-07-25 07:12:58 +08:00
|
|
|
return llvm::ConstantInt::get(ConvertType(E->getType()),
|
2007-10-16 04:41:53 +08:00
|
|
|
CGF.getContext().typesAreCompatible(
|
|
|
|
E->getArgType1(), E->getArgType2()));
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
Value *VisitOffsetOfExpr(const OffsetOfExpr *E);
|
2008-11-12 01:56:53 +08:00
|
|
|
Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
|
2008-08-05 00:51:22 +08:00
|
|
|
Value *VisitAddrLabelExpr(const AddrLabelExpr *E) {
|
2009-10-29 07:59:40 +08:00
|
|
|
llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel());
|
|
|
|
return Builder.CreateBitCast(V, ConvertType(E->getType()));
|
2008-08-05 00:51:22 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// l-values.
|
|
|
|
Value *VisitDeclRefExpr(DeclRefExpr *E) {
|
2009-11-26 11:22:21 +08:00
|
|
|
Expr::EvalResult Result;
|
|
|
|
if (E->Evaluate(Result, CGF.getContext()) && Result.Val.isInt()) {
|
|
|
|
assert(!Result.HasSideEffects && "Constant declref with side-effect?!");
|
|
|
|
return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
|
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
return EmitLoadOfLValue(E);
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
|
|
|
|
return CGF.EmitObjCSelectorExpr(E);
|
2008-08-27 14:57:25 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
|
|
|
|
return CGF.EmitObjCProtocolExpr(E);
|
2008-08-27 14:57:25 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
|
2008-08-27 14:57:25 +08:00
|
|
|
return EmitLoadOfLValue(E);
|
|
|
|
}
|
2008-08-23 18:51:21 +08:00
|
|
|
Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
|
2008-08-29 16:11:39 +08:00
|
|
|
return EmitLoadOfLValue(E);
|
2008-08-27 14:57:25 +08:00
|
|
|
}
|
2009-08-21 01:02:02 +08:00
|
|
|
Value *VisitObjCImplicitSetterGetterRefExpr(
|
|
|
|
ObjCImplicitSetterGetterRefExpr *E) {
|
2008-11-23 06:30:21 +08:00
|
|
|
return EmitLoadOfLValue(E);
|
|
|
|
}
|
2008-08-27 14:57:25 +08:00
|
|
|
Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
|
|
|
|
return CGF.EmitObjCMessageExpr(E).getScalarVal();
|
2008-08-23 18:51:21 +08:00
|
|
|
}
|
|
|
|
|
2009-12-10 03:05:56 +08:00
|
|
|
Value *VisitObjCIsaExpr(ObjCIsaExpr *E) {
|
2009-12-10 07:35:29 +08:00
|
|
|
LValue LV = CGF.EmitObjCIsaExpr(E);
|
|
|
|
Value *V = CGF.EmitLoadOfLValue(LV, E->getType()).getScalarVal();
|
2009-12-10 03:05:56 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
|
2008-05-15 03:38:39 +08:00
|
|
|
Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
|
2009-11-26 11:22:21 +08:00
|
|
|
Value *VisitMemberExpr(MemberExpr *E);
|
2008-04-19 07:10:10 +08:00
|
|
|
Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
|
2008-10-27 07:53:12 +08:00
|
|
|
Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
return EmitLoadOfLValue(E);
|
|
|
|
}
|
2007-10-25 01:18:43 +08:00
|
|
|
|
2009-10-19 04:10:40 +08:00
|
|
|
Value *VisitInitListExpr(InitListExpr *E);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-01-30 01:44:32 +08:00
|
|
|
Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
|
2009-08-01 04:28:54 +08:00
|
|
|
return llvm::Constant::getNullValue(ConvertType(E->getType()));
|
2009-01-30 01:44:32 +08:00
|
|
|
}
|
2009-11-27 12:41:50 +08:00
|
|
|
Value *VisitCastExpr(CastExpr *E) {
|
2009-04-20 11:54:15 +08:00
|
|
|
// Make sure to evaluate VLA bounds now so that we have them for later.
|
|
|
|
if (E->getType()->isVariablyModifiedType())
|
|
|
|
CGF.EmitVLASize(E->getType());
|
|
|
|
|
2009-09-23 06:00:46 +08:00
|
|
|
return EmitCastExpr(E);
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2009-11-27 12:41:50 +08:00
|
|
|
Value *EmitCastExpr(CastExpr *E);
|
2007-08-24 13:35:26 +08:00
|
|
|
|
|
|
|
Value *VisitCallExpr(const CallExpr *E) {
|
2009-05-27 11:37:57 +08:00
|
|
|
if (E->getCallReturnType()->isReferenceType())
|
|
|
|
return EmitLoadOfLValue(E);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-09-01 06:49:20 +08:00
|
|
|
return CGF.EmitCallExpr(E).getScalarVal();
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2008-08-23 11:46:30 +08:00
|
|
|
|
2007-09-01 06:09:40 +08:00
|
|
|
Value *VisitStmtExpr(const StmtExpr *E);
|
2009-02-22 04:00:35 +08:00
|
|
|
|
2009-02-28 17:07:16 +08:00
|
|
|
Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Unary Operators.
|
2010-01-10 05:40:03 +08:00
|
|
|
Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre) {
|
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
return CGF.EmitScalarPrePostIncDec(E, LV, isInc, isPre);
|
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *VisitUnaryPostDec(const UnaryOperator *E) {
|
|
|
|
return VisitPrePostIncDec(E, false, false);
|
|
|
|
}
|
|
|
|
Value *VisitUnaryPostInc(const UnaryOperator *E) {
|
|
|
|
return VisitPrePostIncDec(E, true, false);
|
|
|
|
}
|
|
|
|
Value *VisitUnaryPreDec(const UnaryOperator *E) {
|
|
|
|
return VisitPrePostIncDec(E, false, true);
|
|
|
|
}
|
|
|
|
Value *VisitUnaryPreInc(const UnaryOperator *E) {
|
|
|
|
return VisitPrePostIncDec(E, true, true);
|
|
|
|
}
|
|
|
|
Value *VisitUnaryAddrOf(const UnaryOperator *E) {
|
|
|
|
return EmitLValue(E->getSubExpr()).getAddress();
|
|
|
|
}
|
|
|
|
Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); }
|
|
|
|
Value *VisitUnaryPlus(const UnaryOperator *E) {
|
2009-05-29 23:46:01 +08:00
|
|
|
// This differs from gcc, though, most likely due to a bug in gcc.
|
|
|
|
TestAndClearIgnoreResultAssign();
|
2007-08-24 13:35:26 +08:00
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
|
|
|
Value *VisitUnaryMinus (const UnaryOperator *E);
|
|
|
|
Value *VisitUnaryNot (const UnaryOperator *E);
|
|
|
|
Value *VisitUnaryLNot (const UnaryOperator *E);
|
2007-08-25 05:20:17 +08:00
|
|
|
Value *VisitUnaryReal (const UnaryOperator *E);
|
|
|
|
Value *VisitUnaryImag (const UnaryOperator *E);
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *VisitUnaryExtension(const UnaryOperator *E) {
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
2008-01-29 23:56:48 +08:00
|
|
|
Value *VisitUnaryOffsetOf(const UnaryOperator *E);
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
|
2009-04-15 00:58:56 +08:00
|
|
|
// C++
|
2008-04-08 12:40:51 +08:00
|
|
|
Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
|
|
|
|
return Visit(DAE->getExpr());
|
|
|
|
}
|
2009-04-15 00:58:56 +08:00
|
|
|
Value *VisitCXXThisExpr(CXXThisExpr *TE) {
|
|
|
|
return CGF.LoadCXXThis();
|
2009-09-09 21:00:44 +08:00
|
|
|
}
|
|
|
|
|
2009-05-19 12:48:36 +08:00
|
|
|
Value *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
|
2009-05-31 08:09:15 +08:00
|
|
|
return CGF.EmitCXXExprWithTemporaries(E).getScalarVal();
|
2009-05-19 12:48:36 +08:00
|
|
|
}
|
2009-05-31 09:40:14 +08:00
|
|
|
Value *VisitCXXNewExpr(const CXXNewExpr *E) {
|
|
|
|
return CGF.EmitCXXNewExpr(E);
|
|
|
|
}
|
2009-08-17 05:13:42 +08:00
|
|
|
Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
|
|
|
|
CGF.EmitCXXDeleteExpr(E);
|
|
|
|
return 0;
|
|
|
|
}
|
2009-12-11 06:40:32 +08:00
|
|
|
Value *VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
|
|
|
|
return llvm::ConstantInt::get(Builder.getInt1Ty(),
|
|
|
|
E->EvaluateTrait(CGF.getContext()));
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-09-05 01:36:40 +08:00
|
|
|
Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) {
|
|
|
|
// C++ [expr.pseudo]p1:
|
2009-09-09 21:00:44 +08:00
|
|
|
// The result shall only be used as the operand for the function call
|
2009-09-05 01:36:40 +08:00
|
|
|
// 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
|
|
|
|
// arrow.
|
|
|
|
CGF.EmitScalarExpr(E->getBase());
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-09-15 12:39:46 +08:00
|
|
|
Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
|
|
|
|
return llvm::Constant::getNullValue(ConvertType(E->getType()));
|
|
|
|
}
|
2009-10-30 09:42:31 +08:00
|
|
|
|
|
|
|
Value *VisitCXXThrowExpr(const CXXThrowExpr *E) {
|
|
|
|
CGF.EmitCXXThrowExpr(E);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Binary Operators.
|
|
|
|
Value *EmitMul(const BinOpInfo &Ops) {
|
2009-04-03 02:15:54 +08:00
|
|
|
if (CGF.getContext().getLangOptions().OverflowChecking
|
|
|
|
&& Ops.Ty->isSignedIntegerType())
|
2009-04-02 04:28:16 +08:00
|
|
|
return EmitOverflowCheckedBinOp(Ops);
|
2010-02-16 00:14:01 +08:00
|
|
|
if (Ops.LHS->getType()->isFPOrFPVectorTy())
|
2009-06-17 14:36:24 +08:00
|
|
|
return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul");
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
|
|
|
|
}
|
2009-04-02 04:28:16 +08:00
|
|
|
/// Create a binary op that checks for overflow.
|
|
|
|
/// Currently only supports +, - and *.
|
|
|
|
Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops);
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *EmitDiv(const BinOpInfo &Ops);
|
|
|
|
Value *EmitRem(const BinOpInfo &Ops);
|
|
|
|
Value *EmitAdd(const BinOpInfo &Ops);
|
|
|
|
Value *EmitSub(const BinOpInfo &Ops);
|
|
|
|
Value *EmitShl(const BinOpInfo &Ops);
|
|
|
|
Value *EmitShr(const BinOpInfo &Ops);
|
|
|
|
Value *EmitAnd(const BinOpInfo &Ops) {
|
|
|
|
return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and");
|
|
|
|
}
|
|
|
|
Value *EmitXor(const BinOpInfo &Ops) {
|
|
|
|
return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor");
|
|
|
|
}
|
|
|
|
Value *EmitOr (const BinOpInfo &Ops) {
|
|
|
|
return Builder.CreateOr(Ops.LHS, Ops.RHS, "or");
|
|
|
|
}
|
|
|
|
|
2007-08-25 05:00:35 +08:00
|
|
|
BinOpInfo EmitBinOps(const BinaryOperator *E);
|
2010-04-23 12:16:32 +08:00
|
|
|
LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E,
|
|
|
|
Value *(ScalarExprEmitter::*F)(const BinOpInfo &),
|
|
|
|
Value *&BitFieldResult);
|
|
|
|
|
2007-08-27 05:41:21 +08:00
|
|
|
Value *EmitCompoundAssign(const CompoundAssignOperator *E,
|
2007-08-25 05:00:35 +08:00
|
|
|
Value *(ScalarExprEmitter::*F)(const BinOpInfo &));
|
|
|
|
|
|
|
|
// Binary operators and binary compound assignment operators.
|
|
|
|
#define HANDLEBINOP(OP) \
|
2007-08-27 05:41:21 +08:00
|
|
|
Value *VisitBin ## OP(const BinaryOperator *E) { \
|
|
|
|
return Emit ## OP(EmitBinOps(E)); \
|
|
|
|
} \
|
|
|
|
Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \
|
|
|
|
return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \
|
2007-08-25 05:00:35 +08:00
|
|
|
}
|
2009-12-20 01:50:07 +08:00
|
|
|
HANDLEBINOP(Mul)
|
|
|
|
HANDLEBINOP(Div)
|
|
|
|
HANDLEBINOP(Rem)
|
|
|
|
HANDLEBINOP(Add)
|
|
|
|
HANDLEBINOP(Sub)
|
|
|
|
HANDLEBINOP(Shl)
|
|
|
|
HANDLEBINOP(Shr)
|
|
|
|
HANDLEBINOP(And)
|
|
|
|
HANDLEBINOP(Xor)
|
|
|
|
HANDLEBINOP(Or)
|
2007-08-25 05:00:35 +08:00
|
|
|
#undef HANDLEBINOP
|
2008-08-06 10:00:38 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Comparisons.
|
|
|
|
Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc,
|
|
|
|
unsigned SICmpOpc, unsigned FCmpOpc);
|
|
|
|
#define VISITCOMP(CODE, UI, SI, FP) \
|
|
|
|
Value *VisitBin##CODE(const BinaryOperator *E) { \
|
|
|
|
return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \
|
|
|
|
llvm::FCmpInst::FP); }
|
2009-12-20 01:50:07 +08:00
|
|
|
VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT)
|
|
|
|
VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT)
|
|
|
|
VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE)
|
|
|
|
VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE)
|
|
|
|
VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ)
|
|
|
|
VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE)
|
2007-08-24 13:35:26 +08:00
|
|
|
#undef VISITCOMP
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *VisitBinAssign (const BinaryOperator *E);
|
|
|
|
|
|
|
|
Value *VisitBinLAnd (const BinaryOperator *E);
|
|
|
|
Value *VisitBinLOr (const BinaryOperator *E);
|
|
|
|
Value *VisitBinComma (const BinaryOperator *E);
|
|
|
|
|
2009-11-18 17:41:26 +08:00
|
|
|
Value *VisitBinPtrMemD(const Expr *E) { return EmitLoadOfLValue(E); }
|
|
|
|
Value *VisitBinPtrMemI(const Expr *E) { return EmitLoadOfLValue(E); }
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Other Operators.
|
2009-02-13 02:29:15 +08:00
|
|
|
Value *VisitBlockExpr(const BlockExpr *BE);
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *VisitConditionalOperator(const ConditionalOperator *CO);
|
|
|
|
Value *VisitChooseExpr(ChooseExpr *CE);
|
2007-10-16 04:28:48 +08:00
|
|
|
Value *VisitVAArgExpr(VAArgExpr *VE);
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
|
|
|
|
return CGF.EmitObjCStringLiteral(E);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utilities
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-08-27 00:42:57 +08:00
|
|
|
/// EmitConversionToBool - Convert the specified expression value to a
|
2007-08-27 01:25:57 +08:00
|
|
|
/// boolean (i1) truth value. This is equivalent to "Val != 0".
|
2007-08-27 00:42:57 +08:00
|
|
|
Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
|
2009-10-23 04:10:53 +08:00
|
|
|
assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-27 00:42:57 +08:00
|
|
|
if (SrcType->isRealFloatingType()) {
|
|
|
|
// Compare against 0.0 for fp scalars.
|
2009-08-01 04:28:54 +08:00
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
|
2007-08-27 00:42:57 +08:00
|
|
|
return Builder.CreateFCmpUNE(Src, Zero, "tobool");
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-08-10 02:26:27 +08:00
|
|
|
if (SrcType->isMemberPointerType()) {
|
|
|
|
// Compare against -1.
|
|
|
|
llvm::Value *NegativeOne = llvm::Constant::getAllOnesValue(Src->getType());
|
|
|
|
return Builder.CreateICmpNE(Src, NegativeOne, "tobool");
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-08-25 18:38:11 +08:00
|
|
|
assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) &&
|
2007-08-27 00:42:57 +08:00
|
|
|
"Unknown scalar type to convert");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-27 00:42:57 +08:00
|
|
|
// Because of the type rules of C, we often end up computing a logical value,
|
|
|
|
// then zero extending it to int, then wanting it as a logical value again.
|
|
|
|
// Optimize this common case.
|
|
|
|
if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
|
2009-08-14 05:57:51 +08:00
|
|
|
if (ZI->getOperand(0)->getType() ==
|
|
|
|
llvm::Type::getInt1Ty(CGF.getLLVMContext())) {
|
2007-08-27 00:42:57 +08:00
|
|
|
Value *Result = ZI->getOperand(0);
|
2008-01-30 02:13:51 +08:00
|
|
|
// If there aren't any more uses, zap the instruction to save space.
|
|
|
|
// Note that there can be more uses, for example if this
|
|
|
|
// is the result of an assignment.
|
|
|
|
if (ZI->use_empty())
|
|
|
|
ZI->eraseFromParent();
|
2007-08-27 00:42:57 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-27 00:42:57 +08:00
|
|
|
// Compare against an integer or pointer null.
|
2009-08-01 04:28:54 +08:00
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
|
2007-08-27 00:42:57 +08:00
|
|
|
return Builder.CreateICmpNE(Src, Zero, "tobool");
|
|
|
|
}
|
|
|
|
|
2007-08-26 14:48:56 +08:00
|
|
|
/// EmitScalarConversion - Emit a conversion from the specified type to the
|
|
|
|
/// specified destination type, both of which are LLVM scalar types.
|
2007-08-27 00:34:22 +08:00
|
|
|
Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
|
|
|
|
QualType DstType) {
|
2008-07-27 06:37:01 +08:00
|
|
|
SrcType = CGF.getContext().getCanonicalType(SrcType);
|
|
|
|
DstType = CGF.getContext().getCanonicalType(DstType);
|
2007-08-26 14:48:56 +08:00
|
|
|
if (SrcType == DstType) return Src;
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-26 15:21:11 +08:00
|
|
|
if (DstType->isVoidType()) return 0;
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::LLVMContext &VMContext = CGF.getLLVMContext();
|
2007-08-26 14:48:56 +08:00
|
|
|
|
|
|
|
// Handle conversions to bool first, they are special: comparisons against 0.
|
2007-08-27 00:52:28 +08:00
|
|
|
if (DstType->isBooleanType())
|
|
|
|
return EmitConversionToBool(Src, SrcType);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-26 14:48:56 +08:00
|
|
|
const llvm::Type *DstTy = ConvertType(DstType);
|
|
|
|
|
|
|
|
// Ignore conversions like int -> uint.
|
|
|
|
if (Src->getType() == DstTy)
|
|
|
|
return Src;
|
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
// Handle pointer conversions next: pointers can only be converted to/from
|
|
|
|
// other pointers and integers. Check for pointer types in terms of LLVM, as
|
|
|
|
// some native types (like Obj-C id) may map to a pointer type.
|
2008-08-25 17:51:32 +08:00
|
|
|
if (isa<llvm::PointerType>(DstTy)) {
|
2007-08-26 14:48:56 +08:00
|
|
|
// The source value may be an integer, or a pointer.
|
2009-09-12 12:57:16 +08:00
|
|
|
if (isa<llvm::PointerType>(Src->getType()))
|
2007-08-26 14:48:56 +08:00
|
|
|
return Builder.CreateBitCast(Src, DstTy, "conv");
|
2009-09-12 12:57:16 +08:00
|
|
|
|
2007-08-26 14:48:56 +08:00
|
|
|
assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?");
|
2009-03-04 12:02:35 +08:00
|
|
|
// First, convert to the correct width so that we control the kind of
|
|
|
|
// extension.
|
2009-09-09 21:00:44 +08:00
|
|
|
const llvm::Type *MiddleTy =
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
|
2009-03-04 12:02:35 +08:00
|
|
|
bool InputSigned = SrcType->isSignedIntegerType();
|
|
|
|
llvm::Value* IntResult =
|
|
|
|
Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
|
|
|
|
// Then, cast to pointer.
|
|
|
|
return Builder.CreateIntToPtr(IntResult, DstTy, "conv");
|
2007-08-26 14:48:56 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-08-25 17:51:32 +08:00
|
|
|
if (isa<llvm::PointerType>(Src->getType())) {
|
2007-08-26 14:48:56 +08:00
|
|
|
// Must be an ptr to int cast.
|
|
|
|
assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
|
2007-11-01 07:18:02 +08:00
|
|
|
return Builder.CreatePtrToInt(Src, DstTy, "conv");
|
2007-08-26 14:48:56 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-04-19 07:10:10 +08:00
|
|
|
// A scalar can be splatted to an extended vector of the same element type
|
2009-08-11 07:49:36 +08:00
|
|
|
if (DstType->isExtVectorType() && !SrcType->isVectorType()) {
|
2009-01-18 14:42:49 +08:00
|
|
|
// Cast the scalar to element type
|
2009-09-22 07:43:11 +08:00
|
|
|
QualType EltTy = DstType->getAs<ExtVectorType>()->getElementType();
|
2009-01-18 14:42:49 +08:00
|
|
|
llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy);
|
|
|
|
|
|
|
|
// Insert the element in element zero of an undef vector
|
2009-07-31 07:11:26 +08:00
|
|
|
llvm::Value *UnV = llvm::UndefValue::get(DstTy);
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::Value *Idx =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
|
2009-01-18 14:42:49 +08:00
|
|
|
UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
|
|
|
|
|
|
|
|
// Splat the element across to all elements
|
|
|
|
llvm::SmallVector<llvm::Constant*, 16> Args;
|
|
|
|
unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
|
|
|
|
for (unsigned i = 0; i < NumElements; i++)
|
2009-08-14 05:57:51 +08:00
|
|
|
Args.push_back(llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), 0));
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-07-29 05:22:35 +08:00
|
|
|
llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
|
2009-01-18 14:42:49 +08:00
|
|
|
llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
|
|
|
|
return Yay;
|
|
|
|
}
|
2007-12-30 10:59:45 +08:00
|
|
|
|
2008-02-02 12:51:41 +08:00
|
|
|
// Allow bitcast from vector to integer/fp of the same size.
|
2007-12-05 15:36:10 +08:00
|
|
|
if (isa<llvm::VectorType>(Src->getType()) ||
|
2008-02-02 12:51:41 +08:00
|
|
|
isa<llvm::VectorType>(DstTy))
|
2007-12-05 15:36:10 +08:00
|
|
|
return Builder.CreateBitCast(Src, DstTy, "conv");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-26 14:48:56 +08:00
|
|
|
// Finally, we have the arithmetic types: real int/float.
|
|
|
|
if (isa<llvm::IntegerType>(Src->getType())) {
|
|
|
|
bool InputSigned = SrcType->isSignedIntegerType();
|
2007-12-27 02:20:19 +08:00
|
|
|
if (isa<llvm::IntegerType>(DstTy))
|
|
|
|
return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
|
|
|
|
else if (InputSigned)
|
|
|
|
return Builder.CreateSIToFP(Src, DstTy, "conv");
|
|
|
|
else
|
|
|
|
return Builder.CreateUIToFP(Src, DstTy, "conv");
|
2007-08-26 14:48:56 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-02-16 00:14:01 +08:00
|
|
|
assert(Src->getType()->isFloatingPointTy() && "Unknown real conversion");
|
2007-08-26 14:48:56 +08:00
|
|
|
if (isa<llvm::IntegerType>(DstTy)) {
|
2007-12-27 02:20:19 +08:00
|
|
|
if (DstType->isSignedIntegerType())
|
|
|
|
return Builder.CreateFPToSI(Src, DstTy, "conv");
|
|
|
|
else
|
|
|
|
return Builder.CreateFPToUI(Src, DstTy, "conv");
|
2007-08-26 14:48:56 +08:00
|
|
|
}
|
|
|
|
|
2010-02-16 00:14:01 +08:00
|
|
|
assert(DstTy->isFloatingPointTy() && "Unknown real conversion");
|
2007-12-27 02:20:19 +08:00
|
|
|
if (DstTy->getTypeID() < Src->getType()->getTypeID())
|
|
|
|
return Builder.CreateFPTrunc(Src, DstTy, "conv");
|
|
|
|
else
|
|
|
|
return Builder.CreateFPExt(Src, DstTy, "conv");
|
2007-08-26 14:48:56 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
/// EmitComplexToScalarConversion - Emit a conversion from the specified complex
|
|
|
|
/// type to the specified destination type, where the destination type is an
|
|
|
|
/// LLVM scalar type.
|
2007-08-27 00:34:22 +08:00
|
|
|
Value *ScalarExprEmitter::
|
|
|
|
EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src,
|
|
|
|
QualType SrcTy, QualType DstTy) {
|
2007-08-27 00:52:28 +08:00
|
|
|
// Get the source element type.
|
2009-09-22 07:43:11 +08:00
|
|
|
SrcTy = SrcTy->getAs<ComplexType>()->getElementType();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-27 00:52:28 +08:00
|
|
|
// Handle conversions to bool first, they are special: comparisons against 0.
|
|
|
|
if (DstTy->isBooleanType()) {
|
|
|
|
// Complex != 0 -> (Real != 0) | (Imag != 0)
|
|
|
|
Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy);
|
|
|
|
Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy);
|
|
|
|
return Builder.CreateOr(Src.first, Src.second, "tobool");
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-27 00:34:22 +08:00
|
|
|
// C99 6.3.1.7p2: "When a value of complex type is converted to a real type,
|
|
|
|
// the imaginary part of the complex value is discarded and the value of the
|
|
|
|
// real part is converted according to the conversion rules for the
|
2009-09-09 21:00:44 +08:00
|
|
|
// corresponding real type.
|
2007-08-27 00:34:22 +08:00
|
|
|
return EmitScalarConversion(Src.first, SrcTy, DstTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitExpr(Expr *E) {
|
2008-08-16 08:56:44 +08:00
|
|
|
CGF.ErrorUnsupported(E, "scalar expression");
|
2007-08-24 13:35:26 +08:00
|
|
|
if (E->getType()->isVoidType())
|
|
|
|
return 0;
|
2009-07-31 07:11:26 +08:00
|
|
|
return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
2008-05-15 03:38:39 +08:00
|
|
|
Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
|
|
|
|
llvm::SmallVector<llvm::Constant*, 32> indices;
|
|
|
|
for (unsigned i = 2; i < E->getNumSubExprs(); i++) {
|
|
|
|
indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))));
|
|
|
|
}
|
|
|
|
Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
|
|
|
|
Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
|
2009-07-29 05:22:35 +08:00
|
|
|
Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
|
2008-05-15 03:38:39 +08:00
|
|
|
return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
|
|
|
|
}
|
2009-11-26 11:22:21 +08:00
|
|
|
Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) {
|
|
|
|
Expr::EvalResult Result;
|
|
|
|
if (E->Evaluate(Result, CGF.getContext()) && Result.Val.isInt()) {
|
|
|
|
if (E->isArrow())
|
|
|
|
CGF.EmitScalarExpr(E->getBase());
|
|
|
|
else
|
|
|
|
EmitLValue(E->getBase());
|
|
|
|
return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
|
|
|
|
}
|
|
|
|
return EmitLoadOfLValue(E);
|
|
|
|
}
|
2008-05-15 03:38:39 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
|
2009-05-29 23:46:01 +08:00
|
|
|
TestAndClearIgnoreResultAssign();
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Emit subscript expressions in rvalue context's. For most cases, this just
|
|
|
|
// loads the lvalue formed by the subscript expr. However, we have to be
|
|
|
|
// careful, because the base of a vector subscript is occasionally an rvalue,
|
|
|
|
// so we can't get it as an lvalue.
|
|
|
|
if (!E->getBase()->getType()->isVectorType())
|
|
|
|
return EmitLoadOfLValue(E);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Handle the vector case. The base must be a vector, the index must be an
|
|
|
|
// integer value.
|
|
|
|
Value *Base = Visit(E->getBase());
|
|
|
|
Value *Idx = Visit(E->getIdx());
|
2009-03-28 10:45:41 +08:00
|
|
|
bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType();
|
2009-08-14 05:57:51 +08:00
|
|
|
Idx = Builder.CreateIntCast(Idx,
|
2009-09-09 21:00:44 +08:00
|
|
|
llvm::Type::getInt32Ty(CGF.getLLVMContext()),
|
2009-08-14 05:57:51 +08:00
|
|
|
IdxSigned,
|
2009-03-28 11:27:06 +08:00
|
|
|
"vecidxcast");
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateExtractElement(Base, Idx, "vecext");
|
|
|
|
}
|
|
|
|
|
2009-10-19 04:10:40 +08:00
|
|
|
static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx,
|
|
|
|
unsigned Off, const llvm::Type *I32Ty) {
|
|
|
|
int MV = SVI->getMaskValue(Idx);
|
|
|
|
if (MV == -1)
|
|
|
|
return llvm::UndefValue::get(I32Ty);
|
|
|
|
return llvm::ConstantInt::get(I32Ty, Off+MV);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) {
|
|
|
|
bool Ignore = TestAndClearIgnoreResultAssign();
|
|
|
|
(void)Ignore;
|
|
|
|
assert (Ignore == false && "init list ignored");
|
|
|
|
unsigned NumInitElements = E->getNumInits();
|
|
|
|
|
|
|
|
if (E->hadArrayRangeDesignator())
|
|
|
|
CGF.ErrorUnsupported(E, "GNU array range designator extension");
|
|
|
|
|
|
|
|
const llvm::VectorType *VType =
|
|
|
|
dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
|
|
|
|
|
|
|
|
// We have a scalar in braces. Just use the first element.
|
|
|
|
if (!VType)
|
|
|
|
return Visit(E->getInit(0));
|
|
|
|
|
|
|
|
unsigned ResElts = VType->getNumElements();
|
|
|
|
const llvm::Type *I32Ty = llvm::Type::getInt32Ty(CGF.getLLVMContext());
|
|
|
|
|
|
|
|
// Loop over initializers collecting the Value for each, and remembering
|
|
|
|
// whether the source was swizzle (ExtVectorElementExpr). This will allow
|
|
|
|
// us to fold the shuffle for the swizzle into the shuffle for the vector
|
|
|
|
// initializer, since LLVM optimizers generally do not want to touch
|
|
|
|
// shuffles.
|
|
|
|
unsigned CurIdx = 0;
|
|
|
|
bool VIsUndefShuffle = false;
|
|
|
|
llvm::Value *V = llvm::UndefValue::get(VType);
|
|
|
|
for (unsigned i = 0; i != NumInitElements; ++i) {
|
|
|
|
Expr *IE = E->getInit(i);
|
|
|
|
Value *Init = Visit(IE);
|
|
|
|
llvm::SmallVector<llvm::Constant*, 16> Args;
|
|
|
|
|
|
|
|
const llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType());
|
|
|
|
|
|
|
|
// Handle scalar elements. If the scalar initializer is actually one
|
|
|
|
// element of a different vector of the same width, use shuffle instead of
|
|
|
|
// extract+insert.
|
|
|
|
if (!VVT) {
|
|
|
|
if (isa<ExtVectorElementExpr>(IE)) {
|
|
|
|
llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init);
|
|
|
|
|
|
|
|
if (EI->getVectorOperandType()->getNumElements() == ResElts) {
|
|
|
|
llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand());
|
|
|
|
Value *LHS = 0, *RHS = 0;
|
|
|
|
if (CurIdx == 0) {
|
|
|
|
// insert into undef -> shuffle (src, undef)
|
|
|
|
Args.push_back(C);
|
|
|
|
for (unsigned j = 1; j != ResElts; ++j)
|
|
|
|
Args.push_back(llvm::UndefValue::get(I32Ty));
|
|
|
|
|
|
|
|
LHS = EI->getVectorOperand();
|
|
|
|
RHS = V;
|
|
|
|
VIsUndefShuffle = true;
|
|
|
|
} else if (VIsUndefShuffle) {
|
|
|
|
// insert into undefshuffle && size match -> shuffle (v, src)
|
|
|
|
llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V);
|
|
|
|
for (unsigned j = 0; j != CurIdx; ++j)
|
|
|
|
Args.push_back(getMaskElt(SVV, j, 0, I32Ty));
|
|
|
|
Args.push_back(llvm::ConstantInt::get(I32Ty,
|
|
|
|
ResElts + C->getZExtValue()));
|
|
|
|
for (unsigned j = CurIdx + 1; j != ResElts; ++j)
|
|
|
|
Args.push_back(llvm::UndefValue::get(I32Ty));
|
|
|
|
|
|
|
|
LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
|
|
|
|
RHS = EI->getVectorOperand();
|
|
|
|
VIsUndefShuffle = false;
|
|
|
|
}
|
|
|
|
if (!Args.empty()) {
|
|
|
|
llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts);
|
|
|
|
V = Builder.CreateShuffleVector(LHS, RHS, Mask);
|
|
|
|
++CurIdx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value *Idx = llvm::ConstantInt::get(I32Ty, CurIdx);
|
|
|
|
V = Builder.CreateInsertElement(V, Init, Idx, "vecinit");
|
|
|
|
VIsUndefShuffle = false;
|
|
|
|
++CurIdx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned InitElts = VVT->getNumElements();
|
|
|
|
|
|
|
|
// If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's
|
|
|
|
// input is the same width as the vector being constructed, generate an
|
|
|
|
// optimized shuffle of the swizzle input into the result.
|
2009-10-25 10:26:01 +08:00
|
|
|
unsigned Offset = (CurIdx == 0) ? 0 : ResElts;
|
2009-10-19 04:10:40 +08:00
|
|
|
if (isa<ExtVectorElementExpr>(IE)) {
|
|
|
|
llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init);
|
|
|
|
Value *SVOp = SVI->getOperand(0);
|
|
|
|
const llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType());
|
|
|
|
|
|
|
|
if (OpTy->getNumElements() == ResElts) {
|
|
|
|
for (unsigned j = 0; j != CurIdx; ++j) {
|
|
|
|
// If the current vector initializer is a shuffle with undef, merge
|
|
|
|
// this shuffle directly into it.
|
|
|
|
if (VIsUndefShuffle) {
|
|
|
|
Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0,
|
|
|
|
I32Ty));
|
|
|
|
} else {
|
|
|
|
Args.push_back(llvm::ConstantInt::get(I32Ty, j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned j = 0, je = InitElts; j != je; ++j)
|
|
|
|
Args.push_back(getMaskElt(SVI, j, Offset, I32Ty));
|
|
|
|
for (unsigned j = CurIdx + InitElts; j != ResElts; ++j)
|
|
|
|
Args.push_back(llvm::UndefValue::get(I32Ty));
|
|
|
|
|
|
|
|
if (VIsUndefShuffle)
|
|
|
|
V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
|
|
|
|
|
|
|
|
Init = SVOp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extend init to result vector length, and then shuffle its contribution
|
|
|
|
// to the vector initializer into V.
|
|
|
|
if (Args.empty()) {
|
|
|
|
for (unsigned j = 0; j != InitElts; ++j)
|
|
|
|
Args.push_back(llvm::ConstantInt::get(I32Ty, j));
|
|
|
|
for (unsigned j = InitElts; j != ResElts; ++j)
|
|
|
|
Args.push_back(llvm::UndefValue::get(I32Ty));
|
|
|
|
llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts);
|
|
|
|
Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT),
|
2009-10-25 10:26:01 +08:00
|
|
|
Mask, "vext");
|
2009-10-19 04:10:40 +08:00
|
|
|
|
|
|
|
Args.clear();
|
|
|
|
for (unsigned j = 0; j != CurIdx; ++j)
|
|
|
|
Args.push_back(llvm::ConstantInt::get(I32Ty, j));
|
|
|
|
for (unsigned j = 0; j != InitElts; ++j)
|
2009-10-25 10:26:01 +08:00
|
|
|
Args.push_back(llvm::ConstantInt::get(I32Ty, j+Offset));
|
2009-10-19 04:10:40 +08:00
|
|
|
for (unsigned j = CurIdx + InitElts; j != ResElts; ++j)
|
|
|
|
Args.push_back(llvm::UndefValue::get(I32Ty));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If V is undef, make sure it ends up on the RHS of the shuffle to aid
|
|
|
|
// merging subsequent shuffles into this one.
|
|
|
|
if (CurIdx == 0)
|
|
|
|
std::swap(V, Init);
|
|
|
|
llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts);
|
|
|
|
V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit");
|
|
|
|
VIsUndefShuffle = isa<llvm::UndefValue>(Init);
|
|
|
|
CurIdx += InitElts;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: evaluate codegen vs. shuffling against constant null vector.
|
|
|
|
// Emit remaining default initializers.
|
|
|
|
const llvm::Type *EltTy = VType->getElementType();
|
|
|
|
|
|
|
|
// Emit remaining default initializers
|
|
|
|
for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) {
|
|
|
|
Value *Idx = llvm::ConstantInt::get(I32Ty, CurIdx);
|
|
|
|
llvm::Value *Init = llvm::Constant::getNullValue(EltTy);
|
|
|
|
V = Builder.CreateInsertElement(V, Init, Idx, "vecinit");
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2009-11-24 01:57:54 +08:00
|
|
|
static bool ShouldNullCheckClassCastValue(const CastExpr *CE) {
|
|
|
|
const Expr *E = CE->getSubExpr();
|
2010-03-31 07:58:03 +08:00
|
|
|
|
|
|
|
if (CE->getCastKind() == CastExpr::CK_UncheckedDerivedToBase)
|
|
|
|
return false;
|
2009-11-24 01:57:54 +08:00
|
|
|
|
|
|
|
if (isa<CXXThisExpr>(E)) {
|
|
|
|
// We always assume that 'this' is never null.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) {
|
|
|
|
// And that lvalue casts are never null.
|
|
|
|
if (ICE->isLvalueCast())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-25 02:37:17 +08:00
|
|
|
// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts
|
|
|
|
// have to handle a more broad range of conversions than explicit casts, as they
|
|
|
|
// handle things like function to ptr-to-function decay etc.
|
2009-11-27 12:41:50 +08:00
|
|
|
Value *ScalarExprEmitter::EmitCastExpr(CastExpr *CE) {
|
|
|
|
Expr *E = CE->getSubExpr();
|
2009-09-23 06:00:46 +08:00
|
|
|
QualType DestTy = CE->getType();
|
|
|
|
CastExpr::CastKind Kind = CE->getCastKind();
|
|
|
|
|
2009-08-25 02:37:17 +08:00
|
|
|
if (!DestTy->isVoidType())
|
|
|
|
TestAndClearIgnoreResultAssign();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-11-27 10:07:44 +08:00
|
|
|
// Since almost all cast kinds apply to scalars, this switch doesn't have
|
|
|
|
// a default case, so the compiler will warn on a missing case. The cases
|
|
|
|
// are in the same order as in the CastKind enum.
|
2009-08-25 02:37:17 +08:00
|
|
|
switch (Kind) {
|
2009-09-15 12:48:33 +08:00
|
|
|
case CastExpr::CK_Unknown:
|
2009-11-27 12:41:50 +08:00
|
|
|
// FIXME: All casts should have a known kind!
|
2009-11-17 05:33:53 +08:00
|
|
|
//assert(0 && "Unknown cast kind!");
|
2009-08-25 02:37:17 +08:00
|
|
|
break;
|
2009-11-17 05:33:53 +08:00
|
|
|
|
2009-12-09 07:09:15 +08:00
|
|
|
case CastExpr::CK_AnyPointerToObjCPointerCast:
|
2009-12-12 06:40:48 +08:00
|
|
|
case CastExpr::CK_AnyPointerToBlockPointerCast:
|
2009-09-02 04:52:42 +08:00
|
|
|
case CastExpr::CK_BitCast: {
|
|
|
|
Value *Src = Visit(const_cast<Expr*>(E));
|
|
|
|
return Builder.CreateBitCast(Src, ConvertType(DestTy));
|
|
|
|
}
|
2009-11-17 05:33:53 +08:00
|
|
|
case CastExpr::CK_NoOp:
|
2009-12-18 22:42:03 +08:00
|
|
|
case CastExpr::CK_UserDefinedConversion:
|
2009-11-17 05:33:53 +08:00
|
|
|
return Visit(const_cast<Expr*>(E));
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-11-24 01:57:54 +08:00
|
|
|
case CastExpr::CK_BaseToDerived: {
|
|
|
|
const CXXRecordDecl *DerivedClassDecl =
|
|
|
|
DestTy->getCXXRecordDeclForPointerType();
|
|
|
|
|
2010-04-25 05:23:59 +08:00
|
|
|
return CGF.GetAddressOfDerivedClass(Visit(E), DerivedClassDecl,
|
|
|
|
CE->getBasePath(),
|
|
|
|
ShouldNullCheckClassCastValue(CE));
|
2009-11-24 01:57:54 +08:00
|
|
|
}
|
2010-03-31 07:58:03 +08:00
|
|
|
case CastExpr::CK_UncheckedDerivedToBase:
|
2009-09-12 12:57:16 +08:00
|
|
|
case CastExpr::CK_DerivedToBase: {
|
|
|
|
const RecordType *DerivedClassTy =
|
|
|
|
E->getType()->getAs<PointerType>()->getPointeeType()->getAs<RecordType>();
|
|
|
|
CXXRecordDecl *DerivedClassDecl =
|
|
|
|
cast<CXXRecordDecl>(DerivedClassTy->getDecl());
|
|
|
|
|
2010-04-25 05:06:20 +08:00
|
|
|
return CGF.GetAddressOfBaseClass(Visit(E), DerivedClassDecl,
|
|
|
|
CE->getBasePath(),
|
|
|
|
ShouldNullCheckClassCastValue(CE));
|
2009-09-12 12:57:16 +08:00
|
|
|
}
|
2009-11-27 10:07:44 +08:00
|
|
|
case CastExpr::CK_Dynamic: {
|
|
|
|
Value *V = Visit(const_cast<Expr*>(E));
|
|
|
|
const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE);
|
|
|
|
return CGF.EmitDynamicCast(V, DCE);
|
|
|
|
}
|
2009-11-27 12:41:50 +08:00
|
|
|
case CastExpr::CK_ToUnion:
|
2009-11-17 05:33:53 +08:00
|
|
|
assert(0 && "Should be unreachable!");
|
|
|
|
break;
|
2009-11-27 12:41:50 +08:00
|
|
|
|
2009-11-17 05:33:53 +08:00
|
|
|
case CastExpr::CK_ArrayToPointerDecay: {
|
|
|
|
assert(E->getType()->isArrayType() &&
|
|
|
|
"Array to pointer decay must have array source type!");
|
|
|
|
|
|
|
|
Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays.
|
|
|
|
|
|
|
|
// Note that VLA pointers are always decayed, so we don't need to do
|
|
|
|
// anything here.
|
|
|
|
if (!E->getType()->isVariableArrayType()) {
|
|
|
|
assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
|
|
|
|
assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
|
|
|
|
->getElementType()) &&
|
|
|
|
"Expected pointer to array");
|
|
|
|
V = Builder.CreateStructGEP(V, 0, "arraydecay");
|
|
|
|
}
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
case CastExpr::CK_FunctionToPointerDecay:
|
|
|
|
return EmitLValue(E).getAddress();
|
|
|
|
|
|
|
|
case CastExpr::CK_NullToMemberPointer:
|
|
|
|
return CGF.CGM.EmitNullConstant(DestTy);
|
2009-09-12 12:57:16 +08:00
|
|
|
|
2009-11-27 10:07:44 +08:00
|
|
|
case CastExpr::CK_BaseToDerivedMemberPointer:
|
2009-11-27 12:41:50 +08:00
|
|
|
case CastExpr::CK_DerivedToBaseMemberPointer: {
|
|
|
|
Value *Src = Visit(E);
|
|
|
|
|
|
|
|
// See if we need to adjust the pointer.
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(E->getType()->getAs<MemberPointerType>()->
|
|
|
|
getClass()->getAs<RecordType>()->getDecl());
|
|
|
|
const CXXRecordDecl *DerivedDecl =
|
|
|
|
cast<CXXRecordDecl>(CE->getType()->getAs<MemberPointerType>()->
|
|
|
|
getClass()->getAs<RecordType>()->getDecl());
|
|
|
|
if (CE->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer)
|
|
|
|
std::swap(DerivedDecl, BaseDecl);
|
|
|
|
|
2010-01-31 09:36:53 +08:00
|
|
|
if (llvm::Constant *Adj =
|
2010-04-25 05:27:51 +08:00
|
|
|
CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
|
|
|
|
CE->getBasePath())) {
|
2009-11-27 12:41:50 +08:00
|
|
|
if (CE->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer)
|
|
|
|
Src = Builder.CreateSub(Src, Adj, "adj");
|
|
|
|
else
|
|
|
|
Src = Builder.CreateAdd(Src, Adj, "adj");
|
|
|
|
}
|
|
|
|
return Src;
|
|
|
|
}
|
|
|
|
|
2009-11-27 10:07:44 +08:00
|
|
|
case CastExpr::CK_ConstructorConversion:
|
2009-11-27 12:41:50 +08:00
|
|
|
assert(0 && "Should be unreachable!");
|
2009-11-27 10:07:44 +08:00
|
|
|
break;
|
|
|
|
|
2009-09-15 12:48:33 +08:00
|
|
|
case CastExpr::CK_IntegralToPointer: {
|
|
|
|
Value *Src = Visit(const_cast<Expr*>(E));
|
2009-10-19 02:12:03 +08:00
|
|
|
|
|
|
|
// First, convert to the correct width so that we control the kind of
|
|
|
|
// extension.
|
|
|
|
const llvm::Type *MiddleTy =
|
|
|
|
llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
|
|
|
|
bool InputSigned = E->getType()->isSignedIntegerType();
|
|
|
|
llvm::Value* IntResult =
|
|
|
|
Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
|
|
|
|
|
|
|
|
return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy));
|
2009-09-15 12:48:33 +08:00
|
|
|
}
|
|
|
|
case CastExpr::CK_PointerToIntegral: {
|
|
|
|
Value *Src = Visit(const_cast<Expr*>(E));
|
|
|
|
return Builder.CreatePtrToInt(Src, ConvertType(DestTy));
|
|
|
|
}
|
2009-11-17 05:33:53 +08:00
|
|
|
case CastExpr::CK_ToVoid: {
|
|
|
|
CGF.EmitAnyExpr(E, 0, false, true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case CastExpr::CK_VectorSplat: {
|
|
|
|
const llvm::Type *DstTy = ConvertType(DestTy);
|
|
|
|
Value *Elt = Visit(const_cast<Expr*>(E));
|
|
|
|
|
|
|
|
// Insert the element in element zero of an undef vector
|
|
|
|
llvm::Value *UnV = llvm::UndefValue::get(DstTy);
|
|
|
|
llvm::Value *Idx =
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0);
|
|
|
|
UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp");
|
|
|
|
|
|
|
|
// Splat the element across to all elements
|
|
|
|
llvm::SmallVector<llvm::Constant*, 16> Args;
|
|
|
|
unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
|
|
|
|
for (unsigned i = 0; i < NumElements; i++)
|
|
|
|
Args.push_back(llvm::ConstantInt::get(
|
|
|
|
llvm::Type::getInt32Ty(VMContext), 0));
|
|
|
|
|
|
|
|
llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements);
|
|
|
|
llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat");
|
|
|
|
return Yay;
|
|
|
|
}
|
2009-11-27 10:07:44 +08:00
|
|
|
case CastExpr::CK_IntegralCast:
|
|
|
|
case CastExpr::CK_IntegralToFloating:
|
|
|
|
case CastExpr::CK_FloatingToIntegral:
|
|
|
|
case CastExpr::CK_FloatingCast:
|
2009-11-27 12:41:50 +08:00
|
|
|
return EmitScalarConversion(Visit(E), E->getType(), DestTy);
|
2009-11-17 05:33:53 +08:00
|
|
|
|
2009-12-11 17:26:29 +08:00
|
|
|
case CastExpr::CK_MemberPointerToBoolean:
|
|
|
|
return CGF.EvaluateExprAsBool(E);
|
2009-08-25 02:26:39 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-26 15:26:12 +08:00
|
|
|
// Handle cases where the source is an non-complex type.
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-02-17 07:55:16 +08:00
|
|
|
if (!CGF.hasAggregateLLVMType(E->getType())) {
|
2007-08-26 14:48:56 +08:00
|
|
|
Value *Src = Visit(const_cast<Expr*>(E));
|
|
|
|
|
|
|
|
// Use EmitScalarConversion to perform the conversion.
|
|
|
|
return EmitScalarConversion(Src, E->getType(), DestTy);
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-04-05 00:54:41 +08:00
|
|
|
if (E->getType()->isAnyComplexType()) {
|
2008-02-17 07:55:16 +08:00
|
|
|
// Handle cases where the source is a complex type.
|
2009-05-29 23:46:01 +08:00
|
|
|
bool IgnoreImag = true;
|
|
|
|
bool IgnoreImagAssign = true;
|
|
|
|
bool IgnoreReal = IgnoreResultAssign;
|
|
|
|
bool IgnoreRealAssign = IgnoreResultAssign;
|
|
|
|
if (DestTy->isBooleanType())
|
|
|
|
IgnoreImagAssign = IgnoreImag = false;
|
|
|
|
else if (DestTy->isVoidType()) {
|
|
|
|
IgnoreReal = IgnoreImag = false;
|
|
|
|
IgnoreRealAssign = IgnoreImagAssign = true;
|
|
|
|
}
|
|
|
|
CodeGenFunction::ComplexPairTy V
|
|
|
|
= CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign,
|
|
|
|
IgnoreImagAssign);
|
|
|
|
return EmitComplexToScalarConversion(V, E->getType(), DestTy);
|
2008-02-17 07:55:16 +08:00
|
|
|
}
|
2007-08-26 15:16:41 +08:00
|
|
|
|
2008-02-17 07:55:16 +08:00
|
|
|
// Okay, this is a cast from an aggregate. It must be a cast to void. Just
|
|
|
|
// evaluate the result and return.
|
2009-05-29 23:46:01 +08:00
|
|
|
CGF.EmitAggExpr(E, 0, false, true);
|
2008-02-17 07:55:16 +08:00
|
|
|
return 0;
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
2007-09-01 06:09:40 +08:00
|
|
|
Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
|
2008-07-27 04:23:23 +08:00
|
|
|
return CGF.EmitCompoundStmt(*E->getSubStmt(),
|
|
|
|
!E->getType()->isVoidType()).getScalarVal();
|
2007-09-01 06:09:40 +08:00
|
|
|
}
|
|
|
|
|
2009-02-28 17:07:16 +08:00
|
|
|
Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
|
2009-10-11 04:07:56 +08:00
|
|
|
llvm::Value *V = CGF.GetAddrOfBlockDecl(E);
|
|
|
|
if (E->getType().isObjCGCWeak())
|
|
|
|
return CGF.CGM.getObjCRuntime().EmitObjCWeakRead(CGF, V);
|
2009-11-30 05:23:36 +08:00
|
|
|
return Builder.CreateLoad(V, "tmp");
|
2009-02-22 04:00:35 +08:00
|
|
|
}
|
2007-09-01 06:09:40 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Unary Operators
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
|
2009-05-29 23:46:01 +08:00
|
|
|
TestAndClearIgnoreResultAssign();
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *Op = Visit(E->getSubExpr());
|
2010-02-16 00:14:01 +08:00
|
|
|
if (Op->getType()->isFPOrFPVectorTy())
|
2009-06-17 14:36:24 +08:00
|
|
|
return Builder.CreateFNeg(Op, "neg");
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateNeg(Op, "neg");
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) {
|
2009-05-29 23:46:01 +08:00
|
|
|
TestAndClearIgnoreResultAssign();
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *Op = Visit(E->getSubExpr());
|
|
|
|
return Builder.CreateNot(Op, "neg");
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
|
|
|
|
// Compare operand to zero.
|
|
|
|
Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Invert value.
|
|
|
|
// TODO: Could dynamically modify easy computations here. For example, if
|
|
|
|
// the operand is an icmp ne, turn into icmp eq.
|
|
|
|
BoolVal = Builder.CreateNot(BoolVal, "lnot");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-05-20 02:44:53 +08:00
|
|
|
// ZExt result to the expr type.
|
|
|
|
return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext");
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
Value *ScalarExprEmitter::VisitOffsetOfExpr(const OffsetOfExpr *E) {
|
|
|
|
Expr::EvalResult Result;
|
|
|
|
if(E->Evaluate(Result, CGF.getContext()))
|
|
|
|
return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
|
|
|
|
|
|
|
|
// FIXME: Cannot support code generation for non-constant offsetof.
|
|
|
|
unsigned DiagID = CGF.CGM.getDiags().getCustomDiagID(Diagnostic::Error,
|
|
|
|
"cannot compile non-constant __builtin_offsetof");
|
|
|
|
CGF.CGM.getDiags().Report(CGF.getContext().getFullLoc(E->getLocStart()),
|
|
|
|
DiagID)
|
|
|
|
<< E->getSourceRange();
|
|
|
|
|
|
|
|
return llvm::Constant::getNullValue(ConvertType(E->getType()));
|
|
|
|
}
|
|
|
|
|
2008-11-12 01:56:53 +08:00
|
|
|
/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of
|
|
|
|
/// argument of the sizeof expression as an integer.
|
|
|
|
Value *
|
|
|
|
ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
|
|
|
|
QualType TypeToSize = E->getTypeOfArgument();
|
2009-01-25 06:19:05 +08:00
|
|
|
if (E->isSizeOf()) {
|
2009-09-09 21:00:44 +08:00
|
|
|
if (const VariableArrayType *VAT =
|
2009-01-25 06:19:05 +08:00
|
|
|
CGF.getContext().getAsVariableArrayType(TypeToSize)) {
|
|
|
|
if (E->isArgumentType()) {
|
|
|
|
// sizeof(type) - make sure to emit the VLA size.
|
|
|
|
CGF.EmitVLASize(TypeToSize);
|
2009-04-20 11:21:44 +08:00
|
|
|
} else {
|
|
|
|
// C99 6.5.3.4p2: If the argument is an expression of type
|
|
|
|
// VLA, it is evaluated.
|
|
|
|
CGF.EmitAnyExpr(E->getArgumentExpr());
|
2009-01-25 06:19:05 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-02-06 03:43:10 +08:00
|
|
|
return CGF.GetVLASize(VAT);
|
2008-12-21 11:33:21 +08:00
|
|
|
}
|
2008-12-12 15:38:43 +08:00
|
|
|
}
|
2009-01-25 06:19:05 +08:00
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
// If this isn't sizeof(vla), the result must be constant; use the constant
|
|
|
|
// folding logic so we don't have to duplicate it here.
|
2009-01-25 06:19:05 +08:00
|
|
|
Expr::EvalResult Result;
|
|
|
|
E->Evaluate(Result, CGF.getContext());
|
2009-07-25 07:12:58 +08:00
|
|
|
return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
2007-08-25 05:20:17 +08:00
|
|
|
Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) {
|
|
|
|
Expr *Op = E->getSubExpr();
|
2008-04-05 00:54:41 +08:00
|
|
|
if (Op->getType()->isAnyComplexType())
|
2009-05-29 23:46:01 +08:00
|
|
|
return CGF.EmitComplexExpr(Op, false, true, false, true).first;
|
2007-08-25 05:20:17 +08:00
|
|
|
return Visit(Op);
|
|
|
|
}
|
|
|
|
Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
|
|
|
|
Expr *Op = E->getSubExpr();
|
2008-04-05 00:54:41 +08:00
|
|
|
if (Op->getType()->isAnyComplexType())
|
2009-05-29 23:46:01 +08:00
|
|
|
return CGF.EmitComplexExpr(Op, true, false, true, false).second;
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-05-29 23:46:01 +08:00
|
|
|
// __imag on a scalar returns zero. Emit the subexpr to ensure side
|
|
|
|
// effects are evaluated, but not the actual value.
|
|
|
|
if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid)
|
|
|
|
CGF.EmitLValue(Op);
|
|
|
|
else
|
|
|
|
CGF.EmitScalarExpr(Op, true);
|
2009-08-01 04:28:54 +08:00
|
|
|
return llvm::Constant::getNullValue(ConvertType(E->getType()));
|
2007-08-25 05:20:17 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E) {
|
2009-02-27 14:44:11 +08:00
|
|
|
Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress();
|
2009-01-25 06:38:55 +08:00
|
|
|
const llvm::Type* ResultType = ConvertType(E->getType());
|
2009-02-27 14:44:11 +08:00
|
|
|
return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof");
|
2008-01-29 23:56:48 +08:00
|
|
|
}
|
2007-08-25 05:20:17 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Binary Operators
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) {
|
2009-05-29 23:46:01 +08:00
|
|
|
TestAndClearIgnoreResultAssign();
|
2007-08-24 13:35:26 +08:00
|
|
|
BinOpInfo Result;
|
|
|
|
Result.LHS = Visit(E->getLHS());
|
|
|
|
Result.RHS = Visit(E->getRHS());
|
2007-08-25 05:00:35 +08:00
|
|
|
Result.Ty = E->getType();
|
2007-08-24 13:35:26 +08:00
|
|
|
Result.E = E;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2010-04-23 12:16:32 +08:00
|
|
|
LValue ScalarExprEmitter::EmitCompoundAssignLValue(
|
|
|
|
const CompoundAssignOperator *E,
|
|
|
|
Value *(ScalarExprEmitter::*Func)(const BinOpInfo &),
|
|
|
|
Value *&BitFieldResult) {
|
2009-12-25 23:43:36 +08:00
|
|
|
QualType LHSTy = E->getLHS()->getType();
|
2010-04-23 12:16:32 +08:00
|
|
|
BitFieldResult = 0;
|
2007-08-25 05:00:35 +08:00
|
|
|
BinOpInfo OpInfo;
|
2010-04-23 12:16:32 +08:00
|
|
|
|
2009-03-28 09:22:36 +08:00
|
|
|
if (E->getComputationResultType()->isAnyComplexType()) {
|
2009-09-09 21:00:44 +08:00
|
|
|
// This needs to go through the complex expression emitter, but it's a tad
|
|
|
|
// complicated to do that... I'm leaving it out for now. (Note that we do
|
|
|
|
// actually need the imaginary part of the RHS for multiplication and
|
|
|
|
// division.)
|
2009-03-28 09:22:36 +08:00
|
|
|
CGF.ErrorUnsupported(E, "complex compound assignment");
|
2010-04-23 12:16:32 +08:00
|
|
|
llvm::UndefValue::get(CGF.ConvertType(E->getType()));
|
|
|
|
return LValue();
|
2007-08-25 05:00:35 +08:00
|
|
|
}
|
2010-04-23 12:16:32 +08:00
|
|
|
|
2009-05-23 03:07:20 +08:00
|
|
|
// Emit the RHS first. __block variables need to have the rhs evaluated
|
|
|
|
// first, plus this should improve codegen a little.
|
|
|
|
OpInfo.RHS = Visit(E->getRHS());
|
|
|
|
OpInfo.Ty = E->getComputationResultType();
|
|
|
|
OpInfo.E = E;
|
2009-03-28 09:22:36 +08:00
|
|
|
// Load/convert the LHS.
|
2009-12-16 10:57:00 +08:00
|
|
|
LValue LHSLV = EmitCheckedLValue(E->getLHS());
|
2009-03-28 09:22:36 +08:00
|
|
|
OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy);
|
|
|
|
OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
|
|
|
|
E->getComputationLHSType());
|
2010-04-23 12:16:32 +08:00
|
|
|
|
2007-08-25 05:00:35 +08:00
|
|
|
// Expand the binary operator.
|
|
|
|
Value *Result = (this->*Func)(OpInfo);
|
2010-04-23 12:16:32 +08:00
|
|
|
|
2008-08-06 10:00:38 +08:00
|
|
|
// Convert the result back to the LHS type.
|
2009-03-28 09:22:36 +08:00
|
|
|
Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy);
|
2010-04-23 12:16:32 +08:00
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
// Store the result value into the LHS lvalue. Bit-fields are handled
|
|
|
|
// specially because the result is altered by the store, i.e., [C99 6.5.16p1]
|
|
|
|
// 'An assignment expression has the value of the left operand after the
|
|
|
|
// assignment...'.
|
2010-04-06 05:36:35 +08:00
|
|
|
if (LHSLV.isBitField()) {
|
2009-05-29 23:46:01 +08:00
|
|
|
if (!LHSLV.isVolatileQualified()) {
|
|
|
|
CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy,
|
|
|
|
&Result);
|
2010-04-23 12:16:32 +08:00
|
|
|
BitFieldResult = Result;
|
|
|
|
return LHSLV;
|
2009-05-29 23:46:01 +08:00
|
|
|
} else
|
|
|
|
CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy);
|
|
|
|
} else
|
2008-11-19 17:36:46 +08:00
|
|
|
CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy);
|
2010-04-23 12:16:32 +08:00
|
|
|
return LHSLV;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
|
|
|
|
Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
|
|
|
|
bool Ignore = TestAndClearIgnoreResultAssign();
|
|
|
|
Value *BitFieldResult;
|
|
|
|
LValue LHSLV = EmitCompoundAssignLValue(E, Func, BitFieldResult);
|
|
|
|
if (BitFieldResult)
|
|
|
|
return BitFieldResult;
|
|
|
|
|
2009-05-29 23:46:01 +08:00
|
|
|
if (Ignore)
|
|
|
|
return 0;
|
|
|
|
return EmitLoadOfLValue(LHSLV, E->getType());
|
2007-08-25 05:00:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
|
2010-02-16 00:14:01 +08:00
|
|
|
if (Ops.LHS->getType()->isFPOrFPVectorTy())
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
|
2007-08-25 05:00:35 +08:00
|
|
|
else if (Ops.Ty->isUnsignedIntegerType())
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div");
|
|
|
|
else
|
|
|
|
return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div");
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
|
|
|
|
// Rem in C can't be a floating point type: C99 6.5.5p2.
|
2007-08-25 05:00:35 +08:00
|
|
|
if (Ops.Ty->isUnsignedIntegerType())
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem");
|
|
|
|
else
|
|
|
|
return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem");
|
|
|
|
}
|
|
|
|
|
2009-04-02 04:28:16 +08:00
|
|
|
Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
|
|
|
|
unsigned IID;
|
|
|
|
unsigned OpID = 0;
|
2009-04-02 09:03:55 +08:00
|
|
|
|
2009-04-03 02:15:54 +08:00
|
|
|
switch (Ops.E->getOpcode()) {
|
|
|
|
case BinaryOperator::Add:
|
|
|
|
case BinaryOperator::AddAssign:
|
|
|
|
OpID = 1;
|
|
|
|
IID = llvm::Intrinsic::sadd_with_overflow;
|
|
|
|
break;
|
|
|
|
case BinaryOperator::Sub:
|
|
|
|
case BinaryOperator::SubAssign:
|
|
|
|
OpID = 2;
|
|
|
|
IID = llvm::Intrinsic::ssub_with_overflow;
|
|
|
|
break;
|
|
|
|
case BinaryOperator::Mul:
|
|
|
|
case BinaryOperator::MulAssign:
|
|
|
|
OpID = 3;
|
|
|
|
IID = llvm::Intrinsic::smul_with_overflow;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false && "Unsupported operation for overflow detection");
|
2009-04-09 00:23:09 +08:00
|
|
|
IID = 0;
|
2009-04-03 02:15:54 +08:00
|
|
|
}
|
|
|
|
OpID <<= 1;
|
|
|
|
OpID |= 1;
|
|
|
|
|
2009-04-02 04:28:16 +08:00
|
|
|
const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
|
|
|
|
|
|
|
|
llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1);
|
|
|
|
|
|
|
|
Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS);
|
|
|
|
Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
|
|
|
|
Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
|
|
|
|
|
|
|
|
// Branch in case of overflow.
|
|
|
|
llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
|
|
|
|
llvm::BasicBlock *overflowBB =
|
|
|
|
CGF.createBasicBlock("overflow", CGF.CurFn);
|
|
|
|
llvm::BasicBlock *continueBB =
|
|
|
|
CGF.createBasicBlock("overflow.continue", CGF.CurFn);
|
|
|
|
|
|
|
|
Builder.CreateCondBr(overflow, overflowBB, continueBB);
|
|
|
|
|
|
|
|
// Handle overflow
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(overflowBB);
|
|
|
|
|
|
|
|
// Handler is:
|
2009-09-09 21:00:44 +08:00
|
|
|
// long long *__overflow_handler)(long long a, long long b, char op,
|
2009-04-02 04:28:16 +08:00
|
|
|
// char width)
|
|
|
|
std::vector<const llvm::Type*> handerArgTypes;
|
2009-08-14 05:57:51 +08:00
|
|
|
handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext));
|
|
|
|
handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext));
|
|
|
|
handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext));
|
|
|
|
handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext));
|
|
|
|
llvm::FunctionType *handlerTy = llvm::FunctionType::get(
|
|
|
|
llvm::Type::getInt64Ty(VMContext), handerArgTypes, false);
|
2009-04-02 04:28:16 +08:00
|
|
|
llvm::Value *handlerFunction =
|
|
|
|
CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler",
|
2009-07-30 06:16:19 +08:00
|
|
|
llvm::PointerType::getUnqual(handlerTy));
|
2009-04-02 04:28:16 +08:00
|
|
|
handlerFunction = Builder.CreateLoad(handlerFunction);
|
|
|
|
|
|
|
|
llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction,
|
2009-08-14 05:57:51 +08:00
|
|
|
Builder.CreateSExt(Ops.LHS, llvm::Type::getInt64Ty(VMContext)),
|
|
|
|
Builder.CreateSExt(Ops.RHS, llvm::Type::getInt64Ty(VMContext)),
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), OpID),
|
2009-09-09 21:00:44 +08:00
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext),
|
2009-04-02 04:28:16 +08:00
|
|
|
cast<llvm::IntegerType>(opTy)->getBitWidth()));
|
|
|
|
|
|
|
|
handlerResult = Builder.CreateTrunc(handlerResult, opTy);
|
|
|
|
|
|
|
|
Builder.CreateBr(continueBB);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-04-02 04:28:16 +08:00
|
|
|
// Set up the continuation
|
|
|
|
Builder.SetInsertPoint(continueBB);
|
|
|
|
// Get the correct result
|
|
|
|
llvm::PHINode *phi = Builder.CreatePHI(opTy);
|
|
|
|
phi->reserveOperandSpace(2);
|
|
|
|
phi->addIncoming(result, initialBB);
|
|
|
|
phi->addIncoming(handlerResult, overflowBB);
|
|
|
|
|
|
|
|
return phi;
|
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
|
|
|
|
Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
|
2009-07-15 02:25:06 +08:00
|
|
|
if (!Ops.Ty->isAnyPointerType()) {
|
2009-06-17 14:36:24 +08:00
|
|
|
if (CGF.getContext().getLangOptions().OverflowChecking &&
|
|
|
|
Ops.Ty->isSignedIntegerType())
|
2009-04-02 04:28:16 +08:00
|
|
|
return EmitOverflowCheckedBinOp(Ops);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-02-16 00:14:01 +08:00
|
|
|
if (Ops.LHS->getType()->isFPOrFPVectorTy())
|
2009-06-17 14:36:24 +08:00
|
|
|
return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add");
|
2009-08-12 09:16:29 +08:00
|
|
|
|
|
|
|
// Signed integer overflow is undefined behavior.
|
|
|
|
if (Ops.Ty->isSignedIntegerType())
|
|
|
|
return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add");
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
|
2009-04-02 04:28:16 +08:00
|
|
|
}
|
2009-03-28 10:45:41 +08:00
|
|
|
|
2009-07-11 07:34:53 +08:00
|
|
|
if (Ops.Ty->isPointerType() &&
|
2009-07-30 05:53:49 +08:00
|
|
|
Ops.Ty->getAs<PointerType>()->isVariableArrayType()) {
|
2009-03-28 10:45:41 +08:00
|
|
|
// The amount of the addition needs to account for the VLA size
|
|
|
|
CGF.ErrorUnsupported(Ops.E, "VLA pointer addition");
|
|
|
|
}
|
2008-01-03 14:36:51 +08:00
|
|
|
Value *Ptr, *Idx;
|
|
|
|
Expr *IdxExp;
|
2009-07-30 05:53:49 +08:00
|
|
|
const PointerType *PT = Ops.E->getLHS()->getType()->getAs<PointerType>();
|
2009-09-09 21:00:44 +08:00
|
|
|
const ObjCObjectPointerType *OPT =
|
2009-09-22 07:43:11 +08:00
|
|
|
Ops.E->getLHS()->getType()->getAs<ObjCObjectPointerType>();
|
2009-07-11 07:34:53 +08:00
|
|
|
if (PT || OPT) {
|
2008-01-03 14:36:51 +08:00
|
|
|
Ptr = Ops.LHS;
|
|
|
|
Idx = Ops.RHS;
|
|
|
|
IdxExp = Ops.E->getRHS();
|
2009-07-11 07:34:53 +08:00
|
|
|
} else { // int + pointer
|
2009-07-30 05:53:49 +08:00
|
|
|
PT = Ops.E->getRHS()->getType()->getAs<PointerType>();
|
2009-09-22 07:43:11 +08:00
|
|
|
OPT = Ops.E->getRHS()->getType()->getAs<ObjCObjectPointerType>();
|
2009-07-11 07:34:53 +08:00
|
|
|
assert((PT || OPT) && "Invalid add expr");
|
2008-01-03 14:36:51 +08:00
|
|
|
Ptr = Ops.RHS;
|
|
|
|
Idx = Ops.LHS;
|
|
|
|
IdxExp = Ops.E->getLHS();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
|
2009-04-24 10:40:57 +08:00
|
|
|
if (Width < CGF.LLVMPointerWidth) {
|
2008-01-03 14:36:51 +08:00
|
|
|
// Zero or sign extend the pointer value based on whether the index is
|
|
|
|
// signed or not.
|
2009-08-14 05:57:51 +08:00
|
|
|
const llvm::Type *IdxType =
|
|
|
|
llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
|
2008-07-27 06:37:01 +08:00
|
|
|
if (IdxExp->getType()->isSignedIntegerType())
|
2008-01-03 14:36:51 +08:00
|
|
|
Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
|
|
|
|
else
|
|
|
|
Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
|
|
|
|
}
|
2009-07-11 07:34:53 +08:00
|
|
|
const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType();
|
2009-09-09 21:00:44 +08:00
|
|
|
// Handle interface types, which are not represented with a concrete type.
|
2009-04-25 13:08:32 +08:00
|
|
|
if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) {
|
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
|
|
|
CGF.getContext().getTypeSizeInChars(OIT).getQuantity());
|
2009-04-25 13:08:32 +08:00
|
|
|
Idx = Builder.CreateMul(Idx, InterfaceSize);
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
|
2009-04-25 13:08:32 +08:00
|
|
|
Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
|
|
|
|
Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
|
|
|
|
return Builder.CreateBitCast(Res, Ptr->getType());
|
2009-09-09 21:00:44 +08:00
|
|
|
}
|
2009-04-25 13:08:32 +08:00
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
// Explicitly handle GNU void* and function pointer arithmetic extensions. The
|
|
|
|
// GNU void* casts amount to no-ops since our void* type is i8*, but this is
|
|
|
|
// future proof.
|
2009-01-24 02:51:09 +08:00
|
|
|
if (ElementType->isVoidType() || ElementType->isFunctionType()) {
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
|
2009-01-24 02:51:09 +08:00
|
|
|
Value *Casted = Builder.CreateBitCast(Ptr, i8Ty);
|
2009-04-25 13:08:32 +08:00
|
|
|
Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr");
|
2009-01-24 02:51:09 +08:00
|
|
|
return Builder.CreateBitCast(Res, Ptr->getType());
|
2009-09-09 21:00:44 +08:00
|
|
|
}
|
|
|
|
|
2009-08-12 08:33:55 +08:00
|
|
|
return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr");
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
|
2009-04-02 04:28:16 +08:00
|
|
|
if (!isa<llvm::PointerType>(Ops.LHS->getType())) {
|
2009-04-03 02:15:54 +08:00
|
|
|
if (CGF.getContext().getLangOptions().OverflowChecking
|
|
|
|
&& Ops.Ty->isSignedIntegerType())
|
2009-04-02 04:28:16 +08:00
|
|
|
return EmitOverflowCheckedBinOp(Ops);
|
2009-06-17 14:36:24 +08:00
|
|
|
|
2010-02-16 00:14:01 +08:00
|
|
|
if (Ops.LHS->getType()->isFPOrFPVectorTy())
|
2009-06-17 14:36:24 +08:00
|
|
|
return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub");
|
2010-03-30 01:28:16 +08:00
|
|
|
|
|
|
|
// Signed integer overflow is undefined behavior.
|
|
|
|
if (Ops.Ty->isSignedIntegerType())
|
|
|
|
return Builder.CreateNSWSub(Ops.LHS, Ops.RHS, "sub");
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
|
2009-04-02 04:28:16 +08:00
|
|
|
}
|
2007-08-25 05:00:35 +08:00
|
|
|
|
2009-07-11 07:34:53 +08:00
|
|
|
if (Ops.E->getLHS()->getType()->isPointerType() &&
|
2009-07-30 05:53:49 +08:00
|
|
|
Ops.E->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) {
|
2009-03-28 10:45:41 +08:00
|
|
|
// The amount of the addition needs to account for the VLA size for
|
|
|
|
// ptr-int
|
|
|
|
// The amount of the division needs to account for the VLA size for
|
|
|
|
// ptr-ptr.
|
|
|
|
CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction");
|
|
|
|
}
|
|
|
|
|
2009-01-24 02:51:09 +08:00
|
|
|
const QualType LHSType = Ops.E->getLHS()->getType();
|
2009-07-11 07:34:53 +08:00
|
|
|
const QualType LHSElementType = LHSType->getPointeeType();
|
2008-08-06 10:00:38 +08:00
|
|
|
if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
|
|
|
|
// pointer - int
|
|
|
|
Value *Idx = Ops.RHS;
|
|
|
|
unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
|
2009-04-24 10:40:57 +08:00
|
|
|
if (Width < CGF.LLVMPointerWidth) {
|
2008-08-06 10:00:38 +08:00
|
|
|
// Zero or sign extend the pointer value based on whether the index is
|
|
|
|
// signed or not.
|
2009-08-14 05:57:51 +08:00
|
|
|
const llvm::Type *IdxType =
|
|
|
|
llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth);
|
2008-08-06 10:00:38 +08:00
|
|
|
if (Ops.E->getRHS()->getType()->isSignedIntegerType())
|
|
|
|
Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
|
|
|
|
else
|
|
|
|
Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
|
|
|
|
}
|
|
|
|
Idx = Builder.CreateNeg(Idx, "sub.ptr.neg");
|
2009-01-24 02:51:09 +08:00
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
// Handle interface types, which are not represented with a concrete type.
|
|
|
|
if (const ObjCInterfaceType *OIT =
|
2009-04-25 13:08:32 +08:00
|
|
|
dyn_cast<ObjCInterfaceType>(LHSElementType)) {
|
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
|
|
|
CGF.getContext().
|
|
|
|
getTypeSizeInChars(OIT).getQuantity());
|
2009-04-25 13:08:32 +08:00
|
|
|
Idx = Builder.CreateMul(Idx, InterfaceSize);
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
|
2009-04-25 13:08:32 +08:00
|
|
|
Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
|
|
|
|
Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr");
|
|
|
|
return Builder.CreateBitCast(Res, Ops.LHS->getType());
|
2009-09-09 21:00:44 +08:00
|
|
|
}
|
2009-04-25 13:08:32 +08:00
|
|
|
|
2009-01-24 02:51:09 +08:00
|
|
|
// Explicitly handle GNU void* and function pointer arithmetic
|
2009-09-09 21:00:44 +08:00
|
|
|
// extensions. The GNU void* casts amount to no-ops since our void* type is
|
|
|
|
// i8*, but this is future proof.
|
2009-01-24 02:51:09 +08:00
|
|
|
if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
|
2009-10-13 18:07:13 +08:00
|
|
|
const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
|
2009-01-24 02:51:09 +08:00
|
|
|
Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty);
|
|
|
|
Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr");
|
|
|
|
return Builder.CreateBitCast(Res, Ops.LHS->getType());
|
2009-09-09 21:00:44 +08:00
|
|
|
}
|
|
|
|
|
2009-08-12 08:33:55 +08:00
|
|
|
return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr");
|
2008-08-05 08:47:03 +08:00
|
|
|
} else {
|
2008-08-06 10:00:38 +08:00
|
|
|
// pointer - pointer
|
|
|
|
Value *LHS = Ops.LHS;
|
|
|
|
Value *RHS = Ops.RHS;
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-01-12 01:06:35 +08:00
|
|
|
CharUnits ElementSize;
|
2008-08-05 08:47:03 +08:00
|
|
|
|
2009-02-11 15:21:43 +08:00
|
|
|
// Handle GCC extension for pointer arithmetic on void* and function pointer
|
|
|
|
// types.
|
|
|
|
if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) {
|
2010-01-12 01:06:35 +08:00
|
|
|
ElementSize = CharUnits::One();
|
2008-08-06 10:00:38 +08:00
|
|
|
} else {
|
2010-01-12 01:06:35 +08:00
|
|
|
ElementSize = CGF.getContext().getTypeSizeInChars(LHSElementType);
|
2008-08-06 10:00:38 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-08-06 10:00:38 +08:00
|
|
|
const llvm::Type *ResultType = ConvertType(Ops.Ty);
|
|
|
|
LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
|
|
|
|
RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
|
|
|
|
Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-02-11 15:21:43 +08:00
|
|
|
// Optimize out the shift for element size of 1.
|
2010-01-12 01:06:35 +08:00
|
|
|
if (ElementSize.isOne())
|
2009-02-11 15:21:43 +08:00
|
|
|
return BytesBetween;
|
2009-08-12 06:40:09 +08:00
|
|
|
|
|
|
|
// Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since
|
2009-09-09 21:00:44 +08:00
|
|
|
// pointer difference in C is only defined in the case where both operands
|
|
|
|
// are pointing to elements of an array.
|
2010-01-12 01:06:35 +08:00
|
|
|
Value *BytesPerElt =
|
|
|
|
llvm::ConstantInt::get(ResultType, ElementSize.getQuantity());
|
2009-08-12 06:40:09 +08:00
|
|
|
return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div");
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
|
|
|
|
// LLVM requires the LHS and RHS to be the same type: promote or truncate the
|
|
|
|
// RHS to the same size as the LHS.
|
|
|
|
Value *RHS = Ops.RHS;
|
|
|
|
if (Ops.LHS->getType() != RHS->getType())
|
|
|
|
RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-12-15 05:58:14 +08:00
|
|
|
if (CGF.CatchUndefined
|
|
|
|
&& isa<llvm::IntegerType>(Ops.LHS->getType())) {
|
|
|
|
unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth();
|
|
|
|
llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
|
|
|
|
CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS,
|
|
|
|
llvm::ConstantInt::get(RHS->getType(), Width)),
|
2009-12-15 08:35:12 +08:00
|
|
|
Cont, CGF.getTrapBB());
|
2009-12-15 05:58:14 +08:00
|
|
|
CGF.EmitBlock(Cont);
|
|
|
|
}
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateShl(Ops.LHS, RHS, "shl");
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
|
|
|
|
// LLVM requires the LHS and RHS to be the same type: promote or truncate the
|
|
|
|
// RHS to the same size as the LHS.
|
|
|
|
Value *RHS = Ops.RHS;
|
|
|
|
if (Ops.LHS->getType() != RHS->getType())
|
|
|
|
RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-12-15 05:58:14 +08:00
|
|
|
if (CGF.CatchUndefined
|
|
|
|
&& isa<llvm::IntegerType>(Ops.LHS->getType())) {
|
|
|
|
unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth();
|
|
|
|
llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
|
|
|
|
CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS,
|
|
|
|
llvm::ConstantInt::get(RHS->getType(), Width)),
|
2009-12-15 08:35:12 +08:00
|
|
|
Cont, CGF.getTrapBB());
|
2009-12-15 05:58:14 +08:00
|
|
|
CGF.EmitBlock(Cont);
|
|
|
|
}
|
|
|
|
|
2007-08-25 05:00:35 +08:00
|
|
|
if (Ops.Ty->isUnsignedIntegerType())
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateLShr(Ops.LHS, RHS, "shr");
|
|
|
|
return Builder.CreateAShr(Ops.LHS, RHS, "shr");
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
|
|
|
|
unsigned SICmpOpc, unsigned FCmpOpc) {
|
2009-05-29 23:46:01 +08:00
|
|
|
TestAndClearIgnoreResultAssign();
|
2007-08-27 00:34:22 +08:00
|
|
|
Value *Result;
|
2007-08-24 13:35:26 +08:00
|
|
|
QualType LHSTy = E->getLHS()->getType();
|
2009-12-11 15:36:43 +08:00
|
|
|
if (LHSTy->isMemberFunctionPointerType()) {
|
|
|
|
Value *LHSPtr = CGF.EmitAnyExprToTemp(E->getLHS()).getAggregateAddr();
|
|
|
|
Value *RHSPtr = CGF.EmitAnyExprToTemp(E->getRHS()).getAggregateAddr();
|
|
|
|
llvm::Value *LHSFunc = Builder.CreateStructGEP(LHSPtr, 0);
|
|
|
|
LHSFunc = Builder.CreateLoad(LHSFunc);
|
|
|
|
llvm::Value *RHSFunc = Builder.CreateStructGEP(RHSPtr, 0);
|
|
|
|
RHSFunc = Builder.CreateLoad(RHSFunc);
|
|
|
|
Value *ResultF = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
|
|
|
|
LHSFunc, RHSFunc, "cmp.func");
|
|
|
|
Value *NullPtr = llvm::Constant::getNullValue(LHSFunc->getType());
|
|
|
|
Value *ResultNull = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
|
|
|
|
LHSFunc, NullPtr, "cmp.null");
|
|
|
|
llvm::Value *LHSAdj = Builder.CreateStructGEP(LHSPtr, 1);
|
|
|
|
LHSAdj = Builder.CreateLoad(LHSAdj);
|
|
|
|
llvm::Value *RHSAdj = Builder.CreateStructGEP(RHSPtr, 1);
|
|
|
|
RHSAdj = Builder.CreateLoad(RHSAdj);
|
|
|
|
Value *ResultA = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
|
|
|
|
LHSAdj, RHSAdj, "cmp.adj");
|
|
|
|
if (E->getOpcode() == BinaryOperator::EQ) {
|
|
|
|
Result = Builder.CreateOr(ResultNull, ResultA, "or.na");
|
|
|
|
Result = Builder.CreateAnd(Result, ResultF, "and.f");
|
|
|
|
} else {
|
|
|
|
assert(E->getOpcode() == BinaryOperator::NE &&
|
|
|
|
"Member pointer comparison other than == or != ?");
|
|
|
|
Result = Builder.CreateAnd(ResultNull, ResultA, "and.na");
|
|
|
|
Result = Builder.CreateOr(Result, ResultF, "or.f");
|
|
|
|
}
|
|
|
|
} else if (!LHSTy->isAnyComplexType()) {
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *LHS = Visit(E->getLHS());
|
|
|
|
Value *RHS = Visit(E->getRHS());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-02-16 00:14:01 +08:00
|
|
|
if (LHS->getType()->isFPOrFPVectorTy()) {
|
2008-07-26 04:16:05 +08:00
|
|
|
Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
|
2007-08-24 13:35:26 +08:00
|
|
|
LHS, RHS, "cmp");
|
2008-05-29 23:09:15 +08:00
|
|
|
} else if (LHSTy->isSignedIntegerType()) {
|
|
|
|
Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
|
2007-08-24 13:35:26 +08:00
|
|
|
LHS, RHS, "cmp");
|
|
|
|
} else {
|
2008-05-29 23:09:15 +08:00
|
|
|
// Unsigned integers and pointers.
|
|
|
|
Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
|
2007-08-24 13:35:26 +08:00
|
|
|
LHS, RHS, "cmp");
|
|
|
|
}
|
2009-07-08 09:08:03 +08:00
|
|
|
|
|
|
|
// If this is a vector comparison, sign extend the result to the appropriate
|
|
|
|
// vector integer type and return it (don't convert to bool).
|
|
|
|
if (LHSTy->isVectorType())
|
|
|
|
return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
} else {
|
|
|
|
// Complex Comparison: can only be an equality comparison.
|
|
|
|
CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS());
|
|
|
|
CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-09-22 07:43:11 +08:00
|
|
|
QualType CETy = LHSTy->getAs<ComplexType>()->getElementType();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-27 00:34:22 +08:00
|
|
|
Value *ResultR, *ResultI;
|
2007-08-24 13:35:26 +08:00
|
|
|
if (CETy->isRealFloatingType()) {
|
|
|
|
ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
|
|
|
|
LHS.first, RHS.first, "cmp.r");
|
|
|
|
ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
|
|
|
|
LHS.second, RHS.second, "cmp.i");
|
|
|
|
} else {
|
|
|
|
// Complex comparisons can only be equality comparisons. As such, signed
|
|
|
|
// and unsigned opcodes are the same.
|
|
|
|
ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
|
|
|
|
LHS.first, RHS.first, "cmp.r");
|
|
|
|
ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
|
|
|
|
LHS.second, RHS.second, "cmp.i");
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
if (E->getOpcode() == BinaryOperator::EQ) {
|
|
|
|
Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
|
|
|
|
} else {
|
|
|
|
assert(E->getOpcode() == BinaryOperator::NE &&
|
|
|
|
"Complex comparison other than == or != ?");
|
|
|
|
Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
|
|
|
|
}
|
|
|
|
}
|
2009-01-12 07:22:37 +08:00
|
|
|
|
|
|
|
return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
|
2009-05-29 23:46:01 +08:00
|
|
|
bool Ignore = TestAndClearIgnoreResultAssign();
|
|
|
|
|
|
|
|
// __block variables need to have the rhs evaluated first, plus this should
|
|
|
|
// improve codegen just a little.
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *RHS = Visit(E->getRHS());
|
2009-12-16 10:57:00 +08:00
|
|
|
LValue LHS = EmitCheckedLValue(E->getLHS());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-11-19 17:36:46 +08:00
|
|
|
// Store the value into the LHS. Bit-fields are handled specially
|
2008-11-19 19:54:05 +08:00
|
|
|
// because the result is altered by the store, i.e., [C99 6.5.16p1]
|
|
|
|
// 'An assignment expression has the value of the left operand after
|
2009-03-28 10:45:41 +08:00
|
|
|
// the assignment...'.
|
2010-04-06 05:36:35 +08:00
|
|
|
if (LHS.isBitField()) {
|
2009-05-29 23:46:01 +08:00
|
|
|
if (!LHS.isVolatileQualified()) {
|
|
|
|
CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(),
|
|
|
|
&RHS);
|
|
|
|
return RHS;
|
|
|
|
} else
|
|
|
|
CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType());
|
|
|
|
} else
|
2008-11-19 17:36:46 +08:00
|
|
|
CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType());
|
2009-05-29 23:46:01 +08:00
|
|
|
if (Ignore)
|
|
|
|
return 0;
|
|
|
|
return EmitLoadOfLValue(LHS, E->getType());
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
|
2009-10-17 12:24:20 +08:00
|
|
|
const llvm::Type *ResTy = ConvertType(E->getType());
|
|
|
|
|
2008-11-12 16:26:50 +08:00
|
|
|
// If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
|
|
|
|
// If we have 1 && X, just emit X without inserting the control flow.
|
|
|
|
if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
|
|
|
|
if (Cond == 1) { // If we have 1 && X, just emit X.
|
2008-11-11 15:41:27 +08:00
|
|
|
Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
|
2009-10-17 12:24:20 +08:00
|
|
|
// ZExt result to int or bool.
|
|
|
|
return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext");
|
2008-11-11 15:41:27 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-10-17 12:24:20 +08:00
|
|
|
// 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
|
2008-11-12 16:26:50 +08:00
|
|
|
if (!CGF.ContainsLabel(E->getRHS()))
|
2009-10-17 12:24:20 +08:00
|
|
|
return llvm::Constant::getNullValue(ResTy);
|
2008-11-11 15:41:27 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
|
|
|
|
llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs");
|
2008-11-12 16:26:50 +08:00
|
|
|
|
2008-11-12 16:38:24 +08:00
|
|
|
// Branch on the LHS first. If it is false, go to the failure (cont) block.
|
|
|
|
CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock);
|
|
|
|
|
|
|
|
// Any edges into the ContBlock are now from an (indeterminate number of)
|
|
|
|
// edges from this first condition. All of these values will be false. Start
|
|
|
|
// setting up the PHI node in the Cont Block for this.
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext),
|
|
|
|
"", ContBlock);
|
2008-11-12 16:38:24 +08:00
|
|
|
PN->reserveOperandSpace(2); // Normal case, two inputs.
|
|
|
|
for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
|
|
|
|
PI != PE; ++PI)
|
2009-08-01 01:39:36 +08:00
|
|
|
PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.BeginConditionalBranch();
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(RHSBlock);
|
|
|
|
Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.EndConditionalBranch();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Reaquire the RHS block, as there may be subblocks inserted.
|
|
|
|
RHSBlock = Builder.GetInsertBlock();
|
2008-11-12 16:38:24 +08:00
|
|
|
|
|
|
|
// Emit an unconditional branch from this block to ContBlock. Insert an entry
|
|
|
|
// into the phi node for the edge with the value of RHSCond.
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(ContBlock);
|
|
|
|
PN->addIncoming(RHSCond, RHSBlock);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// ZExt result to int.
|
2009-10-17 12:24:20 +08:00
|
|
|
return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext");
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
|
2009-10-17 12:24:20 +08:00
|
|
|
const llvm::Type *ResTy = ConvertType(E->getType());
|
|
|
|
|
2008-11-12 16:26:50 +08:00
|
|
|
// If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
|
|
|
|
// If we have 0 || X, just emit X without inserting the control flow.
|
|
|
|
if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
|
|
|
|
if (Cond == -1) { // If we have 0 || X, just emit X.
|
2008-11-11 15:41:27 +08:00
|
|
|
Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
|
2009-10-17 12:24:20 +08:00
|
|
|
// ZExt result to int or bool.
|
|
|
|
return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext");
|
2008-11-11 15:41:27 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-10-17 12:24:20 +08:00
|
|
|
// 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
|
2008-11-12 16:26:50 +08:00
|
|
|
if (!CGF.ContainsLabel(E->getRHS()))
|
2009-10-17 12:24:20 +08:00
|
|
|
return llvm::ConstantInt::get(ResTy, 1);
|
2008-11-11 15:41:27 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
|
|
|
|
llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-11-12 16:38:24 +08:00
|
|
|
// Branch on the LHS first. If it is true, go to the success (cont) block.
|
|
|
|
CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock);
|
|
|
|
|
|
|
|
// Any edges into the ContBlock are now from an (indeterminate number of)
|
|
|
|
// edges from this first condition. All of these values will be true. Start
|
|
|
|
// setting up the PHI node in the Cont Block for this.
|
2009-08-14 05:57:51 +08:00
|
|
|
llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext),
|
|
|
|
"", ContBlock);
|
2008-11-12 16:38:24 +08:00
|
|
|
PN->reserveOperandSpace(2); // Normal case, two inputs.
|
|
|
|
for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock);
|
|
|
|
PI != PE; ++PI)
|
2009-08-01 01:39:36 +08:00
|
|
|
PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI);
|
2008-11-12 16:38:24 +08:00
|
|
|
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.BeginConditionalBranch();
|
2009-06-04 10:53:13 +08:00
|
|
|
|
2008-11-12 16:38:24 +08:00
|
|
|
// Emit the RHS condition as a bool value.
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(RHSBlock);
|
|
|
|
Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.EndConditionalBranch();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Reaquire the RHS block, as there may be subblocks inserted.
|
|
|
|
RHSBlock = Builder.GetInsertBlock();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-11-12 16:38:24 +08:00
|
|
|
// Emit an unconditional branch from this block to ContBlock. Insert an entry
|
|
|
|
// into the phi node for the edge with the value of RHSCond.
|
|
|
|
CGF.EmitBlock(ContBlock);
|
2007-08-24 13:35:26 +08:00
|
|
|
PN->addIncoming(RHSCond, RHSBlock);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// ZExt result to int.
|
2009-10-17 12:24:20 +08:00
|
|
|
return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext");
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {
|
|
|
|
CGF.EmitStmt(E->getLHS());
|
2008-11-12 07:11:34 +08:00
|
|
|
CGF.EnsureInsertPoint();
|
2007-08-24 13:35:26 +08:00
|
|
|
return Visit(E->getRHS());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Other Operators
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-11-12 16:55:54 +08:00
|
|
|
/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified
|
|
|
|
/// expression is cheap enough and side-effect-free enough to evaluate
|
|
|
|
/// unconditionally instead of conditionally. This is used to convert control
|
|
|
|
/// flow into selects in some cases.
|
2009-11-04 07:25:48 +08:00
|
|
|
static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E,
|
|
|
|
CodeGenFunction &CGF) {
|
2008-11-12 16:55:54 +08:00
|
|
|
if (const ParenExpr *PE = dyn_cast<ParenExpr>(E))
|
2009-11-04 07:25:48 +08:00
|
|
|
return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr(), CGF);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-11-12 16:55:54 +08:00
|
|
|
// TODO: Allow anything we can constant fold to an integer or fp constant.
|
|
|
|
if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) ||
|
|
|
|
isa<FloatingLiteral>(E))
|
|
|
|
return true;
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-11-12 16:55:54 +08:00
|
|
|
// Non-volatile automatic variables too, to get "cond ? X : Y" where
|
|
|
|
// X and Y are local variables.
|
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
|
2009-11-04 07:25:48 +08:00
|
|
|
if (VD->hasLocalStorage() && !(CGF.getContext()
|
|
|
|
.getCanonicalType(VD->getType())
|
|
|
|
.isVolatileQualified()))
|
2008-11-12 16:55:54 +08:00
|
|
|
return true;
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-11-12 16:55:54 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *ScalarExprEmitter::
|
|
|
|
VisitConditionalOperator(const ConditionalOperator *E) {
|
2009-05-29 23:46:01 +08:00
|
|
|
TestAndClearIgnoreResultAssign();
|
2008-11-12 16:04:58 +08:00
|
|
|
// If the condition constant folds and can be elided, try to avoid emitting
|
|
|
|
// the condition and the dead arm.
|
|
|
|
if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){
|
2008-11-12 02:56:45 +08:00
|
|
|
Expr *Live = E->getLHS(), *Dead = E->getRHS();
|
2008-11-12 16:04:58 +08:00
|
|
|
if (Cond == -1)
|
2008-11-12 02:56:45 +08:00
|
|
|
std::swap(Live, Dead);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-11-12 16:04:58 +08:00
|
|
|
// If the dead side doesn't have labels we need, and if the Live side isn't
|
|
|
|
// the gnu missing ?: extension (which we could handle, but don't bother
|
|
|
|
// to), just emit the Live part.
|
|
|
|
if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part
|
|
|
|
Live) // Live part isn't missing.
|
|
|
|
return Visit(Live);
|
2008-11-12 02:56:45 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
|
|
|
|
2008-11-12 16:55:54 +08:00
|
|
|
// If this is a really simple expression (like x ? 4 : 5), emit this as a
|
|
|
|
// select instead of as control flow. We can only do this if it is cheap and
|
2008-11-16 14:16:27 +08:00
|
|
|
// safe to evaluate the LHS and RHS unconditionally.
|
2009-11-04 07:25:48 +08:00
|
|
|
if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS(),
|
|
|
|
CGF) &&
|
|
|
|
isCheapEnoughToEvaluateUnconditionally(E->getRHS(), CGF)) {
|
2008-11-12 16:55:54 +08:00
|
|
|
llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond());
|
|
|
|
llvm::Value *LHS = Visit(E->getLHS());
|
|
|
|
llvm::Value *RHS = Visit(E->getRHS());
|
|
|
|
return Builder.CreateSelect(CondV, LHS, RHS, "cond");
|
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
|
|
|
|
2008-11-12 18:13:37 +08:00
|
|
|
llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
|
|
|
|
llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
|
2008-11-13 09:38:36 +08:00
|
|
|
llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
|
2008-11-12 16:08:13 +08:00
|
|
|
Value *CondVal = 0;
|
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
// If we don't have the GNU missing condition extension, emit a branch on bool
|
|
|
|
// the normal way.
|
2009-02-14 07:35:32 +08:00
|
|
|
if (E->getLHS()) {
|
|
|
|
// Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for
|
|
|
|
// the branch on bool.
|
|
|
|
CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
|
|
|
|
} else {
|
|
|
|
// Otherwise, for the ?: extension, evaluate the conditional and then
|
|
|
|
// convert it to bool the hard way. We do this explicitly because we need
|
|
|
|
// the unconverted value for the missing middle value of the ?:.
|
2008-11-12 16:08:13 +08:00
|
|
|
CondVal = CGF.EmitScalarExpr(E->getCond());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-02-14 07:35:32 +08:00
|
|
|
// In some cases, EmitScalarConversion will delete the "CondVal" expression
|
|
|
|
// if there are no extra uses (an optimization). Inhibit this by making an
|
|
|
|
// extra dead use, because we're going to add a use of CondVal later. We
|
|
|
|
// don't use the builder for this, because we don't want it to get optimized
|
|
|
|
// away. This leaves dead code, but the ?: extension isn't common.
|
|
|
|
new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder",
|
|
|
|
Builder.GetInsertBlock());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-11-12 16:08:13 +08:00
|
|
|
Value *CondBoolVal =
|
|
|
|
CGF.EmitScalarConversion(CondVal, E->getCond()->getType(),
|
|
|
|
CGF.getContext().BoolTy);
|
|
|
|
Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock);
|
|
|
|
}
|
2009-06-04 11:00:32 +08:00
|
|
|
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.BeginConditionalBranch();
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(LHSBlock);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Handle the GNU extension for missing LHS.
|
2007-11-26 09:40:58 +08:00
|
|
|
Value *LHS;
|
|
|
|
if (E->getLHS())
|
2008-05-17 04:38:39 +08:00
|
|
|
LHS = Visit(E->getLHS());
|
2007-11-26 09:40:58 +08:00
|
|
|
else // Perform promotions, to handle cases like "short ?: int"
|
|
|
|
LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.EndConditionalBranch();
|
2007-08-24 13:35:26 +08:00
|
|
|
LHSBlock = Builder.GetInsertBlock();
|
2008-11-11 17:41:28 +08:00
|
|
|
CGF.EmitBranch(ContBlock);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.BeginConditionalBranch();
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(RHSBlock);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2008-05-17 04:38:39 +08:00
|
|
|
Value *RHS = Visit(E->getRHS());
|
2010-02-05 01:18:07 +08:00
|
|
|
CGF.EndConditionalBranch();
|
2007-08-24 13:35:26 +08:00
|
|
|
RHSBlock = Builder.GetInsertBlock();
|
2008-11-11 17:41:28 +08:00
|
|
|
CGF.EmitBranch(ContBlock);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(ContBlock);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-12-08 04:25:53 +08:00
|
|
|
// If the LHS or RHS is a throw expression, it will be legitimately null.
|
|
|
|
if (!LHS)
|
|
|
|
return RHS;
|
|
|
|
if (!RHS)
|
|
|
|
return LHS;
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Create a PHI node for the real part.
|
|
|
|
llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond");
|
|
|
|
PN->reserveOperandSpace(2);
|
|
|
|
PN->addIncoming(LHS, LHSBlock);
|
|
|
|
PN->addIncoming(RHS, RHSBlock);
|
|
|
|
return PN;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
|
2009-03-04 13:52:32 +08:00
|
|
|
return Visit(E->getChosenSubExpr(CGF.getContext()));
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
2007-12-01 01:56:23 +08:00
|
|
|
Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
|
2009-01-21 01:46:04 +08:00
|
|
|
llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
|
2008-11-04 13:30:00 +08:00
|
|
|
llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
|
|
|
|
|
|
|
|
// If EmitVAArg fails, we fall back to the LLVM instruction.
|
2009-09-09 21:00:44 +08:00
|
|
|
if (!ArgPtr)
|
2008-11-04 13:30:00 +08:00
|
|
|
return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType()));
|
|
|
|
|
2009-05-29 23:46:01 +08:00
|
|
|
// FIXME Volatility.
|
2008-11-04 13:30:00 +08:00
|
|
|
return Builder.CreateLoad(ArgPtr);
|
2007-10-16 04:28:48 +08:00
|
|
|
}
|
|
|
|
|
2009-02-13 02:29:15 +08:00
|
|
|
Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) {
|
2009-03-07 10:35:30 +08:00
|
|
|
return CGF.BuildBlockLiteralTmp(BE);
|
2009-02-13 02:29:15 +08:00
|
|
|
}
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Entry Point into this File
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
/// EmitScalarExpr - Emit the computation of the specified expression of scalar
|
|
|
|
/// type, ignoring the result.
|
2009-05-29 23:46:01 +08:00
|
|
|
Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) {
|
2007-08-24 13:35:26 +08:00
|
|
|
assert(E && !hasAggregateLLVMType(E->getType()) &&
|
|
|
|
"Invalid scalar expression to emit");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2009-05-29 23:46:01 +08:00
|
|
|
return ScalarExprEmitter(*this, IgnoreResultAssign)
|
|
|
|
.Visit(const_cast<Expr*>(E));
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2007-08-26 14:48:56 +08:00
|
|
|
|
|
|
|
/// EmitScalarConversion - Emit a conversion from the specified type to the
|
|
|
|
/// specified destination type, both of which are LLVM scalar types.
|
2007-08-27 00:34:22 +08:00
|
|
|
Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy,
|
|
|
|
QualType DstTy) {
|
2007-08-26 14:48:56 +08:00
|
|
|
assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) &&
|
|
|
|
"Invalid scalar expression to emit");
|
|
|
|
return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy);
|
|
|
|
}
|
2007-08-27 00:34:22 +08:00
|
|
|
|
2009-09-09 21:00:44 +08:00
|
|
|
/// EmitComplexToScalarConversion - Emit a conversion from the specified complex
|
|
|
|
/// type to the specified destination type, where the destination type is an
|
|
|
|
/// LLVM scalar type.
|
2007-08-27 00:34:22 +08:00
|
|
|
Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src,
|
|
|
|
QualType SrcTy,
|
|
|
|
QualType DstTy) {
|
2008-04-05 00:54:41 +08:00
|
|
|
assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) &&
|
2007-08-27 00:34:22 +08:00
|
|
|
"Invalid complex -> scalar conversion");
|
|
|
|
return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy,
|
|
|
|
DstTy);
|
|
|
|
}
|
2007-12-11 03:35:18 +08:00
|
|
|
|
2009-12-10 07:35:29 +08:00
|
|
|
LValue CodeGenFunction::EmitObjCIsaExpr(const ObjCIsaExpr *E) {
|
|
|
|
llvm::Value *V;
|
|
|
|
// object->isa or (*object).isa
|
|
|
|
// Generate code as for: *(Class*)object
|
2010-02-06 03:18:30 +08:00
|
|
|
// build Class* type
|
|
|
|
const llvm::Type *ClassPtrTy = ConvertType(E->getType());
|
|
|
|
|
2009-12-10 07:35:29 +08:00
|
|
|
Expr *BaseExpr = E->getBase();
|
2010-02-06 03:18:30 +08:00
|
|
|
if (BaseExpr->isLvalue(getContext()) != Expr::LV_Valid) {
|
|
|
|
V = CreateTempAlloca(ClassPtrTy, "resval");
|
|
|
|
llvm::Value *Src = EmitScalarExpr(BaseExpr);
|
|
|
|
Builder.CreateStore(Src, V);
|
2010-03-04 06:09:47 +08:00
|
|
|
LValue LV = LValue::MakeAddr(V, MakeQualifiers(E->getType()));
|
|
|
|
V = ScalarExprEmitter(*this).EmitLoadOfLValue(LV, E->getType());
|
2010-02-06 03:18:30 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (E->isArrow())
|
|
|
|
V = ScalarExprEmitter(*this).EmitLoadOfLValue(BaseExpr);
|
|
|
|
else
|
|
|
|
V = EmitLValue(BaseExpr).getAddress();
|
|
|
|
}
|
2009-12-10 07:35:29 +08:00
|
|
|
|
|
|
|
// build Class* type
|
|
|
|
ClassPtrTy = ClassPtrTy->getPointerTo();
|
|
|
|
V = Builder.CreateBitCast(V, ClassPtrTy);
|
|
|
|
LValue LV = LValue::MakeAddr(V, MakeQualifiers(E->getType()));
|
|
|
|
return LV;
|
|
|
|
}
|
|
|
|
|
2010-04-23 12:16:32 +08:00
|
|
|
|
|
|
|
LValue CodeGenFunction::EmitCompoundAssignOperatorLValue(
|
|
|
|
const CompoundAssignOperator *E) {
|
|
|
|
ScalarExprEmitter Scalar(*this);
|
|
|
|
Value *BitFieldResult = 0;
|
|
|
|
switch (E->getOpcode()) {
|
|
|
|
#define COMPOUND_OP(Op) \
|
|
|
|
case BinaryOperator::Op##Assign: \
|
|
|
|
return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \
|
|
|
|
BitFieldResult)
|
|
|
|
COMPOUND_OP(Mul);
|
|
|
|
COMPOUND_OP(Div);
|
|
|
|
COMPOUND_OP(Rem);
|
|
|
|
COMPOUND_OP(Add);
|
|
|
|
COMPOUND_OP(Sub);
|
|
|
|
COMPOUND_OP(Shl);
|
|
|
|
COMPOUND_OP(Shr);
|
|
|
|
COMPOUND_OP(And);
|
|
|
|
COMPOUND_OP(Xor);
|
|
|
|
COMPOUND_OP(Or);
|
|
|
|
#undef COMPOUND_OP
|
|
|
|
|
|
|
|
case BinaryOperator::PtrMemD:
|
|
|
|
case BinaryOperator::PtrMemI:
|
|
|
|
case BinaryOperator::Mul:
|
|
|
|
case BinaryOperator::Div:
|
|
|
|
case BinaryOperator::Rem:
|
|
|
|
case BinaryOperator::Add:
|
|
|
|
case BinaryOperator::Sub:
|
|
|
|
case BinaryOperator::Shl:
|
|
|
|
case BinaryOperator::Shr:
|
|
|
|
case BinaryOperator::LT:
|
|
|
|
case BinaryOperator::GT:
|
|
|
|
case BinaryOperator::LE:
|
|
|
|
case BinaryOperator::GE:
|
|
|
|
case BinaryOperator::EQ:
|
|
|
|
case BinaryOperator::NE:
|
|
|
|
case BinaryOperator::And:
|
|
|
|
case BinaryOperator::Xor:
|
|
|
|
case BinaryOperator::Or:
|
|
|
|
case BinaryOperator::LAnd:
|
|
|
|
case BinaryOperator::LOr:
|
|
|
|
case BinaryOperator::Assign:
|
|
|
|
case BinaryOperator::Comma:
|
|
|
|
assert(false && "Not valid compound assignment operators");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unhandled compound assignment operator");
|
|
|
|
}
|