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