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"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "CGDebugInfo.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"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2008-11-12 16:38:24 +08:00
|
|
|
#include "llvm/Support/CFG.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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-10-23 00:48:22 +08:00
|
|
|
namespace {
|
2007-08-24 13:35:26 +08:00
|
|
|
struct BinOpInfo {
|
|
|
|
Value *LHS;
|
|
|
|
Value *RHS;
|
2007-08-25 05:00:35 +08:00
|
|
|
QualType Ty; // Computation Type.
|
2010-06-27 05:48:21 +08:00
|
|
|
BinaryOperator::Opcode Opcode; // Opcode of BinOp to perform
|
2012-10-02 12:45:10 +08:00
|
|
|
bool FPContractable;
|
2010-06-27 05:48:21 +08:00
|
|
|
const Expr *E; // Entire expr, for error unsupported. May not be binop.
|
2007-08-24 13:35:26 +08:00
|
|
|
};
|
|
|
|
|
2010-11-13 09:35:44 +08:00
|
|
|
static bool MustVisitNullValue(const Expr *E) {
|
|
|
|
// If a null pointer expression's type is the C++0x nullptr_t, then
|
|
|
|
// it's not necessarily a simple constant and it must be evaluated
|
|
|
|
// for its potential side effects.
|
|
|
|
return E->getType()->isNullPtrType();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); }
|
2007-08-24 13:35:26 +08:00
|
|
|
LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); }
|
2012-09-08 10:08:36 +08:00
|
|
|
LValue EmitCheckedLValue(const Expr *E, CodeGenFunction::TypeCheckKind TCK) {
|
|
|
|
return CGF.EmitCheckedLValue(E, TCK);
|
2012-08-24 08:54:33 +08:00
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
void EmitBinOpCheck(Value *Check, const BinOpInfo &Info);
|
|
|
|
|
2013-10-02 10:29:49 +08:00
|
|
|
Value *EmitLoadOfLValue(LValue LV, SourceLocation Loc) {
|
|
|
|
return CGF.EmitLoadOfLValue(LV, Loc).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) {
|
2013-10-02 10:29:49 +08:00
|
|
|
return EmitLoadOfLValue(EmitCheckedLValue(E, CodeGenFunction::TCK_Load),
|
|
|
|
E->getExprLoc());
|
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
|
|
|
|
2012-10-13 06:57:06 +08:00
|
|
|
/// \brief Emit a check that a conversion to or from a floating-point type
|
|
|
|
/// does not overflow.
|
|
|
|
void EmitFloatConversionCheck(Value *OrigSrc, QualType OrigSrcType,
|
|
|
|
Value *Src, QualType SrcType,
|
|
|
|
QualType DstType, llvm::Type *DstTy);
|
|
|
|
|
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
|
|
|
|
2010-05-23 01:45:10 +08:00
|
|
|
/// EmitNullValue - Emit a value that corresponds to null for the given type.
|
|
|
|
Value *EmitNullValue(QualType Ty);
|
|
|
|
|
2010-11-15 17:13:47 +08:00
|
|
|
/// EmitFloatToBoolConversion - Perform an FP to boolean conversion.
|
|
|
|
Value *EmitFloatToBoolConversion(Value *V) {
|
|
|
|
// Compare against 0.0 for fp scalars.
|
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(V->getType());
|
|
|
|
return Builder.CreateFCmpUNE(V, Zero, "tobool");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitPointerToBoolConversion - Perform a pointer to boolean conversion.
|
|
|
|
Value *EmitPointerToBoolConversion(Value *V) {
|
|
|
|
Value *Zero = llvm::ConstantPointerNull::get(
|
|
|
|
cast<llvm::PointerType>(V->getType()));
|
|
|
|
return Builder.CreateICmpNE(V, Zero, "tobool");
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *EmitIntToBoolConversion(Value *V) {
|
|
|
|
// 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>(V)) {
|
|
|
|
if (ZI->getOperand(0)->getType() == Builder.getInt1Ty()) {
|
|
|
|
Value *Result = ZI->getOperand(0);
|
|
|
|
// 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();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 06:55:03 +08:00
|
|
|
return Builder.CreateIsNotNull(V, "tobool");
|
2010-11-15 17:13:47 +08:00
|
|
|
}
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2010-09-17 23:51:28 +08:00
|
|
|
Value *Visit(Expr *E) {
|
|
|
|
return StmtVisitor<ScalarExprEmitter, Value*>::Visit(E);
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *VisitStmt(Stmt *S) {
|
2007-12-12 05:27:55 +08:00
|
|
|
S->dump(CGF.getContext().getSourceManager());
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Stmt can't have complex result type!");
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
Value *VisitExpr(Expr *S);
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-09-17 23:51:28 +08:00
|
|
|
Value *VisitParenExpr(ParenExpr *PE) {
|
2013-07-26 14:16:11 +08:00
|
|
|
return Visit(PE->getSubExpr());
|
2010-09-17 23:51:28 +08:00
|
|
|
}
|
2011-07-15 13:09:51 +08:00
|
|
|
Value *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) {
|
2013-07-26 14:16:11 +08:00
|
|
|
return Visit(E->getReplacement());
|
2011-07-15 13:09:51 +08:00
|
|
|
}
|
2011-04-15 08:35:48 +08:00
|
|
|
Value *VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
|
|
|
|
return Visit(GE->getResultExpr());
|
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
|
|
|
|
// Leaves.
|
|
|
|
Value *VisitIntegerLiteral(const IntegerLiteral *E) {
|
2011-04-20 06:55:03 +08:00
|
|
|
return Builder.getInt(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
|
|
|
}
|
2012-03-07 04:05:56 +08:00
|
|
|
Value *VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
|
|
|
|
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
|
|
|
}
|
2010-07-08 14:14:04 +08:00
|
|
|
Value *VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
|
2010-05-23 01:45:10 +08:00
|
|
|
return EmitNullValue(E->getType());
|
2008-08-24 03:35:47 +08:00
|
|
|
}
|
2008-12-22 06:39:40 +08:00
|
|
|
Value *VisitGNUNullExpr(const GNUNullExpr *E) {
|
2010-05-23 01:45:10 +08:00
|
|
|
return EmitNullValue(E->getType());
|
2008-12-22 06:39:40 +08:00
|
|
|
}
|
2010-08-05 17:58:49 +08:00
|
|
|
Value *VisitOffsetOfExpr(OffsetOfExpr *E);
|
2011-03-12 03:24:49 +08:00
|
|
|
Value *VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *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
|
|
|
|
2011-01-13 06:11:34 +08:00
|
|
|
Value *VisitSizeOfPackExpr(SizeOfPackExpr *E) {
|
2011-04-20 06:55:03 +08:00
|
|
|
return llvm::ConstantInt::get(ConvertType(E->getType()),E->getPackLength());
|
2011-01-13 06:11:34 +08:00
|
|
|
}
|
2011-02-16 16:02:54 +08:00
|
|
|
|
2011-11-06 17:01:30 +08:00
|
|
|
Value *VisitPseudoObjectExpr(PseudoObjectExpr *E) {
|
|
|
|
return CGF.EmitPseudoObjectRValue(E).getScalarVal();
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:02:54 +08:00
|
|
|
Value *VisitOpaqueValueExpr(OpaqueValueExpr *E) {
|
2011-02-17 18:25:35 +08:00
|
|
|
if (E->isGLValue())
|
2013-10-02 10:29:49 +08:00
|
|
|
return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->getExprLoc());
|
2011-02-16 16:02:54 +08:00
|
|
|
|
|
|
|
// Otherwise, assume the mapping is the scalar directly.
|
2011-02-17 18:25:35 +08:00
|
|
|
return CGF.getOpaqueRValueMapping(E).getScalarVal();
|
2011-02-16 16:02:54 +08:00
|
|
|
}
|
2012-03-07 09:58:44 +08:00
|
|
|
|
2012-03-10 11:05:10 +08:00
|
|
|
// l-values.
|
2012-03-10 17:33:50 +08:00
|
|
|
Value *VisitDeclRefExpr(DeclRefExpr *E) {
|
|
|
|
if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) {
|
2012-03-10 11:05:10 +08:00
|
|
|
if (result.isReference())
|
2013-10-02 10:29:49 +08:00
|
|
|
return EmitLoadOfLValue(result.getReferenceLValue(CGF, E),
|
|
|
|
E->getExprLoc());
|
2012-03-10 11:05:10 +08:00
|
|
|
return result.getValue();
|
2010-10-09 09:34:31 +08:00
|
|
|
}
|
2012-03-10 17:33:50 +08:00
|
|
|
return EmitLoadOfLValue(E);
|
2012-03-10 11:05:10 +08:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
Value *VisitObjCMessageExpr(ObjCMessageExpr *E) {
|
2013-07-26 14:16:11 +08:00
|
|
|
if (E->getMethodDecl() &&
|
2014-01-26 00:55:45 +08:00
|
|
|
E->getMethodDecl()->getReturnType()->isReferenceType())
|
2011-03-03 04:09:49 +08:00
|
|
|
return EmitLoadOfLValue(E);
|
2008-08-27 14:57:25 +08:00
|
|
|
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);
|
2013-10-02 10:29:49 +08:00
|
|
|
Value *V = CGF.EmitLoadOfLValue(LV, E->getExprLoc()).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);
|
2013-09-18 11:29:45 +08:00
|
|
|
Value *VisitConvertVectorExpr(ConvertVectorExpr *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) {
|
2012-12-21 11:17:28 +08:00
|
|
|
return EmitNullValue(E->getType());
|
2009-01-30 01:44:32 +08:00
|
|
|
}
|
2011-06-25 05:55:10 +08:00
|
|
|
Value *VisitExplicitCastExpr(ExplicitCastExpr *E) {
|
2010-09-25 01:30:16 +08:00
|
|
|
if (E->getType()->isVariablyModifiedType())
|
2011-06-25 05:55:10 +08:00
|
|
|
CGF.EmitVariablyModifiedType(E->getType());
|
|
|
|
return VisitCastExpr(E);
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2011-06-25 05:55:10 +08:00
|
|
|
Value *VisitCastExpr(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
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Unary Operators.
|
|
|
|
Value *VisitUnaryPostDec(const UnaryOperator *E) {
|
2010-06-27 06:09:34 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
return EmitScalarPrePostIncDec(E, LV, false, false);
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
Value *VisitUnaryPostInc(const UnaryOperator *E) {
|
2010-06-27 06:09:34 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
return EmitScalarPrePostIncDec(E, LV, true, false);
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
Value *VisitUnaryPreDec(const UnaryOperator *E) {
|
2010-06-27 06:09:34 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
return EmitScalarPrePostIncDec(E, LV, false, true);
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
Value *VisitUnaryPreInc(const UnaryOperator *E) {
|
2010-06-27 06:09:34 +08:00
|
|
|
LValue LV = EmitLValue(E->getSubExpr());
|
|
|
|
return EmitScalarPrePostIncDec(E, LV, true, true);
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
2010-06-27 06:09:34 +08:00
|
|
|
|
2011-02-07 10:17:30 +08:00
|
|
|
llvm::Value *EmitAddConsiderOverflowBehavior(const UnaryOperator *E,
|
|
|
|
llvm::Value *InVal,
|
|
|
|
llvm::Value *NextVal,
|
|
|
|
bool IsInc);
|
|
|
|
|
2010-06-27 06:09:34 +08:00
|
|
|
llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
|
|
|
|
bool isInc, bool isPre);
|
|
|
|
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *VisitUnaryAddrOf(const UnaryOperator *E) {
|
2011-02-03 16:15:49 +08:00
|
|
|
if (isa<MemberPointerType>(E->getType())) // never sugared
|
|
|
|
return CGF.CGM.getMemberPointerConstant(E);
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
return EmitLValue(E->getSubExpr()).getAddress();
|
|
|
|
}
|
2010-12-04 20:43:24 +08:00
|
|
|
Value *VisitUnaryDeref(const UnaryOperator *E) {
|
|
|
|
if (E->getType()->isVoidType())
|
|
|
|
return Visit(E->getSubExpr()); // the actual value should be unused
|
|
|
|
return EmitLoadOfLValue(E);
|
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
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());
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-04-15 00:58:56 +08:00
|
|
|
// C++
|
2011-08-09 08:37:14 +08:00
|
|
|
Value *VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E) {
|
2011-08-14 12:50:34 +08:00
|
|
|
return EmitLoadOfLValue(E);
|
2011-08-09 08:37:14 +08:00
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2008-04-08 12:40:51 +08:00
|
|
|
Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
|
|
|
|
return Visit(DAE->getExpr());
|
|
|
|
}
|
2013-04-21 06:23:05 +08:00
|
|
|
Value *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) {
|
|
|
|
CodeGenFunction::CXXDefaultInitExprScope Scope(CGF);
|
|
|
|
return Visit(DIE->getExpr());
|
|
|
|
}
|
2009-04-15 00:58:56 +08:00
|
|
|
Value *VisitCXXThisExpr(CXXThisExpr *TE) {
|
|
|
|
return CGF.LoadCXXThis();
|
2009-09-09 21:00:44 +08:00
|
|
|
}
|
|
|
|
|
2010-12-06 16:20:24 +08:00
|
|
|
Value *VisitExprWithCleanups(ExprWithCleanups *E) {
|
2011-11-10 16:15:53 +08:00
|
|
|
CGF.enterFullExpression(E);
|
|
|
|
CodeGenFunction::RunCleanupsScope Scope(CGF);
|
|
|
|
return Visit(E->getSubExpr());
|
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-09-09 21:00:44 +08:00
|
|
|
|
2013-12-14 04:49:58 +08:00
|
|
|
Value *VisitTypeTraitExpr(const TypeTraitExpr *E) {
|
2010-12-09 06:35:30 +08:00
|
|
|
return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
|
2010-12-07 08:08:36 +08:00
|
|
|
}
|
|
|
|
|
2011-04-28 08:16:57 +08:00
|
|
|
Value *VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
|
|
|
|
return llvm::ConstantInt::get(Builder.getInt32Ty(), E->getValue());
|
|
|
|
}
|
|
|
|
|
2011-04-25 14:54:41 +08:00
|
|
|
Value *VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
|
|
|
|
return llvm::ConstantInt::get(Builder.getInt1Ty(), E->getValue());
|
|
|
|
}
|
|
|
|
|
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) {
|
2010-05-23 01:45:10 +08:00
|
|
|
return EmitNullValue(E->getType());
|
2009-09-15 12:39:46 +08:00
|
|
|
}
|
2009-10-30 09:42:31 +08:00
|
|
|
|
|
|
|
Value *VisitCXXThrowExpr(const CXXThrowExpr *E) {
|
|
|
|
CGF.EmitCXXThrowExpr(E);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-11 05:04:00 +08:00
|
|
|
Value *VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
|
2011-04-20 06:55:03 +08:00
|
|
|
return Builder.getInt1(E->getValue());
|
2010-09-11 05:04:00 +08:00
|
|
|
}
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// Binary Operators.
|
|
|
|
Value *EmitMul(const BinOpInfo &Ops) {
|
2011-05-21 00:38:50 +08:00
|
|
|
if (Ops.Ty->isSignedIntegerOrEnumerationType()) {
|
2012-11-02 06:30:59 +08:00
|
|
|
switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
|
2010-06-27 05:25:03 +08:00
|
|
|
case LangOptions::SOB_Defined:
|
|
|
|
return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
|
2012-08-25 08:32:28 +08:00
|
|
|
case LangOptions::SOB_Undefined:
|
2013-01-18 19:30:38 +08:00
|
|
|
if (!CGF.SanOpts->SignedIntegerOverflow)
|
2012-08-25 08:32:28 +08:00
|
|
|
return Builder.CreateNSWMul(Ops.LHS, Ops.RHS, "mul");
|
|
|
|
// Fall through.
|
2010-06-27 05:25:03 +08:00
|
|
|
case LangOptions::SOB_Trapping:
|
|
|
|
return EmitOverflowCheckedBinOp(Ops);
|
|
|
|
}
|
|
|
|
}
|
2012-11-06 06:21:05 +08:00
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (Ops.Ty->isUnsignedIntegerType() && CGF.SanOpts->UnsignedIntegerOverflow)
|
2012-11-27 23:01:55 +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);
|
2012-09-08 10:08:36 +08:00
|
|
|
|
2010-09-12 05:47:09 +08:00
|
|
|
// Check for undefined division and modulus behaviors.
|
2013-07-26 14:16:11 +08:00
|
|
|
void EmitUndefinedBehaviorIntegerDivAndRemCheck(const BinOpInfo &Ops,
|
2010-09-12 05:47:09 +08:00
|
|
|
llvm::Value *Zero,bool isDiv);
|
2013-01-08 00:43:27 +08:00
|
|
|
// Common helper for getting how wide LHS of shift is.
|
|
|
|
static Value *GetWidthMinusOneValue(Value* LHS,Value* RHS);
|
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 &),
|
2010-06-30 06:00:45 +08:00
|
|
|
Value *&Result);
|
2010-04-23 12:16:32 +08:00
|
|
|
|
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);
|
2011-02-17 18:25:35 +08:00
|
|
|
Value *VisitAbstractConditionalOperator(const AbstractConditionalOperator *);
|
2007-08-24 13:35:26 +08:00
|
|
|
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);
|
|
|
|
}
|
2012-04-19 08:25:12 +08:00
|
|
|
Value *VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
|
|
|
|
return CGF.EmitObjCBoxedExpr(E);
|
2012-03-07 04:05:56 +08:00
|
|
|
}
|
|
|
|
Value *VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
|
|
|
|
return CGF.EmitObjCArrayLiteral(E);
|
|
|
|
}
|
|
|
|
Value *VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
|
|
|
|
return CGF.EmitObjCDictionaryLiteral(E);
|
|
|
|
}
|
2011-06-04 08:47:47 +08:00
|
|
|
Value *VisitAsTypeExpr(AsTypeExpr *CE);
|
2011-10-11 10:20:01 +08:00
|
|
|
Value *VisitAtomicExpr(AtomicExpr *AE);
|
2007-08-24 13:35:26 +08:00
|
|
|
};
|
|
|
|
} // 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
|
|
|
|
2010-11-15 17:13:47 +08:00
|
|
|
if (SrcType->isRealFloatingType())
|
|
|
|
return EmitFloatToBoolConversion(Src);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
if (const MemberPointerType *MPT = dyn_cast<MemberPointerType>(SrcType))
|
|
|
|
return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, Src, MPT);
|
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
|
|
|
|
2010-11-15 17:13:47 +08:00
|
|
|
if (isa<llvm::IntegerType>(Src->getType()))
|
|
|
|
return EmitIntToBoolConversion(Src);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-11-15 17:13:47 +08:00
|
|
|
assert(isa<llvm::PointerType>(Src->getType()));
|
|
|
|
return EmitPointerToBoolConversion(Src);
|
2007-08-27 00:42:57 +08:00
|
|
|
}
|
|
|
|
|
2012-10-13 06:57:06 +08:00
|
|
|
void ScalarExprEmitter::EmitFloatConversionCheck(Value *OrigSrc,
|
|
|
|
QualType OrigSrcType,
|
|
|
|
Value *Src, QualType SrcType,
|
|
|
|
QualType DstType,
|
|
|
|
llvm::Type *DstTy) {
|
|
|
|
using llvm::APFloat;
|
|
|
|
using llvm::APSInt;
|
|
|
|
|
|
|
|
llvm::Type *SrcTy = Src->getType();
|
|
|
|
|
|
|
|
llvm::Value *Check = 0;
|
|
|
|
if (llvm::IntegerType *IntTy = dyn_cast<llvm::IntegerType>(SrcTy)) {
|
|
|
|
// Integer to floating-point. This can fail for unsigned short -> __half
|
|
|
|
// or unsigned __int128 -> float.
|
|
|
|
assert(DstType->isFloatingType());
|
|
|
|
bool SrcIsUnsigned = OrigSrcType->isUnsignedIntegerOrEnumerationType();
|
|
|
|
|
|
|
|
APFloat LargestFloat =
|
|
|
|
APFloat::getLargest(CGF.getContext().getFloatTypeSemantics(DstType));
|
|
|
|
APSInt LargestInt(IntTy->getBitWidth(), SrcIsUnsigned);
|
|
|
|
|
|
|
|
bool IsExact;
|
|
|
|
if (LargestFloat.convertToInteger(LargestInt, APFloat::rmTowardZero,
|
|
|
|
&IsExact) != APFloat::opOK)
|
|
|
|
// The range of representable values of this floating point type includes
|
|
|
|
// all values of this integer type. Don't need an overflow check.
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::Value *Max = llvm::ConstantInt::get(VMContext, LargestInt);
|
|
|
|
if (SrcIsUnsigned)
|
|
|
|
Check = Builder.CreateICmpULE(Src, Max);
|
|
|
|
else {
|
|
|
|
llvm::Value *Min = llvm::ConstantInt::get(VMContext, -LargestInt);
|
|
|
|
llvm::Value *GE = Builder.CreateICmpSGE(Src, Min);
|
|
|
|
llvm::Value *LE = Builder.CreateICmpSLE(Src, Max);
|
|
|
|
Check = Builder.CreateAnd(GE, LE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const llvm::fltSemantics &SrcSema =
|
|
|
|
CGF.getContext().getFloatTypeSemantics(OrigSrcType);
|
|
|
|
if (isa<llvm::IntegerType>(DstTy)) {
|
2013-03-28 07:20:25 +08:00
|
|
|
// Floating-point to integer. This has undefined behavior if the source is
|
|
|
|
// +-Inf, NaN, or doesn't fit into the destination type (after truncation
|
|
|
|
// to an integer).
|
2012-10-13 06:57:06 +08:00
|
|
|
unsigned Width = CGF.getContext().getIntWidth(DstType);
|
|
|
|
bool Unsigned = DstType->isUnsignedIntegerOrEnumerationType();
|
|
|
|
|
|
|
|
APSInt Min = APSInt::getMinValue(Width, Unsigned);
|
2013-03-28 07:20:25 +08:00
|
|
|
APFloat MinSrc(SrcSema, APFloat::uninitialized);
|
2012-10-13 06:57:06 +08:00
|
|
|
if (MinSrc.convertFromAPInt(Min, !Unsigned, APFloat::rmTowardZero) &
|
|
|
|
APFloat::opOverflow)
|
|
|
|
// Don't need an overflow check for lower bound. Just check for
|
|
|
|
// -Inf/NaN.
|
2013-03-19 08:01:12 +08:00
|
|
|
MinSrc = APFloat::getInf(SrcSema, true);
|
|
|
|
else
|
|
|
|
// Find the largest value which is too small to represent (before
|
|
|
|
// truncation toward zero).
|
|
|
|
MinSrc.subtract(APFloat(SrcSema, 1), APFloat::rmTowardNegative);
|
2012-10-13 06:57:06 +08:00
|
|
|
|
|
|
|
APSInt Max = APSInt::getMaxValue(Width, Unsigned);
|
2013-03-28 07:20:25 +08:00
|
|
|
APFloat MaxSrc(SrcSema, APFloat::uninitialized);
|
2012-10-13 06:57:06 +08:00
|
|
|
if (MaxSrc.convertFromAPInt(Max, !Unsigned, APFloat::rmTowardZero) &
|
|
|
|
APFloat::opOverflow)
|
|
|
|
// Don't need an overflow check for upper bound. Just check for
|
|
|
|
// +Inf/NaN.
|
2013-03-19 08:01:12 +08:00
|
|
|
MaxSrc = APFloat::getInf(SrcSema, false);
|
|
|
|
else
|
|
|
|
// Find the smallest value which is too large to represent (before
|
|
|
|
// truncation toward zero).
|
|
|
|
MaxSrc.add(APFloat(SrcSema, 1), APFloat::rmTowardPositive);
|
2012-10-13 06:57:06 +08:00
|
|
|
|
2013-03-28 07:20:25 +08:00
|
|
|
// If we're converting from __half, convert the range to float to match
|
|
|
|
// the type of src.
|
|
|
|
if (OrigSrcType->isHalfType()) {
|
|
|
|
const llvm::fltSemantics &Sema =
|
|
|
|
CGF.getContext().getFloatTypeSemantics(SrcType);
|
|
|
|
bool IsInexact;
|
|
|
|
MinSrc.convert(Sema, APFloat::rmTowardZero, &IsInexact);
|
|
|
|
MaxSrc.convert(Sema, APFloat::rmTowardZero, &IsInexact);
|
|
|
|
}
|
2012-10-13 06:57:06 +08:00
|
|
|
|
2013-03-19 08:01:12 +08:00
|
|
|
llvm::Value *GE =
|
|
|
|
Builder.CreateFCmpOGT(Src, llvm::ConstantFP::get(VMContext, MinSrc));
|
|
|
|
llvm::Value *LE =
|
|
|
|
Builder.CreateFCmpOLT(Src, llvm::ConstantFP::get(VMContext, MaxSrc));
|
|
|
|
Check = Builder.CreateAnd(GE, LE);
|
|
|
|
} else {
|
2013-03-28 07:20:25 +08:00
|
|
|
// FIXME: Maybe split this sanitizer out from float-cast-overflow.
|
|
|
|
//
|
|
|
|
// Floating-point to floating-point. This has undefined behavior if the
|
|
|
|
// source is not in the range of representable values of the destination
|
|
|
|
// type. The C and C++ standards are spectacularly unclear here. We
|
|
|
|
// diagnose finite out-of-range conversions, but allow infinities and NaNs
|
|
|
|
// to convert to the corresponding value in the smaller type.
|
|
|
|
//
|
|
|
|
// C11 Annex F gives all such conversions defined behavior for IEC 60559
|
|
|
|
// conforming implementations. Unfortunately, LLVM's fptrunc instruction
|
|
|
|
// does not.
|
|
|
|
|
|
|
|
// Converting from a lower rank to a higher rank can never have
|
|
|
|
// undefined behavior, since higher-rank types must have a superset
|
|
|
|
// of values of lower-rank types.
|
|
|
|
if (CGF.getContext().getFloatingTypeOrder(OrigSrcType, DstType) != 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(!OrigSrcType->isHalfType() &&
|
|
|
|
"should not check conversion from __half, it has the lowest rank");
|
|
|
|
|
|
|
|
const llvm::fltSemantics &DstSema =
|
|
|
|
CGF.getContext().getFloatTypeSemantics(DstType);
|
|
|
|
APFloat MinBad = APFloat::getLargest(DstSema, false);
|
|
|
|
APFloat MaxBad = APFloat::getInf(DstSema, false);
|
|
|
|
|
|
|
|
bool IsInexact;
|
|
|
|
MinBad.convert(SrcSema, APFloat::rmTowardZero, &IsInexact);
|
|
|
|
MaxBad.convert(SrcSema, APFloat::rmTowardZero, &IsInexact);
|
|
|
|
|
|
|
|
Value *AbsSrc = CGF.EmitNounwindRuntimeCall(
|
|
|
|
CGF.CGM.getIntrinsic(llvm::Intrinsic::fabs, Src->getType()), Src);
|
2013-03-19 08:01:12 +08:00
|
|
|
llvm::Value *GE =
|
2013-03-28 07:20:25 +08:00
|
|
|
Builder.CreateFCmpOGT(AbsSrc, llvm::ConstantFP::get(VMContext, MinBad));
|
2013-03-19 08:01:12 +08:00
|
|
|
llvm::Value *LE =
|
2013-03-28 07:20:25 +08:00
|
|
|
Builder.CreateFCmpOLT(AbsSrc, llvm::ConstantFP::get(VMContext, MaxBad));
|
|
|
|
Check = Builder.CreateNot(Builder.CreateAnd(GE, LE));
|
2013-03-19 08:01:12 +08:00
|
|
|
}
|
2012-10-13 06:57:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Provide a SourceLocation.
|
|
|
|
llvm::Constant *StaticArgs[] = {
|
|
|
|
CGF.EmitCheckTypeDescriptor(OrigSrcType),
|
|
|
|
CGF.EmitCheckTypeDescriptor(DstType)
|
|
|
|
};
|
2012-12-03 03:50:33 +08:00
|
|
|
CGF.EmitCheck(Check, "float_cast_overflow", StaticArgs, OrigSrc,
|
|
|
|
CodeGenFunction::CRK_Recoverable);
|
2012-10-13 06:57:06 +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 *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
|
|
|
|
2012-10-13 06:57:06 +08:00
|
|
|
llvm::Value *OrigSrc = Src;
|
|
|
|
QualType OrigSrcType = SrcType;
|
2011-10-15 07:23:15 +08:00
|
|
|
llvm::Type *SrcTy = Src->getType();
|
|
|
|
|
2013-01-23 19:56:20 +08:00
|
|
|
// If casting to/from storage-only half FP, use special intrinsics.
|
|
|
|
if (SrcType->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) {
|
2011-10-15 07:23:15 +08:00
|
|
|
Src = Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16), Src);
|
|
|
|
SrcType = CGF.getContext().FloatTy;
|
2012-02-07 08:39:47 +08:00
|
|
|
SrcTy = CGF.FloatTy;
|
2011-10-15 07:23:15 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *DstTy = ConvertType(DstType);
|
2007-08-26 14:48:56 +08:00
|
|
|
|
|
|
|
// Ignore conversions like int -> uint.
|
2011-10-15 07:23:15 +08:00
|
|
|
if (SrcTy == DstTy)
|
2007-08-26 14:48:56 +08:00
|
|
|
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.
|
2011-10-15 07:23:15 +08:00
|
|
|
if (isa<llvm::PointerType>(SrcTy))
|
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.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *MiddleTy = CGF.IntPtrTy;
|
2011-05-21 00:38:50 +08:00
|
|
|
bool InputSigned = SrcType->isSignedIntegerOrEnumerationType();
|
2009-03-04 12:02:35 +08:00
|
|
|
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
|
|
|
|
2011-10-15 07:23:15 +08:00
|
|
|
if (isa<llvm::PointerType>(SrcTy)) {
|
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);
|
|
|
|
|
|
|
|
// Splat the element across to all elements
|
|
|
|
unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
|
2013-01-02 04:08:10 +08:00
|
|
|
return Builder.CreateVectorSplat(NumElements, Elt, "splat");
|
2009-01-18 14:42:49 +08:00
|
|
|
}
|
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.
|
2011-10-15 07:23:15 +08:00
|
|
|
if (isa<llvm::VectorType>(SrcTy) ||
|
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.
|
2011-10-15 07:23:15 +08:00
|
|
|
Value *Res = NULL;
|
|
|
|
llvm::Type *ResTy = DstTy;
|
|
|
|
|
2012-10-13 06:57:06 +08:00
|
|
|
// An overflowing conversion has undefined behavior if either the source type
|
|
|
|
// or the destination type is a floating-point type.
|
2013-01-18 19:30:38 +08:00
|
|
|
if (CGF.SanOpts->FloatCastOverflow &&
|
2012-10-13 06:57:06 +08:00
|
|
|
(OrigSrcType->isFloatingType() || DstType->isFloatingType()))
|
2013-01-18 19:30:38 +08:00
|
|
|
EmitFloatConversionCheck(OrigSrc, OrigSrcType, Src, SrcType, DstType,
|
|
|
|
DstTy);
|
2012-10-13 06:57:06 +08:00
|
|
|
|
2011-10-15 07:23:15 +08:00
|
|
|
// Cast to half via float
|
2013-01-23 19:56:20 +08:00
|
|
|
if (DstType->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType)
|
2012-02-07 08:39:47 +08:00
|
|
|
DstTy = CGF.FloatTy;
|
2011-10-15 07:23:15 +08:00
|
|
|
|
|
|
|
if (isa<llvm::IntegerType>(SrcTy)) {
|
2011-05-21 00:38:50 +08:00
|
|
|
bool InputSigned = SrcType->isSignedIntegerOrEnumerationType();
|
2007-12-27 02:20:19 +08:00
|
|
|
if (isa<llvm::IntegerType>(DstTy))
|
2011-10-15 07:23:15 +08:00
|
|
|
Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
|
2007-12-27 02:20:19 +08:00
|
|
|
else if (InputSigned)
|
2011-10-15 07:23:15 +08:00
|
|
|
Res = Builder.CreateSIToFP(Src, DstTy, "conv");
|
2007-12-27 02:20:19 +08:00
|
|
|
else
|
2011-10-15 07:23:15 +08:00
|
|
|
Res = Builder.CreateUIToFP(Src, DstTy, "conv");
|
|
|
|
} else if (isa<llvm::IntegerType>(DstTy)) {
|
|
|
|
assert(SrcTy->isFloatingPointTy() && "Unknown real conversion");
|
2011-05-21 00:38:50 +08:00
|
|
|
if (DstType->isSignedIntegerOrEnumerationType())
|
2011-10-15 07:23:15 +08:00
|
|
|
Res = Builder.CreateFPToSI(Src, DstTy, "conv");
|
|
|
|
else
|
|
|
|
Res = Builder.CreateFPToUI(Src, DstTy, "conv");
|
|
|
|
} else {
|
|
|
|
assert(SrcTy->isFloatingPointTy() && DstTy->isFloatingPointTy() &&
|
|
|
|
"Unknown real conversion");
|
|
|
|
if (DstTy->getTypeID() < SrcTy->getTypeID())
|
|
|
|
Res = Builder.CreateFPTrunc(Src, DstTy, "conv");
|
2007-12-27 02:20:19 +08:00
|
|
|
else
|
2011-10-15 07:23:15 +08:00
|
|
|
Res = Builder.CreateFPExt(Src, DstTy, "conv");
|
2007-08-26 14:48:56 +08:00
|
|
|
}
|
|
|
|
|
2011-10-15 07:23:15 +08:00
|
|
|
if (DstTy != ResTy) {
|
|
|
|
assert(ResTy->isIntegerTy(16) && "Only half FP requires extra conversion");
|
|
|
|
Res = Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16), Res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
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.
|
2013-03-08 05:37:08 +08:00
|
|
|
SrcTy = SrcTy->castAs<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);
|
|
|
|
}
|
|
|
|
|
2010-05-23 01:45:10 +08:00
|
|
|
Value *ScalarExprEmitter::EmitNullValue(QualType Ty) {
|
2012-12-21 11:17:28 +08:00
|
|
|
return CGF.EmitFromMemory(CGF.CGM.EmitNullConstant(Ty), Ty);
|
2010-05-23 01:45:10 +08:00
|
|
|
}
|
2007-08-27 00:34:22 +08:00
|
|
|
|
2012-10-10 03:52:38 +08:00
|
|
|
/// \brief Emit a sanitization check for the given "binary" operation (which
|
|
|
|
/// might actually be a unary increment which has been lowered to a binary
|
|
|
|
/// operation). The check passes if \p Check, which is an \c i1, is \c true.
|
|
|
|
void ScalarExprEmitter::EmitBinOpCheck(Value *Check, const BinOpInfo &Info) {
|
|
|
|
StringRef CheckName;
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<llvm::Constant *, 4> StaticData;
|
|
|
|
SmallVector<llvm::Value *, 2> DynamicData;
|
2012-10-10 03:52:38 +08:00
|
|
|
|
|
|
|
BinaryOperatorKind Opcode = Info.Opcode;
|
|
|
|
if (BinaryOperator::isCompoundAssignmentOp(Opcode))
|
|
|
|
Opcode = BinaryOperator::getOpForCompoundAssignment(Opcode);
|
|
|
|
|
|
|
|
StaticData.push_back(CGF.EmitCheckSourceLocation(Info.E->getExprLoc()));
|
|
|
|
const UnaryOperator *UO = dyn_cast<UnaryOperator>(Info.E);
|
|
|
|
if (UO && UO->getOpcode() == UO_Minus) {
|
|
|
|
CheckName = "negate_overflow";
|
|
|
|
StaticData.push_back(CGF.EmitCheckTypeDescriptor(UO->getType()));
|
|
|
|
DynamicData.push_back(Info.RHS);
|
|
|
|
} else {
|
|
|
|
if (BinaryOperator::isShiftOp(Opcode)) {
|
|
|
|
// Shift LHS negative or too large, or RHS out of bounds.
|
|
|
|
CheckName = "shift_out_of_bounds";
|
|
|
|
const BinaryOperator *BO = cast<BinaryOperator>(Info.E);
|
|
|
|
StaticData.push_back(
|
|
|
|
CGF.EmitCheckTypeDescriptor(BO->getLHS()->getType()));
|
|
|
|
StaticData.push_back(
|
|
|
|
CGF.EmitCheckTypeDescriptor(BO->getRHS()->getType()));
|
|
|
|
} else if (Opcode == BO_Div || Opcode == BO_Rem) {
|
|
|
|
// Divide or modulo by zero, or signed overflow (eg INT_MAX / -1).
|
|
|
|
CheckName = "divrem_overflow";
|
2013-01-08 06:25:52 +08:00
|
|
|
StaticData.push_back(CGF.EmitCheckTypeDescriptor(Info.Ty));
|
2012-10-10 03:52:38 +08:00
|
|
|
} else {
|
|
|
|
// Signed arithmetic overflow (+, -, *).
|
|
|
|
switch (Opcode) {
|
|
|
|
case BO_Add: CheckName = "add_overflow"; break;
|
|
|
|
case BO_Sub: CheckName = "sub_overflow"; break;
|
|
|
|
case BO_Mul: CheckName = "mul_overflow"; break;
|
|
|
|
default: llvm_unreachable("unexpected opcode for bin op check");
|
|
|
|
}
|
2013-01-08 06:25:52 +08:00
|
|
|
StaticData.push_back(CGF.EmitCheckTypeDescriptor(Info.Ty));
|
2012-10-10 03:52:38 +08:00
|
|
|
}
|
|
|
|
DynamicData.push_back(Info.LHS);
|
|
|
|
DynamicData.push_back(Info.RHS);
|
|
|
|
}
|
|
|
|
|
2012-12-03 03:50:33 +08:00
|
|
|
CGF.EmitCheck(Check, CheckName, StaticData, DynamicData,
|
|
|
|
CodeGenFunction::CRK_Recoverable);
|
2012-10-10 03:52:38 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2010-06-08 08:16:34 +08:00
|
|
|
// Vector Mask Case
|
2013-07-26 14:16:11 +08:00
|
|
|
if (E->getNumSubExprs() == 2 ||
|
2010-06-09 10:17:08 +08:00
|
|
|
(E->getNumSubExprs() == 3 && E->getExpr(2)->getType()->isVectorType())) {
|
2010-06-27 15:15:29 +08:00
|
|
|
Value *LHS = CGF.EmitScalarExpr(E->getExpr(0));
|
|
|
|
Value *RHS = CGF.EmitScalarExpr(E->getExpr(1));
|
|
|
|
Value *Mask;
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::VectorType *LTy = cast<llvm::VectorType>(LHS->getType());
|
2010-06-08 08:16:34 +08:00
|
|
|
unsigned LHSElts = LTy->getNumElements();
|
|
|
|
|
|
|
|
if (E->getNumSubExprs() == 3) {
|
|
|
|
Mask = CGF.EmitScalarExpr(E->getExpr(2));
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-06-08 08:16:34 +08:00
|
|
|
// Shuffle LHS & RHS into one input vector.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant*, 32> concat;
|
2010-06-08 08:16:34 +08:00
|
|
|
for (unsigned i = 0; i != LHSElts; ++i) {
|
2011-04-20 06:55:03 +08:00
|
|
|
concat.push_back(Builder.getInt32(2*i));
|
|
|
|
concat.push_back(Builder.getInt32(2*i+1));
|
2010-06-08 08:16:34 +08:00
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-02-15 08:14:06 +08:00
|
|
|
Value* CV = llvm::ConstantVector::get(concat);
|
2010-06-08 08:16:34 +08:00
|
|
|
LHS = Builder.CreateShuffleVector(LHS, RHS, CV, "concat");
|
|
|
|
LHSElts *= 2;
|
|
|
|
} else {
|
|
|
|
Mask = RHS;
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::VectorType *MTy = cast<llvm::VectorType>(Mask->getType());
|
2010-06-08 08:16:34 +08:00
|
|
|
llvm::Constant* EltMask;
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2013-08-01 14:42:40 +08:00
|
|
|
EltMask = llvm::ConstantInt::get(MTy->getElementType(),
|
|
|
|
llvm::NextPowerOf2(LHSElts-1)-1);
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-06-08 08:16:34 +08:00
|
|
|
// Mask off the high bits of each shuffle index.
|
2012-01-25 13:34:41 +08:00
|
|
|
Value *MaskBits = llvm::ConstantVector::getSplat(MTy->getNumElements(),
|
|
|
|
EltMask);
|
2010-06-08 08:16:34 +08:00
|
|
|
Mask = Builder.CreateAnd(Mask, MaskBits, "mask");
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-06-08 08:16:34 +08:00
|
|
|
// newv = undef
|
|
|
|
// mask = mask & maskbits
|
|
|
|
// for each elt
|
|
|
|
// n = extract mask i
|
|
|
|
// x = extract val n
|
|
|
|
// newv = insert newv, x, i
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::VectorType *RTy = llvm::VectorType::get(LTy->getElementType(),
|
2013-07-27 13:00:42 +08:00
|
|
|
MTy->getNumElements());
|
2010-06-08 08:16:34 +08:00
|
|
|
Value* NewV = llvm::UndefValue::get(RTy);
|
|
|
|
for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) {
|
2012-04-06 05:48:40 +08:00
|
|
|
Value *IIndx = Builder.getInt32(i);
|
|
|
|
Value *Indx = Builder.CreateExtractElement(Mask, IIndx, "shuf_idx");
|
2010-06-27 15:15:29 +08:00
|
|
|
Indx = Builder.CreateZExt(Indx, CGF.Int32Ty, "idx_zext");
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-06-08 08:16:34 +08:00
|
|
|
Value *VExt = Builder.CreateExtractElement(LHS, Indx, "shuf_elt");
|
2012-04-06 05:48:40 +08:00
|
|
|
NewV = Builder.CreateInsertElement(NewV, VExt, IIndx, "shuf_ins");
|
2010-06-08 08:16:34 +08:00
|
|
|
}
|
|
|
|
return NewV;
|
2008-05-15 03:38:39 +08:00
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2008-05-15 03:38:39 +08:00
|
|
|
Value* V1 = CGF.EmitScalarExpr(E->getExpr(0));
|
|
|
|
Value* V2 = CGF.EmitScalarExpr(E->getExpr(1));
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant*, 32> indices;
|
2013-08-01 12:51:48 +08:00
|
|
|
for (unsigned i = 2; i < E->getNumSubExprs(); ++i) {
|
2013-08-04 01:40:38 +08:00
|
|
|
llvm::APSInt Idx = E->getShuffleMaskIdx(CGF.getContext(), i-2);
|
|
|
|
// Check for -1 and output it as undef in the IR.
|
|
|
|
if (Idx.isSigned() && Idx.isAllOnesValue())
|
|
|
|
indices.push_back(llvm::UndefValue::get(CGF.Int32Ty));
|
|
|
|
else
|
|
|
|
indices.push_back(Builder.getInt32(Idx.getZExtValue()));
|
2010-06-08 08:16:34 +08:00
|
|
|
}
|
|
|
|
|
2011-02-15 08:14:06 +08:00
|
|
|
Value *SV = llvm::ConstantVector::get(indices);
|
2008-05-15 03:38:39 +08:00
|
|
|
return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
|
|
|
|
}
|
2013-09-18 11:29:45 +08:00
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitConvertVectorExpr(ConvertVectorExpr *E) {
|
|
|
|
QualType SrcType = E->getSrcExpr()->getType(),
|
|
|
|
DstType = E->getType();
|
|
|
|
|
|
|
|
Value *Src = CGF.EmitScalarExpr(E->getSrcExpr());
|
|
|
|
|
|
|
|
SrcType = CGF.getContext().getCanonicalType(SrcType);
|
|
|
|
DstType = CGF.getContext().getCanonicalType(DstType);
|
|
|
|
if (SrcType == DstType) return Src;
|
|
|
|
|
|
|
|
assert(SrcType->isVectorType() &&
|
|
|
|
"ConvertVector source type must be a vector");
|
|
|
|
assert(DstType->isVectorType() &&
|
|
|
|
"ConvertVector destination type must be a vector");
|
|
|
|
|
|
|
|
llvm::Type *SrcTy = Src->getType();
|
|
|
|
llvm::Type *DstTy = ConvertType(DstType);
|
|
|
|
|
|
|
|
// Ignore conversions like int -> uint.
|
|
|
|
if (SrcTy == DstTy)
|
|
|
|
return Src;
|
|
|
|
|
|
|
|
QualType SrcEltType = SrcType->getAs<VectorType>()->getElementType(),
|
|
|
|
DstEltType = DstType->getAs<VectorType>()->getElementType();
|
|
|
|
|
|
|
|
assert(SrcTy->isVectorTy() &&
|
|
|
|
"ConvertVector source IR type must be a vector");
|
|
|
|
assert(DstTy->isVectorTy() &&
|
|
|
|
"ConvertVector destination IR type must be a vector");
|
|
|
|
|
|
|
|
llvm::Type *SrcEltTy = SrcTy->getVectorElementType(),
|
|
|
|
*DstEltTy = DstTy->getVectorElementType();
|
|
|
|
|
|
|
|
if (DstEltType->isBooleanType()) {
|
|
|
|
assert((SrcEltTy->isFloatingPointTy() ||
|
|
|
|
isa<llvm::IntegerType>(SrcEltTy)) && "Unknown boolean conversion");
|
|
|
|
|
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(SrcTy);
|
|
|
|
if (SrcEltTy->isFloatingPointTy()) {
|
|
|
|
return Builder.CreateFCmpUNE(Src, Zero, "tobool");
|
|
|
|
} else {
|
|
|
|
return Builder.CreateICmpNE(Src, Zero, "tobool");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have the arithmetic types: real int/float.
|
|
|
|
Value *Res = NULL;
|
|
|
|
|
|
|
|
if (isa<llvm::IntegerType>(SrcEltTy)) {
|
|
|
|
bool InputSigned = SrcEltType->isSignedIntegerOrEnumerationType();
|
|
|
|
if (isa<llvm::IntegerType>(DstEltTy))
|
|
|
|
Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv");
|
|
|
|
else if (InputSigned)
|
|
|
|
Res = Builder.CreateSIToFP(Src, DstTy, "conv");
|
|
|
|
else
|
|
|
|
Res = Builder.CreateUIToFP(Src, DstTy, "conv");
|
|
|
|
} else if (isa<llvm::IntegerType>(DstEltTy)) {
|
|
|
|
assert(SrcEltTy->isFloatingPointTy() && "Unknown real conversion");
|
|
|
|
if (DstEltType->isSignedIntegerOrEnumerationType())
|
|
|
|
Res = Builder.CreateFPToSI(Src, DstTy, "conv");
|
|
|
|
else
|
|
|
|
Res = Builder.CreateFPToUI(Src, DstTy, "conv");
|
|
|
|
} else {
|
|
|
|
assert(SrcEltTy->isFloatingPointTy() && DstEltTy->isFloatingPointTy() &&
|
|
|
|
"Unknown real conversion");
|
|
|
|
if (DstEltTy->getTypeID() < SrcEltTy->getTypeID())
|
|
|
|
Res = Builder.CreateFPTrunc(Src, DstTy, "conv");
|
|
|
|
else
|
|
|
|
Res = Builder.CreateFPExt(Src, DstTy, "conv");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2009-11-26 11:22:21 +08:00
|
|
|
Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) {
|
2011-12-29 03:48:30 +08:00
|
|
|
llvm::APSInt Value;
|
|
|
|
if (E->EvaluateAsInt(Value, CGF.getContext(), Expr::SE_AllowSideEffects)) {
|
2009-11-26 11:22:21 +08:00
|
|
|
if (E->isArrow())
|
|
|
|
CGF.EmitScalarExpr(E->getBase());
|
|
|
|
else
|
|
|
|
EmitLValue(E->getBase());
|
2011-12-29 03:48:30 +08:00
|
|
|
return Builder.getInt(Value);
|
2009-11-26 11:22:21 +08:00
|
|
|
}
|
2010-10-05 05:46:04 +08:00
|
|
|
|
2009-11-26 11:22:21 +08:00
|
|
|
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());
|
2013-02-23 10:53:19 +08:00
|
|
|
QualType IdxTy = E->getIdx()->getType();
|
|
|
|
|
2013-10-23 06:51:04 +08:00
|
|
|
if (CGF.SanOpts->ArrayBounds)
|
2013-02-23 10:53:19 +08:00
|
|
|
CGF.EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, /*Accessed*/true);
|
|
|
|
|
|
|
|
bool IdxSigned = IdxTy->isSignedIntegerOrEnumerationType();
|
2010-06-27 15:15:29 +08:00
|
|
|
Idx = Builder.CreateIntCast(Idx, CGF.Int32Ty, IdxSigned, "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,
|
2011-07-18 12:24:23 +08:00
|
|
|
unsigned Off, llvm::Type *I32Ty) {
|
2009-10-19 04:10:40 +08:00
|
|
|
int MV = SVI->getMaskValue(Idx);
|
2013-07-26 14:16:11 +08:00
|
|
|
if (MV == -1)
|
2009-10-19 04:10:40 +08:00
|
|
|
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();
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-10-19 04:10:40 +08:00
|
|
|
if (E->hadArrayRangeDesignator())
|
|
|
|
CGF.ErrorUnsupported(E, "GNU array range designator extension");
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::VectorType *VType =
|
2009-10-19 04:10:40 +08:00
|
|
|
dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-09-25 01:48:14 +08:00
|
|
|
if (!VType) {
|
|
|
|
if (NumInitElements == 0) {
|
|
|
|
// C++11 value-initialization for the scalar.
|
|
|
|
return EmitNullValue(E->getType());
|
|
|
|
}
|
|
|
|
// We have a scalar in braces. Just use the first element.
|
2009-10-19 04:10:40 +08:00
|
|
|
return Visit(E->getInit(0));
|
2011-09-25 01:48:14 +08:00
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-10-19 04:10:40 +08:00
|
|
|
unsigned ResElts = VType->getNumElements();
|
2013-07-26 14:16:11 +08:00
|
|
|
|
|
|
|
// Loop over initializers collecting the Value for each, and remembering
|
2009-10-19 04:10:40 +08:00
|
|
|
// 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);
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant*, 16> Args;
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType());
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-10-19 04:10:40 +08:00
|
|
|
// Handle scalar elements. If the scalar initializer is actually one
|
2013-07-26 14:16:11 +08:00
|
|
|
// element of a different vector of the same width, use shuffle instead of
|
2009-10-19 04:10:40 +08:00
|
|
|
// 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);
|
2012-02-14 20:06:21 +08:00
|
|
|
Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
|
2009-10-19 04:10:40 +08:00
|
|
|
|
|
|
|
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)
|
2010-06-27 15:15:29 +08:00
|
|
|
Args.push_back(getMaskElt(SVV, j, 0, CGF.Int32Ty));
|
2011-04-20 06:55:03 +08:00
|
|
|
Args.push_back(Builder.getInt32(ResElts + C->getZExtValue()));
|
2012-02-14 20:06:21 +08:00
|
|
|
Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
|
|
|
|
|
2009-10-19 04:10:40 +08:00
|
|
|
LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0);
|
|
|
|
RHS = EI->getVectorOperand();
|
|
|
|
VIsUndefShuffle = false;
|
|
|
|
}
|
|
|
|
if (!Args.empty()) {
|
2011-02-15 08:14:06 +08:00
|
|
|
llvm::Constant *Mask = llvm::ConstantVector::get(Args);
|
2009-10-19 04:10:40 +08:00
|
|
|
V = Builder.CreateShuffleVector(LHS, RHS, Mask);
|
|
|
|
++CurIdx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-20 06:55:03 +08:00
|
|
|
V = Builder.CreateInsertElement(V, Init, Builder.getInt32(CurIdx),
|
|
|
|
"vecinit");
|
2009-10-19 04:10:40 +08:00
|
|
|
VIsUndefShuffle = false;
|
|
|
|
++CurIdx;
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-10-19 04:10:40 +08:00
|
|
|
unsigned InitElts = VVT->getNumElements();
|
|
|
|
|
2013-07-26 14:16:11 +08:00
|
|
|
// If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's
|
2009-10-19 04:10:40 +08:00
|
|
|
// 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);
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType());
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-10-19 04:10:40 +08:00
|
|
|
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,
|
2010-06-27 15:15:29 +08:00
|
|
|
CGF.Int32Ty));
|
2009-10-19 04:10:40 +08:00
|
|
|
} else {
|
2011-04-20 06:55:03 +08:00
|
|
|
Args.push_back(Builder.getInt32(j));
|
2009-10-19 04:10:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned j = 0, je = InitElts; j != je; ++j)
|
2010-06-27 15:15:29 +08:00
|
|
|
Args.push_back(getMaskElt(SVI, j, Offset, CGF.Int32Ty));
|
2012-02-14 20:06:21 +08:00
|
|
|
Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
|
2009-10-19 04:10:40 +08:00
|
|
|
|
|
|
|
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)
|
2011-04-20 06:55:03 +08:00
|
|
|
Args.push_back(Builder.getInt32(j));
|
2012-02-14 20:06:21 +08:00
|
|
|
Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
|
2011-02-15 08:14:06 +08:00
|
|
|
llvm::Constant *Mask = llvm::ConstantVector::get(Args);
|
2009-10-19 04:10:40 +08:00
|
|
|
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)
|
2011-04-20 06:55:03 +08:00
|
|
|
Args.push_back(Builder.getInt32(j));
|
2009-10-19 04:10:40 +08:00
|
|
|
for (unsigned j = 0; j != InitElts; ++j)
|
2011-04-20 06:55:03 +08:00
|
|
|
Args.push_back(Builder.getInt32(j+Offset));
|
2012-02-14 20:06:21 +08:00
|
|
|
Args.resize(ResElts, llvm::UndefValue::get(CGF.Int32Ty));
|
2009-10-19 04:10:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2011-02-15 08:14:06 +08:00
|
|
|
llvm::Constant *Mask = llvm::ConstantVector::get(Args);
|
2009-10-19 04:10:40 +08:00
|
|
|
V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit");
|
|
|
|
VIsUndefShuffle = isa<llvm::UndefValue>(Init);
|
|
|
|
CurIdx += InitElts;
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-10-19 04:10:40 +08:00
|
|
|
// FIXME: evaluate codegen vs. shuffling against constant null vector.
|
|
|
|
// Emit remaining default initializers.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *EltTy = VType->getElementType();
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-10-19 04:10:40 +08:00
|
|
|
// Emit remaining default initializers
|
|
|
|
for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) {
|
2011-04-20 06:55:03 +08:00
|
|
|
Value *Idx = Builder.getInt32(CurIdx);
|
2009-10-19 04:10:40 +08:00
|
|
|
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
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
if (CE->getCastKind() == CK_UncheckedDerivedToBase)
|
2010-03-31 07:58:03 +08:00
|
|
|
return false;
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-11-24 01:57:54 +08:00
|
|
|
if (isa<CXXThisExpr>(E)) {
|
|
|
|
// We always assume that 'this' is never null.
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-11-24 01:57:54 +08:00
|
|
|
if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) {
|
2010-07-20 12:20:21 +08:00
|
|
|
// And that glvalue casts are never null.
|
2010-08-25 18:28:54 +08:00
|
|
|
if (ICE->getValueKind() != VK_RValue)
|
2009-11-24 01:57:54 +08:00
|
|
|
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.
|
2011-06-25 05:55:10 +08:00
|
|
|
Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
|
2009-11-27 12:41:50 +08:00
|
|
|
Expr *E = CE->getSubExpr();
|
2009-09-23 06:00:46 +08:00
|
|
|
QualType DestTy = CE->getType();
|
2010-08-25 19:45:40 +08:00
|
|
|
CastKind Kind = CE->getCastKind();
|
2013-07-26 14:16:11 +08:00
|
|
|
|
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) {
|
2010-11-15 17:13:47 +08:00
|
|
|
case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!");
|
2012-08-31 08:14:07 +08:00
|
|
|
case CK_BuiltinFnToFnPtr:
|
|
|
|
llvm_unreachable("builtin functions are handled elsewhere");
|
|
|
|
|
2013-07-26 14:16:11 +08:00
|
|
|
case CK_LValueBitCast:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ObjCObjectLValueCast: {
|
2010-07-14 07:17:26 +08:00
|
|
|
Value *V = EmitLValue(E).getAddress();
|
2013-07-26 14:16:11 +08:00
|
|
|
V = Builder.CreateBitCast(V,
|
2010-07-14 07:17:26 +08:00
|
|
|
ConvertType(CGF.getContext().getPointerType(DestTy)));
|
2013-10-02 10:29:49 +08:00
|
|
|
return EmitLoadOfLValue(CGF.MakeNaturalAlignAddrLValue(V, DestTy),
|
|
|
|
CE->getExprLoc());
|
2010-07-14 07:17:26 +08:00
|
|
|
}
|
2011-09-10 09:16:55 +08:00
|
|
|
|
2011-09-09 13:25:32 +08:00
|
|
|
case CK_CPointerToObjCPointerCast:
|
|
|
|
case CK_BlockPointerToObjCPointerCast:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_AnyPointerToBlockPointerCast:
|
|
|
|
case CK_BitCast: {
|
2009-09-02 04:52:42 +08:00
|
|
|
Value *Src = Visit(const_cast<Expr*>(E));
|
2013-12-11 21:39:46 +08:00
|
|
|
llvm::Type *SrcTy = Src->getType();
|
|
|
|
llvm::Type *DstTy = ConvertType(DestTy);
|
|
|
|
if (SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() &&
|
|
|
|
SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) {
|
|
|
|
llvm::Type *MidTy = CGF.CGM.getDataLayout().getIntPtrType(SrcTy);
|
|
|
|
return Builder.CreateIntToPtr(Builder.CreatePtrToInt(Src, MidTy), DstTy);
|
|
|
|
}
|
|
|
|
return Builder.CreateBitCast(Src, DstTy);
|
|
|
|
}
|
|
|
|
case CK_AddressSpaceConversion: {
|
|
|
|
Value *Src = Visit(const_cast<Expr*>(E));
|
|
|
|
return Builder.CreateAddrSpaceCast(Src, ConvertType(DestTy));
|
2009-09-02 04:52:42 +08:00
|
|
|
}
|
2012-01-17 01:27:18 +08:00
|
|
|
case CK_AtomicToNonAtomic:
|
|
|
|
case CK_NonAtomicToAtomic:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_NoOp:
|
|
|
|
case CK_UserDefinedConversion:
|
2009-11-17 05:33:53 +08:00
|
|
|
return Visit(const_cast<Expr*>(E));
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_BaseToDerived: {
|
2012-10-03 09:08:28 +08:00
|
|
|
const CXXRecordDecl *DerivedClassDecl = DestTy->getPointeeCXXRecordDecl();
|
|
|
|
assert(DerivedClassDecl && "BaseToDerived arg isn't a C++ object pointer!");
|
|
|
|
|
2013-02-14 05:18:23 +08:00
|
|
|
llvm::Value *V = Visit(E);
|
|
|
|
|
2013-08-08 09:08:17 +08:00
|
|
|
llvm::Value *Derived =
|
|
|
|
CGF.GetAddressOfDerivedClass(V, DerivedClassDecl,
|
|
|
|
CE->path_begin(), CE->path_end(),
|
|
|
|
ShouldNullCheckClassCastValue(CE));
|
|
|
|
|
2013-02-14 05:18:23 +08:00
|
|
|
// C++11 [expr.static.cast]p11: Behavior is undefined if a downcast is
|
|
|
|
// performed and the object is not of the derived type.
|
|
|
|
if (CGF.SanitizePerformTypeCheck)
|
|
|
|
CGF.EmitTypeCheck(CodeGenFunction::TCK_DowncastPointer, CE->getExprLoc(),
|
2013-08-08 09:08:17 +08:00
|
|
|
Derived, DestTy->getPointeeType());
|
2013-02-14 05:18:23 +08:00
|
|
|
|
2013-08-08 09:08:17 +08:00
|
|
|
return Derived;
|
2009-11-24 01:57:54 +08:00
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_UncheckedDerivedToBase:
|
|
|
|
case CK_DerivedToBase: {
|
2012-10-03 09:08:28 +08:00
|
|
|
const CXXRecordDecl *DerivedClassDecl =
|
|
|
|
E->getType()->getPointeeCXXRecordDecl();
|
|
|
|
assert(DerivedClassDecl && "DerivedToBase arg isn't a C++ object pointer!");
|
2009-09-12 12:57:16 +08:00
|
|
|
|
2013-07-26 14:16:11 +08:00
|
|
|
return CGF.GetAddressOfBaseClass(Visit(E), DerivedClassDecl,
|
2010-08-07 14:22:56 +08:00
|
|
|
CE->path_begin(), CE->path_end(),
|
2010-04-25 05:06:20 +08:00
|
|
|
ShouldNullCheckClassCastValue(CE));
|
2009-09-12 12:57:16 +08:00
|
|
|
}
|
2011-04-11 10:03:26 +08:00
|
|
|
case CK_Dynamic: {
|
2009-11-27 10:07:44 +08:00
|
|
|
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
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ArrayToPointerDecay: {
|
2009-11-17 05:33:53 +08:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2011-07-20 12:31:01 +08:00
|
|
|
// Make sure the array decay ends up being the right type. This matters if
|
|
|
|
// the array type was of an incomplete type.
|
2013-12-11 21:39:46 +08:00
|
|
|
return CGF.Builder.CreatePointerCast(V, ConvertType(CE->getType()));
|
2009-11-17 05:33:53 +08:00
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_FunctionToPointerDecay:
|
2009-11-17 05:33:53 +08:00
|
|
|
return EmitLValue(E).getAddress();
|
|
|
|
|
2010-11-13 09:35:44 +08:00
|
|
|
case CK_NullToPointer:
|
|
|
|
if (MustVisitNullValue(E))
|
|
|
|
(void) Visit(E);
|
|
|
|
|
|
|
|
return llvm::ConstantPointerNull::get(
|
|
|
|
cast<llvm::PointerType>(ConvertType(DestTy)));
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_NullToMemberPointer: {
|
2010-11-13 09:35:44 +08:00
|
|
|
if (MustVisitNullValue(E))
|
2010-08-22 18:59:02 +08:00
|
|
|
(void) Visit(E);
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
const MemberPointerType *MPT = CE->getType()->getAs<MemberPointerType>();
|
|
|
|
return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT);
|
|
|
|
}
|
2009-09-12 12:57:16 +08:00
|
|
|
|
2012-02-15 09:22:51 +08:00
|
|
|
case CK_ReinterpretMemberPointer:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_BaseToDerivedMemberPointer:
|
|
|
|
case CK_DerivedToBaseMemberPointer: {
|
2009-11-27 12:41:50 +08:00
|
|
|
Value *Src = Visit(E);
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-08-22 18:59:02 +08:00
|
|
|
// Note that the AST doesn't distinguish between checked and
|
|
|
|
// unchecked member pointer conversions, so we always have to
|
|
|
|
// implement checked conversions here. This is inefficient when
|
|
|
|
// actual control flow may be required in order to perform the
|
|
|
|
// check, which it is for data member pointers (but not member
|
|
|
|
// function pointers on Itanium and ARM).
|
2010-08-23 09:21:21 +08:00
|
|
|
return CGF.CGM.getCXXABI().EmitMemberPointerConversion(CGF, CE, Src);
|
2009-11-27 12:41:50 +08:00
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-09-10 14:18:15 +08:00
|
|
|
case CK_ARCProduceObject:
|
2011-06-16 07:02:42 +08:00
|
|
|
return CGF.EmitARCRetainScalarExpr(E);
|
2011-09-10 14:18:15 +08:00
|
|
|
case CK_ARCConsumeObject:
|
2011-06-16 07:02:42 +08:00
|
|
|
return CGF.EmitObjCConsumeObject(E->getType(), Visit(E));
|
2011-09-10 14:18:15 +08:00
|
|
|
case CK_ARCReclaimReturnedObject: {
|
2011-07-07 14:58:02 +08:00
|
|
|
llvm::Value *value = Visit(E);
|
|
|
|
value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
|
|
|
|
return CGF.EmitObjCConsumeObject(E->getType(), value);
|
|
|
|
}
|
2011-10-04 14:23:45 +08:00
|
|
|
case CK_ARCExtendBlockObject:
|
|
|
|
return CGF.EmitARCExtendBlockObject(E);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2012-02-22 13:02:47 +08:00
|
|
|
case CK_CopyAndAutoreleaseBlockObject:
|
2012-02-28 09:08:45 +08:00
|
|
|
return CGF.EmitBlockCopyAndAutorelease(Visit(E), E->getType());
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-11-13 17:02:35 +08:00
|
|
|
case CK_FloatingRealToComplex:
|
|
|
|
case CK_FloatingComplexCast:
|
|
|
|
case CK_IntegralRealToComplex:
|
|
|
|
case CK_IntegralComplexCast:
|
2010-11-14 16:17:51 +08:00
|
|
|
case CK_IntegralComplexToFloatingComplex:
|
|
|
|
case CK_FloatingComplexToIntegralComplex:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ConstructorConversion:
|
2010-11-16 14:21:14 +08:00
|
|
|
case CK_ToUnion:
|
|
|
|
llvm_unreachable("scalar cast to non-scalar value");
|
2010-12-04 11:47:34 +08:00
|
|
|
|
2010-12-01 12:43:34 +08:00
|
|
|
case CK_LValueToRValue:
|
|
|
|
assert(CGF.getContext().hasSameUnqualifiedType(E->getType(), DestTy));
|
2010-12-04 11:47:34 +08:00
|
|
|
assert(E->isGLValue() && "lvalue-to-rvalue applied to r-value!");
|
2010-12-01 12:43:34 +08:00
|
|
|
return Visit(const_cast<Expr*>(E));
|
2009-11-27 10:07:44 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_IntegralToPointer: {
|
2009-09-15 12:48:33 +08:00
|
|
|
Value *Src = Visit(const_cast<Expr*>(E));
|
2010-08-25 11:32:38 +08:00
|
|
|
|
2009-10-19 02:12:03 +08:00
|
|
|
// First, convert to the correct width so that we control the kind of
|
|
|
|
// extension.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *MiddleTy = CGF.IntPtrTy;
|
2011-05-21 00:38:50 +08:00
|
|
|
bool InputSigned = E->getType()->isSignedIntegerOrEnumerationType();
|
2009-10-19 02:12:03 +08:00
|
|
|
llvm::Value* IntResult =
|
|
|
|
Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv");
|
2010-08-25 11:32:38 +08:00
|
|
|
|
2009-10-19 02:12:03 +08:00
|
|
|
return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy));
|
2009-09-15 12:48:33 +08:00
|
|
|
}
|
2011-06-25 10:58:47 +08:00
|
|
|
case CK_PointerToIntegral:
|
|
|
|
assert(!DestTy->isBooleanType() && "bool should use PointerToBool");
|
|
|
|
return Builder.CreatePtrToInt(Visit(E), ConvertType(DestTy));
|
2010-08-25 11:32:38 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ToVoid: {
|
2010-12-05 10:00:02 +08:00
|
|
|
CGF.EmitIgnoredExpr(E);
|
2009-11-17 05:33:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_VectorSplat: {
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *DstTy = ConvertType(DestTy);
|
2009-11-17 05:33:53 +08:00
|
|
|
Value *Elt = Visit(const_cast<Expr*>(E));
|
2012-02-06 13:05:50 +08:00
|
|
|
Elt = EmitScalarConversion(Elt, E->getType(),
|
|
|
|
DestTy->getAs<VectorType>()->getElementType());
|
2009-11-17 05:33:53 +08:00
|
|
|
|
|
|
|
// Splat the element across to all elements
|
|
|
|
unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements();
|
2013-01-02 04:08:10 +08:00
|
|
|
return Builder.CreateVectorSplat(NumElements, Elt, "splat");;
|
2009-11-17 05:33:53 +08:00
|
|
|
}
|
2010-11-15 17:13:47 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_IntegralCast:
|
|
|
|
case CK_IntegralToFloating:
|
|
|
|
case CK_FloatingToIntegral:
|
|
|
|
case CK_FloatingCast:
|
2009-11-27 12:41:50 +08:00
|
|
|
return EmitScalarConversion(Visit(E), E->getType(), DestTy);
|
2010-11-15 17:13:47 +08:00
|
|
|
case CK_IntegralToBoolean:
|
|
|
|
return EmitIntToBoolConversion(Visit(E));
|
|
|
|
case CK_PointerToBoolean:
|
|
|
|
return EmitPointerToBoolConversion(Visit(E));
|
|
|
|
case CK_FloatingToBoolean:
|
|
|
|
return EmitFloatToBoolConversion(Visit(E));
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_MemberPointerToBoolean: {
|
2010-08-23 09:21:21 +08:00
|
|
|
llvm::Value *MemPtr = Visit(E);
|
|
|
|
const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
|
|
|
|
return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT);
|
2009-08-25 02:26:39 +08:00
|
|
|
}
|
2010-11-14 16:17:51 +08:00
|
|
|
|
|
|
|
case CK_FloatingComplexToReal:
|
|
|
|
case CK_IntegralComplexToReal:
|
2010-11-16 18:08:07 +08:00
|
|
|
return CGF.EmitComplexExpr(E, false, true).first;
|
2010-11-14 16:17:51 +08:00
|
|
|
|
|
|
|
case CK_FloatingComplexToBoolean:
|
|
|
|
case CK_IntegralComplexToBoolean: {
|
2010-11-16 18:08:07 +08:00
|
|
|
CodeGenFunction::ComplexPairTy V = CGF.EmitComplexExpr(E);
|
2010-11-14 16:17:51 +08:00
|
|
|
|
|
|
|
// TODO: kill this function off, inline appropriate case here
|
|
|
|
return EmitComplexToScalarConversion(V, E->getType(), DestTy);
|
|
|
|
}
|
|
|
|
|
2013-01-20 20:31:11 +08:00
|
|
|
case CK_ZeroToOCLEvent: {
|
2013-12-05 12:47:09 +08:00
|
|
|
assert(DestTy->isEventT() && "CK_ZeroToOCLEvent cast on non-event type");
|
2013-01-20 20:31:11 +08:00
|
|
|
return llvm::Constant::getNullValue(ConvertType(DestTy));
|
|
|
|
}
|
|
|
|
|
2010-08-23 09:21:21 +08:00
|
|
|
}
|
2007-08-26 15:16:41 +08:00
|
|
|
|
2010-11-16 14:21:14 +08:00
|
|
|
llvm_unreachable("unknown scalar cast");
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
2007-09-01 06:09:40 +08:00
|
|
|
Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) {
|
2011-01-26 12:00:11 +08:00
|
|
|
CodeGenFunction::StmtExprEvaluation eval(CGF);
|
2013-06-11 06:04:49 +08:00
|
|
|
llvm::Value *RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(),
|
|
|
|
!E->getType()->isVoidType());
|
|
|
|
if (!RetAlloca)
|
|
|
|
return 0;
|
2013-10-02 10:29:49 +08:00
|
|
|
return CGF.EmitLoadOfScalar(CGF.MakeAddrLValue(RetAlloca, E->getType()),
|
|
|
|
E->getExprLoc());
|
2007-09-01 06:09:40 +08:00
|
|
|
}
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Unary Operators
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-07 10:17:30 +08:00
|
|
|
llvm::Value *ScalarExprEmitter::
|
|
|
|
EmitAddConsiderOverflowBehavior(const UnaryOperator *E,
|
|
|
|
llvm::Value *InVal,
|
|
|
|
llvm::Value *NextVal, bool IsInc) {
|
2012-11-02 06:30:59 +08:00
|
|
|
switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
|
2011-02-07 10:17:30 +08:00
|
|
|
case LangOptions::SOB_Defined:
|
|
|
|
return Builder.CreateAdd(InVal, NextVal, IsInc ? "inc" : "dec");
|
2012-08-25 08:32:28 +08:00
|
|
|
case LangOptions::SOB_Undefined:
|
2013-01-18 19:30:38 +08:00
|
|
|
if (!CGF.SanOpts->SignedIntegerOverflow)
|
2012-08-25 08:32:28 +08:00
|
|
|
return Builder.CreateNSWAdd(InVal, NextVal, IsInc ? "inc" : "dec");
|
|
|
|
// Fall through.
|
2011-02-07 10:17:30 +08:00
|
|
|
case LangOptions::SOB_Trapping:
|
|
|
|
BinOpInfo BinOp;
|
|
|
|
BinOp.LHS = InVal;
|
|
|
|
BinOp.RHS = NextVal;
|
|
|
|
BinOp.Ty = E->getType();
|
|
|
|
BinOp.Opcode = BO_Add;
|
2012-10-04 04:58:04 +08:00
|
|
|
BinOp.FPContractable = false;
|
2011-02-07 10:17:30 +08:00
|
|
|
BinOp.E = E;
|
|
|
|
return EmitOverflowCheckedBinOp(BinOp);
|
|
|
|
}
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unknown SignedOverflowBehaviorTy");
|
2011-02-07 10:17:30 +08:00
|
|
|
}
|
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
llvm::Value *
|
|
|
|
ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
|
|
|
|
bool isInc, bool isPre) {
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
QualType type = E->getSubExpr()->getType();
|
2012-01-17 01:27:18 +08:00
|
|
|
llvm::PHINode *atomicPHI = 0;
|
2013-03-04 00:02:42 +08:00
|
|
|
llvm::Value *value;
|
|
|
|
llvm::Value *input;
|
2011-02-15 17:22:45 +08:00
|
|
|
|
|
|
|
int amount = (isInc ? 1 : -1);
|
|
|
|
|
2012-01-17 01:27:18 +08:00
|
|
|
if (const AtomicType *atomicTy = type->getAs<AtomicType>()) {
|
2013-03-04 00:02:42 +08:00
|
|
|
type = atomicTy->getValueType();
|
|
|
|
if (isInc && type->isBooleanType()) {
|
|
|
|
llvm::Value *True = CGF.EmitToMemory(Builder.getTrue(), type);
|
|
|
|
if (isPre) {
|
|
|
|
Builder.Insert(new llvm::StoreInst(True,
|
|
|
|
LV.getAddress(), LV.isVolatileQualified(),
|
|
|
|
LV.getAlignment().getQuantity(),
|
|
|
|
llvm::SequentiallyConsistent));
|
|
|
|
return Builder.getTrue();
|
|
|
|
}
|
|
|
|
// For atomic bool increment, we just store true and return it for
|
|
|
|
// preincrement, do an atomic swap with true for postincrement
|
|
|
|
return Builder.CreateAtomicRMW(llvm::AtomicRMWInst::Xchg,
|
|
|
|
LV.getAddress(), True, llvm::SequentiallyConsistent);
|
|
|
|
}
|
|
|
|
// Special case for atomic increment / decrement on integers, emit
|
|
|
|
// atomicrmw instructions. We skip this if we want to be doing overflow
|
2013-07-26 14:16:11 +08:00
|
|
|
// checking, and fall into the slow path with the atomic cmpxchg loop.
|
2013-03-04 00:02:42 +08:00
|
|
|
if (!type->isBooleanType() && type->isIntegerType() &&
|
|
|
|
!(type->isUnsignedIntegerType() &&
|
|
|
|
CGF.SanOpts->UnsignedIntegerOverflow) &&
|
|
|
|
CGF.getLangOpts().getSignedOverflowBehavior() !=
|
|
|
|
LangOptions::SOB_Trapping) {
|
|
|
|
llvm::AtomicRMWInst::BinOp aop = isInc ? llvm::AtomicRMWInst::Add :
|
|
|
|
llvm::AtomicRMWInst::Sub;
|
|
|
|
llvm::Instruction::BinaryOps op = isInc ? llvm::Instruction::Add :
|
|
|
|
llvm::Instruction::Sub;
|
|
|
|
llvm::Value *amt = CGF.EmitToMemory(
|
|
|
|
llvm::ConstantInt::get(ConvertType(type), 1, true), type);
|
|
|
|
llvm::Value *old = Builder.CreateAtomicRMW(aop,
|
|
|
|
LV.getAddress(), amt, llvm::SequentiallyConsistent);
|
|
|
|
return isPre ? Builder.CreateBinOp(op, old, amt) : old;
|
|
|
|
}
|
2013-10-02 10:29:49 +08:00
|
|
|
value = EmitLoadOfLValue(LV, E->getExprLoc());
|
2013-03-04 00:02:42 +08:00
|
|
|
input = value;
|
|
|
|
// For every other atomic operation, we need to emit a load-op-cmpxchg loop
|
2012-01-17 01:27:18 +08:00
|
|
|
llvm::BasicBlock *startBB = Builder.GetInsertBlock();
|
|
|
|
llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn);
|
2013-03-04 00:02:42 +08:00
|
|
|
value = CGF.EmitToMemory(value, type);
|
2012-01-17 01:27:18 +08:00
|
|
|
Builder.CreateBr(opBB);
|
|
|
|
Builder.SetInsertPoint(opBB);
|
|
|
|
atomicPHI = Builder.CreatePHI(value->getType(), 2);
|
|
|
|
atomicPHI->addIncoming(value, startBB);
|
|
|
|
value = atomicPHI;
|
2013-03-04 00:02:42 +08:00
|
|
|
} else {
|
2013-10-02 10:29:49 +08:00
|
|
|
value = EmitLoadOfLValue(LV, E->getExprLoc());
|
2013-03-04 00:02:42 +08:00
|
|
|
input = value;
|
2012-01-17 01:27:18 +08:00
|
|
|
}
|
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
// Special case of integer increment that we have to check first: bool++.
|
|
|
|
// Due to promotion rules, we get:
|
|
|
|
// bool++ -> bool = bool + 1
|
|
|
|
// -> bool = (int)bool + 1
|
|
|
|
// -> bool = ((int)bool + 1 != 0)
|
|
|
|
// An interesting aspect of this is that increment is always true.
|
|
|
|
// Decrement does not have this property.
|
|
|
|
if (isInc && type->isBooleanType()) {
|
|
|
|
value = Builder.getTrue();
|
|
|
|
|
|
|
|
// Most common case by far: integer increment.
|
|
|
|
} else if (type->isIntegerType()) {
|
|
|
|
|
2012-08-29 00:55:13 +08:00
|
|
|
llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount, true);
|
2011-02-15 17:22:45 +08:00
|
|
|
|
2011-03-02 09:49:12 +08:00
|
|
|
// Note that signed integer inc/dec with width less than int can't
|
|
|
|
// overflow because of promotion rules; we're just eliding a few steps here.
|
2012-11-27 23:01:55 +08:00
|
|
|
if (value->getType()->getPrimitiveSizeInBits() >=
|
|
|
|
CGF.IntTy->getBitWidth() &&
|
|
|
|
type->isSignedIntegerOrEnumerationType()) {
|
2011-02-15 17:22:45 +08:00
|
|
|
value = EmitAddConsiderOverflowBehavior(E, value, amt, isInc);
|
2012-11-27 23:01:55 +08:00
|
|
|
} else if (value->getType()->getPrimitiveSizeInBits() >=
|
2013-01-18 19:30:38 +08:00
|
|
|
CGF.IntTy->getBitWidth() && type->isUnsignedIntegerType() &&
|
|
|
|
CGF.SanOpts->UnsignedIntegerOverflow) {
|
2012-11-27 23:01:55 +08:00
|
|
|
BinOpInfo BinOp;
|
|
|
|
BinOp.LHS = value;
|
|
|
|
BinOp.RHS = llvm::ConstantInt::get(value->getType(), 1, false);
|
|
|
|
BinOp.Ty = E->getType();
|
|
|
|
BinOp.Opcode = isInc ? BO_Add : BO_Sub;
|
|
|
|
BinOp.FPContractable = false;
|
|
|
|
BinOp.E = E;
|
|
|
|
value = EmitOverflowCheckedBinOp(BinOp);
|
|
|
|
} else
|
2011-02-15 17:22:45 +08:00
|
|
|
value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
// Next most common: pointer increment.
|
|
|
|
} else if (const PointerType *ptr = type->getAs<PointerType>()) {
|
|
|
|
QualType type = ptr->getPointeeType();
|
|
|
|
|
|
|
|
// VLA types don't have constant size.
|
2011-06-25 09:32:37 +08:00
|
|
|
if (const VariableArrayType *vla
|
|
|
|
= CGF.getContext().getAsVariableArrayType(type)) {
|
|
|
|
llvm::Value *numElts = CGF.getVLASize(vla).first;
|
2011-06-25 05:55:10 +08:00
|
|
|
if (!isInc) numElts = Builder.CreateNSWNeg(numElts, "vla.negsize");
|
2012-11-02 06:30:59 +08:00
|
|
|
if (CGF.getLangOpts().isSignedOverflowDefined())
|
2011-06-25 05:55:10 +08:00
|
|
|
value = Builder.CreateGEP(value, numElts, "vla.inc");
|
2011-03-01 08:03:48 +08:00
|
|
|
else
|
2011-06-25 05:55:10 +08:00
|
|
|
value = Builder.CreateInBoundsGEP(value, numElts, "vla.inc");
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
// Arithmetic on function pointers (!) is just +-1.
|
|
|
|
} else if (type->isFunctionType()) {
|
2011-04-20 06:55:03 +08:00
|
|
|
llvm::Value *amt = Builder.getInt32(amount);
|
2011-02-15 17:22:45 +08:00
|
|
|
|
|
|
|
value = CGF.EmitCastToVoidPtr(value);
|
2012-11-02 06:30:59 +08:00
|
|
|
if (CGF.getLangOpts().isSignedOverflowDefined())
|
2011-03-01 08:03:48 +08:00
|
|
|
value = Builder.CreateGEP(value, amt, "incdec.funcptr");
|
|
|
|
else
|
|
|
|
value = Builder.CreateInBoundsGEP(value, amt, "incdec.funcptr");
|
2011-02-15 17:22:45 +08:00
|
|
|
value = Builder.CreateBitCast(value, input->getType());
|
|
|
|
|
|
|
|
// For everything else, we can just do a simple increment.
|
2010-06-27 06:09:34 +08:00
|
|
|
} else {
|
2011-04-20 06:55:03 +08:00
|
|
|
llvm::Value *amt = Builder.getInt32(amount);
|
2012-11-02 06:30:59 +08:00
|
|
|
if (CGF.getLangOpts().isSignedOverflowDefined())
|
2011-03-01 08:03:48 +08:00
|
|
|
value = Builder.CreateGEP(value, amt, "incdec.ptr");
|
|
|
|
else
|
|
|
|
value = Builder.CreateInBoundsGEP(value, amt, "incdec.ptr");
|
2010-06-27 06:09:34 +08:00
|
|
|
}
|
2011-02-15 17:22:45 +08:00
|
|
|
|
|
|
|
// Vector increment/decrement.
|
|
|
|
} else if (type->isVectorType()) {
|
|
|
|
if (type->hasIntegerRepresentation()) {
|
|
|
|
llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount);
|
|
|
|
|
2011-05-07 02:04:18 +08:00
|
|
|
value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
|
2011-02-07 10:17:30 +08:00
|
|
|
} else {
|
2011-02-15 17:22:45 +08:00
|
|
|
value = Builder.CreateFAdd(
|
|
|
|
value,
|
|
|
|
llvm::ConstantFP::get(value->getType(), amount),
|
2011-02-07 10:17:30 +08:00
|
|
|
isInc ? "inc" : "dec");
|
|
|
|
}
|
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
// Floating point.
|
|
|
|
} else if (type->isRealFloatingType()) {
|
2010-06-27 06:09:34 +08:00
|
|
|
// Add the inc/dec to the real part.
|
2011-02-15 17:22:45 +08:00
|
|
|
llvm::Value *amt;
|
2011-10-15 07:23:15 +08:00
|
|
|
|
2013-01-23 19:56:20 +08:00
|
|
|
if (type->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) {
|
2011-10-15 07:23:15 +08:00
|
|
|
// Another special case: half FP increment should be done via float
|
|
|
|
value =
|
|
|
|
Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16),
|
|
|
|
input);
|
|
|
|
}
|
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
if (value->getType()->isFloatTy())
|
|
|
|
amt = llvm::ConstantFP::get(VMContext,
|
|
|
|
llvm::APFloat(static_cast<float>(amount)));
|
|
|
|
else if (value->getType()->isDoubleTy())
|
|
|
|
amt = llvm::ConstantFP::get(VMContext,
|
|
|
|
llvm::APFloat(static_cast<double>(amount)));
|
2010-06-27 06:09:34 +08:00
|
|
|
else {
|
2011-02-15 17:22:45 +08:00
|
|
|
llvm::APFloat F(static_cast<float>(amount));
|
2010-06-27 06:09:34 +08:00
|
|
|
bool ignored;
|
2013-04-17 06:48:15 +08:00
|
|
|
F.convert(CGF.getTarget().getLongDoubleFormat(),
|
|
|
|
llvm::APFloat::rmTowardZero, &ignored);
|
2011-02-15 17:22:45 +08:00
|
|
|
amt = llvm::ConstantFP::get(VMContext, F);
|
2010-06-27 06:09:34 +08:00
|
|
|
}
|
2011-02-15 17:22:45 +08:00
|
|
|
value = Builder.CreateFAdd(value, amt, isInc ? "inc" : "dec");
|
|
|
|
|
2013-01-23 19:56:20 +08:00
|
|
|
if (type->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType)
|
2011-10-15 07:23:15 +08:00
|
|
|
value =
|
|
|
|
Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16),
|
|
|
|
value);
|
|
|
|
|
2011-02-15 17:22:45 +08:00
|
|
|
// Objective-C pointer types.
|
|
|
|
} else {
|
|
|
|
const ObjCObjectPointerType *OPT = type->castAs<ObjCObjectPointerType>();
|
|
|
|
value = CGF.EmitCastToVoidPtr(value);
|
|
|
|
|
|
|
|
CharUnits size = CGF.getContext().getTypeSizeInChars(OPT->getObjectType());
|
|
|
|
if (!isInc) size = -size;
|
|
|
|
llvm::Value *sizeValue =
|
|
|
|
llvm::ConstantInt::get(CGF.SizeTy, size.getQuantity());
|
|
|
|
|
2012-11-02 06:30:59 +08:00
|
|
|
if (CGF.getLangOpts().isSignedOverflowDefined())
|
2011-03-01 08:03:48 +08:00
|
|
|
value = Builder.CreateGEP(value, sizeValue, "incdec.objptr");
|
|
|
|
else
|
|
|
|
value = Builder.CreateInBoundsGEP(value, sizeValue, "incdec.objptr");
|
2011-02-15 17:22:45 +08:00
|
|
|
value = Builder.CreateBitCast(value, input->getType());
|
2010-06-27 06:09:34 +08:00
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2012-01-17 01:27:18 +08:00
|
|
|
if (atomicPHI) {
|
|
|
|
llvm::BasicBlock *opBB = Builder.GetInsertBlock();
|
|
|
|
llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn);
|
|
|
|
llvm::Value *old = Builder.CreateAtomicCmpXchg(LV.getAddress(), atomicPHI,
|
2013-03-04 00:02:42 +08:00
|
|
|
CGF.EmitToMemory(value, type), llvm::SequentiallyConsistent);
|
2012-01-17 01:27:18 +08:00
|
|
|
atomicPHI->addIncoming(old, opBB);
|
|
|
|
llvm::Value *success = Builder.CreateICmpEQ(old, atomicPHI);
|
|
|
|
Builder.CreateCondBr(success, contBB, opBB);
|
|
|
|
Builder.SetInsertPoint(contBB);
|
|
|
|
return isPre ? value : input;
|
|
|
|
}
|
2011-10-15 07:23:15 +08:00
|
|
|
|
2010-06-27 06:09:34 +08:00
|
|
|
// Store the updated result through the lvalue.
|
|
|
|
if (LV.isBitField())
|
2011-06-25 10:11:03 +08:00
|
|
|
CGF.EmitStoreThroughBitfieldLValue(RValue::get(value), LV, &value);
|
2010-06-27 06:09:34 +08:00
|
|
|
else
|
2011-06-25 10:11:03 +08:00
|
|
|
CGF.EmitStoreThroughLValue(RValue::get(value), LV);
|
2011-10-15 07:23:15 +08:00
|
|
|
|
2010-06-27 06:09:34 +08:00
|
|
|
// If this is a postinc, return the value read from memory, otherwise use the
|
|
|
|
// updated value.
|
2011-02-15 17:22:45 +08:00
|
|
|
return isPre ? value : input;
|
2010-06-27 06:09:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) {
|
2009-05-29 23:46:01 +08:00
|
|
|
TestAndClearIgnoreResultAssign();
|
2010-06-27 05:48:21 +08:00
|
|
|
// Emit unary minus with EmitSub so we handle overflow cases etc.
|
|
|
|
BinOpInfo BinOp;
|
2010-06-29 01:12:37 +08:00
|
|
|
BinOp.RHS = Visit(E->getSubExpr());
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-06-29 01:12:37 +08:00
|
|
|
if (BinOp.RHS->getType()->isFPOrFPVectorTy())
|
|
|
|
BinOp.LHS = llvm::ConstantFP::getZeroValueForNegation(BinOp.RHS->getType());
|
2013-07-26 14:16:11 +08:00
|
|
|
else
|
2010-06-29 01:12:37 +08:00
|
|
|
BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType());
|
2010-06-27 05:48:21 +08:00
|
|
|
BinOp.Ty = E->getType();
|
2010-08-25 19:45:40 +08:00
|
|
|
BinOp.Opcode = BO_Sub;
|
2012-10-04 04:58:04 +08:00
|
|
|
BinOp.FPContractable = false;
|
2010-06-27 05:48:21 +08:00
|
|
|
BinOp.E = E;
|
|
|
|
return EmitSub(BinOp);
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2012-01-17 05:02:28 +08:00
|
|
|
// Perform vector logical not on comparison with zero vector.
|
|
|
|
if (E->getType()->isExtVectorType()) {
|
|
|
|
Value *Oper = Visit(E->getSubExpr());
|
|
|
|
Value *Zero = llvm::Constant::getNullValue(Oper->getType());
|
2013-02-21 19:49:56 +08:00
|
|
|
Value *Result;
|
|
|
|
if (Oper->getType()->isFPOrFPVectorTy())
|
|
|
|
Result = Builder.CreateFCmp(llvm::CmpInst::FCMP_OEQ, Oper, Zero, "cmp");
|
|
|
|
else
|
|
|
|
Result = Builder.CreateICmp(llvm::CmpInst::ICMP_EQ, Oper, Zero, "cmp");
|
2012-01-17 05:02:28 +08:00
|
|
|
return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2010-08-05 17:58:49 +08:00
|
|
|
Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) {
|
|
|
|
// Try folding the offsetof to a constant.
|
2011-12-29 03:48:30 +08:00
|
|
|
llvm::APSInt Value;
|
|
|
|
if (E->EvaluateAsInt(Value, CGF.getContext()))
|
|
|
|
return Builder.getInt(Value);
|
2010-08-05 17:58:49 +08:00
|
|
|
|
|
|
|
// Loop over the components of the offsetof to compute the value.
|
|
|
|
unsigned n = E->getNumComponents();
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type* ResultType = ConvertType(E->getType());
|
2010-08-05 17:58:49 +08:00
|
|
|
llvm::Value* Result = llvm::Constant::getNullValue(ResultType);
|
|
|
|
QualType CurrentType = E->getTypeSourceInfo()->getType();
|
|
|
|
for (unsigned i = 0; i != n; ++i) {
|
|
|
|
OffsetOfExpr::OffsetOfNode ON = E->getComponent(i);
|
2010-08-07 00:37:05 +08:00
|
|
|
llvm::Value *Offset = 0;
|
2010-08-05 17:58:49 +08:00
|
|
|
switch (ON.getKind()) {
|
|
|
|
case OffsetOfExpr::OffsetOfNode::Array: {
|
|
|
|
// Compute the index
|
|
|
|
Expr *IdxExpr = E->getIndexExpr(ON.getArrayExprIndex());
|
|
|
|
llvm::Value* Idx = CGF.EmitScalarExpr(IdxExpr);
|
2011-05-21 00:38:50 +08:00
|
|
|
bool IdxSigned = IdxExpr->getType()->isSignedIntegerOrEnumerationType();
|
2010-08-05 17:58:49 +08:00
|
|
|
Idx = Builder.CreateIntCast(Idx, ResultType, IdxSigned, "conv");
|
|
|
|
|
|
|
|
// Save the element type
|
|
|
|
CurrentType =
|
|
|
|
CGF.getContext().getAsArrayType(CurrentType)->getElementType();
|
|
|
|
|
|
|
|
// Compute the element size
|
|
|
|
llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType,
|
|
|
|
CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity());
|
|
|
|
|
|
|
|
// Multiply out to compute the result
|
|
|
|
Offset = Builder.CreateMul(Idx, ElemSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OffsetOfExpr::OffsetOfNode::Field: {
|
|
|
|
FieldDecl *MemberDecl = ON.getField();
|
|
|
|
RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl();
|
|
|
|
const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
// Compute the index of the field in its parent.
|
|
|
|
unsigned i = 0;
|
|
|
|
// FIXME: It would be nice if we didn't have to loop here!
|
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
2012-04-30 10:36:29 +08:00
|
|
|
Field != FieldEnd; ++Field, ++i) {
|
2012-06-07 04:45:41 +08:00
|
|
|
if (*Field == MemberDecl)
|
2010-08-05 17:58:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
assert(i < RL.getFieldCount() && "offsetof field in wrong type");
|
|
|
|
|
|
|
|
// Compute the offset to the field
|
|
|
|
int64_t OffsetInt = RL.getFieldOffset(i) /
|
|
|
|
CGF.getContext().getCharWidth();
|
|
|
|
Offset = llvm::ConstantInt::get(ResultType, OffsetInt);
|
|
|
|
|
|
|
|
// Save the element type.
|
|
|
|
CurrentType = MemberDecl->getType();
|
|
|
|
break;
|
|
|
|
}
|
2010-08-07 00:37:05 +08:00
|
|
|
|
2010-08-05 17:58:49 +08:00
|
|
|
case OffsetOfExpr::OffsetOfNode::Identifier:
|
2010-08-06 09:17:25 +08:00
|
|
|
llvm_unreachable("dependent __builtin_offsetof");
|
2010-08-07 00:37:05 +08:00
|
|
|
|
2010-08-05 17:58:49 +08:00
|
|
|
case OffsetOfExpr::OffsetOfNode::Base: {
|
|
|
|
if (ON.getBase()->isVirtual()) {
|
|
|
|
CGF.ErrorUnsupported(E, "virtual base in offsetof");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl();
|
|
|
|
const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
// Save the element type.
|
|
|
|
CurrentType = ON.getBase()->getType();
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-08-05 17:58:49 +08:00
|
|
|
// Compute the offset to the base.
|
|
|
|
const RecordType *BaseRT = CurrentType->getAs<RecordType>();
|
|
|
|
CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl());
|
2012-07-05 02:45:14 +08:00
|
|
|
CharUnits OffsetInt = RL.getBaseClassOffset(BaseRD);
|
|
|
|
Offset = llvm::ConstantInt::get(ResultType, OffsetInt.getQuantity());
|
2010-08-05 17:58:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Result = Builder.CreateAdd(Result, Offset);
|
|
|
|
}
|
|
|
|
return Result;
|
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
|
|
|
}
|
|
|
|
|
2011-03-12 03:24:49 +08:00
|
|
|
/// VisitUnaryExprOrTypeTraitExpr - Return the size or alignment of the type of
|
2008-11-12 01:56:53 +08:00
|
|
|
/// argument of the sizeof expression as an integer.
|
|
|
|
Value *
|
2011-03-12 03:24:49 +08:00
|
|
|
ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
|
|
|
|
const UnaryExprOrTypeTraitExpr *E) {
|
2008-11-12 01:56:53 +08:00
|
|
|
QualType TypeToSize = E->getTypeOfArgument();
|
2011-03-12 03:24:49 +08:00
|
|
|
if (E->getKind() == UETT_SizeOf) {
|
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.
|
2011-06-25 05:55:10 +08:00
|
|
|
CGF.EmitVariablyModifiedType(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.
|
2010-12-05 10:00:02 +08:00
|
|
|
CGF.EmitIgnoredExpr(E->getArgumentExpr());
|
2009-01-25 06:19:05 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-06-25 05:55:10 +08:00
|
|
|
QualType eltType;
|
|
|
|
llvm::Value *numElts;
|
|
|
|
llvm::tie(numElts, eltType) = CGF.getVLASize(VAT);
|
|
|
|
|
|
|
|
llvm::Value *size = numElts;
|
|
|
|
|
|
|
|
// Scale the number of non-VLA elements by the non-VLA element size.
|
|
|
|
CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType);
|
|
|
|
if (!eltSize.isOne())
|
|
|
|
size = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), numElts);
|
|
|
|
|
|
|
|
return size;
|
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.
|
2011-12-29 03:48:30 +08:00
|
|
|
return Builder.getInt(E->EvaluateKnownConstInt(CGF.getContext()));
|
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();
|
2010-11-16 18:08:07 +08:00
|
|
|
if (Op->getType()->isAnyComplexType()) {
|
|
|
|
// If it's an l-value, load through the appropriate subobject l-value.
|
|
|
|
// Note that we have to ask E because Op might be an l-value that
|
|
|
|
// this won't work for, e.g. an Obj-C property.
|
2010-11-24 13:12:34 +08:00
|
|
|
if (E->isGLValue())
|
2013-10-02 10:29:49 +08:00
|
|
|
return CGF.EmitLoadOfLValue(CGF.EmitLValue(E),
|
|
|
|
E->getExprLoc()).getScalarVal();
|
2010-11-16 18:08:07 +08:00
|
|
|
|
|
|
|
// Otherwise, calculate and project.
|
|
|
|
return CGF.EmitComplexExpr(Op, false, true).first;
|
|
|
|
}
|
|
|
|
|
2007-08-25 05:20:17 +08:00
|
|
|
return Visit(Op);
|
|
|
|
}
|
2010-11-16 18:08:07 +08:00
|
|
|
|
2007-08-25 05:20:17 +08:00
|
|
|
Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) {
|
|
|
|
Expr *Op = E->getSubExpr();
|
2010-11-16 18:08:07 +08:00
|
|
|
if (Op->getType()->isAnyComplexType()) {
|
|
|
|
// If it's an l-value, load through the appropriate subobject l-value.
|
|
|
|
// Note that we have to ask E because Op might be an l-value that
|
|
|
|
// this won't work for, e.g. an Obj-C property.
|
2010-11-24 13:12:34 +08:00
|
|
|
if (Op->isGLValue())
|
2013-10-02 10:29:49 +08:00
|
|
|
return CGF.EmitLoadOfLValue(CGF.EmitLValue(E),
|
|
|
|
E->getExprLoc()).getScalarVal();
|
2010-11-16 18:08:07 +08:00
|
|
|
|
|
|
|
// Otherwise, calculate and project.
|
|
|
|
return CGF.EmitComplexExpr(Op, 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.
|
2012-02-19 04:53:32 +08:00
|
|
|
if (Op->isGLValue())
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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();
|
2010-06-27 05:48:21 +08:00
|
|
|
Result.Opcode = E->getOpcode();
|
2012-10-02 12:45:10 +08:00
|
|
|
Result.FPContractable = E->isFPContractable();
|
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 &),
|
2010-06-30 06:00:45 +08:00
|
|
|
Value *&Result) {
|
2009-12-25 23:43:36 +08:00
|
|
|
QualType LHSTy = E->getLHS()->getType();
|
2007-08-25 05:00:35 +08:00
|
|
|
BinOpInfo OpInfo;
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2013-06-12 09:40:06 +08:00
|
|
|
if (E->getComputationResultType()->isAnyComplexType())
|
|
|
|
return CGF.EmitScalarCompooundAssignWithComplex(E, Result);
|
2013-07-26 14:16:11 +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();
|
2010-06-27 05:48:21 +08:00
|
|
|
OpInfo.Opcode = E->getOpcode();
|
2012-10-04 04:58:04 +08:00
|
|
|
OpInfo.FPContractable = false;
|
2009-05-23 03:07:20 +08:00
|
|
|
OpInfo.E = E;
|
2009-03-28 09:22:36 +08:00
|
|
|
// Load/convert the LHS.
|
2012-09-08 10:08:36 +08:00
|
|
|
LValue LHSLV = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
|
2012-01-17 01:27:18 +08:00
|
|
|
|
|
|
|
llvm::PHINode *atomicPHI = 0;
|
2013-03-04 00:02:42 +08:00
|
|
|
if (const AtomicType *atomicTy = LHSTy->getAs<AtomicType>()) {
|
|
|
|
QualType type = atomicTy->getValueType();
|
|
|
|
if (!type->isBooleanType() && type->isIntegerType() &&
|
|
|
|
!(type->isUnsignedIntegerType() &&
|
|
|
|
CGF.SanOpts->UnsignedIntegerOverflow) &&
|
|
|
|
CGF.getLangOpts().getSignedOverflowBehavior() !=
|
|
|
|
LangOptions::SOB_Trapping) {
|
|
|
|
llvm::AtomicRMWInst::BinOp aop = llvm::AtomicRMWInst::BAD_BINOP;
|
|
|
|
switch (OpInfo.Opcode) {
|
|
|
|
// We don't have atomicrmw operands for *, %, /, <<, >>
|
|
|
|
case BO_MulAssign: case BO_DivAssign:
|
|
|
|
case BO_RemAssign:
|
|
|
|
case BO_ShlAssign:
|
|
|
|
case BO_ShrAssign:
|
|
|
|
break;
|
|
|
|
case BO_AddAssign:
|
|
|
|
aop = llvm::AtomicRMWInst::Add;
|
|
|
|
break;
|
|
|
|
case BO_SubAssign:
|
|
|
|
aop = llvm::AtomicRMWInst::Sub;
|
|
|
|
break;
|
|
|
|
case BO_AndAssign:
|
|
|
|
aop = llvm::AtomicRMWInst::And;
|
|
|
|
break;
|
|
|
|
case BO_XorAssign:
|
|
|
|
aop = llvm::AtomicRMWInst::Xor;
|
|
|
|
break;
|
|
|
|
case BO_OrAssign:
|
|
|
|
aop = llvm::AtomicRMWInst::Or;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid compound assignment type");
|
|
|
|
}
|
|
|
|
if (aop != llvm::AtomicRMWInst::BAD_BINOP) {
|
|
|
|
llvm::Value *amt = CGF.EmitToMemory(EmitScalarConversion(OpInfo.RHS,
|
|
|
|
E->getRHS()->getType(), LHSTy), LHSTy);
|
|
|
|
Builder.CreateAtomicRMW(aop, LHSLV.getAddress(), amt,
|
|
|
|
llvm::SequentiallyConsistent);
|
|
|
|
return LHSLV;
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 01:27:18 +08:00
|
|
|
// FIXME: For floating point types, we should be saving and restoring the
|
|
|
|
// floating point environment in the loop.
|
|
|
|
llvm::BasicBlock *startBB = Builder.GetInsertBlock();
|
|
|
|
llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn);
|
2013-10-02 10:29:49 +08:00
|
|
|
OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->getExprLoc());
|
2013-03-04 00:02:42 +08:00
|
|
|
OpInfo.LHS = CGF.EmitToMemory(OpInfo.LHS, type);
|
2012-01-17 01:27:18 +08:00
|
|
|
Builder.CreateBr(opBB);
|
|
|
|
Builder.SetInsertPoint(opBB);
|
|
|
|
atomicPHI = Builder.CreatePHI(OpInfo.LHS->getType(), 2);
|
|
|
|
atomicPHI->addIncoming(OpInfo.LHS, startBB);
|
|
|
|
OpInfo.LHS = atomicPHI;
|
|
|
|
}
|
2013-03-04 00:02:42 +08:00
|
|
|
else
|
2013-10-02 10:29:49 +08:00
|
|
|
OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->getExprLoc());
|
2012-06-16 10:19:17 +08:00
|
|
|
|
|
|
|
OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy,
|
|
|
|
E->getComputationLHSType());
|
|
|
|
|
2007-08-25 05:00:35 +08:00
|
|
|
// Expand the binary operator.
|
2010-06-30 06:00:45 +08:00
|
|
|
Result = (this->*Func)(OpInfo);
|
2013-07-26 14:16:11 +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);
|
2012-01-17 01:27:18 +08:00
|
|
|
|
|
|
|
if (atomicPHI) {
|
|
|
|
llvm::BasicBlock *opBB = Builder.GetInsertBlock();
|
|
|
|
llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn);
|
|
|
|
llvm::Value *old = Builder.CreateAtomicCmpXchg(LHSLV.getAddress(), atomicPHI,
|
2013-03-04 00:02:42 +08:00
|
|
|
CGF.EmitToMemory(Result, LHSTy), llvm::SequentiallyConsistent);
|
2012-01-17 01:27:18 +08:00
|
|
|
atomicPHI->addIncoming(old, opBB);
|
|
|
|
llvm::Value *success = Builder.CreateICmpEQ(old, atomicPHI);
|
|
|
|
Builder.CreateCondBr(success, contBB, opBB);
|
|
|
|
Builder.SetInsertPoint(contBB);
|
|
|
|
return LHSLV;
|
|
|
|
}
|
2013-07-26 14:16:11 +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-06-30 06:00:45 +08:00
|
|
|
if (LHSLV.isBitField())
|
2011-06-25 10:11:03 +08:00
|
|
|
CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, &Result);
|
2010-06-30 06:00:45 +08:00
|
|
|
else
|
2011-06-25 10:11:03 +08:00
|
|
|
CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV);
|
2010-06-30 06:00:45 +08:00
|
|
|
|
2010-04-23 12:16:32 +08:00
|
|
|
return LHSLV;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E,
|
|
|
|
Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) {
|
|
|
|
bool Ignore = TestAndClearIgnoreResultAssign();
|
2010-06-30 06:00:45 +08:00
|
|
|
Value *RHS;
|
|
|
|
LValue LHS = EmitCompoundAssignLValue(E, Func, RHS);
|
|
|
|
|
|
|
|
// If the result is clearly ignored, return now.
|
2009-05-29 23:46:01 +08:00
|
|
|
if (Ignore)
|
|
|
|
return 0;
|
2010-06-30 06:00:45 +08:00
|
|
|
|
2010-11-16 18:08:07 +08:00
|
|
|
// The result of an assignment in C is the assigned r-value.
|
2012-11-02 06:30:59 +08:00
|
|
|
if (!CGF.getLangOpts().CPlusPlus)
|
2010-11-16 18:08:07 +08:00
|
|
|
return RHS;
|
|
|
|
|
2010-06-30 06:00:45 +08:00
|
|
|
// If the lvalue is non-volatile, return the computed value of the assignment.
|
|
|
|
if (!LHS.isVolatileQualified())
|
|
|
|
return RHS;
|
|
|
|
|
|
|
|
// Otherwise, reload the value.
|
2013-10-02 10:29:49 +08:00
|
|
|
return EmitLoadOfLValue(LHS, E->getExprLoc());
|
2007-08-25 05:00:35 +08:00
|
|
|
}
|
|
|
|
|
2010-09-12 05:47:09 +08:00
|
|
|
void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck(
|
2012-09-08 10:08:36 +08:00
|
|
|
const BinOpInfo &Ops, llvm::Value *Zero, bool isDiv) {
|
2012-11-06 10:30:30 +08:00
|
|
|
llvm::Value *Cond = 0;
|
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (CGF.SanOpts->IntegerDivideByZero)
|
2012-11-06 10:30:30 +08:00
|
|
|
Cond = Builder.CreateICmpNE(Ops.RHS, Zero);
|
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (CGF.SanOpts->SignedIntegerOverflow &&
|
2012-11-06 10:30:30 +08:00
|
|
|
Ops.Ty->hasSignedIntegerRepresentation()) {
|
|
|
|
llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType());
|
2010-09-12 05:47:09 +08:00
|
|
|
|
|
|
|
llvm::Value *IntMin =
|
2011-04-20 06:55:03 +08:00
|
|
|
Builder.getInt(llvm::APInt::getSignedMinValue(Ty->getBitWidth()));
|
2010-09-12 05:47:09 +08:00
|
|
|
llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL);
|
|
|
|
|
2012-09-08 10:08:36 +08:00
|
|
|
llvm::Value *LHSCmp = Builder.CreateICmpNE(Ops.LHS, IntMin);
|
|
|
|
llvm::Value *RHSCmp = Builder.CreateICmpNE(Ops.RHS, NegOne);
|
2012-11-06 10:30:30 +08:00
|
|
|
llvm::Value *Overflow = Builder.CreateOr(LHSCmp, RHSCmp, "or");
|
|
|
|
Cond = Cond ? Builder.CreateAnd(Cond, Overflow, "and") : Overflow;
|
2010-09-12 05:47:09 +08:00
|
|
|
}
|
2012-11-06 10:30:30 +08:00
|
|
|
|
|
|
|
if (Cond)
|
|
|
|
EmitBinOpCheck(Cond, Ops);
|
2010-09-12 05:47:09 +08:00
|
|
|
}
|
2007-08-25 05:00:35 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) {
|
2013-01-18 19:30:38 +08:00
|
|
|
if ((CGF.SanOpts->IntegerDivideByZero ||
|
|
|
|
CGF.SanOpts->SignedIntegerOverflow) &&
|
2012-11-27 23:01:55 +08:00
|
|
|
Ops.Ty->isIntegerType()) {
|
2010-09-12 05:47:09 +08:00
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
|
2012-11-27 23:01:55 +08:00
|
|
|
EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, true);
|
2013-01-18 19:30:38 +08:00
|
|
|
} else if (CGF.SanOpts->FloatDivideByZero &&
|
2012-11-27 23:01:55 +08:00
|
|
|
Ops.Ty->isRealFloatingType()) {
|
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
|
|
|
|
EmitBinOpCheck(Builder.CreateFCmpUNE(Ops.RHS, Zero), Ops);
|
2010-09-12 05:47:09 +08:00
|
|
|
}
|
2012-11-27 23:01:55 +08:00
|
|
|
|
2011-10-28 03:19:51 +08:00
|
|
|
if (Ops.LHS->getType()->isFPOrFPVectorTy()) {
|
|
|
|
llvm::Value *Val = Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div");
|
2012-11-02 06:30:59 +08:00
|
|
|
if (CGF.getLangOpts().OpenCL) {
|
2011-10-28 03:19:51 +08:00
|
|
|
// OpenCL 1.1 7.4: minimum accuracy of single precision / is 2.5ulp
|
|
|
|
llvm::Type *ValTy = Val->getType();
|
|
|
|
if (ValTy->isFloatTy() ||
|
|
|
|
(isa<llvm::VectorType>(ValTy) &&
|
|
|
|
cast<llvm::VectorType>(ValTy)->getElementType()->isFloatTy()))
|
2012-04-10 16:23:07 +08:00
|
|
|
CGF.SetFPAccuracy(Val, 2.5);
|
2011-10-28 03:19:51 +08:00
|
|
|
}
|
|
|
|
return Val;
|
|
|
|
}
|
2010-07-23 23:58:24 +08:00
|
|
|
else if (Ops.Ty->hasUnsignedIntegerRepresentation())
|
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.
|
2013-01-18 19:30:38 +08:00
|
|
|
if (CGF.SanOpts->IntegerDivideByZero) {
|
2010-09-12 05:47:09 +08:00
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty));
|
|
|
|
|
2012-11-27 23:01:55 +08:00
|
|
|
if (Ops.Ty->isIntegerType())
|
2010-09-12 05:47:09 +08:00
|
|
|
EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false);
|
|
|
|
}
|
|
|
|
|
2011-04-10 12:44:11 +08:00
|
|
|
if (Ops.Ty->hasUnsignedIntegerRepresentation())
|
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
|
|
|
|
2012-11-27 23:01:55 +08:00
|
|
|
bool isSigned = Ops.Ty->isSignedIntegerOrEnumerationType();
|
2010-06-27 05:48:21 +08:00
|
|
|
switch (Ops.Opcode) {
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Add:
|
|
|
|
case BO_AddAssign:
|
2009-04-03 02:15:54 +08:00
|
|
|
OpID = 1;
|
2012-11-27 23:01:55 +08:00
|
|
|
IID = isSigned ? llvm::Intrinsic::sadd_with_overflow :
|
|
|
|
llvm::Intrinsic::uadd_with_overflow;
|
2009-04-03 02:15:54 +08:00
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Sub:
|
|
|
|
case BO_SubAssign:
|
2009-04-03 02:15:54 +08:00
|
|
|
OpID = 2;
|
2012-11-27 23:01:55 +08:00
|
|
|
IID = isSigned ? llvm::Intrinsic::ssub_with_overflow :
|
|
|
|
llvm::Intrinsic::usub_with_overflow;
|
2009-04-03 02:15:54 +08:00
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Mul:
|
|
|
|
case BO_MulAssign:
|
2009-04-03 02:15:54 +08:00
|
|
|
OpID = 3;
|
2012-11-27 23:01:55 +08:00
|
|
|
IID = isSigned ? llvm::Intrinsic::smul_with_overflow :
|
|
|
|
llvm::Intrinsic::umul_with_overflow;
|
2009-04-03 02:15:54 +08:00
|
|
|
break;
|
|
|
|
default:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Unsupported operation for overflow detection");
|
2009-04-03 02:15:54 +08:00
|
|
|
}
|
|
|
|
OpID <<= 1;
|
2012-11-27 23:01:55 +08:00
|
|
|
if (isSigned)
|
|
|
|
OpID |= 1;
|
2009-04-03 02:15:54 +08:00
|
|
|
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty);
|
2009-04-02 04:28:16 +08:00
|
|
|
|
2011-07-15 01:45:50 +08:00
|
|
|
llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, opTy);
|
2009-04-02 04:28:16 +08:00
|
|
|
|
|
|
|
Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS);
|
|
|
|
Value *result = Builder.CreateExtractValue(resultAndOverflow, 0);
|
|
|
|
Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1);
|
|
|
|
|
2012-09-08 10:08:36 +08:00
|
|
|
// Handle overflow with llvm.trap if no custom handler has been specified.
|
|
|
|
const std::string *handlerName =
|
2012-11-02 06:30:59 +08:00
|
|
|
&CGF.getLangOpts().OverflowHandler;
|
2012-09-08 10:08:36 +08:00
|
|
|
if (handlerName->empty()) {
|
2012-11-06 06:21:05 +08:00
|
|
|
// If the signed-integer-overflow sanitizer is enabled, emit a call to its
|
2012-11-02 06:15:34 +08:00
|
|
|
// runtime. Otherwise, this is a -ftrapv check, so just emit a trap.
|
2013-01-18 19:30:38 +08:00
|
|
|
if (!isSigned || CGF.SanOpts->SignedIntegerOverflow)
|
2012-11-02 06:15:34 +08:00
|
|
|
EmitBinOpCheck(Builder.CreateNot(overflow), Ops);
|
|
|
|
else
|
2013-01-30 07:31:22 +08:00
|
|
|
CGF.EmitTrapCheck(Builder.CreateNot(overflow));
|
2012-09-08 10:08:36 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-04-02 04:28:16 +08:00
|
|
|
// Branch in case of overflow.
|
2010-09-18 02:29:54 +08:00
|
|
|
llvm::BasicBlock *initialBB = Builder.GetInsertBlock();
|
2011-07-08 05:13:10 +08:00
|
|
|
llvm::Function::iterator insertPt = initialBB;
|
|
|
|
llvm::BasicBlock *continueBB = CGF.createBasicBlock("nooverflow", CGF.CurFn,
|
|
|
|
llvm::next(insertPt));
|
2010-08-07 08:20:46 +08:00
|
|
|
llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn);
|
2009-04-02 04:28:16 +08:00
|
|
|
|
|
|
|
Builder.CreateCondBr(overflow, overflowBB, continueBB);
|
|
|
|
|
2010-09-18 02:29:54 +08:00
|
|
|
// If an overflow handler is set, then we want to call it and then use its
|
|
|
|
// result, if it returns.
|
|
|
|
Builder.SetInsertPoint(overflowBB);
|
|
|
|
|
|
|
|
// Get the overflow handler.
|
2012-02-07 08:39:47 +08:00
|
|
|
llvm::Type *Int8Ty = CGF.Int8Ty;
|
2011-07-10 01:41:47 +08:00
|
|
|
llvm::Type *argTypes[] = { CGF.Int64Ty, CGF.Int64Ty, Int8Ty, Int8Ty };
|
2010-09-18 02:29:54 +08:00
|
|
|
llvm::FunctionType *handlerTy =
|
|
|
|
llvm::FunctionType::get(CGF.Int64Ty, argTypes, true);
|
|
|
|
llvm::Value *handler = CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName);
|
|
|
|
|
|
|
|
// Sign extend the args to 64-bit, so that we can use the same handler for
|
|
|
|
// all types of overflow.
|
|
|
|
llvm::Value *lhs = Builder.CreateSExt(Ops.LHS, CGF.Int64Ty);
|
|
|
|
llvm::Value *rhs = Builder.CreateSExt(Ops.RHS, CGF.Int64Ty);
|
|
|
|
|
|
|
|
// Call the handler with the two arguments, the operation, and the size of
|
|
|
|
// the result.
|
2013-03-01 03:01:20 +08:00
|
|
|
llvm::Value *handlerArgs[] = {
|
|
|
|
lhs,
|
|
|
|
rhs,
|
|
|
|
Builder.getInt8(OpID),
|
|
|
|
Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth())
|
|
|
|
};
|
|
|
|
llvm::Value *handlerResult =
|
|
|
|
CGF.EmitNounwindRuntimeCall(handler, handlerArgs);
|
2010-09-18 02:29:54 +08:00
|
|
|
|
|
|
|
// Truncate the result back to the desired size.
|
|
|
|
handlerResult = Builder.CreateTrunc(handlerResult, opTy);
|
|
|
|
Builder.CreateBr(continueBB);
|
|
|
|
|
2009-04-02 04:28:16 +08:00
|
|
|
Builder.SetInsertPoint(continueBB);
|
2011-03-30 19:28:58 +08:00
|
|
|
llvm::PHINode *phi = Builder.CreatePHI(opTy, 2);
|
2010-09-18 02:29:54 +08:00
|
|
|
phi->addIncoming(result, initialBB);
|
|
|
|
phi->addIncoming(handlerResult, overflowBB);
|
|
|
|
|
|
|
|
return phi;
|
2009-04-02 04:28:16 +08:00
|
|
|
}
|
2007-08-24 13:35:26 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
/// Emit pointer + index arithmetic.
|
|
|
|
static Value *emitPointerArithmetic(CodeGenFunction &CGF,
|
|
|
|
const BinOpInfo &op,
|
|
|
|
bool isSubtraction) {
|
|
|
|
// Must have binary (not unary) expr here. Unary pointer
|
|
|
|
// increment/decrement doesn't use this path.
|
|
|
|
const BinaryOperator *expr = cast<BinaryOperator>(op.E);
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
Value *pointer = op.LHS;
|
|
|
|
Expr *pointerOperand = expr->getLHS();
|
|
|
|
Value *index = op.RHS;
|
|
|
|
Expr *indexOperand = expr->getRHS();
|
|
|
|
|
|
|
|
// In a subtraction, the LHS is always the pointer.
|
|
|
|
if (!isSubtraction && !pointer->getType()->isPointerTy()) {
|
|
|
|
std::swap(pointer, index);
|
|
|
|
std::swap(pointerOperand, indexOperand);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned width = cast<llvm::IntegerType>(index->getType())->getBitWidth();
|
|
|
|
if (width != CGF.PointerWidthInBits) {
|
|
|
|
// Zero-extend or sign-extend the pointer value according to
|
|
|
|
// whether the index is signed or not.
|
|
|
|
bool isSigned = indexOperand->getType()->isSignedIntegerOrEnumerationType();
|
|
|
|
index = CGF.Builder.CreateIntCast(index, CGF.PtrDiffTy, isSigned,
|
|
|
|
"idx.ext");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is subtraction, negate the index.
|
|
|
|
if (isSubtraction)
|
|
|
|
index = CGF.Builder.CreateNeg(index, "idx.neg");
|
|
|
|
|
2013-10-23 06:51:04 +08:00
|
|
|
if (CGF.SanOpts->ArrayBounds)
|
2013-02-23 10:53:19 +08:00
|
|
|
CGF.EmitBoundsCheck(op.E, pointerOperand, index, indexOperand->getType(),
|
|
|
|
/*Accessed*/ false);
|
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
const PointerType *pointerType
|
|
|
|
= pointerOperand->getType()->getAs<PointerType>();
|
|
|
|
if (!pointerType) {
|
|
|
|
QualType objectType = pointerOperand->getType()
|
|
|
|
->castAs<ObjCObjectPointerType>()
|
|
|
|
->getPointeeType();
|
|
|
|
llvm::Value *objectSize
|
|
|
|
= CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(objectType));
|
|
|
|
|
|
|
|
index = CGF.Builder.CreateMul(index, objectSize);
|
|
|
|
|
|
|
|
Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy);
|
|
|
|
result = CGF.Builder.CreateGEP(result, index, "add.ptr");
|
|
|
|
return CGF.Builder.CreateBitCast(result, pointer->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType elementType = pointerType->getPointeeType();
|
|
|
|
if (const VariableArrayType *vla
|
|
|
|
= CGF.getContext().getAsVariableArrayType(elementType)) {
|
|
|
|
// The element count here is the total number of non-VLA elements.
|
|
|
|
llvm::Value *numElements = CGF.getVLASize(vla).first;
|
|
|
|
|
|
|
|
// Effectively, the multiply by the VLA size is part of the GEP.
|
|
|
|
// GEP indexes are signed, and scaling an index isn't permitted to
|
|
|
|
// signed-overflow, so we use the same semantics for our explicit
|
|
|
|
// multiply. We suppress this if overflow is not undefined behavior.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (CGF.getLangOpts().isSignedOverflowDefined()) {
|
2011-06-25 09:32:37 +08:00
|
|
|
index = CGF.Builder.CreateMul(index, numElements, "vla.index");
|
|
|
|
pointer = CGF.Builder.CreateGEP(pointer, index, "add.ptr");
|
|
|
|
} else {
|
|
|
|
index = CGF.Builder.CreateNSWMul(index, numElements, "vla.index");
|
|
|
|
pointer = CGF.Builder.CreateInBoundsGEP(pointer, index, "add.ptr");
|
|
|
|
}
|
|
|
|
return pointer;
|
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.
|
2011-06-25 09:32:37 +08:00
|
|
|
if (elementType->isVoidType() || elementType->isFunctionType()) {
|
|
|
|
Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy);
|
|
|
|
result = CGF.Builder.CreateGEP(result, index, "add.ptr");
|
|
|
|
return CGF.Builder.CreateBitCast(result, pointer->getType());
|
2009-09-09 21:00:44 +08:00
|
|
|
}
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (CGF.getLangOpts().isSignedOverflowDefined())
|
2011-06-25 09:32:37 +08:00
|
|
|
return CGF.Builder.CreateGEP(pointer, index, "add.ptr");
|
|
|
|
|
|
|
|
return CGF.Builder.CreateInBoundsGEP(pointer, index, "add.ptr");
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
2012-10-02 12:45:10 +08:00
|
|
|
// Construct an fmuladd intrinsic to represent a fused mul-add of MulOp and
|
|
|
|
// Addend. Use negMul and negAdd to negate the first operand of the Mul or
|
|
|
|
// the add operand respectively. This allows fmuladd to represent a*b-c, or
|
|
|
|
// c-a*b. Patterns in LLVM should catch the negated forms and translate them to
|
|
|
|
// efficient operations.
|
|
|
|
static Value* buildFMulAdd(llvm::BinaryOperator *MulOp, Value *Addend,
|
|
|
|
const CodeGenFunction &CGF, CGBuilderTy &Builder,
|
|
|
|
bool negMul, bool negAdd) {
|
|
|
|
assert(!(negMul && negAdd) && "Only one of negMul and negAdd should be set.");
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2012-10-02 12:45:10 +08:00
|
|
|
Value *MulOp0 = MulOp->getOperand(0);
|
|
|
|
Value *MulOp1 = MulOp->getOperand(1);
|
|
|
|
if (negMul) {
|
|
|
|
MulOp0 =
|
|
|
|
Builder.CreateFSub(
|
|
|
|
llvm::ConstantFP::getZeroValueForNegation(MulOp0->getType()), MulOp0,
|
|
|
|
"neg");
|
|
|
|
} else if (negAdd) {
|
|
|
|
Addend =
|
|
|
|
Builder.CreateFSub(
|
|
|
|
llvm::ConstantFP::getZeroValueForNegation(Addend->getType()), Addend,
|
|
|
|
"neg");
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *FMulAdd =
|
|
|
|
Builder.CreateCall3(
|
|
|
|
CGF.CGM.getIntrinsic(llvm::Intrinsic::fmuladd, Addend->getType()),
|
|
|
|
MulOp0, MulOp1, Addend);
|
|
|
|
MulOp->eraseFromParent();
|
|
|
|
|
|
|
|
return FMulAdd;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether it would be legal to emit an fmuladd intrinsic call to
|
|
|
|
// represent op and if so, build the fmuladd.
|
|
|
|
//
|
|
|
|
// Checks that (a) the operation is fusable, and (b) -ffp-contract=on.
|
|
|
|
// Does NOT check the type of the operation - it's assumed that this function
|
|
|
|
// will be called from contexts where it's known that the type is contractable.
|
2013-07-26 14:16:11 +08:00
|
|
|
static Value* tryEmitFMulAdd(const BinOpInfo &op,
|
2012-10-02 12:45:10 +08:00
|
|
|
const CodeGenFunction &CGF, CGBuilderTy &Builder,
|
|
|
|
bool isSub=false) {
|
|
|
|
|
|
|
|
assert((op.Opcode == BO_Add || op.Opcode == BO_AddAssign ||
|
|
|
|
op.Opcode == BO_Sub || op.Opcode == BO_SubAssign) &&
|
|
|
|
"Only fadd/fsub can be the root of an fmuladd.");
|
|
|
|
|
|
|
|
// Check whether this op is marked as fusable.
|
|
|
|
if (!op.FPContractable)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Check whether -ffp-contract=on. (If -ffp-contract=off/fast, fusing is
|
|
|
|
// either disabled, or handled entirely by the LLVM backend).
|
2012-11-15 15:51:26 +08:00
|
|
|
if (CGF.CGM.getCodeGenOpts().getFPContractMode() != CodeGenOptions::FPC_On)
|
2012-10-02 12:45:10 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// We have a potentially fusable op. Look for a mul on one of the operands.
|
|
|
|
if (llvm::BinaryOperator* LHSBinOp = dyn_cast<llvm::BinaryOperator>(op.LHS)) {
|
|
|
|
if (LHSBinOp->getOpcode() == llvm::Instruction::FMul) {
|
2012-10-04 11:23:25 +08:00
|
|
|
assert(LHSBinOp->getNumUses() == 0 &&
|
|
|
|
"Operations with multiple uses shouldn't be contracted.");
|
2012-10-02 12:45:10 +08:00
|
|
|
return buildFMulAdd(LHSBinOp, op.RHS, CGF, Builder, false, isSub);
|
|
|
|
}
|
|
|
|
} else if (llvm::BinaryOperator* RHSBinOp =
|
|
|
|
dyn_cast<llvm::BinaryOperator>(op.RHS)) {
|
|
|
|
if (RHSBinOp->getOpcode() == llvm::Instruction::FMul) {
|
2012-10-04 11:23:25 +08:00
|
|
|
assert(RHSBinOp->getNumUses() == 0 &&
|
|
|
|
"Operations with multiple uses shouldn't be contracted.");
|
2012-10-02 12:45:10 +08:00
|
|
|
return buildFMulAdd(RHSBinOp, op.LHS, CGF, Builder, isSub, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &op) {
|
|
|
|
if (op.LHS->getType()->isPointerTy() ||
|
|
|
|
op.RHS->getType()->isPointerTy())
|
|
|
|
return emitPointerArithmetic(CGF, op, /*subtraction*/ false);
|
|
|
|
|
|
|
|
if (op.Ty->isSignedIntegerOrEnumerationType()) {
|
2012-11-02 06:30:59 +08:00
|
|
|
switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
|
2011-06-25 09:32:37 +08:00
|
|
|
case LangOptions::SOB_Defined:
|
|
|
|
return Builder.CreateAdd(op.LHS, op.RHS, "add");
|
2012-08-25 08:32:28 +08:00
|
|
|
case LangOptions::SOB_Undefined:
|
2013-01-18 19:30:38 +08:00
|
|
|
if (!CGF.SanOpts->SignedIntegerOverflow)
|
2012-08-25 08:32:28 +08:00
|
|
|
return Builder.CreateNSWAdd(op.LHS, op.RHS, "add");
|
|
|
|
// Fall through.
|
2011-06-25 09:32:37 +08:00
|
|
|
case LangOptions::SOB_Trapping:
|
|
|
|
return EmitOverflowCheckedBinOp(op);
|
|
|
|
}
|
|
|
|
}
|
2012-11-27 23:01:55 +08:00
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (op.Ty->isUnsignedIntegerType() && CGF.SanOpts->UnsignedIntegerOverflow)
|
2012-11-27 23:01:55 +08:00
|
|
|
return EmitOverflowCheckedBinOp(op);
|
|
|
|
|
2012-10-02 12:45:10 +08:00
|
|
|
if (op.LHS->getType()->isFPOrFPVectorTy()) {
|
|
|
|
// Try to form an fmuladd.
|
|
|
|
if (Value *FMulAdd = tryEmitFMulAdd(op, CGF, Builder))
|
|
|
|
return FMulAdd;
|
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
return Builder.CreateFAdd(op.LHS, op.RHS, "add");
|
2012-10-02 12:45:10 +08:00
|
|
|
}
|
2011-06-25 09:32:37 +08:00
|
|
|
|
|
|
|
return Builder.CreateAdd(op.LHS, op.RHS, "add");
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::EmitSub(const BinOpInfo &op) {
|
|
|
|
// The LHS is always a pointer if either side is.
|
|
|
|
if (!op.LHS->getType()->isPointerTy()) {
|
|
|
|
if (op.Ty->isSignedIntegerOrEnumerationType()) {
|
2012-11-02 06:30:59 +08:00
|
|
|
switch (CGF.getLangOpts().getSignedOverflowBehavior()) {
|
2010-06-27 05:25:03 +08:00
|
|
|
case LangOptions::SOB_Defined:
|
2011-06-25 09:32:37 +08:00
|
|
|
return Builder.CreateSub(op.LHS, op.RHS, "sub");
|
2012-08-25 08:32:28 +08:00
|
|
|
case LangOptions::SOB_Undefined:
|
2013-01-18 19:30:38 +08:00
|
|
|
if (!CGF.SanOpts->SignedIntegerOverflow)
|
2012-08-25 08:32:28 +08:00
|
|
|
return Builder.CreateNSWSub(op.LHS, op.RHS, "sub");
|
|
|
|
// Fall through.
|
2010-06-27 05:25:03 +08:00
|
|
|
case LangOptions::SOB_Trapping:
|
2011-06-25 09:32:37 +08:00
|
|
|
return EmitOverflowCheckedBinOp(op);
|
2010-06-27 05:25:03 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-27 23:01:55 +08:00
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (op.Ty->isUnsignedIntegerType() && CGF.SanOpts->UnsignedIntegerOverflow)
|
2012-11-27 23:01:55 +08:00
|
|
|
return EmitOverflowCheckedBinOp(op);
|
|
|
|
|
2012-10-02 12:45:10 +08:00
|
|
|
if (op.LHS->getType()->isFPOrFPVectorTy()) {
|
|
|
|
// Try to form an fmuladd.
|
|
|
|
if (Value *FMulAdd = tryEmitFMulAdd(op, CGF, Builder, true))
|
|
|
|
return FMulAdd;
|
2011-06-25 09:32:37 +08:00
|
|
|
return Builder.CreateFSub(op.LHS, op.RHS, "sub");
|
2012-10-02 12:45:10 +08:00
|
|
|
}
|
2010-03-30 01:28:16 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
return Builder.CreateSub(op.LHS, op.RHS, "sub");
|
2009-04-02 04:28:16 +08:00
|
|
|
}
|
2007-08-25 05:00:35 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
// If the RHS is not a pointer, then we have normal pointer
|
|
|
|
// arithmetic.
|
|
|
|
if (!op.RHS->getType()->isPointerTy())
|
|
|
|
return emitPointerArithmetic(CGF, op, /*subtraction*/ true);
|
2009-04-25 13:08:32 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
// Otherwise, this is a pointer subtraction.
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
// Do the raw subtraction part.
|
|
|
|
llvm::Value *LHS
|
|
|
|
= Builder.CreatePtrToInt(op.LHS, CGF.PtrDiffTy, "sub.ptr.lhs.cast");
|
|
|
|
llvm::Value *RHS
|
|
|
|
= Builder.CreatePtrToInt(op.RHS, CGF.PtrDiffTy, "sub.ptr.rhs.cast");
|
|
|
|
Value *diffInChars = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
// Okay, figure out the element size.
|
|
|
|
const BinaryOperator *expr = cast<BinaryOperator>(op.E);
|
|
|
|
QualType elementType = expr->getLHS()->getType()->getPointeeType();
|
2009-08-12 06:40:09 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
llvm::Value *divisor = 0;
|
|
|
|
|
|
|
|
// For a variable-length array, this is going to be non-constant.
|
|
|
|
if (const VariableArrayType *vla
|
|
|
|
= CGF.getContext().getAsVariableArrayType(elementType)) {
|
|
|
|
llvm::Value *numElements;
|
|
|
|
llvm::tie(numElements, elementType) = CGF.getVLASize(vla);
|
2011-03-01 08:03:48 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
divisor = numElements;
|
2011-03-01 08:03:48 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
// Scale the number of non-VLA elements by the non-VLA element size.
|
|
|
|
CharUnits eltSize = CGF.getContext().getTypeSizeInChars(elementType);
|
|
|
|
if (!eltSize.isOne())
|
|
|
|
divisor = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), divisor);
|
2011-03-01 08:03:48 +08:00
|
|
|
|
2011-06-25 09:32:37 +08:00
|
|
|
// For everything elese, we can just compute it, safe in the
|
|
|
|
// assumption that Sema won't let anything through that we can't
|
|
|
|
// safely compute the size of.
|
|
|
|
} else {
|
|
|
|
CharUnits elementSize;
|
|
|
|
// Handle GCC extension for pointer arithmetic on void* and
|
|
|
|
// function pointer types.
|
|
|
|
if (elementType->isVoidType() || elementType->isFunctionType())
|
|
|
|
elementSize = CharUnits::One();
|
|
|
|
else
|
|
|
|
elementSize = CGF.getContext().getTypeSizeInChars(elementType);
|
|
|
|
|
|
|
|
// Don't even emit the divide for element size of 1.
|
|
|
|
if (elementSize.isOne())
|
|
|
|
return diffInChars;
|
|
|
|
|
|
|
|
divisor = CGF.CGM.getSize(elementSize);
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-03-01 08:03:48 +08:00
|
|
|
// Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since
|
|
|
|
// pointer difference in C is only defined in the case where both operands
|
|
|
|
// are pointing to elements of an array.
|
2011-06-25 09:32:37 +08:00
|
|
|
return Builder.CreateExactSDiv(diffInChars, divisor, "sub.ptr.div");
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
2013-01-08 00:43:27 +08:00
|
|
|
Value *ScalarExprEmitter::GetWidthMinusOneValue(Value* LHS,Value* RHS) {
|
2013-01-10 17:11:33 +08:00
|
|
|
llvm::IntegerType *Ty;
|
|
|
|
if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(LHS->getType()))
|
|
|
|
Ty = cast<llvm::IntegerType>(VT->getElementType());
|
|
|
|
else
|
|
|
|
Ty = cast<llvm::IntegerType>(LHS->getType());
|
|
|
|
return llvm::ConstantInt::get(RHS->getType(), Ty->getBitWidth() - 1);
|
2013-01-08 00:43:27 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (CGF.SanOpts->Shift && !CGF.getLangOpts().OpenCL &&
|
|
|
|
isa<llvm::IntegerType>(Ops.LHS->getType())) {
|
2013-01-08 00:43:27 +08:00
|
|
|
llvm::Value *WidthMinusOne = GetWidthMinusOneValue(Ops.LHS, RHS);
|
2013-02-26 06:37:49 +08:00
|
|
|
llvm::Value *Valid = Builder.CreateICmpULE(RHS, WidthMinusOne);
|
2012-08-25 08:32:28 +08:00
|
|
|
|
|
|
|
if (Ops.Ty->hasSignedIntegerRepresentation()) {
|
2013-02-26 06:37:49 +08:00
|
|
|
llvm::BasicBlock *Orig = Builder.GetInsertBlock();
|
|
|
|
llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
|
|
|
|
llvm::BasicBlock *CheckBitsShifted = CGF.createBasicBlock("check");
|
|
|
|
Builder.CreateCondBr(Valid, CheckBitsShifted, Cont);
|
|
|
|
|
2012-08-25 08:32:28 +08:00
|
|
|
// Check whether we are shifting any non-zero bits off the top of the
|
|
|
|
// integer.
|
2013-02-26 06:37:49 +08:00
|
|
|
CGF.EmitBlock(CheckBitsShifted);
|
2012-08-25 08:32:28 +08:00
|
|
|
llvm::Value *BitsShiftedOff =
|
|
|
|
Builder.CreateLShr(Ops.LHS,
|
|
|
|
Builder.CreateSub(WidthMinusOne, RHS, "shl.zeros",
|
|
|
|
/*NUW*/true, /*NSW*/true),
|
|
|
|
"shl.check");
|
|
|
|
if (CGF.getLangOpts().CPlusPlus) {
|
|
|
|
// In C99, we are not permitted to shift a 1 bit into the sign bit.
|
|
|
|
// Under C++11's rules, shifting a 1 bit into the sign bit is
|
|
|
|
// OK, but shifting a 1 bit out of it is not. (C89 and C++03 don't
|
|
|
|
// define signed left shifts, so we use the C99 and C++11 rules there).
|
|
|
|
llvm::Value *One = llvm::ConstantInt::get(BitsShiftedOff->getType(), 1);
|
|
|
|
BitsShiftedOff = Builder.CreateLShr(BitsShiftedOff, One);
|
|
|
|
}
|
|
|
|
llvm::Value *Zero = llvm::ConstantInt::get(BitsShiftedOff->getType(), 0);
|
2013-02-26 06:37:49 +08:00
|
|
|
llvm::Value *SecondCheck = Builder.CreateICmpEQ(BitsShiftedOff, Zero);
|
|
|
|
CGF.EmitBlock(Cont);
|
|
|
|
llvm::PHINode *P = Builder.CreatePHI(Valid->getType(), 2);
|
|
|
|
P->addIncoming(Valid, Orig);
|
|
|
|
P->addIncoming(SecondCheck, CheckBitsShifted);
|
|
|
|
Valid = P;
|
2012-08-25 08:32:28 +08:00
|
|
|
}
|
2013-02-26 06:37:49 +08:00
|
|
|
|
|
|
|
EmitBinOpCheck(Valid, Ops);
|
2009-12-15 05:58:14 +08:00
|
|
|
}
|
2013-01-08 00:43:27 +08:00
|
|
|
// OpenCL 6.3j: shift values are effectively % word size of LHS.
|
|
|
|
if (CGF.getLangOpts().OpenCL)
|
|
|
|
RHS = Builder.CreateAnd(RHS, GetWidthMinusOneValue(Ops.LHS, RHS), "shl.mask");
|
2009-12-15 05:58:14 +08:00
|
|
|
|
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
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (CGF.SanOpts->Shift && !CGF.getLangOpts().OpenCL &&
|
|
|
|
isa<llvm::IntegerType>(Ops.LHS->getType()))
|
2013-01-08 00:43:27 +08:00
|
|
|
EmitBinOpCheck(Builder.CreateICmpULE(RHS, GetWidthMinusOneValue(Ops.LHS, RHS)), Ops);
|
|
|
|
|
|
|
|
// OpenCL 6.3j: shift values are effectively % word size of LHS.
|
|
|
|
if (CGF.getLangOpts().OpenCL)
|
|
|
|
RHS = Builder.CreateAnd(RHS, GetWidthMinusOneValue(Ops.LHS, RHS), "shr.mask");
|
2009-12-15 05:58:14 +08:00
|
|
|
|
2010-07-23 23:58:24 +08:00
|
|
|
if (Ops.Ty->hasUnsignedIntegerRepresentation())
|
2007-08-24 13:35:26 +08:00
|
|
|
return Builder.CreateLShr(Ops.LHS, RHS, "shr");
|
|
|
|
return Builder.CreateAShr(Ops.LHS, RHS, "shr");
|
|
|
|
}
|
|
|
|
|
2010-11-18 11:19:30 +08:00
|
|
|
enum IntrinsicType { VCMPEQ, VCMPGT };
|
|
|
|
// return corresponding comparison intrinsic for given vector type
|
|
|
|
static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT,
|
|
|
|
BuiltinType::Kind ElemKind) {
|
|
|
|
switch (ElemKind) {
|
2011-09-23 13:06:16 +08:00
|
|
|
default: llvm_unreachable("unexpected element type");
|
2010-11-18 11:19:30 +08:00
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
|
|
|
|
llvm::Intrinsic::ppc_altivec_vcmpgtub_p;
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
|
|
|
|
llvm::Intrinsic::ppc_altivec_vcmpgtsb_p;
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
|
|
|
|
llvm::Intrinsic::ppc_altivec_vcmpgtuh_p;
|
|
|
|
case BuiltinType::Short:
|
|
|
|
return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
|
|
|
|
llvm::Intrinsic::ppc_altivec_vcmpgtsh_p;
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
|
|
|
|
llvm::Intrinsic::ppc_altivec_vcmpgtuw_p;
|
|
|
|
case BuiltinType::Int:
|
|
|
|
case BuiltinType::Long:
|
|
|
|
return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
|
|
|
|
llvm::Intrinsic::ppc_altivec_vcmpgtsw_p;
|
|
|
|
case BuiltinType::Float:
|
|
|
|
return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpeqfp_p :
|
|
|
|
llvm::Intrinsic::ppc_altivec_vcmpgtfp_p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
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();
|
2010-08-23 09:21:21 +08:00
|
|
|
if (const MemberPointerType *MPT = LHSTy->getAs<MemberPointerType>()) {
|
2010-08-25 19:45:40 +08:00
|
|
|
assert(E->getOpcode() == BO_EQ ||
|
|
|
|
E->getOpcode() == BO_NE);
|
2010-08-22 18:59:02 +08:00
|
|
|
Value *LHS = CGF.EmitScalarExpr(E->getLHS());
|
|
|
|
Value *RHS = CGF.EmitScalarExpr(E->getRHS());
|
2010-08-23 09:21:21 +08:00
|
|
|
Result = CGF.CGM.getCXXABI().EmitMemberPointerComparison(
|
2010-08-25 19:45:40 +08:00
|
|
|
CGF, LHS, RHS, MPT, E->getOpcode() == BO_NE);
|
2009-12-11 15:36:43 +08:00
|
|
|
} 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-11-18 11:19:30 +08:00
|
|
|
// If AltiVec, the comparison results in a numeric type, so we use
|
|
|
|
// intrinsics comparing vectors and giving 0 or 1 as a result
|
2011-03-29 05:00:05 +08:00
|
|
|
if (LHSTy->isVectorType() && !E->getType()->isVectorType()) {
|
2010-11-18 11:19:30 +08:00
|
|
|
// constants for mapping CR6 register bits to predicate result
|
|
|
|
enum { CR6_EQ=0, CR6_EQ_REV, CR6_LT, CR6_LT_REV } CR6;
|
|
|
|
|
|
|
|
llvm::Intrinsic::ID ID = llvm::Intrinsic::not_intrinsic;
|
|
|
|
|
|
|
|
// in several cases vector arguments order will be reversed
|
|
|
|
Value *FirstVecArg = LHS,
|
|
|
|
*SecondVecArg = RHS;
|
|
|
|
|
|
|
|
QualType ElTy = LHSTy->getAs<VectorType>()->getElementType();
|
2011-01-19 14:33:43 +08:00
|
|
|
const BuiltinType *BTy = ElTy->getAs<BuiltinType>();
|
2010-11-18 11:19:30 +08:00
|
|
|
BuiltinType::Kind ElementKind = BTy->getKind();
|
|
|
|
|
|
|
|
switch(E->getOpcode()) {
|
2011-09-23 13:06:16 +08:00
|
|
|
default: llvm_unreachable("is not a comparison operation");
|
2010-11-18 11:19:30 +08:00
|
|
|
case BO_EQ:
|
|
|
|
CR6 = CR6_LT;
|
|
|
|
ID = GetIntrinsic(VCMPEQ, ElementKind);
|
|
|
|
break;
|
|
|
|
case BO_NE:
|
|
|
|
CR6 = CR6_EQ;
|
|
|
|
ID = GetIntrinsic(VCMPEQ, ElementKind);
|
|
|
|
break;
|
|
|
|
case BO_LT:
|
|
|
|
CR6 = CR6_LT;
|
|
|
|
ID = GetIntrinsic(VCMPGT, ElementKind);
|
|
|
|
std::swap(FirstVecArg, SecondVecArg);
|
|
|
|
break;
|
|
|
|
case BO_GT:
|
|
|
|
CR6 = CR6_LT;
|
|
|
|
ID = GetIntrinsic(VCMPGT, ElementKind);
|
|
|
|
break;
|
|
|
|
case BO_LE:
|
|
|
|
if (ElementKind == BuiltinType::Float) {
|
|
|
|
CR6 = CR6_LT;
|
|
|
|
ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
|
|
|
|
std::swap(FirstVecArg, SecondVecArg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CR6 = CR6_EQ;
|
|
|
|
ID = GetIntrinsic(VCMPGT, ElementKind);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BO_GE:
|
|
|
|
if (ElementKind == BuiltinType::Float) {
|
|
|
|
CR6 = CR6_LT;
|
|
|
|
ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CR6 = CR6_EQ;
|
|
|
|
ID = GetIntrinsic(VCMPGT, ElementKind);
|
|
|
|
std::swap(FirstVecArg, SecondVecArg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-04-20 06:55:03 +08:00
|
|
|
Value *CR6Param = Builder.getInt32(CR6);
|
2010-11-18 11:19:30 +08:00
|
|
|
llvm::Function *F = CGF.CGM.getIntrinsic(ID);
|
|
|
|
Result = Builder.CreateCall3(F, CR6Param, FirstVecArg, SecondVecArg, "");
|
|
|
|
return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
|
|
|
|
}
|
|
|
|
|
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");
|
2010-07-23 23:58:24 +08:00
|
|
|
} else if (LHSTy->hasSignedIntegerRepresentation()) {
|
2008-05-29 23:09:15 +08:00
|
|
|
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
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_EQ) {
|
2007-08-24 13:35:26 +08:00
|
|
|
Result = Builder.CreateAnd(ResultR, ResultI, "and.ri");
|
|
|
|
} else {
|
2010-08-25 19:45:40 +08:00
|
|
|
assert(E->getOpcode() == BO_NE &&
|
2007-08-24 13:35:26 +08:00
|
|
|
"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();
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
Value *RHS;
|
|
|
|
LValue LHS;
|
|
|
|
|
|
|
|
switch (E->getLHS()->getType().getObjCLifetime()) {
|
|
|
|
case Qualifiers::OCL_Strong:
|
|
|
|
llvm::tie(LHS, RHS) = CGF.EmitARCStoreStrong(E, Ignore);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
llvm::tie(LHS,RHS) = CGF.EmitARCStoreAutoreleasing(E);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
RHS = Visit(E->getRHS());
|
2012-09-08 10:08:36 +08:00
|
|
|
LHS = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
|
2011-06-16 07:02:42 +08:00
|
|
|
RHS = CGF.EmitARCStoreWeak(LHS.getAddress(), RHS, Ignore);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// No reason to do any of these differently.
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
// __block variables need to have the rhs evaluated first, plus
|
|
|
|
// this should improve codegen just a little.
|
|
|
|
RHS = Visit(E->getRHS());
|
2012-09-08 10:08:36 +08:00
|
|
|
LHS = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
// Store the value into the LHS. 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...'.
|
|
|
|
if (LHS.isBitField())
|
2011-06-25 10:11:03 +08:00
|
|
|
CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, &RHS);
|
2011-06-16 07:02:42 +08:00
|
|
|
else
|
2011-06-25 10:11:03 +08:00
|
|
|
CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS);
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2010-06-30 06:00:45 +08:00
|
|
|
|
|
|
|
// If the result is clearly ignored, return now.
|
2009-05-29 23:46:01 +08:00
|
|
|
if (Ignore)
|
|
|
|
return 0;
|
2010-06-30 06:00:45 +08:00
|
|
|
|
2010-11-16 18:08:07 +08:00
|
|
|
// The result of an assignment in C is the assigned r-value.
|
2012-11-02 06:30:59 +08:00
|
|
|
if (!CGF.getLangOpts().CPlusPlus)
|
2010-11-16 18:08:07 +08:00
|
|
|
return RHS;
|
|
|
|
|
2010-06-30 06:00:45 +08:00
|
|
|
// If the lvalue is non-volatile, return the computed value of the assignment.
|
|
|
|
if (!LHS.isVolatileQualified())
|
|
|
|
return RHS;
|
|
|
|
|
|
|
|
// Otherwise, reload the value.
|
2013-10-02 10:29:49 +08:00
|
|
|
return EmitLoadOfLValue(LHS, E->getExprLoc());
|
2007-08-24 13:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
|
2014-01-07 06:27:43 +08:00
|
|
|
RegionCounter Cnt = CGF.getPGORegionCounter(E);
|
|
|
|
|
2012-01-17 05:02:28 +08:00
|
|
|
// Perform vector logical and on comparisons with zero vectors.
|
|
|
|
if (E->getType()->isVectorType()) {
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
|
2012-01-17 05:02:28 +08:00
|
|
|
Value *LHS = Visit(E->getLHS());
|
|
|
|
Value *RHS = Visit(E->getRHS());
|
|
|
|
Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
|
2013-02-21 19:49:56 +08:00
|
|
|
if (LHS->getType()->isFPOrFPVectorTy()) {
|
|
|
|
LHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero, "cmp");
|
|
|
|
RHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero, "cmp");
|
|
|
|
} else {
|
|
|
|
LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp");
|
|
|
|
RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp");
|
|
|
|
}
|
2012-01-17 05:02:28 +08:00
|
|
|
Value *And = Builder.CreateAnd(LHS, RHS);
|
2013-02-21 19:49:56 +08:00
|
|
|
return Builder.CreateSExt(And, ConvertType(E->getType()), "sext");
|
2012-01-17 05:02:28 +08:00
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ResTy = ConvertType(E->getType());
|
2013-07-26 14:16:11 +08:00
|
|
|
|
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.
|
2011-02-28 07:02:32 +08:00
|
|
|
bool LHSCondVal;
|
|
|
|
if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
|
|
|
|
if (LHSCondVal) { // If we have 1 && X, just emit X.
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
|
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
|
|
|
|
2011-01-26 12:00:11 +08:00
|
|
|
CodeGenFunction::ConditionalEvaluation eval(CGF);
|
|
|
|
|
2008-11-12 16:38:24 +08:00
|
|
|
// Branch on the LHS first. If it is false, go to the failure (cont) block.
|
2014-01-07 06:27:43 +08:00
|
|
|
CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock, Cnt.getCount());
|
2008-11-12 16:38:24 +08:00
|
|
|
|
|
|
|
// 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.
|
2011-03-30 19:28:58 +08:00
|
|
|
llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2,
|
2009-08-14 05:57:51 +08:00
|
|
|
"", ContBlock);
|
2008-11-12 16:38:24 +08:00
|
|
|
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
|
|
|
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.begin(CGF);
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(RHSBlock);
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
|
2014-01-14 05:24:22 +08:00
|
|
|
Cnt.adjustForControlFlow();
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.end(CGF);
|
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.
|
2011-03-30 08:08:31 +08:00
|
|
|
if (CGF.getDebugInfo())
|
|
|
|
// There is no need to emit line number for unconditional branch.
|
|
|
|
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(ContBlock);
|
|
|
|
PN->addIncoming(RHSCond, RHSBlock);
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.applyAdjustmentsToRegion();
|
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) {
|
2014-01-07 06:27:43 +08:00
|
|
|
RegionCounter Cnt = CGF.getPGORegionCounter(E);
|
|
|
|
|
2012-01-17 05:02:28 +08:00
|
|
|
// Perform vector logical or on comparisons with zero vectors.
|
|
|
|
if (E->getType()->isVectorType()) {
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
|
2012-01-17 05:02:28 +08:00
|
|
|
Value *LHS = Visit(E->getLHS());
|
|
|
|
Value *RHS = Visit(E->getRHS());
|
|
|
|
Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
|
2013-02-21 19:49:56 +08:00
|
|
|
if (LHS->getType()->isFPOrFPVectorTy()) {
|
|
|
|
LHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero, "cmp");
|
|
|
|
RHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero, "cmp");
|
|
|
|
} else {
|
|
|
|
LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp");
|
|
|
|
RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp");
|
|
|
|
}
|
2012-01-17 05:02:28 +08:00
|
|
|
Value *Or = Builder.CreateOr(LHS, RHS);
|
2013-02-21 19:49:56 +08:00
|
|
|
return Builder.CreateSExt(Or, ConvertType(E->getType()), "sext");
|
2012-01-17 05:02:28 +08:00
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ResTy = ConvertType(E->getType());
|
2013-07-26 14:16:11 +08:00
|
|
|
|
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.
|
2011-02-28 07:02:32 +08:00
|
|
|
bool LHSCondVal;
|
|
|
|
if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) {
|
|
|
|
if (!LHSCondVal) { // If we have 0 || X, just emit X.
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
|
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
|
|
|
|
2011-01-26 12:00:11 +08:00
|
|
|
CodeGenFunction::ConditionalEvaluation eval(CGF);
|
|
|
|
|
2008-11-12 16:38:24 +08:00
|
|
|
// Branch on the LHS first. If it is true, go to the success (cont) block.
|
2014-01-07 06:27:43 +08:00
|
|
|
CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock,
|
|
|
|
Cnt.getParentCount() - Cnt.getCount());
|
2008-11-12 16:38:24 +08:00
|
|
|
|
|
|
|
// 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.
|
2011-03-30 19:28:58 +08:00
|
|
|
llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2,
|
2009-08-14 05:57:51 +08:00
|
|
|
"", ContBlock);
|
2008-11-12 16:38:24 +08:00
|
|
|
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
|
|
|
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.begin(CGF);
|
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);
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
|
2014-01-14 05:24:22 +08:00
|
|
|
Cnt.adjustForControlFlow();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.end(CGF);
|
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);
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.applyAdjustmentsToRegion();
|
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) {
|
2010-12-05 10:00:02 +08:00
|
|
|
CGF.EmitIgnoredExpr(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) {
|
2011-04-17 07:15:35 +08:00
|
|
|
// Anything that is an integer or floating point constant is fine.
|
2013-11-09 07:00:12 +08:00
|
|
|
return E->IgnoreParens()->isEvaluatable(CGF.getContext());
|
|
|
|
|
|
|
|
// Even non-volatile automatic variables can't be evaluated unconditionally.
|
|
|
|
// Referencing a thread_local may cause non-trivial initialization work to
|
|
|
|
// occur. If we're inside a lambda and one of the variables is from the scope
|
|
|
|
// outside the lambda, that function may have returned already. Reading its
|
|
|
|
// locals is a bad idea. Also, these reads may introduce races there didn't
|
|
|
|
// exist in the source-level program.
|
2008-11-12 16:55:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
Value *ScalarExprEmitter::
|
2011-02-17 18:25:35 +08:00
|
|
|
VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) {
|
2009-05-29 23:46:01 +08:00
|
|
|
TestAndClearIgnoreResultAssign();
|
2011-02-17 18:25:35 +08:00
|
|
|
|
|
|
|
// Bind the common expression if necessary.
|
2012-01-07 04:42:20 +08:00
|
|
|
CodeGenFunction::OpaqueValueMapping binding(CGF, E);
|
2014-01-07 06:27:43 +08:00
|
|
|
RegionCounter Cnt = CGF.getPGORegionCounter(E);
|
2011-02-17 18:25:35 +08:00
|
|
|
|
|
|
|
Expr *condExpr = E->getCond();
|
|
|
|
Expr *lhsExpr = E->getTrueExpr();
|
|
|
|
Expr *rhsExpr = E->getFalseExpr();
|
|
|
|
|
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.
|
2011-02-28 07:02:32 +08:00
|
|
|
bool CondExprBool;
|
|
|
|
if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) {
|
2011-02-17 18:25:35 +08:00
|
|
|
Expr *live = lhsExpr, *dead = rhsExpr;
|
2011-02-28 07:02:32 +08:00
|
|
|
if (!CondExprBool) std::swap(live, dead);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-10-15 10:10:40 +08:00
|
|
|
// If the dead side doesn't have labels we need, just emit the Live part.
|
|
|
|
if (!CGF.ContainsLabel(dead)) {
|
2014-01-07 06:27:43 +08:00
|
|
|
if (CondExprBool)
|
|
|
|
Cnt.beginRegion(Builder);
|
2011-10-15 10:10:40 +08:00
|
|
|
Value *Result = Visit(live);
|
|
|
|
|
|
|
|
// If the live part is a throw expression, it acts like it has a void
|
|
|
|
// type, so evaluating it returns a null Value*. However, a conditional
|
|
|
|
// with non-void type must return a non-null Value*.
|
|
|
|
if (!Result && !E->getType()->isVoidType())
|
|
|
|
Result = llvm::UndefValue::get(CGF.ConvertType(E->getType()));
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
2008-11-12 02:56:45 +08:00
|
|
|
}
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-09-21 06:41:17 +08:00
|
|
|
// OpenCL: If the condition is a vector, we can treat this condition like
|
|
|
|
// the select function.
|
2013-07-26 14:16:11 +08:00
|
|
|
if (CGF.getLangOpts().OpenCL
|
2011-02-17 18:25:35 +08:00
|
|
|
&& condExpr->getType()->isVectorType()) {
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
|
2011-02-17 18:25:35 +08:00
|
|
|
llvm::Value *CondV = CGF.EmitScalarExpr(condExpr);
|
|
|
|
llvm::Value *LHS = Visit(lhsExpr);
|
|
|
|
llvm::Value *RHS = Visit(rhsExpr);
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *condType = ConvertType(condExpr->getType());
|
|
|
|
llvm::VectorType *vecTy = cast<llvm::VectorType>(condType);
|
2013-07-26 14:16:11 +08:00
|
|
|
|
|
|
|
unsigned numElem = vecTy->getNumElements();
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *elemType = vecTy->getElementType();
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2012-01-25 13:34:41 +08:00
|
|
|
llvm::Value *zeroVec = llvm::Constant::getNullValue(vecTy);
|
2010-09-21 06:41:17 +08:00
|
|
|
llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec);
|
2013-07-26 14:16:11 +08:00
|
|
|
llvm::Value *tmp = Builder.CreateSExt(TestMSB,
|
2010-09-21 06:41:17 +08:00
|
|
|
llvm::VectorType::get(elemType,
|
2013-07-26 14:16:11 +08:00
|
|
|
numElem),
|
2010-09-21 06:41:17 +08:00
|
|
|
"sext");
|
|
|
|
llvm::Value *tmp2 = Builder.CreateNot(tmp);
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-09-21 06:41:17 +08:00
|
|
|
// Cast float to int to perform ANDs if necessary.
|
|
|
|
llvm::Value *RHSTmp = RHS;
|
|
|
|
llvm::Value *LHSTmp = LHS;
|
|
|
|
bool wasCast = false;
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType());
|
2012-05-29 08:35:18 +08:00
|
|
|
if (rhsVTy->getElementType()->isFloatingPointTy()) {
|
2010-09-21 06:41:17 +08:00
|
|
|
RHSTmp = Builder.CreateBitCast(RHS, tmp2->getType());
|
|
|
|
LHSTmp = Builder.CreateBitCast(LHS, tmp->getType());
|
|
|
|
wasCast = true;
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-09-21 06:41:17 +08:00
|
|
|
llvm::Value *tmp3 = Builder.CreateAnd(RHSTmp, tmp2);
|
|
|
|
llvm::Value *tmp4 = Builder.CreateAnd(LHSTmp, tmp);
|
|
|
|
llvm::Value *tmp5 = Builder.CreateOr(tmp3, tmp4, "cond");
|
|
|
|
if (wasCast)
|
|
|
|
tmp5 = Builder.CreateBitCast(tmp5, RHS->getType());
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2010-09-21 06:41:17 +08:00
|
|
|
return tmp5;
|
|
|
|
}
|
2013-07-26 14:16:11 +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.
|
2011-02-17 18:25:35 +08:00
|
|
|
if (isCheapEnoughToEvaluateUnconditionally(lhsExpr, CGF) &&
|
|
|
|
isCheapEnoughToEvaluateUnconditionally(rhsExpr, CGF)) {
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
|
|
|
|
2011-02-17 18:25:35 +08:00
|
|
|
llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr);
|
|
|
|
llvm::Value *LHS = Visit(lhsExpr);
|
|
|
|
llvm::Value *RHS = Visit(rhsExpr);
|
2011-12-09 06:01:56 +08:00
|
|
|
if (!LHS) {
|
|
|
|
// If the conditional has void type, make sure we return a null Value*.
|
|
|
|
assert(!RHS && "LHS and RHS types must match");
|
|
|
|
return 0;
|
|
|
|
}
|
2008-11-12 16:55:54 +08:00
|
|
|
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");
|
2011-01-26 12:00:11 +08:00
|
|
|
|
|
|
|
CodeGenFunction::ConditionalEvaluation eval(CGF);
|
2014-01-07 06:27:43 +08:00
|
|
|
CGF.EmitBranchOnBoolExpr(condExpr, LHSBlock, RHSBlock, Cnt.getCount());
|
2009-06-04 11:00:32 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(LHSBlock);
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginRegion(Builder);
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.begin(CGF);
|
2011-02-17 18:25:35 +08:00
|
|
|
Value *LHS = Visit(lhsExpr);
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.end(CGF);
|
2014-01-14 05:24:22 +08:00
|
|
|
Cnt.adjustForControlFlow();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
LHSBlock = Builder.GetInsertBlock();
|
2011-01-26 12:00:11 +08:00
|
|
|
Builder.CreateBr(ContBlock);
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(RHSBlock);
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.beginElseRegion();
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.begin(CGF);
|
2011-02-17 18:25:35 +08:00
|
|
|
Value *RHS = Visit(rhsExpr);
|
2011-01-26 12:00:11 +08:00
|
|
|
eval.end(CGF);
|
2014-01-14 05:24:22 +08:00
|
|
|
Cnt.adjustForControlFlow();
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-01-26 12:00:11 +08:00
|
|
|
RHSBlock = Builder.GetInsertBlock();
|
2007-08-24 13:35:26 +08:00
|
|
|
CGF.EmitBlock(ContBlock);
|
2014-01-07 06:27:43 +08:00
|
|
|
Cnt.applyAdjustmentsToRegion();
|
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.
|
2011-03-30 19:28:58 +08:00
|
|
|
llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), 2, "cond");
|
2007-08-24 13:35:26 +08:00
|
|
|
PN->addIncoming(LHS, LHSBlock);
|
|
|
|
PN->addIncoming(RHS, RHSBlock);
|
|
|
|
return PN;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) {
|
2013-07-20 08:40:58 +08:00
|
|
|
return Visit(E->getChosenSubExpr());
|
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
|
|
|
}
|
|
|
|
|
2011-02-07 18:33:21 +08:00
|
|
|
Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *block) {
|
|
|
|
return CGF.EmitBlockLiteral(block);
|
2009-02-13 02:29:15 +08:00
|
|
|
}
|
|
|
|
|
2011-06-04 08:47:47 +08:00
|
|
|
Value *ScalarExprEmitter::VisitAsTypeExpr(AsTypeExpr *E) {
|
|
|
|
Value *Src = CGF.EmitScalarExpr(E->getSrcExpr());
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *DstTy = ConvertType(E->getType());
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-06-04 08:47:47 +08:00
|
|
|
// Going from vec4->vec3 or vec3->vec4 is a special case and requires
|
|
|
|
// a shuffle vector instead of a bitcast.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *SrcTy = Src->getType();
|
2011-06-04 08:47:47 +08:00
|
|
|
if (isa<llvm::VectorType>(DstTy) && isa<llvm::VectorType>(SrcTy)) {
|
|
|
|
unsigned numElementsDst = cast<llvm::VectorType>(DstTy)->getNumElements();
|
|
|
|
unsigned numElementsSrc = cast<llvm::VectorType>(SrcTy)->getNumElements();
|
2013-07-26 14:16:11 +08:00
|
|
|
if ((numElementsDst == 3 && numElementsSrc == 4)
|
2011-06-04 08:47:47 +08:00
|
|
|
|| (numElementsDst == 4 && numElementsSrc == 3)) {
|
2013-07-26 14:16:11 +08:00
|
|
|
|
|
|
|
|
2011-06-04 08:47:47 +08:00
|
|
|
// In the case of going from int4->float3, a bitcast is needed before
|
|
|
|
// doing a shuffle.
|
2013-07-26 14:16:11 +08:00
|
|
|
llvm::Type *srcElemTy =
|
2011-06-04 08:47:47 +08:00
|
|
|
cast<llvm::VectorType>(SrcTy)->getElementType();
|
2013-07-26 14:16:11 +08:00
|
|
|
llvm::Type *dstElemTy =
|
2011-06-04 08:47:47 +08:00
|
|
|
cast<llvm::VectorType>(DstTy)->getElementType();
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-06-04 08:47:47 +08:00
|
|
|
if ((srcElemTy->isIntegerTy() && dstElemTy->isFloatTy())
|
|
|
|
|| (srcElemTy->isFloatTy() && dstElemTy->isIntegerTy())) {
|
|
|
|
// Create a float type of the same size as the source or destination.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::VectorType *newSrcTy = llvm::VectorType::get(dstElemTy,
|
2011-06-04 08:47:47 +08:00
|
|
|
numElementsSrc);
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-06-04 08:47:47 +08:00
|
|
|
Src = Builder.CreateBitCast(Src, newSrcTy, "astypeCast");
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-06-04 08:47:47 +08:00
|
|
|
llvm::Value *UnV = llvm::UndefValue::get(Src->getType());
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<llvm::Constant*, 3> Args;
|
2011-06-04 08:47:47 +08:00
|
|
|
Args.push_back(Builder.getInt32(0));
|
|
|
|
Args.push_back(Builder.getInt32(1));
|
|
|
|
Args.push_back(Builder.getInt32(2));
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-06-04 08:47:47 +08:00
|
|
|
if (numElementsDst == 4)
|
2012-02-07 08:39:47 +08:00
|
|
|
Args.push_back(llvm::UndefValue::get(CGF.Int32Ty));
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-06-04 08:47:47 +08:00
|
|
|
llvm::Constant *Mask = llvm::ConstantVector::get(Args);
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-06-04 08:47:47 +08:00
|
|
|
return Builder.CreateShuffleVector(Src, UnV, Mask, "astype");
|
|
|
|
}
|
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2011-06-04 08:47:47 +08:00
|
|
|
return Builder.CreateBitCast(Src, DstTy, "astype");
|
|
|
|
}
|
|
|
|
|
2011-10-11 10:20:01 +08:00
|
|
|
Value *ScalarExprEmitter::VisitAtomicExpr(AtomicExpr *E) {
|
|
|
|
return CGF.EmitAtomicExpr(E).getScalarVal();
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-03-08 05:37:08 +08:00
|
|
|
assert(E && hasScalarEvaluationKind(E->getType()) &&
|
2007-08-24 13:35:26 +08:00
|
|
|
"Invalid scalar expression to emit");
|
2009-09-09 21:00:44 +08:00
|
|
|
|
2011-03-08 02:29:53 +08:00
|
|
|
if (isa<CXXDefaultArgExpr>(E))
|
2011-03-08 02:45:56 +08:00
|
|
|
disableDebugInfo();
|
2011-03-08 02:29:53 +08:00
|
|
|
Value *V = ScalarExprEmitter(*this, IgnoreResultAssign)
|
2009-05-29 23:46:01 +08:00
|
|
|
.Visit(const_cast<Expr*>(E));
|
2011-03-08 02:29:53 +08:00
|
|
|
if (isa<CXXDefaultArgExpr>(E))
|
2011-03-08 02:45:56 +08:00
|
|
|
enableDebugInfo();
|
2011-03-08 02:29:53 +08:00
|
|
|
return V;
|
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) {
|
2013-03-08 05:37:08 +08:00
|
|
|
assert(hasScalarEvaluationKind(SrcTy) && hasScalarEvaluationKind(DstTy) &&
|
2007-08-26 14:48:56 +08:00
|
|
|
"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) {
|
2013-03-08 05:37:08 +08:00
|
|
|
assert(SrcTy->isAnyComplexType() && hasScalarEvaluationKind(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
|
|
|
|
2010-06-27 06:09:34 +08:00
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::
|
|
|
|
EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
|
|
|
|
bool isInc, bool isPre) {
|
|
|
|
return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre);
|
|
|
|
}
|
|
|
|
|
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
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ClassPtrTy = ConvertType(E->getType());
|
2010-02-06 03:18:30 +08:00
|
|
|
|
2009-12-10 07:35:29 +08:00
|
|
|
Expr *BaseExpr = E->getBase();
|
2010-11-24 13:12:34 +08:00
|
|
|
if (BaseExpr->isRValue()) {
|
2011-12-20 07:03:09 +08:00
|
|
|
V = CreateMemTemp(E->getType(), "resval");
|
2010-02-06 03:18:30 +08:00
|
|
|
llvm::Value *Src = EmitScalarExpr(BaseExpr);
|
|
|
|
Builder.CreateStore(Src, V);
|
2010-08-21 11:08:16 +08:00
|
|
|
V = ScalarExprEmitter(*this).EmitLoadOfLValue(
|
2013-10-02 10:29:49 +08:00
|
|
|
MakeNaturalAlignAddrLValue(V, E->getType()), E->getExprLoc());
|
2010-08-21 11:08:16 +08:00
|
|
|
} else {
|
|
|
|
if (E->isArrow())
|
|
|
|
V = ScalarExprEmitter(*this).EmitLoadOfLValue(BaseExpr);
|
|
|
|
else
|
|
|
|
V = EmitLValue(BaseExpr).getAddress();
|
2010-02-06 03:18:30 +08:00
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2009-12-10 07:35:29 +08:00
|
|
|
// build Class* type
|
|
|
|
ClassPtrTy = ClassPtrTy->getPointerTo();
|
|
|
|
V = Builder.CreateBitCast(V, ClassPtrTy);
|
2011-12-20 07:03:09 +08:00
|
|
|
return MakeNaturalAlignAddrLValue(V, E->getType());
|
2009-12-10 07:35:29 +08:00
|
|
|
}
|
|
|
|
|
2010-04-23 12:16:32 +08:00
|
|
|
|
2010-12-05 10:00:02 +08:00
|
|
|
LValue CodeGenFunction::EmitCompoundAssignmentLValue(
|
2010-04-23 12:16:32 +08:00
|
|
|
const CompoundAssignOperator *E) {
|
|
|
|
ScalarExprEmitter Scalar(*this);
|
2010-06-30 06:00:45 +08:00
|
|
|
Value *Result = 0;
|
2010-04-23 12:16:32 +08:00
|
|
|
switch (E->getOpcode()) {
|
|
|
|
#define COMPOUND_OP(Op) \
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_##Op##Assign: \
|
2010-04-23 12:16:32 +08:00
|
|
|
return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \
|
2010-06-30 06:00:45 +08:00
|
|
|
Result)
|
2010-04-23 12:16:32 +08:00
|
|
|
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
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_PtrMemD:
|
|
|
|
case BO_PtrMemI:
|
|
|
|
case BO_Mul:
|
|
|
|
case BO_Div:
|
|
|
|
case BO_Rem:
|
|
|
|
case BO_Add:
|
|
|
|
case BO_Sub:
|
|
|
|
case BO_Shl:
|
|
|
|
case BO_Shr:
|
|
|
|
case BO_LT:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_GE:
|
|
|
|
case BO_EQ:
|
|
|
|
case BO_NE:
|
|
|
|
case BO_And:
|
|
|
|
case BO_Xor:
|
|
|
|
case BO_Or:
|
|
|
|
case BO_LAnd:
|
|
|
|
case BO_LOr:
|
|
|
|
case BO_Assign:
|
|
|
|
case BO_Comma:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Not valid compound assignment operators");
|
2010-04-23 12:16:32 +08:00
|
|
|
}
|
2013-07-26 14:16:11 +08:00
|
|
|
|
2010-04-23 12:16:32 +08:00
|
|
|
llvm_unreachable("Unhandled compound assignment operator");
|
|
|
|
}
|