Chris Lattner | 7f02f72 | 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 | 0bc735f | 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 | 7f02f72 | 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 | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 98c5ead | 2008-08-12 05:08:18 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Eli Friedman | 769e411 | 2009-01-24 22:38:55 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecordLayout.h" |
Daniel Dunbar | de7fb84 | 2008-08-11 05:00:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 25ddea7 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/Function.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalVariable.h" |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 24 | #include "llvm/Intrinsics.h" |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CFG.h" |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Chris Lattner | c89bf69 | 2008-01-03 07:05:49 +0000 | [diff] [blame] | 28 | #include <cstdarg> |
Ted Kremenek | 6aad91a | 2007-12-10 23:44:32 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | using llvm::Value; |
| 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | // Scalar Expression Emitter |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
| 38 | struct BinOpInfo { |
| 39 | Value *LHS; |
| 40 | Value *RHS; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 41 | QualType Ty; // Computation Type. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 42 | const BinaryOperator *E; |
| 43 | }; |
| 44 | |
| 45 | namespace { |
| 46 | class VISIBILITY_HIDDEN ScalarExprEmitter |
| 47 | : public StmtVisitor<ScalarExprEmitter, Value*> { |
| 48 | CodeGenFunction &CGF; |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 49 | CGBuilderTy &Builder; |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 51 | public: |
| 52 | |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 53 | ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf), |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 54 | Builder(CGF.Builder) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 55 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 56 | |
| 57 | //===--------------------------------------------------------------------===// |
| 58 | // Utilities |
| 59 | //===--------------------------------------------------------------------===// |
| 60 | |
| 61 | const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } |
| 62 | LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } |
| 63 | |
| 64 | Value *EmitLoadOfLValue(LValue LV, QualType T) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 65 | return CGF.EmitLoadOfLValue(LV, T).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 69 | /// value l-value, this method emits the address of the l-value, then loads |
| 70 | /// and returns the result. |
| 71 | Value *EmitLoadOfLValue(const Expr *E) { |
| 72 | // FIXME: Volatile |
| 73 | return EmitLoadOfLValue(EmitLValue(E), E->getType()); |
| 74 | } |
| 75 | |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 76 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 3420d0d | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 77 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 78 | Value *EmitConversionToBool(Value *Src, QualType DstTy); |
| 79 | |
Chris Lattner | 3707b25 | 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 | 4f1a7b3 | 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 | df6b68c | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 90 | //===--------------------------------------------------------------------===// |
| 91 | // Visitor Methods |
| 92 | //===--------------------------------------------------------------------===// |
| 93 | |
| 94 | Value *VisitStmt(Stmt *S) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 95 | S->dump(CGF.getContext().getSourceManager()); |
Chris Lattner | 7f02f72 | 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 | 59138ba | 2008-04-20 00:45:53 +0000 | [diff] [blame] | 107 | return llvm::ConstantFP::get(E->getValue()); |
Chris Lattner | 7f02f72 | 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 | e7579b5 | 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 | } |
Argyrios Kyrtzidis | 7267f78 | 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 | 3f70456 | 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 | 7f02f72 | 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 | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 123 | CGF.getContext().typesAreCompatible( |
| 124 | E->getArgType1(), E->getArgType2())); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 125 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 126 | Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 127 | Value *VisitAddrLabelExpr(const AddrLabelExpr *E) { |
Daniel Dunbar | 54d1909 | 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 | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 133 | } |
Chris Lattner | 7f02f72 | 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 | 9c3fc70 | 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 | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 150 | Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 151 | return EmitLoadOfLValue(E); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 152 | } |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 153 | Value *VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) { |
| 154 | return EmitLoadOfLValue(E); |
| 155 | } |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 156 | Value *VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 157 | return CGF.EmitObjCMessageExpr(E).getScalarVal(); |
Daniel Dunbar | 0a04d77 | 2008-08-23 10:51:21 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 160 | Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 161 | Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 162 | Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 163 | Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | be20bb5 | 2008-10-26 23:53:12 +0000 | [diff] [blame] | 164 | Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { |
| 165 | return EmitLoadOfLValue(E); |
| 166 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 167 | Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); } |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 168 | Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
| 169 | return EmitLValue(E).getAddress(); |
| 170 | } |
| 171 | |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 172 | Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); } |
Devang Patel | 35634f5 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 173 | |
| 174 | Value *VisitInitListExpr(InitListExpr *E) { |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 175 | unsigned NumInitElements = E->getNumInits(); |
| 176 | |
Douglas Gregor | a9c8780 | 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 | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 181 | const llvm::VectorType *VType = |
Anders Carlsson | f6884ac | 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 | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 187 | |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 188 | unsigned NumVectorElements = VType->getNumElements(); |
| 189 | const llvm::Type *ElementType = VType->getElementType(); |
Anders Carlsson | 7019a9e | 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 | 222d2c8 | 2007-12-18 02:45:33 +0000 | [diff] [blame] | 194 | // Emit initializers |
| 195 | unsigned i; |
| 196 | for (i = 0; i < NumInitElements; ++i) { |
Devang Patel | a83cc33 | 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 | 35634f5 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 200 | } |
Anders Carlsson | 7019a9e | 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 | a83cc33 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 209 | return V; |
Devang Patel | 35634f5 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 211 | |
Douglas Gregor | 3498bdb | 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 | 7f02f72 | 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 | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 222 | return CGF.EmitCallExpr(E).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 223 | } |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 225 | Value *VisitStmtExpr(const StmtExpr *E); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 226 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 227 | Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E); |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 7f02f72 | 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 | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 253 | Value *VisitUnaryReal (const UnaryOperator *E); |
| 254 | Value *VisitUnaryImag (const UnaryOperator *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 255 | Value *VisitUnaryExtension(const UnaryOperator *E) { |
| 256 | return Visit(E->getSubExpr()); |
| 257 | } |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 258 | Value *VisitUnaryOffsetOf(const UnaryOperator *E); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 259 | Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 260 | return Visit(DAE->getExpr()); |
| 261 | } |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 263 | // Binary Operators. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 264 | Value *EmitMul(const BinOpInfo &Ops) { |
| 265 | return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); |
| 266 | } |
| 267 | Value *EmitDiv(const BinOpInfo &Ops); |
| 268 | Value *EmitRem(const BinOpInfo &Ops); |
| 269 | Value *EmitAdd(const BinOpInfo &Ops); |
| 270 | Value *EmitSub(const BinOpInfo &Ops); |
| 271 | Value *EmitShl(const BinOpInfo &Ops); |
| 272 | Value *EmitShr(const BinOpInfo &Ops); |
| 273 | Value *EmitAnd(const BinOpInfo &Ops) { |
| 274 | return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); |
| 275 | } |
| 276 | Value *EmitXor(const BinOpInfo &Ops) { |
| 277 | return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); |
| 278 | } |
| 279 | Value *EmitOr (const BinOpInfo &Ops) { |
| 280 | return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); |
| 281 | } |
| 282 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 283 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 284 | Value *EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 285 | Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); |
| 286 | |
| 287 | // Binary operators and binary compound assignment operators. |
| 288 | #define HANDLEBINOP(OP) \ |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 289 | Value *VisitBin ## OP(const BinaryOperator *E) { \ |
| 290 | return Emit ## OP(EmitBinOps(E)); \ |
| 291 | } \ |
| 292 | Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ |
| 293 | return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 294 | } |
| 295 | HANDLEBINOP(Mul); |
| 296 | HANDLEBINOP(Div); |
| 297 | HANDLEBINOP(Rem); |
| 298 | HANDLEBINOP(Add); |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 299 | HANDLEBINOP(Sub); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 300 | HANDLEBINOP(Shl); |
| 301 | HANDLEBINOP(Shr); |
| 302 | HANDLEBINOP(And); |
| 303 | HANDLEBINOP(Xor); |
| 304 | HANDLEBINOP(Or); |
| 305 | #undef HANDLEBINOP |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 307 | // Comparisons. |
| 308 | Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, |
| 309 | unsigned SICmpOpc, unsigned FCmpOpc); |
| 310 | #define VISITCOMP(CODE, UI, SI, FP) \ |
| 311 | Value *VisitBin##CODE(const BinaryOperator *E) { \ |
| 312 | return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ |
| 313 | llvm::FCmpInst::FP); } |
| 314 | VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT); |
| 315 | VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT); |
| 316 | VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE); |
| 317 | VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE); |
| 318 | VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ); |
| 319 | VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE); |
| 320 | #undef VISITCOMP |
| 321 | |
| 322 | Value *VisitBinAssign (const BinaryOperator *E); |
| 323 | |
| 324 | Value *VisitBinLAnd (const BinaryOperator *E); |
| 325 | Value *VisitBinLOr (const BinaryOperator *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 326 | Value *VisitBinComma (const BinaryOperator *E); |
| 327 | |
| 328 | // Other Operators. |
Mike Stump | df6b68c | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 329 | Value *VisitBlockExpr(const BlockExpr *BE); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 330 | Value *VisitConditionalOperator(const ConditionalOperator *CO); |
| 331 | Value *VisitChooseExpr(ChooseExpr *CE); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 332 | Value *VisitVAArgExpr(VAArgExpr *VE); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 333 | Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 334 | return CGF.EmitObjCStringLiteral(E); |
| 335 | } |
| 336 | }; |
| 337 | } // end anonymous namespace. |
| 338 | |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | // Utilities |
| 341 | //===----------------------------------------------------------------------===// |
| 342 | |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 343 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 3420d0d | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 344 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 345 | Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { |
| 346 | assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs"); |
| 347 | |
| 348 | if (SrcType->isRealFloatingType()) { |
| 349 | // Compare against 0.0 for fp scalars. |
| 350 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 351 | return Builder.CreateFCmpUNE(Src, Zero, "tobool"); |
| 352 | } |
| 353 | |
Daniel Dunbar | d1d66bc | 2008-08-25 10:38:11 +0000 | [diff] [blame] | 354 | assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) && |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 355 | "Unknown scalar type to convert"); |
| 356 | |
| 357 | // Because of the type rules of C, we often end up computing a logical value, |
| 358 | // then zero extending it to int, then wanting it as a logical value again. |
| 359 | // Optimize this common case. |
| 360 | if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) { |
| 361 | if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) { |
| 362 | Value *Result = ZI->getOperand(0); |
Eli Friedman | 356916e | 2008-01-29 18:13:51 +0000 | [diff] [blame] | 363 | // If there aren't any more uses, zap the instruction to save space. |
| 364 | // Note that there can be more uses, for example if this |
| 365 | // is the result of an assignment. |
| 366 | if (ZI->use_empty()) |
| 367 | ZI->eraseFromParent(); |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 368 | return Result; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Compare against an integer or pointer null. |
| 373 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
| 374 | return Builder.CreateICmpNE(Src, Zero, "tobool"); |
| 375 | } |
| 376 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 377 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 378 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 379 | Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, |
| 380 | QualType DstType) { |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 381 | SrcType = CGF.getContext().getCanonicalType(SrcType); |
| 382 | DstType = CGF.getContext().getCanonicalType(DstType); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 383 | if (SrcType == DstType) return Src; |
Chris Lattner | cf28908 | 2007-08-26 07:21:11 +0000 | [diff] [blame] | 384 | |
| 385 | if (DstType->isVoidType()) return 0; |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 386 | |
| 387 | // Handle conversions to bool first, they are special: comparisons against 0. |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 388 | if (DstType->isBooleanType()) |
| 389 | return EmitConversionToBool(Src, SrcType); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 390 | |
| 391 | const llvm::Type *DstTy = ConvertType(DstType); |
| 392 | |
| 393 | // Ignore conversions like int -> uint. |
| 394 | if (Src->getType() == DstTy) |
| 395 | return Src; |
| 396 | |
Daniel Dunbar | 270cc66 | 2008-08-25 09:51:32 +0000 | [diff] [blame] | 397 | // Handle pointer conversions next: pointers can only be converted |
| 398 | // to/from other pointers and integers. Check for pointer types in |
| 399 | // terms of LLVM, as some native types (like Obj-C id) may map to a |
| 400 | // pointer type. |
| 401 | if (isa<llvm::PointerType>(DstTy)) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 402 | // The source value may be an integer, or a pointer. |
| 403 | if (isa<llvm::PointerType>(Src->getType())) |
| 404 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
| 405 | assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
Eli Friedman | 2561542 | 2009-03-04 04:02:35 +0000 | [diff] [blame] | 406 | // First, convert to the correct width so that we control the kind of |
| 407 | // extension. |
| 408 | const llvm::Type *MiddleTy = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 409 | bool InputSigned = SrcType->isSignedIntegerType(); |
| 410 | llvm::Value* IntResult = |
| 411 | Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); |
| 412 | // Then, cast to pointer. |
| 413 | return Builder.CreateIntToPtr(IntResult, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Daniel Dunbar | 270cc66 | 2008-08-25 09:51:32 +0000 | [diff] [blame] | 416 | if (isa<llvm::PointerType>(Src->getType())) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 417 | // Must be an ptr to int cast. |
| 418 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
Anders Carlsson | 50b5a30 | 2007-10-31 23:18:02 +0000 | [diff] [blame] | 419 | return Builder.CreatePtrToInt(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 422 | // A scalar can be splatted to an extended vector of the same element type |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 423 | if (DstType->isExtVectorType() && !isa<VectorType>(SrcType)) { |
| 424 | // Cast the scalar to element type |
| 425 | QualType EltTy = DstType->getAsExtVectorType()->getElementType(); |
| 426 | llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy); |
| 427 | |
| 428 | // Insert the element in element zero of an undef vector |
| 429 | llvm::Value *UnV = llvm::UndefValue::get(DstTy); |
| 430 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 431 | UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp"); |
| 432 | |
| 433 | // Splat the element across to all elements |
| 434 | llvm::SmallVector<llvm::Constant*, 16> Args; |
| 435 | unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); |
| 436 | for (unsigned i = 0; i < NumElements; i++) |
| 437 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
| 438 | |
| 439 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
| 440 | llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); |
| 441 | return Yay; |
| 442 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 3b1ae00 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 444 | // Allow bitcast from vector to integer/fp of the same size. |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 445 | if (isa<llvm::VectorType>(Src->getType()) || |
Chris Lattner | 3b1ae00 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 446 | isa<llvm::VectorType>(DstTy)) |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 447 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 448 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 449 | // Finally, we have the arithmetic types: real int/float. |
| 450 | if (isa<llvm::IntegerType>(Src->getType())) { |
| 451 | bool InputSigned = SrcType->isSignedIntegerType(); |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 452 | if (isa<llvm::IntegerType>(DstTy)) |
| 453 | return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); |
| 454 | else if (InputSigned) |
| 455 | return Builder.CreateSIToFP(Src, DstTy, "conv"); |
| 456 | else |
| 457 | return Builder.CreateUIToFP(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 461 | if (isa<llvm::IntegerType>(DstTy)) { |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 462 | if (DstType->isSignedIntegerType()) |
| 463 | return Builder.CreateFPToSI(Src, DstTy, "conv"); |
| 464 | else |
| 465 | return Builder.CreateFPToUI(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | assert(DstTy->isFloatingPoint() && "Unknown real conversion"); |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 469 | if (DstTy->getTypeID() < Src->getType()->getTypeID()) |
| 470 | return Builder.CreateFPTrunc(Src, DstTy, "conv"); |
| 471 | else |
| 472 | return Builder.CreateFPExt(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 475 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 476 | /// complex type to the specified destination type, where the destination |
| 477 | /// type is an LLVM scalar type. |
| 478 | Value *ScalarExprEmitter:: |
| 479 | EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 480 | QualType SrcTy, QualType DstTy) { |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 481 | // Get the source element type. |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 482 | SrcTy = SrcTy->getAsComplexType()->getElementType(); |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 483 | |
| 484 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 485 | if (DstTy->isBooleanType()) { |
| 486 | // Complex != 0 -> (Real != 0) | (Imag != 0) |
| 487 | Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 488 | Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); |
| 489 | return Builder.CreateOr(Src.first, Src.second, "tobool"); |
| 490 | } |
| 491 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 492 | // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, |
| 493 | // the imaginary part of the complex value is discarded and the value of the |
| 494 | // real part is converted according to the conversion rules for the |
| 495 | // corresponding real type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 496 | return EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 497 | } |
| 498 | |
| 499 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 500 | //===----------------------------------------------------------------------===// |
| 501 | // Visitor Methods |
| 502 | //===----------------------------------------------------------------------===// |
| 503 | |
| 504 | Value *ScalarExprEmitter::VisitExpr(Expr *E) { |
Daniel Dunbar | 488e993 | 2008-08-16 00:56:44 +0000 | [diff] [blame] | 505 | CGF.ErrorUnsupported(E, "scalar expression"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 506 | if (E->getType()->isVoidType()) |
| 507 | return 0; |
| 508 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
| 509 | } |
| 510 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 511 | Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
| 512 | llvm::SmallVector<llvm::Constant*, 32> indices; |
| 513 | for (unsigned i = 2; i < E->getNumSubExprs(); i++) { |
| 514 | indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i)))); |
| 515 | } |
| 516 | Value* V1 = CGF.EmitScalarExpr(E->getExpr(0)); |
| 517 | Value* V2 = CGF.EmitScalarExpr(E->getExpr(1)); |
| 518 | Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size()); |
| 519 | return Builder.CreateShuffleVector(V1, V2, SV, "shuffle"); |
| 520 | } |
| 521 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 522 | Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 523 | // Emit subscript expressions in rvalue context's. For most cases, this just |
| 524 | // loads the lvalue formed by the subscript expr. However, we have to be |
| 525 | // careful, because the base of a vector subscript is occasionally an rvalue, |
| 526 | // so we can't get it as an lvalue. |
| 527 | if (!E->getBase()->getType()->isVectorType()) |
| 528 | return EmitLoadOfLValue(E); |
| 529 | |
| 530 | // Handle the vector case. The base must be a vector, the index must be an |
| 531 | // integer value. |
| 532 | Value *Base = Visit(E->getBase()); |
| 533 | Value *Idx = Visit(E->getIdx()); |
| 534 | |
| 535 | // FIXME: Convert Idx to i32 type. |
| 536 | return Builder.CreateExtractElement(Base, Idx, "vecext"); |
| 537 | } |
| 538 | |
| 539 | /// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but |
| 540 | /// also handle things like function to pointer-to-function decay, and array to |
| 541 | /// pointer decay. |
| 542 | Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) { |
| 543 | const Expr *Op = E->getSubExpr(); |
| 544 | |
| 545 | // If this is due to array->pointer conversion, emit the array expression as |
| 546 | // an l-value. |
| 547 | if (Op->getType()->isArrayType()) { |
| 548 | // FIXME: For now we assume that all source arrays map to LLVM arrays. This |
| 549 | // will not true when we add support for VLAs. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 550 | Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays. |
Eli Friedman | 8f39f5e | 2008-12-20 23:11:59 +0000 | [diff] [blame] | 551 | |
| 552 | if (!Op->getType()->isVariableArrayType()) { |
| 553 | assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer"); |
| 554 | assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 555 | ->getElementType()) && |
| 556 | "Expected pointer to array"); |
| 557 | V = Builder.CreateStructGEP(V, 0, "arraydecay"); |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 558 | } |
Chris Lattner | a9e6372 | 2007-12-12 04:13:20 +0000 | [diff] [blame] | 559 | |
| 560 | // The resultant pointer type can be implicitly casted to other pointer |
Chris Lattner | f31627f | 2008-07-23 06:31:27 +0000 | [diff] [blame] | 561 | // types as well (e.g. void*) and can be implicitly converted to integer. |
| 562 | const llvm::Type *DestTy = ConvertType(E->getType()); |
| 563 | if (V->getType() != DestTy) { |
| 564 | if (isa<llvm::PointerType>(DestTy)) |
| 565 | V = Builder.CreateBitCast(V, DestTy, "ptrconv"); |
| 566 | else { |
| 567 | assert(isa<llvm::IntegerType>(DestTy) && "Unknown array decay"); |
| 568 | V = Builder.CreatePtrToInt(V, DestTy, "ptrconv"); |
| 569 | } |
| 570 | } |
Chris Lattner | a9e6372 | 2007-12-12 04:13:20 +0000 | [diff] [blame] | 571 | return V; |
| 572 | |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 573 | } else if (E->getType()->isReferenceType()) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 574 | // FIXME: An expression cannot have reference type. |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 575 | return EmitLValue(Op).getAddress(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | return EmitCastExpr(Op, E->getType()); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts |
| 583 | // have to handle a more broad range of conversions than explicit casts, as they |
| 584 | // handle things like function to ptr-to-function decay etc. |
| 585 | Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) { |
Chris Lattner | 58a2e94 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 586 | // Handle cases where the source is an non-complex type. |
Chris Lattner | 19a1d7c | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 587 | |
| 588 | if (!CGF.hasAggregateLLVMType(E->getType())) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 589 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 590 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 591 | // Use EmitScalarConversion to perform the conversion. |
| 592 | return EmitScalarConversion(Src, E->getType(), DestTy); |
| 593 | } |
Chris Lattner | 19a1d7c | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 595 | if (E->getType()->isAnyComplexType()) { |
Chris Lattner | 19a1d7c | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 596 | // Handle cases where the source is a complex type. |
| 597 | return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(), |
| 598 | DestTy); |
| 599 | } |
Chris Lattner | 10b00cf | 2007-08-26 07:16:41 +0000 | [diff] [blame] | 600 | |
Chris Lattner | 19a1d7c | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 601 | // Okay, this is a cast from an aggregate. It must be a cast to void. Just |
| 602 | // evaluate the result and return. |
| 603 | CGF.EmitAggExpr(E, 0, false); |
| 604 | return 0; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 607 | Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
Chris Lattner | 91d723d | 2008-07-26 20:23:23 +0000 | [diff] [blame] | 608 | return CGF.EmitCompoundStmt(*E->getSubStmt(), |
| 609 | !E->getType()->isVoidType()).getScalarVal(); |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Mike Stump | a99038c | 2009-02-28 09:07:16 +0000 | [diff] [blame] | 612 | Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { |
| 613 | return Builder.CreateLoad(CGF.GetAddrOfBlockDecl(E), false, "tmp"); |
Mike Stump | 4e7a1f7 | 2009-02-21 20:00:35 +0000 | [diff] [blame] | 614 | } |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 616 | //===----------------------------------------------------------------------===// |
| 617 | // Unary Operators |
| 618 | //===----------------------------------------------------------------------===// |
| 619 | |
| 620 | Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | dfce2a5 | 2007-08-24 16:24:49 +0000 | [diff] [blame] | 621 | bool isInc, bool isPre) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 622 | LValue LV = EmitLValue(E->getSubExpr()); |
Eli Friedman | f52bbeb | 2009-03-23 03:00:06 +0000 | [diff] [blame^] | 623 | QualType ValTy = E->getSubExpr()->getType(); |
| 624 | Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 625 | |
| 626 | int AmountVal = isInc ? 1 : -1; |
| 627 | |
| 628 | Value *NextVal; |
Chris Lattner | 8cc9d08 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 629 | if (const llvm::PointerType *PT = |
| 630 | dyn_cast<llvm::PointerType>(InVal->getType())) { |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 631 | // FIXME: This isn't right for VLAs. |
Chris Lattner | 8cc9d08 | 2009-03-18 04:25:13 +0000 | [diff] [blame] | 632 | llvm::Constant *Inc =llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal); |
| 633 | if (!isa<llvm::FunctionType>(PT->getElementType())) { |
| 634 | NextVal = Builder.CreateGEP(InVal, Inc, "ptrincdec"); |
| 635 | } else { |
| 636 | const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 637 | NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp"); |
| 638 | NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec"); |
| 639 | NextVal = Builder.CreateBitCast(NextVal, InVal->getType()); |
| 640 | } |
Chris Lattner | db3bd4b | 2009-02-11 07:40:06 +0000 | [diff] [blame] | 641 | } else if (InVal->getType() == llvm::Type::Int1Ty && isInc) { |
| 642 | // Bool++ is an interesting case, due to promotion rules, we get: |
| 643 | // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 -> |
| 644 | // Bool = ((int)Bool+1) != 0 |
| 645 | // An interesting aspect of this is that increment is always true. |
| 646 | // Decrement does not have this property. |
| 647 | NextVal = llvm::ConstantInt::getTrue(); |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 648 | } else { |
| 649 | // Add the inc/dec to the real part. |
| 650 | if (isa<llvm::IntegerType>(InVal->getType())) |
| 651 | NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 652 | else if (InVal->getType() == llvm::Type::FloatTy) |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 653 | NextVal = |
Chris Lattner | 59138ba | 2008-04-20 00:45:53 +0000 | [diff] [blame] | 654 | llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal))); |
Chris Lattner | 25ddea7 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 655 | else if (InVal->getType() == llvm::Type::DoubleTy) |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 656 | NextVal = |
Chris Lattner | 59138ba | 2008-04-20 00:45:53 +0000 | [diff] [blame] | 657 | llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal))); |
Chris Lattner | 25ddea7 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 658 | else { |
| 659 | llvm::APFloat F(static_cast<float>(AmountVal)); |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 660 | bool ignored; |
| 661 | F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero, |
| 662 | &ignored); |
Chris Lattner | 25ddea7 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 663 | NextVal = llvm::ConstantFP::get(F); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 664 | } |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 665 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 666 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 667 | |
| 668 | // Store the updated result through the lvalue. |
Eli Friedman | f52bbeb | 2009-03-23 03:00:06 +0000 | [diff] [blame^] | 669 | if (LV.isBitfield()) |
| 670 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, |
| 671 | &NextVal); |
| 672 | else |
| 673 | CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 674 | |
| 675 | // If this is a postinc, return the value read from memory, otherwise use the |
| 676 | // updated value. |
| 677 | return isPre ? NextVal : InVal; |
| 678 | } |
| 679 | |
| 680 | |
| 681 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 682 | Value *Op = Visit(E->getSubExpr()); |
| 683 | return Builder.CreateNeg(Op, "neg"); |
| 684 | } |
| 685 | |
| 686 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 687 | Value *Op = Visit(E->getSubExpr()); |
| 688 | return Builder.CreateNot(Op, "neg"); |
| 689 | } |
| 690 | |
| 691 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 692 | // Compare operand to zero. |
| 693 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
| 694 | |
| 695 | // Invert value. |
| 696 | // TODO: Could dynamically modify easy computations here. For example, if |
| 697 | // the operand is an icmp ne, turn into icmp eq. |
| 698 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
| 699 | |
| 700 | // ZExt result to int. |
| 701 | return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext"); |
| 702 | } |
| 703 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 704 | /// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of |
| 705 | /// argument of the sizeof expression as an integer. |
| 706 | Value * |
| 707 | ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 708 | QualType TypeToSize = E->getTypeOfArgument(); |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 709 | if (E->isSizeOf()) { |
| 710 | if (const VariableArrayType *VAT = |
| 711 | CGF.getContext().getAsVariableArrayType(TypeToSize)) { |
| 712 | if (E->isArgumentType()) { |
| 713 | // sizeof(type) - make sure to emit the VLA size. |
| 714 | CGF.EmitVLASize(TypeToSize); |
| 715 | } |
Anders Carlsson | 6cd586d | 2009-01-30 16:41:04 +0000 | [diff] [blame] | 716 | |
Anders Carlsson | 96f2147 | 2009-02-05 19:43:10 +0000 | [diff] [blame] | 717 | return CGF.GetVLASize(VAT); |
Anders Carlsson | b50525b | 2008-12-21 03:33:21 +0000 | [diff] [blame] | 718 | } |
Anders Carlsson | 5d46315 | 2008-12-12 07:38:43 +0000 | [diff] [blame] | 719 | } |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 720 | |
| 721 | // If this isn't sizeof(vla), the result must be constant; use the |
| 722 | // constant folding logic so we don't have to duplicate it here. |
| 723 | Expr::EvalResult Result; |
| 724 | E->Evaluate(Result, CGF.getContext()); |
| 725 | return llvm::ConstantInt::get(Result.Val.getInt()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 728 | Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { |
| 729 | Expr *Op = E->getSubExpr(); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 730 | if (Op->getType()->isAnyComplexType()) |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 731 | return CGF.EmitComplexExpr(Op).first; |
| 732 | return Visit(Op); |
| 733 | } |
| 734 | Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { |
| 735 | Expr *Op = E->getSubExpr(); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 736 | if (Op->getType()->isAnyComplexType()) |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 737 | return CGF.EmitComplexExpr(Op).second; |
Chris Lattner | 36f8406 | 2007-08-26 05:29:21 +0000 | [diff] [blame] | 738 | |
| 739 | // __imag on a scalar returns zero. Emit it the subexpr to ensure side |
| 740 | // effects are evaluated. |
| 741 | CGF.EmitScalarExpr(Op); |
| 742 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 745 | Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E) |
| 746 | { |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 747 | Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress(); |
Eli Friedman | 769e411 | 2009-01-24 22:38:55 +0000 | [diff] [blame] | 748 | const llvm::Type* ResultType = ConvertType(E->getType()); |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 749 | return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof"); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 750 | } |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 751 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 752 | //===----------------------------------------------------------------------===// |
| 753 | // Binary Operators |
| 754 | //===----------------------------------------------------------------------===// |
| 755 | |
| 756 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
| 757 | BinOpInfo Result; |
| 758 | Result.LHS = Visit(E->getLHS()); |
| 759 | Result.RHS = Visit(E->getRHS()); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 760 | Result.Ty = E->getType(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 761 | Result.E = E; |
| 762 | return Result; |
| 763 | } |
| 764 | |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 765 | Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 766 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { |
| 767 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
| 768 | |
| 769 | BinOpInfo OpInfo; |
| 770 | |
| 771 | // Load the LHS and RHS operands. |
| 772 | LValue LHSLV = EmitLValue(E->getLHS()); |
| 773 | OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); |
Chris Lattner | 04dc764 | 2007-08-26 22:37:40 +0000 | [diff] [blame] | 774 | |
| 775 | // Determine the computation type. If the RHS is complex, then this is one of |
| 776 | // the add/sub/mul/div operators. All of these operators can be computed in |
| 777 | // with just their real component even though the computation domain really is |
| 778 | // complex. |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 779 | QualType ComputeType = E->getComputationType(); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 780 | |
Chris Lattner | 04dc764 | 2007-08-26 22:37:40 +0000 | [diff] [blame] | 781 | // If the computation type is complex, then the RHS is complex. Emit the RHS. |
| 782 | if (const ComplexType *CT = ComputeType->getAsComplexType()) { |
| 783 | ComputeType = CT->getElementType(); |
| 784 | |
| 785 | // Emit the RHS, only keeping the real component. |
| 786 | OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first; |
| 787 | RHSTy = RHSTy->getAsComplexType()->getElementType(); |
| 788 | } else { |
| 789 | // Otherwise the RHS is a simple scalar value. |
| 790 | OpInfo.RHS = Visit(E->getRHS()); |
| 791 | } |
| 792 | |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 793 | QualType LComputeTy, RComputeTy, ResultTy; |
| 794 | |
| 795 | // Compound assignment does not contain enough information about all |
| 796 | // the types involved for pointer arithmetic cases. Figure it out |
| 797 | // here for now. |
| 798 | if (E->getLHS()->getType()->isPointerType()) { |
| 799 | // Pointer arithmetic cases: ptr +=,-= int and ptr -= ptr, |
| 800 | assert((E->getOpcode() == BinaryOperator::AddAssign || |
| 801 | E->getOpcode() == BinaryOperator::SubAssign) && |
| 802 | "Invalid compound assignment operator on pointer type."); |
| 803 | LComputeTy = E->getLHS()->getType(); |
| 804 | |
| 805 | if (E->getRHS()->getType()->isPointerType()) { |
| 806 | // Degenerate case of (ptr -= ptr) allowed by GCC implicit cast |
| 807 | // extension, the conversion from the pointer difference back to |
| 808 | // the LHS type is handled at the end. |
| 809 | assert(E->getOpcode() == BinaryOperator::SubAssign && |
| 810 | "Invalid compound assignment operator on pointer type."); |
| 811 | RComputeTy = E->getLHS()->getType(); |
| 812 | ResultTy = CGF.getContext().getPointerDiffType(); |
| 813 | } else { |
| 814 | RComputeTy = E->getRHS()->getType(); |
| 815 | ResultTy = LComputeTy; |
| 816 | } |
| 817 | } else if (E->getRHS()->getType()->isPointerType()) { |
| 818 | // Degenerate case of (int += ptr) allowed by GCC implicit cast |
| 819 | // extension. |
| 820 | assert(E->getOpcode() == BinaryOperator::AddAssign && |
| 821 | "Invalid compound assignment operator on pointer type."); |
| 822 | LComputeTy = E->getLHS()->getType(); |
| 823 | RComputeTy = E->getRHS()->getType(); |
| 824 | ResultTy = RComputeTy; |
| 825 | } else { |
| 826 | LComputeTy = RComputeTy = ResultTy = ComputeType; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 827 | } |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 828 | |
| 829 | // Convert the LHS/RHS values to the computation type. |
| 830 | OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, LComputeTy); |
| 831 | OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, RComputeTy); |
| 832 | OpInfo.Ty = ResultTy; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 833 | OpInfo.E = E; |
| 834 | |
| 835 | // Expand the binary operator. |
| 836 | Value *Result = (this->*Func)(OpInfo); |
| 837 | |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 838 | // Convert the result back to the LHS type. |
| 839 | Result = EmitScalarConversion(Result, ResultTy, LHSTy); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 840 | |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 841 | // Store the result value into the LHS lvalue. Bit-fields are |
Daniel Dunbar | 371d16f | 2008-11-19 11:54:05 +0000 | [diff] [blame] | 842 | // handled specially because the result is altered by the store, |
| 843 | // i.e., [C99 6.5.16p1] 'An assignment expression has the value of |
| 844 | // the left operand after the assignment...'. |
Eli Friedman | 1849128 | 2008-05-25 14:13:57 +0000 | [diff] [blame] | 845 | if (LHSLV.isBitfield()) |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 846 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy, |
| 847 | &Result); |
| 848 | else |
| 849 | CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy); |
| 850 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 851 | return Result; |
| 852 | } |
| 853 | |
| 854 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 855 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
Nate Begeman | b3ab8dc | 2007-12-30 01:28:16 +0000 | [diff] [blame] | 856 | if (Ops.LHS->getType()->isFPOrFPVector()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 857 | return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 858 | else if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 859 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 860 | else |
| 861 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 862 | } |
| 863 | |
| 864 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 865 | // Rem in C can't be a floating point type: C99 6.5.5p2. |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 866 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 867 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 868 | else |
| 869 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 870 | } |
| 871 | |
| 872 | |
| 873 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 874 | if (!Ops.Ty->isPointerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 875 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 876 | |
| 877 | // FIXME: What about a pointer to a VLA? |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 878 | Value *Ptr, *Idx; |
| 879 | Expr *IdxExp; |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 880 | const PointerType *PT; |
| 881 | if ((PT = Ops.E->getLHS()->getType()->getAsPointerType())) { |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 882 | Ptr = Ops.LHS; |
| 883 | Idx = Ops.RHS; |
| 884 | IdxExp = Ops.E->getRHS(); |
| 885 | } else { // int + pointer |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 886 | PT = Ops.E->getRHS()->getType()->getAsPointerType(); |
| 887 | assert(PT && "Invalid add expr"); |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 888 | Ptr = Ops.RHS; |
| 889 | Idx = Ops.LHS; |
| 890 | IdxExp = Ops.E->getLHS(); |
| 891 | } |
| 892 | |
| 893 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 894 | if (Width < CGF.LLVMPointerWidth) { |
| 895 | // Zero or sign extend the pointer value based on whether the index is |
| 896 | // signed or not. |
| 897 | const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 898 | if (IdxExp->getType()->isSignedIntegerType()) |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 899 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 900 | else |
| 901 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 902 | } |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 903 | |
| 904 | // Explicitly handle GNU void* and function pointer arithmetic |
| 905 | // extensions. The GNU void* casts amount to no-ops since our void* |
| 906 | // type is i8*, but this is future proof. |
| 907 | const QualType ElementType = PT->getPointeeType(); |
| 908 | if (ElementType->isVoidType() || ElementType->isFunctionType()) { |
| 909 | const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 910 | Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); |
| 911 | Value *Res = Builder.CreateGEP(Casted, Idx, "sub.ptr"); |
| 912 | return Builder.CreateBitCast(Res, Ptr->getType()); |
| 913 | } |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 914 | |
| 915 | return Builder.CreateGEP(Ptr, Idx, "add.ptr"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { |
| 919 | if (!isa<llvm::PointerType>(Ops.LHS->getType())) |
| 920 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 921 | |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 922 | const QualType LHSType = Ops.E->getLHS()->getType(); |
| 923 | const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType(); |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 924 | if (!isa<llvm::PointerType>(Ops.RHS->getType())) { |
| 925 | // pointer - int |
| 926 | Value *Idx = Ops.RHS; |
| 927 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 928 | if (Width < CGF.LLVMPointerWidth) { |
| 929 | // Zero or sign extend the pointer value based on whether the index is |
| 930 | // signed or not. |
| 931 | const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 932 | if (Ops.E->getRHS()->getType()->isSignedIntegerType()) |
| 933 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 934 | else |
| 935 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 936 | } |
| 937 | Idx = Builder.CreateNeg(Idx, "sub.ptr.neg"); |
| 938 | |
| 939 | // FIXME: The pointer could point to a VLA. |
Daniel Dunbar | b09fae7 | 2009-01-23 18:51:09 +0000 | [diff] [blame] | 940 | |
| 941 | // Explicitly handle GNU void* and function pointer arithmetic |
| 942 | // extensions. The GNU void* casts amount to no-ops since our |
| 943 | // void* type is i8*, but this is future proof. |
| 944 | if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { |
| 945 | const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 946 | Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); |
| 947 | Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr"); |
| 948 | return Builder.CreateBitCast(Res, Ops.LHS->getType()); |
| 949 | } |
| 950 | |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 951 | return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr"); |
Daniel Dunbar | 820b033 | 2008-08-05 00:47:03 +0000 | [diff] [blame] | 952 | } else { |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 953 | // pointer - pointer |
| 954 | Value *LHS = Ops.LHS; |
| 955 | Value *RHS = Ops.RHS; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 956 | |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 957 | uint64_t ElementSize; |
Daniel Dunbar | 820b033 | 2008-08-05 00:47:03 +0000 | [diff] [blame] | 958 | |
Chris Lattner | e5ed151 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 959 | // Handle GCC extension for pointer arithmetic on void* and function pointer |
| 960 | // types. |
| 961 | if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 962 | ElementSize = 1; |
| 963 | } else { |
| 964 | ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8; |
| 965 | } |
| 966 | |
| 967 | const llvm::Type *ResultType = ConvertType(Ops.Ty); |
| 968 | LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); |
| 969 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 970 | Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); |
| 971 | |
Chris Lattner | e5ed151 | 2009-02-11 07:21:43 +0000 | [diff] [blame] | 972 | // Optimize out the shift for element size of 1. |
| 973 | if (ElementSize == 1) |
| 974 | return BytesBetween; |
| 975 | |
Daniel Dunbar | 8c6f57c | 2008-08-06 02:00:38 +0000 | [diff] [blame] | 976 | // HACK: LLVM doesn't have an divide instruction that 'knows' there is no |
| 977 | // remainder. As such, we handle common power-of-two cases here to generate |
| 978 | // better code. See PR2247. |
| 979 | if (llvm::isPowerOf2_64(ElementSize)) { |
| 980 | Value *ShAmt = |
| 981 | llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize)); |
| 982 | return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr"); |
| 983 | } |
| 984 | |
| 985 | // Otherwise, do a full sdiv. |
| 986 | Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize); |
| 987 | return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 988 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 992 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 993 | // RHS to the same size as the LHS. |
| 994 | Value *RHS = Ops.RHS; |
| 995 | if (Ops.LHS->getType() != RHS->getType()) |
| 996 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 997 | |
| 998 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 999 | } |
| 1000 | |
| 1001 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 1002 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 1003 | // RHS to the same size as the LHS. |
| 1004 | Value *RHS = Ops.RHS; |
| 1005 | if (Ops.LHS->getType() != RHS->getType()) |
| 1006 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 1007 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 1008 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1009 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 1010 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 1011 | } |
| 1012 | |
| 1013 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 1014 | unsigned SICmpOpc, unsigned FCmpOpc) { |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1015 | Value *Result; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1016 | QualType LHSTy = E->getLHS()->getType(); |
Nate Begeman | 7a66d7b | 2008-07-25 20:16:05 +0000 | [diff] [blame] | 1017 | if (!LHSTy->isAnyComplexType() && !LHSTy->isVectorType()) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1018 | Value *LHS = Visit(E->getLHS()); |
| 1019 | Value *RHS = Visit(E->getRHS()); |
| 1020 | |
| 1021 | if (LHS->getType()->isFloatingPoint()) { |
Nate Begeman | 7a66d7b | 2008-07-25 20:16:05 +0000 | [diff] [blame] | 1022 | Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1023 | LHS, RHS, "cmp"); |
Eli Friedman | ec2c126 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 1024 | } else if (LHSTy->isSignedIntegerType()) { |
| 1025 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1026 | LHS, RHS, "cmp"); |
| 1027 | } else { |
Eli Friedman | ec2c126 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 1028 | // Unsigned integers and pointers. |
| 1029 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1030 | LHS, RHS, "cmp"); |
| 1031 | } |
Nate Begeman | 7a66d7b | 2008-07-25 20:16:05 +0000 | [diff] [blame] | 1032 | } else if (LHSTy->isVectorType()) { |
| 1033 | Value *LHS = Visit(E->getLHS()); |
| 1034 | Value *RHS = Visit(E->getRHS()); |
| 1035 | |
| 1036 | if (LHS->getType()->isFPOrFPVector()) { |
| 1037 | Result = Builder.CreateVFCmp((llvm::CmpInst::Predicate)FCmpOpc, |
| 1038 | LHS, RHS, "cmp"); |
| 1039 | } else if (LHSTy->isUnsignedIntegerType()) { |
| 1040 | Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)UICmpOpc, |
| 1041 | LHS, RHS, "cmp"); |
| 1042 | } else { |
| 1043 | // Signed integers and pointers. |
| 1044 | Result = Builder.CreateVICmp((llvm::CmpInst::Predicate)SICmpOpc, |
| 1045 | LHS, RHS, "cmp"); |
| 1046 | } |
| 1047 | return Result; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1048 | } else { |
| 1049 | // Complex Comparison: can only be an equality comparison. |
| 1050 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 1051 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
| 1052 | |
Chris Lattner | 9619662 | 2008-07-26 22:37:01 +0000 | [diff] [blame] | 1053 | QualType CETy = LHSTy->getAsComplexType()->getElementType(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1055 | Value *ResultR, *ResultI; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1056 | if (CETy->isRealFloatingType()) { |
| 1057 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1058 | LHS.first, RHS.first, "cmp.r"); |
| 1059 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 1060 | LHS.second, RHS.second, "cmp.i"); |
| 1061 | } else { |
| 1062 | // Complex comparisons can only be equality comparisons. As such, signed |
| 1063 | // and unsigned opcodes are the same. |
| 1064 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 1065 | LHS.first, RHS.first, "cmp.r"); |
| 1066 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 1067 | LHS.second, RHS.second, "cmp.i"); |
| 1068 | } |
| 1069 | |
| 1070 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 1071 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 1072 | } else { |
| 1073 | assert(E->getOpcode() == BinaryOperator::NE && |
| 1074 | "Complex comparison other than == or != ?"); |
| 1075 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 1076 | } |
| 1077 | } |
Nuno Lopes | 32f6209 | 2009-01-11 23:22:37 +0000 | [diff] [blame] | 1078 | |
| 1079 | return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 1083 | LValue LHS = EmitLValue(E->getLHS()); |
| 1084 | Value *RHS = Visit(E->getRHS()); |
| 1085 | |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1086 | // Store the value into the LHS. Bit-fields are handled specially |
Daniel Dunbar | 371d16f | 2008-11-19 11:54:05 +0000 | [diff] [blame] | 1087 | // because the result is altered by the store, i.e., [C99 6.5.16p1] |
| 1088 | // 'An assignment expression has the value of the left operand after |
| 1089 | // the assignment...'. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1090 | // FIXME: Volatility! |
Eli Friedman | 1849128 | 2008-05-25 14:13:57 +0000 | [diff] [blame] | 1091 | if (LHS.isBitfield()) |
Daniel Dunbar | ed3849b | 2008-11-19 09:36:46 +0000 | [diff] [blame] | 1092 | CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(), |
| 1093 | &RHS); |
| 1094 | else |
| 1095 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 1096 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1097 | // Return the RHS. |
| 1098 | return RHS; |
| 1099 | } |
| 1100 | |
| 1101 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1102 | // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. |
| 1103 | // If we have 1 && X, just emit X without inserting the control flow. |
| 1104 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { |
| 1105 | if (Cond == 1) { // If we have 1 && X, just emit X. |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1106 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 1107 | // ZExt result to int. |
| 1108 | return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext"); |
| 1109 | } |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1110 | |
| 1111 | // 0 && RHS: If it is safe, just elide the RHS, and return 0. |
| 1112 | if (!CGF.ContainsLabel(E->getRHS())) |
| 1113 | return llvm::Constant::getNullValue(CGF.LLVMIntTy); |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1116 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); |
| 1117 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1118 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1119 | // Branch on the LHS first. If it is false, go to the failure (cont) block. |
| 1120 | CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock); |
| 1121 | |
| 1122 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 1123 | // edges from this first condition. All of these values will be false. Start |
| 1124 | // setting up the PHI node in the Cont Block for this. |
| 1125 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::Int1Ty, "", ContBlock); |
| 1126 | PN->reserveOperandSpace(2); // Normal case, two inputs. |
| 1127 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 1128 | PI != PE; ++PI) |
| 1129 | PN->addIncoming(llvm::ConstantInt::getFalse(), *PI); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1130 | |
| 1131 | CGF.EmitBlock(RHSBlock); |
| 1132 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 1133 | |
| 1134 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1135 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1136 | |
| 1137 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 1138 | // into the phi node for the edge with the value of RHSCond. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1139 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1140 | PN->addIncoming(RHSCond, RHSBlock); |
| 1141 | |
| 1142 | // ZExt result to int. |
| 1143 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext"); |
| 1144 | } |
| 1145 | |
| 1146 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1147 | // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. |
| 1148 | // If we have 0 || X, just emit X without inserting the control flow. |
| 1149 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { |
| 1150 | if (Cond == -1) { // If we have 0 || X, just emit X. |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1151 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 1152 | // ZExt result to int. |
| 1153 | return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext"); |
| 1154 | } |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1155 | |
Eli Friedman | 8de8d1d | 2008-12-02 16:02:46 +0000 | [diff] [blame] | 1156 | // 1 || RHS: If it is safe, just elide the RHS, and return 1. |
Chris Lattner | 20eb09d | 2008-11-12 08:26:50 +0000 | [diff] [blame] | 1157 | if (!CGF.ContainsLabel(E->getRHS())) |
Eli Friedman | 8de8d1d | 2008-12-02 16:02:46 +0000 | [diff] [blame] | 1158 | return llvm::ConstantInt::get(CGF.LLVMIntTy, 1); |
Chris Lattner | 0946ccd | 2008-11-11 07:41:27 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1161 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); |
| 1162 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1164 | // Branch on the LHS first. If it is true, go to the success (cont) block. |
| 1165 | CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock); |
| 1166 | |
| 1167 | // Any edges into the ContBlock are now from an (indeterminate number of) |
| 1168 | // edges from this first condition. All of these values will be true. Start |
| 1169 | // setting up the PHI node in the Cont Block for this. |
| 1170 | llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::Int1Ty, "", ContBlock); |
| 1171 | PN->reserveOperandSpace(2); // Normal case, two inputs. |
| 1172 | for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); |
| 1173 | PI != PE; ++PI) |
| 1174 | PN->addIncoming(llvm::ConstantInt::getTrue(), *PI); |
| 1175 | |
| 1176 | // Emit the RHS condition as a bool value. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1177 | CGF.EmitBlock(RHSBlock); |
| 1178 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 1179 | |
| 1180 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1181 | RHSBlock = Builder.GetInsertBlock(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | f7b5ea9 | 2008-11-12 08:38:24 +0000 | [diff] [blame] | 1183 | // Emit an unconditional branch from this block to ContBlock. Insert an entry |
| 1184 | // into the phi node for the edge with the value of RHSCond. |
| 1185 | CGF.EmitBlock(ContBlock); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1186 | PN->addIncoming(RHSCond, RHSBlock); |
| 1187 | |
| 1188 | // ZExt result to int. |
| 1189 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext"); |
| 1190 | } |
| 1191 | |
| 1192 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 1193 | CGF.EmitStmt(E->getLHS()); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 1194 | CGF.EnsureInsertPoint(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1195 | return Visit(E->getRHS()); |
| 1196 | } |
| 1197 | |
| 1198 | //===----------------------------------------------------------------------===// |
| 1199 | // Other Operators |
| 1200 | //===----------------------------------------------------------------------===// |
| 1201 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1202 | /// isCheapEnoughToEvaluateUnconditionally - Return true if the specified |
| 1203 | /// expression is cheap enough and side-effect-free enough to evaluate |
| 1204 | /// unconditionally instead of conditionally. This is used to convert control |
| 1205 | /// flow into selects in some cases. |
| 1206 | static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) { |
| 1207 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 1208 | return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr()); |
| 1209 | |
| 1210 | // TODO: Allow anything we can constant fold to an integer or fp constant. |
| 1211 | if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) || |
| 1212 | isa<FloatingLiteral>(E)) |
| 1213 | return true; |
| 1214 | |
| 1215 | // Non-volatile automatic variables too, to get "cond ? X : Y" where |
| 1216 | // X and Y are local variables. |
| 1217 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 1218 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 1219 | if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified()) |
| 1220 | return true; |
| 1221 | |
| 1222 | return false; |
| 1223 | } |
| 1224 | |
| 1225 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1226 | Value *ScalarExprEmitter:: |
| 1227 | VisitConditionalOperator(const ConditionalOperator *E) { |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1228 | // If the condition constant folds and can be elided, try to avoid emitting |
| 1229 | // the condition and the dead arm. |
| 1230 | if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){ |
Chris Lattner | c657e92 | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1231 | Expr *Live = E->getLHS(), *Dead = E->getRHS(); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1232 | if (Cond == -1) |
Chris Lattner | c657e92 | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1233 | std::swap(Live, Dead); |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1234 | |
| 1235 | // If the dead side doesn't have labels we need, and if the Live side isn't |
| 1236 | // the gnu missing ?: extension (which we could handle, but don't bother |
| 1237 | // to), just emit the Live part. |
| 1238 | if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part |
| 1239 | Live) // Live part isn't missing. |
| 1240 | return Visit(Live); |
Chris Lattner | c657e92 | 2008-11-11 18:56:45 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1243 | |
| 1244 | // If this is a really simple expression (like x ? 4 : 5), emit this as a |
| 1245 | // select instead of as control flow. We can only do this if it is cheap and |
Chris Lattner | 531a550 | 2008-11-16 06:16:27 +0000 | [diff] [blame] | 1246 | // safe to evaluate the LHS and RHS unconditionally. |
Chris Lattner | 9802a51 | 2008-11-12 08:55:54 +0000 | [diff] [blame] | 1247 | if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) && |
| 1248 | isCheapEnoughToEvaluateUnconditionally(E->getRHS())) { |
| 1249 | llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond()); |
| 1250 | llvm::Value *LHS = Visit(E->getLHS()); |
| 1251 | llvm::Value *RHS = Visit(E->getRHS()); |
| 1252 | return Builder.CreateSelect(CondV, LHS, RHS, "cond"); |
| 1253 | } |
| 1254 | |
| 1255 | |
Daniel Dunbar | be65abc | 2008-11-12 10:13:37 +0000 | [diff] [blame] | 1256 | llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); |
| 1257 | llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); |
Daniel Dunbar | 9615ecb | 2008-11-13 01:38:36 +0000 | [diff] [blame] | 1258 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); |
Chris Lattner | 035cf42 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1259 | Value *CondVal = 0; |
Chris Lattner | 31a0984 | 2008-11-12 08:04:58 +0000 | [diff] [blame] | 1260 | |
Chris Lattner | 12d152f | 2009-02-13 23:35:32 +0000 | [diff] [blame] | 1261 | // If we don't have the GNU missing condition extension, emit a branch on |
| 1262 | // bool the normal way. |
| 1263 | if (E->getLHS()) { |
| 1264 | // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for |
| 1265 | // the branch on bool. |
| 1266 | CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); |
| 1267 | } else { |
| 1268 | // Otherwise, for the ?: extension, evaluate the conditional and then |
| 1269 | // convert it to bool the hard way. We do this explicitly because we need |
| 1270 | // the unconverted value for the missing middle value of the ?:. |
Chris Lattner | 035cf42 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1271 | CondVal = CGF.EmitScalarExpr(E->getCond()); |
Chris Lattner | 12d152f | 2009-02-13 23:35:32 +0000 | [diff] [blame] | 1272 | |
| 1273 | // In some cases, EmitScalarConversion will delete the "CondVal" expression |
| 1274 | // if there are no extra uses (an optimization). Inhibit this by making an |
| 1275 | // extra dead use, because we're going to add a use of CondVal later. We |
| 1276 | // don't use the builder for this, because we don't want it to get optimized |
| 1277 | // away. This leaves dead code, but the ?: extension isn't common. |
| 1278 | new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder", |
| 1279 | Builder.GetInsertBlock()); |
| 1280 | |
Chris Lattner | 035cf42 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1281 | Value *CondBoolVal = |
| 1282 | CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), |
| 1283 | CGF.getContext().BoolTy); |
| 1284 | Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); |
Chris Lattner | 035cf42 | 2008-11-12 08:08:13 +0000 | [diff] [blame] | 1285 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1286 | |
| 1287 | CGF.EmitBlock(LHSBlock); |
| 1288 | |
| 1289 | // Handle the GNU extension for missing LHS. |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1290 | Value *LHS; |
| 1291 | if (E->getLHS()) |
Eli Friedman | 856226c | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 1292 | LHS = Visit(E->getLHS()); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1293 | else // Perform promotions, to handle cases like "short ?: int" |
| 1294 | LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType()); |
| 1295 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1296 | LHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 1297 | CGF.EmitBranch(ContBlock); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1298 | |
| 1299 | CGF.EmitBlock(RHSBlock); |
| 1300 | |
Eli Friedman | 856226c | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 1301 | Value *RHS = Visit(E->getRHS()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1302 | RHSBlock = Builder.GetInsertBlock(); |
Daniel Dunbar | d57a871 | 2008-11-11 09:41:28 +0000 | [diff] [blame] | 1303 | CGF.EmitBranch(ContBlock); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1304 | |
| 1305 | CGF.EmitBlock(ContBlock); |
| 1306 | |
Nuno Lopes | 108f55d | 2008-06-04 19:15:45 +0000 | [diff] [blame] | 1307 | if (!LHS || !RHS) { |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1308 | assert(E->getType()->isVoidType() && "Non-void value should have a value"); |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1312 | // Create a PHI node for the real part. |
| 1313 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); |
| 1314 | PN->reserveOperandSpace(2); |
| 1315 | PN->addIncoming(LHS, LHSBlock); |
| 1316 | PN->addIncoming(RHS, RHSBlock); |
| 1317 | return PN; |
| 1318 | } |
| 1319 | |
| 1320 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 1321 | return Visit(E->getChosenSubExpr(CGF.getContext())); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1324 | Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Eli Friedman | 4fd0aa5 | 2009-01-20 17:46:04 +0000 | [diff] [blame] | 1325 | llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); |
Anders Carlsson | ddf7cac | 2008-11-04 05:30:00 +0000 | [diff] [blame] | 1326 | llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); |
| 1327 | |
| 1328 | // If EmitVAArg fails, we fall back to the LLVM instruction. |
| 1329 | if (!ArgPtr) |
| 1330 | return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); |
| 1331 | |
| 1332 | // FIXME: volatile? |
| 1333 | return Builder.CreateLoad(ArgPtr); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Mike Stump | df6b68c | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 1336 | Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) { |
Mike Stump | 0892099 | 2009-03-07 02:35:30 +0000 | [diff] [blame] | 1337 | return CGF.BuildBlockLiteralTmp(BE); |
Mike Stump | df6b68c | 2009-02-12 18:29:15 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1340 | //===----------------------------------------------------------------------===// |
| 1341 | // Entry Point into this File |
| 1342 | //===----------------------------------------------------------------------===// |
| 1343 | |
| 1344 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 1345 | /// complex type, ignoring the result. |
| 1346 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E) { |
| 1347 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 1348 | "Invalid scalar expression to emit"); |
| 1349 | |
| 1350 | return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
| 1351 | } |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1352 | |
| 1353 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 1354 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1355 | Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, |
| 1356 | QualType DstTy) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1357 | assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && |
| 1358 | "Invalid scalar expression to emit"); |
| 1359 | return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); |
| 1360 | } |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1361 | |
| 1362 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 1363 | /// complex type to the specified destination type, where the destination |
| 1364 | /// type is an LLVM scalar type. |
| 1365 | Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, |
| 1366 | QualType SrcTy, |
| 1367 | QualType DstTy) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 1368 | assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) && |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1369 | "Invalid complex -> scalar conversion"); |
| 1370 | return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, |
| 1371 | DstTy); |
| 1372 | } |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1373 | |
| 1374 | Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) { |
| 1375 | assert(V1->getType() == V2->getType() && |
| 1376 | "Vector operands must be of the same type"); |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1377 | unsigned NumElements = |
| 1378 | cast<llvm::VectorType>(V1->getType())->getNumElements(); |
| 1379 | |
| 1380 | va_list va; |
| 1381 | va_start(va, V2); |
| 1382 | |
| 1383 | llvm::SmallVector<llvm::Constant*, 16> Args; |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1384 | for (unsigned i = 0; i < NumElements; i++) { |
| 1385 | int n = va_arg(va, int); |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1386 | assert(n >= 0 && n < (int)NumElements * 2 && |
| 1387 | "Vector shuffle index out of bounds!"); |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1388 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n)); |
| 1389 | } |
| 1390 | |
| 1391 | const char *Name = va_arg(va, const char *); |
| 1392 | va_end(va); |
| 1393 | |
| 1394 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
| 1395 | |
| 1396 | return Builder.CreateShuffleVector(V1, V2, Mask, Name); |
| 1397 | } |
| 1398 | |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1399 | llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals, |
Chris Lattner | 345f720 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 1400 | unsigned NumVals, bool isSplat) { |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1401 | llvm::Value *Vec |
Chris Lattner | 345f720 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 1402 | = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals)); |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1403 | |
Chris Lattner | 345f720 | 2008-07-26 20:15:14 +0000 | [diff] [blame] | 1404 | for (unsigned i = 0, e = NumVals; i != e; ++i) { |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1405 | llvm::Value *Val = isSplat ? Vals[0] : Vals[i]; |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1406 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1407 | Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp"); |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | return Vec; |
| 1411 | } |