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