Chris Lattner | 9fba49a | 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 | 959e5be | 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 | 9fba49a | 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 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
Daniel Dunbar | eee5cd1 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | fa45624 | 2008-08-12 05:08:18 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 63f1ad9 | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecordLayout.h" |
Daniel Dunbar | eee5cd1 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | d54d1f2 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/Function.h" |
Anders Carlsson | 36f07d8 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalVariable.h" |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 24 | #include "llvm/Intrinsics.h" |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CFG.h" |
Mike Stump | fca5da0 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Chris Lattner | c212668 | 2008-01-03 07:05:49 +0000 | [diff] [blame] | 29 | #include <cstdarg> |
Ted Kremenek | 03cf4df | 2007-12-10 23:44:32 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | using llvm::Value; |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // Scalar Expression Emitter |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | |
| 39 | struct BinOpInfo { |
| 40 | Value *LHS; |
| 41 | Value *RHS; |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 42 | QualType Ty; // Computation Type. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 43 | const BinaryOperator *E; |
| 44 | }; |
| 45 | |
| 46 | namespace { |
| 47 | class VISIBILITY_HIDDEN ScalarExprEmitter |
| 48 | : public StmtVisitor<ScalarExprEmitter, Value*> { |
| 49 | CodeGenFunction &CGF; |
Daniel Dunbar | d916e6e | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 50 | CGBuilderTy &Builder; |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 51 | bool IgnoreResultAssign; |
Owen Anderson | 73e7f80 | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 52 | llvm::LLVMContext &VMContext; |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 53 | public: |
| 54 | |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 55 | ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false) |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 56 | : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira), |
Owen Anderson | 73e7f80 | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 57 | VMContext(cgf.getLLVMContext()) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 58 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 59 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 60 | //===--------------------------------------------------------------------===// |
| 61 | // Utilities |
| 62 | //===--------------------------------------------------------------------===// |
| 63 | |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 64 | bool TestAndClearIgnoreResultAssign() { |
Chris Lattner | 08ac852 | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 65 | bool I = IgnoreResultAssign; |
| 66 | IgnoreResultAssign = false; |
| 67 | return I; |
| 68 | } |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 70 | const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } |
| 71 | LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } |
| 72 | |
| 73 | Value *EmitLoadOfLValue(LValue LV, QualType T) { |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 74 | return CGF.EmitLoadOfLValue(LV, T).getScalarVal(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 75 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 76 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 77 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 78 | /// value l-value, this method emits the address of the l-value, then loads |
| 79 | /// and returns the result. |
| 80 | Value *EmitLoadOfLValue(const Expr *E) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 81 | return EmitLoadOfLValue(EmitLValue(E), E->getType()); |
| 82 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 83 | |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 84 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 0594206 | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 85 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 86 | Value *EmitConversionToBool(Value *Src, QualType DstTy); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 87 | |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 88 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 89 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 90 | Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy); |
| 91 | |
| 92 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 93 | /// complex type to the specified destination type, where the destination type |
| 94 | /// is an LLVM scalar type. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 95 | Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 96 | QualType SrcTy, QualType DstTy); |
Mike Stump | 4eb81dc | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 98 | //===--------------------------------------------------------------------===// |
| 99 | // Visitor Methods |
| 100 | //===--------------------------------------------------------------------===// |
| 101 | |
| 102 | Value *VisitStmt(Stmt *S) { |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 103 | S->dump(CGF.getContext().getSourceManager()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 104 | assert(0 && "Stmt can't have complex result type!"); |
| 105 | return 0; |
| 106 | } |
| 107 | Value *VisitExpr(Expr *S); |
| 108 | Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); } |
| 109 | |
| 110 | // Leaves. |
| 111 | Value *VisitIntegerLiteral(const IntegerLiteral *E) { |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 112 | return llvm::ConstantInt::get(VMContext, E->getValue()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 113 | } |
| 114 | Value *VisitFloatingLiteral(const FloatingLiteral *E) { |
Owen Anderson | b56fd0b | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 115 | return llvm::ConstantFP::get(VMContext, E->getValue()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 116 | } |
| 117 | Value *VisitCharacterLiteral(const CharacterLiteral *E) { |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 118 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 119 | } |
Nate Begeman | e9bfe6d | 2007-11-15 05:40:03 +0000 | [diff] [blame] | 120 | Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 121 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
Nate Begeman | e9bfe6d | 2007-11-15 05:40:03 +0000 | [diff] [blame] | 122 | } |
Argiris Kirtzidis | 750eb97 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 123 | Value *VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 124 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Argiris Kirtzidis | 750eb97 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 125 | } |
Anders Carlsson | 774f9c7 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 126 | Value *VisitGNUNullExpr(const GNUNullExpr *E) { |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 127 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Anders Carlsson | 774f9c7 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 128 | } |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 129 | Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 130 | return llvm::ConstantInt::get(ConvertType(E->getType()), |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 131 | CGF.getContext().typesAreCompatible( |
| 132 | E->getArgType1(), E->getArgType2())); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 133 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 134 | Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 135 | Value *VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 136 | llvm::Value *V = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 137 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), |
Daniel Dunbar | b5fda0c | 2008-08-16 01:41:47 +0000 | [diff] [blame] | 138 | CGF.GetIDForAddrOfLabel(E->getLabel())); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 139 | |
Daniel Dunbar | b5fda0c | 2008-08-16 01:41:47 +0000 | [diff] [blame] | 140 | return Builder.CreateIntToPtr(V, ConvertType(E->getType())); |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 141 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 142 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 143 | // l-values. |
| 144 | Value *VisitDeclRefExpr(DeclRefExpr *E) { |
| 145 | if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl())) |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 146 | return llvm::ConstantInt::get(VMContext, EC->getInitVal()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 147 | return EmitLoadOfLValue(E); |
| 148 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 149 | Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) { |
| 150 | return CGF.EmitObjCSelectorExpr(E); |
Daniel Dunbar | 91cc402 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 151 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 152 | Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) { |
| 153 | return CGF.EmitObjCProtocolExpr(E); |
Daniel Dunbar | 91cc402 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 154 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 155 | Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
Daniel Dunbar | 91cc402 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 156 | return EmitLoadOfLValue(E); |
| 157 | } |
Daniel Dunbar | 5e10589 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 158 | Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { |
Daniel Dunbar | e6c3175 | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 159 | return EmitLoadOfLValue(E); |
Daniel Dunbar | 91cc402 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 160 | } |
Fariborz Jahanian | 128cdc5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 161 | Value *VisitObjCImplicitSetterGetterRefExpr( |
| 162 | ObjCImplicitSetterGetterRefExpr *E) { |
Fariborz Jahanian | b0973da | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 163 | return EmitLoadOfLValue(E); |
| 164 | } |
Daniel Dunbar | 91cc402 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 165 | Value *VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 166 | return CGF.EmitObjCMessageExpr(E).getScalarVal(); |
Daniel Dunbar | 5e10589 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 169 | Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | d0e9d09 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 170 | Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 171 | Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 172 | Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | a917798 | 2008-10-26 23:53:12 +0000 | [diff] [blame] | 173 | Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 174 | return EmitLoadOfLValue(E); |
| 175 | } |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 176 | Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); } |
Chris Lattner | c5d3263 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 177 | Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
| 178 | return EmitLValue(E).getAddress(); |
| 179 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 180 | |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 181 | Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); } |
Devang Patel | 01ab130 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 182 | |
| 183 | Value *VisitInitListExpr(InitListExpr *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 184 | bool Ignore = TestAndClearIgnoreResultAssign(); |
| 185 | (void)Ignore; |
| 186 | assert (Ignore == false && "init list ignored"); |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 187 | unsigned NumInitElements = E->getNumInits(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 188 | |
Douglas Gregor | 9fddded | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 189 | if (E->hadArrayRangeDesignator()) { |
| 190 | CGF.ErrorUnsupported(E, "GNU array range designator extension"); |
| 191 | } |
| 192 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 193 | const llvm::VectorType *VType = |
Anders Carlsson | 35ab4f9 | 2008-01-29 01:15:48 +0000 | [diff] [blame] | 194 | dyn_cast<llvm::VectorType>(ConvertType(E->getType())); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 195 | |
Anders Carlsson | 35ab4f9 | 2008-01-29 01:15:48 +0000 | [diff] [blame] | 196 | // We have a scalar in braces. Just use the first element. |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 197 | if (!VType) |
Anders Carlsson | 35ab4f9 | 2008-01-29 01:15:48 +0000 | [diff] [blame] | 198 | return Visit(E->getInit(0)); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 199 | |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 200 | unsigned NumVectorElements = VType->getNumElements(); |
| 201 | const llvm::Type *ElementType = VType->getElementType(); |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 202 | |
| 203 | // Emit individual vector element stores. |
Owen Anderson | e0b5eff | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 204 | llvm::Value *V = llvm::UndefValue::get(VType); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 205 | |
Anders Carlsson | 323d568 | 2007-12-18 02:45:33 +0000 | [diff] [blame] | 206 | // Emit initializers |
| 207 | unsigned i; |
| 208 | for (i = 0; i < NumInitElements; ++i) { |
Devang Patel | 32c3983 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 209 | Value *NewV = Visit(E->getInit(i)); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 210 | Value *Idx = |
| 211 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), i); |
Devang Patel | 32c3983 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 212 | V = Builder.CreateInsertElement(V, NewV, Idx); |
Devang Patel | 01ab130 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 213 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 214 | |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 215 | // Emit remaining default initializers |
| 216 | for (/* Do not initialize i*/; i < NumVectorElements; ++i) { |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 217 | Value *Idx = |
| 218 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), i); |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 219 | llvm::Value *NewV = llvm::Constant::getNullValue(ElementType); |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 220 | V = Builder.CreateInsertElement(V, NewV, Idx); |
| 221 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 222 | |
Devang Patel | 32c3983 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 223 | return V; |
Devang Patel | 01ab130 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 224 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 225 | |
Douglas Gregor | c9e012a | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 226 | Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 227 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Douglas Gregor | c9e012a | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 228 | } |
Eli Friedman | a7ef8e5 | 2009-04-20 03:54:15 +0000 | [diff] [blame] | 229 | Value *VisitCastExpr(const CastExpr *E) { |
Fariborz Jahanian | c8a336f | 2009-08-26 23:31:30 +0000 | [diff] [blame] | 230 | if (E->getCastKind() == CastExpr::CK_UserDefinedConversion) { |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 231 | if (const CXXFunctionalCastExpr *CXXFExpr = |
Fariborz Jahanian | ec17213 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 232 | dyn_cast<CXXFunctionalCastExpr>(E)) |
| 233 | return CGF.EmitCXXFunctionalCastExpr(CXXFExpr).getScalarVal(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 234 | assert(isa<CStyleCastExpr>(E) && |
Fariborz Jahanian | ec17213 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 235 | "VisitCastExpr - missing CStyleCastExpr"); |
Fariborz Jahanian | c8a336f | 2009-08-26 23:31:30 +0000 | [diff] [blame] | 236 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 237 | |
Eli Friedman | a7ef8e5 | 2009-04-20 03:54:15 +0000 | [diff] [blame] | 238 | // Make sure to evaluate VLA bounds now so that we have them for later. |
| 239 | if (E->getType()->isVariablyModifiedType()) |
| 240 | CGF.EmitVLASize(E->getType()); |
| 241 | |
Anders Carlsson | 2c61dbe | 2009-08-24 18:12:39 +0000 | [diff] [blame] | 242 | return EmitCastExpr(E->getSubExpr(), E->getType(), E->getCastKind()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 243 | } |
Anders Carlsson | 2c61dbe | 2009-08-24 18:12:39 +0000 | [diff] [blame] | 244 | Value *EmitCastExpr(const Expr *E, QualType T, CastExpr::CastKind Kind); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 245 | |
| 246 | Value *VisitCallExpr(const CallExpr *E) { |
Anders Carlsson | cd29528 | 2009-05-27 03:37:57 +0000 | [diff] [blame] | 247 | if (E->getCallReturnType()->isReferenceType()) |
| 248 | return EmitLoadOfLValue(E); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 249 | |
Chris Lattner | e24c4cf | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 250 | return CGF.EmitCallExpr(E).getScalarVal(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 251 | } |
Daniel Dunbar | a04840b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 252 | |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 253 | Value *VisitStmtExpr(const StmtExpr *E); |
Mike Stump | fca5da0 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 254 | |
Mike Stump | 2b6933f | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 255 | Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 256 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 257 | // Unary Operators. |
| 258 | Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre); |
| 259 | Value *VisitUnaryPostDec(const UnaryOperator *E) { |
| 260 | return VisitPrePostIncDec(E, false, false); |
| 261 | } |
| 262 | Value *VisitUnaryPostInc(const UnaryOperator *E) { |
| 263 | return VisitPrePostIncDec(E, true, false); |
| 264 | } |
| 265 | Value *VisitUnaryPreDec(const UnaryOperator *E) { |
| 266 | return VisitPrePostIncDec(E, false, true); |
| 267 | } |
| 268 | Value *VisitUnaryPreInc(const UnaryOperator *E) { |
| 269 | return VisitPrePostIncDec(E, true, true); |
| 270 | } |
| 271 | Value *VisitUnaryAddrOf(const UnaryOperator *E) { |
| 272 | return EmitLValue(E->getSubExpr()).getAddress(); |
| 273 | } |
| 274 | Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
| 275 | Value *VisitUnaryPlus(const UnaryOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 276 | // This differs from gcc, though, most likely due to a bug in gcc. |
| 277 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 278 | return Visit(E->getSubExpr()); |
| 279 | } |
| 280 | Value *VisitUnaryMinus (const UnaryOperator *E); |
| 281 | Value *VisitUnaryNot (const UnaryOperator *E); |
| 282 | Value *VisitUnaryLNot (const UnaryOperator *E); |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 283 | Value *VisitUnaryReal (const UnaryOperator *E); |
| 284 | Value *VisitUnaryImag (const UnaryOperator *E); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 285 | Value *VisitUnaryExtension(const UnaryOperator *E) { |
| 286 | return Visit(E->getSubExpr()); |
| 287 | } |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 288 | Value *VisitUnaryOffsetOf(const UnaryOperator *E); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 289 | |
Anders Carlsson | 49d4a57 | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 290 | // C++ |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 291 | Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 292 | return Visit(DAE->getExpr()); |
| 293 | } |
Anders Carlsson | 49d4a57 | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 294 | Value *VisitCXXThisExpr(CXXThisExpr *TE) { |
| 295 | return CGF.LoadCXXThis(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 296 | } |
| 297 | |
Anders Carlsson | 272b5f5 | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 298 | Value *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { |
Anders Carlsson | d677560 | 2009-05-31 00:09:15 +0000 | [diff] [blame] | 299 | return CGF.EmitCXXExprWithTemporaries(E).getScalarVal(); |
Anders Carlsson | 272b5f5 | 2009-05-19 04:48:36 +0000 | [diff] [blame] | 300 | } |
Anders Carlsson | 18e88bc | 2009-05-31 01:40:14 +0000 | [diff] [blame] | 301 | Value *VisitCXXNewExpr(const CXXNewExpr *E) { |
| 302 | return CGF.EmitCXXNewExpr(E); |
| 303 | } |
Anders Carlsson | 133fdaf | 2009-08-16 21:13:42 +0000 | [diff] [blame] | 304 | Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
| 305 | CGF.EmitCXXDeleteExpr(E); |
| 306 | return 0; |
| 307 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 308 | |
Douglas Gregor | 3e36851 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 309 | Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) { |
| 310 | // C++ [expr.pseudo]p1: |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 311 | // The result shall only be used as the operand for the function call |
Douglas Gregor | 3e36851 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 312 | // operator (), and the result of such a call has type void. The only |
| 313 | // effect is the evaluation of the postfix-expression before the dot or |
| 314 | // arrow. |
| 315 | CGF.EmitScalarExpr(E->getBase()); |
| 316 | return 0; |
| 317 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 318 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 319 | // Binary Operators. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 320 | Value *EmitMul(const BinOpInfo &Ops) { |
Mike Stump | f71b774 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 321 | if (CGF.getContext().getLangOptions().OverflowChecking |
| 322 | && Ops.Ty->isSignedIntegerType()) |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 323 | return EmitOverflowCheckedBinOp(Ops); |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 324 | if (Ops.LHS->getType()->isFPOrFPVector()) |
| 325 | return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 326 | return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); |
| 327 | } |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 328 | /// Create a binary op that checks for overflow. |
| 329 | /// Currently only supports +, - and *. |
| 330 | Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 331 | Value *EmitDiv(const BinOpInfo &Ops); |
| 332 | Value *EmitRem(const BinOpInfo &Ops); |
| 333 | Value *EmitAdd(const BinOpInfo &Ops); |
| 334 | Value *EmitSub(const BinOpInfo &Ops); |
| 335 | Value *EmitShl(const BinOpInfo &Ops); |
| 336 | Value *EmitShr(const BinOpInfo &Ops); |
| 337 | Value *EmitAnd(const BinOpInfo &Ops) { |
| 338 | return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); |
| 339 | } |
| 340 | Value *EmitXor(const BinOpInfo &Ops) { |
| 341 | return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); |
| 342 | } |
| 343 | Value *EmitOr (const BinOpInfo &Ops) { |
| 344 | return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); |
| 345 | } |
| 346 | |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 347 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
Chris Lattner | 0d96530 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 348 | Value *EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 349 | Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); |
| 350 | |
| 351 | // Binary operators and binary compound assignment operators. |
| 352 | #define HANDLEBINOP(OP) \ |
Chris Lattner | 0d96530 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 353 | Value *VisitBin ## OP(const BinaryOperator *E) { \ |
| 354 | return Emit ## OP(EmitBinOps(E)); \ |
| 355 | } \ |
| 356 | Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ |
| 357 | return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 358 | } |
| 359 | HANDLEBINOP(Mul); |
| 360 | HANDLEBINOP(Div); |
| 361 | HANDLEBINOP(Rem); |
| 362 | HANDLEBINOP(Add); |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 363 | HANDLEBINOP(Sub); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 364 | HANDLEBINOP(Shl); |
| 365 | HANDLEBINOP(Shr); |
| 366 | HANDLEBINOP(And); |
| 367 | HANDLEBINOP(Xor); |
| 368 | HANDLEBINOP(Or); |
| 369 | #undef HANDLEBINOP |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 371 | // Comparisons. |
| 372 | Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, |
| 373 | unsigned SICmpOpc, unsigned FCmpOpc); |
| 374 | #define VISITCOMP(CODE, UI, SI, FP) \ |
| 375 | Value *VisitBin##CODE(const BinaryOperator *E) { \ |
| 376 | return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ |
| 377 | llvm::FCmpInst::FP); } |
| 378 | VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT); |
| 379 | VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT); |
| 380 | VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE); |
| 381 | VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE); |
| 382 | VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ); |
| 383 | VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE); |
| 384 | #undef VISITCOMP |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 385 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 386 | Value *VisitBinAssign (const BinaryOperator *E); |
| 387 | |
| 388 | Value *VisitBinLAnd (const BinaryOperator *E); |
| 389 | Value *VisitBinLOr (const BinaryOperator *E); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 390 | Value *VisitBinComma (const BinaryOperator *E); |
| 391 | |
| 392 | // Other Operators. |
Mike Stump | 4eb81dc | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 393 | Value *VisitBlockExpr(const BlockExpr *BE); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 394 | Value *VisitConditionalOperator(const ConditionalOperator *CO); |
| 395 | Value *VisitChooseExpr(ChooseExpr *CE); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 396 | Value *VisitVAArgExpr(VAArgExpr *VE); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 397 | Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 398 | return CGF.EmitObjCStringLiteral(E); |
| 399 | } |
| 400 | }; |
| 401 | } // end anonymous namespace. |
| 402 | |
| 403 | //===----------------------------------------------------------------------===// |
| 404 | // Utilities |
| 405 | //===----------------------------------------------------------------------===// |
| 406 | |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 407 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 0594206 | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 408 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 409 | Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { |
| 410 | assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 411 | |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 412 | if (SrcType->isRealFloatingType()) { |
| 413 | // Compare against 0.0 for fp scalars. |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 414 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 415 | return Builder.CreateFCmpUNE(Src, Zero, "tobool"); |
| 416 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 417 | |
Anders Carlsson | e3ee66c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 418 | if (SrcType->isMemberPointerType()) { |
| 419 | // FIXME: This is ABI specific. |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 420 | |
Anders Carlsson | e3ee66c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 421 | // Compare against -1. |
| 422 | llvm::Value *NegativeOne = llvm::Constant::getAllOnesValue(Src->getType()); |
| 423 | return Builder.CreateICmpNE(Src, NegativeOne, "tobool"); |
| 424 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 425 | |
Daniel Dunbar | 5d54eed | 2008-08-25 10:38:11 +0000 | [diff] [blame] | 426 | assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) && |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 427 | "Unknown scalar type to convert"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 428 | |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 429 | // Because of the type rules of C, we often end up computing a logical value, |
| 430 | // then zero extending it to int, then wanting it as a logical value again. |
| 431 | // Optimize this common case. |
| 432 | if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) { |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 433 | if (ZI->getOperand(0)->getType() == |
| 434 | llvm::Type::getInt1Ty(CGF.getLLVMContext())) { |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 435 | Value *Result = ZI->getOperand(0); |
Eli Friedman | 24f3397 | 2008-01-29 18:13:51 +0000 | [diff] [blame] | 436 | // If there aren't any more uses, zap the instruction to save space. |
| 437 | // Note that there can be more uses, for example if this |
| 438 | // is the result of an assignment. |
| 439 | if (ZI->use_empty()) |
| 440 | ZI->eraseFromParent(); |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 441 | return Result; |
| 442 | } |
| 443 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 444 | |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 445 | // Compare against an integer or pointer null. |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 446 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
Chris Lattner | d8d4422 | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 447 | return Builder.CreateICmpNE(Src, Zero, "tobool"); |
| 448 | } |
| 449 | |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 450 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 451 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 452 | Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, |
| 453 | QualType DstType) { |
Chris Lattner | c154ac1 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 454 | SrcType = CGF.getContext().getCanonicalType(SrcType); |
| 455 | DstType = CGF.getContext().getCanonicalType(DstType); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 456 | if (SrcType == DstType) return Src; |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 457 | |
Chris Lattner | e133d7f | 2007-08-26 07:21:11 +0000 | [diff] [blame] | 458 | if (DstType->isVoidType()) return 0; |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 459 | |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 460 | llvm::LLVMContext &VMContext = CGF.getLLVMContext(); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 461 | |
| 462 | // Handle conversions to bool first, they are special: comparisons against 0. |
Chris Lattner | c39c365 | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 463 | if (DstType->isBooleanType()) |
| 464 | return EmitConversionToBool(Src, SrcType); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 465 | |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 466 | const llvm::Type *DstTy = ConvertType(DstType); |
| 467 | |
| 468 | // Ignore conversions like int -> uint. |
| 469 | if (Src->getType() == DstTy) |
| 470 | return Src; |
| 471 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 472 | // Handle pointer conversions next: pointers can only be converted to/from |
| 473 | // other pointers and integers. Check for pointer types in terms of LLVM, as |
| 474 | // some native types (like Obj-C id) may map to a pointer type. |
Daniel Dunbar | 238335f | 2008-08-25 09:51:32 +0000 | [diff] [blame] | 475 | if (isa<llvm::PointerType>(DstTy)) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 476 | // The source value may be an integer, or a pointer. |
Fariborz Jahanian | f7f6cf8 | 2009-07-28 22:00:58 +0000 | [diff] [blame] | 477 | if (isa<llvm::PointerType>(Src->getType())) { |
| 478 | // Some heavy lifting for derived to base conversion. |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 479 | if (const CXXRecordDecl *ClassDecl = |
Fariborz Jahanian | 90e1874 | 2009-07-29 00:44:13 +0000 | [diff] [blame] | 480 | SrcType->getCXXRecordDeclForPointerType()) |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 481 | if (const CXXRecordDecl *BaseClassDecl = |
Fariborz Jahanian | 90e1874 | 2009-07-29 00:44:13 +0000 | [diff] [blame] | 482 | DstType->getCXXRecordDeclForPointerType()) |
| 483 | Src = CGF.AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 484 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
Fariborz Jahanian | f7f6cf8 | 2009-07-28 22:00:58 +0000 | [diff] [blame] | 485 | } |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 486 | assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
Eli Friedman | 35bcec8 | 2009-03-04 04:02:35 +0000 | [diff] [blame] | 487 | // First, convert to the correct width so that we control the kind of |
| 488 | // extension. |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 489 | const llvm::Type *MiddleTy = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 490 | llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); |
Eli Friedman | 35bcec8 | 2009-03-04 04:02:35 +0000 | [diff] [blame] | 491 | bool InputSigned = SrcType->isSignedIntegerType(); |
| 492 | llvm::Value* IntResult = |
| 493 | Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); |
| 494 | // Then, cast to pointer. |
| 495 | return Builder.CreateIntToPtr(IntResult, DstTy, "conv"); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 496 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 497 | |
Daniel Dunbar | 238335f | 2008-08-25 09:51:32 +0000 | [diff] [blame] | 498 | if (isa<llvm::PointerType>(Src->getType())) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 499 | // Must be an ptr to int cast. |
| 500 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
Anders Carlsson | 44db38f | 2007-10-31 23:18:02 +0000 | [diff] [blame] | 501 | return Builder.CreatePtrToInt(Src, DstTy, "conv"); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 502 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 503 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 504 | // A scalar can be splatted to an extended vector of the same element type |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 505 | if (DstType->isExtVectorType() && !SrcType->isVectorType()) { |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 506 | // Cast the scalar to element type |
| 507 | QualType EltTy = DstType->getAsExtVectorType()->getElementType(); |
| 508 | llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy); |
| 509 | |
| 510 | // Insert the element in element zero of an undef vector |
Owen Anderson | e0b5eff | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 511 | llvm::Value *UnV = llvm::UndefValue::get(DstTy); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 512 | llvm::Value *Idx = |
| 513 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 514 | UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp"); |
| 515 | |
| 516 | // Splat the element across to all elements |
| 517 | llvm::SmallVector<llvm::Constant*, 16> Args; |
| 518 | unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); |
| 519 | for (unsigned i = 0; i < NumElements; i++) |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 520 | Args.push_back(llvm::ConstantInt::get( |
| 521 | llvm::Type::getInt32Ty(VMContext), 0)); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 522 | |
Owen Anderson | 17971fa | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 523 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 524 | llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); |
| 525 | return Yay; |
| 526 | } |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 527 | |
Chris Lattner | 4f025a4 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 528 | // Allow bitcast from vector to integer/fp of the same size. |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 529 | if (isa<llvm::VectorType>(Src->getType()) || |
Chris Lattner | 4f025a4 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 530 | isa<llvm::VectorType>(DstTy)) |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 531 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 532 | |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 533 | // Finally, we have the arithmetic types: real int/float. |
| 534 | if (isa<llvm::IntegerType>(Src->getType())) { |
| 535 | bool InputSigned = SrcType->isSignedIntegerType(); |
Anders Carlsson | 4dac3f4 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 536 | if (isa<llvm::IntegerType>(DstTy)) |
| 537 | return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); |
| 538 | else if (InputSigned) |
| 539 | return Builder.CreateSIToFP(Src, DstTy, "conv"); |
| 540 | else |
| 541 | return Builder.CreateUIToFP(Src, DstTy, "conv"); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 542 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 543 | |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 544 | assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 545 | if (isa<llvm::IntegerType>(DstTy)) { |
Anders Carlsson | 4dac3f4 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 546 | if (DstType->isSignedIntegerType()) |
| 547 | return Builder.CreateFPToSI(Src, DstTy, "conv"); |
| 548 | else |
| 549 | return Builder.CreateFPToUI(Src, DstTy, "conv"); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | assert(DstTy->isFloatingPoint() && "Unknown real conversion"); |
Anders Carlsson | 4dac3f4 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 553 | if (DstTy->getTypeID() < Src->getType()->getTypeID()) |
| 554 | return Builder.CreateFPTrunc(Src, DstTy, "conv"); |
| 555 | else |
| 556 | return Builder.CreateFPExt(Src, DstTy, "conv"); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 559 | /// EmitComplexToScalarConversion - Emit a conversion from the specified complex |
| 560 | /// type to the specified destination type, where the destination type is an |
| 561 | /// LLVM scalar type. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 562 | Value *ScalarExprEmitter:: |
| 563 | EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 564 | QualType SrcTy, QualType DstTy) { |
Chris Lattner | c39c365 | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 565 | // Get the source element type. |
Chris Lattner | c154ac1 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 566 | SrcTy = SrcTy->getAsComplexType()->getElementType(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 567 | |
Chris Lattner | c39c365 | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 568 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 569 | if (DstTy->isBooleanType()) { |
| 570 | // Complex != 0 -> (Real != 0) | (Imag != 0) |
| 571 | Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 572 | Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); |
| 573 | return Builder.CreateOr(Src.first, Src.second, "tobool"); |
| 574 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 575 | |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 576 | // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, |
| 577 | // the imaginary part of the complex value is discarded and the value of the |
| 578 | // real part is converted according to the conversion rules for the |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 579 | // corresponding real type. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 580 | return EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 581 | } |
| 582 | |
| 583 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 584 | //===----------------------------------------------------------------------===// |
| 585 | // Visitor Methods |
| 586 | //===----------------------------------------------------------------------===// |
| 587 | |
| 588 | Value *ScalarExprEmitter::VisitExpr(Expr *E) { |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 589 | CGF.ErrorUnsupported(E, "scalar expression"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 590 | if (E->getType()->isVoidType()) |
| 591 | return 0; |
Owen Anderson | e0b5eff | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 592 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Eli Friedman | d0e9d09 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 595 | Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
| 596 | llvm::SmallVector<llvm::Constant*, 32> indices; |
| 597 | for (unsigned i = 2; i < E->getNumSubExprs(); i++) { |
| 598 | indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i)))); |
| 599 | } |
| 600 | Value* V1 = CGF.EmitScalarExpr(E->getExpr(0)); |
| 601 | Value* V2 = CGF.EmitScalarExpr(E->getExpr(1)); |
Owen Anderson | 17971fa | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 602 | Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size()); |
Eli Friedman | d0e9d09 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 603 | return Builder.CreateShuffleVector(V1, V2, SV, "shuffle"); |
| 604 | } |
| 605 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 606 | Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 607 | TestAndClearIgnoreResultAssign(); |
| 608 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 609 | // Emit subscript expressions in rvalue context's. For most cases, this just |
| 610 | // loads the lvalue formed by the subscript expr. However, we have to be |
| 611 | // careful, because the base of a vector subscript is occasionally an rvalue, |
| 612 | // so we can't get it as an lvalue. |
| 613 | if (!E->getBase()->getType()->isVectorType()) |
| 614 | return EmitLoadOfLValue(E); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 615 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 616 | // Handle the vector case. The base must be a vector, the index must be an |
| 617 | // integer value. |
| 618 | Value *Base = Visit(E->getBase()); |
| 619 | Value *Idx = Visit(E->getIdx()); |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 620 | bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType(); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 621 | Idx = Builder.CreateIntCast(Idx, |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 622 | llvm::Type::getInt32Ty(CGF.getLLVMContext()), |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 623 | IdxSigned, |
Eli Friedman | d453194 | 2009-03-28 03:27:06 +0000 | [diff] [blame] | 624 | "vecidxcast"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 625 | return Builder.CreateExtractElement(Base, Idx, "vecext"); |
| 626 | } |
| 627 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 628 | // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts |
| 629 | // have to handle a more broad range of conversions than explicit casts, as they |
| 630 | // handle things like function to ptr-to-function decay etc. |
Anders Carlsson | 2c61dbe | 2009-08-24 18:12:39 +0000 | [diff] [blame] | 631 | Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy, |
| 632 | CastExpr::CastKind Kind) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 633 | if (!DestTy->isVoidType()) |
| 634 | TestAndClearIgnoreResultAssign(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 635 | |
Anders Carlsson | 497794f | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 636 | switch (Kind) { |
| 637 | default: |
| 638 | break; |
Anders Carlsson | f1957c8 | 2009-09-01 20:52:42 +0000 | [diff] [blame] | 639 | case CastExpr::CK_BitCast: { |
| 640 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 641 | return Builder.CreateBitCast(Src, ConvertType(DestTy)); |
| 642 | } |
Anders Carlsson | bade079 | 2009-08-24 18:37:17 +0000 | [diff] [blame] | 643 | case CastExpr::CK_ArrayToPointerDecay: { |
| 644 | assert(E->getType()->isArrayType() && |
| 645 | "Array to pointer decay must have array source type!"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 646 | |
Anders Carlsson | bade079 | 2009-08-24 18:37:17 +0000 | [diff] [blame] | 647 | Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays. |
| 648 | |
| 649 | // Note that VLA pointers are always decayed, so we don't need to do |
| 650 | // anything here. |
| 651 | if (!E->getType()->isVariableArrayType()) { |
| 652 | assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer"); |
| 653 | assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 654 | ->getElementType()) && |
| 655 | "Expected pointer to array"); |
| 656 | V = Builder.CreateStructGEP(V, 0, "arraydecay"); |
| 657 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 658 | |
Anders Carlsson | bade079 | 2009-08-24 18:37:17 +0000 | [diff] [blame] | 659 | // The resultant pointer type can be implicitly casted to other pointer |
| 660 | // types as well (e.g. void*) and can be implicitly converted to integer. |
| 661 | const llvm::Type *DestLTy = ConvertType(DestTy); |
| 662 | if (V->getType() != DestLTy) { |
| 663 | if (isa<llvm::PointerType>(DestLTy)) |
| 664 | V = Builder.CreateBitCast(V, DestLTy, "ptrconv"); |
| 665 | else { |
| 666 | assert(isa<llvm::IntegerType>(DestLTy) && "Unknown array decay"); |
| 667 | V = Builder.CreatePtrToInt(V, DestLTy, "ptrconv"); |
| 668 | } |
| 669 | } |
| 670 | return V; |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 671 | } |
Anders Carlsson | 497794f | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 672 | case CastExpr::CK_NullToMemberPointer: |
| 673 | return CGF.CGM.EmitNullConstant(DestTy); |
| 674 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 675 | |
Chris Lattner | 82e1039 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 676 | // Handle cases where the source is an non-complex type. |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 677 | |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 678 | if (!CGF.hasAggregateLLVMType(E->getType())) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 679 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 680 | |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 681 | // Use EmitScalarConversion to perform the conversion. |
| 682 | return EmitScalarConversion(Src, E->getType(), DestTy); |
| 683 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 684 | |
Chris Lattner | de0908b | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 685 | if (E->getType()->isAnyComplexType()) { |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 686 | // Handle cases where the source is a complex type. |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 687 | bool IgnoreImag = true; |
| 688 | bool IgnoreImagAssign = true; |
| 689 | bool IgnoreReal = IgnoreResultAssign; |
| 690 | bool IgnoreRealAssign = IgnoreResultAssign; |
| 691 | if (DestTy->isBooleanType()) |
| 692 | IgnoreImagAssign = IgnoreImag = false; |
| 693 | else if (DestTy->isVoidType()) { |
| 694 | IgnoreReal = IgnoreImag = false; |
| 695 | IgnoreRealAssign = IgnoreImagAssign = true; |
| 696 | } |
| 697 | CodeGenFunction::ComplexPairTy V |
| 698 | = CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign, |
| 699 | IgnoreImagAssign); |
| 700 | return EmitComplexToScalarConversion(V, E->getType(), DestTy); |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 701 | } |
Chris Lattner | d579f7f | 2007-08-26 07:16:41 +0000 | [diff] [blame] | 702 | |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 703 | // Okay, this is a cast from an aggregate. It must be a cast to void. Just |
| 704 | // evaluate the result and return. |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 705 | CGF.EmitAggExpr(E, 0, false, true); |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 706 | return 0; |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 709 | Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
Chris Lattner | 09cee85 | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 710 | return CGF.EmitCompoundStmt(*E->getSubStmt(), |
| 711 | !E->getType()->isVoidType()).getScalarVal(); |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Mike Stump | 2b6933f | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 714 | Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { |
| 715 | return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp"); |
Mike Stump | fca5da0 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 716 | } |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 718 | //===----------------------------------------------------------------------===// |
| 719 | // Unary Operators |
| 720 | //===----------------------------------------------------------------------===// |
| 721 | |
| 722 | Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | 855e3d7 | 2007-08-24 16:24:49 +0000 | [diff] [blame] | 723 | bool isInc, bool isPre) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 724 | LValue LV = EmitLValue(E->getSubExpr()); |
Eli Friedman | 6a25987 | 2009-03-23 03:00:06 +0000 | [diff] [blame] | 725 | QualType ValTy = E->getSubExpr()->getType(); |
| 726 | Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal(); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 727 | |
| 728 | llvm::LLVMContext &VMContext = CGF.getLLVMContext(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 729 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 730 | int AmountVal = isInc ? 1 : -1; |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 731 | |
| 732 | if (ValTy->isPointerType() && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 733 | ValTy->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 734 | // The amount of the addition/subtraction needs to account for the VLA size |
| 735 | CGF.ErrorUnsupported(E, "VLA pointer inc/dec"); |
| 736 | } |
| 737 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 738 | Value *NextVal; |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 739 | if (const llvm::PointerType *PT = |
Chris Lattner | 8360c61 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 740 | dyn_cast<llvm::PointerType>(InVal->getType())) { |
Owen Anderson | 73e7f80 | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 741 | llvm::Constant *Inc = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 742 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal); |
Chris Lattner | 8360c61 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 743 | if (!isa<llvm::FunctionType>(PT->getElementType())) { |
Fariborz Jahanian | 69b91ca | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 744 | QualType PTEE = ValTy->getPointeeType(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 745 | if (const ObjCInterfaceType *OIT = |
Fariborz Jahanian | 69b91ca | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 746 | dyn_cast<ObjCInterfaceType>(PTEE)) { |
| 747 | // Handle interface types, which are not represented with a concrete type. |
| 748 | int size = CGF.getContext().getTypeSize(OIT) / 8; |
| 749 | if (!isInc) |
| 750 | size = -size; |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 751 | Inc = llvm::ConstantInt::get(Inc->getType(), size); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 752 | const llvm::Type *i8Ty = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 753 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Fariborz Jahanian | 69b91ca | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 754 | InVal = Builder.CreateBitCast(InVal, i8Ty); |
| 755 | NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr"); |
| 756 | llvm::Value *lhs = LV.getAddress(); |
Owen Anderson | 7ec2d8f | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 757 | lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty)); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 758 | LV = LValue::MakeAddr(lhs, ValTy.getCVRQualifiers(), |
Fariborz Jahanian | 69b91ca | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 759 | CGF.getContext().getObjCGCAttrKind(ValTy)); |
Mike Stump | 487ce38 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 760 | } else |
Dan Gohman | 5a74824 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 761 | NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec"); |
Chris Lattner | 8360c61 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 762 | } else { |
Owen Anderson | 73e7f80 | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 763 | const llvm::Type *i8Ty = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 764 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Chris Lattner | 8360c61 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 765 | NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp"); |
| 766 | NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec"); |
| 767 | NextVal = Builder.CreateBitCast(NextVal, InVal->getType()); |
| 768 | } |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 769 | } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) { |
Chris Lattner | 4908317 | 2009-02-11 07:40:06 +0000 | [diff] [blame] | 770 | // Bool++ is an interesting case, due to promotion rules, we get: |
| 771 | // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 -> |
| 772 | // Bool = ((int)Bool+1) != 0 |
| 773 | // An interesting aspect of this is that increment is always true. |
| 774 | // Decrement does not have this property. |
Owen Anderson | d3fd60e | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 775 | NextVal = llvm::ConstantInt::getTrue(VMContext); |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 776 | } else if (isa<llvm::IntegerType>(InVal->getType())) { |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 777 | NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); |
Dan Gohman | c87cf1d | 2009-08-12 01:16:29 +0000 | [diff] [blame] | 778 | |
| 779 | // Signed integer overflow is undefined behavior. |
| 780 | if (ValTy->isSignedIntegerType()) |
| 781 | NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 782 | else |
| 783 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
Chris Lattner | 0dc11f6 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 784 | } else { |
| 785 | // Add the inc/dec to the real part. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 786 | if (InVal->getType() == llvm::Type::getFloatTy(VMContext)) |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 787 | NextVal = |
| 788 | llvm::ConstantFP::get(VMContext, |
Owen Anderson | b56fd0b | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 789 | llvm::APFloat(static_cast<float>(AmountVal))); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 790 | else if (InVal->getType() == llvm::Type::getDoubleTy(VMContext)) |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 791 | NextVal = |
Owen Anderson | b56fd0b | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 792 | llvm::ConstantFP::get(VMContext, |
| 793 | llvm::APFloat(static_cast<double>(AmountVal))); |
Chris Lattner | d54d1f2 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 794 | else { |
| 795 | llvm::APFloat F(static_cast<float>(AmountVal)); |
Dale Johannesen | 2461f61 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 796 | bool ignored; |
| 797 | F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero, |
| 798 | &ignored); |
Owen Anderson | b56fd0b | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 799 | NextVal = llvm::ConstantFP::get(VMContext, F); |
Chris Lattner | b2a7dab | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 801 | NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
Chris Lattner | 0dc11f6 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 802 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 803 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 804 | // Store the updated result through the lvalue. |
Eli Friedman | 6a25987 | 2009-03-23 03:00:06 +0000 | [diff] [blame] | 805 | if (LV.isBitfield()) |
| 806 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, |
| 807 | &NextVal); |
| 808 | else |
| 809 | CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 810 | |
| 811 | // If this is a postinc, return the value read from memory, otherwise use the |
| 812 | // updated value. |
| 813 | return isPre ? NextVal : InVal; |
| 814 | } |
| 815 | |
| 816 | |
| 817 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 818 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 819 | Value *Op = Visit(E->getSubExpr()); |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 820 | if (Op->getType()->isFPOrFPVector()) |
| 821 | return Builder.CreateFNeg(Op, "neg"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 822 | return Builder.CreateNeg(Op, "neg"); |
| 823 | } |
| 824 | |
| 825 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 826 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 827 | Value *Op = Visit(E->getSubExpr()); |
| 828 | return Builder.CreateNot(Op, "neg"); |
| 829 | } |
| 830 | |
| 831 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 832 | // Compare operand to zero. |
| 833 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 834 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 835 | // Invert value. |
| 836 | // TODO: Could dynamically modify easy computations here. For example, if |
| 837 | // the operand is an icmp ne, turn into icmp eq. |
| 838 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 839 | |
Anders Carlsson | 62943f3 | 2009-05-19 18:44:53 +0000 | [diff] [blame] | 840 | // ZExt result to the expr type. |
| 841 | return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 844 | /// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of |
| 845 | /// argument of the sizeof expression as an integer. |
| 846 | Value * |
| 847 | ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 848 | QualType TypeToSize = E->getTypeOfArgument(); |
Eli Friedman | 5a2c38f | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 849 | if (E->isSizeOf()) { |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 850 | if (const VariableArrayType *VAT = |
Eli Friedman | 5a2c38f | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 851 | CGF.getContext().getAsVariableArrayType(TypeToSize)) { |
| 852 | if (E->isArgumentType()) { |
| 853 | // sizeof(type) - make sure to emit the VLA size. |
| 854 | CGF.EmitVLASize(TypeToSize); |
Eli Friedman | 04659bd | 2009-04-20 03:21:44 +0000 | [diff] [blame] | 855 | } else { |
| 856 | // C99 6.5.3.4p2: If the argument is an expression of type |
| 857 | // VLA, it is evaluated. |
| 858 | CGF.EmitAnyExpr(E->getArgumentExpr()); |
Eli Friedman | 5a2c38f | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 859 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 860 | |
Anders Carlsson | 8f30de9 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 861 | return CGF.GetVLASize(VAT); |
Anders Carlsson | 6cb99b7 | 2008-12-21 03:33:21 +0000 | [diff] [blame] | 862 | } |
Anders Carlsson | 9be6aaf | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 863 | } |
Eli Friedman | 5a2c38f | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 864 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 865 | // If this isn't sizeof(vla), the result must be constant; use the constant |
| 866 | // folding logic so we don't have to duplicate it here. |
Eli Friedman | 5a2c38f | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 867 | Expr::EvalResult Result; |
| 868 | E->Evaluate(Result, CGF.getContext()); |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 869 | return llvm::ConstantInt::get(VMContext, Result.Val.getInt()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 872 | Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { |
| 873 | Expr *Op = E->getSubExpr(); |
Chris Lattner | de0908b | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 874 | if (Op->getType()->isAnyComplexType()) |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 875 | return CGF.EmitComplexExpr(Op, false, true, false, true).first; |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 876 | return Visit(Op); |
| 877 | } |
| 878 | Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { |
| 879 | Expr *Op = E->getSubExpr(); |
Chris Lattner | de0908b | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 880 | if (Op->getType()->isAnyComplexType()) |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 881 | return CGF.EmitComplexExpr(Op, true, false, true, false).second; |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 882 | |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 883 | // __imag on a scalar returns zero. Emit the subexpr to ensure side |
| 884 | // effects are evaluated, but not the actual value. |
| 885 | if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid) |
| 886 | CGF.EmitLValue(Op); |
| 887 | else |
| 888 | CGF.EmitScalarExpr(Op, true); |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 889 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 892 | Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E) |
| 893 | { |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 894 | Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress(); |
Eli Friedman | ccffea9 | 2009-01-24 22:38:55 +0000 | [diff] [blame] | 895 | const llvm::Type* ResultType = ConvertType(E->getType()); |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 896 | return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof"); |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 897 | } |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 898 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 899 | //===----------------------------------------------------------------------===// |
| 900 | // Binary Operators |
| 901 | //===----------------------------------------------------------------------===// |
| 902 | |
| 903 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 904 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 905 | BinOpInfo Result; |
| 906 | Result.LHS = Visit(E->getLHS()); |
| 907 | Result.RHS = Visit(E->getRHS()); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 908 | Result.Ty = E->getType(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 909 | Result.E = E; |
| 910 | return Result; |
| 911 | } |
| 912 | |
Chris Lattner | 0d96530 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 913 | Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 914 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 915 | bool Ignore = TestAndClearIgnoreResultAssign(); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 916 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
| 917 | |
| 918 | BinOpInfo OpInfo; |
| 919 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 920 | if (E->getComputationResultType()->isAnyComplexType()) { |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 921 | // This needs to go through the complex expression emitter, but it's a tad |
| 922 | // complicated to do that... I'm leaving it out for now. (Note that we do |
| 923 | // actually need the imaginary part of the RHS for multiplication and |
| 924 | // division.) |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 925 | CGF.ErrorUnsupported(E, "complex compound assignment"); |
Owen Anderson | e0b5eff | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 926 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Mike Stump | 8d96226 | 2009-05-22 19:07:20 +0000 | [diff] [blame] | 929 | // Emit the RHS first. __block variables need to have the rhs evaluated |
| 930 | // first, plus this should improve codegen a little. |
| 931 | OpInfo.RHS = Visit(E->getRHS()); |
| 932 | OpInfo.Ty = E->getComputationResultType(); |
| 933 | OpInfo.E = E; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 934 | // Load/convert the LHS. |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 935 | LValue LHSLV = EmitLValue(E->getLHS()); |
| 936 | OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 937 | OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, |
| 938 | E->getComputationLHSType()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 939 | |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 940 | // Expand the binary operator. |
| 941 | Value *Result = (this->*Func)(OpInfo); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 942 | |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 943 | // Convert the result back to the LHS type. |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 944 | Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy); |
| 945 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 946 | // Store the result value into the LHS lvalue. Bit-fields are handled |
| 947 | // specially because the result is altered by the store, i.e., [C99 6.5.16p1] |
| 948 | // 'An assignment expression has the value of the left operand after the |
| 949 | // assignment...'. |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 950 | if (LHSLV.isBitfield()) { |
| 951 | if (!LHSLV.isVolatileQualified()) { |
| 952 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy, |
| 953 | &Result); |
| 954 | return Result; |
| 955 | } else |
| 956 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy); |
| 957 | } else |
Daniel Dunbar | 2668dd1 | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 958 | CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy); |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 959 | if (Ignore) |
| 960 | return 0; |
| 961 | return EmitLoadOfLValue(LHSLV, E->getType()); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 965 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
Nate Begeman | aade3bf | 2007-12-30 01:28:16 +0000 | [diff] [blame] | 966 | if (Ops.LHS->getType()->isFPOrFPVector()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 967 | return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 968 | else if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 969 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 970 | else |
| 971 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 972 | } |
| 973 | |
| 974 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 975 | // Rem in C can't be a floating point type: C99 6.5.5p2. |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 976 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 977 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 978 | else |
| 979 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 980 | } |
| 981 | |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 982 | Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) { |
| 983 | unsigned IID; |
| 984 | unsigned OpID = 0; |
Mike Stump | 0f595bb | 2009-04-02 01:03:55 +0000 | [diff] [blame] | 985 | |
Mike Stump | f71b774 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 986 | switch (Ops.E->getOpcode()) { |
| 987 | case BinaryOperator::Add: |
| 988 | case BinaryOperator::AddAssign: |
| 989 | OpID = 1; |
| 990 | IID = llvm::Intrinsic::sadd_with_overflow; |
| 991 | break; |
| 992 | case BinaryOperator::Sub: |
| 993 | case BinaryOperator::SubAssign: |
| 994 | OpID = 2; |
| 995 | IID = llvm::Intrinsic::ssub_with_overflow; |
| 996 | break; |
| 997 | case BinaryOperator::Mul: |
| 998 | case BinaryOperator::MulAssign: |
| 999 | OpID = 3; |
| 1000 | IID = llvm::Intrinsic::smul_with_overflow; |
| 1001 | break; |
| 1002 | default: |
| 1003 | assert(false && "Unsupported operation for overflow detection"); |
Daniel Dunbar | 96e909b | 2009-04-08 16:23:09 +0000 | [diff] [blame] | 1004 | IID = 0; |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1005 | } |
Mike Stump | f71b774 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1006 | OpID <<= 1; |
| 1007 | OpID |= 1; |
| 1008 | |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1009 | const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty); |
| 1010 | |
| 1011 | llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1); |
| 1012 | |
| 1013 | Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS); |
| 1014 | Value *result = Builder.CreateExtractValue(resultAndOverflow, 0); |
| 1015 | Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1); |
| 1016 | |
| 1017 | // Branch in case of overflow. |
| 1018 | llvm::BasicBlock *initialBB = Builder.GetInsertBlock(); |
| 1019 | llvm::BasicBlock *overflowBB = |
| 1020 | CGF.createBasicBlock("overflow", CGF.CurFn); |
| 1021 | llvm::BasicBlock *continueBB = |
| 1022 | CGF.createBasicBlock("overflow.continue", CGF.CurFn); |
| 1023 | |
| 1024 | Builder.CreateCondBr(overflow, overflowBB, continueBB); |
| 1025 | |
| 1026 | // Handle overflow |
| 1027 | |
| 1028 | Builder.SetInsertPoint(overflowBB); |
| 1029 | |
| 1030 | // Handler is: |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1031 | // long long *__overflow_handler)(long long a, long long b, char op, |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1032 | // char width) |
| 1033 | std::vector<const llvm::Type*> handerArgTypes; |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1034 | handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext)); |
| 1035 | handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext)); |
| 1036 | handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext)); |
| 1037 | handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext)); |
| 1038 | llvm::FunctionType *handlerTy = llvm::FunctionType::get( |
| 1039 | llvm::Type::getInt64Ty(VMContext), handerArgTypes, false); |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1040 | llvm::Value *handlerFunction = |
| 1041 | CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler", |
Owen Anderson | 7ec2d8f | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1042 | llvm::PointerType::getUnqual(handlerTy)); |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1043 | handlerFunction = Builder.CreateLoad(handlerFunction); |
| 1044 | |
| 1045 | llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction, |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1046 | Builder.CreateSExt(Ops.LHS, llvm::Type::getInt64Ty(VMContext)), |
| 1047 | Builder.CreateSExt(Ops.RHS, llvm::Type::getInt64Ty(VMContext)), |
| 1048 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), OpID), |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1049 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1050 | cast<llvm::IntegerType>(opTy)->getBitWidth())); |
| 1051 | |
| 1052 | handlerResult = Builder.CreateTrunc(handlerResult, opTy); |
| 1053 | |
| 1054 | Builder.CreateBr(continueBB); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1055 | |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1056 | // Set up the continuation |
| 1057 | Builder.SetInsertPoint(continueBB); |
| 1058 | // Get the correct result |
| 1059 | llvm::PHINode *phi = Builder.CreatePHI(opTy); |
| 1060 | phi->reserveOperandSpace(2); |
| 1061 | phi->addIncoming(result, initialBB); |
| 1062 | phi->addIncoming(handlerResult, overflowBB); |
| 1063 | |
| 1064 | return phi; |
| 1065 | } |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1066 | |
| 1067 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1068 | if (!Ops.Ty->isAnyPointerType()) { |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1069 | if (CGF.getContext().getLangOptions().OverflowChecking && |
| 1070 | Ops.Ty->isSignedIntegerType()) |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1071 | return EmitOverflowCheckedBinOp(Ops); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1072 | |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1073 | if (Ops.LHS->getType()->isFPOrFPVector()) |
| 1074 | return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add"); |
Dan Gohman | c87cf1d | 2009-08-12 01:16:29 +0000 | [diff] [blame] | 1075 | |
| 1076 | // Signed integer overflow is undefined behavior. |
| 1077 | if (Ops.Ty->isSignedIntegerType()) |
| 1078 | return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add"); |
| 1079 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1080 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1081 | } |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1082 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1083 | if (Ops.Ty->isPointerType() && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1084 | Ops.Ty->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1085 | // The amount of the addition needs to account for the VLA size |
| 1086 | CGF.ErrorUnsupported(Ops.E, "VLA pointer addition"); |
| 1087 | } |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1088 | Value *Ptr, *Idx; |
| 1089 | Expr *IdxExp; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1090 | const PointerType *PT = Ops.E->getLHS()->getType()->getAs<PointerType>(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1091 | const ObjCObjectPointerType *OPT = |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1092 | Ops.E->getLHS()->getType()->getAsObjCObjectPointerType(); |
| 1093 | if (PT || OPT) { |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1094 | Ptr = Ops.LHS; |
| 1095 | Idx = Ops.RHS; |
| 1096 | IdxExp = Ops.E->getRHS(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1097 | } else { // int + pointer |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1098 | PT = Ops.E->getRHS()->getType()->getAs<PointerType>(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1099 | OPT = Ops.E->getRHS()->getType()->getAsObjCObjectPointerType(); |
| 1100 | assert((PT || OPT) && "Invalid add expr"); |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1101 | Ptr = Ops.RHS; |
| 1102 | Idx = Ops.LHS; |
| 1103 | IdxExp = Ops.E->getLHS(); |
| 1104 | } |
| 1105 | |
| 1106 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
Sanjiv Gupta | cee8fea | 2009-04-24 02:40:57 +0000 | [diff] [blame] | 1107 | if (Width < CGF.LLVMPointerWidth) { |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1108 | // Zero or sign extend the pointer value based on whether the index is |
| 1109 | // signed or not. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1110 | const llvm::Type *IdxType = |
| 1111 | llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); |
Chris Lattner | c154ac1 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 1112 | if (IdxExp->getType()->isSignedIntegerType()) |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1113 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 1114 | else |
| 1115 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 1116 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1117 | const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1118 | // Handle interface types, which are not represented with a concrete type. |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1119 | if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) { |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1120 | llvm::Value *InterfaceSize = |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1121 | llvm::ConstantInt::get(Idx->getType(), |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1122 | CGF.getContext().getTypeSize(OIT) / 8); |
| 1123 | Idx = Builder.CreateMul(Idx, InterfaceSize); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1124 | const llvm::Type *i8Ty = |
| 1125 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1126 | Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); |
| 1127 | Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); |
| 1128 | return Builder.CreateBitCast(Res, Ptr->getType()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1129 | } |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1130 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1131 | // Explicitly handle GNU void* and function pointer arithmetic extensions. The |
| 1132 | // GNU void* casts amount to no-ops since our void* type is i8*, but this is |
| 1133 | // future proof. |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1134 | if (ElementType->isVoidType() || ElementType->isFunctionType()) { |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1135 | const llvm::Type *i8Ty = |
| 1136 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1137 | Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1138 | Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1139 | return Builder.CreateBitCast(Res, Ptr->getType()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1140 | } |
| 1141 | |
Dan Gohman | 5a74824 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1142 | return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1146 | if (!isa<llvm::PointerType>(Ops.LHS->getType())) { |
Mike Stump | f71b774 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1147 | if (CGF.getContext().getLangOptions().OverflowChecking |
| 1148 | && Ops.Ty->isSignedIntegerType()) |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1149 | return EmitOverflowCheckedBinOp(Ops); |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1150 | |
| 1151 | if (Ops.LHS->getType()->isFPOrFPVector()) |
| 1152 | return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1153 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1154 | } |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1155 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1156 | if (Ops.E->getLHS()->getType()->isPointerType() && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1157 | Ops.E->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1158 | // The amount of the addition needs to account for the VLA size for |
| 1159 | // ptr-int |
| 1160 | // The amount of the division needs to account for the VLA size for |
| 1161 | // ptr-ptr. |
| 1162 | CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction"); |
| 1163 | } |
| 1164 | |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1165 | const QualType LHSType = Ops.E->getLHS()->getType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1166 | const QualType LHSElementType = LHSType->getPointeeType(); |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1167 | if (!isa<llvm::PointerType>(Ops.RHS->getType())) { |
| 1168 | // pointer - int |
| 1169 | Value *Idx = Ops.RHS; |
| 1170 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
Sanjiv Gupta | cee8fea | 2009-04-24 02:40:57 +0000 | [diff] [blame] | 1171 | if (Width < CGF.LLVMPointerWidth) { |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1172 | // Zero or sign extend the pointer value based on whether the index is |
| 1173 | // signed or not. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1174 | const llvm::Type *IdxType = |
| 1175 | llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1176 | if (Ops.E->getRHS()->getType()->isSignedIntegerType()) |
| 1177 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 1178 | else |
| 1179 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 1180 | } |
| 1181 | Idx = Builder.CreateNeg(Idx, "sub.ptr.neg"); |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1182 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1183 | // Handle interface types, which are not represented with a concrete type. |
| 1184 | if (const ObjCInterfaceType *OIT = |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1185 | dyn_cast<ObjCInterfaceType>(LHSElementType)) { |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1186 | llvm::Value *InterfaceSize = |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1187 | llvm::ConstantInt::get(Idx->getType(), |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1188 | CGF.getContext().getTypeSize(OIT) / 8); |
| 1189 | Idx = Builder.CreateMul(Idx, InterfaceSize); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1190 | const llvm::Type *i8Ty = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1191 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1192 | Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); |
| 1193 | Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr"); |
| 1194 | return Builder.CreateBitCast(Res, Ops.LHS->getType()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1195 | } |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1196 | |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1197 | // Explicitly handle GNU void* and function pointer arithmetic |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1198 | // extensions. The GNU void* casts amount to no-ops since our void* type is |
| 1199 | // i8*, but this is future proof. |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1200 | if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { |
Owen Anderson | 73e7f80 | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1201 | const llvm::Type *i8Ty = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1202 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1203 | Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); |
| 1204 | Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr"); |
| 1205 | return Builder.CreateBitCast(Res, Ops.LHS->getType()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1206 | } |
| 1207 | |
Dan Gohman | 5a74824 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1208 | return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr"); |
Daniel Dunbar | 0aac9f6 | 2008-08-05 00:47:03 +0000 | [diff] [blame] | 1209 | } else { |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1210 | // pointer - pointer |
| 1211 | Value *LHS = Ops.LHS; |
| 1212 | Value *RHS = Ops.RHS; |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1213 | |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1214 | uint64_t ElementSize; |
Daniel Dunbar | 0aac9f6 | 2008-08-05 00:47:03 +0000 | [diff] [blame] | 1215 | |
Chris Lattner | 6d2e349 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 1216 | // Handle GCC extension for pointer arithmetic on void* and function pointer |
| 1217 | // types. |
| 1218 | if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1219 | ElementSize = 1; |
| 1220 | } else { |
| 1221 | ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8; |
| 1222 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1223 | |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1224 | const llvm::Type *ResultType = ConvertType(Ops.Ty); |
| 1225 | LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); |
| 1226 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 1227 | Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1228 | |
Chris Lattner | 6d2e349 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 1229 | // Optimize out the shift for element size of 1. |
| 1230 | if (ElementSize == 1) |
| 1231 | return BytesBetween; |
Dan Gohman | 8f61ba4 | 2009-08-11 22:40:09 +0000 | [diff] [blame] | 1232 | |
| 1233 | // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1234 | // pointer difference in C is only defined in the case where both operands |
| 1235 | // are pointing to elements of an array. |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1236 | Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize); |
Dan Gohman | 8f61ba4 | 2009-08-11 22:40:09 +0000 | [diff] [blame] | 1237 | return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1238 | } |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 1242 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 1243 | // RHS to the same size as the LHS. |
| 1244 | Value *RHS = Ops.RHS; |
| 1245 | if (Ops.LHS->getType() != RHS->getType()) |
| 1246 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1247 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1248 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 1249 | } |
| 1250 | |
| 1251 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 1252 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 1253 | // RHS to the same size as the LHS. |
| 1254 | Value *RHS = Ops.RHS; |
| 1255 | if (Ops.LHS->getType() != RHS->getType()) |
| 1256 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1257 | |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1258 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1259 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 1260 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 1261 | } |
| 1262 | |
| 1263 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 1264 | unsigned SICmpOpc, unsigned FCmpOpc) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1265 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1266 | Value *Result; |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1267 | QualType LHSTy = E->getLHS()->getType(); |
Chris Lattner | 08ac852 | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 1268 | if (!LHSTy->isAnyComplexType()) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1269 | Value *LHS = Visit(E->getLHS()); |
| 1270 | Value *RHS = Visit(E->getRHS()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1271 | |
Eli Friedman | 04865bc | 2009-07-22 06:07:16 +0000 | [diff] [blame] | 1272 | if (LHS->getType()->isFPOrFPVector()) { |
Nate Begeman | 1591bc5 | 2008-07-25 20:16:05 +0000 | [diff] [blame] | 1273 | Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc, |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1274 | LHS, RHS, "cmp"); |
Eli Friedman | 850ea37 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 1275 | } else if (LHSTy->isSignedIntegerType()) { |
| 1276 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1277 | LHS, RHS, "cmp"); |
| 1278 | } else { |
Eli Friedman | 850ea37 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 1279 | // Unsigned integers and pointers. |
| 1280 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1281 | LHS, RHS, "cmp"); |
| 1282 | } |
Chris Lattner | 08ac852 | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 1283 | |
| 1284 | // If this is a vector comparison, sign extend the result to the appropriate |
| 1285 | // vector integer type and return it (don't convert to bool). |
| 1286 | if (LHSTy->isVectorType()) |
| 1287 | return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1288 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1289 | } else { |
| 1290 | // Complex Comparison: can only be an equality comparison. |
| 1291 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 1292 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1293 | |
Chris Lattner | c154ac1 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 1294 | QualType CETy = LHSTy->getAsComplexType()->getElementType(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1295 | |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1296 | Value *ResultR, *ResultI; |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1297 | if (CETy->isRealFloatingType()) { |
| 1298 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1299 | LHS.first, RHS.first, "cmp.r"); |
| 1300 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1301 | LHS.second, RHS.second, "cmp.i"); |
| 1302 | } else { |
| 1303 | // Complex comparisons can only be equality comparisons. As such, signed |
| 1304 | // and unsigned opcodes are the same. |
| 1305 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 1306 | LHS.first, RHS.first, "cmp.r"); |
| 1307 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 1308 | LHS.second, RHS.second, "cmp.i"); |
| 1309 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1310 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1311 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 1312 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 1313 | } else { |
| 1314 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1315 | "Complex comparison other than == or != ?"); |
| 1316 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 1317 | } |
| 1318 | } |
Nuno Lopes | 9257700 | 2009-01-11 23:22:37 +0000 | [diff] [blame] | 1319 | |
| 1320 | return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1324 | bool Ignore = TestAndClearIgnoreResultAssign(); |
| 1325 | |
| 1326 | // __block variables need to have the rhs evaluated first, plus this should |
| 1327 | // improve codegen just a little. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1328 | Value *RHS = Visit(E->getRHS()); |
Mike Stump | 68df15c | 2009-05-21 21:05:15 +0000 | [diff] [blame] | 1329 | LValue LHS = EmitLValue(E->getLHS()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1330 | |
Daniel Dunbar | 2668dd1 | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1331 | // Store the value into the LHS. Bit-fields are handled specially |
Daniel Dunbar | 2710fc9 | 2008-11-19 11:54:05 +0000 | [diff] [blame] | 1332 | // because the result is altered by the store, i.e., [C99 6.5.16p1] |
| 1333 | // 'An assignment expression has the value of the left operand after |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1334 | // the assignment...'. |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1335 | if (LHS.isBitfield()) { |
| 1336 | if (!LHS.isVolatileQualified()) { |
| 1337 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(), |
| 1338 | &RHS); |
| 1339 | return RHS; |
| 1340 | } else |
| 1341 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType()); |
| 1342 | } else |
Daniel Dunbar | 2668dd1 | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1343 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1344 | if (Ignore) |
| 1345 | return 0; |
| 1346 | return EmitLoadOfLValue(LHS, E->getType()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
Chris Lattner | 715c2a7 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1350 | // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. |
| 1351 | // If we have 1 && X, just emit X without inserting the control flow. |
| 1352 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { |
| 1353 | if (Cond == 1) { // If we have 1 && X, just emit X. |
Chris Lattner | 3f73d0d | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1354 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 1355 | // ZExt result to int. |
| 1356 | return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext"); |
| 1357 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1358 | |
Chris Lattner | 715c2a7 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1359 | // 0 && RHS: If it is safe, just elide the RHS, and return 0. |
| 1360 | if (!CGF.ContainsLabel(E->getRHS())) |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1361 | return llvm::Constant::getNullValue(CGF.LLVMIntTy); |
Chris Lattner | 3f73d0d | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1362 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1363 | |
Daniel Dunbar | 6e3a10c | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1364 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); |
| 1365 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); |
Chris Lattner | 715c2a7 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1366 | |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1367 | // Branch on the LHS first. If it is false, go to the failure (cont) block. |
| 1368 | CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock); |
| 1369 | |
| 1370 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 1371 | // edges from this first condition. All of these values will be false. Start |
| 1372 | // setting up the PHI node in the Cont Block for this. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1373 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), |
| 1374 | "", ContBlock); |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1375 | PN->reserveOperandSpace(2); // Normal case, two inputs. |
| 1376 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 1377 | PI != PE; ++PI) |
Owen Anderson | d3fd60e | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 1378 | PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1379 | |
Anders Carlsson | ac36c0e | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1380 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1381 | CGF.EmitBlock(RHSBlock); |
| 1382 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Anders Carlsson | ac36c0e | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1383 | CGF.PopConditionalTempDestruction(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1384 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1385 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1386 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 1389 | // into the phi node for the edge with the value of RHSCond. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1390 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1391 | PN->addIncoming(RHSCond, RHSBlock); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1392 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1393 | // ZExt result to int. |
| 1394 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext"); |
| 1395 | } |
| 1396 | |
| 1397 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
Chris Lattner | 715c2a7 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1398 | // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. |
| 1399 | // If we have 0 || X, just emit X without inserting the control flow. |
| 1400 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { |
| 1401 | if (Cond == -1) { // If we have 0 || X, just emit X. |
Chris Lattner | 3f73d0d | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1402 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 1403 | // ZExt result to int. |
| 1404 | return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext"); |
| 1405 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1406 | |
Eli Friedman | ea137cd | 2008-12-02 16:02:46 +0000 | [diff] [blame] | 1407 | // 1 || RHS: If it is safe, just elide the RHS, and return 1. |
Chris Lattner | 715c2a7 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1408 | if (!CGF.ContainsLabel(E->getRHS())) |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1409 | return llvm::ConstantInt::get(CGF.LLVMIntTy, 1); |
Chris Lattner | 3f73d0d | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1410 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1411 | |
Daniel Dunbar | 6e3a10c | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1412 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); |
| 1413 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1414 | |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1415 | // Branch on the LHS first. If it is true, go to the success (cont) block. |
| 1416 | CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock); |
| 1417 | |
| 1418 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 1419 | // edges from this first condition. All of these values will be true. Start |
| 1420 | // setting up the PHI node in the Cont Block for this. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1421 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), |
| 1422 | "", ContBlock); |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1423 | PN->reserveOperandSpace(2); // Normal case, two inputs. |
| 1424 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 1425 | PI != PE; ++PI) |
Owen Anderson | d3fd60e | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 1426 | PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI); |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1427 | |
Anders Carlsson | ac36c0e | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1428 | CGF.PushConditionalTempDestruction(); |
| 1429 | |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1430 | // Emit the RHS condition as a bool value. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1431 | CGF.EmitBlock(RHSBlock); |
| 1432 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1433 | |
Anders Carlsson | ac36c0e | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1434 | CGF.PopConditionalTempDestruction(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1435 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1436 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1437 | RHSBlock = Builder.GetInsertBlock(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1438 | |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1439 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 1440 | // into the phi node for the edge with the value of RHSCond. |
| 1441 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1442 | PN->addIncoming(RHSCond, RHSBlock); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1443 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1444 | // ZExt result to int. |
| 1445 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext"); |
| 1446 | } |
| 1447 | |
| 1448 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 1449 | CGF.EmitStmt(E->getLHS()); |
Daniel Dunbar | 5aa22bc | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1450 | CGF.EnsureInsertPoint(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1451 | return Visit(E->getRHS()); |
| 1452 | } |
| 1453 | |
| 1454 | //===----------------------------------------------------------------------===// |
| 1455 | // Other Operators |
| 1456 | //===----------------------------------------------------------------------===// |
| 1457 | |
Chris Lattner | 504a528 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1458 | /// isCheapEnoughToEvaluateUnconditionally - Return true if the specified |
| 1459 | /// expression is cheap enough and side-effect-free enough to evaluate |
| 1460 | /// unconditionally instead of conditionally. This is used to convert control |
| 1461 | /// flow into selects in some cases. |
| 1462 | static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) { |
| 1463 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 1464 | return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1465 | |
Chris Lattner | 504a528 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1466 | // TODO: Allow anything we can constant fold to an integer or fp constant. |
| 1467 | if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) || |
| 1468 | isa<FloatingLiteral>(E)) |
| 1469 | return true; |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1470 | |
Chris Lattner | 504a528 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1471 | // Non-volatile automatic variables too, to get "cond ? X : Y" where |
| 1472 | // X and Y are local variables. |
| 1473 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 1474 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 1475 | if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified()) |
| 1476 | return true; |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1477 | |
Chris Lattner | 504a528 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1478 | return false; |
| 1479 | } |
| 1480 | |
| 1481 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1482 | Value *ScalarExprEmitter:: |
| 1483 | VisitConditionalOperator(const ConditionalOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1484 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1485 | // If the condition constant folds and can be elided, try to avoid emitting |
| 1486 | // the condition and the dead arm. |
| 1487 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){ |
Chris Lattner | 044bffc | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1488 | Expr *Live = E->getLHS(), *Dead = E->getRHS(); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1489 | if (Cond == -1) |
Chris Lattner | 044bffc | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1490 | std::swap(Live, Dead); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1491 | |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1492 | // If the dead side doesn't have labels we need, and if the Live side isn't |
| 1493 | // the gnu missing ?: extension (which we could handle, but don't bother |
| 1494 | // to), just emit the Live part. |
| 1495 | if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part |
| 1496 | Live) // Live part isn't missing. |
| 1497 | return Visit(Live); |
Chris Lattner | 044bffc | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1498 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1499 | |
| 1500 | |
Chris Lattner | 504a528 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1501 | // If this is a really simple expression (like x ? 4 : 5), emit this as a |
| 1502 | // select instead of as control flow. We can only do this if it is cheap and |
Chris Lattner | 1f11af2 | 2008-11-16 06:16:27 +0000 | [diff] [blame] | 1503 | // safe to evaluate the LHS and RHS unconditionally. |
Chris Lattner | 504a528 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1504 | if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) && |
| 1505 | isCheapEnoughToEvaluateUnconditionally(E->getRHS())) { |
| 1506 | llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond()); |
| 1507 | llvm::Value *LHS = Visit(E->getLHS()); |
| 1508 | llvm::Value *RHS = Visit(E->getRHS()); |
| 1509 | return Builder.CreateSelect(CondV, LHS, RHS, "cond"); |
| 1510 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1511 | |
| 1512 | |
Daniel Dunbar | b23e992 | 2008-11-12 10:13:37 +0000 | [diff] [blame] | 1513 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 1514 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
Daniel Dunbar | 6e3a10c | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1515 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Chris Lattner | 67e2246 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1516 | Value *CondVal = 0; |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1517 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1518 | // If we don't have the GNU missing condition extension, emit a branch on bool |
| 1519 | // the normal way. |
Chris Lattner | 8603171 | 2009-02-13 23:35:32 +0000 | [diff] [blame] | 1520 | if (E->getLHS()) { |
| 1521 | // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for |
| 1522 | // the branch on bool. |
| 1523 | CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); |
| 1524 | } else { |
| 1525 | // Otherwise, for the ?: extension, evaluate the conditional and then |
| 1526 | // convert it to bool the hard way. We do this explicitly because we need |
| 1527 | // the unconverted value for the missing middle value of the ?:. |
Chris Lattner | 67e2246 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1528 | CondVal = CGF.EmitScalarExpr(E->getCond()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1529 | |
Chris Lattner | 8603171 | 2009-02-13 23:35:32 +0000 | [diff] [blame] | 1530 | // In some cases, EmitScalarConversion will delete the "CondVal" expression |
| 1531 | // if there are no extra uses (an optimization). Inhibit this by making an |
| 1532 | // extra dead use, because we're going to add a use of CondVal later. We |
| 1533 | // don't use the builder for this, because we don't want it to get optimized |
| 1534 | // away. This leaves dead code, but the ?: extension isn't common. |
| 1535 | new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder", |
| 1536 | Builder.GetInsertBlock()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1537 | |
Chris Lattner | 67e2246 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1538 | Value *CondBoolVal = |
| 1539 | CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), |
| 1540 | CGF.getContext().BoolTy); |
| 1541 | Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); |
Chris Lattner | 67e2246 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1542 | } |
Anders Carlsson | bf3b93a | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1543 | |
| 1544 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1545 | CGF.EmitBlock(LHSBlock); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1546 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1547 | // Handle the GNU extension for missing LHS. |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1548 | Value *LHS; |
| 1549 | if (E->getLHS()) |
Eli Friedman | ce8d703 | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 1550 | LHS = Visit(E->getLHS()); |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1551 | else // Perform promotions, to handle cases like "short ?: int" |
| 1552 | LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType()); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1553 | |
Anders Carlsson | bf3b93a | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1554 | CGF.PopConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1555 | LHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | 5276caa | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 1556 | CGF.EmitBranch(ContBlock); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1557 | |
Anders Carlsson | bf3b93a | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1558 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1559 | CGF.EmitBlock(RHSBlock); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1560 | |
Eli Friedman | ce8d703 | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 1561 | Value *RHS = Visit(E->getRHS()); |
Anders Carlsson | bf3b93a | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1562 | CGF.PopConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1563 | RHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | 5276caa | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 1564 | CGF.EmitBranch(ContBlock); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1565 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1566 | CGF.EmitBlock(ContBlock); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1567 | |
Nuno Lopes | b62ff24 | 2008-06-04 19:15:45 +0000 | [diff] [blame] | 1568 | if (!LHS || !RHS) { |
Chris Lattner | 307da02 | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1569 | assert(E->getType()->isVoidType() && "Non-void value should have a value"); |
| 1570 | return 0; |
| 1571 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1572 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1573 | // Create a PHI node for the real part. |
| 1574 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); |
| 1575 | PN->reserveOperandSpace(2); |
| 1576 | PN->addIncoming(LHS, LHSBlock); |
| 1577 | PN->addIncoming(RHS, RHSBlock); |
| 1578 | return PN; |
| 1579 | } |
| 1580 | |
| 1581 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Eli Friedman | d540c11 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 1582 | return Visit(E->getChosenSubExpr(CGF.getContext())); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Chris Lattner | 307da02 | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1585 | Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Eli Friedman | 8f5e878 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 1586 | llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); |
Anders Carlsson | 285611e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1587 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); |
| 1588 | |
| 1589 | // If EmitVAArg fails, we fall back to the LLVM instruction. |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1590 | if (!ArgPtr) |
Anders Carlsson | 285611e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1591 | return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); |
| 1592 | |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1593 | // FIXME Volatility. |
Anders Carlsson | 285611e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1594 | return Builder.CreateLoad(ArgPtr); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Mike Stump | 4eb81dc | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 1597 | Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) { |
Mike Stump | 1fa52fe | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1598 | return CGF.BuildBlockLiteralTmp(BE); |
Mike Stump | 4eb81dc | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1601 | //===----------------------------------------------------------------------===// |
| 1602 | // Entry Point into this File |
| 1603 | //===----------------------------------------------------------------------===// |
| 1604 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1605 | /// EmitScalarExpr - Emit the computation of the specified expression of scalar |
| 1606 | /// type, ignoring the result. |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1607 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1608 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 1609 | "Invalid scalar expression to emit"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1610 | |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1611 | return ScalarExprEmitter(*this, IgnoreResultAssign) |
| 1612 | .Visit(const_cast<Expr*>(E)); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1613 | } |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1614 | |
| 1615 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 1616 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1617 | Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, |
| 1618 | QualType DstTy) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1619 | assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && |
| 1620 | "Invalid scalar expression to emit"); |
| 1621 | return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); |
| 1622 | } |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1623 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1624 | /// EmitComplexToScalarConversion - Emit a conversion from the specified complex |
| 1625 | /// type to the specified destination type, where the destination type is an |
| 1626 | /// LLVM scalar type. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1627 | Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, |
| 1628 | QualType SrcTy, |
| 1629 | QualType DstTy) { |
Chris Lattner | de0908b | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 1630 | assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) && |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1631 | "Invalid complex -> scalar conversion"); |
| 1632 | return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, |
| 1633 | DstTy); |
| 1634 | } |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1635 | |
| 1636 | Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) { |
| 1637 | assert(V1->getType() == V2->getType() && |
| 1638 | "Vector operands must be of the same type"); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1639 | unsigned NumElements = |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1640 | cast<llvm::VectorType>(V1->getType())->getNumElements(); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1641 | |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1642 | va_list va; |
| 1643 | va_start(va, V2); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1644 | |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1645 | llvm::SmallVector<llvm::Constant*, 16> Args; |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1646 | for (unsigned i = 0; i < NumElements; i++) { |
| 1647 | int n = va_arg(va, int); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1648 | assert(n >= 0 && n < (int)NumElements * 2 && |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1649 | "Vector shuffle index out of bounds!"); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1650 | Args.push_back(llvm::ConstantInt::get( |
| 1651 | llvm::Type::getInt32Ty(VMContext), n)); |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1652 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1653 | |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1654 | const char *Name = va_arg(va, const char *); |
| 1655 | va_end(va); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1656 | |
Owen Anderson | 17971fa | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 1657 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1658 | |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1659 | return Builder.CreateShuffleVector(V1, V2, Mask, Name); |
| 1660 | } |
| 1661 | |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1662 | llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals, |
Chris Lattner | a23eb7b | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 1663 | unsigned NumVals, bool isSplat) { |
Anders Carlsson | 68b8be9 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1664 | llvm::Value *Vec |
Owen Anderson | e0b5eff | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1665 | = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals)); |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1666 | |
Chris Lattner | a23eb7b | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 1667 | for (unsigned i = 0, e = NumVals; i != e; ++i) { |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1668 | llvm::Value *Val = isSplat ? Vals[0] : Vals[i]; |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1669 | llvm::Value *Idx = llvm::ConstantInt::get( |
| 1670 | llvm::Type::getInt32Ty(VMContext), i); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1671 | Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp"); |
Anders Carlsson | 68b8be9 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1672 | } |
Mike Stump | 9b0bd80 | 2009-09-09 13:00:44 +0000 | [diff] [blame^] | 1673 | |
| 1674 | return Vec; |
Anders Carlsson | 68b8be9 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1675 | } |