1 //===--- CGExprScalar.cpp - Emit LLVM Code for Scalar Exprs ---------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This contains code to emit Expr nodes with scalar LLVM types as LLVM code. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CGCXXABI.h" 14 #include "CGCleanup.h" 15 #include "CGDebugInfo.h" 16 #include "CGObjCRuntime.h" 17 #include "CGOpenMPRuntime.h" 18 #include "CodeGenFunction.h" 19 #include "CodeGenModule.h" 20 #include "ConstantEmitter.h" 21 #include "TargetInfo.h" 22 #include "clang/AST/ASTContext.h" 23 #include "clang/AST/Attr.h" 24 #include "clang/AST/DeclObjC.h" 25 #include "clang/AST/Expr.h" 26 #include "clang/AST/RecordLayout.h" 27 #include "clang/AST/StmtVisitor.h" 28 #include "clang/Basic/CodeGenOptions.h" 29 #include "clang/Basic/TargetInfo.h" 30 #include "llvm/ADT/APFixedPoint.h" 31 #include "llvm/ADT/Optional.h" 32 #include "llvm/IR/CFG.h" 33 #include "llvm/IR/Constants.h" 34 #include "llvm/IR/DataLayout.h" 35 #include "llvm/IR/FixedPointBuilder.h" 36 #include "llvm/IR/Function.h" 37 #include "llvm/IR/GetElementPtrTypeIterator.h" 38 #include "llvm/IR/GlobalVariable.h" 39 #include "llvm/IR/Intrinsics.h" 40 #include "llvm/IR/IntrinsicsPowerPC.h" 41 #include "llvm/IR/MatrixBuilder.h" 42 #include "llvm/IR/Module.h" 43 #include <cstdarg> 44 45 using namespace clang; 46 using namespace CodeGen; 47 using llvm::Value; 48 49 //===----------------------------------------------------------------------===// 50 // Scalar Expression Emitter 51 //===----------------------------------------------------------------------===// 52 53 namespace { 54 55 /// Determine whether the given binary operation may overflow. 56 /// Sets \p Result to the value of the operation for BO_Add, BO_Sub, BO_Mul, 57 /// and signed BO_{Div,Rem}. For these opcodes, and for unsigned BO_{Div,Rem}, 58 /// the returned overflow check is precise. The returned value is 'true' for 59 /// all other opcodes, to be conservative. 60 bool mayHaveIntegerOverflow(llvm::ConstantInt *LHS, llvm::ConstantInt *RHS, 61 BinaryOperator::Opcode Opcode, bool Signed, 62 llvm::APInt &Result) { 63 // Assume overflow is possible, unless we can prove otherwise. 64 bool Overflow = true; 65 const auto &LHSAP = LHS->getValue(); 66 const auto &RHSAP = RHS->getValue(); 67 if (Opcode == BO_Add) { 68 if (Signed) 69 Result = LHSAP.sadd_ov(RHSAP, Overflow); 70 else 71 Result = LHSAP.uadd_ov(RHSAP, Overflow); 72 } else if (Opcode == BO_Sub) { 73 if (Signed) 74 Result = LHSAP.ssub_ov(RHSAP, Overflow); 75 else 76 Result = LHSAP.usub_ov(RHSAP, Overflow); 77 } else if (Opcode == BO_Mul) { 78 if (Signed) 79 Result = LHSAP.smul_ov(RHSAP, Overflow); 80 else 81 Result = LHSAP.umul_ov(RHSAP, Overflow); 82 } else if (Opcode == BO_Div || Opcode == BO_Rem) { 83 if (Signed && !RHS->isZero()) 84 Result = LHSAP.sdiv_ov(RHSAP, Overflow); 85 else 86 return false; 87 } 88 return Overflow; 89 } 90 91 struct BinOpInfo { 92 Value *LHS; 93 Value *RHS; 94 QualType Ty; // Computation Type. 95 BinaryOperator::Opcode Opcode; // Opcode of BinOp to perform 96 FPOptions FPFeatures; 97 const Expr *E; // Entire expr, for error unsupported. May not be binop. 98 99 /// Check if the binop can result in integer overflow. 100 bool mayHaveIntegerOverflow() const { 101 // Without constant input, we can't rule out overflow. 102 auto *LHSCI = dyn_cast<llvm::ConstantInt>(LHS); 103 auto *RHSCI = dyn_cast<llvm::ConstantInt>(RHS); 104 if (!LHSCI || !RHSCI) 105 return true; 106 107 llvm::APInt Result; 108 return ::mayHaveIntegerOverflow( 109 LHSCI, RHSCI, Opcode, Ty->hasSignedIntegerRepresentation(), Result); 110 } 111 112 /// Check if the binop computes a division or a remainder. 113 bool isDivremOp() const { 114 return Opcode == BO_Div || Opcode == BO_Rem || Opcode == BO_DivAssign || 115 Opcode == BO_RemAssign; 116 } 117 118 /// Check if the binop can result in an integer division by zero. 119 bool mayHaveIntegerDivisionByZero() const { 120 if (isDivremOp()) 121 if (auto *CI = dyn_cast<llvm::ConstantInt>(RHS)) 122 return CI->isZero(); 123 return true; 124 } 125 126 /// Check if the binop can result in a float division by zero. 127 bool mayHaveFloatDivisionByZero() const { 128 if (isDivremOp()) 129 if (auto *CFP = dyn_cast<llvm::ConstantFP>(RHS)) 130 return CFP->isZero(); 131 return true; 132 } 133 134 /// Check if at least one operand is a fixed point type. In such cases, this 135 /// operation did not follow usual arithmetic conversion and both operands 136 /// might not be of the same type. 137 bool isFixedPointOp() const { 138 // We cannot simply check the result type since comparison operations return 139 // an int. 140 if (const auto *BinOp = dyn_cast<BinaryOperator>(E)) { 141 QualType LHSType = BinOp->getLHS()->getType(); 142 QualType RHSType = BinOp->getRHS()->getType(); 143 return LHSType->isFixedPointType() || RHSType->isFixedPointType(); 144 } 145 if (const auto *UnOp = dyn_cast<UnaryOperator>(E)) 146 return UnOp->getSubExpr()->getType()->isFixedPointType(); 147 return false; 148 } 149 }; 150 151 static bool MustVisitNullValue(const Expr *E) { 152 // If a null pointer expression's type is the C++0x nullptr_t, then 153 // it's not necessarily a simple constant and it must be evaluated 154 // for its potential side effects. 155 return E->getType()->isNullPtrType(); 156 } 157 158 /// If \p E is a widened promoted integer, get its base (unpromoted) type. 159 static llvm::Optional<QualType> getUnwidenedIntegerType(const ASTContext &Ctx, 160 const Expr *E) { 161 const Expr *Base = E->IgnoreImpCasts(); 162 if (E == Base) 163 return llvm::None; 164 165 QualType BaseTy = Base->getType(); 166 if (!BaseTy->isPromotableIntegerType() || 167 Ctx.getTypeSize(BaseTy) >= Ctx.getTypeSize(E->getType())) 168 return llvm::None; 169 170 return BaseTy; 171 } 172 173 /// Check if \p E is a widened promoted integer. 174 static bool IsWidenedIntegerOp(const ASTContext &Ctx, const Expr *E) { 175 return getUnwidenedIntegerType(Ctx, E).hasValue(); 176 } 177 178 /// Check if we can skip the overflow check for \p Op. 179 static bool CanElideOverflowCheck(const ASTContext &Ctx, const BinOpInfo &Op) { 180 assert((isa<UnaryOperator>(Op.E) || isa<BinaryOperator>(Op.E)) && 181 "Expected a unary or binary operator"); 182 183 // If the binop has constant inputs and we can prove there is no overflow, 184 // we can elide the overflow check. 185 if (!Op.mayHaveIntegerOverflow()) 186 return true; 187 188 // If a unary op has a widened operand, the op cannot overflow. 189 if (const auto *UO = dyn_cast<UnaryOperator>(Op.E)) 190 return !UO->canOverflow(); 191 192 // We usually don't need overflow checks for binops with widened operands. 193 // Multiplication with promoted unsigned operands is a special case. 194 const auto *BO = cast<BinaryOperator>(Op.E); 195 auto OptionalLHSTy = getUnwidenedIntegerType(Ctx, BO->getLHS()); 196 if (!OptionalLHSTy) 197 return false; 198 199 auto OptionalRHSTy = getUnwidenedIntegerType(Ctx, BO->getRHS()); 200 if (!OptionalRHSTy) 201 return false; 202 203 QualType LHSTy = *OptionalLHSTy; 204 QualType RHSTy = *OptionalRHSTy; 205 206 // This is the simple case: binops without unsigned multiplication, and with 207 // widened operands. No overflow check is needed here. 208 if ((Op.Opcode != BO_Mul && Op.Opcode != BO_MulAssign) || 209 !LHSTy->isUnsignedIntegerType() || !RHSTy->isUnsignedIntegerType()) 210 return true; 211 212 // For unsigned multiplication the overflow check can be elided if either one 213 // of the unpromoted types are less than half the size of the promoted type. 214 unsigned PromotedSize = Ctx.getTypeSize(Op.E->getType()); 215 return (2 * Ctx.getTypeSize(LHSTy)) < PromotedSize || 216 (2 * Ctx.getTypeSize(RHSTy)) < PromotedSize; 217 } 218 219 class ScalarExprEmitter 220 : public StmtVisitor<ScalarExprEmitter, Value*> { 221 CodeGenFunction &CGF; 222 CGBuilderTy &Builder; 223 bool IgnoreResultAssign; 224 llvm::LLVMContext &VMContext; 225 public: 226 227 ScalarExprEmitter(CodeGenFunction &cgf, bool ira=false) 228 : CGF(cgf), Builder(CGF.Builder), IgnoreResultAssign(ira), 229 VMContext(cgf.getLLVMContext()) { 230 } 231 232 //===--------------------------------------------------------------------===// 233 // Utilities 234 //===--------------------------------------------------------------------===// 235 236 bool TestAndClearIgnoreResultAssign() { 237 bool I = IgnoreResultAssign; 238 IgnoreResultAssign = false; 239 return I; 240 } 241 242 llvm::Type *ConvertType(QualType T) { return CGF.ConvertType(T); } 243 LValue EmitLValue(const Expr *E) { return CGF.EmitLValue(E); } 244 LValue EmitCheckedLValue(const Expr *E, CodeGenFunction::TypeCheckKind TCK) { 245 return CGF.EmitCheckedLValue(E, TCK); 246 } 247 248 void EmitBinOpCheck(ArrayRef<std::pair<Value *, SanitizerMask>> Checks, 249 const BinOpInfo &Info); 250 251 Value *EmitLoadOfLValue(LValue LV, SourceLocation Loc) { 252 return CGF.EmitLoadOfLValue(LV, Loc).getScalarVal(); 253 } 254 255 void EmitLValueAlignmentAssumption(const Expr *E, Value *V) { 256 const AlignValueAttr *AVAttr = nullptr; 257 if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { 258 const ValueDecl *VD = DRE->getDecl(); 259 260 if (VD->getType()->isReferenceType()) { 261 if (const auto *TTy = 262 dyn_cast<TypedefType>(VD->getType().getNonReferenceType())) 263 AVAttr = TTy->getDecl()->getAttr<AlignValueAttr>(); 264 } else { 265 // Assumptions for function parameters are emitted at the start of the 266 // function, so there is no need to repeat that here, 267 // unless the alignment-assumption sanitizer is enabled, 268 // then we prefer the assumption over alignment attribute 269 // on IR function param. 270 if (isa<ParmVarDecl>(VD) && !CGF.SanOpts.has(SanitizerKind::Alignment)) 271 return; 272 273 AVAttr = VD->getAttr<AlignValueAttr>(); 274 } 275 } 276 277 if (!AVAttr) 278 if (const auto *TTy = 279 dyn_cast<TypedefType>(E->getType())) 280 AVAttr = TTy->getDecl()->getAttr<AlignValueAttr>(); 281 282 if (!AVAttr) 283 return; 284 285 Value *AlignmentValue = CGF.EmitScalarExpr(AVAttr->getAlignment()); 286 llvm::ConstantInt *AlignmentCI = cast<llvm::ConstantInt>(AlignmentValue); 287 CGF.emitAlignmentAssumption(V, E, AVAttr->getLocation(), AlignmentCI); 288 } 289 290 /// EmitLoadOfLValue - Given an expression with complex type that represents a 291 /// value l-value, this method emits the address of the l-value, then loads 292 /// and returns the result. 293 Value *EmitLoadOfLValue(const Expr *E) { 294 Value *V = EmitLoadOfLValue(EmitCheckedLValue(E, CodeGenFunction::TCK_Load), 295 E->getExprLoc()); 296 297 EmitLValueAlignmentAssumption(E, V); 298 return V; 299 } 300 301 /// EmitConversionToBool - Convert the specified expression value to a 302 /// boolean (i1) truth value. This is equivalent to "Val != 0". 303 Value *EmitConversionToBool(Value *Src, QualType DstTy); 304 305 /// Emit a check that a conversion from a floating-point type does not 306 /// overflow. 307 void EmitFloatConversionCheck(Value *OrigSrc, QualType OrigSrcType, 308 Value *Src, QualType SrcType, QualType DstType, 309 llvm::Type *DstTy, SourceLocation Loc); 310 311 /// Known implicit conversion check kinds. 312 /// Keep in sync with the enum of the same name in ubsan_handlers.h 313 enum ImplicitConversionCheckKind : unsigned char { 314 ICCK_IntegerTruncation = 0, // Legacy, was only used by clang 7. 315 ICCK_UnsignedIntegerTruncation = 1, 316 ICCK_SignedIntegerTruncation = 2, 317 ICCK_IntegerSignChange = 3, 318 ICCK_SignedIntegerTruncationOrSignChange = 4, 319 }; 320 321 /// Emit a check that an [implicit] truncation of an integer does not 322 /// discard any bits. It is not UB, so we use the value after truncation. 323 void EmitIntegerTruncationCheck(Value *Src, QualType SrcType, Value *Dst, 324 QualType DstType, SourceLocation Loc); 325 326 /// Emit a check that an [implicit] conversion of an integer does not change 327 /// the sign of the value. It is not UB, so we use the value after conversion. 328 /// NOTE: Src and Dst may be the exact same value! (point to the same thing) 329 void EmitIntegerSignChangeCheck(Value *Src, QualType SrcType, Value *Dst, 330 QualType DstType, SourceLocation Loc); 331 332 /// Emit a conversion from the specified type to the specified destination 333 /// type, both of which are LLVM scalar types. 334 struct ScalarConversionOpts { 335 bool TreatBooleanAsSigned; 336 bool EmitImplicitIntegerTruncationChecks; 337 bool EmitImplicitIntegerSignChangeChecks; 338 339 ScalarConversionOpts() 340 : TreatBooleanAsSigned(false), 341 EmitImplicitIntegerTruncationChecks(false), 342 EmitImplicitIntegerSignChangeChecks(false) {} 343 344 ScalarConversionOpts(clang::SanitizerSet SanOpts) 345 : TreatBooleanAsSigned(false), 346 EmitImplicitIntegerTruncationChecks( 347 SanOpts.hasOneOf(SanitizerKind::ImplicitIntegerTruncation)), 348 EmitImplicitIntegerSignChangeChecks( 349 SanOpts.has(SanitizerKind::ImplicitIntegerSignChange)) {} 350 }; 351 Value * 352 EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy, 353 SourceLocation Loc, 354 ScalarConversionOpts Opts = ScalarConversionOpts()); 355 356 /// Convert between either a fixed point and other fixed point or fixed point 357 /// and an integer. 358 Value *EmitFixedPointConversion(Value *Src, QualType SrcTy, QualType DstTy, 359 SourceLocation Loc); 360 361 /// Emit a conversion from the specified complex type to the specified 362 /// destination type, where the destination type is an LLVM scalar type. 363 Value *EmitComplexToScalarConversion(CodeGenFunction::ComplexPairTy Src, 364 QualType SrcTy, QualType DstTy, 365 SourceLocation Loc); 366 367 /// EmitNullValue - Emit a value that corresponds to null for the given type. 368 Value *EmitNullValue(QualType Ty); 369 370 /// EmitFloatToBoolConversion - Perform an FP to boolean conversion. 371 Value *EmitFloatToBoolConversion(Value *V) { 372 // Compare against 0.0 for fp scalars. 373 llvm::Value *Zero = llvm::Constant::getNullValue(V->getType()); 374 return Builder.CreateFCmpUNE(V, Zero, "tobool"); 375 } 376 377 /// EmitPointerToBoolConversion - Perform a pointer to boolean conversion. 378 Value *EmitPointerToBoolConversion(Value *V, QualType QT) { 379 Value *Zero = CGF.CGM.getNullPointer(cast<llvm::PointerType>(V->getType()), QT); 380 381 return Builder.CreateICmpNE(V, Zero, "tobool"); 382 } 383 384 Value *EmitIntToBoolConversion(Value *V) { 385 // Because of the type rules of C, we often end up computing a 386 // logical value, then zero extending it to int, then wanting it 387 // as a logical value again. Optimize this common case. 388 if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(V)) { 389 if (ZI->getOperand(0)->getType() == Builder.getInt1Ty()) { 390 Value *Result = ZI->getOperand(0); 391 // If there aren't any more uses, zap the instruction to save space. 392 // Note that there can be more uses, for example if this 393 // is the result of an assignment. 394 if (ZI->use_empty()) 395 ZI->eraseFromParent(); 396 return Result; 397 } 398 } 399 400 return Builder.CreateIsNotNull(V, "tobool"); 401 } 402 403 //===--------------------------------------------------------------------===// 404 // Visitor Methods 405 //===--------------------------------------------------------------------===// 406 407 Value *Visit(Expr *E) { 408 ApplyDebugLocation DL(CGF, E); 409 return StmtVisitor<ScalarExprEmitter, Value*>::Visit(E); 410 } 411 412 Value *VisitStmt(Stmt *S) { 413 S->dump(llvm::errs(), CGF.getContext()); 414 llvm_unreachable("Stmt can't have complex result type!"); 415 } 416 Value *VisitExpr(Expr *S); 417 418 Value *VisitConstantExpr(ConstantExpr *E) { 419 if (Value *Result = ConstantEmitter(CGF).tryEmitConstantExpr(E)) { 420 if (E->isGLValue()) 421 return CGF.Builder.CreateLoad(Address( 422 Result, CGF.getContext().getTypeAlignInChars(E->getType()))); 423 return Result; 424 } 425 return Visit(E->getSubExpr()); 426 } 427 Value *VisitParenExpr(ParenExpr *PE) { 428 return Visit(PE->getSubExpr()); 429 } 430 Value *VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *E) { 431 return Visit(E->getReplacement()); 432 } 433 Value *VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 434 return Visit(GE->getResultExpr()); 435 } 436 Value *VisitCoawaitExpr(CoawaitExpr *S) { 437 return CGF.EmitCoawaitExpr(*S).getScalarVal(); 438 } 439 Value *VisitCoyieldExpr(CoyieldExpr *S) { 440 return CGF.EmitCoyieldExpr(*S).getScalarVal(); 441 } 442 Value *VisitUnaryCoawait(const UnaryOperator *E) { 443 return Visit(E->getSubExpr()); 444 } 445 446 // Leaves. 447 Value *VisitIntegerLiteral(const IntegerLiteral *E) { 448 return Builder.getInt(E->getValue()); 449 } 450 Value *VisitFixedPointLiteral(const FixedPointLiteral *E) { 451 return Builder.getInt(E->getValue()); 452 } 453 Value *VisitFloatingLiteral(const FloatingLiteral *E) { 454 return llvm::ConstantFP::get(VMContext, E->getValue()); 455 } 456 Value *VisitCharacterLiteral(const CharacterLiteral *E) { 457 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); 458 } 459 Value *VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { 460 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); 461 } 462 Value *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { 463 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); 464 } 465 Value *VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { 466 return EmitNullValue(E->getType()); 467 } 468 Value *VisitGNUNullExpr(const GNUNullExpr *E) { 469 return EmitNullValue(E->getType()); 470 } 471 Value *VisitOffsetOfExpr(OffsetOfExpr *E); 472 Value *VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); 473 Value *VisitAddrLabelExpr(const AddrLabelExpr *E) { 474 llvm::Value *V = CGF.GetAddrOfLabel(E->getLabel()); 475 return Builder.CreateBitCast(V, ConvertType(E->getType())); 476 } 477 478 Value *VisitSizeOfPackExpr(SizeOfPackExpr *E) { 479 return llvm::ConstantInt::get(ConvertType(E->getType()),E->getPackLength()); 480 } 481 482 Value *VisitPseudoObjectExpr(PseudoObjectExpr *E) { 483 return CGF.EmitPseudoObjectRValue(E).getScalarVal(); 484 } 485 486 Value *VisitOpaqueValueExpr(OpaqueValueExpr *E) { 487 if (E->isGLValue()) 488 return EmitLoadOfLValue(CGF.getOrCreateOpaqueLValueMapping(E), 489 E->getExprLoc()); 490 491 // Otherwise, assume the mapping is the scalar directly. 492 return CGF.getOrCreateOpaqueRValueMapping(E).getScalarVal(); 493 } 494 495 // l-values. 496 Value *VisitDeclRefExpr(DeclRefExpr *E) { 497 if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E)) 498 return CGF.emitScalarConstant(Constant, E); 499 return EmitLoadOfLValue(E); 500 } 501 502 Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) { 503 return CGF.EmitObjCSelectorExpr(E); 504 } 505 Value *VisitObjCProtocolExpr(ObjCProtocolExpr *E) { 506 return CGF.EmitObjCProtocolExpr(E); 507 } 508 Value *VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { 509 return EmitLoadOfLValue(E); 510 } 511 Value *VisitObjCMessageExpr(ObjCMessageExpr *E) { 512 if (E->getMethodDecl() && 513 E->getMethodDecl()->getReturnType()->isReferenceType()) 514 return EmitLoadOfLValue(E); 515 return CGF.EmitObjCMessageExpr(E).getScalarVal(); 516 } 517 518 Value *VisitObjCIsaExpr(ObjCIsaExpr *E) { 519 LValue LV = CGF.EmitObjCIsaExpr(E); 520 Value *V = CGF.EmitLoadOfLValue(LV, E->getExprLoc()).getScalarVal(); 521 return V; 522 } 523 524 Value *VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) { 525 VersionTuple Version = E->getVersion(); 526 527 // If we're checking for a platform older than our minimum deployment 528 // target, we can fold the check away. 529 if (Version <= CGF.CGM.getTarget().getPlatformMinVersion()) 530 return llvm::ConstantInt::get(Builder.getInt1Ty(), 1); 531 532 Optional<unsigned> Min = Version.getMinor(), SMin = Version.getSubminor(); 533 llvm::Value *Args[] = { 534 llvm::ConstantInt::get(CGF.CGM.Int32Ty, Version.getMajor()), 535 llvm::ConstantInt::get(CGF.CGM.Int32Ty, Min ? *Min : 0), 536 llvm::ConstantInt::get(CGF.CGM.Int32Ty, SMin ? *SMin : 0), 537 }; 538 539 return CGF.EmitBuiltinAvailable(Args); 540 } 541 542 Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); 543 Value *VisitMatrixSubscriptExpr(MatrixSubscriptExpr *E); 544 Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E); 545 Value *VisitConvertVectorExpr(ConvertVectorExpr *E); 546 Value *VisitMemberExpr(MemberExpr *E); 547 Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } 548 Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 549 // Strictly speaking, we shouldn't be calling EmitLoadOfLValue, which 550 // transitively calls EmitCompoundLiteralLValue, here in C++ since compound 551 // literals aren't l-values in C++. We do so simply because that's the 552 // cleanest way to handle compound literals in C++. 553 // See the discussion here: https://reviews.llvm.org/D64464 554 return EmitLoadOfLValue(E); 555 } 556 557 Value *VisitInitListExpr(InitListExpr *E); 558 559 Value *VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) { 560 assert(CGF.getArrayInitIndex() && 561 "ArrayInitIndexExpr not inside an ArrayInitLoopExpr?"); 562 return CGF.getArrayInitIndex(); 563 } 564 565 Value *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { 566 return EmitNullValue(E->getType()); 567 } 568 Value *VisitExplicitCastExpr(ExplicitCastExpr *E) { 569 CGF.CGM.EmitExplicitCastExprType(E, &CGF); 570 return VisitCastExpr(E); 571 } 572 Value *VisitCastExpr(CastExpr *E); 573 574 Value *VisitCallExpr(const CallExpr *E) { 575 if (E->getCallReturnType(CGF.getContext())->isReferenceType()) 576 return EmitLoadOfLValue(E); 577 578 Value *V = CGF.EmitCallExpr(E).getScalarVal(); 579 580 EmitLValueAlignmentAssumption(E, V); 581 return V; 582 } 583 584 Value *VisitStmtExpr(const StmtExpr *E); 585 586 // Unary Operators. 587 Value *VisitUnaryPostDec(const UnaryOperator *E) { 588 LValue LV = EmitLValue(E->getSubExpr()); 589 return EmitScalarPrePostIncDec(E, LV, false, false); 590 } 591 Value *VisitUnaryPostInc(const UnaryOperator *E) { 592 LValue LV = EmitLValue(E->getSubExpr()); 593 return EmitScalarPrePostIncDec(E, LV, true, false); 594 } 595 Value *VisitUnaryPreDec(const UnaryOperator *E) { 596 LValue LV = EmitLValue(E->getSubExpr()); 597 return EmitScalarPrePostIncDec(E, LV, false, true); 598 } 599 Value *VisitUnaryPreInc(const UnaryOperator *E) { 600 LValue LV = EmitLValue(E->getSubExpr()); 601 return EmitScalarPrePostIncDec(E, LV, true, true); 602 } 603 604 llvm::Value *EmitIncDecConsiderOverflowBehavior(const UnaryOperator *E, 605 llvm::Value *InVal, 606 bool IsInc); 607 608 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 609 bool isInc, bool isPre); 610 611 612 Value *VisitUnaryAddrOf(const UnaryOperator *E) { 613 if (isa<MemberPointerType>(E->getType())) // never sugared 614 return CGF.CGM.getMemberPointerConstant(E); 615 616 return EmitLValue(E->getSubExpr()).getPointer(CGF); 617 } 618 Value *VisitUnaryDeref(const UnaryOperator *E) { 619 if (E->getType()->isVoidType()) 620 return Visit(E->getSubExpr()); // the actual value should be unused 621 return EmitLoadOfLValue(E); 622 } 623 Value *VisitUnaryPlus(const UnaryOperator *E) { 624 // This differs from gcc, though, most likely due to a bug in gcc. 625 TestAndClearIgnoreResultAssign(); 626 return Visit(E->getSubExpr()); 627 } 628 Value *VisitUnaryMinus (const UnaryOperator *E); 629 Value *VisitUnaryNot (const UnaryOperator *E); 630 Value *VisitUnaryLNot (const UnaryOperator *E); 631 Value *VisitUnaryReal (const UnaryOperator *E); 632 Value *VisitUnaryImag (const UnaryOperator *E); 633 Value *VisitUnaryExtension(const UnaryOperator *E) { 634 return Visit(E->getSubExpr()); 635 } 636 637 // C++ 638 Value *VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E) { 639 return EmitLoadOfLValue(E); 640 } 641 Value *VisitSourceLocExpr(SourceLocExpr *SLE) { 642 auto &Ctx = CGF.getContext(); 643 APValue Evaluated = 644 SLE->EvaluateInContext(Ctx, CGF.CurSourceLocExprScope.getDefaultExpr()); 645 return ConstantEmitter(CGF).emitAbstract(SLE->getLocation(), Evaluated, 646 SLE->getType()); 647 } 648 649 Value *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 650 CodeGenFunction::CXXDefaultArgExprScope Scope(CGF, DAE); 651 return Visit(DAE->getExpr()); 652 } 653 Value *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE) { 654 CodeGenFunction::CXXDefaultInitExprScope Scope(CGF, DIE); 655 return Visit(DIE->getExpr()); 656 } 657 Value *VisitCXXThisExpr(CXXThisExpr *TE) { 658 return CGF.LoadCXXThis(); 659 } 660 661 Value *VisitExprWithCleanups(ExprWithCleanups *E); 662 Value *VisitCXXNewExpr(const CXXNewExpr *E) { 663 return CGF.EmitCXXNewExpr(E); 664 } 665 Value *VisitCXXDeleteExpr(const CXXDeleteExpr *E) { 666 CGF.EmitCXXDeleteExpr(E); 667 return nullptr; 668 } 669 670 Value *VisitTypeTraitExpr(const TypeTraitExpr *E) { 671 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue()); 672 } 673 674 Value *VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) { 675 return Builder.getInt1(E->isSatisfied()); 676 } 677 678 Value *VisitRequiresExpr(const RequiresExpr *E) { 679 return Builder.getInt1(E->isSatisfied()); 680 } 681 682 Value *VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { 683 return llvm::ConstantInt::get(Builder.getInt32Ty(), E->getValue()); 684 } 685 686 Value *VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { 687 return llvm::ConstantInt::get(Builder.getInt1Ty(), E->getValue()); 688 } 689 690 Value *VisitCXXPseudoDestructorExpr(const CXXPseudoDestructorExpr *E) { 691 // C++ [expr.pseudo]p1: 692 // The result shall only be used as the operand for the function call 693 // operator (), and the result of such a call has type void. The only 694 // effect is the evaluation of the postfix-expression before the dot or 695 // arrow. 696 CGF.EmitScalarExpr(E->getBase()); 697 return nullptr; 698 } 699 700 Value *VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { 701 return EmitNullValue(E->getType()); 702 } 703 704 Value *VisitCXXThrowExpr(const CXXThrowExpr *E) { 705 CGF.EmitCXXThrowExpr(E); 706 return nullptr; 707 } 708 709 Value *VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { 710 return Builder.getInt1(E->getValue()); 711 } 712 713 // Binary Operators. 714 Value *EmitMul(const BinOpInfo &Ops) { 715 if (Ops.Ty->isSignedIntegerOrEnumerationType()) { 716 switch (CGF.getLangOpts().getSignedOverflowBehavior()) { 717 case LangOptions::SOB_Defined: 718 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); 719 case LangOptions::SOB_Undefined: 720 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) 721 return Builder.CreateNSWMul(Ops.LHS, Ops.RHS, "mul"); 722 LLVM_FALLTHROUGH; 723 case LangOptions::SOB_Trapping: 724 if (CanElideOverflowCheck(CGF.getContext(), Ops)) 725 return Builder.CreateNSWMul(Ops.LHS, Ops.RHS, "mul"); 726 return EmitOverflowCheckedBinOp(Ops); 727 } 728 } 729 730 if (Ops.Ty->isConstantMatrixType()) { 731 llvm::MatrixBuilder<CGBuilderTy> MB(Builder); 732 // We need to check the types of the operands of the operator to get the 733 // correct matrix dimensions. 734 auto *BO = cast<BinaryOperator>(Ops.E); 735 auto *LHSMatTy = dyn_cast<ConstantMatrixType>( 736 BO->getLHS()->getType().getCanonicalType()); 737 auto *RHSMatTy = dyn_cast<ConstantMatrixType>( 738 BO->getRHS()->getType().getCanonicalType()); 739 if (LHSMatTy && RHSMatTy) 740 return MB.CreateMatrixMultiply(Ops.LHS, Ops.RHS, LHSMatTy->getNumRows(), 741 LHSMatTy->getNumColumns(), 742 RHSMatTy->getNumColumns()); 743 return MB.CreateScalarMultiply(Ops.LHS, Ops.RHS); 744 } 745 746 if (Ops.Ty->isUnsignedIntegerType() && 747 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow) && 748 !CanElideOverflowCheck(CGF.getContext(), Ops)) 749 return EmitOverflowCheckedBinOp(Ops); 750 751 if (Ops.LHS->getType()->isFPOrFPVectorTy()) { 752 // Preserve the old values 753 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Ops.FPFeatures); 754 return Builder.CreateFMul(Ops.LHS, Ops.RHS, "mul"); 755 } 756 if (Ops.isFixedPointOp()) 757 return EmitFixedPointBinOp(Ops); 758 return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul"); 759 } 760 /// Create a binary op that checks for overflow. 761 /// Currently only supports +, - and *. 762 Value *EmitOverflowCheckedBinOp(const BinOpInfo &Ops); 763 764 // Check for undefined division and modulus behaviors. 765 void EmitUndefinedBehaviorIntegerDivAndRemCheck(const BinOpInfo &Ops, 766 llvm::Value *Zero,bool isDiv); 767 // Common helper for getting how wide LHS of shift is. 768 static Value *GetWidthMinusOneValue(Value* LHS,Value* RHS); 769 770 // Used for shifting constraints for OpenCL, do mask for powers of 2, URem for 771 // non powers of two. 772 Value *ConstrainShiftValue(Value *LHS, Value *RHS, const Twine &Name); 773 774 Value *EmitDiv(const BinOpInfo &Ops); 775 Value *EmitRem(const BinOpInfo &Ops); 776 Value *EmitAdd(const BinOpInfo &Ops); 777 Value *EmitSub(const BinOpInfo &Ops); 778 Value *EmitShl(const BinOpInfo &Ops); 779 Value *EmitShr(const BinOpInfo &Ops); 780 Value *EmitAnd(const BinOpInfo &Ops) { 781 return Builder.CreateAnd(Ops.LHS, Ops.RHS, "and"); 782 } 783 Value *EmitXor(const BinOpInfo &Ops) { 784 return Builder.CreateXor(Ops.LHS, Ops.RHS, "xor"); 785 } 786 Value *EmitOr (const BinOpInfo &Ops) { 787 return Builder.CreateOr(Ops.LHS, Ops.RHS, "or"); 788 } 789 790 // Helper functions for fixed point binary operations. 791 Value *EmitFixedPointBinOp(const BinOpInfo &Ops); 792 793 BinOpInfo EmitBinOps(const BinaryOperator *E); 794 LValue EmitCompoundAssignLValue(const CompoundAssignOperator *E, 795 Value *(ScalarExprEmitter::*F)(const BinOpInfo &), 796 Value *&Result); 797 798 Value *EmitCompoundAssign(const CompoundAssignOperator *E, 799 Value *(ScalarExprEmitter::*F)(const BinOpInfo &)); 800 801 // Binary operators and binary compound assignment operators. 802 #define HANDLEBINOP(OP) \ 803 Value *VisitBin ## OP(const BinaryOperator *E) { \ 804 return Emit ## OP(EmitBinOps(E)); \ 805 } \ 806 Value *VisitBin ## OP ## Assign(const CompoundAssignOperator *E) { \ 807 return EmitCompoundAssign(E, &ScalarExprEmitter::Emit ## OP); \ 808 } 809 HANDLEBINOP(Mul) 810 HANDLEBINOP(Div) 811 HANDLEBINOP(Rem) 812 HANDLEBINOP(Add) 813 HANDLEBINOP(Sub) 814 HANDLEBINOP(Shl) 815 HANDLEBINOP(Shr) 816 HANDLEBINOP(And) 817 HANDLEBINOP(Xor) 818 HANDLEBINOP(Or) 819 #undef HANDLEBINOP 820 821 // Comparisons. 822 Value *EmitCompare(const BinaryOperator *E, llvm::CmpInst::Predicate UICmpOpc, 823 llvm::CmpInst::Predicate SICmpOpc, 824 llvm::CmpInst::Predicate FCmpOpc, bool IsSignaling); 825 #define VISITCOMP(CODE, UI, SI, FP, SIG) \ 826 Value *VisitBin##CODE(const BinaryOperator *E) { \ 827 return EmitCompare(E, llvm::ICmpInst::UI, llvm::ICmpInst::SI, \ 828 llvm::FCmpInst::FP, SIG); } 829 VISITCOMP(LT, ICMP_ULT, ICMP_SLT, FCMP_OLT, true) 830 VISITCOMP(GT, ICMP_UGT, ICMP_SGT, FCMP_OGT, true) 831 VISITCOMP(LE, ICMP_ULE, ICMP_SLE, FCMP_OLE, true) 832 VISITCOMP(GE, ICMP_UGE, ICMP_SGE, FCMP_OGE, true) 833 VISITCOMP(EQ, ICMP_EQ , ICMP_EQ , FCMP_OEQ, false) 834 VISITCOMP(NE, ICMP_NE , ICMP_NE , FCMP_UNE, false) 835 #undef VISITCOMP 836 837 Value *VisitBinAssign (const BinaryOperator *E); 838 839 Value *VisitBinLAnd (const BinaryOperator *E); 840 Value *VisitBinLOr (const BinaryOperator *E); 841 Value *VisitBinComma (const BinaryOperator *E); 842 843 Value *VisitBinPtrMemD(const Expr *E) { return EmitLoadOfLValue(E); } 844 Value *VisitBinPtrMemI(const Expr *E) { return EmitLoadOfLValue(E); } 845 846 Value *VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *E) { 847 return Visit(E->getSemanticForm()); 848 } 849 850 // Other Operators. 851 Value *VisitBlockExpr(const BlockExpr *BE); 852 Value *VisitAbstractConditionalOperator(const AbstractConditionalOperator *); 853 Value *VisitChooseExpr(ChooseExpr *CE); 854 Value *VisitVAArgExpr(VAArgExpr *VE); 855 Value *VisitObjCStringLiteral(const ObjCStringLiteral *E) { 856 return CGF.EmitObjCStringLiteral(E); 857 } 858 Value *VisitObjCBoxedExpr(ObjCBoxedExpr *E) { 859 return CGF.EmitObjCBoxedExpr(E); 860 } 861 Value *VisitObjCArrayLiteral(ObjCArrayLiteral *E) { 862 return CGF.EmitObjCArrayLiteral(E); 863 } 864 Value *VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { 865 return CGF.EmitObjCDictionaryLiteral(E); 866 } 867 Value *VisitAsTypeExpr(AsTypeExpr *CE); 868 Value *VisitAtomicExpr(AtomicExpr *AE); 869 }; 870 } // end anonymous namespace. 871 872 //===----------------------------------------------------------------------===// 873 // Utilities 874 //===----------------------------------------------------------------------===// 875 876 /// EmitConversionToBool - Convert the specified expression value to a 877 /// boolean (i1) truth value. This is equivalent to "Val != 0". 878 Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) { 879 assert(SrcType.isCanonical() && "EmitScalarConversion strips typedefs"); 880 881 if (SrcType->isRealFloatingType()) 882 return EmitFloatToBoolConversion(Src); 883 884 if (const MemberPointerType *MPT = dyn_cast<MemberPointerType>(SrcType)) 885 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, Src, MPT); 886 887 assert((SrcType->isIntegerType() || isa<llvm::PointerType>(Src->getType())) && 888 "Unknown scalar type to convert"); 889 890 if (isa<llvm::IntegerType>(Src->getType())) 891 return EmitIntToBoolConversion(Src); 892 893 assert(isa<llvm::PointerType>(Src->getType())); 894 return EmitPointerToBoolConversion(Src, SrcType); 895 } 896 897 void ScalarExprEmitter::EmitFloatConversionCheck( 898 Value *OrigSrc, QualType OrigSrcType, Value *Src, QualType SrcType, 899 QualType DstType, llvm::Type *DstTy, SourceLocation Loc) { 900 assert(SrcType->isFloatingType() && "not a conversion from floating point"); 901 if (!isa<llvm::IntegerType>(DstTy)) 902 return; 903 904 CodeGenFunction::SanitizerScope SanScope(&CGF); 905 using llvm::APFloat; 906 using llvm::APSInt; 907 908 llvm::Value *Check = nullptr; 909 const llvm::fltSemantics &SrcSema = 910 CGF.getContext().getFloatTypeSemantics(OrigSrcType); 911 912 // Floating-point to integer. This has undefined behavior if the source is 913 // +-Inf, NaN, or doesn't fit into the destination type (after truncation 914 // to an integer). 915 unsigned Width = CGF.getContext().getIntWidth(DstType); 916 bool Unsigned = DstType->isUnsignedIntegerOrEnumerationType(); 917 918 APSInt Min = APSInt::getMinValue(Width, Unsigned); 919 APFloat MinSrc(SrcSema, APFloat::uninitialized); 920 if (MinSrc.convertFromAPInt(Min, !Unsigned, APFloat::rmTowardZero) & 921 APFloat::opOverflow) 922 // Don't need an overflow check for lower bound. Just check for 923 // -Inf/NaN. 924 MinSrc = APFloat::getInf(SrcSema, true); 925 else 926 // Find the largest value which is too small to represent (before 927 // truncation toward zero). 928 MinSrc.subtract(APFloat(SrcSema, 1), APFloat::rmTowardNegative); 929 930 APSInt Max = APSInt::getMaxValue(Width, Unsigned); 931 APFloat MaxSrc(SrcSema, APFloat::uninitialized); 932 if (MaxSrc.convertFromAPInt(Max, !Unsigned, APFloat::rmTowardZero) & 933 APFloat::opOverflow) 934 // Don't need an overflow check for upper bound. Just check for 935 // +Inf/NaN. 936 MaxSrc = APFloat::getInf(SrcSema, false); 937 else 938 // Find the smallest value which is too large to represent (before 939 // truncation toward zero). 940 MaxSrc.add(APFloat(SrcSema, 1), APFloat::rmTowardPositive); 941 942 // If we're converting from __half, convert the range to float to match 943 // the type of src. 944 if (OrigSrcType->isHalfType()) { 945 const llvm::fltSemantics &Sema = 946 CGF.getContext().getFloatTypeSemantics(SrcType); 947 bool IsInexact; 948 MinSrc.convert(Sema, APFloat::rmTowardZero, &IsInexact); 949 MaxSrc.convert(Sema, APFloat::rmTowardZero, &IsInexact); 950 } 951 952 llvm::Value *GE = 953 Builder.CreateFCmpOGT(Src, llvm::ConstantFP::get(VMContext, MinSrc)); 954 llvm::Value *LE = 955 Builder.CreateFCmpOLT(Src, llvm::ConstantFP::get(VMContext, MaxSrc)); 956 Check = Builder.CreateAnd(GE, LE); 957 958 llvm::Constant *StaticArgs[] = {CGF.EmitCheckSourceLocation(Loc), 959 CGF.EmitCheckTypeDescriptor(OrigSrcType), 960 CGF.EmitCheckTypeDescriptor(DstType)}; 961 CGF.EmitCheck(std::make_pair(Check, SanitizerKind::FloatCastOverflow), 962 SanitizerHandler::FloatCastOverflow, StaticArgs, OrigSrc); 963 } 964 965 // Should be called within CodeGenFunction::SanitizerScope RAII scope. 966 // Returns 'i1 false' when the truncation Src -> Dst was lossy. 967 static std::pair<ScalarExprEmitter::ImplicitConversionCheckKind, 968 std::pair<llvm::Value *, SanitizerMask>> 969 EmitIntegerTruncationCheckHelper(Value *Src, QualType SrcType, Value *Dst, 970 QualType DstType, CGBuilderTy &Builder) { 971 llvm::Type *SrcTy = Src->getType(); 972 llvm::Type *DstTy = Dst->getType(); 973 (void)DstTy; // Only used in assert() 974 975 // This should be truncation of integral types. 976 assert(Src != Dst); 977 assert(SrcTy->getScalarSizeInBits() > Dst->getType()->getScalarSizeInBits()); 978 assert(isa<llvm::IntegerType>(SrcTy) && isa<llvm::IntegerType>(DstTy) && 979 "non-integer llvm type"); 980 981 bool SrcSigned = SrcType->isSignedIntegerOrEnumerationType(); 982 bool DstSigned = DstType->isSignedIntegerOrEnumerationType(); 983 984 // If both (src and dst) types are unsigned, then it's an unsigned truncation. 985 // Else, it is a signed truncation. 986 ScalarExprEmitter::ImplicitConversionCheckKind Kind; 987 SanitizerMask Mask; 988 if (!SrcSigned && !DstSigned) { 989 Kind = ScalarExprEmitter::ICCK_UnsignedIntegerTruncation; 990 Mask = SanitizerKind::ImplicitUnsignedIntegerTruncation; 991 } else { 992 Kind = ScalarExprEmitter::ICCK_SignedIntegerTruncation; 993 Mask = SanitizerKind::ImplicitSignedIntegerTruncation; 994 } 995 996 llvm::Value *Check = nullptr; 997 // 1. Extend the truncated value back to the same width as the Src. 998 Check = Builder.CreateIntCast(Dst, SrcTy, DstSigned, "anyext"); 999 // 2. Equality-compare with the original source value 1000 Check = Builder.CreateICmpEQ(Check, Src, "truncheck"); 1001 // If the comparison result is 'i1 false', then the truncation was lossy. 1002 return std::make_pair(Kind, std::make_pair(Check, Mask)); 1003 } 1004 1005 static bool PromotionIsPotentiallyEligibleForImplicitIntegerConversionCheck( 1006 QualType SrcType, QualType DstType) { 1007 return SrcType->isIntegerType() && DstType->isIntegerType(); 1008 } 1009 1010 void ScalarExprEmitter::EmitIntegerTruncationCheck(Value *Src, QualType SrcType, 1011 Value *Dst, QualType DstType, 1012 SourceLocation Loc) { 1013 if (!CGF.SanOpts.hasOneOf(SanitizerKind::ImplicitIntegerTruncation)) 1014 return; 1015 1016 // We only care about int->int conversions here. 1017 // We ignore conversions to/from pointer and/or bool. 1018 if (!PromotionIsPotentiallyEligibleForImplicitIntegerConversionCheck(SrcType, 1019 DstType)) 1020 return; 1021 1022 unsigned SrcBits = Src->getType()->getScalarSizeInBits(); 1023 unsigned DstBits = Dst->getType()->getScalarSizeInBits(); 1024 // This must be truncation. Else we do not care. 1025 if (SrcBits <= DstBits) 1026 return; 1027 1028 assert(!DstType->isBooleanType() && "we should not get here with booleans."); 1029 1030 // If the integer sign change sanitizer is enabled, 1031 // and we are truncating from larger unsigned type to smaller signed type, 1032 // let that next sanitizer deal with it. 1033 bool SrcSigned = SrcType->isSignedIntegerOrEnumerationType(); 1034 bool DstSigned = DstType->isSignedIntegerOrEnumerationType(); 1035 if (CGF.SanOpts.has(SanitizerKind::ImplicitIntegerSignChange) && 1036 (!SrcSigned && DstSigned)) 1037 return; 1038 1039 CodeGenFunction::SanitizerScope SanScope(&CGF); 1040 1041 std::pair<ScalarExprEmitter::ImplicitConversionCheckKind, 1042 std::pair<llvm::Value *, SanitizerMask>> 1043 Check = 1044 EmitIntegerTruncationCheckHelper(Src, SrcType, Dst, DstType, Builder); 1045 // If the comparison result is 'i1 false', then the truncation was lossy. 1046 1047 // Do we care about this type of truncation? 1048 if (!CGF.SanOpts.has(Check.second.second)) 1049 return; 1050 1051 llvm::Constant *StaticArgs[] = { 1052 CGF.EmitCheckSourceLocation(Loc), CGF.EmitCheckTypeDescriptor(SrcType), 1053 CGF.EmitCheckTypeDescriptor(DstType), 1054 llvm::ConstantInt::get(Builder.getInt8Ty(), Check.first)}; 1055 CGF.EmitCheck(Check.second, SanitizerHandler::ImplicitConversion, StaticArgs, 1056 {Src, Dst}); 1057 } 1058 1059 // Should be called within CodeGenFunction::SanitizerScope RAII scope. 1060 // Returns 'i1 false' when the conversion Src -> Dst changed the sign. 1061 static std::pair<ScalarExprEmitter::ImplicitConversionCheckKind, 1062 std::pair<llvm::Value *, SanitizerMask>> 1063 EmitIntegerSignChangeCheckHelper(Value *Src, QualType SrcType, Value *Dst, 1064 QualType DstType, CGBuilderTy &Builder) { 1065 llvm::Type *SrcTy = Src->getType(); 1066 llvm::Type *DstTy = Dst->getType(); 1067 1068 assert(isa<llvm::IntegerType>(SrcTy) && isa<llvm::IntegerType>(DstTy) && 1069 "non-integer llvm type"); 1070 1071 bool SrcSigned = SrcType->isSignedIntegerOrEnumerationType(); 1072 bool DstSigned = DstType->isSignedIntegerOrEnumerationType(); 1073 (void)SrcSigned; // Only used in assert() 1074 (void)DstSigned; // Only used in assert() 1075 unsigned SrcBits = SrcTy->getScalarSizeInBits(); 1076 unsigned DstBits = DstTy->getScalarSizeInBits(); 1077 (void)SrcBits; // Only used in assert() 1078 (void)DstBits; // Only used in assert() 1079 1080 assert(((SrcBits != DstBits) || (SrcSigned != DstSigned)) && 1081 "either the widths should be different, or the signednesses."); 1082 1083 // NOTE: zero value is considered to be non-negative. 1084 auto EmitIsNegativeTest = [&Builder](Value *V, QualType VType, 1085 const char *Name) -> Value * { 1086 // Is this value a signed type? 1087 bool VSigned = VType->isSignedIntegerOrEnumerationType(); 1088 llvm::Type *VTy = V->getType(); 1089 if (!VSigned) { 1090 // If the value is unsigned, then it is never negative. 1091 // FIXME: can we encounter non-scalar VTy here? 1092 return llvm::ConstantInt::getFalse(VTy->getContext()); 1093 } 1094 // Get the zero of the same type with which we will be comparing. 1095 llvm::Constant *Zero = llvm::ConstantInt::get(VTy, 0); 1096 // %V.isnegative = icmp slt %V, 0 1097 // I.e is %V *strictly* less than zero, does it have negative value? 1098 return Builder.CreateICmp(llvm::ICmpInst::ICMP_SLT, V, Zero, 1099 llvm::Twine(Name) + "." + V->getName() + 1100 ".negativitycheck"); 1101 }; 1102 1103 // 1. Was the old Value negative? 1104 llvm::Value *SrcIsNegative = EmitIsNegativeTest(Src, SrcType, "src"); 1105 // 2. Is the new Value negative? 1106 llvm::Value *DstIsNegative = EmitIsNegativeTest(Dst, DstType, "dst"); 1107 // 3. Now, was the 'negativity status' preserved during the conversion? 1108 // NOTE: conversion from negative to zero is considered to change the sign. 1109 // (We want to get 'false' when the conversion changed the sign) 1110 // So we should just equality-compare the negativity statuses. 1111 llvm::Value *Check = nullptr; 1112 Check = Builder.CreateICmpEQ(SrcIsNegative, DstIsNegative, "signchangecheck"); 1113 // If the comparison result is 'false', then the conversion changed the sign. 1114 return std::make_pair( 1115 ScalarExprEmitter::ICCK_IntegerSignChange, 1116 std::make_pair(Check, SanitizerKind::ImplicitIntegerSignChange)); 1117 } 1118 1119 void ScalarExprEmitter::EmitIntegerSignChangeCheck(Value *Src, QualType SrcType, 1120 Value *Dst, QualType DstType, 1121 SourceLocation Loc) { 1122 if (!CGF.SanOpts.has(SanitizerKind::ImplicitIntegerSignChange)) 1123 return; 1124 1125 llvm::Type *SrcTy = Src->getType(); 1126 llvm::Type *DstTy = Dst->getType(); 1127 1128 // We only care about int->int conversions here. 1129 // We ignore conversions to/from pointer and/or bool. 1130 if (!PromotionIsPotentiallyEligibleForImplicitIntegerConversionCheck(SrcType, 1131 DstType)) 1132 return; 1133 1134 bool SrcSigned = SrcType->isSignedIntegerOrEnumerationType(); 1135 bool DstSigned = DstType->isSignedIntegerOrEnumerationType(); 1136 unsigned SrcBits = SrcTy->getScalarSizeInBits(); 1137 unsigned DstBits = DstTy->getScalarSizeInBits(); 1138 1139 // Now, we do not need to emit the check in *all* of the cases. 1140 // We can avoid emitting it in some obvious cases where it would have been 1141 // dropped by the opt passes (instcombine) always anyways. 1142 // If it's a cast between effectively the same type, no check. 1143 // NOTE: this is *not* equivalent to checking the canonical types. 1144 if (SrcSigned == DstSigned && SrcBits == DstBits) 1145 return; 1146 // At least one of the values needs to have signed type. 1147 // If both are unsigned, then obviously, neither of them can be negative. 1148 if (!SrcSigned && !DstSigned) 1149 return; 1150 // If the conversion is to *larger* *signed* type, then no check is needed. 1151 // Because either sign-extension happens (so the sign will remain), 1152 // or zero-extension will happen (the sign bit will be zero.) 1153 if ((DstBits > SrcBits) && DstSigned) 1154 return; 1155 if (CGF.SanOpts.has(SanitizerKind::ImplicitSignedIntegerTruncation) && 1156 (SrcBits > DstBits) && SrcSigned) { 1157 // If the signed integer truncation sanitizer is enabled, 1158 // and this is a truncation from signed type, then no check is needed. 1159 // Because here sign change check is interchangeable with truncation check. 1160 return; 1161 } 1162 // That's it. We can't rule out any more cases with the data we have. 1163 1164 CodeGenFunction::SanitizerScope SanScope(&CGF); 1165 1166 std::pair<ScalarExprEmitter::ImplicitConversionCheckKind, 1167 std::pair<llvm::Value *, SanitizerMask>> 1168 Check; 1169 1170 // Each of these checks needs to return 'false' when an issue was detected. 1171 ImplicitConversionCheckKind CheckKind; 1172 llvm::SmallVector<std::pair<llvm::Value *, SanitizerMask>, 2> Checks; 1173 // So we can 'and' all the checks together, and still get 'false', 1174 // if at least one of the checks detected an issue. 1175 1176 Check = EmitIntegerSignChangeCheckHelper(Src, SrcType, Dst, DstType, Builder); 1177 CheckKind = Check.first; 1178 Checks.emplace_back(Check.second); 1179 1180 if (CGF.SanOpts.has(SanitizerKind::ImplicitSignedIntegerTruncation) && 1181 (SrcBits > DstBits) && !SrcSigned && DstSigned) { 1182 // If the signed integer truncation sanitizer was enabled, 1183 // and we are truncating from larger unsigned type to smaller signed type, 1184 // let's handle the case we skipped in that check. 1185 Check = 1186 EmitIntegerTruncationCheckHelper(Src, SrcType, Dst, DstType, Builder); 1187 CheckKind = ICCK_SignedIntegerTruncationOrSignChange; 1188 Checks.emplace_back(Check.second); 1189 // If the comparison result is 'i1 false', then the truncation was lossy. 1190 } 1191 1192 llvm::Constant *StaticArgs[] = { 1193 CGF.EmitCheckSourceLocation(Loc), CGF.EmitCheckTypeDescriptor(SrcType), 1194 CGF.EmitCheckTypeDescriptor(DstType), 1195 llvm::ConstantInt::get(Builder.getInt8Ty(), CheckKind)}; 1196 // EmitCheck() will 'and' all the checks together. 1197 CGF.EmitCheck(Checks, SanitizerHandler::ImplicitConversion, StaticArgs, 1198 {Src, Dst}); 1199 } 1200 1201 /// Emit a conversion from the specified type to the specified destination type, 1202 /// both of which are LLVM scalar types. 1203 Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, 1204 QualType DstType, 1205 SourceLocation Loc, 1206 ScalarConversionOpts Opts) { 1207 // All conversions involving fixed point types should be handled by the 1208 // EmitFixedPoint family functions. This is done to prevent bloating up this 1209 // function more, and although fixed point numbers are represented by 1210 // integers, we do not want to follow any logic that assumes they should be 1211 // treated as integers. 1212 // TODO(leonardchan): When necessary, add another if statement checking for 1213 // conversions to fixed point types from other types. 1214 if (SrcType->isFixedPointType()) { 1215 if (DstType->isBooleanType()) 1216 // It is important that we check this before checking if the dest type is 1217 // an integer because booleans are technically integer types. 1218 // We do not need to check the padding bit on unsigned types if unsigned 1219 // padding is enabled because overflow into this bit is undefined 1220 // behavior. 1221 return Builder.CreateIsNotNull(Src, "tobool"); 1222 if (DstType->isFixedPointType() || DstType->isIntegerType()) 1223 return EmitFixedPointConversion(Src, SrcType, DstType, Loc); 1224 1225 llvm_unreachable( 1226 "Unhandled scalar conversion from a fixed point type to another type."); 1227 } else if (DstType->isFixedPointType()) { 1228 if (SrcType->isIntegerType()) 1229 // This also includes converting booleans and enums to fixed point types. 1230 return EmitFixedPointConversion(Src, SrcType, DstType, Loc); 1231 1232 llvm_unreachable( 1233 "Unhandled scalar conversion to a fixed point type from another type."); 1234 } 1235 1236 QualType NoncanonicalSrcType = SrcType; 1237 QualType NoncanonicalDstType = DstType; 1238 1239 SrcType = CGF.getContext().getCanonicalType(SrcType); 1240 DstType = CGF.getContext().getCanonicalType(DstType); 1241 if (SrcType == DstType) return Src; 1242 1243 if (DstType->isVoidType()) return nullptr; 1244 1245 llvm::Value *OrigSrc = Src; 1246 QualType OrigSrcType = SrcType; 1247 llvm::Type *SrcTy = Src->getType(); 1248 1249 // Handle conversions to bool first, they are special: comparisons against 0. 1250 if (DstType->isBooleanType()) 1251 return EmitConversionToBool(Src, SrcType); 1252 1253 llvm::Type *DstTy = ConvertType(DstType); 1254 1255 // Cast from half through float if half isn't a native type. 1256 if (SrcType->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) { 1257 // Cast to FP using the intrinsic if the half type itself isn't supported. 1258 if (DstTy->isFloatingPointTy()) { 1259 if (CGF.getContext().getTargetInfo().useFP16ConversionIntrinsics()) 1260 return Builder.CreateCall( 1261 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16, DstTy), 1262 Src); 1263 } else { 1264 // Cast to other types through float, using either the intrinsic or FPExt, 1265 // depending on whether the half type itself is supported 1266 // (as opposed to operations on half, available with NativeHalfType). 1267 if (CGF.getContext().getTargetInfo().useFP16ConversionIntrinsics()) { 1268 Src = Builder.CreateCall( 1269 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16, 1270 CGF.CGM.FloatTy), 1271 Src); 1272 } else { 1273 Src = Builder.CreateFPExt(Src, CGF.CGM.FloatTy, "conv"); 1274 } 1275 SrcType = CGF.getContext().FloatTy; 1276 SrcTy = CGF.FloatTy; 1277 } 1278 } 1279 1280 // Ignore conversions like int -> uint. 1281 if (SrcTy == DstTy) { 1282 if (Opts.EmitImplicitIntegerSignChangeChecks) 1283 EmitIntegerSignChangeCheck(Src, NoncanonicalSrcType, Src, 1284 NoncanonicalDstType, Loc); 1285 1286 return Src; 1287 } 1288 1289 // Handle pointer conversions next: pointers can only be converted to/from 1290 // other pointers and integers. Check for pointer types in terms of LLVM, as 1291 // some native types (like Obj-C id) may map to a pointer type. 1292 if (auto DstPT = dyn_cast<llvm::PointerType>(DstTy)) { 1293 // The source value may be an integer, or a pointer. 1294 if (isa<llvm::PointerType>(SrcTy)) 1295 return Builder.CreateBitCast(Src, DstTy, "conv"); 1296 1297 assert(SrcType->isIntegerType() && "Not ptr->ptr or int->ptr conversion?"); 1298 // First, convert to the correct width so that we control the kind of 1299 // extension. 1300 llvm::Type *MiddleTy = CGF.CGM.getDataLayout().getIntPtrType(DstPT); 1301 bool InputSigned = SrcType->isSignedIntegerOrEnumerationType(); 1302 llvm::Value* IntResult = 1303 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); 1304 // Then, cast to pointer. 1305 return Builder.CreateIntToPtr(IntResult, DstTy, "conv"); 1306 } 1307 1308 if (isa<llvm::PointerType>(SrcTy)) { 1309 // Must be an ptr to int cast. 1310 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?"); 1311 return Builder.CreatePtrToInt(Src, DstTy, "conv"); 1312 } 1313 1314 // A scalar can be splatted to an extended vector of the same element type 1315 if (DstType->isExtVectorType() && !SrcType->isVectorType()) { 1316 // Sema should add casts to make sure that the source expression's type is 1317 // the same as the vector's element type (sans qualifiers) 1318 assert(DstType->castAs<ExtVectorType>()->getElementType().getTypePtr() == 1319 SrcType.getTypePtr() && 1320 "Splatted expr doesn't match with vector element type?"); 1321 1322 // Splat the element across to all elements 1323 unsigned NumElements = cast<llvm::FixedVectorType>(DstTy)->getNumElements(); 1324 return Builder.CreateVectorSplat(NumElements, Src, "splat"); 1325 } 1326 1327 if (isa<llvm::VectorType>(SrcTy) || isa<llvm::VectorType>(DstTy)) { 1328 // Allow bitcast from vector to integer/fp of the same size. 1329 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); 1330 unsigned DstSize = DstTy->getPrimitiveSizeInBits(); 1331 if (SrcSize == DstSize) 1332 return Builder.CreateBitCast(Src, DstTy, "conv"); 1333 1334 // Conversions between vectors of different sizes are not allowed except 1335 // when vectors of half are involved. Operations on storage-only half 1336 // vectors require promoting half vector operands to float vectors and 1337 // truncating the result, which is either an int or float vector, to a 1338 // short or half vector. 1339 1340 // Source and destination are both expected to be vectors. 1341 llvm::Type *SrcElementTy = cast<llvm::VectorType>(SrcTy)->getElementType(); 1342 llvm::Type *DstElementTy = cast<llvm::VectorType>(DstTy)->getElementType(); 1343 (void)DstElementTy; 1344 1345 assert(((SrcElementTy->isIntegerTy() && 1346 DstElementTy->isIntegerTy()) || 1347 (SrcElementTy->isFloatingPointTy() && 1348 DstElementTy->isFloatingPointTy())) && 1349 "unexpected conversion between a floating-point vector and an " 1350 "integer vector"); 1351 1352 // Truncate an i32 vector to an i16 vector. 1353 if (SrcElementTy->isIntegerTy()) 1354 return Builder.CreateIntCast(Src, DstTy, false, "conv"); 1355 1356 // Truncate a float vector to a half vector. 1357 if (SrcSize > DstSize) 1358 return Builder.CreateFPTrunc(Src, DstTy, "conv"); 1359 1360 // Promote a half vector to a float vector. 1361 return Builder.CreateFPExt(Src, DstTy, "conv"); 1362 } 1363 1364 // Finally, we have the arithmetic types: real int/float. 1365 Value *Res = nullptr; 1366 llvm::Type *ResTy = DstTy; 1367 1368 // An overflowing conversion has undefined behavior if either the source type 1369 // or the destination type is a floating-point type. However, we consider the 1370 // range of representable values for all floating-point types to be 1371 // [-inf,+inf], so no overflow can ever happen when the destination type is a 1372 // floating-point type. 1373 if (CGF.SanOpts.has(SanitizerKind::FloatCastOverflow) && 1374 OrigSrcType->isFloatingType()) 1375 EmitFloatConversionCheck(OrigSrc, OrigSrcType, Src, SrcType, DstType, DstTy, 1376 Loc); 1377 1378 // Cast to half through float if half isn't a native type. 1379 if (DstType->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) { 1380 // Make sure we cast in a single step if from another FP type. 1381 if (SrcTy->isFloatingPointTy()) { 1382 // Use the intrinsic if the half type itself isn't supported 1383 // (as opposed to operations on half, available with NativeHalfType). 1384 if (CGF.getContext().getTargetInfo().useFP16ConversionIntrinsics()) 1385 return Builder.CreateCall( 1386 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16, SrcTy), Src); 1387 // If the half type is supported, just use an fptrunc. 1388 return Builder.CreateFPTrunc(Src, DstTy); 1389 } 1390 DstTy = CGF.FloatTy; 1391 } 1392 1393 if (isa<llvm::IntegerType>(SrcTy)) { 1394 bool InputSigned = SrcType->isSignedIntegerOrEnumerationType(); 1395 if (SrcType->isBooleanType() && Opts.TreatBooleanAsSigned) { 1396 InputSigned = true; 1397 } 1398 if (isa<llvm::IntegerType>(DstTy)) 1399 Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); 1400 else if (InputSigned) 1401 Res = Builder.CreateSIToFP(Src, DstTy, "conv"); 1402 else 1403 Res = Builder.CreateUIToFP(Src, DstTy, "conv"); 1404 } else if (isa<llvm::IntegerType>(DstTy)) { 1405 assert(SrcTy->isFloatingPointTy() && "Unknown real conversion"); 1406 if (DstType->isSignedIntegerOrEnumerationType()) 1407 Res = Builder.CreateFPToSI(Src, DstTy, "conv"); 1408 else 1409 Res = Builder.CreateFPToUI(Src, DstTy, "conv"); 1410 } else { 1411 assert(SrcTy->isFloatingPointTy() && DstTy->isFloatingPointTy() && 1412 "Unknown real conversion"); 1413 if (DstTy->getTypeID() < SrcTy->getTypeID()) 1414 Res = Builder.CreateFPTrunc(Src, DstTy, "conv"); 1415 else 1416 Res = Builder.CreateFPExt(Src, DstTy, "conv"); 1417 } 1418 1419 if (DstTy != ResTy) { 1420 if (CGF.getContext().getTargetInfo().useFP16ConversionIntrinsics()) { 1421 assert(ResTy->isIntegerTy(16) && "Only half FP requires extra conversion"); 1422 Res = Builder.CreateCall( 1423 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16, CGF.CGM.FloatTy), 1424 Res); 1425 } else { 1426 Res = Builder.CreateFPTrunc(Res, ResTy, "conv"); 1427 } 1428 } 1429 1430 if (Opts.EmitImplicitIntegerTruncationChecks) 1431 EmitIntegerTruncationCheck(Src, NoncanonicalSrcType, Res, 1432 NoncanonicalDstType, Loc); 1433 1434 if (Opts.EmitImplicitIntegerSignChangeChecks) 1435 EmitIntegerSignChangeCheck(Src, NoncanonicalSrcType, Res, 1436 NoncanonicalDstType, Loc); 1437 1438 return Res; 1439 } 1440 1441 Value *ScalarExprEmitter::EmitFixedPointConversion(Value *Src, QualType SrcTy, 1442 QualType DstTy, 1443 SourceLocation Loc) { 1444 auto SrcFPSema = CGF.getContext().getFixedPointSemantics(SrcTy); 1445 auto DstFPSema = CGF.getContext().getFixedPointSemantics(DstTy); 1446 llvm::FixedPointBuilder<CGBuilderTy> FPBuilder(Builder); 1447 llvm::Value *Result; 1448 if (DstTy->isIntegerType()) 1449 Result = FPBuilder.CreateFixedToInteger(Src, SrcFPSema, 1450 DstFPSema.getWidth(), 1451 DstFPSema.isSigned()); 1452 else if (SrcTy->isIntegerType()) 1453 Result = FPBuilder.CreateIntegerToFixed(Src, SrcFPSema.isSigned(), 1454 DstFPSema); 1455 else 1456 Result = FPBuilder.CreateFixedToFixed(Src, SrcFPSema, DstFPSema); 1457 return Result; 1458 } 1459 1460 /// Emit a conversion from the specified complex type to the specified 1461 /// destination type, where the destination type is an LLVM scalar type. 1462 Value *ScalarExprEmitter::EmitComplexToScalarConversion( 1463 CodeGenFunction::ComplexPairTy Src, QualType SrcTy, QualType DstTy, 1464 SourceLocation Loc) { 1465 // Get the source element type. 1466 SrcTy = SrcTy->castAs<ComplexType>()->getElementType(); 1467 1468 // Handle conversions to bool first, they are special: comparisons against 0. 1469 if (DstTy->isBooleanType()) { 1470 // Complex != 0 -> (Real != 0) | (Imag != 0) 1471 Src.first = EmitScalarConversion(Src.first, SrcTy, DstTy, Loc); 1472 Src.second = EmitScalarConversion(Src.second, SrcTy, DstTy, Loc); 1473 return Builder.CreateOr(Src.first, Src.second, "tobool"); 1474 } 1475 1476 // C99 6.3.1.7p2: "When a value of complex type is converted to a real type, 1477 // the imaginary part of the complex value is discarded and the value of the 1478 // real part is converted according to the conversion rules for the 1479 // corresponding real type. 1480 return EmitScalarConversion(Src.first, SrcTy, DstTy, Loc); 1481 } 1482 1483 Value *ScalarExprEmitter::EmitNullValue(QualType Ty) { 1484 return CGF.EmitFromMemory(CGF.CGM.EmitNullConstant(Ty), Ty); 1485 } 1486 1487 /// Emit a sanitization check for the given "binary" operation (which 1488 /// might actually be a unary increment which has been lowered to a binary 1489 /// operation). The check passes if all values in \p Checks (which are \c i1), 1490 /// are \c true. 1491 void ScalarExprEmitter::EmitBinOpCheck( 1492 ArrayRef<std::pair<Value *, SanitizerMask>> Checks, const BinOpInfo &Info) { 1493 assert(CGF.IsSanitizerScope); 1494 SanitizerHandler Check; 1495 SmallVector<llvm::Constant *, 4> StaticData; 1496 SmallVector<llvm::Value *, 2> DynamicData; 1497 1498 BinaryOperatorKind Opcode = Info.Opcode; 1499 if (BinaryOperator::isCompoundAssignmentOp(Opcode)) 1500 Opcode = BinaryOperator::getOpForCompoundAssignment(Opcode); 1501 1502 StaticData.push_back(CGF.EmitCheckSourceLocation(Info.E->getExprLoc())); 1503 const UnaryOperator *UO = dyn_cast<UnaryOperator>(Info.E); 1504 if (UO && UO->getOpcode() == UO_Minus) { 1505 Check = SanitizerHandler::NegateOverflow; 1506 StaticData.push_back(CGF.EmitCheckTypeDescriptor(UO->getType())); 1507 DynamicData.push_back(Info.RHS); 1508 } else { 1509 if (BinaryOperator::isShiftOp(Opcode)) { 1510 // Shift LHS negative or too large, or RHS out of bounds. 1511 Check = SanitizerHandler::ShiftOutOfBounds; 1512 const BinaryOperator *BO = cast<BinaryOperator>(Info.E); 1513 StaticData.push_back( 1514 CGF.EmitCheckTypeDescriptor(BO->getLHS()->getType())); 1515 StaticData.push_back( 1516 CGF.EmitCheckTypeDescriptor(BO->getRHS()->getType())); 1517 } else if (Opcode == BO_Div || Opcode == BO_Rem) { 1518 // Divide or modulo by zero, or signed overflow (eg INT_MAX / -1). 1519 Check = SanitizerHandler::DivremOverflow; 1520 StaticData.push_back(CGF.EmitCheckTypeDescriptor(Info.Ty)); 1521 } else { 1522 // Arithmetic overflow (+, -, *). 1523 switch (Opcode) { 1524 case BO_Add: Check = SanitizerHandler::AddOverflow; break; 1525 case BO_Sub: Check = SanitizerHandler::SubOverflow; break; 1526 case BO_Mul: Check = SanitizerHandler::MulOverflow; break; 1527 default: llvm_unreachable("unexpected opcode for bin op check"); 1528 } 1529 StaticData.push_back(CGF.EmitCheckTypeDescriptor(Info.Ty)); 1530 } 1531 DynamicData.push_back(Info.LHS); 1532 DynamicData.push_back(Info.RHS); 1533 } 1534 1535 CGF.EmitCheck(Checks, Check, StaticData, DynamicData); 1536 } 1537 1538 //===----------------------------------------------------------------------===// 1539 // Visitor Methods 1540 //===----------------------------------------------------------------------===// 1541 1542 Value *ScalarExprEmitter::VisitExpr(Expr *E) { 1543 CGF.ErrorUnsupported(E, "scalar expression"); 1544 if (E->getType()->isVoidType()) 1545 return nullptr; 1546 return llvm::UndefValue::get(CGF.ConvertType(E->getType())); 1547 } 1548 1549 Value *ScalarExprEmitter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { 1550 // Vector Mask Case 1551 if (E->getNumSubExprs() == 2) { 1552 Value *LHS = CGF.EmitScalarExpr(E->getExpr(0)); 1553 Value *RHS = CGF.EmitScalarExpr(E->getExpr(1)); 1554 Value *Mask; 1555 1556 auto *LTy = cast<llvm::FixedVectorType>(LHS->getType()); 1557 unsigned LHSElts = LTy->getNumElements(); 1558 1559 Mask = RHS; 1560 1561 auto *MTy = cast<llvm::FixedVectorType>(Mask->getType()); 1562 1563 // Mask off the high bits of each shuffle index. 1564 Value *MaskBits = 1565 llvm::ConstantInt::get(MTy, llvm::NextPowerOf2(LHSElts - 1) - 1); 1566 Mask = Builder.CreateAnd(Mask, MaskBits, "mask"); 1567 1568 // newv = undef 1569 // mask = mask & maskbits 1570 // for each elt 1571 // n = extract mask i 1572 // x = extract val n 1573 // newv = insert newv, x, i 1574 auto *RTy = llvm::FixedVectorType::get(LTy->getElementType(), 1575 MTy->getNumElements()); 1576 Value* NewV = llvm::UndefValue::get(RTy); 1577 for (unsigned i = 0, e = MTy->getNumElements(); i != e; ++i) { 1578 Value *IIndx = llvm::ConstantInt::get(CGF.SizeTy, i); 1579 Value *Indx = Builder.CreateExtractElement(Mask, IIndx, "shuf_idx"); 1580 1581 Value *VExt = Builder.CreateExtractElement(LHS, Indx, "shuf_elt"); 1582 NewV = Builder.CreateInsertElement(NewV, VExt, IIndx, "shuf_ins"); 1583 } 1584 return NewV; 1585 } 1586 1587 Value* V1 = CGF.EmitScalarExpr(E->getExpr(0)); 1588 Value* V2 = CGF.EmitScalarExpr(E->getExpr(1)); 1589 1590 SmallVector<int, 32> Indices; 1591 for (unsigned i = 2; i < E->getNumSubExprs(); ++i) { 1592 llvm::APSInt Idx = E->getShuffleMaskIdx(CGF.getContext(), i-2); 1593 // Check for -1 and output it as undef in the IR. 1594 if (Idx.isSigned() && Idx.isAllOnesValue()) 1595 Indices.push_back(-1); 1596 else 1597 Indices.push_back(Idx.getZExtValue()); 1598 } 1599 1600 return Builder.CreateShuffleVector(V1, V2, Indices, "shuffle"); 1601 } 1602 1603 Value *ScalarExprEmitter::VisitConvertVectorExpr(ConvertVectorExpr *E) { 1604 QualType SrcType = E->getSrcExpr()->getType(), 1605 DstType = E->getType(); 1606 1607 Value *Src = CGF.EmitScalarExpr(E->getSrcExpr()); 1608 1609 SrcType = CGF.getContext().getCanonicalType(SrcType); 1610 DstType = CGF.getContext().getCanonicalType(DstType); 1611 if (SrcType == DstType) return Src; 1612 1613 assert(SrcType->isVectorType() && 1614 "ConvertVector source type must be a vector"); 1615 assert(DstType->isVectorType() && 1616 "ConvertVector destination type must be a vector"); 1617 1618 llvm::Type *SrcTy = Src->getType(); 1619 llvm::Type *DstTy = ConvertType(DstType); 1620 1621 // Ignore conversions like int -> uint. 1622 if (SrcTy == DstTy) 1623 return Src; 1624 1625 QualType SrcEltType = SrcType->castAs<VectorType>()->getElementType(), 1626 DstEltType = DstType->castAs<VectorType>()->getElementType(); 1627 1628 assert(SrcTy->isVectorTy() && 1629 "ConvertVector source IR type must be a vector"); 1630 assert(DstTy->isVectorTy() && 1631 "ConvertVector destination IR type must be a vector"); 1632 1633 llvm::Type *SrcEltTy = cast<llvm::VectorType>(SrcTy)->getElementType(), 1634 *DstEltTy = cast<llvm::VectorType>(DstTy)->getElementType(); 1635 1636 if (DstEltType->isBooleanType()) { 1637 assert((SrcEltTy->isFloatingPointTy() || 1638 isa<llvm::IntegerType>(SrcEltTy)) && "Unknown boolean conversion"); 1639 1640 llvm::Value *Zero = llvm::Constant::getNullValue(SrcTy); 1641 if (SrcEltTy->isFloatingPointTy()) { 1642 return Builder.CreateFCmpUNE(Src, Zero, "tobool"); 1643 } else { 1644 return Builder.CreateICmpNE(Src, Zero, "tobool"); 1645 } 1646 } 1647 1648 // We have the arithmetic types: real int/float. 1649 Value *Res = nullptr; 1650 1651 if (isa<llvm::IntegerType>(SrcEltTy)) { 1652 bool InputSigned = SrcEltType->isSignedIntegerOrEnumerationType(); 1653 if (isa<llvm::IntegerType>(DstEltTy)) 1654 Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); 1655 else if (InputSigned) 1656 Res = Builder.CreateSIToFP(Src, DstTy, "conv"); 1657 else 1658 Res = Builder.CreateUIToFP(Src, DstTy, "conv"); 1659 } else if (isa<llvm::IntegerType>(DstEltTy)) { 1660 assert(SrcEltTy->isFloatingPointTy() && "Unknown real conversion"); 1661 if (DstEltType->isSignedIntegerOrEnumerationType()) 1662 Res = Builder.CreateFPToSI(Src, DstTy, "conv"); 1663 else 1664 Res = Builder.CreateFPToUI(Src, DstTy, "conv"); 1665 } else { 1666 assert(SrcEltTy->isFloatingPointTy() && DstEltTy->isFloatingPointTy() && 1667 "Unknown real conversion"); 1668 if (DstEltTy->getTypeID() < SrcEltTy->getTypeID()) 1669 Res = Builder.CreateFPTrunc(Src, DstTy, "conv"); 1670 else 1671 Res = Builder.CreateFPExt(Src, DstTy, "conv"); 1672 } 1673 1674 return Res; 1675 } 1676 1677 Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) { 1678 if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E)) { 1679 CGF.EmitIgnoredExpr(E->getBase()); 1680 return CGF.emitScalarConstant(Constant, E); 1681 } else { 1682 Expr::EvalResult Result; 1683 if (E->EvaluateAsInt(Result, CGF.getContext(), Expr::SE_AllowSideEffects)) { 1684 llvm::APSInt Value = Result.Val.getInt(); 1685 CGF.EmitIgnoredExpr(E->getBase()); 1686 return Builder.getInt(Value); 1687 } 1688 } 1689 1690 return EmitLoadOfLValue(E); 1691 } 1692 1693 Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { 1694 TestAndClearIgnoreResultAssign(); 1695 1696 // Emit subscript expressions in rvalue context's. For most cases, this just 1697 // loads the lvalue formed by the subscript expr. However, we have to be 1698 // careful, because the base of a vector subscript is occasionally an rvalue, 1699 // so we can't get it as an lvalue. 1700 if (!E->getBase()->getType()->isVectorType()) 1701 return EmitLoadOfLValue(E); 1702 1703 // Handle the vector case. The base must be a vector, the index must be an 1704 // integer value. 1705 Value *Base = Visit(E->getBase()); 1706 Value *Idx = Visit(E->getIdx()); 1707 QualType IdxTy = E->getIdx()->getType(); 1708 1709 if (CGF.SanOpts.has(SanitizerKind::ArrayBounds)) 1710 CGF.EmitBoundsCheck(E, E->getBase(), Idx, IdxTy, /*Accessed*/true); 1711 1712 return Builder.CreateExtractElement(Base, Idx, "vecext"); 1713 } 1714 1715 Value *ScalarExprEmitter::VisitMatrixSubscriptExpr(MatrixSubscriptExpr *E) { 1716 TestAndClearIgnoreResultAssign(); 1717 1718 // Handle the vector case. The base must be a vector, the index must be an 1719 // integer value. 1720 Value *RowIdx = Visit(E->getRowIdx()); 1721 Value *ColumnIdx = Visit(E->getColumnIdx()); 1722 Value *Matrix = Visit(E->getBase()); 1723 1724 // TODO: Should we emit bounds checks with SanitizerKind::ArrayBounds? 1725 llvm::MatrixBuilder<CGBuilderTy> MB(Builder); 1726 return MB.CreateExtractElement( 1727 Matrix, RowIdx, ColumnIdx, 1728 E->getBase()->getType()->getAs<ConstantMatrixType>()->getNumRows()); 1729 } 1730 1731 static int getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx, 1732 unsigned Off) { 1733 int MV = SVI->getMaskValue(Idx); 1734 if (MV == -1) 1735 return -1; 1736 return Off + MV; 1737 } 1738 1739 static int getAsInt32(llvm::ConstantInt *C, llvm::Type *I32Ty) { 1740 assert(llvm::ConstantInt::isValueValidForType(I32Ty, C->getZExtValue()) && 1741 "Index operand too large for shufflevector mask!"); 1742 return C->getZExtValue(); 1743 } 1744 1745 Value *ScalarExprEmitter::VisitInitListExpr(InitListExpr *E) { 1746 bool Ignore = TestAndClearIgnoreResultAssign(); 1747 (void)Ignore; 1748 assert (Ignore == false && "init list ignored"); 1749 unsigned NumInitElements = E->getNumInits(); 1750 1751 if (E->hadArrayRangeDesignator()) 1752 CGF.ErrorUnsupported(E, "GNU array range designator extension"); 1753 1754 llvm::VectorType *VType = 1755 dyn_cast<llvm::VectorType>(ConvertType(E->getType())); 1756 1757 if (!VType) { 1758 if (NumInitElements == 0) { 1759 // C++11 value-initialization for the scalar. 1760 return EmitNullValue(E->getType()); 1761 } 1762 // We have a scalar in braces. Just use the first element. 1763 return Visit(E->getInit(0)); 1764 } 1765 1766 unsigned ResElts = cast<llvm::FixedVectorType>(VType)->getNumElements(); 1767 1768 // Loop over initializers collecting the Value for each, and remembering 1769 // whether the source was swizzle (ExtVectorElementExpr). This will allow 1770 // us to fold the shuffle for the swizzle into the shuffle for the vector 1771 // initializer, since LLVM optimizers generally do not want to touch 1772 // shuffles. 1773 unsigned CurIdx = 0; 1774 bool VIsUndefShuffle = false; 1775 llvm::Value *V = llvm::UndefValue::get(VType); 1776 for (unsigned i = 0; i != NumInitElements; ++i) { 1777 Expr *IE = E->getInit(i); 1778 Value *Init = Visit(IE); 1779 SmallVector<int, 16> Args; 1780 1781 llvm::VectorType *VVT = dyn_cast<llvm::VectorType>(Init->getType()); 1782 1783 // Handle scalar elements. If the scalar initializer is actually one 1784 // element of a different vector of the same width, use shuffle instead of 1785 // extract+insert. 1786 if (!VVT) { 1787 if (isa<ExtVectorElementExpr>(IE)) { 1788 llvm::ExtractElementInst *EI = cast<llvm::ExtractElementInst>(Init); 1789 1790 if (cast<llvm::FixedVectorType>(EI->getVectorOperandType()) 1791 ->getNumElements() == ResElts) { 1792 llvm::ConstantInt *C = cast<llvm::ConstantInt>(EI->getIndexOperand()); 1793 Value *LHS = nullptr, *RHS = nullptr; 1794 if (CurIdx == 0) { 1795 // insert into undef -> shuffle (src, undef) 1796 // shufflemask must use an i32 1797 Args.push_back(getAsInt32(C, CGF.Int32Ty)); 1798 Args.resize(ResElts, -1); 1799 1800 LHS = EI->getVectorOperand(); 1801 RHS = V; 1802 VIsUndefShuffle = true; 1803 } else if (VIsUndefShuffle) { 1804 // insert into undefshuffle && size match -> shuffle (v, src) 1805 llvm::ShuffleVectorInst *SVV = cast<llvm::ShuffleVectorInst>(V); 1806 for (unsigned j = 0; j != CurIdx; ++j) 1807 Args.push_back(getMaskElt(SVV, j, 0)); 1808 Args.push_back(ResElts + C->getZExtValue()); 1809 Args.resize(ResElts, -1); 1810 1811 LHS = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); 1812 RHS = EI->getVectorOperand(); 1813 VIsUndefShuffle = false; 1814 } 1815 if (!Args.empty()) { 1816 V = Builder.CreateShuffleVector(LHS, RHS, Args); 1817 ++CurIdx; 1818 continue; 1819 } 1820 } 1821 } 1822 V = Builder.CreateInsertElement(V, Init, Builder.getInt32(CurIdx), 1823 "vecinit"); 1824 VIsUndefShuffle = false; 1825 ++CurIdx; 1826 continue; 1827 } 1828 1829 unsigned InitElts = cast<llvm::FixedVectorType>(VVT)->getNumElements(); 1830 1831 // If the initializer is an ExtVecEltExpr (a swizzle), and the swizzle's 1832 // input is the same width as the vector being constructed, generate an 1833 // optimized shuffle of the swizzle input into the result. 1834 unsigned Offset = (CurIdx == 0) ? 0 : ResElts; 1835 if (isa<ExtVectorElementExpr>(IE)) { 1836 llvm::ShuffleVectorInst *SVI = cast<llvm::ShuffleVectorInst>(Init); 1837 Value *SVOp = SVI->getOperand(0); 1838 auto *OpTy = cast<llvm::FixedVectorType>(SVOp->getType()); 1839 1840 if (OpTy->getNumElements() == ResElts) { 1841 for (unsigned j = 0; j != CurIdx; ++j) { 1842 // If the current vector initializer is a shuffle with undef, merge 1843 // this shuffle directly into it. 1844 if (VIsUndefShuffle) { 1845 Args.push_back(getMaskElt(cast<llvm::ShuffleVectorInst>(V), j, 0)); 1846 } else { 1847 Args.push_back(j); 1848 } 1849 } 1850 for (unsigned j = 0, je = InitElts; j != je; ++j) 1851 Args.push_back(getMaskElt(SVI, j, Offset)); 1852 Args.resize(ResElts, -1); 1853 1854 if (VIsUndefShuffle) 1855 V = cast<llvm::ShuffleVectorInst>(V)->getOperand(0); 1856 1857 Init = SVOp; 1858 } 1859 } 1860 1861 // Extend init to result vector length, and then shuffle its contribution 1862 // to the vector initializer into V. 1863 if (Args.empty()) { 1864 for (unsigned j = 0; j != InitElts; ++j) 1865 Args.push_back(j); 1866 Args.resize(ResElts, -1); 1867 Init = Builder.CreateShuffleVector(Init, llvm::UndefValue::get(VVT), Args, 1868 "vext"); 1869 1870 Args.clear(); 1871 for (unsigned j = 0; j != CurIdx; ++j) 1872 Args.push_back(j); 1873 for (unsigned j = 0; j != InitElts; ++j) 1874 Args.push_back(j + Offset); 1875 Args.resize(ResElts, -1); 1876 } 1877 1878 // If V is undef, make sure it ends up on the RHS of the shuffle to aid 1879 // merging subsequent shuffles into this one. 1880 if (CurIdx == 0) 1881 std::swap(V, Init); 1882 V = Builder.CreateShuffleVector(V, Init, Args, "vecinit"); 1883 VIsUndefShuffle = isa<llvm::UndefValue>(Init); 1884 CurIdx += InitElts; 1885 } 1886 1887 // FIXME: evaluate codegen vs. shuffling against constant null vector. 1888 // Emit remaining default initializers. 1889 llvm::Type *EltTy = VType->getElementType(); 1890 1891 // Emit remaining default initializers 1892 for (/* Do not initialize i*/; CurIdx < ResElts; ++CurIdx) { 1893 Value *Idx = Builder.getInt32(CurIdx); 1894 llvm::Value *Init = llvm::Constant::getNullValue(EltTy); 1895 V = Builder.CreateInsertElement(V, Init, Idx, "vecinit"); 1896 } 1897 return V; 1898 } 1899 1900 bool CodeGenFunction::ShouldNullCheckClassCastValue(const CastExpr *CE) { 1901 const Expr *E = CE->getSubExpr(); 1902 1903 if (CE->getCastKind() == CK_UncheckedDerivedToBase) 1904 return false; 1905 1906 if (isa<CXXThisExpr>(E->IgnoreParens())) { 1907 // We always assume that 'this' is never null. 1908 return false; 1909 } 1910 1911 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(CE)) { 1912 // And that glvalue casts are never null. 1913 if (ICE->getValueKind() != VK_RValue) 1914 return false; 1915 } 1916 1917 return true; 1918 } 1919 1920 // VisitCastExpr - Emit code for an explicit or implicit cast. Implicit casts 1921 // have to handle a more broad range of conversions than explicit casts, as they 1922 // handle things like function to ptr-to-function decay etc. 1923 Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { 1924 Expr *E = CE->getSubExpr(); 1925 QualType DestTy = CE->getType(); 1926 CastKind Kind = CE->getCastKind(); 1927 1928 // These cases are generally not written to ignore the result of 1929 // evaluating their sub-expressions, so we clear this now. 1930 bool Ignored = TestAndClearIgnoreResultAssign(); 1931 1932 // Since almost all cast kinds apply to scalars, this switch doesn't have 1933 // a default case, so the compiler will warn on a missing case. The cases 1934 // are in the same order as in the CastKind enum. 1935 switch (Kind) { 1936 case CK_Dependent: llvm_unreachable("dependent cast kind in IR gen!"); 1937 case CK_BuiltinFnToFnPtr: 1938 llvm_unreachable("builtin functions are handled elsewhere"); 1939 1940 case CK_LValueBitCast: 1941 case CK_ObjCObjectLValueCast: { 1942 Address Addr = EmitLValue(E).getAddress(CGF); 1943 Addr = Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(DestTy)); 1944 LValue LV = CGF.MakeAddrLValue(Addr, DestTy); 1945 return EmitLoadOfLValue(LV, CE->getExprLoc()); 1946 } 1947 1948 case CK_LValueToRValueBitCast: { 1949 LValue SourceLVal = CGF.EmitLValue(E); 1950 Address Addr = Builder.CreateElementBitCast(SourceLVal.getAddress(CGF), 1951 CGF.ConvertTypeForMem(DestTy)); 1952 LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy); 1953 DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo()); 1954 return EmitLoadOfLValue(DestLV, CE->getExprLoc()); 1955 } 1956 1957 case CK_CPointerToObjCPointerCast: 1958 case CK_BlockPointerToObjCPointerCast: 1959 case CK_AnyPointerToBlockPointerCast: 1960 case CK_BitCast: { 1961 Value *Src = Visit(const_cast<Expr*>(E)); 1962 llvm::Type *SrcTy = Src->getType(); 1963 llvm::Type *DstTy = ConvertType(DestTy); 1964 if (SrcTy->isPtrOrPtrVectorTy() && DstTy->isPtrOrPtrVectorTy() && 1965 SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) { 1966 llvm_unreachable("wrong cast for pointers in different address spaces" 1967 "(must be an address space cast)!"); 1968 } 1969 1970 if (CGF.SanOpts.has(SanitizerKind::CFIUnrelatedCast)) { 1971 if (auto PT = DestTy->getAs<PointerType>()) 1972 CGF.EmitVTablePtrCheckForCast(PT->getPointeeType(), Src, 1973 /*MayBeNull=*/true, 1974 CodeGenFunction::CFITCK_UnrelatedCast, 1975 CE->getBeginLoc()); 1976 } 1977 1978 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers) { 1979 const QualType SrcType = E->getType(); 1980 1981 if (SrcType.mayBeNotDynamicClass() && DestTy.mayBeDynamicClass()) { 1982 // Casting to pointer that could carry dynamic information (provided by 1983 // invariant.group) requires launder. 1984 Src = Builder.CreateLaunderInvariantGroup(Src); 1985 } else if (SrcType.mayBeDynamicClass() && DestTy.mayBeNotDynamicClass()) { 1986 // Casting to pointer that does not carry dynamic information (provided 1987 // by invariant.group) requires stripping it. Note that we don't do it 1988 // if the source could not be dynamic type and destination could be 1989 // dynamic because dynamic information is already laundered. It is 1990 // because launder(strip(src)) == launder(src), so there is no need to 1991 // add extra strip before launder. 1992 Src = Builder.CreateStripInvariantGroup(Src); 1993 } 1994 } 1995 1996 // Update heapallocsite metadata when there is an explicit pointer cast. 1997 if (auto *CI = dyn_cast<llvm::CallBase>(Src)) { 1998 if (CI->getMetadata("heapallocsite") && isa<ExplicitCastExpr>(CE)) { 1999 QualType PointeeType = DestTy->getPointeeType(); 2000 if (!PointeeType.isNull()) 2001 CGF.getDebugInfo()->addHeapAllocSiteMetadata(CI, PointeeType, 2002 CE->getExprLoc()); 2003 } 2004 } 2005 2006 return Builder.CreateBitCast(Src, DstTy); 2007 } 2008 case CK_AddressSpaceConversion: { 2009 Expr::EvalResult Result; 2010 if (E->EvaluateAsRValue(Result, CGF.getContext()) && 2011 Result.Val.isNullPointer()) { 2012 // If E has side effect, it is emitted even if its final result is a 2013 // null pointer. In that case, a DCE pass should be able to 2014 // eliminate the useless instructions emitted during translating E. 2015 if (Result.HasSideEffects) 2016 Visit(E); 2017 return CGF.CGM.getNullPointer(cast<llvm::PointerType>( 2018 ConvertType(DestTy)), DestTy); 2019 } 2020 // Since target may map different address spaces in AST to the same address 2021 // space, an address space conversion may end up as a bitcast. 2022 return CGF.CGM.getTargetCodeGenInfo().performAddrSpaceCast( 2023 CGF, Visit(E), E->getType()->getPointeeType().getAddressSpace(), 2024 DestTy->getPointeeType().getAddressSpace(), ConvertType(DestTy)); 2025 } 2026 case CK_AtomicToNonAtomic: 2027 case CK_NonAtomicToAtomic: 2028 case CK_NoOp: 2029 case CK_UserDefinedConversion: 2030 return Visit(const_cast<Expr*>(E)); 2031 2032 case CK_BaseToDerived: { 2033 const CXXRecordDecl *DerivedClassDecl = DestTy->getPointeeCXXRecordDecl(); 2034 assert(DerivedClassDecl && "BaseToDerived arg isn't a C++ object pointer!"); 2035 2036 Address Base = CGF.EmitPointerWithAlignment(E); 2037 Address Derived = 2038 CGF.GetAddressOfDerivedClass(Base, DerivedClassDecl, 2039 CE->path_begin(), CE->path_end(), 2040 CGF.ShouldNullCheckClassCastValue(CE)); 2041 2042 // C++11 [expr.static.cast]p11: Behavior is undefined if a downcast is 2043 // performed and the object is not of the derived type. 2044 if (CGF.sanitizePerformTypeCheck()) 2045 CGF.EmitTypeCheck(CodeGenFunction::TCK_DowncastPointer, CE->getExprLoc(), 2046 Derived.getPointer(), DestTy->getPointeeType()); 2047 2048 if (CGF.SanOpts.has(SanitizerKind::CFIDerivedCast)) 2049 CGF.EmitVTablePtrCheckForCast( 2050 DestTy->getPointeeType(), Derived.getPointer(), 2051 /*MayBeNull=*/true, CodeGenFunction::CFITCK_DerivedCast, 2052 CE->getBeginLoc()); 2053 2054 return Derived.getPointer(); 2055 } 2056 case CK_UncheckedDerivedToBase: 2057 case CK_DerivedToBase: { 2058 // The EmitPointerWithAlignment path does this fine; just discard 2059 // the alignment. 2060 return CGF.EmitPointerWithAlignment(CE).getPointer(); 2061 } 2062 2063 case CK_Dynamic: { 2064 Address V = CGF.EmitPointerWithAlignment(E); 2065 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE); 2066 return CGF.EmitDynamicCast(V, DCE); 2067 } 2068 2069 case CK_ArrayToPointerDecay: 2070 return CGF.EmitArrayToPointerDecay(E).getPointer(); 2071 case CK_FunctionToPointerDecay: 2072 return EmitLValue(E).getPointer(CGF); 2073 2074 case CK_NullToPointer: 2075 if (MustVisitNullValue(E)) 2076 CGF.EmitIgnoredExpr(E); 2077 2078 return CGF.CGM.getNullPointer(cast<llvm::PointerType>(ConvertType(DestTy)), 2079 DestTy); 2080 2081 case CK_NullToMemberPointer: { 2082 if (MustVisitNullValue(E)) 2083 CGF.EmitIgnoredExpr(E); 2084 2085 const MemberPointerType *MPT = CE->getType()->getAs<MemberPointerType>(); 2086 return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT); 2087 } 2088 2089 case CK_ReinterpretMemberPointer: 2090 case CK_BaseToDerivedMemberPointer: 2091 case CK_DerivedToBaseMemberPointer: { 2092 Value *Src = Visit(E); 2093 2094 // Note that the AST doesn't distinguish between checked and 2095 // unchecked member pointer conversions, so we always have to 2096 // implement checked conversions here. This is inefficient when 2097 // actual control flow may be required in order to perform the 2098 // check, which it is for data member pointers (but not member 2099 // function pointers on Itanium and ARM). 2100 return CGF.CGM.getCXXABI().EmitMemberPointerConversion(CGF, CE, Src); 2101 } 2102 2103 case CK_ARCProduceObject: 2104 return CGF.EmitARCRetainScalarExpr(E); 2105 case CK_ARCConsumeObject: 2106 return CGF.EmitObjCConsumeObject(E->getType(), Visit(E)); 2107 case CK_ARCReclaimReturnedObject: 2108 return CGF.EmitARCReclaimReturnedObject(E, /*allowUnsafe*/ Ignored); 2109 case CK_ARCExtendBlockObject: 2110 return CGF.EmitARCExtendBlockObject(E); 2111 2112 case CK_CopyAndAutoreleaseBlockObject: 2113 return CGF.EmitBlockCopyAndAutorelease(Visit(E), E->getType()); 2114 2115 case CK_FloatingRealToComplex: 2116 case CK_FloatingComplexCast: 2117 case CK_IntegralRealToComplex: 2118 case CK_IntegralComplexCast: 2119 case CK_IntegralComplexToFloatingComplex: 2120 case CK_FloatingComplexToIntegralComplex: 2121 case CK_ConstructorConversion: 2122 case CK_ToUnion: 2123 llvm_unreachable("scalar cast to non-scalar value"); 2124 2125 case CK_LValueToRValue: 2126 assert(CGF.getContext().hasSameUnqualifiedType(E->getType(), DestTy)); 2127 assert(E->isGLValue() && "lvalue-to-rvalue applied to r-value!"); 2128 return Visit(const_cast<Expr*>(E)); 2129 2130 case CK_IntegralToPointer: { 2131 Value *Src = Visit(const_cast<Expr*>(E)); 2132 2133 // First, convert to the correct width so that we control the kind of 2134 // extension. 2135 auto DestLLVMTy = ConvertType(DestTy); 2136 llvm::Type *MiddleTy = CGF.CGM.getDataLayout().getIntPtrType(DestLLVMTy); 2137 bool InputSigned = E->getType()->isSignedIntegerOrEnumerationType(); 2138 llvm::Value* IntResult = 2139 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); 2140 2141 auto *IntToPtr = Builder.CreateIntToPtr(IntResult, DestLLVMTy); 2142 2143 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers) { 2144 // Going from integer to pointer that could be dynamic requires reloading 2145 // dynamic information from invariant.group. 2146 if (DestTy.mayBeDynamicClass()) 2147 IntToPtr = Builder.CreateLaunderInvariantGroup(IntToPtr); 2148 } 2149 return IntToPtr; 2150 } 2151 case CK_PointerToIntegral: { 2152 assert(!DestTy->isBooleanType() && "bool should use PointerToBool"); 2153 auto *PtrExpr = Visit(E); 2154 2155 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers) { 2156 const QualType SrcType = E->getType(); 2157 2158 // Casting to integer requires stripping dynamic information as it does 2159 // not carries it. 2160 if (SrcType.mayBeDynamicClass()) 2161 PtrExpr = Builder.CreateStripInvariantGroup(PtrExpr); 2162 } 2163 2164 return Builder.CreatePtrToInt(PtrExpr, ConvertType(DestTy)); 2165 } 2166 case CK_ToVoid: { 2167 CGF.EmitIgnoredExpr(E); 2168 return nullptr; 2169 } 2170 case CK_VectorSplat: { 2171 llvm::Type *DstTy = ConvertType(DestTy); 2172 Value *Elt = Visit(const_cast<Expr*>(E)); 2173 // Splat the element across to all elements 2174 unsigned NumElements = cast<llvm::FixedVectorType>(DstTy)->getNumElements(); 2175 return Builder.CreateVectorSplat(NumElements, Elt, "splat"); 2176 } 2177 2178 case CK_FixedPointCast: 2179 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2180 CE->getExprLoc()); 2181 2182 case CK_FixedPointToBoolean: 2183 assert(E->getType()->isFixedPointType() && 2184 "Expected src type to be fixed point type"); 2185 assert(DestTy->isBooleanType() && "Expected dest type to be boolean type"); 2186 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2187 CE->getExprLoc()); 2188 2189 case CK_FixedPointToIntegral: 2190 assert(E->getType()->isFixedPointType() && 2191 "Expected src type to be fixed point type"); 2192 assert(DestTy->isIntegerType() && "Expected dest type to be an integer"); 2193 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2194 CE->getExprLoc()); 2195 2196 case CK_IntegralToFixedPoint: 2197 assert(E->getType()->isIntegerType() && 2198 "Expected src type to be an integer"); 2199 assert(DestTy->isFixedPointType() && 2200 "Expected dest type to be fixed point type"); 2201 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2202 CE->getExprLoc()); 2203 2204 case CK_IntegralCast: { 2205 ScalarConversionOpts Opts; 2206 if (auto *ICE = dyn_cast<ImplicitCastExpr>(CE)) { 2207 if (!ICE->isPartOfExplicitCast()) 2208 Opts = ScalarConversionOpts(CGF.SanOpts); 2209 } 2210 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2211 CE->getExprLoc(), Opts); 2212 } 2213 case CK_IntegralToFloating: 2214 case CK_FloatingToIntegral: 2215 case CK_FloatingCast: 2216 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2217 CE->getExprLoc()); 2218 case CK_BooleanToSignedIntegral: { 2219 ScalarConversionOpts Opts; 2220 Opts.TreatBooleanAsSigned = true; 2221 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2222 CE->getExprLoc(), Opts); 2223 } 2224 case CK_IntegralToBoolean: 2225 return EmitIntToBoolConversion(Visit(E)); 2226 case CK_PointerToBoolean: 2227 return EmitPointerToBoolConversion(Visit(E), E->getType()); 2228 case CK_FloatingToBoolean: 2229 return EmitFloatToBoolConversion(Visit(E)); 2230 case CK_MemberPointerToBoolean: { 2231 llvm::Value *MemPtr = Visit(E); 2232 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>(); 2233 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT); 2234 } 2235 2236 case CK_FloatingComplexToReal: 2237 case CK_IntegralComplexToReal: 2238 return CGF.EmitComplexExpr(E, false, true).first; 2239 2240 case CK_FloatingComplexToBoolean: 2241 case CK_IntegralComplexToBoolean: { 2242 CodeGenFunction::ComplexPairTy V = CGF.EmitComplexExpr(E); 2243 2244 // TODO: kill this function off, inline appropriate case here 2245 return EmitComplexToScalarConversion(V, E->getType(), DestTy, 2246 CE->getExprLoc()); 2247 } 2248 2249 case CK_ZeroToOCLOpaqueType: { 2250 assert((DestTy->isEventT() || DestTy->isQueueT() || 2251 DestTy->isOCLIntelSubgroupAVCType()) && 2252 "CK_ZeroToOCLEvent cast on non-event type"); 2253 return llvm::Constant::getNullValue(ConvertType(DestTy)); 2254 } 2255 2256 case CK_IntToOCLSampler: 2257 return CGF.CGM.createOpenCLIntToSamplerConversion(E, CGF); 2258 2259 } // end of switch 2260 2261 llvm_unreachable("unknown scalar cast"); 2262 } 2263 2264 Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { 2265 CodeGenFunction::StmtExprEvaluation eval(CGF); 2266 Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), 2267 !E->getType()->isVoidType()); 2268 if (!RetAlloca.isValid()) 2269 return nullptr; 2270 return CGF.EmitLoadOfScalar(CGF.MakeAddrLValue(RetAlloca, E->getType()), 2271 E->getExprLoc()); 2272 } 2273 2274 Value *ScalarExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) { 2275 CodeGenFunction::RunCleanupsScope Scope(CGF); 2276 Value *V = Visit(E->getSubExpr()); 2277 // Defend against dominance problems caused by jumps out of expression 2278 // evaluation through the shared cleanup block. 2279 Scope.ForceCleanup({&V}); 2280 return V; 2281 } 2282 2283 //===----------------------------------------------------------------------===// 2284 // Unary Operators 2285 //===----------------------------------------------------------------------===// 2286 2287 static BinOpInfo createBinOpInfoFromIncDec(const UnaryOperator *E, 2288 llvm::Value *InVal, bool IsInc, 2289 FPOptions FPFeatures) { 2290 BinOpInfo BinOp; 2291 BinOp.LHS = InVal; 2292 BinOp.RHS = llvm::ConstantInt::get(InVal->getType(), 1, false); 2293 BinOp.Ty = E->getType(); 2294 BinOp.Opcode = IsInc ? BO_Add : BO_Sub; 2295 BinOp.FPFeatures = FPFeatures; 2296 BinOp.E = E; 2297 return BinOp; 2298 } 2299 2300 llvm::Value *ScalarExprEmitter::EmitIncDecConsiderOverflowBehavior( 2301 const UnaryOperator *E, llvm::Value *InVal, bool IsInc) { 2302 llvm::Value *Amount = 2303 llvm::ConstantInt::get(InVal->getType(), IsInc ? 1 : -1, true); 2304 StringRef Name = IsInc ? "inc" : "dec"; 2305 switch (CGF.getLangOpts().getSignedOverflowBehavior()) { 2306 case LangOptions::SOB_Defined: 2307 return Builder.CreateAdd(InVal, Amount, Name); 2308 case LangOptions::SOB_Undefined: 2309 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) 2310 return Builder.CreateNSWAdd(InVal, Amount, Name); 2311 LLVM_FALLTHROUGH; 2312 case LangOptions::SOB_Trapping: 2313 if (!E->canOverflow()) 2314 return Builder.CreateNSWAdd(InVal, Amount, Name); 2315 return EmitOverflowCheckedBinOp(createBinOpInfoFromIncDec( 2316 E, InVal, IsInc, E->getFPFeaturesInEffect(CGF.getLangOpts()))); 2317 } 2318 llvm_unreachable("Unknown SignedOverflowBehaviorTy"); 2319 } 2320 2321 namespace { 2322 /// Handles check and update for lastprivate conditional variables. 2323 class OMPLastprivateConditionalUpdateRAII { 2324 private: 2325 CodeGenFunction &CGF; 2326 const UnaryOperator *E; 2327 2328 public: 2329 OMPLastprivateConditionalUpdateRAII(CodeGenFunction &CGF, 2330 const UnaryOperator *E) 2331 : CGF(CGF), E(E) {} 2332 ~OMPLastprivateConditionalUpdateRAII() { 2333 if (CGF.getLangOpts().OpenMP) 2334 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional( 2335 CGF, E->getSubExpr()); 2336 } 2337 }; 2338 } // namespace 2339 2340 llvm::Value * 2341 ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 2342 bool isInc, bool isPre) { 2343 OMPLastprivateConditionalUpdateRAII OMPRegion(CGF, E); 2344 QualType type = E->getSubExpr()->getType(); 2345 llvm::PHINode *atomicPHI = nullptr; 2346 llvm::Value *value; 2347 llvm::Value *input; 2348 2349 int amount = (isInc ? 1 : -1); 2350 bool isSubtraction = !isInc; 2351 2352 if (const AtomicType *atomicTy = type->getAs<AtomicType>()) { 2353 type = atomicTy->getValueType(); 2354 if (isInc && type->isBooleanType()) { 2355 llvm::Value *True = CGF.EmitToMemory(Builder.getTrue(), type); 2356 if (isPre) { 2357 Builder.CreateStore(True, LV.getAddress(CGF), LV.isVolatileQualified()) 2358 ->setAtomic(llvm::AtomicOrdering::SequentiallyConsistent); 2359 return Builder.getTrue(); 2360 } 2361 // For atomic bool increment, we just store true and return it for 2362 // preincrement, do an atomic swap with true for postincrement 2363 return Builder.CreateAtomicRMW( 2364 llvm::AtomicRMWInst::Xchg, LV.getPointer(CGF), True, 2365 llvm::AtomicOrdering::SequentiallyConsistent); 2366 } 2367 // Special case for atomic increment / decrement on integers, emit 2368 // atomicrmw instructions. We skip this if we want to be doing overflow 2369 // checking, and fall into the slow path with the atomic cmpxchg loop. 2370 if (!type->isBooleanType() && type->isIntegerType() && 2371 !(type->isUnsignedIntegerType() && 2372 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) && 2373 CGF.getLangOpts().getSignedOverflowBehavior() != 2374 LangOptions::SOB_Trapping) { 2375 llvm::AtomicRMWInst::BinOp aop = isInc ? llvm::AtomicRMWInst::Add : 2376 llvm::AtomicRMWInst::Sub; 2377 llvm::Instruction::BinaryOps op = isInc ? llvm::Instruction::Add : 2378 llvm::Instruction::Sub; 2379 llvm::Value *amt = CGF.EmitToMemory( 2380 llvm::ConstantInt::get(ConvertType(type), 1, true), type); 2381 llvm::Value *old = 2382 Builder.CreateAtomicRMW(aop, LV.getPointer(CGF), amt, 2383 llvm::AtomicOrdering::SequentiallyConsistent); 2384 return isPre ? Builder.CreateBinOp(op, old, amt) : old; 2385 } 2386 value = EmitLoadOfLValue(LV, E->getExprLoc()); 2387 input = value; 2388 // For every other atomic operation, we need to emit a load-op-cmpxchg loop 2389 llvm::BasicBlock *startBB = Builder.GetInsertBlock(); 2390 llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn); 2391 value = CGF.EmitToMemory(value, type); 2392 Builder.CreateBr(opBB); 2393 Builder.SetInsertPoint(opBB); 2394 atomicPHI = Builder.CreatePHI(value->getType(), 2); 2395 atomicPHI->addIncoming(value, startBB); 2396 value = atomicPHI; 2397 } else { 2398 value = EmitLoadOfLValue(LV, E->getExprLoc()); 2399 input = value; 2400 } 2401 2402 // Special case of integer increment that we have to check first: bool++. 2403 // Due to promotion rules, we get: 2404 // bool++ -> bool = bool + 1 2405 // -> bool = (int)bool + 1 2406 // -> bool = ((int)bool + 1 != 0) 2407 // An interesting aspect of this is that increment is always true. 2408 // Decrement does not have this property. 2409 if (isInc && type->isBooleanType()) { 2410 value = Builder.getTrue(); 2411 2412 // Most common case by far: integer increment. 2413 } else if (type->isIntegerType()) { 2414 QualType promotedType; 2415 bool canPerformLossyDemotionCheck = false; 2416 if (type->isPromotableIntegerType()) { 2417 promotedType = CGF.getContext().getPromotedIntegerType(type); 2418 assert(promotedType != type && "Shouldn't promote to the same type."); 2419 canPerformLossyDemotionCheck = true; 2420 canPerformLossyDemotionCheck &= 2421 CGF.getContext().getCanonicalType(type) != 2422 CGF.getContext().getCanonicalType(promotedType); 2423 canPerformLossyDemotionCheck &= 2424 PromotionIsPotentiallyEligibleForImplicitIntegerConversionCheck( 2425 type, promotedType); 2426 assert((!canPerformLossyDemotionCheck || 2427 type->isSignedIntegerOrEnumerationType() || 2428 promotedType->isSignedIntegerOrEnumerationType() || 2429 ConvertType(type)->getScalarSizeInBits() == 2430 ConvertType(promotedType)->getScalarSizeInBits()) && 2431 "The following check expects that if we do promotion to different " 2432 "underlying canonical type, at least one of the types (either " 2433 "base or promoted) will be signed, or the bitwidths will match."); 2434 } 2435 if (CGF.SanOpts.hasOneOf( 2436 SanitizerKind::ImplicitIntegerArithmeticValueChange) && 2437 canPerformLossyDemotionCheck) { 2438 // While `x += 1` (for `x` with width less than int) is modeled as 2439 // promotion+arithmetics+demotion, and we can catch lossy demotion with 2440 // ease; inc/dec with width less than int can't overflow because of 2441 // promotion rules, so we omit promotion+demotion, which means that we can 2442 // not catch lossy "demotion". Because we still want to catch these cases 2443 // when the sanitizer is enabled, we perform the promotion, then perform 2444 // the increment/decrement in the wider type, and finally 2445 // perform the demotion. This will catch lossy demotions. 2446 2447 value = EmitScalarConversion(value, type, promotedType, E->getExprLoc()); 2448 Value *amt = llvm::ConstantInt::get(value->getType(), amount, true); 2449 value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec"); 2450 // Do pass non-default ScalarConversionOpts so that sanitizer check is 2451 // emitted. 2452 value = EmitScalarConversion(value, promotedType, type, E->getExprLoc(), 2453 ScalarConversionOpts(CGF.SanOpts)); 2454 2455 // Note that signed integer inc/dec with width less than int can't 2456 // overflow because of promotion rules; we're just eliding a few steps 2457 // here. 2458 } else if (E->canOverflow() && type->isSignedIntegerOrEnumerationType()) { 2459 value = EmitIncDecConsiderOverflowBehavior(E, value, isInc); 2460 } else if (E->canOverflow() && type->isUnsignedIntegerType() && 2461 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) { 2462 value = EmitOverflowCheckedBinOp(createBinOpInfoFromIncDec( 2463 E, value, isInc, E->getFPFeaturesInEffect(CGF.getLangOpts()))); 2464 } else { 2465 llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount, true); 2466 value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec"); 2467 } 2468 2469 // Next most common: pointer increment. 2470 } else if (const PointerType *ptr = type->getAs<PointerType>()) { 2471 QualType type = ptr->getPointeeType(); 2472 2473 // VLA types don't have constant size. 2474 if (const VariableArrayType *vla 2475 = CGF.getContext().getAsVariableArrayType(type)) { 2476 llvm::Value *numElts = CGF.getVLASize(vla).NumElts; 2477 if (!isInc) numElts = Builder.CreateNSWNeg(numElts, "vla.negsize"); 2478 if (CGF.getLangOpts().isSignedOverflowDefined()) 2479 value = Builder.CreateGEP(value, numElts, "vla.inc"); 2480 else 2481 value = CGF.EmitCheckedInBoundsGEP( 2482 value, numElts, /*SignedIndices=*/false, isSubtraction, 2483 E->getExprLoc(), "vla.inc"); 2484 2485 // Arithmetic on function pointers (!) is just +-1. 2486 } else if (type->isFunctionType()) { 2487 llvm::Value *amt = Builder.getInt32(amount); 2488 2489 value = CGF.EmitCastToVoidPtr(value); 2490 if (CGF.getLangOpts().isSignedOverflowDefined()) 2491 value = Builder.CreateGEP(value, amt, "incdec.funcptr"); 2492 else 2493 value = CGF.EmitCheckedInBoundsGEP(value, amt, /*SignedIndices=*/false, 2494 isSubtraction, E->getExprLoc(), 2495 "incdec.funcptr"); 2496 value = Builder.CreateBitCast(value, input->getType()); 2497 2498 // For everything else, we can just do a simple increment. 2499 } else { 2500 llvm::Value *amt = Builder.getInt32(amount); 2501 if (CGF.getLangOpts().isSignedOverflowDefined()) 2502 value = Builder.CreateGEP(value, amt, "incdec.ptr"); 2503 else 2504 value = CGF.EmitCheckedInBoundsGEP(value, amt, /*SignedIndices=*/false, 2505 isSubtraction, E->getExprLoc(), 2506 "incdec.ptr"); 2507 } 2508 2509 // Vector increment/decrement. 2510 } else if (type->isVectorType()) { 2511 if (type->hasIntegerRepresentation()) { 2512 llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount); 2513 2514 value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec"); 2515 } else { 2516 value = Builder.CreateFAdd( 2517 value, 2518 llvm::ConstantFP::get(value->getType(), amount), 2519 isInc ? "inc" : "dec"); 2520 } 2521 2522 // Floating point. 2523 } else if (type->isRealFloatingType()) { 2524 // Add the inc/dec to the real part. 2525 llvm::Value *amt; 2526 2527 if (type->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) { 2528 // Another special case: half FP increment should be done via float 2529 if (CGF.getContext().getTargetInfo().useFP16ConversionIntrinsics()) { 2530 value = Builder.CreateCall( 2531 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16, 2532 CGF.CGM.FloatTy), 2533 input, "incdec.conv"); 2534 } else { 2535 value = Builder.CreateFPExt(input, CGF.CGM.FloatTy, "incdec.conv"); 2536 } 2537 } 2538 2539 if (value->getType()->isFloatTy()) 2540 amt = llvm::ConstantFP::get(VMContext, 2541 llvm::APFloat(static_cast<float>(amount))); 2542 else if (value->getType()->isDoubleTy()) 2543 amt = llvm::ConstantFP::get(VMContext, 2544 llvm::APFloat(static_cast<double>(amount))); 2545 else { 2546 // Remaining types are Half, LongDouble or __float128. Convert from float. 2547 llvm::APFloat F(static_cast<float>(amount)); 2548 bool ignored; 2549 const llvm::fltSemantics *FS; 2550 // Don't use getFloatTypeSemantics because Half isn't 2551 // necessarily represented using the "half" LLVM type. 2552 if (value->getType()->isFP128Ty()) 2553 FS = &CGF.getTarget().getFloat128Format(); 2554 else if (value->getType()->isHalfTy()) 2555 FS = &CGF.getTarget().getHalfFormat(); 2556 else 2557 FS = &CGF.getTarget().getLongDoubleFormat(); 2558 F.convert(*FS, llvm::APFloat::rmTowardZero, &ignored); 2559 amt = llvm::ConstantFP::get(VMContext, F); 2560 } 2561 value = Builder.CreateFAdd(value, amt, isInc ? "inc" : "dec"); 2562 2563 if (type->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) { 2564 if (CGF.getContext().getTargetInfo().useFP16ConversionIntrinsics()) { 2565 value = Builder.CreateCall( 2566 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16, 2567 CGF.CGM.FloatTy), 2568 value, "incdec.conv"); 2569 } else { 2570 value = Builder.CreateFPTrunc(value, input->getType(), "incdec.conv"); 2571 } 2572 } 2573 2574 // Fixed-point types. 2575 } else if (type->isFixedPointType()) { 2576 // Fixed-point types are tricky. In some cases, it isn't possible to 2577 // represent a 1 or a -1 in the type at all. Piggyback off of 2578 // EmitFixedPointBinOp to avoid having to reimplement saturation. 2579 BinOpInfo Info; 2580 Info.E = E; 2581 Info.Ty = E->getType(); 2582 Info.Opcode = isInc ? BO_Add : BO_Sub; 2583 Info.LHS = value; 2584 Info.RHS = llvm::ConstantInt::get(value->getType(), 1, false); 2585 // If the type is signed, it's better to represent this as +(-1) or -(-1), 2586 // since -1 is guaranteed to be representable. 2587 if (type->isSignedFixedPointType()) { 2588 Info.Opcode = isInc ? BO_Sub : BO_Add; 2589 Info.RHS = Builder.CreateNeg(Info.RHS); 2590 } 2591 // Now, convert from our invented integer literal to the type of the unary 2592 // op. This will upscale and saturate if necessary. This value can become 2593 // undef in some cases. 2594 llvm::FixedPointBuilder<CGBuilderTy> FPBuilder(Builder); 2595 auto DstSema = CGF.getContext().getFixedPointSemantics(Info.Ty); 2596 Info.RHS = FPBuilder.CreateIntegerToFixed(Info.RHS, true, DstSema); 2597 value = EmitFixedPointBinOp(Info); 2598 2599 // Objective-C pointer types. 2600 } else { 2601 const ObjCObjectPointerType *OPT = type->castAs<ObjCObjectPointerType>(); 2602 value = CGF.EmitCastToVoidPtr(value); 2603 2604 CharUnits size = CGF.getContext().getTypeSizeInChars(OPT->getObjectType()); 2605 if (!isInc) size = -size; 2606 llvm::Value *sizeValue = 2607 llvm::ConstantInt::get(CGF.SizeTy, size.getQuantity()); 2608 2609 if (CGF.getLangOpts().isSignedOverflowDefined()) 2610 value = Builder.CreateGEP(value, sizeValue, "incdec.objptr"); 2611 else 2612 value = CGF.EmitCheckedInBoundsGEP(value, sizeValue, 2613 /*SignedIndices=*/false, isSubtraction, 2614 E->getExprLoc(), "incdec.objptr"); 2615 value = Builder.CreateBitCast(value, input->getType()); 2616 } 2617 2618 if (atomicPHI) { 2619 llvm::BasicBlock *curBlock = Builder.GetInsertBlock(); 2620 llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn); 2621 auto Pair = CGF.EmitAtomicCompareExchange( 2622 LV, RValue::get(atomicPHI), RValue::get(value), E->getExprLoc()); 2623 llvm::Value *old = CGF.EmitToMemory(Pair.first.getScalarVal(), type); 2624 llvm::Value *success = Pair.second; 2625 atomicPHI->addIncoming(old, curBlock); 2626 Builder.CreateCondBr(success, contBB, atomicPHI->getParent()); 2627 Builder.SetInsertPoint(contBB); 2628 return isPre ? value : input; 2629 } 2630 2631 // Store the updated result through the lvalue. 2632 if (LV.isBitField()) 2633 CGF.EmitStoreThroughBitfieldLValue(RValue::get(value), LV, &value); 2634 else 2635 CGF.EmitStoreThroughLValue(RValue::get(value), LV); 2636 2637 // If this is a postinc, return the value read from memory, otherwise use the 2638 // updated value. 2639 return isPre ? value : input; 2640 } 2641 2642 2643 2644 Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { 2645 TestAndClearIgnoreResultAssign(); 2646 Value *Op = Visit(E->getSubExpr()); 2647 2648 // Generate a unary FNeg for FP ops. 2649 if (Op->getType()->isFPOrFPVectorTy()) 2650 return Builder.CreateFNeg(Op, "fneg"); 2651 2652 // Emit unary minus with EmitSub so we handle overflow cases etc. 2653 BinOpInfo BinOp; 2654 BinOp.RHS = Op; 2655 BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType()); 2656 BinOp.Ty = E->getType(); 2657 BinOp.Opcode = BO_Sub; 2658 BinOp.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); 2659 BinOp.E = E; 2660 return EmitSub(BinOp); 2661 } 2662 2663 Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { 2664 TestAndClearIgnoreResultAssign(); 2665 Value *Op = Visit(E->getSubExpr()); 2666 return Builder.CreateNot(Op, "neg"); 2667 } 2668 2669 Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { 2670 // Perform vector logical not on comparison with zero vector. 2671 if (E->getType()->isVectorType() && 2672 E->getType()->castAs<VectorType>()->getVectorKind() == 2673 VectorType::GenericVector) { 2674 Value *Oper = Visit(E->getSubExpr()); 2675 Value *Zero = llvm::Constant::getNullValue(Oper->getType()); 2676 Value *Result; 2677 if (Oper->getType()->isFPOrFPVectorTy()) { 2678 CodeGenFunction::CGFPOptionsRAII FPOptsRAII( 2679 CGF, E->getFPFeaturesInEffect(CGF.getLangOpts())); 2680 Result = Builder.CreateFCmp(llvm::CmpInst::FCMP_OEQ, Oper, Zero, "cmp"); 2681 } else 2682 Result = Builder.CreateICmp(llvm::CmpInst::ICMP_EQ, Oper, Zero, "cmp"); 2683 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); 2684 } 2685 2686 // Compare operand to zero. 2687 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); 2688 2689 // Invert value. 2690 // TODO: Could dynamically modify easy computations here. For example, if 2691 // the operand is an icmp ne, turn into icmp eq. 2692 BoolVal = Builder.CreateNot(BoolVal, "lnot"); 2693 2694 // ZExt result to the expr type. 2695 return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext"); 2696 } 2697 2698 Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) { 2699 // Try folding the offsetof to a constant. 2700 Expr::EvalResult EVResult; 2701 if (E->EvaluateAsInt(EVResult, CGF.getContext())) { 2702 llvm::APSInt Value = EVResult.Val.getInt(); 2703 return Builder.getInt(Value); 2704 } 2705 2706 // Loop over the components of the offsetof to compute the value. 2707 unsigned n = E->getNumComponents(); 2708 llvm::Type* ResultType = ConvertType(E->getType()); 2709 llvm::Value* Result = llvm::Constant::getNullValue(ResultType); 2710 QualType CurrentType = E->getTypeSourceInfo()->getType(); 2711 for (unsigned i = 0; i != n; ++i) { 2712 OffsetOfNode ON = E->getComponent(i); 2713 llvm::Value *Offset = nullptr; 2714 switch (ON.getKind()) { 2715 case OffsetOfNode::Array: { 2716 // Compute the index 2717 Expr *IdxExpr = E->getIndexExpr(ON.getArrayExprIndex()); 2718 llvm::Value* Idx = CGF.EmitScalarExpr(IdxExpr); 2719 bool IdxSigned = IdxExpr->getType()->isSignedIntegerOrEnumerationType(); 2720 Idx = Builder.CreateIntCast(Idx, ResultType, IdxSigned, "conv"); 2721 2722 // Save the element type 2723 CurrentType = 2724 CGF.getContext().getAsArrayType(CurrentType)->getElementType(); 2725 2726 // Compute the element size 2727 llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType, 2728 CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity()); 2729 2730 // Multiply out to compute the result 2731 Offset = Builder.CreateMul(Idx, ElemSize); 2732 break; 2733 } 2734 2735 case OffsetOfNode::Field: { 2736 FieldDecl *MemberDecl = ON.getField(); 2737 RecordDecl *RD = CurrentType->castAs<RecordType>()->getDecl(); 2738 const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); 2739 2740 // Compute the index of the field in its parent. 2741 unsigned i = 0; 2742 // FIXME: It would be nice if we didn't have to loop here! 2743 for (RecordDecl::field_iterator Field = RD->field_begin(), 2744 FieldEnd = RD->field_end(); 2745 Field != FieldEnd; ++Field, ++i) { 2746 if (*Field == MemberDecl) 2747 break; 2748 } 2749 assert(i < RL.getFieldCount() && "offsetof field in wrong type"); 2750 2751 // Compute the offset to the field 2752 int64_t OffsetInt = RL.getFieldOffset(i) / 2753 CGF.getContext().getCharWidth(); 2754 Offset = llvm::ConstantInt::get(ResultType, OffsetInt); 2755 2756 // Save the element type. 2757 CurrentType = MemberDecl->getType(); 2758 break; 2759 } 2760 2761 case OffsetOfNode::Identifier: 2762 llvm_unreachable("dependent __builtin_offsetof"); 2763 2764 case OffsetOfNode::Base: { 2765 if (ON.getBase()->isVirtual()) { 2766 CGF.ErrorUnsupported(E, "virtual base in offsetof"); 2767 continue; 2768 } 2769 2770 RecordDecl *RD = CurrentType->castAs<RecordType>()->getDecl(); 2771 const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); 2772 2773 // Save the element type. 2774 CurrentType = ON.getBase()->getType(); 2775 2776 // Compute the offset to the base. 2777 const RecordType *BaseRT = CurrentType->getAs<RecordType>(); 2778 CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); 2779 CharUnits OffsetInt = RL.getBaseClassOffset(BaseRD); 2780 Offset = llvm::ConstantInt::get(ResultType, OffsetInt.getQuantity()); 2781 break; 2782 } 2783 } 2784 Result = Builder.CreateAdd(Result, Offset); 2785 } 2786 return Result; 2787 } 2788 2789 /// VisitUnaryExprOrTypeTraitExpr - Return the size or alignment of the type of 2790 /// argument of the sizeof expression as an integer. 2791 Value * 2792 ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr( 2793 const UnaryExprOrTypeTraitExpr *E) { 2794 QualType TypeToSize = E->getTypeOfArgument(); 2795 if (E->getKind() == UETT_SizeOf) { 2796 if (const VariableArrayType *VAT = 2797 CGF.getContext().getAsVariableArrayType(TypeToSize)) { 2798 if (E->isArgumentType()) { 2799 // sizeof(type) - make sure to emit the VLA size. 2800 CGF.EmitVariablyModifiedType(TypeToSize); 2801 } else { 2802 // C99 6.5.3.4p2: If the argument is an expression of type 2803 // VLA, it is evaluated. 2804 CGF.EmitIgnoredExpr(E->getArgumentExpr()); 2805 } 2806 2807 auto VlaSize = CGF.getVLASize(VAT); 2808 llvm::Value *size = VlaSize.NumElts; 2809 2810 // Scale the number of non-VLA elements by the non-VLA element size. 2811 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(VlaSize.Type); 2812 if (!eltSize.isOne()) 2813 size = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), size); 2814 2815 return size; 2816 } 2817 } else if (E->getKind() == UETT_OpenMPRequiredSimdAlign) { 2818 auto Alignment = 2819 CGF.getContext() 2820 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( 2821 E->getTypeOfArgument()->getPointeeType())) 2822 .getQuantity(); 2823 return llvm::ConstantInt::get(CGF.SizeTy, Alignment); 2824 } 2825 2826 // If this isn't sizeof(vla), the result must be constant; use the constant 2827 // folding logic so we don't have to duplicate it here. 2828 return Builder.getInt(E->EvaluateKnownConstInt(CGF.getContext())); 2829 } 2830 2831 Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { 2832 Expr *Op = E->getSubExpr(); 2833 if (Op->getType()->isAnyComplexType()) { 2834 // If it's an l-value, load through the appropriate subobject l-value. 2835 // Note that we have to ask E because Op might be an l-value that 2836 // this won't work for, e.g. an Obj-C property. 2837 if (E->isGLValue()) 2838 return CGF.EmitLoadOfLValue(CGF.EmitLValue(E), 2839 E->getExprLoc()).getScalarVal(); 2840 2841 // Otherwise, calculate and project. 2842 return CGF.EmitComplexExpr(Op, false, true).first; 2843 } 2844 2845 return Visit(Op); 2846 } 2847 2848 Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { 2849 Expr *Op = E->getSubExpr(); 2850 if (Op->getType()->isAnyComplexType()) { 2851 // If it's an l-value, load through the appropriate subobject l-value. 2852 // Note that we have to ask E because Op might be an l-value that 2853 // this won't work for, e.g. an Obj-C property. 2854 if (Op->isGLValue()) 2855 return CGF.EmitLoadOfLValue(CGF.EmitLValue(E), 2856 E->getExprLoc()).getScalarVal(); 2857 2858 // Otherwise, calculate and project. 2859 return CGF.EmitComplexExpr(Op, true, false).second; 2860 } 2861 2862 // __imag on a scalar returns zero. Emit the subexpr to ensure side 2863 // effects are evaluated, but not the actual value. 2864 if (Op->isGLValue()) 2865 CGF.EmitLValue(Op); 2866 else 2867 CGF.EmitScalarExpr(Op, true); 2868 return llvm::Constant::getNullValue(ConvertType(E->getType())); 2869 } 2870 2871 //===----------------------------------------------------------------------===// 2872 // Binary Operators 2873 //===----------------------------------------------------------------------===// 2874 2875 BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { 2876 TestAndClearIgnoreResultAssign(); 2877 BinOpInfo Result; 2878 Result.LHS = Visit(E->getLHS()); 2879 Result.RHS = Visit(E->getRHS()); 2880 Result.Ty = E->getType(); 2881 Result.Opcode = E->getOpcode(); 2882 Result.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); 2883 Result.E = E; 2884 return Result; 2885 } 2886 2887 LValue ScalarExprEmitter::EmitCompoundAssignLValue( 2888 const CompoundAssignOperator *E, 2889 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &), 2890 Value *&Result) { 2891 QualType LHSTy = E->getLHS()->getType(); 2892 BinOpInfo OpInfo; 2893 2894 if (E->getComputationResultType()->isAnyComplexType()) 2895 return CGF.EmitScalarCompoundAssignWithComplex(E, Result); 2896 2897 // Emit the RHS first. __block variables need to have the rhs evaluated 2898 // first, plus this should improve codegen a little. 2899 OpInfo.RHS = Visit(E->getRHS()); 2900 OpInfo.Ty = E->getComputationResultType(); 2901 OpInfo.Opcode = E->getOpcode(); 2902 OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); 2903 OpInfo.E = E; 2904 // Load/convert the LHS. 2905 LValue LHSLV = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store); 2906 2907 llvm::PHINode *atomicPHI = nullptr; 2908 if (const AtomicType *atomicTy = LHSTy->getAs<AtomicType>()) { 2909 QualType type = atomicTy->getValueType(); 2910 if (!type->isBooleanType() && type->isIntegerType() && 2911 !(type->isUnsignedIntegerType() && 2912 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) && 2913 CGF.getLangOpts().getSignedOverflowBehavior() != 2914 LangOptions::SOB_Trapping) { 2915 llvm::AtomicRMWInst::BinOp AtomicOp = llvm::AtomicRMWInst::BAD_BINOP; 2916 llvm::Instruction::BinaryOps Op; 2917 switch (OpInfo.Opcode) { 2918 // We don't have atomicrmw operands for *, %, /, <<, >> 2919 case BO_MulAssign: case BO_DivAssign: 2920 case BO_RemAssign: 2921 case BO_ShlAssign: 2922 case BO_ShrAssign: 2923 break; 2924 case BO_AddAssign: 2925 AtomicOp = llvm::AtomicRMWInst::Add; 2926 Op = llvm::Instruction::Add; 2927 break; 2928 case BO_SubAssign: 2929 AtomicOp = llvm::AtomicRMWInst::Sub; 2930 Op = llvm::Instruction::Sub; 2931 break; 2932 case BO_AndAssign: 2933 AtomicOp = llvm::AtomicRMWInst::And; 2934 Op = llvm::Instruction::And; 2935 break; 2936 case BO_XorAssign: 2937 AtomicOp = llvm::AtomicRMWInst::Xor; 2938 Op = llvm::Instruction::Xor; 2939 break; 2940 case BO_OrAssign: 2941 AtomicOp = llvm::AtomicRMWInst::Or; 2942 Op = llvm::Instruction::Or; 2943 break; 2944 default: 2945 llvm_unreachable("Invalid compound assignment type"); 2946 } 2947 if (AtomicOp != llvm::AtomicRMWInst::BAD_BINOP) { 2948 llvm::Value *Amt = CGF.EmitToMemory( 2949 EmitScalarConversion(OpInfo.RHS, E->getRHS()->getType(), LHSTy, 2950 E->getExprLoc()), 2951 LHSTy); 2952 Value *OldVal = Builder.CreateAtomicRMW( 2953 AtomicOp, LHSLV.getPointer(CGF), Amt, 2954 llvm::AtomicOrdering::SequentiallyConsistent); 2955 2956 // Since operation is atomic, the result type is guaranteed to be the 2957 // same as the input in LLVM terms. 2958 Result = Builder.CreateBinOp(Op, OldVal, Amt); 2959 return LHSLV; 2960 } 2961 } 2962 // FIXME: For floating point types, we should be saving and restoring the 2963 // floating point environment in the loop. 2964 llvm::BasicBlock *startBB = Builder.GetInsertBlock(); 2965 llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn); 2966 OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->getExprLoc()); 2967 OpInfo.LHS = CGF.EmitToMemory(OpInfo.LHS, type); 2968 Builder.CreateBr(opBB); 2969 Builder.SetInsertPoint(opBB); 2970 atomicPHI = Builder.CreatePHI(OpInfo.LHS->getType(), 2); 2971 atomicPHI->addIncoming(OpInfo.LHS, startBB); 2972 OpInfo.LHS = atomicPHI; 2973 } 2974 else 2975 OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->getExprLoc()); 2976 2977 SourceLocation Loc = E->getExprLoc(); 2978 OpInfo.LHS = 2979 EmitScalarConversion(OpInfo.LHS, LHSTy, E->getComputationLHSType(), Loc); 2980 2981 // Expand the binary operator. 2982 Result = (this->*Func)(OpInfo); 2983 2984 // Convert the result back to the LHS type, 2985 // potentially with Implicit Conversion sanitizer check. 2986 Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy, 2987 Loc, ScalarConversionOpts(CGF.SanOpts)); 2988 2989 if (atomicPHI) { 2990 llvm::BasicBlock *curBlock = Builder.GetInsertBlock(); 2991 llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn); 2992 auto Pair = CGF.EmitAtomicCompareExchange( 2993 LHSLV, RValue::get(atomicPHI), RValue::get(Result), E->getExprLoc()); 2994 llvm::Value *old = CGF.EmitToMemory(Pair.first.getScalarVal(), LHSTy); 2995 llvm::Value *success = Pair.second; 2996 atomicPHI->addIncoming(old, curBlock); 2997 Builder.CreateCondBr(success, contBB, atomicPHI->getParent()); 2998 Builder.SetInsertPoint(contBB); 2999 return LHSLV; 3000 } 3001 3002 // Store the result value into the LHS lvalue. Bit-fields are handled 3003 // specially because the result is altered by the store, i.e., [C99 6.5.16p1] 3004 // 'An assignment expression has the value of the left operand after the 3005 // assignment...'. 3006 if (LHSLV.isBitField()) 3007 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, &Result); 3008 else 3009 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV); 3010 3011 if (CGF.getLangOpts().OpenMP) 3012 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, 3013 E->getLHS()); 3014 return LHSLV; 3015 } 3016 3017 Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, 3018 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { 3019 bool Ignore = TestAndClearIgnoreResultAssign(); 3020 Value *RHS = nullptr; 3021 LValue LHS = EmitCompoundAssignLValue(E, Func, RHS); 3022 3023 // If the result is clearly ignored, return now. 3024 if (Ignore) 3025 return nullptr; 3026 3027 // The result of an assignment in C is the assigned r-value. 3028 if (!CGF.getLangOpts().CPlusPlus) 3029 return RHS; 3030 3031 // If the lvalue is non-volatile, return the computed value of the assignment. 3032 if (!LHS.isVolatileQualified()) 3033 return RHS; 3034 3035 // Otherwise, reload the value. 3036 return EmitLoadOfLValue(LHS, E->getExprLoc()); 3037 } 3038 3039 void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck( 3040 const BinOpInfo &Ops, llvm::Value *Zero, bool isDiv) { 3041 SmallVector<std::pair<llvm::Value *, SanitizerMask>, 2> Checks; 3042 3043 if (CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero)) { 3044 Checks.push_back(std::make_pair(Builder.CreateICmpNE(Ops.RHS, Zero), 3045 SanitizerKind::IntegerDivideByZero)); 3046 } 3047 3048 const auto *BO = cast<BinaryOperator>(Ops.E); 3049 if (CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow) && 3050 Ops.Ty->hasSignedIntegerRepresentation() && 3051 !IsWidenedIntegerOp(CGF.getContext(), BO->getLHS()) && 3052 Ops.mayHaveIntegerOverflow()) { 3053 llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType()); 3054 3055 llvm::Value *IntMin = 3056 Builder.getInt(llvm::APInt::getSignedMinValue(Ty->getBitWidth())); 3057 llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL); 3058 3059 llvm::Value *LHSCmp = Builder.CreateICmpNE(Ops.LHS, IntMin); 3060 llvm::Value *RHSCmp = Builder.CreateICmpNE(Ops.RHS, NegOne); 3061 llvm::Value *NotOverflow = Builder.CreateOr(LHSCmp, RHSCmp, "or"); 3062 Checks.push_back( 3063 std::make_pair(NotOverflow, SanitizerKind::SignedIntegerOverflow)); 3064 } 3065 3066 if (Checks.size() > 0) 3067 EmitBinOpCheck(Checks, Ops); 3068 } 3069 3070 Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { 3071 { 3072 CodeGenFunction::SanitizerScope SanScope(&CGF); 3073 if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) || 3074 CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) && 3075 Ops.Ty->isIntegerType() && 3076 (Ops.mayHaveIntegerDivisionByZero() || Ops.mayHaveIntegerOverflow())) { 3077 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); 3078 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, true); 3079 } else if (CGF.SanOpts.has(SanitizerKind::FloatDivideByZero) && 3080 Ops.Ty->isRealFloatingType() && 3081 Ops.mayHaveFloatDivisionByZero()) { 3082 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); 3083 llvm::Value *NonZero = Builder.CreateFCmpUNE(Ops.RHS, Zero); 3084 EmitBinOpCheck(std::make_pair(NonZero, SanitizerKind::FloatDivideByZero), 3085 Ops); 3086 } 3087 } 3088 3089 if (Ops.LHS->getType()->isFPOrFPVectorTy()) { 3090 llvm::Value *Val; 3091 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Ops.FPFeatures); 3092 Val = Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); 3093 if (CGF.getLangOpts().OpenCL && 3094 !CGF.CGM.getCodeGenOpts().CorrectlyRoundedDivSqrt) { 3095 // OpenCL v1.1 s7.4: minimum accuracy of single precision / is 2.5ulp 3096 // OpenCL v1.2 s5.6.4.2: The -cl-fp32-correctly-rounded-divide-sqrt 3097 // build option allows an application to specify that single precision 3098 // floating-point divide (x/y and 1/x) and sqrt used in the program 3099 // source are correctly rounded. 3100 llvm::Type *ValTy = Val->getType(); 3101 if (ValTy->isFloatTy() || 3102 (isa<llvm::VectorType>(ValTy) && 3103 cast<llvm::VectorType>(ValTy)->getElementType()->isFloatTy())) 3104 CGF.SetFPAccuracy(Val, 2.5); 3105 } 3106 return Val; 3107 } 3108 else if (Ops.isFixedPointOp()) 3109 return EmitFixedPointBinOp(Ops); 3110 else if (Ops.Ty->hasUnsignedIntegerRepresentation()) 3111 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); 3112 else 3113 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); 3114 } 3115 3116 Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { 3117 // Rem in C can't be a floating point type: C99 6.5.5p2. 3118 if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) || 3119 CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) && 3120 Ops.Ty->isIntegerType() && 3121 (Ops.mayHaveIntegerDivisionByZero() || Ops.mayHaveIntegerOverflow())) { 3122 CodeGenFunction::SanitizerScope SanScope(&CGF); 3123 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); 3124 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false); 3125 } 3126 3127 if (Ops.Ty->hasUnsignedIntegerRepresentation()) 3128 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); 3129 else 3130 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); 3131 } 3132 3133 Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) { 3134 unsigned IID; 3135 unsigned OpID = 0; 3136 3137 bool isSigned = Ops.Ty->isSignedIntegerOrEnumerationType(); 3138 switch (Ops.Opcode) { 3139 case BO_Add: 3140 case BO_AddAssign: 3141 OpID = 1; 3142 IID = isSigned ? llvm::Intrinsic::sadd_with_overflow : 3143 llvm::Intrinsic::uadd_with_overflow; 3144 break; 3145 case BO_Sub: 3146 case BO_SubAssign: 3147 OpID = 2; 3148 IID = isSigned ? llvm::Intrinsic::ssub_with_overflow : 3149 llvm::Intrinsic::usub_with_overflow; 3150 break; 3151 case BO_Mul: 3152 case BO_MulAssign: 3153 OpID = 3; 3154 IID = isSigned ? llvm::Intrinsic::smul_with_overflow : 3155 llvm::Intrinsic::umul_with_overflow; 3156 break; 3157 default: 3158 llvm_unreachable("Unsupported operation for overflow detection"); 3159 } 3160 OpID <<= 1; 3161 if (isSigned) 3162 OpID |= 1; 3163 3164 CodeGenFunction::SanitizerScope SanScope(&CGF); 3165 llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty); 3166 3167 llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, opTy); 3168 3169 Value *resultAndOverflow = Builder.CreateCall(intrinsic, {Ops.LHS, Ops.RHS}); 3170 Value *result = Builder.CreateExtractValue(resultAndOverflow, 0); 3171 Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1); 3172 3173 // Handle overflow with llvm.trap if no custom handler has been specified. 3174 const std::string *handlerName = 3175 &CGF.getLangOpts().OverflowHandler; 3176 if (handlerName->empty()) { 3177 // If the signed-integer-overflow sanitizer is enabled, emit a call to its 3178 // runtime. Otherwise, this is a -ftrapv check, so just emit a trap. 3179 if (!isSigned || CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) { 3180 llvm::Value *NotOverflow = Builder.CreateNot(overflow); 3181 SanitizerMask Kind = isSigned ? SanitizerKind::SignedIntegerOverflow 3182 : SanitizerKind::UnsignedIntegerOverflow; 3183 EmitBinOpCheck(std::make_pair(NotOverflow, Kind), Ops); 3184 } else 3185 CGF.EmitTrapCheck(Builder.CreateNot(overflow)); 3186 return result; 3187 } 3188 3189 // Branch in case of overflow. 3190 llvm::BasicBlock *initialBB = Builder.GetInsertBlock(); 3191 llvm::BasicBlock *continueBB = 3192 CGF.createBasicBlock("nooverflow", CGF.CurFn, initialBB->getNextNode()); 3193 llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn); 3194 3195 Builder.CreateCondBr(overflow, overflowBB, continueBB); 3196 3197 // If an overflow handler is set, then we want to call it and then use its 3198 // result, if it returns. 3199 Builder.SetInsertPoint(overflowBB); 3200 3201 // Get the overflow handler. 3202 llvm::Type *Int8Ty = CGF.Int8Ty; 3203 llvm::Type *argTypes[] = { CGF.Int64Ty, CGF.Int64Ty, Int8Ty, Int8Ty }; 3204 llvm::FunctionType *handlerTy = 3205 llvm::FunctionType::get(CGF.Int64Ty, argTypes, true); 3206 llvm::FunctionCallee handler = 3207 CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName); 3208 3209 // Sign extend the args to 64-bit, so that we can use the same handler for 3210 // all types of overflow. 3211 llvm::Value *lhs = Builder.CreateSExt(Ops.LHS, CGF.Int64Ty); 3212 llvm::Value *rhs = Builder.CreateSExt(Ops.RHS, CGF.Int64Ty); 3213 3214 // Call the handler with the two arguments, the operation, and the size of 3215 // the result. 3216 llvm::Value *handlerArgs[] = { 3217 lhs, 3218 rhs, 3219 Builder.getInt8(OpID), 3220 Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth()) 3221 }; 3222 llvm::Value *handlerResult = 3223 CGF.EmitNounwindRuntimeCall(handler, handlerArgs); 3224 3225 // Truncate the result back to the desired size. 3226 handlerResult = Builder.CreateTrunc(handlerResult, opTy); 3227 Builder.CreateBr(continueBB); 3228 3229 Builder.SetInsertPoint(continueBB); 3230 llvm::PHINode *phi = Builder.CreatePHI(opTy, 2); 3231 phi->addIncoming(result, initialBB); 3232 phi->addIncoming(handlerResult, overflowBB); 3233 3234 return phi; 3235 } 3236 3237 /// Emit pointer + index arithmetic. 3238 static Value *emitPointerArithmetic(CodeGenFunction &CGF, 3239 const BinOpInfo &op, 3240 bool isSubtraction) { 3241 // Must have binary (not unary) expr here. Unary pointer 3242 // increment/decrement doesn't use this path. 3243 const BinaryOperator *expr = cast<BinaryOperator>(op.E); 3244 3245 Value *pointer = op.LHS; 3246 Expr *pointerOperand = expr->getLHS(); 3247 Value *index = op.RHS; 3248 Expr *indexOperand = expr->getRHS(); 3249 3250 // In a subtraction, the LHS is always the pointer. 3251 if (!isSubtraction && !pointer->getType()->isPointerTy()) { 3252 std::swap(pointer, index); 3253 std::swap(pointerOperand, indexOperand); 3254 } 3255 3256 bool isSigned = indexOperand->getType()->isSignedIntegerOrEnumerationType(); 3257 3258 unsigned width = cast<llvm::IntegerType>(index->getType())->getBitWidth(); 3259 auto &DL = CGF.CGM.getDataLayout(); 3260 auto PtrTy = cast<llvm::PointerType>(pointer->getType()); 3261 3262 // Some versions of glibc and gcc use idioms (particularly in their malloc 3263 // routines) that add a pointer-sized integer (known to be a pointer value) 3264 // to a null pointer in order to cast the value back to an integer or as 3265 // part of a pointer alignment algorithm. This is undefined behavior, but 3266 // we'd like to be able to compile programs that use it. 3267 // 3268 // Normally, we'd generate a GEP with a null-pointer base here in response 3269 // to that code, but it's also UB to dereference a pointer created that 3270 // way. Instead (as an acknowledged hack to tolerate the idiom) we will 3271 // generate a direct cast of the integer value to a pointer. 3272 // 3273 // The idiom (p = nullptr + N) is not met if any of the following are true: 3274 // 3275 // The operation is subtraction. 3276 // The index is not pointer-sized. 3277 // The pointer type is not byte-sized. 3278 // 3279 if (BinaryOperator::isNullPointerArithmeticExtension(CGF.getContext(), 3280 op.Opcode, 3281 expr->getLHS(), 3282 expr->getRHS())) 3283 return CGF.Builder.CreateIntToPtr(index, pointer->getType()); 3284 3285 if (width != DL.getIndexTypeSizeInBits(PtrTy)) { 3286 // Zero-extend or sign-extend the pointer value according to 3287 // whether the index is signed or not. 3288 index = CGF.Builder.CreateIntCast(index, DL.getIndexType(PtrTy), isSigned, 3289 "idx.ext"); 3290 } 3291 3292 // If this is subtraction, negate the index. 3293 if (isSubtraction) 3294 index = CGF.Builder.CreateNeg(index, "idx.neg"); 3295 3296 if (CGF.SanOpts.has(SanitizerKind::ArrayBounds)) 3297 CGF.EmitBoundsCheck(op.E, pointerOperand, index, indexOperand->getType(), 3298 /*Accessed*/ false); 3299 3300 const PointerType *pointerType 3301 = pointerOperand->getType()->getAs<PointerType>(); 3302 if (!pointerType) { 3303 QualType objectType = pointerOperand->getType() 3304 ->castAs<ObjCObjectPointerType>() 3305 ->getPointeeType(); 3306 llvm::Value *objectSize 3307 = CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(objectType)); 3308 3309 index = CGF.Builder.CreateMul(index, objectSize); 3310 3311 Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy); 3312 result = CGF.Builder.CreateGEP(result, index, "add.ptr"); 3313 return CGF.Builder.CreateBitCast(result, pointer->getType()); 3314 } 3315 3316 QualType elementType = pointerType->getPointeeType(); 3317 if (const VariableArrayType *vla 3318 = CGF.getContext().getAsVariableArrayType(elementType)) { 3319 // The element count here is the total number of non-VLA elements. 3320 llvm::Value *numElements = CGF.getVLASize(vla).NumElts; 3321 3322 // Effectively, the multiply by the VLA size is part of the GEP. 3323 // GEP indexes are signed, and scaling an index isn't permitted to 3324 // signed-overflow, so we use the same semantics for our explicit 3325 // multiply. We suppress this if overflow is not undefined behavior. 3326 if (CGF.getLangOpts().isSignedOverflowDefined()) { 3327 index = CGF.Builder.CreateMul(index, numElements, "vla.index"); 3328 pointer = CGF.Builder.CreateGEP(pointer, index, "add.ptr"); 3329 } else { 3330 index = CGF.Builder.CreateNSWMul(index, numElements, "vla.index"); 3331 pointer = 3332 CGF.EmitCheckedInBoundsGEP(pointer, index, isSigned, isSubtraction, 3333 op.E->getExprLoc(), "add.ptr"); 3334 } 3335 return pointer; 3336 } 3337 3338 // Explicitly handle GNU void* and function pointer arithmetic extensions. The 3339 // GNU void* casts amount to no-ops since our void* type is i8*, but this is 3340 // future proof. 3341 if (elementType->isVoidType() || elementType->isFunctionType()) { 3342 Value *result = CGF.EmitCastToVoidPtr(pointer); 3343 result = CGF.Builder.CreateGEP(result, index, "add.ptr"); 3344 return CGF.Builder.CreateBitCast(result, pointer->getType()); 3345 } 3346 3347 if (CGF.getLangOpts().isSignedOverflowDefined()) 3348 return CGF.Builder.CreateGEP(pointer, index, "add.ptr"); 3349 3350 return CGF.EmitCheckedInBoundsGEP(pointer, index, isSigned, isSubtraction, 3351 op.E->getExprLoc(), "add.ptr"); 3352 } 3353 3354 // Construct an fmuladd intrinsic to represent a fused mul-add of MulOp and 3355 // Addend. Use negMul and negAdd to negate the first operand of the Mul or 3356 // the add operand respectively. This allows fmuladd to represent a*b-c, or 3357 // c-a*b. Patterns in LLVM should catch the negated forms and translate them to 3358 // efficient operations. 3359 static Value* buildFMulAdd(llvm::Instruction *MulOp, Value *Addend, 3360 const CodeGenFunction &CGF, CGBuilderTy &Builder, 3361 bool negMul, bool negAdd) { 3362 assert(!(negMul && negAdd) && "Only one of negMul and negAdd should be set."); 3363 3364 Value *MulOp0 = MulOp->getOperand(0); 3365 Value *MulOp1 = MulOp->getOperand(1); 3366 if (negMul) 3367 MulOp0 = Builder.CreateFNeg(MulOp0, "neg"); 3368 if (negAdd) 3369 Addend = Builder.CreateFNeg(Addend, "neg"); 3370 3371 Value *FMulAdd = nullptr; 3372 if (Builder.getIsFPConstrained()) { 3373 assert(isa<llvm::ConstrainedFPIntrinsic>(MulOp) && 3374 "Only constrained operation should be created when Builder is in FP " 3375 "constrained mode"); 3376 FMulAdd = Builder.CreateConstrainedFPCall( 3377 CGF.CGM.getIntrinsic(llvm::Intrinsic::experimental_constrained_fmuladd, 3378 Addend->getType()), 3379 {MulOp0, MulOp1, Addend}); 3380 } else { 3381 FMulAdd = Builder.CreateCall( 3382 CGF.CGM.getIntrinsic(llvm::Intrinsic::fmuladd, Addend->getType()), 3383 {MulOp0, MulOp1, Addend}); 3384 } 3385 MulOp->eraseFromParent(); 3386 3387 return FMulAdd; 3388 } 3389 3390 // Check whether it would be legal to emit an fmuladd intrinsic call to 3391 // represent op and if so, build the fmuladd. 3392 // 3393 // Checks that (a) the operation is fusable, and (b) -ffp-contract=on. 3394 // Does NOT check the type of the operation - it's assumed that this function 3395 // will be called from contexts where it's known that the type is contractable. 3396 static Value* tryEmitFMulAdd(const BinOpInfo &op, 3397 const CodeGenFunction &CGF, CGBuilderTy &Builder, 3398 bool isSub=false) { 3399 3400 assert((op.Opcode == BO_Add || op.Opcode == BO_AddAssign || 3401 op.Opcode == BO_Sub || op.Opcode == BO_SubAssign) && 3402 "Only fadd/fsub can be the root of an fmuladd."); 3403 3404 // Check whether this op is marked as fusable. 3405 if (!op.FPFeatures.allowFPContractWithinStatement()) 3406 return nullptr; 3407 3408 // We have a potentially fusable op. Look for a mul on one of the operands. 3409 // Also, make sure that the mul result isn't used directly. In that case, 3410 // there's no point creating a muladd operation. 3411 if (auto *LHSBinOp = dyn_cast<llvm::BinaryOperator>(op.LHS)) { 3412 if (LHSBinOp->getOpcode() == llvm::Instruction::FMul && 3413 LHSBinOp->use_empty()) 3414 return buildFMulAdd(LHSBinOp, op.RHS, CGF, Builder, false, isSub); 3415 } 3416 if (auto *RHSBinOp = dyn_cast<llvm::BinaryOperator>(op.RHS)) { 3417 if (RHSBinOp->getOpcode() == llvm::Instruction::FMul && 3418 RHSBinOp->use_empty()) 3419 return buildFMulAdd(RHSBinOp, op.LHS, CGF, Builder, isSub, false); 3420 } 3421 3422 if (auto *LHSBinOp = dyn_cast<llvm::CallBase>(op.LHS)) { 3423 if (LHSBinOp->getIntrinsicID() == 3424 llvm::Intrinsic::experimental_constrained_fmul && 3425 LHSBinOp->use_empty()) 3426 return buildFMulAdd(LHSBinOp, op.RHS, CGF, Builder, false, isSub); 3427 } 3428 if (auto *RHSBinOp = dyn_cast<llvm::CallBase>(op.RHS)) { 3429 if (RHSBinOp->getIntrinsicID() == 3430 llvm::Intrinsic::experimental_constrained_fmul && 3431 RHSBinOp->use_empty()) 3432 return buildFMulAdd(RHSBinOp, op.LHS, CGF, Builder, isSub, false); 3433 } 3434 3435 return nullptr; 3436 } 3437 3438 Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &op) { 3439 if (op.LHS->getType()->isPointerTy() || 3440 op.RHS->getType()->isPointerTy()) 3441 return emitPointerArithmetic(CGF, op, CodeGenFunction::NotSubtraction); 3442 3443 if (op.Ty->isSignedIntegerOrEnumerationType()) { 3444 switch (CGF.getLangOpts().getSignedOverflowBehavior()) { 3445 case LangOptions::SOB_Defined: 3446 return Builder.CreateAdd(op.LHS, op.RHS, "add"); 3447 case LangOptions::SOB_Undefined: 3448 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) 3449 return Builder.CreateNSWAdd(op.LHS, op.RHS, "add"); 3450 LLVM_FALLTHROUGH; 3451 case LangOptions::SOB_Trapping: 3452 if (CanElideOverflowCheck(CGF.getContext(), op)) 3453 return Builder.CreateNSWAdd(op.LHS, op.RHS, "add"); 3454 return EmitOverflowCheckedBinOp(op); 3455 } 3456 } 3457 3458 if (op.Ty->isConstantMatrixType()) { 3459 llvm::MatrixBuilder<CGBuilderTy> MB(Builder); 3460 return MB.CreateAdd(op.LHS, op.RHS); 3461 } 3462 3463 if (op.Ty->isUnsignedIntegerType() && 3464 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow) && 3465 !CanElideOverflowCheck(CGF.getContext(), op)) 3466 return EmitOverflowCheckedBinOp(op); 3467 3468 if (op.LHS->getType()->isFPOrFPVectorTy()) { 3469 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, op.FPFeatures); 3470 // Try to form an fmuladd. 3471 if (Value *FMulAdd = tryEmitFMulAdd(op, CGF, Builder)) 3472 return FMulAdd; 3473 3474 return Builder.CreateFAdd(op.LHS, op.RHS, "add"); 3475 } 3476 3477 if (op.isFixedPointOp()) 3478 return EmitFixedPointBinOp(op); 3479 3480 return Builder.CreateAdd(op.LHS, op.RHS, "add"); 3481 } 3482 3483 /// The resulting value must be calculated with exact precision, so the operands 3484 /// may not be the same type. 3485 Value *ScalarExprEmitter::EmitFixedPointBinOp(const BinOpInfo &op) { 3486 using llvm::APSInt; 3487 using llvm::ConstantInt; 3488 3489 // This is either a binary operation where at least one of the operands is 3490 // a fixed-point type, or a unary operation where the operand is a fixed-point 3491 // type. The result type of a binary operation is determined by 3492 // Sema::handleFixedPointConversions(). 3493 QualType ResultTy = op.Ty; 3494 QualType LHSTy, RHSTy; 3495 if (const auto *BinOp = dyn_cast<BinaryOperator>(op.E)) { 3496 RHSTy = BinOp->getRHS()->getType(); 3497 if (const auto *CAO = dyn_cast<CompoundAssignOperator>(BinOp)) { 3498 // For compound assignment, the effective type of the LHS at this point 3499 // is the computation LHS type, not the actual LHS type, and the final 3500 // result type is not the type of the expression but rather the 3501 // computation result type. 3502 LHSTy = CAO->getComputationLHSType(); 3503 ResultTy = CAO->getComputationResultType(); 3504 } else 3505 LHSTy = BinOp->getLHS()->getType(); 3506 } else if (const auto *UnOp = dyn_cast<UnaryOperator>(op.E)) { 3507 LHSTy = UnOp->getSubExpr()->getType(); 3508 RHSTy = UnOp->getSubExpr()->getType(); 3509 } 3510 ASTContext &Ctx = CGF.getContext(); 3511 Value *LHS = op.LHS; 3512 Value *RHS = op.RHS; 3513 3514 auto LHSFixedSema = Ctx.getFixedPointSemantics(LHSTy); 3515 auto RHSFixedSema = Ctx.getFixedPointSemantics(RHSTy); 3516 auto ResultFixedSema = Ctx.getFixedPointSemantics(ResultTy); 3517 auto CommonFixedSema = LHSFixedSema.getCommonSemantics(RHSFixedSema); 3518 3519 // Perform the actual operation. 3520 Value *Result; 3521 llvm::FixedPointBuilder<CGBuilderTy> FPBuilder(Builder); 3522 switch (op.Opcode) { 3523 case BO_AddAssign: 3524 case BO_Add: 3525 Result = FPBuilder.CreateAdd(LHS, LHSFixedSema, RHS, RHSFixedSema); 3526 break; 3527 case BO_SubAssign: 3528 case BO_Sub: 3529 Result = FPBuilder.CreateSub(LHS, LHSFixedSema, RHS, RHSFixedSema); 3530 break; 3531 case BO_MulAssign: 3532 case BO_Mul: 3533 Result = FPBuilder.CreateMul(LHS, LHSFixedSema, RHS, RHSFixedSema); 3534 break; 3535 case BO_DivAssign: 3536 case BO_Div: 3537 Result = FPBuilder.CreateDiv(LHS, LHSFixedSema, RHS, RHSFixedSema); 3538 break; 3539 case BO_ShlAssign: 3540 case BO_Shl: 3541 Result = FPBuilder.CreateShl(LHS, LHSFixedSema, RHS); 3542 break; 3543 case BO_ShrAssign: 3544 case BO_Shr: 3545 Result = FPBuilder.CreateShr(LHS, LHSFixedSema, RHS); 3546 break; 3547 case BO_LT: 3548 return FPBuilder.CreateLT(LHS, LHSFixedSema, RHS, RHSFixedSema); 3549 case BO_GT: 3550 return FPBuilder.CreateGT(LHS, LHSFixedSema, RHS, RHSFixedSema); 3551 case BO_LE: 3552 return FPBuilder.CreateLE(LHS, LHSFixedSema, RHS, RHSFixedSema); 3553 case BO_GE: 3554 return FPBuilder.CreateGE(LHS, LHSFixedSema, RHS, RHSFixedSema); 3555 case BO_EQ: 3556 // For equality operations, we assume any padding bits on unsigned types are 3557 // zero'd out. They could be overwritten through non-saturating operations 3558 // that cause overflow, but this leads to undefined behavior. 3559 return FPBuilder.CreateEQ(LHS, LHSFixedSema, RHS, RHSFixedSema); 3560 case BO_NE: 3561 return FPBuilder.CreateNE(LHS, LHSFixedSema, RHS, RHSFixedSema); 3562 case BO_Cmp: 3563 case BO_LAnd: 3564 case BO_LOr: 3565 llvm_unreachable("Found unimplemented fixed point binary operation"); 3566 case BO_PtrMemD: 3567 case BO_PtrMemI: 3568 case BO_Rem: 3569 case BO_Xor: 3570 case BO_And: 3571 case BO_Or: 3572 case BO_Assign: 3573 case BO_RemAssign: 3574 case BO_AndAssign: 3575 case BO_XorAssign: 3576 case BO_OrAssign: 3577 case BO_Comma: 3578 llvm_unreachable("Found unsupported binary operation for fixed point types."); 3579 } 3580 3581 bool IsShift = BinaryOperator::isShiftOp(op.Opcode) || 3582 BinaryOperator::isShiftAssignOp(op.Opcode); 3583 // Convert to the result type. 3584 return FPBuilder.CreateFixedToFixed(Result, IsShift ? LHSFixedSema 3585 : CommonFixedSema, 3586 ResultFixedSema); 3587 } 3588 3589 Value *ScalarExprEmitter::EmitSub(const BinOpInfo &op) { 3590 // The LHS is always a pointer if either side is. 3591 if (!op.LHS->getType()->isPointerTy()) { 3592 if (op.Ty->isSignedIntegerOrEnumerationType()) { 3593 switch (CGF.getLangOpts().getSignedOverflowBehavior()) { 3594 case LangOptions::SOB_Defined: 3595 return Builder.CreateSub(op.LHS, op.RHS, "sub"); 3596 case LangOptions::SOB_Undefined: 3597 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) 3598 return Builder.CreateNSWSub(op.LHS, op.RHS, "sub"); 3599 LLVM_FALLTHROUGH; 3600 case LangOptions::SOB_Trapping: 3601 if (CanElideOverflowCheck(CGF.getContext(), op)) 3602 return Builder.CreateNSWSub(op.LHS, op.RHS, "sub"); 3603 return EmitOverflowCheckedBinOp(op); 3604 } 3605 } 3606 3607 if (op.Ty->isConstantMatrixType()) { 3608 llvm::MatrixBuilder<CGBuilderTy> MB(Builder); 3609 return MB.CreateSub(op.LHS, op.RHS); 3610 } 3611 3612 if (op.Ty->isUnsignedIntegerType() && 3613 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow) && 3614 !CanElideOverflowCheck(CGF.getContext(), op)) 3615 return EmitOverflowCheckedBinOp(op); 3616 3617 if (op.LHS->getType()->isFPOrFPVectorTy()) { 3618 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, op.FPFeatures); 3619 // Try to form an fmuladd. 3620 if (Value *FMulAdd = tryEmitFMulAdd(op, CGF, Builder, true)) 3621 return FMulAdd; 3622 return Builder.CreateFSub(op.LHS, op.RHS, "sub"); 3623 } 3624 3625 if (op.isFixedPointOp()) 3626 return EmitFixedPointBinOp(op); 3627 3628 return Builder.CreateSub(op.LHS, op.RHS, "sub"); 3629 } 3630 3631 // If the RHS is not a pointer, then we have normal pointer 3632 // arithmetic. 3633 if (!op.RHS->getType()->isPointerTy()) 3634 return emitPointerArithmetic(CGF, op, CodeGenFunction::IsSubtraction); 3635 3636 // Otherwise, this is a pointer subtraction. 3637 3638 // Do the raw subtraction part. 3639 llvm::Value *LHS 3640 = Builder.CreatePtrToInt(op.LHS, CGF.PtrDiffTy, "sub.ptr.lhs.cast"); 3641 llvm::Value *RHS 3642 = Builder.CreatePtrToInt(op.RHS, CGF.PtrDiffTy, "sub.ptr.rhs.cast"); 3643 Value *diffInChars = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); 3644 3645 // Okay, figure out the element size. 3646 const BinaryOperator *expr = cast<BinaryOperator>(op.E); 3647 QualType elementType = expr->getLHS()->getType()->getPointeeType(); 3648 3649 llvm::Value *divisor = nullptr; 3650 3651 // For a variable-length array, this is going to be non-constant. 3652 if (const VariableArrayType *vla 3653 = CGF.getContext().getAsVariableArrayType(elementType)) { 3654 auto VlaSize = CGF.getVLASize(vla); 3655 elementType = VlaSize.Type; 3656 divisor = VlaSize.NumElts; 3657 3658 // Scale the number of non-VLA elements by the non-VLA element size. 3659 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(elementType); 3660 if (!eltSize.isOne()) 3661 divisor = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), divisor); 3662 3663 // For everything elese, we can just compute it, safe in the 3664 // assumption that Sema won't let anything through that we can't 3665 // safely compute the size of. 3666 } else { 3667 CharUnits elementSize; 3668 // Handle GCC extension for pointer arithmetic on void* and 3669 // function pointer types. 3670 if (elementType->isVoidType() || elementType->isFunctionType()) 3671 elementSize = CharUnits::One(); 3672 else 3673 elementSize = CGF.getContext().getTypeSizeInChars(elementType); 3674 3675 // Don't even emit the divide for element size of 1. 3676 if (elementSize.isOne()) 3677 return diffInChars; 3678 3679 divisor = CGF.CGM.getSize(elementSize); 3680 } 3681 3682 // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since 3683 // pointer difference in C is only defined in the case where both operands 3684 // are pointing to elements of an array. 3685 return Builder.CreateExactSDiv(diffInChars, divisor, "sub.ptr.div"); 3686 } 3687 3688 Value *ScalarExprEmitter::GetWidthMinusOneValue(Value* LHS,Value* RHS) { 3689 llvm::IntegerType *Ty; 3690 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(LHS->getType())) 3691 Ty = cast<llvm::IntegerType>(VT->getElementType()); 3692 else 3693 Ty = cast<llvm::IntegerType>(LHS->getType()); 3694 return llvm::ConstantInt::get(RHS->getType(), Ty->getBitWidth() - 1); 3695 } 3696 3697 Value *ScalarExprEmitter::ConstrainShiftValue(Value *LHS, Value *RHS, 3698 const Twine &Name) { 3699 llvm::IntegerType *Ty; 3700 if (auto *VT = dyn_cast<llvm::VectorType>(LHS->getType())) 3701 Ty = cast<llvm::IntegerType>(VT->getElementType()); 3702 else 3703 Ty = cast<llvm::IntegerType>(LHS->getType()); 3704 3705 if (llvm::isPowerOf2_64(Ty->getBitWidth())) 3706 return Builder.CreateAnd(RHS, GetWidthMinusOneValue(LHS, RHS), Name); 3707 3708 return Builder.CreateURem( 3709 RHS, llvm::ConstantInt::get(RHS->getType(), Ty->getBitWidth()), Name); 3710 } 3711 3712 Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { 3713 // TODO: This misses out on the sanitizer check below. 3714 if (Ops.isFixedPointOp()) 3715 return EmitFixedPointBinOp(Ops); 3716 3717 // LLVM requires the LHS and RHS to be the same type: promote or truncate the 3718 // RHS to the same size as the LHS. 3719 Value *RHS = Ops.RHS; 3720 if (Ops.LHS->getType() != RHS->getType()) 3721 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); 3722 3723 bool SanitizeSignedBase = CGF.SanOpts.has(SanitizerKind::ShiftBase) && 3724 Ops.Ty->hasSignedIntegerRepresentation() && 3725 !CGF.getLangOpts().isSignedOverflowDefined() && 3726 !CGF.getLangOpts().CPlusPlus20; 3727 bool SanitizeUnsignedBase = 3728 CGF.SanOpts.has(SanitizerKind::UnsignedShiftBase) && 3729 Ops.Ty->hasUnsignedIntegerRepresentation(); 3730 bool SanitizeBase = SanitizeSignedBase || SanitizeUnsignedBase; 3731 bool SanitizeExponent = CGF.SanOpts.has(SanitizerKind::ShiftExponent); 3732 // OpenCL 6.3j: shift values are effectively % word size of LHS. 3733 if (CGF.getLangOpts().OpenCL) 3734 RHS = ConstrainShiftValue(Ops.LHS, RHS, "shl.mask"); 3735 else if ((SanitizeBase || SanitizeExponent) && 3736 isa<llvm::IntegerType>(Ops.LHS->getType())) { 3737 CodeGenFunction::SanitizerScope SanScope(&CGF); 3738 SmallVector<std::pair<Value *, SanitizerMask>, 2> Checks; 3739 llvm::Value *WidthMinusOne = GetWidthMinusOneValue(Ops.LHS, Ops.RHS); 3740 llvm::Value *ValidExponent = Builder.CreateICmpULE(Ops.RHS, WidthMinusOne); 3741 3742 if (SanitizeExponent) { 3743 Checks.push_back( 3744 std::make_pair(ValidExponent, SanitizerKind::ShiftExponent)); 3745 } 3746 3747 if (SanitizeBase) { 3748 // Check whether we are shifting any non-zero bits off the top of the 3749 // integer. We only emit this check if exponent is valid - otherwise 3750 // instructions below will have undefined behavior themselves. 3751 llvm::BasicBlock *Orig = Builder.GetInsertBlock(); 3752 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); 3753 llvm::BasicBlock *CheckShiftBase = CGF.createBasicBlock("check"); 3754 Builder.CreateCondBr(ValidExponent, CheckShiftBase, Cont); 3755 llvm::Value *PromotedWidthMinusOne = 3756 (RHS == Ops.RHS) ? WidthMinusOne 3757 : GetWidthMinusOneValue(Ops.LHS, RHS); 3758 CGF.EmitBlock(CheckShiftBase); 3759 llvm::Value *BitsShiftedOff = Builder.CreateLShr( 3760 Ops.LHS, Builder.CreateSub(PromotedWidthMinusOne, RHS, "shl.zeros", 3761 /*NUW*/ true, /*NSW*/ true), 3762 "shl.check"); 3763 if (SanitizeUnsignedBase || CGF.getLangOpts().CPlusPlus) { 3764 // In C99, we are not permitted to shift a 1 bit into the sign bit. 3765 // Under C++11's rules, shifting a 1 bit into the sign bit is 3766 // OK, but shifting a 1 bit out of it is not. (C89 and C++03 don't 3767 // define signed left shifts, so we use the C99 and C++11 rules there). 3768 // Unsigned shifts can always shift into the top bit. 3769 llvm::Value *One = llvm::ConstantInt::get(BitsShiftedOff->getType(), 1); 3770 BitsShiftedOff = Builder.CreateLShr(BitsShiftedOff, One); 3771 } 3772 llvm::Value *Zero = llvm::ConstantInt::get(BitsShiftedOff->getType(), 0); 3773 llvm::Value *ValidBase = Builder.CreateICmpEQ(BitsShiftedOff, Zero); 3774 CGF.EmitBlock(Cont); 3775 llvm::PHINode *BaseCheck = Builder.CreatePHI(ValidBase->getType(), 2); 3776 BaseCheck->addIncoming(Builder.getTrue(), Orig); 3777 BaseCheck->addIncoming(ValidBase, CheckShiftBase); 3778 Checks.push_back(std::make_pair( 3779 BaseCheck, SanitizeSignedBase ? SanitizerKind::ShiftBase 3780 : SanitizerKind::UnsignedShiftBase)); 3781 } 3782 3783 assert(!Checks.empty()); 3784 EmitBinOpCheck(Checks, Ops); 3785 } 3786 3787 return Builder.CreateShl(Ops.LHS, RHS, "shl"); 3788 } 3789 3790 Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { 3791 // TODO: This misses out on the sanitizer check below. 3792 if (Ops.isFixedPointOp()) 3793 return EmitFixedPointBinOp(Ops); 3794 3795 // LLVM requires the LHS and RHS to be the same type: promote or truncate the 3796 // RHS to the same size as the LHS. 3797 Value *RHS = Ops.RHS; 3798 if (Ops.LHS->getType() != RHS->getType()) 3799 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); 3800 3801 // OpenCL 6.3j: shift values are effectively % word size of LHS. 3802 if (CGF.getLangOpts().OpenCL) 3803 RHS = ConstrainShiftValue(Ops.LHS, RHS, "shr.mask"); 3804 else if (CGF.SanOpts.has(SanitizerKind::ShiftExponent) && 3805 isa<llvm::IntegerType>(Ops.LHS->getType())) { 3806 CodeGenFunction::SanitizerScope SanScope(&CGF); 3807 llvm::Value *Valid = 3808 Builder.CreateICmpULE(RHS, GetWidthMinusOneValue(Ops.LHS, RHS)); 3809 EmitBinOpCheck(std::make_pair(Valid, SanitizerKind::ShiftExponent), Ops); 3810 } 3811 3812 if (Ops.Ty->hasUnsignedIntegerRepresentation()) 3813 return Builder.CreateLShr(Ops.LHS, RHS, "shr"); 3814 return Builder.CreateAShr(Ops.LHS, RHS, "shr"); 3815 } 3816 3817 enum IntrinsicType { VCMPEQ, VCMPGT }; 3818 // return corresponding comparison intrinsic for given vector type 3819 static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT, 3820 BuiltinType::Kind ElemKind) { 3821 switch (ElemKind) { 3822 default: llvm_unreachable("unexpected element type"); 3823 case BuiltinType::Char_U: 3824 case BuiltinType::UChar: 3825 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p : 3826 llvm::Intrinsic::ppc_altivec_vcmpgtub_p; 3827 case BuiltinType::Char_S: 3828 case BuiltinType::SChar: 3829 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p : 3830 llvm::Intrinsic::ppc_altivec_vcmpgtsb_p; 3831 case BuiltinType::UShort: 3832 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p : 3833 llvm::Intrinsic::ppc_altivec_vcmpgtuh_p; 3834 case BuiltinType::Short: 3835 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p : 3836 llvm::Intrinsic::ppc_altivec_vcmpgtsh_p; 3837 case BuiltinType::UInt: 3838 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p : 3839 llvm::Intrinsic::ppc_altivec_vcmpgtuw_p; 3840 case BuiltinType::Int: 3841 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p : 3842 llvm::Intrinsic::ppc_altivec_vcmpgtsw_p; 3843 case BuiltinType::ULong: 3844 case BuiltinType::ULongLong: 3845 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequd_p : 3846 llvm::Intrinsic::ppc_altivec_vcmpgtud_p; 3847 case BuiltinType::Long: 3848 case BuiltinType::LongLong: 3849 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequd_p : 3850 llvm::Intrinsic::ppc_altivec_vcmpgtsd_p; 3851 case BuiltinType::Float: 3852 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpeqfp_p : 3853 llvm::Intrinsic::ppc_altivec_vcmpgtfp_p; 3854 case BuiltinType::Double: 3855 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_vsx_xvcmpeqdp_p : 3856 llvm::Intrinsic::ppc_vsx_xvcmpgtdp_p; 3857 } 3858 } 3859 3860 Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E, 3861 llvm::CmpInst::Predicate UICmpOpc, 3862 llvm::CmpInst::Predicate SICmpOpc, 3863 llvm::CmpInst::Predicate FCmpOpc, 3864 bool IsSignaling) { 3865 TestAndClearIgnoreResultAssign(); 3866 Value *Result; 3867 QualType LHSTy = E->getLHS()->getType(); 3868 QualType RHSTy = E->getRHS()->getType(); 3869 if (const MemberPointerType *MPT = LHSTy->getAs<MemberPointerType>()) { 3870 assert(E->getOpcode() == BO_EQ || 3871 E->getOpcode() == BO_NE); 3872 Value *LHS = CGF.EmitScalarExpr(E->getLHS()); 3873 Value *RHS = CGF.EmitScalarExpr(E->getRHS()); 3874 Result = CGF.CGM.getCXXABI().EmitMemberPointerComparison( 3875 CGF, LHS, RHS, MPT, E->getOpcode() == BO_NE); 3876 } else if (!LHSTy->isAnyComplexType() && !RHSTy->isAnyComplexType()) { 3877 BinOpInfo BOInfo = EmitBinOps(E); 3878 Value *LHS = BOInfo.LHS; 3879 Value *RHS = BOInfo.RHS; 3880 3881 // If AltiVec, the comparison results in a numeric type, so we use 3882 // intrinsics comparing vectors and giving 0 or 1 as a result 3883 if (LHSTy->isVectorType() && !E->getType()->isVectorType()) { 3884 // constants for mapping CR6 register bits to predicate result 3885 enum { CR6_EQ=0, CR6_EQ_REV, CR6_LT, CR6_LT_REV } CR6; 3886 3887 llvm::Intrinsic::ID ID = llvm::Intrinsic::not_intrinsic; 3888 3889 // in several cases vector arguments order will be reversed 3890 Value *FirstVecArg = LHS, 3891 *SecondVecArg = RHS; 3892 3893 QualType ElTy = LHSTy->castAs<VectorType>()->getElementType(); 3894 BuiltinType::Kind ElementKind = ElTy->castAs<BuiltinType>()->getKind(); 3895 3896 switch(E->getOpcode()) { 3897 default: llvm_unreachable("is not a comparison operation"); 3898 case BO_EQ: 3899 CR6 = CR6_LT; 3900 ID = GetIntrinsic(VCMPEQ, ElementKind); 3901 break; 3902 case BO_NE: 3903 CR6 = CR6_EQ; 3904 ID = GetIntrinsic(VCMPEQ, ElementKind); 3905 break; 3906 case BO_LT: 3907 CR6 = CR6_LT; 3908 ID = GetIntrinsic(VCMPGT, ElementKind); 3909 std::swap(FirstVecArg, SecondVecArg); 3910 break; 3911 case BO_GT: 3912 CR6 = CR6_LT; 3913 ID = GetIntrinsic(VCMPGT, ElementKind); 3914 break; 3915 case BO_LE: 3916 if (ElementKind == BuiltinType::Float) { 3917 CR6 = CR6_LT; 3918 ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p; 3919 std::swap(FirstVecArg, SecondVecArg); 3920 } 3921 else { 3922 CR6 = CR6_EQ; 3923 ID = GetIntrinsic(VCMPGT, ElementKind); 3924 } 3925 break; 3926 case BO_GE: 3927 if (ElementKind == BuiltinType::Float) { 3928 CR6 = CR6_LT; 3929 ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p; 3930 } 3931 else { 3932 CR6 = CR6_EQ; 3933 ID = GetIntrinsic(VCMPGT, ElementKind); 3934 std::swap(FirstVecArg, SecondVecArg); 3935 } 3936 break; 3937 } 3938 3939 Value *CR6Param = Builder.getInt32(CR6); 3940 llvm::Function *F = CGF.CGM.getIntrinsic(ID); 3941 Result = Builder.CreateCall(F, {CR6Param, FirstVecArg, SecondVecArg}); 3942 3943 // The result type of intrinsic may not be same as E->getType(). 3944 // If E->getType() is not BoolTy, EmitScalarConversion will do the 3945 // conversion work. If E->getType() is BoolTy, EmitScalarConversion will 3946 // do nothing, if ResultTy is not i1 at the same time, it will cause 3947 // crash later. 3948 llvm::IntegerType *ResultTy = cast<llvm::IntegerType>(Result->getType()); 3949 if (ResultTy->getBitWidth() > 1 && 3950 E->getType() == CGF.getContext().BoolTy) 3951 Result = Builder.CreateTrunc(Result, Builder.getInt1Ty()); 3952 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType(), 3953 E->getExprLoc()); 3954 } 3955 3956 if (BOInfo.isFixedPointOp()) { 3957 Result = EmitFixedPointBinOp(BOInfo); 3958 } else if (LHS->getType()->isFPOrFPVectorTy()) { 3959 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, BOInfo.FPFeatures); 3960 if (!IsSignaling) 3961 Result = Builder.CreateFCmp(FCmpOpc, LHS, RHS, "cmp"); 3962 else 3963 Result = Builder.CreateFCmpS(FCmpOpc, LHS, RHS, "cmp"); 3964 } else if (LHSTy->hasSignedIntegerRepresentation()) { 3965 Result = Builder.CreateICmp(SICmpOpc, LHS, RHS, "cmp"); 3966 } else { 3967 // Unsigned integers and pointers. 3968 3969 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers && 3970 !isa<llvm::ConstantPointerNull>(LHS) && 3971 !isa<llvm::ConstantPointerNull>(RHS)) { 3972 3973 // Dynamic information is required to be stripped for comparisons, 3974 // because it could leak the dynamic information. Based on comparisons 3975 // of pointers to dynamic objects, the optimizer can replace one pointer 3976 // with another, which might be incorrect in presence of invariant 3977 // groups. Comparison with null is safe because null does not carry any 3978 // dynamic information. 3979 if (LHSTy.mayBeDynamicClass()) 3980 LHS = Builder.CreateStripInvariantGroup(LHS); 3981 if (RHSTy.mayBeDynamicClass()) 3982 RHS = Builder.CreateStripInvariantGroup(RHS); 3983 } 3984 3985 Result = Builder.CreateICmp(UICmpOpc, LHS, RHS, "cmp"); 3986 } 3987 3988 // If this is a vector comparison, sign extend the result to the appropriate 3989 // vector integer type and return it (don't convert to bool). 3990 if (LHSTy->isVectorType()) 3991 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); 3992 3993 } else { 3994 // Complex Comparison: can only be an equality comparison. 3995 CodeGenFunction::ComplexPairTy LHS, RHS; 3996 QualType CETy; 3997 if (auto *CTy = LHSTy->getAs<ComplexType>()) { 3998 LHS = CGF.EmitComplexExpr(E->getLHS()); 3999 CETy = CTy->getElementType(); 4000 } else { 4001 LHS.first = Visit(E->getLHS()); 4002 LHS.second = llvm::Constant::getNullValue(LHS.first->getType()); 4003 CETy = LHSTy; 4004 } 4005 if (auto *CTy = RHSTy->getAs<ComplexType>()) { 4006 RHS = CGF.EmitComplexExpr(E->getRHS()); 4007 assert(CGF.getContext().hasSameUnqualifiedType(CETy, 4008 CTy->getElementType()) && 4009 "The element types must always match."); 4010 (void)CTy; 4011 } else { 4012 RHS.first = Visit(E->getRHS()); 4013 RHS.second = llvm::Constant::getNullValue(RHS.first->getType()); 4014 assert(CGF.getContext().hasSameUnqualifiedType(CETy, RHSTy) && 4015 "The element types must always match."); 4016 } 4017 4018 Value *ResultR, *ResultI; 4019 if (CETy->isRealFloatingType()) { 4020 // As complex comparisons can only be equality comparisons, they 4021 // are never signaling comparisons. 4022 ResultR = Builder.CreateFCmp(FCmpOpc, LHS.first, RHS.first, "cmp.r"); 4023 ResultI = Builder.CreateFCmp(FCmpOpc, LHS.second, RHS.second, "cmp.i"); 4024 } else { 4025 // Complex comparisons can only be equality comparisons. As such, signed 4026 // and unsigned opcodes are the same. 4027 ResultR = Builder.CreateICmp(UICmpOpc, LHS.first, RHS.first, "cmp.r"); 4028 ResultI = Builder.CreateICmp(UICmpOpc, LHS.second, RHS.second, "cmp.i"); 4029 } 4030 4031 if (E->getOpcode() == BO_EQ) { 4032 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); 4033 } else { 4034 assert(E->getOpcode() == BO_NE && 4035 "Complex comparison other than == or != ?"); 4036 Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); 4037 } 4038 } 4039 4040 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType(), 4041 E->getExprLoc()); 4042 } 4043 4044 Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { 4045 bool Ignore = TestAndClearIgnoreResultAssign(); 4046 4047 Value *RHS; 4048 LValue LHS; 4049 4050 switch (E->getLHS()->getType().getObjCLifetime()) { 4051 case Qualifiers::OCL_Strong: 4052 std::tie(LHS, RHS) = CGF.EmitARCStoreStrong(E, Ignore); 4053 break; 4054 4055 case Qualifiers::OCL_Autoreleasing: 4056 std::tie(LHS, RHS) = CGF.EmitARCStoreAutoreleasing(E); 4057 break; 4058 4059 case Qualifiers::OCL_ExplicitNone: 4060 std::tie(LHS, RHS) = CGF.EmitARCStoreUnsafeUnretained(E, Ignore); 4061 break; 4062 4063 case Qualifiers::OCL_Weak: 4064 RHS = Visit(E->getRHS()); 4065 LHS = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store); 4066 RHS = CGF.EmitARCStoreWeak(LHS.getAddress(CGF), RHS, Ignore); 4067 break; 4068 4069 case Qualifiers::OCL_None: 4070 // __block variables need to have the rhs evaluated first, plus 4071 // this should improve codegen just a little. 4072 RHS = Visit(E->getRHS()); 4073 LHS = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store); 4074 4075 // Store the value into the LHS. Bit-fields are handled specially 4076 // because the result is altered by the store, i.e., [C99 6.5.16p1] 4077 // 'An assignment expression has the value of the left operand after 4078 // the assignment...'. 4079 if (LHS.isBitField()) { 4080 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, &RHS); 4081 } else { 4082 CGF.EmitNullabilityCheck(LHS, RHS, E->getExprLoc()); 4083 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS); 4084 } 4085 } 4086 4087 // If the result is clearly ignored, return now. 4088 if (Ignore) 4089 return nullptr; 4090 4091 // The result of an assignment in C is the assigned r-value. 4092 if (!CGF.getLangOpts().CPlusPlus) 4093 return RHS; 4094 4095 // If the lvalue is non-volatile, return the computed value of the assignment. 4096 if (!LHS.isVolatileQualified()) 4097 return RHS; 4098 4099 // Otherwise, reload the value. 4100 return EmitLoadOfLValue(LHS, E->getExprLoc()); 4101 } 4102 4103 Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { 4104 // Perform vector logical and on comparisons with zero vectors. 4105 if (E->getType()->isVectorType()) { 4106 CGF.incrementProfileCounter(E); 4107 4108 Value *LHS = Visit(E->getLHS()); 4109 Value *RHS = Visit(E->getRHS()); 4110 Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType()); 4111 if (LHS->getType()->isFPOrFPVectorTy()) { 4112 CodeGenFunction::CGFPOptionsRAII FPOptsRAII( 4113 CGF, E->getFPFeaturesInEffect(CGF.getLangOpts())); 4114 LHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero, "cmp"); 4115 RHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero, "cmp"); 4116 } else { 4117 LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp"); 4118 RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp"); 4119 } 4120 Value *And = Builder.CreateAnd(LHS, RHS); 4121 return Builder.CreateSExt(And, ConvertType(E->getType()), "sext"); 4122 } 4123 4124 llvm::Type *ResTy = ConvertType(E->getType()); 4125 4126 // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. 4127 // If we have 1 && X, just emit X without inserting the control flow. 4128 bool LHSCondVal; 4129 if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) { 4130 if (LHSCondVal) { // If we have 1 && X, just emit X. 4131 CGF.incrementProfileCounter(E); 4132 4133 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 4134 // ZExt result to int or bool. 4135 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext"); 4136 } 4137 4138 // 0 && RHS: If it is safe, just elide the RHS, and return 0/false. 4139 if (!CGF.ContainsLabel(E->getRHS())) 4140 return llvm::Constant::getNullValue(ResTy); 4141 } 4142 4143 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); 4144 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); 4145 4146 CodeGenFunction::ConditionalEvaluation eval(CGF); 4147 4148 // Branch on the LHS first. If it is false, go to the failure (cont) block. 4149 CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock, 4150 CGF.getProfileCount(E->getRHS())); 4151 4152 // Any edges into the ContBlock are now from an (indeterminate number of) 4153 // edges from this first condition. All of these values will be false. Start 4154 // setting up the PHI node in the Cont Block for this. 4155 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2, 4156 "", ContBlock); 4157 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); 4158 PI != PE; ++PI) 4159 PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI); 4160 4161 eval.begin(CGF); 4162 CGF.EmitBlock(RHSBlock); 4163 CGF.incrementProfileCounter(E); 4164 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 4165 eval.end(CGF); 4166 4167 // Reaquire the RHS block, as there may be subblocks inserted. 4168 RHSBlock = Builder.GetInsertBlock(); 4169 4170 // Emit an unconditional branch from this block to ContBlock. 4171 { 4172 // There is no need to emit line number for unconditional branch. 4173 auto NL = ApplyDebugLocation::CreateEmpty(CGF); 4174 CGF.EmitBlock(ContBlock); 4175 } 4176 // Insert an entry into the phi node for the edge with the value of RHSCond. 4177 PN->addIncoming(RHSCond, RHSBlock); 4178 4179 // Artificial location to preserve the scope information 4180 { 4181 auto NL = ApplyDebugLocation::CreateArtificial(CGF); 4182 PN->setDebugLoc(Builder.getCurrentDebugLocation()); 4183 } 4184 4185 // ZExt result to int. 4186 return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext"); 4187 } 4188 4189 Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { 4190 // Perform vector logical or on comparisons with zero vectors. 4191 if (E->getType()->isVectorType()) { 4192 CGF.incrementProfileCounter(E); 4193 4194 Value *LHS = Visit(E->getLHS()); 4195 Value *RHS = Visit(E->getRHS()); 4196 Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType()); 4197 if (LHS->getType()->isFPOrFPVectorTy()) { 4198 CodeGenFunction::CGFPOptionsRAII FPOptsRAII( 4199 CGF, E->getFPFeaturesInEffect(CGF.getLangOpts())); 4200 LHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero, "cmp"); 4201 RHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero, "cmp"); 4202 } else { 4203 LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp"); 4204 RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp"); 4205 } 4206 Value *Or = Builder.CreateOr(LHS, RHS); 4207 return Builder.CreateSExt(Or, ConvertType(E->getType()), "sext"); 4208 } 4209 4210 llvm::Type *ResTy = ConvertType(E->getType()); 4211 4212 // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. 4213 // If we have 0 || X, just emit X without inserting the control flow. 4214 bool LHSCondVal; 4215 if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) { 4216 if (!LHSCondVal) { // If we have 0 || X, just emit X. 4217 CGF.incrementProfileCounter(E); 4218 4219 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 4220 // ZExt result to int or bool. 4221 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext"); 4222 } 4223 4224 // 1 || RHS: If it is safe, just elide the RHS, and return 1/true. 4225 if (!CGF.ContainsLabel(E->getRHS())) 4226 return llvm::ConstantInt::get(ResTy, 1); 4227 } 4228 4229 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); 4230 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); 4231 4232 CodeGenFunction::ConditionalEvaluation eval(CGF); 4233 4234 // Branch on the LHS first. If it is true, go to the success (cont) block. 4235 CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock, 4236 CGF.getCurrentProfileCount() - 4237 CGF.getProfileCount(E->getRHS())); 4238 4239 // Any edges into the ContBlock are now from an (indeterminate number of) 4240 // edges from this first condition. All of these values will be true. Start 4241 // setting up the PHI node in the Cont Block for this. 4242 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2, 4243 "", ContBlock); 4244 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); 4245 PI != PE; ++PI) 4246 PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI); 4247 4248 eval.begin(CGF); 4249 4250 // Emit the RHS condition as a bool value. 4251 CGF.EmitBlock(RHSBlock); 4252 CGF.incrementProfileCounter(E); 4253 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 4254 4255 eval.end(CGF); 4256 4257 // Reaquire the RHS block, as there may be subblocks inserted. 4258 RHSBlock = Builder.GetInsertBlock(); 4259 4260 // Emit an unconditional branch from this block to ContBlock. Insert an entry 4261 // into the phi node for the edge with the value of RHSCond. 4262 CGF.EmitBlock(ContBlock); 4263 PN->addIncoming(RHSCond, RHSBlock); 4264 4265 // ZExt result to int. 4266 return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext"); 4267 } 4268 4269 Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { 4270 CGF.EmitIgnoredExpr(E->getLHS()); 4271 CGF.EnsureInsertPoint(); 4272 return Visit(E->getRHS()); 4273 } 4274 4275 //===----------------------------------------------------------------------===// 4276 // Other Operators 4277 //===----------------------------------------------------------------------===// 4278 4279 /// isCheapEnoughToEvaluateUnconditionally - Return true if the specified 4280 /// expression is cheap enough and side-effect-free enough to evaluate 4281 /// unconditionally instead of conditionally. This is used to convert control 4282 /// flow into selects in some cases. 4283 static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E, 4284 CodeGenFunction &CGF) { 4285 // Anything that is an integer or floating point constant is fine. 4286 return E->IgnoreParens()->isEvaluatable(CGF.getContext()); 4287 4288 // Even non-volatile automatic variables can't be evaluated unconditionally. 4289 // Referencing a thread_local may cause non-trivial initialization work to 4290 // occur. If we're inside a lambda and one of the variables is from the scope 4291 // outside the lambda, that function may have returned already. Reading its 4292 // locals is a bad idea. Also, these reads may introduce races there didn't 4293 // exist in the source-level program. 4294 } 4295 4296 4297 Value *ScalarExprEmitter:: 4298 VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 4299 TestAndClearIgnoreResultAssign(); 4300 4301 // Bind the common expression if necessary. 4302 CodeGenFunction::OpaqueValueMapping binding(CGF, E); 4303 4304 Expr *condExpr = E->getCond(); 4305 Expr *lhsExpr = E->getTrueExpr(); 4306 Expr *rhsExpr = E->getFalseExpr(); 4307 4308 // If the condition constant folds and can be elided, try to avoid emitting 4309 // the condition and the dead arm. 4310 bool CondExprBool; 4311 if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) { 4312 Expr *live = lhsExpr, *dead = rhsExpr; 4313 if (!CondExprBool) std::swap(live, dead); 4314 4315 // If the dead side doesn't have labels we need, just emit the Live part. 4316 if (!CGF.ContainsLabel(dead)) { 4317 if (CondExprBool) 4318 CGF.incrementProfileCounter(E); 4319 Value *Result = Visit(live); 4320 4321 // If the live part is a throw expression, it acts like it has a void 4322 // type, so evaluating it returns a null Value*. However, a conditional 4323 // with non-void type must return a non-null Value*. 4324 if (!Result && !E->getType()->isVoidType()) 4325 Result = llvm::UndefValue::get(CGF.ConvertType(E->getType())); 4326 4327 return Result; 4328 } 4329 } 4330 4331 // OpenCL: If the condition is a vector, we can treat this condition like 4332 // the select function. 4333 if ((CGF.getLangOpts().OpenCL && condExpr->getType()->isVectorType()) || 4334 condExpr->getType()->isExtVectorType()) { 4335 CGF.incrementProfileCounter(E); 4336 4337 llvm::Value *CondV = CGF.EmitScalarExpr(condExpr); 4338 llvm::Value *LHS = Visit(lhsExpr); 4339 llvm::Value *RHS = Visit(rhsExpr); 4340 4341 llvm::Type *condType = ConvertType(condExpr->getType()); 4342 auto *vecTy = cast<llvm::FixedVectorType>(condType); 4343 4344 unsigned numElem = vecTy->getNumElements(); 4345 llvm::Type *elemType = vecTy->getElementType(); 4346 4347 llvm::Value *zeroVec = llvm::Constant::getNullValue(vecTy); 4348 llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec); 4349 llvm::Value *tmp = Builder.CreateSExt( 4350 TestMSB, llvm::FixedVectorType::get(elemType, numElem), "sext"); 4351 llvm::Value *tmp2 = Builder.CreateNot(tmp); 4352 4353 // Cast float to int to perform ANDs if necessary. 4354 llvm::Value *RHSTmp = RHS; 4355 llvm::Value *LHSTmp = LHS; 4356 bool wasCast = false; 4357 llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType()); 4358 if (rhsVTy->getElementType()->isFloatingPointTy()) { 4359 RHSTmp = Builder.CreateBitCast(RHS, tmp2->getType()); 4360 LHSTmp = Builder.CreateBitCast(LHS, tmp->getType()); 4361 wasCast = true; 4362 } 4363 4364 llvm::Value *tmp3 = Builder.CreateAnd(RHSTmp, tmp2); 4365 llvm::Value *tmp4 = Builder.CreateAnd(LHSTmp, tmp); 4366 llvm::Value *tmp5 = Builder.CreateOr(tmp3, tmp4, "cond"); 4367 if (wasCast) 4368 tmp5 = Builder.CreateBitCast(tmp5, RHS->getType()); 4369 4370 return tmp5; 4371 } 4372 4373 if (condExpr->getType()->isVectorType()) { 4374 CGF.incrementProfileCounter(E); 4375 4376 llvm::Value *CondV = CGF.EmitScalarExpr(condExpr); 4377 llvm::Value *LHS = Visit(lhsExpr); 4378 llvm::Value *RHS = Visit(rhsExpr); 4379 4380 llvm::Type *CondType = ConvertType(condExpr->getType()); 4381 auto *VecTy = cast<llvm::VectorType>(CondType); 4382 llvm::Value *ZeroVec = llvm::Constant::getNullValue(VecTy); 4383 4384 CondV = Builder.CreateICmpNE(CondV, ZeroVec, "vector_cond"); 4385 return Builder.CreateSelect(CondV, LHS, RHS, "vector_select"); 4386 } 4387 4388 // If this is a really simple expression (like x ? 4 : 5), emit this as a 4389 // select instead of as control flow. We can only do this if it is cheap and 4390 // safe to evaluate the LHS and RHS unconditionally. 4391 if (isCheapEnoughToEvaluateUnconditionally(lhsExpr, CGF) && 4392 isCheapEnoughToEvaluateUnconditionally(rhsExpr, CGF)) { 4393 llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr); 4394 llvm::Value *StepV = Builder.CreateZExtOrBitCast(CondV, CGF.Int64Ty); 4395 4396 CGF.incrementProfileCounter(E, StepV); 4397 4398 llvm::Value *LHS = Visit(lhsExpr); 4399 llvm::Value *RHS = Visit(rhsExpr); 4400 if (!LHS) { 4401 // If the conditional has void type, make sure we return a null Value*. 4402 assert(!RHS && "LHS and RHS types must match"); 4403 return nullptr; 4404 } 4405 return Builder.CreateSelect(CondV, LHS, RHS, "cond"); 4406 } 4407 4408 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 4409 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 4410 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 4411 4412 CodeGenFunction::ConditionalEvaluation eval(CGF); 4413 CGF.EmitBranchOnBoolExpr(condExpr, LHSBlock, RHSBlock, 4414 CGF.getProfileCount(lhsExpr)); 4415 4416 CGF.EmitBlock(LHSBlock); 4417 CGF.incrementProfileCounter(E); 4418 eval.begin(CGF); 4419 Value *LHS = Visit(lhsExpr); 4420 eval.end(CGF); 4421 4422 LHSBlock = Builder.GetInsertBlock(); 4423 Builder.CreateBr(ContBlock); 4424 4425 CGF.EmitBlock(RHSBlock); 4426 eval.begin(CGF); 4427 Value *RHS = Visit(rhsExpr); 4428 eval.end(CGF); 4429 4430 RHSBlock = Builder.GetInsertBlock(); 4431 CGF.EmitBlock(ContBlock); 4432 4433 // If the LHS or RHS is a throw expression, it will be legitimately null. 4434 if (!LHS) 4435 return RHS; 4436 if (!RHS) 4437 return LHS; 4438 4439 // Create a PHI node for the real part. 4440 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), 2, "cond"); 4441 PN->addIncoming(LHS, LHSBlock); 4442 PN->addIncoming(RHS, RHSBlock); 4443 return PN; 4444 } 4445 4446 Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { 4447 return Visit(E->getChosenSubExpr()); 4448 } 4449 4450 Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 4451 QualType Ty = VE->getType(); 4452 4453 if (Ty->isVariablyModifiedType()) 4454 CGF.EmitVariablyModifiedType(Ty); 4455 4456 Address ArgValue = Address::invalid(); 4457 Address ArgPtr = CGF.EmitVAArg(VE, ArgValue); 4458 4459 llvm::Type *ArgTy = ConvertType(VE->getType()); 4460 4461 // If EmitVAArg fails, emit an error. 4462 if (!ArgPtr.isValid()) { 4463 CGF.ErrorUnsupported(VE, "va_arg expression"); 4464 return llvm::UndefValue::get(ArgTy); 4465 } 4466 4467 // FIXME Volatility. 4468 llvm::Value *Val = Builder.CreateLoad(ArgPtr); 4469 4470 // If EmitVAArg promoted the type, we must truncate it. 4471 if (ArgTy != Val->getType()) { 4472 if (ArgTy->isPointerTy() && !Val->getType()->isPointerTy()) 4473 Val = Builder.CreateIntToPtr(Val, ArgTy); 4474 else 4475 Val = Builder.CreateTrunc(Val, ArgTy); 4476 } 4477 4478 return Val; 4479 } 4480 4481 Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *block) { 4482 return CGF.EmitBlockLiteral(block); 4483 } 4484 4485 // Convert a vec3 to vec4, or vice versa. 4486 static Value *ConvertVec3AndVec4(CGBuilderTy &Builder, CodeGenFunction &CGF, 4487 Value *Src, unsigned NumElementsDst) { 4488 llvm::Value *UnV = llvm::UndefValue::get(Src->getType()); 4489 static constexpr int Mask[] = {0, 1, 2, -1}; 4490 return Builder.CreateShuffleVector(Src, UnV, 4491 llvm::makeArrayRef(Mask, NumElementsDst)); 4492 } 4493 4494 // Create cast instructions for converting LLVM value \p Src to LLVM type \p 4495 // DstTy. \p Src has the same size as \p DstTy. Both are single value types 4496 // but could be scalar or vectors of different lengths, and either can be 4497 // pointer. 4498 // There are 4 cases: 4499 // 1. non-pointer -> non-pointer : needs 1 bitcast 4500 // 2. pointer -> pointer : needs 1 bitcast or addrspacecast 4501 // 3. pointer -> non-pointer 4502 // a) pointer -> intptr_t : needs 1 ptrtoint 4503 // b) pointer -> non-intptr_t : needs 1 ptrtoint then 1 bitcast 4504 // 4. non-pointer -> pointer 4505 // a) intptr_t -> pointer : needs 1 inttoptr 4506 // b) non-intptr_t -> pointer : needs 1 bitcast then 1 inttoptr 4507 // Note: for cases 3b and 4b two casts are required since LLVM casts do not 4508 // allow casting directly between pointer types and non-integer non-pointer 4509 // types. 4510 static Value *createCastsForTypeOfSameSize(CGBuilderTy &Builder, 4511 const llvm::DataLayout &DL, 4512 Value *Src, llvm::Type *DstTy, 4513 StringRef Name = "") { 4514 auto SrcTy = Src->getType(); 4515 4516 // Case 1. 4517 if (!SrcTy->isPointerTy() && !DstTy->isPointerTy()) 4518 return Builder.CreateBitCast(Src, DstTy, Name); 4519 4520 // Case 2. 4521 if (SrcTy->isPointerTy() && DstTy->isPointerTy()) 4522 return Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DstTy, Name); 4523 4524 // Case 3. 4525 if (SrcTy->isPointerTy() && !DstTy->isPointerTy()) { 4526 // Case 3b. 4527 if (!DstTy->isIntegerTy()) 4528 Src = Builder.CreatePtrToInt(Src, DL.getIntPtrType(SrcTy)); 4529 // Cases 3a and 3b. 4530 return Builder.CreateBitOrPointerCast(Src, DstTy, Name); 4531 } 4532 4533 // Case 4b. 4534 if (!SrcTy->isIntegerTy()) 4535 Src = Builder.CreateBitCast(Src, DL.getIntPtrType(DstTy)); 4536 // Cases 4a and 4b. 4537 return Builder.CreateIntToPtr(Src, DstTy, Name); 4538 } 4539 4540 Value *ScalarExprEmitter::VisitAsTypeExpr(AsTypeExpr *E) { 4541 Value *Src = CGF.EmitScalarExpr(E->getSrcExpr()); 4542 llvm::Type *DstTy = ConvertType(E->getType()); 4543 4544 llvm::Type *SrcTy = Src->getType(); 4545 unsigned NumElementsSrc = 4546 isa<llvm::VectorType>(SrcTy) 4547 ? cast<llvm::FixedVectorType>(SrcTy)->getNumElements() 4548 : 0; 4549 unsigned NumElementsDst = 4550 isa<llvm::VectorType>(DstTy) 4551 ? cast<llvm::FixedVectorType>(DstTy)->getNumElements() 4552 : 0; 4553 4554 // Going from vec3 to non-vec3 is a special case and requires a shuffle 4555 // vector to get a vec4, then a bitcast if the target type is different. 4556 if (NumElementsSrc == 3 && NumElementsDst != 3) { 4557 Src = ConvertVec3AndVec4(Builder, CGF, Src, 4); 4558 4559 if (!CGF.CGM.getCodeGenOpts().PreserveVec3Type) { 4560 Src = createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(), Src, 4561 DstTy); 4562 } 4563 4564 Src->setName("astype"); 4565 return Src; 4566 } 4567 4568 // Going from non-vec3 to vec3 is a special case and requires a bitcast 4569 // to vec4 if the original type is not vec4, then a shuffle vector to 4570 // get a vec3. 4571 if (NumElementsSrc != 3 && NumElementsDst == 3) { 4572 if (!CGF.CGM.getCodeGenOpts().PreserveVec3Type) { 4573 auto *Vec4Ty = llvm::FixedVectorType::get( 4574 cast<llvm::VectorType>(DstTy)->getElementType(), 4); 4575 Src = createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(), Src, 4576 Vec4Ty); 4577 } 4578 4579 Src = ConvertVec3AndVec4(Builder, CGF, Src, 3); 4580 Src->setName("astype"); 4581 return Src; 4582 } 4583 4584 return createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(), 4585 Src, DstTy, "astype"); 4586 } 4587 4588 Value *ScalarExprEmitter::VisitAtomicExpr(AtomicExpr *E) { 4589 return CGF.EmitAtomicExpr(E).getScalarVal(); 4590 } 4591 4592 //===----------------------------------------------------------------------===// 4593 // Entry Point into this File 4594 //===----------------------------------------------------------------------===// 4595 4596 /// Emit the computation of the specified expression of scalar type, ignoring 4597 /// the result. 4598 Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) { 4599 assert(E && hasScalarEvaluationKind(E->getType()) && 4600 "Invalid scalar expression to emit"); 4601 4602 return ScalarExprEmitter(*this, IgnoreResultAssign) 4603 .Visit(const_cast<Expr *>(E)); 4604 } 4605 4606 /// Emit a conversion from the specified type to the specified destination type, 4607 /// both of which are LLVM scalar types. 4608 Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, 4609 QualType DstTy, 4610 SourceLocation Loc) { 4611 assert(hasScalarEvaluationKind(SrcTy) && hasScalarEvaluationKind(DstTy) && 4612 "Invalid scalar expression to emit"); 4613 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy, Loc); 4614 } 4615 4616 /// Emit a conversion from the specified complex type to the specified 4617 /// destination type, where the destination type is an LLVM scalar type. 4618 Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, 4619 QualType SrcTy, 4620 QualType DstTy, 4621 SourceLocation Loc) { 4622 assert(SrcTy->isAnyComplexType() && hasScalarEvaluationKind(DstTy) && 4623 "Invalid complex -> scalar conversion"); 4624 return ScalarExprEmitter(*this) 4625 .EmitComplexToScalarConversion(Src, SrcTy, DstTy, Loc); 4626 } 4627 4628 4629 llvm::Value *CodeGenFunction:: 4630 EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 4631 bool isInc, bool isPre) { 4632 return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre); 4633 } 4634 4635 LValue CodeGenFunction::EmitObjCIsaExpr(const ObjCIsaExpr *E) { 4636 // object->isa or (*object).isa 4637 // Generate code as for: *(Class*)object 4638 4639 Expr *BaseExpr = E->getBase(); 4640 Address Addr = Address::invalid(); 4641 if (BaseExpr->isRValue()) { 4642 Addr = Address(EmitScalarExpr(BaseExpr), getPointerAlign()); 4643 } else { 4644 Addr = EmitLValue(BaseExpr).getAddress(*this); 4645 } 4646 4647 // Cast the address to Class*. 4648 Addr = Builder.CreateElementBitCast(Addr, ConvertType(E->getType())); 4649 return MakeAddrLValue(Addr, E->getType()); 4650 } 4651 4652 4653 LValue CodeGenFunction::EmitCompoundAssignmentLValue( 4654 const CompoundAssignOperator *E) { 4655 ScalarExprEmitter Scalar(*this); 4656 Value *Result = nullptr; 4657 switch (E->getOpcode()) { 4658 #define COMPOUND_OP(Op) \ 4659 case BO_##Op##Assign: \ 4660 return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \ 4661 Result) 4662 COMPOUND_OP(Mul); 4663 COMPOUND_OP(Div); 4664 COMPOUND_OP(Rem); 4665 COMPOUND_OP(Add); 4666 COMPOUND_OP(Sub); 4667 COMPOUND_OP(Shl); 4668 COMPOUND_OP(Shr); 4669 COMPOUND_OP(And); 4670 COMPOUND_OP(Xor); 4671 COMPOUND_OP(Or); 4672 #undef COMPOUND_OP 4673 4674 case BO_PtrMemD: 4675 case BO_PtrMemI: 4676 case BO_Mul: 4677 case BO_Div: 4678 case BO_Rem: 4679 case BO_Add: 4680 case BO_Sub: 4681 case BO_Shl: 4682 case BO_Shr: 4683 case BO_LT: 4684 case BO_GT: 4685 case BO_LE: 4686 case BO_GE: 4687 case BO_EQ: 4688 case BO_NE: 4689 case BO_Cmp: 4690 case BO_And: 4691 case BO_Xor: 4692 case BO_Or: 4693 case BO_LAnd: 4694 case BO_LOr: 4695 case BO_Assign: 4696 case BO_Comma: 4697 llvm_unreachable("Not valid compound assignment operators"); 4698 } 4699 4700 llvm_unreachable("Unhandled compound assignment operator"); 4701 } 4702 4703 struct GEPOffsetAndOverflow { 4704 // The total (signed) byte offset for the GEP. 4705 llvm::Value *TotalOffset; 4706 // The offset overflow flag - true if the total offset overflows. 4707 llvm::Value *OffsetOverflows; 4708 }; 4709 4710 /// Evaluate given GEPVal, which is either an inbounds GEP, or a constant, 4711 /// and compute the total offset it applies from it's base pointer BasePtr. 4712 /// Returns offset in bytes and a boolean flag whether an overflow happened 4713 /// during evaluation. 4714 static GEPOffsetAndOverflow EmitGEPOffsetInBytes(Value *BasePtr, Value *GEPVal, 4715 llvm::LLVMContext &VMContext, 4716 CodeGenModule &CGM, 4717 CGBuilderTy &Builder) { 4718 const auto &DL = CGM.getDataLayout(); 4719 4720 // The total (signed) byte offset for the GEP. 4721 llvm::Value *TotalOffset = nullptr; 4722 4723 // Was the GEP already reduced to a constant? 4724 if (isa<llvm::Constant>(GEPVal)) { 4725 // Compute the offset by casting both pointers to integers and subtracting: 4726 // GEPVal = BasePtr + ptr(Offset) <--> Offset = int(GEPVal) - int(BasePtr) 4727 Value *BasePtr_int = 4728 Builder.CreatePtrToInt(BasePtr, DL.getIntPtrType(BasePtr->getType())); 4729 Value *GEPVal_int = 4730 Builder.CreatePtrToInt(GEPVal, DL.getIntPtrType(GEPVal->getType())); 4731 TotalOffset = Builder.CreateSub(GEPVal_int, BasePtr_int); 4732 return {TotalOffset, /*OffsetOverflows=*/Builder.getFalse()}; 4733 } 4734 4735 auto *GEP = cast<llvm::GEPOperator>(GEPVal); 4736 assert(GEP->getPointerOperand() == BasePtr && 4737 "BasePtr must be the the base of the GEP."); 4738 assert(GEP->isInBounds() && "Expected inbounds GEP"); 4739 4740 auto *IntPtrTy = DL.getIntPtrType(GEP->getPointerOperandType()); 4741 4742 // Grab references to the signed add/mul overflow intrinsics for intptr_t. 4743 auto *Zero = llvm::ConstantInt::getNullValue(IntPtrTy); 4744 auto *SAddIntrinsic = 4745 CGM.getIntrinsic(llvm::Intrinsic::sadd_with_overflow, IntPtrTy); 4746 auto *SMulIntrinsic = 4747 CGM.getIntrinsic(llvm::Intrinsic::smul_with_overflow, IntPtrTy); 4748 4749 // The offset overflow flag - true if the total offset overflows. 4750 llvm::Value *OffsetOverflows = Builder.getFalse(); 4751 4752 /// Return the result of the given binary operation. 4753 auto eval = [&](BinaryOperator::Opcode Opcode, llvm::Value *LHS, 4754 llvm::Value *RHS) -> llvm::Value * { 4755 assert((Opcode == BO_Add || Opcode == BO_Mul) && "Can't eval binop"); 4756 4757 // If the operands are constants, return a constant result. 4758 if (auto *LHSCI = dyn_cast<llvm::ConstantInt>(LHS)) { 4759 if (auto *RHSCI = dyn_cast<llvm::ConstantInt>(RHS)) { 4760 llvm::APInt N; 4761 bool HasOverflow = mayHaveIntegerOverflow(LHSCI, RHSCI, Opcode, 4762 /*Signed=*/true, N); 4763 if (HasOverflow) 4764 OffsetOverflows = Builder.getTrue(); 4765 return llvm::ConstantInt::get(VMContext, N); 4766 } 4767 } 4768 4769 // Otherwise, compute the result with checked arithmetic. 4770 auto *ResultAndOverflow = Builder.CreateCall( 4771 (Opcode == BO_Add) ? SAddIntrinsic : SMulIntrinsic, {LHS, RHS}); 4772 OffsetOverflows = Builder.CreateOr( 4773 Builder.CreateExtractValue(ResultAndOverflow, 1), OffsetOverflows); 4774 return Builder.CreateExtractValue(ResultAndOverflow, 0); 4775 }; 4776 4777 // Determine the total byte offset by looking at each GEP operand. 4778 for (auto GTI = llvm::gep_type_begin(GEP), GTE = llvm::gep_type_end(GEP); 4779 GTI != GTE; ++GTI) { 4780 llvm::Value *LocalOffset; 4781 auto *Index = GTI.getOperand(); 4782 // Compute the local offset contributed by this indexing step: 4783 if (auto *STy = GTI.getStructTypeOrNull()) { 4784 // For struct indexing, the local offset is the byte position of the 4785 // specified field. 4786 unsigned FieldNo = cast<llvm::ConstantInt>(Index)->getZExtValue(); 4787 LocalOffset = llvm::ConstantInt::get( 4788 IntPtrTy, DL.getStructLayout(STy)->getElementOffset(FieldNo)); 4789 } else { 4790 // Otherwise this is array-like indexing. The local offset is the index 4791 // multiplied by the element size. 4792 auto *ElementSize = llvm::ConstantInt::get( 4793 IntPtrTy, DL.getTypeAllocSize(GTI.getIndexedType())); 4794 auto *IndexS = Builder.CreateIntCast(Index, IntPtrTy, /*isSigned=*/true); 4795 LocalOffset = eval(BO_Mul, ElementSize, IndexS); 4796 } 4797 4798 // If this is the first offset, set it as the total offset. Otherwise, add 4799 // the local offset into the running total. 4800 if (!TotalOffset || TotalOffset == Zero) 4801 TotalOffset = LocalOffset; 4802 else 4803 TotalOffset = eval(BO_Add, TotalOffset, LocalOffset); 4804 } 4805 4806 return {TotalOffset, OffsetOverflows}; 4807 } 4808 4809 Value * 4810 CodeGenFunction::EmitCheckedInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList, 4811 bool SignedIndices, bool IsSubtraction, 4812 SourceLocation Loc, const Twine &Name) { 4813 Value *GEPVal = Builder.CreateInBoundsGEP(Ptr, IdxList, Name); 4814 4815 // If the pointer overflow sanitizer isn't enabled, do nothing. 4816 if (!SanOpts.has(SanitizerKind::PointerOverflow)) 4817 return GEPVal; 4818 4819 llvm::Type *PtrTy = Ptr->getType(); 4820 4821 // Perform nullptr-and-offset check unless the nullptr is defined. 4822 bool PerformNullCheck = !NullPointerIsDefined( 4823 Builder.GetInsertBlock()->getParent(), PtrTy->getPointerAddressSpace()); 4824 // Check for overflows unless the GEP got constant-folded, 4825 // and only in the default address space 4826 bool PerformOverflowCheck = 4827 !isa<llvm::Constant>(GEPVal) && PtrTy->getPointerAddressSpace() == 0; 4828 4829 if (!(PerformNullCheck || PerformOverflowCheck)) 4830 return GEPVal; 4831 4832 const auto &DL = CGM.getDataLayout(); 4833 4834 SanitizerScope SanScope(this); 4835 llvm::Type *IntPtrTy = DL.getIntPtrType(PtrTy); 4836 4837 GEPOffsetAndOverflow EvaluatedGEP = 4838 EmitGEPOffsetInBytes(Ptr, GEPVal, getLLVMContext(), CGM, Builder); 4839 4840 assert((!isa<llvm::Constant>(EvaluatedGEP.TotalOffset) || 4841 EvaluatedGEP.OffsetOverflows == Builder.getFalse()) && 4842 "If the offset got constant-folded, we don't expect that there was an " 4843 "overflow."); 4844 4845 auto *Zero = llvm::ConstantInt::getNullValue(IntPtrTy); 4846 4847 // Common case: if the total offset is zero, and we are using C++ semantics, 4848 // where nullptr+0 is defined, don't emit a check. 4849 if (EvaluatedGEP.TotalOffset == Zero && CGM.getLangOpts().CPlusPlus) 4850 return GEPVal; 4851 4852 // Now that we've computed the total offset, add it to the base pointer (with 4853 // wrapping semantics). 4854 auto *IntPtr = Builder.CreatePtrToInt(Ptr, IntPtrTy); 4855 auto *ComputedGEP = Builder.CreateAdd(IntPtr, EvaluatedGEP.TotalOffset); 4856 4857 llvm::SmallVector<std::pair<llvm::Value *, SanitizerMask>, 2> Checks; 4858 4859 if (PerformNullCheck) { 4860 // In C++, if the base pointer evaluates to a null pointer value, 4861 // the only valid pointer this inbounds GEP can produce is also 4862 // a null pointer, so the offset must also evaluate to zero. 4863 // Likewise, if we have non-zero base pointer, we can not get null pointer 4864 // as a result, so the offset can not be -intptr_t(BasePtr). 4865 // In other words, both pointers are either null, or both are non-null, 4866 // or the behaviour is undefined. 4867 // 4868 // C, however, is more strict in this regard, and gives more 4869 // optimization opportunities: in C, additionally, nullptr+0 is undefined. 4870 // So both the input to the 'gep inbounds' AND the output must not be null. 4871 auto *BaseIsNotNullptr = Builder.CreateIsNotNull(Ptr); 4872 auto *ResultIsNotNullptr = Builder.CreateIsNotNull(ComputedGEP); 4873 auto *Valid = 4874 CGM.getLangOpts().CPlusPlus 4875 ? Builder.CreateICmpEQ(BaseIsNotNullptr, ResultIsNotNullptr) 4876 : Builder.CreateAnd(BaseIsNotNullptr, ResultIsNotNullptr); 4877 Checks.emplace_back(Valid, SanitizerKind::PointerOverflow); 4878 } 4879 4880 if (PerformOverflowCheck) { 4881 // The GEP is valid if: 4882 // 1) The total offset doesn't overflow, and 4883 // 2) The sign of the difference between the computed address and the base 4884 // pointer matches the sign of the total offset. 4885 llvm::Value *ValidGEP; 4886 auto *NoOffsetOverflow = Builder.CreateNot(EvaluatedGEP.OffsetOverflows); 4887 if (SignedIndices) { 4888 // GEP is computed as `unsigned base + signed offset`, therefore: 4889 // * If offset was positive, then the computed pointer can not be 4890 // [unsigned] less than the base pointer, unless it overflowed. 4891 // * If offset was negative, then the computed pointer can not be 4892 // [unsigned] greater than the bas pointere, unless it overflowed. 4893 auto *PosOrZeroValid = Builder.CreateICmpUGE(ComputedGEP, IntPtr); 4894 auto *PosOrZeroOffset = 4895 Builder.CreateICmpSGE(EvaluatedGEP.TotalOffset, Zero); 4896 llvm::Value *NegValid = Builder.CreateICmpULT(ComputedGEP, IntPtr); 4897 ValidGEP = 4898 Builder.CreateSelect(PosOrZeroOffset, PosOrZeroValid, NegValid); 4899 } else if (!IsSubtraction) { 4900 // GEP is computed as `unsigned base + unsigned offset`, therefore the 4901 // computed pointer can not be [unsigned] less than base pointer, 4902 // unless there was an overflow. 4903 // Equivalent to `@llvm.uadd.with.overflow(%base, %offset)`. 4904 ValidGEP = Builder.CreateICmpUGE(ComputedGEP, IntPtr); 4905 } else { 4906 // GEP is computed as `unsigned base - unsigned offset`, therefore the 4907 // computed pointer can not be [unsigned] greater than base pointer, 4908 // unless there was an overflow. 4909 // Equivalent to `@llvm.usub.with.overflow(%base, sub(0, %offset))`. 4910 ValidGEP = Builder.CreateICmpULE(ComputedGEP, IntPtr); 4911 } 4912 ValidGEP = Builder.CreateAnd(ValidGEP, NoOffsetOverflow); 4913 Checks.emplace_back(ValidGEP, SanitizerKind::PointerOverflow); 4914 } 4915 4916 assert(!Checks.empty() && "Should have produced some checks."); 4917 4918 llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation(Loc)}; 4919 // Pass the computed GEP to the runtime to avoid emitting poisoned arguments. 4920 llvm::Value *DynamicArgs[] = {IntPtr, ComputedGEP}; 4921 EmitCheck(Checks, SanitizerHandler::PointerOverflow, StaticArgs, DynamicArgs); 4922 4923 return GEPVal; 4924 } 4925