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