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 // Perform VLAT <-> VLST bitcast through memory. 2007 if ((isa<llvm::FixedVectorType>(SrcTy) && 2008 isa<llvm::ScalableVectorType>(DstTy)) || 2009 (isa<llvm::ScalableVectorType>(SrcTy) && 2010 isa<llvm::FixedVectorType>(DstTy))) { 2011 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) { 2012 // Call expressions can't have a scalar return unless the return type 2013 // is a reference type so an lvalue can't be emitted. Create a temp 2014 // alloca to store the call, bitcast the address then load. 2015 QualType RetTy = CE->getCallReturnType(CGF.getContext()); 2016 Address Addr = 2017 CGF.CreateDefaultAlignTempAlloca(SrcTy, "saved-call-rvalue"); 2018 LValue LV = CGF.MakeAddrLValue(Addr, RetTy); 2019 CGF.EmitStoreOfScalar(Src, LV); 2020 Addr = Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(DestTy), 2021 "castFixedSve"); 2022 LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy); 2023 DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo()); 2024 return EmitLoadOfLValue(DestLV, CE->getExprLoc()); 2025 } 2026 2027 Address Addr = EmitLValue(E).getAddress(CGF); 2028 Addr = Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(DestTy)); 2029 LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy); 2030 DestLV.setTBAAInfo(TBAAAccessInfo::getMayAliasInfo()); 2031 return EmitLoadOfLValue(DestLV, CE->getExprLoc()); 2032 } 2033 2034 return Builder.CreateBitCast(Src, DstTy); 2035 } 2036 case CK_AddressSpaceConversion: { 2037 Expr::EvalResult Result; 2038 if (E->EvaluateAsRValue(Result, CGF.getContext()) && 2039 Result.Val.isNullPointer()) { 2040 // If E has side effect, it is emitted even if its final result is a 2041 // null pointer. In that case, a DCE pass should be able to 2042 // eliminate the useless instructions emitted during translating E. 2043 if (Result.HasSideEffects) 2044 Visit(E); 2045 return CGF.CGM.getNullPointer(cast<llvm::PointerType>( 2046 ConvertType(DestTy)), DestTy); 2047 } 2048 // Since target may map different address spaces in AST to the same address 2049 // space, an address space conversion may end up as a bitcast. 2050 return CGF.CGM.getTargetCodeGenInfo().performAddrSpaceCast( 2051 CGF, Visit(E), E->getType()->getPointeeType().getAddressSpace(), 2052 DestTy->getPointeeType().getAddressSpace(), ConvertType(DestTy)); 2053 } 2054 case CK_AtomicToNonAtomic: 2055 case CK_NonAtomicToAtomic: 2056 case CK_NoOp: 2057 case CK_UserDefinedConversion: 2058 return Visit(const_cast<Expr*>(E)); 2059 2060 case CK_BaseToDerived: { 2061 const CXXRecordDecl *DerivedClassDecl = DestTy->getPointeeCXXRecordDecl(); 2062 assert(DerivedClassDecl && "BaseToDerived arg isn't a C++ object pointer!"); 2063 2064 Address Base = CGF.EmitPointerWithAlignment(E); 2065 Address Derived = 2066 CGF.GetAddressOfDerivedClass(Base, DerivedClassDecl, 2067 CE->path_begin(), CE->path_end(), 2068 CGF.ShouldNullCheckClassCastValue(CE)); 2069 2070 // C++11 [expr.static.cast]p11: Behavior is undefined if a downcast is 2071 // performed and the object is not of the derived type. 2072 if (CGF.sanitizePerformTypeCheck()) 2073 CGF.EmitTypeCheck(CodeGenFunction::TCK_DowncastPointer, CE->getExprLoc(), 2074 Derived.getPointer(), DestTy->getPointeeType()); 2075 2076 if (CGF.SanOpts.has(SanitizerKind::CFIDerivedCast)) 2077 CGF.EmitVTablePtrCheckForCast( 2078 DestTy->getPointeeType(), Derived.getPointer(), 2079 /*MayBeNull=*/true, CodeGenFunction::CFITCK_DerivedCast, 2080 CE->getBeginLoc()); 2081 2082 return Derived.getPointer(); 2083 } 2084 case CK_UncheckedDerivedToBase: 2085 case CK_DerivedToBase: { 2086 // The EmitPointerWithAlignment path does this fine; just discard 2087 // the alignment. 2088 return CGF.EmitPointerWithAlignment(CE).getPointer(); 2089 } 2090 2091 case CK_Dynamic: { 2092 Address V = CGF.EmitPointerWithAlignment(E); 2093 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE); 2094 return CGF.EmitDynamicCast(V, DCE); 2095 } 2096 2097 case CK_ArrayToPointerDecay: 2098 return CGF.EmitArrayToPointerDecay(E).getPointer(); 2099 case CK_FunctionToPointerDecay: 2100 return EmitLValue(E).getPointer(CGF); 2101 2102 case CK_NullToPointer: 2103 if (MustVisitNullValue(E)) 2104 CGF.EmitIgnoredExpr(E); 2105 2106 return CGF.CGM.getNullPointer(cast<llvm::PointerType>(ConvertType(DestTy)), 2107 DestTy); 2108 2109 case CK_NullToMemberPointer: { 2110 if (MustVisitNullValue(E)) 2111 CGF.EmitIgnoredExpr(E); 2112 2113 const MemberPointerType *MPT = CE->getType()->getAs<MemberPointerType>(); 2114 return CGF.CGM.getCXXABI().EmitNullMemberPointer(MPT); 2115 } 2116 2117 case CK_ReinterpretMemberPointer: 2118 case CK_BaseToDerivedMemberPointer: 2119 case CK_DerivedToBaseMemberPointer: { 2120 Value *Src = Visit(E); 2121 2122 // Note that the AST doesn't distinguish between checked and 2123 // unchecked member pointer conversions, so we always have to 2124 // implement checked conversions here. This is inefficient when 2125 // actual control flow may be required in order to perform the 2126 // check, which it is for data member pointers (but not member 2127 // function pointers on Itanium and ARM). 2128 return CGF.CGM.getCXXABI().EmitMemberPointerConversion(CGF, CE, Src); 2129 } 2130 2131 case CK_ARCProduceObject: 2132 return CGF.EmitARCRetainScalarExpr(E); 2133 case CK_ARCConsumeObject: 2134 return CGF.EmitObjCConsumeObject(E->getType(), Visit(E)); 2135 case CK_ARCReclaimReturnedObject: 2136 return CGF.EmitARCReclaimReturnedObject(E, /*allowUnsafe*/ Ignored); 2137 case CK_ARCExtendBlockObject: 2138 return CGF.EmitARCExtendBlockObject(E); 2139 2140 case CK_CopyAndAutoreleaseBlockObject: 2141 return CGF.EmitBlockCopyAndAutorelease(Visit(E), E->getType()); 2142 2143 case CK_FloatingRealToComplex: 2144 case CK_FloatingComplexCast: 2145 case CK_IntegralRealToComplex: 2146 case CK_IntegralComplexCast: 2147 case CK_IntegralComplexToFloatingComplex: 2148 case CK_FloatingComplexToIntegralComplex: 2149 case CK_ConstructorConversion: 2150 case CK_ToUnion: 2151 llvm_unreachable("scalar cast to non-scalar value"); 2152 2153 case CK_LValueToRValue: 2154 assert(CGF.getContext().hasSameUnqualifiedType(E->getType(), DestTy)); 2155 assert(E->isGLValue() && "lvalue-to-rvalue applied to r-value!"); 2156 return Visit(const_cast<Expr*>(E)); 2157 2158 case CK_IntegralToPointer: { 2159 Value *Src = Visit(const_cast<Expr*>(E)); 2160 2161 // First, convert to the correct width so that we control the kind of 2162 // extension. 2163 auto DestLLVMTy = ConvertType(DestTy); 2164 llvm::Type *MiddleTy = CGF.CGM.getDataLayout().getIntPtrType(DestLLVMTy); 2165 bool InputSigned = E->getType()->isSignedIntegerOrEnumerationType(); 2166 llvm::Value* IntResult = 2167 Builder.CreateIntCast(Src, MiddleTy, InputSigned, "conv"); 2168 2169 auto *IntToPtr = Builder.CreateIntToPtr(IntResult, DestLLVMTy); 2170 2171 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers) { 2172 // Going from integer to pointer that could be dynamic requires reloading 2173 // dynamic information from invariant.group. 2174 if (DestTy.mayBeDynamicClass()) 2175 IntToPtr = Builder.CreateLaunderInvariantGroup(IntToPtr); 2176 } 2177 return IntToPtr; 2178 } 2179 case CK_PointerToIntegral: { 2180 assert(!DestTy->isBooleanType() && "bool should use PointerToBool"); 2181 auto *PtrExpr = Visit(E); 2182 2183 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers) { 2184 const QualType SrcType = E->getType(); 2185 2186 // Casting to integer requires stripping dynamic information as it does 2187 // not carries it. 2188 if (SrcType.mayBeDynamicClass()) 2189 PtrExpr = Builder.CreateStripInvariantGroup(PtrExpr); 2190 } 2191 2192 return Builder.CreatePtrToInt(PtrExpr, ConvertType(DestTy)); 2193 } 2194 case CK_ToVoid: { 2195 CGF.EmitIgnoredExpr(E); 2196 return nullptr; 2197 } 2198 case CK_VectorSplat: { 2199 llvm::Type *DstTy = ConvertType(DestTy); 2200 Value *Elt = Visit(const_cast<Expr*>(E)); 2201 // Splat the element across to all elements 2202 unsigned NumElements = cast<llvm::FixedVectorType>(DstTy)->getNumElements(); 2203 return Builder.CreateVectorSplat(NumElements, Elt, "splat"); 2204 } 2205 2206 case CK_FixedPointCast: 2207 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2208 CE->getExprLoc()); 2209 2210 case CK_FixedPointToBoolean: 2211 assert(E->getType()->isFixedPointType() && 2212 "Expected src type to be fixed point type"); 2213 assert(DestTy->isBooleanType() && "Expected dest type to be boolean type"); 2214 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2215 CE->getExprLoc()); 2216 2217 case CK_FixedPointToIntegral: 2218 assert(E->getType()->isFixedPointType() && 2219 "Expected src type to be fixed point type"); 2220 assert(DestTy->isIntegerType() && "Expected dest type to be an integer"); 2221 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2222 CE->getExprLoc()); 2223 2224 case CK_IntegralToFixedPoint: 2225 assert(E->getType()->isIntegerType() && 2226 "Expected src type to be an integer"); 2227 assert(DestTy->isFixedPointType() && 2228 "Expected dest type to be fixed point type"); 2229 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2230 CE->getExprLoc()); 2231 2232 case CK_IntegralCast: { 2233 ScalarConversionOpts Opts; 2234 if (auto *ICE = dyn_cast<ImplicitCastExpr>(CE)) { 2235 if (!ICE->isPartOfExplicitCast()) 2236 Opts = ScalarConversionOpts(CGF.SanOpts); 2237 } 2238 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2239 CE->getExprLoc(), Opts); 2240 } 2241 case CK_IntegralToFloating: 2242 case CK_FloatingToIntegral: 2243 case CK_FloatingCast: 2244 case CK_FixedPointToFloating: 2245 case CK_FloatingToFixedPoint: 2246 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2247 CE->getExprLoc()); 2248 case CK_BooleanToSignedIntegral: { 2249 ScalarConversionOpts Opts; 2250 Opts.TreatBooleanAsSigned = true; 2251 return EmitScalarConversion(Visit(E), E->getType(), DestTy, 2252 CE->getExprLoc(), Opts); 2253 } 2254 case CK_IntegralToBoolean: 2255 return EmitIntToBoolConversion(Visit(E)); 2256 case CK_PointerToBoolean: 2257 return EmitPointerToBoolConversion(Visit(E), E->getType()); 2258 case CK_FloatingToBoolean: 2259 return EmitFloatToBoolConversion(Visit(E)); 2260 case CK_MemberPointerToBoolean: { 2261 llvm::Value *MemPtr = Visit(E); 2262 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>(); 2263 return CGF.CGM.getCXXABI().EmitMemberPointerIsNotNull(CGF, MemPtr, MPT); 2264 } 2265 2266 case CK_FloatingComplexToReal: 2267 case CK_IntegralComplexToReal: 2268 return CGF.EmitComplexExpr(E, false, true).first; 2269 2270 case CK_FloatingComplexToBoolean: 2271 case CK_IntegralComplexToBoolean: { 2272 CodeGenFunction::ComplexPairTy V = CGF.EmitComplexExpr(E); 2273 2274 // TODO: kill this function off, inline appropriate case here 2275 return EmitComplexToScalarConversion(V, E->getType(), DestTy, 2276 CE->getExprLoc()); 2277 } 2278 2279 case CK_ZeroToOCLOpaqueType: { 2280 assert((DestTy->isEventT() || DestTy->isQueueT() || 2281 DestTy->isOCLIntelSubgroupAVCType()) && 2282 "CK_ZeroToOCLEvent cast on non-event type"); 2283 return llvm::Constant::getNullValue(ConvertType(DestTy)); 2284 } 2285 2286 case CK_IntToOCLSampler: 2287 return CGF.CGM.createOpenCLIntToSamplerConversion(E, CGF); 2288 2289 } // end of switch 2290 2291 llvm_unreachable("unknown scalar cast"); 2292 } 2293 2294 Value *ScalarExprEmitter::VisitStmtExpr(const StmtExpr *E) { 2295 CodeGenFunction::StmtExprEvaluation eval(CGF); 2296 Address RetAlloca = CGF.EmitCompoundStmt(*E->getSubStmt(), 2297 !E->getType()->isVoidType()); 2298 if (!RetAlloca.isValid()) 2299 return nullptr; 2300 return CGF.EmitLoadOfScalar(CGF.MakeAddrLValue(RetAlloca, E->getType()), 2301 E->getExprLoc()); 2302 } 2303 2304 Value *ScalarExprEmitter::VisitExprWithCleanups(ExprWithCleanups *E) { 2305 CodeGenFunction::RunCleanupsScope Scope(CGF); 2306 Value *V = Visit(E->getSubExpr()); 2307 // Defend against dominance problems caused by jumps out of expression 2308 // evaluation through the shared cleanup block. 2309 Scope.ForceCleanup({&V}); 2310 return V; 2311 } 2312 2313 //===----------------------------------------------------------------------===// 2314 // Unary Operators 2315 //===----------------------------------------------------------------------===// 2316 2317 static BinOpInfo createBinOpInfoFromIncDec(const UnaryOperator *E, 2318 llvm::Value *InVal, bool IsInc, 2319 FPOptions FPFeatures) { 2320 BinOpInfo BinOp; 2321 BinOp.LHS = InVal; 2322 BinOp.RHS = llvm::ConstantInt::get(InVal->getType(), 1, false); 2323 BinOp.Ty = E->getType(); 2324 BinOp.Opcode = IsInc ? BO_Add : BO_Sub; 2325 BinOp.FPFeatures = FPFeatures; 2326 BinOp.E = E; 2327 return BinOp; 2328 } 2329 2330 llvm::Value *ScalarExprEmitter::EmitIncDecConsiderOverflowBehavior( 2331 const UnaryOperator *E, llvm::Value *InVal, bool IsInc) { 2332 llvm::Value *Amount = 2333 llvm::ConstantInt::get(InVal->getType(), IsInc ? 1 : -1, true); 2334 StringRef Name = IsInc ? "inc" : "dec"; 2335 switch (CGF.getLangOpts().getSignedOverflowBehavior()) { 2336 case LangOptions::SOB_Defined: 2337 return Builder.CreateAdd(InVal, Amount, Name); 2338 case LangOptions::SOB_Undefined: 2339 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) 2340 return Builder.CreateNSWAdd(InVal, Amount, Name); 2341 LLVM_FALLTHROUGH; 2342 case LangOptions::SOB_Trapping: 2343 if (!E->canOverflow()) 2344 return Builder.CreateNSWAdd(InVal, Amount, Name); 2345 return EmitOverflowCheckedBinOp(createBinOpInfoFromIncDec( 2346 E, InVal, IsInc, E->getFPFeaturesInEffect(CGF.getLangOpts()))); 2347 } 2348 llvm_unreachable("Unknown SignedOverflowBehaviorTy"); 2349 } 2350 2351 namespace { 2352 /// Handles check and update for lastprivate conditional variables. 2353 class OMPLastprivateConditionalUpdateRAII { 2354 private: 2355 CodeGenFunction &CGF; 2356 const UnaryOperator *E; 2357 2358 public: 2359 OMPLastprivateConditionalUpdateRAII(CodeGenFunction &CGF, 2360 const UnaryOperator *E) 2361 : CGF(CGF), E(E) {} 2362 ~OMPLastprivateConditionalUpdateRAII() { 2363 if (CGF.getLangOpts().OpenMP) 2364 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional( 2365 CGF, E->getSubExpr()); 2366 } 2367 }; 2368 } // namespace 2369 2370 llvm::Value * 2371 ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 2372 bool isInc, bool isPre) { 2373 OMPLastprivateConditionalUpdateRAII OMPRegion(CGF, E); 2374 QualType type = E->getSubExpr()->getType(); 2375 llvm::PHINode *atomicPHI = nullptr; 2376 llvm::Value *value; 2377 llvm::Value *input; 2378 2379 int amount = (isInc ? 1 : -1); 2380 bool isSubtraction = !isInc; 2381 2382 if (const AtomicType *atomicTy = type->getAs<AtomicType>()) { 2383 type = atomicTy->getValueType(); 2384 if (isInc && type->isBooleanType()) { 2385 llvm::Value *True = CGF.EmitToMemory(Builder.getTrue(), type); 2386 if (isPre) { 2387 Builder.CreateStore(True, LV.getAddress(CGF), LV.isVolatileQualified()) 2388 ->setAtomic(llvm::AtomicOrdering::SequentiallyConsistent); 2389 return Builder.getTrue(); 2390 } 2391 // For atomic bool increment, we just store true and return it for 2392 // preincrement, do an atomic swap with true for postincrement 2393 return Builder.CreateAtomicRMW( 2394 llvm::AtomicRMWInst::Xchg, LV.getPointer(CGF), True, 2395 llvm::AtomicOrdering::SequentiallyConsistent); 2396 } 2397 // Special case for atomic increment / decrement on integers, emit 2398 // atomicrmw instructions. We skip this if we want to be doing overflow 2399 // checking, and fall into the slow path with the atomic cmpxchg loop. 2400 if (!type->isBooleanType() && type->isIntegerType() && 2401 !(type->isUnsignedIntegerType() && 2402 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) && 2403 CGF.getLangOpts().getSignedOverflowBehavior() != 2404 LangOptions::SOB_Trapping) { 2405 llvm::AtomicRMWInst::BinOp aop = isInc ? llvm::AtomicRMWInst::Add : 2406 llvm::AtomicRMWInst::Sub; 2407 llvm::Instruction::BinaryOps op = isInc ? llvm::Instruction::Add : 2408 llvm::Instruction::Sub; 2409 llvm::Value *amt = CGF.EmitToMemory( 2410 llvm::ConstantInt::get(ConvertType(type), 1, true), type); 2411 llvm::Value *old = 2412 Builder.CreateAtomicRMW(aop, LV.getPointer(CGF), amt, 2413 llvm::AtomicOrdering::SequentiallyConsistent); 2414 return isPre ? Builder.CreateBinOp(op, old, amt) : old; 2415 } 2416 value = EmitLoadOfLValue(LV, E->getExprLoc()); 2417 input = value; 2418 // For every other atomic operation, we need to emit a load-op-cmpxchg loop 2419 llvm::BasicBlock *startBB = Builder.GetInsertBlock(); 2420 llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn); 2421 value = CGF.EmitToMemory(value, type); 2422 Builder.CreateBr(opBB); 2423 Builder.SetInsertPoint(opBB); 2424 atomicPHI = Builder.CreatePHI(value->getType(), 2); 2425 atomicPHI->addIncoming(value, startBB); 2426 value = atomicPHI; 2427 } else { 2428 value = EmitLoadOfLValue(LV, E->getExprLoc()); 2429 input = value; 2430 } 2431 2432 // Special case of integer increment that we have to check first: bool++. 2433 // Due to promotion rules, we get: 2434 // bool++ -> bool = bool + 1 2435 // -> bool = (int)bool + 1 2436 // -> bool = ((int)bool + 1 != 0) 2437 // An interesting aspect of this is that increment is always true. 2438 // Decrement does not have this property. 2439 if (isInc && type->isBooleanType()) { 2440 value = Builder.getTrue(); 2441 2442 // Most common case by far: integer increment. 2443 } else if (type->isIntegerType()) { 2444 QualType promotedType; 2445 bool canPerformLossyDemotionCheck = false; 2446 if (type->isPromotableIntegerType()) { 2447 promotedType = CGF.getContext().getPromotedIntegerType(type); 2448 assert(promotedType != type && "Shouldn't promote to the same type."); 2449 canPerformLossyDemotionCheck = true; 2450 canPerformLossyDemotionCheck &= 2451 CGF.getContext().getCanonicalType(type) != 2452 CGF.getContext().getCanonicalType(promotedType); 2453 canPerformLossyDemotionCheck &= 2454 PromotionIsPotentiallyEligibleForImplicitIntegerConversionCheck( 2455 type, promotedType); 2456 assert((!canPerformLossyDemotionCheck || 2457 type->isSignedIntegerOrEnumerationType() || 2458 promotedType->isSignedIntegerOrEnumerationType() || 2459 ConvertType(type)->getScalarSizeInBits() == 2460 ConvertType(promotedType)->getScalarSizeInBits()) && 2461 "The following check expects that if we do promotion to different " 2462 "underlying canonical type, at least one of the types (either " 2463 "base or promoted) will be signed, or the bitwidths will match."); 2464 } 2465 if (CGF.SanOpts.hasOneOf( 2466 SanitizerKind::ImplicitIntegerArithmeticValueChange) && 2467 canPerformLossyDemotionCheck) { 2468 // While `x += 1` (for `x` with width less than int) is modeled as 2469 // promotion+arithmetics+demotion, and we can catch lossy demotion with 2470 // ease; inc/dec with width less than int can't overflow because of 2471 // promotion rules, so we omit promotion+demotion, which means that we can 2472 // not catch lossy "demotion". Because we still want to catch these cases 2473 // when the sanitizer is enabled, we perform the promotion, then perform 2474 // the increment/decrement in the wider type, and finally 2475 // perform the demotion. This will catch lossy demotions. 2476 2477 value = EmitScalarConversion(value, type, promotedType, E->getExprLoc()); 2478 Value *amt = llvm::ConstantInt::get(value->getType(), amount, true); 2479 value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec"); 2480 // Do pass non-default ScalarConversionOpts so that sanitizer check is 2481 // emitted. 2482 value = EmitScalarConversion(value, promotedType, type, E->getExprLoc(), 2483 ScalarConversionOpts(CGF.SanOpts)); 2484 2485 // Note that signed integer inc/dec with width less than int can't 2486 // overflow because of promotion rules; we're just eliding a few steps 2487 // here. 2488 } else if (E->canOverflow() && type->isSignedIntegerOrEnumerationType()) { 2489 value = EmitIncDecConsiderOverflowBehavior(E, value, isInc); 2490 } else if (E->canOverflow() && type->isUnsignedIntegerType() && 2491 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) { 2492 value = EmitOverflowCheckedBinOp(createBinOpInfoFromIncDec( 2493 E, value, isInc, E->getFPFeaturesInEffect(CGF.getLangOpts()))); 2494 } else { 2495 llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount, true); 2496 value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec"); 2497 } 2498 2499 // Next most common: pointer increment. 2500 } else if (const PointerType *ptr = type->getAs<PointerType>()) { 2501 QualType type = ptr->getPointeeType(); 2502 2503 // VLA types don't have constant size. 2504 if (const VariableArrayType *vla 2505 = CGF.getContext().getAsVariableArrayType(type)) { 2506 llvm::Value *numElts = CGF.getVLASize(vla).NumElts; 2507 if (!isInc) numElts = Builder.CreateNSWNeg(numElts, "vla.negsize"); 2508 if (CGF.getLangOpts().isSignedOverflowDefined()) 2509 value = Builder.CreateGEP(value, numElts, "vla.inc"); 2510 else 2511 value = CGF.EmitCheckedInBoundsGEP( 2512 value, numElts, /*SignedIndices=*/false, isSubtraction, 2513 E->getExprLoc(), "vla.inc"); 2514 2515 // Arithmetic on function pointers (!) is just +-1. 2516 } else if (type->isFunctionType()) { 2517 llvm::Value *amt = Builder.getInt32(amount); 2518 2519 value = CGF.EmitCastToVoidPtr(value); 2520 if (CGF.getLangOpts().isSignedOverflowDefined()) 2521 value = Builder.CreateGEP(value, amt, "incdec.funcptr"); 2522 else 2523 value = CGF.EmitCheckedInBoundsGEP(value, amt, /*SignedIndices=*/false, 2524 isSubtraction, E->getExprLoc(), 2525 "incdec.funcptr"); 2526 value = Builder.CreateBitCast(value, input->getType()); 2527 2528 // For everything else, we can just do a simple increment. 2529 } else { 2530 llvm::Value *amt = Builder.getInt32(amount); 2531 if (CGF.getLangOpts().isSignedOverflowDefined()) 2532 value = Builder.CreateGEP(value, amt, "incdec.ptr"); 2533 else 2534 value = CGF.EmitCheckedInBoundsGEP(value, amt, /*SignedIndices=*/false, 2535 isSubtraction, E->getExprLoc(), 2536 "incdec.ptr"); 2537 } 2538 2539 // Vector increment/decrement. 2540 } else if (type->isVectorType()) { 2541 if (type->hasIntegerRepresentation()) { 2542 llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount); 2543 2544 value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec"); 2545 } else { 2546 value = Builder.CreateFAdd( 2547 value, 2548 llvm::ConstantFP::get(value->getType(), amount), 2549 isInc ? "inc" : "dec"); 2550 } 2551 2552 // Floating point. 2553 } else if (type->isRealFloatingType()) { 2554 // Add the inc/dec to the real part. 2555 llvm::Value *amt; 2556 2557 if (type->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) { 2558 // Another special case: half FP increment should be done via float 2559 if (CGF.getContext().getTargetInfo().useFP16ConversionIntrinsics()) { 2560 value = Builder.CreateCall( 2561 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_from_fp16, 2562 CGF.CGM.FloatTy), 2563 input, "incdec.conv"); 2564 } else { 2565 value = Builder.CreateFPExt(input, CGF.CGM.FloatTy, "incdec.conv"); 2566 } 2567 } 2568 2569 if (value->getType()->isFloatTy()) 2570 amt = llvm::ConstantFP::get(VMContext, 2571 llvm::APFloat(static_cast<float>(amount))); 2572 else if (value->getType()->isDoubleTy()) 2573 amt = llvm::ConstantFP::get(VMContext, 2574 llvm::APFloat(static_cast<double>(amount))); 2575 else { 2576 // Remaining types are Half, LongDouble or __float128. Convert from float. 2577 llvm::APFloat F(static_cast<float>(amount)); 2578 bool ignored; 2579 const llvm::fltSemantics *FS; 2580 // Don't use getFloatTypeSemantics because Half isn't 2581 // necessarily represented using the "half" LLVM type. 2582 if (value->getType()->isFP128Ty()) 2583 FS = &CGF.getTarget().getFloat128Format(); 2584 else if (value->getType()->isHalfTy()) 2585 FS = &CGF.getTarget().getHalfFormat(); 2586 else 2587 FS = &CGF.getTarget().getLongDoubleFormat(); 2588 F.convert(*FS, llvm::APFloat::rmTowardZero, &ignored); 2589 amt = llvm::ConstantFP::get(VMContext, F); 2590 } 2591 value = Builder.CreateFAdd(value, amt, isInc ? "inc" : "dec"); 2592 2593 if (type->isHalfType() && !CGF.getContext().getLangOpts().NativeHalfType) { 2594 if (CGF.getContext().getTargetInfo().useFP16ConversionIntrinsics()) { 2595 value = Builder.CreateCall( 2596 CGF.CGM.getIntrinsic(llvm::Intrinsic::convert_to_fp16, 2597 CGF.CGM.FloatTy), 2598 value, "incdec.conv"); 2599 } else { 2600 value = Builder.CreateFPTrunc(value, input->getType(), "incdec.conv"); 2601 } 2602 } 2603 2604 // Fixed-point types. 2605 } else if (type->isFixedPointType()) { 2606 // Fixed-point types are tricky. In some cases, it isn't possible to 2607 // represent a 1 or a -1 in the type at all. Piggyback off of 2608 // EmitFixedPointBinOp to avoid having to reimplement saturation. 2609 BinOpInfo Info; 2610 Info.E = E; 2611 Info.Ty = E->getType(); 2612 Info.Opcode = isInc ? BO_Add : BO_Sub; 2613 Info.LHS = value; 2614 Info.RHS = llvm::ConstantInt::get(value->getType(), 1, false); 2615 // If the type is signed, it's better to represent this as +(-1) or -(-1), 2616 // since -1 is guaranteed to be representable. 2617 if (type->isSignedFixedPointType()) { 2618 Info.Opcode = isInc ? BO_Sub : BO_Add; 2619 Info.RHS = Builder.CreateNeg(Info.RHS); 2620 } 2621 // Now, convert from our invented integer literal to the type of the unary 2622 // op. This will upscale and saturate if necessary. This value can become 2623 // undef in some cases. 2624 llvm::FixedPointBuilder<CGBuilderTy> FPBuilder(Builder); 2625 auto DstSema = CGF.getContext().getFixedPointSemantics(Info.Ty); 2626 Info.RHS = FPBuilder.CreateIntegerToFixed(Info.RHS, true, DstSema); 2627 value = EmitFixedPointBinOp(Info); 2628 2629 // Objective-C pointer types. 2630 } else { 2631 const ObjCObjectPointerType *OPT = type->castAs<ObjCObjectPointerType>(); 2632 value = CGF.EmitCastToVoidPtr(value); 2633 2634 CharUnits size = CGF.getContext().getTypeSizeInChars(OPT->getObjectType()); 2635 if (!isInc) size = -size; 2636 llvm::Value *sizeValue = 2637 llvm::ConstantInt::get(CGF.SizeTy, size.getQuantity()); 2638 2639 if (CGF.getLangOpts().isSignedOverflowDefined()) 2640 value = Builder.CreateGEP(value, sizeValue, "incdec.objptr"); 2641 else 2642 value = CGF.EmitCheckedInBoundsGEP(value, sizeValue, 2643 /*SignedIndices=*/false, isSubtraction, 2644 E->getExprLoc(), "incdec.objptr"); 2645 value = Builder.CreateBitCast(value, input->getType()); 2646 } 2647 2648 if (atomicPHI) { 2649 llvm::BasicBlock *curBlock = Builder.GetInsertBlock(); 2650 llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn); 2651 auto Pair = CGF.EmitAtomicCompareExchange( 2652 LV, RValue::get(atomicPHI), RValue::get(value), E->getExprLoc()); 2653 llvm::Value *old = CGF.EmitToMemory(Pair.first.getScalarVal(), type); 2654 llvm::Value *success = Pair.second; 2655 atomicPHI->addIncoming(old, curBlock); 2656 Builder.CreateCondBr(success, contBB, atomicPHI->getParent()); 2657 Builder.SetInsertPoint(contBB); 2658 return isPre ? value : input; 2659 } 2660 2661 // Store the updated result through the lvalue. 2662 if (LV.isBitField()) 2663 CGF.EmitStoreThroughBitfieldLValue(RValue::get(value), LV, &value); 2664 else 2665 CGF.EmitStoreThroughLValue(RValue::get(value), LV); 2666 2667 // If this is a postinc, return the value read from memory, otherwise use the 2668 // updated value. 2669 return isPre ? value : input; 2670 } 2671 2672 2673 2674 Value *ScalarExprEmitter::VisitUnaryMinus(const UnaryOperator *E) { 2675 TestAndClearIgnoreResultAssign(); 2676 Value *Op = Visit(E->getSubExpr()); 2677 2678 // Generate a unary FNeg for FP ops. 2679 if (Op->getType()->isFPOrFPVectorTy()) 2680 return Builder.CreateFNeg(Op, "fneg"); 2681 2682 // Emit unary minus with EmitSub so we handle overflow cases etc. 2683 BinOpInfo BinOp; 2684 BinOp.RHS = Op; 2685 BinOp.LHS = llvm::Constant::getNullValue(BinOp.RHS->getType()); 2686 BinOp.Ty = E->getType(); 2687 BinOp.Opcode = BO_Sub; 2688 BinOp.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); 2689 BinOp.E = E; 2690 return EmitSub(BinOp); 2691 } 2692 2693 Value *ScalarExprEmitter::VisitUnaryNot(const UnaryOperator *E) { 2694 TestAndClearIgnoreResultAssign(); 2695 Value *Op = Visit(E->getSubExpr()); 2696 return Builder.CreateNot(Op, "neg"); 2697 } 2698 2699 Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) { 2700 // Perform vector logical not on comparison with zero vector. 2701 if (E->getType()->isVectorType() && 2702 E->getType()->castAs<VectorType>()->getVectorKind() == 2703 VectorType::GenericVector) { 2704 Value *Oper = Visit(E->getSubExpr()); 2705 Value *Zero = llvm::Constant::getNullValue(Oper->getType()); 2706 Value *Result; 2707 if (Oper->getType()->isFPOrFPVectorTy()) { 2708 CodeGenFunction::CGFPOptionsRAII FPOptsRAII( 2709 CGF, E->getFPFeaturesInEffect(CGF.getLangOpts())); 2710 Result = Builder.CreateFCmp(llvm::CmpInst::FCMP_OEQ, Oper, Zero, "cmp"); 2711 } else 2712 Result = Builder.CreateICmp(llvm::CmpInst::ICMP_EQ, Oper, Zero, "cmp"); 2713 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); 2714 } 2715 2716 // Compare operand to zero. 2717 Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr()); 2718 2719 // Invert value. 2720 // TODO: Could dynamically modify easy computations here. For example, if 2721 // the operand is an icmp ne, turn into icmp eq. 2722 BoolVal = Builder.CreateNot(BoolVal, "lnot"); 2723 2724 // ZExt result to the expr type. 2725 return Builder.CreateZExt(BoolVal, ConvertType(E->getType()), "lnot.ext"); 2726 } 2727 2728 Value *ScalarExprEmitter::VisitOffsetOfExpr(OffsetOfExpr *E) { 2729 // Try folding the offsetof to a constant. 2730 Expr::EvalResult EVResult; 2731 if (E->EvaluateAsInt(EVResult, CGF.getContext())) { 2732 llvm::APSInt Value = EVResult.Val.getInt(); 2733 return Builder.getInt(Value); 2734 } 2735 2736 // Loop over the components of the offsetof to compute the value. 2737 unsigned n = E->getNumComponents(); 2738 llvm::Type* ResultType = ConvertType(E->getType()); 2739 llvm::Value* Result = llvm::Constant::getNullValue(ResultType); 2740 QualType CurrentType = E->getTypeSourceInfo()->getType(); 2741 for (unsigned i = 0; i != n; ++i) { 2742 OffsetOfNode ON = E->getComponent(i); 2743 llvm::Value *Offset = nullptr; 2744 switch (ON.getKind()) { 2745 case OffsetOfNode::Array: { 2746 // Compute the index 2747 Expr *IdxExpr = E->getIndexExpr(ON.getArrayExprIndex()); 2748 llvm::Value* Idx = CGF.EmitScalarExpr(IdxExpr); 2749 bool IdxSigned = IdxExpr->getType()->isSignedIntegerOrEnumerationType(); 2750 Idx = Builder.CreateIntCast(Idx, ResultType, IdxSigned, "conv"); 2751 2752 // Save the element type 2753 CurrentType = 2754 CGF.getContext().getAsArrayType(CurrentType)->getElementType(); 2755 2756 // Compute the element size 2757 llvm::Value* ElemSize = llvm::ConstantInt::get(ResultType, 2758 CGF.getContext().getTypeSizeInChars(CurrentType).getQuantity()); 2759 2760 // Multiply out to compute the result 2761 Offset = Builder.CreateMul(Idx, ElemSize); 2762 break; 2763 } 2764 2765 case OffsetOfNode::Field: { 2766 FieldDecl *MemberDecl = ON.getField(); 2767 RecordDecl *RD = CurrentType->castAs<RecordType>()->getDecl(); 2768 const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); 2769 2770 // Compute the index of the field in its parent. 2771 unsigned i = 0; 2772 // FIXME: It would be nice if we didn't have to loop here! 2773 for (RecordDecl::field_iterator Field = RD->field_begin(), 2774 FieldEnd = RD->field_end(); 2775 Field != FieldEnd; ++Field, ++i) { 2776 if (*Field == MemberDecl) 2777 break; 2778 } 2779 assert(i < RL.getFieldCount() && "offsetof field in wrong type"); 2780 2781 // Compute the offset to the field 2782 int64_t OffsetInt = RL.getFieldOffset(i) / 2783 CGF.getContext().getCharWidth(); 2784 Offset = llvm::ConstantInt::get(ResultType, OffsetInt); 2785 2786 // Save the element type. 2787 CurrentType = MemberDecl->getType(); 2788 break; 2789 } 2790 2791 case OffsetOfNode::Identifier: 2792 llvm_unreachable("dependent __builtin_offsetof"); 2793 2794 case OffsetOfNode::Base: { 2795 if (ON.getBase()->isVirtual()) { 2796 CGF.ErrorUnsupported(E, "virtual base in offsetof"); 2797 continue; 2798 } 2799 2800 RecordDecl *RD = CurrentType->castAs<RecordType>()->getDecl(); 2801 const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD); 2802 2803 // Save the element type. 2804 CurrentType = ON.getBase()->getType(); 2805 2806 // Compute the offset to the base. 2807 const RecordType *BaseRT = CurrentType->getAs<RecordType>(); 2808 CXXRecordDecl *BaseRD = cast<CXXRecordDecl>(BaseRT->getDecl()); 2809 CharUnits OffsetInt = RL.getBaseClassOffset(BaseRD); 2810 Offset = llvm::ConstantInt::get(ResultType, OffsetInt.getQuantity()); 2811 break; 2812 } 2813 } 2814 Result = Builder.CreateAdd(Result, Offset); 2815 } 2816 return Result; 2817 } 2818 2819 /// VisitUnaryExprOrTypeTraitExpr - Return the size or alignment of the type of 2820 /// argument of the sizeof expression as an integer. 2821 Value * 2822 ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr( 2823 const UnaryExprOrTypeTraitExpr *E) { 2824 QualType TypeToSize = E->getTypeOfArgument(); 2825 if (E->getKind() == UETT_SizeOf) { 2826 if (const VariableArrayType *VAT = 2827 CGF.getContext().getAsVariableArrayType(TypeToSize)) { 2828 if (E->isArgumentType()) { 2829 // sizeof(type) - make sure to emit the VLA size. 2830 CGF.EmitVariablyModifiedType(TypeToSize); 2831 } else { 2832 // C99 6.5.3.4p2: If the argument is an expression of type 2833 // VLA, it is evaluated. 2834 CGF.EmitIgnoredExpr(E->getArgumentExpr()); 2835 } 2836 2837 auto VlaSize = CGF.getVLASize(VAT); 2838 llvm::Value *size = VlaSize.NumElts; 2839 2840 // Scale the number of non-VLA elements by the non-VLA element size. 2841 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(VlaSize.Type); 2842 if (!eltSize.isOne()) 2843 size = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), size); 2844 2845 return size; 2846 } 2847 } else if (E->getKind() == UETT_OpenMPRequiredSimdAlign) { 2848 auto Alignment = 2849 CGF.getContext() 2850 .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( 2851 E->getTypeOfArgument()->getPointeeType())) 2852 .getQuantity(); 2853 return llvm::ConstantInt::get(CGF.SizeTy, Alignment); 2854 } 2855 2856 // If this isn't sizeof(vla), the result must be constant; use the constant 2857 // folding logic so we don't have to duplicate it here. 2858 return Builder.getInt(E->EvaluateKnownConstInt(CGF.getContext())); 2859 } 2860 2861 Value *ScalarExprEmitter::VisitUnaryReal(const UnaryOperator *E) { 2862 Expr *Op = E->getSubExpr(); 2863 if (Op->getType()->isAnyComplexType()) { 2864 // If it's an l-value, load through the appropriate subobject l-value. 2865 // Note that we have to ask E because Op might be an l-value that 2866 // this won't work for, e.g. an Obj-C property. 2867 if (E->isGLValue()) 2868 return CGF.EmitLoadOfLValue(CGF.EmitLValue(E), 2869 E->getExprLoc()).getScalarVal(); 2870 2871 // Otherwise, calculate and project. 2872 return CGF.EmitComplexExpr(Op, false, true).first; 2873 } 2874 2875 return Visit(Op); 2876 } 2877 2878 Value *ScalarExprEmitter::VisitUnaryImag(const UnaryOperator *E) { 2879 Expr *Op = E->getSubExpr(); 2880 if (Op->getType()->isAnyComplexType()) { 2881 // If it's an l-value, load through the appropriate subobject l-value. 2882 // Note that we have to ask E because Op might be an l-value that 2883 // this won't work for, e.g. an Obj-C property. 2884 if (Op->isGLValue()) 2885 return CGF.EmitLoadOfLValue(CGF.EmitLValue(E), 2886 E->getExprLoc()).getScalarVal(); 2887 2888 // Otherwise, calculate and project. 2889 return CGF.EmitComplexExpr(Op, true, false).second; 2890 } 2891 2892 // __imag on a scalar returns zero. Emit the subexpr to ensure side 2893 // effects are evaluated, but not the actual value. 2894 if (Op->isGLValue()) 2895 CGF.EmitLValue(Op); 2896 else 2897 CGF.EmitScalarExpr(Op, true); 2898 return llvm::Constant::getNullValue(ConvertType(E->getType())); 2899 } 2900 2901 //===----------------------------------------------------------------------===// 2902 // Binary Operators 2903 //===----------------------------------------------------------------------===// 2904 2905 BinOpInfo ScalarExprEmitter::EmitBinOps(const BinaryOperator *E) { 2906 TestAndClearIgnoreResultAssign(); 2907 BinOpInfo Result; 2908 Result.LHS = Visit(E->getLHS()); 2909 Result.RHS = Visit(E->getRHS()); 2910 Result.Ty = E->getType(); 2911 Result.Opcode = E->getOpcode(); 2912 Result.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); 2913 Result.E = E; 2914 return Result; 2915 } 2916 2917 LValue ScalarExprEmitter::EmitCompoundAssignLValue( 2918 const CompoundAssignOperator *E, 2919 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &), 2920 Value *&Result) { 2921 QualType LHSTy = E->getLHS()->getType(); 2922 BinOpInfo OpInfo; 2923 2924 if (E->getComputationResultType()->isAnyComplexType()) 2925 return CGF.EmitScalarCompoundAssignWithComplex(E, Result); 2926 2927 // Emit the RHS first. __block variables need to have the rhs evaluated 2928 // first, plus this should improve codegen a little. 2929 OpInfo.RHS = Visit(E->getRHS()); 2930 OpInfo.Ty = E->getComputationResultType(); 2931 OpInfo.Opcode = E->getOpcode(); 2932 OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); 2933 OpInfo.E = E; 2934 // Load/convert the LHS. 2935 LValue LHSLV = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store); 2936 2937 llvm::PHINode *atomicPHI = nullptr; 2938 if (const AtomicType *atomicTy = LHSTy->getAs<AtomicType>()) { 2939 QualType type = atomicTy->getValueType(); 2940 if (!type->isBooleanType() && type->isIntegerType() && 2941 !(type->isUnsignedIntegerType() && 2942 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow)) && 2943 CGF.getLangOpts().getSignedOverflowBehavior() != 2944 LangOptions::SOB_Trapping) { 2945 llvm::AtomicRMWInst::BinOp AtomicOp = llvm::AtomicRMWInst::BAD_BINOP; 2946 llvm::Instruction::BinaryOps Op; 2947 switch (OpInfo.Opcode) { 2948 // We don't have atomicrmw operands for *, %, /, <<, >> 2949 case BO_MulAssign: case BO_DivAssign: 2950 case BO_RemAssign: 2951 case BO_ShlAssign: 2952 case BO_ShrAssign: 2953 break; 2954 case BO_AddAssign: 2955 AtomicOp = llvm::AtomicRMWInst::Add; 2956 Op = llvm::Instruction::Add; 2957 break; 2958 case BO_SubAssign: 2959 AtomicOp = llvm::AtomicRMWInst::Sub; 2960 Op = llvm::Instruction::Sub; 2961 break; 2962 case BO_AndAssign: 2963 AtomicOp = llvm::AtomicRMWInst::And; 2964 Op = llvm::Instruction::And; 2965 break; 2966 case BO_XorAssign: 2967 AtomicOp = llvm::AtomicRMWInst::Xor; 2968 Op = llvm::Instruction::Xor; 2969 break; 2970 case BO_OrAssign: 2971 AtomicOp = llvm::AtomicRMWInst::Or; 2972 Op = llvm::Instruction::Or; 2973 break; 2974 default: 2975 llvm_unreachable("Invalid compound assignment type"); 2976 } 2977 if (AtomicOp != llvm::AtomicRMWInst::BAD_BINOP) { 2978 llvm::Value *Amt = CGF.EmitToMemory( 2979 EmitScalarConversion(OpInfo.RHS, E->getRHS()->getType(), LHSTy, 2980 E->getExprLoc()), 2981 LHSTy); 2982 Value *OldVal = Builder.CreateAtomicRMW( 2983 AtomicOp, LHSLV.getPointer(CGF), Amt, 2984 llvm::AtomicOrdering::SequentiallyConsistent); 2985 2986 // Since operation is atomic, the result type is guaranteed to be the 2987 // same as the input in LLVM terms. 2988 Result = Builder.CreateBinOp(Op, OldVal, Amt); 2989 return LHSLV; 2990 } 2991 } 2992 // FIXME: For floating point types, we should be saving and restoring the 2993 // floating point environment in the loop. 2994 llvm::BasicBlock *startBB = Builder.GetInsertBlock(); 2995 llvm::BasicBlock *opBB = CGF.createBasicBlock("atomic_op", CGF.CurFn); 2996 OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->getExprLoc()); 2997 OpInfo.LHS = CGF.EmitToMemory(OpInfo.LHS, type); 2998 Builder.CreateBr(opBB); 2999 Builder.SetInsertPoint(opBB); 3000 atomicPHI = Builder.CreatePHI(OpInfo.LHS->getType(), 2); 3001 atomicPHI->addIncoming(OpInfo.LHS, startBB); 3002 OpInfo.LHS = atomicPHI; 3003 } 3004 else 3005 OpInfo.LHS = EmitLoadOfLValue(LHSLV, E->getExprLoc()); 3006 3007 SourceLocation Loc = E->getExprLoc(); 3008 OpInfo.LHS = 3009 EmitScalarConversion(OpInfo.LHS, LHSTy, E->getComputationLHSType(), Loc); 3010 3011 // Expand the binary operator. 3012 Result = (this->*Func)(OpInfo); 3013 3014 // Convert the result back to the LHS type, 3015 // potentially with Implicit Conversion sanitizer check. 3016 Result = EmitScalarConversion(Result, E->getComputationResultType(), LHSTy, 3017 Loc, ScalarConversionOpts(CGF.SanOpts)); 3018 3019 if (atomicPHI) { 3020 llvm::BasicBlock *curBlock = Builder.GetInsertBlock(); 3021 llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn); 3022 auto Pair = CGF.EmitAtomicCompareExchange( 3023 LHSLV, RValue::get(atomicPHI), RValue::get(Result), E->getExprLoc()); 3024 llvm::Value *old = CGF.EmitToMemory(Pair.first.getScalarVal(), LHSTy); 3025 llvm::Value *success = Pair.second; 3026 atomicPHI->addIncoming(old, curBlock); 3027 Builder.CreateCondBr(success, contBB, atomicPHI->getParent()); 3028 Builder.SetInsertPoint(contBB); 3029 return LHSLV; 3030 } 3031 3032 // Store the result value into the LHS lvalue. Bit-fields are handled 3033 // specially because the result is altered by the store, i.e., [C99 6.5.16p1] 3034 // 'An assignment expression has the value of the left operand after the 3035 // assignment...'. 3036 if (LHSLV.isBitField()) 3037 CGF.EmitStoreThroughBitfieldLValue(RValue::get(Result), LHSLV, &Result); 3038 else 3039 CGF.EmitStoreThroughLValue(RValue::get(Result), LHSLV); 3040 3041 if (CGF.getLangOpts().OpenMP) 3042 CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, 3043 E->getLHS()); 3044 return LHSLV; 3045 } 3046 3047 Value *ScalarExprEmitter::EmitCompoundAssign(const CompoundAssignOperator *E, 3048 Value *(ScalarExprEmitter::*Func)(const BinOpInfo &)) { 3049 bool Ignore = TestAndClearIgnoreResultAssign(); 3050 Value *RHS = nullptr; 3051 LValue LHS = EmitCompoundAssignLValue(E, Func, RHS); 3052 3053 // If the result is clearly ignored, return now. 3054 if (Ignore) 3055 return nullptr; 3056 3057 // The result of an assignment in C is the assigned r-value. 3058 if (!CGF.getLangOpts().CPlusPlus) 3059 return RHS; 3060 3061 // If the lvalue is non-volatile, return the computed value of the assignment. 3062 if (!LHS.isVolatileQualified()) 3063 return RHS; 3064 3065 // Otherwise, reload the value. 3066 return EmitLoadOfLValue(LHS, E->getExprLoc()); 3067 } 3068 3069 void ScalarExprEmitter::EmitUndefinedBehaviorIntegerDivAndRemCheck( 3070 const BinOpInfo &Ops, llvm::Value *Zero, bool isDiv) { 3071 SmallVector<std::pair<llvm::Value *, SanitizerMask>, 2> Checks; 3072 3073 if (CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero)) { 3074 Checks.push_back(std::make_pair(Builder.CreateICmpNE(Ops.RHS, Zero), 3075 SanitizerKind::IntegerDivideByZero)); 3076 } 3077 3078 const auto *BO = cast<BinaryOperator>(Ops.E); 3079 if (CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow) && 3080 Ops.Ty->hasSignedIntegerRepresentation() && 3081 !IsWidenedIntegerOp(CGF.getContext(), BO->getLHS()) && 3082 Ops.mayHaveIntegerOverflow()) { 3083 llvm::IntegerType *Ty = cast<llvm::IntegerType>(Zero->getType()); 3084 3085 llvm::Value *IntMin = 3086 Builder.getInt(llvm::APInt::getSignedMinValue(Ty->getBitWidth())); 3087 llvm::Value *NegOne = llvm::ConstantInt::get(Ty, -1ULL); 3088 3089 llvm::Value *LHSCmp = Builder.CreateICmpNE(Ops.LHS, IntMin); 3090 llvm::Value *RHSCmp = Builder.CreateICmpNE(Ops.RHS, NegOne); 3091 llvm::Value *NotOverflow = Builder.CreateOr(LHSCmp, RHSCmp, "or"); 3092 Checks.push_back( 3093 std::make_pair(NotOverflow, SanitizerKind::SignedIntegerOverflow)); 3094 } 3095 3096 if (Checks.size() > 0) 3097 EmitBinOpCheck(Checks, Ops); 3098 } 3099 3100 Value *ScalarExprEmitter::EmitDiv(const BinOpInfo &Ops) { 3101 { 3102 CodeGenFunction::SanitizerScope SanScope(&CGF); 3103 if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) || 3104 CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) && 3105 Ops.Ty->isIntegerType() && 3106 (Ops.mayHaveIntegerDivisionByZero() || Ops.mayHaveIntegerOverflow())) { 3107 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); 3108 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, true); 3109 } else if (CGF.SanOpts.has(SanitizerKind::FloatDivideByZero) && 3110 Ops.Ty->isRealFloatingType() && 3111 Ops.mayHaveFloatDivisionByZero()) { 3112 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); 3113 llvm::Value *NonZero = Builder.CreateFCmpUNE(Ops.RHS, Zero); 3114 EmitBinOpCheck(std::make_pair(NonZero, SanitizerKind::FloatDivideByZero), 3115 Ops); 3116 } 3117 } 3118 3119 if (Ops.LHS->getType()->isFPOrFPVectorTy()) { 3120 llvm::Value *Val; 3121 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Ops.FPFeatures); 3122 Val = Builder.CreateFDiv(Ops.LHS, Ops.RHS, "div"); 3123 if (CGF.getLangOpts().OpenCL && 3124 !CGF.CGM.getCodeGenOpts().CorrectlyRoundedDivSqrt) { 3125 // OpenCL v1.1 s7.4: minimum accuracy of single precision / is 2.5ulp 3126 // OpenCL v1.2 s5.6.4.2: The -cl-fp32-correctly-rounded-divide-sqrt 3127 // build option allows an application to specify that single precision 3128 // floating-point divide (x/y and 1/x) and sqrt used in the program 3129 // source are correctly rounded. 3130 llvm::Type *ValTy = Val->getType(); 3131 if (ValTy->isFloatTy() || 3132 (isa<llvm::VectorType>(ValTy) && 3133 cast<llvm::VectorType>(ValTy)->getElementType()->isFloatTy())) 3134 CGF.SetFPAccuracy(Val, 2.5); 3135 } 3136 return Val; 3137 } 3138 else if (Ops.isFixedPointOp()) 3139 return EmitFixedPointBinOp(Ops); 3140 else if (Ops.Ty->hasUnsignedIntegerRepresentation()) 3141 return Builder.CreateUDiv(Ops.LHS, Ops.RHS, "div"); 3142 else 3143 return Builder.CreateSDiv(Ops.LHS, Ops.RHS, "div"); 3144 } 3145 3146 Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) { 3147 // Rem in C can't be a floating point type: C99 6.5.5p2. 3148 if ((CGF.SanOpts.has(SanitizerKind::IntegerDivideByZero) || 3149 CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) && 3150 Ops.Ty->isIntegerType() && 3151 (Ops.mayHaveIntegerDivisionByZero() || Ops.mayHaveIntegerOverflow())) { 3152 CodeGenFunction::SanitizerScope SanScope(&CGF); 3153 llvm::Value *Zero = llvm::Constant::getNullValue(ConvertType(Ops.Ty)); 3154 EmitUndefinedBehaviorIntegerDivAndRemCheck(Ops, Zero, false); 3155 } 3156 3157 if (Ops.Ty->hasUnsignedIntegerRepresentation()) 3158 return Builder.CreateURem(Ops.LHS, Ops.RHS, "rem"); 3159 else 3160 return Builder.CreateSRem(Ops.LHS, Ops.RHS, "rem"); 3161 } 3162 3163 Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) { 3164 unsigned IID; 3165 unsigned OpID = 0; 3166 3167 bool isSigned = Ops.Ty->isSignedIntegerOrEnumerationType(); 3168 switch (Ops.Opcode) { 3169 case BO_Add: 3170 case BO_AddAssign: 3171 OpID = 1; 3172 IID = isSigned ? llvm::Intrinsic::sadd_with_overflow : 3173 llvm::Intrinsic::uadd_with_overflow; 3174 break; 3175 case BO_Sub: 3176 case BO_SubAssign: 3177 OpID = 2; 3178 IID = isSigned ? llvm::Intrinsic::ssub_with_overflow : 3179 llvm::Intrinsic::usub_with_overflow; 3180 break; 3181 case BO_Mul: 3182 case BO_MulAssign: 3183 OpID = 3; 3184 IID = isSigned ? llvm::Intrinsic::smul_with_overflow : 3185 llvm::Intrinsic::umul_with_overflow; 3186 break; 3187 default: 3188 llvm_unreachable("Unsupported operation for overflow detection"); 3189 } 3190 OpID <<= 1; 3191 if (isSigned) 3192 OpID |= 1; 3193 3194 CodeGenFunction::SanitizerScope SanScope(&CGF); 3195 llvm::Type *opTy = CGF.CGM.getTypes().ConvertType(Ops.Ty); 3196 3197 llvm::Function *intrinsic = CGF.CGM.getIntrinsic(IID, opTy); 3198 3199 Value *resultAndOverflow = Builder.CreateCall(intrinsic, {Ops.LHS, Ops.RHS}); 3200 Value *result = Builder.CreateExtractValue(resultAndOverflow, 0); 3201 Value *overflow = Builder.CreateExtractValue(resultAndOverflow, 1); 3202 3203 // Handle overflow with llvm.trap if no custom handler has been specified. 3204 const std::string *handlerName = 3205 &CGF.getLangOpts().OverflowHandler; 3206 if (handlerName->empty()) { 3207 // If the signed-integer-overflow sanitizer is enabled, emit a call to its 3208 // runtime. Otherwise, this is a -ftrapv check, so just emit a trap. 3209 if (!isSigned || CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) { 3210 llvm::Value *NotOverflow = Builder.CreateNot(overflow); 3211 SanitizerMask Kind = isSigned ? SanitizerKind::SignedIntegerOverflow 3212 : SanitizerKind::UnsignedIntegerOverflow; 3213 EmitBinOpCheck(std::make_pair(NotOverflow, Kind), Ops); 3214 } else 3215 CGF.EmitTrapCheck(Builder.CreateNot(overflow)); 3216 return result; 3217 } 3218 3219 // Branch in case of overflow. 3220 llvm::BasicBlock *initialBB = Builder.GetInsertBlock(); 3221 llvm::BasicBlock *continueBB = 3222 CGF.createBasicBlock("nooverflow", CGF.CurFn, initialBB->getNextNode()); 3223 llvm::BasicBlock *overflowBB = CGF.createBasicBlock("overflow", CGF.CurFn); 3224 3225 Builder.CreateCondBr(overflow, overflowBB, continueBB); 3226 3227 // If an overflow handler is set, then we want to call it and then use its 3228 // result, if it returns. 3229 Builder.SetInsertPoint(overflowBB); 3230 3231 // Get the overflow handler. 3232 llvm::Type *Int8Ty = CGF.Int8Ty; 3233 llvm::Type *argTypes[] = { CGF.Int64Ty, CGF.Int64Ty, Int8Ty, Int8Ty }; 3234 llvm::FunctionType *handlerTy = 3235 llvm::FunctionType::get(CGF.Int64Ty, argTypes, true); 3236 llvm::FunctionCallee handler = 3237 CGF.CGM.CreateRuntimeFunction(handlerTy, *handlerName); 3238 3239 // Sign extend the args to 64-bit, so that we can use the same handler for 3240 // all types of overflow. 3241 llvm::Value *lhs = Builder.CreateSExt(Ops.LHS, CGF.Int64Ty); 3242 llvm::Value *rhs = Builder.CreateSExt(Ops.RHS, CGF.Int64Ty); 3243 3244 // Call the handler with the two arguments, the operation, and the size of 3245 // the result. 3246 llvm::Value *handlerArgs[] = { 3247 lhs, 3248 rhs, 3249 Builder.getInt8(OpID), 3250 Builder.getInt8(cast<llvm::IntegerType>(opTy)->getBitWidth()) 3251 }; 3252 llvm::Value *handlerResult = 3253 CGF.EmitNounwindRuntimeCall(handler, handlerArgs); 3254 3255 // Truncate the result back to the desired size. 3256 handlerResult = Builder.CreateTrunc(handlerResult, opTy); 3257 Builder.CreateBr(continueBB); 3258 3259 Builder.SetInsertPoint(continueBB); 3260 llvm::PHINode *phi = Builder.CreatePHI(opTy, 2); 3261 phi->addIncoming(result, initialBB); 3262 phi->addIncoming(handlerResult, overflowBB); 3263 3264 return phi; 3265 } 3266 3267 /// Emit pointer + index arithmetic. 3268 static Value *emitPointerArithmetic(CodeGenFunction &CGF, 3269 const BinOpInfo &op, 3270 bool isSubtraction) { 3271 // Must have binary (not unary) expr here. Unary pointer 3272 // increment/decrement doesn't use this path. 3273 const BinaryOperator *expr = cast<BinaryOperator>(op.E); 3274 3275 Value *pointer = op.LHS; 3276 Expr *pointerOperand = expr->getLHS(); 3277 Value *index = op.RHS; 3278 Expr *indexOperand = expr->getRHS(); 3279 3280 // In a subtraction, the LHS is always the pointer. 3281 if (!isSubtraction && !pointer->getType()->isPointerTy()) { 3282 std::swap(pointer, index); 3283 std::swap(pointerOperand, indexOperand); 3284 } 3285 3286 bool isSigned = indexOperand->getType()->isSignedIntegerOrEnumerationType(); 3287 3288 unsigned width = cast<llvm::IntegerType>(index->getType())->getBitWidth(); 3289 auto &DL = CGF.CGM.getDataLayout(); 3290 auto PtrTy = cast<llvm::PointerType>(pointer->getType()); 3291 3292 // Some versions of glibc and gcc use idioms (particularly in their malloc 3293 // routines) that add a pointer-sized integer (known to be a pointer value) 3294 // to a null pointer in order to cast the value back to an integer or as 3295 // part of a pointer alignment algorithm. This is undefined behavior, but 3296 // we'd like to be able to compile programs that use it. 3297 // 3298 // Normally, we'd generate a GEP with a null-pointer base here in response 3299 // to that code, but it's also UB to dereference a pointer created that 3300 // way. Instead (as an acknowledged hack to tolerate the idiom) we will 3301 // generate a direct cast of the integer value to a pointer. 3302 // 3303 // The idiom (p = nullptr + N) is not met if any of the following are true: 3304 // 3305 // The operation is subtraction. 3306 // The index is not pointer-sized. 3307 // The pointer type is not byte-sized. 3308 // 3309 if (BinaryOperator::isNullPointerArithmeticExtension(CGF.getContext(), 3310 op.Opcode, 3311 expr->getLHS(), 3312 expr->getRHS())) 3313 return CGF.Builder.CreateIntToPtr(index, pointer->getType()); 3314 3315 if (width != DL.getIndexTypeSizeInBits(PtrTy)) { 3316 // Zero-extend or sign-extend the pointer value according to 3317 // whether the index is signed or not. 3318 index = CGF.Builder.CreateIntCast(index, DL.getIndexType(PtrTy), isSigned, 3319 "idx.ext"); 3320 } 3321 3322 // If this is subtraction, negate the index. 3323 if (isSubtraction) 3324 index = CGF.Builder.CreateNeg(index, "idx.neg"); 3325 3326 if (CGF.SanOpts.has(SanitizerKind::ArrayBounds)) 3327 CGF.EmitBoundsCheck(op.E, pointerOperand, index, indexOperand->getType(), 3328 /*Accessed*/ false); 3329 3330 const PointerType *pointerType 3331 = pointerOperand->getType()->getAs<PointerType>(); 3332 if (!pointerType) { 3333 QualType objectType = pointerOperand->getType() 3334 ->castAs<ObjCObjectPointerType>() 3335 ->getPointeeType(); 3336 llvm::Value *objectSize 3337 = CGF.CGM.getSize(CGF.getContext().getTypeSizeInChars(objectType)); 3338 3339 index = CGF.Builder.CreateMul(index, objectSize); 3340 3341 Value *result = CGF.Builder.CreateBitCast(pointer, CGF.VoidPtrTy); 3342 result = CGF.Builder.CreateGEP(result, index, "add.ptr"); 3343 return CGF.Builder.CreateBitCast(result, pointer->getType()); 3344 } 3345 3346 QualType elementType = pointerType->getPointeeType(); 3347 if (const VariableArrayType *vla 3348 = CGF.getContext().getAsVariableArrayType(elementType)) { 3349 // The element count here is the total number of non-VLA elements. 3350 llvm::Value *numElements = CGF.getVLASize(vla).NumElts; 3351 3352 // Effectively, the multiply by the VLA size is part of the GEP. 3353 // GEP indexes are signed, and scaling an index isn't permitted to 3354 // signed-overflow, so we use the same semantics for our explicit 3355 // multiply. We suppress this if overflow is not undefined behavior. 3356 if (CGF.getLangOpts().isSignedOverflowDefined()) { 3357 index = CGF.Builder.CreateMul(index, numElements, "vla.index"); 3358 pointer = CGF.Builder.CreateGEP(pointer, index, "add.ptr"); 3359 } else { 3360 index = CGF.Builder.CreateNSWMul(index, numElements, "vla.index"); 3361 pointer = 3362 CGF.EmitCheckedInBoundsGEP(pointer, index, isSigned, isSubtraction, 3363 op.E->getExprLoc(), "add.ptr"); 3364 } 3365 return pointer; 3366 } 3367 3368 // Explicitly handle GNU void* and function pointer arithmetic extensions. The 3369 // GNU void* casts amount to no-ops since our void* type is i8*, but this is 3370 // future proof. 3371 if (elementType->isVoidType() || elementType->isFunctionType()) { 3372 Value *result = CGF.EmitCastToVoidPtr(pointer); 3373 result = CGF.Builder.CreateGEP(result, index, "add.ptr"); 3374 return CGF.Builder.CreateBitCast(result, pointer->getType()); 3375 } 3376 3377 if (CGF.getLangOpts().isSignedOverflowDefined()) 3378 return CGF.Builder.CreateGEP(pointer, index, "add.ptr"); 3379 3380 return CGF.EmitCheckedInBoundsGEP(pointer, index, isSigned, isSubtraction, 3381 op.E->getExprLoc(), "add.ptr"); 3382 } 3383 3384 // Construct an fmuladd intrinsic to represent a fused mul-add of MulOp and 3385 // Addend. Use negMul and negAdd to negate the first operand of the Mul or 3386 // the add operand respectively. This allows fmuladd to represent a*b-c, or 3387 // c-a*b. Patterns in LLVM should catch the negated forms and translate them to 3388 // efficient operations. 3389 static Value* buildFMulAdd(llvm::Instruction *MulOp, Value *Addend, 3390 const CodeGenFunction &CGF, CGBuilderTy &Builder, 3391 bool negMul, bool negAdd) { 3392 assert(!(negMul && negAdd) && "Only one of negMul and negAdd should be set."); 3393 3394 Value *MulOp0 = MulOp->getOperand(0); 3395 Value *MulOp1 = MulOp->getOperand(1); 3396 if (negMul) 3397 MulOp0 = Builder.CreateFNeg(MulOp0, "neg"); 3398 if (negAdd) 3399 Addend = Builder.CreateFNeg(Addend, "neg"); 3400 3401 Value *FMulAdd = nullptr; 3402 if (Builder.getIsFPConstrained()) { 3403 assert(isa<llvm::ConstrainedFPIntrinsic>(MulOp) && 3404 "Only constrained operation should be created when Builder is in FP " 3405 "constrained mode"); 3406 FMulAdd = Builder.CreateConstrainedFPCall( 3407 CGF.CGM.getIntrinsic(llvm::Intrinsic::experimental_constrained_fmuladd, 3408 Addend->getType()), 3409 {MulOp0, MulOp1, Addend}); 3410 } else { 3411 FMulAdd = Builder.CreateCall( 3412 CGF.CGM.getIntrinsic(llvm::Intrinsic::fmuladd, Addend->getType()), 3413 {MulOp0, MulOp1, Addend}); 3414 } 3415 MulOp->eraseFromParent(); 3416 3417 return FMulAdd; 3418 } 3419 3420 // Check whether it would be legal to emit an fmuladd intrinsic call to 3421 // represent op and if so, build the fmuladd. 3422 // 3423 // Checks that (a) the operation is fusable, and (b) -ffp-contract=on. 3424 // Does NOT check the type of the operation - it's assumed that this function 3425 // will be called from contexts where it's known that the type is contractable. 3426 static Value* tryEmitFMulAdd(const BinOpInfo &op, 3427 const CodeGenFunction &CGF, CGBuilderTy &Builder, 3428 bool isSub=false) { 3429 3430 assert((op.Opcode == BO_Add || op.Opcode == BO_AddAssign || 3431 op.Opcode == BO_Sub || op.Opcode == BO_SubAssign) && 3432 "Only fadd/fsub can be the root of an fmuladd."); 3433 3434 // Check whether this op is marked as fusable. 3435 if (!op.FPFeatures.allowFPContractWithinStatement()) 3436 return nullptr; 3437 3438 // We have a potentially fusable op. Look for a mul on one of the operands. 3439 // Also, make sure that the mul result isn't used directly. In that case, 3440 // there's no point creating a muladd operation. 3441 if (auto *LHSBinOp = dyn_cast<llvm::BinaryOperator>(op.LHS)) { 3442 if (LHSBinOp->getOpcode() == llvm::Instruction::FMul && 3443 LHSBinOp->use_empty()) 3444 return buildFMulAdd(LHSBinOp, op.RHS, CGF, Builder, false, isSub); 3445 } 3446 if (auto *RHSBinOp = dyn_cast<llvm::BinaryOperator>(op.RHS)) { 3447 if (RHSBinOp->getOpcode() == llvm::Instruction::FMul && 3448 RHSBinOp->use_empty()) 3449 return buildFMulAdd(RHSBinOp, op.LHS, CGF, Builder, isSub, false); 3450 } 3451 3452 if (auto *LHSBinOp = dyn_cast<llvm::CallBase>(op.LHS)) { 3453 if (LHSBinOp->getIntrinsicID() == 3454 llvm::Intrinsic::experimental_constrained_fmul && 3455 LHSBinOp->use_empty()) 3456 return buildFMulAdd(LHSBinOp, op.RHS, CGF, Builder, false, isSub); 3457 } 3458 if (auto *RHSBinOp = dyn_cast<llvm::CallBase>(op.RHS)) { 3459 if (RHSBinOp->getIntrinsicID() == 3460 llvm::Intrinsic::experimental_constrained_fmul && 3461 RHSBinOp->use_empty()) 3462 return buildFMulAdd(RHSBinOp, op.LHS, CGF, Builder, isSub, false); 3463 } 3464 3465 return nullptr; 3466 } 3467 3468 Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &op) { 3469 if (op.LHS->getType()->isPointerTy() || 3470 op.RHS->getType()->isPointerTy()) 3471 return emitPointerArithmetic(CGF, op, CodeGenFunction::NotSubtraction); 3472 3473 if (op.Ty->isSignedIntegerOrEnumerationType()) { 3474 switch (CGF.getLangOpts().getSignedOverflowBehavior()) { 3475 case LangOptions::SOB_Defined: 3476 return Builder.CreateAdd(op.LHS, op.RHS, "add"); 3477 case LangOptions::SOB_Undefined: 3478 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) 3479 return Builder.CreateNSWAdd(op.LHS, op.RHS, "add"); 3480 LLVM_FALLTHROUGH; 3481 case LangOptions::SOB_Trapping: 3482 if (CanElideOverflowCheck(CGF.getContext(), op)) 3483 return Builder.CreateNSWAdd(op.LHS, op.RHS, "add"); 3484 return EmitOverflowCheckedBinOp(op); 3485 } 3486 } 3487 3488 if (op.Ty->isConstantMatrixType()) { 3489 llvm::MatrixBuilder<CGBuilderTy> MB(Builder); 3490 return MB.CreateAdd(op.LHS, op.RHS); 3491 } 3492 3493 if (op.Ty->isUnsignedIntegerType() && 3494 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow) && 3495 !CanElideOverflowCheck(CGF.getContext(), op)) 3496 return EmitOverflowCheckedBinOp(op); 3497 3498 if (op.LHS->getType()->isFPOrFPVectorTy()) { 3499 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, op.FPFeatures); 3500 // Try to form an fmuladd. 3501 if (Value *FMulAdd = tryEmitFMulAdd(op, CGF, Builder)) 3502 return FMulAdd; 3503 3504 return Builder.CreateFAdd(op.LHS, op.RHS, "add"); 3505 } 3506 3507 if (op.isFixedPointOp()) 3508 return EmitFixedPointBinOp(op); 3509 3510 return Builder.CreateAdd(op.LHS, op.RHS, "add"); 3511 } 3512 3513 /// The resulting value must be calculated with exact precision, so the operands 3514 /// may not be the same type. 3515 Value *ScalarExprEmitter::EmitFixedPointBinOp(const BinOpInfo &op) { 3516 using llvm::APSInt; 3517 using llvm::ConstantInt; 3518 3519 // This is either a binary operation where at least one of the operands is 3520 // a fixed-point type, or a unary operation where the operand is a fixed-point 3521 // type. The result type of a binary operation is determined by 3522 // Sema::handleFixedPointConversions(). 3523 QualType ResultTy = op.Ty; 3524 QualType LHSTy, RHSTy; 3525 if (const auto *BinOp = dyn_cast<BinaryOperator>(op.E)) { 3526 RHSTy = BinOp->getRHS()->getType(); 3527 if (const auto *CAO = dyn_cast<CompoundAssignOperator>(BinOp)) { 3528 // For compound assignment, the effective type of the LHS at this point 3529 // is the computation LHS type, not the actual LHS type, and the final 3530 // result type is not the type of the expression but rather the 3531 // computation result type. 3532 LHSTy = CAO->getComputationLHSType(); 3533 ResultTy = CAO->getComputationResultType(); 3534 } else 3535 LHSTy = BinOp->getLHS()->getType(); 3536 } else if (const auto *UnOp = dyn_cast<UnaryOperator>(op.E)) { 3537 LHSTy = UnOp->getSubExpr()->getType(); 3538 RHSTy = UnOp->getSubExpr()->getType(); 3539 } 3540 ASTContext &Ctx = CGF.getContext(); 3541 Value *LHS = op.LHS; 3542 Value *RHS = op.RHS; 3543 3544 auto LHSFixedSema = Ctx.getFixedPointSemantics(LHSTy); 3545 auto RHSFixedSema = Ctx.getFixedPointSemantics(RHSTy); 3546 auto ResultFixedSema = Ctx.getFixedPointSemantics(ResultTy); 3547 auto CommonFixedSema = LHSFixedSema.getCommonSemantics(RHSFixedSema); 3548 3549 // Perform the actual operation. 3550 Value *Result; 3551 llvm::FixedPointBuilder<CGBuilderTy> FPBuilder(Builder); 3552 switch (op.Opcode) { 3553 case BO_AddAssign: 3554 case BO_Add: 3555 Result = FPBuilder.CreateAdd(LHS, LHSFixedSema, RHS, RHSFixedSema); 3556 break; 3557 case BO_SubAssign: 3558 case BO_Sub: 3559 Result = FPBuilder.CreateSub(LHS, LHSFixedSema, RHS, RHSFixedSema); 3560 break; 3561 case BO_MulAssign: 3562 case BO_Mul: 3563 Result = FPBuilder.CreateMul(LHS, LHSFixedSema, RHS, RHSFixedSema); 3564 break; 3565 case BO_DivAssign: 3566 case BO_Div: 3567 Result = FPBuilder.CreateDiv(LHS, LHSFixedSema, RHS, RHSFixedSema); 3568 break; 3569 case BO_ShlAssign: 3570 case BO_Shl: 3571 Result = FPBuilder.CreateShl(LHS, LHSFixedSema, RHS); 3572 break; 3573 case BO_ShrAssign: 3574 case BO_Shr: 3575 Result = FPBuilder.CreateShr(LHS, LHSFixedSema, RHS); 3576 break; 3577 case BO_LT: 3578 return FPBuilder.CreateLT(LHS, LHSFixedSema, RHS, RHSFixedSema); 3579 case BO_GT: 3580 return FPBuilder.CreateGT(LHS, LHSFixedSema, RHS, RHSFixedSema); 3581 case BO_LE: 3582 return FPBuilder.CreateLE(LHS, LHSFixedSema, RHS, RHSFixedSema); 3583 case BO_GE: 3584 return FPBuilder.CreateGE(LHS, LHSFixedSema, RHS, RHSFixedSema); 3585 case BO_EQ: 3586 // For equality operations, we assume any padding bits on unsigned types are 3587 // zero'd out. They could be overwritten through non-saturating operations 3588 // that cause overflow, but this leads to undefined behavior. 3589 return FPBuilder.CreateEQ(LHS, LHSFixedSema, RHS, RHSFixedSema); 3590 case BO_NE: 3591 return FPBuilder.CreateNE(LHS, LHSFixedSema, RHS, RHSFixedSema); 3592 case BO_Cmp: 3593 case BO_LAnd: 3594 case BO_LOr: 3595 llvm_unreachable("Found unimplemented fixed point binary operation"); 3596 case BO_PtrMemD: 3597 case BO_PtrMemI: 3598 case BO_Rem: 3599 case BO_Xor: 3600 case BO_And: 3601 case BO_Or: 3602 case BO_Assign: 3603 case BO_RemAssign: 3604 case BO_AndAssign: 3605 case BO_XorAssign: 3606 case BO_OrAssign: 3607 case BO_Comma: 3608 llvm_unreachable("Found unsupported binary operation for fixed point types."); 3609 } 3610 3611 bool IsShift = BinaryOperator::isShiftOp(op.Opcode) || 3612 BinaryOperator::isShiftAssignOp(op.Opcode); 3613 // Convert to the result type. 3614 return FPBuilder.CreateFixedToFixed(Result, IsShift ? LHSFixedSema 3615 : CommonFixedSema, 3616 ResultFixedSema); 3617 } 3618 3619 Value *ScalarExprEmitter::EmitSub(const BinOpInfo &op) { 3620 // The LHS is always a pointer if either side is. 3621 if (!op.LHS->getType()->isPointerTy()) { 3622 if (op.Ty->isSignedIntegerOrEnumerationType()) { 3623 switch (CGF.getLangOpts().getSignedOverflowBehavior()) { 3624 case LangOptions::SOB_Defined: 3625 return Builder.CreateSub(op.LHS, op.RHS, "sub"); 3626 case LangOptions::SOB_Undefined: 3627 if (!CGF.SanOpts.has(SanitizerKind::SignedIntegerOverflow)) 3628 return Builder.CreateNSWSub(op.LHS, op.RHS, "sub"); 3629 LLVM_FALLTHROUGH; 3630 case LangOptions::SOB_Trapping: 3631 if (CanElideOverflowCheck(CGF.getContext(), op)) 3632 return Builder.CreateNSWSub(op.LHS, op.RHS, "sub"); 3633 return EmitOverflowCheckedBinOp(op); 3634 } 3635 } 3636 3637 if (op.Ty->isConstantMatrixType()) { 3638 llvm::MatrixBuilder<CGBuilderTy> MB(Builder); 3639 return MB.CreateSub(op.LHS, op.RHS); 3640 } 3641 3642 if (op.Ty->isUnsignedIntegerType() && 3643 CGF.SanOpts.has(SanitizerKind::UnsignedIntegerOverflow) && 3644 !CanElideOverflowCheck(CGF.getContext(), op)) 3645 return EmitOverflowCheckedBinOp(op); 3646 3647 if (op.LHS->getType()->isFPOrFPVectorTy()) { 3648 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, op.FPFeatures); 3649 // Try to form an fmuladd. 3650 if (Value *FMulAdd = tryEmitFMulAdd(op, CGF, Builder, true)) 3651 return FMulAdd; 3652 return Builder.CreateFSub(op.LHS, op.RHS, "sub"); 3653 } 3654 3655 if (op.isFixedPointOp()) 3656 return EmitFixedPointBinOp(op); 3657 3658 return Builder.CreateSub(op.LHS, op.RHS, "sub"); 3659 } 3660 3661 // If the RHS is not a pointer, then we have normal pointer 3662 // arithmetic. 3663 if (!op.RHS->getType()->isPointerTy()) 3664 return emitPointerArithmetic(CGF, op, CodeGenFunction::IsSubtraction); 3665 3666 // Otherwise, this is a pointer subtraction. 3667 3668 // Do the raw subtraction part. 3669 llvm::Value *LHS 3670 = Builder.CreatePtrToInt(op.LHS, CGF.PtrDiffTy, "sub.ptr.lhs.cast"); 3671 llvm::Value *RHS 3672 = Builder.CreatePtrToInt(op.RHS, CGF.PtrDiffTy, "sub.ptr.rhs.cast"); 3673 Value *diffInChars = Builder.CreateSub(LHS, RHS, "sub.ptr.sub"); 3674 3675 // Okay, figure out the element size. 3676 const BinaryOperator *expr = cast<BinaryOperator>(op.E); 3677 QualType elementType = expr->getLHS()->getType()->getPointeeType(); 3678 3679 llvm::Value *divisor = nullptr; 3680 3681 // For a variable-length array, this is going to be non-constant. 3682 if (const VariableArrayType *vla 3683 = CGF.getContext().getAsVariableArrayType(elementType)) { 3684 auto VlaSize = CGF.getVLASize(vla); 3685 elementType = VlaSize.Type; 3686 divisor = VlaSize.NumElts; 3687 3688 // Scale the number of non-VLA elements by the non-VLA element size. 3689 CharUnits eltSize = CGF.getContext().getTypeSizeInChars(elementType); 3690 if (!eltSize.isOne()) 3691 divisor = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), divisor); 3692 3693 // For everything elese, we can just compute it, safe in the 3694 // assumption that Sema won't let anything through that we can't 3695 // safely compute the size of. 3696 } else { 3697 CharUnits elementSize; 3698 // Handle GCC extension for pointer arithmetic on void* and 3699 // function pointer types. 3700 if (elementType->isVoidType() || elementType->isFunctionType()) 3701 elementSize = CharUnits::One(); 3702 else 3703 elementSize = CGF.getContext().getTypeSizeInChars(elementType); 3704 3705 // Don't even emit the divide for element size of 1. 3706 if (elementSize.isOne()) 3707 return diffInChars; 3708 3709 divisor = CGF.CGM.getSize(elementSize); 3710 } 3711 3712 // Otherwise, do a full sdiv. This uses the "exact" form of sdiv, since 3713 // pointer difference in C is only defined in the case where both operands 3714 // are pointing to elements of an array. 3715 return Builder.CreateExactSDiv(diffInChars, divisor, "sub.ptr.div"); 3716 } 3717 3718 Value *ScalarExprEmitter::GetWidthMinusOneValue(Value* LHS,Value* RHS) { 3719 llvm::IntegerType *Ty; 3720 if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(LHS->getType())) 3721 Ty = cast<llvm::IntegerType>(VT->getElementType()); 3722 else 3723 Ty = cast<llvm::IntegerType>(LHS->getType()); 3724 return llvm::ConstantInt::get(RHS->getType(), Ty->getBitWidth() - 1); 3725 } 3726 3727 Value *ScalarExprEmitter::ConstrainShiftValue(Value *LHS, Value *RHS, 3728 const Twine &Name) { 3729 llvm::IntegerType *Ty; 3730 if (auto *VT = dyn_cast<llvm::VectorType>(LHS->getType())) 3731 Ty = cast<llvm::IntegerType>(VT->getElementType()); 3732 else 3733 Ty = cast<llvm::IntegerType>(LHS->getType()); 3734 3735 if (llvm::isPowerOf2_64(Ty->getBitWidth())) 3736 return Builder.CreateAnd(RHS, GetWidthMinusOneValue(LHS, RHS), Name); 3737 3738 return Builder.CreateURem( 3739 RHS, llvm::ConstantInt::get(RHS->getType(), Ty->getBitWidth()), Name); 3740 } 3741 3742 Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) { 3743 // TODO: This misses out on the sanitizer check below. 3744 if (Ops.isFixedPointOp()) 3745 return EmitFixedPointBinOp(Ops); 3746 3747 // LLVM requires the LHS and RHS to be the same type: promote or truncate the 3748 // RHS to the same size as the LHS. 3749 Value *RHS = Ops.RHS; 3750 if (Ops.LHS->getType() != RHS->getType()) 3751 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); 3752 3753 bool SanitizeSignedBase = CGF.SanOpts.has(SanitizerKind::ShiftBase) && 3754 Ops.Ty->hasSignedIntegerRepresentation() && 3755 !CGF.getLangOpts().isSignedOverflowDefined() && 3756 !CGF.getLangOpts().CPlusPlus20; 3757 bool SanitizeUnsignedBase = 3758 CGF.SanOpts.has(SanitizerKind::UnsignedShiftBase) && 3759 Ops.Ty->hasUnsignedIntegerRepresentation(); 3760 bool SanitizeBase = SanitizeSignedBase || SanitizeUnsignedBase; 3761 bool SanitizeExponent = CGF.SanOpts.has(SanitizerKind::ShiftExponent); 3762 // OpenCL 6.3j: shift values are effectively % word size of LHS. 3763 if (CGF.getLangOpts().OpenCL) 3764 RHS = ConstrainShiftValue(Ops.LHS, RHS, "shl.mask"); 3765 else if ((SanitizeBase || SanitizeExponent) && 3766 isa<llvm::IntegerType>(Ops.LHS->getType())) { 3767 CodeGenFunction::SanitizerScope SanScope(&CGF); 3768 SmallVector<std::pair<Value *, SanitizerMask>, 2> Checks; 3769 llvm::Value *WidthMinusOne = GetWidthMinusOneValue(Ops.LHS, Ops.RHS); 3770 llvm::Value *ValidExponent = Builder.CreateICmpULE(Ops.RHS, WidthMinusOne); 3771 3772 if (SanitizeExponent) { 3773 Checks.push_back( 3774 std::make_pair(ValidExponent, SanitizerKind::ShiftExponent)); 3775 } 3776 3777 if (SanitizeBase) { 3778 // Check whether we are shifting any non-zero bits off the top of the 3779 // integer. We only emit this check if exponent is valid - otherwise 3780 // instructions below will have undefined behavior themselves. 3781 llvm::BasicBlock *Orig = Builder.GetInsertBlock(); 3782 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); 3783 llvm::BasicBlock *CheckShiftBase = CGF.createBasicBlock("check"); 3784 Builder.CreateCondBr(ValidExponent, CheckShiftBase, Cont); 3785 llvm::Value *PromotedWidthMinusOne = 3786 (RHS == Ops.RHS) ? WidthMinusOne 3787 : GetWidthMinusOneValue(Ops.LHS, RHS); 3788 CGF.EmitBlock(CheckShiftBase); 3789 llvm::Value *BitsShiftedOff = Builder.CreateLShr( 3790 Ops.LHS, Builder.CreateSub(PromotedWidthMinusOne, RHS, "shl.zeros", 3791 /*NUW*/ true, /*NSW*/ true), 3792 "shl.check"); 3793 if (SanitizeUnsignedBase || CGF.getLangOpts().CPlusPlus) { 3794 // In C99, we are not permitted to shift a 1 bit into the sign bit. 3795 // Under C++11's rules, shifting a 1 bit into the sign bit is 3796 // OK, but shifting a 1 bit out of it is not. (C89 and C++03 don't 3797 // define signed left shifts, so we use the C99 and C++11 rules there). 3798 // Unsigned shifts can always shift into the top bit. 3799 llvm::Value *One = llvm::ConstantInt::get(BitsShiftedOff->getType(), 1); 3800 BitsShiftedOff = Builder.CreateLShr(BitsShiftedOff, One); 3801 } 3802 llvm::Value *Zero = llvm::ConstantInt::get(BitsShiftedOff->getType(), 0); 3803 llvm::Value *ValidBase = Builder.CreateICmpEQ(BitsShiftedOff, Zero); 3804 CGF.EmitBlock(Cont); 3805 llvm::PHINode *BaseCheck = Builder.CreatePHI(ValidBase->getType(), 2); 3806 BaseCheck->addIncoming(Builder.getTrue(), Orig); 3807 BaseCheck->addIncoming(ValidBase, CheckShiftBase); 3808 Checks.push_back(std::make_pair( 3809 BaseCheck, SanitizeSignedBase ? SanitizerKind::ShiftBase 3810 : SanitizerKind::UnsignedShiftBase)); 3811 } 3812 3813 assert(!Checks.empty()); 3814 EmitBinOpCheck(Checks, Ops); 3815 } 3816 3817 return Builder.CreateShl(Ops.LHS, RHS, "shl"); 3818 } 3819 3820 Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) { 3821 // TODO: This misses out on the sanitizer check below. 3822 if (Ops.isFixedPointOp()) 3823 return EmitFixedPointBinOp(Ops); 3824 3825 // LLVM requires the LHS and RHS to be the same type: promote or truncate the 3826 // RHS to the same size as the LHS. 3827 Value *RHS = Ops.RHS; 3828 if (Ops.LHS->getType() != RHS->getType()) 3829 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom"); 3830 3831 // OpenCL 6.3j: shift values are effectively % word size of LHS. 3832 if (CGF.getLangOpts().OpenCL) 3833 RHS = ConstrainShiftValue(Ops.LHS, RHS, "shr.mask"); 3834 else if (CGF.SanOpts.has(SanitizerKind::ShiftExponent) && 3835 isa<llvm::IntegerType>(Ops.LHS->getType())) { 3836 CodeGenFunction::SanitizerScope SanScope(&CGF); 3837 llvm::Value *Valid = 3838 Builder.CreateICmpULE(RHS, GetWidthMinusOneValue(Ops.LHS, RHS)); 3839 EmitBinOpCheck(std::make_pair(Valid, SanitizerKind::ShiftExponent), Ops); 3840 } 3841 3842 if (Ops.Ty->hasUnsignedIntegerRepresentation()) 3843 return Builder.CreateLShr(Ops.LHS, RHS, "shr"); 3844 return Builder.CreateAShr(Ops.LHS, RHS, "shr"); 3845 } 3846 3847 enum IntrinsicType { VCMPEQ, VCMPGT }; 3848 // return corresponding comparison intrinsic for given vector type 3849 static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT, 3850 BuiltinType::Kind ElemKind) { 3851 switch (ElemKind) { 3852 default: llvm_unreachable("unexpected element type"); 3853 case BuiltinType::Char_U: 3854 case BuiltinType::UChar: 3855 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p : 3856 llvm::Intrinsic::ppc_altivec_vcmpgtub_p; 3857 case BuiltinType::Char_S: 3858 case BuiltinType::SChar: 3859 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p : 3860 llvm::Intrinsic::ppc_altivec_vcmpgtsb_p; 3861 case BuiltinType::UShort: 3862 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p : 3863 llvm::Intrinsic::ppc_altivec_vcmpgtuh_p; 3864 case BuiltinType::Short: 3865 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p : 3866 llvm::Intrinsic::ppc_altivec_vcmpgtsh_p; 3867 case BuiltinType::UInt: 3868 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p : 3869 llvm::Intrinsic::ppc_altivec_vcmpgtuw_p; 3870 case BuiltinType::Int: 3871 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p : 3872 llvm::Intrinsic::ppc_altivec_vcmpgtsw_p; 3873 case BuiltinType::ULong: 3874 case BuiltinType::ULongLong: 3875 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequd_p : 3876 llvm::Intrinsic::ppc_altivec_vcmpgtud_p; 3877 case BuiltinType::Long: 3878 case BuiltinType::LongLong: 3879 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequd_p : 3880 llvm::Intrinsic::ppc_altivec_vcmpgtsd_p; 3881 case BuiltinType::Float: 3882 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpeqfp_p : 3883 llvm::Intrinsic::ppc_altivec_vcmpgtfp_p; 3884 case BuiltinType::Double: 3885 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_vsx_xvcmpeqdp_p : 3886 llvm::Intrinsic::ppc_vsx_xvcmpgtdp_p; 3887 case BuiltinType::UInt128: 3888 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequq_p 3889 : llvm::Intrinsic::ppc_altivec_vcmpgtuq_p; 3890 case BuiltinType::Int128: 3891 return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequq_p 3892 : llvm::Intrinsic::ppc_altivec_vcmpgtsq_p; 3893 } 3894 } 3895 3896 Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E, 3897 llvm::CmpInst::Predicate UICmpOpc, 3898 llvm::CmpInst::Predicate SICmpOpc, 3899 llvm::CmpInst::Predicate FCmpOpc, 3900 bool IsSignaling) { 3901 TestAndClearIgnoreResultAssign(); 3902 Value *Result; 3903 QualType LHSTy = E->getLHS()->getType(); 3904 QualType RHSTy = E->getRHS()->getType(); 3905 if (const MemberPointerType *MPT = LHSTy->getAs<MemberPointerType>()) { 3906 assert(E->getOpcode() == BO_EQ || 3907 E->getOpcode() == BO_NE); 3908 Value *LHS = CGF.EmitScalarExpr(E->getLHS()); 3909 Value *RHS = CGF.EmitScalarExpr(E->getRHS()); 3910 Result = CGF.CGM.getCXXABI().EmitMemberPointerComparison( 3911 CGF, LHS, RHS, MPT, E->getOpcode() == BO_NE); 3912 } else if (!LHSTy->isAnyComplexType() && !RHSTy->isAnyComplexType()) { 3913 BinOpInfo BOInfo = EmitBinOps(E); 3914 Value *LHS = BOInfo.LHS; 3915 Value *RHS = BOInfo.RHS; 3916 3917 // If AltiVec, the comparison results in a numeric type, so we use 3918 // intrinsics comparing vectors and giving 0 or 1 as a result 3919 if (LHSTy->isVectorType() && !E->getType()->isVectorType()) { 3920 // constants for mapping CR6 register bits to predicate result 3921 enum { CR6_EQ=0, CR6_EQ_REV, CR6_LT, CR6_LT_REV } CR6; 3922 3923 llvm::Intrinsic::ID ID = llvm::Intrinsic::not_intrinsic; 3924 3925 // in several cases vector arguments order will be reversed 3926 Value *FirstVecArg = LHS, 3927 *SecondVecArg = RHS; 3928 3929 QualType ElTy = LHSTy->castAs<VectorType>()->getElementType(); 3930 BuiltinType::Kind ElementKind = ElTy->castAs<BuiltinType>()->getKind(); 3931 3932 switch(E->getOpcode()) { 3933 default: llvm_unreachable("is not a comparison operation"); 3934 case BO_EQ: 3935 CR6 = CR6_LT; 3936 ID = GetIntrinsic(VCMPEQ, ElementKind); 3937 break; 3938 case BO_NE: 3939 CR6 = CR6_EQ; 3940 ID = GetIntrinsic(VCMPEQ, ElementKind); 3941 break; 3942 case BO_LT: 3943 CR6 = CR6_LT; 3944 ID = GetIntrinsic(VCMPGT, ElementKind); 3945 std::swap(FirstVecArg, SecondVecArg); 3946 break; 3947 case BO_GT: 3948 CR6 = CR6_LT; 3949 ID = GetIntrinsic(VCMPGT, ElementKind); 3950 break; 3951 case BO_LE: 3952 if (ElementKind == BuiltinType::Float) { 3953 CR6 = CR6_LT; 3954 ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p; 3955 std::swap(FirstVecArg, SecondVecArg); 3956 } 3957 else { 3958 CR6 = CR6_EQ; 3959 ID = GetIntrinsic(VCMPGT, ElementKind); 3960 } 3961 break; 3962 case BO_GE: 3963 if (ElementKind == BuiltinType::Float) { 3964 CR6 = CR6_LT; 3965 ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p; 3966 } 3967 else { 3968 CR6 = CR6_EQ; 3969 ID = GetIntrinsic(VCMPGT, ElementKind); 3970 std::swap(FirstVecArg, SecondVecArg); 3971 } 3972 break; 3973 } 3974 3975 Value *CR6Param = Builder.getInt32(CR6); 3976 llvm::Function *F = CGF.CGM.getIntrinsic(ID); 3977 Result = Builder.CreateCall(F, {CR6Param, FirstVecArg, SecondVecArg}); 3978 3979 // The result type of intrinsic may not be same as E->getType(). 3980 // If E->getType() is not BoolTy, EmitScalarConversion will do the 3981 // conversion work. If E->getType() is BoolTy, EmitScalarConversion will 3982 // do nothing, if ResultTy is not i1 at the same time, it will cause 3983 // crash later. 3984 llvm::IntegerType *ResultTy = cast<llvm::IntegerType>(Result->getType()); 3985 if (ResultTy->getBitWidth() > 1 && 3986 E->getType() == CGF.getContext().BoolTy) 3987 Result = Builder.CreateTrunc(Result, Builder.getInt1Ty()); 3988 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType(), 3989 E->getExprLoc()); 3990 } 3991 3992 if (BOInfo.isFixedPointOp()) { 3993 Result = EmitFixedPointBinOp(BOInfo); 3994 } else if (LHS->getType()->isFPOrFPVectorTy()) { 3995 CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, BOInfo.FPFeatures); 3996 if (!IsSignaling) 3997 Result = Builder.CreateFCmp(FCmpOpc, LHS, RHS, "cmp"); 3998 else 3999 Result = Builder.CreateFCmpS(FCmpOpc, LHS, RHS, "cmp"); 4000 } else if (LHSTy->hasSignedIntegerRepresentation()) { 4001 Result = Builder.CreateICmp(SICmpOpc, LHS, RHS, "cmp"); 4002 } else { 4003 // Unsigned integers and pointers. 4004 4005 if (CGF.CGM.getCodeGenOpts().StrictVTablePointers && 4006 !isa<llvm::ConstantPointerNull>(LHS) && 4007 !isa<llvm::ConstantPointerNull>(RHS)) { 4008 4009 // Dynamic information is required to be stripped for comparisons, 4010 // because it could leak the dynamic information. Based on comparisons 4011 // of pointers to dynamic objects, the optimizer can replace one pointer 4012 // with another, which might be incorrect in presence of invariant 4013 // groups. Comparison with null is safe because null does not carry any 4014 // dynamic information. 4015 if (LHSTy.mayBeDynamicClass()) 4016 LHS = Builder.CreateStripInvariantGroup(LHS); 4017 if (RHSTy.mayBeDynamicClass()) 4018 RHS = Builder.CreateStripInvariantGroup(RHS); 4019 } 4020 4021 Result = Builder.CreateICmp(UICmpOpc, LHS, RHS, "cmp"); 4022 } 4023 4024 // If this is a vector comparison, sign extend the result to the appropriate 4025 // vector integer type and return it (don't convert to bool). 4026 if (LHSTy->isVectorType()) 4027 return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext"); 4028 4029 } else { 4030 // Complex Comparison: can only be an equality comparison. 4031 CodeGenFunction::ComplexPairTy LHS, RHS; 4032 QualType CETy; 4033 if (auto *CTy = LHSTy->getAs<ComplexType>()) { 4034 LHS = CGF.EmitComplexExpr(E->getLHS()); 4035 CETy = CTy->getElementType(); 4036 } else { 4037 LHS.first = Visit(E->getLHS()); 4038 LHS.second = llvm::Constant::getNullValue(LHS.first->getType()); 4039 CETy = LHSTy; 4040 } 4041 if (auto *CTy = RHSTy->getAs<ComplexType>()) { 4042 RHS = CGF.EmitComplexExpr(E->getRHS()); 4043 assert(CGF.getContext().hasSameUnqualifiedType(CETy, 4044 CTy->getElementType()) && 4045 "The element types must always match."); 4046 (void)CTy; 4047 } else { 4048 RHS.first = Visit(E->getRHS()); 4049 RHS.second = llvm::Constant::getNullValue(RHS.first->getType()); 4050 assert(CGF.getContext().hasSameUnqualifiedType(CETy, RHSTy) && 4051 "The element types must always match."); 4052 } 4053 4054 Value *ResultR, *ResultI; 4055 if (CETy->isRealFloatingType()) { 4056 // As complex comparisons can only be equality comparisons, they 4057 // are never signaling comparisons. 4058 ResultR = Builder.CreateFCmp(FCmpOpc, LHS.first, RHS.first, "cmp.r"); 4059 ResultI = Builder.CreateFCmp(FCmpOpc, LHS.second, RHS.second, "cmp.i"); 4060 } else { 4061 // Complex comparisons can only be equality comparisons. As such, signed 4062 // and unsigned opcodes are the same. 4063 ResultR = Builder.CreateICmp(UICmpOpc, LHS.first, RHS.first, "cmp.r"); 4064 ResultI = Builder.CreateICmp(UICmpOpc, LHS.second, RHS.second, "cmp.i"); 4065 } 4066 4067 if (E->getOpcode() == BO_EQ) { 4068 Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); 4069 } else { 4070 assert(E->getOpcode() == BO_NE && 4071 "Complex comparison other than == or != ?"); 4072 Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); 4073 } 4074 } 4075 4076 return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType(), 4077 E->getExprLoc()); 4078 } 4079 4080 Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) { 4081 bool Ignore = TestAndClearIgnoreResultAssign(); 4082 4083 Value *RHS; 4084 LValue LHS; 4085 4086 switch (E->getLHS()->getType().getObjCLifetime()) { 4087 case Qualifiers::OCL_Strong: 4088 std::tie(LHS, RHS) = CGF.EmitARCStoreStrong(E, Ignore); 4089 break; 4090 4091 case Qualifiers::OCL_Autoreleasing: 4092 std::tie(LHS, RHS) = CGF.EmitARCStoreAutoreleasing(E); 4093 break; 4094 4095 case Qualifiers::OCL_ExplicitNone: 4096 std::tie(LHS, RHS) = CGF.EmitARCStoreUnsafeUnretained(E, Ignore); 4097 break; 4098 4099 case Qualifiers::OCL_Weak: 4100 RHS = Visit(E->getRHS()); 4101 LHS = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store); 4102 RHS = CGF.EmitARCStoreWeak(LHS.getAddress(CGF), RHS, Ignore); 4103 break; 4104 4105 case Qualifiers::OCL_None: 4106 // __block variables need to have the rhs evaluated first, plus 4107 // this should improve codegen just a little. 4108 RHS = Visit(E->getRHS()); 4109 LHS = EmitCheckedLValue(E->getLHS(), CodeGenFunction::TCK_Store); 4110 4111 // Store the value into the LHS. Bit-fields are handled specially 4112 // because the result is altered by the store, i.e., [C99 6.5.16p1] 4113 // 'An assignment expression has the value of the left operand after 4114 // the assignment...'. 4115 if (LHS.isBitField()) { 4116 CGF.EmitStoreThroughBitfieldLValue(RValue::get(RHS), LHS, &RHS); 4117 } else { 4118 CGF.EmitNullabilityCheck(LHS, RHS, E->getExprLoc()); 4119 CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS); 4120 } 4121 } 4122 4123 // If the result is clearly ignored, return now. 4124 if (Ignore) 4125 return nullptr; 4126 4127 // The result of an assignment in C is the assigned r-value. 4128 if (!CGF.getLangOpts().CPlusPlus) 4129 return RHS; 4130 4131 // If the lvalue is non-volatile, return the computed value of the assignment. 4132 if (!LHS.isVolatileQualified()) 4133 return RHS; 4134 4135 // Otherwise, reload the value. 4136 return EmitLoadOfLValue(LHS, E->getExprLoc()); 4137 } 4138 4139 Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) { 4140 // Perform vector logical and on comparisons with zero vectors. 4141 if (E->getType()->isVectorType()) { 4142 CGF.incrementProfileCounter(E); 4143 4144 Value *LHS = Visit(E->getLHS()); 4145 Value *RHS = Visit(E->getRHS()); 4146 Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType()); 4147 if (LHS->getType()->isFPOrFPVectorTy()) { 4148 CodeGenFunction::CGFPOptionsRAII FPOptsRAII( 4149 CGF, E->getFPFeaturesInEffect(CGF.getLangOpts())); 4150 LHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero, "cmp"); 4151 RHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero, "cmp"); 4152 } else { 4153 LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp"); 4154 RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp"); 4155 } 4156 Value *And = Builder.CreateAnd(LHS, RHS); 4157 return Builder.CreateSExt(And, ConvertType(E->getType()), "sext"); 4158 } 4159 4160 llvm::Type *ResTy = ConvertType(E->getType()); 4161 4162 // If we have 0 && RHS, see if we can elide RHS, if so, just return 0. 4163 // If we have 1 && X, just emit X without inserting the control flow. 4164 bool LHSCondVal; 4165 if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) { 4166 if (LHSCondVal) { // If we have 1 && X, just emit X. 4167 CGF.incrementProfileCounter(E); 4168 4169 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 4170 // ZExt result to int or bool. 4171 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext"); 4172 } 4173 4174 // 0 && RHS: If it is safe, just elide the RHS, and return 0/false. 4175 if (!CGF.ContainsLabel(E->getRHS())) 4176 return llvm::Constant::getNullValue(ResTy); 4177 } 4178 4179 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end"); 4180 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("land.rhs"); 4181 4182 CodeGenFunction::ConditionalEvaluation eval(CGF); 4183 4184 // Branch on the LHS first. If it is false, go to the failure (cont) block. 4185 CGF.EmitBranchOnBoolExpr(E->getLHS(), RHSBlock, ContBlock, 4186 CGF.getProfileCount(E->getRHS())); 4187 4188 // Any edges into the ContBlock are now from an (indeterminate number of) 4189 // edges from this first condition. All of these values will be false. Start 4190 // setting up the PHI node in the Cont Block for this. 4191 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2, 4192 "", ContBlock); 4193 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); 4194 PI != PE; ++PI) 4195 PN->addIncoming(llvm::ConstantInt::getFalse(VMContext), *PI); 4196 4197 eval.begin(CGF); 4198 CGF.EmitBlock(RHSBlock); 4199 CGF.incrementProfileCounter(E); 4200 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 4201 eval.end(CGF); 4202 4203 // Reaquire the RHS block, as there may be subblocks inserted. 4204 RHSBlock = Builder.GetInsertBlock(); 4205 4206 // Emit an unconditional branch from this block to ContBlock. 4207 { 4208 // There is no need to emit line number for unconditional branch. 4209 auto NL = ApplyDebugLocation::CreateEmpty(CGF); 4210 CGF.EmitBlock(ContBlock); 4211 } 4212 // Insert an entry into the phi node for the edge with the value of RHSCond. 4213 PN->addIncoming(RHSCond, RHSBlock); 4214 4215 // Artificial location to preserve the scope information 4216 { 4217 auto NL = ApplyDebugLocation::CreateArtificial(CGF); 4218 PN->setDebugLoc(Builder.getCurrentDebugLocation()); 4219 } 4220 4221 // ZExt result to int. 4222 return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext"); 4223 } 4224 4225 Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) { 4226 // Perform vector logical or on comparisons with zero vectors. 4227 if (E->getType()->isVectorType()) { 4228 CGF.incrementProfileCounter(E); 4229 4230 Value *LHS = Visit(E->getLHS()); 4231 Value *RHS = Visit(E->getRHS()); 4232 Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType()); 4233 if (LHS->getType()->isFPOrFPVectorTy()) { 4234 CodeGenFunction::CGFPOptionsRAII FPOptsRAII( 4235 CGF, E->getFPFeaturesInEffect(CGF.getLangOpts())); 4236 LHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, LHS, Zero, "cmp"); 4237 RHS = Builder.CreateFCmp(llvm::CmpInst::FCMP_UNE, RHS, Zero, "cmp"); 4238 } else { 4239 LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp"); 4240 RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp"); 4241 } 4242 Value *Or = Builder.CreateOr(LHS, RHS); 4243 return Builder.CreateSExt(Or, ConvertType(E->getType()), "sext"); 4244 } 4245 4246 llvm::Type *ResTy = ConvertType(E->getType()); 4247 4248 // If we have 1 || RHS, see if we can elide RHS, if so, just return 1. 4249 // If we have 0 || X, just emit X without inserting the control flow. 4250 bool LHSCondVal; 4251 if (CGF.ConstantFoldsToSimpleInteger(E->getLHS(), LHSCondVal)) { 4252 if (!LHSCondVal) { // If we have 0 || X, just emit X. 4253 CGF.incrementProfileCounter(E); 4254 4255 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 4256 // ZExt result to int or bool. 4257 return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext"); 4258 } 4259 4260 // 1 || RHS: If it is safe, just elide the RHS, and return 1/true. 4261 if (!CGF.ContainsLabel(E->getRHS())) 4262 return llvm::ConstantInt::get(ResTy, 1); 4263 } 4264 4265 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end"); 4266 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("lor.rhs"); 4267 4268 CodeGenFunction::ConditionalEvaluation eval(CGF); 4269 4270 // Branch on the LHS first. If it is true, go to the success (cont) block. 4271 CGF.EmitBranchOnBoolExpr(E->getLHS(), ContBlock, RHSBlock, 4272 CGF.getCurrentProfileCount() - 4273 CGF.getProfileCount(E->getRHS())); 4274 4275 // Any edges into the ContBlock are now from an (indeterminate number of) 4276 // edges from this first condition. All of these values will be true. Start 4277 // setting up the PHI node in the Cont Block for this. 4278 llvm::PHINode *PN = llvm::PHINode::Create(llvm::Type::getInt1Ty(VMContext), 2, 4279 "", ContBlock); 4280 for (llvm::pred_iterator PI = pred_begin(ContBlock), PE = pred_end(ContBlock); 4281 PI != PE; ++PI) 4282 PN->addIncoming(llvm::ConstantInt::getTrue(VMContext), *PI); 4283 4284 eval.begin(CGF); 4285 4286 // Emit the RHS condition as a bool value. 4287 CGF.EmitBlock(RHSBlock); 4288 CGF.incrementProfileCounter(E); 4289 Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS()); 4290 4291 eval.end(CGF); 4292 4293 // Reaquire the RHS block, as there may be subblocks inserted. 4294 RHSBlock = Builder.GetInsertBlock(); 4295 4296 // Emit an unconditional branch from this block to ContBlock. Insert an entry 4297 // into the phi node for the edge with the value of RHSCond. 4298 CGF.EmitBlock(ContBlock); 4299 PN->addIncoming(RHSCond, RHSBlock); 4300 4301 // ZExt result to int. 4302 return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext"); 4303 } 4304 4305 Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) { 4306 CGF.EmitIgnoredExpr(E->getLHS()); 4307 CGF.EnsureInsertPoint(); 4308 return Visit(E->getRHS()); 4309 } 4310 4311 //===----------------------------------------------------------------------===// 4312 // Other Operators 4313 //===----------------------------------------------------------------------===// 4314 4315 /// isCheapEnoughToEvaluateUnconditionally - Return true if the specified 4316 /// expression is cheap enough and side-effect-free enough to evaluate 4317 /// unconditionally instead of conditionally. This is used to convert control 4318 /// flow into selects in some cases. 4319 static bool isCheapEnoughToEvaluateUnconditionally(const Expr *E, 4320 CodeGenFunction &CGF) { 4321 // Anything that is an integer or floating point constant is fine. 4322 return E->IgnoreParens()->isEvaluatable(CGF.getContext()); 4323 4324 // Even non-volatile automatic variables can't be evaluated unconditionally. 4325 // Referencing a thread_local may cause non-trivial initialization work to 4326 // occur. If we're inside a lambda and one of the variables is from the scope 4327 // outside the lambda, that function may have returned already. Reading its 4328 // locals is a bad idea. Also, these reads may introduce races there didn't 4329 // exist in the source-level program. 4330 } 4331 4332 4333 Value *ScalarExprEmitter:: 4334 VisitAbstractConditionalOperator(const AbstractConditionalOperator *E) { 4335 TestAndClearIgnoreResultAssign(); 4336 4337 // Bind the common expression if necessary. 4338 CodeGenFunction::OpaqueValueMapping binding(CGF, E); 4339 4340 Expr *condExpr = E->getCond(); 4341 Expr *lhsExpr = E->getTrueExpr(); 4342 Expr *rhsExpr = E->getFalseExpr(); 4343 4344 // If the condition constant folds and can be elided, try to avoid emitting 4345 // the condition and the dead arm. 4346 bool CondExprBool; 4347 if (CGF.ConstantFoldsToSimpleInteger(condExpr, CondExprBool)) { 4348 Expr *live = lhsExpr, *dead = rhsExpr; 4349 if (!CondExprBool) std::swap(live, dead); 4350 4351 // If the dead side doesn't have labels we need, just emit the Live part. 4352 if (!CGF.ContainsLabel(dead)) { 4353 if (CondExprBool) 4354 CGF.incrementProfileCounter(E); 4355 Value *Result = Visit(live); 4356 4357 // If the live part is a throw expression, it acts like it has a void 4358 // type, so evaluating it returns a null Value*. However, a conditional 4359 // with non-void type must return a non-null Value*. 4360 if (!Result && !E->getType()->isVoidType()) 4361 Result = llvm::UndefValue::get(CGF.ConvertType(E->getType())); 4362 4363 return Result; 4364 } 4365 } 4366 4367 // OpenCL: If the condition is a vector, we can treat this condition like 4368 // the select function. 4369 if ((CGF.getLangOpts().OpenCL && condExpr->getType()->isVectorType()) || 4370 condExpr->getType()->isExtVectorType()) { 4371 CGF.incrementProfileCounter(E); 4372 4373 llvm::Value *CondV = CGF.EmitScalarExpr(condExpr); 4374 llvm::Value *LHS = Visit(lhsExpr); 4375 llvm::Value *RHS = Visit(rhsExpr); 4376 4377 llvm::Type *condType = ConvertType(condExpr->getType()); 4378 auto *vecTy = cast<llvm::FixedVectorType>(condType); 4379 4380 unsigned numElem = vecTy->getNumElements(); 4381 llvm::Type *elemType = vecTy->getElementType(); 4382 4383 llvm::Value *zeroVec = llvm::Constant::getNullValue(vecTy); 4384 llvm::Value *TestMSB = Builder.CreateICmpSLT(CondV, zeroVec); 4385 llvm::Value *tmp = Builder.CreateSExt( 4386 TestMSB, llvm::FixedVectorType::get(elemType, numElem), "sext"); 4387 llvm::Value *tmp2 = Builder.CreateNot(tmp); 4388 4389 // Cast float to int to perform ANDs if necessary. 4390 llvm::Value *RHSTmp = RHS; 4391 llvm::Value *LHSTmp = LHS; 4392 bool wasCast = false; 4393 llvm::VectorType *rhsVTy = cast<llvm::VectorType>(RHS->getType()); 4394 if (rhsVTy->getElementType()->isFloatingPointTy()) { 4395 RHSTmp = Builder.CreateBitCast(RHS, tmp2->getType()); 4396 LHSTmp = Builder.CreateBitCast(LHS, tmp->getType()); 4397 wasCast = true; 4398 } 4399 4400 llvm::Value *tmp3 = Builder.CreateAnd(RHSTmp, tmp2); 4401 llvm::Value *tmp4 = Builder.CreateAnd(LHSTmp, tmp); 4402 llvm::Value *tmp5 = Builder.CreateOr(tmp3, tmp4, "cond"); 4403 if (wasCast) 4404 tmp5 = Builder.CreateBitCast(tmp5, RHS->getType()); 4405 4406 return tmp5; 4407 } 4408 4409 if (condExpr->getType()->isVectorType()) { 4410 CGF.incrementProfileCounter(E); 4411 4412 llvm::Value *CondV = CGF.EmitScalarExpr(condExpr); 4413 llvm::Value *LHS = Visit(lhsExpr); 4414 llvm::Value *RHS = Visit(rhsExpr); 4415 4416 llvm::Type *CondType = ConvertType(condExpr->getType()); 4417 auto *VecTy = cast<llvm::VectorType>(CondType); 4418 llvm::Value *ZeroVec = llvm::Constant::getNullValue(VecTy); 4419 4420 CondV = Builder.CreateICmpNE(CondV, ZeroVec, "vector_cond"); 4421 return Builder.CreateSelect(CondV, LHS, RHS, "vector_select"); 4422 } 4423 4424 // If this is a really simple expression (like x ? 4 : 5), emit this as a 4425 // select instead of as control flow. We can only do this if it is cheap and 4426 // safe to evaluate the LHS and RHS unconditionally. 4427 if (isCheapEnoughToEvaluateUnconditionally(lhsExpr, CGF) && 4428 isCheapEnoughToEvaluateUnconditionally(rhsExpr, CGF)) { 4429 llvm::Value *CondV = CGF.EvaluateExprAsBool(condExpr); 4430 llvm::Value *StepV = Builder.CreateZExtOrBitCast(CondV, CGF.Int64Ty); 4431 4432 CGF.incrementProfileCounter(E, StepV); 4433 4434 llvm::Value *LHS = Visit(lhsExpr); 4435 llvm::Value *RHS = Visit(rhsExpr); 4436 if (!LHS) { 4437 // If the conditional has void type, make sure we return a null Value*. 4438 assert(!RHS && "LHS and RHS types must match"); 4439 return nullptr; 4440 } 4441 return Builder.CreateSelect(CondV, LHS, RHS, "cond"); 4442 } 4443 4444 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true"); 4445 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false"); 4446 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end"); 4447 4448 CodeGenFunction::ConditionalEvaluation eval(CGF); 4449 CGF.EmitBranchOnBoolExpr(condExpr, LHSBlock, RHSBlock, 4450 CGF.getProfileCount(lhsExpr)); 4451 4452 CGF.EmitBlock(LHSBlock); 4453 CGF.incrementProfileCounter(E); 4454 eval.begin(CGF); 4455 Value *LHS = Visit(lhsExpr); 4456 eval.end(CGF); 4457 4458 LHSBlock = Builder.GetInsertBlock(); 4459 Builder.CreateBr(ContBlock); 4460 4461 CGF.EmitBlock(RHSBlock); 4462 eval.begin(CGF); 4463 Value *RHS = Visit(rhsExpr); 4464 eval.end(CGF); 4465 4466 RHSBlock = Builder.GetInsertBlock(); 4467 CGF.EmitBlock(ContBlock); 4468 4469 // If the LHS or RHS is a throw expression, it will be legitimately null. 4470 if (!LHS) 4471 return RHS; 4472 if (!RHS) 4473 return LHS; 4474 4475 // Create a PHI node for the real part. 4476 llvm::PHINode *PN = Builder.CreatePHI(LHS->getType(), 2, "cond"); 4477 PN->addIncoming(LHS, LHSBlock); 4478 PN->addIncoming(RHS, RHSBlock); 4479 return PN; 4480 } 4481 4482 Value *ScalarExprEmitter::VisitChooseExpr(ChooseExpr *E) { 4483 return Visit(E->getChosenSubExpr()); 4484 } 4485 4486 Value *ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) { 4487 QualType Ty = VE->getType(); 4488 4489 if (Ty->isVariablyModifiedType()) 4490 CGF.EmitVariablyModifiedType(Ty); 4491 4492 Address ArgValue = Address::invalid(); 4493 Address ArgPtr = CGF.EmitVAArg(VE, ArgValue); 4494 4495 llvm::Type *ArgTy = ConvertType(VE->getType()); 4496 4497 // If EmitVAArg fails, emit an error. 4498 if (!ArgPtr.isValid()) { 4499 CGF.ErrorUnsupported(VE, "va_arg expression"); 4500 return llvm::UndefValue::get(ArgTy); 4501 } 4502 4503 // FIXME Volatility. 4504 llvm::Value *Val = Builder.CreateLoad(ArgPtr); 4505 4506 // If EmitVAArg promoted the type, we must truncate it. 4507 if (ArgTy != Val->getType()) { 4508 if (ArgTy->isPointerTy() && !Val->getType()->isPointerTy()) 4509 Val = Builder.CreateIntToPtr(Val, ArgTy); 4510 else 4511 Val = Builder.CreateTrunc(Val, ArgTy); 4512 } 4513 4514 return Val; 4515 } 4516 4517 Value *ScalarExprEmitter::VisitBlockExpr(const BlockExpr *block) { 4518 return CGF.EmitBlockLiteral(block); 4519 } 4520 4521 // Convert a vec3 to vec4, or vice versa. 4522 static Value *ConvertVec3AndVec4(CGBuilderTy &Builder, CodeGenFunction &CGF, 4523 Value *Src, unsigned NumElementsDst) { 4524 llvm::Value *UnV = llvm::UndefValue::get(Src->getType()); 4525 static constexpr int Mask[] = {0, 1, 2, -1}; 4526 return Builder.CreateShuffleVector(Src, UnV, 4527 llvm::makeArrayRef(Mask, NumElementsDst)); 4528 } 4529 4530 // Create cast instructions for converting LLVM value \p Src to LLVM type \p 4531 // DstTy. \p Src has the same size as \p DstTy. Both are single value types 4532 // but could be scalar or vectors of different lengths, and either can be 4533 // pointer. 4534 // There are 4 cases: 4535 // 1. non-pointer -> non-pointer : needs 1 bitcast 4536 // 2. pointer -> pointer : needs 1 bitcast or addrspacecast 4537 // 3. pointer -> non-pointer 4538 // a) pointer -> intptr_t : needs 1 ptrtoint 4539 // b) pointer -> non-intptr_t : needs 1 ptrtoint then 1 bitcast 4540 // 4. non-pointer -> pointer 4541 // a) intptr_t -> pointer : needs 1 inttoptr 4542 // b) non-intptr_t -> pointer : needs 1 bitcast then 1 inttoptr 4543 // Note: for cases 3b and 4b two casts are required since LLVM casts do not 4544 // allow casting directly between pointer types and non-integer non-pointer 4545 // types. 4546 static Value *createCastsForTypeOfSameSize(CGBuilderTy &Builder, 4547 const llvm::DataLayout &DL, 4548 Value *Src, llvm::Type *DstTy, 4549 StringRef Name = "") { 4550 auto SrcTy = Src->getType(); 4551 4552 // Case 1. 4553 if (!SrcTy->isPointerTy() && !DstTy->isPointerTy()) 4554 return Builder.CreateBitCast(Src, DstTy, Name); 4555 4556 // Case 2. 4557 if (SrcTy->isPointerTy() && DstTy->isPointerTy()) 4558 return Builder.CreatePointerBitCastOrAddrSpaceCast(Src, DstTy, Name); 4559 4560 // Case 3. 4561 if (SrcTy->isPointerTy() && !DstTy->isPointerTy()) { 4562 // Case 3b. 4563 if (!DstTy->isIntegerTy()) 4564 Src = Builder.CreatePtrToInt(Src, DL.getIntPtrType(SrcTy)); 4565 // Cases 3a and 3b. 4566 return Builder.CreateBitOrPointerCast(Src, DstTy, Name); 4567 } 4568 4569 // Case 4b. 4570 if (!SrcTy->isIntegerTy()) 4571 Src = Builder.CreateBitCast(Src, DL.getIntPtrType(DstTy)); 4572 // Cases 4a and 4b. 4573 return Builder.CreateIntToPtr(Src, DstTy, Name); 4574 } 4575 4576 Value *ScalarExprEmitter::VisitAsTypeExpr(AsTypeExpr *E) { 4577 Value *Src = CGF.EmitScalarExpr(E->getSrcExpr()); 4578 llvm::Type *DstTy = ConvertType(E->getType()); 4579 4580 llvm::Type *SrcTy = Src->getType(); 4581 unsigned NumElementsSrc = 4582 isa<llvm::VectorType>(SrcTy) 4583 ? cast<llvm::FixedVectorType>(SrcTy)->getNumElements() 4584 : 0; 4585 unsigned NumElementsDst = 4586 isa<llvm::VectorType>(DstTy) 4587 ? cast<llvm::FixedVectorType>(DstTy)->getNumElements() 4588 : 0; 4589 4590 // Going from vec3 to non-vec3 is a special case and requires a shuffle 4591 // vector to get a vec4, then a bitcast if the target type is different. 4592 if (NumElementsSrc == 3 && NumElementsDst != 3) { 4593 Src = ConvertVec3AndVec4(Builder, CGF, Src, 4); 4594 4595 if (!CGF.CGM.getCodeGenOpts().PreserveVec3Type) { 4596 Src = createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(), Src, 4597 DstTy); 4598 } 4599 4600 Src->setName("astype"); 4601 return Src; 4602 } 4603 4604 // Going from non-vec3 to vec3 is a special case and requires a bitcast 4605 // to vec4 if the original type is not vec4, then a shuffle vector to 4606 // get a vec3. 4607 if (NumElementsSrc != 3 && NumElementsDst == 3) { 4608 if (!CGF.CGM.getCodeGenOpts().PreserveVec3Type) { 4609 auto *Vec4Ty = llvm::FixedVectorType::get( 4610 cast<llvm::VectorType>(DstTy)->getElementType(), 4); 4611 Src = createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(), Src, 4612 Vec4Ty); 4613 } 4614 4615 Src = ConvertVec3AndVec4(Builder, CGF, Src, 3); 4616 Src->setName("astype"); 4617 return Src; 4618 } 4619 4620 return createCastsForTypeOfSameSize(Builder, CGF.CGM.getDataLayout(), 4621 Src, DstTy, "astype"); 4622 } 4623 4624 Value *ScalarExprEmitter::VisitAtomicExpr(AtomicExpr *E) { 4625 return CGF.EmitAtomicExpr(E).getScalarVal(); 4626 } 4627 4628 //===----------------------------------------------------------------------===// 4629 // Entry Point into this File 4630 //===----------------------------------------------------------------------===// 4631 4632 /// Emit the computation of the specified expression of scalar type, ignoring 4633 /// the result. 4634 Value *CodeGenFunction::EmitScalarExpr(const Expr *E, bool IgnoreResultAssign) { 4635 assert(E && hasScalarEvaluationKind(E->getType()) && 4636 "Invalid scalar expression to emit"); 4637 4638 return ScalarExprEmitter(*this, IgnoreResultAssign) 4639 .Visit(const_cast<Expr *>(E)); 4640 } 4641 4642 /// Emit a conversion from the specified type to the specified destination type, 4643 /// both of which are LLVM scalar types. 4644 Value *CodeGenFunction::EmitScalarConversion(Value *Src, QualType SrcTy, 4645 QualType DstTy, 4646 SourceLocation Loc) { 4647 assert(hasScalarEvaluationKind(SrcTy) && hasScalarEvaluationKind(DstTy) && 4648 "Invalid scalar expression to emit"); 4649 return ScalarExprEmitter(*this).EmitScalarConversion(Src, SrcTy, DstTy, Loc); 4650 } 4651 4652 /// Emit a conversion from the specified complex type to the specified 4653 /// destination type, where the destination type is an LLVM scalar type. 4654 Value *CodeGenFunction::EmitComplexToScalarConversion(ComplexPairTy Src, 4655 QualType SrcTy, 4656 QualType DstTy, 4657 SourceLocation Loc) { 4658 assert(SrcTy->isAnyComplexType() && hasScalarEvaluationKind(DstTy) && 4659 "Invalid complex -> scalar conversion"); 4660 return ScalarExprEmitter(*this) 4661 .EmitComplexToScalarConversion(Src, SrcTy, DstTy, Loc); 4662 } 4663 4664 4665 llvm::Value *CodeGenFunction:: 4666 EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV, 4667 bool isInc, bool isPre) { 4668 return ScalarExprEmitter(*this).EmitScalarPrePostIncDec(E, LV, isInc, isPre); 4669 } 4670 4671 LValue CodeGenFunction::EmitObjCIsaExpr(const ObjCIsaExpr *E) { 4672 // object->isa or (*object).isa 4673 // Generate code as for: *(Class*)object 4674 4675 Expr *BaseExpr = E->getBase(); 4676 Address Addr = Address::invalid(); 4677 if (BaseExpr->isRValue()) { 4678 Addr = Address(EmitScalarExpr(BaseExpr), getPointerAlign()); 4679 } else { 4680 Addr = EmitLValue(BaseExpr).getAddress(*this); 4681 } 4682 4683 // Cast the address to Class*. 4684 Addr = Builder.CreateElementBitCast(Addr, ConvertType(E->getType())); 4685 return MakeAddrLValue(Addr, E->getType()); 4686 } 4687 4688 4689 LValue CodeGenFunction::EmitCompoundAssignmentLValue( 4690 const CompoundAssignOperator *E) { 4691 ScalarExprEmitter Scalar(*this); 4692 Value *Result = nullptr; 4693 switch (E->getOpcode()) { 4694 #define COMPOUND_OP(Op) \ 4695 case BO_##Op##Assign: \ 4696 return Scalar.EmitCompoundAssignLValue(E, &ScalarExprEmitter::Emit##Op, \ 4697 Result) 4698 COMPOUND_OP(Mul); 4699 COMPOUND_OP(Div); 4700 COMPOUND_OP(Rem); 4701 COMPOUND_OP(Add); 4702 COMPOUND_OP(Sub); 4703 COMPOUND_OP(Shl); 4704 COMPOUND_OP(Shr); 4705 COMPOUND_OP(And); 4706 COMPOUND_OP(Xor); 4707 COMPOUND_OP(Or); 4708 #undef COMPOUND_OP 4709 4710 case BO_PtrMemD: 4711 case BO_PtrMemI: 4712 case BO_Mul: 4713 case BO_Div: 4714 case BO_Rem: 4715 case BO_Add: 4716 case BO_Sub: 4717 case BO_Shl: 4718 case BO_Shr: 4719 case BO_LT: 4720 case BO_GT: 4721 case BO_LE: 4722 case BO_GE: 4723 case BO_EQ: 4724 case BO_NE: 4725 case BO_Cmp: 4726 case BO_And: 4727 case BO_Xor: 4728 case BO_Or: 4729 case BO_LAnd: 4730 case BO_LOr: 4731 case BO_Assign: 4732 case BO_Comma: 4733 llvm_unreachable("Not valid compound assignment operators"); 4734 } 4735 4736 llvm_unreachable("Unhandled compound assignment operator"); 4737 } 4738 4739 struct GEPOffsetAndOverflow { 4740 // The total (signed) byte offset for the GEP. 4741 llvm::Value *TotalOffset; 4742 // The offset overflow flag - true if the total offset overflows. 4743 llvm::Value *OffsetOverflows; 4744 }; 4745 4746 /// Evaluate given GEPVal, which is either an inbounds GEP, or a constant, 4747 /// and compute the total offset it applies from it's base pointer BasePtr. 4748 /// Returns offset in bytes and a boolean flag whether an overflow happened 4749 /// during evaluation. 4750 static GEPOffsetAndOverflow EmitGEPOffsetInBytes(Value *BasePtr, Value *GEPVal, 4751 llvm::LLVMContext &VMContext, 4752 CodeGenModule &CGM, 4753 CGBuilderTy &Builder) { 4754 const auto &DL = CGM.getDataLayout(); 4755 4756 // The total (signed) byte offset for the GEP. 4757 llvm::Value *TotalOffset = nullptr; 4758 4759 // Was the GEP already reduced to a constant? 4760 if (isa<llvm::Constant>(GEPVal)) { 4761 // Compute the offset by casting both pointers to integers and subtracting: 4762 // GEPVal = BasePtr + ptr(Offset) <--> Offset = int(GEPVal) - int(BasePtr) 4763 Value *BasePtr_int = 4764 Builder.CreatePtrToInt(BasePtr, DL.getIntPtrType(BasePtr->getType())); 4765 Value *GEPVal_int = 4766 Builder.CreatePtrToInt(GEPVal, DL.getIntPtrType(GEPVal->getType())); 4767 TotalOffset = Builder.CreateSub(GEPVal_int, BasePtr_int); 4768 return {TotalOffset, /*OffsetOverflows=*/Builder.getFalse()}; 4769 } 4770 4771 auto *GEP = cast<llvm::GEPOperator>(GEPVal); 4772 assert(GEP->getPointerOperand() == BasePtr && 4773 "BasePtr must be the the base of the GEP."); 4774 assert(GEP->isInBounds() && "Expected inbounds GEP"); 4775 4776 auto *IntPtrTy = DL.getIntPtrType(GEP->getPointerOperandType()); 4777 4778 // Grab references to the signed add/mul overflow intrinsics for intptr_t. 4779 auto *Zero = llvm::ConstantInt::getNullValue(IntPtrTy); 4780 auto *SAddIntrinsic = 4781 CGM.getIntrinsic(llvm::Intrinsic::sadd_with_overflow, IntPtrTy); 4782 auto *SMulIntrinsic = 4783 CGM.getIntrinsic(llvm::Intrinsic::smul_with_overflow, IntPtrTy); 4784 4785 // The offset overflow flag - true if the total offset overflows. 4786 llvm::Value *OffsetOverflows = Builder.getFalse(); 4787 4788 /// Return the result of the given binary operation. 4789 auto eval = [&](BinaryOperator::Opcode Opcode, llvm::Value *LHS, 4790 llvm::Value *RHS) -> llvm::Value * { 4791 assert((Opcode == BO_Add || Opcode == BO_Mul) && "Can't eval binop"); 4792 4793 // If the operands are constants, return a constant result. 4794 if (auto *LHSCI = dyn_cast<llvm::ConstantInt>(LHS)) { 4795 if (auto *RHSCI = dyn_cast<llvm::ConstantInt>(RHS)) { 4796 llvm::APInt N; 4797 bool HasOverflow = mayHaveIntegerOverflow(LHSCI, RHSCI, Opcode, 4798 /*Signed=*/true, N); 4799 if (HasOverflow) 4800 OffsetOverflows = Builder.getTrue(); 4801 return llvm::ConstantInt::get(VMContext, N); 4802 } 4803 } 4804 4805 // Otherwise, compute the result with checked arithmetic. 4806 auto *ResultAndOverflow = Builder.CreateCall( 4807 (Opcode == BO_Add) ? SAddIntrinsic : SMulIntrinsic, {LHS, RHS}); 4808 OffsetOverflows = Builder.CreateOr( 4809 Builder.CreateExtractValue(ResultAndOverflow, 1), OffsetOverflows); 4810 return Builder.CreateExtractValue(ResultAndOverflow, 0); 4811 }; 4812 4813 // Determine the total byte offset by looking at each GEP operand. 4814 for (auto GTI = llvm::gep_type_begin(GEP), GTE = llvm::gep_type_end(GEP); 4815 GTI != GTE; ++GTI) { 4816 llvm::Value *LocalOffset; 4817 auto *Index = GTI.getOperand(); 4818 // Compute the local offset contributed by this indexing step: 4819 if (auto *STy = GTI.getStructTypeOrNull()) { 4820 // For struct indexing, the local offset is the byte position of the 4821 // specified field. 4822 unsigned FieldNo = cast<llvm::ConstantInt>(Index)->getZExtValue(); 4823 LocalOffset = llvm::ConstantInt::get( 4824 IntPtrTy, DL.getStructLayout(STy)->getElementOffset(FieldNo)); 4825 } else { 4826 // Otherwise this is array-like indexing. The local offset is the index 4827 // multiplied by the element size. 4828 auto *ElementSize = llvm::ConstantInt::get( 4829 IntPtrTy, DL.getTypeAllocSize(GTI.getIndexedType())); 4830 auto *IndexS = Builder.CreateIntCast(Index, IntPtrTy, /*isSigned=*/true); 4831 LocalOffset = eval(BO_Mul, ElementSize, IndexS); 4832 } 4833 4834 // If this is the first offset, set it as the total offset. Otherwise, add 4835 // the local offset into the running total. 4836 if (!TotalOffset || TotalOffset == Zero) 4837 TotalOffset = LocalOffset; 4838 else 4839 TotalOffset = eval(BO_Add, TotalOffset, LocalOffset); 4840 } 4841 4842 return {TotalOffset, OffsetOverflows}; 4843 } 4844 4845 Value * 4846 CodeGenFunction::EmitCheckedInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList, 4847 bool SignedIndices, bool IsSubtraction, 4848 SourceLocation Loc, const Twine &Name) { 4849 Value *GEPVal = Builder.CreateInBoundsGEP(Ptr, IdxList, Name); 4850 4851 // If the pointer overflow sanitizer isn't enabled, do nothing. 4852 if (!SanOpts.has(SanitizerKind::PointerOverflow)) 4853 return GEPVal; 4854 4855 llvm::Type *PtrTy = Ptr->getType(); 4856 4857 // Perform nullptr-and-offset check unless the nullptr is defined. 4858 bool PerformNullCheck = !NullPointerIsDefined( 4859 Builder.GetInsertBlock()->getParent(), PtrTy->getPointerAddressSpace()); 4860 // Check for overflows unless the GEP got constant-folded, 4861 // and only in the default address space 4862 bool PerformOverflowCheck = 4863 !isa<llvm::Constant>(GEPVal) && PtrTy->getPointerAddressSpace() == 0; 4864 4865 if (!(PerformNullCheck || PerformOverflowCheck)) 4866 return GEPVal; 4867 4868 const auto &DL = CGM.getDataLayout(); 4869 4870 SanitizerScope SanScope(this); 4871 llvm::Type *IntPtrTy = DL.getIntPtrType(PtrTy); 4872 4873 GEPOffsetAndOverflow EvaluatedGEP = 4874 EmitGEPOffsetInBytes(Ptr, GEPVal, getLLVMContext(), CGM, Builder); 4875 4876 assert((!isa<llvm::Constant>(EvaluatedGEP.TotalOffset) || 4877 EvaluatedGEP.OffsetOverflows == Builder.getFalse()) && 4878 "If the offset got constant-folded, we don't expect that there was an " 4879 "overflow."); 4880 4881 auto *Zero = llvm::ConstantInt::getNullValue(IntPtrTy); 4882 4883 // Common case: if the total offset is zero, and we are using C++ semantics, 4884 // where nullptr+0 is defined, don't emit a check. 4885 if (EvaluatedGEP.TotalOffset == Zero && CGM.getLangOpts().CPlusPlus) 4886 return GEPVal; 4887 4888 // Now that we've computed the total offset, add it to the base pointer (with 4889 // wrapping semantics). 4890 auto *IntPtr = Builder.CreatePtrToInt(Ptr, IntPtrTy); 4891 auto *ComputedGEP = Builder.CreateAdd(IntPtr, EvaluatedGEP.TotalOffset); 4892 4893 llvm::SmallVector<std::pair<llvm::Value *, SanitizerMask>, 2> Checks; 4894 4895 if (PerformNullCheck) { 4896 // In C++, if the base pointer evaluates to a null pointer value, 4897 // the only valid pointer this inbounds GEP can produce is also 4898 // a null pointer, so the offset must also evaluate to zero. 4899 // Likewise, if we have non-zero base pointer, we can not get null pointer 4900 // as a result, so the offset can not be -intptr_t(BasePtr). 4901 // In other words, both pointers are either null, or both are non-null, 4902 // or the behaviour is undefined. 4903 // 4904 // C, however, is more strict in this regard, and gives more 4905 // optimization opportunities: in C, additionally, nullptr+0 is undefined. 4906 // So both the input to the 'gep inbounds' AND the output must not be null. 4907 auto *BaseIsNotNullptr = Builder.CreateIsNotNull(Ptr); 4908 auto *ResultIsNotNullptr = Builder.CreateIsNotNull(ComputedGEP); 4909 auto *Valid = 4910 CGM.getLangOpts().CPlusPlus 4911 ? Builder.CreateICmpEQ(BaseIsNotNullptr, ResultIsNotNullptr) 4912 : Builder.CreateAnd(BaseIsNotNullptr, ResultIsNotNullptr); 4913 Checks.emplace_back(Valid, SanitizerKind::PointerOverflow); 4914 } 4915 4916 if (PerformOverflowCheck) { 4917 // The GEP is valid if: 4918 // 1) The total offset doesn't overflow, and 4919 // 2) The sign of the difference between the computed address and the base 4920 // pointer matches the sign of the total offset. 4921 llvm::Value *ValidGEP; 4922 auto *NoOffsetOverflow = Builder.CreateNot(EvaluatedGEP.OffsetOverflows); 4923 if (SignedIndices) { 4924 // GEP is computed as `unsigned base + signed offset`, therefore: 4925 // * If offset was positive, then the computed pointer can not be 4926 // [unsigned] less than the base pointer, unless it overflowed. 4927 // * If offset was negative, then the computed pointer can not be 4928 // [unsigned] greater than the bas pointere, unless it overflowed. 4929 auto *PosOrZeroValid = Builder.CreateICmpUGE(ComputedGEP, IntPtr); 4930 auto *PosOrZeroOffset = 4931 Builder.CreateICmpSGE(EvaluatedGEP.TotalOffset, Zero); 4932 llvm::Value *NegValid = Builder.CreateICmpULT(ComputedGEP, IntPtr); 4933 ValidGEP = 4934 Builder.CreateSelect(PosOrZeroOffset, PosOrZeroValid, NegValid); 4935 } else if (!IsSubtraction) { 4936 // GEP is computed as `unsigned base + unsigned offset`, therefore the 4937 // computed pointer can not be [unsigned] less than base pointer, 4938 // unless there was an overflow. 4939 // Equivalent to `@llvm.uadd.with.overflow(%base, %offset)`. 4940 ValidGEP = Builder.CreateICmpUGE(ComputedGEP, IntPtr); 4941 } else { 4942 // GEP is computed as `unsigned base - unsigned offset`, therefore the 4943 // computed pointer can not be [unsigned] greater than base pointer, 4944 // unless there was an overflow. 4945 // Equivalent to `@llvm.usub.with.overflow(%base, sub(0, %offset))`. 4946 ValidGEP = Builder.CreateICmpULE(ComputedGEP, IntPtr); 4947 } 4948 ValidGEP = Builder.CreateAnd(ValidGEP, NoOffsetOverflow); 4949 Checks.emplace_back(ValidGEP, SanitizerKind::PointerOverflow); 4950 } 4951 4952 assert(!Checks.empty() && "Should have produced some checks."); 4953 4954 llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation(Loc)}; 4955 // Pass the computed GEP to the runtime to avoid emitting poisoned arguments. 4956 llvm::Value *DynamicArgs[] = {IntPtr, ComputedGEP}; 4957 EmitCheck(Checks, SanitizerHandler::PointerOverflow, StaticArgs, DynamicArgs); 4958 4959 return GEPVal; 4960 } 4961