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