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