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