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