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