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