Chris Lattner | 9fba49a | 2007-08-24 05:35:26 +0000 | [diff] [blame^] | 1 | //===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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; |
| 31 | const BinaryOperator *E; |
| 32 | }; |
| 33 | |
| 34 | namespace { |
| 35 | class VISIBILITY_HIDDEN ScalarExprEmitter |
| 36 | : public StmtVisitor<ScalarExprEmitter, Value*> { |
| 37 | CodeGenFunction &CGF; |
| 38 | llvm::LLVMBuilder &Builder; |
| 39 | public: |
| 40 | |
| 41 | ScalarExprEmitter(CodeGenFunction &cgf) : CGF(cgf), Builder(CGF.Builder) { |
| 42 | } |
| 43 | |
| 44 | |
| 45 | //===--------------------------------------------------------------------===// |
| 46 | // Utilities |
| 47 | //===--------------------------------------------------------------------===// |
| 48 | |
| 49 | const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } |
| 50 | LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } |
| 51 | |
| 52 | Value *EmitLoadOfLValue(LValue LV, QualType T) { |
| 53 | return CGF.EmitLoadOfLValue(LV, T).getVal(); |
| 54 | } |
| 55 | |
| 56 | /// EmitLoadOfLValue - Given an expression with complex type that represents a |
| 57 | /// value l-value, this method emits the address of the l-value, then loads |
| 58 | /// and returns the result. |
| 59 | Value *EmitLoadOfLValue(const Expr *E) { |
| 60 | // FIXME: Volatile |
| 61 | return EmitLoadOfLValue(EmitLValue(E), E->getType()); |
| 62 | } |
| 63 | |
| 64 | //===--------------------------------------------------------------------===// |
| 65 | // Visitor Methods |
| 66 | //===--------------------------------------------------------------------===// |
| 67 | |
| 68 | Value *VisitStmt(Stmt *S) { |
| 69 | S->dump(); |
| 70 | assert(0 && "Stmt can't have complex result type!"); |
| 71 | return 0; |
| 72 | } |
| 73 | Value *VisitExpr(Expr *S); |
| 74 | Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); } |
| 75 | |
| 76 | // Leaves. |
| 77 | Value *VisitIntegerLiteral(const IntegerLiteral *E) { |
| 78 | return llvm::ConstantInt::get(E->getValue()); |
| 79 | } |
| 80 | Value *VisitFloatingLiteral(const FloatingLiteral *E) { |
| 81 | return llvm::ConstantFP::get(ConvertType(E->getType()), E->getValue()); |
| 82 | } |
| 83 | Value *VisitCharacterLiteral(const CharacterLiteral *E) { |
| 84 | return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); |
| 85 | } |
| 86 | Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { |
| 87 | return llvm::ConstantInt::get(ConvertType(E->getType()), |
| 88 | E->typesAreCompatible()); |
| 89 | } |
| 90 | Value *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) { |
| 91 | return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf()); |
| 92 | } |
| 93 | |
| 94 | // l-values. |
| 95 | Value *VisitDeclRefExpr(DeclRefExpr *E) { |
| 96 | if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl())) |
| 97 | return llvm::ConstantInt::get(EC->getInitVal()); |
| 98 | return EmitLoadOfLValue(E); |
| 99 | } |
| 100 | Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); |
| 101 | Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); } |
| 102 | Value *VisitOCUVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } |
| 103 | Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); } |
| 104 | Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); } |
| 105 | |
| 106 | // FIXME: CompoundLiteralExpr |
| 107 | Value *VisitImplicitCastExpr(const ImplicitCastExpr *E); |
| 108 | Value *VisitCastExpr(const CastExpr *E) { |
| 109 | return EmitCastExpr(E->getSubExpr(), E->getType()); |
| 110 | } |
| 111 | Value *EmitCastExpr(const Expr *E, QualType T); |
| 112 | |
| 113 | Value *VisitCallExpr(const CallExpr *E) { |
| 114 | return CGF.EmitCallExpr(E).getVal(); |
| 115 | } |
| 116 | |
| 117 | // Unary Operators. |
| 118 | Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre); |
| 119 | Value *VisitUnaryPostDec(const UnaryOperator *E) { |
| 120 | return VisitPrePostIncDec(E, false, false); |
| 121 | } |
| 122 | Value *VisitUnaryPostInc(const UnaryOperator *E) { |
| 123 | return VisitPrePostIncDec(E, true, false); |
| 124 | } |
| 125 | Value *VisitUnaryPreDec(const UnaryOperator *E) { |
| 126 | return VisitPrePostIncDec(E, false, true); |
| 127 | } |
| 128 | Value *VisitUnaryPreInc(const UnaryOperator *E) { |
| 129 | return VisitPrePostIncDec(E, true, true); |
| 130 | } |
| 131 | Value *VisitUnaryAddrOf(const UnaryOperator *E) { |
| 132 | return EmitLValue(E->getSubExpr()).getAddress(); |
| 133 | } |
| 134 | Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } |
| 135 | Value *VisitUnaryPlus(const UnaryOperator *E) { |
| 136 | return Visit(E->getSubExpr()); |
| 137 | } |
| 138 | Value *VisitUnaryMinus (const UnaryOperator *E); |
| 139 | Value *VisitUnaryNot (const UnaryOperator *E); |
| 140 | Value *VisitUnaryLNot (const UnaryOperator *E); |
| 141 | Value *VisitUnarySizeOf (const UnaryOperator *E) { |
| 142 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true); |
| 143 | } |
| 144 | Value *VisitUnaryAlignOf (const UnaryOperator *E) { |
| 145 | return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false); |
| 146 | } |
| 147 | Value *EmitSizeAlignOf(QualType TypeToSize, QualType RetType, |
| 148 | bool isSizeOf); |
| 149 | // FIXME: Real,Imag. |
| 150 | Value *VisitUnaryExtension(const UnaryOperator *E) { |
| 151 | return Visit(E->getSubExpr()); |
| 152 | } |
| 153 | |
| 154 | // Binary Operators. |
| 155 | BinOpInfo EmitBinOps(const BinaryOperator *E); |
| 156 | Value *VisitBinMul(const BinaryOperator *E) { return EmitMul(EmitBinOps(E)); } |
| 157 | Value *VisitBinDiv(const BinaryOperator *E) { return EmitDiv(EmitBinOps(E)); } |
| 158 | Value *VisitBinRem(const BinaryOperator *E) { return EmitRem(EmitBinOps(E)); } |
| 159 | Value *VisitBinAdd(const BinaryOperator *E) { return EmitAdd(EmitBinOps(E)); } |
| 160 | Value *VisitBinSub(const BinaryOperator *E) { return EmitSub(EmitBinOps(E)); } |
| 161 | Value *VisitBinShl(const BinaryOperator *E) { return EmitShl(EmitBinOps(E)); } |
| 162 | Value *VisitBinShr(const BinaryOperator *E) { return EmitShr(EmitBinOps(E)); } |
| 163 | Value *VisitBinAnd(const BinaryOperator *E) { return EmitAnd(EmitBinOps(E)); } |
| 164 | Value *VisitBinXor(const BinaryOperator *E) { return EmitXor(EmitBinOps(E)); } |
| 165 | Value *VisitBinOr (const BinaryOperator *E) { return EmitOr (EmitBinOps(E)); } |
| 166 | |
| 167 | Value *EmitMul(const BinOpInfo &Ops) { |
| 168 | return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); |
| 169 | } |
| 170 | Value *EmitDiv(const BinOpInfo &Ops); |
| 171 | Value *EmitRem(const BinOpInfo &Ops); |
| 172 | Value *EmitAdd(const BinOpInfo &Ops); |
| 173 | Value *EmitSub(const BinOpInfo &Ops); |
| 174 | Value *EmitShl(const BinOpInfo &Ops); |
| 175 | Value *EmitShr(const BinOpInfo &Ops); |
| 176 | Value *EmitAnd(const BinOpInfo &Ops) { |
| 177 | return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); |
| 178 | } |
| 179 | Value *EmitXor(const BinOpInfo &Ops) { |
| 180 | return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); |
| 181 | } |
| 182 | Value *EmitOr (const BinOpInfo &Ops) { |
| 183 | return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); |
| 184 | } |
| 185 | |
| 186 | // Comparisons. |
| 187 | Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, |
| 188 | unsigned SICmpOpc, unsigned FCmpOpc); |
| 189 | #define VISITCOMP(CODE, UI, SI, FP) \ |
| 190 | Value *VisitBin##CODE(const BinaryOperator *E) { \ |
| 191 | return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ |
| 192 | llvm::FCmpInst::FP); } |
| 193 | VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT); |
| 194 | VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT); |
| 195 | VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE); |
| 196 | VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE); |
| 197 | VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ); |
| 198 | VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE); |
| 199 | #undef VISITCOMP |
| 200 | |
| 201 | Value *VisitBinAssign (const BinaryOperator *E); |
| 202 | |
| 203 | Value *VisitBinLAnd (const BinaryOperator *E); |
| 204 | Value *VisitBinLOr (const BinaryOperator *E); |
| 205 | |
| 206 | // FIXME: Compound assignment operators. |
| 207 | Value *VisitBinComma (const BinaryOperator *E); |
| 208 | |
| 209 | // Other Operators. |
| 210 | Value *VisitConditionalOperator(const ConditionalOperator *CO); |
| 211 | Value *VisitChooseExpr(ChooseExpr *CE); |
| 212 | Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 213 | return CGF.EmitObjCStringLiteral(E); |
| 214 | } |
| 215 | }; |
| 216 | } // end anonymous namespace. |
| 217 | |
| 218 | //===----------------------------------------------------------------------===// |
| 219 | // Utilities |
| 220 | //===----------------------------------------------------------------------===// |
| 221 | |
| 222 | //===----------------------------------------------------------------------===// |
| 223 | // Visitor Methods |
| 224 | //===----------------------------------------------------------------------===// |
| 225 | |
| 226 | Value *ScalarExprEmitter::VisitExpr(Expr *E) { |
| 227 | fprintf(stderr, "Unimplemented scalar expr!\n"); |
| 228 | E->dump(); |
| 229 | if (E->getType()->isVoidType()) |
| 230 | return 0; |
| 231 | return llvm::UndefValue::get(CGF.ConvertType(E->getType())); |
| 232 | } |
| 233 | |
| 234 | Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { |
| 235 | // Emit subscript expressions in rvalue context's. For most cases, this just |
| 236 | // loads the lvalue formed by the subscript expr. However, we have to be |
| 237 | // careful, because the base of a vector subscript is occasionally an rvalue, |
| 238 | // so we can't get it as an lvalue. |
| 239 | if (!E->getBase()->getType()->isVectorType()) |
| 240 | return EmitLoadOfLValue(E); |
| 241 | |
| 242 | // Handle the vector case. The base must be a vector, the index must be an |
| 243 | // integer value. |
| 244 | Value *Base = Visit(E->getBase()); |
| 245 | Value *Idx = Visit(E->getIdx()); |
| 246 | |
| 247 | // FIXME: Convert Idx to i32 type. |
| 248 | return Builder.CreateExtractElement(Base, Idx, "vecext"); |
| 249 | } |
| 250 | |
| 251 | /// VisitImplicitCastExpr - Implicit casts are the same as normal casts, but |
| 252 | /// also handle things like function to pointer-to-function decay, and array to |
| 253 | /// pointer decay. |
| 254 | Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) { |
| 255 | const Expr *Op = E->getSubExpr(); |
| 256 | |
| 257 | // If this is due to array->pointer conversion, emit the array expression as |
| 258 | // an l-value. |
| 259 | if (Op->getType()->isArrayType()) { |
| 260 | // FIXME: For now we assume that all source arrays map to LLVM arrays. This |
| 261 | // will not true when we add support for VLAs. |
| 262 | llvm::Value *V = EmitLValue(Op).getAddress(); // Bitfields can't be arrays. |
| 263 | |
| 264 | assert(isa<llvm::PointerType>(V->getType()) && |
| 265 | isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) |
| 266 | ->getElementType()) && |
| 267 | "Doesn't support VLAs yet!"); |
| 268 | llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0); |
| 269 | return Builder.CreateGEP(V, Idx0, Idx0, "arraydecay"); |
| 270 | } |
| 271 | |
| 272 | return EmitCastExpr(Op, E->getType()); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts |
| 277 | // have to handle a more broad range of conversions than explicit casts, as they |
| 278 | // handle things like function to ptr-to-function decay etc. |
| 279 | Value *ScalarExprEmitter::EmitCastExpr(const Expr *E, QualType DestTy) { |
| 280 | RValue Src = CGF.EmitAnyExpr(E); |
| 281 | |
| 282 | // If the destination is void, just evaluate the source. |
| 283 | if (DestTy->isVoidType()) |
| 284 | return 0; |
| 285 | |
| 286 | // FIXME: Refactor EmitConversion to not return an RValue. Sink it into this |
| 287 | // method. |
| 288 | return CGF.EmitConversion(Src, E->getType(), DestTy).getVal(); |
| 289 | } |
| 290 | |
| 291 | //===----------------------------------------------------------------------===// |
| 292 | // Unary Operators |
| 293 | //===----------------------------------------------------------------------===// |
| 294 | |
| 295 | Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, |
| 296 | bool isInc, bool isPre) { |
| 297 | LValue LV = EmitLValue(E->getSubExpr()); |
| 298 | // FIXME: Handle volatile! |
| 299 | Value *InVal = CGF.EmitLoadOfLValue(LV/* false*/, |
| 300 | E->getSubExpr()->getType()).getVal(); |
| 301 | |
| 302 | int AmountVal = isInc ? 1 : -1; |
| 303 | |
| 304 | Value *NextVal; |
| 305 | if (isa<llvm::IntegerType>(InVal->getType())) |
| 306 | NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); |
| 307 | else |
| 308 | NextVal = llvm::ConstantFP::get(InVal->getType(), AmountVal); |
| 309 | |
| 310 | // Add the inc/dec to the real part. |
| 311 | NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); |
| 312 | |
| 313 | // Store the updated result through the lvalue. |
| 314 | CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, |
| 315 | E->getSubExpr()->getType()); |
| 316 | |
| 317 | // If this is a postinc, return the value read from memory, otherwise use the |
| 318 | // updated value. |
| 319 | return isPre ? NextVal : InVal; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { |
| 324 | Value *Op = Visit(E->getSubExpr()); |
| 325 | return Builder.CreateNeg(Op, "neg"); |
| 326 | } |
| 327 | |
| 328 | Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { |
| 329 | Value *Op = Visit(E->getSubExpr()); |
| 330 | return Builder.CreateNot(Op, "neg"); |
| 331 | } |
| 332 | |
| 333 | Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { |
| 334 | // Compare operand to zero. |
| 335 | Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); |
| 336 | |
| 337 | // Invert value. |
| 338 | // TODO: Could dynamically modify easy computations here. For example, if |
| 339 | // the operand is an icmp ne, turn into icmp eq. |
| 340 | BoolVal = Builder.CreateNot(BoolVal, "lnot"); |
| 341 | |
| 342 | // ZExt result to int. |
| 343 | return Builder.CreateZExt(BoolVal, CGF.LLVMIntTy, "lnot.ext"); |
| 344 | } |
| 345 | |
| 346 | /// EmitSizeAlignOf - Return the size or alignment of the 'TypeToSize' type as |
| 347 | /// an integer (RetType). |
| 348 | Value *ScalarExprEmitter::EmitSizeAlignOf(QualType TypeToSize, |
| 349 | QualType RetType,bool isSizeOf){ |
| 350 | /// FIXME: This doesn't handle VLAs yet! |
| 351 | std::pair<uint64_t, unsigned> Info = |
| 352 | CGF.getContext().getTypeInfo(TypeToSize, SourceLocation()); |
| 353 | |
| 354 | uint64_t Val = isSizeOf ? Info.first : Info.second; |
| 355 | Val /= 8; // Return size in bytes, not bits. |
| 356 | |
| 357 | assert(RetType->isIntegerType() && "Result type must be an integer!"); |
| 358 | |
| 359 | unsigned ResultWidth = CGF.getContext().getTypeSize(RetType,SourceLocation()); |
| 360 | return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val)); |
| 361 | } |
| 362 | |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | // Binary Operators |
| 365 | //===----------------------------------------------------------------------===// |
| 366 | |
| 367 | BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { |
| 368 | BinOpInfo Result; |
| 369 | Result.LHS = Visit(E->getLHS()); |
| 370 | Result.RHS = Visit(E->getRHS()); |
| 371 | Result.E = E; |
| 372 | return Result; |
| 373 | } |
| 374 | |
| 375 | Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { |
| 376 | if (Ops.LHS->getType()->isFloatingPoint()) |
| 377 | return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); |
| 378 | else if (Ops.E->getType()->isUnsignedIntegerType()) |
| 379 | return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); |
| 380 | else |
| 381 | return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); |
| 382 | } |
| 383 | |
| 384 | Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { |
| 385 | // Rem in C can't be a floating point type: C99 6.5.5p2. |
| 386 | if (Ops.E->getType()->isUnsignedIntegerType()) |
| 387 | return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); |
| 388 | else |
| 389 | return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); |
| 390 | } |
| 391 | |
| 392 | |
| 393 | Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { |
| 394 | if (!Ops.E->getType()->isPointerType()) |
| 395 | return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); |
| 396 | if (isa<llvm::PointerType>(Ops.LHS->getType())) // pointer + int |
| 397 | return Builder.CreateGEP(Ops.LHS, Ops.RHS, "add.ptr"); |
| 398 | // int + pointer |
| 399 | return Builder.CreateGEP(Ops.RHS, Ops.LHS, "add.ptr"); |
| 400 | } |
| 401 | |
| 402 | Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { |
| 403 | if (!isa<llvm::PointerType>(Ops.LHS->getType())) |
| 404 | return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); |
| 405 | |
| 406 | // FIXME: This isn't right for -=. |
| 407 | QualType LHSTy = Ops.E->getLHS()->getType(); |
| 408 | QualType RHSTy = Ops.E->getRHS()->getType(); |
| 409 | |
| 410 | const PointerType *RHSPtrType = dyn_cast<PointerType>(RHSTy.getTypePtr()); |
| 411 | if (RHSPtrType == 0) { // pointer - int |
| 412 | Value *NegatedRHS = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg"); |
| 413 | return Builder.CreateGEP(Ops.LHS, NegatedRHS, "sub.ptr"); |
| 414 | } |
| 415 | |
| 416 | // pointer - pointer |
| 417 | const PointerType *LHSPtrType = cast<PointerType>(LHSTy.getTypePtr()); |
| 418 | QualType LHSElementType = LHSPtrType->getPointeeType(); |
| 419 | assert(LHSElementType == RHSPtrType->getPointeeType() && |
| 420 | "can't subtract pointers with differing element types"); |
| 421 | uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType, |
| 422 | SourceLocation()) / 8; |
| 423 | const llvm::Type *ResultType = ConvertType(Ops.E->getType()); |
| 424 | Value *CastLHS = Builder.CreatePtrToInt(Ops.LHS, ResultType, |
| 425 | "sub.ptr.lhs.cast"); |
| 426 | Value *CastRHS = Builder.CreatePtrToInt(Ops.RHS, ResultType, |
| 427 | "sub.ptr.rhs.cast"); |
| 428 | Value *BytesBetween = Builder.CreateSub(CastLHS, CastRHS, |
| 429 | "sub.ptr.sub"); |
| 430 | |
| 431 | // HACK: LLVM doesn't have an divide instruction that 'knows' there is no |
| 432 | // remainder. As such, we handle common power-of-two cases here to generate |
| 433 | // better code. |
| 434 | if (llvm::isPowerOf2_64(ElementSize)) { |
| 435 | Value *ShAmt = |
| 436 | llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize)); |
| 437 | return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr"); |
| 438 | } |
| 439 | // Otherwise, do a full sdiv. |
| 440 | Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize); |
| 441 | return Builder.CreateSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); |
| 442 | } |
| 443 | |
| 444 | Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { |
| 445 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 446 | // RHS to the same size as the LHS. |
| 447 | Value *RHS = Ops.RHS; |
| 448 | if (Ops.LHS->getType() != RHS->getType()) |
| 449 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 450 | |
| 451 | return Builder.CreateShl(Ops.LHS, RHS, "shl"); |
| 452 | } |
| 453 | |
| 454 | Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { |
| 455 | // LLVM requires the LHS and RHS to be the same type: promote or truncate the |
| 456 | // RHS to the same size as the LHS. |
| 457 | Value *RHS = Ops.RHS; |
| 458 | if (Ops.LHS->getType() != RHS->getType()) |
| 459 | RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); |
| 460 | |
| 461 | if (Ops.E->getType()->isUnsignedIntegerType()) |
| 462 | return Builder.CreateLShr(Ops.LHS, RHS, "shr"); |
| 463 | return Builder.CreateAShr(Ops.LHS, RHS, "shr"); |
| 464 | } |
| 465 | |
| 466 | Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, |
| 467 | unsigned SICmpOpc, unsigned FCmpOpc) { |
| 468 | llvm::Value *Result; |
| 469 | QualType LHSTy = E->getLHS()->getType(); |
| 470 | if (!LHSTy->isComplexType()) { |
| 471 | Value *LHS = Visit(E->getLHS()); |
| 472 | Value *RHS = Visit(E->getRHS()); |
| 473 | |
| 474 | if (LHS->getType()->isFloatingPoint()) { |
| 475 | Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 476 | LHS, RHS, "cmp"); |
| 477 | } else if (LHSTy->isUnsignedIntegerType()) { |
| 478 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 479 | LHS, RHS, "cmp"); |
| 480 | } else { |
| 481 | // Signed integers and pointers. |
| 482 | Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, |
| 483 | LHS, RHS, "cmp"); |
| 484 | } |
| 485 | } else { |
| 486 | // Complex Comparison: can only be an equality comparison. |
| 487 | CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); |
| 488 | CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); |
| 489 | |
| 490 | QualType CETy = |
| 491 | cast<ComplexType>(LHSTy.getCanonicalType())->getElementType(); |
| 492 | |
| 493 | llvm::Value *ResultR, *ResultI; |
| 494 | if (CETy->isRealFloatingType()) { |
| 495 | ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 496 | LHS.first, RHS.first, "cmp.r"); |
| 497 | ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, |
| 498 | LHS.second, RHS.second, "cmp.i"); |
| 499 | } else { |
| 500 | // Complex comparisons can only be equality comparisons. As such, signed |
| 501 | // and unsigned opcodes are the same. |
| 502 | ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 503 | LHS.first, RHS.first, "cmp.r"); |
| 504 | ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, |
| 505 | LHS.second, RHS.second, "cmp.i"); |
| 506 | } |
| 507 | |
| 508 | if (E->getOpcode() == BinaryOperator::EQ) { |
| 509 | Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); |
| 510 | } else { |
| 511 | assert(E->getOpcode() == BinaryOperator::NE && |
| 512 | "Complex comparison other than == or != ?"); |
| 513 | Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | // ZExt result to int. |
| 518 | return Builder.CreateZExt(Result, CGF.LLVMIntTy, "cmp.ext"); |
| 519 | } |
| 520 | |
| 521 | Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { |
| 522 | LValue LHS = EmitLValue(E->getLHS()); |
| 523 | Value *RHS = Visit(E->getRHS()); |
| 524 | |
| 525 | // Store the value into the LHS. |
| 526 | // FIXME: Volatility! |
| 527 | CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); |
| 528 | |
| 529 | // Return the RHS. |
| 530 | return RHS; |
| 531 | } |
| 532 | |
| 533 | Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { |
| 534 | Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS()); |
| 535 | |
| 536 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("land_cont"); |
| 537 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("land_rhs"); |
| 538 | |
| 539 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 540 | Builder.CreateCondBr(LHSCond, RHSBlock, ContBlock); |
| 541 | |
| 542 | CGF.EmitBlock(RHSBlock); |
| 543 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 544 | |
| 545 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 546 | RHSBlock = Builder.GetInsertBlock(); |
| 547 | CGF.EmitBlock(ContBlock); |
| 548 | |
| 549 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 550 | // false. If we evaluated both, the result is the RHS condition. |
| 551 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "land"); |
| 552 | PN->reserveOperandSpace(2); |
| 553 | PN->addIncoming(llvm::ConstantInt::getFalse(), OrigBlock); |
| 554 | PN->addIncoming(RHSCond, RHSBlock); |
| 555 | |
| 556 | // ZExt result to int. |
| 557 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext"); |
| 558 | } |
| 559 | |
| 560 | Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { |
| 561 | Value *LHSCond = CGF.EvaluateExprAsBool(E->getLHS()); |
| 562 | |
| 563 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("lor_cont"); |
| 564 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("lor_rhs"); |
| 565 | |
| 566 | llvm::BasicBlock *OrigBlock = Builder.GetInsertBlock(); |
| 567 | Builder.CreateCondBr(LHSCond, ContBlock, RHSBlock); |
| 568 | |
| 569 | CGF.EmitBlock(RHSBlock); |
| 570 | Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); |
| 571 | |
| 572 | // Reaquire the RHS block, as there may be subblocks inserted. |
| 573 | RHSBlock = Builder.GetInsertBlock(); |
| 574 | CGF.EmitBlock(ContBlock); |
| 575 | |
| 576 | // Create a PHI node. If we just evaluted the LHS condition, the result is |
| 577 | // true. If we evaluated both, the result is the RHS condition. |
| 578 | llvm::PHINode *PN = Builder.CreatePHI(llvm::Type::Int1Ty, "lor"); |
| 579 | PN->reserveOperandSpace(2); |
| 580 | PN->addIncoming(llvm::ConstantInt::getTrue(), OrigBlock); |
| 581 | PN->addIncoming(RHSCond, RHSBlock); |
| 582 | |
| 583 | // ZExt result to int. |
| 584 | return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext"); |
| 585 | } |
| 586 | |
| 587 | Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { |
| 588 | CGF.EmitStmt(E->getLHS()); |
| 589 | return Visit(E->getRHS()); |
| 590 | } |
| 591 | |
| 592 | //===----------------------------------------------------------------------===// |
| 593 | // Other Operators |
| 594 | //===----------------------------------------------------------------------===// |
| 595 | |
| 596 | Value *ScalarExprEmitter:: |
| 597 | VisitConditionalOperator(const ConditionalOperator *E) { |
| 598 | llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?"); |
| 599 | llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:"); |
| 600 | llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont"); |
| 601 | |
| 602 | Value *Cond = CGF.EvaluateExprAsBool(E->getCond()); |
| 603 | Builder.CreateCondBr(Cond, LHSBlock, RHSBlock); |
| 604 | |
| 605 | CGF.EmitBlock(LHSBlock); |
| 606 | |
| 607 | // Handle the GNU extension for missing LHS. |
| 608 | Value *LHS = E->getLHS() ? Visit(E->getLHS()) : Cond; |
| 609 | Builder.CreateBr(ContBlock); |
| 610 | LHSBlock = Builder.GetInsertBlock(); |
| 611 | |
| 612 | CGF.EmitBlock(RHSBlock); |
| 613 | |
| 614 | Value *RHS = Visit(E->getRHS()); |
| 615 | Builder.CreateBr(ContBlock); |
| 616 | RHSBlock = Builder.GetInsertBlock(); |
| 617 | |
| 618 | CGF.EmitBlock(ContBlock); |
| 619 | |
| 620 | // Create a PHI node for the real part. |
| 621 | llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); |
| 622 | PN->reserveOperandSpace(2); |
| 623 | PN->addIncoming(LHS, LHSBlock); |
| 624 | PN->addIncoming(RHS, RHSBlock); |
| 625 | return PN; |
| 626 | } |
| 627 | |
| 628 | Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { |
| 629 | llvm::APSInt CondVal(32); |
| 630 | bool IsConst = E->getCond()->isIntegerConstantExpr(CondVal, CGF.getContext()); |
| 631 | assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst; |
| 632 | |
| 633 | // Emit the LHS or RHS as appropriate. |
| 634 | return Visit(CondVal != 0 ? E->getLHS() : E->getRHS()); |
| 635 | } |
| 636 | |
| 637 | //===----------------------------------------------------------------------===// |
| 638 | // Entry Point into this File |
| 639 | //===----------------------------------------------------------------------===// |
| 640 | |
| 641 | /// EmitComplexExpr - Emit the computation of the specified expression of |
| 642 | /// complex type, ignoring the result. |
| 643 | Value *CodeGenFunction::EmitScalarExpr(const Expr *E) { |
| 644 | assert(E && !hasAggregateLLVMType(E->getType()) && |
| 645 | "Invalid scalar expression to emit"); |
| 646 | |
| 647 | return ScalarExprEmitter(*this).Visit(const_cast<Expr*>(E)); |
| 648 | } |