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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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 | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 19 | #include "llvm/Intrinsics.h" |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
| 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | using llvm::Value; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // Scalar Expression Emitter |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | struct BinOpInfo { |
| 30 | Value *LHS; |
| 31 | Value *RHS; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 32 | QualType Ty; // Computation Type. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 33 | const BinaryOperator *E; |
| 34 | }; |
| 35 | |
| 36 | namespace { |
| 37 | class VISIBILITY_HIDDEN ScalarExprEmitter |
| 38 | : public StmtVisitor<ScalarExprEmitter, Value*> { |
| 39 | CodeGenFunction &CGF; |
Devang Patel | 50c9034 | 2007-10-09 19:49:58 +0000 | [diff] [blame] | 40 | llvm::LLVMFoldingBuilder &Builder; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 41 | public: |
| 42 | |
| 43 | ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf), Builder(CGF.Builder) { |
| 44 | } |
| 45 | |
| 46 | |
| 47 | //===--------------------------------------------------------------------===// |
| 48 | // Utilities |
| 49 | //===--------------------------------------------------------------------===// |
| 50 | |
| 51 | const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } |
| 52 | LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } |
| 53 | |
| 54 | Value *EmitLoadOfLValue(LValue LV, QualType T) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 55 | return CGF.EmitLoadOfLValue(LV, T).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 59 | /// value l-value, this method emits the address of the l-value, then loads |
| 60 | /// and returns the result. |
| 61 | Value *EmitLoadOfLValue(const Expr *E) { |
| 62 | // FIXME: Volatile |
| 63 | return EmitLoadOfLValue(EmitLValue(E), E->getType()); |
| 64 | } |
| 65 | |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 66 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 3420d0d | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 67 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 68 | Value *EmitConversionToBool(Value *Src, QualType DstTy); |
| 69 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 70 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 71 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 72 | Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy); |
| 73 | |
| 74 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 75 | /// complex type to the specified destination type, where the destination |
| 76 | /// type is an LLVM scalar type. |
| 77 | Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 78 | QualType SrcTy, QualType DstTy); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 80 | //===--------------------------------------------------------------------===// |
| 81 | // Visitor Methods |
| 82 | //===--------------------------------------------------------------------===// |
| 83 | |
| 84 | Value *VisitStmt(Stmt *S) { |
Chris Lattner | 419ea7e | 2007-09-13 01:17:29 +0000 | [diff] [blame] | 85 | S->dump(CGF.getContext().SourceMgr); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 86 | assert(0 && "Stmt can't have complex result type!"); |
| 87 | return 0; |
| 88 | } |
| 89 | Value *VisitExpr(Expr *S); |
| 90 | Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); } |
| 91 | |
| 92 | // Leaves. |
| 93 | Value *VisitIntegerLiteral(const IntegerLiteral *E) { |
| 94 | return llvm::ConstantInt::get(E->getValue()); |
| 95 | } |
| 96 | Value *VisitFloatingLiteral(const FloatingLiteral *E) { |
Chris Lattner | c9bec4b | 2007-09-22 18:47:25 +0000 | [diff] [blame] | 97 | return llvm::ConstantFP::get(ConvertType(E->getType()), E->getValue()); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 98 | } |
| 99 | Value *VisitCharacterLiteral(const CharacterLiteral *E) { |
| 100 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
| 101 | } |
| 102 | Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
| 103 | return llvm::ConstantInt::get(ConvertType(E->getType()), |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 104 | CGF.getContext().typesAreCompatible( |
| 105 | E->getArgType1(), E->getArgType2())); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 106 | } |
| 107 | Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
| 108 | return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf()); |
| 109 | } |
| 110 | |
| 111 | // l-values. |
| 112 | Value *VisitDeclRefExpr(DeclRefExpr *E) { |
| 113 | if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl())) |
| 114 | return llvm::ConstantInt::get(EC->getInitVal()); |
| 115 | return EmitLoadOfLValue(E); |
| 116 | } |
| 117 | Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
| 118 | Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); } |
| 119 | Value *VisitOCUVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } |
| 120 | Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); } |
| 121 | Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); } |
| 122 | |
| 123 | // FIXME: CompoundLiteralExpr |
| 124 | Value *VisitImplicitCastExpr(const ImplicitCastExpr *E); |
| 125 | Value *VisitCastExpr(const CastExpr *E) { |
| 126 | return EmitCastExpr(E->getSubExpr(), E->getType()); |
| 127 | } |
| 128 | Value *EmitCastExpr(const Expr *E, QualType T); |
| 129 | |
| 130 | Value *VisitCallExpr(const CallExpr *E) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 131 | return CGF.EmitCallExpr(E).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 134 | Value *VisitStmtExpr(const StmtExpr *E); |
| 135 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 136 | // Unary Operators. |
| 137 | Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre); |
| 138 | Value *VisitUnaryPostDec(const UnaryOperator *E) { |
| 139 | return VisitPrePostIncDec(E, false, false); |
| 140 | } |
| 141 | Value *VisitUnaryPostInc(const UnaryOperator *E) { |
| 142 | return VisitPrePostIncDec(E, true, false); |
| 143 | } |
| 144 | Value *VisitUnaryPreDec(const UnaryOperator *E) { |
| 145 | return VisitPrePostIncDec(E, false, true); |
| 146 | } |
| 147 | Value *VisitUnaryPreInc(const UnaryOperator *E) { |
| 148 | return VisitPrePostIncDec(E, true, true); |
| 149 | } |
| 150 | Value *VisitUnaryAddrOf(const UnaryOperator *E) { |
| 151 | return EmitLValue(E->getSubExpr()).getAddress(); |
| 152 | } |
| 153 | Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
| 154 | Value *VisitUnaryPlus(const UnaryOperator *E) { |
| 155 | return Visit(E->getSubExpr()); |
| 156 | } |
| 157 | Value *VisitUnaryMinus (const UnaryOperator *E); |
| 158 | Value *VisitUnaryNot (const UnaryOperator *E); |
| 159 | Value *VisitUnaryLNot (const UnaryOperator *E); |
| 160 | Value *VisitUnarySizeOf (const UnaryOperator *E) { |
| 161 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true); |
| 162 | } |
| 163 | Value *VisitUnaryAlignOf (const UnaryOperator *E) { |
| 164 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false); |
| 165 | } |
| 166 | Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType, |
| 167 | bool isSizeOf); |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 168 | Value *VisitUnaryReal (const UnaryOperator *E); |
| 169 | Value *VisitUnaryImag (const UnaryOperator *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 170 | Value *VisitUnaryExtension(const UnaryOperator *E) { |
| 171 | return Visit(E->getSubExpr()); |
| 172 | } |
| 173 | |
| 174 | // Binary Operators. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 175 | Value *EmitMul(const BinOpInfo &Ops) { |
| 176 | return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); |
| 177 | } |
| 178 | Value *EmitDiv(const BinOpInfo &Ops); |
| 179 | Value *EmitRem(const BinOpInfo &Ops); |
| 180 | Value *EmitAdd(const BinOpInfo &Ops); |
| 181 | Value *EmitSub(const BinOpInfo &Ops); |
| 182 | Value *EmitShl(const BinOpInfo &Ops); |
| 183 | Value *EmitShr(const BinOpInfo &Ops); |
| 184 | Value *EmitAnd(const BinOpInfo &Ops) { |
| 185 | return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); |
| 186 | } |
| 187 | Value *EmitXor(const BinOpInfo &Ops) { |
| 188 | return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); |
| 189 | } |
| 190 | Value *EmitOr (const BinOpInfo &Ops) { |
| 191 | return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); |
| 192 | } |
| 193 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 194 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 195 | Value *EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 196 | Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); |
| 197 | |
| 198 | // Binary operators and binary compound assignment operators. |
| 199 | #define HANDLEBINOP(OP) \ |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 200 | Value *VisitBin ## OP(const BinaryOperator *E) { \ |
| 201 | return Emit ## OP(EmitBinOps(E)); \ |
| 202 | } \ |
| 203 | Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ |
| 204 | return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 205 | } |
| 206 | HANDLEBINOP(Mul); |
| 207 | HANDLEBINOP(Div); |
| 208 | HANDLEBINOP(Rem); |
| 209 | HANDLEBINOP(Add); |
| 210 | // (Sub) - Sub is handled specially below for ptr-ptr subtract. |
| 211 | HANDLEBINOP(Shl); |
| 212 | HANDLEBINOP(Shr); |
| 213 | HANDLEBINOP(And); |
| 214 | HANDLEBINOP(Xor); |
| 215 | HANDLEBINOP(Or); |
| 216 | #undef HANDLEBINOP |
| 217 | Value *VisitBinSub(const BinaryOperator *E); |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 218 | Value *VisitBinSubAssign(const CompoundAssignOperator *E) { |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 219 | return EmitCompoundAssign(E, &ScalarExprEmitter::EmitSub); |
| 220 | } |
| 221 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 222 | // Comparisons. |
| 223 | Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, |
| 224 | unsigned SICmpOpc, unsigned FCmpOpc); |
| 225 | #define VISITCOMP(CODE, UI, SI, FP) \ |
| 226 | Value *VisitBin##CODE(const BinaryOperator *E) { \ |
| 227 | return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ |
| 228 | llvm::FCmpInst::FP); } |
| 229 | VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT); |
| 230 | VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT); |
| 231 | VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE); |
| 232 | VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE); |
| 233 | VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ); |
| 234 | VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE); |
| 235 | #undef VISITCOMP |
| 236 | |
| 237 | Value *VisitBinAssign (const BinaryOperator *E); |
| 238 | |
| 239 | Value *VisitBinLAnd (const BinaryOperator *E); |
| 240 | Value *VisitBinLOr (const BinaryOperator *E); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 241 | Value *VisitBinComma (const BinaryOperator *E); |
| 242 | |
| 243 | // Other Operators. |
| 244 | Value *VisitConditionalOperator(const ConditionalOperator *CO); |
| 245 | Value *VisitChooseExpr(ChooseExpr *CE); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 246 | Value *VisitVAArgExpr(VAArgExpr *VE); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 247 | Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 248 | return CGF.EmitObjCStringLiteral(E); |
| 249 | } |
| 250 | }; |
| 251 | } // end anonymous namespace. |
| 252 | |
| 253 | //===----------------------------------------------------------------------===// |
| 254 | // Utilities |
| 255 | //===----------------------------------------------------------------------===// |
| 256 | |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 257 | /// EmitConversionToBool - Convert the specified expression value to a |
Chris Lattner | 3420d0d | 2007-08-26 17:25:57 +0000 | [diff] [blame] | 258 | /// boolean (i1) truth value. This is equivalent to "Val != 0". |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 259 | Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { |
| 260 | assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs"); |
| 261 | |
| 262 | if (SrcType->isRealFloatingType()) { |
| 263 | // Compare against 0.0 for fp scalars. |
| 264 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
Chris Lattner | 9abc84e | 2007-08-26 16:42:57 +0000 | [diff] [blame] | 265 | return Builder.CreateFCmpUNE(Src, Zero, "tobool"); |
| 266 | } |
| 267 | |
| 268 | assert((SrcType->isIntegerType() || SrcType->isPointerType()) && |
| 269 | "Unknown scalar type to convert"); |
| 270 | |
| 271 | // Because of the type rules of C, we often end up computing a logical value, |
| 272 | // then zero extending it to int, then wanting it as a logical value again. |
| 273 | // Optimize this common case. |
| 274 | if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) { |
| 275 | if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) { |
| 276 | Value *Result = ZI->getOperand(0); |
| 277 | ZI->eraseFromParent(); |
| 278 | return Result; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // Compare against an integer or pointer null. |
| 283 | llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); |
| 284 | return Builder.CreateICmpNE(Src, Zero, "tobool"); |
| 285 | } |
| 286 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 287 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 288 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 289 | Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, |
| 290 | QualType DstType) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 291 | SrcType = SrcType.getCanonicalType(); |
| 292 | DstType = DstType.getCanonicalType(); |
| 293 | if (SrcType == DstType) return Src; |
Chris Lattner | cf28908 | 2007-08-26 07:21:11 +0000 | [diff] [blame] | 294 | |
| 295 | if (DstType->isVoidType()) return 0; |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 296 | |
| 297 | // Handle conversions to bool first, they are special: comparisons against 0. |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 298 | if (DstType->isBooleanType()) |
| 299 | return EmitConversionToBool(Src, SrcType); |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 300 | |
| 301 | const llvm::Type *DstTy = ConvertType(DstType); |
| 302 | |
| 303 | // Ignore conversions like int -> uint. |
| 304 | if (Src->getType() == DstTy) |
| 305 | return Src; |
| 306 | |
| 307 | // Handle pointer conversions next: pointers can only be converted to/from |
| 308 | // other pointers and integers. |
| 309 | if (isa<PointerType>(DstType)) { |
| 310 | // The source value may be an integer, or a pointer. |
| 311 | if (isa<llvm::PointerType>(Src->getType())) |
| 312 | return Builder.CreateBitCast(Src, DstTy, "conv"); |
| 313 | assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); |
| 314 | return Builder.CreateIntToPtr(Src, DstTy, "conv"); |
| 315 | } |
| 316 | |
| 317 | if (isa<PointerType>(SrcType)) { |
| 318 | // Must be an ptr to int cast. |
| 319 | assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); |
| 320 | return Builder.CreateIntToPtr(Src, DstTy, "conv"); |
| 321 | } |
| 322 | |
| 323 | // Finally, we have the arithmetic types: real int/float. |
| 324 | if (isa<llvm::IntegerType>(Src->getType())) { |
| 325 | bool InputSigned = SrcType->isSignedIntegerType(); |
| 326 | if (isa<llvm::IntegerType>(DstTy)) |
| 327 | return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); |
| 328 | else if (InputSigned) |
| 329 | return Builder.CreateSIToFP(Src, DstTy, "conv"); |
| 330 | else |
| 331 | return Builder.CreateUIToFP(Src, DstTy, "conv"); |
| 332 | } |
| 333 | |
| 334 | assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); |
| 335 | if (isa<llvm::IntegerType>(DstTy)) { |
| 336 | if (DstType->isSignedIntegerType()) |
| 337 | return Builder.CreateFPToSI(Src, DstTy, "conv"); |
| 338 | else |
| 339 | return Builder.CreateFPToUI(Src, DstTy, "conv"); |
| 340 | } |
| 341 | |
| 342 | assert(DstTy->isFloatingPoint() && "Unknown real conversion"); |
| 343 | if (DstTy->getTypeID() < Src->getType()->getTypeID()) |
| 344 | return Builder.CreateFPTrunc(Src, DstTy, "conv"); |
| 345 | else |
| 346 | return Builder.CreateFPExt(Src, DstTy, "conv"); |
| 347 | } |
| 348 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 349 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 350 | /// complex type to the specified destination type, where the destination |
| 351 | /// type is an LLVM scalar type. |
| 352 | Value *ScalarExprEmitter:: |
| 353 | EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, |
| 354 | QualType SrcTy, QualType DstTy) { |
Chris Lattner | ed70f0a | 2007-08-26 16:52:28 +0000 | [diff] [blame] | 355 | // Get the source element type. |
| 356 | SrcTy = cast<ComplexType>(SrcTy.getCanonicalType())->getElementType(); |
| 357 | |
| 358 | // Handle conversions to bool first, they are special: comparisons against 0. |
| 359 | if (DstTy->isBooleanType()) { |
| 360 | // Complex != 0 -> (Real != 0) | (Imag != 0) |
| 361 | Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 362 | Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); |
| 363 | return Builder.CreateOr(Src.first, Src.second, "tobool"); |
| 364 | } |
| 365 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 366 | // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, |
| 367 | // the imaginary part of the complex value is discarded and the value of the |
| 368 | // real part is converted according to the conversion rules for the |
| 369 | // corresponding real type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 370 | return EmitScalarConversion(Src.first, SrcTy, DstTy); |
| 371 | } |
| 372 | |
| 373 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 374 | //===----------------------------------------------------------------------===// |
| 375 | // Visitor Methods |
| 376 | //===----------------------------------------------------------------------===// |
| 377 | |
| 378 | Value *ScalarExprEmitter::VisitExpr(Expr *E) { |
| 379 | fprintf(stderr, "Unimplemented scalar expr!\n"); |
Chris Lattner | 419ea7e | 2007-09-13 01:17:29 +0000 | [diff] [blame] | 380 | E->dump(CGF.getContext().SourceMgr); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 381 | if (E->getType()->isVoidType()) |
| 382 | return 0; |
| 383 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
| 384 | } |
| 385 | |
| 386 | Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 387 | // Emit subscript expressions in rvalue context's. For most cases, this just |
| 388 | // loads the lvalue formed by the subscript expr. However, we have to be |
| 389 | // careful, because the base of a vector subscript is occasionally an rvalue, |
| 390 | // so we can't get it as an lvalue. |
| 391 | if (!E->getBase()->getType()->isVectorType()) |
| 392 | return EmitLoadOfLValue(E); |
| 393 | |
| 394 | // Handle the vector case. The base must be a vector, the index must be an |
| 395 | // integer value. |
| 396 | Value *Base = Visit(E->getBase()); |
| 397 | Value *Idx = Visit(E->getIdx()); |
| 398 | |
| 399 | // FIXME: Convert Idx to i32 type. |
| 400 | return Builder.CreateExtractElement(Base, Idx, "vecext"); |
| 401 | } |
| 402 | |
| 403 | /// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but |
| 404 | /// also handle things like function to pointer-to-function decay, and array to |
| 405 | /// pointer decay. |
| 406 | Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) { |
| 407 | const Expr *Op = E->getSubExpr(); |
| 408 | |
| 409 | // If this is due to array->pointer conversion, emit the array expression as |
| 410 | // an l-value. |
| 411 | if (Op->getType()->isArrayType()) { |
| 412 | // FIXME: For now we assume that all source arrays map to LLVM arrays. This |
| 413 | // will not true when we add support for VLAs. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 414 | Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays. |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 415 | |
| 416 | assert(isa<llvm::PointerType>(V->getType()) && |
| 417 | isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 418 | ->getElementType()) && |
| 419 | "Doesn't support VLAs yet!"); |
| 420 | llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
Ted Kremenek | d627889 | 2007-09-04 17:20:08 +0000 | [diff] [blame] | 421 | |
| 422 | llvm::Value *Ops[] = {Idx0, Idx0}; |
| 423 | return Builder.CreateGEP(V, Ops, Ops+2, "arraydecay"); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 424 | } else if (E->getType()->isReferenceType()) { |
Anders Carlsson | 23af9f2 | 2007-10-13 05:52:34 +0000 | [diff] [blame] | 425 | assert(cast<ReferenceType>(E->getType().getCanonicalType())-> |
| 426 | getReferenceeType() == |
| 427 | Op->getType().getCanonicalType() && "Incompatible types!"); |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 428 | |
| 429 | return EmitLValue(Op).getAddress(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | return EmitCastExpr(Op, E->getType()); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts |
| 437 | // have to handle a more broad range of conversions than explicit casts, as they |
| 438 | // handle things like function to ptr-to-function decay etc. |
| 439 | Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) { |
Chris Lattner | 58a2e94 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 440 | // Handle cases where the source is an non-complex type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 441 | if (!E->getType()->isComplexType()) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 442 | Value *Src = Visit(const_cast<Expr*>(E)); |
| 443 | |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 444 | // Use EmitScalarConversion to perform the conversion. |
| 445 | return EmitScalarConversion(Src, E->getType(), DestTy); |
| 446 | } |
Chris Lattner | 10b00cf | 2007-08-26 07:16:41 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 58a2e94 | 2007-08-26 07:26:12 +0000 | [diff] [blame] | 448 | // Handle cases where the source is a complex type. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 449 | return EmitComplexToScalarConversion(CGF.EmitComplexExpr(E), E->getType(), |
| 450 | DestTy); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 453 | Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 454 | return CGF.EmitCompoundStmt(*E->getSubStmt(), true).getScalarVal(); |
Chris Lattner | 3379320 | 2007-08-31 22:09:40 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 458 | //===----------------------------------------------------------------------===// |
| 459 | // Unary Operators |
| 460 | //===----------------------------------------------------------------------===// |
| 461 | |
| 462 | Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
Chris Lattner | dfce2a5 | 2007-08-24 16:24:49 +0000 | [diff] [blame] | 463 | bool isInc, bool isPre) { |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 464 | LValue LV = EmitLValue(E->getSubExpr()); |
| 465 | // FIXME: Handle volatile! |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 466 | Value *InVal = CGF.EmitLoadOfLValue(LV, // false |
Chris Lattner | 9b65551 | 2007-08-31 22:49:20 +0000 | [diff] [blame] | 467 | E->getSubExpr()->getType()).getScalarVal(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 468 | |
| 469 | int AmountVal = isInc ? 1 : -1; |
| 470 | |
| 471 | Value *NextVal; |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 472 | if (isa<llvm::PointerType>(InVal->getType())) { |
| 473 | // FIXME: This isn't right for VLAs. |
| 474 | NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal); |
| 475 | NextVal = Builder.CreateGEP(InVal, NextVal); |
| 476 | } else { |
| 477 | // Add the inc/dec to the real part. |
| 478 | if (isa<llvm::IntegerType>(InVal->getType())) |
| 479 | NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); |
Chris Lattner | ca2617c | 2007-09-13 06:19:18 +0000 | [diff] [blame] | 480 | else if (InVal->getType() == llvm::Type::FloatTy) |
| 481 | // FIXME: Handle long double. |
| 482 | NextVal = llvm::ConstantFP::get(InVal->getType(), |
| 483 | llvm::APFloat(static_cast<float>(AmountVal))); |
| 484 | else { |
| 485 | // FIXME: Handle long double. |
| 486 | assert(InVal->getType() == llvm::Type::DoubleTy); |
| 487 | NextVal = llvm::ConstantFP::get(InVal->getType(), |
| 488 | llvm::APFloat(static_cast<double>(AmountVal))); |
| 489 | } |
Chris Lattner | e936cc8 | 2007-08-26 05:10:16 +0000 | [diff] [blame] | 490 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 491 | } |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 492 | |
| 493 | // Store the updated result through the lvalue. |
| 494 | CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, |
| 495 | E->getSubExpr()->getType()); |
| 496 | |
| 497 | // If this is a postinc, return the value read from memory, otherwise use the |
| 498 | // updated value. |
| 499 | return isPre ? NextVal : InVal; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 504 | Value *Op = Visit(E->getSubExpr()); |
| 505 | return Builder.CreateNeg(Op, "neg"); |
| 506 | } |
| 507 | |
| 508 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 509 | Value *Op = Visit(E->getSubExpr()); |
| 510 | return Builder.CreateNot(Op, "neg"); |
| 511 | } |
| 512 | |
| 513 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 514 | // Compare operand to zero. |
| 515 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
| 516 | |
| 517 | // Invert value. |
| 518 | // TODO: Could dynamically modify easy computations here. For example, if |
| 519 | // the operand is an icmp ne, turn into icmp eq. |
| 520 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
| 521 | |
| 522 | // ZExt result to int. |
| 523 | return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext"); |
| 524 | } |
| 525 | |
| 526 | /// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as |
| 527 | /// an integer (RetType). |
| 528 | Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize, |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 529 | QualType RetType,bool isSizeOf){ |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 530 | /// FIXME: This doesn't handle VLAs yet! |
| 531 | std::pair<uint64_t, unsigned> Info = |
| 532 | CGF.getContext().getTypeInfo(TypeToSize, SourceLocation()); |
| 533 | |
| 534 | uint64_t Val = isSizeOf ? Info.first : Info.second; |
| 535 | Val /= 8; // Return size in bytes, not bits. |
| 536 | |
| 537 | assert(RetType->isIntegerType() && "Result type must be an integer!"); |
| 538 | |
Hartmut Kaiser | 7b66000 | 2007-10-17 15:00:17 +0000 | [diff] [blame^] | 539 | uint32_t ResultWidth = static_cast<uint32_t>( |
| 540 | CGF.getContext().getTypeSize(RetType, SourceLocation())); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 541 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 542 | } |
| 543 | |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 544 | Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { |
| 545 | Expr *Op = E->getSubExpr(); |
| 546 | if (Op->getType()->isComplexType()) |
| 547 | return CGF.EmitComplexExpr(Op).first; |
| 548 | return Visit(Op); |
| 549 | } |
| 550 | Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { |
| 551 | Expr *Op = E->getSubExpr(); |
| 552 | if (Op->getType()->isComplexType()) |
| 553 | return CGF.EmitComplexExpr(Op).second; |
Chris Lattner | 36f8406 | 2007-08-26 05:29:21 +0000 | [diff] [blame] | 554 | |
| 555 | // __imag on a scalar returns zero. Emit it the subexpr to ensure side |
| 556 | // effects are evaluated. |
| 557 | CGF.EmitScalarExpr(Op); |
| 558 | return llvm::Constant::getNullValue(ConvertType(E->getType())); |
Chris Lattner | 46f93d0 | 2007-08-24 21:20:17 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 562 | //===----------------------------------------------------------------------===// |
| 563 | // Binary Operators |
| 564 | //===----------------------------------------------------------------------===// |
| 565 | |
| 566 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
| 567 | BinOpInfo Result; |
| 568 | Result.LHS = Visit(E->getLHS()); |
| 569 | Result.RHS = Visit(E->getRHS()); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 570 | Result.Ty = E->getType(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 571 | Result.E = E; |
| 572 | return Result; |
| 573 | } |
| 574 | |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 575 | Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 576 | Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { |
| 577 | QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); |
| 578 | |
| 579 | BinOpInfo OpInfo; |
| 580 | |
| 581 | // Load the LHS and RHS operands. |
| 582 | LValue LHSLV = EmitLValue(E->getLHS()); |
| 583 | OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); |
Chris Lattner | 04dc764 | 2007-08-26 22:37:40 +0000 | [diff] [blame] | 584 | |
| 585 | // Determine the computation type. If the RHS is complex, then this is one of |
| 586 | // the add/sub/mul/div operators. All of these operators can be computed in |
| 587 | // with just their real component even though the computation domain really is |
| 588 | // complex. |
Chris Lattner | 3ccf774 | 2007-08-26 21:41:21 +0000 | [diff] [blame] | 589 | QualType ComputeType = E->getComputationType(); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 590 | |
Chris Lattner | 04dc764 | 2007-08-26 22:37:40 +0000 | [diff] [blame] | 591 | // If the computation type is complex, then the RHS is complex. Emit the RHS. |
| 592 | if (const ComplexType *CT = ComputeType->getAsComplexType()) { |
| 593 | ComputeType = CT->getElementType(); |
| 594 | |
| 595 | // Emit the RHS, only keeping the real component. |
| 596 | OpInfo.RHS = CGF.EmitComplexExpr(E->getRHS()).first; |
| 597 | RHSTy = RHSTy->getAsComplexType()->getElementType(); |
| 598 | } else { |
| 599 | // Otherwise the RHS is a simple scalar value. |
| 600 | OpInfo.RHS = Visit(E->getRHS()); |
| 601 | } |
| 602 | |
| 603 | // Convert the LHS/RHS values to the computation type. |
Chris Lattner | e937712 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 604 | OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, ComputeType); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 605 | |
| 606 | // Do not merge types for -= where the LHS is a pointer. |
Chris Lattner | 3b44b57 | 2007-08-25 21:56:20 +0000 | [diff] [blame] | 607 | if (E->getOpcode() != BinaryOperator::SubAssign || |
| 608 | !E->getLHS()->getType()->isPointerType()) { |
Chris Lattner | e937712 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 609 | OpInfo.RHS = EmitScalarConversion(OpInfo.RHS, RHSTy, ComputeType); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 610 | } |
| 611 | OpInfo.Ty = ComputeType; |
| 612 | OpInfo.E = E; |
| 613 | |
| 614 | // Expand the binary operator. |
| 615 | Value *Result = (this->*Func)(OpInfo); |
| 616 | |
| 617 | // Truncate the result back to the LHS type. |
Chris Lattner | e937712 | 2007-08-26 07:08:39 +0000 | [diff] [blame] | 618 | Result = EmitScalarConversion(Result, ComputeType, LHSTy); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 619 | |
| 620 | // Store the result value into the LHS lvalue. |
| 621 | CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, E->getType()); |
| 622 | |
| 623 | return Result; |
| 624 | } |
| 625 | |
| 626 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 627 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
| 628 | if (Ops.LHS->getType()->isFloatingPoint()) |
| 629 | return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 630 | else if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 631 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 632 | else |
| 633 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 634 | } |
| 635 | |
| 636 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 637 | // 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] | 638 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 639 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 640 | else |
| 641 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 642 | } |
| 643 | |
| 644 | |
| 645 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 646 | if (!Ops.Ty->isPointerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 647 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 648 | |
| 649 | // FIXME: What about a pointer to a VLA? |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 650 | if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int |
| 651 | return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr"); |
| 652 | // int + pointer |
| 653 | return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr"); |
| 654 | } |
| 655 | |
| 656 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { |
| 657 | if (!isa<llvm::PointerType>(Ops.LHS->getType())) |
| 658 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
| 659 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 660 | // pointer - int |
| 661 | assert(!isa<llvm::PointerType>(Ops.RHS->getType()) && |
| 662 | "ptr-ptr shouldn't get here"); |
| 663 | // FIXME: The pointer could point to a VLA. |
| 664 | Value *NegatedRHS = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg"); |
| 665 | return Builder.CreateGEP(Ops.LHS, NegatedRHS, "sub.ptr"); |
| 666 | } |
| 667 | |
| 668 | Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) { |
| 669 | // "X - Y" is different from "X -= Y" in one case: when Y is a pointer. In |
| 670 | // the compound assignment case it is invalid, so just handle it here. |
| 671 | if (!E->getRHS()->getType()->isPointerType()) |
| 672 | return EmitSub(EmitBinOps(E)); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 673 | |
| 674 | // pointer - pointer |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 675 | Value *LHS = Visit(E->getLHS()); |
| 676 | Value *RHS = Visit(E->getRHS()); |
| 677 | |
| 678 | const PointerType *LHSPtrType = E->getLHS()->getType()->getAsPointerType(); |
| 679 | assert(LHSPtrType == E->getRHS()->getType()->getAsPointerType() && |
| 680 | "Can't subtract different pointer types"); |
| 681 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 682 | QualType LHSElementType = LHSPtrType->getPointeeType(); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 683 | uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType, |
| 684 | SourceLocation()) / 8; |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 685 | |
| 686 | const llvm::Type *ResultType = ConvertType(E->getType()); |
| 687 | LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); |
| 688 | RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); |
| 689 | Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 690 | |
| 691 | // HACK: LLVM doesn't have an divide instruction that 'knows' there is no |
| 692 | // remainder. As such, we handle common power-of-two cases here to generate |
| 693 | // better code. |
| 694 | if (llvm::isPowerOf2_64(ElementSize)) { |
| 695 | Value *ShAmt = |
| 696 | llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize)); |
| 697 | return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr"); |
| 698 | } |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 700 | // Otherwise, do a full sdiv. |
| 701 | Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize); |
| 702 | return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); |
| 703 | } |
| 704 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 705 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 706 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 707 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 708 | // RHS to the same size as the LHS. |
| 709 | Value *RHS = Ops.RHS; |
| 710 | if (Ops.LHS->getType() != RHS->getType()) |
| 711 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 712 | |
| 713 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 714 | } |
| 715 | |
| 716 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 717 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 718 | // RHS to the same size as the LHS. |
| 719 | Value *RHS = Ops.RHS; |
| 720 | if (Ops.LHS->getType() != RHS->getType()) |
| 721 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 722 | |
Chris Lattner | 1f1ded9 | 2007-08-24 21:00:35 +0000 | [diff] [blame] | 723 | if (Ops.Ty->isUnsignedIntegerType()) |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 724 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 725 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 726 | } |
| 727 | |
| 728 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 729 | unsigned SICmpOpc, unsigned FCmpOpc) { |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 730 | Value *Result; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 731 | QualType LHSTy = E->getLHS()->getType(); |
| 732 | if (!LHSTy->isComplexType()) { |
| 733 | Value *LHS = Visit(E->getLHS()); |
| 734 | Value *RHS = Visit(E->getRHS()); |
| 735 | |
| 736 | if (LHS->getType()->isFloatingPoint()) { |
| 737 | Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 738 | LHS, RHS, "cmp"); |
| 739 | } else if (LHSTy->isUnsignedIntegerType()) { |
| 740 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 741 | LHS, RHS, "cmp"); |
| 742 | } else { |
| 743 | // Signed integers and pointers. |
| 744 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
| 745 | LHS, RHS, "cmp"); |
| 746 | } |
| 747 | } else { |
| 748 | // Complex Comparison: can only be an equality comparison. |
| 749 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 750 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
| 751 | |
| 752 | QualType CETy = |
| 753 | cast<ComplexType>(LHSTy.getCanonicalType())->getElementType(); |
| 754 | |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 755 | Value *ResultR, *ResultI; |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 756 | if (CETy->isRealFloatingType()) { |
| 757 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 758 | LHS.first, RHS.first, "cmp.r"); |
| 759 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 760 | LHS.second, RHS.second, "cmp.i"); |
| 761 | } else { |
| 762 | // Complex comparisons can only be equality comparisons. As such, signed |
| 763 | // and unsigned opcodes are the same. |
| 764 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 765 | LHS.first, RHS.first, "cmp.r"); |
| 766 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 767 | LHS.second, RHS.second, "cmp.i"); |
| 768 | } |
| 769 | |
| 770 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 771 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 772 | } else { |
| 773 | assert(E->getOpcode() == BinaryOperator::NE && |
| 774 | "Complex comparison other than == or != ?"); |
| 775 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | // ZExt result to int. |
| 780 | return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext"); |
| 781 | } |
| 782 | |
| 783 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 784 | LValue LHS = EmitLValue(E->getLHS()); |
| 785 | Value *RHS = Visit(E->getRHS()); |
| 786 | |
| 787 | // Store the value into the LHS. |
| 788 | // FIXME: Volatility! |
| 789 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); |
| 790 | |
| 791 | // Return the RHS. |
| 792 | return RHS; |
| 793 | } |
| 794 | |
| 795 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
| 796 | Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS()); |
| 797 | |
| 798 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont"); |
| 799 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs"); |
| 800 | |
| 801 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 802 | Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock); |
| 803 | |
| 804 | CGF.EmitBlock(RHSBlock); |
| 805 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 806 | |
| 807 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 808 | RHSBlock = Builder.GetInsertBlock(); |
| 809 | CGF.EmitBlock(ContBlock); |
| 810 | |
| 811 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 812 | // false. If we evaluated both, the result is the RHS condition. |
| 813 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land"); |
| 814 | PN->reserveOperandSpace(2); |
| 815 | PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock); |
| 816 | PN->addIncoming(RHSCond, RHSBlock); |
| 817 | |
| 818 | // ZExt result to int. |
| 819 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext"); |
| 820 | } |
| 821 | |
| 822 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
| 823 | Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS()); |
| 824 | |
| 825 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont"); |
| 826 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs"); |
| 827 | |
| 828 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 829 | Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock); |
| 830 | |
| 831 | CGF.EmitBlock(RHSBlock); |
| 832 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 833 | |
| 834 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 835 | RHSBlock = Builder.GetInsertBlock(); |
| 836 | CGF.EmitBlock(ContBlock); |
| 837 | |
| 838 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 839 | // true. If we evaluated both, the result is the RHS condition. |
| 840 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor"); |
| 841 | PN->reserveOperandSpace(2); |
| 842 | PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock); |
| 843 | PN->addIncoming(RHSCond, RHSBlock); |
| 844 | |
| 845 | // ZExt result to int. |
| 846 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext"); |
| 847 | } |
| 848 | |
| 849 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 850 | CGF.EmitStmt(E->getLHS()); |
| 851 | return Visit(E->getRHS()); |
| 852 | } |
| 853 | |
| 854 | //===----------------------------------------------------------------------===// |
| 855 | // Other Operators |
| 856 | //===----------------------------------------------------------------------===// |
| 857 | |
| 858 | Value *ScalarExprEmitter:: |
| 859 | VisitConditionalOperator(const ConditionalOperator *E) { |
| 860 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 861 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 862 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 863 | |
| 864 | Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
| 865 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
| 866 | |
| 867 | CGF.EmitBlock(LHSBlock); |
| 868 | |
| 869 | // Handle the GNU extension for missing LHS. |
| 870 | Value *LHS = E->getLHS() ? Visit(E->getLHS()) : Cond; |
| 871 | Builder.CreateBr(ContBlock); |
| 872 | LHSBlock = Builder.GetInsertBlock(); |
| 873 | |
| 874 | CGF.EmitBlock(RHSBlock); |
| 875 | |
| 876 | Value *RHS = Visit(E->getRHS()); |
| 877 | Builder.CreateBr(ContBlock); |
| 878 | RHSBlock = Builder.GetInsertBlock(); |
| 879 | |
| 880 | CGF.EmitBlock(ContBlock); |
| 881 | |
| 882 | // Create a PHI node for the real part. |
| 883 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); |
| 884 | PN->reserveOperandSpace(2); |
| 885 | PN->addIncoming(LHS, LHSBlock); |
| 886 | PN->addIncoming(RHS, RHSBlock); |
| 887 | return PN; |
| 888 | } |
| 889 | |
| 890 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
| 891 | llvm::APSInt CondVal(32); |
| 892 | bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, CGF.getContext()); |
| 893 | assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst; |
| 894 | |
| 895 | // Emit the LHS or RHS as appropriate. |
| 896 | return Visit(CondVal != 0 ? E->getLHS() : E->getRHS()); |
| 897 | } |
| 898 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 899 | Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) |
| 900 | { |
| 901 | llvm::Value *ArgValue = EmitLValue(VE->getSubExpr()).getAddress(); |
| 902 | |
| 903 | llvm::Value *V = Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); |
| 904 | return V; |
| 905 | } |
| 906 | |
Chris Lattner | 7f02f72 | 2007-08-24 05:35:26 +0000 | [diff] [blame] | 907 | //===----------------------------------------------------------------------===// |
| 908 | // Entry Point into this File |
| 909 | //===----------------------------------------------------------------------===// |
| 910 | |
| 911 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 912 | /// complex type, ignoring the result. |
| 913 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E) { |
| 914 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 915 | "Invalid scalar expression to emit"); |
| 916 | |
| 917 | return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
| 918 | } |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 919 | |
| 920 | /// EmitScalarConversion - Emit a conversion from the specified type to the |
| 921 | /// specified destination type, both of which are LLVM scalar types. |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 922 | Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, |
| 923 | QualType DstTy) { |
Chris Lattner | 3707b25 | 2007-08-26 06:48:56 +0000 | [diff] [blame] | 924 | assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && |
| 925 | "Invalid scalar expression to emit"); |
| 926 | return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); |
| 927 | } |
Chris Lattner | 4f1a7b3 | 2007-08-26 16:34:22 +0000 | [diff] [blame] | 928 | |
| 929 | /// EmitComplexToScalarConversion - Emit a conversion from the specified |
| 930 | /// complex type to the specified destination type, where the destination |
| 931 | /// type is an LLVM scalar type. |
| 932 | Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, |
| 933 | QualType SrcTy, |
| 934 | QualType DstTy) { |
| 935 | assert(SrcTy->isComplexType() && !hasAggregateLLVMType(DstTy) && |
| 936 | "Invalid complex -> scalar conversion"); |
| 937 | return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, |
| 938 | DstTy); |
| 939 | } |