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