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" |
| 16 | #include "clang/AST/AST.h" |
Chris Lattner | 25ddea7 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Function.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 20 | #include "llvm/GlobalVariable.h" |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 21 | #include "llvm/Intrinsics.h" |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Chris Lattner | c89bf69 | 2008-01-03 07:05:49 +0000 | [diff] [blame] | 23 | #include <cstdarg> |
Ted Kremenek | 6aad91a | 2007-12-10 23:44:32 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | using llvm::Value; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // Scalar Expression Emitter |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | struct BinOpInfo { |
| 34 | Value *LHS; |
| 35 | Value *RHS; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 36 | QualType Ty; // Computation Type. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 37 | const BinaryOperator *E; |
| 38 | }; |
| 39 | |
| 40 | namespace { |
| 41 | class VISIBILITY_HIDDEN ScalarExprEmitter |
| 42 | : public StmtVisitor<ScalarExprEmitter, Value*> { |
| 43 | CodeGenFunction &CGF; |
Chris Lattner | 50b3674 | 2008-04-13 07:32:11 +0000 | [diff] [blame] | 44 | llvm::IRBuilder &Builder; |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 45 | CGObjCRuntime *Runtime; |
| 46 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 47 | public: |
| 48 | |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 49 | ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf), |
| 50 | Builder(CGF.Builder), |
| 51 | Runtime(CGF.CGM.getObjCRuntime()) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 52 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 53 | |
| 54 | //===--------------------------------------------------------------------===// |
| 55 | // Utilities |
| 56 | //===--------------------------------------------------------------------===// |
| 57 | |
| 58 | const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } |
| 59 | LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } |
| 60 | |
| 61 | Value *EmitLoadOfLValue(LValue LV, QualType T) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 62 | return CGF.EmitLoadOfLValue(LV, T).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 66 | /// value l-value, this method emits the address of the l-value, then loads |
| 67 | /// and returns the result. |
| 68 | Value *EmitLoadOfLValue(const Expr *E) { |
| 69 | // FIXME: Volatile |
| 70 | return EmitLoadOfLValue(EmitLValue(E), E->getType()); |
| 71 | } |
| 72 | |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 73 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 3420d0d | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 74 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 75 | Value *EmitConversionToBool(Value *Src, QualType DstTy); |
| 76 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 77 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 78 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 79 | Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy); |
| 80 | |
| 81 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 82 | /// complex type to the specified destination type, where the destination |
| 83 | /// type is an LLVM scalar type. |
| 84 | Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 85 | QualType SrcTy, QualType DstTy); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 87 | //===--------------------------------------------------------------------===// |
| 88 | // Visitor Methods |
| 89 | //===--------------------------------------------------------------------===// |
| 90 | |
| 91 | Value *VisitStmt(Stmt *S) { |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 92 | S->dump(CGF.getContext().getSourceManager()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 93 | assert(0 && "Stmt can't have complex result type!"); |
| 94 | return 0; |
| 95 | } |
| 96 | Value *VisitExpr(Expr *S); |
| 97 | Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); } |
| 98 | |
| 99 | // Leaves. |
| 100 | Value *VisitIntegerLiteral(const IntegerLiteral *E) { |
| 101 | return llvm::ConstantInt::get(E->getValue()); |
| 102 | } |
| 103 | Value *VisitFloatingLiteral(const FloatingLiteral *E) { |
Chris Lattner | 59138ba | 2008-04-20 00:45:53 +0000 | [diff] [blame] | 104 | return llvm::ConstantFP::get(E->getValue()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 105 | } |
| 106 | Value *VisitCharacterLiteral(const CharacterLiteral *E) { |
| 107 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
| 108 | } |
Nate Begeman | e7579b5 | 2007-11-15 05:40:03 +0000 | [diff] [blame] | 109 | Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
| 110 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
| 111 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 112 | Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
| 113 | return llvm::ConstantInt::get(ConvertType(E->getType()), |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 114 | CGF.getContext().typesAreCompatible( |
| 115 | E->getArgType1(), E->getArgType2())); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 116 | } |
| 117 | Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
| 118 | return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf()); |
| 119 | } |
| 120 | |
| 121 | // l-values. |
| 122 | Value *VisitDeclRefExpr(DeclRefExpr *E) { |
| 123 | if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl())) |
| 124 | return llvm::ConstantInt::get(EC->getInitVal()); |
| 125 | return EmitLoadOfLValue(E); |
| 126 | } |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 127 | Value *VisitObjCMessageExpr(ObjCMessageExpr *E); |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 128 | Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { return EmitLoadOfLValue(E);} |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 129 | Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 130 | Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 131 | Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 132 | Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } |
Eli Friedman | 06e863f | 2008-05-13 23:18:27 +0000 | [diff] [blame] | 133 | Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { return EmitLoadOfLValue(E); } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 134 | Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); } |
| 135 | Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); } |
Devang Patel | 35634f5 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 136 | |
| 137 | Value *VisitInitListExpr(InitListExpr *E) { |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 138 | unsigned NumInitElements = E->getNumInits(); |
| 139 | |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 140 | const llvm::VectorType *VType = |
Anders Carlsson | f6884ac | 2008-01-29 01:15:48 +0000 | [diff] [blame] | 141 | dyn_cast<llvm::VectorType>(ConvertType(E->getType())); |
| 142 | |
| 143 | // We have a scalar in braces. Just use the first element. |
| 144 | if (!VType) |
| 145 | return Visit(E->getInit(0)); |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 146 | |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 147 | unsigned NumVectorElements = VType->getNumElements(); |
| 148 | const llvm::Type *ElementType = VType->getElementType(); |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 149 | |
| 150 | // Emit individual vector element stores. |
| 151 | llvm::Value *V = llvm::UndefValue::get(VType); |
| 152 | |
Anders Carlsson | 222d2c8 | 2007-12-18 02:45:33 +0000 | [diff] [blame] | 153 | // Emit initializers |
| 154 | unsigned i; |
| 155 | for (i = 0; i < NumInitElements; ++i) { |
Devang Patel | a83cc33 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 156 | Value *NewV = Visit(E->getInit(i)); |
| 157 | Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 158 | V = Builder.CreateInsertElement(V, NewV, Idx); |
Devang Patel | 35634f5 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 159 | } |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 160 | |
| 161 | // Emit remaining default initializers |
| 162 | for (/* Do not initialize i*/; i < NumVectorElements; ++i) { |
| 163 | Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
| 164 | llvm::Value *NewV = llvm::Constant::getNullValue(ElementType); |
| 165 | V = Builder.CreateInsertElement(V, NewV, Idx); |
| 166 | } |
| 167 | |
Devang Patel | a83cc33 | 2007-10-24 18:05:48 +0000 | [diff] [blame] | 168 | return V; |
Devang Patel | 35634f5 | 2007-10-24 17:18:43 +0000 | [diff] [blame] | 169 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 171 | Value *VisitImplicitCastExpr(const ImplicitCastExpr *E); |
| 172 | Value *VisitCastExpr(const CastExpr *E) { |
| 173 | return EmitCastExpr(E->getSubExpr(), E->getType()); |
| 174 | } |
| 175 | Value *EmitCastExpr(const Expr *E, QualType T); |
| 176 | |
| 177 | Value *VisitCallExpr(const CallExpr *E) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 178 | return CGF.EmitCallExpr(E).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 181 | Value *VisitStmtExpr(const StmtExpr *E); |
| 182 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 183 | // Unary Operators. |
| 184 | Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre); |
| 185 | Value *VisitUnaryPostDec(const UnaryOperator *E) { |
| 186 | return VisitPrePostIncDec(E, false, false); |
| 187 | } |
| 188 | Value *VisitUnaryPostInc(const UnaryOperator *E) { |
| 189 | return VisitPrePostIncDec(E, true, false); |
| 190 | } |
| 191 | Value *VisitUnaryPreDec(const UnaryOperator *E) { |
| 192 | return VisitPrePostIncDec(E, false, true); |
| 193 | } |
| 194 | Value *VisitUnaryPreInc(const UnaryOperator *E) { |
| 195 | return VisitPrePostIncDec(E, true, true); |
| 196 | } |
| 197 | Value *VisitUnaryAddrOf(const UnaryOperator *E) { |
| 198 | return EmitLValue(E->getSubExpr()).getAddress(); |
| 199 | } |
| 200 | Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
| 201 | Value *VisitUnaryPlus(const UnaryOperator *E) { |
| 202 | return Visit(E->getSubExpr()); |
| 203 | } |
| 204 | Value *VisitUnaryMinus (const UnaryOperator *E); |
| 205 | Value *VisitUnaryNot (const UnaryOperator *E); |
| 206 | Value *VisitUnaryLNot (const UnaryOperator *E); |
| 207 | Value *VisitUnarySizeOf (const UnaryOperator *E) { |
| 208 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true); |
| 209 | } |
| 210 | Value *VisitUnaryAlignOf (const UnaryOperator *E) { |
| 211 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false); |
| 212 | } |
| 213 | Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType, |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 214 | bool isSizeOf); |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 215 | Value *VisitUnaryReal (const UnaryOperator *E); |
| 216 | Value *VisitUnaryImag (const UnaryOperator *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 217 | Value *VisitUnaryExtension(const UnaryOperator *E) { |
| 218 | return Visit(E->getSubExpr()); |
| 219 | } |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 220 | Value *VisitUnaryOffsetOf(const UnaryOperator *E); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 221 | Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { |
| 222 | return Visit(DAE->getExpr()); |
| 223 | } |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 225 | // Binary Operators. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 226 | Value *EmitMul(const BinOpInfo &Ops) { |
| 227 | return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); |
| 228 | } |
| 229 | Value *EmitDiv(const BinOpInfo &Ops); |
| 230 | Value *EmitRem(const BinOpInfo &Ops); |
| 231 | Value *EmitAdd(const BinOpInfo &Ops); |
| 232 | Value *EmitSub(const BinOpInfo &Ops); |
| 233 | Value *EmitShl(const BinOpInfo &Ops); |
| 234 | Value *EmitShr(const BinOpInfo &Ops); |
| 235 | Value *EmitAnd(const BinOpInfo &Ops) { |
| 236 | return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); |
| 237 | } |
| 238 | Value *EmitXor(const BinOpInfo &Ops) { |
| 239 | return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); |
| 240 | } |
| 241 | Value *EmitOr (const BinOpInfo &Ops) { |
| 242 | return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); |
| 243 | } |
| 244 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 245 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 246 | Value *EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 247 | Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); |
| 248 | |
| 249 | // Binary operators and binary compound assignment operators. |
| 250 | #define HANDLEBINOP(OP) \ |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 251 | Value *VisitBin ## OP(const BinaryOperator *E) { \ |
| 252 | return Emit ## OP(EmitBinOps(E)); \ |
| 253 | } \ |
| 254 | Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ |
| 255 | return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 256 | } |
| 257 | HANDLEBINOP(Mul); |
| 258 | HANDLEBINOP(Div); |
| 259 | HANDLEBINOP(Rem); |
| 260 | HANDLEBINOP(Add); |
| 261 | // (Sub) - Sub is handled specially below for ptr-ptr subtract. |
| 262 | HANDLEBINOP(Shl); |
| 263 | HANDLEBINOP(Shr); |
| 264 | HANDLEBINOP(And); |
| 265 | HANDLEBINOP(Xor); |
| 266 | HANDLEBINOP(Or); |
| 267 | #undef HANDLEBINOP |
| 268 | Value *VisitBinSub(const BinaryOperator *E); |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 269 | Value *VisitBinSubAssign(const CompoundAssignOperator *E) { |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 270 | return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub); |
| 271 | } |
| 272 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 273 | // Comparisons. |
| 274 | Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, |
| 275 | unsigned SICmpOpc, unsigned FCmpOpc); |
| 276 | #define VISITCOMP(CODE, UI, SI, FP) \ |
| 277 | Value *VisitBin##CODE(const BinaryOperator *E) { \ |
| 278 | return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ |
| 279 | llvm::FCmpInst::FP); } |
| 280 | VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT); |
| 281 | VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT); |
| 282 | VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE); |
| 283 | VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE); |
| 284 | VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ); |
| 285 | VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE); |
| 286 | #undef VISITCOMP |
| 287 | |
| 288 | Value *VisitBinAssign (const BinaryOperator *E); |
| 289 | |
| 290 | Value *VisitBinLAnd (const BinaryOperator *E); |
| 291 | Value *VisitBinLOr (const BinaryOperator *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 292 | Value *VisitBinComma (const BinaryOperator *E); |
| 293 | |
| 294 | // Other Operators. |
| 295 | Value *VisitConditionalOperator(const ConditionalOperator *CO); |
| 296 | Value *VisitChooseExpr(ChooseExpr *CE); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 297 | Value *VisitOverloadExpr(OverloadExpr *OE); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 298 | Value *VisitVAArgExpr(VAArgExpr *VE); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 299 | Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 300 | return CGF.EmitObjCStringLiteral(E); |
| 301 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 302 | Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 303 | }; |
| 304 | } // end anonymous namespace. |
| 305 | |
| 306 | //===----------------------------------------------------------------------===// |
| 307 | // Utilities |
| 308 | //===----------------------------------------------------------------------===// |
| 309 | |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 310 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 3420d0d | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 311 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 312 | Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { |
| 313 | assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs"); |
| 314 | |
| 315 | if (SrcType->isRealFloatingType()) { |
| 316 | // Compare against 0.0 for fp scalars. |
| 317 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 318 | return Builder.CreateFCmpUNE(Src, Zero, "tobool"); |
| 319 | } |
| 320 | |
| 321 | assert((SrcType->isIntegerType() || SrcType->isPointerType()) && |
| 322 | "Unknown scalar type to convert"); |
| 323 | |
| 324 | // Because of the type rules of C, we often end up computing a logical value, |
| 325 | // then zero extending it to int, then wanting it as a logical value again. |
| 326 | // Optimize this common case. |
| 327 | if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) { |
| 328 | if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) { |
| 329 | Value *Result = ZI->getOperand(0); |
Eli Friedman | 356916e | 2008-01-29 18:13:51 +0000 | [diff] [blame] | 330 | // If there aren't any more uses, zap the instruction to save space. |
| 331 | // Note that there can be more uses, for example if this |
| 332 | // is the result of an assignment. |
| 333 | if (ZI->use_empty()) |
| 334 | ZI->eraseFromParent(); |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 335 | return Result; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // Compare against an integer or pointer null. |
| 340 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
| 341 | return Builder.CreateICmpNE(Src, Zero, "tobool"); |
| 342 | } |
| 343 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 344 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 345 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 346 | Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, |
| 347 | QualType DstType) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 348 | SrcType = SrcType.getCanonicalType(); |
| 349 | DstType = DstType.getCanonicalType(); |
| 350 | if (SrcType == DstType) return Src; |
Chris Lattner | cf28908 | 2007-08-26 07:21:11 +0000 | [diff] [blame] | 351 | |
| 352 | if (DstType->isVoidType()) return 0; |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 353 | |
| 354 | // Handle conversions to bool first, they are special: comparisons against 0. |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 355 | if (DstType->isBooleanType()) |
| 356 | return EmitConversionToBool(Src, SrcType); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 357 | |
| 358 | const llvm::Type *DstTy = ConvertType(DstType); |
| 359 | |
| 360 | // Ignore conversions like int -> uint. |
| 361 | if (Src->getType() == DstTy) |
| 362 | return Src; |
| 363 | |
| 364 | // Handle pointer conversions next: pointers can only be converted to/from |
| 365 | // other pointers and integers. |
| 366 | if (isa<PointerType>(DstType)) { |
| 367 | // The source value may be an integer, or a pointer. |
| 368 | if (isa<llvm::PointerType>(Src->getType())) |
| 369 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
| 370 | assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
| 371 | return Builder.CreateIntToPtr(Src, DstTy, "conv"); |
| 372 | } |
| 373 | |
| 374 | if (isa<PointerType>(SrcType)) { |
| 375 | // Must be an ptr to int cast. |
| 376 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
Anders Carlsson | 50b5a30 | 2007-10-31 23:18:02 +0000 | [diff] [blame] | 377 | return Builder.CreatePtrToInt(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 380 | // A scalar can be splatted to an extended vector of the same element type |
| 381 | if (DstType->isExtVectorType() && !isa<VectorType>(SrcType) && |
Chris Lattner | 3b1ae00 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 382 | cast<llvm::VectorType>(DstTy)->getElementType() == Src->getType()) |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 383 | return CGF.EmitVector(&Src, DstType->getAsVectorType()->getNumElements(), |
| 384 | true); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 3b1ae00 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 386 | // Allow bitcast from vector to integer/fp of the same size. |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 387 | if (isa<llvm::VectorType>(Src->getType()) || |
Chris Lattner | 3b1ae00 | 2008-02-02 04:51:41 +0000 | [diff] [blame] | 388 | isa<llvm::VectorType>(DstTy)) |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 389 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
Anders Carlsson | 7019a9e | 2007-12-05 07:36:10 +0000 | [diff] [blame] | 390 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 391 | // Finally, we have the arithmetic types: real int/float. |
| 392 | if (isa<llvm::IntegerType>(Src->getType())) { |
| 393 | bool InputSigned = SrcType->isSignedIntegerType(); |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 394 | if (isa<llvm::IntegerType>(DstTy)) |
| 395 | return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); |
| 396 | else if (InputSigned) |
| 397 | return Builder.CreateSIToFP(Src, DstTy, "conv"); |
| 398 | else |
| 399 | return Builder.CreateUIToFP(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 403 | if (isa<llvm::IntegerType>(DstTy)) { |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 404 | if (DstType->isSignedIntegerType()) |
| 405 | return Builder.CreateFPToSI(Src, DstTy, "conv"); |
| 406 | else |
| 407 | return Builder.CreateFPToUI(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | assert(DstTy->isFloatingPoint() && "Unknown real conversion"); |
Anders Carlsson | b5ce097 | 2007-12-26 18:20:19 +0000 | [diff] [blame] | 411 | if (DstTy->getTypeID() < Src->getType()->getTypeID()) |
| 412 | return Builder.CreateFPTrunc(Src, DstTy, "conv"); |
| 413 | else |
| 414 | return Builder.CreateFPExt(Src, DstTy, "conv"); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 417 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 418 | /// complex type to the specified destination type, where the destination |
| 419 | /// type is an LLVM scalar type. |
| 420 | Value *ScalarExprEmitter:: |
| 421 | EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 422 | QualType SrcTy, QualType DstTy) { |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 423 | // Get the source element type. |
| 424 | SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType(); |
| 425 | |
| 426 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 427 | if (DstTy->isBooleanType()) { |
| 428 | // Complex != 0 -> (Real != 0) | (Imag != 0) |
| 429 | Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 430 | Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); |
| 431 | return Builder.CreateOr(Src.first, Src.second, "tobool"); |
| 432 | } |
| 433 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 434 | // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, |
| 435 | // the imaginary part of the complex value is discarded and the value of the |
| 436 | // real part is converted according to the conversion rules for the |
| 437 | // corresponding real type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 438 | return EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 439 | } |
| 440 | |
| 441 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 442 | //===----------------------------------------------------------------------===// |
| 443 | // Visitor Methods |
| 444 | //===----------------------------------------------------------------------===// |
| 445 | |
| 446 | Value *ScalarExprEmitter::VisitExpr(Expr *E) { |
Chris Lattner | dc4d280 | 2007-12-02 01:49:16 +0000 | [diff] [blame] | 447 | CGF.WarnUnsupported(E, "scalar expression"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 448 | if (E->getType()->isVoidType()) |
| 449 | return 0; |
| 450 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
| 451 | } |
| 452 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 453 | Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { |
| 454 | llvm::SmallVector<llvm::Constant*, 32> indices; |
| 455 | for (unsigned i = 2; i < E->getNumSubExprs(); i++) { |
| 456 | indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i)))); |
| 457 | } |
| 458 | Value* V1 = CGF.EmitScalarExpr(E->getExpr(0)); |
| 459 | Value* V2 = CGF.EmitScalarExpr(E->getExpr(1)); |
| 460 | Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size()); |
| 461 | return Builder.CreateShuffleVector(V1, V2, SV, "shuffle"); |
| 462 | } |
| 463 | |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 464 | Value *ScalarExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 465 | // Only the lookup mechanism and first two arguments of the method |
| 466 | // implementation vary between runtimes. We can get the receiver and |
| 467 | // arguments in generic code. |
| 468 | |
| 469 | // Find the receiver |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 470 | llvm::Value *Receiver = CGF.EmitScalarExpr(E->getReceiver()); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 471 | |
| 472 | // Process the arguments |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 473 | unsigned ArgC = E->getNumArgs(); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 474 | llvm::SmallVector<llvm::Value*, 16> Args; |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 475 | for (unsigned i = 0; i != ArgC; ++i) { |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 476 | Expr *ArgExpr = E->getArg(i); |
| 477 | QualType ArgTy = ArgExpr->getType(); |
| 478 | if (!CGF.hasAggregateLLVMType(ArgTy)) { |
| 479 | // Scalar argument is passed by-value. |
| 480 | Args.push_back(CGF.EmitScalarExpr(ArgExpr)); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 481 | } else if (ArgTy->isAnyComplexType()) { |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 482 | // Make a temporary alloca to pass the argument. |
| 483 | llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy)); |
| 484 | CGF.EmitComplexExprIntoAddr(ArgExpr, DestMem, false); |
| 485 | Args.push_back(DestMem); |
| 486 | } else { |
| 487 | llvm::Value *DestMem = CGF.CreateTempAlloca(ConvertType(ArgTy)); |
| 488 | CGF.EmitAggExpr(ArgExpr, DestMem, false); |
| 489 | Args.push_back(DestMem); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // Get the selector string |
| 494 | std::string SelStr = E->getSelector().getName(); |
| 495 | llvm::Constant *Selector = CGF.CGM.GetAddrOfConstantString(SelStr); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 496 | |
| 497 | llvm::Value *SelPtr = Builder.CreateStructGEP(Selector, 0); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 498 | return Runtime->GenerateMessageSend(Builder, ConvertType(E->getType()), |
Chris Lattner | c8aa5f1 | 2008-04-04 04:07:35 +0000 | [diff] [blame] | 499 | CGF.LoadObjCSelf(), |
Chris Lattner | ce5605e | 2008-03-30 23:25:33 +0000 | [diff] [blame] | 500 | Receiver, SelPtr, |
| 501 | &Args[0], Args.size()); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 504 | Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 505 | // Emit subscript expressions in rvalue context's. For most cases, this just |
| 506 | // loads the lvalue formed by the subscript expr. However, we have to be |
| 507 | // careful, because the base of a vector subscript is occasionally an rvalue, |
| 508 | // so we can't get it as an lvalue. |
| 509 | if (!E->getBase()->getType()->isVectorType()) |
| 510 | return EmitLoadOfLValue(E); |
| 511 | |
| 512 | // Handle the vector case. The base must be a vector, the index must be an |
| 513 | // integer value. |
| 514 | Value *Base = Visit(E->getBase()); |
| 515 | Value *Idx = Visit(E->getIdx()); |
| 516 | |
| 517 | // FIXME: Convert Idx to i32 type. |
| 518 | return Builder.CreateExtractElement(Base, Idx, "vecext"); |
| 519 | } |
| 520 | |
| 521 | /// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but |
| 522 | /// also handle things like function to pointer-to-function decay, and array to |
| 523 | /// pointer decay. |
| 524 | Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) { |
| 525 | const Expr *Op = E->getSubExpr(); |
| 526 | |
| 527 | // If this is due to array->pointer conversion, emit the array expression as |
| 528 | // an l-value. |
| 529 | if (Op->getType()->isArrayType()) { |
| 530 | // FIXME: For now we assume that all source arrays map to LLVM arrays. This |
| 531 | // will not true when we add support for VLAs. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 532 | Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 533 | |
| 534 | assert(isa<llvm::PointerType>(V->getType()) && |
| 535 | isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 536 | ->getElementType()) && |
| 537 | "Doesn't support VLAs yet!"); |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 538 | V = Builder.CreateStructGEP(V, 0, "arraydecay"); |
Chris Lattner | a9e6372 | 2007-12-12 04:13:20 +0000 | [diff] [blame] | 539 | |
| 540 | // The resultant pointer type can be implicitly casted to other pointer |
| 541 | // types as well, for example void*. |
| 542 | const llvm::Type *DestPTy = ConvertType(E->getType()); |
| 543 | assert(isa<llvm::PointerType>(DestPTy) && |
| 544 | "Only expect implicit cast to pointer"); |
| 545 | if (V->getType() != DestPTy) |
| 546 | V = Builder.CreateBitCast(V, DestPTy, "ptrconv"); |
| 547 | return V; |
| 548 | |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 549 | } else if (E->getType()->isReferenceType()) { |
Anders Carlsson | 23af9f2 | 2007-10-13 05:52:34 +0000 | [diff] [blame] | 550 | assert(cast<ReferenceType>(E->getType().getCanonicalType())-> |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 551 | getPointeeType() == |
Anders Carlsson | 23af9f2 | 2007-10-13 05:52:34 +0000 | [diff] [blame] | 552 | Op->getType().getCanonicalType() && "Incompatible types!"); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 553 | |
| 554 | return EmitLValue(Op).getAddress(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | return EmitCastExpr(Op, E->getType()); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts |
| 562 | // have to handle a more broad range of conversions than explicit casts, as they |
| 563 | // handle things like function to ptr-to-function decay etc. |
| 564 | Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) { |
Chris Lattner | 58a2e94 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 565 | // Handle cases where the source is an non-complex type. |
Chris Lattner | 19a1d7c | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 566 | |
| 567 | if (!CGF.hasAggregateLLVMType(E->getType())) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 568 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 569 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 570 | // Use EmitScalarConversion to perform the conversion. |
| 571 | return EmitScalarConversion(Src, E->getType(), DestTy); |
| 572 | } |
Chris Lattner | 19a1d7c | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 574 | if (E->getType()->isAnyComplexType()) { |
Chris Lattner | 19a1d7c | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 575 | // Handle cases where the source is a complex type. |
| 576 | return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(), |
| 577 | DestTy); |
| 578 | } |
Chris Lattner | 10b00cf | 2007-08-26 07:16:41 +0000 | [diff] [blame] | 579 | |
Chris Lattner | 19a1d7c | 2008-02-16 23:55:16 +0000 | [diff] [blame] | 580 | // Okay, this is a cast from an aggregate. It must be a cast to void. Just |
| 581 | // evaluate the result and return. |
| 582 | CGF.EmitAggExpr(E, 0, false); |
| 583 | return 0; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 586 | Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 587 | return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal(); |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 591 | //===----------------------------------------------------------------------===// |
| 592 | // Unary Operators |
| 593 | //===----------------------------------------------------------------------===// |
| 594 | |
| 595 | Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | dfce2a5 | 2007-08-24 16:24:49 +0000 | [diff] [blame] | 596 | bool isInc, bool isPre) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 597 | LValue LV = EmitLValue(E->getSubExpr()); |
| 598 | // FIXME: Handle volatile! |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 599 | Value *InVal = CGF.EmitLoadOfLValue(LV, // false |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 600 | E->getSubExpr()->getType()).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 601 | |
| 602 | int AmountVal = isInc ? 1 : -1; |
| 603 | |
| 604 | Value *NextVal; |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 605 | if (isa<llvm::PointerType>(InVal->getType())) { |
| 606 | // FIXME: This isn't right for VLAs. |
| 607 | NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal); |
Chris Lattner | 36b6a0a | 2008-03-19 05:19:41 +0000 | [diff] [blame] | 608 | NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec"); |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 609 | } else { |
| 610 | // Add the inc/dec to the real part. |
| 611 | if (isa<llvm::IntegerType>(InVal->getType())) |
| 612 | NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 613 | else if (InVal->getType() == llvm::Type::FloatTy) |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 614 | NextVal = |
Chris Lattner | 59138ba | 2008-04-20 00:45:53 +0000 | [diff] [blame] | 615 | llvm::ConstantFP::get(llvm::APFloat(static_cast<float>(AmountVal))); |
Chris Lattner | 25ddea7 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 616 | else if (InVal->getType() == llvm::Type::DoubleTy) |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 617 | NextVal = |
Chris Lattner | 59138ba | 2008-04-20 00:45:53 +0000 | [diff] [blame] | 618 | llvm::ConstantFP::get(llvm::APFloat(static_cast<double>(AmountVal))); |
Chris Lattner | 25ddea7 | 2008-04-20 00:50:39 +0000 | [diff] [blame] | 619 | else { |
| 620 | llvm::APFloat F(static_cast<float>(AmountVal)); |
| 621 | F.convert(*CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero); |
| 622 | NextVal = llvm::ConstantFP::get(F); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 623 | } |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 624 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 625 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 626 | |
| 627 | // Store the updated result through the lvalue. |
| 628 | CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, |
| 629 | E->getSubExpr()->getType()); |
| 630 | |
| 631 | // If this is a postinc, return the value read from memory, otherwise use the |
| 632 | // updated value. |
| 633 | return isPre ? NextVal : InVal; |
| 634 | } |
| 635 | |
| 636 | |
| 637 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 638 | Value *Op = Visit(E->getSubExpr()); |
| 639 | return Builder.CreateNeg(Op, "neg"); |
| 640 | } |
| 641 | |
| 642 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 643 | Value *Op = Visit(E->getSubExpr()); |
| 644 | return Builder.CreateNot(Op, "neg"); |
| 645 | } |
| 646 | |
| 647 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 648 | // Compare operand to zero. |
| 649 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
| 650 | |
| 651 | // Invert value. |
| 652 | // TODO: Could dynamically modify easy computations here. For example, if |
| 653 | // the operand is an icmp ne, turn into icmp eq. |
| 654 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
| 655 | |
| 656 | // ZExt result to int. |
| 657 | return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext"); |
| 658 | } |
| 659 | |
| 660 | /// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as |
| 661 | /// an integer (RetType). |
| 662 | Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize, |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 663 | QualType RetType,bool isSizeOf){ |
Chris Lattner | a269ebf | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 664 | assert(RetType->isIntegerType() && "Result type must be an integer!"); |
| 665 | uint32_t ResultWidth = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 666 | static_cast<uint32_t>(CGF.getContext().getTypeSize(RetType)); |
Chris Lattner | a269ebf | 2008-02-21 05:45:29 +0000 | [diff] [blame] | 667 | |
| 668 | // sizeof(void) and __alignof__(void) = 1 as a gcc extension. |
| 669 | if (TypeToSize->isVoidType()) |
| 670 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, 1)); |
| 671 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 672 | /// FIXME: This doesn't handle VLAs yet! |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 673 | std::pair<uint64_t, unsigned> Info = CGF.getContext().getTypeInfo(TypeToSize); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 674 | |
| 675 | uint64_t Val = isSizeOf ? Info.first : Info.second; |
| 676 | Val /= 8; // Return size in bytes, not bits. |
| 677 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 678 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 679 | } |
| 680 | |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 681 | Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { |
| 682 | Expr *Op = E->getSubExpr(); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 683 | if (Op->getType()->isAnyComplexType()) |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 684 | return CGF.EmitComplexExpr(Op).first; |
| 685 | return Visit(Op); |
| 686 | } |
| 687 | Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { |
| 688 | Expr *Op = E->getSubExpr(); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 689 | if (Op->getType()->isAnyComplexType()) |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 690 | return CGF.EmitComplexExpr(Op).second; |
Chris Lattner | 36f8406 | 2007-08-26 05:29:21 +0000 | [diff] [blame] | 691 | |
| 692 | // __imag on a scalar returns zero. Emit it the subexpr to ensure side |
| 693 | // effects are evaluated. |
| 694 | CGF.EmitScalarExpr(Op); |
| 695 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 698 | Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E) |
| 699 | { |
| 700 | int64_t Val = E->evaluateOffsetOf(CGF.getContext()); |
| 701 | |
| 702 | assert(E->getType()->isIntegerType() && "Result type must be an integer!"); |
| 703 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 704 | uint32_t ResultWidth = |
| 705 | static_cast<uint32_t>(CGF.getContext().getTypeSize(E->getType())); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 706 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 707 | } |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 708 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 709 | //===----------------------------------------------------------------------===// |
| 710 | // Binary Operators |
| 711 | //===----------------------------------------------------------------------===// |
| 712 | |
| 713 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
| 714 | BinOpInfo Result; |
| 715 | Result.LHS = Visit(E->getLHS()); |
| 716 | Result.RHS = Visit(E->getRHS()); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 717 | Result.Ty = E->getType(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 718 | Result.E = E; |
| 719 | return Result; |
| 720 | } |
| 721 | |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 722 | Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 723 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { |
| 724 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
| 725 | |
| 726 | BinOpInfo OpInfo; |
| 727 | |
| 728 | // Load the LHS and RHS operands. |
| 729 | LValue LHSLV = EmitLValue(E->getLHS()); |
| 730 | OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); |
Chris Lattner | 04dc764 | 2007-08-26 22:37:40 +0000 | [diff] [blame] | 731 | |
| 732 | // Determine the computation type. If the RHS is complex, then this is one of |
| 733 | // the add/sub/mul/div operators. All of these operators can be computed in |
| 734 | // with just their real component even though the computation domain really is |
| 735 | // complex. |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 736 | QualType ComputeType = E->getComputationType(); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 04dc764 | 2007-08-26 22:37:40 +0000 | [diff] [blame] | 738 | // If the computation type is complex, then the RHS is complex. Emit the RHS. |
| 739 | if (const ComplexType *CT = ComputeType->getAsComplexType()) { |
| 740 | ComputeType = CT->getElementType(); |
| 741 | |
| 742 | // Emit the RHS, only keeping the real component. |
| 743 | OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first; |
| 744 | RHSTy = RHSTy->getAsComplexType()->getElementType(); |
| 745 | } else { |
| 746 | // Otherwise the RHS is a simple scalar value. |
| 747 | OpInfo.RHS = Visit(E->getRHS()); |
| 748 | } |
| 749 | |
| 750 | // Convert the LHS/RHS values to the computation type. |
Chris Lattner | e937712 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 751 | OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 752 | |
Devang Patel | f86206f | 2007-10-25 22:19:13 +0000 | [diff] [blame] | 753 | // Do not merge types for -= or += where the LHS is a pointer. |
| 754 | if (!(E->getOpcode() == BinaryOperator::SubAssign || |
Devang Patel | 03f7c03 | 2007-10-30 18:31:12 +0000 | [diff] [blame] | 755 | E->getOpcode() == BinaryOperator::AddAssign) || |
Chris Lattner | 3b44b57 | 2007-08-25 21:56:20 +0000 | [diff] [blame] | 756 | !E->getLHS()->getType()->isPointerType()) { |
Chris Lattner | e937712 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 757 | OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 758 | } |
| 759 | OpInfo.Ty = ComputeType; |
| 760 | OpInfo.E = E; |
| 761 | |
| 762 | // Expand the binary operator. |
| 763 | Value *Result = (this->*Func)(OpInfo); |
| 764 | |
| 765 | // Truncate the result back to the LHS type. |
Chris Lattner | e937712 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 766 | Result = EmitScalarConversion(Result, ComputeType, LHSTy); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 767 | |
| 768 | // Store the result value into the LHS lvalue. |
| 769 | CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType()); |
| 770 | |
Eli Friedman | 1849128 | 2008-05-25 14:13:57 +0000 | [diff] [blame] | 771 | // For bitfields, we need the value in the bitfield |
| 772 | // FIXME: This adds an extra bitfield load |
| 773 | if (LHSLV.isBitfield()) |
| 774 | Result = EmitLoadOfLValue(LHSLV, LHSTy); |
| 775 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 776 | return Result; |
| 777 | } |
| 778 | |
| 779 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 780 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
Nate Begeman | b3ab8dc | 2007-12-30 01:28:16 +0000 | [diff] [blame] | 781 | if (Ops.LHS->getType()->isFPOrFPVector()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 782 | return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 783 | else if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 784 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 785 | else |
| 786 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 787 | } |
| 788 | |
| 789 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 790 | // 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] | 791 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 792 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 793 | else |
| 794 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 795 | } |
| 796 | |
| 797 | |
| 798 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 799 | if (!Ops.Ty->isPointerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 800 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 801 | |
| 802 | // FIXME: What about a pointer to a VLA? |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 803 | Value *Ptr, *Idx; |
| 804 | Expr *IdxExp; |
| 805 | if (isa<llvm::PointerType>(Ops.LHS->getType())) { // pointer + int |
| 806 | Ptr = Ops.LHS; |
| 807 | Idx = Ops.RHS; |
| 808 | IdxExp = Ops.E->getRHS(); |
| 809 | } else { // int + pointer |
| 810 | Ptr = Ops.RHS; |
| 811 | Idx = Ops.LHS; |
| 812 | IdxExp = Ops.E->getLHS(); |
| 813 | } |
| 814 | |
| 815 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 816 | if (Width < CGF.LLVMPointerWidth) { |
| 817 | // Zero or sign extend the pointer value based on whether the index is |
| 818 | // signed or not. |
| 819 | const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 820 | if (IdxExp->getType().getCanonicalType()->isSignedIntegerType()) |
| 821 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 822 | else |
| 823 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 824 | } |
| 825 | |
| 826 | return Builder.CreateGEP(Ptr, Idx, "add.ptr"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { |
| 830 | if (!isa<llvm::PointerType>(Ops.LHS->getType())) |
| 831 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
| 832 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 833 | // pointer - int |
| 834 | assert(!isa<llvm::PointerType>(Ops.RHS->getType()) && |
| 835 | "ptr-ptr shouldn't get here"); |
| 836 | // FIXME: The pointer could point to a VLA. |
Chris Lattner | 6860f3c | 2008-01-31 04:12:50 +0000 | [diff] [blame] | 837 | Value *Idx = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg"); |
| 838 | |
| 839 | unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); |
| 840 | if (Width < CGF.LLVMPointerWidth) { |
| 841 | // Zero or sign extend the pointer value based on whether the index is |
| 842 | // signed or not. |
| 843 | const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth); |
| 844 | if (Ops.E->getRHS()->getType().getCanonicalType()->isSignedIntegerType()) |
| 845 | Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); |
| 846 | else |
| 847 | Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); |
| 848 | } |
| 849 | |
| 850 | return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) { |
| 854 | // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In |
| 855 | // the compound assignment case it is invalid, so just handle it here. |
| 856 | if (!E->getRHS()->getType()->isPointerType()) |
| 857 | return EmitSub(EmitBinOps(E)); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 858 | |
| 859 | // pointer - pointer |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 860 | Value *LHS = Visit(E->getLHS()); |
| 861 | Value *RHS = Visit(E->getRHS()); |
| 862 | |
Seo Sanghyeon | ec86b97 | 2007-12-03 06:23:43 +0000 | [diff] [blame] | 863 | const QualType LHSType = E->getLHS()->getType().getCanonicalType(); |
Seo Sanghyeon | 9bb947a | 2007-12-26 05:21:37 +0000 | [diff] [blame] | 864 | const QualType LHSElementType = cast<PointerType>(LHSType)->getPointeeType(); |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 865 | uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 866 | |
| 867 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 868 | LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); |
| 869 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 870 | Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 871 | |
| 872 | // HACK: LLVM doesn't have an divide instruction that 'knows' there is no |
| 873 | // remainder. As such, we handle common power-of-two cases here to generate |
| 874 | // better code. |
| 875 | if (llvm::isPowerOf2_64(ElementSize)) { |
| 876 | Value *ShAmt = |
| 877 | llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize)); |
| 878 | return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr"); |
| 879 | } |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 880 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 881 | // Otherwise, do a full sdiv. |
| 882 | Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize); |
| 883 | return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); |
| 884 | } |
| 885 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 886 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 887 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 888 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 889 | // RHS to the same size as the LHS. |
| 890 | Value *RHS = Ops.RHS; |
| 891 | if (Ops.LHS->getType() != RHS->getType()) |
| 892 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 893 | |
| 894 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 895 | } |
| 896 | |
| 897 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 898 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 899 | // RHS to the same size as the LHS. |
| 900 | Value *RHS = Ops.RHS; |
| 901 | if (Ops.LHS->getType() != RHS->getType()) |
| 902 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 903 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 904 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 905 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 906 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 907 | } |
| 908 | |
| 909 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 910 | unsigned SICmpOpc, unsigned FCmpOpc) { |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 911 | Value *Result; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 912 | QualType LHSTy = E->getLHS()->getType(); |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 913 | if (!LHSTy->isAnyComplexType()) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 914 | Value *LHS = Visit(E->getLHS()); |
| 915 | Value *RHS = Visit(E->getRHS()); |
| 916 | |
| 917 | if (LHS->getType()->isFloatingPoint()) { |
| 918 | Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 919 | LHS, RHS, "cmp"); |
Eli Friedman | ec2c126 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 920 | } else if (LHSTy->isSignedIntegerType()) { |
| 921 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 922 | LHS, RHS, "cmp"); |
| 923 | } else { |
Eli Friedman | ec2c126 | 2008-05-29 15:09:15 +0000 | [diff] [blame] | 924 | // Unsigned integers and pointers. |
| 925 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 926 | LHS, RHS, "cmp"); |
| 927 | } |
| 928 | } else { |
| 929 | // Complex Comparison: can only be an equality comparison. |
| 930 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 931 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
| 932 | |
| 933 | QualType CETy = |
| 934 | cast<ComplexType>(LHSTy.getCanonicalType())->getElementType(); |
| 935 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 936 | Value *ResultR, *ResultI; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 937 | if (CETy->isRealFloatingType()) { |
| 938 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 939 | LHS.first, RHS.first, "cmp.r"); |
| 940 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 941 | LHS.second, RHS.second, "cmp.i"); |
| 942 | } else { |
| 943 | // Complex comparisons can only be equality comparisons. As such, signed |
| 944 | // and unsigned opcodes are the same. |
| 945 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 946 | LHS.first, RHS.first, "cmp.r"); |
| 947 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 948 | LHS.second, RHS.second, "cmp.i"); |
| 949 | } |
| 950 | |
| 951 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 952 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 953 | } else { |
| 954 | assert(E->getOpcode() == BinaryOperator::NE && |
| 955 | "Complex comparison other than == or != ?"); |
| 956 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | // ZExt result to int. |
| 961 | return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext"); |
| 962 | } |
| 963 | |
| 964 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 965 | LValue LHS = EmitLValue(E->getLHS()); |
| 966 | Value *RHS = Visit(E->getRHS()); |
| 967 | |
| 968 | // Store the value into the LHS. |
| 969 | // FIXME: Volatility! |
| 970 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); |
Eli Friedman | 1849128 | 2008-05-25 14:13:57 +0000 | [diff] [blame] | 971 | |
| 972 | // For bitfields, we need the value in the bitfield |
| 973 | // FIXME: This adds an extra bitfield load |
| 974 | if (LHS.isBitfield()) |
| 975 | return EmitLoadOfLValue(LHS, E->getLHS()->getType()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 976 | // Return the RHS. |
| 977 | return RHS; |
| 978 | } |
| 979 | |
| 980 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
| 981 | Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS()); |
| 982 | |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 983 | llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("land_cont"); |
| 984 | llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("land_rhs"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 985 | |
| 986 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 987 | Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock); |
| 988 | |
| 989 | CGF.EmitBlock(RHSBlock); |
| 990 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 991 | |
| 992 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 993 | RHSBlock = Builder.GetInsertBlock(); |
| 994 | CGF.EmitBlock(ContBlock); |
| 995 | |
| 996 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 997 | // false. If we evaluated both, the result is the RHS condition. |
| 998 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land"); |
| 999 | PN->reserveOperandSpace(2); |
| 1000 | PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock); |
| 1001 | PN->addIncoming(RHSCond, RHSBlock); |
| 1002 | |
| 1003 | // ZExt result to int. |
| 1004 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext"); |
| 1005 | } |
| 1006 | |
| 1007 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
| 1008 | Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS()); |
| 1009 | |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 1010 | llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("lor_cont"); |
| 1011 | llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("lor_rhs"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1012 | |
| 1013 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 1014 | Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock); |
| 1015 | |
| 1016 | CGF.EmitBlock(RHSBlock); |
| 1017 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 1018 | |
| 1019 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 1020 | RHSBlock = Builder.GetInsertBlock(); |
| 1021 | CGF.EmitBlock(ContBlock); |
| 1022 | |
| 1023 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 1024 | // true. If we evaluated both, the result is the RHS condition. |
| 1025 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor"); |
| 1026 | PN->reserveOperandSpace(2); |
| 1027 | PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock); |
| 1028 | PN->addIncoming(RHSCond, RHSBlock); |
| 1029 | |
| 1030 | // ZExt result to int. |
| 1031 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext"); |
| 1032 | } |
| 1033 | |
| 1034 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 1035 | CGF.EmitStmt(E->getLHS()); |
| 1036 | return Visit(E->getRHS()); |
| 1037 | } |
| 1038 | |
| 1039 | //===----------------------------------------------------------------------===// |
| 1040 | // Other Operators |
| 1041 | //===----------------------------------------------------------------------===// |
| 1042 | |
| 1043 | Value *ScalarExprEmitter:: |
| 1044 | VisitConditionalOperator(const ConditionalOperator *E) { |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 1045 | llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?"); |
| 1046 | llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:"); |
| 1047 | llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1049 | // Evaluate the conditional, then convert it to bool. We do this explicitly |
| 1050 | // because we need the unconverted value if this is a GNU ?: expression with |
| 1051 | // missing middle value. |
| 1052 | Value *CondVal = CGF.EmitScalarExpr(E->getCond()); |
Chris Lattner | c89bf69 | 2008-01-03 07:05:49 +0000 | [diff] [blame] | 1053 | Value *CondBoolVal =CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), |
| 1054 | CGF.getContext().BoolTy); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1055 | Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1056 | |
| 1057 | CGF.EmitBlock(LHSBlock); |
| 1058 | |
| 1059 | // Handle the GNU extension for missing LHS. |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1060 | Value *LHS; |
| 1061 | if (E->getLHS()) |
Eli Friedman | 856226c | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 1062 | LHS = Visit(E->getLHS()); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 1063 | else // Perform promotions, to handle cases like "short ?: int" |
| 1064 | LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType()); |
| 1065 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1066 | Builder.CreateBr(ContBlock); |
| 1067 | LHSBlock = Builder.GetInsertBlock(); |
| 1068 | |
| 1069 | CGF.EmitBlock(RHSBlock); |
| 1070 | |
Eli Friedman | 856226c | 2008-05-16 20:38:39 +0000 | [diff] [blame] | 1071 | Value *RHS = Visit(E->getRHS()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1072 | Builder.CreateBr(ContBlock); |
| 1073 | RHSBlock = Builder.GetInsertBlock(); |
| 1074 | |
| 1075 | CGF.EmitBlock(ContBlock); |
| 1076 | |
Nuno Lopes | 108f55d | 2008-06-04 19:15:45 +0000 | [diff] [blame^] | 1077 | if (!LHS || !RHS) { |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1078 | assert(E->getType()->isVoidType() && "Non-void value should have a value"); |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1082 | // Create a PHI node for the real part. |
| 1083 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); |
| 1084 | PN->reserveOperandSpace(2); |
| 1085 | PN->addIncoming(LHS, LHSBlock); |
| 1086 | PN->addIncoming(RHS, RHSBlock); |
| 1087 | return PN; |
| 1088 | } |
| 1089 | |
| 1090 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1091 | // Emit the LHS or RHS as appropriate. |
Devang Patel | e9b8c0a | 2007-10-30 20:59:40 +0000 | [diff] [blame] | 1092 | return |
| 1093 | Visit(E->isConditionTrue(CGF.getContext()) ? E->getLHS() : E->getRHS()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1096 | Value *ScalarExprEmitter::VisitOverloadExpr(OverloadExpr *E) { |
Nate Begeman | 67295d0 | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 1097 | return CGF.EmitCallExpr(E->getFn(), E->arg_begin(), |
| 1098 | E->getNumArgs(CGF.getContext())).getScalarVal(); |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1101 | Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1102 | llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress(); |
| 1103 | |
| 1104 | llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); |
| 1105 | return V; |
| 1106 | } |
| 1107 | |
Chris Lattner | 2202bce | 2007-11-30 17:56:23 +0000 | [diff] [blame] | 1108 | Value *ScalarExprEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1109 | std::string str; |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 1110 | llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; |
| 1111 | CGF.getContext().getObjCEncodingForType(E->getEncodedType(), str, |
| 1112 | EncodingRecordTypes); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 1113 | |
| 1114 | llvm::Constant *C = llvm::ConstantArray::get(str); |
| 1115 | C = new llvm::GlobalVariable(C->getType(), true, |
| 1116 | llvm::GlobalValue::InternalLinkage, |
| 1117 | C, ".str", &CGF.CGM.getModule()); |
| 1118 | llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 1119 | llvm::Constant *Zeros[] = { Zero, Zero }; |
| 1120 | C = llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2); |
| 1121 | |
| 1122 | return C; |
| 1123 | } |
| 1124 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 1125 | //===----------------------------------------------------------------------===// |
| 1126 | // Entry Point into this File |
| 1127 | //===----------------------------------------------------------------------===// |
| 1128 | |
| 1129 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 1130 | /// complex type, ignoring the result. |
| 1131 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E) { |
| 1132 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 1133 | "Invalid scalar expression to emit"); |
| 1134 | |
| 1135 | return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
| 1136 | } |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1137 | |
| 1138 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 1139 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1140 | Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, |
| 1141 | QualType DstTy) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 1142 | assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && |
| 1143 | "Invalid scalar expression to emit"); |
| 1144 | return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); |
| 1145 | } |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1146 | |
| 1147 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 1148 | /// complex type to the specified destination type, where the destination |
| 1149 | /// type is an LLVM scalar type. |
| 1150 | Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, |
| 1151 | QualType SrcTy, |
| 1152 | QualType DstTy) { |
Chris Lattner | 9b2dc28 | 2008-04-04 16:54:41 +0000 | [diff] [blame] | 1153 | assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) && |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 1154 | "Invalid complex -> scalar conversion"); |
| 1155 | return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, |
| 1156 | DstTy); |
| 1157 | } |
Anders Carlsson | cc23aca | 2007-12-10 19:35:18 +0000 | [diff] [blame] | 1158 | |
| 1159 | Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) { |
| 1160 | assert(V1->getType() == V2->getType() && |
| 1161 | "Vector operands must be of the same type"); |
| 1162 | |
| 1163 | unsigned NumElements = |
| 1164 | cast<llvm::VectorType>(V1->getType())->getNumElements(); |
| 1165 | |
| 1166 | va_list va; |
| 1167 | va_start(va, V2); |
| 1168 | |
| 1169 | llvm::SmallVector<llvm::Constant*, 16> Args; |
| 1170 | |
| 1171 | for (unsigned i = 0; i < NumElements; i++) { |
| 1172 | int n = va_arg(va, int); |
| 1173 | |
| 1174 | assert(n >= 0 && n < (int)NumElements * 2 && |
| 1175 | "Vector shuffle index out of bounds!"); |
| 1176 | |
| 1177 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, n)); |
| 1178 | } |
| 1179 | |
| 1180 | const char *Name = va_arg(va, const char *); |
| 1181 | va_end(va); |
| 1182 | |
| 1183 | llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); |
| 1184 | |
| 1185 | return Builder.CreateShuffleVector(V1, V2, Mask, Name); |
| 1186 | } |
| 1187 | |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1188 | llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals, |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1189 | unsigned NumVals, bool isSplat) |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1190 | { |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1191 | llvm::Value *Vec |
| 1192 | = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals)); |
| 1193 | |
| 1194 | for (unsigned i = 0, e = NumVals ; i != e; ++i) { |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1195 | llvm::Value *Val = isSplat ? Vals[0] : Vals[i]; |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1196 | llvm::Value *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i); |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1197 | Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp"); |
Anders Carlsson | 6086bbd | 2007-12-15 21:23:30 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | return Vec; |
| 1201 | } |