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