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