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