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()) |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame^] | 1130 | CGF.EmitLoadOfPropertyRefLValue(LV); |
Fariborz Jahanian | 699c060 | 2010-09-04 19:49:18 +0000 | [diff] [blame] | 1131 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 1132 | else |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1133 | CGF.EmitAnyExpr(E, AggValueSlot::ignored(), true); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1134 | return 0; |
| 1135 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1136 | case CK_VectorSplat: { |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1137 | const llvm::Type *DstTy = ConvertType(DestTy); |
| 1138 | Value *Elt = Visit(const_cast<Expr*>(E)); |
| 1139 | |
| 1140 | // Insert the element in element zero of an undef vector |
| 1141 | llvm::Value *UnV = llvm::UndefValue::get(DstTy); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1142 | llvm::Value *Idx = llvm::ConstantInt::get(CGF.Int32Ty, 0); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1143 | UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp"); |
| 1144 | |
| 1145 | // Splat the element across to all elements |
| 1146 | llvm::SmallVector<llvm::Constant*, 16> Args; |
| 1147 | unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1148 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Int32Ty, 0); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1149 | for (unsigned i = 0; i < NumElements; i++) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1150 | Args.push_back(Zero); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1151 | |
| 1152 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
| 1153 | llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); |
| 1154 | return Yay; |
| 1155 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1156 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1157 | case CK_IntegralCast: |
| 1158 | case CK_IntegralToFloating: |
| 1159 | case CK_FloatingToIntegral: |
| 1160 | case CK_FloatingCast: |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1161 | return EmitScalarConversion(Visit(E), E->getType(), DestTy); |
Eli Friedman | ad35a83 | 2009-11-16 21:33:53 +0000 | [diff] [blame] | 1162 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1163 | case CK_IntegralToBoolean: |
| 1164 | return EmitIntToBoolConversion(Visit(E)); |
| 1165 | case CK_PointerToBoolean: |
| 1166 | return EmitPointerToBoolConversion(Visit(E)); |
| 1167 | case CK_FloatingToBoolean: |
| 1168 | return EmitFloatToBoolConversion(Visit(E)); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1169 | case CK_MemberPointerToBoolean: { |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 1170 | llvm::Value *MemPtr = Visit(E); |
| 1171 | const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>(); |
| 1172 | return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT); |
Anders Carlsson | e977624 | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 1173 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1174 | |
| 1175 | case CK_FloatingComplexToReal: |
| 1176 | case CK_IntegralComplexToReal: |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1177 | return CGF.EmitComplexExpr(E, false, true).first; |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1178 | |
| 1179 | case CK_FloatingComplexToBoolean: |
| 1180 | case CK_IntegralComplexToBoolean: { |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1181 | CodeGenFunction::ComplexPairTy V = CGF.EmitComplexExpr(E); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1182 | |
| 1183 | // TODO: kill this function off, inline appropriate case here |
| 1184 | return EmitComplexToScalarConversion(V, E->getType(), DestTy); |
| 1185 | } |
| 1186 | |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 1187 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1188 | |
John McCall | 61ad0e6 | 2010-11-16 06:21:14 +0000 | [diff] [blame] | 1189 | llvm_unreachable("unknown scalar cast"); |
Chris Lattner | 19a1d7c | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 1190 | return 0; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 1193 | Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 1194 | return CGF.EmitCompoundStmt(*E->getSubStmt(), |
| 1195 | !E->getType()->isVoidType()).getScalarVal(); |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 1198 | Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { |
Fariborz Jahanian | f7bcc7e | 2009-10-10 20:07:56 +0000 | [diff] [blame] | 1199 | llvm::Value *V = CGF.GetAddrOfBlockDecl(E); |
| 1200 | if (E->getType().isObjCGCWeak()) |
| 1201 | return CGF.CGM.getObjCRuntime().EmitObjCWeakRead(CGF, V); |
Fariborz Jahanian | 469a20d | 2010-09-03 23:07:53 +0000 | [diff] [blame] | 1202 | return CGF.EmitLoadOfScalar(V, false, 0, E->getType()); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 1203 | } |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 1204 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1205 | //===----------------------------------------------------------------------===// |
| 1206 | // Unary Operators |
| 1207 | //===----------------------------------------------------------------------===// |
| 1208 | |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1209 | llvm::Value *ScalarExprEmitter:: |
| 1210 | EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 1211 | bool isInc, bool isPre) { |
| 1212 | |
| 1213 | QualType ValTy = E->getSubExpr()->getType(); |
| 1214 | llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy); |
| 1215 | |
| 1216 | int AmountVal = isInc ? 1 : -1; |
| 1217 | |
| 1218 | if (ValTy->isPointerType() && |
| 1219 | ValTy->getAs<PointerType>()->isVariableArrayType()) { |
| 1220 | // The amount of the addition/subtraction needs to account for the VLA size |
| 1221 | CGF.ErrorUnsupported(E, "VLA pointer inc/dec"); |
| 1222 | } |
| 1223 | |
| 1224 | llvm::Value *NextVal; |
| 1225 | if (const llvm::PointerType *PT = |
| 1226 | dyn_cast<llvm::PointerType>(InVal->getType())) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1227 | llvm::Constant *Inc = llvm::ConstantInt::get(CGF.Int32Ty, AmountVal); |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1228 | if (!isa<llvm::FunctionType>(PT->getElementType())) { |
| 1229 | QualType PTEE = ValTy->getPointeeType(); |
| 1230 | if (const ObjCObjectType *OIT = PTEE->getAs<ObjCObjectType>()) { |
| 1231 | // Handle interface types, which are not represented with a concrete |
| 1232 | // type. |
| 1233 | int size = CGF.getContext().getTypeSize(OIT) / 8; |
| 1234 | if (!isInc) |
| 1235 | size = -size; |
| 1236 | Inc = llvm::ConstantInt::get(Inc->getType(), size); |
| 1237 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
| 1238 | InVal = Builder.CreateBitCast(InVal, i8Ty); |
| 1239 | NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr"); |
| 1240 | llvm::Value *lhs = LV.getAddress(); |
| 1241 | lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty)); |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 1242 | LV = CGF.MakeAddrLValue(lhs, ValTy); |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1243 | } else |
| 1244 | NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec"); |
| 1245 | } else { |
| 1246 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
| 1247 | NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp"); |
| 1248 | NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec"); |
| 1249 | NextVal = Builder.CreateBitCast(NextVal, InVal->getType()); |
| 1250 | } |
| 1251 | } else if (InVal->getType()->isIntegerTy(1) && isInc) { |
| 1252 | // Bool++ is an interesting case, due to promotion rules, we get: |
| 1253 | // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 -> |
| 1254 | // Bool = ((int)Bool+1) != 0 |
| 1255 | // An interesting aspect of this is that increment is always true. |
| 1256 | // Decrement does not have this property. |
| 1257 | NextVal = llvm::ConstantInt::getTrue(VMContext); |
| 1258 | } else if (isa<llvm::IntegerType>(InVal->getType())) { |
| 1259 | NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); |
| 1260 | |
Chris Lattner | 640d326 | 2010-06-26 22:18:28 +0000 | [diff] [blame] | 1261 | if (!ValTy->isSignedIntegerType()) |
| 1262 | // Unsigned integer inc is always two's complement. |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1263 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
Chris Lattner | 640d326 | 2010-06-26 22:18:28 +0000 | [diff] [blame] | 1264 | else { |
| 1265 | switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { |
| 1266 | case LangOptions::SOB_Undefined: |
| 1267 | NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 1268 | break; |
| 1269 | case LangOptions::SOB_Defined: |
| 1270 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 1271 | break; |
| 1272 | case LangOptions::SOB_Trapping: |
| 1273 | BinOpInfo BinOp; |
| 1274 | BinOp.LHS = InVal; |
| 1275 | BinOp.RHS = NextVal; |
| 1276 | BinOp.Ty = E->getType(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1277 | BinOp.Opcode = BO_Add; |
Chris Lattner | 640d326 | 2010-06-26 22:18:28 +0000 | [diff] [blame] | 1278 | BinOp.E = E; |
John McCall | 401be6b | 2010-08-05 17:39:44 +0000 | [diff] [blame] | 1279 | NextVal = EmitOverflowCheckedBinOp(BinOp); |
| 1280 | break; |
Chris Lattner | 640d326 | 2010-06-26 22:18:28 +0000 | [diff] [blame] | 1281 | } |
| 1282 | } |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 1283 | } else { |
| 1284 | // Add the inc/dec to the real part. |
| 1285 | if (InVal->getType()->isFloatTy()) |
| 1286 | NextVal = |
| 1287 | llvm::ConstantFP::get(VMContext, |
| 1288 | llvm::APFloat(static_cast<float>(AmountVal))); |
| 1289 | else if (InVal->getType()->isDoubleTy()) |
| 1290 | NextVal = |
| 1291 | llvm::ConstantFP::get(VMContext, |
| 1292 | llvm::APFloat(static_cast<double>(AmountVal))); |
| 1293 | else { |
| 1294 | llvm::APFloat F(static_cast<float>(AmountVal)); |
| 1295 | bool ignored; |
| 1296 | F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero, |
| 1297 | &ignored); |
| 1298 | NextVal = llvm::ConstantFP::get(VMContext, F); |
| 1299 | } |
| 1300 | NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 1301 | } |
| 1302 | |
| 1303 | // Store the updated result through the lvalue. |
| 1304 | if (LV.isBitField()) |
| 1305 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal); |
| 1306 | else |
| 1307 | CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy); |
| 1308 | |
| 1309 | // If this is a postinc, return the value read from memory, otherwise use the |
| 1310 | // updated value. |
| 1311 | return isPre ? NextVal : InVal; |
| 1312 | } |
| 1313 | |
| 1314 | |
| 1315 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1316 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1317 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1318 | // Emit unary minus with EmitSub so we handle overflow cases etc. |
| 1319 | BinOpInfo BinOp; |
Chris Lattner | 4ac0d83 | 2010-06-28 17:12:37 +0000 | [diff] [blame] | 1320 | BinOp.RHS = Visit(E->getSubExpr()); |
| 1321 | |
| 1322 | if (BinOp.RHS->getType()->isFPOrFPVectorTy()) |
| 1323 | BinOp.LHS = llvm::ConstantFP::getZeroValueForNegation(BinOp.RHS->getType()); |
| 1324 | else |
| 1325 | BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType()); |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1326 | BinOp.Ty = E->getType(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1327 | BinOp.Opcode = BO_Sub; |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1328 | BinOp.E = E; |
| 1329 | return EmitSub(BinOp); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1333 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1334 | Value *Op = Visit(E->getSubExpr()); |
| 1335 | return Builder.CreateNot(Op, "neg"); |
| 1336 | } |
| 1337 | |
| 1338 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 1339 | // Compare operand to zero. |
| 1340 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1341 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1342 | // Invert value. |
| 1343 | // TODO: Could dynamically modify easy computations here. For example, if |
| 1344 | // the operand is an icmp ne, turn into icmp eq. |
| 1345 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1346 | |
Anders Carlsson | 9f84d88 | 2009-05-19 18:44:53 +0000 | [diff] [blame] | 1347 | // ZExt result to the expr type. |
| 1348 | return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1351 | Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) { |
| 1352 | // Try folding the offsetof to a constant. |
| 1353 | Expr::EvalResult EvalResult; |
| 1354 | if (E->Evaluate(EvalResult, CGF.getContext())) |
| 1355 | return llvm::ConstantInt::get(VMContext, EvalResult.Val.getInt()); |
| 1356 | |
| 1357 | // Loop over the components of the offsetof to compute the value. |
| 1358 | unsigned n = E->getNumComponents(); |
| 1359 | const llvm::Type* ResultType = ConvertType(E->getType()); |
| 1360 | llvm::Value* Result = llvm::Constant::getNullValue(ResultType); |
| 1361 | QualType CurrentType = E->getTypeSourceInfo()->getType(); |
| 1362 | for (unsigned i = 0; i != n; ++i) { |
| 1363 | OffsetOfExpr::OffsetOfNode ON = E->getComponent(i); |
Eli Friedman | 16fd39f | 2010-08-06 16:37:05 +0000 | [diff] [blame] | 1364 | llvm::Value *Offset = 0; |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1365 | switch (ON.getKind()) { |
| 1366 | case OffsetOfExpr::OffsetOfNode::Array: { |
| 1367 | // Compute the index |
| 1368 | Expr *IdxExpr = E->getIndexExpr(ON.getArrayExprIndex()); |
| 1369 | llvm::Value* Idx = CGF.EmitScalarExpr(IdxExpr); |
| 1370 | bool IdxSigned = IdxExpr->getType()->isSignedIntegerType(); |
| 1371 | Idx = Builder.CreateIntCast(Idx, ResultType, IdxSigned, "conv"); |
| 1372 | |
| 1373 | // Save the element type |
| 1374 | CurrentType = |
| 1375 | CGF.getContext().getAsArrayType(CurrentType)->getElementType(); |
| 1376 | |
| 1377 | // Compute the element size |
| 1378 | llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType, |
| 1379 | CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity()); |
| 1380 | |
| 1381 | // Multiply out to compute the result |
| 1382 | Offset = Builder.CreateMul(Idx, ElemSize); |
| 1383 | break; |
| 1384 | } |
| 1385 | |
| 1386 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 1387 | FieldDecl *MemberDecl = ON.getField(); |
| 1388 | RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl(); |
| 1389 | const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); |
| 1390 | |
| 1391 | // Compute the index of the field in its parent. |
| 1392 | unsigned i = 0; |
| 1393 | // FIXME: It would be nice if we didn't have to loop here! |
| 1394 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 1395 | FieldEnd = RD->field_end(); |
| 1396 | Field != FieldEnd; (void)++Field, ++i) { |
| 1397 | if (*Field == MemberDecl) |
| 1398 | break; |
| 1399 | } |
| 1400 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
| 1401 | |
| 1402 | // Compute the offset to the field |
| 1403 | int64_t OffsetInt = RL.getFieldOffset(i) / |
| 1404 | CGF.getContext().getCharWidth(); |
| 1405 | Offset = llvm::ConstantInt::get(ResultType, OffsetInt); |
| 1406 | |
| 1407 | // Save the element type. |
| 1408 | CurrentType = MemberDecl->getType(); |
| 1409 | break; |
| 1410 | } |
Eli Friedman | 16fd39f | 2010-08-06 16:37:05 +0000 | [diff] [blame] | 1411 | |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1412 | case OffsetOfExpr::OffsetOfNode::Identifier: |
Eli Friedman | 6d4e44b | 2010-08-06 01:17:25 +0000 | [diff] [blame] | 1413 | llvm_unreachable("dependent __builtin_offsetof"); |
Eli Friedman | 16fd39f | 2010-08-06 16:37:05 +0000 | [diff] [blame] | 1414 | |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1415 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 1416 | if (ON.getBase()->isVirtual()) { |
| 1417 | CGF.ErrorUnsupported(E, "virtual base in offsetof"); |
| 1418 | continue; |
| 1419 | } |
| 1420 | |
| 1421 | RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl(); |
| 1422 | const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); |
| 1423 | |
| 1424 | // Save the element type. |
| 1425 | CurrentType = ON.getBase()->getType(); |
| 1426 | |
| 1427 | // Compute the offset to the base. |
| 1428 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 1429 | CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 1430 | int64_t OffsetInt = RL.getBaseClassOffsetInBits(BaseRD) / |
Eli Friedman | 0027d2b | 2010-08-05 09:58:49 +0000 | [diff] [blame] | 1431 | CGF.getContext().getCharWidth(); |
| 1432 | Offset = llvm::ConstantInt::get(ResultType, OffsetInt); |
| 1433 | break; |
| 1434 | } |
| 1435 | } |
| 1436 | Result = Builder.CreateAdd(Result, Offset); |
| 1437 | } |
| 1438 | return Result; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1441 | /// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of |
| 1442 | /// argument of the sizeof expression as an integer. |
| 1443 | Value * |
| 1444 | ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1445 | QualType TypeToSize = E->getTypeOfArgument(); |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1446 | if (E->isSizeOf()) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1447 | if (const VariableArrayType *VAT = |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1448 | CGF.getContext().getAsVariableArrayType(TypeToSize)) { |
| 1449 | if (E->isArgumentType()) { |
| 1450 | // sizeof(type) - make sure to emit the VLA size. |
| 1451 | CGF.EmitVLASize(TypeToSize); |
Eli Friedman | 8f426fa | 2009-04-20 03:21:44 +0000 | [diff] [blame] | 1452 | } else { |
| 1453 | // C99 6.5.3.4p2: If the argument is an expression of type |
| 1454 | // VLA, it is evaluated. |
| 1455 | CGF.EmitAnyExpr(E->getArgumentExpr()); |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1456 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1457 | |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 1458 | return CGF.GetVLASize(VAT); |
Anders Carlsson | b50525b | 2008-12-21 03:33:21 +0000 | [diff] [blame] | 1459 | } |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 1460 | } |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1461 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1462 | // If this isn't sizeof(vla), the result must be constant; use the constant |
| 1463 | // folding logic so we don't have to duplicate it here. |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 1464 | Expr::EvalResult Result; |
| 1465 | E->Evaluate(Result, CGF.getContext()); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1466 | return llvm::ConstantInt::get(VMContext, Result.Val.getInt()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1469 | Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { |
| 1470 | Expr *Op = E->getSubExpr(); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1471 | if (Op->getType()->isAnyComplexType()) { |
| 1472 | // If it's an l-value, load through the appropriate subobject l-value. |
| 1473 | // Note that we have to ask E because Op might be an l-value that |
| 1474 | // this won't work for, e.g. an Obj-C property. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1475 | if (E->isGLValue()) |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1476 | return CGF.EmitLoadOfLValue(CGF.EmitLValue(E), E->getType()) |
| 1477 | .getScalarVal(); |
| 1478 | |
| 1479 | // Otherwise, calculate and project. |
| 1480 | return CGF.EmitComplexExpr(Op, false, true).first; |
| 1481 | } |
| 1482 | |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1483 | return Visit(Op); |
| 1484 | } |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1486 | Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { |
| 1487 | Expr *Op = E->getSubExpr(); |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1488 | if (Op->getType()->isAnyComplexType()) { |
| 1489 | // If it's an l-value, load through the appropriate subobject l-value. |
| 1490 | // Note that we have to ask E because Op might be an l-value that |
| 1491 | // this won't work for, e.g. an Obj-C property. |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1492 | if (Op->isGLValue()) |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1493 | return CGF.EmitLoadOfLValue(CGF.EmitLValue(E), E->getType()) |
| 1494 | .getScalarVal(); |
| 1495 | |
| 1496 | // Otherwise, calculate and project. |
| 1497 | return CGF.EmitComplexExpr(Op, true, false).second; |
| 1498 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1499 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1500 | // __imag on a scalar returns zero. Emit the subexpr to ensure side |
| 1501 | // effects are evaluated, but not the actual value. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1502 | CGF.EmitScalarExpr(Op, true); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1503 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1506 | //===----------------------------------------------------------------------===// |
| 1507 | // Binary Operators |
| 1508 | //===----------------------------------------------------------------------===// |
| 1509 | |
| 1510 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1511 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1512 | BinOpInfo Result; |
| 1513 | Result.LHS = Visit(E->getLHS()); |
| 1514 | Result.RHS = Visit(E->getRHS()); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1515 | Result.Ty = E->getType(); |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1516 | Result.Opcode = E->getOpcode(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1517 | Result.E = E; |
| 1518 | return Result; |
| 1519 | } |
| 1520 | |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1521 | LValue ScalarExprEmitter::EmitCompoundAssignLValue( |
| 1522 | const CompoundAssignOperator *E, |
| 1523 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &), |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1524 | Value *&Result) { |
Benjamin Kramer | 54d76db | 2009-12-25 15:43:36 +0000 | [diff] [blame] | 1525 | QualType LHSTy = E->getLHS()->getType(); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1526 | BinOpInfo OpInfo; |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1527 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1528 | if (E->getComputationResultType()->isAnyComplexType()) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1529 | // This needs to go through the complex expression emitter, but it's a tad |
| 1530 | // complicated to do that... I'm leaving it out for now. (Note that we do |
| 1531 | // actually need the imaginary part of the RHS for multiplication and |
| 1532 | // division.) |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1533 | CGF.ErrorUnsupported(E, "complex compound assignment"); |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1534 | Result = llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1535 | return LValue(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1536 | } |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1537 | |
Mike Stump | cc0442f | 2009-05-22 19:07:20 +0000 | [diff] [blame] | 1538 | // Emit the RHS first. __block variables need to have the rhs evaluated |
| 1539 | // first, plus this should improve codegen a little. |
| 1540 | OpInfo.RHS = Visit(E->getRHS()); |
| 1541 | OpInfo.Ty = E->getComputationResultType(); |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1542 | OpInfo.Opcode = E->getOpcode(); |
Mike Stump | cc0442f | 2009-05-22 19:07:20 +0000 | [diff] [blame] | 1543 | OpInfo.E = E; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1544 | // Load/convert the LHS. |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 1545 | LValue LHSLV = EmitCheckedLValue(E->getLHS()); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1546 | OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1547 | OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, |
| 1548 | E->getComputationLHSType()); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1550 | // Expand the binary operator. |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1551 | Result = (this->*Func)(OpInfo); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1552 | |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1553 | // Convert the result back to the LHS type. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1554 | Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy); |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1555 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1556 | // Store the result value into the LHS lvalue. Bit-fields are handled |
| 1557 | // specially because the result is altered by the store, i.e., [C99 6.5.16p1] |
| 1558 | // 'An assignment expression has the value of the left operand after the |
| 1559 | // assignment...'. |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1560 | if (LHSLV.isBitField()) |
| 1561 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy, |
| 1562 | &Result); |
| 1563 | else |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1564 | CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy); |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1565 | |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 1566 | return LHSLV; |
| 1567 | } |
| 1568 | |
| 1569 | Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, |
| 1570 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { |
| 1571 | bool Ignore = TestAndClearIgnoreResultAssign(); |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1572 | Value *RHS; |
| 1573 | LValue LHS = EmitCompoundAssignLValue(E, Func, RHS); |
| 1574 | |
| 1575 | // If the result is clearly ignored, return now. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1576 | if (Ignore) |
| 1577 | return 0; |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1578 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 1579 | // The result of an assignment in C is the assigned r-value. |
| 1580 | if (!CGF.getContext().getLangOptions().CPlusPlus) |
| 1581 | return RHS; |
| 1582 | |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1583 | // Objective-C property assignment never reloads the value following a store. |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame^] | 1584 | if (LHS.isPropertyRef()) |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 1585 | return RHS; |
| 1586 | |
| 1587 | // If the lvalue is non-volatile, return the computed value of the assignment. |
| 1588 | if (!LHS.isVolatileQualified()) |
| 1589 | return RHS; |
| 1590 | |
| 1591 | // Otherwise, reload the value. |
| 1592 | return EmitLoadOfLValue(LHS, E->getType()); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1595 | void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck( |
| 1596 | const BinOpInfo &Ops, |
| 1597 | llvm::Value *Zero, bool isDiv) { |
| 1598 | llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn); |
| 1599 | llvm::BasicBlock *contBB = |
| 1600 | CGF.createBasicBlock(isDiv ? "div.cont" : "rem.cont", CGF.CurFn); |
| 1601 | |
| 1602 | const llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType()); |
| 1603 | |
| 1604 | if (Ops.Ty->hasSignedIntegerRepresentation()) { |
| 1605 | llvm::Value *IntMin = |
| 1606 | llvm::ConstantInt::get(VMContext, |
| 1607 | llvm::APInt::getSignedMinValue(Ty->getBitWidth())); |
| 1608 | llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL); |
| 1609 | |
| 1610 | llvm::Value *Cond1 = Builder.CreateICmpEQ(Ops.RHS, Zero); |
| 1611 | llvm::Value *LHSCmp = Builder.CreateICmpEQ(Ops.LHS, IntMin); |
| 1612 | llvm::Value *RHSCmp = Builder.CreateICmpEQ(Ops.RHS, NegOne); |
| 1613 | llvm::Value *Cond2 = Builder.CreateAnd(LHSCmp, RHSCmp, "and"); |
| 1614 | Builder.CreateCondBr(Builder.CreateOr(Cond1, Cond2, "or"), |
| 1615 | overflowBB, contBB); |
| 1616 | } else { |
| 1617 | CGF.Builder.CreateCondBr(Builder.CreateICmpEQ(Ops.RHS, Zero), |
| 1618 | overflowBB, contBB); |
| 1619 | } |
| 1620 | EmitOverflowBB(overflowBB); |
| 1621 | Builder.SetInsertPoint(contBB); |
| 1622 | } |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1624 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
Chris Lattner | 8023030 | 2010-09-11 21:47:09 +0000 | [diff] [blame] | 1625 | if (isTrapvOverflowBehavior()) { |
| 1626 | llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); |
| 1627 | |
| 1628 | if (Ops.Ty->isIntegerType()) |
| 1629 | EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, true); |
| 1630 | else if (Ops.Ty->isRealFloatingType()) { |
| 1631 | llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", |
| 1632 | CGF.CurFn); |
| 1633 | llvm::BasicBlock *DivCont = CGF.createBasicBlock("div.cont", CGF.CurFn); |
| 1634 | CGF.Builder.CreateCondBr(Builder.CreateFCmpOEQ(Ops.RHS, Zero), |
| 1635 | overflowBB, DivCont); |
| 1636 | EmitOverflowBB(overflowBB); |
| 1637 | Builder.SetInsertPoint(DivCont); |
| 1638 | } |
| 1639 | } |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 1640 | if (Ops.LHS->getType()->isFPOrFPVectorTy()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1641 | return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1642 | else if (Ops.Ty->hasUnsignedIntegerRepresentation()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1643 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 1644 | else |
| 1645 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 1646 | } |
| 1647 | |
| 1648 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 1649 | // 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] | 1650 | if (isTrapvOverflowBehavior()) { |
| 1651 | llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); |
| 1652 | |
| 1653 | if (Ops.Ty->isIntegerType()) |
| 1654 | EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false); |
| 1655 | } |
| 1656 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1657 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1658 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 1659 | else |
| 1660 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 1661 | } |
| 1662 | |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1663 | Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) { |
| 1664 | unsigned IID; |
| 1665 | unsigned OpID = 0; |
Mike Stump | 5d8b2cf | 2009-04-02 01:03:55 +0000 | [diff] [blame] | 1666 | |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1667 | switch (Ops.Opcode) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1668 | case BO_Add: |
| 1669 | case BO_AddAssign: |
Mike Stump | 035cf89 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1670 | OpID = 1; |
| 1671 | IID = llvm::Intrinsic::sadd_with_overflow; |
| 1672 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1673 | case BO_Sub: |
| 1674 | case BO_SubAssign: |
Mike Stump | 035cf89 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1675 | OpID = 2; |
| 1676 | IID = llvm::Intrinsic::ssub_with_overflow; |
| 1677 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1678 | case BO_Mul: |
| 1679 | case BO_MulAssign: |
Mike Stump | 035cf89 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1680 | OpID = 3; |
| 1681 | IID = llvm::Intrinsic::smul_with_overflow; |
| 1682 | break; |
| 1683 | default: |
| 1684 | assert(false && "Unsupported operation for overflow detection"); |
Daniel Dunbar | ab4eff6 | 2009-04-08 16:23:09 +0000 | [diff] [blame] | 1685 | IID = 0; |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1686 | } |
Mike Stump | 035cf89 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1687 | OpID <<= 1; |
| 1688 | OpID |= 1; |
| 1689 | |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1690 | const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty); |
| 1691 | |
| 1692 | llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1); |
| 1693 | |
| 1694 | Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS); |
| 1695 | Value *result = Builder.CreateExtractValue(resultAndOverflow, 0); |
| 1696 | Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1); |
| 1697 | |
| 1698 | // Branch in case of overflow. |
David Chisnall | 7f18e67 | 2010-09-17 18:29:54 +0000 | [diff] [blame] | 1699 | llvm::BasicBlock *initialBB = Builder.GetInsertBlock(); |
Chris Lattner | 93a0035 | 2010-08-07 00:20:46 +0000 | [diff] [blame] | 1700 | llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn); |
| 1701 | llvm::BasicBlock *continueBB = CGF.createBasicBlock("nooverflow", CGF.CurFn); |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1702 | |
| 1703 | Builder.CreateCondBr(overflow, overflowBB, continueBB); |
| 1704 | |
Chris Lattner | 93a0035 | 2010-08-07 00:20:46 +0000 | [diff] [blame] | 1705 | // Handle overflow with llvm.trap. |
David Chisnall | 7f18e67 | 2010-09-17 18:29:54 +0000 | [diff] [blame] | 1706 | const std::string *handlerName = |
| 1707 | &CGF.getContext().getLangOptions().OverflowHandler; |
| 1708 | if (handlerName->empty()) { |
| 1709 | EmitOverflowBB(overflowBB); |
| 1710 | Builder.SetInsertPoint(continueBB); |
| 1711 | return result; |
| 1712 | } |
| 1713 | |
| 1714 | // If an overflow handler is set, then we want to call it and then use its |
| 1715 | // result, if it returns. |
| 1716 | Builder.SetInsertPoint(overflowBB); |
| 1717 | |
| 1718 | // Get the overflow handler. |
| 1719 | const llvm::Type *Int8Ty = llvm::Type::getInt8Ty(VMContext); |
| 1720 | std::vector<const llvm::Type*> argTypes; |
| 1721 | argTypes.push_back(CGF.Int64Ty); argTypes.push_back(CGF.Int64Ty); |
| 1722 | argTypes.push_back(Int8Ty); argTypes.push_back(Int8Ty); |
| 1723 | llvm::FunctionType *handlerTy = |
| 1724 | llvm::FunctionType::get(CGF.Int64Ty, argTypes, true); |
| 1725 | llvm::Value *handler = CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName); |
| 1726 | |
| 1727 | // Sign extend the args to 64-bit, so that we can use the same handler for |
| 1728 | // all types of overflow. |
| 1729 | llvm::Value *lhs = Builder.CreateSExt(Ops.LHS, CGF.Int64Ty); |
| 1730 | llvm::Value *rhs = Builder.CreateSExt(Ops.RHS, CGF.Int64Ty); |
| 1731 | |
| 1732 | // Call the handler with the two arguments, the operation, and the size of |
| 1733 | // the result. |
| 1734 | llvm::Value *handlerResult = Builder.CreateCall4(handler, lhs, rhs, |
| 1735 | Builder.getInt8(OpID), |
| 1736 | Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth())); |
| 1737 | |
| 1738 | // Truncate the result back to the desired size. |
| 1739 | handlerResult = Builder.CreateTrunc(handlerResult, opTy); |
| 1740 | Builder.CreateBr(continueBB); |
| 1741 | |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1742 | Builder.SetInsertPoint(continueBB); |
David Chisnall | 7f18e67 | 2010-09-17 18:29:54 +0000 | [diff] [blame] | 1743 | llvm::PHINode *phi = Builder.CreatePHI(opTy); |
| 1744 | phi->reserveOperandSpace(2); |
| 1745 | phi->addIncoming(result, initialBB); |
| 1746 | phi->addIncoming(handlerResult, overflowBB); |
| 1747 | |
| 1748 | return phi; |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1749 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1750 | |
| 1751 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1752 | if (!Ops.Ty->isAnyPointerType()) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1753 | if (Ops.Ty->hasSignedIntegerRepresentation()) { |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 1754 | switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { |
| 1755 | case LangOptions::SOB_Undefined: |
| 1756 | return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add"); |
| 1757 | case LangOptions::SOB_Defined: |
| 1758 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
| 1759 | case LangOptions::SOB_Trapping: |
| 1760 | return EmitOverflowCheckedBinOp(Ops); |
| 1761 | } |
| 1762 | } |
| 1763 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 1764 | if (Ops.LHS->getType()->isFPOrFPVectorTy()) |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1765 | return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add"); |
Dan Gohman | bf933a0 | 2009-08-12 01:16:29 +0000 | [diff] [blame] | 1766 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1767 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1768 | } |
Eli Friedman | daa24a2 | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1769 | |
Chris Lattner | 7f215c1 | 2010-06-26 21:52:32 +0000 | [diff] [blame] | 1770 | // Must have binary (not unary) expr here. Unary pointer decrement doesn't |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1771 | // use this path. |
| 1772 | const BinaryOperator *BinOp = cast<BinaryOperator>(Ops.E); |
| 1773 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1774 | if (Ops.Ty->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1775 | Ops.Ty->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | daa24a2 | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1776 | // The amount of the addition needs to account for the VLA size |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1777 | CGF.ErrorUnsupported(BinOp, "VLA pointer addition"); |
Eli Friedman | daa24a2 | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1778 | } |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1779 | |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1780 | Value *Ptr, *Idx; |
| 1781 | Expr *IdxExp; |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1782 | const PointerType *PT = BinOp->getLHS()->getType()->getAs<PointerType>(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1783 | const ObjCObjectPointerType *OPT = |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1784 | BinOp->getLHS()->getType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1785 | if (PT || OPT) { |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1786 | Ptr = Ops.LHS; |
| 1787 | Idx = Ops.RHS; |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1788 | IdxExp = BinOp->getRHS(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1789 | } else { // int + pointer |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1790 | PT = BinOp->getRHS()->getType()->getAs<PointerType>(); |
| 1791 | OPT = BinOp->getRHS()->getType()->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1792 | assert((PT || OPT) && "Invalid add expr"); |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1793 | Ptr = Ops.RHS; |
| 1794 | Idx = Ops.LHS; |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1795 | IdxExp = BinOp->getLHS(); |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
Sanjiv Gupta | 7cabee5 | 2009-04-24 02:40:57 +0000 | [diff] [blame] | 1799 | if (Width < CGF.LLVMPointerWidth) { |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1800 | // Zero or sign extend the pointer value based on whether the index is |
| 1801 | // signed or not. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1802 | const llvm::Type *IdxType = CGF.IntPtrTy; |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 1803 | if (IdxExp->getType()->isSignedIntegerType()) |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1804 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 1805 | else |
| 1806 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 1807 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1808 | const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1809 | // Handle interface types, which are not represented with a concrete type. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1810 | if (const ObjCObjectType *OIT = ElementType->getAs<ObjCObjectType>()) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1811 | llvm::Value *InterfaceSize = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1812 | llvm::ConstantInt::get(Idx->getType(), |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1813 | CGF.getContext().getTypeSizeInChars(OIT).getQuantity()); |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1814 | Idx = Builder.CreateMul(Idx, InterfaceSize); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1815 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1816 | Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); |
| 1817 | Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); |
| 1818 | return Builder.CreateBitCast(Res, Ptr->getType()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1819 | } |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1820 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1821 | // Explicitly handle GNU void* and function pointer arithmetic extensions. The |
| 1822 | // GNU void* casts amount to no-ops since our void* type is i8*, but this is |
| 1823 | // future proof. |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1824 | if (ElementType->isVoidType() || ElementType->isFunctionType()) { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1825 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1826 | Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1827 | Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1828 | return Builder.CreateBitCast(Res, Ptr->getType()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Dan Gohman | 664f893 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1831 | return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1835 | if (!isa<llvm::PointerType>(Ops.LHS->getType())) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1836 | if (Ops.Ty->hasSignedIntegerRepresentation()) { |
Chris Lattner | a4d7145 | 2010-06-26 21:25:03 +0000 | [diff] [blame] | 1837 | switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { |
| 1838 | case LangOptions::SOB_Undefined: |
| 1839 | return Builder.CreateNSWSub(Ops.LHS, Ops.RHS, "sub"); |
| 1840 | case LangOptions::SOB_Defined: |
| 1841 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
| 1842 | case LangOptions::SOB_Trapping: |
| 1843 | return EmitOverflowCheckedBinOp(Ops); |
| 1844 | } |
| 1845 | } |
| 1846 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 1847 | if (Ops.LHS->getType()->isFPOrFPVectorTy()) |
Chris Lattner | 87415d2 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1848 | return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub"); |
Chris Lattner | 2eb91e4 | 2010-03-29 17:28:16 +0000 | [diff] [blame] | 1849 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1850 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1851 | } |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1852 | |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1853 | // Must have binary (not unary) expr here. Unary pointer increment doesn't |
| 1854 | // use this path. |
| 1855 | const BinaryOperator *BinOp = cast<BinaryOperator>(Ops.E); |
| 1856 | |
| 1857 | if (BinOp->getLHS()->getType()->isPointerType() && |
| 1858 | BinOp->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | daa24a2 | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1859 | // The amount of the addition needs to account for the VLA size for |
| 1860 | // ptr-int |
| 1861 | // The amount of the division needs to account for the VLA size for |
| 1862 | // ptr-ptr. |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1863 | CGF.ErrorUnsupported(BinOp, "VLA pointer subtraction"); |
Eli Friedman | daa24a2 | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1866 | const QualType LHSType = BinOp->getLHS()->getType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1867 | const QualType LHSElementType = LHSType->getPointeeType(); |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1868 | if (!isa<llvm::PointerType>(Ops.RHS->getType())) { |
| 1869 | // pointer - int |
| 1870 | Value *Idx = Ops.RHS; |
| 1871 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
Sanjiv Gupta | 7cabee5 | 2009-04-24 02:40:57 +0000 | [diff] [blame] | 1872 | if (Width < CGF.LLVMPointerWidth) { |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1873 | // Zero or sign extend the pointer value based on whether the index is |
| 1874 | // signed or not. |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1875 | const llvm::Type *IdxType = CGF.IntPtrTy; |
Chris Lattner | 9a20723 | 2010-06-26 21:48:21 +0000 | [diff] [blame] | 1876 | if (BinOp->getRHS()->getType()->isSignedIntegerType()) |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1877 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 1878 | else |
| 1879 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 1880 | } |
| 1881 | Idx = Builder.CreateNeg(Idx, "sub.ptr.neg"); |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1882 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1883 | // Handle interface types, which are not represented with a concrete type. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1884 | if (const ObjCObjectType *OIT = LHSElementType->getAs<ObjCObjectType>()) { |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1885 | llvm::Value *InterfaceSize = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1886 | llvm::ConstantInt::get(Idx->getType(), |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1887 | CGF.getContext(). |
| 1888 | getTypeSizeInChars(OIT).getQuantity()); |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1889 | Idx = Builder.CreateMul(Idx, InterfaceSize); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1890 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1891 | Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); |
| 1892 | Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr"); |
| 1893 | return Builder.CreateBitCast(Res, Ops.LHS->getType()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1894 | } |
Daniel Dunbar | 2a86625 | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1895 | |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1896 | // Explicitly handle GNU void* and function pointer arithmetic |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1897 | // extensions. The GNU void* casts amount to no-ops since our void* type is |
| 1898 | // i8*, but this is future proof. |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1899 | if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 1900 | const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1901 | Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); |
| 1902 | Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr"); |
| 1903 | return Builder.CreateBitCast(Res, Ops.LHS->getType()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Dan Gohman | 664f893 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1906 | return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr"); |
Daniel Dunbar | 820b033 | 2008-08-05 00:47:03 +0000 | [diff] [blame] | 1907 | } else { |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1908 | // pointer - pointer |
| 1909 | Value *LHS = Ops.LHS; |
| 1910 | Value *RHS = Ops.RHS; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1911 | |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1912 | CharUnits ElementSize; |
Daniel Dunbar | 820b033 | 2008-08-05 00:47:03 +0000 | [diff] [blame] | 1913 | |
Chris Lattner | e5ed151 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 1914 | // Handle GCC extension for pointer arithmetic on void* and function pointer |
| 1915 | // types. |
| 1916 | if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1917 | ElementSize = CharUnits::One(); |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1918 | } else { |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1919 | ElementSize = CGF.getContext().getTypeSizeInChars(LHSElementType); |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1920 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1921 | |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1922 | const llvm::Type *ResultType = ConvertType(Ops.Ty); |
| 1923 | LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); |
| 1924 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 1925 | Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1926 | |
Chris Lattner | e5ed151 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 1927 | // Optimize out the shift for element size of 1. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1928 | if (ElementSize.isOne()) |
Chris Lattner | e5ed151 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 1929 | return BytesBetween; |
Dan Gohman | df11094 | 2009-08-11 22:40:09 +0000 | [diff] [blame] | 1930 | |
| 1931 | // 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] | 1932 | // pointer difference in C is only defined in the case where both operands |
| 1933 | // are pointing to elements of an array. |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 1934 | Value *BytesPerElt = |
| 1935 | llvm::ConstantInt::get(ResultType, ElementSize.getQuantity()); |
Dan Gohman | df11094 | 2009-08-11 22:40:09 +0000 | [diff] [blame] | 1936 | return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1937 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
| 1940 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 1941 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 1942 | // RHS to the same size as the LHS. |
| 1943 | Value *RHS = Ops.RHS; |
| 1944 | if (Ops.LHS->getType() != RHS->getType()) |
| 1945 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1946 | |
Mike Stump | be07f60 | 2009-12-14 21:58:14 +0000 | [diff] [blame] | 1947 | if (CGF.CatchUndefined |
| 1948 | && isa<llvm::IntegerType>(Ops.LHS->getType())) { |
| 1949 | unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth(); |
| 1950 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 1951 | CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS, |
| 1952 | llvm::ConstantInt::get(RHS->getType(), Width)), |
Mike Stump | 15037ca | 2009-12-15 00:35:12 +0000 | [diff] [blame] | 1953 | Cont, CGF.getTrapBB()); |
Mike Stump | be07f60 | 2009-12-14 21:58:14 +0000 | [diff] [blame] | 1954 | CGF.EmitBlock(Cont); |
| 1955 | } |
| 1956 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1957 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 1958 | } |
| 1959 | |
| 1960 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 1961 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 1962 | // RHS to the same size as the LHS. |
| 1963 | Value *RHS = Ops.RHS; |
| 1964 | if (Ops.LHS->getType() != RHS->getType()) |
| 1965 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 1966 | |
Mike Stump | be07f60 | 2009-12-14 21:58:14 +0000 | [diff] [blame] | 1967 | if (CGF.CatchUndefined |
| 1968 | && isa<llvm::IntegerType>(Ops.LHS->getType())) { |
| 1969 | unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth(); |
| 1970 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 1971 | CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS, |
| 1972 | llvm::ConstantInt::get(RHS->getType(), Width)), |
Mike Stump | 15037ca | 2009-12-15 00:35:12 +0000 | [diff] [blame] | 1973 | Cont, CGF.getTrapBB()); |
Mike Stump | be07f60 | 2009-12-14 21:58:14 +0000 | [diff] [blame] | 1974 | CGF.EmitBlock(Cont); |
| 1975 | } |
| 1976 | |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1977 | if (Ops.Ty->hasUnsignedIntegerRepresentation()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1978 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 1979 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 1980 | } |
| 1981 | |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 1982 | enum IntrinsicType { VCMPEQ, VCMPGT }; |
| 1983 | // return corresponding comparison intrinsic for given vector type |
| 1984 | static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT, |
| 1985 | BuiltinType::Kind ElemKind) { |
| 1986 | switch (ElemKind) { |
| 1987 | default: assert(0 && "unexpected element type"); |
| 1988 | case BuiltinType::Char_U: |
| 1989 | case BuiltinType::UChar: |
| 1990 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p : |
| 1991 | llvm::Intrinsic::ppc_altivec_vcmpgtub_p; |
| 1992 | break; |
| 1993 | case BuiltinType::Char_S: |
| 1994 | case BuiltinType::SChar: |
| 1995 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p : |
| 1996 | llvm::Intrinsic::ppc_altivec_vcmpgtsb_p; |
| 1997 | break; |
| 1998 | case BuiltinType::UShort: |
| 1999 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p : |
| 2000 | llvm::Intrinsic::ppc_altivec_vcmpgtuh_p; |
| 2001 | break; |
| 2002 | case BuiltinType::Short: |
| 2003 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p : |
| 2004 | llvm::Intrinsic::ppc_altivec_vcmpgtsh_p; |
| 2005 | break; |
| 2006 | case BuiltinType::UInt: |
| 2007 | case BuiltinType::ULong: |
| 2008 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p : |
| 2009 | llvm::Intrinsic::ppc_altivec_vcmpgtuw_p; |
| 2010 | break; |
| 2011 | case BuiltinType::Int: |
| 2012 | case BuiltinType::Long: |
| 2013 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p : |
| 2014 | llvm::Intrinsic::ppc_altivec_vcmpgtsw_p; |
| 2015 | break; |
| 2016 | case BuiltinType::Float: |
| 2017 | return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpeqfp_p : |
| 2018 | llvm::Intrinsic::ppc_altivec_vcmpgtfp_p; |
| 2019 | break; |
| 2020 | } |
| 2021 | return llvm::Intrinsic::not_intrinsic; |
| 2022 | } |
| 2023 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2024 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 2025 | unsigned SICmpOpc, unsigned FCmpOpc) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2026 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2027 | Value *Result; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2028 | QualType LHSTy = E->getLHS()->getType(); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 2029 | if (const MemberPointerType *MPT = LHSTy->getAs<MemberPointerType>()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2030 | assert(E->getOpcode() == BO_EQ || |
| 2031 | E->getOpcode() == BO_NE); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2032 | Value *LHS = CGF.EmitScalarExpr(E->getLHS()); |
| 2033 | Value *RHS = CGF.EmitScalarExpr(E->getRHS()); |
John McCall | 0bab0cd | 2010-08-23 01:21:21 +0000 | [diff] [blame] | 2034 | Result = CGF.CGM.getCXXABI().EmitMemberPointerComparison( |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2035 | CGF, LHS, RHS, MPT, E->getOpcode() == BO_NE); |
Eli Friedman | b81c786 | 2009-12-11 07:36:43 +0000 | [diff] [blame] | 2036 | } else if (!LHSTy->isAnyComplexType()) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2037 | Value *LHS = Visit(E->getLHS()); |
| 2038 | Value *RHS = Visit(E->getRHS()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2039 | |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 2040 | // If AltiVec, the comparison results in a numeric type, so we use |
| 2041 | // intrinsics comparing vectors and giving 0 or 1 as a result |
| 2042 | if (LHSTy->isVectorType() && CGF.getContext().getLangOptions().AltiVec) { |
| 2043 | // constants for mapping CR6 register bits to predicate result |
| 2044 | enum { CR6_EQ=0, CR6_EQ_REV, CR6_LT, CR6_LT_REV } CR6; |
| 2045 | |
| 2046 | llvm::Intrinsic::ID ID = llvm::Intrinsic::not_intrinsic; |
| 2047 | |
| 2048 | // in several cases vector arguments order will be reversed |
| 2049 | Value *FirstVecArg = LHS, |
| 2050 | *SecondVecArg = RHS; |
| 2051 | |
| 2052 | QualType ElTy = LHSTy->getAs<VectorType>()->getElementType(); |
| 2053 | Type *Ty = CGF.getContext().getCanonicalType(ElTy).getTypePtr(); |
| 2054 | const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty); |
| 2055 | BuiltinType::Kind ElementKind = BTy->getKind(); |
| 2056 | |
| 2057 | switch(E->getOpcode()) { |
| 2058 | default: assert(0 && "is not a comparison operation"); |
| 2059 | case BO_EQ: |
| 2060 | CR6 = CR6_LT; |
| 2061 | ID = GetIntrinsic(VCMPEQ, ElementKind); |
| 2062 | break; |
| 2063 | case BO_NE: |
| 2064 | CR6 = CR6_EQ; |
| 2065 | ID = GetIntrinsic(VCMPEQ, ElementKind); |
| 2066 | break; |
| 2067 | case BO_LT: |
| 2068 | CR6 = CR6_LT; |
| 2069 | ID = GetIntrinsic(VCMPGT, ElementKind); |
| 2070 | std::swap(FirstVecArg, SecondVecArg); |
| 2071 | break; |
| 2072 | case BO_GT: |
| 2073 | CR6 = CR6_LT; |
| 2074 | ID = GetIntrinsic(VCMPGT, ElementKind); |
| 2075 | break; |
| 2076 | case BO_LE: |
| 2077 | if (ElementKind == BuiltinType::Float) { |
| 2078 | CR6 = CR6_LT; |
| 2079 | ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p; |
| 2080 | std::swap(FirstVecArg, SecondVecArg); |
| 2081 | } |
| 2082 | else { |
| 2083 | CR6 = CR6_EQ; |
| 2084 | ID = GetIntrinsic(VCMPGT, ElementKind); |
| 2085 | } |
| 2086 | break; |
| 2087 | case BO_GE: |
| 2088 | if (ElementKind == BuiltinType::Float) { |
| 2089 | CR6 = CR6_LT; |
| 2090 | ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p; |
| 2091 | } |
| 2092 | else { |
| 2093 | CR6 = CR6_EQ; |
| 2094 | ID = GetIntrinsic(VCMPGT, ElementKind); |
| 2095 | std::swap(FirstVecArg, SecondVecArg); |
| 2096 | } |
| 2097 | break; |
| 2098 | } |
| 2099 | |
| 2100 | Value *CR6Param = llvm::ConstantInt::get(CGF.Int32Ty, CR6); |
| 2101 | llvm::Function *F = CGF.CGM.getIntrinsic(ID); |
| 2102 | Result = Builder.CreateCall3(F, CR6Param, FirstVecArg, SecondVecArg, ""); |
| 2103 | return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); |
| 2104 | } |
| 2105 | |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 2106 | if (LHS->getType()->isFPOrFPVectorTy()) { |
Nate Begeman | 7a66d7b | 2008-07-25 20:16:05 +0000 | [diff] [blame] | 2107 | Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2108 | LHS, RHS, "cmp"); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2109 | } else if (LHSTy->hasSignedIntegerRepresentation()) { |
Eli Friedman | ec2c126 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 2110 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2111 | LHS, RHS, "cmp"); |
| 2112 | } else { |
Eli Friedman | ec2c126 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 2113 | // Unsigned integers and pointers. |
| 2114 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2115 | LHS, RHS, "cmp"); |
| 2116 | } |
Chris Lattner | 9c10fcf | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 2117 | |
| 2118 | // If this is a vector comparison, sign extend the result to the appropriate |
| 2119 | // vector integer type and return it (don't convert to bool). |
| 2120 | if (LHSTy->isVectorType()) |
| 2121 | return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2122 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2123 | } else { |
| 2124 | // Complex Comparison: can only be an equality comparison. |
| 2125 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 2126 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2127 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2128 | QualType CETy = LHSTy->getAs<ComplexType>()->getElementType(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2129 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2130 | Value *ResultR, *ResultI; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2131 | if (CETy->isRealFloatingType()) { |
| 2132 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 2133 | LHS.first, RHS.first, "cmp.r"); |
| 2134 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 2135 | LHS.second, RHS.second, "cmp.i"); |
| 2136 | } else { |
| 2137 | // Complex comparisons can only be equality comparisons. As such, signed |
| 2138 | // and unsigned opcodes are the same. |
| 2139 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 2140 | LHS.first, RHS.first, "cmp.r"); |
| 2141 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 2142 | LHS.second, RHS.second, "cmp.i"); |
| 2143 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2144 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2145 | if (E->getOpcode() == BO_EQ) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2146 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 2147 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2148 | assert(E->getOpcode() == BO_NE && |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2149 | "Complex comparison other than == or != ?"); |
| 2150 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 2151 | } |
| 2152 | } |
Nuno Lopes | 32f6209 | 2009-01-11 23:22:37 +0000 | [diff] [blame] | 2153 | |
| 2154 | return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2158 | bool Ignore = TestAndClearIgnoreResultAssign(); |
| 2159 | |
| 2160 | // __block variables need to have the rhs evaluated first, plus this should |
| 2161 | // improve codegen just a little. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2162 | Value *RHS = Visit(E->getRHS()); |
Mike Stump | b14e62d | 2009-12-16 02:57:00 +0000 | [diff] [blame] | 2163 | LValue LHS = EmitCheckedLValue(E->getLHS()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2164 | |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 2165 | // Store the value into the LHS. Bit-fields are handled specially |
Daniel Dunbar | 371d16f | 2008-11-19 11:54:05 +0000 | [diff] [blame] | 2166 | // because the result is altered by the store, i.e., [C99 6.5.16p1] |
| 2167 | // 'An assignment expression has the value of the left operand after |
Eli Friedman | daa24a2 | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 2168 | // the assignment...'. |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2169 | if (LHS.isBitField()) |
| 2170 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(), |
| 2171 | &RHS); |
| 2172 | else |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 2173 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2174 | |
| 2175 | // If the result is clearly ignored, return now. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2176 | if (Ignore) |
| 2177 | return 0; |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2178 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 2179 | // The result of an assignment in C is the assigned r-value. |
| 2180 | if (!CGF.getContext().getLangOptions().CPlusPlus) |
| 2181 | return RHS; |
| 2182 | |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2183 | // Objective-C property assignment never reloads the value following a store. |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame^] | 2184 | if (LHS.isPropertyRef()) |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2185 | return RHS; |
| 2186 | |
| 2187 | // If the lvalue is non-volatile, return the computed value of the assignment. |
| 2188 | if (!LHS.isVolatileQualified()) |
| 2189 | return RHS; |
| 2190 | |
| 2191 | // Otherwise, reload the value. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2192 | return EmitLoadOfLValue(LHS, E->getType()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2196 | const llvm::Type *ResTy = ConvertType(E->getType()); |
| 2197 | |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 2198 | // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. |
| 2199 | // If we have 1 && X, just emit X without inserting the control flow. |
| 2200 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { |
| 2201 | if (Cond == 1) { // If we have 1 && X, just emit X. |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2202 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2203 | // ZExt result to int or bool. |
| 2204 | return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext"); |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2205 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2206 | |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2207 | // 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] | 2208 | if (!CGF.ContainsLabel(E->getRHS())) |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2209 | return llvm::Constant::getNullValue(ResTy); |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2210 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2211 | |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 2212 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); |
| 2213 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 2214 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2215 | // Branch on the LHS first. If it is false, go to the failure (cont) block. |
| 2216 | CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock); |
| 2217 | |
| 2218 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 2219 | // edges from this first condition. All of these values will be false. Start |
| 2220 | // setting up the PHI node in the Cont Block for this. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2221 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), |
| 2222 | "", ContBlock); |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2223 | PN->reserveOperandSpace(2); // Normal case, two inputs. |
| 2224 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 2225 | PI != PE; ++PI) |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 2226 | PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2227 | |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 2228 | CGF.BeginConditionalBranch(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2229 | CGF.EmitBlock(RHSBlock); |
| 2230 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 2231 | CGF.EndConditionalBranch(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2232 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2233 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 2234 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2235 | |
| 2236 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 2237 | // into the phi node for the edge with the value of RHSCond. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2238 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2239 | PN->addIncoming(RHSCond, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2240 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2241 | // ZExt result to int. |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2242 | return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2246 | const llvm::Type *ResTy = ConvertType(E->getType()); |
| 2247 | |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 2248 | // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. |
| 2249 | // If we have 0 || X, just emit X without inserting the control flow. |
| 2250 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { |
| 2251 | if (Cond == -1) { // If we have 0 || X, just emit X. |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2252 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2253 | // ZExt result to int or bool. |
| 2254 | return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext"); |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2255 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2256 | |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2257 | // 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] | 2258 | if (!CGF.ContainsLabel(E->getRHS())) |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2259 | return llvm::ConstantInt::get(ResTy, 1); |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 2260 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2261 | |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 2262 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); |
| 2263 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2264 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2265 | // Branch on the LHS first. If it is true, go to the success (cont) block. |
| 2266 | CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock); |
| 2267 | |
| 2268 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 2269 | // edges from this first condition. All of these values will be true. Start |
| 2270 | // setting up the PHI node in the Cont Block for this. |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2271 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), |
| 2272 | "", ContBlock); |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2273 | PN->reserveOperandSpace(2); // Normal case, two inputs. |
| 2274 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 2275 | PI != PE; ++PI) |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 2276 | PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI); |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2277 | |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 2278 | CGF.BeginConditionalBranch(); |
Anders Carlsson | 33da07d | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 2279 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2280 | // Emit the RHS condition as a bool value. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2281 | CGF.EmitBlock(RHSBlock); |
| 2282 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2283 | |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 2284 | CGF.EndConditionalBranch(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2285 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2286 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 2287 | RHSBlock = Builder.GetInsertBlock(); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2288 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 2289 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 2290 | // into the phi node for the edge with the value of RHSCond. |
| 2291 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2292 | PN->addIncoming(RHSCond, RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2293 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2294 | // ZExt result to int. |
Chris Lattner | 7804bcb | 2009-10-17 04:24:20 +0000 | [diff] [blame] | 2295 | return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 2299 | CGF.EmitStmt(E->getLHS()); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2300 | CGF.EnsureInsertPoint(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2301 | return Visit(E->getRHS()); |
| 2302 | } |
| 2303 | |
| 2304 | //===----------------------------------------------------------------------===// |
| 2305 | // Other Operators |
| 2306 | //===----------------------------------------------------------------------===// |
| 2307 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2308 | /// isCheapEnoughToEvaluateUnconditionally - Return true if the specified |
| 2309 | /// expression is cheap enough and side-effect-free enough to evaluate |
| 2310 | /// unconditionally instead of conditionally. This is used to convert control |
| 2311 | /// flow into selects in some cases. |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 2312 | static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E, |
| 2313 | CodeGenFunction &CGF) { |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2314 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 2315 | return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr(), CGF); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2316 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2317 | // TODO: Allow anything we can constant fold to an integer or fp constant. |
| 2318 | if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) || |
| 2319 | isa<FloatingLiteral>(E)) |
| 2320 | return true; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2321 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2322 | // Non-volatile automatic variables too, to get "cond ? X : Y" where |
| 2323 | // X and Y are local variables. |
| 2324 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 2325 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 2326 | if (VD->hasLocalStorage() && !(CGF.getContext() |
| 2327 | .getCanonicalType(VD->getType()) |
| 2328 | .isVolatileQualified())) |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2329 | return true; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2330 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2331 | return false; |
| 2332 | } |
| 2333 | |
| 2334 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2335 | Value *ScalarExprEmitter:: |
| 2336 | VisitConditionalOperator(const ConditionalOperator *E) { |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2337 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 2338 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2339 | // the condition and the dead arm. |
| 2340 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){ |
Chris Lattner | c657e92 | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 2341 | Expr *Live = E->getLHS(), *Dead = E->getRHS(); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 2342 | if (Cond == -1) |
Chris Lattner | c657e92 | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 2343 | std::swap(Live, Dead); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2344 | |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 2345 | // If the dead side doesn't have labels we need, and if the Live side isn't |
| 2346 | // the gnu missing ?: extension (which we could handle, but don't bother |
| 2347 | // to), just emit the Live part. |
| 2348 | if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part |
| 2349 | Live) // Live part isn't missing. |
| 2350 | return Visit(Live); |
Chris Lattner | c657e92 | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 2351 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2352 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 2353 | // OpenCL: If the condition is a vector, we can treat this condition like |
| 2354 | // the select function. |
| 2355 | if (CGF.getContext().getLangOptions().OpenCL |
| 2356 | && E->getCond()->getType()->isVectorType()) { |
| 2357 | llvm::Value *CondV = CGF.EmitScalarExpr(E->getCond()); |
| 2358 | llvm::Value *LHS = Visit(E->getLHS()); |
| 2359 | llvm::Value *RHS = Visit(E->getRHS()); |
| 2360 | |
| 2361 | const llvm::Type *condType = ConvertType(E->getCond()->getType()); |
| 2362 | const llvm::VectorType *vecTy = cast<llvm::VectorType>(condType); |
| 2363 | |
| 2364 | unsigned numElem = vecTy->getNumElements(); |
| 2365 | const llvm::Type *elemType = vecTy->getElementType(); |
| 2366 | |
| 2367 | std::vector<llvm::Constant*> Zvals; |
| 2368 | for (unsigned i = 0; i < numElem; ++i) |
| 2369 | Zvals.push_back(llvm::ConstantInt::get(elemType,0)); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2370 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 2371 | llvm::Value *zeroVec = llvm::ConstantVector::get(Zvals); |
| 2372 | llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec); |
| 2373 | llvm::Value *tmp = Builder.CreateSExt(TestMSB, |
| 2374 | llvm::VectorType::get(elemType, |
| 2375 | numElem), |
| 2376 | "sext"); |
| 2377 | llvm::Value *tmp2 = Builder.CreateNot(tmp); |
| 2378 | |
| 2379 | // Cast float to int to perform ANDs if necessary. |
| 2380 | llvm::Value *RHSTmp = RHS; |
| 2381 | llvm::Value *LHSTmp = LHS; |
| 2382 | bool wasCast = false; |
| 2383 | const llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType()); |
| 2384 | if (rhsVTy->getElementType()->isFloatTy()) { |
| 2385 | RHSTmp = Builder.CreateBitCast(RHS, tmp2->getType()); |
| 2386 | LHSTmp = Builder.CreateBitCast(LHS, tmp->getType()); |
| 2387 | wasCast = true; |
| 2388 | } |
| 2389 | |
| 2390 | llvm::Value *tmp3 = Builder.CreateAnd(RHSTmp, tmp2); |
| 2391 | llvm::Value *tmp4 = Builder.CreateAnd(LHSTmp, tmp); |
| 2392 | llvm::Value *tmp5 = Builder.CreateOr(tmp3, tmp4, "cond"); |
| 2393 | if (wasCast) |
| 2394 | tmp5 = Builder.CreateBitCast(tmp5, RHS->getType()); |
| 2395 | |
| 2396 | return tmp5; |
| 2397 | } |
| 2398 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2399 | // If this is a really simple expression (like x ? 4 : 5), emit this as a |
| 2400 | // 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] | 2401 | // safe to evaluate the LHS and RHS unconditionally. |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 2402 | if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS(), |
| 2403 | CGF) && |
| 2404 | isCheapEnoughToEvaluateUnconditionally(E->getRHS(), CGF)) { |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 2405 | llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond()); |
| 2406 | llvm::Value *LHS = Visit(E->getLHS()); |
| 2407 | llvm::Value *RHS = Visit(E->getRHS()); |
| 2408 | return Builder.CreateSelect(CondV, LHS, RHS, "cond"); |
| 2409 | } |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2410 | |
Daniel Dunbar | be65abc | 2008-11-12 10:13:37 +0000 | [diff] [blame] | 2411 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 2412 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 2413 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Fariborz Jahanian | af9b968 | 2010-09-17 15:51:28 +0000 | [diff] [blame] | 2414 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2415 | // If we don't have the GNU missing condition extension, emit a branch on bool |
| 2416 | // the normal way. |
Chris Lattner | 12d152f | 2009-02-13 23:35:32 +0000 | [diff] [blame] | 2417 | if (E->getLHS()) { |
| 2418 | // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for |
| 2419 | // the branch on bool. |
| 2420 | CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); |
| 2421 | } else { |
| 2422 | // Otherwise, for the ?: extension, evaluate the conditional and then |
| 2423 | // convert it to bool the hard way. We do this explicitly because we need |
| 2424 | // the unconverted value for the missing middle value of the ?:. |
Fariborz Jahanian | af9b968 | 2010-09-17 15:51:28 +0000 | [diff] [blame] | 2425 | Expr *save = E->getSAVE(); |
| 2426 | assert(save && "VisitConditionalOperator - save is null"); |
| 2427 | // Intentianlly not doing direct assignment to ConditionalSaveExprs[save] !! |
| 2428 | Value *SaveVal = CGF.EmitScalarExpr(save); |
| 2429 | CGF.ConditionalSaveExprs[save] = SaveVal; |
| 2430 | Value *CondVal = Visit(E->getCond()); |
Chris Lattner | 12d152f | 2009-02-13 23:35:32 +0000 | [diff] [blame] | 2431 | // In some cases, EmitScalarConversion will delete the "CondVal" expression |
| 2432 | // if there are no extra uses (an optimization). Inhibit this by making an |
| 2433 | // extra dead use, because we're going to add a use of CondVal later. We |
| 2434 | // don't use the builder for this, because we don't want it to get optimized |
| 2435 | // away. This leaves dead code, but the ?: extension isn't common. |
| 2436 | new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder", |
| 2437 | Builder.GetInsertBlock()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2438 | |
Chris Lattner | 035cf42 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 2439 | Value *CondBoolVal = |
| 2440 | CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), |
| 2441 | CGF.getContext().BoolTy); |
| 2442 | Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); |
Chris Lattner | 035cf42 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 2443 | } |
Anders Carlsson | fb6fa30 | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 2444 | |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 2445 | CGF.BeginConditionalBranch(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2446 | CGF.EmitBlock(LHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2448 | // Handle the GNU extension for missing LHS. |
Fariborz Jahanian | af9b968 | 2010-09-17 15:51:28 +0000 | [diff] [blame] | 2449 | Value *LHS = Visit(E->getTrueExpr()); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2450 | |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 2451 | CGF.EndConditionalBranch(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2452 | LHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 2453 | CGF.EmitBranch(ContBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2454 | |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 2455 | CGF.BeginConditionalBranch(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2456 | CGF.EmitBlock(RHSBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2457 | |
Eli Friedman | 856226c | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 2458 | Value *RHS = Visit(E->getRHS()); |
Anders Carlsson | 72119a8 | 2010-02-04 17:18:07 +0000 | [diff] [blame] | 2459 | CGF.EndConditionalBranch(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2460 | RHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 2461 | CGF.EmitBranch(ContBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2462 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2463 | CGF.EmitBlock(ContBlock); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2464 | |
Eli Friedman | 48daf59 | 2009-12-07 20:25:53 +0000 | [diff] [blame] | 2465 | // If the LHS or RHS is a throw expression, it will be legitimately null. |
| 2466 | if (!LHS) |
| 2467 | return RHS; |
| 2468 | if (!RHS) |
| 2469 | return LHS; |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2470 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2471 | // Create a PHI node for the real part. |
| 2472 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); |
| 2473 | PN->reserveOperandSpace(2); |
| 2474 | PN->addIncoming(LHS, LHSBlock); |
| 2475 | PN->addIncoming(RHS, RHSBlock); |
| 2476 | return PN; |
| 2477 | } |
| 2478 | |
| 2479 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 2480 | return Visit(E->getChosenSubExpr(CGF.getContext())); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 2483 | Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 2484 | llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 2485 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); |
| 2486 | |
| 2487 | // If EmitVAArg fails, we fall back to the LLVM instruction. |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2488 | if (!ArgPtr) |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 2489 | return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); |
| 2490 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2491 | // FIXME Volatility. |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 2492 | return Builder.CreateLoad(ArgPtr); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Mike Stump | df6b68c | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 2495 | Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 2496 | return CGF.BuildBlockLiteralTmp(BE); |
Mike Stump | df6b68c | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2499 | //===----------------------------------------------------------------------===// |
| 2500 | // Entry Point into this File |
| 2501 | //===----------------------------------------------------------------------===// |
| 2502 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2503 | /// EmitScalarExpr - Emit the computation of the specified expression of scalar |
| 2504 | /// type, ignoring the result. |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2505 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2506 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 2507 | "Invalid scalar expression to emit"); |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2508 | |
Mike Stump | 7f79f9b | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 2509 | return ScalarExprEmitter(*this, IgnoreResultAssign) |
| 2510 | .Visit(const_cast<Expr*>(E)); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 2511 | } |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 2512 | |
| 2513 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 2514 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2515 | Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, |
| 2516 | QualType DstTy) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 2517 | assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && |
| 2518 | "Invalid scalar expression to emit"); |
| 2519 | return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); |
| 2520 | } |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2521 | |
Mike Stump | db52dcd | 2009-09-09 13:00:44 +0000 | [diff] [blame] | 2522 | /// EmitComplexToScalarConversion - Emit a conversion from the specified complex |
| 2523 | /// type to the specified destination type, where the destination type is an |
| 2524 | /// LLVM scalar type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2525 | Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, |
| 2526 | QualType SrcTy, |
| 2527 | QualType DstTy) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 2528 | assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) && |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 2529 | "Invalid complex -> scalar conversion"); |
| 2530 | return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, |
| 2531 | DstTy); |
| 2532 | } |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 2533 | |
Chris Lattner | 8c11a65 | 2010-06-26 22:09:34 +0000 | [diff] [blame] | 2534 | |
| 2535 | llvm::Value *CodeGenFunction:: |
| 2536 | EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, |
| 2537 | bool isInc, bool isPre) { |
| 2538 | return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre); |
| 2539 | } |
| 2540 | |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 2541 | LValue CodeGenFunction::EmitObjCIsaExpr(const ObjCIsaExpr *E) { |
| 2542 | llvm::Value *V; |
| 2543 | // object->isa or (*object).isa |
| 2544 | // Generate code as for: *(Class*)object |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 2545 | // build Class* type |
| 2546 | const llvm::Type *ClassPtrTy = ConvertType(E->getType()); |
Fariborz Jahanian | 5ed676c | 2010-02-05 19:18:30 +0000 | [diff] [blame] | 2547 | |
| 2548 | Expr *BaseExpr = E->getBase(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2549 | if (BaseExpr->isRValue()) { |
Fariborz Jahanian | 5ed676c | 2010-02-05 19:18:30 +0000 | [diff] [blame] | 2550 | V = CreateTempAlloca(ClassPtrTy, "resval"); |
| 2551 | llvm::Value *Src = EmitScalarExpr(BaseExpr); |
| 2552 | Builder.CreateStore(Src, V); |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2553 | V = ScalarExprEmitter(*this).EmitLoadOfLValue( |
| 2554 | MakeAddrLValue(V, E->getType()), E->getType()); |
| 2555 | } else { |
| 2556 | if (E->isArrow()) |
| 2557 | V = ScalarExprEmitter(*this).EmitLoadOfLValue(BaseExpr); |
| 2558 | else |
| 2559 | V = EmitLValue(BaseExpr).getAddress(); |
Fariborz Jahanian | 5ed676c | 2010-02-05 19:18:30 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | // build Class* type |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 2563 | ClassPtrTy = ClassPtrTy->getPointerTo(); |
| 2564 | V = Builder.CreateBitCast(V, ClassPtrTy); |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 2565 | return MakeAddrLValue(V, E->getType()); |
Fariborz Jahanian | 820bca4 | 2009-12-09 23:35:29 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2568 | |
| 2569 | LValue CodeGenFunction::EmitCompoundAssignOperatorLValue( |
| 2570 | const CompoundAssignOperator *E) { |
| 2571 | ScalarExprEmitter Scalar(*this); |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2572 | Value *Result = 0; |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2573 | switch (E->getOpcode()) { |
| 2574 | #define COMPOUND_OP(Op) \ |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2575 | case BO_##Op##Assign: \ |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2576 | return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \ |
Daniel Dunbar | d7f7d08 | 2010-06-29 22:00:45 +0000 | [diff] [blame] | 2577 | Result) |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2578 | COMPOUND_OP(Mul); |
| 2579 | COMPOUND_OP(Div); |
| 2580 | COMPOUND_OP(Rem); |
| 2581 | COMPOUND_OP(Add); |
| 2582 | COMPOUND_OP(Sub); |
| 2583 | COMPOUND_OP(Shl); |
| 2584 | COMPOUND_OP(Shr); |
| 2585 | COMPOUND_OP(And); |
| 2586 | COMPOUND_OP(Xor); |
| 2587 | COMPOUND_OP(Or); |
| 2588 | #undef COMPOUND_OP |
| 2589 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2590 | case BO_PtrMemD: |
| 2591 | case BO_PtrMemI: |
| 2592 | case BO_Mul: |
| 2593 | case BO_Div: |
| 2594 | case BO_Rem: |
| 2595 | case BO_Add: |
| 2596 | case BO_Sub: |
| 2597 | case BO_Shl: |
| 2598 | case BO_Shr: |
| 2599 | case BO_LT: |
| 2600 | case BO_GT: |
| 2601 | case BO_LE: |
| 2602 | case BO_GE: |
| 2603 | case BO_EQ: |
| 2604 | case BO_NE: |
| 2605 | case BO_And: |
| 2606 | case BO_Xor: |
| 2607 | case BO_Or: |
| 2608 | case BO_LAnd: |
| 2609 | case BO_LOr: |
| 2610 | case BO_Assign: |
| 2611 | case BO_Comma: |
Douglas Gregor | 6a03e34 | 2010-04-23 04:16:32 +0000 | [diff] [blame] | 2612 | assert(false && "Not valid compound assignment operators"); |
| 2613 | break; |
| 2614 | } |
| 2615 | |
| 2616 | llvm_unreachable("Unhandled compound assignment operator"); |
| 2617 | } |