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 | |
Devang Patel | 78ba3d4 | 2010-10-04 21:46:04 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CodeGenOptions.h" |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
Fariborz Jahanian | f7bcc7e | 2009-10-10 20:07:56 +0000 | [diff] [blame] | 17 | #include "CGObjCRuntime.h" |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 18 | #include "CodeGenModule.h" |
Devang Patel | 78ba3d4 | 2010-10-04 21:46:04 +0000 | [diff] [blame] | 19 | #include "CGDebugInfo.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 98c5ead | 2008-08-12 05:08:18 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 25ddea7 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
| 26 | #include "llvm/Function.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 27 | #include "llvm/GlobalVariable.h" |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 28 | #include "llvm/Intrinsics.h" |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CFG.h" |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetData.h" |
Chris Lattner | c89bf69 | 2008-01-03 07:05:49 +0000 | [diff] [blame] | 32 | #include <cstdarg> |
Ted Kremenek | 6aad91a | 2007-12-10 23:44:32 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | using namespace CodeGen; |
| 36 | using llvm::Value; |
| 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // Scalar Expression Emitter |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
Benjamin Kramer | 79ba2a6 | 2010-10-22 16:48:22 +0000 | [diff] [blame] | 42 | namespace { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 43 | struct BinOpInfo { |
| 44 | Value *LHS; |
| 45 | Value *RHS; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 46 | QualType Ty; // Computation Type. |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 47 | BinaryOperator::Opcode Opcode; // Opcode of BinOp to perform |
| 48 | const Expr *E; // Entire expr, for error unsupported. May not be binop. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 51 | static bool MustVisitNullValue(const Expr *E) { |
| 52 | // If a null pointer expression's type is the C++0x nullptr_t, then |
| 53 | // it's not necessarily a simple constant and it must be evaluated |
| 54 | // for its potential side effects. |
| 55 | return E->getType()->isNullPtrType(); |
| 56 | } |
| 57 | |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 58 | class ScalarExprEmitter |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 59 | : public StmtVisitor<ScalarExprEmitter, Value*> { |
| 60 | CodeGenFunction &CGF; |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 61 | CGBuilderTy &Builder; |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 62 | bool IgnoreResultAssign; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 63 | llvm::LLVMContext &VMContext; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 64 | public: |
| 65 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 66 | ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false) |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 67 | : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 68 | VMContext(cgf.getLLVMContext()) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 69 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 71 | //===--------------------------------------------------------------------===// |
| 72 | // Utilities |
| 73 | //===--------------------------------------------------------------------===// |
| 74 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 75 | bool TestAndClearIgnoreResultAssign() { |
Chris Lattner | 9c10fcf | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 76 | bool I = IgnoreResultAssign; |
| 77 | IgnoreResultAssign = false; |
| 78 | return I; |
| 79 | } |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 81 | llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 82 | LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 83 | LValue EmitCheckedLValue(const Expr *E) { return CGF.EmitCheckedLValue(E); } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 84 | |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 85 | Value *EmitLoadOfLValue(LValue LV) { |
| 86 | return CGF.EmitLoadOfLValue(LV).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 87 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 89 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 90 | /// value l-value, this method emits the address of the l-value, then loads |
| 91 | /// and returns the result. |
| 92 | Value *EmitLoadOfLValue(const Expr *E) { |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 93 | return EmitLoadOfLValue(EmitCheckedLValue(E)); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 94 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 96 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 3420d0d | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 97 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 98 | Value *EmitConversionToBool(Value *Src, QualType DstTy); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 100 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 101 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 102 | Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy); |
| 103 | |
| 104 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 105 | /// complex type to the specified destination type, where the destination type |
| 106 | /// is an LLVM scalar type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 107 | Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 108 | QualType SrcTy, QualType DstTy); |
Mike Stump | df6b68c | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 109 | |
Anders Carlsson | a40a9f3 | 2010-05-22 17:45:10 +0000 | [diff] [blame] | 110 | /// EmitNullValue - Emit a value that corresponds to null for the given type. |
| 111 | Value *EmitNullValue(QualType Ty); |
| 112 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 113 | /// EmitFloatToBoolConversion - Perform an FP to boolean conversion. |
| 114 | Value *EmitFloatToBoolConversion(Value *V) { |
| 115 | // Compare against 0.0 for fp scalars. |
| 116 | llvm::Value *Zero = llvm::Constant::getNullValue(V->getType()); |
| 117 | return Builder.CreateFCmpUNE(V, Zero, "tobool"); |
| 118 | } |
| 119 | |
| 120 | /// EmitPointerToBoolConversion - Perform a pointer to boolean conversion. |
| 121 | Value *EmitPointerToBoolConversion(Value *V) { |
| 122 | Value *Zero = llvm::ConstantPointerNull::get( |
| 123 | cast<llvm::PointerType>(V->getType())); |
| 124 | return Builder.CreateICmpNE(V, Zero, "tobool"); |
| 125 | } |
| 126 | |
| 127 | Value *EmitIntToBoolConversion(Value *V) { |
| 128 | // Because of the type rules of C, we often end up computing a |
| 129 | // logical value, then zero extending it to int, then wanting it |
| 130 | // as a logical value again. Optimize this common case. |
| 131 | if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(V)) { |
| 132 | if (ZI->getOperand(0)->getType() == Builder.getInt1Ty()) { |
| 133 | Value *Result = ZI->getOperand(0); |
| 134 | // If there aren't any more uses, zap the instruction to save space. |
| 135 | // Note that there can be more uses, for example if this |
| 136 | // is the result of an assignment. |
| 137 | if (ZI->use_empty()) |
| 138 | ZI->eraseFromParent(); |
| 139 | return Result; |
| 140 | } |
| 141 | } |
| 142 | |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 143 | return Builder.CreateIsNotNull(V, "tobool"); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 146 | //===--------------------------------------------------------------------===// |
| 147 | // Visitor Methods |
| 148 | //===--------------------------------------------------------------------===// |
| 149 | |
Fariborz Jahanian | af9b968 | 2010-09-17 15:51:28 +0000 | [diff] [blame] | 150 | Value *Visit(Expr *E) { |
Fariborz Jahanian | af9b968 | 2010-09-17 15:51:28 +0000 | [diff] [blame] | 151 | return StmtVisitor<ScalarExprEmitter, Value*>::Visit(E); |
| 152 | } |
| 153 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 154 | Value *VisitStmt(Stmt *S) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 155 | S->dump(CGF.getContext().getSourceManager()); |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 156 | llvm_unreachable("Stmt can't have complex result type!"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 157 | } |
| 158 | Value *VisitExpr(Expr *S); |
Fariborz Jahanian | f51dc64 | 2009-10-21 23:45:42 +0000 | [diff] [blame] | 159 | |
Fariborz Jahanian | af9b968 | 2010-09-17 15:51:28 +0000 | [diff] [blame] | 160 | Value *VisitParenExpr(ParenExpr *PE) { |
| 161 | return Visit(PE->getSubExpr()); |
| 162 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 163 | Value *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) { |
| 164 | return Visit(E->getReplacement()); |
| 165 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 166 | Value *VisitGenericSelectionExpr(GenericSelectionExpr *GE) { |
| 167 | return Visit(GE->getResultExpr()); |
| 168 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 169 | |
| 170 | // Leaves. |
| 171 | Value *VisitIntegerLiteral(const IntegerLiteral *E) { |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 172 | return Builder.getInt(E->getValue()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 173 | } |
| 174 | Value *VisitFloatingLiteral(const FloatingLiteral *E) { |
Owen Anderson | bc0a222 | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 175 | return llvm::ConstantFP::get(VMContext, E->getValue()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 176 | } |
| 177 | Value *VisitCharacterLiteral(const CharacterLiteral *E) { |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 178 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 179 | } |
Nate Begeman | e7579b5 | 2007-11-15 05:40:03 +0000 | [diff] [blame] | 180 | Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 181 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
Nate Begeman | e7579b5 | 2007-11-15 05:40:03 +0000 | [diff] [blame] | 182 | } |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 183 | Value *VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Anders Carlsson | a40a9f3 | 2010-05-22 17:45:10 +0000 | [diff] [blame] | 184 | return EmitNullValue(E->getType()); |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 185 | } |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 186 | Value *VisitGNUNullExpr(const GNUNullExpr *E) { |
Anders Carlsson | a40a9f3 | 2010-05-22 17:45:10 +0000 | [diff] [blame] | 187 | return EmitNullValue(E->getType()); |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 188 | } |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 189 | Value *VisitOffsetOfExpr(OffsetOfExpr *E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 190 | Value *VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 191 | Value *VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 192 | llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel()); |
| 193 | return Builder.CreateBitCast(V, ConvertType(E->getType())); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 194 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | 9370c8f | 2011-01-12 22:11:34 +0000 | [diff] [blame] | 196 | Value *VisitSizeOfPackExpr(SizeOfPackExpr *E) { |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 197 | return llvm::ConstantInt::get(ConvertType(E->getType()),E->getPackLength()); |
Douglas Gregor | 9370c8f | 2011-01-12 22:11:34 +0000 | [diff] [blame] | 198 | } |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 199 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 200 | Value *VisitPseudoObjectExpr(PseudoObjectExpr *E) { |
| 201 | return CGF.EmitPseudoObjectRValue(E).getScalarVal(); |
| 202 | } |
| 203 | |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 204 | Value *VisitOpaqueValueExpr(OpaqueValueExpr *E) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 205 | if (E->isGLValue()) |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 206 | return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E)); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 207 | |
| 208 | // Otherwise, assume the mapping is the scalar directly. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 209 | return CGF.getOpaqueRValueMapping(E).getScalarVal(); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 210 | } |
Douglas Gregor | 9370c8f | 2011-01-12 22:11:34 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 212 | // l-values. |
| 213 | Value *VisitDeclRefExpr(DeclRefExpr *E) { |
Eli Friedman | 2866527 | 2009-11-26 03:22:21 +0000 | [diff] [blame] | 214 | Expr::EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 215 | if (!E->EvaluateAsRValue(Result, CGF.getContext())) |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 216 | return EmitLoadOfLValue(E); |
| 217 | |
| 218 | assert(!Result.HasSideEffects && "Constant declref with side-effect?!"); |
| 219 | |
| 220 | llvm::Constant *C; |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 221 | if (Result.Val.isInt()) |
| 222 | C = Builder.getInt(Result.Val.getInt()); |
| 223 | else if (Result.Val.isFloat()) |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 224 | C = llvm::ConstantFP::get(VMContext, Result.Val.getFloat()); |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 225 | else |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 226 | return EmitLoadOfLValue(E); |
John McCall | 189d6ef | 2010-10-09 01:34:31 +0000 | [diff] [blame] | 227 | |
| 228 | // Make sure we emit a debug reference to the global variable. |
| 229 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { |
| 230 | if (!CGF.getContext().DeclMustBeEmitted(VD)) |
| 231 | CGF.EmitDeclRefExprDbgValue(E, C); |
| 232 | } else if (isa<EnumConstantDecl>(E->getDecl())) { |
| 233 | CGF.EmitDeclRefExprDbgValue(E, C); |
| 234 | } |
| 235 | |
| 236 | return C; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 237 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 238 | Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) { |
| 239 | return CGF.EmitObjCSelectorExpr(E); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 240 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 241 | Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) { |
| 242 | return CGF.EmitObjCProtocolExpr(E); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 243 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 244 | Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 245 | return EmitLoadOfLValue(E); |
| 246 | } |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 247 | Value *VisitObjCMessageExpr(ObjCMessageExpr *E) { |
Fariborz Jahanian | 180ff3a | 2011-03-02 20:09:49 +0000 | [diff] [blame] | 248 | if (E->getMethodDecl() && |
| 249 | E->getMethodDecl()->getResultType()->isReferenceType()) |
| 250 | return EmitLoadOfLValue(E); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 251 | return CGF.EmitObjCMessageExpr(E).getScalarVal(); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 254 | Value *VisitObjCIsaExpr(ObjCIsaExpr *E) { |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 255 | LValue LV = CGF.EmitObjCIsaExpr(E); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 256 | Value *V = CGF.EmitLoadOfLValue(LV).getScalarVal(); |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 257 | return V; |
| 258 | } |
| 259 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 260 | Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 261 | Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E); |
Eli Friedman | 2866527 | 2009-11-26 03:22:21 +0000 | [diff] [blame] | 262 | Value *VisitMemberExpr(MemberExpr *E); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 263 | Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | be20bb5 | 2008-10-26 23:53:12 +0000 | [diff] [blame] | 264 | Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 265 | return EmitLoadOfLValue(E); |
| 266 | } |
Devang Patel | 35634f5 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 267 | |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 268 | Value *VisitInitListExpr(InitListExpr *E); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 269 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 270 | Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Anders Carlsson | 3cb18bc | 2010-05-14 15:05:19 +0000 | [diff] [blame] | 271 | return CGF.CGM.EmitNullConstant(E->getType()); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 272 | } |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 273 | Value *VisitExplicitCastExpr(ExplicitCastExpr *E) { |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 274 | if (E->getType()->isVariablyModifiedType()) |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 275 | CGF.EmitVariablyModifiedType(E->getType()); |
| 276 | return VisitCastExpr(E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 277 | } |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 278 | Value *VisitCastExpr(CastExpr *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 279 | |
| 280 | Value *VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | e9f2f45 | 2009-05-27 03:37:57 +0000 | [diff] [blame] | 281 | if (E->getCallReturnType()->isReferenceType()) |
| 282 | return EmitLoadOfLValue(E); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 284 | return CGF.EmitCallExpr(E).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 285 | } |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 287 | Value *VisitStmtExpr(const StmtExpr *E); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 288 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 289 | Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 290 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 291 | // Unary Operators. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 292 | Value *VisitUnaryPostDec(const UnaryOperator *E) { |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 293 | LValue LV = EmitLValue(E->getSubExpr()); |
| 294 | return EmitScalarPrePostIncDec(E, LV, false, false); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 295 | } |
| 296 | Value *VisitUnaryPostInc(const UnaryOperator *E) { |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 297 | LValue LV = EmitLValue(E->getSubExpr()); |
| 298 | return EmitScalarPrePostIncDec(E, LV, true, false); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 299 | } |
| 300 | Value *VisitUnaryPreDec(const UnaryOperator *E) { |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 301 | LValue LV = EmitLValue(E->getSubExpr()); |
| 302 | return EmitScalarPrePostIncDec(E, LV, false, true); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 303 | } |
| 304 | Value *VisitUnaryPreInc(const UnaryOperator *E) { |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 305 | LValue LV = EmitLValue(E->getSubExpr()); |
| 306 | return EmitScalarPrePostIncDec(E, LV, true, true); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 307 | } |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 308 | |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 309 | llvm::Value *EmitAddConsiderOverflowBehavior(const UnaryOperator *E, |
| 310 | llvm::Value *InVal, |
| 311 | llvm::Value *NextVal, |
| 312 | bool IsInc); |
| 313 | |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 314 | llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 315 | bool isInc, bool isPre); |
| 316 | |
| 317 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 318 | Value *VisitUnaryAddrOf(const UnaryOperator *E) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 319 | if (isa<MemberPointerType>(E->getType())) // never sugared |
| 320 | return CGF.CGM.getMemberPointerConstant(E); |
| 321 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 322 | return EmitLValue(E->getSubExpr()).getAddress(); |
| 323 | } |
John McCall | fd56900 | 2010-12-04 12:43:24 +0000 | [diff] [blame] | 324 | Value *VisitUnaryDeref(const UnaryOperator *E) { |
| 325 | if (E->getType()->isVoidType()) |
| 326 | return Visit(E->getSubExpr()); // the actual value should be unused |
| 327 | return EmitLoadOfLValue(E); |
| 328 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 329 | Value *VisitUnaryPlus(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 330 | // This differs from gcc, though, most likely due to a bug in gcc. |
| 331 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 332 | return Visit(E->getSubExpr()); |
| 333 | } |
| 334 | Value *VisitUnaryMinus (const UnaryOperator *E); |
| 335 | Value *VisitUnaryNot (const UnaryOperator *E); |
| 336 | Value *VisitUnaryLNot (const UnaryOperator *E); |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 337 | Value *VisitUnaryReal (const UnaryOperator *E); |
| 338 | Value *VisitUnaryImag (const UnaryOperator *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 339 | Value *VisitUnaryExtension(const UnaryOperator *E) { |
| 340 | return Visit(E->getSubExpr()); |
| 341 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 342 | |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 343 | // C++ |
Douglas Gregor | 3f86ce1 | 2011-08-09 00:37:14 +0000 | [diff] [blame] | 344 | Value *VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E) { |
Eli Friedman | ec24b0e | 2011-08-14 04:50:34 +0000 | [diff] [blame] | 345 | return EmitLoadOfLValue(E); |
Douglas Gregor | 3f86ce1 | 2011-08-09 00:37:14 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 348 | Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 349 | return Visit(DAE->getExpr()); |
| 350 | } |
Anders Carlsson | 5f4307b | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 351 | Value *VisitCXXThisExpr(CXXThisExpr *TE) { |
| 352 | return CGF.LoadCXXThis(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 353 | } |
| 354 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 355 | Value *VisitExprWithCleanups(ExprWithCleanups *E) { |
John McCall | 1a343eb | 2011-11-10 08:15:53 +0000 | [diff] [blame] | 356 | CGF.enterFullExpression(E); |
| 357 | CodeGenFunction::RunCleanupsScope Scope(CGF); |
| 358 | return Visit(E->getSubExpr()); |
Anders Carlsson | 7f6ad15 | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 359 | } |
Anders Carlsson | a00703d | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 360 | Value *VisitCXXNewExpr(const CXXNewExpr *E) { |
| 361 | return CGF.EmitCXXNewExpr(E); |
| 362 | } |
Anders Carlsson | 60e282c | 2009-08-16 21:13:42 +0000 | [diff] [blame] | 363 | Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
| 364 | CGF.EmitCXXDeleteExpr(E); |
| 365 | return 0; |
| 366 | } |
Eli Friedman | 9dfebdc | 2009-12-10 22:40:32 +0000 | [diff] [blame] | 367 | Value *VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 368 | return Builder.getInt1(E->getValue()); |
Eli Friedman | 9dfebdc | 2009-12-10 22:40:32 +0000 | [diff] [blame] | 369 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 370 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 371 | Value *VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 372 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 373 | } |
| 374 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 375 | Value *VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 376 | return llvm::ConstantInt::get(Builder.getInt32Ty(), E->getValue()); |
| 377 | } |
| 378 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 379 | Value *VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 380 | return llvm::ConstantInt::get(Builder.getInt1Ty(), E->getValue()); |
| 381 | } |
| 382 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 383 | Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) { |
| 384 | // C++ [expr.pseudo]p1: |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 385 | // 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] | 386 | // operator (), and the result of such a call has type void. The only |
| 387 | // effect is the evaluation of the postfix-expression before the dot or |
| 388 | // arrow. |
| 389 | CGF.EmitScalarExpr(E->getBase()); |
| 390 | return 0; |
| 391 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 392 | |
Anders Carlsson | c1eb14a | 2009-09-15 04:39:46 +0000 | [diff] [blame] | 393 | Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Anders Carlsson | a40a9f3 | 2010-05-22 17:45:10 +0000 | [diff] [blame] | 394 | return EmitNullValue(E->getType()); |
Anders Carlsson | c1eb14a | 2009-09-15 04:39:46 +0000 | [diff] [blame] | 395 | } |
Anders Carlsson | 756b5c4 | 2009-10-30 01:42:31 +0000 | [diff] [blame] | 396 | |
| 397 | Value *VisitCXXThrowExpr(const CXXThrowExpr *E) { |
| 398 | CGF.EmitCXXThrowExpr(E); |
| 399 | return 0; |
| 400 | } |
| 401 | |
Sebastian Redl | 98294de | 2010-09-10 21:04:00 +0000 | [diff] [blame] | 402 | Value *VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 403 | return Builder.getInt1(E->getValue()); |
Sebastian Redl | 98294de | 2010-09-10 21:04:00 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 406 | // Binary Operators. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 407 | Value *EmitMul(const BinOpInfo &Ops) { |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 408 | if (Ops.Ty->isSignedIntegerOrEnumerationType()) { |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 409 | switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { |
| 410 | case LangOptions::SOB_Undefined: |
| 411 | return Builder.CreateNSWMul(Ops.LHS, Ops.RHS, "mul"); |
| 412 | case LangOptions::SOB_Defined: |
| 413 | return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); |
| 414 | case LangOptions::SOB_Trapping: |
| 415 | return EmitOverflowCheckedBinOp(Ops); |
| 416 | } |
| 417 | } |
| 418 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 419 | if (Ops.LHS->getType()->isFPOrFPVectorTy()) |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 420 | return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 421 | return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); |
| 422 | } |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 423 | bool isTrapvOverflowBehavior() { |
| 424 | return CGF.getContext().getLangOptions().getSignedOverflowBehavior() |
| 425 | == LangOptions::SOB_Trapping; |
| 426 | } |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 427 | /// Create a binary op that checks for overflow. |
| 428 | /// Currently only supports +, - and *. |
| 429 | Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops); |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 430 | // Emit the overflow BB when -ftrapv option is activated. |
| 431 | void EmitOverflowBB(llvm::BasicBlock *overflowBB) { |
| 432 | Builder.SetInsertPoint(overflowBB); |
| 433 | llvm::Function *Trap = CGF.CGM.getIntrinsic(llvm::Intrinsic::trap); |
| 434 | Builder.CreateCall(Trap); |
| 435 | Builder.CreateUnreachable(); |
| 436 | } |
| 437 | // Check for undefined division and modulus behaviors. |
| 438 | void EmitUndefinedBehaviorIntegerDivAndRemCheck(const BinOpInfo &Ops, |
| 439 | llvm::Value *Zero,bool isDiv); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 440 | Value *EmitDiv(const BinOpInfo &Ops); |
| 441 | Value *EmitRem(const BinOpInfo &Ops); |
| 442 | Value *EmitAdd(const BinOpInfo &Ops); |
| 443 | Value *EmitSub(const BinOpInfo &Ops); |
| 444 | Value *EmitShl(const BinOpInfo &Ops); |
| 445 | Value *EmitShr(const BinOpInfo &Ops); |
| 446 | Value *EmitAnd(const BinOpInfo &Ops) { |
| 447 | return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); |
| 448 | } |
| 449 | Value *EmitXor(const BinOpInfo &Ops) { |
| 450 | return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); |
| 451 | } |
| 452 | Value *EmitOr (const BinOpInfo &Ops) { |
| 453 | return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); |
| 454 | } |
| 455 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 456 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 457 | LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E, |
| 458 | Value *(ScalarExprEmitter::*F)(const BinOpInfo &), |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 459 | Value *&Result); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 460 | |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 461 | Value *EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 462 | Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); |
| 463 | |
| 464 | // Binary operators and binary compound assignment operators. |
| 465 | #define HANDLEBINOP(OP) \ |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 466 | Value *VisitBin ## OP(const BinaryOperator *E) { \ |
| 467 | return Emit ## OP(EmitBinOps(E)); \ |
| 468 | } \ |
| 469 | Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ |
| 470 | return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 471 | } |
Daniel Dunbar | 7177dee | 2009-12-19 17:50:07 +0000 | [diff] [blame] | 472 | HANDLEBINOP(Mul) |
| 473 | HANDLEBINOP(Div) |
| 474 | HANDLEBINOP(Rem) |
| 475 | HANDLEBINOP(Add) |
| 476 | HANDLEBINOP(Sub) |
| 477 | HANDLEBINOP(Shl) |
| 478 | HANDLEBINOP(Shr) |
| 479 | HANDLEBINOP(And) |
| 480 | HANDLEBINOP(Xor) |
| 481 | HANDLEBINOP(Or) |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 482 | #undef HANDLEBINOP |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 484 | // Comparisons. |
| 485 | Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, |
| 486 | unsigned SICmpOpc, unsigned FCmpOpc); |
| 487 | #define VISITCOMP(CODE, UI, SI, FP) \ |
| 488 | Value *VisitBin##CODE(const BinaryOperator *E) { \ |
| 489 | return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ |
| 490 | llvm::FCmpInst::FP); } |
Daniel Dunbar | 7177dee | 2009-12-19 17:50:07 +0000 | [diff] [blame] | 491 | VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT) |
| 492 | VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT) |
| 493 | VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE) |
| 494 | VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE) |
| 495 | VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ) |
| 496 | VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 497 | #undef VISITCOMP |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 499 | Value *VisitBinAssign (const BinaryOperator *E); |
| 500 | |
| 501 | Value *VisitBinLAnd (const BinaryOperator *E); |
| 502 | Value *VisitBinLOr (const BinaryOperator *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 503 | Value *VisitBinComma (const BinaryOperator *E); |
| 504 | |
Eli Friedman | 25b825d | 2009-11-18 09:41:26 +0000 | [diff] [blame] | 505 | Value *VisitBinPtrMemD(const Expr *E) { return EmitLoadOfLValue(E); } |
| 506 | Value *VisitBinPtrMemI(const Expr *E) { return EmitLoadOfLValue(E); } |
| 507 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 508 | // Other Operators. |
Mike Stump | df6b68c | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 509 | Value *VisitBlockExpr(const BlockExpr *BE); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 510 | Value *VisitAbstractConditionalOperator(const AbstractConditionalOperator *); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 511 | Value *VisitChooseExpr(ChooseExpr *CE); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 512 | Value *VisitVAArgExpr(VAArgExpr *VE); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 513 | Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 514 | return CGF.EmitObjCStringLiteral(E); |
| 515 | } |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 516 | Value *VisitAsTypeExpr(AsTypeExpr *CE); |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 517 | Value *VisitAtomicExpr(AtomicExpr *AE); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 518 | }; |
| 519 | } // end anonymous namespace. |
| 520 | |
| 521 | //===----------------------------------------------------------------------===// |
| 522 | // Utilities |
| 523 | //===----------------------------------------------------------------------===// |
| 524 | |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 525 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 3420d0d | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 526 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 527 | Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 528 | assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 529 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 530 | if (SrcType->isRealFloatingType()) |
| 531 | return EmitFloatToBoolConversion(Src); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 532 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 533 | if (const MemberPointerType *MPT = dyn_cast<MemberPointerType>(SrcType)) |
| 534 | return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, Src, MPT); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 535 | |
Daniel Dunbar | d1d66bc | 2008-08-25 10:38:11 +0000 | [diff] [blame] | 536 | assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) && |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 537 | "Unknown scalar type to convert"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 538 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 539 | if (isa<llvm::IntegerType>(Src->getType())) |
| 540 | return EmitIntToBoolConversion(Src); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 541 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 542 | assert(isa<llvm::PointerType>(Src->getType())); |
| 543 | return EmitPointerToBoolConversion(Src); |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 546 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 547 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 548 | Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, |
| 549 | QualType DstType) { |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 550 | SrcType = CGF.getContext().getCanonicalType(SrcType); |
| 551 | DstType = CGF.getContext().getCanonicalType(DstType); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 552 | if (SrcType == DstType) return Src; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 553 | |
Chris Lattner | cf28908 | 2007-08-26 07:21:11 +0000 | [diff] [blame] | 554 | if (DstType->isVoidType()) return 0; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 555 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 556 | llvm::Type *SrcTy = Src->getType(); |
| 557 | |
| 558 | // Floating casts might be a bit special: if we're doing casts to / from half |
| 559 | // FP, we should go via special intrinsics. |
| 560 | if (SrcType->isHalfType()) { |
| 561 | Src = Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16), Src); |
| 562 | SrcType = CGF.getContext().FloatTy; |
| 563 | SrcTy = llvm::Type::getFloatTy(VMContext); |
| 564 | } |
| 565 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 566 | // Handle conversions to bool first, they are special: comparisons against 0. |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 567 | if (DstType->isBooleanType()) |
| 568 | return EmitConversionToBool(Src, SrcType); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 569 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 570 | llvm::Type *DstTy = ConvertType(DstType); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 571 | |
| 572 | // Ignore conversions like int -> uint. |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 573 | if (SrcTy == DstTy) |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 574 | return Src; |
| 575 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 576 | // Handle pointer conversions next: pointers can only be converted to/from |
| 577 | // other pointers and integers. Check for pointer types in terms of LLVM, as |
| 578 | // 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] | 579 | if (isa<llvm::PointerType>(DstTy)) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 580 | // The source value may be an integer, or a pointer. |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 581 | if (isa<llvm::PointerType>(SrcTy)) |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 582 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
Anders Carlsson | 191dfe9 | 2009-09-12 04:57:16 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 584 | assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
Eli Friedman | 2561542 | 2009-03-04 04:02:35 +0000 | [diff] [blame] | 585 | // First, convert to the correct width so that we control the kind of |
| 586 | // extension. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 587 | llvm::Type *MiddleTy = CGF.IntPtrTy; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 588 | bool InputSigned = SrcType->isSignedIntegerOrEnumerationType(); |
Eli Friedman | 2561542 | 2009-03-04 04:02:35 +0000 | [diff] [blame] | 589 | llvm::Value* IntResult = |
| 590 | Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); |
| 591 | // Then, cast to pointer. |
| 592 | return Builder.CreateIntToPtr(IntResult, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 593 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 594 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 595 | if (isa<llvm::PointerType>(SrcTy)) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 596 | // Must be an ptr to int cast. |
| 597 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
Anders Carlsson | 50b5a30 | 2007-10-31 23:18:02 +0000 | [diff] [blame] | 598 | return Builder.CreatePtrToInt(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 599 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 600 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 601 | // 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] | 602 | if (DstType->isExtVectorType() && !SrcType->isVectorType()) { |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 603 | // Cast the scalar to element type |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 604 | QualType EltTy = DstType->getAs<ExtVectorType>()->getElementType(); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 605 | llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy); |
| 606 | |
| 607 | // Insert the element in element zero of an undef vector |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 608 | llvm::Value *UnV = llvm::UndefValue::get(DstTy); |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 609 | llvm::Value *Idx = Builder.getInt32(0); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 610 | UnV = Builder.CreateInsertElement(UnV, Elt, Idx); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 611 | |
| 612 | // Splat the element across to all elements |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 613 | SmallVector<llvm::Constant*, 16> Args; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 614 | unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 615 | for (unsigned i = 0; i != NumElements; ++i) |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 616 | Args.push_back(Builder.getInt32(0)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 617 | |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 618 | llvm::Constant *Mask = llvm::ConstantVector::get(Args); |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 619 | llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); |
| 620 | return Yay; |
| 621 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 622 | |
Chris Lattner | 3b1ae00 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 623 | // Allow bitcast from vector to integer/fp of the same size. |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 624 | if (isa<llvm::VectorType>(SrcTy) || |
Chris Lattner | 3b1ae00 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 625 | isa<llvm::VectorType>(DstTy)) |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 626 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 628 | // Finally, we have the arithmetic types: real int/float. |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 629 | Value *Res = NULL; |
| 630 | llvm::Type *ResTy = DstTy; |
| 631 | |
| 632 | // Cast to half via float |
| 633 | if (DstType->isHalfType()) |
| 634 | DstTy = llvm::Type::getFloatTy(VMContext); |
| 635 | |
| 636 | if (isa<llvm::IntegerType>(SrcTy)) { |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 637 | bool InputSigned = SrcType->isSignedIntegerOrEnumerationType(); |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 638 | if (isa<llvm::IntegerType>(DstTy)) |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 639 | Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 640 | else if (InputSigned) |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 641 | Res = Builder.CreateSIToFP(Src, DstTy, "conv"); |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 642 | else |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 643 | Res = Builder.CreateUIToFP(Src, DstTy, "conv"); |
| 644 | } else if (isa<llvm::IntegerType>(DstTy)) { |
| 645 | assert(SrcTy->isFloatingPointTy() && "Unknown real conversion"); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 646 | if (DstType->isSignedIntegerOrEnumerationType()) |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 647 | Res = Builder.CreateFPToSI(Src, DstTy, "conv"); |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 648 | else |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 649 | Res = Builder.CreateFPToUI(Src, DstTy, "conv"); |
| 650 | } else { |
| 651 | assert(SrcTy->isFloatingPointTy() && DstTy->isFloatingPointTy() && |
| 652 | "Unknown real conversion"); |
| 653 | if (DstTy->getTypeID() < SrcTy->getTypeID()) |
| 654 | Res = Builder.CreateFPTrunc(Src, DstTy, "conv"); |
| 655 | else |
| 656 | Res = Builder.CreateFPExt(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 659 | if (DstTy != ResTy) { |
| 660 | assert(ResTy->isIntegerTy(16) && "Only half FP requires extra conversion"); |
| 661 | Res = Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16), Res); |
| 662 | } |
| 663 | |
| 664 | return Res; |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 667 | /// EmitComplexToScalarConversion - Emit a conversion from the specified complex |
| 668 | /// type to the specified destination type, where the destination type is an |
| 669 | /// LLVM scalar type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 670 | Value *ScalarExprEmitter:: |
| 671 | EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 672 | QualType SrcTy, QualType DstTy) { |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 673 | // Get the source element type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 674 | SrcTy = SrcTy->getAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 675 | |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 676 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 677 | if (DstTy->isBooleanType()) { |
| 678 | // Complex != 0 -> (Real != 0) | (Imag != 0) |
| 679 | Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 680 | Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); |
| 681 | return Builder.CreateOr(Src.first, Src.second, "tobool"); |
| 682 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 683 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 684 | // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, |
| 685 | // the imaginary part of the complex value is discarded and the value of the |
| 686 | // real part is converted according to the conversion rules for the |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 687 | // corresponding real type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 688 | return EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 689 | } |
| 690 | |
Anders Carlsson | a40a9f3 | 2010-05-22 17:45:10 +0000 | [diff] [blame] | 691 | Value *ScalarExprEmitter::EmitNullValue(QualType Ty) { |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 692 | if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) |
| 693 | return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT); |
| 694 | |
| 695 | return llvm::Constant::getNullValue(ConvertType(Ty)); |
Anders Carlsson | a40a9f3 | 2010-05-22 17:45:10 +0000 | [diff] [blame] | 696 | } |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 697 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 698 | //===----------------------------------------------------------------------===// |
| 699 | // Visitor Methods |
| 700 | //===----------------------------------------------------------------------===// |
| 701 | |
| 702 | Value *ScalarExprEmitter::VisitExpr(Expr *E) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 703 | CGF.ErrorUnsupported(E, "scalar expression"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 704 | if (E->getType()->isVoidType()) |
| 705 | return 0; |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 706 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 709 | Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 710 | // Vector Mask Case |
| 711 | if (E->getNumSubExprs() == 2 || |
Rafael Espindola | 3f4cb12 | 2010-06-09 02:17:08 +0000 | [diff] [blame] | 712 | (E->getNumSubExprs() == 3 && E->getExpr(2)->getType()->isVectorType())) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 713 | Value *LHS = CGF.EmitScalarExpr(E->getExpr(0)); |
| 714 | Value *RHS = CGF.EmitScalarExpr(E->getExpr(1)); |
| 715 | Value *Mask; |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 717 | llvm::VectorType *LTy = cast<llvm::VectorType>(LHS->getType()); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 718 | unsigned LHSElts = LTy->getNumElements(); |
| 719 | |
| 720 | if (E->getNumSubExprs() == 3) { |
| 721 | Mask = CGF.EmitScalarExpr(E->getExpr(2)); |
| 722 | |
| 723 | // Shuffle LHS & RHS into one input vector. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 724 | SmallVector<llvm::Constant*, 32> concat; |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 725 | for (unsigned i = 0; i != LHSElts; ++i) { |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 726 | concat.push_back(Builder.getInt32(2*i)); |
| 727 | concat.push_back(Builder.getInt32(2*i+1)); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 730 | Value* CV = llvm::ConstantVector::get(concat); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 731 | LHS = Builder.CreateShuffleVector(LHS, RHS, CV, "concat"); |
| 732 | LHSElts *= 2; |
| 733 | } else { |
| 734 | Mask = RHS; |
| 735 | } |
| 736 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 737 | llvm::VectorType *MTy = cast<llvm::VectorType>(Mask->getType()); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 738 | llvm::Constant* EltMask; |
| 739 | |
| 740 | // Treat vec3 like vec4. |
| 741 | if ((LHSElts == 6) && (E->getNumSubExprs() == 3)) |
| 742 | EltMask = llvm::ConstantInt::get(MTy->getElementType(), |
| 743 | (1 << llvm::Log2_32(LHSElts+2))-1); |
| 744 | else if ((LHSElts == 3) && (E->getNumSubExprs() == 2)) |
| 745 | EltMask = llvm::ConstantInt::get(MTy->getElementType(), |
| 746 | (1 << llvm::Log2_32(LHSElts+1))-1); |
| 747 | else |
| 748 | EltMask = llvm::ConstantInt::get(MTy->getElementType(), |
| 749 | (1 << llvm::Log2_32(LHSElts))-1); |
| 750 | |
| 751 | // Mask off the high bits of each shuffle index. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 752 | SmallVector<llvm::Constant *, 32> MaskV; |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 753 | for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) |
| 754 | MaskV.push_back(EltMask); |
| 755 | |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 756 | Value* MaskBits = llvm::ConstantVector::get(MaskV); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 757 | Mask = Builder.CreateAnd(Mask, MaskBits, "mask"); |
| 758 | |
| 759 | // newv = undef |
| 760 | // mask = mask & maskbits |
| 761 | // for each elt |
| 762 | // n = extract mask i |
| 763 | // x = extract val n |
| 764 | // newv = insert newv, x, i |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 765 | llvm::VectorType *RTy = llvm::VectorType::get(LTy->getElementType(), |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 766 | MTy->getNumElements()); |
| 767 | Value* NewV = llvm::UndefValue::get(RTy); |
| 768 | for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) { |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 769 | Value *Indx = Builder.getInt32(i); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 770 | Indx = Builder.CreateExtractElement(Mask, Indx, "shuf_idx"); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 771 | Indx = Builder.CreateZExt(Indx, CGF.Int32Ty, "idx_zext"); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 772 | |
| 773 | // Handle vec3 special since the index will be off by one for the RHS. |
| 774 | if ((LHSElts == 6) && (E->getNumSubExprs() == 3)) { |
| 775 | Value *cmpIndx, *newIndx; |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 776 | cmpIndx = Builder.CreateICmpUGT(Indx, Builder.getInt32(3), |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 777 | "cmp_shuf_idx"); |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 778 | newIndx = Builder.CreateSub(Indx, Builder.getInt32(1), "shuf_idx_adj"); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 779 | Indx = Builder.CreateSelect(cmpIndx, newIndx, Indx, "sel_shuf_idx"); |
| 780 | } |
| 781 | Value *VExt = Builder.CreateExtractElement(LHS, Indx, "shuf_elt"); |
| 782 | NewV = Builder.CreateInsertElement(NewV, VExt, Indx, "shuf_ins"); |
| 783 | } |
| 784 | return NewV; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 785 | } |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 786 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 787 | Value* V1 = CGF.EmitScalarExpr(E->getExpr(0)); |
| 788 | Value* V2 = CGF.EmitScalarExpr(E->getExpr(1)); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 789 | |
| 790 | // Handle vec3 special since the index will be off by one for the RHS. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 791 | llvm::VectorType *VTy = cast<llvm::VectorType>(V1->getType()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 792 | SmallVector<llvm::Constant*, 32> indices; |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 793 | for (unsigned i = 2; i < E->getNumSubExprs(); i++) { |
Eli Friedman | 0eb47fc | 2011-05-19 00:37:32 +0000 | [diff] [blame] | 794 | unsigned Idx = E->getShuffleMaskIdx(CGF.getContext(), i-2); |
| 795 | if (VTy->getNumElements() == 3 && Idx > 3) |
| 796 | Idx -= 1; |
| 797 | indices.push_back(Builder.getInt32(Idx)); |
Nate Begeman | 37b6a57 | 2010-06-08 00:16:34 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 800 | Value *SV = llvm::ConstantVector::get(indices); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 801 | return Builder.CreateShuffleVector(V1, V2, SV, "shuffle"); |
| 802 | } |
Eli Friedman | 2866527 | 2009-11-26 03:22:21 +0000 | [diff] [blame] | 803 | Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) { |
| 804 | Expr::EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 805 | if (E->EvaluateAsRValue(Result, CGF.getContext()) && Result.Val.isInt()) { |
Eli Friedman | 2866527 | 2009-11-26 03:22:21 +0000 | [diff] [blame] | 806 | if (E->isArrow()) |
| 807 | CGF.EmitScalarExpr(E->getBase()); |
| 808 | else |
| 809 | EmitLValue(E->getBase()); |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 810 | return Builder.getInt(Result.Val.getInt()); |
Eli Friedman | 2866527 | 2009-11-26 03:22:21 +0000 | [diff] [blame] | 811 | } |
Devang Patel | 78ba3d4 | 2010-10-04 21:46:04 +0000 | [diff] [blame] | 812 | |
| 813 | // Emit debug info for aggregate now, if it was delayed to reduce |
| 814 | // debug info size. |
| 815 | CGDebugInfo *DI = CGF.getDebugInfo(); |
| 816 | if (DI && CGF.CGM.getCodeGenOpts().LimitDebugInfo) { |
| 817 | QualType PQTy = E->getBase()->IgnoreParenImpCasts()->getType(); |
| 818 | if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) |
Devang Patel | 49c8465 | 2010-10-04 22:28:23 +0000 | [diff] [blame] | 819 | if (FieldDecl *M = dyn_cast<FieldDecl>(E->getMemberDecl())) |
Devang Patel | 78ba3d4 | 2010-10-04 21:46:04 +0000 | [diff] [blame] | 820 | DI->getOrCreateRecordType(PTy->getPointeeType(), |
| 821 | M->getParent()->getLocation()); |
Devang Patel | 7fa8ab2 | 2010-10-04 22:13:18 +0000 | [diff] [blame] | 822 | } |
Eli Friedman | 2866527 | 2009-11-26 03:22:21 +0000 | [diff] [blame] | 823 | return EmitLoadOfLValue(E); |
| 824 | } |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 825 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 826 | Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 827 | TestAndClearIgnoreResultAssign(); |
| 828 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 829 | // Emit subscript expressions in rvalue context's. For most cases, this just |
| 830 | // loads the lvalue formed by the subscript expr. However, we have to be |
| 831 | // careful, because the base of a vector subscript is occasionally an rvalue, |
| 832 | // so we can't get it as an lvalue. |
| 833 | if (!E->getBase()->getType()->isVectorType()) |
| 834 | return EmitLoadOfLValue(E); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 835 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 836 | // Handle the vector case. The base must be a vector, the index must be an |
| 837 | // integer value. |
| 838 | Value *Base = Visit(E->getBase()); |
| 839 | Value *Idx = Visit(E->getIdx()); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 840 | bool IdxSigned = E->getIdx()->getType()->isSignedIntegerOrEnumerationType(); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 841 | Idx = Builder.CreateIntCast(Idx, CGF.Int32Ty, IdxSigned, "vecidxcast"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 842 | return Builder.CreateExtractElement(Base, Idx, "vecext"); |
| 843 | } |
| 844 | |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 845 | static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 846 | unsigned Off, llvm::Type *I32Ty) { |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 847 | int MV = SVI->getMaskValue(Idx); |
| 848 | if (MV == -1) |
| 849 | return llvm::UndefValue::get(I32Ty); |
| 850 | return llvm::ConstantInt::get(I32Ty, Off+MV); |
| 851 | } |
| 852 | |
| 853 | Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) { |
| 854 | bool Ignore = TestAndClearIgnoreResultAssign(); |
| 855 | (void)Ignore; |
| 856 | assert (Ignore == false && "init list ignored"); |
| 857 | unsigned NumInitElements = E->getNumInits(); |
| 858 | |
| 859 | if (E->hadArrayRangeDesignator()) |
| 860 | CGF.ErrorUnsupported(E, "GNU array range designator extension"); |
| 861 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 862 | llvm::VectorType *VType = |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 863 | dyn_cast<llvm::VectorType>(ConvertType(E->getType())); |
| 864 | |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 865 | if (!VType) { |
| 866 | if (NumInitElements == 0) { |
| 867 | // C++11 value-initialization for the scalar. |
| 868 | return EmitNullValue(E->getType()); |
| 869 | } |
| 870 | // We have a scalar in braces. Just use the first element. |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 871 | return Visit(E->getInit(0)); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 872 | } |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 873 | |
| 874 | unsigned ResElts = VType->getNumElements(); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 875 | |
| 876 | // Loop over initializers collecting the Value for each, and remembering |
| 877 | // whether the source was swizzle (ExtVectorElementExpr). This will allow |
| 878 | // us to fold the shuffle for the swizzle into the shuffle for the vector |
| 879 | // initializer, since LLVM optimizers generally do not want to touch |
| 880 | // shuffles. |
| 881 | unsigned CurIdx = 0; |
| 882 | bool VIsUndefShuffle = false; |
| 883 | llvm::Value *V = llvm::UndefValue::get(VType); |
| 884 | for (unsigned i = 0; i != NumInitElements; ++i) { |
| 885 | Expr *IE = E->getInit(i); |
| 886 | Value *Init = Visit(IE); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 887 | SmallVector<llvm::Constant*, 16> Args; |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 888 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 889 | llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType()); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 890 | |
| 891 | // Handle scalar elements. If the scalar initializer is actually one |
| 892 | // element of a different vector of the same width, use shuffle instead of |
| 893 | // extract+insert. |
| 894 | if (!VVT) { |
| 895 | if (isa<ExtVectorElementExpr>(IE)) { |
| 896 | llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init); |
| 897 | |
| 898 | if (EI->getVectorOperandType()->getNumElements() == ResElts) { |
| 899 | llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand()); |
| 900 | Value *LHS = 0, *RHS = 0; |
| 901 | if (CurIdx == 0) { |
| 902 | // insert into undef -> shuffle (src, undef) |
| 903 | Args.push_back(C); |
| 904 | for (unsigned j = 1; j != ResElts; ++j) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 905 | Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 906 | |
| 907 | LHS = EI->getVectorOperand(); |
| 908 | RHS = V; |
| 909 | VIsUndefShuffle = true; |
| 910 | } else if (VIsUndefShuffle) { |
| 911 | // insert into undefshuffle && size match -> shuffle (v, src) |
| 912 | llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V); |
| 913 | for (unsigned j = 0; j != CurIdx; ++j) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 914 | Args.push_back(getMaskElt(SVV, j, 0, CGF.Int32Ty)); |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 915 | Args.push_back(Builder.getInt32(ResElts + C->getZExtValue())); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 916 | for (unsigned j = CurIdx + 1; j != ResElts; ++j) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 917 | Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 918 | |
| 919 | LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); |
| 920 | RHS = EI->getVectorOperand(); |
| 921 | VIsUndefShuffle = false; |
| 922 | } |
| 923 | if (!Args.empty()) { |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 924 | llvm::Constant *Mask = llvm::ConstantVector::get(Args); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 925 | V = Builder.CreateShuffleVector(LHS, RHS, Mask); |
| 926 | ++CurIdx; |
| 927 | continue; |
| 928 | } |
| 929 | } |
| 930 | } |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 931 | V = Builder.CreateInsertElement(V, Init, Builder.getInt32(CurIdx), |
| 932 | "vecinit"); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 933 | VIsUndefShuffle = false; |
| 934 | ++CurIdx; |
| 935 | continue; |
| 936 | } |
| 937 | |
| 938 | unsigned InitElts = VVT->getNumElements(); |
| 939 | |
| 940 | // If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's |
| 941 | // input is the same width as the vector being constructed, generate an |
| 942 | // optimized shuffle of the swizzle input into the result. |
Nate Begeman | a99f083 | 2009-10-25 02:26:01 +0000 | [diff] [blame] | 943 | unsigned Offset = (CurIdx == 0) ? 0 : ResElts; |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 944 | if (isa<ExtVectorElementExpr>(IE)) { |
| 945 | llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init); |
| 946 | Value *SVOp = SVI->getOperand(0); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 947 | llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType()); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 948 | |
| 949 | if (OpTy->getNumElements() == ResElts) { |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 950 | for (unsigned j = 0; j != CurIdx; ++j) { |
| 951 | // If the current vector initializer is a shuffle with undef, merge |
| 952 | // this shuffle directly into it. |
| 953 | if (VIsUndefShuffle) { |
| 954 | Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 955 | CGF.Int32Ty)); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 956 | } else { |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 957 | Args.push_back(Builder.getInt32(j)); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | for (unsigned j = 0, je = InitElts; j != je; ++j) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 961 | Args.push_back(getMaskElt(SVI, j, Offset, CGF.Int32Ty)); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 962 | for (unsigned j = CurIdx + InitElts; j != ResElts; ++j) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 963 | Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 964 | |
| 965 | if (VIsUndefShuffle) |
| 966 | V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); |
| 967 | |
| 968 | Init = SVOp; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | // Extend init to result vector length, and then shuffle its contribution |
| 973 | // to the vector initializer into V. |
| 974 | if (Args.empty()) { |
| 975 | for (unsigned j = 0; j != InitElts; ++j) |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 976 | Args.push_back(Builder.getInt32(j)); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 977 | for (unsigned j = InitElts; j != ResElts; ++j) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 978 | Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 979 | llvm::Constant *Mask = llvm::ConstantVector::get(Args); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 980 | Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT), |
Nate Begeman | a99f083 | 2009-10-25 02:26:01 +0000 | [diff] [blame] | 981 | Mask, "vext"); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 982 | |
| 983 | Args.clear(); |
| 984 | for (unsigned j = 0; j != CurIdx; ++j) |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 985 | Args.push_back(Builder.getInt32(j)); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 986 | for (unsigned j = 0; j != InitElts; ++j) |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 987 | Args.push_back(Builder.getInt32(j+Offset)); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 988 | for (unsigned j = CurIdx + InitElts; j != ResElts; ++j) |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 989 | Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | // If V is undef, make sure it ends up on the RHS of the shuffle to aid |
| 993 | // merging subsequent shuffles into this one. |
| 994 | if (CurIdx == 0) |
| 995 | std::swap(V, Init); |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 996 | llvm::Constant *Mask = llvm::ConstantVector::get(Args); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 997 | V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit"); |
| 998 | VIsUndefShuffle = isa<llvm::UndefValue>(Init); |
| 999 | CurIdx += InitElts; |
| 1000 | } |
| 1001 | |
| 1002 | // FIXME: evaluate codegen vs. shuffling against constant null vector. |
| 1003 | // Emit remaining default initializers. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1004 | llvm::Type *EltTy = VType->getElementType(); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Emit remaining default initializers |
| 1007 | for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) { |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 1008 | Value *Idx = Builder.getInt32(CurIdx); |
Nate Begeman | 0533b30 | 2009-10-18 20:10:40 +0000 | [diff] [blame] | 1009 | llvm::Value *Init = llvm::Constant::getNullValue(EltTy); |
| 1010 | V = Builder.CreateInsertElement(V, Init, Idx, "vecinit"); |
| 1011 | } |
| 1012 | return V; |
| 1013 | } |
| 1014 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1015 | static bool ShouldNullCheckClassCastValue(const CastExpr *CE) { |
| 1016 | const Expr *E = CE->getSubExpr(); |
John McCall | 23cba80 | 2010-03-30 23:58:03 +0000 | [diff] [blame] | 1017 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1018 | if (CE->getCastKind() == CK_UncheckedDerivedToBase) |
John McCall | 23cba80 | 2010-03-30 23:58:03 +0000 | [diff] [blame] | 1019 | return false; |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1020 | |
| 1021 | if (isa<CXXThisExpr>(E)) { |
| 1022 | // We always assume that 'this' is never null. |
| 1023 | return false; |
| 1024 | } |
| 1025 | |
| 1026 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) { |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1027 | // And that glvalue casts are never null. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1028 | if (ICE->getValueKind() != VK_RValue) |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1029 | return false; |
| 1030 | } |
| 1031 | |
| 1032 | return true; |
| 1033 | } |
| 1034 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1035 | // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts |
| 1036 | // have to handle a more broad range of conversions than explicit casts, as they |
| 1037 | // handle things like function to ptr-to-function decay etc. |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1038 | Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1039 | Expr *E = CE->getSubExpr(); |
Anders Carlsson | 592a2bb | 2009-09-22 22:00:46 +0000 | [diff] [blame] | 1040 | QualType DestTy = CE->getType(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1041 | CastKind Kind = CE->getCastKind(); |
Anders Carlsson | 592a2bb | 2009-09-22 22:00:46 +0000 | [diff] [blame] | 1042 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1043 | if (!DestTy->isVoidType()) |
| 1044 | TestAndClearIgnoreResultAssign(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1045 | |
Eli Friedman | 8c3e7e7 | 2009-11-27 02:07:44 +0000 | [diff] [blame] | 1046 | // Since almost all cast kinds apply to scalars, this switch doesn't have |
| 1047 | // a default case, so the compiler will warn on a missing case. The cases |
| 1048 | // are in the same order as in the CastKind enum. |
Anders Carlsson | e977624 | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 1049 | switch (Kind) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1050 | case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!"); |
| 1051 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1052 | case CK_LValueBitCast: |
| 1053 | case CK_ObjCObjectLValueCast: { |
Douglas Gregor | e39a389 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 1054 | Value *V = EmitLValue(E).getAddress(); |
| 1055 | V = Builder.CreateBitCast(V, |
| 1056 | ConvertType(CGF.getContext().getPointerType(DestTy))); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1057 | return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy)); |
Douglas Gregor | e39a389 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 1058 | } |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 1059 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1060 | case CK_CPointerToObjCPointerCast: |
| 1061 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1062 | case CK_AnyPointerToBlockPointerCast: |
| 1063 | case CK_BitCast: { |
Anders Carlsson | cb3c308 | 2009-09-01 20:52:42 +0000 | [diff] [blame] | 1064 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 1065 | return Builder.CreateBitCast(Src, ConvertType(DestTy)); |
| 1066 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1067 | case CK_NoOp: |
| 1068 | case CK_UserDefinedConversion: |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1069 | return Visit(const_cast<Expr*>(E)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1070 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1071 | case CK_BaseToDerived: { |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1072 | const CXXRecordDecl *DerivedClassDecl = |
| 1073 | DestTy->getCXXRecordDeclForPointerType(); |
| 1074 | |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 1075 | return CGF.GetAddressOfDerivedClass(Visit(E), DerivedClassDecl, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1076 | CE->path_begin(), CE->path_end(), |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 1077 | ShouldNullCheckClassCastValue(CE)); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 1078 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1079 | case CK_UncheckedDerivedToBase: |
| 1080 | case CK_DerivedToBase: { |
Anders Carlsson | 191dfe9 | 2009-09-12 04:57:16 +0000 | [diff] [blame] | 1081 | const RecordType *DerivedClassTy = |
| 1082 | E->getType()->getAs<PointerType>()->getPointeeType()->getAs<RecordType>(); |
| 1083 | CXXRecordDecl *DerivedClassDecl = |
| 1084 | cast<CXXRecordDecl>(DerivedClassTy->getDecl()); |
| 1085 | |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 1086 | return CGF.GetAddressOfBaseClass(Visit(E), DerivedClassDecl, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1087 | CE->path_begin(), CE->path_end(), |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 1088 | ShouldNullCheckClassCastValue(CE)); |
Anders Carlsson | 191dfe9 | 2009-09-12 04:57:16 +0000 | [diff] [blame] | 1089 | } |
Anders Carlsson | 575b374 | 2011-04-11 02:03:26 +0000 | [diff] [blame] | 1090 | case CK_Dynamic: { |
Eli Friedman | 8c3e7e7 | 2009-11-27 02:07:44 +0000 | [diff] [blame] | 1091 | Value *V = Visit(const_cast<Expr*>(E)); |
| 1092 | const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE); |
| 1093 | return CGF.EmitDynamicCast(V, DCE); |
| 1094 | } |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1095 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1096 | case CK_ArrayToPointerDecay: { |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1097 | assert(E->getType()->isArrayType() && |
| 1098 | "Array to pointer decay must have array source type!"); |
| 1099 | |
| 1100 | Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays. |
| 1101 | |
| 1102 | // Note that VLA pointers are always decayed, so we don't need to do |
| 1103 | // anything here. |
| 1104 | if (!E->getType()->isVariableArrayType()) { |
| 1105 | assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer"); |
| 1106 | assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 1107 | ->getElementType()) && |
| 1108 | "Expected pointer to array"); |
| 1109 | V = Builder.CreateStructGEP(V, 0, "arraydecay"); |
| 1110 | } |
| 1111 | |
Chris Lattner | 410b12e | 2011-07-20 04:31:01 +0000 | [diff] [blame] | 1112 | // Make sure the array decay ends up being the right type. This matters if |
| 1113 | // the array type was of an incomplete type. |
Chris Lattner | cb8095f | 2011-07-20 04:59:57 +0000 | [diff] [blame] | 1114 | return CGF.Builder.CreateBitCast(V, ConvertType(CE->getType())); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1115 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1116 | case CK_FunctionToPointerDecay: |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1117 | return EmitLValue(E).getAddress(); |
| 1118 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1119 | case CK_NullToPointer: |
| 1120 | if (MustVisitNullValue(E)) |
| 1121 | (void) Visit(E); |
| 1122 | |
| 1123 | return llvm::ConstantPointerNull::get( |
| 1124 | cast<llvm::PointerType>(ConvertType(DestTy))); |
| 1125 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1126 | case CK_NullToMemberPointer: { |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1127 | if (MustVisitNullValue(E)) |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1128 | (void) Visit(E); |
| 1129 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 1130 | const MemberPointerType *MPT = CE->getType()->getAs<MemberPointerType>(); |
| 1131 | return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT); |
| 1132 | } |
Anders Carlsson | 191dfe9 | 2009-09-12 04:57:16 +0000 | [diff] [blame] | 1133 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1134 | case CK_BaseToDerivedMemberPointer: |
| 1135 | case CK_DerivedToBaseMemberPointer: { |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1136 | Value *Src = Visit(E); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1137 | |
| 1138 | // Note that the AST doesn't distinguish between checked and |
| 1139 | // unchecked member pointer conversions, so we always have to |
| 1140 | // implement checked conversions here. This is inefficient when |
| 1141 | // actual control flow may be required in order to perform the |
| 1142 | // check, which it is for data member pointers (but not member |
| 1143 | // function pointers on Itanium and ARM). |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 1144 | return CGF.CGM.getCXXABI().EmitMemberPointerConversion(CGF, CE, Src); |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1145 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1146 | |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 1147 | case CK_ARCProduceObject: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1148 | return CGF.EmitARCRetainScalarExpr(E); |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 1149 | case CK_ARCConsumeObject: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1150 | return CGF.EmitObjCConsumeObject(E->getType(), Visit(E)); |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 1151 | case CK_ARCReclaimReturnedObject: { |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 1152 | llvm::Value *value = Visit(E); |
| 1153 | value = CGF.EmitARCRetainAutoreleasedReturnValue(value); |
| 1154 | return CGF.EmitObjCConsumeObject(E->getType(), value); |
| 1155 | } |
John McCall | 348f16f | 2011-10-04 06:23:45 +0000 | [diff] [blame] | 1156 | case CK_ARCExtendBlockObject: |
| 1157 | return CGF.EmitARCExtendBlockObject(E); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1158 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 1159 | case CK_FloatingRealToComplex: |
| 1160 | case CK_FloatingComplexCast: |
| 1161 | case CK_IntegralRealToComplex: |
| 1162 | case CK_IntegralComplexCast: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1163 | case CK_IntegralComplexToFloatingComplex: |
| 1164 | case CK_FloatingComplexToIntegralComplex: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1165 | case CK_ConstructorConversion: |
John McCall | 61ad0e6 | 2010-11-16 06:21:14 +0000 | [diff] [blame] | 1166 | case CK_ToUnion: |
| 1167 | llvm_unreachable("scalar cast to non-scalar value"); |
Eli Friedman | 8c3e7e7 | 2009-11-27 02:07:44 +0000 | [diff] [blame] | 1168 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1169 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 1170 | case CK_LValueToRValue: |
| 1171 | assert(CGF.getContext().hasSameUnqualifiedType(E->getType(), DestTy)); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1172 | assert(E->isGLValue() && "lvalue-to-rvalue applied to r-value!"); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 1173 | return Visit(const_cast<Expr*>(E)); |
Eli Friedman | 8c3e7e7 | 2009-11-27 02:07:44 +0000 | [diff] [blame] | 1174 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1175 | case CK_IntegralToPointer: { |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 1176 | Value *Src = Visit(const_cast<Expr*>(E)); |
Daniel Dunbar | 89f176d | 2010-08-25 03:32:38 +0000 | [diff] [blame] | 1177 | |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 1178 | // First, convert to the correct width so that we control the kind of |
| 1179 | // extension. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1180 | llvm::Type *MiddleTy = CGF.IntPtrTy; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1181 | bool InputSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 1182 | llvm::Value* IntResult = |
| 1183 | Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); |
Daniel Dunbar | 89f176d | 2010-08-25 03:32:38 +0000 | [diff] [blame] | 1184 | |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 1185 | return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy)); |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 1186 | } |
Eli Friedman | 6594942 | 2011-06-25 02:58:47 +0000 | [diff] [blame] | 1187 | case CK_PointerToIntegral: |
| 1188 | assert(!DestTy->isBooleanType() && "bool should use PointerToBool"); |
| 1189 | return Builder.CreatePtrToInt(Visit(E), ConvertType(DestTy)); |
Daniel Dunbar | 89f176d | 2010-08-25 03:32:38 +0000 | [diff] [blame] | 1190 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1191 | case CK_ToVoid: { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1192 | CGF.EmitIgnoredExpr(E); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1193 | return 0; |
| 1194 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1195 | case CK_VectorSplat: { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1196 | llvm::Type *DstTy = ConvertType(DestTy); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1197 | Value *Elt = Visit(const_cast<Expr*>(E)); |
| 1198 | |
| 1199 | // Insert the element in element zero of an undef vector |
| 1200 | llvm::Value *UnV = llvm::UndefValue::get(DstTy); |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 1201 | llvm::Value *Idx = Builder.getInt32(0); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1202 | UnV = Builder.CreateInsertElement(UnV, Elt, Idx); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1203 | |
| 1204 | // Splat the element across to all elements |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1205 | SmallVector<llvm::Constant*, 16> Args; |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1206 | unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 1207 | llvm::Constant *Zero = Builder.getInt32(0); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1208 | for (unsigned i = 0; i < NumElements; i++) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1209 | Args.push_back(Zero); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1210 | |
Chris Lattner | fb018d1 | 2011-02-15 00:14:06 +0000 | [diff] [blame] | 1211 | llvm::Constant *Mask = llvm::ConstantVector::get(Args); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1212 | llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); |
| 1213 | return Yay; |
| 1214 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1215 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1216 | case CK_IntegralCast: |
| 1217 | case CK_IntegralToFloating: |
| 1218 | case CK_FloatingToIntegral: |
| 1219 | case CK_FloatingCast: |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1220 | return EmitScalarConversion(Visit(E), E->getType(), DestTy); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1221 | case CK_IntegralToBoolean: |
| 1222 | return EmitIntToBoolConversion(Visit(E)); |
| 1223 | case CK_PointerToBoolean: |
| 1224 | return EmitPointerToBoolConversion(Visit(E)); |
| 1225 | case CK_FloatingToBoolean: |
| 1226 | return EmitFloatToBoolConversion(Visit(E)); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1227 | case CK_MemberPointerToBoolean: { |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 1228 | llvm::Value *MemPtr = Visit(E); |
| 1229 | const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>(); |
| 1230 | return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT); |
Anders Carlsson | e977624 | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 1231 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1232 | |
| 1233 | case CK_FloatingComplexToReal: |
| 1234 | case CK_IntegralComplexToReal: |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1235 | return CGF.EmitComplexExpr(E, false, true).first; |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1236 | |
| 1237 | case CK_FloatingComplexToBoolean: |
| 1238 | case CK_IntegralComplexToBoolean: { |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1239 | CodeGenFunction::ComplexPairTy V = CGF.EmitComplexExpr(E); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1240 | |
| 1241 | // TODO: kill this function off, inline appropriate case here |
| 1242 | return EmitComplexToScalarConversion(V, E->getType(), DestTy); |
| 1243 | } |
| 1244 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 1245 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1246 | |
John McCall | 61ad0e6 | 2010-11-16 06:21:14 +0000 | [diff] [blame] | 1247 | llvm_unreachable("unknown scalar cast"); |
Chris Lattner | 19a1d7c | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 1248 | return 0; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 1251 | Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 1252 | CodeGenFunction::StmtExprEvaluation eval(CGF); |
| 1253 | return CGF.EmitCompoundStmt(*E->getSubStmt(), !E->getType()->isVoidType()) |
| 1254 | .getScalarVal(); |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1257 | Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { |
John McCall | a5bcb8f | 2011-03-16 02:53:38 +0000 | [diff] [blame] | 1258 | LValue LV = CGF.EmitBlockDeclRefLValue(E); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1259 | return CGF.EmitLoadOfLValue(LV).getScalarVal(); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 1260 | } |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 1261 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1262 | //===----------------------------------------------------------------------===// |
| 1263 | // Unary Operators |
| 1264 | //===----------------------------------------------------------------------===// |
| 1265 | |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1266 | llvm::Value *ScalarExprEmitter:: |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 1267 | EmitAddConsiderOverflowBehavior(const UnaryOperator *E, |
| 1268 | llvm::Value *InVal, |
| 1269 | llvm::Value *NextVal, bool IsInc) { |
| 1270 | switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { |
| 1271 | case LangOptions::SOB_Undefined: |
| 1272 | return Builder.CreateNSWAdd(InVal, NextVal, IsInc ? "inc" : "dec"); |
| 1273 | break; |
| 1274 | case LangOptions::SOB_Defined: |
| 1275 | return Builder.CreateAdd(InVal, NextVal, IsInc ? "inc" : "dec"); |
| 1276 | break; |
| 1277 | case LangOptions::SOB_Trapping: |
| 1278 | BinOpInfo BinOp; |
| 1279 | BinOp.LHS = InVal; |
| 1280 | BinOp.RHS = NextVal; |
| 1281 | BinOp.Ty = E->getType(); |
| 1282 | BinOp.Opcode = BO_Add; |
| 1283 | BinOp.E = E; |
| 1284 | return EmitOverflowCheckedBinOp(BinOp); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 1285 | } |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1286 | llvm_unreachable("Unknown SignedOverflowBehaviorTy"); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1289 | llvm::Value * |
| 1290 | ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 1291 | bool isInc, bool isPre) { |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1292 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1293 | QualType type = E->getSubExpr()->getType(); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1294 | llvm::Value *value = EmitLoadOfLValue(LV); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1295 | llvm::Value *input = value; |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 1296 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1297 | int amount = (isInc ? 1 : -1); |
| 1298 | |
| 1299 | // Special case of integer increment that we have to check first: bool++. |
| 1300 | // Due to promotion rules, we get: |
| 1301 | // bool++ -> bool = bool + 1 |
| 1302 | // -> bool = (int)bool + 1 |
| 1303 | // -> bool = ((int)bool + 1 != 0) |
| 1304 | // An interesting aspect of this is that increment is always true. |
| 1305 | // Decrement does not have this property. |
| 1306 | if (isInc && type->isBooleanType()) { |
| 1307 | value = Builder.getTrue(); |
| 1308 | |
| 1309 | // Most common case by far: integer increment. |
| 1310 | } else if (type->isIntegerType()) { |
| 1311 | |
| 1312 | llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount); |
| 1313 | |
Eli Friedman | fa0b409 | 2011-03-02 01:49:12 +0000 | [diff] [blame] | 1314 | // Note that signed integer inc/dec with width less than int can't |
| 1315 | // overflow because of promotion rules; we're just eliding a few steps here. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1316 | if (type->isSignedIntegerOrEnumerationType() && |
Eli Friedman | fa0b409 | 2011-03-02 01:49:12 +0000 | [diff] [blame] | 1317 | value->getType()->getPrimitiveSizeInBits() >= |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 1318 | CGF.IntTy->getBitWidth()) |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1319 | value = EmitAddConsiderOverflowBehavior(E, value, amt, isInc); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1320 | else |
| 1321 | value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec"); |
| 1322 | |
| 1323 | // Next most common: pointer increment. |
| 1324 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 1325 | QualType type = ptr->getPointeeType(); |
| 1326 | |
| 1327 | // VLA types don't have constant size. |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 1328 | if (const VariableArrayType *vla |
| 1329 | = CGF.getContext().getAsVariableArrayType(type)) { |
| 1330 | llvm::Value *numElts = CGF.getVLASize(vla).first; |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1331 | if (!isInc) numElts = Builder.CreateNSWNeg(numElts, "vla.negsize"); |
Chris Lattner | 2cb4222 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 1332 | if (CGF.getContext().getLangOptions().isSignedOverflowDefined()) |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1333 | value = Builder.CreateGEP(value, numElts, "vla.inc"); |
Chris Lattner | 2cb4222 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 1334 | else |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1335 | value = Builder.CreateInBoundsGEP(value, numElts, "vla.inc"); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1336 | |
| 1337 | // Arithmetic on function pointers (!) is just +-1. |
| 1338 | } else if (type->isFunctionType()) { |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 1339 | llvm::Value *amt = Builder.getInt32(amount); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1340 | |
| 1341 | value = CGF.EmitCastToVoidPtr(value); |
Chris Lattner | 2cb4222 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 1342 | if (CGF.getContext().getLangOptions().isSignedOverflowDefined()) |
| 1343 | value = Builder.CreateGEP(value, amt, "incdec.funcptr"); |
| 1344 | else |
| 1345 | value = Builder.CreateInBoundsGEP(value, amt, "incdec.funcptr"); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1346 | value = Builder.CreateBitCast(value, input->getType()); |
| 1347 | |
| 1348 | // For everything else, we can just do a simple increment. |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 1349 | } else { |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 1350 | llvm::Value *amt = Builder.getInt32(amount); |
Chris Lattner | 2cb4222 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 1351 | if (CGF.getContext().getLangOptions().isSignedOverflowDefined()) |
| 1352 | value = Builder.CreateGEP(value, amt, "incdec.ptr"); |
| 1353 | else |
| 1354 | value = Builder.CreateInBoundsGEP(value, amt, "incdec.ptr"); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | // Vector increment/decrement. |
| 1358 | } else if (type->isVectorType()) { |
| 1359 | if (type->hasIntegerRepresentation()) { |
| 1360 | llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount); |
| 1361 | |
Eli Friedman | d4b9ee3 | 2011-05-06 18:04:18 +0000 | [diff] [blame] | 1362 | value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec"); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1363 | } else { |
| 1364 | value = Builder.CreateFAdd( |
| 1365 | value, |
| 1366 | llvm::ConstantFP::get(value->getType(), amount), |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 1367 | isInc ? "inc" : "dec"); |
| 1368 | } |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 1369 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1370 | // Floating point. |
| 1371 | } else if (type->isRealFloatingType()) { |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1372 | // Add the inc/dec to the real part. |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1373 | llvm::Value *amt; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1374 | |
| 1375 | if (type->isHalfType()) { |
| 1376 | // Another special case: half FP increment should be done via float |
| 1377 | value = |
| 1378 | Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16), |
| 1379 | input); |
| 1380 | } |
| 1381 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1382 | if (value->getType()->isFloatTy()) |
| 1383 | amt = llvm::ConstantFP::get(VMContext, |
| 1384 | llvm::APFloat(static_cast<float>(amount))); |
| 1385 | else if (value->getType()->isDoubleTy()) |
| 1386 | amt = llvm::ConstantFP::get(VMContext, |
| 1387 | llvm::APFloat(static_cast<double>(amount))); |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1388 | else { |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1389 | llvm::APFloat F(static_cast<float>(amount)); |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1390 | bool ignored; |
| 1391 | F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero, |
| 1392 | &ignored); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1393 | amt = llvm::ConstantFP::get(VMContext, F); |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1394 | } |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1395 | value = Builder.CreateFAdd(value, amt, isInc ? "inc" : "dec"); |
| 1396 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1397 | if (type->isHalfType()) |
| 1398 | value = |
| 1399 | Builder.CreateCall(CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16), |
| 1400 | value); |
| 1401 | |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1402 | // Objective-C pointer types. |
| 1403 | } else { |
| 1404 | const ObjCObjectPointerType *OPT = type->castAs<ObjCObjectPointerType>(); |
| 1405 | value = CGF.EmitCastToVoidPtr(value); |
| 1406 | |
| 1407 | CharUnits size = CGF.getContext().getTypeSizeInChars(OPT->getObjectType()); |
| 1408 | if (!isInc) size = -size; |
| 1409 | llvm::Value *sizeValue = |
| 1410 | llvm::ConstantInt::get(CGF.SizeTy, size.getQuantity()); |
| 1411 | |
Chris Lattner | 2cb4222 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 1412 | if (CGF.getContext().getLangOptions().isSignedOverflowDefined()) |
| 1413 | value = Builder.CreateGEP(value, sizeValue, "incdec.objptr"); |
| 1414 | else |
| 1415 | value = Builder.CreateInBoundsGEP(value, sizeValue, "incdec.objptr"); |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1416 | value = Builder.CreateBitCast(value, input->getType()); |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1417 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1418 | |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1419 | // Store the updated result through the lvalue. |
| 1420 | if (LV.isBitField()) |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1421 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(value), LV, &value); |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1422 | else |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1423 | CGF.EmitStoreThroughLValue(RValue::get(value), LV); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1424 | |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1425 | // If this is a postinc, return the value read from memory, otherwise use the |
| 1426 | // updated value. |
John McCall | 5936e33 | 2011-02-15 09:22:45 +0000 | [diff] [blame] | 1427 | return isPre ? value : input; |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | |
| 1431 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1432 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1433 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1434 | // Emit unary minus with EmitSub so we handle overflow cases etc. |
| 1435 | BinOpInfo BinOp; |
Chris Lattner | 4ac0d83 | 2010-06-28 17:12:37 +0000 | [diff] [blame] | 1436 | BinOp.RHS = Visit(E->getSubExpr()); |
| 1437 | |
| 1438 | if (BinOp.RHS->getType()->isFPOrFPVectorTy()) |
| 1439 | BinOp.LHS = llvm::ConstantFP::getZeroValueForNegation(BinOp.RHS->getType()); |
| 1440 | else |
| 1441 | BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType()); |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1442 | BinOp.Ty = E->getType(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1443 | BinOp.Opcode = BO_Sub; |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1444 | BinOp.E = E; |
| 1445 | return EmitSub(BinOp); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1449 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1450 | Value *Op = Visit(E->getSubExpr()); |
| 1451 | return Builder.CreateNot(Op, "neg"); |
| 1452 | } |
| 1453 | |
| 1454 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 1455 | // Compare operand to zero. |
| 1456 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1457 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1458 | // Invert value. |
| 1459 | // TODO: Could dynamically modify easy computations here. For example, if |
| 1460 | // the operand is an icmp ne, turn into icmp eq. |
| 1461 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1462 | |
Anders Carlsson | 9f84d88 | 2009-05-19 18:44:53 +0000 | [diff] [blame] | 1463 | // ZExt result to the expr type. |
| 1464 | return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1467 | Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 1468 | // Try folding the offsetof to a constant. |
| 1469 | Expr::EvalResult EvalResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 1470 | if (E->EvaluateAsRValue(EvalResult, CGF.getContext())) |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 1471 | return Builder.getInt(EvalResult.Val.getInt()); |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Loop over the components of the offsetof to compute the value. |
| 1474 | unsigned n = E->getNumComponents(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1475 | llvm::Type* ResultType = ConvertType(E->getType()); |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1476 | llvm::Value* Result = llvm::Constant::getNullValue(ResultType); |
| 1477 | QualType CurrentType = E->getTypeSourceInfo()->getType(); |
| 1478 | for (unsigned i = 0; i != n; ++i) { |
| 1479 | OffsetOfExpr::OffsetOfNode ON = E->getComponent(i); |
Eli Friedman | 16fd39f | 2010-08-06 16:37:05 +0000 | [diff] [blame] | 1480 | llvm::Value *Offset = 0; |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1481 | switch (ON.getKind()) { |
| 1482 | case OffsetOfExpr::OffsetOfNode::Array: { |
| 1483 | // Compute the index |
| 1484 | Expr *IdxExpr = E->getIndexExpr(ON.getArrayExprIndex()); |
| 1485 | llvm::Value* Idx = CGF.EmitScalarExpr(IdxExpr); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1486 | bool IdxSigned = IdxExpr->getType()->isSignedIntegerOrEnumerationType(); |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1487 | Idx = Builder.CreateIntCast(Idx, ResultType, IdxSigned, "conv"); |
| 1488 | |
| 1489 | // Save the element type |
| 1490 | CurrentType = |
| 1491 | CGF.getContext().getAsArrayType(CurrentType)->getElementType(); |
| 1492 | |
| 1493 | // Compute the element size |
| 1494 | llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType, |
| 1495 | CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity()); |
| 1496 | |
| 1497 | // Multiply out to compute the result |
| 1498 | Offset = Builder.CreateMul(Idx, ElemSize); |
| 1499 | break; |
| 1500 | } |
| 1501 | |
| 1502 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 1503 | FieldDecl *MemberDecl = ON.getField(); |
| 1504 | RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl(); |
| 1505 | const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); |
| 1506 | |
| 1507 | // Compute the index of the field in its parent. |
| 1508 | unsigned i = 0; |
| 1509 | // FIXME: It would be nice if we didn't have to loop here! |
| 1510 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 1511 | FieldEnd = RD->field_end(); |
| 1512 | Field != FieldEnd; (void)++Field, ++i) { |
| 1513 | if (*Field == MemberDecl) |
| 1514 | break; |
| 1515 | } |
| 1516 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
| 1517 | |
| 1518 | // Compute the offset to the field |
| 1519 | int64_t OffsetInt = RL.getFieldOffset(i) / |
| 1520 | CGF.getContext().getCharWidth(); |
| 1521 | Offset = llvm::ConstantInt::get(ResultType, OffsetInt); |
| 1522 | |
| 1523 | // Save the element type. |
| 1524 | CurrentType = MemberDecl->getType(); |
| 1525 | break; |
| 1526 | } |
Eli Friedman | 16fd39f | 2010-08-06 16:37:05 +0000 | [diff] [blame] | 1527 | |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1528 | case OffsetOfExpr::OffsetOfNode::Identifier: |
Eli Friedman | 6d4e44b | 2010-08-06 01:17:25 +0000 | [diff] [blame] | 1529 | llvm_unreachable("dependent __builtin_offsetof"); |
Eli Friedman | 16fd39f | 2010-08-06 16:37:05 +0000 | [diff] [blame] | 1530 | |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1531 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 1532 | if (ON.getBase()->isVirtual()) { |
| 1533 | CGF.ErrorUnsupported(E, "virtual base in offsetof"); |
| 1534 | continue; |
| 1535 | } |
| 1536 | |
| 1537 | RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl(); |
| 1538 | const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); |
| 1539 | |
| 1540 | // Save the element type. |
| 1541 | CurrentType = ON.getBase()->getType(); |
| 1542 | |
| 1543 | // Compute the offset to the base. |
| 1544 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 1545 | CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 1546 | int64_t OffsetInt = RL.getBaseClassOffsetInBits(BaseRD) / |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1547 | CGF.getContext().getCharWidth(); |
| 1548 | Offset = llvm::ConstantInt::get(ResultType, OffsetInt); |
| 1549 | break; |
| 1550 | } |
| 1551 | } |
| 1552 | Result = Builder.CreateAdd(Result, Offset); |
| 1553 | } |
| 1554 | return Result; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1557 | /// VisitUnaryExprOrTypeTraitExpr - Return the size or alignment of the type of |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1558 | /// argument of the sizeof expression as an integer. |
| 1559 | Value * |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1560 | ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr( |
| 1561 | const UnaryExprOrTypeTraitExpr *E) { |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1562 | QualType TypeToSize = E->getTypeOfArgument(); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 1563 | if (E->getKind() == UETT_SizeOf) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1564 | if (const VariableArrayType *VAT = |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1565 | CGF.getContext().getAsVariableArrayType(TypeToSize)) { |
| 1566 | if (E->isArgumentType()) { |
| 1567 | // sizeof(type) - make sure to emit the VLA size. |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1568 | CGF.EmitVariablyModifiedType(TypeToSize); |
Eli Friedman | 8f426fa | 2009-04-20 03:21:44 +0000 | [diff] [blame] | 1569 | } else { |
| 1570 | // C99 6.5.3.4p2: If the argument is an expression of type |
| 1571 | // VLA, it is evaluated. |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 1572 | CGF.EmitIgnoredExpr(E->getArgumentExpr()); |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1573 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1574 | |
John McCall | bc8d40d | 2011-06-24 21:55:10 +0000 | [diff] [blame] | 1575 | QualType eltType; |
| 1576 | llvm::Value *numElts; |
| 1577 | llvm::tie(numElts, eltType) = CGF.getVLASize(VAT); |
| 1578 | |
| 1579 | llvm::Value *size = numElts; |
| 1580 | |
| 1581 | // Scale the number of non-VLA elements by the non-VLA element size. |
| 1582 | CharUnits eltSize = CGF.getContext().getTypeSizeInChars(eltType); |
| 1583 | if (!eltSize.isOne()) |
| 1584 | size = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), numElts); |
| 1585 | |
| 1586 | return size; |
Anders Carlsson | b50525b | 2008-12-21 03:33:21 +0000 | [diff] [blame] | 1587 | } |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 1588 | } |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1589 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1590 | // If this isn't sizeof(vla), the result must be constant; use the constant |
| 1591 | // folding logic so we don't have to duplicate it here. |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1592 | Expr::EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 1593 | E->EvaluateAsRValue(Result, CGF.getContext()); |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 1594 | return Builder.getInt(Result.Val.getInt()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1597 | Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { |
| 1598 | Expr *Op = E->getSubExpr(); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1599 | if (Op->getType()->isAnyComplexType()) { |
| 1600 | // If it's an l-value, load through the appropriate subobject l-value. |
| 1601 | // Note that we have to ask E because Op might be an l-value that |
| 1602 | // this won't work for, e.g. an Obj-C property. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1603 | if (E->isGLValue()) |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1604 | return CGF.EmitLoadOfLValue(CGF.EmitLValue(E)).getScalarVal(); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1605 | |
| 1606 | // Otherwise, calculate and project. |
| 1607 | return CGF.EmitComplexExpr(Op, false, true).first; |
| 1608 | } |
| 1609 | |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1610 | return Visit(Op); |
| 1611 | } |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1612 | |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1613 | Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { |
| 1614 | Expr *Op = E->getSubExpr(); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1615 | if (Op->getType()->isAnyComplexType()) { |
| 1616 | // If it's an l-value, load through the appropriate subobject l-value. |
| 1617 | // Note that we have to ask E because Op might be an l-value that |
| 1618 | // this won't work for, e.g. an Obj-C property. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1619 | if (Op->isGLValue()) |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1620 | return CGF.EmitLoadOfLValue(CGF.EmitLValue(E)).getScalarVal(); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1621 | |
| 1622 | // Otherwise, calculate and project. |
| 1623 | return CGF.EmitComplexExpr(Op, true, false).second; |
| 1624 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1625 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1626 | // __imag on a scalar returns zero. Emit the subexpr to ensure side |
| 1627 | // effects are evaluated, but not the actual value. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1628 | CGF.EmitScalarExpr(Op, true); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1629 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1632 | //===----------------------------------------------------------------------===// |
| 1633 | // Binary Operators |
| 1634 | //===----------------------------------------------------------------------===// |
| 1635 | |
| 1636 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1637 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1638 | BinOpInfo Result; |
| 1639 | Result.LHS = Visit(E->getLHS()); |
| 1640 | Result.RHS = Visit(E->getRHS()); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1641 | Result.Ty = E->getType(); |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1642 | Result.Opcode = E->getOpcode(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1643 | Result.E = E; |
| 1644 | return Result; |
| 1645 | } |
| 1646 | |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1647 | LValue ScalarExprEmitter::EmitCompoundAssignLValue( |
| 1648 | const CompoundAssignOperator *E, |
| 1649 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &), |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1650 | Value *&Result) { |
Benjamin Kramer | 54d76db | 2009-12-25 15:43:36 +0000 | [diff] [blame] | 1651 | QualType LHSTy = E->getLHS()->getType(); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1652 | BinOpInfo OpInfo; |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1653 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1654 | if (E->getComputationResultType()->isAnyComplexType()) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1655 | // This needs to go through the complex expression emitter, but it's a tad |
| 1656 | // complicated to do that... I'm leaving it out for now. (Note that we do |
| 1657 | // actually need the imaginary part of the RHS for multiplication and |
| 1658 | // division.) |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1659 | CGF.ErrorUnsupported(E, "complex compound assignment"); |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1660 | Result = llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1661 | return LValue(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1662 | } |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1663 | |
Mike Stump | cc0442f | 2009-05-22 19:07:20 +0000 | [diff] [blame] | 1664 | // Emit the RHS first. __block variables need to have the rhs evaluated |
| 1665 | // first, plus this should improve codegen a little. |
| 1666 | OpInfo.RHS = Visit(E->getRHS()); |
| 1667 | OpInfo.Ty = E->getComputationResultType(); |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1668 | OpInfo.Opcode = E->getOpcode(); |
Mike Stump | cc0442f | 2009-05-22 19:07:20 +0000 | [diff] [blame] | 1669 | OpInfo.E = E; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1670 | // Load/convert the LHS. |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 1671 | LValue LHSLV = EmitCheckedLValue(E->getLHS()); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1672 | OpInfo.LHS = EmitLoadOfLValue(LHSLV); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1673 | OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, |
| 1674 | E->getComputationLHSType()); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1676 | // Expand the binary operator. |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1677 | Result = (this->*Func)(OpInfo); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1678 | |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1679 | // Convert the result back to the LHS type. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1680 | Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1681 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1682 | // Store the result value into the LHS lvalue. Bit-fields are handled |
| 1683 | // specially because the result is altered by the store, i.e., [C99 6.5.16p1] |
| 1684 | // 'An assignment expression has the value of the left operand after the |
| 1685 | // assignment...'. |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1686 | if (LHSLV.isBitField()) |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1687 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, &Result); |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1688 | else |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1689 | CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV); |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1690 | |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1691 | return LHSLV; |
| 1692 | } |
| 1693 | |
| 1694 | Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, |
| 1695 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { |
| 1696 | bool Ignore = TestAndClearIgnoreResultAssign(); |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1697 | Value *RHS; |
| 1698 | LValue LHS = EmitCompoundAssignLValue(E, Func, RHS); |
| 1699 | |
| 1700 | // If the result is clearly ignored, return now. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1701 | if (Ignore) |
| 1702 | return 0; |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1703 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1704 | // The result of an assignment in C is the assigned r-value. |
| 1705 | if (!CGF.getContext().getLangOptions().CPlusPlus) |
| 1706 | return RHS; |
| 1707 | |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1708 | // If the lvalue is non-volatile, return the computed value of the assignment. |
| 1709 | if (!LHS.isVolatileQualified()) |
| 1710 | return RHS; |
| 1711 | |
| 1712 | // Otherwise, reload the value. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1713 | return EmitLoadOfLValue(LHS); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1716 | void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck( |
| 1717 | const BinOpInfo &Ops, |
| 1718 | llvm::Value *Zero, bool isDiv) { |
Bill Wendling | 14ef319 | 2011-07-07 21:13:10 +0000 | [diff] [blame] | 1719 | llvm::Function::iterator insertPt = Builder.GetInsertBlock(); |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1720 | llvm::BasicBlock *contBB = |
Bill Wendling | 14ef319 | 2011-07-07 21:13:10 +0000 | [diff] [blame] | 1721 | CGF.createBasicBlock(isDiv ? "div.cont" : "rem.cont", CGF.CurFn, |
| 1722 | llvm::next(insertPt)); |
| 1723 | llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn); |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1724 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1725 | llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType()); |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1726 | |
| 1727 | if (Ops.Ty->hasSignedIntegerRepresentation()) { |
| 1728 | llvm::Value *IntMin = |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 1729 | Builder.getInt(llvm::APInt::getSignedMinValue(Ty->getBitWidth())); |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1730 | llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL); |
| 1731 | |
| 1732 | llvm::Value *Cond1 = Builder.CreateICmpEQ(Ops.RHS, Zero); |
| 1733 | llvm::Value *LHSCmp = Builder.CreateICmpEQ(Ops.LHS, IntMin); |
| 1734 | llvm::Value *RHSCmp = Builder.CreateICmpEQ(Ops.RHS, NegOne); |
| 1735 | llvm::Value *Cond2 = Builder.CreateAnd(LHSCmp, RHSCmp, "and"); |
| 1736 | Builder.CreateCondBr(Builder.CreateOr(Cond1, Cond2, "or"), |
| 1737 | overflowBB, contBB); |
| 1738 | } else { |
| 1739 | CGF.Builder.CreateCondBr(Builder.CreateICmpEQ(Ops.RHS, Zero), |
| 1740 | overflowBB, contBB); |
| 1741 | } |
| 1742 | EmitOverflowBB(overflowBB); |
| 1743 | Builder.SetInsertPoint(contBB); |
| 1744 | } |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1746 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1747 | if (isTrapvOverflowBehavior()) { |
| 1748 | llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); |
| 1749 | |
| 1750 | if (Ops.Ty->isIntegerType()) |
| 1751 | EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, true); |
| 1752 | else if (Ops.Ty->isRealFloatingType()) { |
Bill Wendling | 14ef319 | 2011-07-07 21:13:10 +0000 | [diff] [blame] | 1753 | llvm::Function::iterator insertPt = Builder.GetInsertBlock(); |
| 1754 | llvm::BasicBlock *DivCont = CGF.createBasicBlock("div.cont", CGF.CurFn, |
| 1755 | llvm::next(insertPt)); |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1756 | llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", |
| 1757 | CGF.CurFn); |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1758 | CGF.Builder.CreateCondBr(Builder.CreateFCmpOEQ(Ops.RHS, Zero), |
| 1759 | overflowBB, DivCont); |
| 1760 | EmitOverflowBB(overflowBB); |
| 1761 | Builder.SetInsertPoint(DivCont); |
| 1762 | } |
| 1763 | } |
Peter Collingbourne | c5096cb | 2011-10-27 19:19:51 +0000 | [diff] [blame] | 1764 | if (Ops.LHS->getType()->isFPOrFPVectorTy()) { |
| 1765 | llvm::Value *Val = Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
| 1766 | if (CGF.getContext().getLangOptions().OpenCL) { |
| 1767 | // OpenCL 1.1 7.4: minimum accuracy of single precision / is 2.5ulp |
| 1768 | llvm::Type *ValTy = Val->getType(); |
| 1769 | if (ValTy->isFloatTy() || |
| 1770 | (isa<llvm::VectorType>(ValTy) && |
| 1771 | cast<llvm::VectorType>(ValTy)->getElementType()->isFloatTy())) |
| 1772 | CGF.SetFPAccuracy(Val, 5, 2); |
| 1773 | } |
| 1774 | return Val; |
| 1775 | } |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1776 | else if (Ops.Ty->hasUnsignedIntegerRepresentation()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1777 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 1778 | else |
| 1779 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 1780 | } |
| 1781 | |
| 1782 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 1783 | // Rem in C can't be a floating point type: C99 6.5.5p2. |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1784 | if (isTrapvOverflowBehavior()) { |
| 1785 | llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); |
| 1786 | |
| 1787 | if (Ops.Ty->isIntegerType()) |
| 1788 | EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false); |
| 1789 | } |
| 1790 | |
Eli Friedman | 52d6874 | 2011-04-10 04:44:11 +0000 | [diff] [blame] | 1791 | if (Ops.Ty->hasUnsignedIntegerRepresentation()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1792 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 1793 | else |
| 1794 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 1795 | } |
| 1796 | |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1797 | Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) { |
| 1798 | unsigned IID; |
| 1799 | unsigned OpID = 0; |
Mike Stump | 5d8b2cf | 2009-04-02 01:03:55 +0000 | [diff] [blame] | 1800 | |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1801 | switch (Ops.Opcode) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1802 | case BO_Add: |
| 1803 | case BO_AddAssign: |
Mike Stump | 035cf89 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1804 | OpID = 1; |
| 1805 | IID = llvm::Intrinsic::sadd_with_overflow; |
| 1806 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1807 | case BO_Sub: |
| 1808 | case BO_SubAssign: |
Mike Stump | 035cf89 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1809 | OpID = 2; |
| 1810 | IID = llvm::Intrinsic::ssub_with_overflow; |
| 1811 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1812 | case BO_Mul: |
| 1813 | case BO_MulAssign: |
Mike Stump | 035cf89 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1814 | OpID = 3; |
| 1815 | IID = llvm::Intrinsic::smul_with_overflow; |
| 1816 | break; |
| 1817 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1818 | llvm_unreachable("Unsupported operation for overflow detection"); |
Daniel Dunbar | ab4eff6 | 2009-04-08 16:23:09 +0000 | [diff] [blame] | 1819 | IID = 0; |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1820 | } |
Mike Stump | 035cf89 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1821 | OpID <<= 1; |
| 1822 | OpID |= 1; |
| 1823 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1824 | llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty); |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1825 | |
Benjamin Kramer | 8dd55a3 | 2011-07-14 17:45:50 +0000 | [diff] [blame] | 1826 | llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, opTy); |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1827 | |
| 1828 | Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS); |
| 1829 | Value *result = Builder.CreateExtractValue(resultAndOverflow, 0); |
| 1830 | Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1); |
| 1831 | |
| 1832 | // Branch in case of overflow. |
David Chisnall | 7f18e67 | 2010-09-17 18:29:54 +0000 | [diff] [blame] | 1833 | llvm::BasicBlock *initialBB = Builder.GetInsertBlock(); |
Bill Wendling | 14ef319 | 2011-07-07 21:13:10 +0000 | [diff] [blame] | 1834 | llvm::Function::iterator insertPt = initialBB; |
| 1835 | llvm::BasicBlock *continueBB = CGF.createBasicBlock("nooverflow", CGF.CurFn, |
| 1836 | llvm::next(insertPt)); |
Chris Lattner | 93a0035 | 2010-08-07 00:20:46 +0000 | [diff] [blame] | 1837 | llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn); |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1838 | |
| 1839 | Builder.CreateCondBr(overflow, overflowBB, continueBB); |
| 1840 | |
Chris Lattner | 93a0035 | 2010-08-07 00:20:46 +0000 | [diff] [blame] | 1841 | // Handle overflow with llvm.trap. |
David Chisnall | 7f18e67 | 2010-09-17 18:29:54 +0000 | [diff] [blame] | 1842 | const std::string *handlerName = |
| 1843 | &CGF.getContext().getLangOptions().OverflowHandler; |
| 1844 | if (handlerName->empty()) { |
| 1845 | EmitOverflowBB(overflowBB); |
| 1846 | Builder.SetInsertPoint(continueBB); |
| 1847 | return result; |
| 1848 | } |
| 1849 | |
| 1850 | // If an overflow handler is set, then we want to call it and then use its |
| 1851 | // result, if it returns. |
| 1852 | Builder.SetInsertPoint(overflowBB); |
| 1853 | |
| 1854 | // Get the overflow handler. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1855 | llvm::Type *Int8Ty = llvm::Type::getInt8Ty(VMContext); |
| 1856 | llvm::Type *argTypes[] = { CGF.Int64Ty, CGF.Int64Ty, Int8Ty, Int8Ty }; |
David Chisnall | 7f18e67 | 2010-09-17 18:29:54 +0000 | [diff] [blame] | 1857 | llvm::FunctionType *handlerTy = |
| 1858 | llvm::FunctionType::get(CGF.Int64Ty, argTypes, true); |
| 1859 | llvm::Value *handler = CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName); |
| 1860 | |
| 1861 | // Sign extend the args to 64-bit, so that we can use the same handler for |
| 1862 | // all types of overflow. |
| 1863 | llvm::Value *lhs = Builder.CreateSExt(Ops.LHS, CGF.Int64Ty); |
| 1864 | llvm::Value *rhs = Builder.CreateSExt(Ops.RHS, CGF.Int64Ty); |
| 1865 | |
| 1866 | // Call the handler with the two arguments, the operation, and the size of |
| 1867 | // the result. |
| 1868 | llvm::Value *handlerResult = Builder.CreateCall4(handler, lhs, rhs, |
| 1869 | Builder.getInt8(OpID), |
| 1870 | Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth())); |
| 1871 | |
| 1872 | // Truncate the result back to the desired size. |
| 1873 | handlerResult = Builder.CreateTrunc(handlerResult, opTy); |
| 1874 | Builder.CreateBr(continueBB); |
| 1875 | |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1876 | Builder.SetInsertPoint(continueBB); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1877 | llvm::PHINode *phi = Builder.CreatePHI(opTy, 2); |
David Chisnall | 7f18e67 | 2010-09-17 18:29:54 +0000 | [diff] [blame] | 1878 | phi->addIncoming(result, initialBB); |
| 1879 | phi->addIncoming(handlerResult, overflowBB); |
| 1880 | |
| 1881 | return phi; |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1882 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1883 | |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 1884 | /// Emit pointer + index arithmetic. |
| 1885 | static Value *emitPointerArithmetic(CodeGenFunction &CGF, |
| 1886 | const BinOpInfo &op, |
| 1887 | bool isSubtraction) { |
| 1888 | // Must have binary (not unary) expr here. Unary pointer |
| 1889 | // increment/decrement doesn't use this path. |
| 1890 | const BinaryOperator *expr = cast<BinaryOperator>(op.E); |
| 1891 | |
| 1892 | Value *pointer = op.LHS; |
| 1893 | Expr *pointerOperand = expr->getLHS(); |
| 1894 | Value *index = op.RHS; |
| 1895 | Expr *indexOperand = expr->getRHS(); |
| 1896 | |
| 1897 | // In a subtraction, the LHS is always the pointer. |
| 1898 | if (!isSubtraction && !pointer->getType()->isPointerTy()) { |
| 1899 | std::swap(pointer, index); |
| 1900 | std::swap(pointerOperand, indexOperand); |
| 1901 | } |
| 1902 | |
| 1903 | unsigned width = cast<llvm::IntegerType>(index->getType())->getBitWidth(); |
| 1904 | if (width != CGF.PointerWidthInBits) { |
| 1905 | // Zero-extend or sign-extend the pointer value according to |
| 1906 | // whether the index is signed or not. |
| 1907 | bool isSigned = indexOperand->getType()->isSignedIntegerOrEnumerationType(); |
| 1908 | index = CGF.Builder.CreateIntCast(index, CGF.PtrDiffTy, isSigned, |
| 1909 | "idx.ext"); |
| 1910 | } |
| 1911 | |
| 1912 | // If this is subtraction, negate the index. |
| 1913 | if (isSubtraction) |
| 1914 | index = CGF.Builder.CreateNeg(index, "idx.neg"); |
| 1915 | |
| 1916 | const PointerType *pointerType |
| 1917 | = pointerOperand->getType()->getAs<PointerType>(); |
| 1918 | if (!pointerType) { |
| 1919 | QualType objectType = pointerOperand->getType() |
| 1920 | ->castAs<ObjCObjectPointerType>() |
| 1921 | ->getPointeeType(); |
| 1922 | llvm::Value *objectSize |
| 1923 | = CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(objectType)); |
| 1924 | |
| 1925 | index = CGF.Builder.CreateMul(index, objectSize); |
| 1926 | |
| 1927 | Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy); |
| 1928 | result = CGF.Builder.CreateGEP(result, index, "add.ptr"); |
| 1929 | return CGF.Builder.CreateBitCast(result, pointer->getType()); |
| 1930 | } |
| 1931 | |
| 1932 | QualType elementType = pointerType->getPointeeType(); |
| 1933 | if (const VariableArrayType *vla |
| 1934 | = CGF.getContext().getAsVariableArrayType(elementType)) { |
| 1935 | // The element count here is the total number of non-VLA elements. |
| 1936 | llvm::Value *numElements = CGF.getVLASize(vla).first; |
| 1937 | |
| 1938 | // Effectively, the multiply by the VLA size is part of the GEP. |
| 1939 | // GEP indexes are signed, and scaling an index isn't permitted to |
| 1940 | // signed-overflow, so we use the same semantics for our explicit |
| 1941 | // multiply. We suppress this if overflow is not undefined behavior. |
| 1942 | if (CGF.getLangOptions().isSignedOverflowDefined()) { |
| 1943 | index = CGF.Builder.CreateMul(index, numElements, "vla.index"); |
| 1944 | pointer = CGF.Builder.CreateGEP(pointer, index, "add.ptr"); |
| 1945 | } else { |
| 1946 | index = CGF.Builder.CreateNSWMul(index, numElements, "vla.index"); |
| 1947 | pointer = CGF.Builder.CreateInBoundsGEP(pointer, index, "add.ptr"); |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 1948 | } |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 1949 | return pointer; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1950 | } |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1951 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1952 | // Explicitly handle GNU void* and function pointer arithmetic extensions. The |
| 1953 | // GNU void* casts amount to no-ops since our void* type is i8*, but this is |
| 1954 | // future proof. |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 1955 | if (elementType->isVoidType() || elementType->isFunctionType()) { |
| 1956 | Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy); |
| 1957 | result = CGF.Builder.CreateGEP(result, index, "add.ptr"); |
| 1958 | return CGF.Builder.CreateBitCast(result, pointer->getType()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 1961 | if (CGF.getLangOptions().isSignedOverflowDefined()) |
| 1962 | return CGF.Builder.CreateGEP(pointer, index, "add.ptr"); |
| 1963 | |
| 1964 | return CGF.Builder.CreateInBoundsGEP(pointer, index, "add.ptr"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 1967 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &op) { |
| 1968 | if (op.LHS->getType()->isPointerTy() || |
| 1969 | op.RHS->getType()->isPointerTy()) |
| 1970 | return emitPointerArithmetic(CGF, op, /*subtraction*/ false); |
| 1971 | |
| 1972 | if (op.Ty->isSignedIntegerOrEnumerationType()) { |
| 1973 | switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { |
| 1974 | case LangOptions::SOB_Undefined: |
| 1975 | return Builder.CreateNSWAdd(op.LHS, op.RHS, "add"); |
| 1976 | case LangOptions::SOB_Defined: |
| 1977 | return Builder.CreateAdd(op.LHS, op.RHS, "add"); |
| 1978 | case LangOptions::SOB_Trapping: |
| 1979 | return EmitOverflowCheckedBinOp(op); |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | if (op.LHS->getType()->isFPOrFPVectorTy()) |
| 1984 | return Builder.CreateFAdd(op.LHS, op.RHS, "add"); |
| 1985 | |
| 1986 | return Builder.CreateAdd(op.LHS, op.RHS, "add"); |
| 1987 | } |
| 1988 | |
| 1989 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &op) { |
| 1990 | // The LHS is always a pointer if either side is. |
| 1991 | if (!op.LHS->getType()->isPointerTy()) { |
| 1992 | if (op.Ty->isSignedIntegerOrEnumerationType()) { |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 1993 | switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { |
| 1994 | case LangOptions::SOB_Undefined: |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 1995 | return Builder.CreateNSWSub(op.LHS, op.RHS, "sub"); |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 1996 | case LangOptions::SOB_Defined: |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 1997 | return Builder.CreateSub(op.LHS, op.RHS, "sub"); |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 1998 | case LangOptions::SOB_Trapping: |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 1999 | return EmitOverflowCheckedBinOp(op); |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 2000 | } |
| 2001 | } |
| 2002 | |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2003 | if (op.LHS->getType()->isFPOrFPVectorTy()) |
| 2004 | return Builder.CreateFSub(op.LHS, op.RHS, "sub"); |
Chris Lattner | 2eb91e4 | 2010-03-29 17:28:16 +0000 | [diff] [blame] | 2005 | |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2006 | return Builder.CreateSub(op.LHS, op.RHS, "sub"); |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 2007 | } |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 2008 | |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2009 | // If the RHS is not a pointer, then we have normal pointer |
| 2010 | // arithmetic. |
| 2011 | if (!op.RHS->getType()->isPointerTy()) |
| 2012 | return emitPointerArithmetic(CGF, op, /*subtraction*/ true); |
Eli Friedman | daa24a2 | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 2013 | |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2014 | // Otherwise, this is a pointer subtraction. |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 2015 | |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2016 | // Do the raw subtraction part. |
| 2017 | llvm::Value *LHS |
| 2018 | = Builder.CreatePtrToInt(op.LHS, CGF.PtrDiffTy, "sub.ptr.lhs.cast"); |
| 2019 | llvm::Value *RHS |
| 2020 | = Builder.CreatePtrToInt(op.RHS, CGF.PtrDiffTy, "sub.ptr.rhs.cast"); |
| 2021 | Value *diffInChars = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 2022 | |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2023 | // Okay, figure out the element size. |
| 2024 | const BinaryOperator *expr = cast<BinaryOperator>(op.E); |
| 2025 | QualType elementType = expr->getLHS()->getType()->getPointeeType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2026 | |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2027 | llvm::Value *divisor = 0; |
| 2028 | |
| 2029 | // For a variable-length array, this is going to be non-constant. |
| 2030 | if (const VariableArrayType *vla |
| 2031 | = CGF.getContext().getAsVariableArrayType(elementType)) { |
| 2032 | llvm::Value *numElements; |
| 2033 | llvm::tie(numElements, elementType) = CGF.getVLASize(vla); |
| 2034 | |
| 2035 | divisor = numElements; |
| 2036 | |
| 2037 | // Scale the number of non-VLA elements by the non-VLA element size. |
| 2038 | CharUnits eltSize = CGF.getContext().getTypeSizeInChars(elementType); |
| 2039 | if (!eltSize.isOne()) |
| 2040 | divisor = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), divisor); |
| 2041 | |
| 2042 | // For everything elese, we can just compute it, safe in the |
| 2043 | // assumption that Sema won't let anything through that we can't |
| 2044 | // safely compute the size of. |
| 2045 | } else { |
| 2046 | CharUnits elementSize; |
| 2047 | // Handle GCC extension for pointer arithmetic on void* and |
| 2048 | // function pointer types. |
| 2049 | if (elementType->isVoidType() || elementType->isFunctionType()) |
| 2050 | elementSize = CharUnits::One(); |
| 2051 | else |
| 2052 | elementSize = CGF.getContext().getTypeSizeInChars(elementType); |
| 2053 | |
| 2054 | // Don't even emit the divide for element size of 1. |
| 2055 | if (elementSize.isOne()) |
| 2056 | return diffInChars; |
| 2057 | |
| 2058 | divisor = CGF.CGM.getSize(elementSize); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2059 | } |
Chris Lattner | 2cb4222 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 2060 | |
Chris Lattner | 2cb4222 | 2011-03-01 00:03:48 +0000 | [diff] [blame] | 2061 | // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since |
| 2062 | // pointer difference in C is only defined in the case where both operands |
| 2063 | // are pointing to elements of an array. |
John McCall | 913dab2 | 2011-06-25 01:32:37 +0000 | [diff] [blame] | 2064 | return Builder.CreateExactSDiv(diffInChars, divisor, "sub.ptr.div"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 2068 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 2069 | // RHS to the same size as the LHS. |
| 2070 | Value *RHS = Ops.RHS; |
| 2071 | if (Ops.LHS->getType() != RHS->getType()) |
| 2072 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2073 | |
Mike Stump | be07f60 | 2009-12-14 21:58:14 +0000 | [diff] [blame] | 2074 | if (CGF.CatchUndefined |
| 2075 | && isa<llvm::IntegerType>(Ops.LHS->getType())) { |
| 2076 | unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth(); |
| 2077 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 2078 | CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS, |
| 2079 | llvm::ConstantInt::get(RHS->getType(), Width)), |
Mike Stump | 15037ca | 2009-12-15 00:35:12 +0000 | [diff] [blame] | 2080 | Cont, CGF.getTrapBB()); |
Mike Stump | be07f60 | 2009-12-14 21:58:14 +0000 | [diff] [blame] | 2081 | CGF.EmitBlock(Cont); |
| 2082 | } |
| 2083 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2084 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 2085 | } |
| 2086 | |
| 2087 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 2088 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 2089 | // RHS to the same size as the LHS. |
| 2090 | Value *RHS = Ops.RHS; |
| 2091 | if (Ops.LHS->getType() != RHS->getType()) |
| 2092 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2093 | |
Mike Stump | be07f60 | 2009-12-14 21:58:14 +0000 | [diff] [blame] | 2094 | if (CGF.CatchUndefined |
| 2095 | && isa<llvm::IntegerType>(Ops.LHS->getType())) { |
| 2096 | unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth(); |
| 2097 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 2098 | CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS, |
| 2099 | llvm::ConstantInt::get(RHS->getType(), Width)), |
Mike Stump | 15037ca | 2009-12-15 00:35:12 +0000 | [diff] [blame] | 2100 | Cont, CGF.getTrapBB()); |
Mike Stump | be07f60 | 2009-12-14 21:58:14 +0000 | [diff] [blame] | 2101 | CGF.EmitBlock(Cont); |
| 2102 | } |
| 2103 | |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2104 | if (Ops.Ty->hasUnsignedIntegerRepresentation()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2105 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 2106 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 2107 | } |
| 2108 | |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 2109 | enum IntrinsicType { VCMPEQ, VCMPGT }; |
| 2110 | // return corresponding comparison intrinsic for given vector type |
| 2111 | static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT, |
| 2112 | BuiltinType::Kind ElemKind) { |
| 2113 | switch (ElemKind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2114 | default: llvm_unreachable("unexpected element type"); |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 2115 | case BuiltinType::Char_U: |
| 2116 | case BuiltinType::UChar: |
| 2117 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p : |
| 2118 | llvm::Intrinsic::ppc_altivec_vcmpgtub_p; |
| 2119 | break; |
| 2120 | case BuiltinType::Char_S: |
| 2121 | case BuiltinType::SChar: |
| 2122 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p : |
| 2123 | llvm::Intrinsic::ppc_altivec_vcmpgtsb_p; |
| 2124 | break; |
| 2125 | case BuiltinType::UShort: |
| 2126 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p : |
| 2127 | llvm::Intrinsic::ppc_altivec_vcmpgtuh_p; |
| 2128 | break; |
| 2129 | case BuiltinType::Short: |
| 2130 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p : |
| 2131 | llvm::Intrinsic::ppc_altivec_vcmpgtsh_p; |
| 2132 | break; |
| 2133 | case BuiltinType::UInt: |
| 2134 | case BuiltinType::ULong: |
| 2135 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p : |
| 2136 | llvm::Intrinsic::ppc_altivec_vcmpgtuw_p; |
| 2137 | break; |
| 2138 | case BuiltinType::Int: |
| 2139 | case BuiltinType::Long: |
| 2140 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p : |
| 2141 | llvm::Intrinsic::ppc_altivec_vcmpgtsw_p; |
| 2142 | break; |
| 2143 | case BuiltinType::Float: |
| 2144 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpeqfp_p : |
| 2145 | llvm::Intrinsic::ppc_altivec_vcmpgtfp_p; |
| 2146 | break; |
| 2147 | } |
| 2148 | return llvm::Intrinsic::not_intrinsic; |
| 2149 | } |
| 2150 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2151 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 2152 | unsigned SICmpOpc, unsigned FCmpOpc) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2153 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2154 | Value *Result; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2155 | QualType LHSTy = E->getLHS()->getType(); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 2156 | if (const MemberPointerType *MPT = LHSTy->getAs<MemberPointerType>()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2157 | assert(E->getOpcode() == BO_EQ || |
| 2158 | E->getOpcode() == BO_NE); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2159 | Value *LHS = CGF.EmitScalarExpr(E->getLHS()); |
| 2160 | Value *RHS = CGF.EmitScalarExpr(E->getRHS()); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 2161 | Result = CGF.CGM.getCXXABI().EmitMemberPointerComparison( |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2162 | CGF, LHS, RHS, MPT, E->getOpcode() == BO_NE); |
Eli Friedman | b81c786 | 2009-12-11 07:36:43 +0000 | [diff] [blame] | 2163 | } else if (!LHSTy->isAnyComplexType()) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2164 | Value *LHS = Visit(E->getLHS()); |
| 2165 | Value *RHS = Visit(E->getRHS()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2166 | |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 2167 | // If AltiVec, the comparison results in a numeric type, so we use |
| 2168 | // intrinsics comparing vectors and giving 0 or 1 as a result |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 2169 | if (LHSTy->isVectorType() && !E->getType()->isVectorType()) { |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 2170 | // constants for mapping CR6 register bits to predicate result |
| 2171 | enum { CR6_EQ=0, CR6_EQ_REV, CR6_LT, CR6_LT_REV } CR6; |
| 2172 | |
| 2173 | llvm::Intrinsic::ID ID = llvm::Intrinsic::not_intrinsic; |
| 2174 | |
| 2175 | // in several cases vector arguments order will be reversed |
| 2176 | Value *FirstVecArg = LHS, |
| 2177 | *SecondVecArg = RHS; |
| 2178 | |
| 2179 | QualType ElTy = LHSTy->getAs<VectorType>()->getElementType(); |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2180 | const BuiltinType *BTy = ElTy->getAs<BuiltinType>(); |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 2181 | BuiltinType::Kind ElementKind = BTy->getKind(); |
| 2182 | |
| 2183 | switch(E->getOpcode()) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2184 | default: llvm_unreachable("is not a comparison operation"); |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 2185 | case BO_EQ: |
| 2186 | CR6 = CR6_LT; |
| 2187 | ID = GetIntrinsic(VCMPEQ, ElementKind); |
| 2188 | break; |
| 2189 | case BO_NE: |
| 2190 | CR6 = CR6_EQ; |
| 2191 | ID = GetIntrinsic(VCMPEQ, ElementKind); |
| 2192 | break; |
| 2193 | case BO_LT: |
| 2194 | CR6 = CR6_LT; |
| 2195 | ID = GetIntrinsic(VCMPGT, ElementKind); |
| 2196 | std::swap(FirstVecArg, SecondVecArg); |
| 2197 | break; |
| 2198 | case BO_GT: |
| 2199 | CR6 = CR6_LT; |
| 2200 | ID = GetIntrinsic(VCMPGT, ElementKind); |
| 2201 | break; |
| 2202 | case BO_LE: |
| 2203 | if (ElementKind == BuiltinType::Float) { |
| 2204 | CR6 = CR6_LT; |
| 2205 | ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p; |
| 2206 | std::swap(FirstVecArg, SecondVecArg); |
| 2207 | } |
| 2208 | else { |
| 2209 | CR6 = CR6_EQ; |
| 2210 | ID = GetIntrinsic(VCMPGT, ElementKind); |
| 2211 | } |
| 2212 | break; |
| 2213 | case BO_GE: |
| 2214 | if (ElementKind == BuiltinType::Float) { |
| 2215 | CR6 = CR6_LT; |
| 2216 | ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p; |
| 2217 | } |
| 2218 | else { |
| 2219 | CR6 = CR6_EQ; |
| 2220 | ID = GetIntrinsic(VCMPGT, ElementKind); |
| 2221 | std::swap(FirstVecArg, SecondVecArg); |
| 2222 | } |
| 2223 | break; |
| 2224 | } |
| 2225 | |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 2226 | Value *CR6Param = Builder.getInt32(CR6); |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 2227 | llvm::Function *F = CGF.CGM.getIntrinsic(ID); |
| 2228 | Result = Builder.CreateCall3(F, CR6Param, FirstVecArg, SecondVecArg, ""); |
| 2229 | return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); |
| 2230 | } |
| 2231 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 2232 | if (LHS->getType()->isFPOrFPVectorTy()) { |
Nate Begeman | 7a66d7b | 2008-07-25 20:16:05 +0000 | [diff] [blame] | 2233 | Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2234 | LHS, RHS, "cmp"); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2235 | } else if (LHSTy->hasSignedIntegerRepresentation()) { |
Eli Friedman | ec2c126 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 2236 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2237 | LHS, RHS, "cmp"); |
| 2238 | } else { |
Eli Friedman | ec2c126 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 2239 | // Unsigned integers and pointers. |
| 2240 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2241 | LHS, RHS, "cmp"); |
| 2242 | } |
Chris Lattner | 9c10fcf | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 2243 | |
| 2244 | // If this is a vector comparison, sign extend the result to the appropriate |
| 2245 | // vector integer type and return it (don't convert to bool). |
| 2246 | if (LHSTy->isVectorType()) |
| 2247 | return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2248 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2249 | } else { |
| 2250 | // Complex Comparison: can only be an equality comparison. |
| 2251 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 2252 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2253 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2254 | QualType CETy = LHSTy->getAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2255 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2256 | Value *ResultR, *ResultI; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2257 | if (CETy->isRealFloatingType()) { |
| 2258 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 2259 | LHS.first, RHS.first, "cmp.r"); |
| 2260 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 2261 | LHS.second, RHS.second, "cmp.i"); |
| 2262 | } else { |
| 2263 | // Complex comparisons can only be equality comparisons. As such, signed |
| 2264 | // and unsigned opcodes are the same. |
| 2265 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 2266 | LHS.first, RHS.first, "cmp.r"); |
| 2267 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 2268 | LHS.second, RHS.second, "cmp.i"); |
| 2269 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2270 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2271 | if (E->getOpcode() == BO_EQ) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2272 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 2273 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2274 | assert(E->getOpcode() == BO_NE && |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2275 | "Complex comparison other than == or != ?"); |
| 2276 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 2277 | } |
| 2278 | } |
Nuno Lopes | 32f6209 | 2009-01-11 23:22:37 +0000 | [diff] [blame] | 2279 | |
| 2280 | return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2284 | bool Ignore = TestAndClearIgnoreResultAssign(); |
| 2285 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2286 | Value *RHS; |
| 2287 | LValue LHS; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2288 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2289 | switch (E->getLHS()->getType().getObjCLifetime()) { |
| 2290 | case Qualifiers::OCL_Strong: |
| 2291 | llvm::tie(LHS, RHS) = CGF.EmitARCStoreStrong(E, Ignore); |
| 2292 | break; |
| 2293 | |
| 2294 | case Qualifiers::OCL_Autoreleasing: |
| 2295 | llvm::tie(LHS,RHS) = CGF.EmitARCStoreAutoreleasing(E); |
| 2296 | break; |
| 2297 | |
| 2298 | case Qualifiers::OCL_Weak: |
| 2299 | RHS = Visit(E->getRHS()); |
| 2300 | LHS = EmitCheckedLValue(E->getLHS()); |
| 2301 | RHS = CGF.EmitARCStoreWeak(LHS.getAddress(), RHS, Ignore); |
| 2302 | break; |
| 2303 | |
| 2304 | // No reason to do any of these differently. |
| 2305 | case Qualifiers::OCL_None: |
| 2306 | case Qualifiers::OCL_ExplicitNone: |
| 2307 | // __block variables need to have the rhs evaluated first, plus |
| 2308 | // this should improve codegen just a little. |
| 2309 | RHS = Visit(E->getRHS()); |
| 2310 | LHS = EmitCheckedLValue(E->getLHS()); |
| 2311 | |
| 2312 | // Store the value into the LHS. Bit-fields are handled specially |
| 2313 | // because the result is altered by the store, i.e., [C99 6.5.16p1] |
| 2314 | // 'An assignment expression has the value of the left operand after |
| 2315 | // the assignment...'. |
| 2316 | if (LHS.isBitField()) |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2317 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, &RHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2318 | else |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2319 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2320 | } |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2321 | |
| 2322 | // If the result is clearly ignored, return now. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2323 | if (Ignore) |
| 2324 | return 0; |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2325 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 2326 | // The result of an assignment in C is the assigned r-value. |
| 2327 | if (!CGF.getContext().getLangOptions().CPlusPlus) |
| 2328 | return RHS; |
| 2329 | |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2330 | // If the lvalue is non-volatile, return the computed value of the assignment. |
| 2331 | if (!LHS.isVolatileQualified()) |
| 2332 | return RHS; |
| 2333 | |
| 2334 | // Otherwise, reload the value. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2335 | return EmitLoadOfLValue(LHS); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2339 | llvm::Type *ResTy = ConvertType(E->getType()); |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2340 | |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 2341 | // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. |
| 2342 | // If we have 1 && X, just emit X without inserting the control flow. |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 2343 | bool LHSCondVal; |
| 2344 | if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) { |
| 2345 | if (LHSCondVal) { // If we have 1 && X, just emit X. |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2346 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2347 | // ZExt result to int or bool. |
| 2348 | return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext"); |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2349 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2350 | |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2351 | // 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] | 2352 | if (!CGF.ContainsLabel(E->getRHS())) |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2353 | return llvm::Constant::getNullValue(ResTy); |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2354 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2355 | |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 2356 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); |
| 2357 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 2358 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2359 | CodeGenFunction::ConditionalEvaluation eval(CGF); |
| 2360 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2361 | // Branch on the LHS first. If it is false, go to the failure (cont) block. |
| 2362 | CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock); |
| 2363 | |
| 2364 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 2365 | // edges from this first condition. All of these values will be false. Start |
| 2366 | // setting up the PHI node in the Cont Block for this. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2367 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2368 | "", ContBlock); |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2369 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 2370 | PI != PE; ++PI) |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 2371 | PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2372 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2373 | eval.begin(CGF); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2374 | CGF.EmitBlock(RHSBlock); |
| 2375 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2376 | eval.end(CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2377 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2378 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 2379 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2380 | |
| 2381 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 2382 | // into the phi node for the edge with the value of RHSCond. |
Devang Patel | acd7236 | 2011-03-30 00:08:31 +0000 | [diff] [blame] | 2383 | if (CGF.getDebugInfo()) |
| 2384 | // There is no need to emit line number for unconditional branch. |
| 2385 | Builder.SetCurrentDebugLocation(llvm::DebugLoc()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2386 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2387 | PN->addIncoming(RHSCond, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2388 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2389 | // ZExt result to int. |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2390 | return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2394 | llvm::Type *ResTy = ConvertType(E->getType()); |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2395 | |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 2396 | // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. |
| 2397 | // If we have 0 || X, just emit X without inserting the control flow. |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 2398 | bool LHSCondVal; |
| 2399 | if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) { |
| 2400 | if (!LHSCondVal) { // If we have 0 || X, just emit X. |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2401 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2402 | // ZExt result to int or bool. |
| 2403 | return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext"); |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2404 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2405 | |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2406 | // 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] | 2407 | if (!CGF.ContainsLabel(E->getRHS())) |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2408 | return llvm::ConstantInt::get(ResTy, 1); |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2409 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2410 | |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 2411 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); |
| 2412 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2413 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2414 | CodeGenFunction::ConditionalEvaluation eval(CGF); |
| 2415 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2416 | // Branch on the LHS first. If it is true, go to the success (cont) block. |
| 2417 | CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock); |
| 2418 | |
| 2419 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 2420 | // edges from this first condition. All of these values will be true. Start |
| 2421 | // setting up the PHI node in the Cont Block for this. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2422 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2423 | "", ContBlock); |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2424 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 2425 | PI != PE; ++PI) |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 2426 | PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI); |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2427 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2428 | eval.begin(CGF); |
Anders Carlsson | 33da07d | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 2429 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2430 | // Emit the RHS condition as a bool value. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2431 | CGF.EmitBlock(RHSBlock); |
| 2432 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2433 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2434 | eval.end(CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2435 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2436 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 2437 | RHSBlock = Builder.GetInsertBlock(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2438 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2439 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 2440 | // into the phi node for the edge with the value of RHSCond. |
| 2441 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2442 | PN->addIncoming(RHSCond, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2443 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2444 | // ZExt result to int. |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2445 | return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2449 | CGF.EmitIgnoredExpr(E->getLHS()); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2450 | CGF.EnsureInsertPoint(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2451 | return Visit(E->getRHS()); |
| 2452 | } |
| 2453 | |
| 2454 | //===----------------------------------------------------------------------===// |
| 2455 | // Other Operators |
| 2456 | //===----------------------------------------------------------------------===// |
| 2457 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2458 | /// isCheapEnoughToEvaluateUnconditionally - Return true if the specified |
| 2459 | /// expression is cheap enough and side-effect-free enough to evaluate |
| 2460 | /// unconditionally instead of conditionally. This is used to convert control |
| 2461 | /// flow into selects in some cases. |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 2462 | static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E, |
| 2463 | CodeGenFunction &CGF) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2464 | E = E->IgnoreParens(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2465 | |
Chris Lattner | c6bea67 | 2011-04-16 23:15:35 +0000 | [diff] [blame] | 2466 | // Anything that is an integer or floating point constant is fine. |
| 2467 | if (E->isConstantInitializer(CGF.getContext(), false)) |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2468 | return true; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2469 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2470 | // Non-volatile automatic variables too, to get "cond ? X : Y" where |
| 2471 | // X and Y are local variables. |
| 2472 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2473 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 2474 | if (VD->hasLocalStorage() && !(CGF.getContext() |
| 2475 | .getCanonicalType(VD->getType()) |
| 2476 | .isVolatileQualified())) |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2477 | return true; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2478 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2479 | return false; |
| 2480 | } |
| 2481 | |
| 2482 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2483 | Value *ScalarExprEmitter:: |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2484 | VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2485 | TestAndClearIgnoreResultAssign(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2486 | |
| 2487 | // Bind the common expression if necessary. |
| 2488 | CodeGenFunction::OpaqueValueMapping binding(CGF, E); |
| 2489 | |
| 2490 | Expr *condExpr = E->getCond(); |
| 2491 | Expr *lhsExpr = E->getTrueExpr(); |
| 2492 | Expr *rhsExpr = E->getFalseExpr(); |
| 2493 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 2494 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2495 | // the condition and the dead arm. |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 2496 | bool CondExprBool; |
| 2497 | if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2498 | Expr *live = lhsExpr, *dead = rhsExpr; |
Chris Lattner | c2c9001 | 2011-02-27 23:02:32 +0000 | [diff] [blame] | 2499 | if (!CondExprBool) std::swap(live, dead); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2500 | |
Eli Friedman | c8645e3 | 2011-10-15 02:10:40 +0000 | [diff] [blame] | 2501 | // If the dead side doesn't have labels we need, just emit the Live part. |
| 2502 | if (!CGF.ContainsLabel(dead)) { |
| 2503 | Value *Result = Visit(live); |
| 2504 | |
| 2505 | // If the live part is a throw expression, it acts like it has a void |
| 2506 | // type, so evaluating it returns a null Value*. However, a conditional |
| 2507 | // with non-void type must return a non-null Value*. |
| 2508 | if (!Result && !E->getType()->isVoidType()) |
| 2509 | Result = llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
| 2510 | |
| 2511 | return Result; |
| 2512 | } |
Chris Lattner | c657e92 | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 2513 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2514 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 2515 | // OpenCL: If the condition is a vector, we can treat this condition like |
| 2516 | // the select function. |
| 2517 | if (CGF.getContext().getLangOptions().OpenCL |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2518 | && condExpr->getType()->isVectorType()) { |
| 2519 | llvm::Value *CondV = CGF.EmitScalarExpr(condExpr); |
| 2520 | llvm::Value *LHS = Visit(lhsExpr); |
| 2521 | llvm::Value *RHS = Visit(rhsExpr); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 2522 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2523 | llvm::Type *condType = ConvertType(condExpr->getType()); |
| 2524 | llvm::VectorType *vecTy = cast<llvm::VectorType>(condType); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 2525 | |
| 2526 | unsigned numElem = vecTy->getNumElements(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2527 | llvm::Type *elemType = vecTy->getElementType(); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 2528 | |
| 2529 | std::vector<llvm::Constant*> Zvals; |
| 2530 | for (unsigned i = 0; i < numElem; ++i) |
Chris Lattner | 48431f9 | 2011-04-19 22:55:03 +0000 | [diff] [blame] | 2531 | Zvals.push_back(llvm::ConstantInt::get(elemType, 0)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2532 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 2533 | llvm::Value *zeroVec = llvm::ConstantVector::get(Zvals); |
| 2534 | llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec); |
| 2535 | llvm::Value *tmp = Builder.CreateSExt(TestMSB, |
| 2536 | llvm::VectorType::get(elemType, |
| 2537 | numElem), |
| 2538 | "sext"); |
| 2539 | llvm::Value *tmp2 = Builder.CreateNot(tmp); |
| 2540 | |
| 2541 | // Cast float to int to perform ANDs if necessary. |
| 2542 | llvm::Value *RHSTmp = RHS; |
| 2543 | llvm::Value *LHSTmp = LHS; |
| 2544 | bool wasCast = false; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2545 | llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType()); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 2546 | if (rhsVTy->getElementType()->isFloatTy()) { |
| 2547 | RHSTmp = Builder.CreateBitCast(RHS, tmp2->getType()); |
| 2548 | LHSTmp = Builder.CreateBitCast(LHS, tmp->getType()); |
| 2549 | wasCast = true; |
| 2550 | } |
| 2551 | |
| 2552 | llvm::Value *tmp3 = Builder.CreateAnd(RHSTmp, tmp2); |
| 2553 | llvm::Value *tmp4 = Builder.CreateAnd(LHSTmp, tmp); |
| 2554 | llvm::Value *tmp5 = Builder.CreateOr(tmp3, tmp4, "cond"); |
| 2555 | if (wasCast) |
| 2556 | tmp5 = Builder.CreateBitCast(tmp5, RHS->getType()); |
| 2557 | |
| 2558 | return tmp5; |
| 2559 | } |
| 2560 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2561 | // If this is a really simple expression (like x ? 4 : 5), emit this as a |
| 2562 | // 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] | 2563 | // safe to evaluate the LHS and RHS unconditionally. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2564 | if (isCheapEnoughToEvaluateUnconditionally(lhsExpr, CGF) && |
| 2565 | isCheapEnoughToEvaluateUnconditionally(rhsExpr, CGF)) { |
| 2566 | llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr); |
| 2567 | llvm::Value *LHS = Visit(lhsExpr); |
| 2568 | llvm::Value *RHS = Visit(rhsExpr); |
Eli Friedman | 1e4f68c | 2011-12-08 22:01:56 +0000 | [diff] [blame^] | 2569 | if (!LHS) { |
| 2570 | // If the conditional has void type, make sure we return a null Value*. |
| 2571 | assert(!RHS && "LHS and RHS types must match"); |
| 2572 | return 0; |
| 2573 | } |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2574 | return Builder.CreateSelect(CondV, LHS, RHS, "cond"); |
| 2575 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2576 | |
Daniel Dunbar | be65abc | 2008-11-12 10:13:37 +0000 | [diff] [blame] | 2577 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 2578 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 2579 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2580 | |
| 2581 | CodeGenFunction::ConditionalEvaluation eval(CGF); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2582 | CGF.EmitBranchOnBoolExpr(condExpr, LHSBlock, RHSBlock); |
Anders Carlsson | fb6fa30 | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 2583 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2584 | CGF.EmitBlock(LHSBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2585 | eval.begin(CGF); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2586 | Value *LHS = Visit(lhsExpr); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2587 | eval.end(CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2588 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2589 | LHSBlock = Builder.GetInsertBlock(); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2590 | Builder.CreateBr(ContBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2591 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2592 | CGF.EmitBlock(RHSBlock); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2593 | eval.begin(CGF); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2594 | Value *RHS = Visit(rhsExpr); |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2595 | eval.end(CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2596 | |
John McCall | 150b462 | 2011-01-26 04:00:11 +0000 | [diff] [blame] | 2597 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2598 | CGF.EmitBlock(ContBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2599 | |
Eli Friedman | 48daf59 | 2009-12-07 20:25:53 +0000 | [diff] [blame] | 2600 | // If the LHS or RHS is a throw expression, it will be legitimately null. |
| 2601 | if (!LHS) |
| 2602 | return RHS; |
| 2603 | if (!RHS) |
| 2604 | return LHS; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2606 | // Create a PHI node for the real part. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2607 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), 2, "cond"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2608 | PN->addIncoming(LHS, LHSBlock); |
| 2609 | PN->addIncoming(RHS, RHSBlock); |
| 2610 | return PN; |
| 2611 | } |
| 2612 | |
| 2613 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 2614 | return Visit(E->getChosenSubExpr(CGF.getContext())); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 2617 | Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 2618 | llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 2619 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); |
| 2620 | |
| 2621 | // If EmitVAArg fails, we fall back to the LLVM instruction. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2622 | if (!ArgPtr) |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 2623 | return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); |
| 2624 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2625 | // FIXME Volatility. |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 2626 | return Builder.CreateLoad(ArgPtr); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2629 | Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *block) { |
| 2630 | return CGF.EmitBlockLiteral(block); |
Mike Stump | df6b68c | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 2633 | Value *ScalarExprEmitter::VisitAsTypeExpr(AsTypeExpr *E) { |
| 2634 | Value *Src = CGF.EmitScalarExpr(E->getSrcExpr()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2635 | llvm::Type *DstTy = ConvertType(E->getType()); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 2636 | |
| 2637 | // Going from vec4->vec3 or vec3->vec4 is a special case and requires |
| 2638 | // a shuffle vector instead of a bitcast. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2639 | llvm::Type *SrcTy = Src->getType(); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 2640 | if (isa<llvm::VectorType>(DstTy) && isa<llvm::VectorType>(SrcTy)) { |
| 2641 | unsigned numElementsDst = cast<llvm::VectorType>(DstTy)->getNumElements(); |
| 2642 | unsigned numElementsSrc = cast<llvm::VectorType>(SrcTy)->getNumElements(); |
| 2643 | if ((numElementsDst == 3 && numElementsSrc == 4) |
| 2644 | || (numElementsDst == 4 && numElementsSrc == 3)) { |
| 2645 | |
| 2646 | |
| 2647 | // In the case of going from int4->float3, a bitcast is needed before |
| 2648 | // doing a shuffle. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2649 | llvm::Type *srcElemTy = |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 2650 | cast<llvm::VectorType>(SrcTy)->getElementType(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2651 | llvm::Type *dstElemTy = |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 2652 | cast<llvm::VectorType>(DstTy)->getElementType(); |
| 2653 | |
| 2654 | if ((srcElemTy->isIntegerTy() && dstElemTy->isFloatTy()) |
| 2655 | || (srcElemTy->isFloatTy() && dstElemTy->isIntegerTy())) { |
| 2656 | // Create a float type of the same size as the source or destination. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2657 | llvm::VectorType *newSrcTy = llvm::VectorType::get(dstElemTy, |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 2658 | numElementsSrc); |
| 2659 | |
| 2660 | Src = Builder.CreateBitCast(Src, newSrcTy, "astypeCast"); |
| 2661 | } |
| 2662 | |
| 2663 | llvm::Value *UnV = llvm::UndefValue::get(Src->getType()); |
| 2664 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2665 | SmallVector<llvm::Constant*, 3> Args; |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 2666 | Args.push_back(Builder.getInt32(0)); |
| 2667 | Args.push_back(Builder.getInt32(1)); |
| 2668 | Args.push_back(Builder.getInt32(2)); |
| 2669 | |
| 2670 | if (numElementsDst == 4) |
| 2671 | Args.push_back(llvm::UndefValue::get( |
| 2672 | llvm::Type::getInt32Ty(CGF.getLLVMContext()))); |
| 2673 | |
| 2674 | llvm::Constant *Mask = llvm::ConstantVector::get(Args); |
| 2675 | |
| 2676 | return Builder.CreateShuffleVector(Src, UnV, Mask, "astype"); |
| 2677 | } |
| 2678 | } |
| 2679 | |
| 2680 | return Builder.CreateBitCast(Src, DstTy, "astype"); |
| 2681 | } |
| 2682 | |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 2683 | Value *ScalarExprEmitter::VisitAtomicExpr(AtomicExpr *E) { |
| 2684 | return CGF.EmitAtomicExpr(E).getScalarVal(); |
| 2685 | } |
| 2686 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2687 | //===----------------------------------------------------------------------===// |
| 2688 | // Entry Point into this File |
| 2689 | //===----------------------------------------------------------------------===// |
| 2690 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2691 | /// EmitScalarExpr - Emit the computation of the specified expression of scalar |
| 2692 | /// type, ignoring the result. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2693 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2694 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 2695 | "Invalid scalar expression to emit"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2696 | |
Devang Patel | 5de7a0e | 2011-03-07 18:29:53 +0000 | [diff] [blame] | 2697 | if (isa<CXXDefaultArgExpr>(E)) |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 2698 | disableDebugInfo(); |
Devang Patel | 5de7a0e | 2011-03-07 18:29:53 +0000 | [diff] [blame] | 2699 | Value *V = ScalarExprEmitter(*this, IgnoreResultAssign) |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2700 | .Visit(const_cast<Expr*>(E)); |
Devang Patel | 5de7a0e | 2011-03-07 18:29:53 +0000 | [diff] [blame] | 2701 | if (isa<CXXDefaultArgExpr>(E)) |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 2702 | enableDebugInfo(); |
Devang Patel | 5de7a0e | 2011-03-07 18:29:53 +0000 | [diff] [blame] | 2703 | return V; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2704 | } |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 2705 | |
| 2706 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 2707 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2708 | Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, |
| 2709 | QualType DstTy) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 2710 | assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && |
| 2711 | "Invalid scalar expression to emit"); |
| 2712 | return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); |
| 2713 | } |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2714 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2715 | /// EmitComplexToScalarConversion - Emit a conversion from the specified complex |
| 2716 | /// type to the specified destination type, where the destination type is an |
| 2717 | /// LLVM scalar type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2718 | Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, |
| 2719 | QualType SrcTy, |
| 2720 | QualType DstTy) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 2721 | assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) && |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2722 | "Invalid complex -> scalar conversion"); |
| 2723 | return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, |
| 2724 | DstTy); |
| 2725 | } |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 2726 | |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 2727 | |
| 2728 | llvm::Value *CodeGenFunction:: |
| 2729 | EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 2730 | bool isInc, bool isPre) { |
| 2731 | return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre); |
| 2732 | } |
| 2733 | |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 2734 | LValue CodeGenFunction::EmitObjCIsaExpr(const ObjCIsaExpr *E) { |
| 2735 | llvm::Value *V; |
| 2736 | // object->isa or (*object).isa |
| 2737 | // Generate code as for: *(Class*)object |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 2738 | // build Class* type |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2739 | llvm::Type *ClassPtrTy = ConvertType(E->getType()); |
Fariborz Jahanian | 5ed676c | 2010-02-05 19:18:30 +0000 | [diff] [blame] | 2740 | |
| 2741 | Expr *BaseExpr = E->getBase(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2742 | if (BaseExpr->isRValue()) { |
Fariborz Jahanian | 5ed676c | 2010-02-05 19:18:30 +0000 | [diff] [blame] | 2743 | V = CreateTempAlloca(ClassPtrTy, "resval"); |
| 2744 | llvm::Value *Src = EmitScalarExpr(BaseExpr); |
| 2745 | Builder.CreateStore(Src, V); |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2746 | V = ScalarExprEmitter(*this).EmitLoadOfLValue( |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2747 | MakeAddrLValue(V, E->getType())); |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2748 | } else { |
| 2749 | if (E->isArrow()) |
| 2750 | V = ScalarExprEmitter(*this).EmitLoadOfLValue(BaseExpr); |
| 2751 | else |
| 2752 | V = EmitLValue(BaseExpr).getAddress(); |
Fariborz Jahanian | 5ed676c | 2010-02-05 19:18:30 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | // build Class* type |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 2756 | ClassPtrTy = ClassPtrTy->getPointerTo(); |
| 2757 | V = Builder.CreateBitCast(V, ClassPtrTy); |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2758 | return MakeAddrLValue(V, E->getType()); |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2761 | |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 2762 | LValue CodeGenFunction::EmitCompoundAssignmentLValue( |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2763 | const CompoundAssignOperator *E) { |
| 2764 | ScalarExprEmitter Scalar(*this); |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2765 | Value *Result = 0; |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2766 | switch (E->getOpcode()) { |
| 2767 | #define COMPOUND_OP(Op) \ |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2768 | case BO_##Op##Assign: \ |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2769 | return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \ |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2770 | Result) |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2771 | COMPOUND_OP(Mul); |
| 2772 | COMPOUND_OP(Div); |
| 2773 | COMPOUND_OP(Rem); |
| 2774 | COMPOUND_OP(Add); |
| 2775 | COMPOUND_OP(Sub); |
| 2776 | COMPOUND_OP(Shl); |
| 2777 | COMPOUND_OP(Shr); |
| 2778 | COMPOUND_OP(And); |
| 2779 | COMPOUND_OP(Xor); |
| 2780 | COMPOUND_OP(Or); |
| 2781 | #undef COMPOUND_OP |
| 2782 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2783 | case BO_PtrMemD: |
| 2784 | case BO_PtrMemI: |
| 2785 | case BO_Mul: |
| 2786 | case BO_Div: |
| 2787 | case BO_Rem: |
| 2788 | case BO_Add: |
| 2789 | case BO_Sub: |
| 2790 | case BO_Shl: |
| 2791 | case BO_Shr: |
| 2792 | case BO_LT: |
| 2793 | case BO_GT: |
| 2794 | case BO_LE: |
| 2795 | case BO_GE: |
| 2796 | case BO_EQ: |
| 2797 | case BO_NE: |
| 2798 | case BO_And: |
| 2799 | case BO_Xor: |
| 2800 | case BO_Or: |
| 2801 | case BO_LAnd: |
| 2802 | case BO_LOr: |
| 2803 | case BO_Assign: |
| 2804 | case BO_Comma: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2805 | llvm_unreachable("Not valid compound assignment operators"); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | llvm_unreachable("Unhandled compound assignment operator"); |
| 2809 | } |