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