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