CGExprScalar.cpp revision daa8e4e888758d55a7a759dd4a91b83921cef222
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 "clang/Frontend/CodeGenOptions.h" 15#include "CodeGenFunction.h" 16#include "CGCXXABI.h" 17#include "CGObjCRuntime.h" 18#include "CodeGenModule.h" 19#include "CGDebugInfo.h" 20#include "clang/AST/ASTContext.h" 21#include "clang/AST/DeclObjC.h" 22#include "clang/AST/RecordLayout.h" 23#include "clang/AST/StmtVisitor.h" 24#include "clang/Basic/TargetInfo.h" 25#include "llvm/Constants.h" 26#include "llvm/Function.h" 27#include "llvm/GlobalVariable.h" 28#include "llvm/Intrinsics.h" 29#include "llvm/Module.h" 30#include "llvm/Support/CFG.h" 31#include "llvm/Target/TargetData.h" 32#include <cstdarg> 33 34using namespace clang; 35using namespace CodeGen; 36using llvm::Value; 37 38//===----------------------------------------------------------------------===// 39// Scalar Expression Emitter 40//===----------------------------------------------------------------------===// 41 42namespace { 43struct BinOpInfo { 44 Value *LHS; 45 Value *RHS; 46 QualType Ty; // Computation Type. 47 BinaryOperator::Opcode Opcode; // Opcode of BinOp to perform 48 const Expr *E; // Entire expr, for error unsupported. May not be binop. 49}; 50 51static bool MustVisitNullValue(const Expr *E) { 52 // If a null pointer expression's type is the C++0x nullptr_t, then 53 // it's not necessarily a simple constant and it must be evaluated 54 // for its potential side effects. 55 return E->getType()->isNullPtrType(); 56} 57 58class ScalarExprEmitter 59 : public StmtVisitor<ScalarExprEmitter, Value*> { 60 CodeGenFunction &CGF; 61 CGBuilderTy &Builder; 62 bool IgnoreResultAssign; 63 llvm::LLVMContext &VMContext; 64public: 65 66 ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false) 67 : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira), 68 VMContext(cgf.getLLVMContext()) { 69 } 70 71 //===--------------------------------------------------------------------===// 72 // Utilities 73 //===--------------------------------------------------------------------===// 74 75 bool TestAndClearIgnoreResultAssign() { 76 bool I = IgnoreResultAssign; 77 IgnoreResultAssign = false; 78 return I; 79 } 80 81 const llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } 82 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } 83 LValue EmitCheckedLValue(const Expr *E) { return CGF.EmitCheckedLValue(E); } 84 85 Value *EmitLoadOfLValue(LValue LV, QualType T) { 86 return CGF.EmitLoadOfLValue(LV, T).getScalarVal(); 87 } 88 89 /// EmitLoadOfLValue - Given an expression with complex type that represents a 90 /// value l-value, this method emits the address of the l-value, then loads 91 /// and returns the result. 92 Value *EmitLoadOfLValue(const Expr *E) { 93 return EmitLoadOfLValue(EmitCheckedLValue(E), E->getType()); 94 } 95 96 /// EmitConversionToBool - Convert the specified expression value to a 97 /// boolean (i1) truth value. This is equivalent to "Val != 0". 98 Value *EmitConversionToBool(Value *Src, QualType DstTy); 99 100 /// EmitScalarConversion - Emit a conversion from the specified type to the 101 /// specified destination type, both of which are LLVM scalar types. 102 Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy); 103 104 /// EmitComplexToScalarConversion - Emit a conversion from the specified 105 /// complex type to the specified destination type, where the destination type 106 /// is an LLVM scalar type. 107 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, 108 QualType SrcTy, QualType DstTy); 109 110 /// EmitNullValue - Emit a value that corresponds to null for the given type. 111 Value *EmitNullValue(QualType Ty); 112 113 /// EmitFloatToBoolConversion - Perform an FP to boolean conversion. 114 Value *EmitFloatToBoolConversion(Value *V) { 115 // Compare against 0.0 for fp scalars. 116 llvm::Value *Zero = llvm::Constant::getNullValue(V->getType()); 117 return Builder.CreateFCmpUNE(V, Zero, "tobool"); 118 } 119 120 /// EmitPointerToBoolConversion - Perform a pointer to boolean conversion. 121 Value *EmitPointerToBoolConversion(Value *V) { 122 Value *Zero = llvm::ConstantPointerNull::get( 123 cast<llvm::PointerType>(V->getType())); 124 return Builder.CreateICmpNE(V, Zero, "tobool"); 125 } 126 127 Value *EmitIntToBoolConversion(Value *V) { 128 // Because of the type rules of C, we often end up computing a 129 // logical value, then zero extending it to int, then wanting it 130 // as a logical value again. Optimize this common case. 131 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(V)) { 132 if (ZI->getOperand(0)->getType() == Builder.getInt1Ty()) { 133 Value *Result = ZI->getOperand(0); 134 // If there aren't any more uses, zap the instruction to save space. 135 // Note that there can be more uses, for example if this 136 // is the result of an assignment. 137 if (ZI->use_empty()) 138 ZI->eraseFromParent(); 139 return Result; 140 } 141 } 142 143 const llvm::IntegerType *Ty = cast<llvm::IntegerType>(V->getType()); 144 Value *Zero = llvm::ConstantInt::get(Ty, 0); 145 return Builder.CreateICmpNE(V, Zero, "tobool"); 146 } 147 148 //===--------------------------------------------------------------------===// 149 // Visitor Methods 150 //===--------------------------------------------------------------------===// 151 152 Value *Visit(Expr *E) { 153 llvm::DenseMap<const Expr *, llvm::Value *>::iterator I = 154 CGF.ConditionalSaveExprs.find(E); 155 if (I != CGF.ConditionalSaveExprs.end()) 156 return I->second; 157 158 return StmtVisitor<ScalarExprEmitter, Value*>::Visit(E); 159 } 160 161 Value *VisitStmt(Stmt *S) { 162 S->dump(CGF.getContext().getSourceManager()); 163 assert(0 && "Stmt can't have complex result type!"); 164 return 0; 165 } 166 Value *VisitExpr(Expr *S); 167 168 Value *VisitParenExpr(ParenExpr *PE) { 169 return Visit(PE->getSubExpr()); 170 } 171 172 // Leaves. 173 Value *VisitIntegerLiteral(const IntegerLiteral *E) { 174 return llvm::ConstantInt::get(VMContext, E->getValue()); 175 } 176 Value *VisitFloatingLiteral(const FloatingLiteral *E) { 177 return llvm::ConstantFP::get(VMContext, E->getValue()); 178 } 179 Value *VisitCharacterLiteral(const CharacterLiteral *E) { 180 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); 181 } 182 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { 183 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); 184 } 185 Value *VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { 186 return EmitNullValue(E->getType()); 187 } 188 Value *VisitGNUNullExpr(const GNUNullExpr *E) { 189 return EmitNullValue(E->getType()); 190 } 191 Value *VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) { 192 return llvm::ConstantInt::get(ConvertType(E->getType()), 193 CGF.getContext().typesAreCompatible( 194 E->getArgType1(), E->getArgType2())); 195 } 196 Value *VisitOffsetOfExpr(OffsetOfExpr *E); 197 Value *VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E); 198 Value *VisitAddrLabelExpr(const AddrLabelExpr *E) { 199 llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel()); 200 return Builder.CreateBitCast(V, ConvertType(E->getType())); 201 } 202 203 // l-values. 204 Value *VisitDeclRefExpr(DeclRefExpr *E) { 205 Expr::EvalResult Result; 206 if (!E->Evaluate(Result, CGF.getContext())) 207 return EmitLoadOfLValue(E); 208 209 assert(!Result.HasSideEffects && "Constant declref with side-effect?!"); 210 211 llvm::Constant *C; 212 if (Result.Val.isInt()) { 213 C = llvm::ConstantInt::get(VMContext, Result.Val.getInt()); 214 } else if (Result.Val.isFloat()) { 215 C = llvm::ConstantFP::get(VMContext, Result.Val.getFloat()); 216 } else { 217 return EmitLoadOfLValue(E); 218 } 219 220 // Make sure we emit a debug reference to the global variable. 221 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { 222 if (!CGF.getContext().DeclMustBeEmitted(VD)) 223 CGF.EmitDeclRefExprDbgValue(E, C); 224 } else if (isa<EnumConstantDecl>(E->getDecl())) { 225 CGF.EmitDeclRefExprDbgValue(E, C); 226 } 227 228 return C; 229 } 230 Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) { 231 return CGF.EmitObjCSelectorExpr(E); 232 } 233 Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) { 234 return CGF.EmitObjCProtocolExpr(E); 235 } 236 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 237 return EmitLoadOfLValue(E); 238 } 239 Value *VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { 240 return EmitLoadOfLValue(E); 241 } 242 Value *VisitObjCImplicitSetterGetterRefExpr( 243 ObjCImplicitSetterGetterRefExpr *E) { 244 return EmitLoadOfLValue(E); 245 } 246 Value *VisitObjCMessageExpr(ObjCMessageExpr *E) { 247 return CGF.EmitObjCMessageExpr(E).getScalarVal(); 248 } 249 250 Value *VisitObjCIsaExpr(ObjCIsaExpr *E) { 251 LValue LV = CGF.EmitObjCIsaExpr(E); 252 Value *V = CGF.EmitLoadOfLValue(LV, E->getType()).getScalarVal(); 253 return V; 254 } 255 256 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); 257 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E); 258 Value *VisitMemberExpr(MemberExpr *E); 259 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } 260 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 261 return EmitLoadOfLValue(E); 262 } 263 264 Value *VisitInitListExpr(InitListExpr *E); 265 266 Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { 267 return CGF.CGM.EmitNullConstant(E->getType()); 268 } 269 Value *VisitCastExpr(CastExpr *E) { 270 // Make sure to evaluate VLA bounds now so that we have them for later. 271 if (E->getType()->isVariablyModifiedType()) 272 CGF.EmitVLASize(E->getType()); 273 274 return EmitCastExpr(E); 275 } 276 Value *EmitCastExpr(CastExpr *E); 277 278 Value *VisitCallExpr(const CallExpr *E) { 279 if (E->getCallReturnType()->isReferenceType()) 280 return EmitLoadOfLValue(E); 281 282 return CGF.EmitCallExpr(E).getScalarVal(); 283 } 284 285 Value *VisitStmtExpr(const StmtExpr *E); 286 287 Value *VisitBlockDeclRefExpr(const BlockDeclRefExpr *E); 288 289 // Unary Operators. 290 Value *VisitUnaryPostDec(const UnaryOperator *E) { 291 LValue LV = EmitLValue(E->getSubExpr()); 292 return EmitScalarPrePostIncDec(E, LV, false, false); 293 } 294 Value *VisitUnaryPostInc(const UnaryOperator *E) { 295 LValue LV = EmitLValue(E->getSubExpr()); 296 return EmitScalarPrePostIncDec(E, LV, true, false); 297 } 298 Value *VisitUnaryPreDec(const UnaryOperator *E) { 299 LValue LV = EmitLValue(E->getSubExpr()); 300 return EmitScalarPrePostIncDec(E, LV, false, true); 301 } 302 Value *VisitUnaryPreInc(const UnaryOperator *E) { 303 LValue LV = EmitLValue(E->getSubExpr()); 304 return EmitScalarPrePostIncDec(E, LV, true, true); 305 } 306 307 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 308 bool isInc, bool isPre); 309 310 311 Value *VisitUnaryAddrOf(const UnaryOperator *E) { 312 // If the sub-expression is an instance member reference, 313 // EmitDeclRefLValue will magically emit it with the appropriate 314 // value as the "address". 315 return EmitLValue(E->getSubExpr()).getAddress(); 316 } 317 Value *VisitUnaryDeref(const Expr *E) { return EmitLoadOfLValue(E); } 318 Value *VisitUnaryPlus(const UnaryOperator *E) { 319 // This differs from gcc, though, most likely due to a bug in gcc. 320 TestAndClearIgnoreResultAssign(); 321 return Visit(E->getSubExpr()); 322 } 323 Value *VisitUnaryMinus (const UnaryOperator *E); 324 Value *VisitUnaryNot (const UnaryOperator *E); 325 Value *VisitUnaryLNot (const UnaryOperator *E); 326 Value *VisitUnaryReal (const UnaryOperator *E); 327 Value *VisitUnaryImag (const UnaryOperator *E); 328 Value *VisitUnaryExtension(const UnaryOperator *E) { 329 return Visit(E->getSubExpr()); 330 } 331 332 // C++ 333 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 334 return Visit(DAE->getExpr()); 335 } 336 Value *VisitCXXThisExpr(CXXThisExpr *TE) { 337 return CGF.LoadCXXThis(); 338 } 339 340 Value *VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { 341 return CGF.EmitCXXExprWithTemporaries(E).getScalarVal(); 342 } 343 Value *VisitCXXNewExpr(const CXXNewExpr *E) { 344 return CGF.EmitCXXNewExpr(E); 345 } 346 Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) { 347 CGF.EmitCXXDeleteExpr(E); 348 return 0; 349 } 350 Value *VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { 351 return llvm::ConstantInt::get(Builder.getInt1Ty(), E->getValue()); 352 } 353 354 Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) { 355 // C++ [expr.pseudo]p1: 356 // The result shall only be used as the operand for the function call 357 // operator (), and the result of such a call has type void. The only 358 // effect is the evaluation of the postfix-expression before the dot or 359 // arrow. 360 CGF.EmitScalarExpr(E->getBase()); 361 return 0; 362 } 363 364 Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { 365 return EmitNullValue(E->getType()); 366 } 367 368 Value *VisitCXXThrowExpr(const CXXThrowExpr *E) { 369 CGF.EmitCXXThrowExpr(E); 370 return 0; 371 } 372 373 Value *VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { 374 return llvm::ConstantInt::get(Builder.getInt1Ty(), E->getValue()); 375 } 376 377 // Binary Operators. 378 Value *EmitMul(const BinOpInfo &Ops) { 379 if (Ops.Ty->hasSignedIntegerRepresentation()) { 380 switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { 381 case LangOptions::SOB_Undefined: 382 return Builder.CreateNSWMul(Ops.LHS, Ops.RHS, "mul"); 383 case LangOptions::SOB_Defined: 384 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); 385 case LangOptions::SOB_Trapping: 386 return EmitOverflowCheckedBinOp(Ops); 387 } 388 } 389 390 if (Ops.LHS->getType()->isFPOrFPVectorTy()) 391 return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul"); 392 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); 393 } 394 bool isTrapvOverflowBehavior() { 395 return CGF.getContext().getLangOptions().getSignedOverflowBehavior() 396 == LangOptions::SOB_Trapping; 397 } 398 /// Create a binary op that checks for overflow. 399 /// Currently only supports +, - and *. 400 Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops); 401 // Emit the overflow BB when -ftrapv option is activated. 402 void EmitOverflowBB(llvm::BasicBlock *overflowBB) { 403 Builder.SetInsertPoint(overflowBB); 404 llvm::Function *Trap = CGF.CGM.getIntrinsic(llvm::Intrinsic::trap); 405 Builder.CreateCall(Trap); 406 Builder.CreateUnreachable(); 407 } 408 // Check for undefined division and modulus behaviors. 409 void EmitUndefinedBehaviorIntegerDivAndRemCheck(const BinOpInfo &Ops, 410 llvm::Value *Zero,bool isDiv); 411 Value *EmitDiv(const BinOpInfo &Ops); 412 Value *EmitRem(const BinOpInfo &Ops); 413 Value *EmitAdd(const BinOpInfo &Ops); 414 Value *EmitSub(const BinOpInfo &Ops); 415 Value *EmitShl(const BinOpInfo &Ops); 416 Value *EmitShr(const BinOpInfo &Ops); 417 Value *EmitAnd(const BinOpInfo &Ops) { 418 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); 419 } 420 Value *EmitXor(const BinOpInfo &Ops) { 421 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); 422 } 423 Value *EmitOr (const BinOpInfo &Ops) { 424 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); 425 } 426 427 BinOpInfo EmitBinOps(const BinaryOperator *E); 428 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E, 429 Value *(ScalarExprEmitter::*F)(const BinOpInfo &), 430 Value *&Result); 431 432 Value *EmitCompoundAssign(const CompoundAssignOperator *E, 433 Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); 434 435 // Binary operators and binary compound assignment operators. 436#define HANDLEBINOP(OP) \ 437 Value *VisitBin ## OP(const BinaryOperator *E) { \ 438 return Emit ## OP(EmitBinOps(E)); \ 439 } \ 440 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ 441 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ 442 } 443 HANDLEBINOP(Mul) 444 HANDLEBINOP(Div) 445 HANDLEBINOP(Rem) 446 HANDLEBINOP(Add) 447 HANDLEBINOP(Sub) 448 HANDLEBINOP(Shl) 449 HANDLEBINOP(Shr) 450 HANDLEBINOP(And) 451 HANDLEBINOP(Xor) 452 HANDLEBINOP(Or) 453#undef HANDLEBINOP 454 455 // Comparisons. 456 Value *EmitCompare(const BinaryOperator *E, unsigned UICmpOpc, 457 unsigned SICmpOpc, unsigned FCmpOpc); 458#define VISITCOMP(CODE, UI, SI, FP) \ 459 Value *VisitBin##CODE(const BinaryOperator *E) { \ 460 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ 461 llvm::FCmpInst::FP); } 462 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT) 463 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT) 464 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE) 465 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE) 466 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ) 467 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE) 468#undef VISITCOMP 469 470 Value *VisitBinAssign (const BinaryOperator *E); 471 472 Value *VisitBinLAnd (const BinaryOperator *E); 473 Value *VisitBinLOr (const BinaryOperator *E); 474 Value *VisitBinComma (const BinaryOperator *E); 475 476 Value *VisitBinPtrMemD(const Expr *E) { return EmitLoadOfLValue(E); } 477 Value *VisitBinPtrMemI(const Expr *E) { return EmitLoadOfLValue(E); } 478 479 // Other Operators. 480 Value *VisitBlockExpr(const BlockExpr *BE); 481 Value *VisitConditionalOperator(const ConditionalOperator *CO); 482 Value *VisitChooseExpr(ChooseExpr *CE); 483 Value *VisitVAArgExpr(VAArgExpr *VE); 484 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { 485 return CGF.EmitObjCStringLiteral(E); 486 } 487}; 488} // end anonymous namespace. 489 490//===----------------------------------------------------------------------===// 491// Utilities 492//===----------------------------------------------------------------------===// 493 494/// EmitConversionToBool - Convert the specified expression value to a 495/// boolean (i1) truth value. This is equivalent to "Val != 0". 496Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { 497 assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs"); 498 499 if (SrcType->isRealFloatingType()) 500 return EmitFloatToBoolConversion(Src); 501 502 if (const MemberPointerType *MPT = dyn_cast<MemberPointerType>(SrcType)) 503 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, Src, MPT); 504 505 assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) && 506 "Unknown scalar type to convert"); 507 508 if (isa<llvm::IntegerType>(Src->getType())) 509 return EmitIntToBoolConversion(Src); 510 511 assert(isa<llvm::PointerType>(Src->getType())); 512 return EmitPointerToBoolConversion(Src); 513} 514 515/// EmitScalarConversion - Emit a conversion from the specified type to the 516/// specified destination type, both of which are LLVM scalar types. 517Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, 518 QualType DstType) { 519 SrcType = CGF.getContext().getCanonicalType(SrcType); 520 DstType = CGF.getContext().getCanonicalType(DstType); 521 if (SrcType == DstType) return Src; 522 523 if (DstType->isVoidType()) return 0; 524 525 // Handle conversions to bool first, they are special: comparisons against 0. 526 if (DstType->isBooleanType()) 527 return EmitConversionToBool(Src, SrcType); 528 529 const llvm::Type *DstTy = ConvertType(DstType); 530 531 // Ignore conversions like int -> uint. 532 if (Src->getType() == DstTy) 533 return Src; 534 535 // Handle pointer conversions next: pointers can only be converted to/from 536 // other pointers and integers. Check for pointer types in terms of LLVM, as 537 // some native types (like Obj-C id) may map to a pointer type. 538 if (isa<llvm::PointerType>(DstTy)) { 539 // The source value may be an integer, or a pointer. 540 if (isa<llvm::PointerType>(Src->getType())) 541 return Builder.CreateBitCast(Src, DstTy, "conv"); 542 543 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); 544 // First, convert to the correct width so that we control the kind of 545 // extension. 546 const llvm::Type *MiddleTy = CGF.IntPtrTy; 547 bool InputSigned = SrcType->isSignedIntegerType(); 548 llvm::Value* IntResult = 549 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); 550 // Then, cast to pointer. 551 return Builder.CreateIntToPtr(IntResult, DstTy, "conv"); 552 } 553 554 if (isa<llvm::PointerType>(Src->getType())) { 555 // Must be an ptr to int cast. 556 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); 557 return Builder.CreatePtrToInt(Src, DstTy, "conv"); 558 } 559 560 // A scalar can be splatted to an extended vector of the same element type 561 if (DstType->isExtVectorType() && !SrcType->isVectorType()) { 562 // Cast the scalar to element type 563 QualType EltTy = DstType->getAs<ExtVectorType>()->getElementType(); 564 llvm::Value *Elt = EmitScalarConversion(Src, SrcType, EltTy); 565 566 // Insert the element in element zero of an undef vector 567 llvm::Value *UnV = llvm::UndefValue::get(DstTy); 568 llvm::Value *Idx = llvm::ConstantInt::get(CGF.Int32Ty, 0); 569 UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp"); 570 571 // Splat the element across to all elements 572 llvm::SmallVector<llvm::Constant*, 16> Args; 573 unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); 574 for (unsigned i = 0; i < NumElements; i++) 575 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 0)); 576 577 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); 578 llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); 579 return Yay; 580 } 581 582 // Allow bitcast from vector to integer/fp of the same size. 583 if (isa<llvm::VectorType>(Src->getType()) || 584 isa<llvm::VectorType>(DstTy)) 585 return Builder.CreateBitCast(Src, DstTy, "conv"); 586 587 // Finally, we have the arithmetic types: real int/float. 588 if (isa<llvm::IntegerType>(Src->getType())) { 589 bool InputSigned = SrcType->isSignedIntegerType(); 590 if (isa<llvm::IntegerType>(DstTy)) 591 return Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); 592 else if (InputSigned) 593 return Builder.CreateSIToFP(Src, DstTy, "conv"); 594 else 595 return Builder.CreateUIToFP(Src, DstTy, "conv"); 596 } 597 598 assert(Src->getType()->isFloatingPointTy() && "Unknown real conversion"); 599 if (isa<llvm::IntegerType>(DstTy)) { 600 if (DstType->isSignedIntegerType()) 601 return Builder.CreateFPToSI(Src, DstTy, "conv"); 602 else 603 return Builder.CreateFPToUI(Src, DstTy, "conv"); 604 } 605 606 assert(DstTy->isFloatingPointTy() && "Unknown real conversion"); 607 if (DstTy->getTypeID() < Src->getType()->getTypeID()) 608 return Builder.CreateFPTrunc(Src, DstTy, "conv"); 609 else 610 return Builder.CreateFPExt(Src, DstTy, "conv"); 611} 612 613/// EmitComplexToScalarConversion - Emit a conversion from the specified complex 614/// type to the specified destination type, where the destination type is an 615/// LLVM scalar type. 616Value *ScalarExprEmitter:: 617EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, 618 QualType SrcTy, QualType DstTy) { 619 // Get the source element type. 620 SrcTy = SrcTy->getAs<ComplexType>()->getElementType(); 621 622 // Handle conversions to bool first, they are special: comparisons against 0. 623 if (DstTy->isBooleanType()) { 624 // Complex != 0 -> (Real != 0) | (Imag != 0) 625 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy); 626 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy); 627 return Builder.CreateOr(Src.first, Src.second, "tobool"); 628 } 629 630 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, 631 // the imaginary part of the complex value is discarded and the value of the 632 // real part is converted according to the conversion rules for the 633 // corresponding real type. 634 return EmitScalarConversion(Src.first, SrcTy, DstTy); 635} 636 637Value *ScalarExprEmitter::EmitNullValue(QualType Ty) { 638 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) 639 return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT); 640 641 return llvm::Constant::getNullValue(ConvertType(Ty)); 642} 643 644//===----------------------------------------------------------------------===// 645// Visitor Methods 646//===----------------------------------------------------------------------===// 647 648Value *ScalarExprEmitter::VisitExpr(Expr *E) { 649 CGF.ErrorUnsupported(E, "scalar expression"); 650 if (E->getType()->isVoidType()) 651 return 0; 652 return llvm::UndefValue::get(CGF.ConvertType(E->getType())); 653} 654 655Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { 656 // Vector Mask Case 657 if (E->getNumSubExprs() == 2 || 658 (E->getNumSubExprs() == 3 && E->getExpr(2)->getType()->isVectorType())) { 659 Value *LHS = CGF.EmitScalarExpr(E->getExpr(0)); 660 Value *RHS = CGF.EmitScalarExpr(E->getExpr(1)); 661 Value *Mask; 662 663 const llvm::VectorType *LTy = cast<llvm::VectorType>(LHS->getType()); 664 unsigned LHSElts = LTy->getNumElements(); 665 666 if (E->getNumSubExprs() == 3) { 667 Mask = CGF.EmitScalarExpr(E->getExpr(2)); 668 669 // Shuffle LHS & RHS into one input vector. 670 llvm::SmallVector<llvm::Constant*, 32> concat; 671 for (unsigned i = 0; i != LHSElts; ++i) { 672 concat.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 2*i)); 673 concat.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 2*i+1)); 674 } 675 676 Value* CV = llvm::ConstantVector::get(concat.begin(), concat.size()); 677 LHS = Builder.CreateShuffleVector(LHS, RHS, CV, "concat"); 678 LHSElts *= 2; 679 } else { 680 Mask = RHS; 681 } 682 683 const llvm::VectorType *MTy = cast<llvm::VectorType>(Mask->getType()); 684 llvm::Constant* EltMask; 685 686 // Treat vec3 like vec4. 687 if ((LHSElts == 6) && (E->getNumSubExprs() == 3)) 688 EltMask = llvm::ConstantInt::get(MTy->getElementType(), 689 (1 << llvm::Log2_32(LHSElts+2))-1); 690 else if ((LHSElts == 3) && (E->getNumSubExprs() == 2)) 691 EltMask = llvm::ConstantInt::get(MTy->getElementType(), 692 (1 << llvm::Log2_32(LHSElts+1))-1); 693 else 694 EltMask = llvm::ConstantInt::get(MTy->getElementType(), 695 (1 << llvm::Log2_32(LHSElts))-1); 696 697 // Mask off the high bits of each shuffle index. 698 llvm::SmallVector<llvm::Constant *, 32> MaskV; 699 for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) 700 MaskV.push_back(EltMask); 701 702 Value* MaskBits = llvm::ConstantVector::get(MaskV.begin(), MaskV.size()); 703 Mask = Builder.CreateAnd(Mask, MaskBits, "mask"); 704 705 // newv = undef 706 // mask = mask & maskbits 707 // for each elt 708 // n = extract mask i 709 // x = extract val n 710 // newv = insert newv, x, i 711 const llvm::VectorType *RTy = llvm::VectorType::get(LTy->getElementType(), 712 MTy->getNumElements()); 713 Value* NewV = llvm::UndefValue::get(RTy); 714 for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) { 715 Value *Indx = llvm::ConstantInt::get(CGF.Int32Ty, i); 716 Indx = Builder.CreateExtractElement(Mask, Indx, "shuf_idx"); 717 Indx = Builder.CreateZExt(Indx, CGF.Int32Ty, "idx_zext"); 718 719 // Handle vec3 special since the index will be off by one for the RHS. 720 if ((LHSElts == 6) && (E->getNumSubExprs() == 3)) { 721 Value *cmpIndx, *newIndx; 722 cmpIndx = Builder.CreateICmpUGT(Indx, 723 llvm::ConstantInt::get(CGF.Int32Ty, 3), 724 "cmp_shuf_idx"); 725 newIndx = Builder.CreateSub(Indx, llvm::ConstantInt::get(CGF.Int32Ty,1), 726 "shuf_idx_adj"); 727 Indx = Builder.CreateSelect(cmpIndx, newIndx, Indx, "sel_shuf_idx"); 728 } 729 Value *VExt = Builder.CreateExtractElement(LHS, Indx, "shuf_elt"); 730 NewV = Builder.CreateInsertElement(NewV, VExt, Indx, "shuf_ins"); 731 } 732 return NewV; 733 } 734 735 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0)); 736 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1)); 737 738 // Handle vec3 special since the index will be off by one for the RHS. 739 llvm::SmallVector<llvm::Constant*, 32> indices; 740 for (unsigned i = 2; i < E->getNumSubExprs(); i++) { 741 llvm::Constant *C = cast<llvm::Constant>(CGF.EmitScalarExpr(E->getExpr(i))); 742 const llvm::VectorType *VTy = cast<llvm::VectorType>(V1->getType()); 743 if (VTy->getNumElements() == 3) { 744 if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) { 745 uint64_t cVal = CI->getZExtValue(); 746 if (cVal > 3) { 747 C = llvm::ConstantInt::get(C->getType(), cVal-1); 748 } 749 } 750 } 751 indices.push_back(C); 752 } 753 754 Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size()); 755 return Builder.CreateShuffleVector(V1, V2, SV, "shuffle"); 756} 757Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) { 758 Expr::EvalResult Result; 759 if (E->Evaluate(Result, CGF.getContext()) && Result.Val.isInt()) { 760 if (E->isArrow()) 761 CGF.EmitScalarExpr(E->getBase()); 762 else 763 EmitLValue(E->getBase()); 764 return llvm::ConstantInt::get(VMContext, Result.Val.getInt()); 765 } 766 767 // Emit debug info for aggregate now, if it was delayed to reduce 768 // debug info size. 769 CGDebugInfo *DI = CGF.getDebugInfo(); 770 if (DI && CGF.CGM.getCodeGenOpts().LimitDebugInfo) { 771 QualType PQTy = E->getBase()->IgnoreParenImpCasts()->getType(); 772 if (const PointerType * PTy = dyn_cast<PointerType>(PQTy)) 773 if (FieldDecl *M = dyn_cast<FieldDecl>(E->getMemberDecl())) 774 DI->getOrCreateRecordType(PTy->getPointeeType(), 775 M->getParent()->getLocation()); 776 } 777 return EmitLoadOfLValue(E); 778} 779 780Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 781 TestAndClearIgnoreResultAssign(); 782 783 // Emit subscript expressions in rvalue context's. For most cases, this just 784 // loads the lvalue formed by the subscript expr. However, we have to be 785 // careful, because the base of a vector subscript is occasionally an rvalue, 786 // so we can't get it as an lvalue. 787 if (!E->getBase()->getType()->isVectorType()) 788 return EmitLoadOfLValue(E); 789 790 // Handle the vector case. The base must be a vector, the index must be an 791 // integer value. 792 Value *Base = Visit(E->getBase()); 793 Value *Idx = Visit(E->getIdx()); 794 bool IdxSigned = E->getIdx()->getType()->isSignedIntegerType(); 795 Idx = Builder.CreateIntCast(Idx, CGF.Int32Ty, IdxSigned, "vecidxcast"); 796 return Builder.CreateExtractElement(Base, Idx, "vecext"); 797} 798 799static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx, 800 unsigned Off, const llvm::Type *I32Ty) { 801 int MV = SVI->getMaskValue(Idx); 802 if (MV == -1) 803 return llvm::UndefValue::get(I32Ty); 804 return llvm::ConstantInt::get(I32Ty, Off+MV); 805} 806 807Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) { 808 bool Ignore = TestAndClearIgnoreResultAssign(); 809 (void)Ignore; 810 assert (Ignore == false && "init list ignored"); 811 unsigned NumInitElements = E->getNumInits(); 812 813 if (E->hadArrayRangeDesignator()) 814 CGF.ErrorUnsupported(E, "GNU array range designator extension"); 815 816 const llvm::VectorType *VType = 817 dyn_cast<llvm::VectorType>(ConvertType(E->getType())); 818 819 // We have a scalar in braces. Just use the first element. 820 if (!VType) 821 return Visit(E->getInit(0)); 822 823 unsigned ResElts = VType->getNumElements(); 824 825 // Loop over initializers collecting the Value for each, and remembering 826 // whether the source was swizzle (ExtVectorElementExpr). This will allow 827 // us to fold the shuffle for the swizzle into the shuffle for the vector 828 // initializer, since LLVM optimizers generally do not want to touch 829 // shuffles. 830 unsigned CurIdx = 0; 831 bool VIsUndefShuffle = false; 832 llvm::Value *V = llvm::UndefValue::get(VType); 833 for (unsigned i = 0; i != NumInitElements; ++i) { 834 Expr *IE = E->getInit(i); 835 Value *Init = Visit(IE); 836 llvm::SmallVector<llvm::Constant*, 16> Args; 837 838 const llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType()); 839 840 // Handle scalar elements. If the scalar initializer is actually one 841 // element of a different vector of the same width, use shuffle instead of 842 // extract+insert. 843 if (!VVT) { 844 if (isa<ExtVectorElementExpr>(IE)) { 845 llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init); 846 847 if (EI->getVectorOperandType()->getNumElements() == ResElts) { 848 llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand()); 849 Value *LHS = 0, *RHS = 0; 850 if (CurIdx == 0) { 851 // insert into undef -> shuffle (src, undef) 852 Args.push_back(C); 853 for (unsigned j = 1; j != ResElts; ++j) 854 Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); 855 856 LHS = EI->getVectorOperand(); 857 RHS = V; 858 VIsUndefShuffle = true; 859 } else if (VIsUndefShuffle) { 860 // insert into undefshuffle && size match -> shuffle (v, src) 861 llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V); 862 for (unsigned j = 0; j != CurIdx; ++j) 863 Args.push_back(getMaskElt(SVV, j, 0, CGF.Int32Ty)); 864 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, 865 ResElts + C->getZExtValue())); 866 for (unsigned j = CurIdx + 1; j != ResElts; ++j) 867 Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); 868 869 LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); 870 RHS = EI->getVectorOperand(); 871 VIsUndefShuffle = false; 872 } 873 if (!Args.empty()) { 874 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); 875 V = Builder.CreateShuffleVector(LHS, RHS, Mask); 876 ++CurIdx; 877 continue; 878 } 879 } 880 } 881 Value *Idx = llvm::ConstantInt::get(CGF.Int32Ty, CurIdx); 882 V = Builder.CreateInsertElement(V, Init, Idx, "vecinit"); 883 VIsUndefShuffle = false; 884 ++CurIdx; 885 continue; 886 } 887 888 unsigned InitElts = VVT->getNumElements(); 889 890 // If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's 891 // input is the same width as the vector being constructed, generate an 892 // optimized shuffle of the swizzle input into the result. 893 unsigned Offset = (CurIdx == 0) ? 0 : ResElts; 894 if (isa<ExtVectorElementExpr>(IE)) { 895 llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init); 896 Value *SVOp = SVI->getOperand(0); 897 const llvm::VectorType *OpTy = cast<llvm::VectorType>(SVOp->getType()); 898 899 if (OpTy->getNumElements() == ResElts) { 900 for (unsigned j = 0; j != CurIdx; ++j) { 901 // If the current vector initializer is a shuffle with undef, merge 902 // this shuffle directly into it. 903 if (VIsUndefShuffle) { 904 Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0, 905 CGF.Int32Ty)); 906 } else { 907 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, j)); 908 } 909 } 910 for (unsigned j = 0, je = InitElts; j != je; ++j) 911 Args.push_back(getMaskElt(SVI, j, Offset, CGF.Int32Ty)); 912 for (unsigned j = CurIdx + InitElts; j != ResElts; ++j) 913 Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); 914 915 if (VIsUndefShuffle) 916 V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); 917 918 Init = SVOp; 919 } 920 } 921 922 // Extend init to result vector length, and then shuffle its contribution 923 // to the vector initializer into V. 924 if (Args.empty()) { 925 for (unsigned j = 0; j != InitElts; ++j) 926 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, j)); 927 for (unsigned j = InitElts; j != ResElts; ++j) 928 Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); 929 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); 930 Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT), 931 Mask, "vext"); 932 933 Args.clear(); 934 for (unsigned j = 0; j != CurIdx; ++j) 935 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, j)); 936 for (unsigned j = 0; j != InitElts; ++j) 937 Args.push_back(llvm::ConstantInt::get(CGF.Int32Ty, j+Offset)); 938 for (unsigned j = CurIdx + InitElts; j != ResElts; ++j) 939 Args.push_back(llvm::UndefValue::get(CGF.Int32Ty)); 940 } 941 942 // If V is undef, make sure it ends up on the RHS of the shuffle to aid 943 // merging subsequent shuffles into this one. 944 if (CurIdx == 0) 945 std::swap(V, Init); 946 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], ResElts); 947 V = Builder.CreateShuffleVector(V, Init, Mask, "vecinit"); 948 VIsUndefShuffle = isa<llvm::UndefValue>(Init); 949 CurIdx += InitElts; 950 } 951 952 // FIXME: evaluate codegen vs. shuffling against constant null vector. 953 // Emit remaining default initializers. 954 const llvm::Type *EltTy = VType->getElementType(); 955 956 // Emit remaining default initializers 957 for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) { 958 Value *Idx = llvm::ConstantInt::get(CGF.Int32Ty, CurIdx); 959 llvm::Value *Init = llvm::Constant::getNullValue(EltTy); 960 V = Builder.CreateInsertElement(V, Init, Idx, "vecinit"); 961 } 962 return V; 963} 964 965static bool ShouldNullCheckClassCastValue(const CastExpr *CE) { 966 const Expr *E = CE->getSubExpr(); 967 968 if (CE->getCastKind() == CK_UncheckedDerivedToBase) 969 return false; 970 971 if (isa<CXXThisExpr>(E)) { 972 // We always assume that 'this' is never null. 973 return false; 974 } 975 976 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) { 977 // And that glvalue casts are never null. 978 if (ICE->getValueKind() != VK_RValue) 979 return false; 980 } 981 982 return true; 983} 984 985// VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts 986// have to handle a more broad range of conversions than explicit casts, as they 987// handle things like function to ptr-to-function decay etc. 988Value *ScalarExprEmitter::EmitCastExpr(CastExpr *CE) { 989 Expr *E = CE->getSubExpr(); 990 QualType DestTy = CE->getType(); 991 CastKind Kind = CE->getCastKind(); 992 993 if (!DestTy->isVoidType()) 994 TestAndClearIgnoreResultAssign(); 995 996 // Since almost all cast kinds apply to scalars, this switch doesn't have 997 // a default case, so the compiler will warn on a missing case. The cases 998 // are in the same order as in the CastKind enum. 999 switch (Kind) { 1000 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!"); 1001 1002 case CK_Unknown: 1003 // FIXME: All casts should have a known kind! 1004 //assert(0 && "Unknown cast kind!"); 1005 break; 1006 1007 case CK_LValueBitCast: 1008 case CK_ObjCObjectLValueCast: { 1009 Value *V = EmitLValue(E).getAddress(); 1010 V = Builder.CreateBitCast(V, 1011 ConvertType(CGF.getContext().getPointerType(DestTy))); 1012 return EmitLoadOfLValue(CGF.MakeAddrLValue(V, DestTy), DestTy); 1013 } 1014 1015 case CK_AnyPointerToObjCPointerCast: 1016 case CK_AnyPointerToBlockPointerCast: 1017 case CK_BitCast: { 1018 Value *Src = Visit(const_cast<Expr*>(E)); 1019 return Builder.CreateBitCast(Src, ConvertType(DestTy)); 1020 } 1021 case CK_NoOp: 1022 case CK_UserDefinedConversion: 1023 return Visit(const_cast<Expr*>(E)); 1024 1025 case CK_BaseToDerived: { 1026 const CXXRecordDecl *DerivedClassDecl = 1027 DestTy->getCXXRecordDeclForPointerType(); 1028 1029 return CGF.GetAddressOfDerivedClass(Visit(E), DerivedClassDecl, 1030 CE->path_begin(), CE->path_end(), 1031 ShouldNullCheckClassCastValue(CE)); 1032 } 1033 case CK_UncheckedDerivedToBase: 1034 case CK_DerivedToBase: { 1035 const RecordType *DerivedClassTy = 1036 E->getType()->getAs<PointerType>()->getPointeeType()->getAs<RecordType>(); 1037 CXXRecordDecl *DerivedClassDecl = 1038 cast<CXXRecordDecl>(DerivedClassTy->getDecl()); 1039 1040 return CGF.GetAddressOfBaseClass(Visit(E), DerivedClassDecl, 1041 CE->path_begin(), CE->path_end(), 1042 ShouldNullCheckClassCastValue(CE)); 1043 } 1044 case CK_Dynamic: { 1045 Value *V = Visit(const_cast<Expr*>(E)); 1046 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE); 1047 return CGF.EmitDynamicCast(V, DCE); 1048 } 1049 case CK_ToUnion: 1050 assert(0 && "Should be unreachable!"); 1051 break; 1052 1053 case CK_ArrayToPointerDecay: { 1054 assert(E->getType()->isArrayType() && 1055 "Array to pointer decay must have array source type!"); 1056 1057 Value *V = EmitLValue(E).getAddress(); // Bitfields can't be arrays. 1058 1059 // Note that VLA pointers are always decayed, so we don't need to do 1060 // anything here. 1061 if (!E->getType()->isVariableArrayType()) { 1062 assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer"); 1063 assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType()) 1064 ->getElementType()) && 1065 "Expected pointer to array"); 1066 V = Builder.CreateStructGEP(V, 0, "arraydecay"); 1067 } 1068 1069 return V; 1070 } 1071 case CK_FunctionToPointerDecay: 1072 return EmitLValue(E).getAddress(); 1073 1074 case CK_NullToPointer: 1075 if (MustVisitNullValue(E)) 1076 (void) Visit(E); 1077 1078 return llvm::ConstantPointerNull::get( 1079 cast<llvm::PointerType>(ConvertType(DestTy))); 1080 1081 case CK_NullToMemberPointer: { 1082 if (MustVisitNullValue(E)) 1083 (void) Visit(E); 1084 1085 const MemberPointerType *MPT = CE->getType()->getAs<MemberPointerType>(); 1086 return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT); 1087 } 1088 1089 case CK_BaseToDerivedMemberPointer: 1090 case CK_DerivedToBaseMemberPointer: { 1091 Value *Src = Visit(E); 1092 1093 // Note that the AST doesn't distinguish between checked and 1094 // unchecked member pointer conversions, so we always have to 1095 // implement checked conversions here. This is inefficient when 1096 // actual control flow may be required in order to perform the 1097 // check, which it is for data member pointers (but not member 1098 // function pointers on Itanium and ARM). 1099 return CGF.CGM.getCXXABI().EmitMemberPointerConversion(CGF, CE, Src); 1100 } 1101 1102 case CK_FloatingRealToComplex: 1103 case CK_FloatingComplexCast: 1104 case CK_IntegralRealToComplex: 1105 case CK_IntegralComplexCast: 1106 case CK_IntegralComplexToFloatingComplex: 1107 case CK_FloatingComplexToIntegralComplex: 1108 case CK_ConstructorConversion: 1109 assert(0 && "Should be unreachable!"); 1110 break; 1111 1112 case CK_IntegralToPointer: { 1113 Value *Src = Visit(const_cast<Expr*>(E)); 1114 1115 // First, convert to the correct width so that we control the kind of 1116 // extension. 1117 const llvm::Type *MiddleTy = CGF.IntPtrTy; 1118 bool InputSigned = E->getType()->isSignedIntegerType(); 1119 llvm::Value* IntResult = 1120 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); 1121 1122 return Builder.CreateIntToPtr(IntResult, ConvertType(DestTy)); 1123 } 1124 case CK_PointerToIntegral: { 1125 Value *Src = Visit(const_cast<Expr*>(E)); 1126 1127 // Handle conversion to bool correctly. 1128 if (DestTy->isBooleanType()) 1129 return EmitScalarConversion(Src, E->getType(), DestTy); 1130 1131 return Builder.CreatePtrToInt(Src, ConvertType(DestTy)); 1132 } 1133 case CK_ToVoid: { 1134 if (E->Classify(CGF.getContext()).isGLValue()) { 1135 LValue LV = CGF.EmitLValue(E); 1136 if (LV.isPropertyRef()) 1137 CGF.EmitLoadOfPropertyRefLValue(LV, E->getType()); 1138 else if (LV.isKVCRef()) 1139 CGF.EmitLoadOfKVCRefLValue(LV, E->getType()); 1140 } 1141 else 1142 CGF.EmitAnyExpr(E, AggValueSlot::ignored(), true); 1143 return 0; 1144 } 1145 case CK_VectorSplat: { 1146 const llvm::Type *DstTy = ConvertType(DestTy); 1147 Value *Elt = Visit(const_cast<Expr*>(E)); 1148 1149 // Insert the element in element zero of an undef vector 1150 llvm::Value *UnV = llvm::UndefValue::get(DstTy); 1151 llvm::Value *Idx = llvm::ConstantInt::get(CGF.Int32Ty, 0); 1152 UnV = Builder.CreateInsertElement(UnV, Elt, Idx, "tmp"); 1153 1154 // Splat the element across to all elements 1155 llvm::SmallVector<llvm::Constant*, 16> Args; 1156 unsigned NumElements = cast<llvm::VectorType>(DstTy)->getNumElements(); 1157 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Int32Ty, 0); 1158 for (unsigned i = 0; i < NumElements; i++) 1159 Args.push_back(Zero); 1160 1161 llvm::Constant *Mask = llvm::ConstantVector::get(&Args[0], NumElements); 1162 llvm::Value *Yay = Builder.CreateShuffleVector(UnV, UnV, Mask, "splat"); 1163 return Yay; 1164 } 1165 1166 case CK_IntegralCast: 1167 case CK_IntegralToFloating: 1168 case CK_FloatingToIntegral: 1169 case CK_FloatingCast: 1170 return EmitScalarConversion(Visit(E), E->getType(), DestTy); 1171 1172 case CK_IntegralToBoolean: 1173 return EmitIntToBoolConversion(Visit(E)); 1174 case CK_PointerToBoolean: 1175 return EmitPointerToBoolConversion(Visit(E)); 1176 case CK_FloatingToBoolean: 1177 return EmitFloatToBoolConversion(Visit(E)); 1178 case CK_MemberPointerToBoolean: { 1179 llvm::Value *MemPtr = Visit(E); 1180 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>(); 1181 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT); 1182 } 1183 1184 case CK_FloatingComplexToReal: 1185 case CK_IntegralComplexToReal: 1186 return CGF.EmitComplexExpr(E, false, true, false, true).first; 1187 1188 case CK_FloatingComplexToBoolean: 1189 case CK_IntegralComplexToBoolean: { 1190 CodeGenFunction::ComplexPairTy V 1191 = CGF.EmitComplexExpr(E, false, false, false, false); 1192 1193 // TODO: kill this function off, inline appropriate case here 1194 return EmitComplexToScalarConversion(V, E->getType(), DestTy); 1195 } 1196 1197 } 1198 1199 // Handle cases where the source is an non-complex type. 1200 1201 if (!CGF.hasAggregateLLVMType(E->getType())) { 1202 Value *Src = Visit(const_cast<Expr*>(E)); 1203 1204 // Use EmitScalarConversion to perform the conversion. 1205 return EmitScalarConversion(Src, E->getType(), DestTy); 1206 } 1207 1208 // Handle cases where the source is a complex type. 1209 // TODO: when we're certain about cast kinds, we should just be able 1210 // to assert that no complexes make it here. 1211 if (E->getType()->isAnyComplexType()) { 1212 bool IgnoreImag = true; 1213 bool IgnoreImagAssign = true; 1214 bool IgnoreReal = IgnoreResultAssign; 1215 bool IgnoreRealAssign = IgnoreResultAssign; 1216 if (DestTy->isBooleanType()) 1217 IgnoreImagAssign = IgnoreImag = false; 1218 else if (DestTy->isVoidType()) { 1219 IgnoreReal = IgnoreImag = false; 1220 IgnoreRealAssign = IgnoreImagAssign = true; 1221 } 1222 CodeGenFunction::ComplexPairTy V 1223 = CGF.EmitComplexExpr(E, IgnoreReal, IgnoreImag, IgnoreRealAssign, 1224 IgnoreImagAssign); 1225 return EmitComplexToScalarConversion(V, E->getType(), DestTy); 1226 } 1227 1228 // Okay, this is a cast from an aggregate. It must be a cast to void. Just 1229 // evaluate the result and return. 1230 CGF.EmitAggExpr(E, AggValueSlot::ignored(), true); 1231 return 0; 1232} 1233 1234Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { 1235 return CGF.EmitCompoundStmt(*E->getSubStmt(), 1236 !E->getType()->isVoidType()).getScalarVal(); 1237} 1238 1239Value *ScalarExprEmitter::VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) { 1240 llvm::Value *V = CGF.GetAddrOfBlockDecl(E); 1241 if (E->getType().isObjCGCWeak()) 1242 return CGF.CGM.getObjCRuntime().EmitObjCWeakRead(CGF, V); 1243 return CGF.EmitLoadOfScalar(V, false, 0, E->getType()); 1244} 1245 1246//===----------------------------------------------------------------------===// 1247// Unary Operators 1248//===----------------------------------------------------------------------===// 1249 1250llvm::Value *ScalarExprEmitter:: 1251EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 1252 bool isInc, bool isPre) { 1253 1254 QualType ValTy = E->getSubExpr()->getType(); 1255 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy); 1256 1257 int AmountVal = isInc ? 1 : -1; 1258 1259 if (ValTy->isPointerType() && 1260 ValTy->getAs<PointerType>()->isVariableArrayType()) { 1261 // The amount of the addition/subtraction needs to account for the VLA size 1262 CGF.ErrorUnsupported(E, "VLA pointer inc/dec"); 1263 } 1264 1265 llvm::Value *NextVal; 1266 if (const llvm::PointerType *PT = 1267 dyn_cast<llvm::PointerType>(InVal->getType())) { 1268 llvm::Constant *Inc = llvm::ConstantInt::get(CGF.Int32Ty, AmountVal); 1269 if (!isa<llvm::FunctionType>(PT->getElementType())) { 1270 QualType PTEE = ValTy->getPointeeType(); 1271 if (const ObjCObjectType *OIT = PTEE->getAs<ObjCObjectType>()) { 1272 // Handle interface types, which are not represented with a concrete 1273 // type. 1274 int size = CGF.getContext().getTypeSize(OIT) / 8; 1275 if (!isInc) 1276 size = -size; 1277 Inc = llvm::ConstantInt::get(Inc->getType(), size); 1278 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1279 InVal = Builder.CreateBitCast(InVal, i8Ty); 1280 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr"); 1281 llvm::Value *lhs = LV.getAddress(); 1282 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty)); 1283 LV = CGF.MakeAddrLValue(lhs, ValTy); 1284 } else 1285 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec"); 1286 } else { 1287 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1288 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp"); 1289 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec"); 1290 NextVal = Builder.CreateBitCast(NextVal, InVal->getType()); 1291 } 1292 } else if (InVal->getType()->isIntegerTy(1) && isInc) { 1293 // Bool++ is an interesting case, due to promotion rules, we get: 1294 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 -> 1295 // Bool = ((int)Bool+1) != 0 1296 // An interesting aspect of this is that increment is always true. 1297 // Decrement does not have this property. 1298 NextVal = llvm::ConstantInt::getTrue(VMContext); 1299 } else if (isa<llvm::IntegerType>(InVal->getType())) { 1300 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal); 1301 1302 if (!ValTy->isSignedIntegerType()) 1303 // Unsigned integer inc is always two's complement. 1304 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); 1305 else { 1306 switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { 1307 case LangOptions::SOB_Undefined: 1308 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec"); 1309 break; 1310 case LangOptions::SOB_Defined: 1311 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec"); 1312 break; 1313 case LangOptions::SOB_Trapping: 1314 BinOpInfo BinOp; 1315 BinOp.LHS = InVal; 1316 BinOp.RHS = NextVal; 1317 BinOp.Ty = E->getType(); 1318 BinOp.Opcode = BO_Add; 1319 BinOp.E = E; 1320 NextVal = EmitOverflowCheckedBinOp(BinOp); 1321 break; 1322 } 1323 } 1324 } else { 1325 // Add the inc/dec to the real part. 1326 if (InVal->getType()->isFloatTy()) 1327 NextVal = 1328 llvm::ConstantFP::get(VMContext, 1329 llvm::APFloat(static_cast<float>(AmountVal))); 1330 else if (InVal->getType()->isDoubleTy()) 1331 NextVal = 1332 llvm::ConstantFP::get(VMContext, 1333 llvm::APFloat(static_cast<double>(AmountVal))); 1334 else { 1335 llvm::APFloat F(static_cast<float>(AmountVal)); 1336 bool ignored; 1337 F.convert(CGF.Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero, 1338 &ignored); 1339 NextVal = llvm::ConstantFP::get(VMContext, F); 1340 } 1341 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec"); 1342 } 1343 1344 // Store the updated result through the lvalue. 1345 if (LV.isBitField()) 1346 CGF.EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal); 1347 else 1348 CGF.EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy); 1349 1350 // If this is a postinc, return the value read from memory, otherwise use the 1351 // updated value. 1352 return isPre ? NextVal : InVal; 1353} 1354 1355 1356 1357Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { 1358 TestAndClearIgnoreResultAssign(); 1359 // Emit unary minus with EmitSub so we handle overflow cases etc. 1360 BinOpInfo BinOp; 1361 BinOp.RHS = Visit(E->getSubExpr()); 1362 1363 if (BinOp.RHS->getType()->isFPOrFPVectorTy()) 1364 BinOp.LHS = llvm::ConstantFP::getZeroValueForNegation(BinOp.RHS->getType()); 1365 else 1366 BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType()); 1367 BinOp.Ty = E->getType(); 1368 BinOp.Opcode = BO_Sub; 1369 BinOp.E = E; 1370 return EmitSub(BinOp); 1371} 1372 1373Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { 1374 TestAndClearIgnoreResultAssign(); 1375 Value *Op = Visit(E->getSubExpr()); 1376 return Builder.CreateNot(Op, "neg"); 1377} 1378 1379Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { 1380 // Compare operand to zero. 1381 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); 1382 1383 // Invert value. 1384 // TODO: Could dynamically modify easy computations here. For example, if 1385 // the operand is an icmp ne, turn into icmp eq. 1386 BoolVal = Builder.CreateNot(BoolVal, "lnot"); 1387 1388 // ZExt result to the expr type. 1389 return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext"); 1390} 1391 1392Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) { 1393 // Try folding the offsetof to a constant. 1394 Expr::EvalResult EvalResult; 1395 if (E->Evaluate(EvalResult, CGF.getContext())) 1396 return llvm::ConstantInt::get(VMContext, EvalResult.Val.getInt()); 1397 1398 // Loop over the components of the offsetof to compute the value. 1399 unsigned n = E->getNumComponents(); 1400 const llvm::Type* ResultType = ConvertType(E->getType()); 1401 llvm::Value* Result = llvm::Constant::getNullValue(ResultType); 1402 QualType CurrentType = E->getTypeSourceInfo()->getType(); 1403 for (unsigned i = 0; i != n; ++i) { 1404 OffsetOfExpr::OffsetOfNode ON = E->getComponent(i); 1405 llvm::Value *Offset = 0; 1406 switch (ON.getKind()) { 1407 case OffsetOfExpr::OffsetOfNode::Array: { 1408 // Compute the index 1409 Expr *IdxExpr = E->getIndexExpr(ON.getArrayExprIndex()); 1410 llvm::Value* Idx = CGF.EmitScalarExpr(IdxExpr); 1411 bool IdxSigned = IdxExpr->getType()->isSignedIntegerType(); 1412 Idx = Builder.CreateIntCast(Idx, ResultType, IdxSigned, "conv"); 1413 1414 // Save the element type 1415 CurrentType = 1416 CGF.getContext().getAsArrayType(CurrentType)->getElementType(); 1417 1418 // Compute the element size 1419 llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType, 1420 CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity()); 1421 1422 // Multiply out to compute the result 1423 Offset = Builder.CreateMul(Idx, ElemSize); 1424 break; 1425 } 1426 1427 case OffsetOfExpr::OffsetOfNode::Field: { 1428 FieldDecl *MemberDecl = ON.getField(); 1429 RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl(); 1430 const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); 1431 1432 // Compute the index of the field in its parent. 1433 unsigned i = 0; 1434 // FIXME: It would be nice if we didn't have to loop here! 1435 for (RecordDecl::field_iterator Field = RD->field_begin(), 1436 FieldEnd = RD->field_end(); 1437 Field != FieldEnd; (void)++Field, ++i) { 1438 if (*Field == MemberDecl) 1439 break; 1440 } 1441 assert(i < RL.getFieldCount() && "offsetof field in wrong type"); 1442 1443 // Compute the offset to the field 1444 int64_t OffsetInt = RL.getFieldOffset(i) / 1445 CGF.getContext().getCharWidth(); 1446 Offset = llvm::ConstantInt::get(ResultType, OffsetInt); 1447 1448 // Save the element type. 1449 CurrentType = MemberDecl->getType(); 1450 break; 1451 } 1452 1453 case OffsetOfExpr::OffsetOfNode::Identifier: 1454 llvm_unreachable("dependent __builtin_offsetof"); 1455 1456 case OffsetOfExpr::OffsetOfNode::Base: { 1457 if (ON.getBase()->isVirtual()) { 1458 CGF.ErrorUnsupported(E, "virtual base in offsetof"); 1459 continue; 1460 } 1461 1462 RecordDecl *RD = CurrentType->getAs<RecordType>()->getDecl(); 1463 const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); 1464 1465 // Save the element type. 1466 CurrentType = ON.getBase()->getType(); 1467 1468 // Compute the offset to the base. 1469 const RecordType *BaseRT = CurrentType->getAs<RecordType>(); 1470 CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); 1471 int64_t OffsetInt = RL.getBaseClassOffsetInBits(BaseRD) / 1472 CGF.getContext().getCharWidth(); 1473 Offset = llvm::ConstantInt::get(ResultType, OffsetInt); 1474 break; 1475 } 1476 } 1477 Result = Builder.CreateAdd(Result, Offset); 1478 } 1479 return Result; 1480} 1481 1482/// VisitSizeOfAlignOfExpr - Return the size or alignment of the type of 1483/// argument of the sizeof expression as an integer. 1484Value * 1485ScalarExprEmitter::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) { 1486 QualType TypeToSize = E->getTypeOfArgument(); 1487 if (E->isSizeOf()) { 1488 if (const VariableArrayType *VAT = 1489 CGF.getContext().getAsVariableArrayType(TypeToSize)) { 1490 if (E->isArgumentType()) { 1491 // sizeof(type) - make sure to emit the VLA size. 1492 CGF.EmitVLASize(TypeToSize); 1493 } else { 1494 // C99 6.5.3.4p2: If the argument is an expression of type 1495 // VLA, it is evaluated. 1496 CGF.EmitAnyExpr(E->getArgumentExpr()); 1497 } 1498 1499 return CGF.GetVLASize(VAT); 1500 } 1501 } 1502 1503 // If this isn't sizeof(vla), the result must be constant; use the constant 1504 // folding logic so we don't have to duplicate it here. 1505 Expr::EvalResult Result; 1506 E->Evaluate(Result, CGF.getContext()); 1507 return llvm::ConstantInt::get(VMContext, Result.Val.getInt()); 1508} 1509 1510Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { 1511 Expr *Op = E->getSubExpr(); 1512 if (Op->getType()->isAnyComplexType()) 1513 return CGF.EmitComplexExpr(Op, false, true, false, true).first; 1514 return Visit(Op); 1515} 1516Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { 1517 Expr *Op = E->getSubExpr(); 1518 if (Op->getType()->isAnyComplexType()) 1519 return CGF.EmitComplexExpr(Op, true, false, true, false).second; 1520 1521 // __imag on a scalar returns zero. Emit the subexpr to ensure side 1522 // effects are evaluated, but not the actual value. 1523 if (E->isLvalue(CGF.getContext()) == Expr::LV_Valid) 1524 CGF.EmitLValue(Op); 1525 else 1526 CGF.EmitScalarExpr(Op, true); 1527 return llvm::Constant::getNullValue(ConvertType(E->getType())); 1528} 1529 1530//===----------------------------------------------------------------------===// 1531// Binary Operators 1532//===----------------------------------------------------------------------===// 1533 1534BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { 1535 TestAndClearIgnoreResultAssign(); 1536 BinOpInfo Result; 1537 Result.LHS = Visit(E->getLHS()); 1538 Result.RHS = Visit(E->getRHS()); 1539 Result.Ty = E->getType(); 1540 Result.Opcode = E->getOpcode(); 1541 Result.E = E; 1542 return Result; 1543} 1544 1545LValue ScalarExprEmitter::EmitCompoundAssignLValue( 1546 const CompoundAssignOperator *E, 1547 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &), 1548 Value *&Result) { 1549 QualType LHSTy = E->getLHS()->getType(); 1550 BinOpInfo OpInfo; 1551 1552 if (E->getComputationResultType()->isAnyComplexType()) { 1553 // This needs to go through the complex expression emitter, but it's a tad 1554 // complicated to do that... I'm leaving it out for now. (Note that we do 1555 // actually need the imaginary part of the RHS for multiplication and 1556 // division.) 1557 CGF.ErrorUnsupported(E, "complex compound assignment"); 1558 Result = llvm::UndefValue::get(CGF.ConvertType(E->getType())); 1559 return LValue(); 1560 } 1561 1562 // Emit the RHS first. __block variables need to have the rhs evaluated 1563 // first, plus this should improve codegen a little. 1564 OpInfo.RHS = Visit(E->getRHS()); 1565 OpInfo.Ty = E->getComputationResultType(); 1566 OpInfo.Opcode = E->getOpcode(); 1567 OpInfo.E = E; 1568 // Load/convert the LHS. 1569 LValue LHSLV = EmitCheckedLValue(E->getLHS()); 1570 OpInfo.LHS = EmitLoadOfLValue(LHSLV, LHSTy); 1571 OpInfo.LHS = EmitScalarConversion(OpInfo.LHS, LHSTy, 1572 E->getComputationLHSType()); 1573 1574 // Expand the binary operator. 1575 Result = (this->*Func)(OpInfo); 1576 1577 // Convert the result back to the LHS type. 1578 Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy); 1579 1580 // Store the result value into the LHS lvalue. Bit-fields are handled 1581 // specially because the result is altered by the store, i.e., [C99 6.5.16p1] 1582 // 'An assignment expression has the value of the left operand after the 1583 // assignment...'. 1584 if (LHSLV.isBitField()) 1585 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, LHSTy, 1586 &Result); 1587 else 1588 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV, LHSTy); 1589 1590 return LHSLV; 1591} 1592 1593Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, 1594 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { 1595 bool Ignore = TestAndClearIgnoreResultAssign(); 1596 Value *RHS; 1597 LValue LHS = EmitCompoundAssignLValue(E, Func, RHS); 1598 1599 // If the result is clearly ignored, return now. 1600 if (Ignore) 1601 return 0; 1602 1603 // Objective-C property assignment never reloads the value following a store. 1604 if (LHS.isPropertyRef() || LHS.isKVCRef()) 1605 return RHS; 1606 1607 // If the lvalue is non-volatile, return the computed value of the assignment. 1608 if (!LHS.isVolatileQualified()) 1609 return RHS; 1610 1611 // Otherwise, reload the value. 1612 return EmitLoadOfLValue(LHS, E->getType()); 1613} 1614 1615void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck( 1616 const BinOpInfo &Ops, 1617 llvm::Value *Zero, bool isDiv) { 1618 llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn); 1619 llvm::BasicBlock *contBB = 1620 CGF.createBasicBlock(isDiv ? "div.cont" : "rem.cont", CGF.CurFn); 1621 1622 const llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType()); 1623 1624 if (Ops.Ty->hasSignedIntegerRepresentation()) { 1625 llvm::Value *IntMin = 1626 llvm::ConstantInt::get(VMContext, 1627 llvm::APInt::getSignedMinValue(Ty->getBitWidth())); 1628 llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL); 1629 1630 llvm::Value *Cond1 = Builder.CreateICmpEQ(Ops.RHS, Zero); 1631 llvm::Value *LHSCmp = Builder.CreateICmpEQ(Ops.LHS, IntMin); 1632 llvm::Value *RHSCmp = Builder.CreateICmpEQ(Ops.RHS, NegOne); 1633 llvm::Value *Cond2 = Builder.CreateAnd(LHSCmp, RHSCmp, "and"); 1634 Builder.CreateCondBr(Builder.CreateOr(Cond1, Cond2, "or"), 1635 overflowBB, contBB); 1636 } else { 1637 CGF.Builder.CreateCondBr(Builder.CreateICmpEQ(Ops.RHS, Zero), 1638 overflowBB, contBB); 1639 } 1640 EmitOverflowBB(overflowBB); 1641 Builder.SetInsertPoint(contBB); 1642} 1643 1644Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { 1645 if (isTrapvOverflowBehavior()) { 1646 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); 1647 1648 if (Ops.Ty->isIntegerType()) 1649 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, true); 1650 else if (Ops.Ty->isRealFloatingType()) { 1651 llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", 1652 CGF.CurFn); 1653 llvm::BasicBlock *DivCont = CGF.createBasicBlock("div.cont", CGF.CurFn); 1654 CGF.Builder.CreateCondBr(Builder.CreateFCmpOEQ(Ops.RHS, Zero), 1655 overflowBB, DivCont); 1656 EmitOverflowBB(overflowBB); 1657 Builder.SetInsertPoint(DivCont); 1658 } 1659 } 1660 if (Ops.LHS->getType()->isFPOrFPVectorTy()) 1661 return Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); 1662 else if (Ops.Ty->hasUnsignedIntegerRepresentation()) 1663 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); 1664 else 1665 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); 1666} 1667 1668Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { 1669 // Rem in C can't be a floating point type: C99 6.5.5p2. 1670 if (isTrapvOverflowBehavior()) { 1671 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); 1672 1673 if (Ops.Ty->isIntegerType()) 1674 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false); 1675 } 1676 1677 if (Ops.Ty->isUnsignedIntegerType()) 1678 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); 1679 else 1680 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); 1681} 1682 1683Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) { 1684 unsigned IID; 1685 unsigned OpID = 0; 1686 1687 switch (Ops.Opcode) { 1688 case BO_Add: 1689 case BO_AddAssign: 1690 OpID = 1; 1691 IID = llvm::Intrinsic::sadd_with_overflow; 1692 break; 1693 case BO_Sub: 1694 case BO_SubAssign: 1695 OpID = 2; 1696 IID = llvm::Intrinsic::ssub_with_overflow; 1697 break; 1698 case BO_Mul: 1699 case BO_MulAssign: 1700 OpID = 3; 1701 IID = llvm::Intrinsic::smul_with_overflow; 1702 break; 1703 default: 1704 assert(false && "Unsupported operation for overflow detection"); 1705 IID = 0; 1706 } 1707 OpID <<= 1; 1708 OpID |= 1; 1709 1710 const llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty); 1711 1712 llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, &opTy, 1); 1713 1714 Value *resultAndOverflow = Builder.CreateCall2(intrinsic, Ops.LHS, Ops.RHS); 1715 Value *result = Builder.CreateExtractValue(resultAndOverflow, 0); 1716 Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1); 1717 1718 // Branch in case of overflow. 1719 llvm::BasicBlock *initialBB = Builder.GetInsertBlock(); 1720 llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn); 1721 llvm::BasicBlock *continueBB = CGF.createBasicBlock("nooverflow", CGF.CurFn); 1722 1723 Builder.CreateCondBr(overflow, overflowBB, continueBB); 1724 1725 // Handle overflow with llvm.trap. 1726 const std::string *handlerName = 1727 &CGF.getContext().getLangOptions().OverflowHandler; 1728 if (handlerName->empty()) { 1729 EmitOverflowBB(overflowBB); 1730 Builder.SetInsertPoint(continueBB); 1731 return result; 1732 } 1733 1734 // If an overflow handler is set, then we want to call it and then use its 1735 // result, if it returns. 1736 Builder.SetInsertPoint(overflowBB); 1737 1738 // Get the overflow handler. 1739 const llvm::Type *Int8Ty = llvm::Type::getInt8Ty(VMContext); 1740 std::vector<const llvm::Type*> argTypes; 1741 argTypes.push_back(CGF.Int64Ty); argTypes.push_back(CGF.Int64Ty); 1742 argTypes.push_back(Int8Ty); argTypes.push_back(Int8Ty); 1743 llvm::FunctionType *handlerTy = 1744 llvm::FunctionType::get(CGF.Int64Ty, argTypes, true); 1745 llvm::Value *handler = CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName); 1746 1747 // Sign extend the args to 64-bit, so that we can use the same handler for 1748 // all types of overflow. 1749 llvm::Value *lhs = Builder.CreateSExt(Ops.LHS, CGF.Int64Ty); 1750 llvm::Value *rhs = Builder.CreateSExt(Ops.RHS, CGF.Int64Ty); 1751 1752 // Call the handler with the two arguments, the operation, and the size of 1753 // the result. 1754 llvm::Value *handlerResult = Builder.CreateCall4(handler, lhs, rhs, 1755 Builder.getInt8(OpID), 1756 Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth())); 1757 1758 // Truncate the result back to the desired size. 1759 handlerResult = Builder.CreateTrunc(handlerResult, opTy); 1760 Builder.CreateBr(continueBB); 1761 1762 Builder.SetInsertPoint(continueBB); 1763 llvm::PHINode *phi = Builder.CreatePHI(opTy); 1764 phi->reserveOperandSpace(2); 1765 phi->addIncoming(result, initialBB); 1766 phi->addIncoming(handlerResult, overflowBB); 1767 1768 return phi; 1769} 1770 1771Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) { 1772 if (!Ops.Ty->isAnyPointerType()) { 1773 if (Ops.Ty->hasSignedIntegerRepresentation()) { 1774 switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { 1775 case LangOptions::SOB_Undefined: 1776 return Builder.CreateNSWAdd(Ops.LHS, Ops.RHS, "add"); 1777 case LangOptions::SOB_Defined: 1778 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); 1779 case LangOptions::SOB_Trapping: 1780 return EmitOverflowCheckedBinOp(Ops); 1781 } 1782 } 1783 1784 if (Ops.LHS->getType()->isFPOrFPVectorTy()) 1785 return Builder.CreateFAdd(Ops.LHS, Ops.RHS, "add"); 1786 1787 return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add"); 1788 } 1789 1790 // Must have binary (not unary) expr here. Unary pointer decrement doesn't 1791 // use this path. 1792 const BinaryOperator *BinOp = cast<BinaryOperator>(Ops.E); 1793 1794 if (Ops.Ty->isPointerType() && 1795 Ops.Ty->getAs<PointerType>()->isVariableArrayType()) { 1796 // The amount of the addition needs to account for the VLA size 1797 CGF.ErrorUnsupported(BinOp, "VLA pointer addition"); 1798 } 1799 1800 Value *Ptr, *Idx; 1801 Expr *IdxExp; 1802 const PointerType *PT = BinOp->getLHS()->getType()->getAs<PointerType>(); 1803 const ObjCObjectPointerType *OPT = 1804 BinOp->getLHS()->getType()->getAs<ObjCObjectPointerType>(); 1805 if (PT || OPT) { 1806 Ptr = Ops.LHS; 1807 Idx = Ops.RHS; 1808 IdxExp = BinOp->getRHS(); 1809 } else { // int + pointer 1810 PT = BinOp->getRHS()->getType()->getAs<PointerType>(); 1811 OPT = BinOp->getRHS()->getType()->getAs<ObjCObjectPointerType>(); 1812 assert((PT || OPT) && "Invalid add expr"); 1813 Ptr = Ops.RHS; 1814 Idx = Ops.LHS; 1815 IdxExp = BinOp->getLHS(); 1816 } 1817 1818 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); 1819 if (Width < CGF.LLVMPointerWidth) { 1820 // Zero or sign extend the pointer value based on whether the index is 1821 // signed or not. 1822 const llvm::Type *IdxType = CGF.IntPtrTy; 1823 if (IdxExp->getType()->isSignedIntegerType()) 1824 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); 1825 else 1826 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); 1827 } 1828 const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType(); 1829 // Handle interface types, which are not represented with a concrete type. 1830 if (const ObjCObjectType *OIT = ElementType->getAs<ObjCObjectType>()) { 1831 llvm::Value *InterfaceSize = 1832 llvm::ConstantInt::get(Idx->getType(), 1833 CGF.getContext().getTypeSizeInChars(OIT).getQuantity()); 1834 Idx = Builder.CreateMul(Idx, InterfaceSize); 1835 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1836 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); 1837 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); 1838 return Builder.CreateBitCast(Res, Ptr->getType()); 1839 } 1840 1841 // Explicitly handle GNU void* and function pointer arithmetic extensions. The 1842 // GNU void* casts amount to no-ops since our void* type is i8*, but this is 1843 // future proof. 1844 if (ElementType->isVoidType() || ElementType->isFunctionType()) { 1845 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1846 Value *Casted = Builder.CreateBitCast(Ptr, i8Ty); 1847 Value *Res = Builder.CreateGEP(Casted, Idx, "add.ptr"); 1848 return Builder.CreateBitCast(Res, Ptr->getType()); 1849 } 1850 1851 return Builder.CreateInBoundsGEP(Ptr, Idx, "add.ptr"); 1852} 1853 1854Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) { 1855 if (!isa<llvm::PointerType>(Ops.LHS->getType())) { 1856 if (Ops.Ty->hasSignedIntegerRepresentation()) { 1857 switch (CGF.getContext().getLangOptions().getSignedOverflowBehavior()) { 1858 case LangOptions::SOB_Undefined: 1859 return Builder.CreateNSWSub(Ops.LHS, Ops.RHS, "sub"); 1860 case LangOptions::SOB_Defined: 1861 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); 1862 case LangOptions::SOB_Trapping: 1863 return EmitOverflowCheckedBinOp(Ops); 1864 } 1865 } 1866 1867 if (Ops.LHS->getType()->isFPOrFPVectorTy()) 1868 return Builder.CreateFSub(Ops.LHS, Ops.RHS, "sub"); 1869 1870 return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub"); 1871 } 1872 1873 // Must have binary (not unary) expr here. Unary pointer increment doesn't 1874 // use this path. 1875 const BinaryOperator *BinOp = cast<BinaryOperator>(Ops.E); 1876 1877 if (BinOp->getLHS()->getType()->isPointerType() && 1878 BinOp->getLHS()->getType()->getAs<PointerType>()->isVariableArrayType()) { 1879 // The amount of the addition needs to account for the VLA size for 1880 // ptr-int 1881 // The amount of the division needs to account for the VLA size for 1882 // ptr-ptr. 1883 CGF.ErrorUnsupported(BinOp, "VLA pointer subtraction"); 1884 } 1885 1886 const QualType LHSType = BinOp->getLHS()->getType(); 1887 const QualType LHSElementType = LHSType->getPointeeType(); 1888 if (!isa<llvm::PointerType>(Ops.RHS->getType())) { 1889 // pointer - int 1890 Value *Idx = Ops.RHS; 1891 unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth(); 1892 if (Width < CGF.LLVMPointerWidth) { 1893 // Zero or sign extend the pointer value based on whether the index is 1894 // signed or not. 1895 const llvm::Type *IdxType = CGF.IntPtrTy; 1896 if (BinOp->getRHS()->getType()->isSignedIntegerType()) 1897 Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext"); 1898 else 1899 Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext"); 1900 } 1901 Idx = Builder.CreateNeg(Idx, "sub.ptr.neg"); 1902 1903 // Handle interface types, which are not represented with a concrete type. 1904 if (const ObjCObjectType *OIT = LHSElementType->getAs<ObjCObjectType>()) { 1905 llvm::Value *InterfaceSize = 1906 llvm::ConstantInt::get(Idx->getType(), 1907 CGF.getContext(). 1908 getTypeSizeInChars(OIT).getQuantity()); 1909 Idx = Builder.CreateMul(Idx, InterfaceSize); 1910 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1911 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); 1912 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "add.ptr"); 1913 return Builder.CreateBitCast(Res, Ops.LHS->getType()); 1914 } 1915 1916 // Explicitly handle GNU void* and function pointer arithmetic 1917 // extensions. The GNU void* casts amount to no-ops since our void* type is 1918 // i8*, but this is future proof. 1919 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { 1920 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext); 1921 Value *LHSCasted = Builder.CreateBitCast(Ops.LHS, i8Ty); 1922 Value *Res = Builder.CreateGEP(LHSCasted, Idx, "sub.ptr"); 1923 return Builder.CreateBitCast(Res, Ops.LHS->getType()); 1924 } 1925 1926 return Builder.CreateInBoundsGEP(Ops.LHS, Idx, "sub.ptr"); 1927 } else { 1928 // pointer - pointer 1929 Value *LHS = Ops.LHS; 1930 Value *RHS = Ops.RHS; 1931 1932 CharUnits ElementSize; 1933 1934 // Handle GCC extension for pointer arithmetic on void* and function pointer 1935 // types. 1936 if (LHSElementType->isVoidType() || LHSElementType->isFunctionType()) { 1937 ElementSize = CharUnits::One(); 1938 } else { 1939 ElementSize = CGF.getContext().getTypeSizeInChars(LHSElementType); 1940 } 1941 1942 const llvm::Type *ResultType = ConvertType(Ops.Ty); 1943 LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast"); 1944 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast"); 1945 Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); 1946 1947 // Optimize out the shift for element size of 1. 1948 if (ElementSize.isOne()) 1949 return BytesBetween; 1950 1951 // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since 1952 // pointer difference in C is only defined in the case where both operands 1953 // are pointing to elements of an array. 1954 Value *BytesPerElt = 1955 llvm::ConstantInt::get(ResultType, ElementSize.getQuantity()); 1956 return Builder.CreateExactSDiv(BytesBetween, BytesPerElt, "sub.ptr.div"); 1957 } 1958} 1959 1960Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { 1961 // LLVM requires the LHS and RHS to be the same type: promote or truncate the 1962 // RHS to the same size as the LHS. 1963 Value *RHS = Ops.RHS; 1964 if (Ops.LHS->getType() != RHS->getType()) 1965 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); 1966 1967 if (CGF.CatchUndefined 1968 && isa<llvm::IntegerType>(Ops.LHS->getType())) { 1969 unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth(); 1970 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); 1971 CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS, 1972 llvm::ConstantInt::get(RHS->getType(), Width)), 1973 Cont, CGF.getTrapBB()); 1974 CGF.EmitBlock(Cont); 1975 } 1976 1977 return Builder.CreateShl(Ops.LHS, RHS, "shl"); 1978} 1979 1980Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { 1981 // LLVM requires the LHS and RHS to be the same type: promote or truncate the 1982 // RHS to the same size as the LHS. 1983 Value *RHS = Ops.RHS; 1984 if (Ops.LHS->getType() != RHS->getType()) 1985 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); 1986 1987 if (CGF.CatchUndefined 1988 && isa<llvm::IntegerType>(Ops.LHS->getType())) { 1989 unsigned Width = cast<llvm::IntegerType>(Ops.LHS->getType())->getBitWidth(); 1990 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); 1991 CGF.Builder.CreateCondBr(Builder.CreateICmpULT(RHS, 1992 llvm::ConstantInt::get(RHS->getType(), Width)), 1993 Cont, CGF.getTrapBB()); 1994 CGF.EmitBlock(Cont); 1995 } 1996 1997 if (Ops.Ty->hasUnsignedIntegerRepresentation()) 1998 return Builder.CreateLShr(Ops.LHS, RHS, "shr"); 1999 return Builder.CreateAShr(Ops.LHS, RHS, "shr"); 2000} 2001 2002Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc, 2003 unsigned SICmpOpc, unsigned FCmpOpc) { 2004 TestAndClearIgnoreResultAssign(); 2005 Value *Result; 2006 QualType LHSTy = E->getLHS()->getType(); 2007 if (const MemberPointerType *MPT = LHSTy->getAs<MemberPointerType>()) { 2008 assert(E->getOpcode() == BO_EQ || 2009 E->getOpcode() == BO_NE); 2010 Value *LHS = CGF.EmitScalarExpr(E->getLHS()); 2011 Value *RHS = CGF.EmitScalarExpr(E->getRHS()); 2012 Result = CGF.CGM.getCXXABI().EmitMemberPointerComparison( 2013 CGF, LHS, RHS, MPT, E->getOpcode() == BO_NE); 2014 } else if (!LHSTy->isAnyComplexType()) { 2015 Value *LHS = Visit(E->getLHS()); 2016 Value *RHS = Visit(E->getRHS()); 2017 2018 if (LHS->getType()->isFPOrFPVectorTy()) { 2019 Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc, 2020 LHS, RHS, "cmp"); 2021 } else if (LHSTy->hasSignedIntegerRepresentation()) { 2022 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc, 2023 LHS, RHS, "cmp"); 2024 } else { 2025 // Unsigned integers and pointers. 2026 Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, 2027 LHS, RHS, "cmp"); 2028 } 2029 2030 // If this is a vector comparison, sign extend the result to the appropriate 2031 // vector integer type and return it (don't convert to bool). 2032 if (LHSTy->isVectorType()) 2033 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); 2034 2035 } else { 2036 // Complex Comparison: can only be an equality comparison. 2037 CodeGenFunction::ComplexPairTy LHS = CGF.EmitComplexExpr(E->getLHS()); 2038 CodeGenFunction::ComplexPairTy RHS = CGF.EmitComplexExpr(E->getRHS()); 2039 2040 QualType CETy = LHSTy->getAs<ComplexType>()->getElementType(); 2041 2042 Value *ResultR, *ResultI; 2043 if (CETy->isRealFloatingType()) { 2044 ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, 2045 LHS.first, RHS.first, "cmp.r"); 2046 ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, 2047 LHS.second, RHS.second, "cmp.i"); 2048 } else { 2049 // Complex comparisons can only be equality comparisons. As such, signed 2050 // and unsigned opcodes are the same. 2051 ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, 2052 LHS.first, RHS.first, "cmp.r"); 2053 ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc, 2054 LHS.second, RHS.second, "cmp.i"); 2055 } 2056 2057 if (E->getOpcode() == BO_EQ) { 2058 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); 2059 } else { 2060 assert(E->getOpcode() == BO_NE && 2061 "Complex comparison other than == or != ?"); 2062 Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); 2063 } 2064 } 2065 2066 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType()); 2067} 2068 2069Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { 2070 bool Ignore = TestAndClearIgnoreResultAssign(); 2071 2072 // __block variables need to have the rhs evaluated first, plus this should 2073 // improve codegen just a little. 2074 Value *RHS = Visit(E->getRHS()); 2075 LValue LHS = EmitCheckedLValue(E->getLHS()); 2076 2077 // Store the value into the LHS. Bit-fields are handled specially 2078 // because the result is altered by the store, i.e., [C99 6.5.16p1] 2079 // 'An assignment expression has the value of the left operand after 2080 // the assignment...'. 2081 if (LHS.isBitField()) 2082 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, E->getType(), 2083 &RHS); 2084 else 2085 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS, E->getType()); 2086 2087 // If the result is clearly ignored, return now. 2088 if (Ignore) 2089 return 0; 2090 2091 // Objective-C property assignment never reloads the value following a store. 2092 if (LHS.isPropertyRef() || LHS.isKVCRef()) 2093 return RHS; 2094 2095 // If the lvalue is non-volatile, return the computed value of the assignment. 2096 if (!LHS.isVolatileQualified()) 2097 return RHS; 2098 2099 // Otherwise, reload the value. 2100 return EmitLoadOfLValue(LHS, E->getType()); 2101} 2102 2103Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { 2104 const llvm::Type *ResTy = ConvertType(E->getType()); 2105 2106 // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. 2107 // If we have 1 && X, just emit X without inserting the control flow. 2108 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { 2109 if (Cond == 1) { // If we have 1 && X, just emit X. 2110 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 2111 // ZExt result to int or bool. 2112 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext"); 2113 } 2114 2115 // 0 && RHS: If it is safe, just elide the RHS, and return 0/false. 2116 if (!CGF.ContainsLabel(E->getRHS())) 2117 return llvm::Constant::getNullValue(ResTy); 2118 } 2119 2120 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); 2121 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); 2122 2123 // Branch on the LHS first. If it is false, go to the failure (cont) block. 2124 CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock); 2125 2126 // Any edges into the ContBlock are now from an (indeterminate number of) 2127 // edges from this first condition. All of these values will be false. Start 2128 // setting up the PHI node in the Cont Block for this. 2129 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2130 "", ContBlock); 2131 PN->reserveOperandSpace(2); // Normal case, two inputs. 2132 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); 2133 PI != PE; ++PI) 2134 PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI); 2135 2136 CGF.BeginConditionalBranch(); 2137 CGF.EmitBlock(RHSBlock); 2138 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 2139 CGF.EndConditionalBranch(); 2140 2141 // Reaquire the RHS block, as there may be subblocks inserted. 2142 RHSBlock = Builder.GetInsertBlock(); 2143 2144 // Emit an unconditional branch from this block to ContBlock. Insert an entry 2145 // into the phi node for the edge with the value of RHSCond. 2146 CGF.EmitBlock(ContBlock); 2147 PN->addIncoming(RHSCond, RHSBlock); 2148 2149 // ZExt result to int. 2150 return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext"); 2151} 2152 2153Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { 2154 const llvm::Type *ResTy = ConvertType(E->getType()); 2155 2156 // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. 2157 // If we have 0 || X, just emit X without inserting the control flow. 2158 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) { 2159 if (Cond == -1) { // If we have 0 || X, just emit X. 2160 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 2161 // ZExt result to int or bool. 2162 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext"); 2163 } 2164 2165 // 1 || RHS: If it is safe, just elide the RHS, and return 1/true. 2166 if (!CGF.ContainsLabel(E->getRHS())) 2167 return llvm::ConstantInt::get(ResTy, 1); 2168 } 2169 2170 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); 2171 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); 2172 2173 // Branch on the LHS first. If it is true, go to the success (cont) block. 2174 CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock); 2175 2176 // Any edges into the ContBlock are now from an (indeterminate number of) 2177 // edges from this first condition. All of these values will be true. Start 2178 // setting up the PHI node in the Cont Block for this. 2179 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2180 "", ContBlock); 2181 PN->reserveOperandSpace(2); // Normal case, two inputs. 2182 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); 2183 PI != PE; ++PI) 2184 PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI); 2185 2186 CGF.BeginConditionalBranch(); 2187 2188 // Emit the RHS condition as a bool value. 2189 CGF.EmitBlock(RHSBlock); 2190 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 2191 2192 CGF.EndConditionalBranch(); 2193 2194 // Reaquire the RHS block, as there may be subblocks inserted. 2195 RHSBlock = Builder.GetInsertBlock(); 2196 2197 // Emit an unconditional branch from this block to ContBlock. Insert an entry 2198 // into the phi node for the edge with the value of RHSCond. 2199 CGF.EmitBlock(ContBlock); 2200 PN->addIncoming(RHSCond, RHSBlock); 2201 2202 // ZExt result to int. 2203 return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext"); 2204} 2205 2206Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { 2207 CGF.EmitStmt(E->getLHS()); 2208 CGF.EnsureInsertPoint(); 2209 return Visit(E->getRHS()); 2210} 2211 2212//===----------------------------------------------------------------------===// 2213// Other Operators 2214//===----------------------------------------------------------------------===// 2215 2216/// isCheapEnoughToEvaluateUnconditionally - Return true if the specified 2217/// expression is cheap enough and side-effect-free enough to evaluate 2218/// unconditionally instead of conditionally. This is used to convert control 2219/// flow into selects in some cases. 2220static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E, 2221 CodeGenFunction &CGF) { 2222 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) 2223 return isCheapEnoughToEvaluateUnconditionally(PE->getSubExpr(), CGF); 2224 2225 // TODO: Allow anything we can constant fold to an integer or fp constant. 2226 if (isa<IntegerLiteral>(E) || isa<CharacterLiteral>(E) || 2227 isa<FloatingLiteral>(E)) 2228 return true; 2229 2230 // Non-volatile automatic variables too, to get "cond ? X : Y" where 2231 // X and Y are local variables. 2232 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 2233 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) 2234 if (VD->hasLocalStorage() && !(CGF.getContext() 2235 .getCanonicalType(VD->getType()) 2236 .isVolatileQualified())) 2237 return true; 2238 2239 return false; 2240} 2241 2242 2243Value *ScalarExprEmitter:: 2244VisitConditionalOperator(const ConditionalOperator *E) { 2245 TestAndClearIgnoreResultAssign(); 2246 // If the condition constant folds and can be elided, try to avoid emitting 2247 // the condition and the dead arm. 2248 if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getCond())){ 2249 Expr *Live = E->getLHS(), *Dead = E->getRHS(); 2250 if (Cond == -1) 2251 std::swap(Live, Dead); 2252 2253 // If the dead side doesn't have labels we need, and if the Live side isn't 2254 // the gnu missing ?: extension (which we could handle, but don't bother 2255 // to), just emit the Live part. 2256 if ((!Dead || !CGF.ContainsLabel(Dead)) && // No labels in dead part 2257 Live) // Live part isn't missing. 2258 return Visit(Live); 2259 } 2260 2261 // OpenCL: If the condition is a vector, we can treat this condition like 2262 // the select function. 2263 if (CGF.getContext().getLangOptions().OpenCL 2264 && E->getCond()->getType()->isVectorType()) { 2265 llvm::Value *CondV = CGF.EmitScalarExpr(E->getCond()); 2266 llvm::Value *LHS = Visit(E->getLHS()); 2267 llvm::Value *RHS = Visit(E->getRHS()); 2268 2269 const llvm::Type *condType = ConvertType(E->getCond()->getType()); 2270 const llvm::VectorType *vecTy = cast<llvm::VectorType>(condType); 2271 2272 unsigned numElem = vecTy->getNumElements(); 2273 const llvm::Type *elemType = vecTy->getElementType(); 2274 2275 std::vector<llvm::Constant*> Zvals; 2276 for (unsigned i = 0; i < numElem; ++i) 2277 Zvals.push_back(llvm::ConstantInt::get(elemType,0)); 2278 2279 llvm::Value *zeroVec = llvm::ConstantVector::get(Zvals); 2280 llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec); 2281 llvm::Value *tmp = Builder.CreateSExt(TestMSB, 2282 llvm::VectorType::get(elemType, 2283 numElem), 2284 "sext"); 2285 llvm::Value *tmp2 = Builder.CreateNot(tmp); 2286 2287 // Cast float to int to perform ANDs if necessary. 2288 llvm::Value *RHSTmp = RHS; 2289 llvm::Value *LHSTmp = LHS; 2290 bool wasCast = false; 2291 const llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType()); 2292 if (rhsVTy->getElementType()->isFloatTy()) { 2293 RHSTmp = Builder.CreateBitCast(RHS, tmp2->getType()); 2294 LHSTmp = Builder.CreateBitCast(LHS, tmp->getType()); 2295 wasCast = true; 2296 } 2297 2298 llvm::Value *tmp3 = Builder.CreateAnd(RHSTmp, tmp2); 2299 llvm::Value *tmp4 = Builder.CreateAnd(LHSTmp, tmp); 2300 llvm::Value *tmp5 = Builder.CreateOr(tmp3, tmp4, "cond"); 2301 if (wasCast) 2302 tmp5 = Builder.CreateBitCast(tmp5, RHS->getType()); 2303 2304 return tmp5; 2305 } 2306 2307 // If this is a really simple expression (like x ? 4 : 5), emit this as a 2308 // select instead of as control flow. We can only do this if it is cheap and 2309 // safe to evaluate the LHS and RHS unconditionally. 2310 if (E->getLHS() && isCheapEnoughToEvaluateUnconditionally(E->getLHS(), 2311 CGF) && 2312 isCheapEnoughToEvaluateUnconditionally(E->getRHS(), CGF)) { 2313 llvm::Value *CondV = CGF.EvaluateExprAsBool(E->getCond()); 2314 llvm::Value *LHS = Visit(E->getLHS()); 2315 llvm::Value *RHS = Visit(E->getRHS()); 2316 return Builder.CreateSelect(CondV, LHS, RHS, "cond"); 2317 } 2318 2319 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 2320 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 2321 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 2322 2323 // If we don't have the GNU missing condition extension, emit a branch on bool 2324 // the normal way. 2325 if (E->getLHS()) { 2326 // Otherwise, just use EmitBranchOnBoolExpr to get small and simple code for 2327 // the branch on bool. 2328 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock); 2329 } else { 2330 // Otherwise, for the ?: extension, evaluate the conditional and then 2331 // convert it to bool the hard way. We do this explicitly because we need 2332 // the unconverted value for the missing middle value of the ?:. 2333 Expr *save = E->getSAVE(); 2334 assert(save && "VisitConditionalOperator - save is null"); 2335 // Intentianlly not doing direct assignment to ConditionalSaveExprs[save] !! 2336 Value *SaveVal = CGF.EmitScalarExpr(save); 2337 CGF.ConditionalSaveExprs[save] = SaveVal; 2338 Value *CondVal = Visit(E->getCond()); 2339 // In some cases, EmitScalarConversion will delete the "CondVal" expression 2340 // if there are no extra uses (an optimization). Inhibit this by making an 2341 // extra dead use, because we're going to add a use of CondVal later. We 2342 // don't use the builder for this, because we don't want it to get optimized 2343 // away. This leaves dead code, but the ?: extension isn't common. 2344 new llvm::BitCastInst(CondVal, CondVal->getType(), "dummy?:holder", 2345 Builder.GetInsertBlock()); 2346 2347 Value *CondBoolVal = 2348 CGF.EmitScalarConversion(CondVal, E->getCond()->getType(), 2349 CGF.getContext().BoolTy); 2350 Builder.CreateCondBr(CondBoolVal, LHSBlock, RHSBlock); 2351 } 2352 2353 CGF.BeginConditionalBranch(); 2354 CGF.EmitBlock(LHSBlock); 2355 2356 // Handle the GNU extension for missing LHS. 2357 Value *LHS = Visit(E->getTrueExpr()); 2358 2359 CGF.EndConditionalBranch(); 2360 LHSBlock = Builder.GetInsertBlock(); 2361 CGF.EmitBranch(ContBlock); 2362 2363 CGF.BeginConditionalBranch(); 2364 CGF.EmitBlock(RHSBlock); 2365 2366 Value *RHS = Visit(E->getRHS()); 2367 CGF.EndConditionalBranch(); 2368 RHSBlock = Builder.GetInsertBlock(); 2369 CGF.EmitBranch(ContBlock); 2370 2371 CGF.EmitBlock(ContBlock); 2372 2373 // If the LHS or RHS is a throw expression, it will be legitimately null. 2374 if (!LHS) 2375 return RHS; 2376 if (!RHS) 2377 return LHS; 2378 2379 // Create a PHI node for the real part. 2380 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), "cond"); 2381 PN->reserveOperandSpace(2); 2382 PN->addIncoming(LHS, LHSBlock); 2383 PN->addIncoming(RHS, RHSBlock); 2384 return PN; 2385} 2386 2387Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { 2388 return Visit(E->getChosenSubExpr(CGF.getContext())); 2389} 2390 2391Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 2392 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr()); 2393 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType()); 2394 2395 // If EmitVAArg fails, we fall back to the LLVM instruction. 2396 if (!ArgPtr) 2397 return Builder.CreateVAArg(ArgValue, ConvertType(VE->getType())); 2398 2399 // FIXME Volatility. 2400 return Builder.CreateLoad(ArgPtr); 2401} 2402 2403Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *BE) { 2404 return CGF.BuildBlockLiteralTmp(BE); 2405} 2406 2407//===----------------------------------------------------------------------===// 2408// Entry Point into this File 2409//===----------------------------------------------------------------------===// 2410 2411/// EmitScalarExpr - Emit the computation of the specified expression of scalar 2412/// type, ignoring the result. 2413Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) { 2414 assert(E && !hasAggregateLLVMType(E->getType()) && 2415 "Invalid scalar expression to emit"); 2416 2417 return ScalarExprEmitter(*this, IgnoreResultAssign) 2418 .Visit(const_cast<Expr*>(E)); 2419} 2420 2421/// EmitScalarConversion - Emit a conversion from the specified type to the 2422/// specified destination type, both of which are LLVM scalar types. 2423Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, 2424 QualType DstTy) { 2425 assert(!hasAggregateLLVMType(SrcTy) && !hasAggregateLLVMType(DstTy) && 2426 "Invalid scalar expression to emit"); 2427 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy); 2428} 2429 2430/// EmitComplexToScalarConversion - Emit a conversion from the specified complex 2431/// type to the specified destination type, where the destination type is an 2432/// LLVM scalar type. 2433Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, 2434 QualType SrcTy, 2435 QualType DstTy) { 2436 assert(SrcTy->isAnyComplexType() && !hasAggregateLLVMType(DstTy) && 2437 "Invalid complex -> scalar conversion"); 2438 return ScalarExprEmitter(*this).EmitComplexToScalarConversion(Src, SrcTy, 2439 DstTy); 2440} 2441 2442 2443llvm::Value *CodeGenFunction:: 2444EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 2445 bool isInc, bool isPre) { 2446 return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre); 2447} 2448 2449LValue CodeGenFunction::EmitObjCIsaExpr(const ObjCIsaExpr *E) { 2450 llvm::Value *V; 2451 // object->isa or (*object).isa 2452 // Generate code as for: *(Class*)object 2453 // build Class* type 2454 const llvm::Type *ClassPtrTy = ConvertType(E->getType()); 2455 2456 Expr *BaseExpr = E->getBase(); 2457 if (BaseExpr->isLvalue(getContext()) != Expr::LV_Valid) { 2458 V = CreateTempAlloca(ClassPtrTy, "resval"); 2459 llvm::Value *Src = EmitScalarExpr(BaseExpr); 2460 Builder.CreateStore(Src, V); 2461 V = ScalarExprEmitter(*this).EmitLoadOfLValue( 2462 MakeAddrLValue(V, E->getType()), E->getType()); 2463 } else { 2464 if (E->isArrow()) 2465 V = ScalarExprEmitter(*this).EmitLoadOfLValue(BaseExpr); 2466 else 2467 V = EmitLValue(BaseExpr).getAddress(); 2468 } 2469 2470 // build Class* type 2471 ClassPtrTy = ClassPtrTy->getPointerTo(); 2472 V = Builder.CreateBitCast(V, ClassPtrTy); 2473 return MakeAddrLValue(V, E->getType()); 2474} 2475 2476 2477LValue CodeGenFunction::EmitCompoundAssignOperatorLValue( 2478 const CompoundAssignOperator *E) { 2479 ScalarExprEmitter Scalar(*this); 2480 Value *Result = 0; 2481 switch (E->getOpcode()) { 2482#define COMPOUND_OP(Op) \ 2483 case BO_##Op##Assign: \ 2484 return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \ 2485 Result) 2486 COMPOUND_OP(Mul); 2487 COMPOUND_OP(Div); 2488 COMPOUND_OP(Rem); 2489 COMPOUND_OP(Add); 2490 COMPOUND_OP(Sub); 2491 COMPOUND_OP(Shl); 2492 COMPOUND_OP(Shr); 2493 COMPOUND_OP(And); 2494 COMPOUND_OP(Xor); 2495 COMPOUND_OP(Or); 2496#undef COMPOUND_OP 2497 2498 case BO_PtrMemD: 2499 case BO_PtrMemI: 2500 case BO_Mul: 2501 case BO_Div: 2502 case BO_Rem: 2503 case BO_Add: 2504 case BO_Sub: 2505 case BO_Shl: 2506 case BO_Shr: 2507 case BO_LT: 2508 case BO_GT: 2509 case BO_LE: 2510 case BO_GE: 2511 case BO_EQ: 2512 case BO_NE: 2513 case BO_And: 2514 case BO_Xor: 2515 case BO_Or: 2516 case BO_LAnd: 2517 case BO_LOr: 2518 case BO_Assign: 2519 case BO_Comma: 2520 assert(false && "Not valid compound assignment operators"); 2521 break; 2522 } 2523 2524 llvm_unreachable("Unhandled compound assignment operator"); 2525} 2526