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