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