CGExprScalar.cpp revision 745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bb
19fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===// 29fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream// 39fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream// The LLVM Compiler Infrastructure 49fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream// 59fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream// This file is distributed under the University of Illinois Open Source 69fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream// License. See LICENSE.TXT for details. 79fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream// 89fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream//===----------------------------------------------------------------------===// 99fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream// 109fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream// This contains code to emit Expr nodes with scalar LLVM types as LLVM code. 119fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream// 129fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream//===----------------------------------------------------------------------===// 139fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 149fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "CodeGenFunction.h" 159fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "CGCXXABI.h" 169fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "CGObjCRuntime.h" 179fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "CodeGenModule.h" 189fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "clang/AST/ASTContext.h" 199fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "clang/AST/DeclObjC.h" 209fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "clang/AST/RecordLayout.h" 219fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "clang/AST/StmtVisitor.h" 229fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "clang/Basic/TargetInfo.h" 239fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "llvm/Constants.h" 249fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "llvm/Function.h" 259fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "llvm/GlobalVariable.h" 269fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "llvm/Intrinsics.h" 279fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "llvm/Module.h" 289fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "llvm/Support/CFG.h" 299fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include "llvm/Target/TargetData.h" 309fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream#include <cstdarg> 319fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 329fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstreamusing namespace clang; 339fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstreamusing namespace CodeGen; 349fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstreamusing llvm::Value; 359fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 369fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream//===----------------------------------------------------------------------===// 379fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream// Scalar Expression Emitter 389fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream//===----------------------------------------------------------------------===// 399fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 409fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstreamstruct BinOpInfo { 419fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *LHS; 429fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *RHS; 439fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream QualType Ty; // Computation Type. 449fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream BinaryOperator::Opcode Opcode; // Opcode of BinOp to perform 459fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream const Expr *E; // Entire expr, for error unsupported. May not be binop. 469fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream}; 479fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 489fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstreamnamespace { 499fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstreamclass ScalarExprEmitter 509fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream : public StmtVisitor<ScalarExprEmitter, Value*> { 519fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream CodeGenFunction &CGF; 529fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream CGBuilderTy &Builder; 539fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream bool IgnoreResultAssign; 549fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream llvm::LLVMContext &VMContext; 559fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstreampublic: 569fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 579fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false) 589fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira), 599fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream VMContext(cgf.getLLVMContext()) { 609fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 619fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 629fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream //===--------------------------------------------------------------------===// 639fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream // Utilities 649fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream //===--------------------------------------------------------------------===// 659fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 669fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream bool TestAndClearIgnoreResultAssign() { 679fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream bool I = IgnoreResultAssign; 689fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream IgnoreResultAssign = false; 699fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return I; 709fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 719fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 729fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } 739fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } 749fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream LValue EmitCheckedLValue(const Expr *E) { return CGF.EmitCheckedLValue(E); } 759fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 769fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *EmitLoadOfLValue(LValue LV, QualType T) { 779fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return CGF.EmitLoadOfLValue(LV, T).getScalarVal(); 789fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 799fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 809fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// EmitLoadOfLValue - Given an expression with complex type that represents a 819fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// value l-value, this method emits the address of the l-value, then loads 829fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// and returns the result. 839fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *EmitLoadOfLValue(const Expr *E) { 849fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return EmitLoadOfLValue(EmitCheckedLValue(E), E->getType()); 859fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 869fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 879fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// EmitConversionToBool - Convert the specified expression value to a 889fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// boolean (i1) truth value. This is equivalent to "Val != 0". 899fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *EmitConversionToBool(Value *Src, QualType DstTy); 909fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 919fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// EmitScalarConversion - Emit a conversion from the specified type to the 929fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// specified destination type, both of which are LLVM scalar types. 939fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy); 949fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 959fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// EmitComplexToScalarConversion - Emit a conversion from the specified 969fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// complex type to the specified destination type, where the destination type 979fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// is an LLVM scalar type. 989fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, 999fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream QualType SrcTy, QualType DstTy); 1009fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 1019fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream /// EmitNullValue - Emit a value that corresponds to null for the given type. 1029fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *EmitNullValue(QualType Ty); 1039fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 1049fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream //===--------------------------------------------------------------------===// 1059fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream // Visitor Methods 1069fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream //===--------------------------------------------------------------------===// 1079fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 1089fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *Visit(Expr *E) { 1099fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream llvm::DenseMap<const Expr *, llvm::Value *>::iterator I = 1109fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream CGF.ConditionalSaveExprs.find(E); 1119fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream if (I != CGF.ConditionalSaveExprs.end()) 1129fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return I->second; 1139fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 1149fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return StmtVisitor<ScalarExprEmitter, Value*>::Visit(E); 1159fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1169fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 1179fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitStmt(Stmt *S) { 1189fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream S->dump(CGF.getContext().getSourceManager()); 1199fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream assert(0 && "Stmt can't have complex result type!"); 1209fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return 0; 1219fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1229fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitExpr(Expr *S); 1239fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 1249fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitParenExpr(ParenExpr *PE) { 1259fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return Visit(PE->getSubExpr()); 1269fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1279fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 1289fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream // Leaves. 1299fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitIntegerLiteral(const IntegerLiteral *E) { 1309fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return llvm::ConstantInt::get(VMContext, E->getValue()); 1319fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1329fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitFloatingLiteral(const FloatingLiteral *E) { 1339fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return llvm::ConstantFP::get(VMContext, E->getValue()); 1349fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1359fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitCharacterLiteral(const CharacterLiteral *E) { 1369fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); 1379fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1389fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { 1399fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); 1409fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1419fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { 1429fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return EmitNullValue(E->getType()); 1439fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1449fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitGNUNullExpr(const GNUNullExpr *E) { 1459fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return EmitNullValue(E->getType()); 1469fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1479fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { 1489fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return llvm::ConstantInt::get(ConvertType(E->getType()), 1499fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream CGF.getContext().typesAreCompatible( 1509fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream E->getArgType1(), E->getArgType2())); 1519fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1529fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitOffsetOfExpr(OffsetOfExpr *E); 1539fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); 1549fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitAddrLabelExpr(const AddrLabelExpr *E) { 1559fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel()); 1569fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return Builder.CreateBitCast(V, ConvertType(E->getType())); 1579fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1589fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 1599fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream // l-values. 1609fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitDeclRefExpr(DeclRefExpr *E) { 1619fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Expr::EvalResult Result; 1629fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream if (E->Evaluate(Result, CGF.getContext()) && Result.Val.isInt()) { 1639fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream assert(!Result.HasSideEffects && "Constant declref with side-effect?!"); 1649fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream llvm::ConstantInt *CI 1659fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream = llvm::ConstantInt::get(VMContext, Result.Val.getInt()); 1669fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream CGF.EmitDeclRefExprDbgValue(E, CI); 1679fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return CI; 1689fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1699fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return EmitLoadOfLValue(E); 1709fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1719fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) { 1729fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return CGF.EmitObjCSelectorExpr(E); 1739fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1749fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) { 1759fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return CGF.EmitObjCProtocolExpr(E); 1769fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1779fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 1789fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return EmitLoadOfLValue(E); 1799fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1809fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { 1819fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return EmitLoadOfLValue(E); 1829fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1839fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitObjCImplicitSetterGetterRefExpr( 1849fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream ObjCImplicitSetterGetterRefExpr *E) { 1859fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return EmitLoadOfLValue(E); 1869fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1879fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitObjCMessageExpr(ObjCMessageExpr *E) { 1889fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return CGF.EmitObjCMessageExpr(E).getScalarVal(); 1899fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1909fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 1919fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitObjCIsaExpr(ObjCIsaExpr *E) { 1929fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream LValue LV = CGF.EmitObjCIsaExpr(E); 1939fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *V = CGF.EmitLoadOfLValue(LV, E->getType()).getScalarVal(); 1949fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return V; 1959fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 1969fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 1979fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); 1989fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E); 1999fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitMemberExpr(MemberExpr *E); 2009fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } 2019fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 2029fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return EmitLoadOfLValue(E); 2039fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 2049fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 2059fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitInitListExpr(InitListExpr *E); 2069fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 2079fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { 2089fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return CGF.CGM.EmitNullConstant(E->getType()); 2099fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 2109fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream Value *VisitCastExpr(CastExpr *E) { 2119fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream // Make sure to evaluate VLA bounds now so that we have them for later. 2129fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream if (E->getType()->isVariablyModifiedType()) 2139fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream CGF.EmitVLASize(E->getType()); 2149fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream 2159fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream return EmitCastExpr(E); 2169fd67c44777b350dc56f3e70c88963b0d966ffc7quake upstream } 217 Value *EmitCastExpr(CastExpr *E); 218 219 Value *VisitCallExpr(const CallExpr *E) { 220 if (E->getCallReturnType()->isReferenceType()) 221 return EmitLoadOfLValue(E); 222 223 return CGF.EmitCallExpr(E).getScalarVal(); 224 } 225 226 Value *VisitStmtExpr(const StmtExpr *E); 227 228 Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E); 229 230 // Unary Operators. 231 Value *VisitUnaryPostDec(const UnaryOperator *E) { 232 LValue LV = EmitLValue(E->getSubExpr()); 233 return EmitScalarPrePostIncDec(E, LV, false, false); 234 } 235 Value *VisitUnaryPostInc(const UnaryOperator *E) { 236 LValue LV = EmitLValue(E->getSubExpr()); 237 return EmitScalarPrePostIncDec(E, LV, true, false); 238 } 239 Value *VisitUnaryPreDec(const UnaryOperator *E) { 240 LValue LV = EmitLValue(E->getSubExpr()); 241 return EmitScalarPrePostIncDec(E, LV, false, true); 242 } 243 Value *VisitUnaryPreInc(const UnaryOperator *E) { 244 LValue LV = EmitLValue(E->getSubExpr()); 245 return EmitScalarPrePostIncDec(E, LV, true, true); 246 } 247 248 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 249 bool isInc, bool isPre); 250 251 252 Value *VisitUnaryAddrOf(const UnaryOperator *E) { 253 // If the sub-expression is an instance member reference, 254 // EmitDeclRefLValue will magically emit it with the appropriate 255 // value as the "address". 256 return EmitLValue(E->getSubExpr()).getAddress(); 257 } 258 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } 259 Value *VisitUnaryPlus(const UnaryOperator *E) { 260 // This differs from gcc, though, most likely due to a bug in gcc. 261 TestAndClearIgnoreResultAssign(); 262 return Visit(E->getSubExpr()); 263 } 264 Value *VisitUnaryMinus (const UnaryOperator *E); 265 Value *VisitUnaryNot (const UnaryOperator *E); 266 Value *VisitUnaryLNot (const UnaryOperator *E); 267 Value *VisitUnaryReal (const UnaryOperator *E); 268 Value *VisitUnaryImag (const UnaryOperator *E); 269 Value *VisitUnaryExtension(const UnaryOperator *E) { 270 return Visit(E->getSubExpr()); 271 } 272 273 // C++ 274 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 275 return Visit(DAE->getExpr()); 276 } 277 Value *VisitCXXThisExpr(CXXThisExpr *TE) { 278 return CGF.LoadCXXThis(); 279 } 280 281 Value *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { 282 return CGF.EmitCXXExprWithTemporaries(E).getScalarVal(); 283 } 284 Value *VisitCXXNewExpr(const CXXNewExpr *E) { 285 return CGF.EmitCXXNewExpr(E); 286 } 287 Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) { 288 CGF.EmitCXXDeleteExpr(E); 289 return 0; 290 } 291 Value *VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { 292 return llvm::ConstantInt::get(Builder.getInt1Ty(), E->getValue()); 293 } 294 295 Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) { 296 // C++ [expr.pseudo]p1: 297 // The result shall only be used as the operand for the function call 298 // operator (), and the result of such a call has type void. The only 299 // effect is the evaluation of the postfix-expression before the dot or 300 // arrow. 301 CGF.EmitScalarExpr(E->getBase()); 302 return 0; 303 } 304 305 Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { 306 return EmitNullValue(E->getType()); 307 } 308 309 Value *VisitCXXThrowExpr(const CXXThrowExpr *E) { 310 CGF.EmitCXXThrowExpr(E); 311 return 0; 312 } 313 314 Value *VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { 315 return llvm::ConstantInt::get(Builder.getInt1Ty(), E->getValue()); 316 } 317 318 // Binary Operators. 319 Value *EmitMul(const BinOpInfo &Ops) { 320 if (Ops.Ty->hasSignedIntegerRepresentation()) { 321 switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { 322 case LangOptions::SOB_Undefined: 323 return Builder.CreateNSWMul(Ops.LHS, Ops.RHS, "mul"); 324 case LangOptions::SOB_Defined: 325 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); 326 case LangOptions::SOB_Trapping: 327 return EmitOverflowCheckedBinOp(Ops); 328 } 329 } 330 331 if (Ops.LHS->getType()->isFPOrFPVectorTy()) 332 return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul"); 333 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); 334 } 335 bool isTrapvOverflowBehavior() { 336 return CGF.getContext().getLangOptions().getSignedOverflowBehavior() 337 == LangOptions::SOB_Trapping; 338 } 339 /// Create a binary op that checks for overflow. 340 /// Currently only supports +, - and *. 341 Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops); 342 // Emit the overflow BB when -ftrapv option is activated. 343 void EmitOverflowBB(llvm::BasicBlock *overflowBB) { 344 Builder.SetInsertPoint(overflowBB); 345 llvm::Function *Trap = CGF.CGM.getIntrinsic(llvm::Intrinsic::trap); 346 Builder.CreateCall(Trap); 347 Builder.CreateUnreachable(); 348 } 349 // Check for undefined division and modulus behaviors. 350 void EmitUndefinedBehaviorIntegerDivAndRemCheck(const BinOpInfo &Ops, 351 llvm::Value *Zero,bool isDiv); 352 Value *EmitDiv(const BinOpInfo &Ops); 353 Value *EmitRem(const BinOpInfo &Ops); 354 Value *EmitAdd(const BinOpInfo &Ops); 355 Value *EmitSub(const BinOpInfo &Ops); 356 Value *EmitShl(const BinOpInfo &Ops); 357 Value *EmitShr(const BinOpInfo &Ops); 358 Value *EmitAnd(const BinOpInfo &Ops) { 359 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); 360 } 361 Value *EmitXor(const BinOpInfo &Ops) { 362 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); 363 } 364 Value *EmitOr (const BinOpInfo &Ops) { 365 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); 366 } 367 368 BinOpInfo EmitBinOps(const BinaryOperator *E); 369 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E, 370 Value *(ScalarExprEmitter::*F)(const BinOpInfo &), 371 Value *&Result); 372 373 Value *EmitCompoundAssign(const CompoundAssignOperator *E, 374 Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); 375 376 // Binary operators and binary compound assignment operators. 377#define HANDLEBINOP(OP) \ 378 Value *VisitBin ## OP(const BinaryOperator *E) { \ 379 return Emit ## OP(EmitBinOps(E)); \ 380 } \ 381 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ 382 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ 383 } 384 HANDLEBINOP(Mul) 385 HANDLEBINOP(Div) 386 HANDLEBINOP(Rem) 387 HANDLEBINOP(Add) 388 HANDLEBINOP(Sub) 389 HANDLEBINOP(Shl) 390 HANDLEBINOP(Shr) 391 HANDLEBINOP(And) 392 HANDLEBINOP(Xor) 393 HANDLEBINOP(Or) 394#undef HANDLEBINOP 395 396 // Comparisons. 397 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, 398 unsigned SICmpOpc, unsigned FCmpOpc); 399#define VISITCOMP(CODE, UI, SI, FP) \ 400 Value *VisitBin##CODE(const BinaryOperator *E) { \ 401 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ 402 llvm::FCmpInst::FP); } 403 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT) 404 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT) 405 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE) 406 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE) 407 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ) 408 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE) 409#undef VISITCOMP 410 411 Value *VisitBinAssign (const BinaryOperator *E); 412 413 Value *VisitBinLAnd (const BinaryOperator *E); 414 Value *VisitBinLOr (const BinaryOperator *E); 415 Value *VisitBinComma (const BinaryOperator *E); 416 417 Value *VisitBinPtrMemD(const Expr *E) { return EmitLoadOfLValue(E); } 418 Value *VisitBinPtrMemI(const Expr *E) { return EmitLoadOfLValue(E); } 419 420 // Other Operators. 421 Value *VisitBlockExpr(const BlockExpr *BE); 422 Value *VisitConditionalOperator(const ConditionalOperator *CO); 423 Value *VisitChooseExpr(ChooseExpr *CE); 424 Value *VisitVAArgExpr(VAArgExpr *VE); 425 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { 426 return CGF.EmitObjCStringLiteral(E); 427 } 428}; 429} // end anonymous namespace. 430 431//===----------------------------------------------------------------------===// 432// Utilities 433//===----------------------------------------------------------------------===// 434 435/// EmitConversionToBool - Convert the specified expression value to a 436/// boolean (i1) truth value. This is equivalent to "Val != 0". 437Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { 438 assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs"); 439 440 if (SrcType->isRealFloatingType()) { 441 // Compare against 0.0 for fp scalars. 442 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); 443 return Builder.CreateFCmpUNE(Src, Zero, "tobool"); 444 } 445 446 if (const MemberPointerType *MPT = dyn_cast<MemberPointerType>(SrcType)) 447 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, Src, MPT); 448 449 assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) && 450 "Unknown scalar type to convert"); 451 452 // Because of the type rules of C, we often end up computing a logical value, 453 // then zero extending it to int, then wanting it as a logical value again. 454 // Optimize this common case. 455 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) { 456 if (ZI->getOperand(0)->getType() == 457 llvm::Type::getInt1Ty(CGF.getLLVMContext())) { 458 Value *Result = ZI->getOperand(0); 459 // If there aren't any more uses, zap the instruction to save space. 460 // Note that there can be more uses, for example if this 461 // is the result of an assignment. 462 if (ZI->use_empty()) 463 ZI->eraseFromParent(); 464 return Result; 465 } 466 } 467 468 // Compare against an integer or pointer null. 469 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); 470 return Builder.CreateICmpNE(Src, Zero, "tobool"); 471} 472 473/// EmitScalarConversion - Emit a conversion from the specified type to the 474/// specified destination type, both of which are LLVM scalar types. 475Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, 476 QualType DstType) { 477 SrcType = CGF.getContext().getCanonicalType(SrcType); 478 DstType = CGF.getContext().getCanonicalType(DstType); 479 if (SrcType == DstType) return Src; 480 481 if (DstType->isVoidType()) return 0; 482 483 // Handle conversions to bool first, they are special: comparisons against 0. 484 if (DstType->isBooleanType()) 485 return EmitConversionToBool(Src, SrcType); 486 487 const llvm::Type *DstTy = ConvertType(DstType); 488 489 // Ignore conversions like int -> uint. 490 if (Src->getType() == DstTy) 491 return Src; 492 493 // Handle pointer conversions next: pointers can only be converted to/from 494 // other pointers and integers. Check for pointer types in terms of LLVM, as 495 // some native types (like Obj-C id) may map to a pointer type. 496 if (isa<llvm::PointerType>(DstTy)) { 497 // The source value may be an integer, or a pointer. 498 if (isa<llvm::PointerType>(Src->getType())) 499 return Builder.CreateBitCast(Src, DstTy, "conv"); 500 501 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); 502 // First, convert to the correct width so that we control the kind of 503 // extension. 504 const llvm::Type *MiddleTy = CGF.IntPtrTy; 505 bool InputSigned = SrcType->isSignedIntegerType(); 506 llvm::Value* IntResult = 507 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); 508 // Then, cast to pointer. 509 return Builder.CreateIntToPtr(IntResult, DstTy, "conv"); 510 } 511 512 if (isa<llvm::PointerType>(Src->getType())) { 513 // Must be an ptr to int cast. 514 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); 515 return Builder.CreatePtrToInt(Src, DstTy, "conv"); 516 } 517 518 // A scalar can be splatted to an extended vector of the same element type 519 if (DstType->isExtVectorType() && !SrcType->isVectorType()) { 520 // Cast the scalar to element type 521 QualType EltTy = DstType->getAs<ExtVectorType>()->getElementType(); 522 llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy); 523 524 // Insert the element in element zero of an undef vector 525 llvm::Value *UnV = llvm::UndefValue::get(DstTy); 526 llvm::Value *Idx = llvm::ConstantInt::get(CGF.Int32Ty, 0); 527 UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp"); 528 529 // Splat the element across to all elements 530 llvm::SmallVector<llvm::Constant*, 16> Args; 531 unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); 532 for (unsigned i = 0; i < NumElements; i++) 533 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 0)); 534 535 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); 536 llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); 537 return Yay; 538 } 539 540 // Allow bitcast from vector to integer/fp of the same size. 541 if (isa<llvm::VectorType>(Src->getType()) || 542 isa<llvm::VectorType>(DstTy)) 543 return Builder.CreateBitCast(Src, DstTy, "conv"); 544 545 // Finally, we have the arithmetic types: real int/float. 546 if (isa<llvm::IntegerType>(Src->getType())) { 547 bool InputSigned = SrcType->isSignedIntegerType(); 548 if (isa<llvm::IntegerType>(DstTy)) 549 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); 550 else if (InputSigned) 551 return Builder.CreateSIToFP(Src, DstTy, "conv"); 552 else 553 return Builder.CreateUIToFP(Src, DstTy, "conv"); 554 } 555 556 assert(Src->getType()->isFloatingPointTy() && "Unknown real conversion"); 557 if (isa<llvm::IntegerType>(DstTy)) { 558 if (DstType->isSignedIntegerType()) 559 return Builder.CreateFPToSI(Src, DstTy, "conv"); 560 else 561 return Builder.CreateFPToUI(Src, DstTy, "conv"); 562 } 563 564 assert(DstTy->isFloatingPointTy() && "Unknown real conversion"); 565 if (DstTy->getTypeID() < Src->getType()->getTypeID()) 566 return Builder.CreateFPTrunc(Src, DstTy, "conv"); 567 else 568 return Builder.CreateFPExt(Src, DstTy, "conv"); 569} 570 571/// EmitComplexToScalarConversion - Emit a conversion from the specified complex 572/// type to the specified destination type, where the destination type is an 573/// LLVM scalar type. 574Value *ScalarExprEmitter:: 575EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, 576 QualType SrcTy, QualType DstTy) { 577 // Get the source element type. 578 SrcTy = SrcTy->getAs<ComplexType>()->getElementType(); 579 580 // Handle conversions to bool first, they are special: comparisons against 0. 581 if (DstTy->isBooleanType()) { 582 // Complex != 0 -> (Real != 0) | (Imag != 0) 583 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); 584 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); 585 return Builder.CreateOr(Src.first, Src.second, "tobool"); 586 } 587 588 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, 589 // the imaginary part of the complex value is discarded and the value of the 590 // real part is converted according to the conversion rules for the 591 // corresponding real type. 592 return EmitScalarConversion(Src.first, SrcTy, DstTy); 593} 594 595Value *ScalarExprEmitter::EmitNullValue(QualType Ty) { 596 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) 597 return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT); 598 599 return llvm::Constant::getNullValue(ConvertType(Ty)); 600} 601 602//===----------------------------------------------------------------------===// 603// Visitor Methods 604//===----------------------------------------------------------------------===// 605 606Value *ScalarExprEmitter::VisitExpr(Expr *E) { 607 CGF.ErrorUnsupported(E, "scalar expression"); 608 if (E->getType()->isVoidType()) 609 return 0; 610 return llvm::UndefValue::get(CGF.ConvertType(E->getType())); 611} 612 613Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { 614 // Vector Mask Case 615 if (E->getNumSubExprs() == 2 || 616 (E->getNumSubExprs() == 3 && E->getExpr(2)->getType()->isVectorType())) { 617 Value *LHS = CGF.EmitScalarExpr(E->getExpr(0)); 618 Value *RHS = CGF.EmitScalarExpr(E->getExpr(1)); 619 Value *Mask; 620 621 const llvm::VectorType *LTy = cast<llvm::VectorType>(LHS->getType()); 622 unsigned LHSElts = LTy->getNumElements(); 623 624 if (E->getNumSubExprs() == 3) { 625 Mask = CGF.EmitScalarExpr(E->getExpr(2)); 626 627 // Shuffle LHS & RHS into one input vector. 628 llvm::SmallVector<llvm::Constant*, 32> concat; 629 for (unsigned i = 0; i != LHSElts; ++i) { 630 concat.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 2*i)); 631 concat.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 2*i+1)); 632 } 633 634 Value* CV = llvm::ConstantVector::get(concat.begin(), concat.size()); 635 LHS = Builder.CreateShuffleVector(LHS, RHS, CV, "concat"); 636 LHSElts *= 2; 637 } else { 638 Mask = RHS; 639 } 640 641 const llvm::VectorType *MTy = cast<llvm::VectorType>(Mask->getType()); 642 llvm::Constant* EltMask; 643 644 // Treat vec3 like vec4. 645 if ((LHSElts == 6) && (E->getNumSubExprs() == 3)) 646 EltMask = llvm::ConstantInt::get(MTy->getElementType(), 647 (1 << llvm::Log2_32(LHSElts+2))-1); 648 else if ((LHSElts == 3) && (E->getNumSubExprs() == 2)) 649 EltMask = llvm::ConstantInt::get(MTy->getElementType(), 650 (1 << llvm::Log2_32(LHSElts+1))-1); 651 else 652 EltMask = llvm::ConstantInt::get(MTy->getElementType(), 653 (1 << llvm::Log2_32(LHSElts))-1); 654 655 // Mask off the high bits of each shuffle index. 656 llvm::SmallVector<llvm::Constant *, 32> MaskV; 657 for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) 658 MaskV.push_back(EltMask); 659 660 Value* MaskBits = llvm::ConstantVector::get(MaskV.begin(), MaskV.size()); 661 Mask = Builder.CreateAnd(Mask, MaskBits, "mask"); 662 663 // newv = undef 664 // mask = mask & maskbits 665 // for each elt 666 // n = extract mask i 667 // x = extract val n 668 // newv = insert newv, x, i 669 const llvm::VectorType *RTy = llvm::VectorType::get(LTy->getElementType(), 670 MTy->getNumElements()); 671 Value* NewV = llvm::UndefValue::get(RTy); 672 for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) { 673 Value *Indx = llvm::ConstantInt::get(CGF.Int32Ty, i); 674 Indx = Builder.CreateExtractElement(Mask, Indx, "shuf_idx"); 675 Indx = Builder.CreateZExt(Indx, CGF.Int32Ty, "idx_zext"); 676 677 // Handle vec3 special since the index will be off by one for the RHS. 678 if ((LHSElts == 6) && (E->getNumSubExprs() == 3)) { 679 Value *cmpIndx, *newIndx; 680 cmpIndx = Builder.CreateICmpUGT(Indx, 681 llvm::ConstantInt::get(CGF.Int32Ty, 3), 682 "cmp_shuf_idx"); 683 newIndx = Builder.CreateSub(Indx, llvm::ConstantInt::get(CGF.Int32Ty,1), 684 "shuf_idx_adj"); 685 Indx = Builder.CreateSelect(cmpIndx, newIndx, Indx, "sel_shuf_idx"); 686 } 687 Value *VExt = Builder.CreateExtractElement(LHS, Indx, "shuf_elt"); 688 NewV = Builder.CreateInsertElement(NewV, VExt, Indx, "shuf_ins"); 689 } 690 return NewV; 691 } 692 693 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0)); 694 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1)); 695 696 // Handle vec3 special since the index will be off by one for the RHS. 697 llvm::SmallVector<llvm::Constant*, 32> indices; 698 for (unsigned i = 2; i < E->getNumSubExprs(); i++) { 699 llvm::Constant *C = cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))); 700 const llvm::VectorType *VTy = cast<llvm::VectorType>(V1->getType()); 701 if (VTy->getNumElements() == 3) { 702 if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) { 703 uint64_t cVal = CI->getZExtValue(); 704 if (cVal > 3) { 705 C = llvm::ConstantInt::get(C->getType(), cVal-1); 706 } 707 } 708 } 709 indices.push_back(C); 710 } 711 712 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size()); 713 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle"); 714} 715Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) { 716 Expr::EvalResult Result; 717 if (E->Evaluate(Result, CGF.getContext()) && Result.Val.isInt()) { 718 if (E->isArrow()) 719 CGF.EmitScalarExpr(E->getBase()); 720 else 721 EmitLValue(E->getBase()); 722 return llvm::ConstantInt::get(VMContext, Result.Val.getInt()); 723 } 724 return EmitLoadOfLValue(E); 725} 726 727Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 728 TestAndClearIgnoreResultAssign(); 729 730 // Emit subscript expressions in rvalue context's. For most cases, this just 731 // loads the lvalue formed by the subscript expr. However, we have to be 732 // careful, because the base of a vector subscript is occasionally an rvalue, 733 // so we can't get it as an lvalue. 734 if (!E->getBase()->getType()->isVectorType()) 735 return EmitLoadOfLValue(E); 736 737 // Handle the vector case. The base must be a vector, the index must be an 738 // integer value. 739 Value *Base = Visit(E->getBase()); 740 Value *Idx = Visit(E->getIdx()); 741 bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType(); 742 Idx = Builder.CreateIntCast(Idx, CGF.Int32Ty, IdxSigned, "vecidxcast"); 743 return Builder.CreateExtractElement(Base, Idx, "vecext"); 744} 745 746static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx, 747 unsigned Off, const llvm::Type *I32Ty) { 748 int MV = SVI->getMaskValue(Idx); 749 if (MV == -1) 750 return llvm::UndefValue::get(I32Ty); 751 return llvm::ConstantInt::get(I32Ty, Off+MV); 752} 753 754Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) { 755 bool Ignore = TestAndClearIgnoreResultAssign(); 756 (void)Ignore; 757 assert (Ignore == false && "init list ignored"); 758 unsigned NumInitElements = E->getNumInits(); 759 760 if (E->hadArrayRangeDesignator()) 761 CGF.ErrorUnsupported(E, "GNU array range designator extension"); 762 763 const llvm::VectorType *VType = 764 dyn_cast<llvm::VectorType>(ConvertType(E->getType())); 765 766 // We have a scalar in braces. Just use the first element. 767 if (!VType) 768 return Visit(E->getInit(0)); 769 770 unsigned ResElts = VType->getNumElements(); 771 772 // Loop over initializers collecting the Value for each, and remembering 773 // whether the source was swizzle (ExtVectorElementExpr). This will allow 774 // us to fold the shuffle for the swizzle into the shuffle for the vector 775 // initializer, since LLVM optimizers generally do not want to touch 776 // shuffles. 777 unsigned CurIdx = 0; 778 bool VIsUndefShuffle = false; 779 llvm::Value *V = llvm::UndefValue::get(VType); 780 for (unsigned i = 0; i != NumInitElements; ++i) { 781 Expr *IE = E->getInit(i); 782 Value *Init = Visit(IE); 783 llvm::SmallVector<llvm::Constant*, 16> Args; 784 785 const llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType()); 786 787 // Handle scalar elements. If the scalar initializer is actually one 788 // element of a different vector of the same width, use shuffle instead of 789 // extract+insert. 790 if (!VVT) { 791 if (isa<ExtVectorElementExpr>(IE)) { 792 llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init); 793 794 if (EI->getVectorOperandType()->getNumElements() == ResElts) { 795 llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand()); 796 Value *LHS = 0, *RHS = 0; 797 if (CurIdx == 0) { 798 // insert into undef -> shuffle (src, undef) 799 Args.push_back(C); 800 for (unsigned j = 1; j != ResElts; ++j) 801 Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); 802 803 LHS = EI->getVectorOperand(); 804 RHS = V; 805 VIsUndefShuffle = true; 806 } else if (VIsUndefShuffle) { 807 // insert into undefshuffle && size match -> shuffle (v, src) 808 llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V); 809 for (unsigned j = 0; j != CurIdx; ++j) 810 Args.push_back(getMaskElt(SVV, j, 0, CGF.Int32Ty)); 811 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 812 ResElts + C->getZExtValue())); 813 for (unsigned j = CurIdx + 1; j != ResElts; ++j) 814 Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); 815 816 LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); 817 RHS = EI->getVectorOperand(); 818 VIsUndefShuffle = false; 819 } 820 if (!Args.empty()) { 821 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); 822 V = Builder.CreateShuffleVector(LHS, RHS, Mask); 823 ++CurIdx; 824 continue; 825 } 826 } 827 } 828 Value *Idx = llvm::ConstantInt::get(CGF.Int32Ty, CurIdx); 829 V = Builder.CreateInsertElement(V, Init, Idx, "vecinit"); 830 VIsUndefShuffle = false; 831 ++CurIdx; 832 continue; 833 } 834 835 unsigned InitElts = VVT->getNumElements(); 836 837 // If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's 838 // input is the same width as the vector being constructed, generate an 839 // optimized shuffle of the swizzle input into the result. 840 unsigned Offset = (CurIdx == 0) ? 0 : ResElts; 841 if (isa<ExtVectorElementExpr>(IE)) { 842 llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init); 843 Value *SVOp = SVI->getOperand(0); 844 const llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType()); 845 846 if (OpTy->getNumElements() == ResElts) { 847 for (unsigned j = 0; j != CurIdx; ++j) { 848 // If the current vector initializer is a shuffle with undef, merge 849 // this shuffle directly into it. 850 if (VIsUndefShuffle) { 851 Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0, 852 CGF.Int32Ty)); 853 } else { 854 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, j)); 855 } 856 } 857 for (unsigned j = 0, je = InitElts; j != je; ++j) 858 Args.push_back(getMaskElt(SVI, j, Offset, CGF.Int32Ty)); 859 for (unsigned j = CurIdx + InitElts; j != ResElts; ++j) 860 Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); 861 862 if (VIsUndefShuffle) 863 V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); 864 865 Init = SVOp; 866 } 867 } 868 869 // Extend init to result vector length, and then shuffle its contribution 870 // to the vector initializer into V. 871 if (Args.empty()) { 872 for (unsigned j = 0; j != InitElts; ++j) 873 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, j)); 874 for (unsigned j = InitElts; j != ResElts; ++j) 875 Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); 876 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); 877 Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT), 878 Mask, "vext"); 879 880 Args.clear(); 881 for (unsigned j = 0; j != CurIdx; ++j) 882 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, j)); 883 for (unsigned j = 0; j != InitElts; ++j) 884 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, j+Offset)); 885 for (unsigned j = CurIdx + InitElts; j != ResElts; ++j) 886 Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); 887 } 888 889 // If V is undef, make sure it ends up on the RHS of the shuffle to aid 890 // merging subsequent shuffles into this one. 891 if (CurIdx == 0) 892 std::swap(V, Init); 893 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); 894 V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit"); 895 VIsUndefShuffle = isa<llvm::UndefValue>(Init); 896 CurIdx += InitElts; 897 } 898 899 // FIXME: evaluate codegen vs. shuffling against constant null vector. 900 // Emit remaining default initializers. 901 const llvm::Type *EltTy = VType->getElementType(); 902 903 // Emit remaining default initializers 904 for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) { 905 Value *Idx = llvm::ConstantInt::get(CGF.Int32Ty, CurIdx); 906 llvm::Value *Init = llvm::Constant::getNullValue(EltTy); 907 V = Builder.CreateInsertElement(V, Init, Idx, "vecinit"); 908 } 909 return V; 910} 911 912static bool ShouldNullCheckClassCastValue(const CastExpr *CE) { 913 const Expr *E = CE->getSubExpr(); 914 915 if (CE->getCastKind() == CK_UncheckedDerivedToBase) 916 return false; 917 918 if (isa<CXXThisExpr>(E)) { 919 // We always assume that 'this' is never null. 920 return false; 921 } 922 923 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) { 924 // And that glvalue casts are never null. 925 if (ICE->getValueKind() != VK_RValue) 926 return false; 927 } 928 929 return true; 930} 931 932// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts 933// have to handle a more broad range of conversions than explicit casts, as they 934// handle things like function to ptr-to-function decay etc. 935Value *ScalarExprEmitter::EmitCastExpr(CastExpr *CE) { 936 Expr *E = CE->getSubExpr(); 937 QualType DestTy = CE->getType(); 938 CastKind Kind = CE->getCastKind(); 939 940 if (!DestTy->isVoidType()) 941 TestAndClearIgnoreResultAssign(); 942 943 // Since almost all cast kinds apply to scalars, this switch doesn't have 944 // a default case, so the compiler will warn on a missing case. The cases 945 // are in the same order as in the CastKind enum. 946 switch (Kind) { 947 case CK_Unknown: 948 // FIXME: All casts should have a known kind! 949 //assert(0 && "Unknown cast kind!"); 950 break; 951 952 case CK_LValueBitCast: 953 case CK_ObjCObjectLValueCast: { 954 Value *V = EmitLValue(E).getAddress(); 955 V = Builder.CreateBitCast(V, 956 ConvertType(CGF.getContext().getPointerType(DestTy))); 957 return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), DestTy); 958 } 959 960 case CK_AnyPointerToObjCPointerCast: 961 case CK_AnyPointerToBlockPointerCast: 962 case CK_BitCast: { 963 Value *Src = Visit(const_cast<Expr*>(E)); 964 return Builder.CreateBitCast(Src, ConvertType(DestTy)); 965 } 966 case CK_NoOp: 967 case CK_UserDefinedConversion: 968 return Visit(const_cast<Expr*>(E)); 969 970 case CK_BaseToDerived: { 971 const CXXRecordDecl *DerivedClassDecl = 972 DestTy->getCXXRecordDeclForPointerType(); 973 974 return CGF.GetAddressOfDerivedClass(Visit(E), DerivedClassDecl, 975 CE->path_begin(), CE->path_end(), 976 ShouldNullCheckClassCastValue(CE)); 977 } 978 case CK_UncheckedDerivedToBase: 979 case CK_DerivedToBase: { 980 const RecordType *DerivedClassTy = 981 E->getType()->getAs<PointerType>()->getPointeeType()->getAs<RecordType>(); 982 CXXRecordDecl *DerivedClassDecl = 983 cast<CXXRecordDecl>(DerivedClassTy->getDecl()); 984 985 return CGF.GetAddressOfBaseClass(Visit(E), DerivedClassDecl, 986 CE->path_begin(), CE->path_end(), 987 ShouldNullCheckClassCastValue(CE)); 988 } 989 case CK_Dynamic: { 990 Value *V = Visit(const_cast<Expr*>(E)); 991 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE); 992 return CGF.EmitDynamicCast(V, DCE); 993 } 994 case CK_ToUnion: 995 assert(0 && "Should be unreachable!"); 996 break; 997 998 case CK_ArrayToPointerDecay: { 999 assert(E->getType()->isArrayType() && 1000 "Array to pointer decay must have array source type!"); 1001 1002 Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays. 1003 1004 // Note that VLA pointers are always decayed, so we don't need to do 1005 // anything here. 1006 if (!E->getType()->isVariableArrayType()) { 1007 assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer"); 1008 assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) 1009 ->getElementType()) && 1010 "Expected pointer to array"); 1011 V = Builder.CreateStructGEP(V, 0, "arraydecay"); 1012 } 1013 1014 return V; 1015 } 1016 case CK_FunctionToPointerDecay: 1017 return EmitLValue(E).getAddress(); 1018 1019 case CK_NullToMemberPointer: { 1020 // If the subexpression's type is the C++0x nullptr_t, emit the 1021 // subexpression, which may have side effects. 1022 if (E->getType()->isNullPtrType()) 1023 (void) Visit(E); 1024 1025 const MemberPointerType *MPT = CE->getType()->getAs<MemberPointerType>(); 1026 return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT); 1027 } 1028 1029 case CK_BaseToDerivedMemberPointer: 1030 case CK_DerivedToBaseMemberPointer: { 1031 Value *Src = Visit(E); 1032 1033 // Note that the AST doesn't distinguish between checked and 1034 // unchecked member pointer conversions, so we always have to 1035 // implement checked conversions here. This is inefficient when 1036 // actual control flow may be required in order to perform the 1037 // check, which it is for data member pointers (but not member 1038 // function pointers on Itanium and ARM). 1039 return CGF.CGM.getCXXABI().EmitMemberPointerConversion(CGF, CE, Src); 1040 } 1041 1042 1043 case CK_ConstructorConversion: 1044 assert(0 && "Should be unreachable!"); 1045 break; 1046 1047 case CK_IntegralToPointer: { 1048 Value *Src = Visit(const_cast<Expr*>(E)); 1049 1050 // First, convert to the correct width so that we control the kind of 1051 // extension. 1052 const llvm::Type *MiddleTy = CGF.IntPtrTy; 1053 bool InputSigned = E->getType()->isSignedIntegerType(); 1054 llvm::Value* IntResult = 1055 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); 1056 1057 return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy)); 1058 } 1059 case CK_PointerToIntegral: { 1060 Value *Src = Visit(const_cast<Expr*>(E)); 1061 1062 // Handle conversion to bool correctly. 1063 if (DestTy->isBooleanType()) 1064 return EmitScalarConversion(Src, E->getType(), DestTy); 1065 1066 return Builder.CreatePtrToInt(Src, ConvertType(DestTy)); 1067 } 1068 case CK_ToVoid: { 1069 if (E->Classify(CGF.getContext()).isGLValue()) { 1070 LValue LV = CGF.EmitLValue(E); 1071 if (LV.isPropertyRef()) 1072 CGF.EmitLoadOfPropertyRefLValue(LV, E->getType()); 1073 else if (LV.isKVCRef()) 1074 CGF.EmitLoadOfKVCRefLValue(LV, E->getType()); 1075 } 1076 else 1077 CGF.EmitAnyExpr(E, AggValueSlot::ignored(), true); 1078 return 0; 1079 } 1080 case CK_VectorSplat: { 1081 const llvm::Type *DstTy = ConvertType(DestTy); 1082 Value *Elt = Visit(const_cast<Expr*>(E)); 1083 1084 // Insert the element in element zero of an undef vector 1085 llvm::Value *UnV = llvm::UndefValue::get(DstTy); 1086 llvm::Value *Idx = llvm::ConstantInt::get(CGF.Int32Ty, 0); 1087 UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp"); 1088 1089 // Splat the element across to all elements 1090 llvm::SmallVector<llvm::Constant*, 16> Args; 1091 unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); 1092 for (unsigned i = 0; i < NumElements; i++) 1093 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 0)); 1094 1095 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); 1096 llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); 1097 return Yay; 1098 } 1099 case CK_IntegralCast: 1100 case CK_IntegralToFloating: 1101 case CK_FloatingToIntegral: 1102 case CK_FloatingCast: 1103 return EmitScalarConversion(Visit(E), E->getType(), DestTy); 1104 1105 case CK_MemberPointerToBoolean: { 1106 llvm::Value *MemPtr = Visit(E); 1107 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>(); 1108 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT); 1109 } 1110 } 1111 1112 // Handle cases where the source is an non-complex type. 1113 1114 if (!CGF.hasAggregateLLVMType(E->getType())) { 1115 Value *Src = Visit(const_cast<Expr*>(E)); 1116 1117 // Use EmitScalarConversion to perform the conversion. 1118 return EmitScalarConversion(Src, E->getType(), DestTy); 1119 } 1120 1121 if (E->getType()->isAnyComplexType()) { 1122 // Handle cases where the source is a complex type. 1123 bool IgnoreImag = true; 1124 bool IgnoreImagAssign = true; 1125 bool IgnoreReal = IgnoreResultAssign; 1126 bool IgnoreRealAssign = IgnoreResultAssign; 1127 if (DestTy->isBooleanType()) 1128 IgnoreImagAssign = IgnoreImag = false; 1129 else if (DestTy->isVoidType()) { 1130 IgnoreReal = IgnoreImag = false; 1131 IgnoreRealAssign = IgnoreImagAssign = true; 1132 } 1133 CodeGenFunction::ComplexPairTy V 1134 = CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign, 1135 IgnoreImagAssign); 1136 return EmitComplexToScalarConversion(V, E->getType(), DestTy); 1137 } 1138 1139 // Okay, this is a cast from an aggregate. It must be a cast to void. Just 1140 // evaluate the result and return. 1141 CGF.EmitAggExpr(E, AggValueSlot::ignored(), true); 1142 return 0; 1143} 1144 1145Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { 1146 return CGF.EmitCompoundStmt(*E->getSubStmt(), 1147 !E->getType()->isVoidType()).getScalarVal(); 1148} 1149 1150Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 1151 llvm::Value *V = CGF.GetAddrOfBlockDecl(E); 1152 if (E->getType().isObjCGCWeak()) 1153 return CGF.CGM.getObjCRuntime().EmitObjCWeakRead(CGF, V); 1154 return CGF.EmitLoadOfScalar(V, false, 0, E->getType()); 1155} 1156 1157//===----------------------------------------------------------------------===// 1158// Unary Operators 1159//===----------------------------------------------------------------------===// 1160 1161llvm::Value *ScalarExprEmitter:: 1162EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 1163 bool isInc, bool isPre) { 1164 1165 QualType ValTy = E->getSubExpr()->getType(); 1166 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy); 1167 1168 int AmountVal = isInc ? 1 : -1; 1169 1170 if (ValTy->isPointerType() && 1171 ValTy->getAs<PointerType>()->isVariableArrayType()) { 1172 // The amount of the addition/subtraction needs to account for the VLA size 1173 CGF.ErrorUnsupported(E, "VLA pointer inc/dec"); 1174 } 1175 1176 llvm::Value *NextVal; 1177 if (const llvm::PointerType *PT = 1178 dyn_cast<llvm::PointerType>(InVal->getType())) { 1179 llvm::Constant *Inc = llvm::ConstantInt::get(CGF.Int32Ty, AmountVal); 1180 if (!isa<llvm::FunctionType>(PT->getElementType())) { 1181 QualType PTEE = ValTy->getPointeeType(); 1182 if (const ObjCObjectType *OIT = PTEE->getAs<ObjCObjectType>()) { 1183 // Handle interface types, which are not represented with a concrete 1184 // type. 1185 int size = CGF.getContext().getTypeSize(OIT) / 8; 1186 if (!isInc) 1187 size = -size; 1188 Inc = llvm::ConstantInt::get(Inc->getType(), size); 1189 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1190 InVal = Builder.CreateBitCast(InVal, i8Ty); 1191 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr"); 1192 llvm::Value *lhs = LV.getAddress(); 1193 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty)); 1194 LV = CGF.MakeAddrLValue(lhs, ValTy); 1195 } else 1196 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec"); 1197 } else { 1198 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1199 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp"); 1200 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec"); 1201 NextVal = Builder.CreateBitCast(NextVal, InVal->getType()); 1202 } 1203 } else if (InVal->getType()->isIntegerTy(1) && isInc) { 1204 // Bool++ is an interesting case, due to promotion rules, we get: 1205 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 -> 1206 // Bool = ((int)Bool+1) != 0 1207 // An interesting aspect of this is that increment is always true. 1208 // Decrement does not have this property. 1209 NextVal = llvm::ConstantInt::getTrue(VMContext); 1210 } else if (isa<llvm::IntegerType>(InVal->getType())) { 1211 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); 1212 1213 if (!ValTy->isSignedIntegerType()) 1214 // Unsigned integer inc is always two's complement. 1215 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); 1216 else { 1217 switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { 1218 case LangOptions::SOB_Undefined: 1219 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); 1220 break; 1221 case LangOptions::SOB_Defined: 1222 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); 1223 break; 1224 case LangOptions::SOB_Trapping: 1225 BinOpInfo BinOp; 1226 BinOp.LHS = InVal; 1227 BinOp.RHS = NextVal; 1228 BinOp.Ty = E->getType(); 1229 BinOp.Opcode = BO_Add; 1230 BinOp.E = E; 1231 NextVal = EmitOverflowCheckedBinOp(BinOp); 1232 break; 1233 } 1234 } 1235 } else { 1236 // Add the inc/dec to the real part. 1237 if (InVal->getType()->isFloatTy()) 1238 NextVal = 1239 llvm::ConstantFP::get(VMContext, 1240 llvm::APFloat(static_cast<float>(AmountVal))); 1241 else if (InVal->getType()->isDoubleTy()) 1242 NextVal = 1243 llvm::ConstantFP::get(VMContext, 1244 llvm::APFloat(static_cast<double>(AmountVal))); 1245 else { 1246 llvm::APFloat F(static_cast<float>(AmountVal)); 1247 bool ignored; 1248 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero, 1249 &ignored); 1250 NextVal = llvm::ConstantFP::get(VMContext, F); 1251 } 1252 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec"); 1253 } 1254 1255 // Store the updated result through the lvalue. 1256 if (LV.isBitField()) 1257 CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal); 1258 else 1259 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy); 1260 1261 // If this is a postinc, return the value read from memory, otherwise use the 1262 // updated value. 1263 return isPre ? NextVal : InVal; 1264} 1265 1266 1267 1268Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { 1269 TestAndClearIgnoreResultAssign(); 1270 // Emit unary minus with EmitSub so we handle overflow cases etc. 1271 BinOpInfo BinOp; 1272 BinOp.RHS = Visit(E->getSubExpr()); 1273 1274 if (BinOp.RHS->getType()->isFPOrFPVectorTy()) 1275 BinOp.LHS = llvm::ConstantFP::getZeroValueForNegation(BinOp.RHS->getType()); 1276 else 1277 BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType()); 1278 BinOp.Ty = E->getType(); 1279 BinOp.Opcode = BO_Sub; 1280 BinOp.E = E; 1281 return EmitSub(BinOp); 1282} 1283 1284Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { 1285 TestAndClearIgnoreResultAssign(); 1286 Value *Op = Visit(E->getSubExpr()); 1287 return Builder.CreateNot(Op, "neg"); 1288} 1289 1290Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { 1291 // Compare operand to zero. 1292 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); 1293 1294 // Invert value. 1295 // TODO: Could dynamically modify easy computations here. For example, if 1296 // the operand is an icmp ne, turn into icmp eq. 1297 BoolVal = Builder.CreateNot(BoolVal, "lnot"); 1298 1299 // ZExt result to the expr type. 1300 return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext"); 1301} 1302 1303Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) { 1304 // Try folding the offsetof to a constant. 1305 Expr::EvalResult EvalResult; 1306 if (E->Evaluate(EvalResult, CGF.getContext())) 1307 return llvm::ConstantInt::get(VMContext, EvalResult.Val.getInt()); 1308 1309 // Loop over the components of the offsetof to compute the value. 1310 unsigned n = E->getNumComponents(); 1311 const llvm::Type* ResultType = ConvertType(E->getType()); 1312 llvm::Value* Result = llvm::Constant::getNullValue(ResultType); 1313 QualType CurrentType = E->getTypeSourceInfo()->getType(); 1314 for (unsigned i = 0; i != n; ++i) { 1315 OffsetOfExpr::OffsetOfNode ON = E->getComponent(i); 1316 llvm::Value *Offset = 0; 1317 switch (ON.getKind()) { 1318 case OffsetOfExpr::OffsetOfNode::Array: { 1319 // Compute the index 1320 Expr *IdxExpr = E->getIndexExpr(ON.getArrayExprIndex()); 1321 llvm::Value* Idx = CGF.EmitScalarExpr(IdxExpr); 1322 bool IdxSigned = IdxExpr->getType()->isSignedIntegerType(); 1323 Idx = Builder.CreateIntCast(Idx, ResultType, IdxSigned, "conv"); 1324 1325 // Save the element type 1326 CurrentType = 1327 CGF.getContext().getAsArrayType(CurrentType)->getElementType(); 1328 1329 // Compute the element size 1330 llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType, 1331 CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity()); 1332 1333 // Multiply out to compute the result 1334 Offset = Builder.CreateMul(Idx, ElemSize); 1335 break; 1336 } 1337 1338 case OffsetOfExpr::OffsetOfNode::Field: { 1339 FieldDecl *MemberDecl = ON.getField(); 1340 RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl(); 1341 const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); 1342 1343 // Compute the index of the field in its parent. 1344 unsigned i = 0; 1345 // FIXME: It would be nice if we didn't have to loop here! 1346 for (RecordDecl::field_iterator Field = RD->field_begin(), 1347 FieldEnd = RD->field_end(); 1348 Field != FieldEnd; (void)++Field, ++i) { 1349 if (*Field == MemberDecl) 1350 break; 1351 } 1352 assert(i < RL.getFieldCount() && "offsetof field in wrong type"); 1353 1354 // Compute the offset to the field 1355 int64_t OffsetInt = RL.getFieldOffset(i) / 1356 CGF.getContext().getCharWidth(); 1357 Offset = llvm::ConstantInt::get(ResultType, OffsetInt); 1358 1359 // Save the element type. 1360 CurrentType = MemberDecl->getType(); 1361 break; 1362 } 1363 1364 case OffsetOfExpr::OffsetOfNode::Identifier: 1365 llvm_unreachable("dependent __builtin_offsetof"); 1366 1367 case OffsetOfExpr::OffsetOfNode::Base: { 1368 if (ON.getBase()->isVirtual()) { 1369 CGF.ErrorUnsupported(E, "virtual base in offsetof"); 1370 continue; 1371 } 1372 1373 RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl(); 1374 const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); 1375 1376 // Save the element type. 1377 CurrentType = ON.getBase()->getType(); 1378 1379 // Compute the offset to the base. 1380 const RecordType *BaseRT = CurrentType->getAs<RecordType>(); 1381 CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); 1382 int64_t OffsetInt = RL.getBaseClassOffset(BaseRD) / 1383 CGF.getContext().getCharWidth(); 1384 Offset = llvm::ConstantInt::get(ResultType, OffsetInt); 1385 break; 1386 } 1387 } 1388 Result = Builder.CreateAdd(Result, Offset); 1389 } 1390 return Result; 1391} 1392 1393/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of 1394/// argument of the sizeof expression as an integer. 1395Value * 1396ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { 1397 QualType TypeToSize = E->getTypeOfArgument(); 1398 if (E->isSizeOf()) { 1399 if (const VariableArrayType *VAT = 1400 CGF.getContext().getAsVariableArrayType(TypeToSize)) { 1401 if (E->isArgumentType()) { 1402 // sizeof(type) - make sure to emit the VLA size. 1403 CGF.EmitVLASize(TypeToSize); 1404 } else { 1405 // C99 6.5.3.4p2: If the argument is an expression of type 1406 // VLA, it is evaluated. 1407 CGF.EmitAnyExpr(E->getArgumentExpr()); 1408 } 1409 1410 return CGF.GetVLASize(VAT); 1411 } 1412 } 1413 1414 // If this isn't sizeof(vla), the result must be constant; use the constant 1415 // folding logic so we don't have to duplicate it here. 1416 Expr::EvalResult Result; 1417 E->Evaluate(Result, CGF.getContext()); 1418 return llvm::ConstantInt::get(VMContext, Result.Val.getInt()); 1419} 1420 1421Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { 1422 Expr *Op = E->getSubExpr(); 1423 if (Op->getType()->isAnyComplexType()) 1424 return CGF.EmitComplexExpr(Op, false, true, false, true).first; 1425 return Visit(Op); 1426} 1427Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { 1428 Expr *Op = E->getSubExpr(); 1429 if (Op->getType()->isAnyComplexType()) 1430 return CGF.EmitComplexExpr(Op, true, false, true, false).second; 1431 1432 // __imag on a scalar returns zero. Emit the subexpr to ensure side 1433 // effects are evaluated, but not the actual value. 1434 if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid) 1435 CGF.EmitLValue(Op); 1436 else 1437 CGF.EmitScalarExpr(Op, true); 1438 return llvm::Constant::getNullValue(ConvertType(E->getType())); 1439} 1440 1441//===----------------------------------------------------------------------===// 1442// Binary Operators 1443//===----------------------------------------------------------------------===// 1444 1445BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { 1446 TestAndClearIgnoreResultAssign(); 1447 BinOpInfo Result; 1448 Result.LHS = Visit(E->getLHS()); 1449 Result.RHS = Visit(E->getRHS()); 1450 Result.Ty = E->getType(); 1451 Result.Opcode = E->getOpcode(); 1452 Result.E = E; 1453 return Result; 1454} 1455 1456LValue ScalarExprEmitter::EmitCompoundAssignLValue( 1457 const CompoundAssignOperator *E, 1458 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &), 1459 Value *&Result) { 1460 QualType LHSTy = E->getLHS()->getType(); 1461 BinOpInfo OpInfo; 1462 1463 if (E->getComputationResultType()->isAnyComplexType()) { 1464 // This needs to go through the complex expression emitter, but it's a tad 1465 // complicated to do that... I'm leaving it out for now. (Note that we do 1466 // actually need the imaginary part of the RHS for multiplication and 1467 // division.) 1468 CGF.ErrorUnsupported(E, "complex compound assignment"); 1469 Result = llvm::UndefValue::get(CGF.ConvertType(E->getType())); 1470 return LValue(); 1471 } 1472 1473 // Emit the RHS first. __block variables need to have the rhs evaluated 1474 // first, plus this should improve codegen a little. 1475 OpInfo.RHS = Visit(E->getRHS()); 1476 OpInfo.Ty = E->getComputationResultType(); 1477 OpInfo.Opcode = E->getOpcode(); 1478 OpInfo.E = E; 1479 // Load/convert the LHS. 1480 LValue LHSLV = EmitCheckedLValue(E->getLHS()); 1481 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); 1482 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, 1483 E->getComputationLHSType()); 1484 1485 // Expand the binary operator. 1486 Result = (this->*Func)(OpInfo); 1487 1488 // Convert the result back to the LHS type. 1489 Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy); 1490 1491 // Store the result value into the LHS lvalue. Bit-fields are handled 1492 // specially because the result is altered by the store, i.e., [C99 6.5.16p1] 1493 // 'An assignment expression has the value of the left operand after the 1494 // assignment...'. 1495 if (LHSLV.isBitField()) 1496 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy, 1497 &Result); 1498 else 1499 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy); 1500 1501 return LHSLV; 1502} 1503 1504Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, 1505 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { 1506 bool Ignore = TestAndClearIgnoreResultAssign(); 1507 Value *RHS; 1508 LValue LHS = EmitCompoundAssignLValue(E, Func, RHS); 1509 1510 // If the result is clearly ignored, return now. 1511 if (Ignore) 1512 return 0; 1513 1514 // Objective-C property assignment never reloads the value following a store. 1515 if (LHS.isPropertyRef() || LHS.isKVCRef()) 1516 return RHS; 1517 1518 // If the lvalue is non-volatile, return the computed value of the assignment. 1519 if (!LHS.isVolatileQualified()) 1520 return RHS; 1521 1522 // Otherwise, reload the value. 1523 return EmitLoadOfLValue(LHS, E->getType()); 1524} 1525 1526void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck( 1527 const BinOpInfo &Ops, 1528 llvm::Value *Zero, bool isDiv) { 1529 llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn); 1530 llvm::BasicBlock *contBB = 1531 CGF.createBasicBlock(isDiv ? "div.cont" : "rem.cont", CGF.CurFn); 1532 1533 const llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType()); 1534 1535 if (Ops.Ty->hasSignedIntegerRepresentation()) { 1536 llvm::Value *IntMin = 1537 llvm::ConstantInt::get(VMContext, 1538 llvm::APInt::getSignedMinValue(Ty->getBitWidth())); 1539 llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL); 1540 1541 llvm::Value *Cond1 = Builder.CreateICmpEQ(Ops.RHS, Zero); 1542 llvm::Value *LHSCmp = Builder.CreateICmpEQ(Ops.LHS, IntMin); 1543 llvm::Value *RHSCmp = Builder.CreateICmpEQ(Ops.RHS, NegOne); 1544 llvm::Value *Cond2 = Builder.CreateAnd(LHSCmp, RHSCmp, "and"); 1545 Builder.CreateCondBr(Builder.CreateOr(Cond1, Cond2, "or"), 1546 overflowBB, contBB); 1547 } else { 1548 CGF.Builder.CreateCondBr(Builder.CreateICmpEQ(Ops.RHS, Zero), 1549 overflowBB, contBB); 1550 } 1551 EmitOverflowBB(overflowBB); 1552 Builder.SetInsertPoint(contBB); 1553} 1554 1555Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { 1556 if (isTrapvOverflowBehavior()) { 1557 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); 1558 1559 if (Ops.Ty->isIntegerType()) 1560 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, true); 1561 else if (Ops.Ty->isRealFloatingType()) { 1562 llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", 1563 CGF.CurFn); 1564 llvm::BasicBlock *DivCont = CGF.createBasicBlock("div.cont", CGF.CurFn); 1565 CGF.Builder.CreateCondBr(Builder.CreateFCmpOEQ(Ops.RHS, Zero), 1566 overflowBB, DivCont); 1567 EmitOverflowBB(overflowBB); 1568 Builder.SetInsertPoint(DivCont); 1569 } 1570 } 1571 if (Ops.LHS->getType()->isFPOrFPVectorTy()) 1572 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); 1573 else if (Ops.Ty->hasUnsignedIntegerRepresentation()) 1574 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); 1575 else 1576 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); 1577} 1578 1579Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { 1580 // Rem in C can't be a floating point type: C99 6.5.5p2. 1581 if (isTrapvOverflowBehavior()) { 1582 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); 1583 1584 if (Ops.Ty->isIntegerType()) 1585 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false); 1586 } 1587 1588 if (Ops.Ty->isUnsignedIntegerType()) 1589 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); 1590 else 1591 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); 1592} 1593 1594Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) { 1595 unsigned IID; 1596 unsigned OpID = 0; 1597 1598 switch (Ops.Opcode) { 1599 case BO_Add: 1600 case BO_AddAssign: 1601 OpID = 1; 1602 IID = llvm::Intrinsic::sadd_with_overflow; 1603 break; 1604 case BO_Sub: 1605 case BO_SubAssign: 1606 OpID = 2; 1607 IID = llvm::Intrinsic::ssub_with_overflow; 1608 break; 1609 case BO_Mul: 1610 case BO_MulAssign: 1611 OpID = 3; 1612 IID = llvm::Intrinsic::smul_with_overflow; 1613 break; 1614 default: 1615 assert(false && "Unsupported operation for overflow detection"); 1616 IID = 0; 1617 } 1618 OpID <<= 1; 1619 OpID |= 1; 1620 1621 const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty); 1622 1623 llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1); 1624 1625 Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS); 1626 Value *result = Builder.CreateExtractValue(resultAndOverflow, 0); 1627 Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1); 1628 1629 // Branch in case of overflow. 1630 llvm::BasicBlock *initialBB = Builder.GetInsertBlock(); 1631 llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn); 1632 llvm::BasicBlock *continueBB = CGF.createBasicBlock("nooverflow", CGF.CurFn); 1633 1634 Builder.CreateCondBr(overflow, overflowBB, continueBB); 1635 1636 // Handle overflow with llvm.trap. 1637 const std::string *handlerName = 1638 &CGF.getContext().getLangOptions().OverflowHandler; 1639 if (handlerName->empty()) { 1640 EmitOverflowBB(overflowBB); 1641 Builder.SetInsertPoint(continueBB); 1642 return result; 1643 } 1644 1645 // If an overflow handler is set, then we want to call it and then use its 1646 // result, if it returns. 1647 Builder.SetInsertPoint(overflowBB); 1648 1649 // Get the overflow handler. 1650 const llvm::Type *Int8Ty = llvm::Type::getInt8Ty(VMContext); 1651 std::vector<const llvm::Type*> argTypes; 1652 argTypes.push_back(CGF.Int64Ty); argTypes.push_back(CGF.Int64Ty); 1653 argTypes.push_back(Int8Ty); argTypes.push_back(Int8Ty); 1654 llvm::FunctionType *handlerTy = 1655 llvm::FunctionType::get(CGF.Int64Ty, argTypes, true); 1656 llvm::Value *handler = CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName); 1657 1658 // Sign extend the args to 64-bit, so that we can use the same handler for 1659 // all types of overflow. 1660 llvm::Value *lhs = Builder.CreateSExt(Ops.LHS, CGF.Int64Ty); 1661 llvm::Value *rhs = Builder.CreateSExt(Ops.RHS, CGF.Int64Ty); 1662 1663 // Call the handler with the two arguments, the operation, and the size of 1664 // the result. 1665 llvm::Value *handlerResult = Builder.CreateCall4(handler, lhs, rhs, 1666 Builder.getInt8(OpID), 1667 Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth())); 1668 1669 // Truncate the result back to the desired size. 1670 handlerResult = Builder.CreateTrunc(handlerResult, opTy); 1671 Builder.CreateBr(continueBB); 1672 1673 Builder.SetInsertPoint(continueBB); 1674 llvm::PHINode *phi = Builder.CreatePHI(opTy); 1675 phi->reserveOperandSpace(2); 1676 phi->addIncoming(result, initialBB); 1677 phi->addIncoming(handlerResult, overflowBB); 1678 1679 return phi; 1680} 1681 1682Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { 1683 if (!Ops.Ty->isAnyPointerType()) { 1684 if (Ops.Ty->hasSignedIntegerRepresentation()) { 1685 switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { 1686 case LangOptions::SOB_Undefined: 1687 return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add"); 1688 case LangOptions::SOB_Defined: 1689 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); 1690 case LangOptions::SOB_Trapping: 1691 return EmitOverflowCheckedBinOp(Ops); 1692 } 1693 } 1694 1695 if (Ops.LHS->getType()->isFPOrFPVectorTy()) 1696 return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add"); 1697 1698 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); 1699 } 1700 1701 // Must have binary (not unary) expr here. Unary pointer decrement doesn't 1702 // use this path. 1703 const BinaryOperator *BinOp = cast<BinaryOperator>(Ops.E); 1704 1705 if (Ops.Ty->isPointerType() && 1706 Ops.Ty->getAs<PointerType>()->isVariableArrayType()) { 1707 // The amount of the addition needs to account for the VLA size 1708 CGF.ErrorUnsupported(BinOp, "VLA pointer addition"); 1709 } 1710 1711 Value *Ptr, *Idx; 1712 Expr *IdxExp; 1713 const PointerType *PT = BinOp->getLHS()->getType()->getAs<PointerType>(); 1714 const ObjCObjectPointerType *OPT = 1715 BinOp->getLHS()->getType()->getAs<ObjCObjectPointerType>(); 1716 if (PT || OPT) { 1717 Ptr = Ops.LHS; 1718 Idx = Ops.RHS; 1719 IdxExp = BinOp->getRHS(); 1720 } else { // int + pointer 1721 PT = BinOp->getRHS()->getType()->getAs<PointerType>(); 1722 OPT = BinOp->getRHS()->getType()->getAs<ObjCObjectPointerType>(); 1723 assert((PT || OPT) && "Invalid add expr"); 1724 Ptr = Ops.RHS; 1725 Idx = Ops.LHS; 1726 IdxExp = BinOp->getLHS(); 1727 } 1728 1729 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); 1730 if (Width < CGF.LLVMPointerWidth) { 1731 // Zero or sign extend the pointer value based on whether the index is 1732 // signed or not. 1733 const llvm::Type *IdxType = CGF.IntPtrTy; 1734 if (IdxExp->getType()->isSignedIntegerType()) 1735 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); 1736 else 1737 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); 1738 } 1739 const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType(); 1740 // Handle interface types, which are not represented with a concrete type. 1741 if (const ObjCObjectType *OIT = ElementType->getAs<ObjCObjectType>()) { 1742 llvm::Value *InterfaceSize = 1743 llvm::ConstantInt::get(Idx->getType(), 1744 CGF.getContext().getTypeSizeInChars(OIT).getQuantity()); 1745 Idx = Builder.CreateMul(Idx, InterfaceSize); 1746 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1747 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); 1748 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); 1749 return Builder.CreateBitCast(Res, Ptr->getType()); 1750 } 1751 1752 // Explicitly handle GNU void* and function pointer arithmetic extensions. The 1753 // GNU void* casts amount to no-ops since our void* type is i8*, but this is 1754 // future proof. 1755 if (ElementType->isVoidType() || ElementType->isFunctionType()) { 1756 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1757 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); 1758 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); 1759 return Builder.CreateBitCast(Res, Ptr->getType()); 1760 } 1761 1762 return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr"); 1763} 1764 1765Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { 1766 if (!isa<llvm::PointerType>(Ops.LHS->getType())) { 1767 if (Ops.Ty->hasSignedIntegerRepresentation()) { 1768 switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { 1769 case LangOptions::SOB_Undefined: 1770 return Builder.CreateNSWSub(Ops.LHS, Ops.RHS, "sub"); 1771 case LangOptions::SOB_Defined: 1772 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); 1773 case LangOptions::SOB_Trapping: 1774 return EmitOverflowCheckedBinOp(Ops); 1775 } 1776 } 1777 1778 if (Ops.LHS->getType()->isFPOrFPVectorTy()) 1779 return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub"); 1780 1781 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); 1782 } 1783 1784 // Must have binary (not unary) expr here. Unary pointer increment doesn't 1785 // use this path. 1786 const BinaryOperator *BinOp = cast<BinaryOperator>(Ops.E); 1787 1788 if (BinOp->getLHS()->getType()->isPointerType() && 1789 BinOp->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) { 1790 // The amount of the addition needs to account for the VLA size for 1791 // ptr-int 1792 // The amount of the division needs to account for the VLA size for 1793 // ptr-ptr. 1794 CGF.ErrorUnsupported(BinOp, "VLA pointer subtraction"); 1795 } 1796 1797 const QualType LHSType = BinOp->getLHS()->getType(); 1798 const QualType LHSElementType = LHSType->getPointeeType(); 1799 if (!isa<llvm::PointerType>(Ops.RHS->getType())) { 1800 // pointer - int 1801 Value *Idx = Ops.RHS; 1802 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); 1803 if (Width < CGF.LLVMPointerWidth) { 1804 // Zero or sign extend the pointer value based on whether the index is 1805 // signed or not. 1806 const llvm::Type *IdxType = CGF.IntPtrTy; 1807 if (BinOp->getRHS()->getType()->isSignedIntegerType()) 1808 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); 1809 else 1810 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); 1811 } 1812 Idx = Builder.CreateNeg(Idx, "sub.ptr.neg"); 1813 1814 // Handle interface types, which are not represented with a concrete type. 1815 if (const ObjCObjectType *OIT = LHSElementType->getAs<ObjCObjectType>()) { 1816 llvm::Value *InterfaceSize = 1817 llvm::ConstantInt::get(Idx->getType(), 1818 CGF.getContext(). 1819 getTypeSizeInChars(OIT).getQuantity()); 1820 Idx = Builder.CreateMul(Idx, InterfaceSize); 1821 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1822 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); 1823 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr"); 1824 return Builder.CreateBitCast(Res, Ops.LHS->getType()); 1825 } 1826 1827 // Explicitly handle GNU void* and function pointer arithmetic 1828 // extensions. The GNU void* casts amount to no-ops since our void* type is 1829 // i8*, but this is future proof. 1830 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { 1831 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1832 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); 1833 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr"); 1834 return Builder.CreateBitCast(Res, Ops.LHS->getType()); 1835 } 1836 1837 return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr"); 1838 } else { 1839 // pointer - pointer 1840 Value *LHS = Ops.LHS; 1841 Value *RHS = Ops.RHS; 1842 1843 CharUnits ElementSize; 1844 1845 // Handle GCC extension for pointer arithmetic on void* and function pointer 1846 // types. 1847 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { 1848 ElementSize = CharUnits::One(); 1849 } else { 1850 ElementSize = CGF.getContext().getTypeSizeInChars(LHSElementType); 1851 } 1852 1853 const llvm::Type *ResultType = ConvertType(Ops.Ty); 1854 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); 1855 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); 1856 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); 1857 1858 // Optimize out the shift for element size of 1. 1859 if (ElementSize.isOne()) 1860 return BytesBetween; 1861 1862 // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since 1863 // pointer difference in C is only defined in the case where both operands 1864 // are pointing to elements of an array. 1865 Value *BytesPerElt = 1866 llvm::ConstantInt::get(ResultType, ElementSize.getQuantity()); 1867 return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); 1868 } 1869} 1870 1871Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { 1872 // LLVM requires the LHS and RHS to be the same type: promote or truncate the 1873 // RHS to the same size as the LHS. 1874 Value *RHS = Ops.RHS; 1875 if (Ops.LHS->getType() != RHS->getType()) 1876 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); 1877 1878 if (CGF.CatchUndefined 1879 && isa<llvm::IntegerType>(Ops.LHS->getType())) { 1880 unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth(); 1881 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); 1882 CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS, 1883 llvm::ConstantInt::get(RHS->getType(), Width)), 1884 Cont, CGF.getTrapBB()); 1885 CGF.EmitBlock(Cont); 1886 } 1887 1888 return Builder.CreateShl(Ops.LHS, RHS, "shl"); 1889} 1890 1891Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { 1892 // LLVM requires the LHS and RHS to be the same type: promote or truncate the 1893 // RHS to the same size as the LHS. 1894 Value *RHS = Ops.RHS; 1895 if (Ops.LHS->getType() != RHS->getType()) 1896 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); 1897 1898 if (CGF.CatchUndefined 1899 && isa<llvm::IntegerType>(Ops.LHS->getType())) { 1900 unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth(); 1901 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); 1902 CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS, 1903 llvm::ConstantInt::get(RHS->getType(), Width)), 1904 Cont, CGF.getTrapBB()); 1905 CGF.EmitBlock(Cont); 1906 } 1907 1908 if (Ops.Ty->hasUnsignedIntegerRepresentation()) 1909 return Builder.CreateLShr(Ops.LHS, RHS, "shr"); 1910 return Builder.CreateAShr(Ops.LHS, RHS, "shr"); 1911} 1912 1913Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, 1914 unsigned SICmpOpc, unsigned FCmpOpc) { 1915 TestAndClearIgnoreResultAssign(); 1916 Value *Result; 1917 QualType LHSTy = E->getLHS()->getType(); 1918 if (const MemberPointerType *MPT = LHSTy->getAs<MemberPointerType>()) { 1919 assert(E->getOpcode() == BO_EQ || 1920 E->getOpcode() == BO_NE); 1921 Value *LHS = CGF.EmitScalarExpr(E->getLHS()); 1922 Value *RHS = CGF.EmitScalarExpr(E->getRHS()); 1923 Result = CGF.CGM.getCXXABI().EmitMemberPointerComparison( 1924 CGF, LHS, RHS, MPT, E->getOpcode() == BO_NE); 1925 } else if (!LHSTy->isAnyComplexType()) { 1926 Value *LHS = Visit(E->getLHS()); 1927 Value *RHS = Visit(E->getRHS()); 1928 1929 if (LHS->getType()->isFPOrFPVectorTy()) { 1930 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc, 1931 LHS, RHS, "cmp"); 1932 } else if (LHSTy->hasSignedIntegerRepresentation()) { 1933 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, 1934 LHS, RHS, "cmp"); 1935 } else { 1936 // Unsigned integers and pointers. 1937 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, 1938 LHS, RHS, "cmp"); 1939 } 1940 1941 // If this is a vector comparison, sign extend the result to the appropriate 1942 // vector integer type and return it (don't convert to bool). 1943 if (LHSTy->isVectorType()) 1944 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); 1945 1946 } else { 1947 // Complex Comparison: can only be an equality comparison. 1948 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); 1949 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); 1950 1951 QualType CETy = LHSTy->getAs<ComplexType>()->getElementType(); 1952 1953 Value *ResultR, *ResultI; 1954 if (CETy->isRealFloatingType()) { 1955 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, 1956 LHS.first, RHS.first, "cmp.r"); 1957 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, 1958 LHS.second, RHS.second, "cmp.i"); 1959 } else { 1960 // Complex comparisons can only be equality comparisons. As such, signed 1961 // and unsigned opcodes are the same. 1962 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, 1963 LHS.first, RHS.first, "cmp.r"); 1964 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, 1965 LHS.second, RHS.second, "cmp.i"); 1966 } 1967 1968 if (E->getOpcode() == BO_EQ) { 1969 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); 1970 } else { 1971 assert(E->getOpcode() == BO_NE && 1972 "Complex comparison other than == or != ?"); 1973 Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); 1974 } 1975 } 1976 1977 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); 1978} 1979 1980Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { 1981 bool Ignore = TestAndClearIgnoreResultAssign(); 1982 1983 // __block variables need to have the rhs evaluated first, plus this should 1984 // improve codegen just a little. 1985 Value *RHS = Visit(E->getRHS()); 1986 LValue LHS = EmitCheckedLValue(E->getLHS()); 1987 1988 // Store the value into the LHS. Bit-fields are handled specially 1989 // because the result is altered by the store, i.e., [C99 6.5.16p1] 1990 // 'An assignment expression has the value of the left operand after 1991 // the assignment...'. 1992 if (LHS.isBitField()) 1993 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(), 1994 &RHS); 1995 else 1996 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); 1997 1998 // If the result is clearly ignored, return now. 1999 if (Ignore) 2000 return 0; 2001 2002 // Objective-C property assignment never reloads the value following a store. 2003 if (LHS.isPropertyRef() || LHS.isKVCRef()) 2004 return RHS; 2005 2006 // If the lvalue is non-volatile, return the computed value of the assignment. 2007 if (!LHS.isVolatileQualified()) 2008 return RHS; 2009 2010 // Otherwise, reload the value. 2011 return EmitLoadOfLValue(LHS, E->getType()); 2012} 2013 2014Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { 2015 const llvm::Type *ResTy = ConvertType(E->getType()); 2016 2017 // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. 2018 // If we have 1 && X, just emit X without inserting the control flow. 2019 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { 2020 if (Cond == 1) { // If we have 1 && X, just emit X. 2021 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 2022 // ZExt result to int or bool. 2023 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext"); 2024 } 2025 2026 // 0 && RHS: If it is safe, just elide the RHS, and return 0/false. 2027 if (!CGF.ContainsLabel(E->getRHS())) 2028 return llvm::Constant::getNullValue(ResTy); 2029 } 2030 2031 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); 2032 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); 2033 2034 // Branch on the LHS first. If it is false, go to the failure (cont) block. 2035 CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock); 2036 2037 // Any edges into the ContBlock are now from an (indeterminate number of) 2038 // edges from this first condition. All of these values will be false. Start 2039 // setting up the PHI node in the Cont Block for this. 2040 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2041 "", ContBlock); 2042 PN->reserveOperandSpace(2); // Normal case, two inputs. 2043 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); 2044 PI != PE; ++PI) 2045 PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI); 2046 2047 CGF.BeginConditionalBranch(); 2048 CGF.EmitBlock(RHSBlock); 2049 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 2050 CGF.EndConditionalBranch(); 2051 2052 // Reaquire the RHS block, as there may be subblocks inserted. 2053 RHSBlock = Builder.GetInsertBlock(); 2054 2055 // Emit an unconditional branch from this block to ContBlock. Insert an entry 2056 // into the phi node for the edge with the value of RHSCond. 2057 CGF.EmitBlock(ContBlock); 2058 PN->addIncoming(RHSCond, RHSBlock); 2059 2060 // ZExt result to int. 2061 return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext"); 2062} 2063 2064Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { 2065 const llvm::Type *ResTy = ConvertType(E->getType()); 2066 2067 // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. 2068 // If we have 0 || X, just emit X without inserting the control flow. 2069 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { 2070 if (Cond == -1) { // If we have 0 || X, just emit X. 2071 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 2072 // ZExt result to int or bool. 2073 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext"); 2074 } 2075 2076 // 1 || RHS: If it is safe, just elide the RHS, and return 1/true. 2077 if (!CGF.ContainsLabel(E->getRHS())) 2078 return llvm::ConstantInt::get(ResTy, 1); 2079 } 2080 2081 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); 2082 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); 2083 2084 // Branch on the LHS first. If it is true, go to the success (cont) block. 2085 CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock); 2086 2087 // Any edges into the ContBlock are now from an (indeterminate number of) 2088 // edges from this first condition. All of these values will be true. Start 2089 // setting up the PHI node in the Cont Block for this. 2090 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2091 "", ContBlock); 2092 PN->reserveOperandSpace(2); // Normal case, two inputs. 2093 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); 2094 PI != PE; ++PI) 2095 PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI); 2096 2097 CGF.BeginConditionalBranch(); 2098 2099 // Emit the RHS condition as a bool value. 2100 CGF.EmitBlock(RHSBlock); 2101 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 2102 2103 CGF.EndConditionalBranch(); 2104 2105 // Reaquire the RHS block, as there may be subblocks inserted. 2106 RHSBlock = Builder.GetInsertBlock(); 2107 2108 // Emit an unconditional branch from this block to ContBlock. Insert an entry 2109 // into the phi node for the edge with the value of RHSCond. 2110 CGF.EmitBlock(ContBlock); 2111 PN->addIncoming(RHSCond, RHSBlock); 2112 2113 // ZExt result to int. 2114 return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext"); 2115} 2116 2117Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { 2118 CGF.EmitStmt(E->getLHS()); 2119 CGF.EnsureInsertPoint(); 2120 return Visit(E->getRHS()); 2121} 2122 2123//===----------------------------------------------------------------------===// 2124// Other Operators 2125//===----------------------------------------------------------------------===// 2126 2127/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified 2128/// expression is cheap enough and side-effect-free enough to evaluate 2129/// unconditionally instead of conditionally. This is used to convert control 2130/// flow into selects in some cases. 2131static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E, 2132 CodeGenFunction &CGF) { 2133 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) 2134 return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr(), CGF); 2135 2136 // TODO: Allow anything we can constant fold to an integer or fp constant. 2137 if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) || 2138 isa<FloatingLiteral>(E)) 2139 return true; 2140 2141 // Non-volatile automatic variables too, to get "cond ? X : Y" where 2142 // X and Y are local variables. 2143 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 2144 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) 2145 if (VD->hasLocalStorage() && !(CGF.getContext() 2146 .getCanonicalType(VD->getType()) 2147 .isVolatileQualified())) 2148 return true; 2149 2150 return false; 2151} 2152 2153 2154Value *ScalarExprEmitter:: 2155VisitConditionalOperator(const ConditionalOperator *E) { 2156 TestAndClearIgnoreResultAssign(); 2157 // If the condition constant folds and can be elided, try to avoid emitting 2158 // the condition and the dead arm. 2159 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){ 2160 Expr *Live = E->getLHS(), *Dead = E->getRHS(); 2161 if (Cond == -1) 2162 std::swap(Live, Dead); 2163 2164 // If the dead side doesn't have labels we need, and if the Live side isn't 2165 // the gnu missing ?: extension (which we could handle, but don't bother 2166 // to), just emit the Live part. 2167 if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part 2168 Live) // Live part isn't missing. 2169 return Visit(Live); 2170 } 2171 2172 // OpenCL: If the condition is a vector, we can treat this condition like 2173 // the select function. 2174 if (CGF.getContext().getLangOptions().OpenCL 2175 && E->getCond()->getType()->isVectorType()) { 2176 llvm::Value *CondV = CGF.EmitScalarExpr(E->getCond()); 2177 llvm::Value *LHS = Visit(E->getLHS()); 2178 llvm::Value *RHS = Visit(E->getRHS()); 2179 2180 const llvm::Type *condType = ConvertType(E->getCond()->getType()); 2181 const llvm::VectorType *vecTy = cast<llvm::VectorType>(condType); 2182 2183 unsigned numElem = vecTy->getNumElements(); 2184 const llvm::Type *elemType = vecTy->getElementType(); 2185 2186 std::vector<llvm::Constant*> Zvals; 2187 for (unsigned i = 0; i < numElem; ++i) 2188 Zvals.push_back(llvm::ConstantInt::get(elemType,0)); 2189 2190 llvm::Value *zeroVec = llvm::ConstantVector::get(Zvals); 2191 llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec); 2192 llvm::Value *tmp = Builder.CreateSExt(TestMSB, 2193 llvm::VectorType::get(elemType, 2194 numElem), 2195 "sext"); 2196 llvm::Value *tmp2 = Builder.CreateNot(tmp); 2197 2198 // Cast float to int to perform ANDs if necessary. 2199 llvm::Value *RHSTmp = RHS; 2200 llvm::Value *LHSTmp = LHS; 2201 bool wasCast = false; 2202 const llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType()); 2203 if (rhsVTy->getElementType()->isFloatTy()) { 2204 RHSTmp = Builder.CreateBitCast(RHS, tmp2->getType()); 2205 LHSTmp = Builder.CreateBitCast(LHS, tmp->getType()); 2206 wasCast = true; 2207 } 2208 2209 llvm::Value *tmp3 = Builder.CreateAnd(RHSTmp, tmp2); 2210 llvm::Value *tmp4 = Builder.CreateAnd(LHSTmp, tmp); 2211 llvm::Value *tmp5 = Builder.CreateOr(tmp3, tmp4, "cond"); 2212 if (wasCast) 2213 tmp5 = Builder.CreateBitCast(tmp5, RHS->getType()); 2214 2215 return tmp5; 2216 } 2217 2218 // If this is a really simple expression (like x ? 4 : 5), emit this as a 2219 // select instead of as control flow. We can only do this if it is cheap and 2220 // safe to evaluate the LHS and RHS unconditionally. 2221 if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS(), 2222 CGF) && 2223 isCheapEnoughToEvaluateUnconditionally(E->getRHS(), CGF)) { 2224 llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond()); 2225 llvm::Value *LHS = Visit(E->getLHS()); 2226 llvm::Value *RHS = Visit(E->getRHS()); 2227 return Builder.CreateSelect(CondV, LHS, RHS, "cond"); 2228 } 2229 2230 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 2231 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 2232 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 2233 2234 // If we don't have the GNU missing condition extension, emit a branch on bool 2235 // the normal way. 2236 if (E->getLHS()) { 2237 // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for 2238 // the branch on bool. 2239 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); 2240 } else { 2241 // Otherwise, for the ?: extension, evaluate the conditional and then 2242 // convert it to bool the hard way. We do this explicitly because we need 2243 // the unconverted value for the missing middle value of the ?:. 2244 Expr *save = E->getSAVE(); 2245 assert(save && "VisitConditionalOperator - save is null"); 2246 // Intentianlly not doing direct assignment to ConditionalSaveExprs[save] !! 2247 Value *SaveVal = CGF.EmitScalarExpr(save); 2248 CGF.ConditionalSaveExprs[save] = SaveVal; 2249 Value *CondVal = Visit(E->getCond()); 2250 // In some cases, EmitScalarConversion will delete the "CondVal" expression 2251 // if there are no extra uses (an optimization). Inhibit this by making an 2252 // extra dead use, because we're going to add a use of CondVal later. We 2253 // don't use the builder for this, because we don't want it to get optimized 2254 // away. This leaves dead code, but the ?: extension isn't common. 2255 new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder", 2256 Builder.GetInsertBlock()); 2257 2258 Value *CondBoolVal = 2259 CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), 2260 CGF.getContext().BoolTy); 2261 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); 2262 } 2263 2264 CGF.BeginConditionalBranch(); 2265 CGF.EmitBlock(LHSBlock); 2266 2267 // Handle the GNU extension for missing LHS. 2268 Value *LHS = Visit(E->getTrueExpr()); 2269 2270 CGF.EndConditionalBranch(); 2271 LHSBlock = Builder.GetInsertBlock(); 2272 CGF.EmitBranch(ContBlock); 2273 2274 CGF.BeginConditionalBranch(); 2275 CGF.EmitBlock(RHSBlock); 2276 2277 Value *RHS = Visit(E->getRHS()); 2278 CGF.EndConditionalBranch(); 2279 RHSBlock = Builder.GetInsertBlock(); 2280 CGF.EmitBranch(ContBlock); 2281 2282 CGF.EmitBlock(ContBlock); 2283 2284 // If the LHS or RHS is a throw expression, it will be legitimately null. 2285 if (!LHS) 2286 return RHS; 2287 if (!RHS) 2288 return LHS; 2289 2290 // Create a PHI node for the real part. 2291 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); 2292 PN->reserveOperandSpace(2); 2293 PN->addIncoming(LHS, LHSBlock); 2294 PN->addIncoming(RHS, RHSBlock); 2295 return PN; 2296} 2297 2298Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { 2299 return Visit(E->getChosenSubExpr(CGF.getContext())); 2300} 2301 2302Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 2303 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 2304 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 2305 2306 // If EmitVAArg fails, we fall back to the LLVM instruction. 2307 if (!ArgPtr) 2308 return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); 2309 2310 // FIXME Volatility. 2311 return Builder.CreateLoad(ArgPtr); 2312} 2313 2314Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) { 2315 return CGF.BuildBlockLiteralTmp(BE); 2316} 2317 2318//===----------------------------------------------------------------------===// 2319// Entry Point into this File 2320//===----------------------------------------------------------------------===// 2321 2322/// EmitScalarExpr - Emit the computation of the specified expression of scalar 2323/// type, ignoring the result. 2324Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) { 2325 assert(E && !hasAggregateLLVMType(E->getType()) && 2326 "Invalid scalar expression to emit"); 2327 2328 return ScalarExprEmitter(*this, IgnoreResultAssign) 2329 .Visit(const_cast<Expr*>(E)); 2330} 2331 2332/// EmitScalarConversion - Emit a conversion from the specified type to the 2333/// specified destination type, both of which are LLVM scalar types. 2334Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, 2335 QualType DstTy) { 2336 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && 2337 "Invalid scalar expression to emit"); 2338 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); 2339} 2340 2341/// EmitComplexToScalarConversion - Emit a conversion from the specified complex 2342/// type to the specified destination type, where the destination type is an 2343/// LLVM scalar type. 2344Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, 2345 QualType SrcTy, 2346 QualType DstTy) { 2347 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) && 2348 "Invalid complex -> scalar conversion"); 2349 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, 2350 DstTy); 2351} 2352 2353 2354llvm::Value *CodeGenFunction:: 2355EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 2356 bool isInc, bool isPre) { 2357 return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre); 2358} 2359 2360LValue CodeGenFunction::EmitObjCIsaExpr(const ObjCIsaExpr *E) { 2361 llvm::Value *V; 2362 // object->isa or (*object).isa 2363 // Generate code as for: *(Class*)object 2364 // build Class* type 2365 const llvm::Type *ClassPtrTy = ConvertType(E->getType()); 2366 2367 Expr *BaseExpr = E->getBase(); 2368 if (BaseExpr->isLvalue(getContext()) != Expr::LV_Valid) { 2369 V = CreateTempAlloca(ClassPtrTy, "resval"); 2370 llvm::Value *Src = EmitScalarExpr(BaseExpr); 2371 Builder.CreateStore(Src, V); 2372 V = ScalarExprEmitter(*this).EmitLoadOfLValue( 2373 MakeAddrLValue(V, E->getType()), E->getType()); 2374 } else { 2375 if (E->isArrow()) 2376 V = ScalarExprEmitter(*this).EmitLoadOfLValue(BaseExpr); 2377 else 2378 V = EmitLValue(BaseExpr).getAddress(); 2379 } 2380 2381 // build Class* type 2382 ClassPtrTy = ClassPtrTy->getPointerTo(); 2383 V = Builder.CreateBitCast(V, ClassPtrTy); 2384 return MakeAddrLValue(V, E->getType()); 2385} 2386 2387 2388LValue CodeGenFunction::EmitCompoundAssignOperatorLValue( 2389 const CompoundAssignOperator *E) { 2390 ScalarExprEmitter Scalar(*this); 2391 Value *Result = 0; 2392 switch (E->getOpcode()) { 2393#define COMPOUND_OP(Op) \ 2394 case BO_##Op##Assign: \ 2395 return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \ 2396 Result) 2397 COMPOUND_OP(Mul); 2398 COMPOUND_OP(Div); 2399 COMPOUND_OP(Rem); 2400 COMPOUND_OP(Add); 2401 COMPOUND_OP(Sub); 2402 COMPOUND_OP(Shl); 2403 COMPOUND_OP(Shr); 2404 COMPOUND_OP(And); 2405 COMPOUND_OP(Xor); 2406 COMPOUND_OP(Or); 2407#undef COMPOUND_OP 2408 2409 case BO_PtrMemD: 2410 case BO_PtrMemI: 2411 case BO_Mul: 2412 case BO_Div: 2413 case BO_Rem: 2414 case BO_Add: 2415 case BO_Sub: 2416 case BO_Shl: 2417 case BO_Shr: 2418 case BO_LT: 2419 case BO_GT: 2420 case BO_LE: 2421 case BO_GE: 2422 case BO_EQ: 2423 case BO_NE: 2424 case BO_And: 2425 case BO_Xor: 2426 case BO_Or: 2427 case BO_LAnd: 2428 case BO_LOr: 2429 case BO_Assign: 2430 case BO_Comma: 2431 assert(false && "Not valid compound assignment operators"); 2432 break; 2433 } 2434 2435 llvm_unreachable("Unhandled compound assignment operator"); 2436} 2437