Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1 | //===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Expr nodes with scalar LLVM types as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Fariborz Jahanian | 07ca727 | 2009-10-10 20:07:56 +0000 | [diff] [blame] | 15 | #include "CGObjCRuntime.h" |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 16 | #include "CodeGenModule.h" |
Daniel Dunbar | ad319a7 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 6630e10 | 2008-08-12 05:08:18 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
Daniel Dunbar | ad319a7 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | ff2367c | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 21 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/Function.h" |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 24 | #include "llvm/GlobalVariable.h" |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 26 | #include "llvm/Module.h" |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 35710d18 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CFG.h" |
Mike Stump | cb2fbcb | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 1800c18 | 2008-01-03 07:05:49 +0000 | [diff] [blame] | 30 | #include <cstdarg> |
Ted Kremenek | f182e81 | 2007-12-10 23:44:32 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | using namespace CodeGen; |
| 34 | using llvm::Value; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // Scalar Expression Emitter |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
| 40 | struct BinOpInfo { |
| 41 | Value *LHS; |
| 42 | Value *RHS; |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 43 | QualType Ty; // Computation Type. |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 44 | const BinaryOperator *E; |
| 45 | }; |
| 46 | |
| 47 | namespace { |
| 48 | class VISIBILITY_HIDDEN ScalarExprEmitter |
| 49 | : public StmtVisitor<ScalarExprEmitter, Value*> { |
| 50 | CodeGenFunction &CGF; |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 51 | CGBuilderTy &Builder; |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 52 | bool IgnoreResultAssign; |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 53 | llvm::LLVMContext &VMContext; |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 54 | public: |
| 55 | |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 56 | ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false) |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 57 | : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira), |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 58 | VMContext(cgf.getLLVMContext()) { |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 59 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 61 | //===--------------------------------------------------------------------===// |
| 62 | // Utilities |
| 63 | //===--------------------------------------------------------------------===// |
| 64 | |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 65 | bool TestAndClearIgnoreResultAssign() { |
Chris Lattner | 2a7deb6 | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 66 | bool I = IgnoreResultAssign; |
| 67 | IgnoreResultAssign = false; |
| 68 | return I; |
| 69 | } |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 71 | const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } |
| 72 | LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } |
| 73 | |
| 74 | Value *EmitLoadOfLValue(LValue LV, QualType T) { |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 75 | return CGF.EmitLoadOfLValue(LV, T).getScalarVal(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 76 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 78 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 79 | /// value l-value, this method emits the address of the l-value, then loads |
| 80 | /// and returns the result. |
| 81 | Value *EmitLoadOfLValue(const Expr *E) { |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 82 | return EmitLoadOfLValue(EmitLValue(E), E->getType()); |
| 83 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 84 | |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 85 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 2e92888 | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 86 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 87 | Value *EmitConversionToBool(Value *Src, QualType DstTy); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 89 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 90 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 91 | Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy); |
| 92 | |
| 93 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 94 | /// complex type to the specified destination type, where the destination type |
| 95 | /// is an LLVM scalar type. |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 96 | Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 97 | QualType SrcTy, QualType DstTy); |
Mike Stump | ab3afd8 | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 99 | //===--------------------------------------------------------------------===// |
| 100 | // Visitor Methods |
| 101 | //===--------------------------------------------------------------------===// |
| 102 | |
| 103 | Value *VisitStmt(Stmt *S) { |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 104 | S->dump(CGF.getContext().getSourceManager()); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 105 | assert(0 && "Stmt can't have complex result type!"); |
| 106 | return 0; |
| 107 | } |
| 108 | Value *VisitExpr(Expr *S); |
Fariborz Jahanian | 52987dc | 2009-10-21 23:45:42 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 110 | Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); } |
| 111 | |
| 112 | // Leaves. |
| 113 | Value *VisitIntegerLiteral(const IntegerLiteral *E) { |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 114 | return llvm::ConstantInt::get(VMContext, E->getValue()); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 115 | } |
| 116 | Value *VisitFloatingLiteral(const FloatingLiteral *E) { |
Owen Anderson | e05f2ed | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 117 | return llvm::ConstantFP::get(VMContext, E->getValue()); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 118 | } |
| 119 | Value *VisitCharacterLiteral(const CharacterLiteral *E) { |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 120 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 121 | } |
Nate Begeman | 4c18c23 | 2007-11-15 05:40:03 +0000 | [diff] [blame] | 122 | Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 123 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
Nate Begeman | 4c18c23 | 2007-11-15 05:40:03 +0000 | [diff] [blame] | 124 | } |
Argyrios Kyrtzidis | ce4528f | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 125 | Value *VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 126 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Argyrios Kyrtzidis | ce4528f | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 127 | } |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 128 | Value *VisitGNUNullExpr(const GNUNullExpr *E) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 129 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 131 | Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 132 | return llvm::ConstantInt::get(ConvertType(E->getType()), |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 133 | CGF.getContext().typesAreCompatible( |
| 134 | E->getArgType1(), E->getArgType2())); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 135 | } |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 136 | Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
Daniel Dunbar | 88402ce | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 137 | Value *VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 138 | llvm::Value *V = |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 139 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), |
Daniel Dunbar | 8bc821a | 2008-08-16 01:41:47 +0000 | [diff] [blame] | 140 | CGF.GetIDForAddrOfLabel(E->getLabel())); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | 8bc821a | 2008-08-16 01:41:47 +0000 | [diff] [blame] | 142 | return Builder.CreateIntToPtr(V, ConvertType(E->getType())); |
Daniel Dunbar | 88402ce | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 143 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 145 | // l-values. |
| 146 | Value *VisitDeclRefExpr(DeclRefExpr *E) { |
| 147 | if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl())) |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 148 | return llvm::ConstantInt::get(VMContext, EC->getInitVal()); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 149 | return EmitLoadOfLValue(E); |
| 150 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 151 | Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) { |
| 152 | return CGF.EmitObjCSelectorExpr(E); |
Daniel Dunbar | 55310df | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 153 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 154 | Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) { |
| 155 | return CGF.EmitObjCProtocolExpr(E); |
Daniel Dunbar | 55310df | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 156 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 157 | Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
Daniel Dunbar | 55310df | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 158 | return EmitLoadOfLValue(E); |
| 159 | } |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 160 | Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { |
Daniel Dunbar | 9e22c0d | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 161 | return EmitLoadOfLValue(E); |
Daniel Dunbar | 55310df | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 162 | } |
Fariborz Jahanian | 9a84665 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 163 | Value *VisitObjCImplicitSetterGetterRefExpr( |
| 164 | ObjCImplicitSetterGetterRefExpr *E) { |
Fariborz Jahanian | 9ac5351 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 165 | return EmitLoadOfLValue(E); |
| 166 | } |
Daniel Dunbar | 55310df | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 167 | Value *VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 168 | return CGF.EmitObjCMessageExpr(E).getScalarVal(); |
Daniel Dunbar | c8317a4 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 171 | Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 172 | Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 173 | Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 174 | Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | 084bc32 | 2008-10-26 23:53:12 +0000 | [diff] [blame] | 175 | Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 176 | return EmitLoadOfLValue(E); |
| 177 | } |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 178 | Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); } |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 179 | Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
| 180 | return EmitLValue(E).getAddress(); |
| 181 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 182 | |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 183 | Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); } |
Devang Patel | 43fc86d | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 184 | |
Nate Begeman | 1935163 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 185 | Value *VisitInitListExpr(InitListExpr *E); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 186 | |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 187 | Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 188 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 189 | } |
Eli Friedman | bc633be | 2009-04-20 03:54:15 +0000 | [diff] [blame] | 190 | Value *VisitCastExpr(const CastExpr *E) { |
| 191 | // Make sure to evaluate VLA bounds now so that we have them for later. |
| 192 | if (E->getType()->isVariablyModifiedType()) |
| 193 | CGF.EmitVLASize(E->getType()); |
| 194 | |
Anders Carlsson | 8c978b4 | 2009-09-22 22:00:46 +0000 | [diff] [blame] | 195 | return EmitCastExpr(E); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 196 | } |
Anders Carlsson | 8c978b4 | 2009-09-22 22:00:46 +0000 | [diff] [blame] | 197 | Value *EmitCastExpr(const CastExpr *E); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 198 | |
| 199 | Value *VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | d8b7ae2 | 2009-05-27 03:37:57 +0000 | [diff] [blame] | 200 | if (E->getCallReturnType()->isReferenceType()) |
| 201 | return EmitLoadOfLValue(E); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 4647a21 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 203 | return CGF.EmitCallExpr(E).getScalarVal(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 204 | } |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 04a913b | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 206 | Value *VisitStmtExpr(const StmtExpr *E); |
Mike Stump | cb2fbcb | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 207 | |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 208 | Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 210 | // Unary Operators. |
| 211 | Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre); |
| 212 | Value *VisitUnaryPostDec(const UnaryOperator *E) { |
| 213 | return VisitPrePostIncDec(E, false, false); |
| 214 | } |
| 215 | Value *VisitUnaryPostInc(const UnaryOperator *E) { |
| 216 | return VisitPrePostIncDec(E, true, false); |
| 217 | } |
| 218 | Value *VisitUnaryPreDec(const UnaryOperator *E) { |
| 219 | return VisitPrePostIncDec(E, false, true); |
| 220 | } |
| 221 | Value *VisitUnaryPreInc(const UnaryOperator *E) { |
| 222 | return VisitPrePostIncDec(E, true, true); |
| 223 | } |
| 224 | Value *VisitUnaryAddrOf(const UnaryOperator *E) { |
| 225 | return EmitLValue(E->getSubExpr()).getAddress(); |
| 226 | } |
| 227 | Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
| 228 | Value *VisitUnaryPlus(const UnaryOperator *E) { |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 229 | // This differs from gcc, though, most likely due to a bug in gcc. |
| 230 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 231 | return Visit(E->getSubExpr()); |
| 232 | } |
| 233 | Value *VisitUnaryMinus (const UnaryOperator *E); |
| 234 | Value *VisitUnaryNot (const UnaryOperator *E); |
| 235 | Value *VisitUnaryLNot (const UnaryOperator *E); |
Chris Lattner | 9f0ad96 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 236 | Value *VisitUnaryReal (const UnaryOperator *E); |
| 237 | Value *VisitUnaryImag (const UnaryOperator *E); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 238 | Value *VisitUnaryExtension(const UnaryOperator *E) { |
| 239 | return Visit(E->getSubExpr()); |
| 240 | } |
Anders Carlsson | a8dc3e6 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 241 | Value *VisitUnaryOffsetOf(const UnaryOperator *E); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 242 | |
Anders Carlsson | a5d077d | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 243 | // C++ |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 244 | Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 245 | return Visit(DAE->getExpr()); |
| 246 | } |
Anders Carlsson | a5d077d | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 247 | Value *VisitCXXThisExpr(CXXThisExpr *TE) { |
| 248 | return CGF.LoadCXXThis(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Anders Carlsson | c82b86d | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 251 | Value *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { |
Anders Carlsson | 2262b30 | 2009-05-31 00:09:15 +0000 | [diff] [blame] | 252 | return CGF.EmitCXXExprWithTemporaries(E).getScalarVal(); |
Anders Carlsson | c82b86d | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 253 | } |
Anders Carlsson | 4a7b49b | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 254 | Value *VisitCXXNewExpr(const CXXNewExpr *E) { |
| 255 | return CGF.EmitCXXNewExpr(E); |
| 256 | } |
Anders Carlsson | 81f0df9 | 2009-08-16 21:13:42 +0000 | [diff] [blame] | 257 | Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
| 258 | CGF.EmitCXXDeleteExpr(E); |
| 259 | return 0; |
| 260 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 261 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 262 | Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) { |
| 263 | // C++ [expr.pseudo]p1: |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 264 | // The result shall only be used as the operand for the function call |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 265 | // operator (), and the result of such a call has type void. The only |
| 266 | // effect is the evaluation of the postfix-expression before the dot or |
| 267 | // arrow. |
| 268 | CGF.EmitScalarExpr(E->getBase()); |
| 269 | return 0; |
| 270 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 271 | |
Anders Carlsson | 04c3bf4 | 2009-09-15 04:39:46 +0000 | [diff] [blame] | 272 | Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
| 273 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
| 274 | } |
| 275 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 276 | // Binary Operators. |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 277 | Value *EmitMul(const BinOpInfo &Ops) { |
Mike Stump | d3e3885 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 278 | if (CGF.getContext().getLangOptions().OverflowChecking |
| 279 | && Ops.Ty->isSignedIntegerType()) |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 280 | return EmitOverflowCheckedBinOp(Ops); |
Chris Lattner | 94dfae2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 281 | if (Ops.LHS->getType()->isFPOrFPVector()) |
| 282 | return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul"); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 283 | return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); |
| 284 | } |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 285 | /// Create a binary op that checks for overflow. |
| 286 | /// Currently only supports +, - and *. |
| 287 | Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 288 | Value *EmitDiv(const BinOpInfo &Ops); |
| 289 | Value *EmitRem(const BinOpInfo &Ops); |
| 290 | Value *EmitAdd(const BinOpInfo &Ops); |
| 291 | Value *EmitSub(const BinOpInfo &Ops); |
| 292 | Value *EmitShl(const BinOpInfo &Ops); |
| 293 | Value *EmitShr(const BinOpInfo &Ops); |
| 294 | Value *EmitAnd(const BinOpInfo &Ops) { |
| 295 | return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); |
| 296 | } |
| 297 | Value *EmitXor(const BinOpInfo &Ops) { |
| 298 | return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); |
| 299 | } |
| 300 | Value *EmitOr (const BinOpInfo &Ops) { |
| 301 | return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); |
| 302 | } |
| 303 | |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 304 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
Chris Lattner | b633469 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 305 | Value *EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 306 | Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); |
| 307 | |
| 308 | // Binary operators and binary compound assignment operators. |
| 309 | #define HANDLEBINOP(OP) \ |
Chris Lattner | b633469 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 310 | Value *VisitBin ## OP(const BinaryOperator *E) { \ |
| 311 | return Emit ## OP(EmitBinOps(E)); \ |
| 312 | } \ |
| 313 | Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ |
| 314 | return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 315 | } |
| 316 | HANDLEBINOP(Mul); |
| 317 | HANDLEBINOP(Div); |
| 318 | HANDLEBINOP(Rem); |
| 319 | HANDLEBINOP(Add); |
Daniel Dunbar | bfb1cd7 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 320 | HANDLEBINOP(Sub); |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 321 | HANDLEBINOP(Shl); |
| 322 | HANDLEBINOP(Shr); |
| 323 | HANDLEBINOP(And); |
| 324 | HANDLEBINOP(Xor); |
| 325 | HANDLEBINOP(Or); |
| 326 | #undef HANDLEBINOP |
Daniel Dunbar | bfb1cd7 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 328 | // Comparisons. |
| 329 | Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, |
| 330 | unsigned SICmpOpc, unsigned FCmpOpc); |
| 331 | #define VISITCOMP(CODE, UI, SI, FP) \ |
| 332 | Value *VisitBin##CODE(const BinaryOperator *E) { \ |
| 333 | return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ |
| 334 | llvm::FCmpInst::FP); } |
| 335 | VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT); |
| 336 | VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT); |
| 337 | VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE); |
| 338 | VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE); |
| 339 | VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ); |
| 340 | VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE); |
| 341 | #undef VISITCOMP |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 343 | Value *VisitBinAssign (const BinaryOperator *E); |
| 344 | |
| 345 | Value *VisitBinLAnd (const BinaryOperator *E); |
| 346 | Value *VisitBinLOr (const BinaryOperator *E); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 347 | Value *VisitBinComma (const BinaryOperator *E); |
| 348 | |
| 349 | // Other Operators. |
Mike Stump | ab3afd8 | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 350 | Value *VisitBlockExpr(const BlockExpr *BE); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 351 | Value *VisitConditionalOperator(const ConditionalOperator *CO); |
| 352 | Value *VisitChooseExpr(ChooseExpr *CE); |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 353 | Value *VisitVAArgExpr(VAArgExpr *VE); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 354 | Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 355 | return CGF.EmitObjCStringLiteral(E); |
| 356 | } |
| 357 | }; |
| 358 | } // end anonymous namespace. |
| 359 | |
| 360 | //===----------------------------------------------------------------------===// |
| 361 | // Utilities |
| 362 | //===----------------------------------------------------------------------===// |
| 363 | |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 364 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 2e92888 | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 365 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 366 | Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 367 | assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 368 | |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 369 | if (SrcType->isRealFloatingType()) { |
| 370 | // Compare against 0.0 for fp scalars. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 371 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 372 | return Builder.CreateFCmpUNE(Src, Zero, "tobool"); |
| 373 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 374 | |
Anders Carlsson | f48123b | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 375 | if (SrcType->isMemberPointerType()) { |
| 376 | // FIXME: This is ABI specific. |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 377 | |
Anders Carlsson | f48123b | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 378 | // Compare against -1. |
| 379 | llvm::Value *NegativeOne = llvm::Constant::getAllOnesValue(Src->getType()); |
| 380 | return Builder.CreateICmpNE(Src, NegativeOne, "tobool"); |
| 381 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 382 | |
Daniel Dunbar | ef957f3 | 2008-08-25 10:38:11 +0000 | [diff] [blame] | 383 | assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) && |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 384 | "Unknown scalar type to convert"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 385 | |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 386 | // Because of the type rules of C, we often end up computing a logical value, |
| 387 | // then zero extending it to int, then wanting it as a logical value again. |
| 388 | // Optimize this common case. |
| 389 | if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) { |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 390 | if (ZI->getOperand(0)->getType() == |
| 391 | llvm::Type::getInt1Ty(CGF.getLLVMContext())) { |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 392 | Value *Result = ZI->getOperand(0); |
Eli Friedman | 7031d73 | 2008-01-29 18:13:51 +0000 | [diff] [blame] | 393 | // If there aren't any more uses, zap the instruction to save space. |
| 394 | // Note that there can be more uses, for example if this |
| 395 | // is the result of an assignment. |
| 396 | if (ZI->use_empty()) |
| 397 | ZI->eraseFromParent(); |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 398 | return Result; |
| 399 | } |
| 400 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 401 | |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 402 | // Compare against an integer or pointer null. |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 403 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
Chris Lattner | e004438 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 404 | return Builder.CreateICmpNE(Src, Zero, "tobool"); |
| 405 | } |
| 406 | |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 407 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 408 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 409 | Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, |
| 410 | QualType DstType) { |
Chris Lattner | 0f398c4 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 411 | SrcType = CGF.getContext().getCanonicalType(SrcType); |
| 412 | DstType = CGF.getContext().getCanonicalType(DstType); |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 413 | if (SrcType == DstType) return Src; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 414 | |
Chris Lattner | 08c611e | 2007-08-26 07:21:11 +0000 | [diff] [blame] | 415 | if (DstType->isVoidType()) return 0; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 416 | |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 417 | llvm::LLVMContext &VMContext = CGF.getLLVMContext(); |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 418 | |
| 419 | // Handle conversions to bool first, they are special: comparisons against 0. |
Chris Lattner | c141c1b | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 420 | if (DstType->isBooleanType()) |
| 421 | return EmitConversionToBool(Src, SrcType); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 423 | const llvm::Type *DstTy = ConvertType(DstType); |
| 424 | |
| 425 | // Ignore conversions like int -> uint. |
| 426 | if (Src->getType() == DstTy) |
| 427 | return Src; |
| 428 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 429 | // Handle pointer conversions next: pointers can only be converted to/from |
| 430 | // other pointers and integers. Check for pointer types in terms of LLVM, as |
| 431 | // some native types (like Obj-C id) may map to a pointer type. |
Daniel Dunbar | 427f873 | 2008-08-25 09:51:32 +0000 | [diff] [blame] | 432 | if (isa<llvm::PointerType>(DstTy)) { |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 433 | // The source value may be an integer, or a pointer. |
Anders Carlsson | 12f5a25 | 2009-09-12 04:57:16 +0000 | [diff] [blame] | 434 | if (isa<llvm::PointerType>(Src->getType())) |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 435 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
Anders Carlsson | 12f5a25 | 2009-09-12 04:57:16 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 437 | assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
Eli Friedman | 42d2a3a | 2009-03-04 04:02:35 +0000 | [diff] [blame] | 438 | // First, convert to the correct width so that we control the kind of |
| 439 | // extension. |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 440 | const llvm::Type *MiddleTy = |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 441 | llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); |
Eli Friedman | 42d2a3a | 2009-03-04 04:02:35 +0000 | [diff] [blame] | 442 | bool InputSigned = SrcType->isSignedIntegerType(); |
| 443 | llvm::Value* IntResult = |
| 444 | Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); |
| 445 | // Then, cast to pointer. |
| 446 | return Builder.CreateIntToPtr(IntResult, DstTy, "conv"); |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 447 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 448 | |
Daniel Dunbar | 427f873 | 2008-08-25 09:51:32 +0000 | [diff] [blame] | 449 | if (isa<llvm::PointerType>(Src->getType())) { |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 450 | // Must be an ptr to int cast. |
| 451 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
Anders Carlsson | e89b84a | 2007-10-31 23:18:02 +0000 | [diff] [blame] | 452 | return Builder.CreatePtrToInt(Src, DstTy, "conv"); |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 453 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 454 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 455 | // A scalar can be splatted to an extended vector of the same element type |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 456 | if (DstType->isExtVectorType() && !SrcType->isVectorType()) { |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 457 | // Cast the scalar to element type |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 458 | QualType EltTy = DstType->getAs<ExtVectorType>()->getElementType(); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 459 | llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy); |
| 460 | |
| 461 | // Insert the element in element zero of an undef vector |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 462 | llvm::Value *UnV = llvm::UndefValue::get(DstTy); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 463 | llvm::Value *Idx = |
| 464 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 465 | UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp"); |
| 466 | |
| 467 | // Splat the element across to all elements |
| 468 | llvm::SmallVector<llvm::Constant*, 16> Args; |
| 469 | unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); |
| 470 | for (unsigned i = 0; i < NumElements; i++) |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 471 | Args.push_back(llvm::ConstantInt::get( |
| 472 | llvm::Type::getInt32Ty(VMContext), 0)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 473 | |
Owen Anderson | 3cc120a | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 474 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 475 | llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); |
| 476 | return Yay; |
| 477 | } |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 6cba8e9 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 479 | // Allow bitcast from vector to integer/fp of the same size. |
Anders Carlsson | a297e7a | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 480 | if (isa<llvm::VectorType>(Src->getType()) || |
Chris Lattner | 6cba8e9 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 481 | isa<llvm::VectorType>(DstTy)) |
Anders Carlsson | a297e7a | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 482 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 484 | // Finally, we have the arithmetic types: real int/float. |
| 485 | if (isa<llvm::IntegerType>(Src->getType())) { |
| 486 | bool InputSigned = SrcType->isSignedIntegerType(); |
Anders Carlsson | c9d41e7 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 487 | if (isa<llvm::IntegerType>(DstTy)) |
| 488 | return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); |
| 489 | else if (InputSigned) |
| 490 | return Builder.CreateSIToFP(Src, DstTy, "conv"); |
| 491 | else |
| 492 | return Builder.CreateUIToFP(Src, DstTy, "conv"); |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 493 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 494 | |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 495 | assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 496 | if (isa<llvm::IntegerType>(DstTy)) { |
Anders Carlsson | c9d41e7 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 497 | if (DstType->isSignedIntegerType()) |
| 498 | return Builder.CreateFPToSI(Src, DstTy, "conv"); |
| 499 | else |
| 500 | return Builder.CreateFPToUI(Src, DstTy, "conv"); |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | assert(DstTy->isFloatingPoint() && "Unknown real conversion"); |
Anders Carlsson | c9d41e7 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 504 | if (DstTy->getTypeID() < Src->getType()->getTypeID()) |
| 505 | return Builder.CreateFPTrunc(Src, DstTy, "conv"); |
| 506 | else |
| 507 | return Builder.CreateFPExt(Src, DstTy, "conv"); |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 510 | /// EmitComplexToScalarConversion - Emit a conversion from the specified complex |
| 511 | /// type to the specified destination type, where the destination type is an |
| 512 | /// LLVM scalar type. |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 513 | Value *ScalarExprEmitter:: |
| 514 | EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 515 | QualType SrcTy, QualType DstTy) { |
Chris Lattner | c141c1b | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 516 | // Get the source element type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 517 | SrcTy = SrcTy->getAs<ComplexType>()->getElementType(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 518 | |
Chris Lattner | c141c1b | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 519 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 520 | if (DstTy->isBooleanType()) { |
| 521 | // Complex != 0 -> (Real != 0) | (Imag != 0) |
| 522 | Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 523 | Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); |
| 524 | return Builder.CreateOr(Src.first, Src.second, "tobool"); |
| 525 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 527 | // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, |
| 528 | // the imaginary part of the complex value is discarded and the value of the |
| 529 | // real part is converted according to the conversion rules for the |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 530 | // corresponding real type. |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 531 | return EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 532 | } |
| 533 | |
| 534 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 535 | //===----------------------------------------------------------------------===// |
| 536 | // Visitor Methods |
| 537 | //===----------------------------------------------------------------------===// |
| 538 | |
| 539 | Value *ScalarExprEmitter::VisitExpr(Expr *E) { |
Fariborz Jahanian | 52987dc | 2009-10-21 23:45:42 +0000 | [diff] [blame] | 540 | if (const BinaryOperator *BExpr = dyn_cast<BinaryOperator>(E)) |
Fariborz Jahanian | ffba662 | 2009-10-22 22:57:31 +0000 | [diff] [blame] | 541 | if (BExpr->getOpcode() == BinaryOperator::PtrMemD) { |
| 542 | LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(BExpr); |
| 543 | Value *InVal = CGF.EmitLoadOfLValue(LV, E->getType()).getScalarVal(); |
| 544 | return InVal; |
| 545 | } |
Fariborz Jahanian | 52987dc | 2009-10-21 23:45:42 +0000 | [diff] [blame] | 546 | |
Daniel Dunbar | a7c8cf6 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 547 | CGF.ErrorUnsupported(E, "scalar expression"); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 548 | if (E->getType()->isVoidType()) |
| 549 | return 0; |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 550 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 553 | Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
| 554 | llvm::SmallVector<llvm::Constant*, 32> indices; |
| 555 | for (unsigned i = 2; i < E->getNumSubExprs(); i++) { |
| 556 | indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i)))); |
| 557 | } |
| 558 | Value* V1 = CGF.EmitScalarExpr(E->getExpr(0)); |
| 559 | Value* V2 = CGF.EmitScalarExpr(E->getExpr(1)); |
Owen Anderson | 3cc120a | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 560 | Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size()); |
Eli Friedman | a1b4ed8 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 561 | return Builder.CreateShuffleVector(V1, V2, SV, "shuffle"); |
| 562 | } |
| 563 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 564 | Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 565 | TestAndClearIgnoreResultAssign(); |
| 566 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 567 | // Emit subscript expressions in rvalue context's. For most cases, this just |
| 568 | // loads the lvalue formed by the subscript expr. However, we have to be |
| 569 | // careful, because the base of a vector subscript is occasionally an rvalue, |
| 570 | // so we can't get it as an lvalue. |
| 571 | if (!E->getBase()->getType()->isVectorType()) |
| 572 | return EmitLoadOfLValue(E); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 574 | // Handle the vector case. The base must be a vector, the index must be an |
| 575 | // integer value. |
| 576 | Value *Base = Visit(E->getBase()); |
| 577 | Value *Idx = Visit(E->getIdx()); |
Eli Friedman | e381f7e | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 578 | bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType(); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 579 | Idx = Builder.CreateIntCast(Idx, |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 580 | llvm::Type::getInt32Ty(CGF.getLLVMContext()), |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 581 | IdxSigned, |
Eli Friedman | 754d5ac | 2009-03-28 03:27:06 +0000 | [diff] [blame] | 582 | "vecidxcast"); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 583 | return Builder.CreateExtractElement(Base, Idx, "vecext"); |
| 584 | } |
| 585 | |
Nate Begeman | 1935163 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 586 | static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx, |
| 587 | unsigned Off, const llvm::Type *I32Ty) { |
| 588 | int MV = SVI->getMaskValue(Idx); |
| 589 | if (MV == -1) |
| 590 | return llvm::UndefValue::get(I32Ty); |
| 591 | return llvm::ConstantInt::get(I32Ty, Off+MV); |
| 592 | } |
| 593 | |
| 594 | Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) { |
| 595 | bool Ignore = TestAndClearIgnoreResultAssign(); |
| 596 | (void)Ignore; |
| 597 | assert (Ignore == false && "init list ignored"); |
| 598 | unsigned NumInitElements = E->getNumInits(); |
| 599 | |
| 600 | if (E->hadArrayRangeDesignator()) |
| 601 | CGF.ErrorUnsupported(E, "GNU array range designator extension"); |
| 602 | |
| 603 | const llvm::VectorType *VType = |
| 604 | dyn_cast<llvm::VectorType>(ConvertType(E->getType())); |
| 605 | |
| 606 | // We have a scalar in braces. Just use the first element. |
| 607 | if (!VType) |
| 608 | return Visit(E->getInit(0)); |
| 609 | |
| 610 | unsigned ResElts = VType->getNumElements(); |
| 611 | const llvm::Type *I32Ty = llvm::Type::getInt32Ty(CGF.getLLVMContext()); |
| 612 | |
| 613 | // Loop over initializers collecting the Value for each, and remembering |
| 614 | // whether the source was swizzle (ExtVectorElementExpr). This will allow |
| 615 | // us to fold the shuffle for the swizzle into the shuffle for the vector |
| 616 | // initializer, since LLVM optimizers generally do not want to touch |
| 617 | // shuffles. |
| 618 | unsigned CurIdx = 0; |
| 619 | bool VIsUndefShuffle = false; |
| 620 | llvm::Value *V = llvm::UndefValue::get(VType); |
| 621 | for (unsigned i = 0; i != NumInitElements; ++i) { |
| 622 | Expr *IE = E->getInit(i); |
| 623 | Value *Init = Visit(IE); |
| 624 | llvm::SmallVector<llvm::Constant*, 16> Args; |
| 625 | |
| 626 | const llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType()); |
| 627 | |
| 628 | // Handle scalar elements. If the scalar initializer is actually one |
| 629 | // element of a different vector of the same width, use shuffle instead of |
| 630 | // extract+insert. |
| 631 | if (!VVT) { |
| 632 | if (isa<ExtVectorElementExpr>(IE)) { |
| 633 | llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init); |
| 634 | |
| 635 | if (EI->getVectorOperandType()->getNumElements() == ResElts) { |
| 636 | llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand()); |
| 637 | Value *LHS = 0, *RHS = 0; |
| 638 | if (CurIdx == 0) { |
| 639 | // insert into undef -> shuffle (src, undef) |
| 640 | Args.push_back(C); |
| 641 | for (unsigned j = 1; j != ResElts; ++j) |
| 642 | Args.push_back(llvm::UndefValue::get(I32Ty)); |
| 643 | |
| 644 | LHS = EI->getVectorOperand(); |
| 645 | RHS = V; |
| 646 | VIsUndefShuffle = true; |
| 647 | } else if (VIsUndefShuffle) { |
| 648 | // insert into undefshuffle && size match -> shuffle (v, src) |
| 649 | llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V); |
| 650 | for (unsigned j = 0; j != CurIdx; ++j) |
| 651 | Args.push_back(getMaskElt(SVV, j, 0, I32Ty)); |
| 652 | Args.push_back(llvm::ConstantInt::get(I32Ty, |
| 653 | ResElts + C->getZExtValue())); |
| 654 | for (unsigned j = CurIdx + 1; j != ResElts; ++j) |
| 655 | Args.push_back(llvm::UndefValue::get(I32Ty)); |
| 656 | |
| 657 | LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); |
| 658 | RHS = EI->getVectorOperand(); |
| 659 | VIsUndefShuffle = false; |
| 660 | } |
| 661 | if (!Args.empty()) { |
| 662 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); |
| 663 | V = Builder.CreateShuffleVector(LHS, RHS, Mask); |
| 664 | ++CurIdx; |
| 665 | continue; |
| 666 | } |
| 667 | } |
| 668 | } |
| 669 | Value *Idx = llvm::ConstantInt::get(I32Ty, CurIdx); |
| 670 | V = Builder.CreateInsertElement(V, Init, Idx, "vecinit"); |
| 671 | VIsUndefShuffle = false; |
| 672 | ++CurIdx; |
| 673 | continue; |
| 674 | } |
| 675 | |
| 676 | unsigned InitElts = VVT->getNumElements(); |
| 677 | |
| 678 | // If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's |
| 679 | // input is the same width as the vector being constructed, generate an |
| 680 | // optimized shuffle of the swizzle input into the result. |
Nate Begeman | b8326be | 2009-10-25 02:26:01 +0000 | [diff] [blame^] | 681 | unsigned Offset = (CurIdx == 0) ? 0 : ResElts; |
Nate Begeman | 1935163 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 682 | if (isa<ExtVectorElementExpr>(IE)) { |
| 683 | llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init); |
| 684 | Value *SVOp = SVI->getOperand(0); |
| 685 | const llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType()); |
| 686 | |
| 687 | if (OpTy->getNumElements() == ResElts) { |
Nate Begeman | 1935163 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 688 | for (unsigned j = 0; j != CurIdx; ++j) { |
| 689 | // If the current vector initializer is a shuffle with undef, merge |
| 690 | // this shuffle directly into it. |
| 691 | if (VIsUndefShuffle) { |
| 692 | Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0, |
| 693 | I32Ty)); |
| 694 | } else { |
| 695 | Args.push_back(llvm::ConstantInt::get(I32Ty, j)); |
| 696 | } |
| 697 | } |
| 698 | for (unsigned j = 0, je = InitElts; j != je; ++j) |
| 699 | Args.push_back(getMaskElt(SVI, j, Offset, I32Ty)); |
| 700 | for (unsigned j = CurIdx + InitElts; j != ResElts; ++j) |
| 701 | Args.push_back(llvm::UndefValue::get(I32Ty)); |
| 702 | |
| 703 | if (VIsUndefShuffle) |
| 704 | V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); |
| 705 | |
| 706 | Init = SVOp; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | // Extend init to result vector length, and then shuffle its contribution |
| 711 | // to the vector initializer into V. |
| 712 | if (Args.empty()) { |
| 713 | for (unsigned j = 0; j != InitElts; ++j) |
| 714 | Args.push_back(llvm::ConstantInt::get(I32Ty, j)); |
| 715 | for (unsigned j = InitElts; j != ResElts; ++j) |
| 716 | Args.push_back(llvm::UndefValue::get(I32Ty)); |
| 717 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); |
| 718 | Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT), |
Nate Begeman | b8326be | 2009-10-25 02:26:01 +0000 | [diff] [blame^] | 719 | Mask, "vext"); |
Nate Begeman | 1935163 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 720 | |
| 721 | Args.clear(); |
| 722 | for (unsigned j = 0; j != CurIdx; ++j) |
| 723 | Args.push_back(llvm::ConstantInt::get(I32Ty, j)); |
| 724 | for (unsigned j = 0; j != InitElts; ++j) |
Nate Begeman | b8326be | 2009-10-25 02:26:01 +0000 | [diff] [blame^] | 725 | Args.push_back(llvm::ConstantInt::get(I32Ty, j+Offset)); |
Nate Begeman | 1935163 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 726 | for (unsigned j = CurIdx + InitElts; j != ResElts; ++j) |
| 727 | Args.push_back(llvm::UndefValue::get(I32Ty)); |
| 728 | } |
| 729 | |
| 730 | // If V is undef, make sure it ends up on the RHS of the shuffle to aid |
| 731 | // merging subsequent shuffles into this one. |
| 732 | if (CurIdx == 0) |
| 733 | std::swap(V, Init); |
| 734 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); |
| 735 | V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit"); |
| 736 | VIsUndefShuffle = isa<llvm::UndefValue>(Init); |
| 737 | CurIdx += InitElts; |
| 738 | } |
| 739 | |
| 740 | // FIXME: evaluate codegen vs. shuffling against constant null vector. |
| 741 | // Emit remaining default initializers. |
| 742 | const llvm::Type *EltTy = VType->getElementType(); |
| 743 | |
| 744 | // Emit remaining default initializers |
| 745 | for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) { |
| 746 | Value *Idx = llvm::ConstantInt::get(I32Ty, CurIdx); |
| 747 | llvm::Value *Init = llvm::Constant::getNullValue(EltTy); |
| 748 | V = Builder.CreateInsertElement(V, Init, Idx, "vecinit"); |
| 749 | } |
| 750 | return V; |
| 751 | } |
| 752 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 753 | // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts |
| 754 | // have to handle a more broad range of conversions than explicit casts, as they |
| 755 | // handle things like function to ptr-to-function decay etc. |
Anders Carlsson | 8c978b4 | 2009-09-22 22:00:46 +0000 | [diff] [blame] | 756 | Value *ScalarExprEmitter::EmitCastExpr(const CastExpr *CE) { |
| 757 | const Expr *E = CE->getSubExpr(); |
| 758 | QualType DestTy = CE->getType(); |
| 759 | CastExpr::CastKind Kind = CE->getCastKind(); |
| 760 | |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 761 | if (!DestTy->isVoidType()) |
| 762 | TestAndClearIgnoreResultAssign(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 763 | |
Anders Carlsson | 3df53bc | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 764 | switch (Kind) { |
| 765 | default: |
Anders Carlsson | 7cd39e0 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 766 | // FIXME: Assert here. |
| 767 | // assert(0 && "Unhandled cast kind!"); |
| 768 | break; |
| 769 | case CastExpr::CK_Unknown: |
| 770 | // FIXME: We should really assert here - Unknown casts should never get |
| 771 | // as far as to codegen. |
Anders Carlsson | 3df53bc | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 772 | break; |
Anders Carlsson | f1ae6d4 | 2009-09-01 20:52:42 +0000 | [diff] [blame] | 773 | case CastExpr::CK_BitCast: { |
| 774 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 775 | return Builder.CreateBitCast(Src, ConvertType(DestTy)); |
| 776 | } |
Anders Carlsson | c327709 | 2009-08-24 18:37:17 +0000 | [diff] [blame] | 777 | case CastExpr::CK_ArrayToPointerDecay: { |
| 778 | assert(E->getType()->isArrayType() && |
| 779 | "Array to pointer decay must have array source type!"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 780 | |
Anders Carlsson | c327709 | 2009-08-24 18:37:17 +0000 | [diff] [blame] | 781 | Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays. |
| 782 | |
| 783 | // Note that VLA pointers are always decayed, so we don't need to do |
| 784 | // anything here. |
| 785 | if (!E->getType()->isVariableArrayType()) { |
| 786 | assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer"); |
| 787 | assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 788 | ->getElementType()) && |
| 789 | "Expected pointer to array"); |
| 790 | V = Builder.CreateStructGEP(V, 0, "arraydecay"); |
| 791 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 792 | |
Anders Carlsson | c327709 | 2009-08-24 18:37:17 +0000 | [diff] [blame] | 793 | // The resultant pointer type can be implicitly casted to other pointer |
| 794 | // types as well (e.g. void*) and can be implicitly converted to integer. |
| 795 | const llvm::Type *DestLTy = ConvertType(DestTy); |
| 796 | if (V->getType() != DestLTy) { |
| 797 | if (isa<llvm::PointerType>(DestLTy)) |
| 798 | V = Builder.CreateBitCast(V, DestLTy, "ptrconv"); |
| 799 | else { |
| 800 | assert(isa<llvm::IntegerType>(DestLTy) && "Unknown array decay"); |
| 801 | V = Builder.CreatePtrToInt(V, DestLTy, "ptrconv"); |
| 802 | } |
| 803 | } |
| 804 | return V; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 805 | } |
Anders Carlsson | 3df53bc | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 806 | case CastExpr::CK_NullToMemberPointer: |
| 807 | return CGF.CGM.EmitNullConstant(DestTy); |
Anders Carlsson | 12f5a25 | 2009-09-12 04:57:16 +0000 | [diff] [blame] | 808 | |
| 809 | case CastExpr::CK_DerivedToBase: { |
| 810 | const RecordType *DerivedClassTy = |
| 811 | E->getType()->getAs<PointerType>()->getPointeeType()->getAs<RecordType>(); |
| 812 | CXXRecordDecl *DerivedClassDecl = |
| 813 | cast<CXXRecordDecl>(DerivedClassTy->getDecl()); |
| 814 | |
| 815 | const RecordType *BaseClassTy = |
| 816 | DestTy->getAs<PointerType>()->getPointeeType()->getAs<RecordType>(); |
| 817 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl()); |
| 818 | |
| 819 | Value *Src = Visit(const_cast<Expr*>(E)); |
Anders Carlsson | 360e7d0 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 820 | |
Anders Carlsson | 8c978b4 | 2009-09-22 22:00:46 +0000 | [diff] [blame] | 821 | bool NullCheckValue = true; |
Anders Carlsson | 360e7d0 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 822 | |
Anders Carlsson | 8c978b4 | 2009-09-22 22:00:46 +0000 | [diff] [blame] | 823 | if (isa<CXXThisExpr>(E)) { |
| 824 | // We always assume that 'this' is never null. |
Anders Carlsson | 360e7d0 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 825 | NullCheckValue = false; |
Anders Carlsson | 8c978b4 | 2009-09-22 22:00:46 +0000 | [diff] [blame] | 826 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) { |
| 827 | // And that lvalue casts are never null. |
| 828 | if (ICE->isLvalueCast()) |
| 829 | NullCheckValue = false; |
| 830 | } |
Anders Carlsson | 12f5a25 | 2009-09-12 04:57:16 +0000 | [diff] [blame] | 831 | return CGF.GetAddressCXXOfBaseClass(Src, DerivedClassDecl, BaseClassDecl, |
Anders Carlsson | 360e7d0 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 832 | NullCheckValue); |
Anders Carlsson | 12f5a25 | 2009-09-12 04:57:16 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Anders Carlsson | 7cd39e0 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 835 | case CastExpr::CK_IntegralToPointer: { |
| 836 | Value *Src = Visit(const_cast<Expr*>(E)); |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 837 | |
| 838 | // First, convert to the correct width so that we control the kind of |
| 839 | // extension. |
| 840 | const llvm::Type *MiddleTy = |
| 841 | llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); |
| 842 | bool InputSigned = E->getType()->isSignedIntegerType(); |
| 843 | llvm::Value* IntResult = |
| 844 | Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); |
| 845 | |
| 846 | return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy)); |
Anders Carlsson | 7cd39e0 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | case CastExpr::CK_PointerToIntegral: { |
| 850 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 851 | return Builder.CreatePtrToInt(Src, ConvertType(DestTy)); |
| 852 | } |
| 853 | |
Anders Carlsson | 3df53bc | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 854 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 855 | |
Chris Lattner | 5de3b17 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 856 | // Handle cases where the source is an non-complex type. |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 857 | |
Chris Lattner | df53e20 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 858 | if (!CGF.hasAggregateLLVMType(E->getType())) { |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 859 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 860 | |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 861 | // Use EmitScalarConversion to perform the conversion. |
| 862 | return EmitScalarConversion(Src, E->getType(), DestTy); |
| 863 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 864 | |
Chris Lattner | f3bc75a | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 865 | if (E->getType()->isAnyComplexType()) { |
Chris Lattner | df53e20 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 866 | // Handle cases where the source is a complex type. |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 867 | bool IgnoreImag = true; |
| 868 | bool IgnoreImagAssign = true; |
| 869 | bool IgnoreReal = IgnoreResultAssign; |
| 870 | bool IgnoreRealAssign = IgnoreResultAssign; |
| 871 | if (DestTy->isBooleanType()) |
| 872 | IgnoreImagAssign = IgnoreImag = false; |
| 873 | else if (DestTy->isVoidType()) { |
| 874 | IgnoreReal = IgnoreImag = false; |
| 875 | IgnoreRealAssign = IgnoreImagAssign = true; |
| 876 | } |
| 877 | CodeGenFunction::ComplexPairTy V |
| 878 | = CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign, |
| 879 | IgnoreImagAssign); |
| 880 | return EmitComplexToScalarConversion(V, E->getType(), DestTy); |
Chris Lattner | df53e20 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 881 | } |
Chris Lattner | 46c7161 | 2007-08-26 07:16:41 +0000 | [diff] [blame] | 882 | |
Chris Lattner | df53e20 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 883 | // Okay, this is a cast from an aggregate. It must be a cast to void. Just |
| 884 | // evaluate the result and return. |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 885 | CGF.EmitAggExpr(E, 0, false, true); |
Chris Lattner | df53e20 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 886 | return 0; |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Chris Lattner | 04a913b | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 889 | Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
Chris Lattner | 7e80097 | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 890 | return CGF.EmitCompoundStmt(*E->getSubStmt(), |
| 891 | !E->getType()->isVoidType()).getScalarVal(); |
Chris Lattner | 04a913b | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Mike Stump | 1db7d04 | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 894 | Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { |
Fariborz Jahanian | 07ca727 | 2009-10-10 20:07:56 +0000 | [diff] [blame] | 895 | llvm::Value *V = CGF.GetAddrOfBlockDecl(E); |
| 896 | if (E->getType().isObjCGCWeak()) |
| 897 | return CGF.CGM.getObjCRuntime().EmitObjCWeakRead(CGF, V); |
| 898 | return Builder.CreateLoad(V, false, "tmp"); |
Mike Stump | cb2fbcb | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 899 | } |
Chris Lattner | 04a913b | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 900 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 901 | //===----------------------------------------------------------------------===// |
| 902 | // Unary Operators |
| 903 | //===----------------------------------------------------------------------===// |
| 904 | |
| 905 | Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | 100198f | 2007-08-24 16:24:49 +0000 | [diff] [blame] | 906 | bool isInc, bool isPre) { |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 907 | LValue LV = EmitLValue(E->getSubExpr()); |
Eli Friedman | 7b47167 | 2009-03-23 03:00:06 +0000 | [diff] [blame] | 908 | QualType ValTy = E->getSubExpr()->getType(); |
| 909 | Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal(); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 910 | |
| 911 | llvm::LLVMContext &VMContext = CGF.getLLVMContext(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 912 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 913 | int AmountVal = isInc ? 1 : -1; |
Eli Friedman | e381f7e | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 914 | |
| 915 | if (ValTy->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 916 | ValTy->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | e381f7e | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 917 | // The amount of the addition/subtraction needs to account for the VLA size |
| 918 | CGF.ErrorUnsupported(E, "VLA pointer inc/dec"); |
| 919 | } |
| 920 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 921 | Value *NextVal; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 922 | if (const llvm::PointerType *PT = |
Chris Lattner | c2a0b97 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 923 | dyn_cast<llvm::PointerType>(InVal->getType())) { |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 924 | llvm::Constant *Inc = |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 925 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal); |
Chris Lattner | c2a0b97 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 926 | if (!isa<llvm::FunctionType>(PT->getElementType())) { |
Fariborz Jahanian | c3443a3 | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 927 | QualType PTEE = ValTy->getPointeeType(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 928 | if (const ObjCInterfaceType *OIT = |
Fariborz Jahanian | c3443a3 | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 929 | dyn_cast<ObjCInterfaceType>(PTEE)) { |
| 930 | // Handle interface types, which are not represented with a concrete type. |
| 931 | int size = CGF.getContext().getTypeSize(OIT) / 8; |
| 932 | if (!isInc) |
| 933 | size = -size; |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 934 | Inc = llvm::ConstantInt::get(Inc->getType(), size); |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 935 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Fariborz Jahanian | c3443a3 | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 936 | InVal = Builder.CreateBitCast(InVal, i8Ty); |
| 937 | NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr"); |
| 938 | llvm::Value *lhs = LV.getAddress(); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 939 | lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty)); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 940 | LV = LValue::MakeAddr(lhs, CGF.MakeQualifiers(ValTy)); |
Mike Stump | 658fe02 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 941 | } else |
Dan Gohman | 43b4484 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 942 | NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec"); |
Chris Lattner | c2a0b97 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 943 | } else { |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 944 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Chris Lattner | c2a0b97 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 945 | NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp"); |
| 946 | NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec"); |
| 947 | NextVal = Builder.CreateBitCast(NextVal, InVal->getType()); |
| 948 | } |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 949 | } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) { |
Chris Lattner | 746b213 | 2009-02-11 07:40:06 +0000 | [diff] [blame] | 950 | // Bool++ is an interesting case, due to promotion rules, we get: |
| 951 | // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 -> |
| 952 | // Bool = ((int)Bool+1) != 0 |
| 953 | // An interesting aspect of this is that increment is always true. |
| 954 | // Decrement does not have this property. |
Owen Anderson | fe4e347 | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 955 | NextVal = llvm::ConstantInt::getTrue(VMContext); |
Chris Lattner | 94dfae2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 956 | } else if (isa<llvm::IntegerType>(InVal->getType())) { |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 957 | NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); |
Dan Gohman | b321039 | 2009-08-12 01:16:29 +0000 | [diff] [blame] | 958 | |
| 959 | // Signed integer overflow is undefined behavior. |
| 960 | if (ValTy->isSignedIntegerType()) |
| 961 | NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 962 | else |
| 963 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
Chris Lattner | a01d898 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 964 | } else { |
| 965 | // Add the inc/dec to the real part. |
Benjamin Kramer | dde0fee | 2009-10-05 13:47:21 +0000 | [diff] [blame] | 966 | if (InVal->getType()->isFloatTy()) |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 967 | NextVal = |
| 968 | llvm::ConstantFP::get(VMContext, |
Owen Anderson | e05f2ed | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 969 | llvm::APFloat(static_cast<float>(AmountVal))); |
Benjamin Kramer | dde0fee | 2009-10-05 13:47:21 +0000 | [diff] [blame] | 970 | else if (InVal->getType()->isDoubleTy()) |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 971 | NextVal = |
Owen Anderson | e05f2ed | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 972 | llvm::ConstantFP::get(VMContext, |
| 973 | llvm::APFloat(static_cast<double>(AmountVal))); |
Chris Lattner | ff2367c | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 974 | else { |
| 975 | llvm::APFloat F(static_cast<float>(AmountVal)); |
Dale Johannesen | c48814b | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 976 | bool ignored; |
| 977 | F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero, |
| 978 | &ignored); |
Owen Anderson | e05f2ed | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 979 | NextVal = llvm::ConstantFP::get(VMContext, F); |
Chris Lattner | d3d8aca | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 980 | } |
Chris Lattner | 94dfae2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 981 | NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
Chris Lattner | a01d898 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 982 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 983 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 984 | // Store the updated result through the lvalue. |
Eli Friedman | 7b47167 | 2009-03-23 03:00:06 +0000 | [diff] [blame] | 985 | if (LV.isBitfield()) |
| 986 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, |
| 987 | &NextVal); |
| 988 | else |
| 989 | CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 990 | |
| 991 | // If this is a postinc, return the value read from memory, otherwise use the |
| 992 | // updated value. |
| 993 | return isPre ? NextVal : InVal; |
| 994 | } |
| 995 | |
| 996 | |
| 997 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 998 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 999 | Value *Op = Visit(E->getSubExpr()); |
Chris Lattner | 94dfae2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1000 | if (Op->getType()->isFPOrFPVector()) |
| 1001 | return Builder.CreateFNeg(Op, "neg"); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1002 | return Builder.CreateNeg(Op, "neg"); |
| 1003 | } |
| 1004 | |
| 1005 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1006 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1007 | Value *Op = Visit(E->getSubExpr()); |
| 1008 | return Builder.CreateNot(Op, "neg"); |
| 1009 | } |
| 1010 | |
| 1011 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 1012 | // Compare operand to zero. |
| 1013 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1015 | // Invert value. |
| 1016 | // TODO: Could dynamically modify easy computations here. For example, if |
| 1017 | // the operand is an icmp ne, turn into icmp eq. |
| 1018 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1019 | |
Anders Carlsson | 775640d | 2009-05-19 18:44:53 +0000 | [diff] [blame] | 1020 | // ZExt result to the expr type. |
| 1021 | return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext"); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1024 | /// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of |
| 1025 | /// argument of the sizeof expression as an integer. |
| 1026 | Value * |
| 1027 | ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1028 | QualType TypeToSize = E->getTypeOfArgument(); |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1029 | if (E->isSizeOf()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1030 | if (const VariableArrayType *VAT = |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1031 | CGF.getContext().getAsVariableArrayType(TypeToSize)) { |
| 1032 | if (E->isArgumentType()) { |
| 1033 | // sizeof(type) - make sure to emit the VLA size. |
| 1034 | CGF.EmitVLASize(TypeToSize); |
Eli Friedman | 3253e18 | 2009-04-20 03:21:44 +0000 | [diff] [blame] | 1035 | } else { |
| 1036 | // C99 6.5.3.4p2: If the argument is an expression of type |
| 1037 | // VLA, it is evaluated. |
| 1038 | CGF.EmitAnyExpr(E->getArgumentExpr()); |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1039 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1040 | |
Anders Carlsson | 31f8649 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 1041 | return CGF.GetVLASize(VAT); |
Anders Carlsson | 76dbc04 | 2008-12-21 03:33:21 +0000 | [diff] [blame] | 1042 | } |
Anders Carlsson | 3003288 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 1043 | } |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1044 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1045 | // If this isn't sizeof(vla), the result must be constant; use the constant |
| 1046 | // folding logic so we don't have to duplicate it here. |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1047 | Expr::EvalResult Result; |
| 1048 | E->Evaluate(Result, CGF.getContext()); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1049 | return llvm::ConstantInt::get(VMContext, Result.Val.getInt()); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Chris Lattner | 9f0ad96 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1052 | Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { |
| 1053 | Expr *Op = E->getSubExpr(); |
Chris Lattner | f3bc75a | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 1054 | if (Op->getType()->isAnyComplexType()) |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1055 | return CGF.EmitComplexExpr(Op, false, true, false, true).first; |
Chris Lattner | 9f0ad96 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1056 | return Visit(Op); |
| 1057 | } |
| 1058 | Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { |
| 1059 | Expr *Op = E->getSubExpr(); |
Chris Lattner | f3bc75a | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 1060 | if (Op->getType()->isAnyComplexType()) |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1061 | return CGF.EmitComplexExpr(Op, true, false, true, false).second; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1062 | |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1063 | // __imag on a scalar returns zero. Emit the subexpr to ensure side |
| 1064 | // effects are evaluated, but not the actual value. |
| 1065 | if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid) |
| 1066 | CGF.EmitLValue(Op); |
| 1067 | else |
| 1068 | CGF.EmitScalarExpr(Op, true); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1069 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Chris Lattner | 9f0ad96 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E) { |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1073 | Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress(); |
Eli Friedman | 8549e5d | 2009-01-24 22:38:55 +0000 | [diff] [blame] | 1074 | const llvm::Type* ResultType = ConvertType(E->getType()); |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 1075 | return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof"); |
Anders Carlsson | a8dc3e6 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1076 | } |
Chris Lattner | 9f0ad96 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1078 | //===----------------------------------------------------------------------===// |
| 1079 | // Binary Operators |
| 1080 | //===----------------------------------------------------------------------===// |
| 1081 | |
| 1082 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1083 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1084 | BinOpInfo Result; |
| 1085 | Result.LHS = Visit(E->getLHS()); |
| 1086 | Result.RHS = Visit(E->getRHS()); |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1087 | Result.Ty = E->getType(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1088 | Result.E = E; |
| 1089 | return Result; |
| 1090 | } |
| 1091 | |
Chris Lattner | b633469 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 1092 | Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1093 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1094 | bool Ignore = TestAndClearIgnoreResultAssign(); |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1095 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
| 1096 | |
| 1097 | BinOpInfo OpInfo; |
| 1098 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1099 | if (E->getComputationResultType()->isAnyComplexType()) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1100 | // This needs to go through the complex expression emitter, but it's a tad |
| 1101 | // complicated to do that... I'm leaving it out for now. (Note that we do |
| 1102 | // actually need the imaginary part of the RHS for multiplication and |
| 1103 | // division.) |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1104 | CGF.ErrorUnsupported(E, "complex compound assignment"); |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1105 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Mike Stump | c63428b | 2009-05-22 19:07:20 +0000 | [diff] [blame] | 1108 | // Emit the RHS first. __block variables need to have the rhs evaluated |
| 1109 | // first, plus this should improve codegen a little. |
| 1110 | OpInfo.RHS = Visit(E->getRHS()); |
| 1111 | OpInfo.Ty = E->getComputationResultType(); |
| 1112 | OpInfo.E = E; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1113 | // Load/convert the LHS. |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1114 | LValue LHSLV = EmitLValue(E->getLHS()); |
| 1115 | OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1116 | OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, |
| 1117 | E->getComputationLHSType()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1118 | |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1119 | // Expand the binary operator. |
| 1120 | Value *Result = (this->*Func)(OpInfo); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1121 | |
Daniel Dunbar | bfb1cd7 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1122 | // Convert the result back to the LHS type. |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1123 | Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy); |
| 1124 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1125 | // Store the result value into the LHS lvalue. Bit-fields are handled |
| 1126 | // specially because the result is altered by the store, i.e., [C99 6.5.16p1] |
| 1127 | // 'An assignment expression has the value of the left operand after the |
| 1128 | // assignment...'. |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1129 | if (LHSLV.isBitfield()) { |
| 1130 | if (!LHSLV.isVolatileQualified()) { |
| 1131 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy, |
| 1132 | &Result); |
| 1133 | return Result; |
| 1134 | } else |
| 1135 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy); |
| 1136 | } else |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1137 | CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy); |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1138 | if (Ignore) |
| 1139 | return 0; |
| 1140 | return EmitLoadOfLValue(LHSLV, E->getType()); |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1144 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
Nate Begeman | 628028b | 2007-12-30 01:28:16 +0000 | [diff] [blame] | 1145 | if (Ops.LHS->getType()->isFPOrFPVector()) |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1146 | return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1147 | else if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1148 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 1149 | else |
| 1150 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 1151 | } |
| 1152 | |
| 1153 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 1154 | // Rem in C can't be a floating point type: C99 6.5.5p2. |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1155 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1156 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 1157 | else |
| 1158 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 1159 | } |
| 1160 | |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1161 | Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) { |
| 1162 | unsigned IID; |
| 1163 | unsigned OpID = 0; |
Mike Stump | 4096859 | 2009-04-02 01:03:55 +0000 | [diff] [blame] | 1164 | |
Mike Stump | d3e3885 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1165 | switch (Ops.E->getOpcode()) { |
| 1166 | case BinaryOperator::Add: |
| 1167 | case BinaryOperator::AddAssign: |
| 1168 | OpID = 1; |
| 1169 | IID = llvm::Intrinsic::sadd_with_overflow; |
| 1170 | break; |
| 1171 | case BinaryOperator::Sub: |
| 1172 | case BinaryOperator::SubAssign: |
| 1173 | OpID = 2; |
| 1174 | IID = llvm::Intrinsic::ssub_with_overflow; |
| 1175 | break; |
| 1176 | case BinaryOperator::Mul: |
| 1177 | case BinaryOperator::MulAssign: |
| 1178 | OpID = 3; |
| 1179 | IID = llvm::Intrinsic::smul_with_overflow; |
| 1180 | break; |
| 1181 | default: |
| 1182 | assert(false && "Unsupported operation for overflow detection"); |
Daniel Dunbar | d92123f | 2009-04-08 16:23:09 +0000 | [diff] [blame] | 1183 | IID = 0; |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1184 | } |
Mike Stump | d3e3885 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1185 | OpID <<= 1; |
| 1186 | OpID |= 1; |
| 1187 | |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1188 | const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty); |
| 1189 | |
| 1190 | llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1); |
| 1191 | |
| 1192 | Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS); |
| 1193 | Value *result = Builder.CreateExtractValue(resultAndOverflow, 0); |
| 1194 | Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1); |
| 1195 | |
| 1196 | // Branch in case of overflow. |
| 1197 | llvm::BasicBlock *initialBB = Builder.GetInsertBlock(); |
| 1198 | llvm::BasicBlock *overflowBB = |
| 1199 | CGF.createBasicBlock("overflow", CGF.CurFn); |
| 1200 | llvm::BasicBlock *continueBB = |
| 1201 | CGF.createBasicBlock("overflow.continue", CGF.CurFn); |
| 1202 | |
| 1203 | Builder.CreateCondBr(overflow, overflowBB, continueBB); |
| 1204 | |
| 1205 | // Handle overflow |
| 1206 | |
| 1207 | Builder.SetInsertPoint(overflowBB); |
| 1208 | |
| 1209 | // Handler is: |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1210 | // long long *__overflow_handler)(long long a, long long b, char op, |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1211 | // char width) |
| 1212 | std::vector<const llvm::Type*> handerArgTypes; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1213 | handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext)); |
| 1214 | handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext)); |
| 1215 | handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext)); |
| 1216 | handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext)); |
| 1217 | llvm::FunctionType *handlerTy = llvm::FunctionType::get( |
| 1218 | llvm::Type::getInt64Ty(VMContext), handerArgTypes, false); |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1219 | llvm::Value *handlerFunction = |
| 1220 | CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler", |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1221 | llvm::PointerType::getUnqual(handlerTy)); |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1222 | handlerFunction = Builder.CreateLoad(handlerFunction); |
| 1223 | |
| 1224 | llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1225 | Builder.CreateSExt(Ops.LHS, llvm::Type::getInt64Ty(VMContext)), |
| 1226 | Builder.CreateSExt(Ops.RHS, llvm::Type::getInt64Ty(VMContext)), |
| 1227 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), OpID), |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1228 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1229 | cast<llvm::IntegerType>(opTy)->getBitWidth())); |
| 1230 | |
| 1231 | handlerResult = Builder.CreateTrunc(handlerResult, opTy); |
| 1232 | |
| 1233 | Builder.CreateBr(continueBB); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1234 | |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1235 | // Set up the continuation |
| 1236 | Builder.SetInsertPoint(continueBB); |
| 1237 | // Get the correct result |
| 1238 | llvm::PHINode *phi = Builder.CreatePHI(opTy); |
| 1239 | phi->reserveOperandSpace(2); |
| 1240 | phi->addIncoming(result, initialBB); |
| 1241 | phi->addIncoming(handlerResult, overflowBB); |
| 1242 | |
| 1243 | return phi; |
| 1244 | } |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1245 | |
| 1246 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1247 | if (!Ops.Ty->isAnyPointerType()) { |
Chris Lattner | 94dfae2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1248 | if (CGF.getContext().getLangOptions().OverflowChecking && |
| 1249 | Ops.Ty->isSignedIntegerType()) |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1250 | return EmitOverflowCheckedBinOp(Ops); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1251 | |
Chris Lattner | 94dfae2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1252 | if (Ops.LHS->getType()->isFPOrFPVector()) |
| 1253 | return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add"); |
Dan Gohman | b321039 | 2009-08-12 01:16:29 +0000 | [diff] [blame] | 1254 | |
| 1255 | // Signed integer overflow is undefined behavior. |
| 1256 | if (Ops.Ty->isSignedIntegerType()) |
| 1257 | return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add"); |
| 1258 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1259 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1260 | } |
Eli Friedman | e381f7e | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1261 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1262 | if (Ops.Ty->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1263 | Ops.Ty->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | e381f7e | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1264 | // The amount of the addition needs to account for the VLA size |
| 1265 | CGF.ErrorUnsupported(Ops.E, "VLA pointer addition"); |
| 1266 | } |
Chris Lattner | 20455f2 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1267 | Value *Ptr, *Idx; |
| 1268 | Expr *IdxExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1269 | const PointerType *PT = Ops.E->getLHS()->getType()->getAs<PointerType>(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1270 | const ObjCObjectPointerType *OPT = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1271 | Ops.E->getLHS()->getType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1272 | if (PT || OPT) { |
Chris Lattner | 20455f2 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1273 | Ptr = Ops.LHS; |
| 1274 | Idx = Ops.RHS; |
| 1275 | IdxExp = Ops.E->getRHS(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1276 | } else { // int + pointer |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1277 | PT = Ops.E->getRHS()->getType()->getAs<PointerType>(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1278 | OPT = Ops.E->getRHS()->getType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1279 | assert((PT || OPT) && "Invalid add expr"); |
Chris Lattner | 20455f2 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1280 | Ptr = Ops.RHS; |
| 1281 | Idx = Ops.LHS; |
| 1282 | IdxExp = Ops.E->getLHS(); |
| 1283 | } |
| 1284 | |
| 1285 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
Sanjiv Gupta | 4742515 | 2009-04-24 02:40:57 +0000 | [diff] [blame] | 1286 | if (Width < CGF.LLVMPointerWidth) { |
Chris Lattner | 20455f2 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1287 | // Zero or sign extend the pointer value based on whether the index is |
| 1288 | // signed or not. |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1289 | const llvm::Type *IdxType = |
| 1290 | llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); |
Chris Lattner | 0f398c4 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 1291 | if (IdxExp->getType()->isSignedIntegerType()) |
Chris Lattner | 20455f2 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1292 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 1293 | else |
| 1294 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 1295 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1296 | const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1297 | // Handle interface types, which are not represented with a concrete type. |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1298 | if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1299 | llvm::Value *InterfaceSize = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1300 | llvm::ConstantInt::get(Idx->getType(), |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1301 | CGF.getContext().getTypeSize(OIT) / 8); |
| 1302 | Idx = Builder.CreateMul(Idx, InterfaceSize); |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1303 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1304 | Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); |
| 1305 | Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); |
| 1306 | return Builder.CreateBitCast(Res, Ptr->getType()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1307 | } |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1308 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1309 | // Explicitly handle GNU void* and function pointer arithmetic extensions. The |
| 1310 | // GNU void* casts amount to no-ops since our void* type is i8*, but this is |
| 1311 | // future proof. |
Daniel Dunbar | 42a8cd3 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1312 | if (ElementType->isVoidType() || ElementType->isFunctionType()) { |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1313 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | 42a8cd3 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1314 | Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1315 | Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); |
Daniel Dunbar | 42a8cd3 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1316 | return Builder.CreateBitCast(Res, Ptr->getType()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Dan Gohman | 43b4484 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1319 | return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr"); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1323 | if (!isa<llvm::PointerType>(Ops.LHS->getType())) { |
Mike Stump | d3e3885 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1324 | if (CGF.getContext().getLangOptions().OverflowChecking |
| 1325 | && Ops.Ty->isSignedIntegerType()) |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1326 | return EmitOverflowCheckedBinOp(Ops); |
Chris Lattner | 94dfae2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1327 | |
| 1328 | if (Ops.LHS->getType()->isFPOrFPVector()) |
| 1329 | return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub"); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1330 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
Mike Stump | 0c61b73 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1331 | } |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1332 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1333 | if (Ops.E->getLHS()->getType()->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1334 | Ops.E->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | e381f7e | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1335 | // The amount of the addition needs to account for the VLA size for |
| 1336 | // ptr-int |
| 1337 | // The amount of the division needs to account for the VLA size for |
| 1338 | // ptr-ptr. |
| 1339 | CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction"); |
| 1340 | } |
| 1341 | |
Daniel Dunbar | 42a8cd3 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1342 | const QualType LHSType = Ops.E->getLHS()->getType(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1343 | const QualType LHSElementType = LHSType->getPointeeType(); |
Daniel Dunbar | bfb1cd7 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1344 | if (!isa<llvm::PointerType>(Ops.RHS->getType())) { |
| 1345 | // pointer - int |
| 1346 | Value *Idx = Ops.RHS; |
| 1347 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
Sanjiv Gupta | 4742515 | 2009-04-24 02:40:57 +0000 | [diff] [blame] | 1348 | if (Width < CGF.LLVMPointerWidth) { |
Daniel Dunbar | bfb1cd7 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1349 | // Zero or sign extend the pointer value based on whether the index is |
| 1350 | // signed or not. |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1351 | const llvm::Type *IdxType = |
| 1352 | llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); |
Daniel Dunbar | bfb1cd7 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1353 | if (Ops.E->getRHS()->getType()->isSignedIntegerType()) |
| 1354 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 1355 | else |
| 1356 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 1357 | } |
| 1358 | Idx = Builder.CreateNeg(Idx, "sub.ptr.neg"); |
Daniel Dunbar | 42a8cd3 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1359 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1360 | // Handle interface types, which are not represented with a concrete type. |
| 1361 | if (const ObjCInterfaceType *OIT = |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1362 | dyn_cast<ObjCInterfaceType>(LHSElementType)) { |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1363 | llvm::Value *InterfaceSize = |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1364 | llvm::ConstantInt::get(Idx->getType(), |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1365 | CGF.getContext().getTypeSize(OIT) / 8); |
| 1366 | Idx = Builder.CreateMul(Idx, InterfaceSize); |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1367 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1368 | Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); |
| 1369 | Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr"); |
| 1370 | return Builder.CreateBitCast(Res, Ops.LHS->getType()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1371 | } |
Daniel Dunbar | ef2ffbc | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1372 | |
Daniel Dunbar | 42a8cd3 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1373 | // Explicitly handle GNU void* and function pointer arithmetic |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1374 | // extensions. The GNU void* casts amount to no-ops since our void* type is |
| 1375 | // i8*, but this is future proof. |
Daniel Dunbar | 42a8cd3 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1376 | if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1377 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | 42a8cd3 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1378 | Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); |
| 1379 | Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr"); |
| 1380 | return Builder.CreateBitCast(Res, Ops.LHS->getType()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Dan Gohman | 43b4484 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1383 | return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr"); |
Daniel Dunbar | 5aa55d5 | 2008-08-05 00:47:03 +0000 | [diff] [blame] | 1384 | } else { |
Daniel Dunbar | bfb1cd7 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1385 | // pointer - pointer |
| 1386 | Value *LHS = Ops.LHS; |
| 1387 | Value *RHS = Ops.RHS; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1388 | |
Daniel Dunbar | bfb1cd7 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1389 | uint64_t ElementSize; |
Daniel Dunbar | 5aa55d5 | 2008-08-05 00:47:03 +0000 | [diff] [blame] | 1390 | |
Chris Lattner | 60dcdc7 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 1391 | // Handle GCC extension for pointer arithmetic on void* and function pointer |
| 1392 | // types. |
| 1393 | if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { |
Daniel Dunbar | bfb1cd7 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1394 | ElementSize = 1; |
| 1395 | } else { |
| 1396 | ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8; |
| 1397 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1398 | |
Daniel Dunbar | bfb1cd7 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1399 | const llvm::Type *ResultType = ConvertType(Ops.Ty); |
| 1400 | LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); |
| 1401 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 1402 | Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1403 | |
Chris Lattner | 60dcdc7 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 1404 | // Optimize out the shift for element size of 1. |
| 1405 | if (ElementSize == 1) |
| 1406 | return BytesBetween; |
Dan Gohman | 1cebe56 | 2009-08-11 22:40:09 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1409 | // pointer difference in C is only defined in the case where both operands |
| 1410 | // are pointing to elements of an array. |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1411 | Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize); |
Dan Gohman | 1cebe56 | 2009-08-11 22:40:09 +0000 | [diff] [blame] | 1412 | return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1413 | } |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 1417 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 1418 | // RHS to the same size as the LHS. |
| 1419 | Value *RHS = Ops.RHS; |
| 1420 | if (Ops.LHS->getType() != RHS->getType()) |
| 1421 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1422 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1423 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 1424 | } |
| 1425 | |
| 1426 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 1427 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 1428 | // RHS to the same size as the LHS. |
| 1429 | Value *RHS = Ops.RHS; |
| 1430 | if (Ops.LHS->getType() != RHS->getType()) |
| 1431 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | 3d966d6 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1433 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1434 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 1435 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 1436 | } |
| 1437 | |
| 1438 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 1439 | unsigned SICmpOpc, unsigned FCmpOpc) { |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1440 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1441 | Value *Result; |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1442 | QualType LHSTy = E->getLHS()->getType(); |
Chris Lattner | 2a7deb6 | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 1443 | if (!LHSTy->isAnyComplexType()) { |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1444 | Value *LHS = Visit(E->getLHS()); |
| 1445 | Value *RHS = Visit(E->getRHS()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1446 | |
Eli Friedman | 5ac6905 | 2009-07-22 06:07:16 +0000 | [diff] [blame] | 1447 | if (LHS->getType()->isFPOrFPVector()) { |
Nate Begeman | fe79ca2 | 2008-07-25 20:16:05 +0000 | [diff] [blame] | 1448 | Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc, |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1449 | LHS, RHS, "cmp"); |
Eli Friedman | 3c28524 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 1450 | } else if (LHSTy->isSignedIntegerType()) { |
| 1451 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1452 | LHS, RHS, "cmp"); |
| 1453 | } else { |
Eli Friedman | 3c28524 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 1454 | // Unsigned integers and pointers. |
| 1455 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1456 | LHS, RHS, "cmp"); |
| 1457 | } |
Chris Lattner | 2a7deb6 | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 1458 | |
| 1459 | // If this is a vector comparison, sign extend the result to the appropriate |
| 1460 | // vector integer type and return it (don't convert to bool). |
| 1461 | if (LHSTy->isVectorType()) |
| 1462 | return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1463 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1464 | } else { |
| 1465 | // Complex Comparison: can only be an equality comparison. |
| 1466 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 1467 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1468 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1469 | QualType CETy = LHSTy->getAs<ComplexType>()->getElementType(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1471 | Value *ResultR, *ResultI; |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1472 | if (CETy->isRealFloatingType()) { |
| 1473 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1474 | LHS.first, RHS.first, "cmp.r"); |
| 1475 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1476 | LHS.second, RHS.second, "cmp.i"); |
| 1477 | } else { |
| 1478 | // Complex comparisons can only be equality comparisons. As such, signed |
| 1479 | // and unsigned opcodes are the same. |
| 1480 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 1481 | LHS.first, RHS.first, "cmp.r"); |
| 1482 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 1483 | LHS.second, RHS.second, "cmp.i"); |
| 1484 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1486 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 1487 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 1488 | } else { |
| 1489 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1490 | "Complex comparison other than == or != ?"); |
| 1491 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 1492 | } |
| 1493 | } |
Nuno Lopes | a0abe62 | 2009-01-11 23:22:37 +0000 | [diff] [blame] | 1494 | |
| 1495 | return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1499 | bool Ignore = TestAndClearIgnoreResultAssign(); |
| 1500 | |
| 1501 | // __block variables need to have the rhs evaluated first, plus this should |
| 1502 | // improve codegen just a little. |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1503 | Value *RHS = Visit(E->getRHS()); |
Mike Stump | aed08f9 | 2009-05-21 21:05:15 +0000 | [diff] [blame] | 1504 | LValue LHS = EmitLValue(E->getLHS()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1505 | |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1506 | // Store the value into the LHS. Bit-fields are handled specially |
Daniel Dunbar | 7689f6b | 2008-11-19 11:54:05 +0000 | [diff] [blame] | 1507 | // because the result is altered by the store, i.e., [C99 6.5.16p1] |
| 1508 | // 'An assignment expression has the value of the left operand after |
Eli Friedman | e381f7e | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1509 | // the assignment...'. |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1510 | if (LHS.isBitfield()) { |
| 1511 | if (!LHS.isVolatileQualified()) { |
| 1512 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(), |
| 1513 | &RHS); |
| 1514 | return RHS; |
| 1515 | } else |
| 1516 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType()); |
| 1517 | } else |
Daniel Dunbar | 9b1335e | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1518 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1519 | if (Ignore) |
| 1520 | return 0; |
| 1521 | return EmitLoadOfLValue(LHS, E->getType()); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
Chris Lattner | 671fec8 | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 1525 | const llvm::Type *ResTy = ConvertType(E->getType()); |
| 1526 | |
Chris Lattner | 8b08458 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1527 | // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. |
| 1528 | // If we have 1 && X, just emit X without inserting the control flow. |
| 1529 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { |
| 1530 | if (Cond == 1) { // If we have 1 && X, just emit X. |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1531 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Chris Lattner | 671fec8 | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 1532 | // ZExt result to int or bool. |
| 1533 | return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext"); |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1534 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | 671fec8 | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 1536 | // 0 && RHS: If it is safe, just elide the RHS, and return 0/false. |
Chris Lattner | 8b08458 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1537 | if (!CGF.ContainsLabel(E->getRHS())) |
Chris Lattner | 671fec8 | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 1538 | return llvm::Constant::getNullValue(ResTy); |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1539 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1540 | |
Daniel Dunbar | a612e79 | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1541 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); |
| 1542 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); |
Chris Lattner | 8b08458 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1543 | |
Chris Lattner | 35710d18 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1544 | // Branch on the LHS first. If it is false, go to the failure (cont) block. |
| 1545 | CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock); |
| 1546 | |
| 1547 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 1548 | // edges from this first condition. All of these values will be false. Start |
| 1549 | // setting up the PHI node in the Cont Block for this. |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1550 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), |
| 1551 | "", ContBlock); |
Chris Lattner | 35710d18 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1552 | PN->reserveOperandSpace(2); // Normal case, two inputs. |
| 1553 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 1554 | PI != PE; ++PI) |
Owen Anderson | fe4e347 | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 1555 | PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1556 | |
Anders Carlsson | f47a3de | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1557 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1558 | CGF.EmitBlock(RHSBlock); |
| 1559 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Anders Carlsson | f47a3de | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1560 | CGF.PopConditionalTempDestruction(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1561 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1562 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1563 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 35710d18 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1564 | |
| 1565 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 1566 | // into the phi node for the edge with the value of RHSCond. |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1567 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1568 | PN->addIncoming(RHSCond, RHSBlock); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1569 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1570 | // ZExt result to int. |
Chris Lattner | 671fec8 | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 1571 | return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext"); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
Chris Lattner | 671fec8 | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 1575 | const llvm::Type *ResTy = ConvertType(E->getType()); |
| 1576 | |
Chris Lattner | 8b08458 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1577 | // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. |
| 1578 | // If we have 0 || X, just emit X without inserting the control flow. |
| 1579 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { |
| 1580 | if (Cond == -1) { // If we have 0 || X, just emit X. |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1581 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Chris Lattner | 671fec8 | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 1582 | // ZExt result to int or bool. |
| 1583 | return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext"); |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1584 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1585 | |
Chris Lattner | 671fec8 | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 1586 | // 1 || RHS: If it is safe, just elide the RHS, and return 1/true. |
Chris Lattner | 8b08458 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1587 | if (!CGF.ContainsLabel(E->getRHS())) |
Chris Lattner | 671fec8 | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 1588 | return llvm::ConstantInt::get(ResTy, 1); |
Chris Lattner | 5b1964b | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1589 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1590 | |
Daniel Dunbar | a612e79 | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1591 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); |
| 1592 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1593 | |
Chris Lattner | 35710d18 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1594 | // Branch on the LHS first. If it is true, go to the success (cont) block. |
| 1595 | CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock); |
| 1596 | |
| 1597 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 1598 | // edges from this first condition. All of these values will be true. Start |
| 1599 | // setting up the PHI node in the Cont Block for this. |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1600 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), |
| 1601 | "", ContBlock); |
Chris Lattner | 35710d18 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1602 | PN->reserveOperandSpace(2); // Normal case, two inputs. |
| 1603 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 1604 | PI != PE; ++PI) |
Owen Anderson | fe4e347 | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 1605 | PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI); |
Chris Lattner | 35710d18 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1606 | |
Anders Carlsson | f47a3de | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1607 | CGF.PushConditionalTempDestruction(); |
| 1608 | |
Chris Lattner | 35710d18 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1609 | // Emit the RHS condition as a bool value. |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1610 | CGF.EmitBlock(RHSBlock); |
| 1611 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1612 | |
Anders Carlsson | f47a3de | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1613 | CGF.PopConditionalTempDestruction(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1614 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1615 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1616 | RHSBlock = Builder.GetInsertBlock(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1617 | |
Chris Lattner | 35710d18 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1618 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 1619 | // into the phi node for the edge with the value of RHSCond. |
| 1620 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1621 | PN->addIncoming(RHSCond, RHSBlock); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1622 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1623 | // ZExt result to int. |
Chris Lattner | 671fec8 | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 1624 | return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext"); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 1628 | CGF.EmitStmt(E->getLHS()); |
Daniel Dunbar | 5c7e393 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1629 | CGF.EnsureInsertPoint(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1630 | return Visit(E->getRHS()); |
| 1631 | } |
| 1632 | |
| 1633 | //===----------------------------------------------------------------------===// |
| 1634 | // Other Operators |
| 1635 | //===----------------------------------------------------------------------===// |
| 1636 | |
Chris Lattner | 3fd91f83 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1637 | /// isCheapEnoughToEvaluateUnconditionally - Return true if the specified |
| 1638 | /// expression is cheap enough and side-effect-free enough to evaluate |
| 1639 | /// unconditionally instead of conditionally. This is used to convert control |
| 1640 | /// flow into selects in some cases. |
| 1641 | static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) { |
| 1642 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 1643 | return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1644 | |
Chris Lattner | 3fd91f83 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1645 | // TODO: Allow anything we can constant fold to an integer or fp constant. |
| 1646 | if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) || |
| 1647 | isa<FloatingLiteral>(E)) |
| 1648 | return true; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1649 | |
Chris Lattner | 3fd91f83 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1650 | // Non-volatile automatic variables too, to get "cond ? X : Y" where |
| 1651 | // X and Y are local variables. |
| 1652 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 1653 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 1654 | if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified()) |
| 1655 | return true; |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1656 | |
Chris Lattner | 3fd91f83 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1657 | return false; |
| 1658 | } |
| 1659 | |
| 1660 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1661 | Value *ScalarExprEmitter:: |
| 1662 | VisitConditionalOperator(const ConditionalOperator *E) { |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1663 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1664 | // If the condition constant folds and can be elided, try to avoid emitting |
| 1665 | // the condition and the dead arm. |
| 1666 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){ |
Chris Lattner | d53e233 | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1667 | Expr *Live = E->getLHS(), *Dead = E->getRHS(); |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1668 | if (Cond == -1) |
Chris Lattner | d53e233 | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1669 | std::swap(Live, Dead); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1670 | |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1671 | // If the dead side doesn't have labels we need, and if the Live side isn't |
| 1672 | // the gnu missing ?: extension (which we could handle, but don't bother |
| 1673 | // to), just emit the Live part. |
| 1674 | if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part |
| 1675 | Live) // Live part isn't missing. |
| 1676 | return Visit(Live); |
Chris Lattner | d53e233 | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1677 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1678 | |
| 1679 | |
Chris Lattner | 3fd91f83 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1680 | // If this is a really simple expression (like x ? 4 : 5), emit this as a |
| 1681 | // select instead of as control flow. We can only do this if it is cheap and |
Chris Lattner | 9ce8a53 | 2008-11-16 06:16:27 +0000 | [diff] [blame] | 1682 | // safe to evaluate the LHS and RHS unconditionally. |
Chris Lattner | 3fd91f83 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1683 | if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) && |
| 1684 | isCheapEnoughToEvaluateUnconditionally(E->getRHS())) { |
| 1685 | llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond()); |
| 1686 | llvm::Value *LHS = Visit(E->getLHS()); |
| 1687 | llvm::Value *RHS = Visit(E->getRHS()); |
| 1688 | return Builder.CreateSelect(CondV, LHS, RHS, "cond"); |
| 1689 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1690 | |
| 1691 | |
Daniel Dunbar | d2a53a7 | 2008-11-12 10:13:37 +0000 | [diff] [blame] | 1692 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 1693 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
Daniel Dunbar | a612e79 | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1694 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Chris Lattner | 51e7118 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1695 | Value *CondVal = 0; |
Chris Lattner | cd43929 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1696 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1697 | // If we don't have the GNU missing condition extension, emit a branch on bool |
| 1698 | // the normal way. |
Chris Lattner | cd7bc14 | 2009-02-13 23:35:32 +0000 | [diff] [blame] | 1699 | if (E->getLHS()) { |
| 1700 | // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for |
| 1701 | // the branch on bool. |
| 1702 | CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); |
| 1703 | } else { |
| 1704 | // Otherwise, for the ?: extension, evaluate the conditional and then |
| 1705 | // convert it to bool the hard way. We do this explicitly because we need |
| 1706 | // the unconverted value for the missing middle value of the ?:. |
Chris Lattner | 51e7118 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1707 | CondVal = CGF.EmitScalarExpr(E->getCond()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1708 | |
Chris Lattner | cd7bc14 | 2009-02-13 23:35:32 +0000 | [diff] [blame] | 1709 | // In some cases, EmitScalarConversion will delete the "CondVal" expression |
| 1710 | // if there are no extra uses (an optimization). Inhibit this by making an |
| 1711 | // extra dead use, because we're going to add a use of CondVal later. We |
| 1712 | // don't use the builder for this, because we don't want it to get optimized |
| 1713 | // away. This leaves dead code, but the ?: extension isn't common. |
| 1714 | new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder", |
| 1715 | Builder.GetInsertBlock()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | 51e7118 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1717 | Value *CondBoolVal = |
| 1718 | CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), |
| 1719 | CGF.getContext().BoolTy); |
| 1720 | Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); |
Chris Lattner | 51e7118 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1721 | } |
Anders Carlsson | 43c52cd | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1722 | |
| 1723 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1724 | CGF.EmitBlock(LHSBlock); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1725 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1726 | // Handle the GNU extension for missing LHS. |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1727 | Value *LHS; |
| 1728 | if (E->getLHS()) |
Eli Friedman | d5a4838 | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 1729 | LHS = Visit(E->getLHS()); |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1730 | else // Perform promotions, to handle cases like "short ?: int" |
| 1731 | LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType()); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1732 | |
Anders Carlsson | 43c52cd | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1733 | CGF.PopConditionalTempDestruction(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1734 | LHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | c56e676 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 1735 | CGF.EmitBranch(ContBlock); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1736 | |
Anders Carlsson | 43c52cd | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1737 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1738 | CGF.EmitBlock(RHSBlock); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1739 | |
Eli Friedman | d5a4838 | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 1740 | Value *RHS = Visit(E->getRHS()); |
Anders Carlsson | 43c52cd | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1741 | CGF.PopConditionalTempDestruction(); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1742 | RHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | c56e676 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 1743 | CGF.EmitBranch(ContBlock); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1744 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1745 | CGF.EmitBlock(ContBlock); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1746 | |
Nuno Lopes | 7bd6e58 | 2008-06-04 19:15:45 +0000 | [diff] [blame] | 1747 | if (!LHS || !RHS) { |
Chris Lattner | b6a7b58 | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1748 | assert(E->getType()->isVoidType() && "Non-void value should have a value"); |
| 1749 | return 0; |
| 1750 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1751 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1752 | // Create a PHI node for the real part. |
| 1753 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); |
| 1754 | PN->reserveOperandSpace(2); |
| 1755 | PN->addIncoming(LHS, LHSBlock); |
| 1756 | PN->addIncoming(RHS, RHSBlock); |
| 1757 | return PN; |
| 1758 | } |
| 1759 | |
| 1760 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Eli Friedman | e0a5b8b | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 1761 | return Visit(E->getChosenSubExpr(CGF.getContext())); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Chris Lattner | b6a7b58 | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1764 | Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Eli Friedman | ddea0ad | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 1765 | llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); |
Anders Carlsson | 13abd7e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1766 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); |
| 1767 | |
| 1768 | // If EmitVAArg fails, we fall back to the LLVM instruction. |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1769 | if (!ArgPtr) |
Anders Carlsson | 13abd7e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1770 | return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); |
| 1771 | |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1772 | // FIXME Volatility. |
Anders Carlsson | 13abd7e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1773 | return Builder.CreateLoad(ArgPtr); |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Mike Stump | ab3afd8 | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 1776 | Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) { |
Mike Stump | aeb0ffd | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1777 | return CGF.BuildBlockLiteralTmp(BE); |
Mike Stump | ab3afd8 | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1780 | //===----------------------------------------------------------------------===// |
| 1781 | // Entry Point into this File |
| 1782 | //===----------------------------------------------------------------------===// |
| 1783 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1784 | /// EmitScalarExpr - Emit the computation of the specified expression of scalar |
| 1785 | /// type, ignoring the result. |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1786 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) { |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1787 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 1788 | "Invalid scalar expression to emit"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1789 | |
Mike Stump | df0fe27 | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1790 | return ScalarExprEmitter(*this, IgnoreResultAssign) |
| 1791 | .Visit(const_cast<Expr*>(E)); |
Chris Lattner | 2da04b3 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1792 | } |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1793 | |
| 1794 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 1795 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1796 | Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, |
| 1797 | QualType DstTy) { |
Chris Lattner | 3474c20 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1798 | assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && |
| 1799 | "Invalid scalar expression to emit"); |
| 1800 | return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); |
| 1801 | } |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1802 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1803 | /// EmitComplexToScalarConversion - Emit a conversion from the specified complex |
| 1804 | /// type to the specified destination type, where the destination type is an |
| 1805 | /// LLVM scalar type. |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1806 | Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, |
| 1807 | QualType SrcTy, |
| 1808 | QualType DstTy) { |
Chris Lattner | f3bc75a | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 1809 | assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) && |
Chris Lattner | 42e6b81 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1810 | "Invalid complex -> scalar conversion"); |
| 1811 | return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, |
| 1812 | DstTy); |
| 1813 | } |
Anders Carlsson | b9eb82c | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1814 | |
| 1815 | Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) { |
| 1816 | assert(V1->getType() == V2->getType() && |
| 1817 | "Vector operands must be of the same type"); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1818 | unsigned NumElements = |
Anders Carlsson | b9eb82c | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1819 | cast<llvm::VectorType>(V1->getType())->getNumElements(); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1820 | |
Anders Carlsson | b9eb82c | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1821 | va_list va; |
| 1822 | va_start(va, V2); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1823 | |
Anders Carlsson | b9eb82c | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1824 | llvm::SmallVector<llvm::Constant*, 16> Args; |
Anders Carlsson | b9eb82c | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1825 | for (unsigned i = 0; i < NumElements; i++) { |
| 1826 | int n = va_arg(va, int); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1827 | assert(n >= 0 && n < (int)NumElements * 2 && |
Anders Carlsson | b9eb82c | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1828 | "Vector shuffle index out of bounds!"); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1829 | Args.push_back(llvm::ConstantInt::get( |
| 1830 | llvm::Type::getInt32Ty(VMContext), n)); |
Anders Carlsson | b9eb82c | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1831 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1832 | |
Anders Carlsson | b9eb82c | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1833 | const char *Name = va_arg(va, const char *); |
| 1834 | va_end(va); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1835 | |
Owen Anderson | 3cc120a | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 1836 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1837 | |
Anders Carlsson | b9eb82c | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1838 | return Builder.CreateShuffleVector(V1, V2, Mask, Name); |
| 1839 | } |
| 1840 | |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1841 | llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals, |
Chris Lattner | 6284378 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 1842 | unsigned NumVals, bool isSplat) { |
Anders Carlsson | f5f6544 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1843 | llvm::Value *Vec |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1844 | = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals)); |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1845 | |
Chris Lattner | 6284378 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 1846 | for (unsigned i = 0, e = NumVals; i != e; ++i) { |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1847 | llvm::Value *Val = isSplat ? Vals[0] : Vals[i]; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1848 | llvm::Value *Idx = llvm::ConstantInt::get( |
| 1849 | llvm::Type::getInt32Ty(VMContext), i); |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1850 | Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp"); |
Anders Carlsson | f5f6544 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1851 | } |
Mike Stump | 4a3999f | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1852 | |
| 1853 | return Vec; |
Anders Carlsson | f5f6544 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1854 | } |