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