CGExprScalar.cpp revision 8bfd31f9dad09cd52225d868bbd92a9bebe87775
1//===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// 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 "CGObjCRuntime.h" 16#include "CodeGenModule.h" 17#include "clang/AST/ASTContext.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/RecordLayout.h" 20#include "clang/AST/StmtVisitor.h" 21#include "clang/Basic/TargetInfo.h" 22#include "llvm/Constants.h" 23#include "llvm/Function.h" 24#include "llvm/GlobalVariable.h" 25#include "llvm/Intrinsics.h" 26#include "llvm/Module.h" 27#include "llvm/Support/Compiler.h" 28#include "llvm/Support/CFG.h" 29#include "llvm/Target/TargetData.h" 30#include <cstdarg> 31 32using namespace clang; 33using namespace CodeGen; 34using llvm::Value; 35 36//===----------------------------------------------------------------------===// 37// Scalar Expression Emitter 38//===----------------------------------------------------------------------===// 39 40struct BinOpInfo { 41 Value *LHS; 42 Value *RHS; 43 QualType Ty; // Computation Type. 44 const BinaryOperator *E; 45}; 46 47namespace { 48class VISIBILITY_HIDDEN ScalarExprEmitter 49 : public StmtVisitor<ScalarExprEmitter, Value*> { 50 CodeGenFunction &CGF; 51 CGBuilderTy &Builder; 52 bool IgnoreResultAssign; 53 llvm::LLVMContext &VMContext; 54public: 55 56 ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false) 57 : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira), 58 VMContext(cgf.getLLVMContext()) { 59 } 60 61 //===--------------------------------------------------------------------===// 62 // Utilities 63 //===--------------------------------------------------------------------===// 64 65 bool TestAndClearIgnoreResultAssign() { 66 bool I = IgnoreResultAssign; 67 IgnoreResultAssign = false; 68 return I; 69 } 70 71 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } 72 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } 73 74 Value *EmitLoadOfLValue(LValue LV, QualType T) { 75 return CGF.EmitLoadOfLValue(LV, T).getScalarVal(); 76 } 77 78 /// EmitLoadOfLValue - Given an expression with complex type that represents a 79 /// value l-value, this method emits the address of the l-value, then loads 80 /// and returns the result. 81 Value *EmitLoadOfLValue(const Expr *E) { 82 return EmitLoadOfLValue(EmitLValue(E), E->getType()); 83 } 84 85 /// EmitConversionToBool - Convert the specified expression value to a 86 /// boolean (i1) truth value. This is equivalent to "Val != 0". 87 Value *EmitConversionToBool(Value *Src, QualType DstTy); 88 89 /// EmitScalarConversion - Emit a conversion from the specified type to the 90 /// specified destination type, both of which are LLVM scalar types. 91 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy); 92 93 /// EmitComplexToScalarConversion - Emit a conversion from the specified 94 /// complex type to the specified destination type, where the destination type 95 /// is an LLVM scalar type. 96 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, 97 QualType SrcTy, QualType DstTy); 98 99 //===--------------------------------------------------------------------===// 100 // Visitor Methods 101 //===--------------------------------------------------------------------===// 102 103 Value *VisitStmt(Stmt *S) { 104 S->dump(CGF.getContext().getSourceManager()); 105 assert(0 && "Stmt can't have complex result type!"); 106 return 0; 107 } 108 Value *VisitExpr(Expr *S); 109 110 Value *VisitParenExpr(ParenExpr *PE) { return Visit(PE->getSubExpr()); } 111 112 // Leaves. 113 Value *VisitIntegerLiteral(const IntegerLiteral *E) { 114 return llvm::ConstantInt::get(VMContext, E->getValue()); 115 } 116 Value *VisitFloatingLiteral(const FloatingLiteral *E) { 117 return llvm::ConstantFP::get(VMContext, E->getValue()); 118 } 119 Value *VisitCharacterLiteral(const CharacterLiteral *E) { 120 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); 121 } 122 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { 123 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); 124 } 125 Value *VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) { 126 return llvm::Constant::getNullValue(ConvertType(E->getType())); 127 } 128 Value *VisitGNUNullExpr(const GNUNullExpr *E) { 129 return llvm::Constant::getNullValue(ConvertType(E->getType())); 130 } 131 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { 132 return llvm::ConstantInt::get(ConvertType(E->getType()), 133 CGF.getContext().typesAreCompatible( 134 E->getArgType1(), E->getArgType2())); 135 } 136 Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); 137 Value *VisitAddrLabelExpr(const AddrLabelExpr *E) { 138 llvm::Value *V = 139 llvm::ConstantInt::get(llvm::Type::getInt32Ty(CGF.getLLVMContext()), 140 CGF.GetIDForAddrOfLabel(E->getLabel())); 141 142 return Builder.CreateIntToPtr(V, ConvertType(E->getType())); 143 } 144 145 // l-values. 146 Value *VisitDeclRefExpr(DeclRefExpr *E) { 147 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl())) 148 return llvm::ConstantInt::get(VMContext, EC->getInitVal()); 149 return EmitLoadOfLValue(E); 150 } 151 Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) { 152 return CGF.EmitObjCSelectorExpr(E); 153 } 154 Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) { 155 return CGF.EmitObjCProtocolExpr(E); 156 } 157 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 158 return EmitLoadOfLValue(E); 159 } 160 Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { 161 return EmitLoadOfLValue(E); 162 } 163 Value *VisitObjCImplicitSetterGetterRefExpr( 164 ObjCImplicitSetterGetterRefExpr *E) { 165 return EmitLoadOfLValue(E); 166 } 167 Value *VisitObjCMessageExpr(ObjCMessageExpr *E) { 168 return CGF.EmitObjCMessageExpr(E).getScalarVal(); 169 } 170 171 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); 172 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E); 173 Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); } 174 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } 175 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 176 return EmitLoadOfLValue(E); 177 } 178 Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); } 179 Value *VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { 180 return EmitLValue(E).getAddress(); 181 } 182 183 Value *VisitPredefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); } 184 185 Value *VisitInitListExpr(InitListExpr *E); 186 187 Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { 188 return llvm::Constant::getNullValue(ConvertType(E->getType())); 189 } 190 Value *VisitCastExpr(const CastExpr *E) { 191 // Make sure to evaluate VLA bounds now so that we have them for later. 192 if (E->getType()->isVariablyModifiedType()) 193 CGF.EmitVLASize(E->getType()); 194 195 return EmitCastExpr(E); 196 } 197 Value *EmitCastExpr(const CastExpr *E); 198 199 Value *VisitCallExpr(const CallExpr *E) { 200 if (E->getCallReturnType()->isReferenceType()) 201 return EmitLoadOfLValue(E); 202 203 return CGF.EmitCallExpr(E).getScalarVal(); 204 } 205 206 Value *VisitStmtExpr(const StmtExpr *E); 207 208 Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E); 209 210 // Unary Operators. 211 Value *VisitPrePostIncDec(const UnaryOperator *E, bool isInc, bool isPre); 212 Value *VisitUnaryPostDec(const UnaryOperator *E) { 213 return VisitPrePostIncDec(E, false, false); 214 } 215 Value *VisitUnaryPostInc(const UnaryOperator *E) { 216 return VisitPrePostIncDec(E, true, false); 217 } 218 Value *VisitUnaryPreDec(const UnaryOperator *E) { 219 return VisitPrePostIncDec(E, false, true); 220 } 221 Value *VisitUnaryPreInc(const UnaryOperator *E) { 222 return VisitPrePostIncDec(E, true, true); 223 } 224 Value *VisitUnaryAddrOf(const UnaryOperator *E) { 225 return EmitLValue(E->getSubExpr()).getAddress(); 226 } 227 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } 228 Value *VisitUnaryPlus(const UnaryOperator *E) { 229 // This differs from gcc, though, most likely due to a bug in gcc. 230 TestAndClearIgnoreResultAssign(); 231 return Visit(E->getSubExpr()); 232 } 233 Value *VisitUnaryMinus (const UnaryOperator *E); 234 Value *VisitUnaryNot (const UnaryOperator *E); 235 Value *VisitUnaryLNot (const UnaryOperator *E); 236 Value *VisitUnaryReal (const UnaryOperator *E); 237 Value *VisitUnaryImag (const UnaryOperator *E); 238 Value *VisitUnaryExtension(const UnaryOperator *E) { 239 return Visit(E->getSubExpr()); 240 } 241 Value *VisitUnaryOffsetOf(const UnaryOperator *E); 242 243 // C++ 244 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 245 return Visit(DAE->getExpr()); 246 } 247 Value *VisitCXXThisExpr(CXXThisExpr *TE) { 248 return CGF.LoadCXXThis(); 249 } 250 251 Value *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { 252 return CGF.EmitCXXExprWithTemporaries(E).getScalarVal(); 253 } 254 Value *VisitCXXNewExpr(const CXXNewExpr *E) { 255 return CGF.EmitCXXNewExpr(E); 256 } 257 Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) { 258 CGF.EmitCXXDeleteExpr(E); 259 return 0; 260 } 261 262 Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) { 263 // C++ [expr.pseudo]p1: 264 // The result shall only be used as the operand for the function call 265 // operator (), and the result of such a call has type void. The only 266 // effect is the evaluation of the postfix-expression before the dot or 267 // arrow. 268 CGF.EmitScalarExpr(E->getBase()); 269 return 0; 270 } 271 272 Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { 273 return llvm::Constant::getNullValue(ConvertType(E->getType())); 274 } 275 276 // Binary Operators. 277 Value *EmitMul(const BinOpInfo &Ops) { 278 if (CGF.getContext().getLangOptions().OverflowChecking 279 && Ops.Ty->isSignedIntegerType()) 280 return EmitOverflowCheckedBinOp(Ops); 281 if (Ops.LHS->getType()->isFPOrFPVector()) 282 return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul"); 283 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); 284 } 285 /// Create a binary op that checks for overflow. 286 /// Currently only supports +, - and *. 287 Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops); 288 Value *EmitDiv(const BinOpInfo &Ops); 289 Value *EmitRem(const BinOpInfo &Ops); 290 Value *EmitAdd(const BinOpInfo &Ops); 291 Value *EmitSub(const BinOpInfo &Ops); 292 Value *EmitShl(const BinOpInfo &Ops); 293 Value *EmitShr(const BinOpInfo &Ops); 294 Value *EmitAnd(const BinOpInfo &Ops) { 295 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); 296 } 297 Value *EmitXor(const BinOpInfo &Ops) { 298 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); 299 } 300 Value *EmitOr (const BinOpInfo &Ops) { 301 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); 302 } 303 304 BinOpInfo EmitBinOps(const BinaryOperator *E); 305 Value *EmitCompoundAssign(const CompoundAssignOperator *E, 306 Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); 307 308 // Binary operators and binary compound assignment operators. 309#define HANDLEBINOP(OP) \ 310 Value *VisitBin ## OP(const BinaryOperator *E) { \ 311 return Emit ## OP(EmitBinOps(E)); \ 312 } \ 313 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ 314 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ 315 } 316 HANDLEBINOP(Mul); 317 HANDLEBINOP(Div); 318 HANDLEBINOP(Rem); 319 HANDLEBINOP(Add); 320 HANDLEBINOP(Sub); 321 HANDLEBINOP(Shl); 322 HANDLEBINOP(Shr); 323 HANDLEBINOP(And); 324 HANDLEBINOP(Xor); 325 HANDLEBINOP(Or); 326#undef HANDLEBINOP 327 328 // Comparisons. 329 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, 330 unsigned SICmpOpc, unsigned FCmpOpc); 331#define VISITCOMP(CODE, UI, SI, FP) \ 332 Value *VisitBin##CODE(const BinaryOperator *E) { \ 333 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ 334 llvm::FCmpInst::FP); } 335 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT); 336 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT); 337 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE); 338 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE); 339 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ); 340 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE); 341#undef VISITCOMP 342 343 Value *VisitBinAssign (const BinaryOperator *E); 344 345 Value *VisitBinLAnd (const BinaryOperator *E); 346 Value *VisitBinLOr (const BinaryOperator *E); 347 Value *VisitBinComma (const BinaryOperator *E); 348 349 // Other Operators. 350 Value *VisitBlockExpr(const BlockExpr *BE); 351 Value *VisitConditionalOperator(const ConditionalOperator *CO); 352 Value *VisitChooseExpr(ChooseExpr *CE); 353 Value *VisitVAArgExpr(VAArgExpr *VE); 354 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { 355 return CGF.EmitObjCStringLiteral(E); 356 } 357}; 358} // end anonymous namespace. 359 360//===----------------------------------------------------------------------===// 361// Utilities 362//===----------------------------------------------------------------------===// 363 364/// EmitConversionToBool - Convert the specified expression value to a 365/// boolean (i1) truth value. This is equivalent to "Val != 0". 366Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { 367 assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs"); 368 369 if (SrcType->isRealFloatingType()) { 370 // Compare against 0.0 for fp scalars. 371 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); 372 return Builder.CreateFCmpUNE(Src, Zero, "tobool"); 373 } 374 375 if (SrcType->isMemberPointerType()) { 376 // FIXME: This is ABI specific. 377 378 // Compare against -1. 379 llvm::Value *NegativeOne = llvm::Constant::getAllOnesValue(Src->getType()); 380 return Builder.CreateICmpNE(Src, NegativeOne, "tobool"); 381 } 382 383 assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) && 384 "Unknown scalar type to convert"); 385 386 // Because of the type rules of C, we often end up computing a logical value, 387 // then zero extending it to int, then wanting it as a logical value again. 388 // Optimize this common case. 389 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) { 390 if (ZI->getOperand(0)->getType() == 391 llvm::Type::getInt1Ty(CGF.getLLVMContext())) { 392 Value *Result = ZI->getOperand(0); 393 // If there aren't any more uses, zap the instruction to save space. 394 // Note that there can be more uses, for example if this 395 // is the result of an assignment. 396 if (ZI->use_empty()) 397 ZI->eraseFromParent(); 398 return Result; 399 } 400 } 401 402 // Compare against an integer or pointer null. 403 llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType()); 404 return Builder.CreateICmpNE(Src, Zero, "tobool"); 405} 406 407/// EmitScalarConversion - Emit a conversion from the specified type to the 408/// specified destination type, both of which are LLVM scalar types. 409Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, 410 QualType DstType) { 411 SrcType = CGF.getContext().getCanonicalType(SrcType); 412 DstType = CGF.getContext().getCanonicalType(DstType); 413 if (SrcType == DstType) return Src; 414 415 if (DstType->isVoidType()) return 0; 416 417 llvm::LLVMContext &VMContext = CGF.getLLVMContext(); 418 419 // Handle conversions to bool first, they are special: comparisons against 0. 420 if (DstType->isBooleanType()) 421 return EmitConversionToBool(Src, SrcType); 422 423 const llvm::Type *DstTy = ConvertType(DstType); 424 425 // Ignore conversions like int -> uint. 426 if (Src->getType() == DstTy) 427 return Src; 428 429 // Handle pointer conversions next: pointers can only be converted to/from 430 // other pointers and integers. Check for pointer types in terms of LLVM, as 431 // some native types (like Obj-C id) may map to a pointer type. 432 if (isa<llvm::PointerType>(DstTy)) { 433 // The source value may be an integer, or a pointer. 434 if (isa<llvm::PointerType>(Src->getType())) 435 return Builder.CreateBitCast(Src, DstTy, "conv"); 436 437 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); 438 // First, convert to the correct width so that we control the kind of 439 // extension. 440 const llvm::Type *MiddleTy = 441 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); 442 bool InputSigned = SrcType->isSignedIntegerType(); 443 llvm::Value* IntResult = 444 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); 445 // Then, cast to pointer. 446 return Builder.CreateIntToPtr(IntResult, DstTy, "conv"); 447 } 448 449 if (isa<llvm::PointerType>(Src->getType())) { 450 // Must be an ptr to int cast. 451 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); 452 return Builder.CreatePtrToInt(Src, DstTy, "conv"); 453 } 454 455 // A scalar can be splatted to an extended vector of the same element type 456 if (DstType->isExtVectorType() && !SrcType->isVectorType()) { 457 // Cast the scalar to element type 458 QualType EltTy = DstType->getAs<ExtVectorType>()->getElementType(); 459 llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy); 460 461 // Insert the element in element zero of an undef vector 462 llvm::Value *UnV = llvm::UndefValue::get(DstTy); 463 llvm::Value *Idx = 464 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0); 465 UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp"); 466 467 // Splat the element across to all elements 468 llvm::SmallVector<llvm::Constant*, 16> Args; 469 unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); 470 for (unsigned i = 0; i < NumElements; i++) 471 Args.push_back(llvm::ConstantInt::get( 472 llvm::Type::getInt32Ty(VMContext), 0)); 473 474 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); 475 llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); 476 return Yay; 477 } 478 479 // Allow bitcast from vector to integer/fp of the same size. 480 if (isa<llvm::VectorType>(Src->getType()) || 481 isa<llvm::VectorType>(DstTy)) 482 return Builder.CreateBitCast(Src, DstTy, "conv"); 483 484 // Finally, we have the arithmetic types: real int/float. 485 if (isa<llvm::IntegerType>(Src->getType())) { 486 bool InputSigned = SrcType->isSignedIntegerType(); 487 if (isa<llvm::IntegerType>(DstTy)) 488 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); 489 else if (InputSigned) 490 return Builder.CreateSIToFP(Src, DstTy, "conv"); 491 else 492 return Builder.CreateUIToFP(Src, DstTy, "conv"); 493 } 494 495 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion"); 496 if (isa<llvm::IntegerType>(DstTy)) { 497 if (DstType->isSignedIntegerType()) 498 return Builder.CreateFPToSI(Src, DstTy, "conv"); 499 else 500 return Builder.CreateFPToUI(Src, DstTy, "conv"); 501 } 502 503 assert(DstTy->isFloatingPoint() && "Unknown real conversion"); 504 if (DstTy->getTypeID() < Src->getType()->getTypeID()) 505 return Builder.CreateFPTrunc(Src, DstTy, "conv"); 506 else 507 return Builder.CreateFPExt(Src, DstTy, "conv"); 508} 509 510/// EmitComplexToScalarConversion - Emit a conversion from the specified complex 511/// type to the specified destination type, where the destination type is an 512/// LLVM scalar type. 513Value *ScalarExprEmitter:: 514EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, 515 QualType SrcTy, QualType DstTy) { 516 // Get the source element type. 517 SrcTy = SrcTy->getAs<ComplexType>()->getElementType(); 518 519 // Handle conversions to bool first, they are special: comparisons against 0. 520 if (DstTy->isBooleanType()) { 521 // Complex != 0 -> (Real != 0) | (Imag != 0) 522 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); 523 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); 524 return Builder.CreateOr(Src.first, Src.second, "tobool"); 525 } 526 527 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, 528 // the imaginary part of the complex value is discarded and the value of the 529 // real part is converted according to the conversion rules for the 530 // corresponding real type. 531 return EmitScalarConversion(Src.first, SrcTy, DstTy); 532} 533 534 535//===----------------------------------------------------------------------===// 536// Visitor Methods 537//===----------------------------------------------------------------------===// 538 539Value *ScalarExprEmitter::VisitExpr(Expr *E) { 540 if (const BinaryOperator *BExpr = dyn_cast<BinaryOperator>(E)) 541 if (BExpr->getOpcode() == BinaryOperator::PtrMemD) { 542 LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(BExpr); 543 Value *InVal = CGF.EmitLoadOfLValue(LV, E->getType()).getScalarVal(); 544 return InVal; 545 } 546 547 CGF.ErrorUnsupported(E, "scalar expression"); 548 if (E->getType()->isVoidType()) 549 return 0; 550 return llvm::UndefValue::get(CGF.ConvertType(E->getType())); 551} 552 553Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { 554 llvm::SmallVector<llvm::Constant*, 32> indices; 555 for (unsigned i = 2; i < E->getNumSubExprs(); i++) { 556 indices.push_back(cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i)))); 557 } 558 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0)); 559 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1)); 560 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size()); 561 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle"); 562} 563 564Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 565 TestAndClearIgnoreResultAssign(); 566 567 // Emit subscript expressions in rvalue context's. For most cases, this just 568 // loads the lvalue formed by the subscript expr. However, we have to be 569 // careful, because the base of a vector subscript is occasionally an rvalue, 570 // so we can't get it as an lvalue. 571 if (!E->getBase()->getType()->isVectorType()) 572 return EmitLoadOfLValue(E); 573 574 // Handle the vector case. The base must be a vector, the index must be an 575 // integer value. 576 Value *Base = Visit(E->getBase()); 577 Value *Idx = Visit(E->getIdx()); 578 bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType(); 579 Idx = Builder.CreateIntCast(Idx, 580 llvm::Type::getInt32Ty(CGF.getLLVMContext()), 581 IdxSigned, 582 "vecidxcast"); 583 return Builder.CreateExtractElement(Base, Idx, "vecext"); 584} 585 586static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx, 587 unsigned Off, const llvm::Type *I32Ty) { 588 int MV = SVI->getMaskValue(Idx); 589 if (MV == -1) 590 return llvm::UndefValue::get(I32Ty); 591 return llvm::ConstantInt::get(I32Ty, Off+MV); 592} 593 594Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) { 595 bool Ignore = TestAndClearIgnoreResultAssign(); 596 (void)Ignore; 597 assert (Ignore == false && "init list ignored"); 598 unsigned NumInitElements = E->getNumInits(); 599 600 if (E->hadArrayRangeDesignator()) 601 CGF.ErrorUnsupported(E, "GNU array range designator extension"); 602 603 const llvm::VectorType *VType = 604 dyn_cast<llvm::VectorType>(ConvertType(E->getType())); 605 606 // We have a scalar in braces. Just use the first element. 607 if (!VType) 608 return Visit(E->getInit(0)); 609 610 unsigned ResElts = VType->getNumElements(); 611 const llvm::Type *I32Ty = llvm::Type::getInt32Ty(CGF.getLLVMContext()); 612 613 // Loop over initializers collecting the Value for each, and remembering 614 // whether the source was swizzle (ExtVectorElementExpr). This will allow 615 // us to fold the shuffle for the swizzle into the shuffle for the vector 616 // initializer, since LLVM optimizers generally do not want to touch 617 // shuffles. 618 unsigned CurIdx = 0; 619 bool VIsUndefShuffle = false; 620 llvm::Value *V = llvm::UndefValue::get(VType); 621 for (unsigned i = 0; i != NumInitElements; ++i) { 622 Expr *IE = E->getInit(i); 623 Value *Init = Visit(IE); 624 llvm::SmallVector<llvm::Constant*, 16> Args; 625 626 const llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType()); 627 628 // Handle scalar elements. If the scalar initializer is actually one 629 // element of a different vector of the same width, use shuffle instead of 630 // extract+insert. 631 if (!VVT) { 632 if (isa<ExtVectorElementExpr>(IE)) { 633 llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init); 634 635 if (EI->getVectorOperandType()->getNumElements() == ResElts) { 636 llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand()); 637 Value *LHS = 0, *RHS = 0; 638 if (CurIdx == 0) { 639 // insert into undef -> shuffle (src, undef) 640 Args.push_back(C); 641 for (unsigned j = 1; j != ResElts; ++j) 642 Args.push_back(llvm::UndefValue::get(I32Ty)); 643 644 LHS = EI->getVectorOperand(); 645 RHS = V; 646 VIsUndefShuffle = true; 647 } else if (VIsUndefShuffle) { 648 // insert into undefshuffle && size match -> shuffle (v, src) 649 llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V); 650 for (unsigned j = 0; j != CurIdx; ++j) 651 Args.push_back(getMaskElt(SVV, j, 0, I32Ty)); 652 Args.push_back(llvm::ConstantInt::get(I32Ty, 653 ResElts + C->getZExtValue())); 654 for (unsigned j = CurIdx + 1; j != ResElts; ++j) 655 Args.push_back(llvm::UndefValue::get(I32Ty)); 656 657 LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); 658 RHS = EI->getVectorOperand(); 659 VIsUndefShuffle = false; 660 } 661 if (!Args.empty()) { 662 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); 663 V = Builder.CreateShuffleVector(LHS, RHS, Mask); 664 ++CurIdx; 665 continue; 666 } 667 } 668 } 669 Value *Idx = llvm::ConstantInt::get(I32Ty, CurIdx); 670 V = Builder.CreateInsertElement(V, Init, Idx, "vecinit"); 671 VIsUndefShuffle = false; 672 ++CurIdx; 673 continue; 674 } 675 676 unsigned InitElts = VVT->getNumElements(); 677 678 // If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's 679 // input is the same width as the vector being constructed, generate an 680 // optimized shuffle of the swizzle input into the result. 681 if (isa<ExtVectorElementExpr>(IE)) { 682 llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init); 683 Value *SVOp = SVI->getOperand(0); 684 const llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType()); 685 686 if (OpTy->getNumElements() == ResElts) { 687 unsigned Offset = (CurIdx == 0) ? 0 : ResElts; 688 689 for (unsigned j = 0; j != CurIdx; ++j) { 690 // If the current vector initializer is a shuffle with undef, merge 691 // this shuffle directly into it. 692 if (VIsUndefShuffle) { 693 Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0, 694 I32Ty)); 695 } else { 696 Args.push_back(llvm::ConstantInt::get(I32Ty, j)); 697 } 698 } 699 for (unsigned j = 0, je = InitElts; j != je; ++j) 700 Args.push_back(getMaskElt(SVI, j, Offset, I32Ty)); 701 for (unsigned j = CurIdx + InitElts; j != ResElts; ++j) 702 Args.push_back(llvm::UndefValue::get(I32Ty)); 703 704 if (VIsUndefShuffle) 705 V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); 706 707 Init = SVOp; 708 } 709 } 710 711 // Extend init to result vector length, and then shuffle its contribution 712 // to the vector initializer into V. 713 if (Args.empty()) { 714 for (unsigned j = 0; j != InitElts; ++j) 715 Args.push_back(llvm::ConstantInt::get(I32Ty, j)); 716 for (unsigned j = InitElts; j != ResElts; ++j) 717 Args.push_back(llvm::UndefValue::get(I32Ty)); 718 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); 719 Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT), 720 Mask, "vecext"); 721 722 Args.clear(); 723 for (unsigned j = 0; j != CurIdx; ++j) 724 Args.push_back(llvm::ConstantInt::get(I32Ty, j)); 725 for (unsigned j = 0; j != InitElts; ++j) 726 Args.push_back(llvm::ConstantInt::get(I32Ty, j+ResElts)); 727 for (unsigned j = CurIdx + InitElts; j != ResElts; ++j) 728 Args.push_back(llvm::UndefValue::get(I32Ty)); 729 } 730 731 // If V is undef, make sure it ends up on the RHS of the shuffle to aid 732 // merging subsequent shuffles into this one. 733 if (CurIdx == 0) 734 std::swap(V, Init); 735 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); 736 V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit"); 737 VIsUndefShuffle = isa<llvm::UndefValue>(Init); 738 CurIdx += InitElts; 739 } 740 741 // FIXME: evaluate codegen vs. shuffling against constant null vector. 742 // Emit remaining default initializers. 743 const llvm::Type *EltTy = VType->getElementType(); 744 745 // Emit remaining default initializers 746 for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) { 747 Value *Idx = llvm::ConstantInt::get(I32Ty, CurIdx); 748 llvm::Value *Init = llvm::Constant::getNullValue(EltTy); 749 V = Builder.CreateInsertElement(V, Init, Idx, "vecinit"); 750 } 751 return V; 752} 753 754// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts 755// have to handle a more broad range of conversions than explicit casts, as they 756// handle things like function to ptr-to-function decay etc. 757Value *ScalarExprEmitter::EmitCastExpr(const CastExpr *CE) { 758 const Expr *E = CE->getSubExpr(); 759 QualType DestTy = CE->getType(); 760 CastExpr::CastKind Kind = CE->getCastKind(); 761 762 if (!DestTy->isVoidType()) 763 TestAndClearIgnoreResultAssign(); 764 765 switch (Kind) { 766 default: 767 // FIXME: Assert here. 768 // assert(0 && "Unhandled cast kind!"); 769 break; 770 case CastExpr::CK_Unknown: 771 // FIXME: We should really assert here - Unknown casts should never get 772 // as far as to codegen. 773 break; 774 case CastExpr::CK_BitCast: { 775 Value *Src = Visit(const_cast<Expr*>(E)); 776 return Builder.CreateBitCast(Src, ConvertType(DestTy)); 777 } 778 case CastExpr::CK_ArrayToPointerDecay: { 779 assert(E->getType()->isArrayType() && 780 "Array to pointer decay must have array source type!"); 781 782 Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays. 783 784 // Note that VLA pointers are always decayed, so we don't need to do 785 // anything here. 786 if (!E->getType()->isVariableArrayType()) { 787 assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer"); 788 assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) 789 ->getElementType()) && 790 "Expected pointer to array"); 791 V = Builder.CreateStructGEP(V, 0, "arraydecay"); 792 } 793 794 // The resultant pointer type can be implicitly casted to other pointer 795 // types as well (e.g. void*) and can be implicitly converted to integer. 796 const llvm::Type *DestLTy = ConvertType(DestTy); 797 if (V->getType() != DestLTy) { 798 if (isa<llvm::PointerType>(DestLTy)) 799 V = Builder.CreateBitCast(V, DestLTy, "ptrconv"); 800 else { 801 assert(isa<llvm::IntegerType>(DestLTy) && "Unknown array decay"); 802 V = Builder.CreatePtrToInt(V, DestLTy, "ptrconv"); 803 } 804 } 805 return V; 806 } 807 case CastExpr::CK_NullToMemberPointer: 808 return CGF.CGM.EmitNullConstant(DestTy); 809 810 case CastExpr::CK_DerivedToBase: { 811 const RecordType *DerivedClassTy = 812 E->getType()->getAs<PointerType>()->getPointeeType()->getAs<RecordType>(); 813 CXXRecordDecl *DerivedClassDecl = 814 cast<CXXRecordDecl>(DerivedClassTy->getDecl()); 815 816 const RecordType *BaseClassTy = 817 DestTy->getAs<PointerType>()->getPointeeType()->getAs<RecordType>(); 818 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl()); 819 820 Value *Src = Visit(const_cast<Expr*>(E)); 821 822 bool NullCheckValue = true; 823 824 if (isa<CXXThisExpr>(E)) { 825 // We always assume that 'this' is never null. 826 NullCheckValue = false; 827 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) { 828 // And that lvalue casts are never null. 829 if (ICE->isLvalueCast()) 830 NullCheckValue = false; 831 } 832 return CGF.GetAddressCXXOfBaseClass(Src, DerivedClassDecl, BaseClassDecl, 833 NullCheckValue); 834 } 835 836 case CastExpr::CK_IntegralToPointer: { 837 Value *Src = Visit(const_cast<Expr*>(E)); 838 839 // First, convert to the correct width so that we control the kind of 840 // extension. 841 const llvm::Type *MiddleTy = 842 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); 843 bool InputSigned = E->getType()->isSignedIntegerType(); 844 llvm::Value* IntResult = 845 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); 846 847 return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy)); 848 } 849 850 case CastExpr::CK_PointerToIntegral: { 851 Value *Src = Visit(const_cast<Expr*>(E)); 852 return Builder.CreatePtrToInt(Src, ConvertType(DestTy)); 853 } 854 855 } 856 857 // Handle cases where the source is an non-complex type. 858 859 if (!CGF.hasAggregateLLVMType(E->getType())) { 860 Value *Src = Visit(const_cast<Expr*>(E)); 861 862 // Use EmitScalarConversion to perform the conversion. 863 return EmitScalarConversion(Src, E->getType(), DestTy); 864 } 865 866 if (E->getType()->isAnyComplexType()) { 867 // Handle cases where the source is a complex type. 868 bool IgnoreImag = true; 869 bool IgnoreImagAssign = true; 870 bool IgnoreReal = IgnoreResultAssign; 871 bool IgnoreRealAssign = IgnoreResultAssign; 872 if (DestTy->isBooleanType()) 873 IgnoreImagAssign = IgnoreImag = false; 874 else if (DestTy->isVoidType()) { 875 IgnoreReal = IgnoreImag = false; 876 IgnoreRealAssign = IgnoreImagAssign = true; 877 } 878 CodeGenFunction::ComplexPairTy V 879 = CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign, 880 IgnoreImagAssign); 881 return EmitComplexToScalarConversion(V, E->getType(), DestTy); 882 } 883 884 // Okay, this is a cast from an aggregate. It must be a cast to void. Just 885 // evaluate the result and return. 886 CGF.EmitAggExpr(E, 0, false, true); 887 return 0; 888} 889 890Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { 891 return CGF.EmitCompoundStmt(*E->getSubStmt(), 892 !E->getType()->isVoidType()).getScalarVal(); 893} 894 895Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 896 llvm::Value *V = CGF.GetAddrOfBlockDecl(E); 897 if (E->getType().isObjCGCWeak()) 898 return CGF.CGM.getObjCRuntime().EmitObjCWeakRead(CGF, V); 899 return Builder.CreateLoad(V, false, "tmp"); 900} 901 902//===----------------------------------------------------------------------===// 903// Unary Operators 904//===----------------------------------------------------------------------===// 905 906Value *ScalarExprEmitter::VisitPrePostIncDec(const UnaryOperator *E, 907 bool isInc, bool isPre) { 908 LValue LV = EmitLValue(E->getSubExpr()); 909 QualType ValTy = E->getSubExpr()->getType(); 910 Value *InVal = CGF.EmitLoadOfLValue(LV, ValTy).getScalarVal(); 911 912 llvm::LLVMContext &VMContext = CGF.getLLVMContext(); 913 914 int AmountVal = isInc ? 1 : -1; 915 916 if (ValTy->isPointerType() && 917 ValTy->getAs<PointerType>()->isVariableArrayType()) { 918 // The amount of the addition/subtraction needs to account for the VLA size 919 CGF.ErrorUnsupported(E, "VLA pointer inc/dec"); 920 } 921 922 Value *NextVal; 923 if (const llvm::PointerType *PT = 924 dyn_cast<llvm::PointerType>(InVal->getType())) { 925 llvm::Constant *Inc = 926 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal); 927 if (!isa<llvm::FunctionType>(PT->getElementType())) { 928 QualType PTEE = ValTy->getPointeeType(); 929 if (const ObjCInterfaceType *OIT = 930 dyn_cast<ObjCInterfaceType>(PTEE)) { 931 // Handle interface types, which are not represented with a concrete type. 932 int size = CGF.getContext().getTypeSize(OIT) / 8; 933 if (!isInc) 934 size = -size; 935 Inc = llvm::ConstantInt::get(Inc->getType(), size); 936 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 937 InVal = Builder.CreateBitCast(InVal, i8Ty); 938 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr"); 939 llvm::Value *lhs = LV.getAddress(); 940 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty)); 941 LV = LValue::MakeAddr(lhs, CGF.MakeQualifiers(ValTy)); 942 } else 943 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec"); 944 } else { 945 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 946 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp"); 947 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec"); 948 NextVal = Builder.CreateBitCast(NextVal, InVal->getType()); 949 } 950 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) { 951 // Bool++ is an interesting case, due to promotion rules, we get: 952 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 -> 953 // Bool = ((int)Bool+1) != 0 954 // An interesting aspect of this is that increment is always true. 955 // Decrement does not have this property. 956 NextVal = llvm::ConstantInt::getTrue(VMContext); 957 } else if (isa<llvm::IntegerType>(InVal->getType())) { 958 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); 959 960 // Signed integer overflow is undefined behavior. 961 if (ValTy->isSignedIntegerType()) 962 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); 963 else 964 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); 965 } else { 966 // Add the inc/dec to the real part. 967 if (InVal->getType()->isFloatTy()) 968 NextVal = 969 llvm::ConstantFP::get(VMContext, 970 llvm::APFloat(static_cast<float>(AmountVal))); 971 else if (InVal->getType()->isDoubleTy()) 972 NextVal = 973 llvm::ConstantFP::get(VMContext, 974 llvm::APFloat(static_cast<double>(AmountVal))); 975 else { 976 llvm::APFloat F(static_cast<float>(AmountVal)); 977 bool ignored; 978 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero, 979 &ignored); 980 NextVal = llvm::ConstantFP::get(VMContext, F); 981 } 982 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec"); 983 } 984 985 // Store the updated result through the lvalue. 986 if (LV.isBitfield()) 987 CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, 988 &NextVal); 989 else 990 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy); 991 992 // If this is a postinc, return the value read from memory, otherwise use the 993 // updated value. 994 return isPre ? NextVal : InVal; 995} 996 997 998Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { 999 TestAndClearIgnoreResultAssign(); 1000 Value *Op = Visit(E->getSubExpr()); 1001 if (Op->getType()->isFPOrFPVector()) 1002 return Builder.CreateFNeg(Op, "neg"); 1003 return Builder.CreateNeg(Op, "neg"); 1004} 1005 1006Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { 1007 TestAndClearIgnoreResultAssign(); 1008 Value *Op = Visit(E->getSubExpr()); 1009 return Builder.CreateNot(Op, "neg"); 1010} 1011 1012Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { 1013 // Compare operand to zero. 1014 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); 1015 1016 // Invert value. 1017 // TODO: Could dynamically modify easy computations here. For example, if 1018 // the operand is an icmp ne, turn into icmp eq. 1019 BoolVal = Builder.CreateNot(BoolVal, "lnot"); 1020 1021 // ZExt result to the expr type. 1022 return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext"); 1023} 1024 1025/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of 1026/// argument of the sizeof expression as an integer. 1027Value * 1028ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { 1029 QualType TypeToSize = E->getTypeOfArgument(); 1030 if (E->isSizeOf()) { 1031 if (const VariableArrayType *VAT = 1032 CGF.getContext().getAsVariableArrayType(TypeToSize)) { 1033 if (E->isArgumentType()) { 1034 // sizeof(type) - make sure to emit the VLA size. 1035 CGF.EmitVLASize(TypeToSize); 1036 } else { 1037 // C99 6.5.3.4p2: If the argument is an expression of type 1038 // VLA, it is evaluated. 1039 CGF.EmitAnyExpr(E->getArgumentExpr()); 1040 } 1041 1042 return CGF.GetVLASize(VAT); 1043 } 1044 } 1045 1046 // If this isn't sizeof(vla), the result must be constant; use the constant 1047 // folding logic so we don't have to duplicate it here. 1048 Expr::EvalResult Result; 1049 E->Evaluate(Result, CGF.getContext()); 1050 return llvm::ConstantInt::get(VMContext, Result.Val.getInt()); 1051} 1052 1053Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { 1054 Expr *Op = E->getSubExpr(); 1055 if (Op->getType()->isAnyComplexType()) 1056 return CGF.EmitComplexExpr(Op, false, true, false, true).first; 1057 return Visit(Op); 1058} 1059Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { 1060 Expr *Op = E->getSubExpr(); 1061 if (Op->getType()->isAnyComplexType()) 1062 return CGF.EmitComplexExpr(Op, true, false, true, false).second; 1063 1064 // __imag on a scalar returns zero. Emit the subexpr to ensure side 1065 // effects are evaluated, but not the actual value. 1066 if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid) 1067 CGF.EmitLValue(Op); 1068 else 1069 CGF.EmitScalarExpr(Op, true); 1070 return llvm::Constant::getNullValue(ConvertType(E->getType())); 1071} 1072 1073Value *ScalarExprEmitter::VisitUnaryOffsetOf(const UnaryOperator *E) { 1074 Value* ResultAsPtr = EmitLValue(E->getSubExpr()).getAddress(); 1075 const llvm::Type* ResultType = ConvertType(E->getType()); 1076 return Builder.CreatePtrToInt(ResultAsPtr, ResultType, "offsetof"); 1077} 1078 1079//===----------------------------------------------------------------------===// 1080// Binary Operators 1081//===----------------------------------------------------------------------===// 1082 1083BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { 1084 TestAndClearIgnoreResultAssign(); 1085 BinOpInfo Result; 1086 Result.LHS = Visit(E->getLHS()); 1087 Result.RHS = Visit(E->getRHS()); 1088 Result.Ty = E->getType(); 1089 Result.E = E; 1090 return Result; 1091} 1092 1093Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, 1094 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { 1095 bool Ignore = TestAndClearIgnoreResultAssign(); 1096 QualType LHSTy = E->getLHS()->getType(), RHSTy = E->getRHS()->getType(); 1097 1098 BinOpInfo OpInfo; 1099 1100 if (E->getComputationResultType()->isAnyComplexType()) { 1101 // This needs to go through the complex expression emitter, but it's a tad 1102 // complicated to do that... I'm leaving it out for now. (Note that we do 1103 // actually need the imaginary part of the RHS for multiplication and 1104 // division.) 1105 CGF.ErrorUnsupported(E, "complex compound assignment"); 1106 return llvm::UndefValue::get(CGF.ConvertType(E->getType())); 1107 } 1108 1109 // Emit the RHS first. __block variables need to have the rhs evaluated 1110 // first, plus this should improve codegen a little. 1111 OpInfo.RHS = Visit(E->getRHS()); 1112 OpInfo.Ty = E->getComputationResultType(); 1113 OpInfo.E = E; 1114 // Load/convert the LHS. 1115 LValue LHSLV = EmitLValue(E->getLHS()); 1116 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); 1117 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, 1118 E->getComputationLHSType()); 1119 1120 // Expand the binary operator. 1121 Value *Result = (this->*Func)(OpInfo); 1122 1123 // Convert the result back to the LHS type. 1124 Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy); 1125 1126 // Store the result value into the LHS lvalue. Bit-fields are handled 1127 // specially because the result is altered by the store, i.e., [C99 6.5.16p1] 1128 // 'An assignment expression has the value of the left operand after the 1129 // assignment...'. 1130 if (LHSLV.isBitfield()) { 1131 if (!LHSLV.isVolatileQualified()) { 1132 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy, 1133 &Result); 1134 return Result; 1135 } else 1136 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy); 1137 } else 1138 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy); 1139 if (Ignore) 1140 return 0; 1141 return EmitLoadOfLValue(LHSLV, E->getType()); 1142} 1143 1144 1145Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { 1146 if (Ops.LHS->getType()->isFPOrFPVector()) 1147 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); 1148 else if (Ops.Ty->isUnsignedIntegerType()) 1149 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); 1150 else 1151 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); 1152} 1153 1154Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { 1155 // Rem in C can't be a floating point type: C99 6.5.5p2. 1156 if (Ops.Ty->isUnsignedIntegerType()) 1157 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); 1158 else 1159 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); 1160} 1161 1162Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) { 1163 unsigned IID; 1164 unsigned OpID = 0; 1165 1166 switch (Ops.E->getOpcode()) { 1167 case BinaryOperator::Add: 1168 case BinaryOperator::AddAssign: 1169 OpID = 1; 1170 IID = llvm::Intrinsic::sadd_with_overflow; 1171 break; 1172 case BinaryOperator::Sub: 1173 case BinaryOperator::SubAssign: 1174 OpID = 2; 1175 IID = llvm::Intrinsic::ssub_with_overflow; 1176 break; 1177 case BinaryOperator::Mul: 1178 case BinaryOperator::MulAssign: 1179 OpID = 3; 1180 IID = llvm::Intrinsic::smul_with_overflow; 1181 break; 1182 default: 1183 assert(false && "Unsupported operation for overflow detection"); 1184 IID = 0; 1185 } 1186 OpID <<= 1; 1187 OpID |= 1; 1188 1189 const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty); 1190 1191 llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1); 1192 1193 Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS); 1194 Value *result = Builder.CreateExtractValue(resultAndOverflow, 0); 1195 Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1); 1196 1197 // Branch in case of overflow. 1198 llvm::BasicBlock *initialBB = Builder.GetInsertBlock(); 1199 llvm::BasicBlock *overflowBB = 1200 CGF.createBasicBlock("overflow", CGF.CurFn); 1201 llvm::BasicBlock *continueBB = 1202 CGF.createBasicBlock("overflow.continue", CGF.CurFn); 1203 1204 Builder.CreateCondBr(overflow, overflowBB, continueBB); 1205 1206 // Handle overflow 1207 1208 Builder.SetInsertPoint(overflowBB); 1209 1210 // Handler is: 1211 // long long *__overflow_handler)(long long a, long long b, char op, 1212 // char width) 1213 std::vector<const llvm::Type*> handerArgTypes; 1214 handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext)); 1215 handerArgTypes.push_back(llvm::Type::getInt64Ty(VMContext)); 1216 handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext)); 1217 handerArgTypes.push_back(llvm::Type::getInt8Ty(VMContext)); 1218 llvm::FunctionType *handlerTy = llvm::FunctionType::get( 1219 llvm::Type::getInt64Ty(VMContext), handerArgTypes, false); 1220 llvm::Value *handlerFunction = 1221 CGF.CGM.getModule().getOrInsertGlobal("__overflow_handler", 1222 llvm::PointerType::getUnqual(handlerTy)); 1223 handlerFunction = Builder.CreateLoad(handlerFunction); 1224 1225 llvm::Value *handlerResult = Builder.CreateCall4(handlerFunction, 1226 Builder.CreateSExt(Ops.LHS, llvm::Type::getInt64Ty(VMContext)), 1227 Builder.CreateSExt(Ops.RHS, llvm::Type::getInt64Ty(VMContext)), 1228 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), OpID), 1229 llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1230 cast<llvm::IntegerType>(opTy)->getBitWidth())); 1231 1232 handlerResult = Builder.CreateTrunc(handlerResult, opTy); 1233 1234 Builder.CreateBr(continueBB); 1235 1236 // Set up the continuation 1237 Builder.SetInsertPoint(continueBB); 1238 // Get the correct result 1239 llvm::PHINode *phi = Builder.CreatePHI(opTy); 1240 phi->reserveOperandSpace(2); 1241 phi->addIncoming(result, initialBB); 1242 phi->addIncoming(handlerResult, overflowBB); 1243 1244 return phi; 1245} 1246 1247Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { 1248 if (!Ops.Ty->isAnyPointerType()) { 1249 if (CGF.getContext().getLangOptions().OverflowChecking && 1250 Ops.Ty->isSignedIntegerType()) 1251 return EmitOverflowCheckedBinOp(Ops); 1252 1253 if (Ops.LHS->getType()->isFPOrFPVector()) 1254 return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add"); 1255 1256 // Signed integer overflow is undefined behavior. 1257 if (Ops.Ty->isSignedIntegerType()) 1258 return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add"); 1259 1260 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); 1261 } 1262 1263 if (Ops.Ty->isPointerType() && 1264 Ops.Ty->getAs<PointerType>()->isVariableArrayType()) { 1265 // The amount of the addition needs to account for the VLA size 1266 CGF.ErrorUnsupported(Ops.E, "VLA pointer addition"); 1267 } 1268 Value *Ptr, *Idx; 1269 Expr *IdxExp; 1270 const PointerType *PT = Ops.E->getLHS()->getType()->getAs<PointerType>(); 1271 const ObjCObjectPointerType *OPT = 1272 Ops.E->getLHS()->getType()->getAs<ObjCObjectPointerType>(); 1273 if (PT || OPT) { 1274 Ptr = Ops.LHS; 1275 Idx = Ops.RHS; 1276 IdxExp = Ops.E->getRHS(); 1277 } else { // int + pointer 1278 PT = Ops.E->getRHS()->getType()->getAs<PointerType>(); 1279 OPT = Ops.E->getRHS()->getType()->getAs<ObjCObjectPointerType>(); 1280 assert((PT || OPT) && "Invalid add expr"); 1281 Ptr = Ops.RHS; 1282 Idx = Ops.LHS; 1283 IdxExp = Ops.E->getLHS(); 1284 } 1285 1286 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); 1287 if (Width < CGF.LLVMPointerWidth) { 1288 // Zero or sign extend the pointer value based on whether the index is 1289 // signed or not. 1290 const llvm::Type *IdxType = 1291 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); 1292 if (IdxExp->getType()->isSignedIntegerType()) 1293 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); 1294 else 1295 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); 1296 } 1297 const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType(); 1298 // Handle interface types, which are not represented with a concrete type. 1299 if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) { 1300 llvm::Value *InterfaceSize = 1301 llvm::ConstantInt::get(Idx->getType(), 1302 CGF.getContext().getTypeSize(OIT) / 8); 1303 Idx = Builder.CreateMul(Idx, InterfaceSize); 1304 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1305 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); 1306 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); 1307 return Builder.CreateBitCast(Res, Ptr->getType()); 1308 } 1309 1310 // Explicitly handle GNU void* and function pointer arithmetic extensions. The 1311 // GNU void* casts amount to no-ops since our void* type is i8*, but this is 1312 // future proof. 1313 if (ElementType->isVoidType() || ElementType->isFunctionType()) { 1314 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1315 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); 1316 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); 1317 return Builder.CreateBitCast(Res, Ptr->getType()); 1318 } 1319 1320 return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr"); 1321} 1322 1323Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { 1324 if (!isa<llvm::PointerType>(Ops.LHS->getType())) { 1325 if (CGF.getContext().getLangOptions().OverflowChecking 1326 && Ops.Ty->isSignedIntegerType()) 1327 return EmitOverflowCheckedBinOp(Ops); 1328 1329 if (Ops.LHS->getType()->isFPOrFPVector()) 1330 return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub"); 1331 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); 1332 } 1333 1334 if (Ops.E->getLHS()->getType()->isPointerType() && 1335 Ops.E->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) { 1336 // The amount of the addition needs to account for the VLA size for 1337 // ptr-int 1338 // The amount of the division needs to account for the VLA size for 1339 // ptr-ptr. 1340 CGF.ErrorUnsupported(Ops.E, "VLA pointer subtraction"); 1341 } 1342 1343 const QualType LHSType = Ops.E->getLHS()->getType(); 1344 const QualType LHSElementType = LHSType->getPointeeType(); 1345 if (!isa<llvm::PointerType>(Ops.RHS->getType())) { 1346 // pointer - int 1347 Value *Idx = Ops.RHS; 1348 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); 1349 if (Width < CGF.LLVMPointerWidth) { 1350 // Zero or sign extend the pointer value based on whether the index is 1351 // signed or not. 1352 const llvm::Type *IdxType = 1353 llvm::IntegerType::get(VMContext, CGF.LLVMPointerWidth); 1354 if (Ops.E->getRHS()->getType()->isSignedIntegerType()) 1355 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); 1356 else 1357 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); 1358 } 1359 Idx = Builder.CreateNeg(Idx, "sub.ptr.neg"); 1360 1361 // Handle interface types, which are not represented with a concrete type. 1362 if (const ObjCInterfaceType *OIT = 1363 dyn_cast<ObjCInterfaceType>(LHSElementType)) { 1364 llvm::Value *InterfaceSize = 1365 llvm::ConstantInt::get(Idx->getType(), 1366 CGF.getContext().getTypeSize(OIT) / 8); 1367 Idx = Builder.CreateMul(Idx, InterfaceSize); 1368 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1369 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); 1370 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr"); 1371 return Builder.CreateBitCast(Res, Ops.LHS->getType()); 1372 } 1373 1374 // Explicitly handle GNU void* and function pointer arithmetic 1375 // extensions. The GNU void* casts amount to no-ops since our void* type is 1376 // i8*, but this is future proof. 1377 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { 1378 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1379 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); 1380 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr"); 1381 return Builder.CreateBitCast(Res, Ops.LHS->getType()); 1382 } 1383 1384 return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr"); 1385 } else { 1386 // pointer - pointer 1387 Value *LHS = Ops.LHS; 1388 Value *RHS = Ops.RHS; 1389 1390 uint64_t ElementSize; 1391 1392 // Handle GCC extension for pointer arithmetic on void* and function pointer 1393 // types. 1394 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { 1395 ElementSize = 1; 1396 } else { 1397 ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8; 1398 } 1399 1400 const llvm::Type *ResultType = ConvertType(Ops.Ty); 1401 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); 1402 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); 1403 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); 1404 1405 // Optimize out the shift for element size of 1. 1406 if (ElementSize == 1) 1407 return BytesBetween; 1408 1409 // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since 1410 // pointer difference in C is only defined in the case where both operands 1411 // are pointing to elements of an array. 1412 Value *BytesPerElt = llvm::ConstantInt::get(ResultType, ElementSize); 1413 return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); 1414 } 1415} 1416 1417Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { 1418 // LLVM requires the LHS and RHS to be the same type: promote or truncate the 1419 // RHS to the same size as the LHS. 1420 Value *RHS = Ops.RHS; 1421 if (Ops.LHS->getType() != RHS->getType()) 1422 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); 1423 1424 return Builder.CreateShl(Ops.LHS, RHS, "shl"); 1425} 1426 1427Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { 1428 // LLVM requires the LHS and RHS to be the same type: promote or truncate the 1429 // RHS to the same size as the LHS. 1430 Value *RHS = Ops.RHS; 1431 if (Ops.LHS->getType() != RHS->getType()) 1432 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); 1433 1434 if (Ops.Ty->isUnsignedIntegerType()) 1435 return Builder.CreateLShr(Ops.LHS, RHS, "shr"); 1436 return Builder.CreateAShr(Ops.LHS, RHS, "shr"); 1437} 1438 1439Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, 1440 unsigned SICmpOpc, unsigned FCmpOpc) { 1441 TestAndClearIgnoreResultAssign(); 1442 Value *Result; 1443 QualType LHSTy = E->getLHS()->getType(); 1444 if (!LHSTy->isAnyComplexType()) { 1445 Value *LHS = Visit(E->getLHS()); 1446 Value *RHS = Visit(E->getRHS()); 1447 1448 if (LHS->getType()->isFPOrFPVector()) { 1449 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc, 1450 LHS, RHS, "cmp"); 1451 } else if (LHSTy->isSignedIntegerType()) { 1452 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, 1453 LHS, RHS, "cmp"); 1454 } else { 1455 // Unsigned integers and pointers. 1456 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, 1457 LHS, RHS, "cmp"); 1458 } 1459 1460 // If this is a vector comparison, sign extend the result to the appropriate 1461 // vector integer type and return it (don't convert to bool). 1462 if (LHSTy->isVectorType()) 1463 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); 1464 1465 } else { 1466 // Complex Comparison: can only be an equality comparison. 1467 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); 1468 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); 1469 1470 QualType CETy = LHSTy->getAs<ComplexType>()->getElementType(); 1471 1472 Value *ResultR, *ResultI; 1473 if (CETy->isRealFloatingType()) { 1474 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, 1475 LHS.first, RHS.first, "cmp.r"); 1476 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, 1477 LHS.second, RHS.second, "cmp.i"); 1478 } else { 1479 // Complex comparisons can only be equality comparisons. As such, signed 1480 // and unsigned opcodes are the same. 1481 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, 1482 LHS.first, RHS.first, "cmp.r"); 1483 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, 1484 LHS.second, RHS.second, "cmp.i"); 1485 } 1486 1487 if (E->getOpcode() == BinaryOperator::EQ) { 1488 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); 1489 } else { 1490 assert(E->getOpcode() == BinaryOperator::NE && 1491 "Complex comparison other than == or != ?"); 1492 Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); 1493 } 1494 } 1495 1496 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); 1497} 1498 1499Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { 1500 bool Ignore = TestAndClearIgnoreResultAssign(); 1501 1502 // __block variables need to have the rhs evaluated first, plus this should 1503 // improve codegen just a little. 1504 Value *RHS = Visit(E->getRHS()); 1505 LValue LHS = EmitLValue(E->getLHS()); 1506 1507 // Store the value into the LHS. Bit-fields are handled specially 1508 // because the result is altered by the store, i.e., [C99 6.5.16p1] 1509 // 'An assignment expression has the value of the left operand after 1510 // the assignment...'. 1511 if (LHS.isBitfield()) { 1512 if (!LHS.isVolatileQualified()) { 1513 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(), 1514 &RHS); 1515 return RHS; 1516 } else 1517 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType()); 1518 } else 1519 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); 1520 if (Ignore) 1521 return 0; 1522 return EmitLoadOfLValue(LHS, E->getType()); 1523} 1524 1525Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { 1526 const llvm::Type *ResTy = ConvertType(E->getType()); 1527 1528 // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. 1529 // If we have 1 && X, just emit X without inserting the control flow. 1530 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { 1531 if (Cond == 1) { // If we have 1 && X, just emit X. 1532 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 1533 // ZExt result to int or bool. 1534 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext"); 1535 } 1536 1537 // 0 && RHS: If it is safe, just elide the RHS, and return 0/false. 1538 if (!CGF.ContainsLabel(E->getRHS())) 1539 return llvm::Constant::getNullValue(ResTy); 1540 } 1541 1542 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); 1543 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); 1544 1545 // Branch on the LHS first. If it is false, go to the failure (cont) block. 1546 CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock); 1547 1548 // Any edges into the ContBlock are now from an (indeterminate number of) 1549 // edges from this first condition. All of these values will be false. Start 1550 // setting up the PHI node in the Cont Block for this. 1551 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 1552 "", ContBlock); 1553 PN->reserveOperandSpace(2); // Normal case, two inputs. 1554 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); 1555 PI != PE; ++PI) 1556 PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI); 1557 1558 CGF.PushConditionalTempDestruction(); 1559 CGF.EmitBlock(RHSBlock); 1560 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 1561 CGF.PopConditionalTempDestruction(); 1562 1563 // Reaquire the RHS block, as there may be subblocks inserted. 1564 RHSBlock = Builder.GetInsertBlock(); 1565 1566 // Emit an unconditional branch from this block to ContBlock. Insert an entry 1567 // into the phi node for the edge with the value of RHSCond. 1568 CGF.EmitBlock(ContBlock); 1569 PN->addIncoming(RHSCond, RHSBlock); 1570 1571 // ZExt result to int. 1572 return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext"); 1573} 1574 1575Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { 1576 const llvm::Type *ResTy = ConvertType(E->getType()); 1577 1578 // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. 1579 // If we have 0 || X, just emit X without inserting the control flow. 1580 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { 1581 if (Cond == -1) { // If we have 0 || X, just emit X. 1582 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 1583 // ZExt result to int or bool. 1584 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext"); 1585 } 1586 1587 // 1 || RHS: If it is safe, just elide the RHS, and return 1/true. 1588 if (!CGF.ContainsLabel(E->getRHS())) 1589 return llvm::ConstantInt::get(ResTy, 1); 1590 } 1591 1592 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); 1593 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); 1594 1595 // Branch on the LHS first. If it is true, go to the success (cont) block. 1596 CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock); 1597 1598 // Any edges into the ContBlock are now from an (indeterminate number of) 1599 // edges from this first condition. All of these values will be true. Start 1600 // setting up the PHI node in the Cont Block for this. 1601 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 1602 "", ContBlock); 1603 PN->reserveOperandSpace(2); // Normal case, two inputs. 1604 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); 1605 PI != PE; ++PI) 1606 PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI); 1607 1608 CGF.PushConditionalTempDestruction(); 1609 1610 // Emit the RHS condition as a bool value. 1611 CGF.EmitBlock(RHSBlock); 1612 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 1613 1614 CGF.PopConditionalTempDestruction(); 1615 1616 // Reaquire the RHS block, as there may be subblocks inserted. 1617 RHSBlock = Builder.GetInsertBlock(); 1618 1619 // Emit an unconditional branch from this block to ContBlock. Insert an entry 1620 // into the phi node for the edge with the value of RHSCond. 1621 CGF.EmitBlock(ContBlock); 1622 PN->addIncoming(RHSCond, RHSBlock); 1623 1624 // ZExt result to int. 1625 return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext"); 1626} 1627 1628Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { 1629 CGF.EmitStmt(E->getLHS()); 1630 CGF.EnsureInsertPoint(); 1631 return Visit(E->getRHS()); 1632} 1633 1634//===----------------------------------------------------------------------===// 1635// Other Operators 1636//===----------------------------------------------------------------------===// 1637 1638/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified 1639/// expression is cheap enough and side-effect-free enough to evaluate 1640/// unconditionally instead of conditionally. This is used to convert control 1641/// flow into selects in some cases. 1642static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E) { 1643 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) 1644 return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr()); 1645 1646 // TODO: Allow anything we can constant fold to an integer or fp constant. 1647 if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) || 1648 isa<FloatingLiteral>(E)) 1649 return true; 1650 1651 // Non-volatile automatic variables too, to get "cond ? X : Y" where 1652 // X and Y are local variables. 1653 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 1654 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) 1655 if (VD->hasLocalStorage() && !VD->getType().isVolatileQualified()) 1656 return true; 1657 1658 return false; 1659} 1660 1661 1662Value *ScalarExprEmitter:: 1663VisitConditionalOperator(const ConditionalOperator *E) { 1664 TestAndClearIgnoreResultAssign(); 1665 // If the condition constant folds and can be elided, try to avoid emitting 1666 // the condition and the dead arm. 1667 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){ 1668 Expr *Live = E->getLHS(), *Dead = E->getRHS(); 1669 if (Cond == -1) 1670 std::swap(Live, Dead); 1671 1672 // If the dead side doesn't have labels we need, and if the Live side isn't 1673 // the gnu missing ?: extension (which we could handle, but don't bother 1674 // to), just emit the Live part. 1675 if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part 1676 Live) // Live part isn't missing. 1677 return Visit(Live); 1678 } 1679 1680 1681 // If this is a really simple expression (like x ? 4 : 5), emit this as a 1682 // select instead of as control flow. We can only do this if it is cheap and 1683 // safe to evaluate the LHS and RHS unconditionally. 1684 if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS()) && 1685 isCheapEnoughToEvaluateUnconditionally(E->getRHS())) { 1686 llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond()); 1687 llvm::Value *LHS = Visit(E->getLHS()); 1688 llvm::Value *RHS = Visit(E->getRHS()); 1689 return Builder.CreateSelect(CondV, LHS, RHS, "cond"); 1690 } 1691 1692 1693 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 1694 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 1695 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 1696 Value *CondVal = 0; 1697 1698 // If we don't have the GNU missing condition extension, emit a branch on bool 1699 // the normal way. 1700 if (E->getLHS()) { 1701 // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for 1702 // the branch on bool. 1703 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); 1704 } else { 1705 // Otherwise, for the ?: extension, evaluate the conditional and then 1706 // convert it to bool the hard way. We do this explicitly because we need 1707 // the unconverted value for the missing middle value of the ?:. 1708 CondVal = CGF.EmitScalarExpr(E->getCond()); 1709 1710 // In some cases, EmitScalarConversion will delete the "CondVal" expression 1711 // if there are no extra uses (an optimization). Inhibit this by making an 1712 // extra dead use, because we're going to add a use of CondVal later. We 1713 // don't use the builder for this, because we don't want it to get optimized 1714 // away. This leaves dead code, but the ?: extension isn't common. 1715 new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder", 1716 Builder.GetInsertBlock()); 1717 1718 Value *CondBoolVal = 1719 CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), 1720 CGF.getContext().BoolTy); 1721 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); 1722 } 1723 1724 CGF.PushConditionalTempDestruction(); 1725 CGF.EmitBlock(LHSBlock); 1726 1727 // Handle the GNU extension for missing LHS. 1728 Value *LHS; 1729 if (E->getLHS()) 1730 LHS = Visit(E->getLHS()); 1731 else // Perform promotions, to handle cases like "short ?: int" 1732 LHS = EmitScalarConversion(CondVal, E->getCond()->getType(), E->getType()); 1733 1734 CGF.PopConditionalTempDestruction(); 1735 LHSBlock = Builder.GetInsertBlock(); 1736 CGF.EmitBranch(ContBlock); 1737 1738 CGF.PushConditionalTempDestruction(); 1739 CGF.EmitBlock(RHSBlock); 1740 1741 Value *RHS = Visit(E->getRHS()); 1742 CGF.PopConditionalTempDestruction(); 1743 RHSBlock = Builder.GetInsertBlock(); 1744 CGF.EmitBranch(ContBlock); 1745 1746 CGF.EmitBlock(ContBlock); 1747 1748 if (!LHS || !RHS) { 1749 assert(E->getType()->isVoidType() && "Non-void value should have a value"); 1750 return 0; 1751 } 1752 1753 // Create a PHI node for the real part. 1754 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); 1755 PN->reserveOperandSpace(2); 1756 PN->addIncoming(LHS, LHSBlock); 1757 PN->addIncoming(RHS, RHSBlock); 1758 return PN; 1759} 1760 1761Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { 1762 return Visit(E->getChosenSubExpr(CGF.getContext())); 1763} 1764 1765Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 1766 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 1767 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 1768 1769 // If EmitVAArg fails, we fall back to the LLVM instruction. 1770 if (!ArgPtr) 1771 return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); 1772 1773 // FIXME Volatility. 1774 return Builder.CreateLoad(ArgPtr); 1775} 1776 1777Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) { 1778 return CGF.BuildBlockLiteralTmp(BE); 1779} 1780 1781//===----------------------------------------------------------------------===// 1782// Entry Point into this File 1783//===----------------------------------------------------------------------===// 1784 1785/// EmitScalarExpr - Emit the computation of the specified expression of scalar 1786/// type, ignoring the result. 1787Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) { 1788 assert(E && !hasAggregateLLVMType(E->getType()) && 1789 "Invalid scalar expression to emit"); 1790 1791 return ScalarExprEmitter(*this, IgnoreResultAssign) 1792 .Visit(const_cast<Expr*>(E)); 1793} 1794 1795/// EmitScalarConversion - Emit a conversion from the specified type to the 1796/// specified destination type, both of which are LLVM scalar types. 1797Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, 1798 QualType DstTy) { 1799 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && 1800 "Invalid scalar expression to emit"); 1801 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); 1802} 1803 1804/// EmitComplexToScalarConversion - Emit a conversion from the specified complex 1805/// type to the specified destination type, where the destination type is an 1806/// LLVM scalar type. 1807Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, 1808 QualType SrcTy, 1809 QualType DstTy) { 1810 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) && 1811 "Invalid complex -> scalar conversion"); 1812 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, 1813 DstTy); 1814} 1815 1816Value *CodeGenFunction::EmitShuffleVector(Value* V1, Value *V2, ...) { 1817 assert(V1->getType() == V2->getType() && 1818 "Vector operands must be of the same type"); 1819 unsigned NumElements = 1820 cast<llvm::VectorType>(V1->getType())->getNumElements(); 1821 1822 va_list va; 1823 va_start(va, V2); 1824 1825 llvm::SmallVector<llvm::Constant*, 16> Args; 1826 for (unsigned i = 0; i < NumElements; i++) { 1827 int n = va_arg(va, int); 1828 assert(n >= 0 && n < (int)NumElements * 2 && 1829 "Vector shuffle index out of bounds!"); 1830 Args.push_back(llvm::ConstantInt::get( 1831 llvm::Type::getInt32Ty(VMContext), n)); 1832 } 1833 1834 const char *Name = va_arg(va, const char *); 1835 va_end(va); 1836 1837 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); 1838 1839 return Builder.CreateShuffleVector(V1, V2, Mask, Name); 1840} 1841 1842llvm::Value *CodeGenFunction::EmitVector(llvm::Value * const *Vals, 1843 unsigned NumVals, bool isSplat) { 1844 llvm::Value *Vec 1845 = llvm::UndefValue::get(llvm::VectorType::get(Vals[0]->getType(), NumVals)); 1846 1847 for (unsigned i = 0, e = NumVals; i != e; ++i) { 1848 llvm::Value *Val = isSplat ? Vals[0] : Vals[i]; 1849 llvm::Value *Idx = llvm::ConstantInt::get( 1850 llvm::Type::getInt32Ty(VMContext), i); 1851 Vec = Builder.CreateInsertElement(Vec, Val, Idx, "tmp"); 1852 } 1853 1854 return Vec; 1855} 1856