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