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