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) |
Owen Anderson | 73e7f80 | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 56 | : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira), |
| 57 | VMContext(cgf.getLLVMContext()) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 58 | } |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 59 | |
| 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 | } |
| 76 | |
| 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 | } |
| 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); |
| 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 |
| 93 | /// complex type to the specified destination type, where the destination |
| 94 | /// type is an LLVM scalar type. |
| 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) { |
Daniel Dunbar | b5fda0c | 2008-08-16 01:41:47 +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())); |
| 139 | |
| 140 | return Builder.CreateIntToPtr(V, ConvertType(E->getType())); |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 141 | } |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 142 | |
| 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 | } |
Daniel Dunbar | 91cc402 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 149 | Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) { |
| 150 | return CGF.EmitObjCSelectorExpr(E); |
| 151 | } |
| 152 | Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) { |
| 153 | return CGF.EmitObjCProtocolExpr(E); |
| 154 | } |
| 155 | Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
| 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 | } |
| 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(); |
| 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 | |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +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())); |
| 195 | |
| 196 | // We have a scalar in braces. Just use the first element. |
| 197 | if (!VType) |
| 198 | return Visit(E->getInit(0)); |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +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); |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +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 | } |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 214 | |
| 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 | } |
| 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 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +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) { |
Fariborz Jahanian | ec17213 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 231 | if (const CXXFunctionalCastExpr *CXXFExpr = |
| 232 | dyn_cast<CXXFunctionalCastExpr>(E)) |
| 233 | return CGF.EmitCXXFunctionalCastExpr(CXXFExpr).getScalarVal(); |
| 234 | assert(isa<CStyleCastExpr>(E) && |
| 235 | "VisitCastExpr - missing CStyleCastExpr"); |
Fariborz Jahanian | c8a336f | 2009-08-26 23:31:30 +0000 | [diff] [blame] | 236 | } |
| 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); |
| 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); |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +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); |
Anders Carlsson | 49d4a57 | 2009-04-14 16:58:56 +0000 | [diff] [blame] | 289 | |
| 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(); |
| 296 | } |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 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 | } |
Douglas Gregor | 3e36851 | 2009-09-04 17:36:40 +0000 | [diff] [blame^] | 308 | |
| 309 | Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) { |
| 310 | // C++ [expr.pseudo]p1: |
| 311 | // The result shall only be used as the operand for the function call |
| 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 | } |
| 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 |
| 385 | |
| 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"); |
| 411 | |
| 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 | } |
| 417 | |
Anders Carlsson | e3ee66c | 2009-08-09 18:26:27 +0000 | [diff] [blame] | 418 | if (SrcType->isMemberPointerType()) { |
| 419 | // FIXME: This is ABI specific. |
| 420 | |
| 421 | // Compare against -1. |
| 422 | llvm::Value *NegativeOne = llvm::Constant::getAllOnesValue(Src->getType()); |
| 423 | return Builder.CreateICmpNE(Src, NegativeOne, "tobool"); |
| 424 | } |
| 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"); |
| 428 | |
| 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 | } |
| 444 | |
| 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; |
Chris Lattner | e133d7f | 2007-08-26 07:21:11 +0000 | [diff] [blame] | 457 | |
| 458 | if (DstType->isVoidType()) return 0; |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 459 | |
| 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); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 465 | |
| 466 | const llvm::Type *DstTy = ConvertType(DstType); |
| 467 | |
| 468 | // Ignore conversions like int -> uint. |
| 469 | if (Src->getType() == DstTy) |
| 470 | return Src; |
| 471 | |
Daniel Dunbar | 238335f | 2008-08-25 09:51:32 +0000 | [diff] [blame] | 472 | // Handle pointer conversions next: pointers can only be converted |
| 473 | // to/from other pointers and integers. Check for pointer types in |
| 474 | // terms of LLVM, as some native types (like Obj-C id) may map to a |
| 475 | // pointer type. |
| 476 | if (isa<llvm::PointerType>(DstTy)) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 477 | // The source value may be an integer, or a pointer. |
Fariborz Jahanian | f7f6cf8 | 2009-07-28 22:00:58 +0000 | [diff] [blame] | 478 | if (isa<llvm::PointerType>(Src->getType())) { |
| 479 | // Some heavy lifting for derived to base conversion. |
Fariborz Jahanian | 90e1874 | 2009-07-29 00:44:13 +0000 | [diff] [blame] | 480 | if (const CXXRecordDecl *ClassDecl = |
| 481 | SrcType->getCXXRecordDeclForPointerType()) |
| 482 | if (const CXXRecordDecl *BaseClassDecl = |
| 483 | DstType->getCXXRecordDeclForPointerType()) |
| 484 | Src = CGF.AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 485 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
Fariborz Jahanian | f7f6cf8 | 2009-07-28 22:00:58 +0000 | [diff] [blame] | 486 | } |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 487 | assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
Eli Friedman | 35bcec8 | 2009-03-04 04:02:35 +0000 | [diff] [blame] | 488 | // First, convert to the correct width so that we control the kind of |
| 489 | // extension. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 490 | const llvm::Type *MiddleTy = |
| 491 | llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); |
Eli Friedman | 35bcec8 | 2009-03-04 04:02:35 +0000 | [diff] [blame] | 492 | bool InputSigned = SrcType->isSignedIntegerType(); |
| 493 | llvm::Value* IntResult = |
| 494 | Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); |
| 495 | // Then, cast to pointer. |
| 496 | return Builder.CreateIntToPtr(IntResult, DstTy, "conv"); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Daniel Dunbar | 238335f | 2008-08-25 09:51:32 +0000 | [diff] [blame] | 499 | if (isa<llvm::PointerType>(Src->getType())) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 500 | // Must be an ptr to int cast. |
| 501 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
Anders Carlsson | 44db38f | 2007-10-31 23:18:02 +0000 | [diff] [blame] | 502 | return Builder.CreatePtrToInt(Src, DstTy, "conv"); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 505 | // 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] | 506 | if (DstType->isExtVectorType() && !SrcType->isVectorType()) { |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 507 | // Cast the scalar to element type |
| 508 | QualType EltTy = DstType->getAsExtVectorType()->getElementType(); |
| 509 | llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy); |
| 510 | |
| 511 | // Insert the element in element zero of an undef vector |
Owen Anderson | e0b5eff | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 512 | llvm::Value *UnV = llvm::UndefValue::get(DstTy); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 513 | llvm::Value *Idx = |
| 514 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 515 | UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp"); |
| 516 | |
| 517 | // Splat the element across to all elements |
| 518 | llvm::SmallVector<llvm::Constant*, 16> Args; |
| 519 | unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); |
| 520 | for (unsigned i = 0; i < NumElements; i++) |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 521 | Args.push_back(llvm::ConstantInt::get( |
| 522 | llvm::Type::getInt32Ty(VMContext), 0)); |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 523 | |
Owen Anderson | 17971fa | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 524 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
Nate Begeman | 7903d05 | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 525 | llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); |
| 526 | return Yay; |
| 527 | } |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 4f025a4 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 529 | // Allow bitcast from vector to integer/fp of the same size. |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 530 | if (isa<llvm::VectorType>(Src->getType()) || |
Chris Lattner | 4f025a4 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 531 | isa<llvm::VectorType>(DstTy)) |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 532 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
Anders Carlsson | 4513ecb | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 534 | // Finally, we have the arithmetic types: real int/float. |
| 535 | if (isa<llvm::IntegerType>(Src->getType())) { |
| 536 | bool InputSigned = SrcType->isSignedIntegerType(); |
Anders Carlsson | 4dac3f4 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 537 | if (isa<llvm::IntegerType>(DstTy)) |
| 538 | return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); |
| 539 | else if (InputSigned) |
| 540 | return Builder.CreateSIToFP(Src, DstTy, "conv"); |
| 541 | else |
| 542 | return Builder.CreateUIToFP(Src, DstTy, "conv"); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 546 | if (isa<llvm::IntegerType>(DstTy)) { |
Anders Carlsson | 4dac3f4 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 547 | if (DstType->isSignedIntegerType()) |
| 548 | return Builder.CreateFPToSI(Src, DstTy, "conv"); |
| 549 | else |
| 550 | return Builder.CreateFPToUI(Src, DstTy, "conv"); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | assert(DstTy->isFloatingPoint() && "Unknown real conversion"); |
Anders Carlsson | 4dac3f4 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 554 | if (DstTy->getTypeID() < Src->getType()->getTypeID()) |
| 555 | return Builder.CreateFPTrunc(Src, DstTy, "conv"); |
| 556 | else |
| 557 | return Builder.CreateFPExt(Src, DstTy, "conv"); |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 560 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 561 | /// complex type to the specified destination type, where the destination |
| 562 | /// type is an LLVM scalar type. |
| 563 | Value *ScalarExprEmitter:: |
| 564 | EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 565 | QualType SrcTy, QualType DstTy) { |
Chris Lattner | c39c365 | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 566 | // Get the source element type. |
Chris Lattner | c154ac1 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 567 | SrcTy = SrcTy->getAsComplexType()->getElementType(); |
Chris Lattner | c39c365 | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 568 | |
| 569 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 570 | if (DstTy->isBooleanType()) { |
| 571 | // Complex != 0 -> (Real != 0) | (Imag != 0) |
| 572 | Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 573 | Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); |
| 574 | return Builder.CreateOr(Src.first, Src.second, "tobool"); |
| 575 | } |
| 576 | |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 577 | // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, |
| 578 | // the imaginary part of the complex value is discarded and the value of the |
| 579 | // real part is converted according to the conversion rules for the |
| 580 | // corresponding real type. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 581 | return EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 582 | } |
| 583 | |
| 584 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 585 | //===----------------------------------------------------------------------===// |
| 586 | // Visitor Methods |
| 587 | //===----------------------------------------------------------------------===// |
| 588 | |
| 589 | Value *ScalarExprEmitter::VisitExpr(Expr *E) { |
Daniel Dunbar | 9503b78 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 590 | CGF.ErrorUnsupported(E, "scalar expression"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 591 | if (E->getType()->isVoidType()) |
| 592 | return 0; |
Owen Anderson | e0b5eff | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 593 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Eli Friedman | d0e9d09 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 596 | Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
| 597 | llvm::SmallVector<llvm::Constant*, 32> indices; |
| 598 | for (unsigned i = 2; i < E->getNumSubExprs(); i++) { |
| 599 | indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i)))); |
| 600 | } |
| 601 | Value* V1 = CGF.EmitScalarExpr(E->getExpr(0)); |
| 602 | Value* V2 = CGF.EmitScalarExpr(E->getExpr(1)); |
Owen Anderson | 17971fa | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 603 | Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size()); |
Eli Friedman | d0e9d09 | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 604 | return Builder.CreateShuffleVector(V1, V2, SV, "shuffle"); |
| 605 | } |
| 606 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 607 | Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 608 | TestAndClearIgnoreResultAssign(); |
| 609 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 610 | // Emit subscript expressions in rvalue context's. For most cases, this just |
| 611 | // loads the lvalue formed by the subscript expr. However, we have to be |
| 612 | // careful, because the base of a vector subscript is occasionally an rvalue, |
| 613 | // so we can't get it as an lvalue. |
| 614 | if (!E->getBase()->getType()->isVectorType()) |
| 615 | return EmitLoadOfLValue(E); |
| 616 | |
| 617 | // Handle the vector case. The base must be a vector, the index must be an |
| 618 | // integer value. |
| 619 | Value *Base = Visit(E->getBase()); |
| 620 | Value *Idx = Visit(E->getIdx()); |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 621 | bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType(); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 622 | Idx = Builder.CreateIntCast(Idx, |
| 623 | llvm::Type::getInt32Ty(CGF.getLLVMContext()), |
| 624 | IdxSigned, |
Eli Friedman | d453194 | 2009-03-28 03:27:06 +0000 | [diff] [blame] | 625 | "vecidxcast"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 626 | return Builder.CreateExtractElement(Base, Idx, "vecext"); |
| 627 | } |
| 628 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 629 | // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts |
| 630 | // have to handle a more broad range of conversions than explicit casts, as they |
| 631 | // handle things like function to ptr-to-function decay etc. |
Anders Carlsson | 2c61dbe | 2009-08-24 18:12:39 +0000 | [diff] [blame] | 632 | Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy, |
| 633 | CastExpr::CastKind Kind) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 634 | if (!DestTy->isVoidType()) |
| 635 | TestAndClearIgnoreResultAssign(); |
Anders Carlsson | 497794f | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 636 | |
| 637 | switch (Kind) { |
| 638 | default: |
| 639 | break; |
Anders Carlsson | f1957c8 | 2009-09-01 20:52:42 +0000 | [diff] [blame] | 640 | case CastExpr::CK_BitCast: { |
| 641 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 642 | return Builder.CreateBitCast(Src, ConvertType(DestTy)); |
| 643 | } |
Anders Carlsson | bade079 | 2009-08-24 18:37:17 +0000 | [diff] [blame] | 644 | case CastExpr::CK_ArrayToPointerDecay: { |
| 645 | assert(E->getType()->isArrayType() && |
| 646 | "Array to pointer decay must have array source type!"); |
| 647 | |
| 648 | Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays. |
| 649 | |
| 650 | // Note that VLA pointers are always decayed, so we don't need to do |
| 651 | // anything here. |
| 652 | if (!E->getType()->isVariableArrayType()) { |
| 653 | assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer"); |
| 654 | assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 655 | ->getElementType()) && |
| 656 | "Expected pointer to array"); |
| 657 | V = Builder.CreateStructGEP(V, 0, "arraydecay"); |
| 658 | } |
| 659 | |
| 660 | // The resultant pointer type can be implicitly casted to other pointer |
| 661 | // types as well (e.g. void*) and can be implicitly converted to integer. |
| 662 | const llvm::Type *DestLTy = ConvertType(DestTy); |
| 663 | if (V->getType() != DestLTy) { |
| 664 | if (isa<llvm::PointerType>(DestLTy)) |
| 665 | V = Builder.CreateBitCast(V, DestLTy, "ptrconv"); |
| 666 | else { |
| 667 | assert(isa<llvm::IntegerType>(DestLTy) && "Unknown array decay"); |
| 668 | V = Builder.CreatePtrToInt(V, DestLTy, "ptrconv"); |
| 669 | } |
| 670 | } |
| 671 | return V; |
| 672 | } |
Anders Carlsson | 497794f | 2009-08-24 18:26:39 +0000 | [diff] [blame] | 673 | case CastExpr::CK_NullToMemberPointer: |
| 674 | return CGF.CGM.EmitNullConstant(DestTy); |
| 675 | } |
| 676 | |
Chris Lattner | 82e1039 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 677 | // Handle cases where the source is an non-complex type. |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 678 | |
| 679 | if (!CGF.hasAggregateLLVMType(E->getType())) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 680 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 681 | |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 682 | // Use EmitScalarConversion to perform the conversion. |
| 683 | return EmitScalarConversion(Src, E->getType(), DestTy); |
| 684 | } |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 685 | |
Chris Lattner | de0908b | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 686 | if (E->getType()->isAnyComplexType()) { |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 687 | // Handle cases where the source is a complex type. |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 688 | bool IgnoreImag = true; |
| 689 | bool IgnoreImagAssign = true; |
| 690 | bool IgnoreReal = IgnoreResultAssign; |
| 691 | bool IgnoreRealAssign = IgnoreResultAssign; |
| 692 | if (DestTy->isBooleanType()) |
| 693 | IgnoreImagAssign = IgnoreImag = false; |
| 694 | else if (DestTy->isVoidType()) { |
| 695 | IgnoreReal = IgnoreImag = false; |
| 696 | IgnoreRealAssign = IgnoreImagAssign = true; |
| 697 | } |
| 698 | CodeGenFunction::ComplexPairTy V |
| 699 | = CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign, |
| 700 | IgnoreImagAssign); |
| 701 | return EmitComplexToScalarConversion(V, E->getType(), DestTy); |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 702 | } |
Chris Lattner | d579f7f | 2007-08-26 07:16:41 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 704 | // Okay, this is a cast from an aggregate. It must be a cast to void. Just |
| 705 | // evaluate the result and return. |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 706 | CGF.EmitAggExpr(E, 0, false, true); |
Chris Lattner | 7728879 | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 707 | return 0; |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 710 | Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
Chris Lattner | 09cee85 | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 711 | return CGF.EmitCompoundStmt(*E->getSubStmt(), |
| 712 | !E->getType()->isVoidType()).getScalarVal(); |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Mike Stump | 2b6933f | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 715 | Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { |
| 716 | return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp"); |
Mike Stump | fca5da0 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 717 | } |
Chris Lattner | ea6cdd7 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 718 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 719 | //===----------------------------------------------------------------------===// |
| 720 | // Unary Operators |
| 721 | //===----------------------------------------------------------------------===// |
| 722 | |
| 723 | Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | 855e3d7 | 2007-08-24 16:24:49 +0000 | [diff] [blame] | 724 | bool isInc, bool isPre) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 725 | LValue LV = EmitLValue(E->getSubExpr()); |
Eli Friedman | 6a25987 | 2009-03-23 03:00:06 +0000 | [diff] [blame] | 726 | QualType ValTy = E->getSubExpr()->getType(); |
| 727 | Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal(); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 728 | |
| 729 | llvm::LLVMContext &VMContext = CGF.getLLVMContext(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 730 | |
| 731 | int AmountVal = isInc ? 1 : -1; |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 732 | |
| 733 | if (ValTy->isPointerType() && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 734 | ValTy->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 735 | // The amount of the addition/subtraction needs to account for the VLA size |
| 736 | CGF.ErrorUnsupported(E, "VLA pointer inc/dec"); |
| 737 | } |
| 738 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 739 | Value *NextVal; |
Chris Lattner | 8360c61 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 740 | if (const llvm::PointerType *PT = |
| 741 | dyn_cast<llvm::PointerType>(InVal->getType())) { |
Owen Anderson | 73e7f80 | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 742 | llvm::Constant *Inc = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 743 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal); |
Chris Lattner | 8360c61 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 744 | if (!isa<llvm::FunctionType>(PT->getElementType())) { |
Fariborz Jahanian | 69b91ca | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 745 | QualType PTEE = ValTy->getPointeeType(); |
| 746 | if (const ObjCInterfaceType *OIT = |
| 747 | dyn_cast<ObjCInterfaceType>(PTEE)) { |
| 748 | // Handle interface types, which are not represented with a concrete type. |
| 749 | int size = CGF.getContext().getTypeSize(OIT) / 8; |
| 750 | if (!isInc) |
| 751 | size = -size; |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 752 | Inc = llvm::ConstantInt::get(Inc->getType(), size); |
Fariborz Jahanian | 69b91ca | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 753 | const llvm::Type *i8Ty = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 754 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Fariborz Jahanian | 69b91ca | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 755 | InVal = Builder.CreateBitCast(InVal, i8Ty); |
| 756 | NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr"); |
| 757 | llvm::Value *lhs = LV.getAddress(); |
Owen Anderson | 7ec2d8f | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 758 | lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty)); |
Fariborz Jahanian | 69b91ca | 2009-07-16 22:04:59 +0000 | [diff] [blame] | 759 | LV = LValue::MakeAddr(lhs, ValTy.getCVRQualifiers(), |
| 760 | CGF.getContext().getObjCGCAttrKind(ValTy)); |
Mike Stump | 487ce38 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 761 | } else |
Dan Gohman | 5a74824 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 762 | NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec"); |
Chris Lattner | 8360c61 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 763 | } else { |
Owen Anderson | 73e7f80 | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 764 | const llvm::Type *i8Ty = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 765 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Chris Lattner | 8360c61 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 766 | NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp"); |
| 767 | NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec"); |
| 768 | NextVal = Builder.CreateBitCast(NextVal, InVal->getType()); |
| 769 | } |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 770 | } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) { |
Chris Lattner | 4908317 | 2009-02-11 07:40:06 +0000 | [diff] [blame] | 771 | // Bool++ is an interesting case, due to promotion rules, we get: |
| 772 | // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 -> |
| 773 | // Bool = ((int)Bool+1) != 0 |
| 774 | // An interesting aspect of this is that increment is always true. |
| 775 | // Decrement does not have this property. |
Owen Anderson | d3fd60e | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 776 | NextVal = llvm::ConstantInt::getTrue(VMContext); |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 777 | } else if (isa<llvm::IntegerType>(InVal->getType())) { |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 778 | NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); |
Dan Gohman | c87cf1d | 2009-08-12 01:16:29 +0000 | [diff] [blame] | 779 | |
| 780 | // Signed integer overflow is undefined behavior. |
| 781 | if (ValTy->isSignedIntegerType()) |
| 782 | NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 783 | else |
| 784 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
Chris Lattner | 0dc11f6 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 785 | } else { |
| 786 | // Add the inc/dec to the real part. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 787 | if (InVal->getType() == llvm::Type::getFloatTy(VMContext)) |
Devang Patel | 0f2a8fb | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 788 | NextVal = |
Owen Anderson | b56fd0b | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 789 | llvm::ConstantFP::get(VMContext, |
| 790 | llvm::APFloat(static_cast<float>(AmountVal))); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 791 | else if (InVal->getType() == llvm::Type::getDoubleTy(VMContext)) |
Devang Patel | 0f2a8fb | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 792 | NextVal = |
Owen Anderson | b56fd0b | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 793 | llvm::ConstantFP::get(VMContext, |
| 794 | llvm::APFloat(static_cast<double>(AmountVal))); |
Chris Lattner | d54d1f2 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 795 | else { |
| 796 | llvm::APFloat F(static_cast<float>(AmountVal)); |
Dale Johannesen | 2461f61 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 797 | bool ignored; |
| 798 | F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero, |
| 799 | &ignored); |
Owen Anderson | b56fd0b | 2009-07-27 21:00:51 +0000 | [diff] [blame] | 800 | NextVal = llvm::ConstantFP::get(VMContext, F); |
Chris Lattner | b2a7dab | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 801 | } |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 802 | NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
Chris Lattner | 0dc11f6 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 803 | } |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 804 | |
| 805 | // Store the updated result through the lvalue. |
Eli Friedman | 6a25987 | 2009-03-23 03:00:06 +0000 | [diff] [blame] | 806 | if (LV.isBitfield()) |
| 807 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, |
| 808 | &NextVal); |
| 809 | else |
| 810 | CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 811 | |
| 812 | // If this is a postinc, return the value read from memory, otherwise use the |
| 813 | // updated value. |
| 814 | return isPre ? NextVal : InVal; |
| 815 | } |
| 816 | |
| 817 | |
| 818 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 819 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 820 | Value *Op = Visit(E->getSubExpr()); |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 821 | if (Op->getType()->isFPOrFPVector()) |
| 822 | return Builder.CreateFNeg(Op, "neg"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 823 | return Builder.CreateNeg(Op, "neg"); |
| 824 | } |
| 825 | |
| 826 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 827 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 828 | Value *Op = Visit(E->getSubExpr()); |
| 829 | return Builder.CreateNot(Op, "neg"); |
| 830 | } |
| 831 | |
| 832 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 833 | // Compare operand to zero. |
| 834 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
| 835 | |
| 836 | // Invert value. |
| 837 | // TODO: Could dynamically modify easy computations here. For example, if |
| 838 | // the operand is an icmp ne, turn into icmp eq. |
| 839 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
| 840 | |
Anders Carlsson | 62943f3 | 2009-05-19 18:44:53 +0000 | [diff] [blame] | 841 | // ZExt result to the expr type. |
| 842 | return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 845 | /// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of |
| 846 | /// argument of the sizeof expression as an integer. |
| 847 | Value * |
| 848 | ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 849 | QualType TypeToSize = E->getTypeOfArgument(); |
Eli Friedman | 5a2c38f | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 850 | if (E->isSizeOf()) { |
| 851 | if (const VariableArrayType *VAT = |
| 852 | CGF.getContext().getAsVariableArrayType(TypeToSize)) { |
| 853 | if (E->isArgumentType()) { |
| 854 | // sizeof(type) - make sure to emit the VLA size. |
| 855 | CGF.EmitVLASize(TypeToSize); |
Eli Friedman | 04659bd | 2009-04-20 03:21:44 +0000 | [diff] [blame] | 856 | } else { |
| 857 | // C99 6.5.3.4p2: If the argument is an expression of type |
| 858 | // VLA, it is evaluated. |
| 859 | CGF.EmitAnyExpr(E->getArgumentExpr()); |
Eli Friedman | 5a2c38f | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 860 | } |
Anders Carlsson | d309f57 | 2009-01-30 16:41:04 +0000 | [diff] [blame] | 861 | |
Anders Carlsson | 8f30de9 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 862 | return CGF.GetVLASize(VAT); |
Anders Carlsson | 6cb99b7 | 2008-12-21 03:33:21 +0000 | [diff] [blame] | 863 | } |
Anders Carlsson | 9be6aaf | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 864 | } |
Eli Friedman | 5a2c38f | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 865 | |
| 866 | // If this isn't sizeof(vla), the result must be constant; use the |
| 867 | // constant folding logic so we don't have to duplicate it here. |
| 868 | Expr::EvalResult Result; |
| 869 | E->Evaluate(Result, CGF.getContext()); |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 870 | return llvm::ConstantInt::get(VMContext, Result.Val.getInt()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 873 | Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { |
| 874 | Expr *Op = E->getSubExpr(); |
Chris Lattner | de0908b | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 875 | if (Op->getType()->isAnyComplexType()) |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 876 | return CGF.EmitComplexExpr(Op, false, true, false, true).first; |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 877 | return Visit(Op); |
| 878 | } |
| 879 | Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { |
| 880 | Expr *Op = E->getSubExpr(); |
Chris Lattner | de0908b | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 881 | if (Op->getType()->isAnyComplexType()) |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 882 | return CGF.EmitComplexExpr(Op, true, false, true, false).second; |
Chris Lattner | db8a6c9 | 2007-08-26 05:29:21 +0000 | [diff] [blame] | 883 | |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 884 | // __imag on a scalar returns zero. Emit the subexpr to ensure side |
| 885 | // effects are evaluated, but not the actual value. |
| 886 | if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid) |
| 887 | CGF.EmitLValue(Op); |
| 888 | else |
| 889 | CGF.EmitScalarExpr(Op, true); |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 890 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 893 | Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E) |
| 894 | { |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 895 | Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress(); |
Eli Friedman | ccffea9 | 2009-01-24 22:38:55 +0000 | [diff] [blame] | 896 | const llvm::Type* ResultType = ConvertType(E->getType()); |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 897 | return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof"); |
Anders Carlsson | 52774ad | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 898 | } |
Chris Lattner | 01211af | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 899 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 900 | //===----------------------------------------------------------------------===// |
| 901 | // Binary Operators |
| 902 | //===----------------------------------------------------------------------===// |
| 903 | |
| 904 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 905 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 906 | BinOpInfo Result; |
| 907 | Result.LHS = Visit(E->getLHS()); |
| 908 | Result.RHS = Visit(E->getRHS()); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 909 | Result.Ty = E->getType(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 910 | Result.E = E; |
| 911 | return Result; |
| 912 | } |
| 913 | |
Chris Lattner | 0d96530 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 914 | Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 915 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 916 | bool Ignore = TestAndClearIgnoreResultAssign(); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 917 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
| 918 | |
| 919 | BinOpInfo OpInfo; |
| 920 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 921 | if (E->getComputationResultType()->isAnyComplexType()) { |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 922 | // This needs to go through the complex expression emitter, but |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 923 | // it's a tad complicated to do that... I'm leaving it out for now. |
| 924 | // (Note that we do actually need the imaginary part of the RHS for |
| 925 | // multiplication and division.) |
| 926 | CGF.ErrorUnsupported(E, "complex compound assignment"); |
Owen Anderson | e0b5eff | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 927 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Mike Stump | 8d96226 | 2009-05-22 19:07:20 +0000 | [diff] [blame] | 930 | // Emit the RHS first. __block variables need to have the rhs evaluated |
| 931 | // first, plus this should improve codegen a little. |
| 932 | OpInfo.RHS = Visit(E->getRHS()); |
| 933 | OpInfo.Ty = E->getComputationResultType(); |
| 934 | OpInfo.E = E; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 935 | // Load/convert the LHS. |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 936 | LValue LHSLV = EmitLValue(E->getLHS()); |
| 937 | OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 938 | OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, |
| 939 | E->getComputationLHSType()); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 940 | |
| 941 | // Expand the binary operator. |
| 942 | Value *Result = (this->*Func)(OpInfo); |
| 943 | |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 944 | // Convert the result back to the LHS type. |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 945 | Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy); |
| 946 | |
Daniel Dunbar | 2668dd1 | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 947 | // Store the result value into the LHS lvalue. Bit-fields are |
Daniel Dunbar | 2710fc9 | 2008-11-19 11:54:05 +0000 | [diff] [blame] | 948 | // handled specially because the result is altered by the store, |
| 949 | // i.e., [C99 6.5.16p1] 'An assignment expression has the value of |
| 950 | // the left operand after the assignment...'. |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 951 | if (LHSLV.isBitfield()) { |
| 952 | if (!LHSLV.isVolatileQualified()) { |
| 953 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy, |
| 954 | &Result); |
| 955 | return Result; |
| 956 | } else |
| 957 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy); |
| 958 | } else |
Daniel Dunbar | 2668dd1 | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 959 | CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy); |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 960 | if (Ignore) |
| 961 | return 0; |
| 962 | return EmitLoadOfLValue(LHSLV, E->getType()); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 966 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
Nate Begeman | aade3bf | 2007-12-30 01:28:16 +0000 | [diff] [blame] | 967 | if (Ops.LHS->getType()->isFPOrFPVector()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 968 | return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 969 | else if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 970 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 971 | else |
| 972 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 973 | } |
| 974 | |
| 975 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 976 | // 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] | 977 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 978 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 979 | else |
| 980 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 981 | } |
| 982 | |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 983 | Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) { |
| 984 | unsigned IID; |
| 985 | unsigned OpID = 0; |
Mike Stump | 0f595bb | 2009-04-02 01:03:55 +0000 | [diff] [blame] | 986 | |
Mike Stump | f71b774 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 987 | switch (Ops.E->getOpcode()) { |
| 988 | case BinaryOperator::Add: |
| 989 | case BinaryOperator::AddAssign: |
| 990 | OpID = 1; |
| 991 | IID = llvm::Intrinsic::sadd_with_overflow; |
| 992 | break; |
| 993 | case BinaryOperator::Sub: |
| 994 | case BinaryOperator::SubAssign: |
| 995 | OpID = 2; |
| 996 | IID = llvm::Intrinsic::ssub_with_overflow; |
| 997 | break; |
| 998 | case BinaryOperator::Mul: |
| 999 | case BinaryOperator::MulAssign: |
| 1000 | OpID = 3; |
| 1001 | IID = llvm::Intrinsic::smul_with_overflow; |
| 1002 | break; |
| 1003 | default: |
| 1004 | assert(false && "Unsupported operation for overflow detection"); |
Daniel Dunbar | 96e909b | 2009-04-08 16:23:09 +0000 | [diff] [blame] | 1005 | IID = 0; |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1006 | } |
Mike Stump | f71b774 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1007 | OpID <<= 1; |
| 1008 | OpID |= 1; |
| 1009 | |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1010 | const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty); |
| 1011 | |
| 1012 | llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1); |
| 1013 | |
| 1014 | Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS); |
| 1015 | Value *result = Builder.CreateExtractValue(resultAndOverflow, 0); |
| 1016 | Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1); |
| 1017 | |
| 1018 | // Branch in case of overflow. |
| 1019 | llvm::BasicBlock *initialBB = Builder.GetInsertBlock(); |
| 1020 | llvm::BasicBlock *overflowBB = |
| 1021 | CGF.createBasicBlock("overflow", CGF.CurFn); |
| 1022 | llvm::BasicBlock *continueBB = |
| 1023 | CGF.createBasicBlock("overflow.continue", CGF.CurFn); |
| 1024 | |
| 1025 | Builder.CreateCondBr(overflow, overflowBB, continueBB); |
| 1026 | |
| 1027 | // Handle overflow |
| 1028 | |
| 1029 | Builder.SetInsertPoint(overflowBB); |
| 1030 | |
| 1031 | // Handler is: |
| 1032 | // long long *__overflow_handler)(long long a, long long b, char op, |
| 1033 | // char width) |
| 1034 | std::vector<const llvm::Type*> handerArgTypes; |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1035 | handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext)); |
| 1036 | handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext)); |
| 1037 | handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext)); |
| 1038 | handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext)); |
| 1039 | llvm::FunctionType *handlerTy = llvm::FunctionType::get( |
| 1040 | llvm::Type::getInt64Ty(VMContext), handerArgTypes, false); |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1041 | llvm::Value *handlerFunction = |
| 1042 | CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler", |
Owen Anderson | 7ec2d8f | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1043 | llvm::PointerType::getUnqual(handlerTy)); |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1044 | handlerFunction = Builder.CreateLoad(handlerFunction); |
| 1045 | |
| 1046 | llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction, |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1047 | Builder.CreateSExt(Ops.LHS, llvm::Type::getInt64Ty(VMContext)), |
| 1048 | Builder.CreateSExt(Ops.RHS, llvm::Type::getInt64Ty(VMContext)), |
| 1049 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), OpID), |
| 1050 | llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1051 | cast<llvm::IntegerType>(opTy)->getBitWidth())); |
| 1052 | |
| 1053 | handlerResult = Builder.CreateTrunc(handlerResult, opTy); |
| 1054 | |
| 1055 | Builder.CreateBr(continueBB); |
| 1056 | |
| 1057 | // Set up the continuation |
| 1058 | Builder.SetInsertPoint(continueBB); |
| 1059 | // Get the correct result |
| 1060 | llvm::PHINode *phi = Builder.CreatePHI(opTy); |
| 1061 | phi->reserveOperandSpace(2); |
| 1062 | phi->addIncoming(result, initialBB); |
| 1063 | phi->addIncoming(handlerResult, overflowBB); |
| 1064 | |
| 1065 | return phi; |
| 1066 | } |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1067 | |
| 1068 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1069 | if (!Ops.Ty->isAnyPointerType()) { |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1070 | if (CGF.getContext().getLangOptions().OverflowChecking && |
| 1071 | Ops.Ty->isSignedIntegerType()) |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1072 | return EmitOverflowCheckedBinOp(Ops); |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1073 | |
| 1074 | if (Ops.LHS->getType()->isFPOrFPVector()) |
| 1075 | return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add"); |
Dan Gohman | c87cf1d | 2009-08-12 01:16:29 +0000 | [diff] [blame] | 1076 | |
| 1077 | // Signed integer overflow is undefined behavior. |
| 1078 | if (Ops.Ty->isSignedIntegerType()) |
| 1079 | return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add"); |
| 1080 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1081 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1082 | } |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1083 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1084 | if (Ops.Ty->isPointerType() && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1085 | Ops.Ty->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1086 | // The amount of the addition needs to account for the VLA size |
| 1087 | CGF.ErrorUnsupported(Ops.E, "VLA pointer addition"); |
| 1088 | } |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1089 | Value *Ptr, *Idx; |
| 1090 | Expr *IdxExp; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1091 | const PointerType *PT = Ops.E->getLHS()->getType()->getAs<PointerType>(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1092 | const ObjCObjectPointerType *OPT = |
| 1093 | Ops.E->getLHS()->getType()->getAsObjCObjectPointerType(); |
| 1094 | if (PT || OPT) { |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1095 | Ptr = Ops.LHS; |
| 1096 | Idx = Ops.RHS; |
| 1097 | IdxExp = Ops.E->getRHS(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1098 | } else { // int + pointer |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1099 | PT = Ops.E->getRHS()->getType()->getAs<PointerType>(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1100 | OPT = Ops.E->getRHS()->getType()->getAsObjCObjectPointerType(); |
| 1101 | assert((PT || OPT) && "Invalid add expr"); |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1102 | Ptr = Ops.RHS; |
| 1103 | Idx = Ops.LHS; |
| 1104 | IdxExp = Ops.E->getLHS(); |
| 1105 | } |
| 1106 | |
| 1107 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
Sanjiv Gupta | cee8fea | 2009-04-24 02:40:57 +0000 | [diff] [blame] | 1108 | if (Width < CGF.LLVMPointerWidth) { |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1109 | // Zero or sign extend the pointer value based on whether the index is |
| 1110 | // signed or not. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1111 | const llvm::Type *IdxType = |
| 1112 | llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); |
Chris Lattner | c154ac1 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 1113 | if (IdxExp->getType()->isSignedIntegerType()) |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1114 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 1115 | else |
| 1116 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 1117 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1118 | const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType(); |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1119 | // Handle interface types, which are not represented with a concrete |
| 1120 | // type. |
| 1121 | if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) { |
| 1122 | llvm::Value *InterfaceSize = |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1123 | llvm::ConstantInt::get(Idx->getType(), |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1124 | CGF.getContext().getTypeSize(OIT) / 8); |
| 1125 | Idx = Builder.CreateMul(Idx, InterfaceSize); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1126 | const llvm::Type *i8Ty = |
| 1127 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1128 | Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); |
| 1129 | Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); |
| 1130 | return Builder.CreateBitCast(Res, Ptr->getType()); |
| 1131 | } |
| 1132 | |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1133 | // Explicitly handle GNU void* and function pointer arithmetic |
| 1134 | // extensions. The GNU void* casts amount to no-ops since our void* |
| 1135 | // type is i8*, but this is future proof. |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1136 | if (ElementType->isVoidType() || ElementType->isFunctionType()) { |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1137 | const llvm::Type *i8Ty = |
| 1138 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1139 | Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1140 | Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1141 | return Builder.CreateBitCast(Res, Ptr->getType()); |
| 1142 | } |
Chris Lattner | 17c0cb0 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 1143 | |
Dan Gohman | 5a74824 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1144 | return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1148 | if (!isa<llvm::PointerType>(Ops.LHS->getType())) { |
Mike Stump | f71b774 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 1149 | if (CGF.getContext().getLangOptions().OverflowChecking |
| 1150 | && Ops.Ty->isSignedIntegerType()) |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1151 | return EmitOverflowCheckedBinOp(Ops); |
Chris Lattner | 291a2b3 | 2009-06-17 06:36:24 +0000 | [diff] [blame] | 1152 | |
| 1153 | if (Ops.LHS->getType()->isFPOrFPVector()) |
| 1154 | return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1155 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1156 | } |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1157 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1158 | if (Ops.E->getLHS()->getType()->isPointerType() && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1159 | Ops.E->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) { |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1160 | // The amount of the addition needs to account for the VLA size for |
| 1161 | // ptr-int |
| 1162 | // The amount of the division needs to account for the VLA size for |
| 1163 | // ptr-ptr. |
| 1164 | CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction"); |
| 1165 | } |
| 1166 | |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1167 | const QualType LHSType = Ops.E->getLHS()->getType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1168 | const QualType LHSElementType = LHSType->getPointeeType(); |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1169 | if (!isa<llvm::PointerType>(Ops.RHS->getType())) { |
| 1170 | // pointer - int |
| 1171 | Value *Idx = Ops.RHS; |
| 1172 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
Sanjiv Gupta | cee8fea | 2009-04-24 02:40:57 +0000 | [diff] [blame] | 1173 | if (Width < CGF.LLVMPointerWidth) { |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1174 | // Zero or sign extend the pointer value based on whether the index is |
| 1175 | // signed or not. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1176 | const llvm::Type *IdxType = |
| 1177 | llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1178 | if (Ops.E->getRHS()->getType()->isSignedIntegerType()) |
| 1179 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 1180 | else |
| 1181 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 1182 | } |
| 1183 | Idx = Builder.CreateNeg(Idx, "sub.ptr.neg"); |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1184 | |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1185 | // Handle interface types, which are not represented with a concrete |
| 1186 | // type. |
| 1187 | if (const ObjCInterfaceType *OIT = |
| 1188 | dyn_cast<ObjCInterfaceType>(LHSElementType)) { |
| 1189 | llvm::Value *InterfaceSize = |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1190 | llvm::ConstantInt::get(Idx->getType(), |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1191 | CGF.getContext().getTypeSize(OIT) / 8); |
| 1192 | Idx = Builder.CreateMul(Idx, InterfaceSize); |
Owen Anderson | 73e7f80 | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1193 | const llvm::Type *i8Ty = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1194 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Daniel Dunbar | 6864c0d | 2009-04-25 05:08:32 +0000 | [diff] [blame] | 1195 | Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); |
| 1196 | Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr"); |
| 1197 | return Builder.CreateBitCast(Res, Ops.LHS->getType()); |
| 1198 | } |
| 1199 | |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1200 | // Explicitly handle GNU void* and function pointer arithmetic |
| 1201 | // extensions. The GNU void* casts amount to no-ops since our |
| 1202 | // void* type is i8*, but this is future proof. |
| 1203 | if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { |
Owen Anderson | 73e7f80 | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1204 | const llvm::Type *i8Ty = |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1205 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Daniel Dunbar | 4fd58ab | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 1206 | Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); |
| 1207 | Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr"); |
| 1208 | return Builder.CreateBitCast(Res, Ops.LHS->getType()); |
| 1209 | } |
| 1210 | |
Dan Gohman | 5a74824 | 2009-08-12 00:33:55 +0000 | [diff] [blame] | 1211 | return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr"); |
Daniel Dunbar | 0aac9f6 | 2008-08-05 00:47:03 +0000 | [diff] [blame] | 1212 | } else { |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1213 | // pointer - pointer |
| 1214 | Value *LHS = Ops.LHS; |
| 1215 | Value *RHS = Ops.RHS; |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1216 | |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1217 | uint64_t ElementSize; |
Daniel Dunbar | 0aac9f6 | 2008-08-05 00:47:03 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | 6d2e349 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 1219 | // Handle GCC extension for pointer arithmetic on void* and function pointer |
| 1220 | // types. |
| 1221 | if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { |
Daniel Dunbar | 5d7d038 | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 1222 | ElementSize = 1; |
| 1223 | } else { |
| 1224 | ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8; |
| 1225 | } |
| 1226 | |
| 1227 | const llvm::Type *ResultType = ConvertType(Ops.Ty); |
| 1228 | LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); |
| 1229 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 1230 | Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); |
| 1231 | |
Chris Lattner | 6d2e349 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 1232 | // Optimize out the shift for element size of 1. |
| 1233 | if (ElementSize == 1) |
| 1234 | return BytesBetween; |
Dan Gohman | 8f61ba4 | 2009-08-11 22:40:09 +0000 | [diff] [blame] | 1235 | |
| 1236 | // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since |
| 1237 | // pointer difference in C is only defined in the case where both |
| 1238 | // operands are pointing to elements of an array. |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1239 | Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize); |
Dan Gohman | 8f61ba4 | 2009-08-11 22:40:09 +0000 | [diff] [blame] | 1240 | return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1241 | } |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 1245 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 1246 | // RHS to the same size as the LHS. |
| 1247 | Value *RHS = Ops.RHS; |
| 1248 | if (Ops.LHS->getType() != RHS->getType()) |
| 1249 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 1250 | |
| 1251 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 1252 | } |
| 1253 | |
| 1254 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 1255 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 1256 | // RHS to the same size as the LHS. |
| 1257 | Value *RHS = Ops.RHS; |
| 1258 | if (Ops.LHS->getType() != RHS->getType()) |
| 1259 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 1260 | |
Chris Lattner | 660e31d | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1261 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1262 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 1263 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 1264 | } |
| 1265 | |
| 1266 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 1267 | unsigned SICmpOpc, unsigned FCmpOpc) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1268 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1269 | Value *Result; |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1270 | QualType LHSTy = E->getLHS()->getType(); |
Chris Lattner | 08ac852 | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 1271 | if (!LHSTy->isAnyComplexType()) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1272 | Value *LHS = Visit(E->getLHS()); |
| 1273 | Value *RHS = Visit(E->getRHS()); |
| 1274 | |
Eli Friedman | 04865bc | 2009-07-22 06:07:16 +0000 | [diff] [blame] | 1275 | if (LHS->getType()->isFPOrFPVector()) { |
Nate Begeman | 1591bc5 | 2008-07-25 20:16:05 +0000 | [diff] [blame] | 1276 | Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc, |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1277 | LHS, RHS, "cmp"); |
Eli Friedman | 850ea37 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 1278 | } else if (LHSTy->isSignedIntegerType()) { |
| 1279 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1280 | LHS, RHS, "cmp"); |
| 1281 | } else { |
Eli Friedman | 850ea37 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 1282 | // Unsigned integers and pointers. |
| 1283 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1284 | LHS, RHS, "cmp"); |
| 1285 | } |
Chris Lattner | 08ac852 | 2009-07-08 01:08:03 +0000 | [diff] [blame] | 1286 | |
| 1287 | // If this is a vector comparison, sign extend the result to the appropriate |
| 1288 | // vector integer type and return it (don't convert to bool). |
| 1289 | if (LHSTy->isVectorType()) |
| 1290 | return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); |
Nate Begeman | 1591bc5 | 2008-07-25 20:16:05 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1292 | } else { |
| 1293 | // Complex Comparison: can only be an equality comparison. |
| 1294 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 1295 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
| 1296 | |
Chris Lattner | c154ac1 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 1297 | QualType CETy = LHSTy->getAsComplexType()->getElementType(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1298 | |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1299 | Value *ResultR, *ResultI; |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1300 | if (CETy->isRealFloatingType()) { |
| 1301 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1302 | LHS.first, RHS.first, "cmp.r"); |
| 1303 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1304 | LHS.second, RHS.second, "cmp.i"); |
| 1305 | } else { |
| 1306 | // Complex comparisons can only be equality comparisons. As such, signed |
| 1307 | // and unsigned opcodes are the same. |
| 1308 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 1309 | LHS.first, RHS.first, "cmp.r"); |
| 1310 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 1311 | LHS.second, RHS.second, "cmp.i"); |
| 1312 | } |
| 1313 | |
| 1314 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 1315 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 1316 | } else { |
| 1317 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1318 | "Complex comparison other than == or != ?"); |
| 1319 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 1320 | } |
| 1321 | } |
Nuno Lopes | 9257700 | 2009-01-11 23:22:37 +0000 | [diff] [blame] | 1322 | |
| 1323 | return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1327 | bool Ignore = TestAndClearIgnoreResultAssign(); |
| 1328 | |
| 1329 | // __block variables need to have the rhs evaluated first, plus this should |
| 1330 | // improve codegen just a little. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1331 | Value *RHS = Visit(E->getRHS()); |
Mike Stump | 68df15c | 2009-05-21 21:05:15 +0000 | [diff] [blame] | 1332 | LValue LHS = EmitLValue(E->getLHS()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1333 | |
Daniel Dunbar | 2668dd1 | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1334 | // Store the value into the LHS. Bit-fields are handled specially |
Daniel Dunbar | 2710fc9 | 2008-11-19 11:54:05 +0000 | [diff] [blame] | 1335 | // because the result is altered by the store, i.e., [C99 6.5.16p1] |
| 1336 | // 'An assignment expression has the value of the left operand after |
Eli Friedman | 4a0073b | 2009-03-28 02:45:41 +0000 | [diff] [blame] | 1337 | // the assignment...'. |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1338 | if (LHS.isBitfield()) { |
| 1339 | if (!LHS.isVolatileQualified()) { |
| 1340 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(), |
| 1341 | &RHS); |
| 1342 | return RHS; |
| 1343 | } else |
| 1344 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType()); |
| 1345 | } else |
Daniel Dunbar | 2668dd1 | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1346 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1347 | if (Ignore) |
| 1348 | return 0; |
| 1349 | return EmitLoadOfLValue(LHS, E->getType()); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
Chris Lattner | 715c2a7 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1353 | // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. |
| 1354 | // If we have 1 && X, just emit X without inserting the control flow. |
| 1355 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { |
| 1356 | if (Cond == 1) { // If we have 1 && X, just emit X. |
Chris Lattner | 3f73d0d | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1357 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 1358 | // ZExt result to int. |
| 1359 | return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext"); |
| 1360 | } |
Chris Lattner | 715c2a7 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1361 | |
| 1362 | // 0 && RHS: If it is safe, just elide the RHS, and return 0. |
| 1363 | if (!CGF.ContainsLabel(E->getRHS())) |
Owen Anderson | f37b84b | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1364 | return llvm::Constant::getNullValue(CGF.LLVMIntTy); |
Chris Lattner | 3f73d0d | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Daniel Dunbar | 6e3a10c | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1367 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); |
| 1368 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); |
Chris Lattner | 715c2a7 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1369 | |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1370 | // Branch on the LHS first. If it is false, go to the failure (cont) block. |
| 1371 | CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock); |
| 1372 | |
| 1373 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 1374 | // edges from this first condition. All of these values will be false. Start |
| 1375 | // setting up the PHI node in the Cont Block for this. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1376 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), |
| 1377 | "", ContBlock); |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1378 | PN->reserveOperandSpace(2); // Normal case, two inputs. |
| 1379 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 1380 | PI != PE; ++PI) |
Owen Anderson | d3fd60e | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 1381 | PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1382 | |
Anders Carlsson | ac36c0e | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1383 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1384 | CGF.EmitBlock(RHSBlock); |
| 1385 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
Anders Carlsson | ac36c0e | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1386 | CGF.PopConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1389 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 1392 | // into the phi node for the edge with the value of RHSCond. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1393 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1394 | PN->addIncoming(RHSCond, RHSBlock); |
| 1395 | |
| 1396 | // ZExt result to int. |
| 1397 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext"); |
| 1398 | } |
| 1399 | |
| 1400 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
Chris Lattner | 715c2a7 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1401 | // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. |
| 1402 | // If we have 0 || X, just emit X without inserting the control flow. |
| 1403 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { |
| 1404 | if (Cond == -1) { // If we have 0 || X, just emit X. |
Chris Lattner | 3f73d0d | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1405 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 1406 | // ZExt result to int. |
| 1407 | return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext"); |
| 1408 | } |
Chris Lattner | 715c2a7 | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1409 | |
Eli Friedman | ea137cd | 2008-12-02 16:02:46 +0000 | [diff] [blame] | 1410 | // 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] | 1411 | if (!CGF.ContainsLabel(E->getRHS())) |
Owen Anderson | b17ec71 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1412 | return llvm::ConstantInt::get(CGF.LLVMIntTy, 1); |
Chris Lattner | 3f73d0d | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Daniel Dunbar | 6e3a10c | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1415 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); |
| 1416 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1417 | |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1418 | // Branch on the LHS first. If it is true, go to the success (cont) block. |
| 1419 | CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock); |
| 1420 | |
| 1421 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 1422 | // edges from this first condition. All of these values will be true. Start |
| 1423 | // setting up the PHI node in the Cont Block for this. |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1424 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), |
| 1425 | "", ContBlock); |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1426 | PN->reserveOperandSpace(2); // Normal case, two inputs. |
| 1427 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 1428 | PI != PE; ++PI) |
Owen Anderson | d3fd60e | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 1429 | PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI); |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1430 | |
Anders Carlsson | ac36c0e | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1431 | CGF.PushConditionalTempDestruction(); |
| 1432 | |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1433 | // Emit the RHS condition as a bool value. |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1434 | CGF.EmitBlock(RHSBlock); |
| 1435 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 1436 | |
Anders Carlsson | ac36c0e | 2009-06-04 02:53:13 +0000 | [diff] [blame] | 1437 | CGF.PopConditionalTempDestruction(); |
| 1438 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1439 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1440 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1441 | |
Chris Lattner | 7f80bb3 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1442 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 1443 | // into the phi node for the edge with the value of RHSCond. |
| 1444 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1445 | PN->addIncoming(RHSCond, RHSBlock); |
| 1446 | |
| 1447 | // ZExt result to int. |
| 1448 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext"); |
| 1449 | } |
| 1450 | |
| 1451 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 1452 | CGF.EmitStmt(E->getLHS()); |
Daniel Dunbar | 5aa22bc | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1453 | CGF.EnsureInsertPoint(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1454 | return Visit(E->getRHS()); |
| 1455 | } |
| 1456 | |
| 1457 | //===----------------------------------------------------------------------===// |
| 1458 | // Other Operators |
| 1459 | //===----------------------------------------------------------------------===// |
| 1460 | |
Chris Lattner | 504a528 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1461 | /// isCheapEnoughToEvaluateUnconditionally - Return true if the specified |
| 1462 | /// expression is cheap enough and side-effect-free enough to evaluate |
| 1463 | /// unconditionally instead of conditionally. This is used to convert control |
| 1464 | /// flow into selects in some cases. |
| 1465 | static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) { |
| 1466 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 1467 | return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr()); |
| 1468 | |
| 1469 | // TODO: Allow anything we can constant fold to an integer or fp constant. |
| 1470 | if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) || |
| 1471 | isa<FloatingLiteral>(E)) |
| 1472 | return true; |
| 1473 | |
| 1474 | // Non-volatile automatic variables too, to get "cond ? X : Y" where |
| 1475 | // X and Y are local variables. |
| 1476 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 1477 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 1478 | if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified()) |
| 1479 | return true; |
| 1480 | |
| 1481 | return false; |
| 1482 | } |
| 1483 | |
| 1484 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1485 | Value *ScalarExprEmitter:: |
| 1486 | VisitConditionalOperator(const ConditionalOperator *E) { |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1487 | TestAndClearIgnoreResultAssign(); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1488 | // If the condition constant folds and can be elided, try to avoid emitting |
| 1489 | // the condition and the dead arm. |
| 1490 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){ |
Chris Lattner | 044bffc | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1491 | Expr *Live = E->getLHS(), *Dead = E->getRHS(); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1492 | if (Cond == -1) |
Chris Lattner | 044bffc | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1493 | std::swap(Live, Dead); |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1494 | |
| 1495 | // If the dead side doesn't have labels we need, and if the Live side isn't |
| 1496 | // the gnu missing ?: extension (which we could handle, but don't bother |
| 1497 | // to), just emit the Live part. |
| 1498 | if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part |
| 1499 | Live) // Live part isn't missing. |
| 1500 | return Visit(Live); |
Chris Lattner | 044bffc | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Chris Lattner | 504a528 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1503 | |
| 1504 | // If this is a really simple expression (like x ? 4 : 5), emit this as a |
| 1505 | // 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] | 1506 | // safe to evaluate the LHS and RHS unconditionally. |
Chris Lattner | 504a528 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1507 | if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) && |
| 1508 | isCheapEnoughToEvaluateUnconditionally(E->getRHS())) { |
| 1509 | llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond()); |
| 1510 | llvm::Value *LHS = Visit(E->getLHS()); |
| 1511 | llvm::Value *RHS = Visit(E->getRHS()); |
| 1512 | return Builder.CreateSelect(CondV, LHS, RHS, "cond"); |
| 1513 | } |
| 1514 | |
| 1515 | |
Daniel Dunbar | b23e992 | 2008-11-12 10:13:37 +0000 | [diff] [blame] | 1516 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 1517 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
Daniel Dunbar | 6e3a10c | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1518 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Chris Lattner | 67e2246 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1519 | Value *CondVal = 0; |
Chris Lattner | 3d6606b | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1520 | |
Chris Lattner | 8603171 | 2009-02-13 23:35:32 +0000 | [diff] [blame] | 1521 | // If we don't have the GNU missing condition extension, emit a branch on |
| 1522 | // bool the normal way. |
| 1523 | if (E->getLHS()) { |
| 1524 | // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for |
| 1525 | // the branch on bool. |
| 1526 | CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); |
| 1527 | } else { |
| 1528 | // Otherwise, for the ?: extension, evaluate the conditional and then |
| 1529 | // convert it to bool the hard way. We do this explicitly because we need |
| 1530 | // the unconverted value for the missing middle value of the ?:. |
Chris Lattner | 67e2246 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1531 | CondVal = CGF.EmitScalarExpr(E->getCond()); |
Chris Lattner | 8603171 | 2009-02-13 23:35:32 +0000 | [diff] [blame] | 1532 | |
| 1533 | // In some cases, EmitScalarConversion will delete the "CondVal" expression |
| 1534 | // if there are no extra uses (an optimization). Inhibit this by making an |
| 1535 | // extra dead use, because we're going to add a use of CondVal later. We |
| 1536 | // don't use the builder for this, because we don't want it to get optimized |
| 1537 | // away. This leaves dead code, but the ?: extension isn't common. |
| 1538 | new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder", |
| 1539 | Builder.GetInsertBlock()); |
| 1540 | |
Chris Lattner | 67e2246 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1541 | Value *CondBoolVal = |
| 1542 | CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), |
| 1543 | CGF.getContext().BoolTy); |
| 1544 | Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); |
Chris Lattner | 67e2246 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1545 | } |
Anders Carlsson | bf3b93a | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1546 | |
| 1547 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1548 | CGF.EmitBlock(LHSBlock); |
| 1549 | |
| 1550 | // Handle the GNU extension for missing LHS. |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1551 | Value *LHS; |
| 1552 | if (E->getLHS()) |
Eli Friedman | ce8d703 | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 1553 | LHS = Visit(E->getLHS()); |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1554 | else // Perform promotions, to handle cases like "short ?: int" |
| 1555 | LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType()); |
| 1556 | |
Anders Carlsson | bf3b93a | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1557 | CGF.PopConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1558 | LHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | 5276caa | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 1559 | CGF.EmitBranch(ContBlock); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1560 | |
Anders Carlsson | bf3b93a | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1561 | CGF.PushConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1562 | CGF.EmitBlock(RHSBlock); |
| 1563 | |
Eli Friedman | ce8d703 | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 1564 | Value *RHS = Visit(E->getRHS()); |
Anders Carlsson | bf3b93a | 2009-06-04 03:00:32 +0000 | [diff] [blame] | 1565 | CGF.PopConditionalTempDestruction(); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1566 | RHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | 5276caa | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 1567 | CGF.EmitBranch(ContBlock); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1568 | |
| 1569 | CGF.EmitBlock(ContBlock); |
| 1570 | |
Nuno Lopes | b62ff24 | 2008-06-04 19:15:45 +0000 | [diff] [blame] | 1571 | if (!LHS || !RHS) { |
Chris Lattner | 307da02 | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1572 | assert(E->getType()->isVoidType() && "Non-void value should have a value"); |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1576 | // Create a PHI node for the real part. |
| 1577 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); |
| 1578 | PN->reserveOperandSpace(2); |
| 1579 | PN->addIncoming(LHS, LHSBlock); |
| 1580 | PN->addIncoming(RHS, RHSBlock); |
| 1581 | return PN; |
| 1582 | } |
| 1583 | |
| 1584 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Eli Friedman | d540c11 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 1585 | return Visit(E->getChosenSubExpr(CGF.getContext())); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Chris Lattner | 307da02 | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1588 | Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Eli Friedman | 8f5e878 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 1589 | llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); |
Anders Carlsson | 285611e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1590 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); |
| 1591 | |
| 1592 | // If EmitVAArg fails, we fall back to the LLVM instruction. |
| 1593 | if (!ArgPtr) |
| 1594 | return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); |
| 1595 | |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1596 | // FIXME Volatility. |
Anders Carlsson | 285611e | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1597 | return Builder.CreateLoad(ArgPtr); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Mike Stump | 4eb81dc | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 1600 | Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) { |
Mike Stump | 1fa52fe | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1601 | return CGF.BuildBlockLiteralTmp(BE); |
Mike Stump | 4eb81dc | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1604 | //===----------------------------------------------------------------------===// |
| 1605 | // Entry Point into this File |
| 1606 | //===----------------------------------------------------------------------===// |
| 1607 | |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1608 | /// EmitScalarExpr - Emit the computation of the specified expression of |
| 1609 | /// scalar type, ignoring the result. |
| 1610 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) { |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1611 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 1612 | "Invalid scalar expression to emit"); |
| 1613 | |
Mike Stump | b8fc73e | 2009-05-29 15:46:01 +0000 | [diff] [blame] | 1614 | return ScalarExprEmitter(*this, IgnoreResultAssign) |
| 1615 | .Visit(const_cast<Expr*>(E)); |
Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1616 | } |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1617 | |
| 1618 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 1619 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1620 | Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, |
| 1621 | QualType DstTy) { |
Chris Lattner | 4e05d1e | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1622 | assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && |
| 1623 | "Invalid scalar expression to emit"); |
| 1624 | return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); |
| 1625 | } |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1626 | |
| 1627 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 1628 | /// complex type to the specified destination type, where the destination |
| 1629 | /// type is an LLVM scalar type. |
| 1630 | Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, |
| 1631 | QualType SrcTy, |
| 1632 | QualType DstTy) { |
Chris Lattner | de0908b | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 1633 | assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) && |
Chris Lattner | fb182ee | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1634 | "Invalid complex -> scalar conversion"); |
| 1635 | return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, |
| 1636 | DstTy); |
| 1637 | } |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1638 | |
| 1639 | Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) { |
| 1640 | assert(V1->getType() == V2->getType() && |
| 1641 | "Vector operands must be of the same type"); |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1642 | unsigned NumElements = |
| 1643 | cast<llvm::VectorType>(V1->getType())->getNumElements(); |
| 1644 | |
| 1645 | va_list va; |
| 1646 | va_start(va, V2); |
| 1647 | |
| 1648 | llvm::SmallVector<llvm::Constant*, 16> Args; |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1649 | for (unsigned i = 0; i < NumElements; i++) { |
| 1650 | int n = va_arg(va, int); |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1651 | assert(n >= 0 && n < (int)NumElements * 2 && |
| 1652 | "Vector shuffle index out of bounds!"); |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1653 | Args.push_back(llvm::ConstantInt::get( |
| 1654 | llvm::Type::getInt32Ty(VMContext), n)); |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | const char *Name = va_arg(va, const char *); |
| 1658 | va_end(va); |
| 1659 | |
Owen Anderson | 17971fa | 2009-07-28 21:22:35 +0000 | [diff] [blame] | 1660 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
Anders Carlsson | a9234fe | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1661 | |
| 1662 | return Builder.CreateShuffleVector(V1, V2, Mask, Name); |
| 1663 | } |
| 1664 | |
Anders Carlsson | 68b8be9 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1665 | llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals, |
Chris Lattner | a23eb7b | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 1666 | unsigned NumVals, bool isSplat) { |
Anders Carlsson | 68b8be9 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1667 | llvm::Value *Vec |
Owen Anderson | e0b5eff | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1668 | = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals)); |
Anders Carlsson | 68b8be9 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | a23eb7b | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 1670 | for (unsigned i = 0, e = NumVals; i != e; ++i) { |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1671 | llvm::Value *Val = isSplat ? Vals[0] : Vals[i]; |
Owen Anderson | 3f5cc0a | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1672 | llvm::Value *Idx = llvm::ConstantInt::get( |
| 1673 | llvm::Type::getInt32Ty(VMContext), i); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1674 | Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp"); |
Anders Carlsson | 68b8be9 | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | return Vec; |
| 1678 | } |