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